Set paths to input file ensembles using a consistent pattern across sites
Source:R/setEnsemblePaths.R
setEnsemblePaths.RdPropagates a filename pattern into the relevant paths of a multi-site
settings. For example if your files are named like "mymet/siteA/scenario1.nc"
up through "mymet/siteZ/scenario50.nc",
setEnsemblePaths(settings, n_reps = 50, "mymet/{id}/scenario{n}.nc") will
add them all to your settings in one shot.
Usage
setEnsemblePaths(
settings,
n_reps,
input_type = "met",
path_template = "./{id}/{n}.nc",
...
)Details
Operates on one input section (met, poolinitcond, etc) at a time because it's common to have different path conventions for met vs IC.
The path template should be a string recognized by glue::glue(),
with curly braces wrapping any expressions to be interpolated.
Variables in these expressions are resolved in this order:
Variables defined in each site's
run$sitesettings block (notably meaning{id}will contain the site id that is always present as run$site$id)Variables passed as named arguments in
...(these take the same value at every site){n}is treated specially as a replicate number and replaced with the sequence1:n_reps
If the same name is defined in both run$site and ..., uses the latter.
All variables must have length either 1 or n_reps.
If inputs$<input_type> does not exist, it will be created with a path
element that matches the requested pattern. If it does exist, any existing
path element will be overwritten.
Examples
s <- as.Settings(list(
run = list(
start.date = "TBD",
site = list(),
inputs = list(
met = list(),
poolinitcond = list()
)
)
))
m <- createMultiSiteSettings(s, c("a1", "b2"))
m1 <- setEnsemblePaths(m, 2)
m1$run$site.a1$inputs
#> $met
#> $met$path
#> $met$path$path1
#> [1] "./a1/1.nc"
#>
#> $met$path$path2
#> [1] "./a1/2.nc"
#>
#>
#>
#> $poolinitcond
#> list()
#>
m2 <- setEnsemblePaths(
m, 2, "poolinitcond",
icdir = "some/long/path",
path_template = "{icdir}/{id}/{n}.nc"
)
m2$run$site.a1$inputs
#> $met
#> list()
#>
#> $poolinitcond
#> $poolinitcond$path
#> $poolinitcond$path$path1
#> [1] "some/long/path/a1/1.nc"
#>
#> $poolinitcond$path$path2
#> [1] "some/long/path/a1/2.nc"
#>
#>
#>
# using site-specific values in the path template:
m_bysite <- createMultiSiteSettings(
templateSettings = s,
siteIds = data.frame(id = c("a1", "b2"), grid_cell = c("35N81W", "42N80W"))
)
m2_bysite <- setEnsemblePaths(
settings = m_bysite,
n_reps = 2,
input_type = "met",
icdir = "some/long/path",
GCM = c("CESM2", "MIROC6"),
path_template = "{icdir}/{grid_cell}/{GCM}.nc"
)
m2_bysite$run$site.a1$inputs
#> $met
#> $met$path
#> $met$path$path1
#> [1] "some/long/path/35N81W/CESM2.nc"
#>
#> $met$path$path2
#> [1] "some/long/path/35N81W/MIROC6.nc"
#>
#>
#>
#> $poolinitcond
#> list()
#>