Skip to content

Commit

Permalink
fix var names
Browse files Browse the repository at this point in the history
  • Loading branch information
dramanica committed Sep 12, 2023
1 parent 2e38302 commit e3905fe
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
18 changes: 11 additions & 7 deletions R/download_worldclim_future.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ download_worldclim_future <- function(dataset, bio_var, filename){
# get resolution from the dataset name and convert it to the original
res_conversion <- data.frame(our_res = c("10m","5m","2.5m", "0.5m"),
wc_res = c("10m","5m", "2.5m", "30s"))
wc_res <- res_conversion$wc_res[res_conversion$our_res==substr(dataset,
start = regexpr("_\\d+\\.?\\d+m",dataset)+1,
stop=nchar(dataset))]
wc_res <- res_conversion$wc_res[res_conversion$our_res==tail(strsplit(dataset,"_")[[1]],1)]
gcm <- c("ACCESS-CM2", "BCC-CSM2-MR", "CMCC-ESM2", "EC-Earth3-Veg", "FIO-ESM-2-0",
"GFDL-ESM4", "GISS-E2-1-G", "HadGEM3-GC31-LL", "INM-CM5-0", "IPSL-CM6A-LR",
"MIROC6", "MPI-ESM1-2-HR", "MRI-ESM2-0", "UKESM1-0-LL")
Expand Down Expand Up @@ -73,12 +71,18 @@ download_worldclim_future <- function(dataset, bio_var, filename){

var_names <- names(wc_list[[1]])
sds_list <- list()
for (i_var in var_names){
sds_list[[i_var]]<-terra::rast(lapply(wc_list, terra::subset,subset=i_var))
names(sds_list[[i_var]])<-rep(i_var,nlyr((sds_list[[i_var]])))
for (i in 1:length(var_names)){
i_var <- var_names[i]
if (!any(postfix %in% c("bioc","elev"))){
new_var_name <-paste0(var_prefix,sprintf("%02d",i))
} else {
new_var_name <- i_var
}
sds_list[[new_var_name]]<-terra::rast(lapply(wc_list, terra::subset,subset=i_var))
names(sds_list[[new_var_name]])<-rep(new_var_name,nlyr((sds_list[[new_var_name]])))
}
wc_sds <- terra::sds(sds_list)

terra::writeCDF(wc_sds,filename=filename, compression=9,
overwrite=TRUE)
# fix time axis (this is a workaround if we open the file with sf)
Expand Down
2 changes: 2 additions & 0 deletions data-raw/helper_functions/verify_files_by_dataset.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# verify that all the variables in the tables are actually found in the files
# this requires all data to have been downloaded
library(pastclim)
full_meta <- pastclim:::dataset_list_included
in_dir <- get_data_path()
problem_rows <- vector()
Expand All @@ -9,6 +10,7 @@ for (i in 1:nrow(full_meta)){
nc_in <- ncdf4::nc_open(file.path(in_dir, full_meta$file_name[i]))
if (!full_meta$ncvar[i] %in% names(nc_in$var)){
message("problem with ",full_meta$ncvar[i]," in ", full_meta$file_name[i],"\n")
stop("we had a problem")
problem_rows[i]<-TRUE
} else {
problem_rows[i]<-FALSE
Expand Down

0 comments on commit e3905fe

Please sign in to comment.