# Code to test where goes the 'use_attributes' of Step():
library(startR)
library(tictoc)
rm(list = ls())
repos <- '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc'
data <- Start(dat = repos,
              var = 'tas',
              sdate = c('20170101', '20180101'),
              ensemble = indices(1 : 2),
              time = 'all',
              latitude = 'all', #indices(1:40),
              longitude = 'all', #indices(1 : 40),
              return_vars = list(latitude = 'dat', longitude = 'dat', time = 'sdate'),
              #retrieve = TRUE)
              retrieve = FALSE)

#attributes(data)$Variables$dat1$latitude # latitude /NULL

#source("./R/ByChunks.R")

funp <- function(x) {
          lat = attributes(x)$Variables$dat1$latitude
          weight = sqrt(cos(lat * pi / 180))
          corrected = Apply(list(x), target_dims = "latitude", 
                            fun = function(x) {x * weight})
        }



step <- Step(fun = funp,
             target_dims = 'latitude',
             output_dims = 'latitude',
             use_attributes = list(data = "Variables"))
wf <- AddStep(data, step)

            chunks = list(ensemble = 2,
                          sdate = 2))
toc()

dim(res$output1)
head(res$output1)
summary(res$output1)

# ----------------------------------------------------------

fun_deb <- function(x) {
             y = Season(x, posdim = 'time', monini=1, moninf=1, monsup=12)
             lons_data = as.vector(attr(x, 'Variables')$dat1$longitude)
             lats_data = as.vector(attr(x, 'Variables')$dat1$latitude)
             resgrid = "r360x180"   # prlr
             r <- s2dverification::CDORemap(y, lons_data, lats_data, resgrid, 
                                      'bil', crop = FALSE, force_remap = TRUE)
             s <- abind::adrop(r$data_array, drop = 1)
             return(s)
} 

library(multiApply)
test <- Apply(list(data), target_dims = c('time', 'longitude', 'latitude'),
              fun = fun_deb, use_attributes = list(data = "Variables"))


fun_deb2 <- function(x) {
             lons_data = as.vector(attr(x, 'Variables')$dat1$longitude)
             lats_data = as.vector(attr(x, 'Variables')$dat1$latitude)
             resgrid = "r360x180"   # prlr
             r <- s2dverification::CDORemap(x, lons_data, lats_data, resgrid,
                                      'bil', crop = FALSE, force_remap = TRUE)[[1]]
             return(r)
}


step <- Step(fun = fun_deb2,
             target_dims = c('latitude','longitude'),
             output_dims = c('latitude', 'longitude'),
             use_attributes = list(data = "Variables"))
wf <- AddStep(data, step)
tic()
res <- Compute(workflow = wf,
            chunks = list(ensemble = 2,
                          sdate = 2))
toc()

dim(res$output1)
head(res$output1)
summary(res$output1)



# --------------------------
fun_deb1 <- function(x) {
             y = Season(x, posdim = 'time', monini = 1, moninf = 1, monsup = 7)
             return(y)
}

test1 <- Apply(list(data), target_dims = c('time'),
              fun = fun_deb1, use_attributes = list(data = "Variables"))

is.numeric(as.vector(attr(data, 'Variables')$dat1$longitude))

fun_deb2 <- function(x) {
             lons_data = as.vector(attr(x, 'Variables')$dat1$longitude)
             lats_data = as.vector(attr(x, 'Variables')$dat1$latitude)
             resgrid = "r360x180"   # prlr
             r <- s2dverification::CDORemap(x, lons_data, lats_data, resgrid,
                                      'bil', crop = FALSE, force_remap = TRUE)
             return(r)
}

test2 <- Apply(list(data = data), 
               target_dims = c('longitude', 'latitude'),
               fun = fun_deb2,
               use_attributes = list(data = "Variables"))
#debug Apply
output_dims = NULL; margins = NULL; use_attributes = NULL
extra_info = NULL; guess_dim_names = TRUE; ncores = NULL; split_factor = 1

