library(startR)
library(s2dverification)
lats.min <- 27
lats.max <- 81
lons.min <- -85
lons.max <- 45

path_list <- list(list(path='/esarchive/exp/ecmwf/system5c3s/monthly_mean/$var$_f6h/$var$_$sdate$.nc'),
                  list(path='/esarchive/exp/ukmo/glosea5_system13-c3s/monthly_mean/$var$_f24h/$var$_$sdate$.nc'))#,
#                  list(path='/esarchive/exp/meteofrance/system6_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc'),
#                  list(path='/esarchive/exp/cmcc/system3_m1-c3s/monthly_mean/$var$_f6h/$var$_$sdate$.nc'))

  sdates_exp <- '19931101'
#  sdates_exp <- c('19990901', '19991101')
#  sdates_exp <- c('19991101', '19990901')
  data <- Start(dat = path_list,
                var = 'psl',
                member = indices(1),
                sdate = sdates_exp,
                time = indices(1),
                latitude = values(list(lats.min, lats.max)),
                longitude = values(list(lons.min, lons.max)),
                latitude_reorder = Sort(),
                transform = CDORemapper,
#	transform_params = list(grid = 'r360x180',
#                transform_params = list(grid ='r360x181',
                transform_params = list(grid ='/esarchive/exp/ecmwf/system5c3s/daily_mean/psl_f6h/psl_19991101.nc',
                                        method = 'bilinear',
                                        crop = c(lons.min,lons.max,lats.min,lats.max)),
                transform_vars = c('latitude', 'longitude'),
                synonims = list(latitude=c('lat','latitude'),
                                longitude=c('lon','longitude'),
                                member=c('ensemble','realization')),
                transform_extra_cells = 2,
                return_vars = list(latitude = 'dat',
                                   longitude = 'dat',
                                   time = NULL),
           retrieve = TRUE, debug = TRUE)

attribtues(data)$Variables
PlotEquiMap(data, lon = -85:45, lat = 27.5:80.5)
PlotEquiMap(data, lon = -85:45, lat = 27:81, filled.continents = F)

dim(data)
PlotLayout('PlotEquiMap',c(2,3), var=drop(data),
           lat= 27:81,
           lon= -85:45,
           filled.continents=F)

path_list <- list(list(path =
               '/esarchive/exp/ecmwf/system5c3s/monthly_mean/$var$_f6h/$var$_$sdate$.nc'))
lons.min <- -85
data <- Start(dat = path_list,
                var = 'psl',
                member = indices(1),
                sdate = sdates_exp,
                time = indices(1),
                latitude = values(list(lats.min, lats.max)),
#                longitude = values(list(lons.min, lons.max)),
                latitude_reorder = Sort(),
              longitude = values(list(lons.min, lons.max)),

#                longitude_reorder = CircularSort(360,0),
                synonims = list(latitude=c('lat','latitude'),
                                longitude=c('lon','longitude'),
                                member=c('ensemble','realization')),
                return_vars = list(latitude = 'dat',
                                   longitude = 'dat',
                                   time = NULL),
            retrieve = TRUE, debug = F)

lapply(files, function(x) {source(paste0("../Flor/startR/R/",x))})
files  <- list.files("../Flor/startR/R")


#---------------------
library(startR)
lats.min <- -90
lats.max <- 90
lons.min <- 0
lons.max <- 360

path_list <- list(list(name = 'system5', 
                  path = '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc'),
                  list(name = 'glosea5',
             path= '/esarchive/exp/ukmo/glosea5_system13-c3s/monthly_mean/$var$_f24h/$var$_$sdate$.nc'),
                  list(name = 'meteofrance', 
                  path='/esarchive/exp/meteofrance/system6_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc'),
                  list(name = 'system3', 
                  path='/esarchive/exp/cmcc/system3_m1-c3s/monthly_mean/$var$_f6h/$var$_$sdate$.nc'))

variable <- 'psl'
years <- 1993:2016
month_ini <- list(12:8,c(3:1,12,11,10),c(6:2),c(9:5))


mon <- 2
sea <- 1
  sdates_exp <- paste0(years,sprintf('%02d',month_ini[[sea]][mon]),'01')
  print(sdates_exp)
  sdates_exp <- c('19990901', '19991101')
  sdates_exp <- c('19991101', '19990901')
  data <- Start(dat = path_list,
                var = variable,
                #member = indices(1),
                member = 'all',
                #member = list(list(1,10), list(1,5), list(1,3), list(1,9)),
                #member_depends = 'dat', 	
                sdate = sdates_exp,
                time = indices(1),
                latitude = values(list(0, 20)),
                longitude = values(list(0, 5)),
                latitude_reorder = Sort(),
                #longitude_reorder = CircularSort(0, 360),
                transform = CDORemapper,
                transform_params = list(grid ='r360x181',
                                        method = 'con',
                                        crop = c(lons.min,lons.max,lats.min,lats.max)),
                transform_vars = c('latitude', 'longitude'),
                synonims = list(latitude=c('lat','latitude'),
                                longitude=c('lon','longitude'),
                                member=c('ensemble','realization')),
                transform_extra_cells = 2,
                return_vars = list(latitude = 'dat',
                                   longitude = 'dat',
                                   time = NULL),
                retrieve = T)

data[1,1, , ,1,1,1] 
data[1,1,25:51,,1,1,1]

