context("Transform with 'all'")

test_that("1. Specify lat and lon with 'all'; retrieve = TRUE", {

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(
data1 <- Start(dat = path,
              var = 'tos',
              sdate = paste0(1960),
              time = indices(1:2), #'all',
              lat = 'all',
              lon = 'all',
#              lat_reorder = Sort(decreasing = F),
#              lon_reorder = CircularSort(0, 360),
#              lat_var = 'lat',
#              lon_var = 'lon',
              fyear = indices(1),
              member = indices(1),
              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 = T)
)

suppressWarnings(
data2 <- Start(dat = path,
              var = 'tos',
              sdate = paste0(1960),
              time = indices(1:2), #'all',
              lat = values(list(-90, 90)),
              lon = values(list(0, 359.9)),
              lat_reorder = Sort(decreasing = F),
              lon_reorder = CircularSort(0, 360),
#              lat_var = 'lat',
#              lon_var = 'lon',
              fyear = indices(1),
              member = indices(1),
              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 = NULL, lon = NULL, time = 'sdate'),
              retrieve = T)
)

expect_equal(
dim(data1),
c(dat = 1, var = 1, sdate = 1, time = 2, lat = 50, lon = 100, fyear = 1, member = 1) 
)
expect_equal(
dim(data1),
dim(data2)
)
expect_equal(
as.vector(data1),
as.vector(data2)
)
expect_equal(
data1[1, 1, 1, 2, 10:12, 20, 1, 1],
c(274.6942, 276.2658, 278.2566),
tolerance = 0.0001
)

})

test_that("2. Specify lat and lon with 'all'; retrieve = FALSE", {

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
)


})
