From c91f2744284e98f4cdb1aa3af3efbb07d4008e43 Mon Sep 17 00:00:00 2001 From: CeresBarros Date: Thu, 6 Sep 2018 11:18:33 -0700 Subject: [PATCH 1/5] added TODO clean-ups --- R/initialCommunityProducer.R | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/R/initialCommunityProducer.R b/R/initialCommunityProducer.R index 8acc293..43929e0 100644 --- a/R/initialCommunityProducer.R +++ b/R/initialCommunityProducer.R @@ -1,13 +1,13 @@ initialCommunityProducer <- function(speciesLayers, speciesPresence, studyArea, rstStudyArea) { - specieslayerInStudyArea <- crop(speciesLayers, studyArea) + specieslayerInStudyArea <- crop(speciesLayers, studyArea) ## TODO: Ceres: this is probably no longer necessary #if(isTRUE(tryCatch(getCluster(), error=function(x) TRUE, silent=TRUE))) beginCluster() - specieslayerInStudyArea <- specieslayerInStudyArea * rstStudyArea - names(specieslayerInStudyArea) <- names(speciesLayers) + specieslayerInStudyArea <- specieslayerInStudyArea * rstStudyArea ## TODO: Ceres: this is probably no longer necessary + names(specieslayerInStudyArea) <- names(speciesLayers) ## TODO: Ceres: this is probably no longer necessary #specieslayerInStudyArea <- specieslayerInStudyArea*(!is.na(rstStudyArea)) # specieslayerInStudyArea <- suppressWarnings(fastMask(specieslayerInStudyArea, # studyArea)) - speciesNames <- names(specieslayerInStudyArea)[which(maxValue(specieslayerInStudyArea) >= speciesPresence)] - specieslayerBySpecies <- raster::subset(specieslayerInStudyArea, speciesNames[1]) + speciesNames <- names(specieslayerInStudyArea)[which(maxValue(specieslayerInStudyArea) >= speciesPresence)] ## TODO: Ceres: this is probably no longer necessary + specieslayerBySpecies <- raster::subset(specieslayerInStudyArea, speciesNames[1]) ## TODO: Ceres: this is probably no longer necessary specieslayerBySpecies[which(is.na(specieslayerBySpecies[]) & specieslayerBySpecies[] <= 5)] <- 0 ## Ceres: this is weird, shouldn't this be OR? # specieslayerBySpecies[Which(is.na(specieslayerBySpecies) & specieslayerBySpecies<=5, # cells = TRUE)] <- 0 # 5% or less presence removed From ff7c5d416b733559f420e0358f4d7e4401ee7b87 Mon Sep 17 00:00:00 2001 From: CeresBarros Date: Thu, 6 Sep 2018 11:25:13 -0700 Subject: [PATCH 2/5] pull from upstream development --- Boreal_LBMRDataPrep.R | 1 - 1 file changed, 1 deletion(-) diff --git a/Boreal_LBMRDataPrep.R b/Boreal_LBMRDataPrep.R index fb3b525..1e667cb 100644 --- a/Boreal_LBMRDataPrep.R +++ b/Boreal_LBMRDataPrep.R @@ -560,7 +560,6 @@ Save <- function(sim) { if (!suppliedElsewhere(sim$rstStudyRegion)) { needRstSR <- TRUE } else { - browser() if (!identical(extent(sim$rstStudyRegion), extent(biomassMap))) { needRstSR <- TRUE } else { From 41b43472dea02f767438be7a4780ede825a22882 Mon Sep 17 00:00:00 2001 From: CeresBarros Date: Tue, 25 Sep 2018 18:21:25 -0700 Subject: [PATCH 3/5] bugfix and improvement: *speciesList needs to be updated after filtering speciesLayers; *speciesNamesEnd now follow 'Genu_spp[_ssp]' format even if species list is not supplied *minor clean-up --- R/loadkNNSpeciesLayers.R | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/R/loadkNNSpeciesLayers.R b/R/loadkNNSpeciesLayers.R index 049c280..7dcd2ec 100644 --- a/R/loadkNNSpeciesLayers.R +++ b/R/loadkNNSpeciesLayers.R @@ -2,7 +2,7 @@ ## Function to load kNN species layers from online data repository ## ------------------------------------------------------------------ -## dataPath: directory to data folder +## dPath: directory to data folder ## rasterToMatch: passed to prepInputs ## studyArea: passed to prepInputs ## species is either a character vector of species names to download, @@ -12,11 +12,18 @@ ## Defaults to 1 ## url: is the source url for the data, passed to prepInputs. -loadkNNSpeciesLayers <- function(dataPath, rasterToMatch, studyArea, +loadkNNSpeciesLayers <- function(dPath, rasterToMatch, studyArea, speciesList = "all", thresh = 1, url, cachePath, ...) { + if(!file.exists(file.path(dPath, "kNN-Species.tar"))){ + httr::GET(url = "http://tree.pfc.forestry.ca/kNN-Species.tar", + httr::user_agent(getOption("reproducible.useragent")), + httr::progress(), + httr::write_disk(file.path(dPath, "kNN-Species.tar"))) + } + ## get all kNN species - allSpp <- Cache(untar, tarfile = file.path(dataPath, "kNN-Species.tar"), list = TRUE) + allSpp <- Cache(untar, tarfile = file.path(dPath, "kNN-Species.tar"), list = TRUE) allSpp <- allSpp %>% grep(".zip", ., value = TRUE) %>% sub("_v0.zip", "", .) %>% @@ -34,6 +41,17 @@ loadkNNSpeciesLayers <- function(dataPath, rasterToMatch, studyArea, nrow = length(speciesList), ncol = 2, byrow = FALSE) colnames(speciesList) = c("speciesNamesRaw", "speciesNamesEnd") + ## change to Genu_spp format + namesEnd <- paste0(toupper(substring(speciesList[,2], 1, 1)), tolower(substring(speciesList[,2], 2, 4)), + "_", tolower(substring(gsub("^[[:alpha:]]*_|_[[:alpha:]]*$", "", speciesList[,2]), 1, 3))) + + ## if sub species are to kept separate, "add them back" + if (any(grepl("_.*_", speciesList[,2]))) { + namesEnd[grepl("_.*_", speciesList[,2])] <- namesEnd[grepl("_.*_", speciesList[,2])] %>% + paste0(., "_", tolower(sub(".*_", "", speciesList[grepl("_.*_", speciesList[,2]), 2]))) + } + speciesList[, 2] <- namesEnd + } else if(class(speciesList) == "matrix") { ## check column names if(!setequal(colnames(speciesList), c("speciesNamesRaw", "speciesNamesEnd"))) @@ -68,7 +86,7 @@ loadkNNSpeciesLayers <- function(dataPath, rasterToMatch, studyArea, targetFile = targetFile, url = url, archive = asPath(c("kNN-Species.tar", paste0("NFI_MODIS250m_kNN_Species_", sp, "_v0.zip"))), - destinationPath = asPath(dataPath), + destinationPath = asPath(dPath), fun = "raster::raster", studyArea = studyArea, rasterToMatch = rasterToMatch, @@ -102,7 +120,7 @@ loadkNNSpeciesLayers <- function(dataPath, rasterToMatch, studyArea, sumSpecies <- spp2sum[[i]] newLayerName <- names(spp2sum)[i] - fname <- .suffix(file.path(dataPath, paste0("KNN", newLayerName, ".tif")), suffix) + fname <- .suffix(file.path(dPath, paste0("KNN", newLayerName, ".tif")), suffix) a <- Cache(sumRastersBySpecies, speciesLayers = species1[sumSpecies], newLayerName = newLayerName, @@ -130,6 +148,9 @@ loadkNNSpeciesLayers <- function(dataPath, rasterToMatch, studyArea, ## remove layers that had < thresh pixels with biomass species1[layerData < thresh] <- NULL + ## update speciesList + speciesList <- speciesList[speciesList[, 2] %in% names(species1),] + ## return stack and final species matrix list(specieslayers = stack(species1), speciesList = speciesList) } From 8445995e5f0dfa2c777e078996e3d7deb02d14b4 Mon Sep 17 00:00:00 2001 From: CeresBarros Date: Thu, 15 Nov 2018 09:36:52 -0800 Subject: [PATCH 4/5] biomassMap url uncommented in prepInputs --- Boreal_LBMRDataPrep.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Boreal_LBMRDataPrep.R b/Boreal_LBMRDataPrep.R index 5b08437..5194792 100644 --- a/Boreal_LBMRDataPrep.R +++ b/Boreal_LBMRDataPrep.R @@ -453,7 +453,7 @@ Save <- function(sim) { targetFile = asPath(basename(biomassMapFilename)), archive = asPath(c("kNN-StructureBiomass.tar", "NFI_MODIS250m_kNN_Structure_Biomass_TotalLiveAboveGround_v0.zip")), - #url = extractURL("biomassMap"), + url = extractURL("biomassMap"), destinationPath = dPath, studyArea = sim$shpStudyArea, rasterToMatch = sim$rasterToMatch, From 80c619e01a50384ef46eac59f9baabbf9e72c14c Mon Sep 17 00:00:00 2001 From: CeresBarros Date: Thu, 15 Nov 2018 09:37:58 -0800 Subject: [PATCH 5/5] minor: all dataPath changed to dPath in comment (keep dataPath usage to SpaDES::dataPath() for consistency) --- R/loadkNNSpeciesLayers.R | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/R/loadkNNSpeciesLayers.R b/R/loadkNNSpeciesLayers.R index 706cf3a..b7bb169 100644 --- a/R/loadkNNSpeciesLayers.R +++ b/R/loadkNNSpeciesLayers.R @@ -12,7 +12,7 @@ ## Defaults to 1 ## url: is the source url for the data, passed to prepInputs. -loadkNNSpeciesLayers <- function(dataPath, rasterToMatch, studyArea, +loadkNNSpeciesLayers <- function(dPath, rasterToMatch, studyArea, speciesList = NULL, thresh = 1, url, cachePath, ...) { @@ -38,20 +38,20 @@ loadkNNSpeciesLayers <- function(dataPath, rasterToMatch, studyArea, species1 <- Cache(loadFun, url = url, spp = speciesList, #[, "speciesNamesRaw"], #loadFun, - dataPath = dataPath, + dPath = dPath, suffix = suffix, studyArea = studyArea, rasterToMatch = rasterToMatch, userTags = "kNN_SppLoad") # species1 <- Cache(lapply, seq_len(NROW(speciesList)), # spp = speciesList, #[, "speciesNamesRaw"], - # loadFun, url = url, dataPath = dataPath, + # loadFun, url = url, dPath = dPath, # suffix = suffix, # studyArea = studyArea, rasterToMatch = rasterToMatch, # userTags = "kNN_SppLoad") ## get all kNN species if (FALSE) { #TODO This no longer does all species } - allSpp <- Cache(untar, tarfile = file.path(dataPath, "kNN-Species.tar"), list = TRUE) + allSpp <- Cache(untar, tarfile = file.path(dPath, "kNN-Species.tar"), list = TRUE) allSpp <- allSpp %>% grep(".zip", ., value = TRUE) %>% sub("_v0.zip", "", .) %>% @@ -83,7 +83,7 @@ loadkNNSpeciesLayers <- function(dataPath, rasterToMatch, studyArea, sumSpecies <- spp2sum[[i]] newLayerName <- names(spp2sum)[i] - fname <- .suffix(file.path(dataPath, paste0("KNN", newLayerName, ".tif")), suffix) + fname <- .suffix(file.path(dPath, paste0("KNN", newLayerName, ".tif")), suffix) a <- Cache(sumRastersBySpecies, speciesLayers = species1[sumSpecies], newLayerName = newLayerName, @@ -135,7 +135,7 @@ sumRastersBySpecies <- function(speciesLayers, layersToSum, ras_out # Work around for Cache } -loadFun <- function(speciesListIndex, spp, suffix, url, dataPath, +loadFun <- function(speciesListIndex, spp, suffix, url, dPath, studyArea, rasterToMatch) { if (is.null(spp)) { @@ -173,7 +173,7 @@ loadFun <- function(speciesListIndex, spp, suffix, url, dataPath, targetFile = targetFile, url = url, archive = archive, - destinationPath = asPath(dataPath), + destinationPath = asPath(dPath), fun = "raster::raster")#, #studyArea = studyArea, #rasterToMatch = rasterToMatch, @@ -203,7 +203,7 @@ loadFun <- function(speciesListIndex, spp, suffix, url, dataPath, species1 <- Map(targetFile = targetFiles, archive = archives, filename2 = postProcessedFilenames, MoreArgs = list(url = url, - destinationPath = asPath(dataPath), + destinationPath = asPath(dPath), fun = "raster::raster", studyArea = studyArea, rasterToMatch = rasterToMatch, @@ -216,7 +216,7 @@ loadFun <- function(speciesListIndex, spp, suffix, url, dataPath, # targetFile = targetFile, # url = url, # archive = archive, - # destinationPath = asPath(dataPath), + # destinationPath = asPath(dPath), # fun = "raster::raster", # studyArea = studyArea, # rasterToMatch = rasterToMatch,