-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParallelInteractive_processingtemplate.R
More file actions
117 lines (100 loc) · 5.2 KB
/
ParallelInteractive_processingtemplate.R
File metadata and controls
117 lines (100 loc) · 5.2 KB
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
####---- READ ME ----###
# Use this script as a template for setting up and running batch processing jobs on your local machine.
# This utilizes one or more cores in a multicored machine.
# The required inputs are standardized for ALL functions defined in the Raster Extractions R source file
##---- REQUIRED INPUTS ----##
PROJECT_NAME<-"GCMC2_ParallelTest" # string with a project name
rasterdir<-"S:/GCMC/Data/Climate/PRISM/" # Path to rasters being extracted (first child directories will be used for variable names, i.e. ~/PRISM/precip, ~/PRISM/tmax become the variables "precip,"tmax")
extractionlayer = "C:/Users/wik191/OneDrive - Harvard University/_Projects/Andrea_Bellavia/sites_10M.shp" # Path to your file containing features to be extracted to- csv, shp, gdb
layername = "sites_10M" # Layer name used when extraction layer is an SHP or GDB, otherwise set to NA
IDfield<-"ORIG_FID" # Field in extraction layer specifying IDs for features, can be unique or not, used to chunk up batch jobs
Xfield<- "X" # Field denoting your X axis (only used for csv)
Yfield<- "Y" # Field denoting your Y axis (only used for csv)
startdatefield = "start_date" # Field in extraction layer specifying first date of observations
enddatefield = "end_date" # Field in extraction layer specifying last date of observations
predays = 0 # Integer specifying how many days preceding 'startingdatefield' to extract data. i.e. 365 means data extraction will begin 1 year before values in startdatefield
weights = NA # string specifying file path to raster weights, should only be used when extraction layer is a polygon layer
##---- Required Packages
listOfPackages <- c("batchtools","terra","tools","reshape2","ids")
for (i in listOfPackages){
if(! i %in% installed.packages()){
install.packages(i, dependencies = TRUE)
}
require(i)
}
##REQUIRED##
##---- Initialize conf files and template
##---- Initialize batchtools
##---- Create a temporary registry item
if(file.exists(paste(PROJECT_NAME,"Registry",sep="_"))){
reg = loadRegistry(paste(PROJECT_NAME,"Registry",sep="_"),writeable = TRUE)
}else{
reg = makeRegistry(file.dir = paste(PROJECT_NAME,"Registry",sep="_"), seed = 42)
}
##############################################################
##---- Set up the batch processing jobs
##---- Use the 'batchgrid' function to create a grid of variable combinations to process over. function considers input rasters, input features, and any weighting layers
batchgrid = function(rasterdir,extractionlayer,layername,IDfield,Xfield,Yfield,startdatefield,enddatefield,predays,weightslayers){
require("tools")
##---- Set up the batch processing jobs
pvars = list.dirs(path = rasterdir,full.names = FALSE,recursive = FALSE)
if(file_ext(extractionlayer)=="csv"){
feature<-read.csv(extractionlayer,stringsAsFactors = FALSE)
feature$OID<-1:nrow(feature)
write.csv(x = feature,file = paste0(file_path_sans_ext(extractionlayer),"_tmp",".csv"),row.names = FALSE)
feature<-feature$OID
layername = NA
weightslayers = NA
extractionlayer<-paste0(file_path_sans_ext(extractionlayer),"_tmp",".csv")
IDfield="OID"
}else if(file_ext(extractionlayer) %in% c("shp","gdb")){
require('terra')
vectorfile<- vect(x=extractionlayer,layer=layername)
vectorfile$OID<-1:nrow(vectorfile)
writeVector(x = vectorfile,filename = paste0(file_path_sans_ext(extractionlayer),"_tmp.",file_ext(extractionlayer)),layer=layername,overwrite=TRUE)
feature<- unlist(unique(values(vectorfile[,"OID"])))
Xfield = NA
Yfield = NA
extractionlayer<-paste0(file_path_sans_ext(extractionlayer),"_tmp.",file_ext(extractionlayer))
IDfield="OID"
if (file_ext(extractionlayer)=="shp"){
layername<-paste0(extractionlayer,"_tmp")
}
}
output<- expand.grid(vars = pvars,
pieces = feature,
rasterdir = rasterdir,
extractionlayer = extractionlayer,
layername = layername,
IDfield = IDfield,
Xfield = Xfield,
Yfield = Yfield,
startdatefield = startdatefield,
enddatefield = enddatefield,
predays = predays,
weightslayers = weightslayers,
stringsAsFactors = FALSE)
return(output)
}
##---- Make sure registry is empty
clearRegistry(reg)
##---- create jobs from variable grid
jobs<- batchMap(fun = extract.rast,
batchgrid(rasterdir = rasterdir,
extractionlayer = extractionlayer,
layername = layername,
IDfield = IDfield,
Xfield = Xfield,
Yfield = Yfield,
startdatefield = startdatefield,
enddatefield = enddatefield,
predays = predays,
weightslayers = weights),
reg = reg)
jobs$chunk<-chunk(jobs$job.id,chunk.size = 90)
setJobNames(jobs,paste(abbreviate(PROJECT_NAME),jobs$job.id,sep=""),reg=reg)
getJobTable()
getStatus()
##---- Submit Jobs
batchtools::submitJobs(resources = c(walltime=360000000000, memory=2048),reg = reg)
waitForJobs()