-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetValues_tmax.R
More file actions
48 lines (41 loc) · 1.23 KB
/
getValues_tmax.R
File metadata and controls
48 lines (41 loc) · 1.23 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
library(raster)
library(sf)
library(here)
library(tidyverse)
library(doParallel)
options(scipen = 999)
p.tif <- list.files("E:\\AGCD\\bomR\\tmax-mean\\01day", pattern = ".nc$",
recursive = TRUE)
tmp <- raster(paste0("E:\\AGCD\\bomR\\tmax-mean\\01day\\", p.tif[1]))
shps <- list.files(here("shp"), pattern = "shp$")
i <- 1
for (i in 1:length(shps)){
shp <- st_read(here("shp", shps[i])) %>%
st_transform(crs = crs(tmp))
plot(shp)
#Define how many cores (memory is limiting factor here)
UseCores <- 15
#Register CoreCluster
cl <- makeCluster(UseCores)
registerDoParallel(cl)
e <- 1
df <- foreach(e= 1:length(p.tif),.combine=rbind) %dopar% {
library(raster)
library(sf)
library(here)
library(tidyverse)
library(fasterize)
r1 <- raster(paste0("E:\\AGCD\\bomR\\tmax-mean\\01day\\", p.tif[e]))
#plot(r1)
rst <- crop(r1, shp)
plot(rst)
df <- as.data.frame(mean(getValues(rst), na.rm = TRUE))
colnames(df)[1] <- "mean"
df$date <- str_split_fixed(p.tif[e], "_", 5)[,4]
df$loc <- str_sub(shps[i], end = -5)
df
}
stopCluster(cl)
cat(i ,"of", length(shps), "\n")
saveRDS(df, here("values", paste0("tmax-mean_", str_sub(shps[i], end = -5))))
}