CHESS baseline: create metadata and convert to tif


Create metadata of datasets corresponding to summarised environmental conditions for periods of 20 years from the CHESS project, and convert them to tif format. We use these conditions as baseline for training our models.

Load packages, read data and source custom scripts

rm(list = ls())
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(stars)
#> Loading required package: abind
#> Loading required package: sf
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
path_proj <- day2day::git_path()
path_data <- file.path(path_proj, "data")
path_raw <- file.path(path_data, "raw")
path_cleaned <- file.path(path_data, "cleaned")
path_baseline <- file.path(path_raw, "chess-baseline")

source(file.path(path_proj, "src", "22-nc-to-tif.R"))

Create metadata

regex_extract <- paste(
    "(chess)-[[:alpha:]]+_(.+)_[[:alpha:]]{2}",
    "1km_20yr-([[:alpha:]]+)-([[:alpha:]]+)",
    "([[:digit:]]+)-([[:digit:]]+)\\.nc",
    sep = "_"
)

metadata <- create_metadata(path_baseline,regex_extract) %>%
    mutate(rcp = NA_character_)

Convert to tif

purrr::walk2(metadata$file, metadata$variable,
             ~ nc_to_tif(.x, .y, path_baseline, path_cleaned))

Save metadata

write.csv(metadata, file.path(path_baseline, "metadata.csv"), row.names = FALSE)
metadata <- mutate(metadata, file = sub("\\.nc$", "\\.tif", file))
write.csv(metadata, file.path(path_cleaned, "chess-metadata.csv"), row.names = FALSE)

Time to execute the task

Only useful when executed with Rscript.

proc.time()
#>    user  system elapsed 
#>  90.708   4.566  95.230