#'@rdname examples CST_Analogs
#'@title examples of the Downscaling using Analogs based on large scale fields.
#' 
#'@author Carmen Alvarez-Castro, \email{carmen.alvarez-castro@cmcc.it}
#'@author Nuria Perez-Zanon \email{nuria.perez@bsc.es}
#'        
#'@description examples of CST_Analogs and Analogs
#'rm(list=ls())
#'require(s2dverification)
#'require(CSTools)
#'require(multiApply)
#'@example1
#configfile
# configfile <- "/Library/Frameworks/R.framework/Versions/3.6/Resources/
#library/s2dverification/config/new2.conf"
# ConfigFileCreate(configfile, confirm = FALSE)
# c <- ConfigFileOpen(configfile)
# c <- ConfigEditDefinition(c, 'DEFAULT_VAR_MIN', '-1e19', confirm = FALSE)
# c <- ConfigEditDefinition(c, 'DEFAULT_VAR_MAX', '1e19', confirm = FALSE)
# data_path <- "/Users/greenbird/Documents/ecmwf"
# exp_data_path <- paste0(data_path, '/experimentA/')
# obs_data_path <- paste0(data_path, '/observationA/')
# c <- ConfigAddEntry(c, 'experiments', dataset_name = 'experimentA', 
#                     var_name = 'msl', main_path = exp_data_path,
#                     file_path = '$STORE_FREQ$_mean/$VAR_NAME$_3hourly/
# $VAR_NAME$_$START_DATE$.nc')
# c <- ConfigAddEntry(c, 'observations', dataset_name = 'observationA', 
#                     var_name = 'msl', main_path = obs_data_path,
#                     file_path = '$STORE_FREQ$_mean/$VAR_NAME$/
# $VAR_NAME$_$START_DATE$.nc')
# ConfigFileSave(c, configfile, confirm = FALSE)

startDates <- c('20170501')
#Large scale
c(expL, obsL) %<-% (
  CST_Load('msl', c('experimentA'), c('observationA'), startDates, 
           output = 'areave',grid='r122x122',latmin = 30, latmax = 40, 
           lonmin = -5, lonmax = 20, configfile = configfile))
gridL <- length(expL$lon)*length(expL$lat)
exp <- expL$data[1:(5*gridL)]
obs <- obsL$data[1:(5*gridL)]
expL$data <- expL$data[1,1,1,1:(5*gridL)]
dim(expL$data) <- c(dataset = 1, member = 1,time = 5, lat = length(expL$lat), 
                    lon = length(expL$lon))
obsL$data <- obsL$data[1,1,1,1:(5*gridL)]
dim(obsL$data) <- c(dataset = 1, member = 1,time = 5, lat = length(expL$lat), 
                    lon = length(expL$lon))

#local scale
c(expVar, obsVar) %<-% (
  CST_Load('msl', c('experimentA'), c('observationA'), startDates, 
           output = 'areave',
           grid='r122x122',latmin = 34, latmax = 38, lonmin = -2, lonmax = 2, 
           configfile = configfile))
gridVar <- length(expVar$lon)*length(expVar$lat)
exp <- expVar$data[1:(5*gridVar)]
obs <- obsVar$data[1:(5*gridVar)]
expVar$data <- expVar$data[1,1,1,1:(5*gridVar)]
dim(expVar$data) <- c(dataset = 1, member = 1,time = 5, 
                      lat = length(expVar$lat),lon = length(expVar$lon))
obsVar$data <- obsVar$data[1,1,1,1:(5*gridVar)]
dim(obsVar$data) <- c(dataset = 1, member = 1,time = 5, 
                      lat = length(expVar$lat), lon = length(expVar$lon))
#CST_Analogs
#test <- CST_Analogs(expL,obsL,expVar,obsVar,criteria = "Large_dist")
# test <- CST_Analogs(expL,obsL,expVar,obsVar,criteria = "Large_dist")
# Error in names(dim(metric1)) <- `*vtmp*` : 
#   attempt to set an attribute on NULL
# In addition: Warning message:
#   In Analogs(expL$data, obsL$data, time_obsL = timevector, 
expVar = expVar$data,  :
#                
#                Error in names(dim(metric1)) <- `*vtmp*` : 
#                attempt to set an attribute on NULL 

#'@example2
expL <- lonlat_data$exp
obsL <- lonlat_data$obs
dim(obsL$data) <- c(dataset = 1, member = 1, time = 18, lat = 22, lon = 53)
dim(expL$data) <- c(dataset = 1, member = 15, time = 18, lat = 22, lon = 53)
expVar=lonlat_prec
expVar$data=expVar$data*86400*1000
obsVar=lonlat_prec
obsVar$data=obsVar$data*86400*1000*56
dim(obsVar$data) <- c(dataset = 1, member = 31, time = 18, lat = 4, lon = 4)
dim(expVar$data) <- c(dataset = 1, member = 31, time = 18, lat = 4, lon = 4)
downscaled_largedist <- CST_Analogs(expL,obsL,expVar,obsVar,
                                    criteria="Large_dist")
#Error in `[.default`(time_obsL, best) : invalid subscript type 'list' 
downscaled_localdis <- Analogs(expL,obsL,expVar,obsVar,criteria="Local_dist")
# Error in Analogs(expL, obsL, expVar, obsVar, criteria = "Local_dist") : 
#   Parameter 'expL' must have the dimensions 'lat' and 'lon'.
downscaled_localcor <- Analogs(expL,obsL,expVar,obsVar,criteria="Local_cor")
# Error in Analogs(expL, obsL, expVar, obsVar, criteria = "Local_cor") : 
#   Parameter 'expL' must have the dimensions 'lat' and 'lon'.

#' @example3
# Creating experimental data
exp <- 1 : (1 * 1 * 4 * 8 * 8)*16
dim(exp) <- c(dataset = 1, member = 1, sdate = 1, ftime = 4, lat = 8, lon = 8)
lon <- seq(10, 13.5, 0.5)
dim(lon) <- c(lon = length(lon))
lat <- seq(40, 43.5, 0.5)
dim(lat) <- c(lat = length(lat))
Dates=list()
Dates$start=c("20170501")
Dates$end=c("20170505")
expL <- list(data = exp, lon = lon, lat = lat, Dates=Dates)
# Creating observations data
obs <- 1 : (1 * 1 * 4 * 8 * 8) *14
dim(obs) <- c(dataset = 1, member = 1, sdate = 1, ftime = 4, lat = 8, lon = 8)
lon <- seq(10, 13.5, 0.5)
dim(lon) <- c(lon = length(lon))
lat <- seq(40, 43.5, 0.5)
dim(lat) <- c(lat = length(lat))
Dates=list()
Dates$start=c("20170501")
Dates$end=c("20170505")
obsL <- list(data = obs, lon = lon, lat = lat, Dates=Dates)
# Downscaling
downscaled_largedist <- CST_Analogs(expL=expL,obsL=obsL,expVar=expL,
                                    obsVar=obsL,criteria="Large_dist")
# Error in dim(newX) <- c(prod(d.call), d2) : 
#   dims [product 1] do not match the length of object [16] 