|
| 1 | +#!/usr/bin/env Rscript |
| 2 | + |
| 3 | +# Converts a directory full of gridded ERA5 meteorology data |
| 4 | +# from PEcAn's standard netCDF format to RothC weather statistics. |
| 5 | + |
| 6 | +# This is basically a thin wrapper around `met2model.RothC()`, |
| 7 | +# and is specific to ERA5 only by its assumptions about the filenames: |
| 8 | +# It assumes inputs has a directory per location and ensemble member (1-10) |
| 9 | +# containing one file per year, e.g. |
| 10 | +# path/to/ERA5_<location>_<ens>/ERA5.<ens>.<year>.nc |
| 11 | +# and produces output with one directory per location containing one multiyear |
| 12 | +# file per ensemble member, e.g. |
| 13 | +# path/to/ERA5_<location>/ERA5.<ens>.<startdate>.<enddate>.nc |
| 14 | + |
| 15 | +# For a related script that takes a list of locations to be converted rather |
| 16 | +# than operate on the entire input directory, see |
| 17 | +# https://github.com/ccmmf/workflows/blob/main/2a_grass/01_ERA5_nc_to_clim.R |
| 18 | + |
| 19 | + |
| 20 | +## --------- runtime values: change for your system and simulation --------- |
| 21 | + |
| 22 | +options <- list( |
| 23 | + optparse::make_option("--site_era5_path", |
| 24 | + default = "data_raw/ERA5_CA_nc", |
| 25 | + help = paste( |
| 26 | + "Path to your existing ERA5 data in PEcAn CF format, organized as", |
| 27 | + "single-site, single-year netcdfs in subdirectories per ensemble member.", |
| 28 | + "Files should be named", |
| 29 | + "'<site_era5_path>/ERA5_<siteid>_<ensid>/ERA5.<ensid>.<year>.nc'" |
| 30 | + ) |
| 31 | + ), |
| 32 | + optparse::make_option("--site_rothc_met_path", |
| 33 | + default = "data/ERA5_CA_RothC", |
| 34 | + help = paste( |
| 35 | + "Output path:", |
| 36 | + "single-site, multi-year weather summaries, one per ensemble member.", |
| 37 | + "Files will be named", |
| 38 | + "<site_rothc_met_path>/<siteid>/ERA5.<ensid>.<start>.<end>.dat" |
| 39 | + ) |
| 40 | + ), |
| 41 | + optparse::make_option("--start_date", |
| 42 | + default = "2016-01-01", |
| 43 | + help = "Date to begin clim file", |
| 44 | + ), |
| 45 | + optparse::make_option("--end_date", |
| 46 | + default = "2024-12-31", |
| 47 | + help = "Date to end clim file", |
| 48 | + ), |
| 49 | + optparse::make_option("--n_cores", |
| 50 | + default = Sys.getenv("NCPUS", 1L), |
| 51 | + help = "number of CPUs to use in parallel", |
| 52 | + ), |
| 53 | + optparse::make_option("--parallel_strategy", |
| 54 | + default = "multisession", |
| 55 | + help = "Strategy for parallel conversion, passed to future::plan()", |
| 56 | + ) |
| 57 | +) |> |
| 58 | + # Show default values in help message |
| 59 | + purrr::modify(\(x) { |
| 60 | + x@help <- paste(x@help, "[default: %default]") |
| 61 | + x |
| 62 | + }) |
| 63 | + |
| 64 | +args <- optparse::OptionParser(option_list = options) |> |
| 65 | + optparse::parse_args() |
| 66 | + |
| 67 | + |
| 68 | +# ----------- end system-specific --------------------------------- |
| 69 | + |
| 70 | + |
| 71 | +future::plan(args$parallel_strategy, workers = as.numeric(args$n_cores)) |
| 72 | + |
| 73 | + |
| 74 | +dirs <- list.dirs(args$site_era5_path, recursive = FALSE, full.names = FALSE) |> |
| 75 | + data.frame(indir = _) |> |
| 76 | + dplyr::mutate( |
| 77 | + in_path = file.path(args$site_era5_path, indir), |
| 78 | + location = sub(r"(ERA5_(.*)_(\d+)$)", "\\1", indir), |
| 79 | + ens_num = sub(r"(ERA5_(.*)_(\d+)$)", "\\2", indir), |
| 80 | + in_prefix = paste0("ERA5.", ens_num), |
| 81 | + out_path = file.path(args$site_rothc_met_path, paste0("ERA5_", location)) |
| 82 | + ) |
| 83 | + |
| 84 | +if (!dir.exists(args$site_rothc_met_path)) { |
| 85 | + dir.create(args$site_rothc_met_path, recursive = TRUE) |
| 86 | +} |
| 87 | + |
| 88 | +furrr::future_pwalk( |
| 89 | + dirs, |
| 90 | + function(in_path, in_prefix, out_path, ...) { |
| 91 | + PEcAn.RothC::met2model.RothC( |
| 92 | + in.path = in_path, |
| 93 | + start_date = args$start_date, |
| 94 | + end_date = args$end_date, |
| 95 | + in.prefix = in_prefix, |
| 96 | + outfolder = out_path |
| 97 | + ) |
| 98 | + } |
| 99 | +) |
0 commit comments