library(startR)
library(ClimProjDiags)
dataset <- "/esarchive/recon/ecmwf/era5/daily_mean/$var$_f1h/$var$_$sdate$.nc"
sdates <- c(as.Date("1990-10-01") + c(0, 31, 30), 
            as.Date("1991-10-01") + c(0, 31, 30), 
            as.Date("1992-10-01") + c(0, 31, 30))

tasmax <- Start(dataset = dataset,
                var = 'tasmax',
                sdate = format(sdates, "%Y%m"),
                time = 'all',
                longitude = indices(1:10),
                latitude = indices(1:20),
                return_vars = list(time = NULL, latitude = NULL, longitude = NULL),
                num_procs = 4, retrieve = FALSE,
                synonims=list(longitude = c('lon', 'longitude'), latitude = c('lat', 'latitude')))
tasmin <- Start(dataset = dataset,
                var = 'tasmin',
                sdate = format(sdates, "%Y%m"),
                time = 'all',
                longitude = indices(1:10),
                latitude = indices(1:20),
                return_vars = list(time = NULL, latitude = NULL, longitude = NULL),
                num_procs = 4, retrieve = FALSE,
                synonims=list(longitude = c('lon', 'longitude'), latitude = c('lat', 'latitude')))

# Bug fixed in branch bugfix-dtrref-dates.tmax:
source("/esarchive/scratch/nperez/git/ClimProjDiags/R/DTRRef.R")
library(PCICt)
library(multiApply)

step <- Step(DTRRef,
             target_dims = list(tasmax = c('time'), tasmin = c('time')),
             output_dims = list(dtr.ref = c('time'), season = NULL),
             use_attributes = list(tasmax = "Variables", tasmin = "Variables"))

workflow <- AddStep(list(tasmax = tasmax, tasmin = tasmin), step, by.seasons = TRUE)

res <- Compute(workflow$dtr.ref,
               chunks = list(latitude = 2, longitude = 2),
               threads_load = 2,
               threads_compute = 4)
> names(res)
[1] "dtr.ref" "season" 
> dim(res$dtr.ref)
     time   dataset       var     sdate longitude  latitude 
        1         1         1         9        10        20 
> summary(res$dtr.ref)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.1201  0.1370  0.1465  0.1488  0.1588  0.2016 





