Skip to content

Commit

Permalink
warning message
Browse files Browse the repository at this point in the history
  • Loading branch information
ramarty committed Dec 1, 2023
1 parent 196ee6f commit e0353be
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
15 changes: 8 additions & 7 deletions R/blackmarbler.R
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,20 @@ file_to_raster <- function(f,
str_replace_all("_Std", "")

qf_name <- paste0(variable_short, "_Quality")

if(qf_name %in% var_names){

#qf <- h5_data$HDFEOS$GRIDS$VIIRS_Grid_DNB_2d$`Data Fields`[[paste0(variable, "_Quality")]]
qf <- h5_data[[paste0("HDFEOS/GRIDS/VIIRS_Grid_DNB_2d/Data Fields/", variable, "_Quality")]][,]
qf <- h5_data[[paste0("HDFEOS/GRIDS/VIIRS_Grid_DNB_2d/Data Fields/", qf_name)]][,]

for(val in quality_flag_rm){ # out[qf %in% quality_flag_rm] doesn't work, so loop
out[qf == val] <- NA
}

}

}

if(class(out[1,1])[1] != "numeric"){
out <- matrix(as.numeric(out), # Convert to numeric matrix
ncol = ncol(out))
Expand Down Expand Up @@ -544,7 +546,7 @@ bm_extract <- function(roi_sf,
saveRDS(r_agg, out_path)

} else{
cat(paste0('"', out_path, '" already exists; skipping.\n'))
warning(paste0('"', out_path, '" already exists; skipping.\n'))
}

r_out <- NULL # Saving as file, so output from function should be NULL
Expand Down Expand Up @@ -743,7 +745,7 @@ bm_raster <- function(roi_sf,
writeRaster(r, out_path)

} else{
cat(paste0('"', out_path, '" already exists; skipping.\n'))
warning(paste0('"', out_path, '" already exists; skipping.\n'))
}

r_out <- NULL # Saving as tif file, so output from function should be NULL
Expand Down Expand Up @@ -863,10 +865,9 @@ bm_raster_i <- function(roi_sf,
#stop(st_intersects(bm_tiles_sf, roi_sf, sparse = F))
}
)



grid_use_sf <- bm_tiles_sf[inter>0,]

# Make Raster ----------------------------------------------------------------
tile_ids_rx <- grid_use_sf$TileID %>% paste(collapse = "|")
bm_files_df <- bm_files_df[bm_files_df$name %>% str_detect(tile_ids_rx),]
Expand Down
45 changes: 45 additions & 0 deletions readme_figures/additional_analysis_for_readme.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Additional Analysis for Readme

#### Setup
# Load packages
library(blackmarbler)
library(geodata)
library(sf)
library(raster)
library(ggplot2)

#### Define NASA bearer token
bearer <- "BEARER-TOKEN-HERE"
bearer <- read.csv("~/Desktop/bearer_bm.csv")$token

### ROI
# Define region of interest (roi). The roi must be (1) an sf polygon and (2)
# in the WGS84 (epsg:4326) coordinate reference system. Here, we use the
# getData function to load a polygon of Ghana
roi_sf <- gadm(country = "NER", level=0, path = tempdir()) |> st_as_sf()

### Daily data: raster for February 5, 2021
r <- bm_raster(roi_sf = roi_sf,
product_id = "VNP46A4",
date = "2021",
bearer = bearer,
variable = "NearNadir_Composite_Snow_Free")

r_cf <- bm_raster(roi_sf = roi_sf,
product_id = "VNP46A2",
date = "2023-07-23",
bearer = bearer,
variable = "Gap_Filled_DNB_BRDF-Corrected_NTL")
r_cf[] <- as.numeric(r_cf[] == 0)
plot(r_cf)


r <- bm_raster(roi_sf = roi_sf,
product_id = "VNP46A2",
date = "2023-07-23",
bearer = bearer,
variable = "Gap_Filled_DNB_BRDF-Corrected_NTL",
quality_flag_rm = c(255))

r[] <- log(r[] + 1)
plot(r)

0 comments on commit e0353be

Please sign in to comment.