tests/testthat/test-PrepFire.R

context("Fire data preparation")

test_that("extract date from CCI fire image name", {
  x <- '2014_09_01_ConfidenceLevel'
  dt <- 20140901
  extracted <- extract_date_fire(x)

  expect_equal(dt, extracted, tolerance = 1e-6)
})

test_that("extract fire dates from CCI fire time series", {
  ts_conf <- c(0,95,2,5,16,50,2,14,6,12,14,96)
  ts_doy <- c(0,33,0,0,0,0,0,0,6,0,0,364)
  dts <- c('2001-01-01','2001-02-01','2001-03-01','2001-04-01','2001-05-01','2001-06-01',
           '2001-07-01','2001-08-01','2001-09-01','2001-10-01','2001-11-01','2001-12-01')
  conf_level <- 90
  extracted <- getFireDate(ts_conf, ts_doy, dts, conf_level)
  fire_dts <- as.Date(c('2001-02-02','2001-12-30'))

  expect_equal(fire_dts, extracted, tolerance = 1e-6)
})

test_that("tidy CCI fire data, downloaded from GEE", {
  # input data
  dat_fre <- data.frame(
    id = as.character(c(1:5)),
    ecoReg = c("439", "443", "446", "464", "465"),
    coords = c("-44.73352786322798, -22.53985954585717", "-41.39313945937375, -19.29783891124197",
               "-69.9654938653852, 0.27016355552512544", "-61.11340684981286, 4.481013076022512",
               "-51.65145304226181, 3.7299310127821945"),
    img_doy_ = rep(paste0("'2000_",sprintf('%02.f',1:12),"_01_BurnDate'", collapse = ', '),5),
    doy_fire_ = c('0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0',
                  '0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0',
                  '0, 0, 0, 0, -1, -1, 0, 0, 0, 0, -1, -1',
                  '0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0',
                  '0, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0'),
    img_conf_ = rep(paste0("'2000_",sprintf('%02.f',1:12),"_01_ConfidenceLevel'", collapse = ', '),5),
    conf_fire_ = c("1, 1, 1, 1, 27, 1, 1, 2, 1, 1, 1, 1",
                   "1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1",
                   "1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0",
                   "19, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1",
                   "1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 5, 3"),
    nimg = c("831", "690", "639", "648", "159")
  )

  # tidy data
  fire_tidy <- tidy_fire_date(dat_fre)
  # expected result for fire doy
  expected_doy <- data.frame(
    id = as.character(c(1:5)),
    ecoReg = c("439", "443", "446", "464", "465"),
    coords = c("-44.73352786322798, -22.53985954585717", "-41.39313945937375, -19.29783891124197",
               "-69.9654938653852, 0.27016355552512544", "-61.11340684981286, 4.481013076022512",
               "-51.65145304226181, 3.7299310127821945"),
    nimg = as.character(rep(12,5)),
    '2000-01-01' = rep(0,5),
    '2000-02-01' = c(0,0,0,0,-1),
    '2000-03-01' = c(0,0,0,0,-1),
    '2000-04-01' = c(0,0,0,0,0),
    '2000-05-01' = c(0,0,-1,0,0),
    '2000-06-01' = c(0,0,-1,-1,0),
    '2000-07-01' = c(0,0,0,0,0),
    '2000-08-01' = c(0,0,0,0,0),
    '2000-09-01' = c(0,0,0,0,0),
    '2000-10-01' = c(0,0,0,0,0),
    '2000-11-01' = c(0,0,-1,0,0),
    '2000-12-01' = c(0,0,-1,0,0)
  )
  names(expected_doy) <- c('id', 'ecoReg', 'coords', 'nimg', paste0('2000-',sprintf('%02.f',1:12),'-01'))
  # expected results for fire confidence
  expected_conf <- data.frame(
    id = as.character(c(1:5)),
    ecoReg = c("439", "443", "446", "464", "465"),
    coords = c("-44.73352786322798, -22.53985954585717", "-41.39313945937375, -19.29783891124197",
               "-69.9654938653852, 0.27016355552512544", "-61.11340684981286, 4.481013076022512",
               "-51.65145304226181, 3.7299310127821945"),
    nimg = as.character(rep(12,5)),
    '2000-01-01' = c(1,1,1,19,1),
    '2000-02-01' = c(1,1,1,1,0),
    '2000-03-01' = c(1,1,1,1,0),
    '2000-04-01' = c(1,1,1,1,1),
    '2000-05-01' = c(27,1,0,1,1),
    '2000-06-01' = c(1,1,0,0,1),
    '2000-07-01' = c(1,1,1,1,1),
    '2000-08-01' = c(2,1,1,1,1),
    '2000-09-01' = c(1,1,1,1,1),
    '2000-10-01' = c(1,1,1,1,1),
    '2000-11-01' = c(1,1,0,1,5),
    '2000-12-01' = c(1,1,0,1,3)
  )
  names(expected_conf) <- c('id', 'ecoReg', 'coords', 'nimg', paste0('2000-',sprintf('%02.f',1:12),'-01'))

  expect_equal(fire_tidy$fire_doy, expected_doy, tolerance = 1e-6)
  expect_equal(fire_tidy$fire_conf, expected_conf, tolerance = 1e-6)
})
RETURN-project/GEE.aux documentation built on Dec. 18, 2021, 8:46 a.m.