From 9dc8102fa2c6a08a52dabedf0ee7f2472fed44f2 Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Mon, 29 Oct 2018 17:44:44 -0400 Subject: [PATCH 1/4] reorder sequence in .inputObjects --> for standAlone case --- Boreal_LBMRDataPrep.R | 84 +++++++++++++++++++++++++------------------ 1 file changed, 50 insertions(+), 34 deletions(-) diff --git a/Boreal_LBMRDataPrep.R b/Boreal_LBMRDataPrep.R index 76a2f69..b802e8f 100644 --- a/Boreal_LBMRDataPrep.R +++ b/Boreal_LBMRDataPrep.R @@ -135,23 +135,23 @@ checkSpeciesLayerNames <- function(specieslayers) { } 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) + return(specieslayers) } estimateParameters <- function(sim) { @@ -444,14 +444,50 @@ Save <- function(sim) { polyCenter <- SpatialPoints(coords = data.frame(x = c(-1349980), y = c(6986895)), proj4string = crsUsed) + + seedToKeep <- .GlobalEnv$.Random.seed + set.seed(1234) sim$shpStudyAreaLarge <- SpaDES.tools::randomPolygon(x = polyCenter, hectares = 10000) + .GlobalEnv$.Random.seed <- seedToKeep } - needRstSR <- FALSE - if (!suppliedElsewhere("rasterToMatch", sim)) { - needRstSR <- TRUE + if (!suppliedElsewhere("shpStudyArea", sim)) { + message("'shpStudyArea' was not provided by user. Using the same as 'shpStudyAreaLarge'") + sim$shpStudyArea <- sim$shpStudyAreaLarge + } + + needRTM <- FALSE + if (is.null(sim$rasterToMatch)) { + if (!suppliedElsewhere("rasterToMatch", sim)) { + needRTM <- TRUE + message("There is no rasterToMatch supplied; will attempt to use biomassMap") + } else { + stop("rasterToMatch is going to be supplied, but ", currentModule(sim), " requires it ", + "as part of its .inputObjects. Please make it accessible to ", currentModule(sim), + " in the .inputObjects by passing it in as an object in simInit(objects = list(rasterToMatch = aRaster)", + " or in a module that gets loaded prior to ", currentModule(sim)) + } } - if (needRstSR) { + + if (!suppliedElsewhere("biomassMap", sim) || needRTM) { + sim$biomassMap <- Cache(prepInputs, + targetFile = asPath(basename(biomassMapFilename)), + archive = asPath(c("kNN-StructureBiomass.tar", + "NFI_MODIS250m_kNN_Structure_Biomass_TotalLiveAboveGround_v0.zip")), + #url = extractURL("biomassMap"), + destinationPath = dPath, + studyArea = sim$shpStudyArea, + rasterToMatch = sim$rasterToMatch, + useSAcrs = TRUE, + method = "bilinear", + datatype = "INT2U", + filename2 = TRUE, overwrite = TRUE, + userTags = c("stable", currentModule(sim))) + } + + if (needRTM) { + # if we need rasterToMatch, that means a) we don't have it, but b) we will have biomassMap + sim$rasterToMatch <- sim$biomassMap message(" Rasterizing the shpStudyAreaLarge polygon map") if (!is(sim$shpStudyAreaLarge, "SpatialPolygonsDataFrame")) { dfData <- if (is.null(rownames(sim$shpStudyAreaLarge))) { @@ -481,11 +517,6 @@ Save <- function(sim) { datatype = "INT2U", overwrite = TRUE) } - if (!suppliedElsewhere("shpStudyArea", sim)) { - message("'shpStudyArea' was not provided by user. Using the same as 'shpStudyAreaLarge'") - sim$shpStudyArea <- sim$shpStudyAreaLarge - } - if (!identical(crsUsed, crs(sim$shpStudyAreaLarge))) { sim$shpStudyAreaLarge <- spTransform(sim$shpStudyAreaLarge, crsUsed) #faster without Cache } @@ -496,21 +527,6 @@ Save <- function(sim) { cacheTags = c(currentModule(sim), "function:.inputObjects", "function:spades") - if (!suppliedElsewhere("biomassMap", sim)) { - sim$biomassMap <- Cache(prepInputs, - targetFile = biomassMapFilename, - archive = asPath(c("kNN-StructureBiomass.tar", - "NFI_MODIS250m_kNN_Structure_Biomass_TotalLiveAboveGround_v0.zip")), - url = extractURL("biomassMap"), - destinationPath = dPath, - studyArea = sim$shpStudyArea, - useSAcrs = TRUE, - method = "bilinear", - datatype = "INT2U", - filename2 = TRUE, - userTags = c("stable", currentModule(sim))) - } - # LCC2005 if (!suppliedElsewhere("LCC2005", sim)) { sim$LCC2005 <- Cache(prepInputs, @@ -522,7 +538,7 @@ Save <- function(sim) { rasterToMatch = sim$rasterToMatch, method = "bilinear", datatype = "INT2U", - filename2 = TRUE, + filename2 = TRUE, overwrite = TRUE, userTags = currentModule(sim)) projection(sim$LCC2005) <- projection(sim$rasterToMatch) @@ -575,17 +591,17 @@ Save <- function(sim) { # stand age map if (!suppliedElsewhere("standAgeMap", sim)) { sim$standAgeMap <- Cache(prepInputs, #notOlderThan = Sys.time(), - targetFile = standAgeMapFilename, + targetFile = basename(standAgeMapFilename), archive = asPath(c("kNN-StructureStandVolume.tar", "NFI_MODIS250m_kNN_Structure_Stand_Age_v0.zip")), destinationPath = dPath, - url = extractURL("standAgeMap"), + #url = extractURL("standAgeMap"), fun = "raster::raster", studyArea = sim$shpStudyAreaLarge, rasterToMatch = sim$rasterToMatch, method = "bilinear", datatype = "INT2U", - filename2 = TRUE, + filename2 = TRUE, overwrite = TRUE, userTags = c("stable", currentModule(sim))) } @@ -613,7 +629,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", From 8791fadd79e5c9e4d3598fe4807c5b1b84732dcd Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Mon, 29 Oct 2018 17:51:30 -0400 Subject: [PATCH 2/4] Checksums.txt rm Small --- data/CHECKSUMS.txt | 113 ++++----------------------------------------- 1 file changed, 9 insertions(+), 104 deletions(-) diff --git a/data/CHECKSUMS.txt b/data/CHECKSUMS.txt index e2a3c87..8ca402b 100644 --- a/data/CHECKSUMS.txt +++ b/data/CHECKSUMS.txt @@ -1,17 +1,5 @@ "file" "checksum" "filesize" "algorithm" -"Abie_sp_CASFRI_PAUL_KNN.tif" "4f165162f6493f48" "18264" "xxhash64" -"Abie_sp_CASFRI_PAUL.tif" "f0f92f66ee7f68d0" "1654116" "xxhash64" "Beaudoin_2014_CJFR.pdf" "f1bde1a33abbd27e" "6719588" "xxhash64" -"Betu_pap_CASFRI_PAUL_KNN.tif" "8a1ed376c1962f6e" "1888845" "xxhash64" -"Betu_pap_CASFRI_PAUL.tif" "3abd550c08aea4e4" "1888845" "xxhash64" -"Canada_2005_metadata_v1_4.doc" "2c7d726e8c5bf598" "84480" "xxhash64" -"CASFRIAbie_sp.tif" "f0f92f66ee7f68d0" "1654116" "xxhash64" -"CASFRIBetu_pap.tif" "3abd550c08aea4e4" "1888845" "xxhash64" -"CASFRILari_lar.tif" "c954e48e72c89f00" "2297793" "xxhash64" -"CASFRIPice_gla.tif" "e469e559a0b76d14" "3551382" "xxhash64" -"CASFRIPice_mar.tif" "e9074d2b2251c491" "4493318" "xxhash64" -"CASFRIPinu_sp.tif" "95b01dd98f5f928f" "3471545" "xxhash64" -"CASFRIPopu_tre.tif" "2782945d50199271" "4675044" "xxhash64" "ecodistrict_shp.zip" "f27e61272efbab97" "9278195" "xxhash64" "ecodistricts.dbf" "83ec9bcdba1d4c5e" "70983" "xxhash64" "ecodistricts.prj" "157d6afc7106f4fd" "181" "xxhash64" @@ -33,113 +21,30 @@ "ecozones.sbx" "c74f977febc73339" "148" "xxhash64" "ecozones.shp" "ee87a41e9ae6c9c3" "1846312" "xxhash64" "ecozones.shx" "40ca5d282f2e53c5" "300" "xxhash64" -"file2a306f723200bd.tif" "049a925cdd698a47" "63414" "xxhash64" -"kNN-Species.tar" "7906f46e0279659c" "2497034240" "xxhash64" -"kNN-StructureBiomass.tar" "6a5e5faea8ef8f99" "2685040640" "xxhash64" -"kNN-StructureStandVolume.tar" "9a1a0a8ef7356ec4" "1808496640" "xxhash64" -"KNNPinu_sp_SMALL_All.tif" "fad454c0054392e1" "52061" "xxhash64" -"KNNPinu_sp.tif" "088ef9e6ebd0be48" "11485999" "xxhash64" -"KNNPinu_spSMALL_All.tif" "fad454c0054392e1" "52061" "xxhash64" +"KNNPinu_sp_DMI_All.tif" "47e402579e1333bd" "791634" "xxhash64" "LandCoverOfCanada2005_V1_4.zip" "e100c037a257e377" "47185238" "xxhash64" -"Landweb_CASFRI_GIDs_attributes3.csv" "adb4e9da358d5043" "1087080291" "xxhash64" -"Landweb_CASFRI_GIDs_README.txt" "0292cda5da24ceb5" "4833" "xxhash64" -"Landweb_CASFRI_GIDs.tif" "b55471364ef1c93e" "141284856" "xxhash64" -"Landweb_CASFRI_GIDsStudyArea.tif" "c8dab6cb00e241dc" "354487657" "xxhash64" -"Lari_lar_CASFRI_PAUL_KNN.tif" "ee8df3788b2b57a7" "2297793" "xxhash64" -"Lari_lar_CASFRI_PAUL.tif" "c954e48e72c89f00" "2297793" "xxhash64" -"LC_2005_Legend_39Classes.pdf" "0b764e8f30536b7c" "1407110" "xxhash64" "LCC2005_V1_4a.tif" "b50567fc83bcc5de" "437916202" "xxhash64" "NFI_MAP_V0_metadata.xls" "79285734f8f2deb0" "74240" "xxhash64" -"NFI_MODIS250m_kNN_Species_Abie_Las_v0_SMALL_All.tif" "070fc9a923c54a24" "7092" "xxhash64" "NFI_MODIS250m_kNN_Species_Abie_Las_v0.tif" "ae77900de22dad3a" "46393844" "xxhash64" -"NFI_MODIS250m_kNN_Species_Abie_Las_v0.tif.aux.xml" "8d648e102dfb6ff5" "2089" "xxhash64" -"NFI_MODIS250m_kNN_Species_Abie_Las_v0.tif.xml" "bb17f9f28c57d03b" "558" "xxhash64" "NFI_MODIS250m_kNN_Species_Abie_Las_v0.zip" "dd4c589d6a6b4c41" "39842269" "xxhash64" +"NFI_MODIS250m_kNN_Species_Abie_Las_v0_DMI_All.tif" "e409725698109673" "69677" "xxhash64" "NFI_MODIS250m_kNN_Species_Pice_Gla_v0.tif" "76ff34659902fc5d" "214920877" "xxhash64" -"NFI_MODIS250m_kNN_Species_Pice_Gla_v0.tif.aux.xml" "963c02a8d3076ad2" "2089" "xxhash64" -"NFI_MODIS250m_kNN_Species_Pice_Gla_v0.tif.xml" "35b2165180e0eb72" "558" "xxhash64" "NFI_MODIS250m_kNN_Species_Pice_Gla_v0.zip" "9de914719e369c22" "203485408" "xxhash64" +"NFI_MODIS250m_kNN_Species_Pice_Gla_v0_DMI_All.tif" "100aae6e350f7d70" "1227360" "xxhash64" "NFI_MODIS250m_kNN_Species_Pice_Mar_v0.tif" "c11ae718d7510af8" "328900034" "xxhash64" -"NFI_MODIS250m_kNN_Species_Pice_Mar_v0.tif.aux.xml" "9bd981bd20b7305f" "2429" "xxhash64" -"NFI_MODIS250m_kNN_Species_Pice_Mar_v0.tif.xml" "9996ba3b279455ce" "558" "xxhash64" "NFI_MODIS250m_kNN_Species_Pice_Mar_v0.zip" "c4fdba27669f6bb5" "312260446" "xxhash64" +"NFI_MODIS250m_kNN_Species_Pice_Mar_v0_DMI_All.tif" "cd591519ec66307f" "1259215" "xxhash64" "NFI_MODIS250m_kNN_Species_Pinu_Ban_v0.tif" "87abafa6c3eace5d" "183295505" "xxhash64" -"NFI_MODIS250m_kNN_Species_Pinu_Ban_v0.tif.aux.xml" "f25433c1466c37c6" "2214" "xxhash64" -"NFI_MODIS250m_kNN_Species_Pinu_Ban_v0.tif.xml" "75e4358a72f93333" "558" "xxhash64" "NFI_MODIS250m_kNN_Species_Pinu_Ban_v0.zip" "55f42346b41888a3" "171989683" "xxhash64" +"NFI_MODIS250m_kNN_Species_Pinu_Ban_v0_DMI_All.tif" "8dc24a3bbe99dfab" "659197" "xxhash64" "NFI_MODIS250m_kNN_Species_Pinu_Con_v0.tif" "ffc7972f325a2383" "60905480" "xxhash64" -"NFI_MODIS250m_kNN_Species_Pinu_Con_v0.tif.aux.xml" "8cf7866b4b29f7e5" "2197" "xxhash64" -"NFI_MODIS250m_kNN_Species_Pinu_Con_v0.tif.xml" "751254d0d2c76fb7" "558" "xxhash64" "NFI_MODIS250m_kNN_Species_Pinu_Con_v0.zip" "a21fcc82350ab25a" "53967883" "xxhash64" +"NFI_MODIS250m_kNN_Species_Pinu_Con_v0_DMI_All.tif" "7b17b170faed5016" "326825" "xxhash64" "NFI_MODIS250m_kNN_Species_Popu_Tre_v0.tif" "e871be8011844a74" "235379759" "xxhash64" -"NFI_MODIS250m_kNN_Species_Popu_Tre_v0.tif.aux.xml" "89116187ecf283cf" "2300" "xxhash64" -"NFI_MODIS250m_kNN_Species_Popu_Tre_v0.tif.xml" "aaf068d63a2a4648" "558" "xxhash64" "NFI_MODIS250m_kNN_Species_Popu_Tre_v0.zip" "558ca21bfc0de04c" "223049966" "xxhash64" +"NFI_MODIS250m_kNN_Species_Popu_Tre_v0_DMI_All.tif" "791588e8ca61a120" "1707740" "xxhash64" "NFI_MODIS250m_kNN_Structure_Biomass_TotalLiveAboveGround_v0.tif" "1a07864f573e0efb" "474053079" "xxhash64" -"NFI_MODIS250m_kNN_Structure_Biomass_TotalLiveAboveGround_v0.tif.aux.xml" "98af0905bce31915" "2117" "xxhash64" -"NFI_MODIS250m_kNN_Structure_Biomass_TotalLiveAboveGround_v0.tif.xml" "396edfc4d59fa1f6" "580" "xxhash64" -"NFI_MODIS250m_kNN_Structure_Biomass_TotalLiveAboveGround_v0.zip" "8e13ae321f43313a" "449276419" "xxhash64" "NFI_MODIS250m_kNN_Structure_Stand_Age_v0.tif" "a816295a58b2851a" "427919414" "xxhash64" -"NFI_MODIS250m_kNN_Structure_Stand_Age_v0.tif.aux.xml" "b0389448efaeac5e" "2280" "xxhash64" -"NFI_MODIS250m_kNN_Structure_Stand_Age_v0.tif.xml" "dae96fa4db64ea5c" "561" "xxhash64" -"NFI_MODIS250m_kNN_Structure_Stand_Age_v0.zip" "22cd9a6a9e4cdf1d" "407564483" "xxhash64" -"out.dbf" "f934aeab7d616f17" "75" "xxhash64" -"out.prj" "ed6306a09777ed21" "439" "xxhash64" -"out.shp" "e8be9e48e17cb196" "236" "xxhash64" -"out.shx" "88293ed43a79403f" "108" "xxhash64" -"PaulAbie_sp.tif" "1d61ad7b026d8348" "1349643" "xxhash64" -"PaulBetu_pap.tif" "1d61ad7b026d8348" "1349643" "xxhash64" -"PaulLari_lar.tif" "1d61ad7b026d8348" "1349643" "xxhash64" -"PaulPice_gla.tif" "b200affbd698f8fc" "3982285" "xxhash64" -"PaulPice_mar.tif" "f4f0d87bd0b60abe" "4592149" "xxhash64" -"PaulPinu_sp.tif" "e8a6081dc77d59c9" "3844095" "xxhash64" -"PaulPopu_tre.tif" "ec73e35a343263c0" "3845310" "xxhash64" -"PaulSppFilled.tif" "a112daba4e9c5594" "54313749" "xxhash64" -"PaulTrimmed.tif" "16345ff5d54527b8" "88665920" "xxhash64" -"Pice_gla_CASFRI_PAUL_KNN.tif" "e93a7477f2736b97" "108700" "xxhash64" -"Pice_gla_CASFRI_PAUL.tif" "a96419d0a8b94f89" "5540950" "xxhash64" -"Pice_mar_CASFRI_PAUL_KNN.tif" "db4175f1eb50664c" "123442" "xxhash64" -"Pice_mar_CASFRI_PAUL.tif" "68f4be525d0819ce" "6640825" "xxhash64" -"Pinu_sp_CASFRI_PAUL_KNN.tif" "7d5dbf5bdd3099dc" "84495" "xxhash64" -"Pinu_sp_CASFRI_PAUL.tif" "faf9386dc76ab049" "5426813" "xxhash64" -"Popu_tre_CASFRI_PAUL_KNN.tif" "a9d3979f8f0d9761" "123597" "xxhash64" -"Popu_tre_CASFRI_PAUL.tif" "e289e85170d8f561" "6092621" "xxhash64" -"rstStudyRegion.tif" "173778c1482e09d8" "10939" "xxhash64" -"shpLandWeb5.dbf" "980d9ee4b9f1023e" "75" "xxhash64" -"shpLandWeb5.prj" "ed6306a09777ed21" "439" "xxhash64" -"shpLandWeb5.shp" "e8be9e48e17cb196" "236" "xxhash64" -"shpLandWeb5.shx" "88293ed43a79403f" "108" "xxhash64" -"Smallecodistricts.dbf" "d99f5f9d582aa3f5" "61145" "xxhash64" -"Smallecodistricts.prj" "e15626207e03581e" "147" "xxhash64" -"Smallecodistricts.shp" "4d5255ca31b252ac" "1748764" "xxhash64" -"Smallecodistricts.shx" "61a7b2eb7c95d6a7" "2164" "xxhash64" -"Smallecoregions.dbf" "118ea273f62111ef" "18091" "xxhash64" -"Smallecoregions.prj" "e15626207e03581e" "147" "xxhash64" -"Smallecoregions.shp" "5bdf608b4cb1cf51" "707760" "xxhash64" -"Smallecoregions.shx" "046499a4bb5ce761" "468" "xxhash64" -"Smallecozones.dbf" "2e4940c4785bd6a3" "3281" "xxhash64" -"Smallecozones.prj" "e15626207e03581e" "147" "xxhash64" -"Smallecozones.shp" "f34407bc68957eb3" "283712" "xxhash64" -"Smallecozones.shx" "4328f65b19880af6" "164" "xxhash64" -"SmallKNNPinu_sp.tif" "fad454c0054392e1" "52061" "xxhash64" -"SmallLCC2005_V1_4a.tif" "a216132905231bc8" "69820" "xxhash64" -"SmallNFI_MODIS250m_kNN_Species_Abie_Las_v0.tif" "070fc9a923c54a24" "7092" "xxhash64" -"SmallNFI_MODIS250m_kNN_Species_Pice_Gla_v0.tif" "52e58c9582fc3abd" "151864" "xxhash64" -"SmallNFI_MODIS250m_kNN_Species_Pice_Mar_v0.tif" "1a4e8d530a89b794" "150796" "xxhash64" -"SmallNFI_MODIS250m_kNN_Species_Pinu_Ban_v0.tif" "c502fb69dd431ad9" "42419" "xxhash64" -"SmallNFI_MODIS250m_kNN_Species_Pinu_Con_v0.tif" "deaf5f2ae4b931fc" "23331" "xxhash64" -"SmallNFI_MODIS250m_kNN_Species_Popu_Tre_v0.tif" "c62d1a93475a3379" "183548" "xxhash64" -"SmallNFI_MODIS250m_kNN_Structure_Biomass_TotalLiveAboveGround_v0.tif" "3158360d8e4eaaf1" "167515" "xxhash64" -"SmallNFI_MODIS250m_kNN_Structure_Stand_Age_v0.tif" "3f7d96d5cd28184f" "188279" "xxhash64" +"rasterToMatch.tif" "91fa9043d26d93e9" "1212" "xxhash64" +"rstStudyRegion.tif" "264e9fd679127ec5" "175651" "xxhash64" "speciesTraits.csv" "155e633022e134cf" "9994" "xxhash64" -"SPP_1990_FILLED_100m_NAD83_LCC_BYTE_VEG.dat" "bf7f215abc64dcc4" "361056624" "xxhash64" -"SPP_1990_FILLED_100m_NAD83_LCC_BYTE_VEG.dat.aux.xml" "87f669675c809639" "2300" "xxhash64" -"SPP_1990_FILLED_100m_NAD83_LCC_BYTE_VEG.hdr" "0b80bd00a8070db3" "1107" "xxhash64" -"SPP_1990_FILLED_100m_NAD83_LCC_BYTE_VEG.prj" "be92e09fe7e5a01d" "478" "xxhash64" -"SPP_1990_FILLED_100m_NAD83_LCC_BYTE_VEG.wld" "ebbecfa02b6e948a" "90" "xxhash64" -"studyArea.dbf" "dd109c1ef0f9b18f" "781921" "xxhash64" -"studyArea.prj" "ed6306a09777ed21" "439" "xxhash64" -"studyArea.shp" "c356ec5010147e50" "390784" "xxhash64" -"studyArea.shx" "92bb0156b58252e8" "1892" "xxhash64" -"StudyAreaMask.tif" "ec13eb48654a8ca2" "4276065" "xxhash64" -"StudyAreaMask250.tif" "963c67c425aee474" "2140385" "xxhash64" From 42013dbe8310931b1f7373a7338101146f0cea8b Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Mon, 29 Oct 2018 17:52:26 -0400 Subject: [PATCH 3/4] Boreal_LBMRDataPrep.Rmd bugfixes for stand alone --- Boreal_LBMRDataPrep.Rmd | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Boreal_LBMRDataPrep.Rmd b/Boreal_LBMRDataPrep.Rmd index 68ff1ba..c068888 100644 --- a/Boreal_LBMRDataPrep.Rmd +++ b/Boreal_LBMRDataPrep.Rmd @@ -62,11 +62,10 @@ paths <- getPaths() ```{r get-study-area} library(raster) -cachePath <- file.path("Boreal_LBMRDataPrep", "cache") -modulePath <- Cache(readline, paste0("Where is the module path? (e.g., ~/module, with no quotes).\n", - "Press Enter to accept the path in getPaths()$modulePath: "), - cacheRepo = cachePath) -setPaths(cachePath = cachePath, modulePath = modulePath) +# modulePath <- Cache(readline, paste0("Where is the module path? (e.g., ~/module, with no quotes).\n", +# "Press Enter to accept the path in getPaths()$modulePath: "), +# cacheRepo = cachePath) +# setPaths(cachePath = cachePath, modulePath = modulePath) ## do you want to hand-draw a map or use defaults? # - note that large areas will take longer to compute @@ -89,16 +88,17 @@ if (handDrawMap) { shpStudyAreaLarge <- SpatialPolygons(list(Polygons(list(Polygon(severalrandompoints$coords)), ID = 1)), proj4string = crs(canadaMap)) } + Plot(shpStudyAreaLarge, addTo = "canadaMap", col = "red") } -Plot(shpStudyAreaLarge, addTo = "canadaMap", col = "red") times <- list(start = 0, end = 10) modules <- list("Boreal_LBMRDataPrep") objects <- if (handDrawMap) list("shpStudyAreaLarge" = shpStudyAreaLarge, "shpStudyArea" = shpStudyAreaLarge) else list() -mySim <- simInit(times = times, params = parameters, modules = modules, - objects = objects) +mySim <- simInit(times = times, #params = parameters, + modules = append(modules, "LBMR"), + objects = objects, paths = getPaths()) ``` # Run `spades` From b75e5363400ad02a272efde7bf14882fab6cb503 Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Thu, 1 Nov 2018 01:04:41 -0400 Subject: [PATCH 4/4] ecoregionMap was not correctly masked --- Boreal_LBMRDataPrep.R | 15 +++++++++----- R/ecoregionProducers.R | 45 ++++++------------------------------------ 2 files changed, 16 insertions(+), 44 deletions(-) diff --git a/Boreal_LBMRDataPrep.R b/Boreal_LBMRDataPrep.R index b802e8f..9765470 100644 --- a/Boreal_LBMRDataPrep.R +++ b/Boreal_LBMRDataPrep.R @@ -178,14 +178,19 @@ estimateParameters <- function(sim) { ecoregion = 1:1031) 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) ecoregionFiles <- Cache(ecoregionProducer, - studyAreaRaster = initialCommFiles$initialCommunityMap, - ecoregionMapFull = sim$ecoDistrict, + #studyAreaRaster = initialCommFiles$initialCommunityMap, + #ecoregionMapFull = sim$ecoDistrict, + ecoregionMap = ecoregionMap, ecoregionName = "ECODISTRIC", ecoregionActiveStatus = ecoregionstatus, - studyArea = sim$studyArea, - rstStudyArea = rstStudyRegionBinary, - maskFn = fastMask, + 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 be1cb67..4289de1 100644 --- a/R/ecoregionProducers.R +++ b/R/ecoregionProducers.R @@ -1,48 +1,15 @@ -ecoregionProducer <- function(studyAreaRaster, ecoregionMapFull, ecoregionName, - ecoregionActiveStatus, studyArea, rstStudyArea, maskFn) { +ecoregionProducer <- function(ecoregionMap, ecoregionName, + ecoregionActiveStatus, rasterToMatch) { # change the coordinate reference for all spatialpolygons message("ecoregionProducer 1: ", Sys.time()) - ecoregionMapInStudy <- raster::intersect(ecoregionMapFull, fixErrors(aggregate(studyArea))) - # ecoregions <- ecoregionMapInStudy@data[,ecoregionName] - # ecoregionTable <- data.table(mapcode = numeric(), - # ecoregion = character()) - # mapcode <- 1 - # for(ecoregion in unique(ecoregions)){ - # # for(ecoregion in ecoregions){ - # singleecoMapPoly <- ecoregionMapInStudy[ecoregionMapInStudy@data[,ecoregionName]==ecoregion,] - # studyAreaRaster <- setValues(studyAreaRaster, mapcode) - # singleecoMapRaster <- crop(studyAreaRaster, singleecoMapPoly) - # singleecoMapRaster <- suppressWarnings(maskFn(singleecoMapRaster, singleecoMapPoly)) - # if(length(unique(getValues(singleecoMapRaster)))==1){ - # if(is.na(unique(getValues(singleecoMapRaster)))){ - # ecoregionTable <- rbind(ecoregionTable, - # data.table(mapcode = NA, - # ecoregion = ecoregion)) - # } else { - # ecoregionTable <- rbind(ecoregionTable, - # data.table(mapcode = mapcode, - # ecoregion = ecoregion)) - # } - # } else { - # ecoregionTable <- rbind(ecoregionTable, - # data.table(mapcode = mapcode, - # ecoregion = ecoregion)) - # } - # - # if(mapcode == 1){ - # ecoregionMap <- singleecoMapRaster - # } else { - # ecoregionMap <- merge(ecoregionMap, singleecoMapRaster) - # } - # mapcode <- mapcode + 1 - # } + #ecoregionMapInStudy <- raster::intersect(ecoregionMapFull, fixErrors(aggregate(studyArea))) # Alternative message("ecoregionProducer fastRasterize: ", Sys.time()) - ecoregionMap <- fasterize::fasterize(sf::st_as_sf(ecoregionMapInStudy), studyAreaRaster, field = "ECODISTRIC") + ecoregionMap <- fasterize::fasterize(sf::st_as_sf(ecoregionMap), raster(rasterToMatch), field = "ECODISTRIC") + ecoregionMap[is.na(rasterToMatch[])] <- NA - #ecoregionMap1 <- rasterize(ecoregionMapInStudy, studyAreaRaster, field = "ECODISTRIC") - ecoregionFactorValues <- unique(ecoregionMap[]) + ecoregionFactorValues <- na.omit(unique(ecoregionMap[])) ecoregionTable <- data.table(mapcode = seq_along(ecoregionFactorValues[!is.na(ecoregionFactorValues)]), ecoregion = as.numeric(ecoregionFactorValues[!is.na(ecoregionFactorValues)]))