library(startR)
library(s2dverification)

# Start
path_exp <- '/esarchive/exp/ecmwf/system5_m1/daily_mean/$var$_f6h/$var$_$sdate$.nc'
variable <- 'psl'

lats.min <- 27
lats.max <- 81
lons.min <- -85.5
	lons.min <- 275
lons.max <- 45

dim_params <- list(dat = list(list(path=path_exp)),
              var = variable,
              member = 1,
              sdate = '19821201',
              time = indices(1:30),
              latitude = values(list(lats.min, lats.max)),
              longitude = values(list(lons.min, lons.max)),
              latitude_reorder = Sort(),
              longitude_reorder = CircularSort(0, 360))
hcst <- Start(dat = list(list(path=path_exp)),
              var = variable,
              member = 1,
              sdate = '19821201',
              time = indices(1:30),            
              latitude = values(list(lats.min, lats.max)),
              longitude = values(list(lons.min, lons.max)),
              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)
	      retrieve = FALSE)
all(attributes(hcst)$Variables$dat1$longitude == c(0:45, 275:359))
dim(hcst)
#      dat       var    member     sdate      time  latitude longitude 
#        1         1         1         1        30        55       132 
attributes(hcst)$Files 
#"/esarchive/exp/ecmwf/system5_m1/daily_mean/psl_f6h/psl_19821201.nc"
PlotEquiMap(hcst[,,,,1,,c(41:132, 1:39)], lon = attributes(hcst3)$Variable$dat1$longitude,
            lat = attributes(hcst)$Variable$dat1$latitude, filled.continents = F)

PlotEquiMap(hcst3[,,,,1,,c(41:131, 1:40)], lon = attributes(hcst3)$Variable$dat1$longitude,
            lat = attributes(hcst)$Variable$dat1$latitude, filled.continents = F)

# LOAD:
path <- list(list(name = 'system5', path = '/esarchive/exp/ecmwf/system5_m1/daily_mean/$VAR_NAME$_f6h/$VAR_NAME$_$START_DATE$.nc'))
data <- Load(var = variable, 
             exp = path, obs = NULL, sdates = '19821201', nmember = 1,
             nmemberobs = NULL, nleadtime = 30, leadtimemin = 1,
             leadtimemax = NULL, storefreq = "daily", sampleperiod = 1,
             lonmin = lons.min, lonmax = lons.max, latmin = lats.min, latmax = lats.max,
             output = "lonlat", method = "conservative", grid = 'r360x181',
             maskmod = vector("list", 15), maskobs = vector("list", 15),
configfile = NULL, varmin = NULL, varm
ax = NULL, silent = FALSE,
nprocs = NULL, dimnames = NULL, remapcells = 2,
path_glob_permissive = "partial")

data$source_files
#[1] "/esarchive/exp/ecmwf/system5_m1/daily_mean/psl_f6h/psl_19821201.nc"

dim(data$mod)
#dataset  member   sdate   ftime     lat     lon 
#      1      25       1      30      55     131 

all(data$lon == c(0:45, 275:359))

all(data$mod[1,1,1, , , ] == hcst[1,1,1,1, , ,1:131]) # which is the extra longitude value?
dev.new()
PlotEquiMap(data$mod[1, 1, 1, 1, ,], lon = data$lon, lat = data$lat, filled.continents = F)
range(data$mod[1, 1, 1, 1, , ])
range(hcst[1, 1, 1, 1, 1, , ])
