# ex1_8
context("Start() two dats in one call")

test_that("1. ex1_8", {

path_tas <- paste0('/esarchive/exp/ecearth/a1ua/cmorfiles/DCPP/EC-Earth-Consortium/EC-Earth3/',
                   'dcppA-hindcast/$member$/Amon/$var$/gr/v20190713/',
                   '$var$_Amon_EC-Earth3_dcppA-hindcast_s$sdate$-$member$_gr_$fyear$.nc')
path_tos <- paste0('/esarchive/exp/ecearth/a1ua/cmorfiles/DCPP/EC-Earth-Consortium/EC-Earth3/',
                   'dcppA-hindcast/$member$/Omon/$var$/gr/v20190713/',
                   '$var$_Omon_EC-Earth3_dcppA-hindcast_s$sdate$-$member$_gr_$fyear$.nc')
suppressWarnings(
data <- Start(dataset = list(list(path = path_tas), list(path = path_tos)),
              var = c('tas', 'tos'),
              sdate = paste0(1960:1962),
              fmonth = 1,
              lat = values(list(8, 10)),
              lon = values(list(8, 10)),
              fyear = 'all',
              member = indices(1),
              fyear_depends = 'sdate',
              fmonth_across = 'fyear',
              merge_across_dims = TRUE,
              synonims = list(fmonth = c('fmonth', 'time'),
                              lon = c('lon', 'longitude'),
                              lat = c('lat', 'latitude')),
              metadata_dims = 'var',
              return_vars = list(lat = 'dataset', lon = 'dataset'),
              retrieve = TRUE)
)

expect_equal(
dim(data),
c(dataset = 2, var = 2, sdate = 3, fmonth = 1, lat = 3, lon = 3, member = 1)
)
expect_equal(
sum(data, na.rm = T),
15396.7,
tolerance = 0.0001
)
expect_equal(
length(data[is.na(data)]),
54
)
expect_equal(
sum(is.na(data[1,1,,,,,])),
0
)
# Amon also has tos
expect_equal(
sum(is.na(data[1,2,,,,,])),
0
)
expect_equal(
sum(is.na(data[2,1,,,,,])),
27
)
# WRONG!!!! Omon should have tos. The value should be 0
expect_equal(
sum(is.na(data[2,2,,,,,])),
27
)

expect_equal(
names(attr(data, 'Variables')),
c("common", "dat1", "dat2")
)
expect_equal(
names(attr(data, 'Variables')$common),
c("fmonth", "tas", "tos")
)
expect_equal(
names(attr(data, 'Variables')$dat1),
c("lat", "lon")
)
expect_equal(
names(attr(data, 'Variables')$dat2),
c("lat", "lon")
)
expect_equal(
length(attr(data, 'Variables')$common$tas),
17
)
expect_equal(
length(attr(data, 'Variables')$common$tos),
16
)

})
