@@ -145,7 +145,7 @@ remove_fill_value <- function(x, variable){
145
145
)){
146
146
x [][x [] == 65535 ] <- NA
147
147
}
148
-
148
+
149
149
return (x )
150
150
}
151
151
@@ -297,7 +297,7 @@ file_to_raster <- function(f,
297
297
nCols <- ncol(out )
298
298
res <- nRows
299
299
nodata_val <- NA
300
- myCrs <- 4326
300
+ myCrs <- " EPSG: 4326"
301
301
302
302
# # Make Raster
303
303
@@ -309,13 +309,15 @@ file_to_raster <- function(f,
309
309
out [out == nodata_val ] <- NA
310
310
311
311
# turn the out object into a raster
312
- outr <- raster(out ,crs = myCrs )
312
+ outr <- terra :: rast(out ,
313
+ crs = myCrs ,
314
+ extent = c(xMin ,xMax ,yMin ,yMax ))
313
315
314
316
# create extents class
315
- rasExt <- raster :: extent(c(xMin ,xMax ,yMin ,yMax ))
317
+ # rasExt <- raster::extent(c(xMin,xMax,yMin,yMax))
316
318
317
319
# assign the extents to the raster
318
- extent(outr ) <- rasExt
320
+ # extent(outr) <- rasExt
319
321
320
322
# set fill values to NA
321
323
outr <- remove_fill_value(outr , variable )
@@ -457,7 +459,7 @@ download_raster <- function(file_name,
457
459
write_disk(download_path , overwrite = TRUE ),
458
460
progress())
459
461
}
460
-
462
+
461
463
462
464
if (response $ status_code != 200 ){
463
465
message(" Error in downloading data" )
@@ -637,6 +639,17 @@ bm_extract <- function(roi_sf,
637
639
# NTL Variable ---------------------------------------------------------------
638
640
variable <- define_variable(variable , product_id )
639
641
642
+ # Filename root --------------------------------------------------------------
643
+ # Define outside of lapply, as use this later to aggregate rasters
644
+ if (output_location_type == " file" ){
645
+ out_name_begin <- paste0(file_prefix ,
646
+ product_id , " _" ,
647
+ variable , " _" ,
648
+ " qflag" ,
649
+ quality_flag_rm %> % paste0(collapse = " _" ), " _" ,
650
+ aggregation_fun %> % paste0(collapse = " _" ))
651
+ }
652
+
640
653
if (interpol_na == T ){
641
654
642
655
# ### Create raster
@@ -680,8 +693,6 @@ bm_extract <- function(roi_sf,
680
693
681
694
ntl_df $ date <- NULL
682
695
r <- bind_cols(n_obs_df , ntl_df )
683
- # r <- ntl_df %>%
684
- # left_join(n_obs_df, by = "date")
685
696
686
697
# Apply through each date, extract, then append
687
698
} else {
@@ -698,7 +709,8 @@ bm_extract <- function(roi_sf,
698
709
# ### If save to file
699
710
if (output_location_type == " file" ){
700
711
701
- out_name <- paste0(file_prefix , product_id , " _" , date_name_i , " .Rds" )
712
+ out_name_end <- paste0(" _" , date_name_i , " .Rds" )
713
+ out_name <- paste0(out_name_begin , out_name_end )
702
714
out_path <- file.path(file_dir , out_name )
703
715
704
716
make_raster <- TRUE
@@ -822,6 +834,15 @@ bm_extract <- function(roi_sf,
822
834
823
835
}
824
836
837
+ # Output dataframe when output_location_type = "file" ------------------------
838
+ if (output_location_type == " file" ){
839
+ r <- file_dir %> %
840
+ list.files(full.names = T ,
841
+ pattern = paste0(" *.Rds" )) %> %
842
+ str_subset(out_name_begin ) %> %
843
+ map_df(readRDS )
844
+ }
845
+
825
846
unlink(temp_dir , recursive = T )
826
847
return (r )
827
848
}
@@ -961,6 +982,16 @@ bm_raster <- function(roi_sf,
961
982
# NTL Variable ---------------------------------------------------------------
962
983
variable <- define_variable(variable , product_id )
963
984
985
+ # Filename root --------------------------------------------------------------
986
+ # Define outside of lapply, as use this later to aggregate rasters
987
+ if (output_location_type == " file" ){
988
+ out_name_begin <- paste0(file_prefix ,
989
+ product_id , " _" ,
990
+ variable , " _" ,
991
+ " qflag" ,
992
+ quality_flag_rm %> % paste0(collapse = " _" ))
993
+ }
994
+
964
995
# Download data --------------------------------------------------------------
965
996
r_list <- lapply(date , function (date_i ){
966
997
@@ -972,7 +1003,13 @@ bm_raster <- function(roi_sf,
972
1003
973
1004
# ### If save as tif format
974
1005
if (output_location_type == " file" ){
975
- out_name <- paste0(file_prefix , product_id , " _" , date_name_i , " .tif" )
1006
+
1007
+ # # Output path
1008
+ out_name_end <- paste0(" _" ,
1009
+ date_name_i ,
1010
+ " .tif" )
1011
+ out_name <- paste0(out_name_begin , out_name_end )
1012
+
976
1013
out_path <- file.path(file_dir , out_name )
977
1014
978
1015
make_raster <- TRUE
@@ -1048,6 +1085,15 @@ bm_raster <- function(roi_sf,
1048
1085
1049
1086
unlink(temp_dir , recursive = T )
1050
1087
1088
+ # Output raster when output_location_type = "file" ---------------------------
1089
+ if (output_location_type == " file" ){
1090
+ r <- file_dir %> %
1091
+ list.files(full.names = T ,
1092
+ pattern = paste0(" *.tif" )) %> %
1093
+ str_subset(out_name_begin ) %> %
1094
+ rast()
1095
+ }
1096
+
1051
1097
return (r )
1052
1098
}
1053
1099
@@ -1089,9 +1135,6 @@ bm_raster_i <- function(roi_sf,
1089
1135
}
1090
1136
1091
1137
# Grab tile dataframe --------------------------------------------------------
1092
- # product_id <- "VNP46A4"
1093
- # date <- "2021-10-15"
1094
-
1095
1138
year <- date %> % year()
1096
1139
month <- date %> % month()
1097
1140
day <- date %> % yday()
@@ -1107,10 +1150,6 @@ bm_raster_i <- function(roi_sf,
1107
1150
bm_tiles_sf <- bm_tiles_sf [! (bm_tiles_sf $ TileID %> % str_detect(" h00" )),]
1108
1151
bm_tiles_sf <- bm_tiles_sf [! (bm_tiles_sf $ TileID %> % str_detect(" v00" )),]
1109
1152
1110
- # inter <- st_intersects(bm_tiles_sf, roi_1row_sf, sparse = F) %>% as.vector()
1111
- # inter <- st_intersects(bm_tiles_sf, roi_sf, sparse = F) %>%
1112
- # apply(1, sum)
1113
-
1114
1153
inter <- tryCatch(
1115
1154
{
1116
1155
inter <- st_intersects(bm_tiles_sf , roi_sf , sparse = F ) %> %
@@ -1121,11 +1160,10 @@ bm_raster_i <- function(roi_sf,
1121
1160
error = function (e ){
1122
1161
warning(" Issue with `roi_sf` intersecting with blackmarble tiles; try buffering by a width of 0: eg, st_buffer(roi_sf, 0)" )
1123
1162
stop(" Issue with `roi_sf` intersecting with blackmarble tiles; try buffering by a width of 0: eg, st_buffer(roi_sf, 0)" )
1124
- # stop(st_intersects(bm_tiles_sf, roi_sf, sparse = F))
1125
1163
}
1126
1164
)
1127
1165
1128
- grid_use_sf <- bm_tiles_sf [inter > 0 ,]
1166
+ grid_use_sf <- bm_tiles_sf [inter > 0 ,]
1129
1167
1130
1168
# Make Raster ----------------------------------------------------------------
1131
1169
tile_ids_rx <- grid_use_sf $ TileID %> % paste(collapse = " |" )
@@ -1150,16 +1188,11 @@ bm_raster_i <- function(roi_sf,
1150
1188
r <- r_list [[1 ]]
1151
1189
} else {
1152
1190
1153
- # # Mosaic rasters together
1154
- names(r_list ) <- NULL
1155
- r_list $ fun <- max
1156
-
1157
- r <- do.call(raster :: mosaic , r_list )
1158
-
1191
+ r <- do.call(terra :: mosaic , c(r_list , fun = " max" ))
1159
1192
}
1160
1193
1161
1194
# # Crop
1162
- r <- r %> % crop(roi_sf )
1195
+ r <- r %> % terra :: crop(roi_sf )
1163
1196
1164
1197
unlink(file.path(temp_dir , product_id ), recursive = T )
1165
1198
0 commit comments