source('/esarchive/scratch/nperez/git/s2dverification/R/Utils.R')
source('/esarchive/scratch/nperez/git/s2dverification/R/ConfigFileOpen.R')
source('/esarchive/scratch/nperez/git/s2dverification/R/ConfigApplyMatchingEntries.R')
source("/es")
source('/esarchive/scratch/nperez/git/s2dverification/R/LeapYear.R')
library(ncdf4)
library(plyr)
library(testthat)
library(bigmemory)
library(abind)
library(parallel)
var = 'tas'
exp = list(list(name = 'glosea5', 
                path = '/esarchive/exp/glosea5/specs-seasonal_i1p1/$STORE_FREQ$_mean/$VAR_NAME$-allmemb/$VAR_NAME$_$START_DATE$.nc'),
           list(name = 'ecmwf/system4_m1'),
           list(name = 'meteofrance/system5_m1'))
obs = 'erainterim'
sdates = format(seq(as.Date(paste0(1992, 11, '01'), "%Y%m%d"),
                    as.Date(paste0(2012, 11, '01'), "%Y%m%d"), by = 'year'),
                "%Y%m%d")
nmember = 9
nmemberobs = NULL
nleadtime = NULL
leadtimemin = 2
leadtimemax = 4
storefreq = "monthly"
sampleperiod = 1
lonmin = -20
lonmax = 70
latmin = 25
latmax = 75 
output = 'lonlat' 
method = 'bilinear'
grid = "r256x128" 
maskmod = vector("list", 15); maskobs = vector("list", 15) 
configfile = NULL; varmin = NULL; varmax = NULL
silent = FALSE; nprocs = NULL; dimnames = NULL 
remapcells = 2; path_glob_permissive = 'partial'
source("/esarchive/scratch/nperez/git/Flor/source_lines.R")
source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Load.R", 856, 898)
source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Load.R", 901, 1387)
source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Load.R", 1388, 1592)
source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Load.R", 1593, 2204)

var = 'prlr'
exp = list(#list(name = 'ecmwfS5', path = "/esarchive/exp/ecmwf/system5c3s/$STORE_FREQ$_mean/$VAR_NAME$_s0-24h/$VAR_NAME$_$START_DATE$.nc"),
           list(name = 'ecmwfS4', path = "/esarchive/exp/ecmwf/system4_m1/$STORE_FREQ$/$VAR_NAME$/$VAR_NAME$_$START_DATE$.nc"), 
           list(name = 'ecmwfS5', path = '/esarchive/exp/ecmwf/system5c3s/$STORE_FREQ$_mean/$VAR_NAME$_s0-24h/$VAR_NAME$_$START_DATE$.nc'))
obs = list(list(name = 'era5', path = '/esarchive/recon/ecmwf/era5/$STORE_FREQ$_mean/$VAR_NAME$_f1h-r1440x721cds/$VAR_NAME$_$YEAR$$MONTH$.nc'))
sdates = c('19931101', '19941101') #paste0(1993:2018, '1101')
nmember = 5;leadtimemin = 2
leadtimemax = NULL
storefreq = "daily"; sampleperiod = 1
latmin = 42; latmax = 49; lonmin = 4; lonmax = 11
output = 'lonlat'; nprocs = 1
method = 'conservative'; grid = NULL
maskmod = vector("list", 15); maskobs = vector("list", 15)
configfile = NULL; varmin = NULL; varmax = NULL
silent = FALSE; dimnames = NULL
remapcells = 2; path_glob_permissive = 'partial'
nmemberobs = NULL; nleadtime = NULL
source("/esarchive/scratch/nperez/git/s2dverification/R/LeapYear.R")

# ---------------------
source("/esarchive/scratch/nperez/git/s2dverification/R/Load.R")
library(abind)
library(testthat)
library(bigmemory)
data <- Load(var = 'prlr',
             exp = list(list(name = 'ecmwfS5', path = "/esarchive/exp/ecmwf/system5c3s/$STORE_FREQ$_mean/$VAR_NAME$_s0-24h/$VAR_NAME$_$START_DATE$.nc")),
             obs = list(list(name = 'era5', path = '/esarchive/recon/ecmwf/era5/$STORE_FREQ$_mean/$VAR_NAME$_f1h-r1440x721cds/$VAR_NAME$_$YEAR$$MONTH$.nc')),
             sdates = c('19931101', '19941101'), #paste0(1993:2018, '1101')
             nmember = 25,leadtimemin = 1,
             leadtimemax = NULL,
             storefreq = "daily", sampleperiod = 1,
             latmin = 42, latmax = 49, lonmin = 4, lonmax = 11,
             output = 'lonlat', nprocs = 1,
             method = 'conservative', grid = 'r360x181',
             maskmod = vector("list", 15), maskobs = vector("list", 15),
             configfile = NULL, varmin = NULL, varmax = NULL,
             silent = FALSE, dimnames = NULL,
             remapcells = 2, path_glob_permissive = 'partial',
             nmemberobs = NULL, nleadtime = NULL)
dim(data$mod)
dim(data$obs)
sum(is.na(data$mod))
sum(is.na(data$obs))
s2dverification::PlotEquiMap(data$mod[1,1,1,1,,], data$lon, data$lat,
                             filled.continents = FALSE)
dev.new()
s2dverification::PlotEquiMap(data$obs[1,1,2,1,,], data$lon, data$lat,
                             filled.continents = FALSE)
data$Dates$start[1]
data$Dates$end[1]
length(data$Dates$start)
obs <- s2dverification::Load(var = 'prlr', exp = NULL, 
                             obs = list(list(name = 'era5', path = '/esarchive/recon/ecmwf/era5/$STORE_FREQ$_mean/$VAR_NAME$_f1h-r1440x721cds/$VAR_NAME$_$YEAR$$MONTH$.nc')),
                             sdates = c('19931101', '19941101'), 
                             #paste0(1993:2018, '1101')
                             nmember = 25,leadtimemin = 1,
                             leadtimemax = 2,
                             storefreq = "daily", sampleperiod = 1,
                             latmin = 42, latmax = 49, lonmin = 4, lonmax = 11,
                             output = 'lonlat', nprocs = 1,
                             method = 'conservative', grid = 'r360x181',
                             maskmod = vector("list", 15), maskobs = vector("list", 15),
                             configfile = NULL, varmin = NULL, varmax = NULL,
                             silent = FALSE, dimnames = NULL,
                             remapcells = 2, path_glob_permissive = 'partial',
                             nmemberobs = 1, nleadtime = NULL)
dev.new()
s2dverification::PlotEquiMap(obs$obs[1,1,2,2,,], obs$lon, obs$lat,
                             filled.continents = FALSE)
data <- Load(var = 'prlr',
             exp = list(list(name = 'ecmwfS5', path = "/esarchive/exp/ecmwf/system5c3s/$STORE_FREQ$_mean/$VAR_NAME$_s0-24h/$VAR_NAME$_$START_DATE$.nc")),
             obs = list(list(name = 'era5', path = '/esarchive/recon/ecmwf/era5/$STORE_FREQ$_mean/$VAR_NAME$_f1h-r1440x721cds/$VAR_NAME$_$YEAR$$MONTH$.nc')),
             sdates = c('19931101', '19941101'), #paste0(1993:2018, '1101')
             nmember = 25,leadtimemin = 1,
             leadtimemax = NULL,
             storefreq = "daily", sampleperiod = 1,
             latmin = 42, latmax = 49, lonmin = 4, lonmax = 11,
             output = 'lonlat', nprocs = 1,
             method = 'conservative', grid = 'r360x181',
             maskmod = vector("list", 15), maskobs = vector("list", 15),
             configfile = NULL, varmin = NULL, varmax = NULL,
             silent = FALSE, dimnames = NULL,
             remapcells = 2, path_glob_permissive = 'partial',
             nmemberobs = NULL, nleadtime = NULL)

---------------------
