#if (identical(Sys.getenv("NOT_CRAN"), "")) Sys.setenv(NOT_CRAN='true')

context("Start() lon Reorder non-transform 0to360 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-[350, 370]
#3 resolution           1-1     2-<1    3->1    4-> mixed
#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
path_exp <- '/esarchive/exp/ncar/cesm-dple/monthly_mean/$var$/$var$_$sdate$.nc'

## Origin longitude in file: [0:358.75] step 1.25 degrees #288 values
## latitude:  -90 o 90 {-90, -89.05759 ...} #192 values
##############################################
test_that("1-1-4-2-1-1-1-x", {
skip_on_cran()
lons.min <- 10
lons.max <- 20
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '20001101',
             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'),
                             member=c('ensemble','realization')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)

# lon
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(10, 20),
  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),
  9
  )
  expect_equal(
  class(attr(res, 'Variables')$dat1$longitude),
  'array'
  )
})

##############################################
test_that("1-2-4-2-1-1-1-x", {
skip_on_cran()
lons.min <- 20
lons.max <- 10
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '20001101',
             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'),
                             member=c('ensemble','realization')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(10, 20),
  tolerance = 0.0001
  )
  expect_equal(
  (attr(res, 'Variables')$dat1$longitude)[1] < (attr(res, 'Variables')$dat1$longitude)[2],
  FALSE
  )
})
##############################################
test_that("1-5-4-2-1-1-1-x", {
skip_on_cran()
lons.min <- -10
lons.max <- 10
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '20001101',
             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'),
                             member=c('ensemble','realization')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(0, 10),
  tolerance = 0.0001
  )
  expect_equal(
  (attr(res, 'Variables')$dat1$longitude)[1] < (attr(res, 'Variables')$dat1$longitude)[2],
  TRUE
  )

})

##############################################
test_that("1-6-4-2-1-1-1-x", {
skip_on_cran()
lons.min <- 10
lons.max <- -10
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '20001101',
             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'),
                             member=c('ensemble','realization')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(0, 10),
  tolerance = 0.0001
  )
  expect_equal(
  (attr(res, 'Variables')$dat1$longitude)[1] < (attr(res, 'Variables')$dat1$longitude)[2],
  FALSE
  )
})

##############################################
test_that("1-7-4-2-1-1-1-x", {
skip_on_cran()
lons.min <- 330
lons.max <- 350
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '20001101',
             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'),
                             member=c('ensemble','realization')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(330, 350),
  tolerance = 0.0001
  )
  expect_equal(
  (attr(res, 'Variables')$dat1$longitude)[1] < (attr(res, 'Variables')$dat1$longitude)[2],
  TRUE
  )
})
##############################################
test_that("1-8-4-2-1-1-1-x", {
skip_on_cran()
lons.min <- 350
lons.max <- 370
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '20001101',
             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'),
                             member=c('ensemble','realization')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(350, 358.75),
  tolerance = 0.0001
  )
  expect_equal(
  (attr(res, 'Variables')$dat1$longitude)[1] < (attr(res, 'Variables')$dat1$longitude)[2],
  TRUE
  )
})
##############################################
##############################################
##############################################
test_that("1-1-4-2-2-2-1-x", {
skip_on_cran()
lons.min <- 10
lons.max <- 20
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '20001101',
             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'),
                             member=c('ensemble','realization')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(10, 20),
  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),
  9
  )
  expect_equal(
  class(attr(res, 'Variables')$dat1$longitude),
  'array'
  )
})
##############################################
test_that("1-1-4-2-2-3-1-x", {
skip_on_cran()
lons.min <- 10
lons.max <- 20
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '20001101',
             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'),
                             member=c('ensemble','realization')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(10, 20),
  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),
  9
  )
})
##############################################
test_that("1-2-4-2-2-2-1-x", {
skip_on_cran()
lons.min <- 20
lons.max <- 10
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '20001101',
             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'),
                             member=c('ensemble','realization')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  length((attr(res, 'Variables')$dat1$longitude)),
  c(281)
  )
  expect_equal(
  as.vector((attr(res, 'Variables')$dat1$longitude))[9:10],
  c(10, 20),
  tolerance = 0.0001
  )
  expect_equal(
  (attr(res, 'Variables')$dat1$longitude)[1] < (attr(res, 'Variables')$dat1$longitude)[2],
  TRUE
  )
})
##############################################
test_that("1-2-4-2-2-3-1-x", {
skip_on_cran()
lons.min <- 20
lons.max <- 10
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '20001101',
             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'),
                             member=c('ensemble','realization')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  length((attr(res, 'Variables')$dat1$longitude)),
  c(281)
  )
  expect_equal(
  as.vector((attr(res, 'Variables')$dat1$longitude))[153:154],
  c(10, 20),
  tolerance = 0.0001
  )
  expect_equal(
  (attr(res, 'Variables')$dat1$longitude)[1] < (attr(res, 'Variables')$dat1$longitude)[2],
  TRUE
  )
})

##############################################
test_that("1-3-4-2-2-2-1-x", {
skip_on_cran()
lons.min <- -10
lons.max <- -20
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '20001101',
             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'),
                             member=c('ensemble','realization')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  length((attr(res, 'Variables')$dat1$longitude)),
  c(281)
  )
  expect_equal(
  as.vector((attr(res, 'Variables')$dat1$longitude))[273:274],
  c(340, 350),
  tolerance = 0.0001
  )
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(0, 358.75),
  tolerance = 0.0001
  )
})
##############################################
test_that("1-3-4-2-2-3-1-x", {
skip_on_cran()
lons.min <- -10
lons.max <- -20
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '20001101',
             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'),
                             member=c('ensemble','realization')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  length((attr(res, 'Variables')$dat1$longitude)),
  c(281)
  )
  expect_equal(
  as.vector((attr(res, 'Variables')$dat1$longitude))[129:130],
  c(-20, -10),
  tolerance = 0.0001
  )
})
##############################################
test_that("1-4-4-2-2-2-1-x", {
skip_on_cran()
lons.min <- -20
lons.max <- -10
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '20001101',
             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'),
                             member=c('ensemble','realization')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(340, 350),
  tolerance = 0.0001
  )
  expect_equal(
  (attr(res, 'Variables')$dat1$longitude)[1] < (attr(res, 'Variables')$dat1$longitude)[2],
  TRUE
  )
})
##############################################
test_that("1-4-4-2-2-3-1-x", {
skip_on_cran()
lons.min <- -20
lons.max <- -10
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '20001101',
             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'),
                             member=c('ensemble','realization')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(-20, -10),
  tolerance = 0.0001
  )
  expect_equal(
  (attr(res, 'Variables')$dat1$longitude)[1] < (attr(res, 'Variables')$dat1$longitude)[2],
  TRUE
  )
})

##############################################
test_that("1-5-4-2-2-2-1-x", {
skip_on_cran()
lons.min <- 330
lons.max <- 350
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '20001101',
             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'),
                             member=c('ensemble','realization')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(330, 350),
  tolerance = 0.0001
  )
})
##############################################
test_that("1-5-4-2-2-3-1-x", {
skip_on_cran()
lons.min <- 330
lons.max <- 350
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '20001101',
             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'),
                             member=c('ensemble','realization')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(-30, -10),
  tolerance = 0.0001
  )
  expect_equal(
  (attr(res, 'Variables')$dat1$longitude)[1] < (attr(res, 'Variables')$dat1$longitude)[2],
  TRUE
  )
})

##############################################
test_that("1-6-4-2-2-2-1-x", {
skip_on_cran()
lons.min <- 350
lons.max <- 370
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '20001101',
             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'),
                             member=c('ensemble','realization')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  length((attr(res, 'Variables')$dat1$longitude)),
  c(17),
  tolerance = 0.0001
  )
  expect_equal(
  as.vector((attr(res, 'Variables')$dat1$longitude))[9:10],
  c(10, 350),
  tolerance = 0.0001
  )
})
##############################################
test_that("1-6-4-2-2-3-1-x", {
skip_on_cran()
lons.min <- 350
lons.max <- 370
lats.min <- 10
lats.max <- 20
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '20001101',
             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'),
                             member=c('ensemble','realization')),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = NULL),
             retrieve = F)
  expect_equal(
  length((attr(res, 'Variables')$dat1$longitude)),
  c(17),
  tolerance = 0.0001
  )
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(-10, 10),
  tolerance = 0.0001
  )
})
