Skip to content

Commit 2a25d2d

Browse files
committed
initial terra and output raster or dataframe even if output is file #8
1 parent 30a7090 commit 2a25d2d

File tree

3 files changed

+117
-27
lines changed

3 files changed

+117
-27
lines changed

R/blackmarbler.R

Lines changed: 59 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ remove_fill_value <- function(x, variable){
145145
)){
146146
x[][x[] == 65535] <- NA
147147
}
148-
148+
149149
return(x)
150150
}
151151

@@ -297,7 +297,7 @@ file_to_raster <- function(f,
297297
nCols <- ncol(out)
298298
res <- nRows
299299
nodata_val <- NA
300-
myCrs <- 4326
300+
myCrs <- "EPSG:4326"
301301

302302
## Make Raster
303303

@@ -309,13 +309,15 @@ file_to_raster <- function(f,
309309
out[out == nodata_val] <- NA
310310

311311
#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))
313315

314316
#create extents class
315-
rasExt <- raster::extent(c(xMin,xMax,yMin,yMax))
317+
#rasExt <- raster::extent(c(xMin,xMax,yMin,yMax))
316318

317319
#assign the extents to the raster
318-
extent(outr) <- rasExt
320+
#extent(outr) <- rasExt
319321

320322
#set fill values to NA
321323
outr <- remove_fill_value(outr, variable)
@@ -457,7 +459,7 @@ download_raster <- function(file_name,
457459
write_disk(download_path, overwrite = TRUE),
458460
progress())
459461
}
460-
462+
461463

462464
if(response$status_code != 200){
463465
message("Error in downloading data")
@@ -637,6 +639,17 @@ bm_extract <- function(roi_sf,
637639
# NTL Variable ---------------------------------------------------------------
638640
variable <- define_variable(variable, product_id)
639641

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+
640653
if(interpol_na == T){
641654

642655
#### Create raster
@@ -680,8 +693,6 @@ bm_extract <- function(roi_sf,
680693

681694
ntl_df$date <- NULL
682695
r <- bind_cols(n_obs_df, ntl_df)
683-
#r <- ntl_df %>%
684-
# left_join(n_obs_df, by = "date")
685696

686697
# Apply through each date, extract, then append
687698
} else{
@@ -698,7 +709,8 @@ bm_extract <- function(roi_sf,
698709
#### If save to file
699710
if(output_location_type == "file"){
700711

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)
702714
out_path <- file.path(file_dir, out_name)
703715

704716
make_raster <- TRUE
@@ -822,6 +834,15 @@ bm_extract <- function(roi_sf,
822834

823835
}
824836

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+
825846
unlink(temp_dir, recursive = T)
826847
return(r)
827848
}
@@ -961,6 +982,16 @@ bm_raster <- function(roi_sf,
961982
# NTL Variable ---------------------------------------------------------------
962983
variable <- define_variable(variable, product_id)
963984

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+
964995
# Download data --------------------------------------------------------------
965996
r_list <- lapply(date, function(date_i){
966997

@@ -972,7 +1003,13 @@ bm_raster <- function(roi_sf,
9721003

9731004
#### If save as tif format
9741005
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+
9761013
out_path <- file.path(file_dir, out_name)
9771014

9781015
make_raster <- TRUE
@@ -1048,6 +1085,15 @@ bm_raster <- function(roi_sf,
10481085

10491086
unlink(temp_dir, recursive = T)
10501087

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+
10511097
return(r)
10521098
}
10531099

@@ -1089,9 +1135,6 @@ bm_raster_i <- function(roi_sf,
10891135
}
10901136

10911137
# Grab tile dataframe --------------------------------------------------------
1092-
#product_id <- "VNP46A4"
1093-
#date <- "2021-10-15"
1094-
10951138
year <- date %>% year()
10961139
month <- date %>% month()
10971140
day <- date %>% yday()
@@ -1107,10 +1150,6 @@ bm_raster_i <- function(roi_sf,
11071150
bm_tiles_sf <- bm_tiles_sf[!(bm_tiles_sf$TileID %>% str_detect("h00")),]
11081151
bm_tiles_sf <- bm_tiles_sf[!(bm_tiles_sf$TileID %>% str_detect("v00")),]
11091152

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-
11141153
inter <- tryCatch(
11151154
{
11161155
inter <- st_intersects(bm_tiles_sf, roi_sf, sparse = F) %>%
@@ -1121,11 +1160,10 @@ bm_raster_i <- function(roi_sf,
11211160
error = function(e){
11221161
warning("Issue with `roi_sf` intersecting with blackmarble tiles; try buffering by a width of 0: eg, st_buffer(roi_sf, 0)")
11231162
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))
11251163
}
11261164
)
11271165

1128-
grid_use_sf <- bm_tiles_sf[inter>0,]
1166+
grid_use_sf <- bm_tiles_sf[inter > 0,]
11291167

11301168
# Make Raster ----------------------------------------------------------------
11311169
tile_ids_rx <- grid_use_sf$TileID %>% paste(collapse = "|")
@@ -1150,16 +1188,11 @@ bm_raster_i <- function(roi_sf,
11501188
r <- r_list[[1]]
11511189
} else{
11521190

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"))
11591192
}
11601193

11611194
## Crop
1162-
r <- r %>% crop(roi_sf)
1195+
r <- r %>% terra::crop(roi_sf)
11631196

11641197
unlink(file.path(temp_dir, product_id), recursive = T)
11651198

readme_figures/readme_test.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ library(dplyr)
1212
library(purrr)
1313
library(lubridate)
1414
library(tidyr)
15-
library(raster)
15+
library(terra)
1616
library(sf)
1717
library(exactextractr)
1818
library(stringr)

readme_figures/testing.R

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Testing
2+
3+
# Setup ------------------------------------------------------------------------
4+
library(geodata)
5+
library(sf)
6+
library(terra)
7+
library(ggplot2)
8+
9+
library(readr)
10+
library(hdf5r)
11+
library(dplyr)
12+
library(purrr)
13+
library(lubridate)
14+
library(tidyr)
15+
library(sf)
16+
library(exactextractr)
17+
library(stringr)
18+
library(httr)
19+
20+
bearer <- read.csv("~/Desktop/bearer_bm.csv")$token
21+
22+
roi_sf <- gadm(country = "CHE", level=1, path = tempdir()) |> st_as_sf()
23+
24+
roi_sf = roi_sf
25+
product_id = "VNP46A3"
26+
date = "2018-04"
27+
bearer = bearer
28+
variable = "AllAngle_Composite_Snow_Free"
29+
quality_flag_rm = NULL
30+
check_all_tiles_exist = TRUE
31+
interpol_na = FALSE
32+
output_location_type = "memory"
33+
file_dir = NULL
34+
file_prefix = NULL
35+
file_skip_if_exists = TRUE
36+
quiet = FALSE
37+
38+
r_202110 <- bm_raster(roi_sf = roi_sf,
39+
product_id = "VNP46A3",
40+
date = "2021-10-01",
41+
bearer = bearer)
42+
43+
e_202110 <- bm_raster(roi_sf = roi_sf,
44+
product_id = "VNP46A3",
45+
date = c("2021-10-01", "2021-11-01"),
46+
bearer = bearer,
47+
output_location_type = "file",
48+
file_dir = "~/Desktop/test1")
49+
50+
e_202110 <- bm_extract(roi_sf = roi_sf,
51+
product_id = "VNP46A3",
52+
date = c("2021-10-01", "2021-11-01"),
53+
bearer = bearer,
54+
output_location_type = "file",
55+
file_dir = "~/Desktop/test1")
56+
57+

0 commit comments

Comments
 (0)