# This unit test uses three different selector forms: indices(), values(), and 'all', to do 
# the transformation. "lat_reorder" is also tested.
# Their results should be all identical and consistent with cdo result (with precision difference). 
# The selected lat/lon range is all the grids here.
# "lon_reorder = CircularSort(0, 360)" and "lat = Sort()" are used in all the tests.
# To see different lat_reorder options, go to "test-Start-transform-lat-Sort-*".
# If values, the lat selector is [-90, 90] or [90, -90]; if indices, c(1:640) or c(640:1).

# Note that the original latitude is descending [90:-90].

context("Transform: three selector forms")

#---------------------------------------------------------------
# cdo is used to verify the data values
library(easyNCDF)
path <- "/esarchive/exp/ecmwf/system5_m1/monthly_mean/tas_f6h/tas_20000101.nc"
file <- NcOpen(path)
arr <- NcToArray(file,
                 dim_indices = list(time = 1, ensemble = 1,
                                    latitude = 1:640, longitude = 1:1296),
                 vars_to_read = 'tas')
lats <- NcToArray(file,
                  dim_indices = list(latitude = 1:640), vars_to_read = 'latitude')
lons <- NcToArray(file,
                  dim_indices = list(longitude = 1:1296), vars_to_read = 'longitude')
NcClose(file)
arr2 <- s2dv::CDORemap(arr, lons = as.vector(lons), lats = as.vector(lats),
                       grid = 'r100x50', method = 'con', crop = FALSE)$data_array
#---------------------------------------------------------------

path <- '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc'

test_that("1. indices", {

suppressWarnings(
res1 <- Start(dat = path,
             var = 'tas',
             sdate = '20000101',
             ensemble = indices(1),
             time = indices(1),
             latitude = indices(1:640),
             latitude_reorder = Sort(),
             longitude = indices(1:1296),
             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',
                                longitude = 'dat',
                                time = 'sdate'),
             retrieve = T)
)

 
suppressWarnings(
res2 <- Start(dat = path,
             var = 'tas',
             sdate = '20000101',
             ensemble = indices(1),
             time = indices(1),
             latitude = indices(640:1),
             latitude_reorder = Sort(),
             longitude = indices(1:1296),
             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',
                                longitude = 'dat',
                                time = 'sdate'),
             retrieve = T)
)

expect_equal(
as.vector(res1),
as.vector(arr2),
tolerance = 0.0001
)
#WRONG!!!!! res2 lat is descending now
#expect_equal(
#as.vector(res1),
#as.vector(res2)
#)

})


test_that("2. values", {

suppressWarnings(
res1 <- Start(dat = path,
             var = 'tas',
             sdate = '20000101',
             ensemble = indices(1),
             time = indices(1),
             latitude = values(list(-90, 90)),
             latitude_reorder = Sort(),
             longitude = values(list(0, 359.9)),
             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',
                                longitude = 'dat',
                                time = 'sdate'),
             retrieve = T)
)
suppressWarnings(
res2 <- Start(dat = path,
             var = 'tas',
             sdate = '20000101',
             ensemble = indices(1),
             time = indices(1),
             latitude = values(list(90, -90)),
             latitude_reorder = Sort(),
             longitude = values(list(0, 359.9)),
             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',
                                longitude = 'dat',
                                time = 'sdate'),
             retrieve = T)
)

expect_equal(
as.vector(res1),
as.vector(arr2),
tolerance = 0.0001
)
expect_equal(
as.vector(res2),
as.vector(arr2),
tolerance = 0.0001
)


})


test_that("3. all", {

suppressWarnings(
res <- Start(dat = path,
             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',
                                longitude = 'dat',
                                time = 'sdate'),
             retrieve = T)
)

expect_equal(
as.vector(res),
as.vector(arr2),
tolerance = 0.0001
)

})
