From 9a5269062c3ce0803b7415d2212df665902de743 Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Sat, 27 Oct 2018 22:48:27 -0700 Subject: [PATCH] Check that specieslayers has correct names --- Boreal_LBMRDataPrep.R | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/Boreal_LBMRDataPrep.R b/Boreal_LBMRDataPrep.R index 5f31ddb..76a2f69 100644 --- a/Boreal_LBMRDataPrep.R +++ b/Boreal_LBMRDataPrep.R @@ -102,6 +102,7 @@ defineModule(sim, list( doEvent.Boreal_LBMRDataPrep <- function(sim, eventTime, eventType, debug = FALSE) { if (eventType == "init") { + sim$specieslayers <- checkSpeciesLayerNames(sim$specieslayers) sim <- estimateParameters(sim) # schedule future event(s) @@ -122,6 +123,37 @@ doEvent.Boreal_LBMRDataPrep <- function(sim, eventTime, eventType, debug = FALSE # - keep event functions short and clean, modularize by calling subroutines from section below. ### template initialization + +checkSpeciesLayerNames <- function(specieslayers) { + if (!all(names(specieslayers) %in% c("Pice_mar", "Pice_gla", "Abie_sp", "Pinu_sp", "Popu_tre", "Mixed"))) { + speciesEquivalencyTable <- data.table(Common = c("Black.Spruce", "White.Spruce", "Fir", "Pine", "Deciduous", "Mixed"), + Latin = c("Pice_mar", "Pice_gla", "Abie_sp", "Pinu_sp", "Popu_tre", "Mixed")) + matches <- pmatch(names(specieslayers), speciesEquivalencyTable$Common) + if (any(is.na(matches))) { + stop("specieslayers is expecting the species names to be Pice_mar, Pice_gla, Abie_sp, Pinu_sp, Popu_tre, Mixed.", + "Please rename them to these names") + } + names(specieslayers) <- speciesEquivalencyTable$Latin[matches] + } + + + if (FALSE) { + # not needed, but this is for calculating vegetation type maps, from a species abundances stack + VTM <- Cache(pemisc::makeVegTypeMap, sim$specieslayers, sim$vegLeadingProportion) + tableCC <- table(factorValues(CCvtm, CCvtm[], att = "Species")) + tablePP <- table(factorValues(VTM, VTM[], att = "Species")) + + propCC <- round(tableCC/sum(tableCC),2) + propPP <- round(tablePP/sum(tablePP),2) + names(propPP) <- speciesEquivalencyTable$Common[pmatch(names(propPP), speciesEquivalencyTable$Latin)] + + propCC[match(names(propPP), names(propCC))] + propPP + } + + return(specieslayers) +} + estimateParameters <- function(sim) { # # ! ----- EDIT BELOW ----- ! # cPath <- cachePath(sim) @@ -581,7 +613,7 @@ Save <- function(sim) { writeRaster(specieslayersList$specieslayers, file.path(outputPath(sim), "speciesLayers.grd"), overwrite = TRUE) sim$specieslayers <- specieslayersList$specieslayers sim$speciesList <- specieslayersList$speciesList - } + } # 3. species maps sim$speciesTable <- Cache(prepInputs, "speciesTraits.csv",