-
Notifications
You must be signed in to change notification settings - Fork 0
/
10c-post-processing-caribou.R
153 lines (126 loc) · 5.46 KB
/
10c-post-processing-caribou.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
###########################################
# #
# P O S T H O C C A R I B O U #
# #
###########################################
usrEmail <- ifelse(Sys.info()[["user"]] == "tmichele",
"tati.micheletti@gmail.com",
NULL)
googledrive::drive_auth(usrEmail)
skipUpdates <- ifelse(Sys.info()[["user"]] == "tmichele", TRUE, FALSE)
if(!skipUpdates){
source("01-packages.R")
} else {
if (file.exists(".Renviron")) readRenviron(".Renviron")
.libPaths(normalizePath(
file.path("packages", version$platform, paste0(version$major, ".", strsplit(version$minor, "[.]")[[1]][1])),
winslash = "/",
mustWork = FALSE
))
message("Using libPaths:\n", paste(.libPaths(), collapse = "\n"))
library("Require")
Require(c("data.table", "plyr", "pryr",
"PredictiveEcology/LandR@development", ## TODO: workaround weird raster/sf method problem
"PredictiveEcology/SpaDES.core@development (>= 1.0.10.9002)",
"archive", "config", "googledrive", "httr", "slackr"), upgrade = FALSE)
}
source("02-init.R")
source("03-paths.R")
source("04-options.R")
source("05-google-ids.R")
source("R/makeStudyArea_WBI.R")
source("R/rstCurrentBurnListGenerator_WBI.R")
Require("raster")
Require("plyr")
Require("usefulFuns")
stepCacheTag <- c(paste0("cache:10b"),
paste0("runName:", runName))
do.call(setPaths, posthocPaths)
# RTM
pathRTM <- file.path(Paths$inputPath, paste0(studyAreaName, "_rtm.tif"))
if (file.exists(pathRTM)){
rasterToMatch <- raster(pathRTM)
} else stop("RTM doesn't exist. Please run script 'source('06-studyArea.R')'")
# Make RTM have only 1's
rasterToMatch[!is.na(rasterToMatch)] <- 1
# STUDY AREA
pathSA <- file.path(Paths[["inputPath"]], paste0(studyAreaName, "_SA.qs"))
if (file.exists(pathSA)){
studyArea <- qs::qread(pathSA)
} else {
studyArea <- makeStudyArea(studyAreaName)
}
# Info
Run <- strsplit(runName, split = "_")[[1]][4]
Province <- strsplit(runName, split = "_")[[1]][1]
ClimateModel <- strsplit(runName, split = "_")[[1]][2]
RCP <- strsplit(runName, split = "_")[[1]][3]
# Determine study area long name
studyAreaLongName <- switch(studyAreaName,
AB = "Alberta",
BC = "British Columbia",
SK = "Saskatchewan",
MB = "Manitoba",
NT = "Northwest Territories & Nunavut",
NU = "Northwest Territories & Nunavut",
YT = "Yukon",
RIA = "RIA")
# CREATE NEEDED OBJECTS
# Important ones:
waterValues <- 18
landcoverMap <- Cache(LandR::prepInputsLCC, destinationPath = Paths$inputPath,
studyArea = studyArea,
rasterToMatch = rasterToMatch,
filename2 = paste0("LCC_", Province, ".tif"),
userTags = c("objectName:landcoverMap",
stepCacheTag,
"outFun:Cache"),
omitArgs = c("destinationPath", "filename2"))
watersVals <- raster::getValues(landcoverMap)
watersValsToChange <- watersVals
watersValsToChange[!is.na(watersValsToChange) & !watersValsToChange %in% waterValues] <- NA
waterRaster <- raster::setValues(x = landcoverMap, watersValsToChange)
waterRaster[!is.na(waterRaster)] <- 1
parameters <- list(
caribouPopGrowthModel = list(
".plotInitialTime" = NULL,
"climateModel" = ClimateModel,
"useFuture" = FALSE,
"recoveryTime" = 40,
# "whichPolysToIgnore" = c("Yates", "Bistcho", "Maxhamish"),
".useDummyData" = FALSE,
".growthInterval" = 10,
"recruitmentModelVersion" = "Johnson", # Johnson or ECCC
"recruitmentModelNumber" = "M4",
"femaleSurvivalModelNumber" = c("M1", "M4") # M1:M5 --> best models: M1, M4
)
# ATTENTION: recruitmentModelNumber and recruitmentModelVersion need to be paired. ie.
# if you want to run M3 from ECCC and M1 and M4 from Johnson you should put these as
# "recruitmentModelVersion" = c("ECCC", "Johnson", "Johnson"),
# "recruitmentModelNumber" = c("M3", "M1", "M4"),
# otherwise it will repeat the recruitmentModelVersion for all recruitmentModelNumber
)
modules <- list("caribouPopGrowthModel")
# Reset input paths to the folder where simulation outputs are
# setPaths(inputPath = file.path(getwd(), "outputs", runName)) # THIS IS THE ORIGINAL FOR WHEN THE RUNS ARE DONE
setPaths(inputPath = file.path(getwd(), "outputs/_archive_2022-02-14", runName)) # THIS IS THE CURRENT TO TEST CODE. WHEN READY, Comment out
rstCurrentBurnList <- rstCurrentBurnListGenerator_WBI(pathInputs = Paths$inputPath)
# Add objects
objects <- list(
"studyArea" = studyArea,
"rasterToMatch" = rasterToMatch,
"usrEmail" = usrEmail,
"waterRaster" = waterRaster,
"rstCurrentBurnList" = rstCurrentBurnList,
"runName" = runName,
"shortProvinceName" = Province)
# Set simulation times
Times <- list(start = 2011, end = 2091)
message(crayon::yellow(paste0("Starting simulations for CARIBOU using ", paste(ClimateModel, RCP, collapse = " "),
" for ", Province, " (", Run, ")")))
simOut <- simInitAndSpades(times = Times,
params = parameters,
modules = modules,
objects = objects,
paths = Paths,
loadOrder = unlist(modules))