#this code shows the problem with CST_Load, that only assign the correct class if exp and obs are loaded: 
#https://earth.bsc.es/gitlab/external/cstools/issues/19

library(CSTools)
library(zeallot)

# 10 starting dates:
start <- as.Date(paste("1993", "11", "01", sep = ""), "%Y%m%d")
end <- as.Date(paste("2002", "11", "01", sep = ""), "%Y%m%d")
dateseq <- format(seq(start, end, by = "year"), "%Y%m%d")
# path structure:
glosea5 <- '/esnas/exp/glosea5/specs-seasonal_i1p1/$STORE_FREQ$_mean/$VAR_NAME$-allmemb/$VAR_NAME$_$START_DATE$.nc'
# CST_Load using zeallot package:
c(exp, obs) %<-% 
  CST_Load(var = "tas", exp = list(list(name = 'glosea5', path = glosea5), 
                                      list(name = 'ecmwf/system4_m1'),
                                      list(name = 'meteofrance/system5_m1')),
           obs = "erainterim", sdates = dateseq, leadtimemin = 2, leadtimemax = 4,
           lonmin = -20, lonmax = 70, latmin = 25, latmax = 75, 
           storefreq = "monthly", sampleperiod = 1,  nmember = 9, 
           output = "lonlat", method = "bilinear", 
           grid = "r256x128")
# Check class:
class(exp)
class(obs)
dim(exp$data)

exp$load_parameters$sdates


rm(list = c("obs", "exp"))


exp <-  CST_Load(var = "tas", exp = list(list(name = 'glosea5', path = glosea5), 
                                   list(name = 'ecmwf/system4_m1'),
                                   list(name = 'meteofrance/system5_m1')),
           #obs = "erainterim", 
           sdates = dateseq, leadtimemin = 2, leadtimemax = 4,
           lonmin = -20, lonmax = 70, latmin = 25, latmax = 75, 
           storefreq = "monthly", sampleperiod = 1,  nmember = 9, 
           output = "lonlat", method = "bilinear", 
           grid = "r256x128")
class(exp)
class(obs)

str(lonlat_prec)
class(lonlat_prec)
str(lonlat_data)
class(lonlat_data)
str(lonlat_data$exp)
