ex1_13_implicit_dependency.R 2.88 KB
Newer Older
aho's avatar
aho committed
# Author: An-Chi Ho
aho's avatar
aho committed
# Date: 13rd July 2021
aho's avatar
aho committed
#---------------------------------------------------------------------
# This script shows how to use a value array as the inner dimension selector to express
# dependency on a file dimension. By this means, we don't need to specify the *_across
# parameter and Start() can recognize this dependecy relationship.
# In the first case, 'time' is dependent on 'sdate'. We create the actual time values
# for each sdate beforehand. The time array is two-dimensional with the names 'time'
# and 'sdate'.
# In the second case, 'region' is dependent on 'sdate'. The two files have different 
# index for Nino3. sdate 1993 has 'Nino3' at index 9 while sdate 2013 has 'Nino3' at 
aho's avatar
aho committed
# index 11. Create a value array for region selector so Start() can look for 'Nino3' in 
aho's avatar
aho committed
# each file.
#---------------------------------------------------------------------

library(startR)
library(lubridate)

# Case 1: 'time' depends on 'sdate'
repos <- '/esarchive/exp/ecmwf/system4_m1/daily_mean/$var$_f24h/$var$_$sdate$.nc'

yr1 <- "2001"
sdates <- ymd("20010501") + rep(years(0:2), each = 1)
times <- array(ymd("20010501") + days(0:30) + rep(years(0:2), each = 31),
               dim = c(time = 31, sdate = 3))
times <- as.POSIXct(times * 86400, tz = 'UTC', origin = '1970-01-01')

exp <- Start(dat = repos,
             var = 'tos',
             sdate = format(sdates, "%Y%m%d"),
             time = times,  #dim: [time = 31, sdate = 3]. time is corresponding to each sdate
             ensemble = indices(1:5),
             lat = 'all', 
             lon = 'all',
             synonims = list(lat = c('lat', 'latitude'),
                             lon = c('lon', 'longitude')),
             return_vars = list(lon = NULL, lat = NULL, time = 'sdate'),
             retrieve = T) 

#=============================================================================

# Case 2: 'region' depends on 'sdate'
path <- paste0('/esarchive/exp/ecearth/a35b/diags/DCPP/EC-Earth-Consortium/',
               'EC-Earth3-HR/dcppA-hindcast/r1i1p1f1/Omon/$var$_mixed/gn/v20201107/',
               '$var$_Omon_EC-Earth3-HR_dcppA-hindcast_s$sdate$-r1i1p1f1_gn_$chunk$.nc')

region <- array('Nino3', dim = c(sdate = 2, region = 1))

data <- Start(dat = path,
              var = 'tosmean',
              sdate = c('1993', '2013'),
              chunk = indices(1:2),
              chunk_depends = 'sdate',
              region = region,
              time = 'all', 
              time_across = 'chunk',
              merge_across_dims = TRUE,
              return_vars = list(time = c('sdate', 'chunk'),
                                 region = 'sdate'),
aho's avatar
aho committed
              retrieve = T)
aho's avatar
aho committed

aho's avatar
aho committed
dim(data)
#   dat    var  sdate region   time 
#     1      1      2      1      2 

data[1, 1, , 1, ]
#         [,1]     [,2]
#[1,] 24.98788 24.46488  # --> region index 9 in original file
#[2,] 24.47482 24.75953  # --> region index 11 in orginal file