context("Start() largest_dims_length check")
# When certain inner dim of files is not consistent, the parameter 'largest_dims_length' can
# be used to ensure the returned array has the largest length of inner dimensions.

test_that("1. inconsistent member length", {
# system5c3s: sdate = '19931101' has 25 members, '20200901' has 51 members.
# system3: 40 members.

repos <- list(list(name = 'system5c3s',
                   path = "/esarchive/exp/ecmwf/system5c3s/monthly_mean/g500_f12h/$var$_$sdate$.nc"),
              list(name = 'system3_m1-c3s',
                   path = "/esarchive/exp/cmcc/system3_m1-c3s/monthly_mean/g500_f12h/$var$_$sdate$.nc"))

# largest_dims_length = FALSE
suppressWarnings(
dat1 <- Start(dataset = repos,
              var = "g500", 
              sdate = c("19931101","20200901"),
              time = 'all',
              ensemble = 'all',
              latitude = values(list(10, 12)),      
              latitude_reorder = Sort(decreasing = T),              
              longitude = values(list(10, 11)),              
              longitude_reorder = CircularSort(0, 360),
              largest_dims_length = FALSE,
              synonims = list(longitude = c('lon', 'longitude'),                              
                              latitude = c('lat', 'latitude')),              
              return_vars = list(     time = NULL,
                                      latitude = 'dataset',                            
                                      longitude = 'dataset'),          
              retrieve = T)
)
  expect_equal(
  dim(dat1),
  c(dataset = 2, var = 1, sdate = 2, time = 6, ensemble = 40, latitude = 3, longitude = 2)
  )
  expect_equal(
  length(dat1[is.na(dat1)]),
  3000
  )
  expect_equal(
  sum(as.numeric(is.na(dat1[1, 1, 1, , , , ]))),
  540
  )
  expect_equal(
  sum(as.numeric(is.na(dat1[1, 1, 2, , , , ]))),
  540
  )

# largest_dims_length = TRUE
suppressWarnings(
dat2 <- Start(dataset = repos,
              var = "g500",
              sdate = c("19931101","20200901"),
              time = 'all',
              ensemble = 'all',
              latitude = values(list(10, 12)),
              latitude_reorder = Sort(decreasing = T),
              longitude = values(list(10, 11)),
              longitude_reorder = CircularSort(0, 360),
              largest_dims_length = TRUE,
              synonims = list(longitude = c('lon', 'longitude'),
                              latitude = c('lat', 'latitude')),
              return_vars = list(     time = NULL,
                                      latitude = 'dataset',
                                      longitude = 'dataset'),
              retrieve = T)
)
  expect_equal(
  dim(dat2),
  c(dataset = 2, var = 1, sdate = 2, time = 6, ensemble = 51, latitude = 3, longitude = 2)
  )
  expect_equal(
  length(dat2[is.na(dat2)]),
  3528
  )
  expect_equal(
  sum(as.numeric(is.na(dat2[1, 1, 1, , , , ]))),
  936
  )
  expect_equal(
  sum(as.numeric(is.na(dat2[1, 1, 2, , , , ]))),
  0
  )

# largest_dims_length = c(ensemble = 51)
suppressWarnings(
dat3 <- Start(dataset = repos,
              var = "g500",
              sdate = c("19931101","20200901"),
              time = 'all',
              ensemble = 'all',
              latitude = values(list(10, 12)),
              latitude_reorder = Sort(decreasing = T),
              longitude = values(list(10, 11)),
              longitude_reorder = CircularSort(0, 360),
              largest_dims_length = c(ensemble = 51),
              synonims = list(longitude = c('lon', 'longitude'),
                              latitude = c('lat', 'latitude')),
              return_vars = list(     time = NULL,
                                      latitude = 'dataset',
                                      longitude = 'dataset'),
              retrieve = T)
)
  expect_equal(
  dim(dat3),
  c(dataset = 2, var = 1, sdate = 2, time = 6, ensemble = 51, latitude = 3, longitude = 2)
  )
  expect_equal(
  length(dat3[is.na(dat3)]),
  3528
  )
  expect_equal(
  sum(as.numeric(is.na(dat3[1, 1, 1, , , , ]))),
  936
  )
  expect_equal(
  sum(as.numeric(is.na(dat3[1, 1, 2, , , , ]))),
  0
  )

# Compare dat1, dat2, and dat3
  expect_equal(
  mean(dat2, na.rm = T),
  mean(dat3, na.rm = T)
  )
  expect_equal(
  mean(dat1[, , , , 1:25, , ], na.rm = T),
  mean(dat2[, , , , 1:25, , ], na.rm = T)
  )
  expect_equal(
  mean(dat1[, , , , 1:25, , ], na.rm = T),
  mean(dat3[, , , , 1:25, , ], na.rm = T)
  )

})
