-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy path4_predictModelToStudyArea.R
74 lines (61 loc) · 2.51 KB
/
4_predictModelToStudyArea.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
# File: 4_predictModelToStudyArea.r
# Purpose: create the distribution model prediction raster
## start with a fresh workspace with no objects loaded
library(raster)
library(randomForest)
removeTmpFiles(48) # clean old (>2days) Raster temporary files
####
## two lines need your attention. The one directly below (loc_scripts)
## and about line 26 where you choose which Rdata file to use,
# get paths, other settings
# get the customized version of the predict function
# source(paste(loc_scripts, "helper/RasterPredictMod.R", sep = "/"), local = TRUE)
# load data ----
# get the rdata file
setwd(loc_model)
dir.create(paste0(model_species,"/outputs/model_predictions"), recursive = T, showWarnings = F)
setwd(paste0(model_species,"/outputs"))
# load rdata
load(paste0("rdata/",modelrun_meta_data$model_run_name,".Rdata"))
##Make the raster stack
stackOrder <- names(df.full)[indVarCols]
setwd(loc_envVars)
# find matching var rasters (with folder for temporal vars)
raslist <- list.files(pattern = ".tif$", recursive = TRUE)
fullL <- list()
# attach file names to env var names
for (i in 1:length(stackOrder)) {
rs <- raslist[grep(paste0(stackOrder[i],".tif"), raslist, ignore.case = TRUE)]
if (length(rs) > 1) {
# always take most recent temporal raster
rs1 <- do.call(rbind.data.frame, strsplit(rs, "_|/"))
rs1$nm <- rs
rs <- rs1$nm[which.max(as.numeric(rs1[,2]))]
}
fullL[[i]] <- rs
}
names(fullL) <- stackOrder
rm(rs,rs1)
envStack <- stack(fullL)
# run prediction ----
setwd(paste0(loc_model, "/", model_species,"/outputs"))
fileNm <- paste0("model_predictions/", model_run_name,".tif")
# use parallel processing if packages installed
if (all(c("snow","parallel") %in% installed.packages())) {
try({
beginCluster(type = "SOCK")
outRas <- clusterR(envStack, predict, args = list(model=rf.full, type = "prob", index = 2), verbose = T)
writeRaster(outRas, filename = fileNm, format = "GTiff", overwrite = TRUE)
})
try(endCluster())
if (!exists("outRas")) {
cat("Cluster processing failed. Falling back to single-core processing...\n")
outRas <- predict(object=envStack, model=rf.full, type = "prob", index=2,
filename=fileNm, format = "GTiff", overwrite=TRUE)
}
# otherwise regular processing
} else {
cat("Using single-core processing for prediction.\nInstall package 'snow' for faster cluster (multi-core) processing.\n")
outRas <- predict(object=envStack, model=rf.full, type = "prob", index=2,
filename=fileNm, format = "GTiff", overwrite=TRUE)
}