#inverted latitudes?
library(s2dverification)
sdates.test <-  paste0(c(1981:1982), '0601')
data   <- Load('tos', exp=NULL, obs='hadisst_v1.1' ,
              sdates.test ,
               grid = 't170grid',
              method='bilinear', 
              leadtimemin = 1,
              leadtimemax = 8,
              output = 'lonlat',
			  nprocs=1)
PlotEquiMap(data$obs[1,1,1,1,,], lat = data$lat, lon = data$lon, filled.con = F)

lon <- data$lon
lat <- data$lat[length(data$lat):1]
obs <- s2dverification::Subset(data$obs, along = 'lat',
             indices = length(data$lat):1)

```
PlotEquiMap(obs[1,1,1,1,,], lat = lat, lon = lon, filled.con = F)



# Delete previous data 
rm(list=ls())
gc()
library(boot)
library(s2dv)
library(s2dverification)
library(startR)
library(ncdf4)
library(multiApply)
library(SpecsVerification)
# ATL
lonmin <- -179
lonmax <-  179
latmin <- -60
latmax <-  50
sdates <-  paste0(c(1981:2018), '0601')
# Nudg-var - 2018
repos_a2my  <-  paste0('/esarchive/exp/ecearth/a2my/monthly_mean/',
                 '$var$_f3h/$var$_Omon_EC-Earth3_WND_',
                 'S$sdate$_$member$_$chunk$.nc'
                 )
print('loading a2my')
a2my <- Start(dat = repos_a2my,
             var = 'tos',
             member = 'all',
             sdate = sdates,
             chunk = 'all',
          #   time = 'all',
             time = indices(1:8),  #first time step per day
             chunk_depends = 'sdate',
             time_across = 'chunk',
             merge_across_dims = TRUE,
             lat = values(list(latmin, latmax)),
             lat_reorder = Sort(), ##1
             lon = values(list(lonmin, lonmax)),
             lon_reorder = CircularSort(-180, 180), ##2
             synonims = list(lat = c('lat', 'latitude'),
                             lon = c('lon', 'longitude')),
             return_vars = list(lon = 'dat',  ##3
                                lat = 'dat',  ##3
                                time = 'sdate'),
             retrieve = F)
##lons <- attr(exp, 'Variables')$common$tos$dim[[1]]$vals
##lats <- attr(exp, 'Variables')$common$tos$dim[[2]]$vals
lons <- as.vector(attr(a2my, 'Variables')$dat1$lon)
lats <- as.vector(attr(a2my, 'Variables')$dat1$lat)
dates <- attr(a2my, 'Variables')$common$time
dates_file <- sort(unique(gsub('-', '', sapply(as.character(dates), substr, 1, 7))))
sdates.test <-  paste0(c(1981:1982), '0601')
obs.data2.test   <- s2dv::Load('tos', exp=NULL, obs='hadisst_v1.1' 
              ,sdates.test 
              , grid = 't170grid'
              ,method='bilinear', 
              ,leadtimemin = 1
              ,leadtimemax = 8
              ,output = 'lonlat'
			 # ,configfile='/esarchive/scratch/eexarcho/Eleftheria/conf_files_s2dverification/BSC_v2.8.2.conf'
			  ,nprocs=1
			  ,latmin = latmin,latmax = latmax,
			  ,lonmin = lonmin,lonmax = lonmax,
			  )
PlotEquiMap(obs.data2.test$obs[1,1,1,1,,], lon = obs.data2.test$lon,
            lat = obs.data2.test$lat, filled.cont = F)
obs.data2.test2 <- s2dverification::Subset(obs.data2.test$obs, along = 'lat',
                  indices = length(obs.data2.test$lat):1)
PlotEquiMap(obs.data2.test2[1,1,1,1,,], lon = obs.data2.test$lon,
            lat = obs.data2.test$lat[157:1], filled.cont = F)

repos_exp <-  paste0('/esarchive/scratch/eexarcho/Eleftheria/TRIATLAS/Analysis_a33d_a33e_a33f/',
                       'Data/a33d/tos/', 
                      'tos_Amon_EC-Earth3-CC_historical_S$sdate$_$member$_gr_$chunk$.nc'
				          )
sdates <-  paste0(c(2005:2006), '0501')
exp <- Start(dat = repos_exp,
             var = 'tos',
             member = 'all',
             sdate = sdates,
             chunk = 'all', 
             time = 'all',
             chunk_depends = 'sdate',
             time_across = 'chunk',
             merge_across_dims = TRUE,
             lat = 'all',
             lon = 'all',
             #lat = values(list(20, 30)),
             #lat_reorder = Sort(decreasing = T),
             #lon = values(list(20, 30)),
             #lon_reorder = CircularSort(0, 360),
             #transform = CDORemapper,
             #transform_extra_cells = 2,
             #transform_params = list(grid = 'r360x180',
             #                        method = 'conservative',
             #                        crop = c(20, 30, 20, 30)),
             #transform_vars = c('lat', 'lon'),
             synonims = list(lat = c('lat', 'latitude'),
                             lon = c('lon', 'longitude')),
             return_vars = list(lon = NULL,
                                lat = NULL,
                                time = 'sdate'),
             retrieve = T)
lon <- attributes(exp)$Variables$common$lon
lat <- attributes(exp)$Variables$common$lat
s2dv::PlotEquiMap(exp[1,1,1,1,1,,], lon = lon, lat = lat, filled.continents = F)
