# Load data, no computation
path1 <- "/esarchive/exp/ecmwf/system5_m1/monthly_mean/tas_f6h/tas_20170101.nc"
path2 <- "/esarchive/exp/ecmwf/system5_m1/monthly_mean/tas_f6h/tas_20180101.nc"
var <- "tas"

##ncdf4
library(ncdf4)
file1 <- nc_open(path1)
names(file1$dim)
dat1 <- ncvar_get(file1, var, 
                  start = c(longitude = 1, latitude = 1, ensemble = 1, time = 1),
                  count = c(longitude = 40, latitude = 12, ensemble = 20, time = 7))
dim(dat1)
dat1[1, 1, 1:3, 1]
  lat <- ncvar_get(file1, 'latitude')
  lon <- ncvar_get(file1, 'longitude')

PlotEquiMap(dat1[, , 1, 1], lat = lat[1:12], lon = lon[1:40])

#---
file2 <- nc_open(path2)
names(file2$dim)
dat2 <- ncvar_get(file2, var, 
                  start = c(longitude = 1, latitude = 1, ensemble = 1, time = 1),
                  count = c(longitude = 40, latitude = 12, ensemble = 20, time = 7))
dat2[1, 1, 1:3, 1]
PlotEquiMap(dat2[, , 1, 1], lat = lat[1:12], lon = lon[1:40])

##s2dv::Load
  library(s2dverification)
  startDates <- c("20170101","20180101")

#  path1 <- "/esarchive/exp/ecmwf/system5_m1/monthly_mean/tas_f6h/tas_20170101.nc"

  ecmwf <- list(name = 'system5_m1',
                   path = file.path('/esarchive/exp/ecmwf',
                                    '$EXP_NAME$/$STORE_FREQ$_mean',
                                    '$VAR_NAME$_f6h/$VAR_NAME$_$START_DATE$.nc'))
         
  data <- Load(var = var,
               exp = list(ecmwf),
               sdates = startDates,
               storefreq = "monthly",
               leadtimemax = 7,
               output = 'lonlat',
               nmember = 20,
               latmin = lat[12], latmax = lat[1],
               lonmin = lon[1], lonmax = lon[40])$mod
  dim(data)
  data[1, 1:3, 1, 1, 1, 1]
  PlotEquiMap(data[1, 1, 1, 1, , ], lat = lat[1:12], lon = lon[1:40])

##startR::Start
  library(startR)
  repos <- "/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc"

  data_start <- Start(dat = repos,
                var = var,
                sdate = startDates,
                ensemble = indices(1:20),
                time = indices(1), #'all',
                latitude = indices(1:12),
                longitude = indices(1:40),
                #return_vars = list(time = c('sdate'), longitude = NULL, latitude = NULL),
                retrieve = TRUE
               )
 data_start[1,1,2,1,1,1:10,1]
 PlotEquiMap(data_start[1,1,2,1,1,,], lat = lat[1:12], lon = lon[1:40])



