Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Description: Implementation of several components of the Carbon Budget Model of
Canadian Forest Service (v3).
URL:
https://github.com/PredictiveEcology/CBMutils
Version: 2.5.4.9001
Version: 2.5.4.9002
Authors@R: c(
person("Céline", "Boisvenue", email = "celine.boisvenue@nrcan-rncan.gc.ca", role = c("aut", "cre")),
person("Alex M", "Chubaty", email = "achubaty@for-cast.ca", role = c("aut"), comment = c(ORCID = "0000-0001-7146-8135")),
Expand Down Expand Up @@ -44,12 +44,10 @@ Suggests:
FNN,
qs2,
reproducible,
SpaDES.core,
rmarkdown,
testthat
Remotes:
PredictiveEcology/reproducible@development,
PredictiveEcology/SpaDES.core@development,
PredictiveEcology/CBM4r@main
VignetteBuilder: knitr, rmarkdown
Encoding: UTF-8
Expand Down
6 changes: 2 additions & 4 deletions R/CBM-plots_mapNPP.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,14 @@ cbm4MapNPP <- function(cbm4_results, years = NULL, yearStart = 1){
#' @export
simMapNPP <- function(simCBM, year = NULL, useCache = TRUE){

if (is.null(year)){
year <- SpaDES.core::convertTimeunit(SpaDES.core::times(simCBM)$current, "year")
}
if (is.null(year)) year <- simYears(simCBM)$current

if (!is.null(simCBM$CBM4data)){

cbm4MapNPP(
simCBM$CBM4data,
years = year,
yearStart = SpaDES.core::start(simCBM)
yearStart = simYears(simCBM)$start
)[[1]]

}else{
Expand Down
6 changes: 2 additions & 4 deletions R/CBM-plots_mapTotalCarbon.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,14 @@ cbm4MapTotalCarbon <- function(cbm4_results, years = NULL, yearStart = 1){
#' @export
simMapTotalCarbon <- function(simCBM, year = NULL, useCache = TRUE){

if (is.null(year)){
year <- SpaDES.core::convertTimeunit(SpaDES.core::times(simCBM)$current, "year")
}
if (is.null(year)) year <- simYears(simCBM)$current

if (!is.null(simCBM$CBM4data)){

cbm4MapTotalCarbon(
simCBM$CBM4data,
years = year,
yearStart = SpaDES.core::start(simCBM)
yearStart = simYears(simCBM)$start
)[[1]]

}else{
Expand Down
4 changes: 2 additions & 2 deletions R/CBM-plots_plotEmissionsProducts.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ simPlotEmissionsProducts <- function(simCBM, years = NULL, useCache = TRUE){
cbm4PlotEmissionsProducts(
simCBM$CBM4data,
years = years,
yearStart = SpaDES.core::start(simCBM)
yearStart = simYears(simCBM)$start
)

}else{

if (is.null(years)) years <- SpaDES.core::start(simCBM):SpaDES.core::end(simCBM)
if (is.null(years)) years <- with(simYears(simCBM), start:end)

spadesCBMdbPlotEmissionsProducts(
simCBM$spadesCBMdb,
Expand Down
6 changes: 3 additions & 3 deletions R/CBM-plots_plotPoolProportions.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ simPlotPoolProportions <- function(simCBM, years = NULL, useCache = TRUE){

cbm4PlotPoolProportions(
simCBM$CBM4data,
years = years,
yearStart = SpaDES.core::start(simCBM)
years = years,
yearStart = simYears(simCBM)$start
)

}else{

if (is.null(years)) years <- c(0, SpaDES.core::start(simCBM):SpaDES.core::end(simCBM))
if (is.null(years)) years <- c(0, with(simYears(simCBM), start:end))

spadesCBMdbPlotPoolProportions(
simCBM$spadesCBMdb,
Expand Down
4 changes: 1 addition & 3 deletions R/spadesCBMdb_dbReadRaw.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
#' @export
simCBMdbReadRaw <- function(simCBM, year, table, ...){

if (missing(year)){
year <- SpaDES.core::convertTimeunit(SpaDES.core::times(simCBM)$current, "year")
}
if (missing(year)) year <- simYears(simCBM)$current

spadesCBMdbReadRaw(simCBM$spadesCBMdb, year = year, table = table, ...)
}
Expand Down
10 changes: 3 additions & 7 deletions R/spadesCBMdb_dbReadSummary.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,9 @@
simCBMdbReadSummary <- function(simCBM, summary, by = "cohortID", units = "t/ha",
year = NULL, years = NULL, useCache = TRUE){

if (is.null(year) & by != "year"){
year <- SpaDES.core::convertTimeunit(SpaDES.core::times(simCBM)$current, "year")
}
if (is.null(years) & by == "year"){
simTimes <- lapply(SpaDES.core::times(simCBM)[c("start", "end")], SpaDES.core::convertTimeunit, "year")
years <- simTimes$start:simTimes$end
}
if (is.null(year) & by != "year") year <- simYears(simCBM)$current
if (is.null(years) & by == "year") years <- with(simYears(simCBM), start:end)

spadesCBMdbReadSummary(
simCBM$spadesCBMdb,
summary = summary,
Expand Down
4 changes: 1 addition & 3 deletions R/spadesCBMdb_dbReadTable.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
#' @export
simCBMdbReadTable <- function(simCBM, year, table, useCache = TRUE){

if (missing(year)){
year <- SpaDES.core::convertTimeunit(SpaDES.core::times(simCBM)$current, "year")
}
if (missing(year)) year <- simYears(simCBM)$current

spadesCBMdbReadTable(simCBM$spadesCBMdb, year = year, table = table, useCache = useCache)
}
Expand Down
4 changes: 1 addition & 3 deletions R/spadesCBMdb_dbWrite.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
#' @export
simCBMdbWrite <- function(simCBM, year, parameters = TRUE, state = TRUE, flux = TRUE, pools = TRUE){

if (missing(year)){
year <- SpaDES.core::convertTimeunit(SpaDES.core::times(simCBM)$current, "year")
}
if (missing(year)) year <- simYears(simCBM)$current

spadesCBMdbWrite(
simCBM$spadesCBMdb,
Expand Down
21 changes: 21 additions & 0 deletions R/utils-simList.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

# Get simList times in years without using SpaDES.core functions
simYears <- function(sim){

simTimes <- sim@simtimes[c("current", "start", "end")]
simTimes <- lapply(simTimes, function(st){
if (attr(st, "unit") == "second"){
st <- st / (60 * 60 * 24 * 365.25)
attr(st, "unit") <- "year"
}
return(st)
})

if (!all(sapply(simTimes, attr, "unit") == "year") |
any(simTimes$start > c(sim$simTimes$current, sim$simTimes$end)) |
any(simTimes$end < c(sim$simTimes$current, sim$simTimes$start))) stop(
"Converting sim@simtimes to unit 'year' failed")

return(simTimes)
}

2 changes: 2 additions & 0 deletions tests/testthat/devel-runTests.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ testthat::test_local(filter = "Boudewyn")
testthat::test_local(filter = "CBM-DB")
testthat::test_local(filter = "CBM-plots")
testthat::test_local(filter = "CBM-tools")
testthat::test_local(filter = "Data")
testthat::test_local(filter = "Misc")
testthat::test_local(filter = "Python")
testthat::test_local(filter = "spadesCBMdb")

# Run individual tests
testthat::test_local(filter = "Boudewyn_cumPoolsCreate")
Expand Down
3 changes: 1 addition & 2 deletions tests/testthat/test-spadesCBMdb_plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ masterRaster <- terra::rast(
ncols = 1950, xmax = 1950 * 30,
nrows = 1900, ymax = 1900 * 30)

simCBM <- SpaDES.core::simInit(
times = list(start = 1985, end = 2011),
simCBM <- list(
spadesCBMdb = spadesCBMdb,
masterRaster = masterRaster
)
Expand Down
27 changes: 17 additions & 10 deletions tests/testthat/test-spadesCBMdb_read-write.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ test_that("simCBMdbWrite", {

spadesCBMdbTemp <- file.path(testDirs$temp$outputs, "spadesCBMdb", "simCBMdbWrite")

simCBM <- SpaDES.core::simInit(times = list(start = 1985, end = 2011))
simCBM$spadesCBMdb <- spadesCBMdbTemp
simCBM$cbm_vars <- list(key = "test", flux = "test", pools = "test")
simCBM <- list(
spadesCBMdb = spadesCBMdbTemp,
cbm_vars = list(key = "test", flux = "test", pools = "test")
)

simCBMdbWrite(
simCBM,
simCBM, year = 1985,
parameters = FALSE,
state = FALSE,
flux = TRUE,
Expand Down Expand Up @@ -82,8 +83,9 @@ test_that("spadesCBMdbReadRaw", {

test_that("simCBMdbReadRaw", {

simCBM <- SpaDES.core::simInit(times = list(start = 1985, end = 2011))
simCBM$spadesCBMdb <- spadesCBMdb
simCBM <- list(
spadesCBMdb = spadesCBMdb
)

key2011 <- simCBMdbReadRaw(
simCBM = simCBM,
Expand All @@ -98,6 +100,7 @@ test_that("simCBMdbReadRaw", {

pools1985 <- simCBMdbReadRaw(
simCBM = simCBM,
year = 1985,
table = "pools"
)
expect_is(pools1985, "data.table")
Expand Down Expand Up @@ -150,11 +153,13 @@ test_that("spadesCBMdbReadTable", {

test_that("simCBMdbReadTable", {

simCBM <- SpaDES.core::simInit(times = list(start = 1985, end = 2011))
simCBM$spadesCBMdb <- spadesCBMdb
simCBM <- list(
spadesCBMdb = spadesCBMdb
)

pools1985 <- simCBMdbReadTable(
simCBM = simCBM,
year = 1985,
table = "pools",
useCache = FALSE
)
Expand All @@ -168,6 +173,7 @@ test_that("simCBMdbReadTable", {
expect_equal(
simCBMdbReadTable(
simCBM = simCBM,
year = 1985,
table = "pools",
useCache = TRUE
),
Expand Down Expand Up @@ -252,8 +258,9 @@ test_that("spadesCBMdbReadSummary", {

test_that("simCBMdbReadSummary", {

simCBM <- SpaDES.core::simInit(times = list(start = 1985, end = 2011))
simCBM$spadesCBMdb <- spadesCBMdb
simCBM <- list(
spadesCBMdb = spadesCBMdb
)

pools1985 <- simCBMdbReadSummary(
simCBM = simCBM,
Expand Down
Loading