-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonthlyThornthwaite_CYVERSE.R
72 lines (60 loc) · 2.3 KB
/
monthlyThornthwaite_CYVERSE.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
# Thornthwaite Calc on Livneh data for CYVERSE
# MAC 08/08/2018
# installing parts of SPEI
#packageurl <- "https://cran.r-project.org/src/contrib/Archive/goftest/goftest_1.0-3.tar.gz"
#install.packages(packageurl, repos=NULL, type="source")
library(SPEI)
library(raster)
library(zoo)
#library(rasterVis)
# set rasteroptions
rasterOptions(progress = 'text')
# dates 1915-2015 PRISM data
dates=seq(as.Date("1915-01-01"), as.Date("2015-12-31"), by="month")
# load data from local
tave<-stack("/scratch/processed/WESTmonthlyLivneh_tave_1915_2015.grd")
tave<-tave[[which(dates=="2015-01-01"):which(dates=="2015-03-01")]]
# # calculate monthly thornthwaite
# tw <- function(Tave, lat) {
# SPEI::thornthwaite(Tave, lat, na.rm=TRUE)
# }
#
# a <- raster(tave)
# lat <- init(a, "y")
# thorn <- raster::overlay(tave, lat, fun = Vectorize(tw), filename="/scratch/processed/WESTmonthlyLivneh_thornthwaite_1915_2015.grd") # add filename
# VECTORIZED thornthwaite ET ->
# https://stackoverflow.com/questions/50452135/thornthwaite-evapotranspiration-on-a-raster-dataset-error-formula-not-vectoris
th <- function(Tave, lat) {
as.vector(SPEI::thornthwaite(as.vector(Tave), lat, na.rm=TRUE))
}
a <- raster(tave)
lat <- init(a, "y")
#lon <- init(a, "x")
out <- brick(tave, values=FALSE)
# for (i in 1:ncell(tave)) {
# out[i] <- th(tave[i], lat[i])
# print(i/ncell(tave)*100)
# }
# use parallel
library(parallel)
#f2<-function(i){return(th(tave[i], lat[i]))}
f2<-function(i){out[i] <- th(tave[i], lat[i])}
# cores <- detectCores() - 1
# cl <- makeCluster(7, type='FORK')
# out<-mclapply(1:ncell(tave), f2, mc.cores=7)
# stopCluster(cl)
library(pbmcapply)
cl <- makeCluster(7, type='FORK')
out<-pbmclapply(1:ncell(tave), f2, mc.cores=7)
stopCluster(cl)
# use setValues to assign back to out brick
# use setValues to assign back to out brick
outMat<-matrix(unlist(out), ncol = nlayers(tave), byrow = TRUE) # ncol number of months
tempGrid <- brick(tave, values=FALSE)
tempGrid<-setValues(tempGrid, outMat)
# coerce back into raster
# df.raster <- as.data.frame(matrix(unlist(out), ncol = 72, byrow = TRUE))
# df.raster$lat<-getValues(lat)
# df.raster$lon<-getValues(lon)
# thornGrid <- rasterFromXYZ(df.raster, res=c(0.0625,0.0625))
writeRaster(harg,filename="/scratch/processed/WESTmonthlyLivneh_thornthwaite_1915_2015.grd", overwrite=TRUE )