context("Start() metadata_dims check")

test_that("1. One data set, one var", {
  repos <- "/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc"
  data <- Start(dat = list(list(name = 'system5_m1', path = repos)),
                var = 'tas',
                sdate = '20170101',
                ensemble = indices(1),
                time = indices(1),
                lat = indices(1:10),
                lon = indices(1:10),
                synonims = list(lat = c('lat', 'latitude'),
                                lon = c('lon', 'longitude')),
                return_vars = list(time = 'sdate',
                                   longitude = 'dat',
                                   latitude = 'dat'),
                metadata_dims = 'dat',  # it can be omitted since it is automatically specified as 'dat'
                retrieve = T
               )

  expect_equal(
  length(attr(data, 'Variables')),
  2
  )
  expect_equal(
  names(attr(data, 'Variables')),
  c("common", "system5_m1")
  )
  expect_equal(
  names(attr(data, 'Variables')$common),
  c('time', 'tas')
  )
  expect_equal(
  names(attr(data, 'Variables')$system5_m1),
  c("longitude", "latitude")
  )
  expect_equal(
  length(attr(data, 'Variables')$common$tas),
  12
  )

})


test_that("2. Two data sets, one var", {
  repos <- "/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc"
  repos2 <- "/esarchive/exp/ecmwf/system4_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc"

  data <- Start(dat = list(list(name = 'system4_m1', path = repos2),
                           list(name = 'system5_m1', path = repos)),
                var = 'tas',
                sdate = '20170101',
                ensemble = indices(1),
                time = indices(1),
                lat = indices(1:10),
                lon = indices(1:10),
                synonims = list(lat = c('lat', 'latitude'),
                                lon = c('lon', 'longitude')),
                return_vars = list(time = 'sdate',
                                   longitude = 'dat',
                                   latitude = 'dat'),
                metadata_dims = 'dat',  # it can be omitted since it is automatically specified as 'dat'
                retrieve = T
               )

  expect_equal(
  length(attr(data, 'Variables')),
  3
  )
  expect_equal(
  names(attr(data, 'Variables')),
  c("common", "system4_m1", "system5_m1")
  )
  expect_equal(
  names(attr(data, 'Variables')$common),
  'time'
  )
  expect_equal(
  names(attr(data, 'Variables')$system4_m1),
  c("longitude", "latitude", "tas")
  )
  expect_equal(
  names(attr(data, 'Variables')$system5_m1),
  c("longitude", "latitude", "tas")
  )
  expect_equal(
  length(attr(data, 'Variables')$system5_m1$tas),
  12
  )
  expect_equal(
  length(attr(data, 'Variables')$system4_m1$tas),
  11
  )

})

test_that("3. One data set, two vars", {
  repos <- paste0('/esarchive/exp/ecearth/a1tr/cmorfiles/CMIP/EC-Earth-Consortium/',
                  'EC-Earth3/historical/r24i1p1f1/Amon/$var$/gr/v20190312/',
                  '$var$_Amon_EC-Earth3_historical_r24i1p1f1_gr_185001-185012.nc')
  var <- c('tas', 'clt')
  sdate <- '20170101'

  data <- Start(dat = repos,
                var = var, 
                time = indices(1),
                lat = indices(1:10),
                lon = indices(10:19), 
                return_vars = list(lat = NULL, lon = NULL),
                metadata_dims = 'var',
                synonims = list(lat = c('lat', 'latitude'),
                                lon = c('lon', 'longitude')),
                retrieve = TRUE
               )

  expect_equal(
  length(attr(data, 'Variables')),
  2
  )
  expect_equal(
  names(attr(data, 'Variables')),
  c("common", "dat1")
  )
  expect_equal(
  names(attr(data, 'Variables')$common),
  c('lat', 'lon', 'tas', 'clt')
  )
  expect_equal(
  is.null(attr(data, 'Variables')$dat1),
  TRUE
  )
  expect_equal(
  length(attr(data, 'Variables')$common$tas),
  17
  )
  expect_equal(
  length(attr(data, 'Variables')$common$clt),
  16
  )

})

test_that("4. Two data sets, two vars", {
  repos <- "/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc"
  repos2 <- "/esarchive/exp/ecmwf/system4_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc"

  data <- Start(dat = list(list(name = 'system4_m1', path = repos2),
                           list(name = 'system5_m1', path = repos)),
                var = c('tas', 'sfcWind'),
                sdate = '20170101',
                ensemble = indices(1),
                time = indices(1),
                lat = indices(1:10),
                lon = indices(1:10),
                synonims = list(lat = c('lat', 'latitude'),
                                lon = c('lon', 'longitude')),
                return_vars = list(time = 'sdate',
                                   longitude = 'dat',
                                   latitude = 'dat'),
                metadata_dims = 'dat',
                retrieve = T
               )

  expect_equal(
  length(attr(data, 'Variables')),
  3
  )
  expect_equal(
  names(attr(data, 'Variables')),
  c("common", "system4_m1", "system5_m1")
  )
  expect_equal(
  names(attr(data, 'Variables')$common),
  'time'
  )
  expect_equal(
  names(attr(data, 'Variables')$system4_m1),
  c("longitude", "latitude", "tas")
  )
  expect_equal(
  names(attr(data, 'Variables')$system5_m1),
  c("longitude", "latitude", "tas")
  )
  expect_equal(
  length(attr(data, 'Variables')$system5_m1$tas),
  12
  )
  expect_equal(
  length(attr(data, 'Variables')$system4_m1$tas),
  11
  )


  data <- Start(dat = list(list(name = 'system4_m1', path = repos2),
                           list(name = 'system5_m1', path = repos)),
                var = c('tas', 'sfcWind'),
                sdate = '20170101',
                ensemble = indices(1),
                time = indices(1),
                lat = indices(1:10),
                lon = indices(1:10),
                synonims = list(lat = c('lat', 'latitude'),
                                lon = c('lon', 'longitude')),
                return_vars = list(time = 'sdate',
                                   longitude = 'dat',
                                   latitude = 'dat'),
                metadata_dims = c('dat', 'var'),
                retrieve = T
               )
  expect_equal(
  length(attr(data, 'Variables')),
  3
  )
  expect_equal(
  names(attr(data, 'Variables')),
  c("common", "system4_m1", "system5_m1")
  )
  expect_equal(
  names(attr(data, 'Variables')$common),
  'time'
  )
  expect_equal(
  names(attr(data, 'Variables')$system4_m1),
  c("longitude", "latitude", "tas", "sfcWind")
  )
  expect_equal(
  names(attr(data, 'Variables')$system5_m1),
  c("longitude", "latitude", "tas", "sfcWind")
  )
  expect_equal(
  length(attr(data, 'Variables')$system5_m1$tas),
  12
  )
  expect_equal(
  length(attr(data, 'Variables')$system5_m1$sfcWind),
  12
  )
  expect_equal(
  length(attr(data, 'Variables')$system4_m1$tas),
  11
  )
  expect_equal(
  length(attr(data, 'Variables')$system4_m1$sfcWind),
  11
  )

})

test_that("5. Specify metadata_dims with another file dimension", {
  repos <- "/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc"
  repos2 <- "/esarchive/exp/ecmwf/system4_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc"

  data <- Start(dat = list(list(name = 'system4_m1', path = repos2),
                           list(name = 'system5_m1', path = repos)),
                var = c('tas', 'sfcWind'),
                sdate = c('20150101', '20160101', '20170101'),
                ensemble = indices(1),
                time = indices(1),
                lat = indices(1:10),
                lon = indices(1:10),
                synonims = list(lat = c('lat', 'latitude'),
                                lon = c('lon', 'longitude')),
                return_vars = list(time = 'sdate',
                                   longitude = 'dat',
                                   latitude = 'dat'),
                metadata_dims = 'sdate',
                retrieve = T
               )
  expect_equal(
  length(attr(data, 'Variables')),
  3
  )
  expect_equal(
  names(attr(data, 'Variables')),
  c("common", "system4_m1", "system5_m1")
  )
  expect_equal(
  names(attr(data, 'Variables')$common),
  c('time', 'tas', 'tas', 'tas')
  )
  expect_equal(
  names(attr(data, 'Variables')$system4_m1),
  c("longitude", "latitude")
  )
  expect_equal(
  names(attr(data, 'Variables')$system5_m1),
  c("longitude", "latitude")
  )
  expect_equal(
  length(attr(data, 'Variables')$common$tas),
  12
  )

})


test_that("6. One data set, two vars from one file", {
mask_path <- '/esarchive/autosubmit/con_files/mask.regions.Ec3.0_O1L46.nc'

data <- Start(repos = mask_path,
              var = c('nav_lon', 'nav_lat'),
              t = 'first',
              z = 'first',
              x = 'all',
              y = 'all',
              return_vars = list(var_names = NULL),
              var_var = 'var_names',
              retrieve = T)

  expect_equal(
  length(attr(data, 'Variables')),
  2
  )
  expect_equal(
  names(attr(data, 'Variables')),
  c("common", "dat1")
  )
  expect_equal(
  names(attr(data, 'Variables')$common),
  c("var_names", "nav_lon", "nav_lat")
  )
  expect_equal(
  is.null(attr(data, 'Variables')$dat1),
  TRUE
  )
  expect_equal(
  length(attr(data, 'Variables')$common$nav_lat),
  8
  )

})
