context("Transform with 'all'")

test_that("1. Chunk along non-lat/lon dim", {
#skip_on_cran()

path <- '/esarchive/exp/ecearth/a1ua/cmorfiles/DCPP/EC-Earth-Consortium/EC-Earth3/dcppA-hindcast/$member$/Amon/$var$/gr/v20190713/$var$_Amon_*_s$sdate$-$member$_gr_$fyear$.nc'
suppressWarnings(
data <- Start(dat = path,
              var = 'tos',
              sdate = paste0(1960),
              time = indices(1:2), #'all',
              lat = 'all',
              lon = 'all',
              fyear = indices(1),
              member = indices(1:2),
              transform = CDORemapper,
              transform_extra_cells = 2,
              transform_params = list(grid = 'r100x50', method = 'conservative', crop = FALSE),
              transform_vars = c('lat','lon'),
              synonims = list(lon = c('lon', 'longitude'), lat = c('lat', 'latitude')),
              return_vars = list(lat = 'dat', lon = 'dat', time = 'sdate'),
              retrieve = FALSE)
)

  func <- function(x) {
    a <- mean(x, na.rm = TRUE)
    return(a)
  }
  step <- Step(func, target_dims = c('time'),
                    output_dims = NULL)
  wf <- AddStep(data, step)
suppressWarnings(
  res <- Compute(wf,
                 chunks = list(member = 2))
)

expect_equal(
dim(res$output1),
c(dat = 1, var = 1, sdate = 1, lat = 50, lon = 100, fyear = 1, member = 2)
)
expect_equal(
res$output1[1, 1, 1, 10:12, 20, 1, 1],
c(274.2808, 275.8509, 277.7623),
tolerance = 0.0001
)

})

test_that("2. chunk along lon", {
#skip_on_cran()

#!!!!!!!!!!!!!!!!!!!NOTE: the results are not identical when exp has extra cells = 2!!!!!!!!!!!!!!!!!!
# But exp2 (retrieve = T) has the same results with extra_cells = 2 and 8.

suppressWarnings(
exp <- Start(dat = '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc',
             var = 'tas',
             sdate = '20000101',
             ensemble = indices(1),
             time = indices(1),
             latitude = 'all', 
             latitude_reorder = Sort(),
             longitude = 'all',
             longitude_reorder = CircularSort(0, 360),
             transform = CDORemapper,
             transform_params = list(grid = 'r100x50',
                                     method = 'con',
                                     crop = FALSE),
             transform_vars = c('latitude', 'longitude'),
             transform_extra_cells = 8,
             synonims = list(latitude = c('lat', 'latitude'),
                             longitude = c('longitude', 'lon')),
             return_vars = list(latitude = 'dat',#NULL,
                                longitude = 'dat',#NULL,
                                time = 'sdate'),
             retrieve = F)
)

  func <- function(x) {
    return(x)
  }
  step <- Step(func, target_dims = 'time', output_dims = 'time')
  wf <- AddStep(exp, step)
suppressWarnings(
  res <- Compute(wf, chunks = list(longitude = 2))
)
suppressWarnings(
  res2 <- Compute(wf, chunks = list(ensemble = 1))
)

expect_equal(
res$output1,
res2$output1
)

# Check with retrieve = TRUE
suppressWarnings(
exp2 <- Start(dat = '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc',
             var = 'tas',
             sdate = '20000101',
             ensemble = indices(1),
             time = indices(1),
             latitude = 'all',
             latitude_reorder = Sort(),
             longitude = 'all',
             longitude_reorder = CircularSort(0, 360),
             transform = CDORemapper,
             transform_params = list(grid = 'r100x50',
                                     method = 'con',
                                     crop = FALSE),
             transform_vars = c('latitude', 'longitude'),
             transform_extra_cells = 2,
             synonims = list(latitude = c('lat', 'latitude'),
                             longitude = c('longitude', 'lon')),
             return_vars = list(latitude = 'dat',#NULL,
                                longitude = 'dat',#NULL,
                                time = 'sdate'),
             retrieve = T)
)

expect_equal(
as.vector(res$output1),
as.vector(exp2)
)

})
