context("Start() lon Reorder non-transform -180to180 test")
#1 selector type        1-values(list)  2-values(vector)  3-indices     4-'all'  5-mix
#2 selector range       1-[10, 20]      2-[20, 10]        3-[-10, -20]  4-[-20, -10]  5-[-10, 10]  6-[10, -10]  7-[300, 350]  8-[170, 190]
#3 resolution           1-1     2-<1    3->1    
#4 returns_var          1-NULL  2-'dat'                 
#5 lat_reorder/Sort()   1-No    2-Yes,decreasing = F    3-Yes,decreasing = T
#6 lon_reorder/CircularSort()   1-No    2-Yes,(0, 360)  3-Yes,(-180, 180)       
#7 transform            1-NO    2-YES           
#8 transform_crop       1-T     2-F     3-region    4-x

##############################################
# 3-2

## Origin longitude in file: [-179.71875:180]
path_exp <- '/esarchive/recon/ecmwf/era5/original_files/reorder/daily_mean/$var$/$var$_$sdate$.nc'
variable <- 'tas'
sdate <- '199212'

##############################################
test_that("1-1-2-2-1-1-1-x", {
lons.min <- 10
lons.max <- 20
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = variable,
             sdate = sdate,
             time = indices(4),
             latitude = values(list(lats.min, lats.max)),
             longitude = values(list(lons.min, lons.max)),
             synonims = list(latitude=c('lat','latitude'),
                             longitude=c('lon','longitude')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)

# lon
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(10.12500, 19.96875),
  tolerance = 0.0001
  )
  expect_equal(
  (attr(res, 'Variables')$dat1$longitude)[1] < (attr(res, 'Variables')$dat1$longitude)[2],
  TRUE
  )
  expect_equal(
  length(attr(res, 'Variables')$dat1$longitude),
  36
  )
  expect_equal(
  class(attr(res, 'Variables')$dat1$longitude),
  'array'
  )
})

##############################################
test_that("1-2-2-2-1-1-1-x", {
lons.min <- 20
lons.max <- 10
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = variable,
             sdate = sdate,
             time = indices(4),
             latitude = values(list(lats.min, lats.max)),
             longitude = values(list(lons.min, lons.max)),
             synonims = list(latitude=c('lat','latitude'),
                             longitude=c('lon','longitude')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(10.12500, 19.96875),
  tolerance = 0.0001
  )
  expect_equal(
  (attr(res, 'Variables')$dat1$longitude)[1] < (attr(res, 'Variables')$dat1$longitude)[2],
  FALSE
  )
})

##############################################
test_that("1-3-2-2-1-1-1-x", {
lons.min <- -10
lons.max <- -20
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = variable,
             sdate = sdate,
             time = indices(4),
             latitude = values(c(lats.min:lats.max)),
             longitude = values(list(lons.min, lons.max)),
             synonims = list(latitude=c('lat','latitude'),
                             longitude=c('lon','longitude')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)

  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(-19.96875, -10.12500),
  tolerance = 0.0001
  )
  expect_equal(
  (attr(res, 'Variables')$dat1$longitude)[1] < (attr(res, 'Variables')$dat1$longitude)[2],
  FALSE
  )
})

##############################################
test_that("1-4-2-2-1-1-1-x", {
lons.min <- -20
lons.max <- -10
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = variable,
             sdate = sdate,
             time = indices(3:4),
             latitude = values(c(lats.min:lats.max)),
             longitude = values(list(lons.min, lons.max)),
             synonims = list(latitude=c('lat','latitude'),
                             longitude=c('lon','longitude')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)

  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(-19.96875, -10.12500),
  tolerance = 0.0001
  )
  expect_equal(
  (attr(res, 'Variables')$dat1$longitude)[1] < (attr(res, 'Variables')$dat1$longitude)[2],
  TRUE
  )
})

##############################################
test_that("1-5-2-2-1-1-1-x", {
lons.min <- -10
lons.max <- 10
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = variable,
             sdate = sdate,
             time = indices(3:4),
             latitude = values(c(lats.min:lats.max)),
             longitude = values(list(lons.min, lons.max)),
             synonims = list(latitude=c('lat','latitude'),
                             longitude=c('lon','longitude')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(-9.84375,9.84375),
  tolerance = 0.0001
  )
  expect_equal(
  (attr(res, 'Variables')$dat1$longitude)[1] < (attr(res, 'Variables')$dat1$longitude)[2],
  TRUE
  )

})

##############################################
test_that("1-6-2-2-1-1-1-x", {
lons.min <- 10
lons.max <- -10
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = variable,
             sdate = sdate,
             time = indices(3:4),
             latitude = values(c(lats.min:lats.max)),
             longitude = values(list(lons.min, lons.max)),
             synonims = list(latitude=c('lat','latitude'),
                             longitude=c('lon','longitude')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c( -9.84375, 9.84375),
  tolerance = 0.0001
  )
  expect_equal(
  (attr(res, 'Variables')$dat1$longitude)[1] < (attr(res, 'Variables')$dat1$longitude)[2],
  FALSE
  )
})

##############################################
test_that("1-8-2-2-1-1-1-x", {
lons.min <- 170
lons.max <- 190
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = variable,
             sdate = sdate,
             time = indices(3:4),
             latitude = values(c(lats.min:lats.max)),
             longitude = values(list(lons.min, lons.max)),
             synonims = list(latitude=c('lat','latitude'),
                             longitude=c('lon','longitude')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(170.1562, 180),
  tolerance = 0.0001
  )
  expect_equal(
  (attr(res, 'Variables')$dat1$longitude)[1] < (attr(res, 'Variables')$dat1$longitude)[2],
  TRUE
  )
})
##############################################
##############################################
##############################################
test_that("1-1-2-2-2-2-1-x", {
lons.min <- 10
lons.max <- 20
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = variable,
             sdate = sdate,
             time = indices(4),
             latitude = values(list(lats.min, lats.max)),
             latitude_reorder = Sort(),
             longitude_reorder = CircularSort(0, 360),
             longitude = values(list(lons.min, lons.max)),
             synonims = list(latitude=c('lat','latitude'),
                             longitude=c('lon','longitude')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(10.12500, 19.96875),
  tolerance = 0.0001
  )
  expect_equal(
  (attr(res, 'Variables')$dat1$longitude)[1] < (attr(res, 'Variables')$dat1$longitude)[2],
  TRUE
  )
  expect_equal(
  length(attr(res, 'Variables')$dat1$longitude),
  36
  )
  expect_equal(
  class(attr(res, 'Variables')$dat1$longitude),
  'array'
  )
})
##############################################
test_that("1-1-2-2-2-3-1-x", {
lons.min <- 10
lons.max <- 20
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = variable,
             sdate = sdate,
             time = indices(4),
             latitude = values(list(lats.min, lats.max)),
             latitude_reorder = Sort(),
             longitude_reorder = CircularSort(-180, 180),
             longitude = values(list(lons.min, lons.max)),
             synonims = list(latitude=c('lat','latitude'),
                             longitude=c('lon','longitude')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(10.12500, 19.96875),
  tolerance = 0.0001
  )
  expect_equal(
  (attr(res, 'Variables')$dat1$longitude)[1] < (attr(res, 'Variables')$dat1$longitude)[2],
  TRUE
  )
  expect_equal(
  length(attr(res, 'Variables')$dat1$longitude),
  36
  )
})
##############################################
test_that("1-2-2-2-2-2-1-x", {
lons.min <- 20
lons.max <- 10
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = variable,
             sdate = sdate,
             time = indices(4),
             latitude = values(list(lats.min, lats.max)),
             latitude_reorder = Sort(),
             longitude_reorder = CircularSort(0, 360),
             longitude = values(list(lons.min, lons.max)),
             synonims = list(latitude=c('lat','latitude'),
                             longitude=c('lon','longitude')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  length((attr(res, 'Variables')$dat1$longitude)),
  1244
  )
  expect_equal(
  as.vector((attr(res, 'Variables')$dat1$longitude))[36:37],
  c(9.84375, 20.25000),
  tolerance = 0.0001
  )
  expect_equal(
  (attr(res, 'Variables')$dat1$longitude)[1] < (attr(res, 'Variables')$dat1$longitude)[2],
  TRUE
  )
})
##############################################
test_that("1-2-2-2-2-3-1-x", {
lons.min <- 20
lons.max <- 10
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = variable,
             sdate = sdate,
             time = indices(4),
             latitude = values(list(lats.min, lats.max)),
             latitude_reorder = Sort(),
             longitude_reorder = CircularSort(-180, 180),
             longitude = values(list(lons.min, lons.max)),
             synonims = list(latitude=c('lat','latitude'),
                             longitude=c('lon','longitude')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  length((attr(res, 'Variables')$dat1$longitude)),
  1244
  )
  expect_equal(
  as.vector((attr(res, 'Variables')$dat1$longitude))[676:677],
  c(9.84375, 20.25000),
  tolerance = 0.0001
  )
  expect_equal(
  (attr(res, 'Variables')$dat1$longitude)[1] < (attr(res, 'Variables')$dat1$longitude)[2],
  TRUE
  )
})

##############################################
test_that("1-3-2-2-2-2-1-x", {
lons.min <- -10
lons.max <- -20
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = variable,
             sdate = sdate,
             time = indices(4),
             latitude = values(list(lats.min, lats.max)),
             latitude_reorder = Sort(),
             longitude_reorder = CircularSort(0, 360),
             longitude = values(list(lons.min, lons.max)),
             synonims = list(latitude=c('lat','latitude'),
                             longitude=c('lon','longitude')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  length((attr(res, 'Variables')$dat1$longitude)),
  1244
  )
  expect_equal(
  as.vector((attr(res, 'Variables')$dat1$longitude))[1209:1210],
  c(339.7500, 350.1562),
  tolerance = 0.0001
  )
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(0, 359.7222),
  tolerance = 0.0001
  )
})
##############################################
test_that("1-3-2-2-2-3-1-x", {
lons.min <- -10
lons.max <- -20
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = variable,
             sdate = sdate,
             time = indices(4),
             latitude = values(list(lats.min, lats.max)),
             latitude_reorder = Sort(),
             longitude_reorder = CircularSort(-180, 180),
             longitude = values(list(lons.min, lons.max)),
             synonims = list(latitude=c('lat','latitude'),
                             longitude=c('lon','longitude')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  length((attr(res, 'Variables')$dat1$longitude)),
  1244
  )
  expect_equal(
  as.vector((attr(res, 'Variables')$dat1$longitude))[569:570],
  c(-20.25000, -9.84375),
  tolerance = 0.0001
  )
})
##############################################
test_that("1-4-2-2-2-2-1-x", {
lons.min <- -20
lons.max <- -10
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = variable,
             sdate = sdate,
             time = indices(4),
             latitude = values(list(lats.min, lats.max)),
             latitude_reorder = Sort(),
             longitude_reorder = CircularSort(0, 360),
             longitude = values(list(lons.min, lons.max)),
             synonims = list(latitude=c('lat','latitude'),
                             longitude=c('lon','longitude')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(340.0312, 349.8750),
  tolerance = 0.0001
  )
  expect_equal(
  (attr(res, 'Variables')$dat1$longitude)[1] < (attr(res, 'Variables')$dat1$longitude)[2],
  TRUE
  )
})
##############################################
test_that("1-4-2-2-2-3-1-x", {
lons.min <- -20
lons.max <- -10
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = variable,
             sdate = sdate,
             time = indices(4),
             latitude = values(list(lats.min, lats.max)),
             latitude_reorder = Sort(),
             longitude_reorder = CircularSort(-180, 180),
             longitude = values(list(lons.min, lons.max)),
             synonims = list(latitude=c('lat','latitude'),
                             longitude=c('lon','longitude')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(-19.96875, -10.12500),
  tolerance = 0.0001
  )
  expect_equal(
  (attr(res, 'Variables')$dat1$longitude)[1] < (attr(res, 'Variables')$dat1$longitude)[2],
  TRUE
  )
})
##############################################
test_that("1-5-2-2-2-2-1-x", {
lons.min <- -10
lons.max <- 10
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = variable,
             sdate = sdate,
             time = indices(4),
             latitude = values(list(lats.min, lats.max)),
             latitude_reorder = Sort(),
             longitude_reorder = CircularSort(0, 360),
             longitude = values(list(lons.min, lons.max)),
             synonims = list(latitude=c('lat','latitude'),
                             longitude=c('lon','longitude')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(0, 359.7188),
  tolerance = 0.0001
  )
  expect_equal(
  (attr(res, 'Variables')$dat1$longitude)[1] < (attr(res, 'Variables')$dat1$longitude)[2],
  TRUE
  )
  expect_equal(
  length(attr(res, 'Variables')$dat1$longitude),
  71
  )
  expect_equal(
  as.vector((attr(res, 'Variables')$dat1$longitude))[36:37],
  c(9.84375, 350.15625),
  tolerance = 0.0001
  )
})

##############################################
test_that("1-5-2-2-2-3-1-x", {
lons.min <- -10
lons.max <- 10
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = variable,
             sdate = sdate,
             time = indices(4),
             latitude = values(list(lats.min, lats.max)),
             latitude_reorder = Sort(),
             longitude_reorder = CircularSort(-180, 180),
             longitude = values(list(lons.min, lons.max)),
             synonims = list(latitude=c('lat','latitude'),
                             longitude=c('lon','longitude')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(-9.84375, 9.84375),
  tolerance = 0.0001
  )
  expect_equal(
  (attr(res, 'Variables')$dat1$longitude)[1] < (attr(res, 'Variables')$dat1$longitude)[2],
  TRUE
  )
})
##############################################
test_that("1-6-2-2-2-2-1-x", {
lons.min <- 20
lons.max <- -10
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = variable,
             sdate = sdate,
             time = indices(4),
             latitude = values(list(lats.min, lats.max)),
             latitude_reorder = Sort(),
             longitude_reorder = CircularSort(0, 360),
             longitude = values(list(lons.min, lons.max)),
             synonims = list(latitude=c('lat','latitude'),
                             longitude=c('lon','longitude')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(20.2500, 349.8750),
  tolerance = 0.001
  )
  expect_equal(
  (attr(res, 'Variables')$dat1$longitude)[1] < (attr(res, 'Variables')$dat1$longitude)[2],
  TRUE
  )
  expect_equal(
  length(attr(res, 'Variables')$dat1$longitude),
  1173
  )
})

##############################################
test_that("1-6-2-2-2-3-1-x", {
lons.min <- 20
lons.max <- -10
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = variable,
             sdate = sdate,
             time = indices(4),
             latitude = values(list(lats.min, lats.max)),
             latitude_reorder = Sort(),
             longitude_reorder = CircularSort(-180, 180),
             longitude = values(list(lons.min, lons.max)),
             synonims = list(latitude=c('lat','latitude'),
                             longitude=c('lon','longitude')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(-180, 179.7188),
  tolerance = 0.001
  )
  expect_equal(
  (attr(res, 'Variables')$dat1$longitude)[1] < (attr(res, 'Variables')$dat1$longitude)[2],
  TRUE
  )
  expect_equal(
  length(attr(res, 'Variables')$dat1$longitude),
  1173
  )
  expect_equal(
  as.vector((attr(res, 'Variables')$dat1$longitude))[605:606],
  c(-10.12500, 20.25000),
  tolerance = 0.0001
  )
})
##############################################
test_that("1-7-2-2-2-2-1-x", {
lons.min <- 330
lons.max <- 350
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = variable,
             sdate = sdate,
             time = indices(4),
             latitude = values(list(lats.min, lats.max)),
             latitude_reorder = Sort(),
             longitude_reorder = CircularSort(0, 360),
             longitude = values(list(lons.min, lons.max)),
             synonims = list(latitude=c('lat','latitude'),
                             longitude=c('lon','longitude')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(330.1875, 349.8750),
  tolerance = 0.0001
  )
  expect_equal(
  length(attr(res, 'Variables')$dat1$longitude),
  71
  )

})
##############################################
test_that("1-7-2-2-2-3-1-x", {
lons.min <- 330
lons.max <- 350
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = variable,
             sdate = sdate,
             time = indices(4),
             latitude = values(list(lats.min, lats.max)),
             latitude_reorder = Sort(),
             longitude_reorder = CircularSort(-180, 180),
             longitude = values(list(lons.min, lons.max)),
             synonims = list(latitude=c('lat','latitude'),
                             longitude=c('lon','longitude')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(-29.8125, -10.1250),
  tolerance = 0.0001
  )
  expect_equal(
  (attr(res, 'Variables')$dat1$longitude)[1] < (attr(res, 'Variables')$dat1$longitude)[2],
  TRUE
  )
  expect_equal(
  length(attr(res, 'Variables')$dat1$longitude),
  71
  )

})

##############################################
test_that("1-8-2-2-2-2-1-x", {
lons.min <- 350
lons.max <- 370
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = variable,
             sdate = sdate,
             time = indices(4),
             latitude = values(list(lats.min, lats.max)),
             latitude_reorder = Sort(),
             longitude_reorder = CircularSort(0, 360),
             longitude = values(list(lons.min, lons.max)),
             synonims = list(latitude=c('lat','latitude'),
                             longitude=c('lon','longitude')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  length((attr(res, 'Variables')$dat1$longitude)),
  71,
  tolerance = 0.0001
  )
  expect_equal(
  as.vector((attr(res, 'Variables')$dat1$longitude))[36:37],
  c(9.84375, 350.15625),
  tolerance = 0.0001
  )
})
##############################################
test_that("1-8-2-2-2-3-1-x", {
lons.min <- 350
lons.max <- 370
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = variable,
             sdate = sdate,
             time = indices(4),
             latitude = values(list(lats.min, lats.max)),
             latitude_reorder = Sort(),
             longitude_reorder = CircularSort(-180, 180),
             longitude = values(list(lons.min, lons.max)),
             synonims = list(latitude=c('lat','latitude'),
                             longitude=c('lon','longitude')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  length((attr(res, 'Variables')$dat1$longitude)),
  71,
  tolerance = 0.0001
  )
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(-9.84375, 9.84375),
  tolerance = 0.0001
  )
})
