-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonthlyHargreavesPETraster.R
73 lines (63 loc) · 2.22 KB
/
monthlyHargreavesPETraster.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
# create monthly Hargreaves based water balance
# Applying SPEI::Hargreaves to raster grid using overlay creates problem with monthly cycling through each grid
# This code creates driving grids without needing specific time series cycles to be respected
# code taken from SPEI::Hargreaves
# MAC 10/1/18
library(raster)
# set rasteroptions
rasterOptions(progress = 'text')
# load grids Livneh or PRISM
tmin<-stack("/scratch/crimmins/PRISM/monthly/processed/west/resampled/resampledWESTmonthlyPRISM_tmin_1895_2017.grd")
tmax<-stack("/scratch/crimmins/PRISM/monthly/processed/west/resampled/resampledWESTmonthlyPRISM_tmax_1895_2017.grd")
# get tDiff and tAvg
tDiff<-tmax-tmin
tAvg<-(tmax+tmin)/2
# get latitudes
lat<-init(tmin, 'y')
latStack <- stack(replicate(12, lat))
# create month grid of values
month <- latStack
for(i in 1:12){
month[[i]]<-setValues(latStack[[i]],i)
}
# days in month stack
mlen <- c(31, 28.25, 31, 30, 31, 30, 31, 31, 30, 31, 30,
31)
monthDays <- latStack
for(i in 1:12){
monthDays[[i]]<-setValues(latStack[[i]],mlen[i])
}
# monthly radiation calc based on month and latitude
monthRadcalc<-function(c,lat){
J <- as.integer(30.5 * c - 14.6)
delta <- 0.409 * sin(0.0172 * J - 1.39)
dr <- 1 + 0.033 * cos(0.0172 * J)
latr <- lat/57.2957795
sset <- -tan(latr) * tan(delta)
omegas <- sset * 0
omegas[sset >= {
-1
} & sset <= 1] <- acos(sset[sset >= {
-1
} & sset <= 1])
omegas[sset < {
-1
}] <- max(omegas)
Ra <- 37.6 * dr * (omegas * sin(latr) * sin(delta) +
cos(latr) * cos(delta) * sin(omegas))
Ra <- ifelse(Ra < 0, 0, Ra)
}
# get grid stack of month average radiation
monthRad <- raster::overlay(month,latStack,fun = monthRadcalc)
# create full time series stack
years=100 # 1915-2015
allRad<- stack(replicate(101, monthRad))
# daily ETO calc - average daily ET0 by month
ET0 <- function(Ra, tavg, tdiff) {
0.0023 * 0.408 * Ra * (tavg + 17.8) * tdiff^0.5
}
harg <- raster::overlay(allRad, tAvg, tDiff, fun = ET0)
# get total monthly ET0 in mm
harg<-harg*stack(replicate(101, monthDays))
# write PET to grid
writeRaster(harg,filename="/scratch/crimmins/PRISM/monthly/processed/west/resampled/resampledWESTmonthlyPRISM_hargreaves_1895_2017.grd", overwrite=TRUE )