From 0fad6e6909a5e51cf0f6a890c9030055d8e37432 Mon Sep 17 00:00:00 2001 From: Alex Chubaty Date: Tue, 13 Nov 2018 13:09:12 -0700 Subject: [PATCH 1/6] speciesTable: longer dispersal for white spruce with https://github.com/eliotmcintire/LandWeb/issues/96 --- Boreal_LBMRDataPrep.R | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Boreal_LBMRDataPrep.R b/Boreal_LBMRDataPrep.R index 76987f0..33713c5 100644 --- a/Boreal_LBMRDataPrep.R +++ b/Boreal_LBMRDataPrep.R @@ -326,12 +326,16 @@ estimateParameters <- function(sim) { ## filter table to existing species layers speciesTable <- speciesTable[species %in% names(sim$specieslayers)] + ## adjust some species-specific values + speciesTable[species == "Pice_gla", seeddistance_max := 2000] ## (see LandWeb#96) + message("10: ", Sys.time()) # Take the smallest values of every column, within species, because it is northern boreal forest speciesTable <- speciesTable[species %in% names(sim$specieslayers), ][ , ':='(species1 = NULL, species2 = NULL)] %>% .[, lapply(.SD, function(x) if (is.numeric(x)) min(x, na.rm = TRUE) else x[1]), by = "species"] + sim$species <- speciesTable initialCommunities <- simulationMaps$initialCommunity[, .(mapcode, description = NA, species)] set(initialCommunities, NULL, paste("age", 1:15, sep = ""), NA) @@ -339,7 +343,7 @@ estimateParameters <- function(sim) { message("11: ", Sys.time()) ## filter communities to species that have traits - initialCommunities <- initialCommunities[initialCommunities$species %in% speciesTable$species,] + initialCommunities <- initialCommunities[initialCommunities$species %in% sim$species$species,] initialCommunitiesFn <- function(initialCommunities, speciesTable) { for (i in 1:nrow(initialCommunities)) { @@ -352,10 +356,9 @@ estimateParameters <- function(sim) { } message("12: ", Sys.time()) - sim$initialCommunities <- Cache(initialCommunitiesFn, initialCommunities, speciesTable, + sim$initialCommunities <- Cache(initialCommunitiesFn, initialCommunities, sim$species, userTags = "stable") - sim$species <- speciesTable sim$minRelativeB <- data.frame(ecoregion = sim$ecoregion[active == "yes",]$ecoregion, X1 = 0.2, X2 = 0.4, X3 = 0.5, X4 = 0.7, X5 = 0.9) From 33a99510d996d2422bb9d8fec94fe8abaf655739 Mon Sep 17 00:00:00 2001 From: Alex Chubaty Date: Tue, 13 Nov 2018 16:29:32 -0700 Subject: [PATCH 2/6] speciesTable: decrease aspen longevity with https://github.com/eliotmcintire/LandWeb/issues/67 --- Boreal_LBMRDataPrep.R | 1 + 1 file changed, 1 insertion(+) diff --git a/Boreal_LBMRDataPrep.R b/Boreal_LBMRDataPrep.R index 33713c5..b78283b 100644 --- a/Boreal_LBMRDataPrep.R +++ b/Boreal_LBMRDataPrep.R @@ -328,6 +328,7 @@ estimateParameters <- function(sim) { ## adjust some species-specific values speciesTable[species == "Pice_gla", seeddistance_max := 2000] ## (see LandWeb#96) + #speciesTable[species == "Popu_tre", longevity := 80] ## (see LandWeb#67) message("10: ", Sys.time()) From 5e1864186ef3111bd249d0b1f7f17ee88f7cb06b Mon Sep 17 00:00:00 2001 From: Alex Chubaty Date: Wed, 14 Nov 2018 10:43:29 -0700 Subject: [PATCH 3/6] add overrides with https://github.com/eliotmcintire/LandWeb/issues/97 and https://github.com/eliotmcintire/LandWeb/issues/67 --- Boreal_LBMRDataPrep.R | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Boreal_LBMRDataPrep.R b/Boreal_LBMRDataPrep.R index b78283b..f5384d6 100644 --- a/Boreal_LBMRDataPrep.R +++ b/Boreal_LBMRDataPrep.R @@ -328,7 +328,6 @@ estimateParameters <- function(sim) { ## adjust some species-specific values speciesTable[species == "Pice_gla", seeddistance_max := 2000] ## (see LandWeb#96) - #speciesTable[species == "Popu_tre", longevity := 80] ## (see LandWeb#67) message("10: ", Sys.time()) @@ -484,7 +483,7 @@ Save <- function(sim) { } # Layers provided by David Andison sometimes have LTHRC, sometimes LTHFC ... chose whichever - LTHxC <- grep("(LTH.+C)",names(sim$shpStudyAreaLarge), value= TRUE) + LTHxC <- grep("(LTH.+C)",names(sim$shpStudyAreaLarge), value = TRUE) fieldName <- if (length(LTHxC)) { LTHxC } else { @@ -640,10 +639,20 @@ Save <- function(sim) { sim$studyArea <- sim$shpStudyAreaLarge } - if (!suppliedElsewhere("speciesThreshold", sim = sim)) { sim$speciesThreshold <- 50 } + if (!is.null(sim$override.Boreal_LBMRDataPrep.inputObjects)) + sim <- sim$override.Boreal_LBMRDataPrep.inputObjects(sim) + return(invisible(sim)) } + +override.Boreal_LBMRDataPrep.inputObjects <- function(sim) { + if (grepl("aspen80", sim$runName)) { + speciesTable[species == "Popu_tre", longevity := 80] ## (see LandWeb#67) + sim$speciesTable[species == "Popu_tre", longevity := 80] ## (see LandWeb#67) + } + sim +} From dbcb6e83477ed8cff5c625a54c5449b7d9f961a9 Mon Sep 17 00:00:00 2001 From: Alex Chubaty Date: Wed, 14 Nov 2018 11:03:50 -0700 Subject: [PATCH 4/6] don't write intermediate ecoregionProducer outputs to disk --- Boreal_LBMRDataPrep.R | 8 +------- R/ecoregionProducers.R | 8 +++++--- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/Boreal_LBMRDataPrep.R b/Boreal_LBMRDataPrep.R index f5384d6..23d09e3 100644 --- a/Boreal_LBMRDataPrep.R +++ b/Boreal_LBMRDataPrep.R @@ -151,18 +151,12 @@ estimateParameters <- function(sim) { message("ecoregionProducer: ", Sys.time()) # Note: this ecoregionMap is NOT the Canadian EcoRegion -- it is for LBMR, which uses "ecoregion" - ecoregionMap <- Cache( postProcess, sim$ecoDistrict, - studyArea = sim$shpStudyArea) + ecoregionMap <- Cache(postProcess, sim$ecoDistrict, studyArea = sim$shpStudyArea, filename2 = NULL) ecoregionFiles <- Cache(ecoregionProducer, - #studyAreaRaster = initialCommFiles$initialCommunityMap, - #ecoregionMapFull = sim$ecoDistrict, ecoregionMap = ecoregionMap, ecoregionName = "ECODISTRIC", ecoregionActiveStatus = ecoregionstatus, rasterToMatch = initialCommFiles$initialCommunityMap, #sim$rasterToMatch, - #studyArea = sim$studyArea, - #rstStudyArea = rstStudyRegionBinary, - #maskFn = fastMask, userTags = "stable") message("3: ", Sys.time()) diff --git a/R/ecoregionProducers.R b/R/ecoregionProducers.R index 4289de1..3d60439 100644 --- a/R/ecoregionProducers.R +++ b/R/ecoregionProducers.R @@ -11,8 +11,10 @@ ecoregionProducer <- function(ecoregionMap, ecoregionName, ecoregionFactorValues <- na.omit(unique(ecoregionMap[])) - ecoregionTable <- data.table(mapcode = seq_along(ecoregionFactorValues[!is.na(ecoregionFactorValues)]), - ecoregion = as.numeric(ecoregionFactorValues[!is.na(ecoregionFactorValues)])) + ecoregionTable <- data.table( + mapcode = seq_along(ecoregionFactorValues[!is.na(ecoregionFactorValues)]), + ecoregion = as.numeric(ecoregionFactorValues[!is.na(ecoregionFactorValues)]) + ) message("ecoregionProducer mapvalues: ", Sys.time()) ecoregionMap[] <- plyr::mapvalues(ecoregionMap[], from = ecoregionTable$ecoregion, to = ecoregionTable$mapcode) ecoregionActiveStatus[, ecoregion := as.character(ecoregion)] @@ -21,7 +23,7 @@ ecoregionProducer <- function(ecoregionMap, ecoregionName, ecoregionTable <- dplyr::left_join(ecoregionTable, ecoregionActiveStatus, by = "ecoregion") %>% - data.table + data.table() ecoregionTable[is.na(active), active := "no"] ecoregionTable <- ecoregionTable[,.(active, mapcode, ecoregion)] return(list(ecoregionMap = ecoregionMap, From e8919813fe7931fec0dc79b8fe1269ca8cfdd202 Mon Sep 17 00:00:00 2001 From: Alex Chubaty Date: Wed, 14 Nov 2018 11:49:34 -0700 Subject: [PATCH 5/6] pass runName as module param with https://github.com/eliotmcintire/LandWeb/issues/97 --- Boreal_LBMRDataPrep.R | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Boreal_LBMRDataPrep.R b/Boreal_LBMRDataPrep.R index 23d09e3..6a82111 100644 --- a/Boreal_LBMRDataPrep.R +++ b/Boreal_LBMRDataPrep.R @@ -19,6 +19,9 @@ defineModule(sim, list( documentation = list("README.txt", "Boreal_LBMRDataPrep.Rmd"), reqdPkgs = list("data.table", "dplyr", "fasterize", "gdalUtils", "raster", "rgeos"), parameters = rbind( + defineParameter("runName", "character", NA_character_, NA, NA, + paste("The name of the current simulation run, used to override", + "certain default input values (see override functions below).")), defineParameter(".crsUsed", "CRS", raster::crs( paste("+proj=lcc +lat_1=49 +lat_2=77 +lat_0=0 +lon_0=-95 +x_0=0 +y_0=0", "+datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0") @@ -342,7 +345,7 @@ estimateParameters <- function(sim) { initialCommunitiesFn <- function(initialCommunities, speciesTable) { for (i in 1:nrow(initialCommunities)) { agelength <- sample(1:15, 1) - ages <- sort(sample(1:speciesTable[species == initialCommunities$species[i],longevity], + ages <- sort(sample(1:speciesTable[species == initialCommunities$species[i], longevity], agelength)) initialCommunities[i, 4:(agelength + 3)] <- ages } @@ -637,16 +640,15 @@ Save <- function(sim) { sim$speciesThreshold <- 50 } - if (!is.null(sim$override.Boreal_LBMRDataPrep.inputObjects)) - sim <- sim$override.Boreal_LBMRDataPrep.inputObjects(sim) + if (!is.null(override.Boreal_LBMRDataPrep.inputObjects)) + sim <- override.Boreal_LBMRDataPrep.inputObjects(sim) return(invisible(sim)) } override.Boreal_LBMRDataPrep.inputObjects <- function(sim) { - if (grepl("aspen80", sim$runName)) { - speciesTable[species == "Popu_tre", longevity := 80] ## (see LandWeb#67) - sim$speciesTable[species == "Popu_tre", longevity := 80] ## (see LandWeb#67) + if (grepl("aspen80", P(sim)$runName)) { + speciesTable[LandisCode == "POPU.TRE", Longevity := 80] ## (see LandWeb#67) } sim } From a13438b874efb1c177f1495cbb94aa470c929d9b Mon Sep 17 00:00:00 2001 From: Alex Chubaty Date: Wed, 14 Nov 2018 12:12:00 -0700 Subject: [PATCH 6/6] fix override of speciesTable with https://github.com/eliotmcintire/LandWeb/issues/97 --- Boreal_LBMRDataPrep.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Boreal_LBMRDataPrep.R b/Boreal_LBMRDataPrep.R index 6a82111..5b08437 100644 --- a/Boreal_LBMRDataPrep.R +++ b/Boreal_LBMRDataPrep.R @@ -648,7 +648,7 @@ Save <- function(sim) { override.Boreal_LBMRDataPrep.inputObjects <- function(sim) { if (grepl("aspen80", P(sim)$runName)) { - speciesTable[LandisCode == "POPU.TRE", Longevity := 80] ## (see LandWeb#67) + sim$speciesTable[LandisCode == "POPU.TRE", Longevity := 80] ## (see LandWeb#67) } sim }