Skip to content

Commit

Permalink
Terra issues in saving
Browse files Browse the repository at this point in the history
  • Loading branch information
haachicanoy committed Apr 7, 2022
1 parent fb7e934 commit 4a88824
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
33 changes: 33 additions & 0 deletions auxiliary/download_era5_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# R options
g <- gc(reset = T); rm(list = ls()) # Empty garbage collector
.rs.restartR() # Restart R session
options(warn = -1, scipen = 999) # Remove warning alerts and scientific notation
suppressMessages(library(pacman))
suppressMessages(pacman::p_load(tidyverse, ecmwfr, terra))

cds.usr <- "63618"
cds.key <- "0168398e-3f9f-4a6a-9430-01d176e61e90"
ecmwfr::wf_set_key(user = cds.usr, key = cds.key, service = "cds")

request <- list(
format = "zip",
variable = "2m_temperature",
statistic = "24_hour_maximum",
year = "2021",
month = "12",
day = c("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"),
dataset_short_name = "sis-agrometeorological-indicators",
target = "D:/tmax.zip"
)

file <- ecmwfr::wf_request(user = cds.usr,
request = request,
transfer = TRUE,
path = "~",
verbose = TRUE)

# Unzip the downloaded file and then read it

root <- 'C:/Users/haachicanoy/Downloads/tmax_ERA5' # I've manually changed the location of the files
r <- terra::rast(x = list.files(path = root, full.names = T))
plot(r[[1]] - 273.15) # To show Celsius degrees
7 changes: 6 additions & 1 deletion hotspots/00_get_flooding_variable.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ isos %>%
names(crd)[ncol(crd)] <- 'Flood'
crd <- crd[,c('x','y','Flood')]

rst <- terra::rast(x = crd, type = 'xyz', crs = terra::crs(shp))
tryCatch(expr = {rst <- terra::rast(x = crd, type = 'xyz', crs = terra::crs(shp))},
error = function(e){cat('Terra format failed\n')},
finally = {
rst <- raster::rasterFromXYZ(xyz = crd, crs = raster::crs(shp))
rst <- terra::rast(rst)
})

out <- paste0(root,'/data/',iso,'/flooding/flood.tif')

Expand Down
1 change: 1 addition & 0 deletions hotspots/01_link_IPinfo_hotspots.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ select_eco_vars <- function(root, iso, cntr, ip){
} else {
result$Percentile <- as.numeric(result$Percentile)
}
result$Name <- unique(ip_tbl$`Impact pathway`)

return(result)

Expand Down

0 comments on commit 4a88824

Please sign in to comment.