library(yaml)
library(startR)
decadal_dic <- read_yaml("/esarchive/scratch/nperez/git/Flor/Decadal/HadGEM3_archive.yml")
names(decadal_dic$esarchive$Decadal)
  # to read from recipe:
  model = 'HadGEM3'
  var = 'tas'
  lat_min = 0
  lat_max = 10
  lon_min = 0
  lon_max = 10
  sim <- 'historical'
  #model = 'EC-Earth3-i1'
  sdates <- 1960:2015
  years <- 1961:2016


path <- '/esarchive/exp/CMIP6/dcppA-hindcast/hadgem3-gc31-mm/cmip6-dcppA-hindcast_i1p1/DCPP/MOHC/HadGEM3-GC31-MM/dcppA-hindcast/$member$/Amon/$var$/gr/v20200316/$var$_*_s$sdate$_$member$_gr_$sdate$11-$aux$03.nc'
fyears <- 1

  fmonths = (1 + 12*(fyears[1]-1)) : (2 + 12*fyears[length(fyears)])
  exp <- Start(dat = path,
               var = var,
               sdate = paste0(sdates),
               aux = 'all',
               aux_depends = 'sdate',
               lat = values(list(lat_min, lat_max)),
               lon = values(list(lon_min, lon_max)),
               fmonth = indices(fmonths),
               member = 'all',
               synonims = list(fmonth = c('fmonth','time'),
                               lon = c('lon', 'longitude'),
                               lat = c('lat', 'latitude')),
               return_vars = list(lat = NULL, lon = NULL),
               lat_reorder = Sort(decreasing = F),
               lon_reorder = CircularSort(0, 360),
               num_procs = 1, retrieve = F)


path <- '/esarchive/obs/noaa/ghcn_v2/monthly_mean/$var$/$var$_$year$$month$.nc'

reference <- Start(dat = path,
                   var = var,
                   year = paste0(years_hist),
                   month = 'all',
                   lat = values(list(lat_min, lat_max)),
                   lon = values(list(lon_min, lon_max)),
                   synonims = list(lat = c('lat','latitude'), 
                                   lon = c('lon','longitude'),
                                   month = c('month','time')),
                   return_vars = list(lat = NULL, lon = NULL),
                   lat_reorder = Sort(decreasing = F),
                   lon_reorder = CircularSort(0, 360),
                   num_procs = 1, retrieve = FALSE)

all(attributes(hist)$Variables$common$lat == attributes(exp)$Variables$common$lat)
all(attributes(reference)$Variables$common$lat == attributes(hist)$Variables$common$lat)

