  library(s2dverification)
  library(multiApply)

  #Write the code for a use case where you load 'psl' data for 10 start dates, 
  #for 4 forecast times, for the Mediterranean region, for 
  #1) an in-house EC-Earth experiment of your choice, and for
  #2) the ECMWF system5 model, as well as for
  #3) the ERA-5 reconstruction

  #Set the data path
  ecearth <- list(name = 'm04o',
                  path = file.path('/esarchive/exp/ecearth',
                                   '/$EXP_NAME$/monthly_mean',
                                   '/$VAR_NAME$_f6h/$VAR_NAME$_$START_DATE$.nc'))
  #/esarchive/exp/ecearth/m04o/monthly_mean/psl_f6h
  #psl_19930501.nc

  ecmwf <- list(name = 'system5c3s',
                path = file.path('/esarchive/exp/ecmwf',
                                 '/$EXP_NAME$/monthly_mean',
                                 '/$VAR_NAME$_f6h/$VAR_NAME$_$START_DATE$.nc'))
  #/esarchive/exp/ecmwf/system5c3s/monthly_mean/psl_f6h
  #psl_20190301.nc

  era5 <- list(name = 'era5',
               path = file.path('/esarchive/recon/ecmwf',
                                '$OBS_NAME$/monthly_mean',
                                '$VAR_NAME$_f1h/$VAR_NAME$_$YEAR$$MONTH$.nc'))
  #/esarchive/recon/ecmwf/era5/monthly_mean/psl_f1h
  #psl_201808.nc

  startDates <- c(paste0(1993:2002,'1101'))
  time_length <- 4
  lon <- c(-6, 36)
  lat <- c(30, 46)

  data <- Load(var = 'psl',
               exp = list(ecearth, ecmwf),
               obs = list(era5),
               sdates = startDates,
               storefreq = "monthly",
               leadtimemax = time_length,
               output = 'lonlat',
               lonmin = lon[1], lonmax = lon[2],
               latmin = lat[1], latmax = lat[2])
  
  mod <- data$mod
  obs <- data$obs
  dim(mod)  #member follows ecearth = 10
  dim(obs)


  #Correct the bias of the data (using Clim and Ano).
  #Compute the CRPSS for each forecast time, latitude and longitude. 
  #You will need to use ProbBins, multiApply, and think a lot.
  clim <- Clim(mod, obs)
  ano_mod <- Ano(mod, clim$clim_exp)
  ano_obs <- Ano(obs, clim$clim_obs)
#  mod_corr <- mod - (clim$clim_exp - clim$clim_obs)

  mod_corr <- array(0, dim = dim(ano_mod))
  for (i in 1 : 2) {
    for (j in 1 : 10) {
      for (k in 1 : 10) {
        mod_corr[i, j, k, , , ] <- clim$clim_obs[1, 1, , , ] + ano_mod[i, j, k, , , ]
      }
    }
  }

  mod_corr_bind <- array(mod_corr, dim = c(bind = 20, sdate = 10, ftime = 4, lat = 23, lon = 60))
  mod_corr_reorder <- multiApply:::.aperm2(mod_corr_bind, c(2, 1, 3, 4, 5))
  obs_sub <- s2dverification::Subset(obs, c('dataset'), 1, drop = TRUE)

  CRPS <- multiApply::Apply(data = list(mod_corr_reorder, obs_sub),
                            margins = list(c('ftime', 'lat', 'lon'), 
                                           c('ftime', 'lat', 'lon')),
                            #target_dims = list(c('sdate', 'member'), c('sdate')),
                            fun = SpecsVerification::EnsCrps)$output1
  names(dim(CRPS))[1] <- c('sdate')

  mod_corr_sub <- s2dverification::Subset(mod_corr_reorder, c('bind'), c(1:2))
  CRPS_ref <- multiApply::Apply(data = list(mod_corr_sub, obs_sub),
                                margins = list(c('ftime', 'lat', 'lon'), 
                                               c('ftime', 'lat', 'lon')),
                                fun = SpecsVerification::EnsCrps)$output1
  names(dim(CRPS_ref))[1] <- c('sdate')

  CRPSS <- multiApply::Apply(data = list(CRPS, CRPS_ref),
                             margins = list(c('ftime', 'lat', 'lon'), 
                                            c('ftime', 'lat', 'lon')),
                             fun = SpecsVerification::SkillScore)$output1




#  PB <- ProbBins(CRPS, fcyr = 'all', posdates = 1,
#                 posdim = 2, compPeriod = "Full period")

 

  #Plot the CRPSS map of the first forecast time with PlotEquiMap.   
  #Plot all CRPSS maps (for all forecast times) in a single figure with PlotLayout and PlotEquiMap.
  #You can check the s2dverification paper for help, the s2dverification user manual on gitlab,
  # and ask Suso or Núria.

  brks <- seq(min(CRPSS, na.rm = TRUE), 
              max(CRPSS, na.rm = TRUE), 
              length.out = 31)

  PlotEquiMap(CRPSS[1, 1, , ], data$lon, data$lat, 
              toptitle = 'CRPSS of 1st ftime', title_scale = 0.7,
              axes_label_scale = 0.7,
              intylat = 5, intxlon = 5,
              brks = brks
             # dots = CRPSS[1, 1, 1, , ] > 0
             )
  PlotLayout(PlotEquiMap, c('lat', 'lon'), CRPSS[1, , , ], data$lon, data$lat,
             toptitle = 'CRPSS', title_scale = 0.7,
             titles = paste('ftime', 1:4),
             axes_label_scale = 0.7,
             intylat = 5, intxlon = 5,
             brks = brks
             )


  #Plot a map of the most likely quintiles for each experiment using the function
  #CSTools::PlotMostLikelyQuantileMap.








