context("multiStart() NA insertion")
# Same case as ex2_11.
#---------------------------------------------------------------

  # Define paths
  # HadGEM3 daily data (360_day)
  path_hadgem3 <- paste0('/esarchive/exp/CMIP6/dcppA-hindcast/hadgem3-gc31-mm/',
                         'cmip6-dcppA-hindcast_i1p1/DCPP/MOHC/HadGEM3-GC31-MM/',
                         'dcppA-hindcast/r1i1p1f2/day/$var$/gn/v20200101/',
                         '$var$_day_HadGEM3-GC31-MM_dcppA-hindcast_s$sdate$-r1i1p1f2_gn_$fyear$.nc')
  ## tasmax_day_HadGEM3-GC31-MM_dcppA-hindcast_s2000-r1i1p1f2_gn_20001101-20101230.nc

  # MPI-ESM1.2-HR (standard)
  path_mpi_esm <- paste0('/esarchive/exp/CMIP6/dcppA-hindcast/mpi-esm1-2-hr/',
                         'cmip6-dcppA-hindcast_i1p1/DCPP/MPI-M/MPI-ESM1-2-HR/',
                         'dcppA-hindcast/r1i1p1f1/day/$var$/gn/v20200101/',
                         '$var$_day_MPI-ESM1-2-HR_dcppA-hindcast_s$sdate$-r1i1p1f1_gn_$fyear$.nc')
  ## tasmax_day_MPI-ESM1-2-HR_dcppA-hindcast_s2000-r1i1p1f1_gn_20001101-20101231.nc

  # Define selectors for each dataset
  sdate <- '2000'
  fyear_hadgem3 <- paste0(sdate, '1101-', as.numeric(sdate) + 10, '1230')
  fyear_mpi_esm <- paste0(sdate, '1101-', as.numeric(sdate) + 10, '1231')
  # To align the time steps, insert an NA in hadgem3
  time_hadgem3 <- indices(c(60, NA, 61)) #12/30, NA, 1/1
  time_mpi_esm <- indices(60:62) #12/30, 12/31, 1/1




test_that("1. retrieve = T", {

suppressWarnings(
res <- multiStart(
         dat = list(list(name = 'hadgem3', path = path_hadgem3),
                    list(name = 'mpi_esm', path = path_mpi_esm)),
         var = 'tasmax',
         sdate = '2000',
         fyear = list(list(name = 'hadgem3', fyear = fyear_hadgem3),
                      list(name = 'mpi_esm', fyear = fyear_mpi_esm)),
         time = list(list(name = 'hadgem3', time = time_hadgem3),
                     list(name = 'mpi_esm', time = time_mpi_esm)),
         lat = values(list(-10, 10)),
         lon = values(list(0, 10)),
         transform = CDORemapper,
         transform_params = list(grid = 'r360x181',
                                 method = 'conservative',
                                 crop = c(0, 10, -10, 10)),
         transform_vars = c('lat', 'lon'),
         return_vars = list(lat = NULL, lon = NULL, time = 'sdate'),
         retrieve = TRUE)
)


expect_equal(
dim(res),
c(dat = 2, var = 1, sdate = 1, fyear = 1, time = 3, lat = 21, lon = 11)
)
expect_equal(
length(which(is.na(res))),
231
)
expect_equal(
res[1, 1, 1, 1, , 1, 1],
c(297.5703, NA, 297.5506),
tolerance = 0.0001
)

})

test_that("2. retrieve = F; Compute locally", {

suppressWarnings(
res <- multiStart(
         dat = list(list(name = 'hadgem3', path = path_hadgem3),
                    list(name = 'mpi_esm', path = path_mpi_esm)),
         var = 'tasmax', sdate = '2000',
         fyear = list(list(name = 'hadgem3', fyear = fyear_hadgem3),
                      list(name = 'mpi_esm', fyear = fyear_mpi_esm)),
         time = list(list(name = 'hadgem3', time = time_hadgem3),
                     list(name = 'mpi_esm', time = time_mpi_esm)),
         lat = indices(3:5), lon = indices(1),
         return_vars = list(lat = 'dat', lon = 'dat', time = 'dat'),
         retrieve = FALSE)
)

#---------------------------------------
expect_equal(
attr(res[[1]], 'Dimensions'),
c(dat = 1, var = 1, sdate = 1, fyear = 1, time = 3, lat = 3, lon = 1)
)
expect_equal(
attr(res[[2]], 'Dimensions'),
c(dat = 1, var = 1, sdate = 1, fyear = 1, time = 3, lat = 3, lon = 1)
)
expect_equal(
attr(res[[1]], 'na_pos_record')[[1]]$time,
2
)
expect_equal(
sum(unlist(attr(res[[2]], 'na_pos_record')[[1]])),
0
)

#---------------------------------------

func_sum <- function(x, y) {
  return(x + y)
}

# 'time' and 'fyear' can also be the operational dimension
step <- Step(func_sum, target_dims = list(hadgem3 = c('var', 'lon'),
                                          mpi_esm = c('var', 'lon')),
                       output_dims = c('var', 'lon'))

wf <- AddStep(res, step)

output1 <- Compute(wf, chunks = list(lat = 2))
output2 <- Compute(wf, chunks = list(lat = 2, time = 2))

expect_equal(
dim(output1$output1),
c(var = 1, lon = 1, time = 3, dat = 1, sdate = 1, fyear = 1, lat = 3)
)
expect_equal(
dim(output2$output1),
c(var = 1, lon = 1, time = 3, dat = 1, sdate = 1, fyear = 1, lat = 3)
)
expect_equal(
output1$output1[1,1,,1,1,1,1],
c(503.0177, NA, 507.7226),
tolerance = 0.001
)
expect_equal(
all(is.na(output1$output1[1,1,2,1,1,1,])),
TRUE
)

expect_equal(
as.vector(output1$output1),
as.vector(output2$output1)
)



})
