#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 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/ecmwf/system5_m1/daily_mean/$var$_f6h/$var$_$sdate$.nc'

## Origin longitude in file: [0:359.722222222222]

##############################################
test_that("1-1-2-2-1-1-1-x", {
lons.min <- 10
lons.max <- 20
lats.min <- 10
lats.max <- 20
suppressWarnings(
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '19821201',
             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),
  37
  )
  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
suppressWarnings(
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '19821201',
             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-2-2-1-1-1-x", {
lons.min <- -10
lons.max <- 10
lats.min <- 10
lats.max <- 20
suppressWarnings(
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '19821201',
             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-2-2-1-1-1-x", {
lons.min <- 10
lons.max <- -10
lats.min <- 10
lats.max <- 20
suppressWarnings(
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '19821201',
             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-2-2-1-1-1-x", {
lons.min <- 330
lons.max <- 350
lats.min <- 10
lats.max <- 20
suppressWarnings(
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '19821201',
             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-2-2-1-1-1-x", {
lons.min <- 350
lons.max <- 370
lats.min <- 10
lats.max <- 20
suppressWarnings(
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '19821201',
             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, 359.7222222),
  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
suppressWarnings(
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '19821201',
             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),
  37
  )
  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
suppressWarnings(
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '19821201',
             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),
  37
  )
})
##############################################
test_that("1-2-2-2-2-2-1-x", {
lons.min <- 20
lons.max <- 10
lats.min <- 10
lats.max <- 20
suppressWarnings(
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '19821201',
             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(1261)
  )
  expect_equal(
  as.vector((attr(res, 'Variables')$dat1$longitude))[37:38],
  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-2-2-2-3-1-x", {
lons.min <- 20
lons.max <- 10
lats.min <- 10
lats.max <- 20
suppressWarnings(
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '19821201',
             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(1261)
  )
  expect_equal(
  as.vector((attr(res, 'Variables')$dat1$longitude))[685:686],
  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-2-2-2-2-1-x", {
lons.min <- -10
lons.max <- -20
lats.min <- 10
lats.max <- 20
suppressWarnings(
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '19821201',
             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(1261)
  )
  expect_equal(
  as.vector((attr(res, 'Variables')$dat1$longitude))[1225:1226],
  c(340, 350),
  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
suppressWarnings(
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '19821201',
             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(1261)
  )
  expect_equal(
  as.vector((attr(res, 'Variables')$dat1$longitude))[577:578],
  c(-20, -10),
  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
suppressWarnings(
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '19821201',
             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-2-2-2-3-1-x", {
lons.min <- -20
lons.max <- -10
lats.min <- 10
lats.max <- 20
suppressWarnings(
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '19821201',
             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-2-2-2-2-1-x", {
lons.min <- 330
lons.max <- 350
lats.min <- 10
lats.max <- 20
suppressWarnings(
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '19821201',
             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-2-2-2-3-1-x", {
lons.min <- 330
lons.max <- 350
lats.min <- 10
lats.max <- 20
suppressWarnings(
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '19821201',
             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-2-2-2-2-1-x", {
lons.min <- 350
lons.max <- 370
lats.min <- 10
lats.max <- 20
suppressWarnings(
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '19821201',
             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(73),
  tolerance = 0.0001
  )
  expect_equal(
  as.vector((attr(res, 'Variables')$dat1$longitude))[37:38],
  c(10, 350),
  tolerance = 0.0001
  )
})
##############################################
test_that("1-6-2-2-2-3-1-x", {
lons.min <- 350
lons.max <- 370
lats.min <- 10
lats.max <- 20

suppressWarnings(
res <- Start(dat = list(list(path=path_exp)),
             var = 'psl',
             member = 'all',
             sdate = '19821201',
             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(73),
  tolerance = 0.0001
  )
  expect_equal(
  range((attr(res, 'Variables')$dat1$longitude)),
  c(-10, 10),
  tolerance = 0.0001
  )
})
