#'CST_Analogs examples and tests
#'@example
#'expL <- lonlat_data$exp
#'obsL <- lonlat_data$obs
#'dim(obsL$data) <- c(dataset = 1, member = 1, time = 18, lat = 22, lon = 53)
#'expVar <- lonlat_prec$exp
#'obsVar <- lonlat_prec$obs
#'#downscaledVar <- CST_Analogs(....)
#'
#'
#'@example
#'expL <- 1 : (8 * 10 * 2 * 6 * 7)
#'dim(expL) <- c(lat = 8, lon = 10, time = 2, member = 6, sdate = 7)
#'obsL <- 1 : (8 * 10 * 5)
#'dim(obsL) <- c(lat = 8, lon = 10, time = 5)
#'position <- Select(expL = expL, obsL = obsL)$position
#'dim(position)
#'pos <- BestAnalog(position)
#'pos
#'dim(pos)
#'res <- Apply(list(position), target_dims = c('time','pos') , fun = BestAnalog, criteria = 'Large_dist')$output1


#'lat_local <- lat <- seq(0, 19, 2.5)
#'lon_local <- lon <- seq(0, 23, 2.5)
#'position= Select(expL, obsL, criteria = "Local_dist", lon_local = lon, 
#'             lat_local = lat,
#'             region = c(lonmin = 0, lonmax = 5, latmin = 0, latmax = 5))$position
#'res <- Apply(list(position), target_dims = c('time','pos') , fun = BestAnalog, criteria = 'Local_dist')$output1
#'res <- Apply(list(position), target_dims = c('time', 'pos'), fun = BestAnalog, criteria = 'Large_dist')$output1
#'
#'#'@examples
#'exp <- (1  + 2): (4 * 3 + 2)
#'dim(exp) <- c(lat = 4, lon = 3)
#'obs <- 1 : c(5 * 4 * 3)
#'dim(obs) <- c(time = 5, lat = 4, lon = 3)
#'res <- .select(exp, obs)
#'res
#'res <- .select(exp, obs, metric = 'cor')
#'dim(res)
#'
#'@example
# expL <- (1  + 2): (4 * 3 * 2 + 2)
# dim(expL) <- c(lat = 4, lon = 3, time = 2)
# obsL <- 1 : c(4 * 3 * 5)
# dim(obsL) <- c(lat = 4, lon = 3, time = 5)
# res = Select(expL, obsL)
# expL <- (1  + 2): (8 * 10 * 2 + 2)
# dim(expL) <- c(lat = 8, lon = 10, time = 2)
# obsL <- 1 : c(8 * 10 * 5)
# dim(obsL) <- c(lat = 8, lon = 10, time = 5)
# lat_local <- lat <- seq(0, 19, 2.5)
# lon_local <- lon <- seq(0, 23, 2.5)
# res = Select(expL, obsL, criteria = "Local_dist", lon_local = lon, 
#              lat_local = lat,
#              region = c(lonmin = 0, lonmax = 5, latmin = 0, latmax = 5))
# probar mas ejemplos con diferentes criterios, latitudes, longitudes
#'@example
#'#This auxiliar function looks for replecated dimension names between two vectors.
#'# The repeated dimensions (different than 'lon' and 'lat') will be replaced in the first 
#'# vector with the same name and extra label '_exp'.
#'# Example for 'time' and member' repeated dim in the same order of names 
#'# dimension for 'expL' and 'obsL'
#'expL <- 1 : (8*10*2*6*7)
#'expL <- 1 :c(8*10*2*6*7)
#'dim(expL) <- c(lat = 8, lon = 10, time = 2, member = 6, sdate = 7)
#'obsL <- 1 : (8*10*5*3)
#'dim(obsL) <- c(lat = 8, time = 5, member = 3, lon = 10)
#'names_exp <- names(dim(expL))
#'names_obs <- names(dim(obsL))
#'replace_repeat_dimnames(names_exp, names_obs)
#'#[1] "lat"        "lon"        "time_exp"   "member_exp" "sdate" 
#'
#'@example
#' Example for time and memeber repeated with different order
#'dim(obsL) <- c(lon = 10, member = 3, time = 5, lat = 8)
#'names_obs <- names(dim(obsL))
#'replace_repeat_dimnames(names_exp, names_obs)
#'#[1] "lat"        "lon"        "time_exp"   "member_exp" "sdate"     
#'
#'# Example for no repeated names:
#'dim(obsL) <- c(lon = 10, time_obs = 5, member_obs = 3, lat = 8)
#'names_obs <- names(dim(obsL))
#'replace_repeat_dimnames(names_exp, names_obs)
#'#[1] "lat"    "lon"    "time"   "member" "sdate" 
#'
#'  
# data <- 1:(20 * 3 * 2 * 4) 
# dim(data) <- c(lon = 20, lat = 3, time = 2, model = 4)
# lon <- seq(2, 40, 2)
# lat <- c(1, 5, 10)
# a <- SelBox(data = data, lon = lon, lat = lat, region = c(2, 20, 1, 5),
#             londim = 1, latdim = 2, mask = NULL)
# str(a)

