
#' Training 
#'@param var_name 
#'@return training 
#'@useDynLib CSTools
#'@export 

mytraining <- function(var_name) {

#if (!is.loaded('training')) { 
#    dyn.load("/home/sclim/cle/cstools/src/training.so") 

# Input data paths
pathSI <- "/home/sclim/cle/regionalizacion/asun/eraint/datosAscii_SI/"
SIlength <- nchar(pathSI)
pathLR <- "/home/sclim/cle/regionalizacion/asun/eraint/datosAscii_LR/"
LRlength <- nchar(pathLR)
# High observation data path
obspath <- "/MASIVO/cle/datos/REJ5/"
obslength <- nchar(obspath)
# Output data path
pathsig <- "/home/sclim/cle/cstools/src/pred_sig/"
siglength <- nchar(pathsig)
# Number of analog situations to consider
nanx <- 155
# neni: total number of reanalysis grid-points that have been used in the 
# selection of the significant predictors. It may vary depending on:
# 1.- the selected method (next neibourgh or weighted 4 points interpolation)
# 2.- the reanalysis resolution  
neni <- 48

# ERAInterim grid parameters
# ngridd=((2*nlat)-1)*((2*nlon)-1) (LR grid)
ngridd <- 14076
nlat <- 18
nlon <- 30
ic <- nlat*nlon
nlatt <- 25 
nlont <- 37
id <- nlatt*nlont
slat <- 55.5
slon <- "-28.5"
rlat <- "-1.5"
rlon <- 1.5
slatt <- 63.
slont <- "-36."

# nd: number of training days
# nm: number of estimated days
# f.i, nm = 36600 -> regionalization of 100 years daily data 
nd <- 200

  predSig <- .Fortran("training", 
                      varR = var_name,
                      pathSI = pathSI,
                      SIlength = as.integer(SIlength), 
                      pathLR = pathLR,
                      LRlength = as.integer(LRlength), 
                      obspath = obspath,    
                      obslength = as.integer(obslength), 
                      pathsig = pathsig,
                      siglength = as.integer(siglength), 
                      nanx = as.integer(nanx),
                      neni = as.integer(neni),
                      ngridd = as.integer(ngridd),
                      nlat = as.integer(nlat),
                      nlon = as.integer(nlon),
                      ic = as.integer(ic),
                      nlatt = as.integer(nlatt),
                      nlont = as.integer(nlont),
                      id = as.integer(id),
                      slat = as.numeric(slat),
                      slon = as.numeric(slon),
                      rlat = as.numeric(rlat),
                      rlon = as.numeric(rlon),
                      slatt = as.numeric(slatt),
                      slont = as.numeric(slont),
                      nd = as.integer(nd),
                      PACKAGE = 'CSTools') 
}
              
                      


#                                   obs = obs,
#                                  hr_obs = hr_obs,
#                                  nlat = nlat,
#                                  nlon = nlon, 
#				  ic = ic,
#                                  nlatt = nlatt,
#                                  nlont = nlont,
#                                  id = id,
#                                  slat = slat,
#                                  slon = slon, 
#				  nger = nger,
#                                  nm = nm,
#				  nd = nd,
# 				  obs_res,
#                                  obs_ngrid,
#                                  hr_res,
                                  
#  # check inputs
#  # check exp
#  # check exp_lat
## completar los checks (depende de lo exigente que seamos con la funcion)
#  if (!is.numeric(exp_lat)) {
#    stop("Parameter 'exp_lat' must be a numeric vector.")
#  }
#  if (length(exp_lat) < 2) {
#    stop("Parameter 'exp_lat' must be a vector with multiple values.")
#  }
## En esta parte calculamos todo lo que nuestra funcion necesite como argumento
#  exp_res <- c(exp_lat, NA) - c(NA, exp_lat)
#  exp_res <- exp_res[!is.na(exp_res)]
#  if (!all(exp_res == exp_res[1])) {
#    stop("Parameter 'exp_lat' must be in regular grid.")
#  } else {
#    exp_res <- exp_res[1]
#  }
#  nlat <- (exp_lat[length(exp_lat)] - exp_lat[1]) / exp_res
#  nlon <- (exp_lon[length(exp_lon)] - exp_lon[1]) / exp_res
#  ic <- nlat * nlon
#  extra_nw_dom <- 7.5 
#  extra_se_dom <- 3 
#  dif_grids <- extra_nw_dom+extra_se_dom
#  nlatt <- nlat + (dif_grids / exp_res)
#  nlont <- nlon + (dif_grids / exp_res)
#  id <- nlatt * nlont
#  slat <- max(exp_lat)
#  slon <- exp_lon[1] # if -180 : 180
#  slon <- min(c(exp_lon[which(exp_lon > 180)] - 360, 
#                exp_lon[which(exp_lon <= 180)])) # if lon 0 :360
#
#  if (any(names(dim(obs_psl)) == 'time')) {
#    time_dim <- which(names(dim(obs_psl)) == 'time')
#  } else if (any(names(dim(obs_psl)) == 'sdate')) {
#    if (any(names(dim(obs_psl)) == 'ftime')) {
#        obs_psl <- MergeDims(obs_psl, c('ftime', 'sdate'), rename_dim = 'time')
#        time_dim <- which(names(dim(obs_psl)) == 'time')
#  }
#  if (any(names(dim(obs_psl)) == 'gridpoint')) {
#    gp_dim <- which(names(dim(obs_psl)) == 'gridpoint')
#  } else if (any(names(dim(obs_psl)) == 'lat')) {
#    if (any(names(dim(obs_psl)) == 'lon')) {
#        obs_psl <- MergeDims(obs_psl, c('lat', 'lon'), rename_dim = 'gridpoint')
#        gp_dim <- which(names(dim(obs_psl)) == 'gridpoint')
#  }  
  
#  nm <- exp$time 
#  nd <- obs$time 
  
# completar los argumentos de la funcion training (basado en los programas de fortran)
# y ver la manera de obtenerlos de la informacion que nos pasa la funcion AnalogPredictors.
# Una vez que tenga todo claro, ir probandolo en R a ver si funciona (habria que hacerlo con el
# dyn.load como habia hecho la prueba).



