

compute_statistics <- function(recipe, data, agg = 'global'){
  
  # data$hcst: s2dv_cube containing the hindcast
  
  # obs: s2dv_cube containing the observations
  # recipe: auto-s2s recipe as provided by read_yaml
  
  time_dim <- 'syear'
  memb_dim <- 'ensemble'
  
  
  ## Duplicate obs along hcst ensemble dimension
  obs_data <- adrop(data$obs$data, drop = 9)
  obs_data <- InsertDim(data = obs_data, pos = 9, lendim = 25, name = 'ensemble')
  
  
  statistics_list <- tolower(recipe$Analysis$Workflow$Statistics$metric)
  
  statistics <- list()
  
  for (stat in strsplit(statistics_list, ", | |,")[[1]]) {
    # Whether the fair version of the metric is to be computed
    if (stat %in% c('cov', 'covariance')) {
      
     covariance <- Apply(data = list(x= obs_data, y=data$hcst$data), 
                         target_dims = c(time_dim, memb_dim),
                         fun = function(x,y){cov(as.vector(x),as.vector(y), 
                                                 use = "everything", 
                                                 method = "pearson")})$output1
      
      statistics[[ stat ]] <- covariance
      
    } ## close if on cov
    
    
    if (stat %in% c('std', 'standard_deviation')) {
      
      ## Calculate standard deviation
      std_hcst <- Apply(data = data$hcst$data, 
                        target_dims = c(time_dim, memb_dim), 
                        fun = 'sd')$output1
      
      std_obs <- Apply(data = data$obs$data, 
                       target_dims = c(time_dim, memb_dim), 
                       fun = 'sd')$output1
      
      statistics[[ stat ]] <- list('std_hcst' = std_hcst, 'std_obs' = std_obs)
      
    } ## close if on std
    
    if (stat %in% c('var', 'variance')) {
      
      ## Calculate standard deviation
      var_hcst <- (Apply(data = data$hcst$data, 
                         target_dims = c(time_dim, memb_dim), 
                         fun = 'sd')$output1)^2
      
      var_obs <- (Apply(data = data$obs$data, 
                        target_dims = c(time_dim, memb_dim), 
                        fun = 'sd')$output1)^2
      
      statistics[[ stat ]] <- list('var_hcst' = var_hcst, 'var_obs' = var_obs)
      
    } ## close if on var
    
  }
  
  info(recipe$Run$logger, "##### STATISTICS COMPUTATION COMPLETE #####")
  .log_memory_usage(recipe$Run$logger, when = "After statistics computation")
 
   # Save outputs
  if (recipe$Analysis$Workflow$Skill$save != 'none') {
    info(recipe$Run$logger, "##### START SAVING STATISTICS #####")
  }
  recipe$Run$output_dir <- paste0(recipe$Run$output_dir,
                                  "/outputs/Statistics/")
  
  if (recipe$Analysis$Workflow$Statistics$save == 'all') {
    # Save all statistics
      save_metrics(recipe = recipe, skill = statistics, ## Not able to save data with these dimensions
                   data_cube = data$hcst, agg = agg) ## The length of parameter 'order' should be the same with the dimension length of parameter 'data'. 
  }
  
  # Return results
  return(statistics)
}  

