library(s2dverification)
library(ncdf4)
library(startR)
library(abind)
source("R/CST_SaveExp.R")
source("../as.s2dv_cube.R")

glosea5 <- '/esnas/exp/glosea5/specs-seasonal_i1p1/$STORE_FREQ$_mean/$VAR_NAME$-allmemb/$VAR_NAME$_$START_DATE$.nc'

# Test 1:
clim_var <- 'tas'
dateseq <- c('19921101', '19931101', '19941101')
Glosea5 <- Load(var = clim_var, exp = list(list(name = 'glosea5', path = glosea5)), 
                sdates = dateseq, leadtimemin = 2, leadtimemax = 4,
                lonmin = -20, lonmax = 70, latmin = 25, latmax = 75, 
                storefreq = "monthly", sampleperiod = 1,  nmember = 9, 
                output = "lonlat") 
Glosea5 <- as.s2dv_cube(Glosea5)
CST_SaveExp(Glosea5, destination = "../path/")

# Test 2:
TwoModels <- Load(var = clim_var, exp = list(list(name = 'glosea5', path = glosea5),#, 
                                list(name = 'ecmwf/system4_m1')),
                sdates = dateseq, leadtimemin = 2, leadtimemax = 4,
                lonmin = -20, lonmax = 70, latmin = 25, latmax = 75,
                storefreq = "monthly", sampleperiod = 1,  nmember = 9,
                output = "lonlat", 
                grid = 'r360x180')
TwoModels <- as.s2dv_cube(TwoModels)
CST_SaveExp(TwoModels, destination = "../path/")

# Test 3:
ThreeModels <- Load(var = clim_var, exp = list(list(name = 'glosea5', path = glosea5), 
                                          list(name = 'ecmwf/system4_m1'),
                                          list(name = 'meteofrance/system5_m1')), 
                sdates = dateseq, leadtimemin = 2, leadtimemax = 4,
                lonmin = -20, lonmax = 70, latmin = 25, latmax = 75,
                storefreq = "monthly", sampleperiod = 1,  nmember = 9,
                output = "lonlat",
                grid = 'r360x180')
ThreeModels <- as.s2dv_cube(ThreeModels)
CST_SaveExp(ThreeModels, destination = "../path/")

# Test 4:
ThreeModels2 <- Load(var = 'tasmax', exp = list(list(name = 'glosea5', path = glosea5), 
                                          list(name = 'ecmwf/system4_m1'),
                                          list(name = 'meteofrance/system5_m1')), 
                sdates = dateseq, leadtimemin = 1, leadtimemax = 2,
                #lonmin = -20, lonmax = 70, latmin = 25, latmax = 75,
                storefreq = "monthly", sampleperiod = 1,  nmember = 9,
                output = "lonlat", 
               
 grid = 'r360x180')
ThreeModels2 <- as.s2dv_cube(ThreeModels2)
CST_SaveExp(ThreeModels2, destination = "../path/")

#Test 5:
repos <- '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc'
data <- Start(dat = repos,
              var = 'tas',
              sdate = c('20170101', '20180101'),
              ensemble = indices(1:20),
              time = 'all',
              latitude = 'all',
              longitude = indices(1:40),
              return_vars = list(latitude = 'dat', longitude = 'dat', time = 'sdate'),
              retrieve = TRUE)
data <- as.s2dv_cube(data)
CST_SaveExp(data, destination = "../path")


