library(lubridate)
library(startR)
library(s2dverification)
library(multiApply)

obs_path <- '/esarchive/recon/ecmwf/era5/monthly_mean/$var$_f1h/$var$_$sdate$.nc'
pobs <- paste0('/esarchive/recon/ecmwf/era5/monthly_mean/',
               '$VAR_NAME$_f1h/$VAR_NAME$_$YEAR$$MONTH$.nc')
y1 <- 1993
y2 <- 1995
var_name<-'sfcWind'

lons.min <- 0
lons.max <- 360
lats.min <- -90
lats.max <- 90

#-------------------------------
# Interpolation
#-------------------------------

obs <- Start(dat = obs_path,
             var = var_name,
#             sdate = paste0(y1:y2, '12'),
             sdate = '201811',
             time = 'all',
             latitude = values(list(lats.min, lats.max)),
             latitude_reorder = Sort(),
             longitude = values(list(lons.min, lons.max)),
             synonims = list(longitude = c('lon', 'longitude'),
                             latitude = c('lat', 'latitude')),
             transform = CDORemapper,
             transform_params = list(grid = 'r360x181',
                                     method = 'conservative',
                                     crop = c(lons.min, lons.max, lats.min, lats.max)),
             transform_vars = c('latitude', 'longitude'),
             return_vars = list(time = NULL,
                                latitude = 'dat',
                                longitude = 'dat'),
             split_multiselected_dims = TRUE,
             merge_across_dims = TRUE,
             num_procs = 4,
             retrieve = T)

# load + interpolate data s2dverification
obs1 <- Load(var = var_name,
             obs = list(list(path = pobs)),
             sdates = '20181101',
#             sdates = paste0(y1:y2, '1201'),
             leadtimemin = 1,
             leadtimemax = 1,
             output = 'lonlat',
             grid = 'r360x181',
             method = 'conservative',
             storefreq = 'monthly',
             latmin = lats.min,
             latmax = lats.max,
             lonmin = lons.min,
             lonmax = lons.max)

#--Start-------------------------------------------------------
dim(obs)
#      dat       var     sdate      time  latitude longitude 
#        1         1         1         1       181       360 
range(obs)
#[1]  0.7421155 13.8849268
str(attr(obs, 'Variables')$common[[var_name]]$dim[[1]])
# $ name         : chr "longitude"
# $ vals         : num [1:360(1d)] 0 1 2 3 4 5 6 7 8 9 ...
str(attr(obs, 'Variables')$common[[var_name]]$dim[[2]])
# $ name         : chr "latitude"
# $ vals         : num [1:181(1d)] -90 -89 -88 -87 -86 -85 -84 -83 -82 -81 ...
obs[1,1,1,1,1:10,1]
# [1] 6.018566 5.994382 6.071358 6.440867 6.190617 5.967206 6.078894 6.252083
# [9] 5.850513 6.541878
obs[1,1,1,1,172:181,1]  #lat 81-90
# [1] 8.709892 7.824319 7.057877 6.296812 5.628533 5.132293 5.027134 5.121124
# [9] 4.966911 4.959648


#--Load--------------------------------------------------------
dim(obs1$obs)
#dataset  member   sdate   ftime     lat     lon 
#      1       1       1       1     181     360 
range(obs1$obs)
#[1]  0.74212 13.88500
str(obs1)
# $ lon            : num [1:360(1d)] 0 1 2 3 4 5 6 7 8 9 ...
# $ lat            : num [1:181(1d)] 90 89 88 87 86 85 84 83 82 81 ...
obs1$obs[1,1,1,1,1:10,1]  #lat 90-81
# [1] 4.9596 4.9669 5.1211 5.0271 5.1323 5.6285 6.2968 7.0579 7.8243 8.7099

diff <- drop(obs1$obs) - drop(obs)[181:1,]
range(diff)
#[1] -0.0004999390  0.0004998016


## use: cdo remapcon,r360x181 sfcWind_201811.nc sfcWind_201811_regrid.nc
library(easyNCDF)
  path <- '/home/Earth/aho/aho-testtest/startR/issue/issue37/sfcWind_201811_regrid.nc'
  fnc <- NcOpen(path)
  fnc_dims <- NcReadDims(fnc)
  fnc_arr <- NcToArray(fnc, vars_to_read = var_name)
  dim(fnc_arr)
# var  lon  lat time 
#   1  360  181    1 
str(attr(fnc_arr, 'variables')[[var_name]]$dim[[1]])
# $ name         : chr "lon"
# $ vals         : num [1:360(1d)] 0 1 2 3 4 5 6 7 8 9 ...
str(attr(fnc_arr, 'variables')[[var_name]]$dim[[2]])
# $ name         : chr "lat"
# $ vals         : num [1:181(1d)] -90 -89 -88 -87 -86 -85 -84 -83 -82 -81 ...
range(fnc_arr)
# [1]  0.7421154 13.8849262
fnc_arr[1, 1, 172:181, 1] 
# [1] 8.709892 7.824319 7.057878 6.296811 5.628532 5.132293 5.027134 5.121124
# [9] 4.966911 4.959648  #same as obs

fnc_arr2 <- s2dverification:::.aperm2(fnc_arr, c(4,3,2,1))
diff2 <- drop(fnc_arr2) - drop(obs)
range(diff2)
#[1] -2.848792e-05  2.306064e-05
diff3 <- drop(fnc_arr2)[181:1, ] - drop(obs1$obs)
range(diff3)
#[1] -0.0004997562  0.0004998590


#--------------------------------
# No interpolation 
#-------------------------------

obs_start <- Start(dat = obs_path,
                   var = var_name,
                   sdate = paste0(y1:y2, '12'),
                   time = 'all',
                   latitude = values(list(lats.min, lats.max)),
                   latitude_reorder = Sort(),
                   longitude = values(list(lons.min, lons.max)),
                   synonims = list(longitude = c('lon', 'longitude'),
                                   latitude = c('lat', 'latitude')),
                   return_vars = list(time = NULL,
                                      latitude = 'dat',
                                      longitude = 'dat'),
                   split_multiselected_dims = TRUE,
                   merge_across_dims = TRUE,
                   num_procs = 4,
                   retrieve = T)

pobs <- paste0('/esarchive/recon/ecmwf/era5/monthly_mean/',
               '$VAR_NAME$_f1h/$VAR_NAME$_$YEAR$$MONTH$.nc')

obs_s2dv <- Load(var = var_name,
                 obs = list(list(path = pobs)),
                 sdates = paste0(y1:y2, '1201'),
                 leadtimemin = 1,
                 leadtimemax = 1,
                 output = 'lonlat',
                 storefreq = 'monthly',
                 latmin = lats.min,
                 latmax = lats.max,
                 lonmin = lons.min,
                 lonmax = lons.max)

range(obs_start)
range(obs_s2dv$obs)
diff1 <- drop(obs_start) - drop(obs_s2dv$obs)[, 36:1, ]

