library(ClimProjDiags)
Aggregation <- function(recipe, data) {
  agg <- 'spatial' #recipe$Analysis$Workflow$Aggregation$type
  weighted <- TRUE
  
  if (agg == 'spatial') {
    if (weighted) {
      res <- WeightedMean(data$hcst$data,
                          lat = data$hcst$coords$latitude,
                          lon = data$hcst$coords$longitude,
                          londim = 'longitude',
                          latdim = 'latitude',
                          region = NULL)
      res <- InsertDim(res, pos = 8, len = 1, name = 'region')
      data$hcst$coords$latitude <- NULL
      data$hcst$coords$longitude <- NULL
      tmp1 <- s2dv_cube(data = res,
                        coords = append(data$hcst$coords, 
                                      list(region = 'NAO region')),
                        varName = 'psl',
                        metadata = data$hcst$attrs$Variable$metadata,
                        Datasets = data$hcst$attrs$Datasets,
                        Dates = data$hcst$attrs$Dates,
                        when = data$hcst$attrs$when,
                        source_files = data$hcst$attrs$source_files)
      res <- WeightedMean(data$obs$data,
                          lat = data$obs$coords$latitude,
                          lon = data$obs$coords$longitude,
                          londim = 'longitude',
                          latdim = 'latitude',
                          region = NULL)
      res <- InsertDim(res, pos = 8, len = 1, name = 'region')
      data$obs$coords$latitude <- NULL
      data$obs$coords$longitude <- NULL
      tmp2 <- s2dv_cube(data = res,
                        coords = append(data$obs$coords,
                                      list(region = 'NAO region')),
                        varName = 'psl',
                        metadata = data$obs$attrs$Variable$metadata,
                        Datasets = data$obs$attrs$Datasets,
                        Dates = data$obs$attrs$Dates,
                        when = data$obs$attrs$when,
                        source_files = data$obs$attrs$source_files)
      result <- list(hcst = tmp1, obs = tmp2)
    }
  }
return(result)
}

                       
                       
