#'@rdname DynBiasCorrection
#'@title Performing a Bias Correction conditioned by the dynamical
#'properties of the data.
#'
#'@author Carmen Alvarez-Castro, \email{carmen.alvarez-castro@cmcc.it}
#'
#'@description This function perform a bias correction conditioned by the 
#'dynamical properties of the dataset. This function used the functions 
#''CST_Predictability' to divide in terciles the two dynamical proxies 
#'computed with CST_ProxiesAttractor or ProxiesAttractor. A bias correction
#'between the model and the observations is performed using the division into
#'terciles of the local dimension 'dim' and inverse of the persistence 'theta'.
#'For instance, model values with lower 'dim' will be corrected with observed 
#'values with lower 'dim', and the same for theta. The function gives two options
#'of bias correction: one for 'dim' and one for 'theta'
#'
#'@references Faranda, D., Alvarez-Castro, M.C., Messori, G., Rodriguez, D., 
#'and Yiou, P. (2019). The hammam effect or how a warm ocean enhances large 
#'scale atmospheric predictability.Nature Communications, 10(1), 1316. 
#'DOI = https://doi.org/10.1038/s41467-019-09305-8 "
#'@references Faranda, D., Gabriele Messori and Pascal Yiou. (2017).
#' Dynamical proxies of North Atlantic predictability and extremes. 
#' Scientific Reports, 7-41278, 2017.
#'
#'@param pred.dim.mod output of CST_Predictability or a list of two lists 'qdim' and 'pos.d'. The 'qdim' list
#'contains values of local dimension 'dim' divided by terciles: 
#'d1: lower tercile and more predictability, 
#'d2: middle tercile, 
#'d3: higher tercile and less predictability
#'The 'pos.d' list contains the position of each tercile in parameter 'dim'
#'@param pred.theta.mod output of CST_Predictability or a list of two lists 'qtheta' and 'pos.t'. 
#'The 'qtheta' list contains values of the inverse of persistence 'theta' 
#'divided by terciles: 
#'th1: lower tercile and more predictability, 
#'th2: middle tercile, 
#'th3: higher tercile and less predictability
#'The 'pos.t' list contains the position of each tercile in parameter 'theta'
#'
#'@param pred.dim.obs same than pred.dim.obs but for the observations
#'
#'@param pred.theta.obs same than pred.theta.obs but for the observations
#'
#'@param ncores The number of cores to use in parallel computation
#'
#'@return dim.bias an s2dvcube object with a bias correction performed 
#'conditioned by local dimension 'dim'
#'
#'@return theta.bias an s2dvcube object with a bias correction performed 
#'conditioned by the inverse of the persistence 'theta'
#'
#'
#'@examples
#'# example 1: simple data s2dvcube style
#' expL <- rnorm(1:200)
#' dim(expL) <- c(member=10,lat = 4, lon = 5)
#' obsL <- c(rnorm(1:180),expL[1,,]*1.2)
#' dim(obsL) <- c(time = 10,lat = 4, lon = 5)
#' time_obsL <- paste(rep("01", 50), rep("01", 50), 1953 : 2003, sep = "-")
#' time_expL <- time_obsL[1]
#' lon <- seq(-1,5,1.5)
#' lat <- seq(30,35,1.5)
#' qm=0.60
#' expL <- s2dv_cube(data = expL, lat = lat, lon = lon, 
#'                   Dates = list(start = time_expL, end = time_expL))
#' obsL <- s2dv_cube(data = obsL, lat = lat, lon = lon,
#'                   Dates = list(start = time_obsL[1:10], end = time_obsL[1:10]))
#' attractor <- CST_ProxiesAttractor(data=obsL,quanti=qm)
#' predynObs = Predictability(dim=attractor$dim,theta=attractor$theta)
#' attractorMod<- CST_ProxiesAttractor(data=expL,quanti=qm)
#' predynMod = CST_Predictability(dim=attractorMod$dim,theta=attractorMod$theta)
#' dynbias= DynBiasCorrection(pred.dim.obs=predynObs$pred.dim,pred.theta.obs=predynMod$pred.theta,
#                            pred.dim.mod=predynObs$pred.dim,pred.theta.mod=predynMod$pred.theta)
#'@export
DynBiasCorrection<- function(pred.dim.obs,pred.theta.obs,pred.dim.mod,
                             pred.theta.mod,ncores=NULL){
  if (is.null(pred.dim.mod)) {
    stop("Parameter 'pred.dim.mod' is mandatory")
  }  
  if (is.null(pred.dim.obs)) {
    stop("Parameter 'pred.dim.obs' is mandatory")
  }    
  if (is.null(pred.theta.mod)) {
    stop("Parameter 'pred.dim.mod' is mandatory")
  }  
  if (is.null(pred.theta.obs)) {
    stop("Parameter 'pred.dim.obs' is mandatory")
  }
  dim.bias <- theta.bis <- NULL   
  return(list(dim.bias=dim.bias,theta.bias=theta.bias))
}
