library(startR)

lats.min = 25
lats.max = 52
lons.min = -10  
lons.max = 40

ini <- 1981
fin <- 2020
numyears <- fin - ini +1
mth = '05'
start <- as.Date(paste(ini, mth, "01", sep = ""), "%Y%m%d")
end <- as.Date(paste(fin, mth, "01", sep = ""), "%Y%m%d")
dateseq <- format(seq(start, end, by = "year"), "%Y%m%d")

exp_path <- '/esarchive/exp/ecmwf/system5c3s/monthly_mean/$var$_f6h/$var$_$sdate$.nc'

exp_start <- Start(dat = exp_path,
             var = 'tas',
             sdate = dateseq,
             time = indices(2:4),
             ensemble = indices(1:10),
             latitude = values(list(lats.min, lats.max)),
             latitude_reorder = Sort(decreasing = TRUE),
             longitude = values(list(lons.min, lons.max)),
             longitude_reorder = CircularSort(0, 360),
             synonims = list(latitude=c('lat','latitude'),
                             longitude=c('lon','longitude')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = TRUE)

attributes(exp_start)$Variables$dat1$lat
attributes(exp_start)$Variables$dat1$lon

# ------------------------------
library(CSTools)
lat_min = 25
lat_max = 52
lon_min = -10
lon_max = 40
ini <- 1981
fin <- 2020
numyears <- fin - ini +1
mth = '05'
start <- as.Date(paste(ini, mth, "01", sep = ""), "%Y%m%d")
end <- as.Date(paste(fin, mth, "01", sep = ""), "%Y%m%d")
dateseq <- format(seq(start, end, by = "year"), "%Y%m%d")
mon1 <- 2
monf <- 4
forecastsys <- 'system5c3s'
obs <- 'erainterim'
grid <- "256x128"
clim_var = 'tas'
exp <- CST_Load(var = clim_var, exp = forecastsys,
                         sdates = dateseq, leadtimemin = mon1, leadtimemax = monf,
                         lonmin = lon_min, lonmax = lon_max,
                         latmin = lat_min, latmax = lat_max,
                         storefreq = "monthly", sampleperiod = 1,  nmember = 10,
                         output = "lonlat")

# ----------------------
> exp_start[1,1,1,1,1,1,1]
[1] 286.9749
> exp$data[1,1,1,1,1,1]
[1] 286.97
all(exp$lon == as.vector(attributes(exp_start)$Variables$dat1$lon))
all(exp$lat == as.vector(attributes(exp_start)$Variables$dat1$lat))

library(s2dv)
PlotEquiMap(exp$data[1,1,1,1,,], lat = exp$lat, lon = exp$lon, filled.cont = F)
dev.new()
PlotEquiMap(exp_start[1,1,1,1,1,,], 
            lat = as.vector(attributes(exp_start)$Variables$dat1$lati),
            lon = as.vector(attributes(exp_start)$Variables$dat1$lon), filled.con = F)

for ( t in 1:3) {
 for (e in 1:10) {
  for (s in 1:40) {
   print(all(round(exp$data[1,e,s,t,,], 2) == round(exp_start[1,1,s,t,e,,], 2)))
  }
 }
}
range(round(exp$data[1,1,1,1,,], 1) - round(exp_start[1,1,1,1,1,,], 1)))

# ------------------------------
#----------- Regrid:

library(CSTools)
lat_min = 25
lat_max = 52
lon_min = -10  
lon_max = 40
ini <- 1981
fin <- 2020
numyears <- fin - ini +1
mth = '05'
start <- as.Date(paste(ini, mth, "01", sep = ""), "%Y%m%d")
end <- as.Date(paste(fin, mth, "01", sep = ""), "%Y%m%d")
dateseq <- format(seq(start, end, by = "year"), "%Y%m%d")
mon1 <- 2
monf <- 4
forecastsys <- 'system5c3s'
obs <- 'erainterim'
grid <- "256x128"
clim_var = 'tas'
exp <- CST_Load(var = clim_var, exp = forecastsys,
                         sdates = dateseq, leadtimemin = mon1, leadtimemax = monf,
                         lonmin = lon_min, lonmax = lon_max,
                         latmin = lat_min, latmax = lat_max, 
                         storefreq = "monthly", sampleperiod = 1,  nmember = 10, 
                         output = "lonlat")


obs_path <- '/esarchive/recon/ecmwf/erainterim/monthly_mean/$var$_f6h/$var$_$month$.nc'
 
