# cd /esarchive/scratch/allabres/gitlab_clones/csdownscale
# git pull origin dev-Rpackage

library(s2dv)
library(CSTools)
library(FNN)
library(multiApply)
source('/esarchive/scratch/jramon/GitLab_jramon/downscaling/csdownscale/R/Analogs.R')
source('/esarchive/scratch/jramon/GitLab_jramon/downscaling/csdownscale/R/Utils.R')
source('/esarchive/scratch/jramon/GitLab_jramon/downscaling/csdownscale/R/Interpolation.R')
source('/esarchive/scratch/jramon/GitLab_jramon/downscaling/csdownscale/R/Intbc.R')
source('/esarchive/scratch/jramon/GitLab_jramon/downscaling/csdownscale/R/Intlr.R')

### load data and transform to s2dvcube-----------------------------------------------------------------------
    var <- 'tas'

    year_in <- 1993
    year_fi <- 2016
    mm <- '05'
    fcst_year <- 0 # 0 if we are only interested in the hindcast; otherwise: year that we wish to calibrate
    leadtime <- 0 # (forecast date = start date + leadtime; leadtime = 0 -> same forecast date as start date)

    ref_system <- 'ERA5-Land'
    fcst_system <- 'ECMWF-s5'

    aggregation <- 'daily' # 'seasonal', 'monthly', 'daily'
    interpolate_observations <- FALSE #interpolate at load with startR
    load_selection <- 'both' #(could be 'exp', 'obs' or 'both')
    circularlons <- FALSE # TRUE
    load_as_s2dvcube <- TRUE # if TRUE will create a s2dv_cube object (a part from loading exp and/or obs and/or fcst as startR_array)
    clean_data <- TRUE # remove unnecessary R objects once data is loaded as s2dv (only used when lad_as_s2dvcube <- TRUE)

    region <- 'user_defined' #options: 'user_defined' (define lat/lon min/max before (or after, before is ok) calling module_conf.R), 'LAC' (Latin America and Caribbean), 'A' (Argentina), 'B' (Brazil), 'C' (Caribbean)
    lons.min <- 306 #-54
    lons.max <- 316 #-44
    lats.min <- -4
    lats.max <- 2

    # to load data (already loaded and save as data_CAMETA_for_tests_positivelons.RData)
 #   code_directory <- '/home/Earth/allabres/Documents/gitlab_projects/alba/Postprocessing_Workflow/'
 #   source(paste0(code_directory, 'module_conf.R')) # will create: 'fcst_path', 'fcst_sys', 'lats.max', 'lats.min', 'lons.max', 'lons.min', 'month_name', 'ref_path', 'ref_sys', 'region_name', 'selected_dates', 'workflow_conf'
 #   source(paste0(code_directory, 'module_load.R')) # will create: 'dates', 'exp', 'exp_fcst', 'lats.hcst', 'lats.ref', 'lons.hcst', 'lons.ref', 'obs', 'workflow_load', 'ref.path', 'hcst.path', 'fcst.path'
                                                    # will remove (if clean_data == TRUE & load_as_s2dvcube == TRUE): fcst_path, workflow_conf, workflow_load, ref.path, hcst.path, fcst.path, dates, exp, exp_fcst, lats.hcst, lats.ref, lons.hcst, lons.ref, obs
    ### END load data ----------------------------------------------------------------------------------------

### save data for future tests -------------------------------------------------------------------------------
    
    output_dir <- '/esarchive/scratch/allabres/review_csdownscale/'
    #save(var, region, region_name, selected_dates, leadtime, year_in, year_fi, fcst_sys, ref_system, loaded_data_hcst_s2dv, loaded_data_ref_s2dv, 
    #                                            file = paste0(output_dir, 'data_CAMETA_for_tests_circularlons.RData')) # first test issue with negative lons:
                                                                        #      cdo    sellonlatbox: Processed 1 variable over 216 timesteps [69.99s 348MB].
                                                                        #      cdo    invertlat: Processed 1 variable over 216 timesteps [0.71s 348MB].
                                                                        #      Error in latlon2ij(griddes, lats, lons) :
                                                                        #      Please use the same convention for the latitudes in the source file and the longitude values in 'points'.
  #  save(var, region, region_name, selected_dates, leadtime, year_in, year_fi, fcst_sys, ref_system, loaded_data_hcst_s2dv, loaded_data_ref_s2dv, file = paste0(output_dir, 'data_CAMETA_for_tests_positivelons.RData'))
    
## test Intlr to a point -------------------------------------------------------------------------------------`

    load(paste0(output_dir,  'data_CAMETA_for_tests_positivelons.RData'))
    # loaded_data_hcst_s2dv$source_files <- paste0(loaded_data_hcst_s2dv$source_files, var, '_', selected_dates[1], '.nc') # needed because otherwise: ERROR:
                                                                                                                        #    cdo    sellonlatbox: Open failed on >/esarchive/exp/ecmwf/system5c3s/daily_mean/tas_f6h/<
                                                                                                                        #                    Is a directory
                                                                                                                        #Error in if (griddes$gridtype == "gaussian") { :
                                                                                                                        #argument is of length zero
    # loaded_data_ref_s2dv$source_files <- paste0(loaded_data_ref_s2dv$source_files, '/', var, '_', substr(selected_dates[1],1,6), '.nc') # defined in source_file_exp/obs function parameter
    loaded_data_hcst_s2dv$source_files <- paste0(output_dir, 'source_file_exp.nc')
    loaded_data_ref_s2dv$source_files <- paste0(output_dir, 'source_file_obs.nc')

    library(plyr) # for laply
    test_intlr <- CST_Intlr(exp = loaded_data_hcst_s2dv, 
                            obs = loaded_data_ref_s2dv, 
                            lr_method = 'basic', 
                            points = list(lat = -2.2, lon = 310.5), 
                            int_method = 'bil', 
                            method_point_interp = 'bilinear', 
                            sdate_dim = 'sdate',
                            region = c(loaded_data_ref_s2dv$lon[1],
                                       loaded_data_ref_s2dv$lon[length(loaded_data_ref_s2dv$lon)],
                                       loaded_data_ref_s2dv$lat[length(loaded_data_ref_s2dv$lat)],
                                       loaded_data_ref_s2dv$lat[1]
                                        ), 
                            ncores = 1
    )

#
# ERROR:    
# cdo    sellonlatbox: Processed 1 variable over 216 timesteps [63.86s 335MB].
# cdo    invertlat: Processed 1 variable over 216 timesteps [0.59s 335MB].
# cdo    sellonlatbox: Open failed on >/esarchive/recon/ecmwf/era5land/daily_mean/tas_f1h<
#                     Is a directory
# Error in if (griddes$gridtype == "gaussian") { :
#   argument is of length zero
# In addition: Warning message:
# In system(paste0("cdo griddes ", ncfile, " 2>/dev/null | egrep 'gridtype|xsize|ysize|xfirst|xinc|yfirst|yinc'"),  :
#   running command 'cdo griddes /esarchive/recon/ecmwf/era5land/daily_mean/tas_f1h_cropped 2>/dev/null | egrep 'gridtype|xsize|ysize|xfirst|xinc|yfirst|yinc'' had status 1
#

# test with CST_Load:
   # source("https://earth.bsc.es/gitlab/nperez/Flor/-/raw/master/CST_Load_devel_from_s2dv.R")
   #
   # path_ERA5prlr_CDS <- list(path = '/esarchive/recon/ecmwf/era5land/$STORE_FREQ$_mean/$VAR_NAME$_f1h/$VAR_NAME$_$YEAR$$MONTH$.nc')
   # path_system5_CDS <- list(path = '/esarchive/exp/ecmwf/system5c3s/$STORE_FREQ$_mean/$VAR_NAME$_f6h/$VAR_NAME$_$START_DATE$.nc')
   # sdates <- paste0(year_in:year_fi, '0101')
   #
   # library(zeallot)
   # c(loaded_exp, loaded_obs) %<-% CST_Load_s2dv(var = var, 
   #                             exp = list(path_system5_CDS), 
   #                             obs = list(path_ERA5prlr_CDS), 
   #                             sdates = sdates,
   #                             lonmax = lons.max, lonmin = lons.min,
   #                             latmax = lats.max, latmin = lats.min, 
   #                             storefreq = aggregation, 
   #                             leadtimemin = min(leadtime) + 1, leadtimemax = max(leadtime) + 1, 
   #                             output = "lonlat") # THERE IS A REGRID OF OBS BEFORE LOADING !!!!
   # save(loaded_exp, loaded_obs, file = paste0(output_dir, 'data_CAMETE_for_test_loaded_with_CST_Load_s2dv.RData'))

    load(paste0(output_dir, 'data_CAMETE_for_test_loaded_with_CST_Load_s2dv.RData'))
    test_intlr <- CST_Intlr(exp = loaded_exp, 
                                obs = loaded_obs, 
                                lr_method = 'basic',
                                points = list(lat = -2.2, lon = 310.5), 
                                int_method = 'bil', 
                                method_point_interp = 'bilinear', 
                                source_file_exp = loaded_exp$source_files[1], 
                                source_file_obs = loaded_obs$source_files[1],
                                region = c(loaded_obs$lon[1],
                                        loaded_obs$lon[length(loaded_obs$lon)],
                                        loaded_obs$lat[length(loaded_obs$lat)],
                                        loaded_obs$lat[1]
                                            ), 
                                ncores = 1
        )
    # ERROR:
    # cdo    sellonlatbox: Processed 1 variable over 216 timesteps [69.15s 346MB].
    # cdo    invertlat: Processed 1 variable over 216 timesteps [0.61s 346MB].
    # cdf_create        : ncid = -1  mode = 4352  chunksizehint = 0 file = /esarchive/recon/ecmwf/era5land/daily_mean/tas_f1h/tas_199301.nc_cropped
    #
    # Error (cdf_create): /esarchive/recon/ecmwf/era5land/daily_mean/tas_f1h/tas_199301.nc_cropped: Permission denied
    # Error in if (griddes$gridtype == "gaussian") { :
    #   argument is of length zero
    # In addition: Warning message:
    # In system(paste0("cdo griddes ", ncfile, " 2>/dev/null | egrep 'gridtype|xsize|ysize|xfirst|xinc|yfirst|yinc'"),  :
    #   running command 'cdo griddes /esarchive/recon/ecmwf/era5land/daily_mean/tas_f1h/tas_199301.nc_cropped 2>/dev/null | egrep 'gridtype|xsize|ysize|xfirst|xinc|yfirst|yinc'' had status 1
    #
