# version victoria https://earth.bsc.es/gitlab/es/auto-s2s/-/blob/dev-Loading-multivar/modules/Skill/Skill.R
# This function takes an array and drops dimensions of length = 1 that are not
# needed by the saving or plotting functions, aka 'droppable_dims'. 
# The essential dimensions are: 'var', 'bin', 'time', and 'latitude'/'longitude'
#   or 'region'.
# The 'droppable' dimensions are: 'dat', 'sday', 'sweek', 'ensemble', 'nobs', 
#   'nexp, 'exp_memb', 'obs_memb'.
.drop_dims <- function(metric_array) {
  # Define dimensions that are not essential for saving
  droppable_dims <- c("dat", "sday", "sweek", "ensemble", "nobs",
                      "nexp", "exp_memb", "obs_memb") # , "bin")
  # Select non-essential dimensions of length 1
  dims_to_drop <- intersect(names(which(dim(metric_array) == 1)),
                            droppable_dims)
  drop_indices <- grep(paste(dims_to_drop, collapse = "|"),
                       names(dim(metric_array)))
  # Drop selected dimensions
  metric_array <- abind::adrop(metric_array, drop = drop_indices)
  # If array has memb dim (Corr case), change name to 'ensemble'
  if ("exp_memb" %in% names(dim(metric_array))) {
    names(dim(metric_array))[which(names(dim(metric_array)) ==
                                   "exp_memb")] <- "ensemble"
  }
  return(metric_array)
}

## TODO: Replace with ClimProjDiags::Subset and add var and dat dimensions
#.drop_dims <- function(metric_array) {
#  # Drop all singleton dimensions
#  dims <- dim(metric_array)
#  metric_array <- drop(metric_array)
#  if ("region" %in% names(dims)) {
#    if (!is.array(metric_array)) {
#      dim(metric_array) <- length(metric_array)
#      if (!all(dims > 1)) {
#        if ("syear" %in% names(dims)) {
#          names(dim(metric_array)) <- "syear"
#        } else {
#          names(dim(metric_array)) <- "time"
#        }
#      } else {
#        names(dim(metric_array)) <- names(dims[which(dims > 1)])
#      }
#    }
#    if (!("time" %in% names(dim(metric_array)))) {
#      dim(metric_array) <- c("time" = 1, dim(metric_array))
#    }
#    # If latitude was singleton, add it back
#    if (!("region" %in% names(dim(metric_array)))) {
#      dim(metric_array) <- c(dim(metric_array), "region" = 1)
#    }
#    # If array has memb dim (Corr case), change name to 'ensemble'
#    if ("exp_memb" %in% names(dim(metric_array))) {
#      names(dim(metric_array))[which(names(dim(metric_array)) ==
#                                   "exp_memb")] <- "ensemble"
#    }
#  } else if (all(c("latitude", "longiguted") %in% names(dims))) {
#    # If the array becomes a vector as a result, restore dimensions.
#    ## This applies to the case of 'corr' with one lon and one lat
#    if (!is.array(metric_array)) {
#      dim(metric_array) <- length(metric_array)
#      names(dim(metric_array)) <- names(dims[which(dims > 1)])
#    }
#    # If the array becomes a vector as a result, restore dimensions.
#    ## This applies to the case of 'corr' with one lon and one lat
#    # If time happened to be a singleton dimension, add it back in the array
#   if (!("time" %in% names(dim(metric_array)))) {
#      dim(metric_array) <- c("time" = 1, dim(metric_array))
#    }
#    # If latitude was singleton, add it back
#    if (!("latitude" %in% names(dim(metric_array)))) {
#      dim(metric_array) <- c(dim(metric_array), "latitude" = 1)
#    }
#    # If longitude was singleton, add it back
#    if (!("longitude" %in% names(dim(metric_array)))) {
#      dim(metric_array) <- c(dim(metric_array), "longitude" = 1)
#    }
#    # If array has memb dim (Corr case), change name to 'ensemble'
#    if ("exp_memb" %in% names(dim(metric_array))) {
#      names(dim(metric_array))[which(names(dim(metric_array)) ==
#                                   "exp_memb")] <- "ensemble"
#    } else {
#      stop("what dimensions")
#    }
#  }
#  return(metric_array)
#}


