test-Start-calendar.R 5.72 KB
Newer Older
context("Start() different calendar")

test_that("1. 360_day, daily", {

  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')
 
 sdate <-  c('2000', '2001')
 fyear_hadgem3 <- indices(1)

  suppressWarnings(
    data <- Start(dat = path_hadgem3,
                  var = 'tasmax',
                  sdate = sdate,
                  fyear = fyear_hadgem3,
                  fyear_depends = 'sdate',
                  time = indices(1:3),
                  lat = indices(1), lon = indices(1),
                  return_vars = list(lat = 'dat', lon = 'dat', time = 'sdate'),
                  retrieve = FALSE)
  )

expect_equal(
  dim(attr(data, 'Variables')$common$time),
  c(sdate = 2, time = 3)
)
 time <- c(as.POSIXct('2000-11-01 12:00:00', tz = 'UTC'), 
           as.POSIXct('2000-11-02 12:00:00', tz = 'UTC'), 
           as.POSIXct('2000-11-03 12:00:00', tz = 'UTC'))
 attr(time, "tzone") <- "UTC"
expect_equal(
  attr(data, 'Variables')$common$time[1, ],
  time
)
 time <- c(
           as.POSIXct('2001-11-01 12:00:00', tz = 'UTC'),
           as.POSIXct('2001-11-02 12:00:00', tz = 'UTC'),
           as.POSIXct('2001-11-03 12:00:00', tz = 'UTC'))
 attr(time, "tzone") <- "UTC"
expect_equal(
  attr(data, 'Variables')$common$time[2, ],
  time
)


})

test_that("2. 365_day, daily", {
path_bcc_csm2 <- '/esarchive/exp/CMIP6/dcppA-hindcast/bcc-csm2-mr/cmip6-dcppA-hindcast_i1p1/DCPP/BCC/BCC-CSM2-MR/dcppA-hindcast/r1i1p1f1/day/$var$/gn/v20200408/$var$_day_BCC-CSM2-MR_dcppA-hindcast_s$sdate$-r1i1p1f1_gn_19800101-19891231.nc'

  data <- Start(dat = path_bcc_csm2,
                var = 'tasmax',
                sdate = '1980',
                time = indices(1:3),
                lat = indices(1),
                lon = indices(1),
                return_vars = list(lat = 'dat', lon = 'dat', time = 'sdate'),
                retrieve = FALSE) 

expect_equal(
  dim(attr(data, 'Variables')$common$time),
  c(sdate = 1, time = 3)
)
 time <- c(as.POSIXct('1980-01-01 12:00:00', tz = 'UTC'),
           as.POSIXct('1980-01-02 12:00:00', tz = 'UTC'),
           as.POSIXct('1980-01-03 12:00:00', tz = 'UTC'))
 attr(time, "tzone") <- "UTC"
expect_equal(
  attr(data, 'Variables')$common$time[1, ],
  time
)


})


test_that("3. gregorian/standard, daily", {

  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')

  var <- 'tasmax'
  sdate <- '2000'
  fyear_mpi_esm <- paste0(sdate, '1101-', as.numeric(sdate) + 10, '1231')

  data <- Start(dat = path_mpi_esm,
                var = var,
                sdate = sdate,
                fyear = fyear_mpi_esm,
                fyear_depends = 'sdate',
                time = indices(1:3),
                lat = indices(1),
                lon = indices(1),
                return_vars = list(lat = 'dat', lon = 'dat', time = 'sdate'),
                retrieve = FALSE) 

expect_equal(
  dim(attr(data, 'Variables')$common$time),
  c(sdate = 1, time = 3)
)
 time <- c(as.POSIXct('2000-11-01 12:00:00', tz = 'UTC'),
           as.POSIXct('2000-11-02 12:00:00', tz = 'UTC'),
           as.POSIXct('2000-11-03 12:00:00', tz = 'UTC'))
 attr(time, "tzone") <- "UTC"
expect_equal(
  attr(data, 'Variables')$common$time[1, ],
  time
)


})

test_that("4. gregorian/standard, monthly", {

  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/Amon/$var$/gn/v20200320/',
                         '$var$_Amon_MPI-ESM1-2-HR_dcppA-hindcast_s$sdate$-r1i1p1f1_gn_200011-201012.nc')
  sdate <- '2000'
  fyear_mpi_esm <- paste0(sdate, '1101-', as.numeric(sdate) + 10, '1231')

  data <- Start(dat = path_mpi_esm,
                var = 'tasmax',
                sdate = '2000',
                time = indices(1:3),
                lat = indices(1),
                lon = indices(1),
                return_vars = list(lat = 'dat', lon = 'dat', time = 'sdate'),
                retrieve = FALSE)

expect_equal(
  dim(attr(data, 'Variables')$common$time),
  c(sdate = 1, time = 3)
)
 time <- c(as.POSIXct('2000-11-16 00:00:00', tz = 'UTC'),
           as.POSIXct('2000-12-16 12:00:00', tz = 'UTC'),
           as.POSIXct('2001-01-16 12:00:00', tz = 'UTC'))
 attr(time, "tzone") <- "UTC"
expect_equal(
  attr(data, 'Variables')$common$time[1, ],
  time
)

})

test_that("4. gregorian/standard, 6hrly", {
  repos_obs <- paste0('/esarchive/recon/ecmwf/erainterim/6hourly/',
                      '$var$/$var$_199405.nc')
  date <- paste0('1994-05-', sprintf('%02d', 1:31), ' 00:00:00')
  date <- as.POSIXct(date, tz = 'UTC')
#  attr(date, 'tzone') <- 'UTC'
  data <- Start(dat = repos_obs,
                var = 'tas',
                time = date, 
                latitude = indices(1),
                longitude = indices(1),
                return_vars = list(latitude = NULL,
                                   longitude = NULL,
                                   time = NULL),
                retrieve = TRUE)

expect_equal(
  as.vector(attr(data, 'Variables')$common$time[1:31]),
  as.vector(date)
)
expect_equal(
  as.vector(data)[1:5],
  c(255.0120, 256.8095, 254.3654, 254.6059, 257.0551),
  tolerance = 0.0001
)
expect_equal(
  as.vector(data)[length(data)],
  c(268.2216),
  tolerance = 0.0001
)
})