-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspiPRISM.R
42 lines (34 loc) · 1.37 KB
/
spiPRISM.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
# SPI/SPEI for monthly PRISM
# MAC 08/08/18
library(SPEI)
library(raster)
library(zoo)
library(rasterVis)
# set rasteroptions
rasterOptions(progress = 'text')
# map layers
states <- getData('GADM', country='United States', level=1)
# dates 1895-2017 PRISM data
dates=seq(as.Date("1895-01-01"), as.Date("2017-12-01"), by="month")
# load RESAMPLED data
prec<-stack("/scratch/crimmins/PRISM/monthly/processed/west/resampled/resampledWESTmonthlyPRISM_prec_1895_2017.grd")
prec<-prec[[which(dates=="1915-01-01"):which(dates=="2015-12-01")]] # breaks at 1918
# calculate SPI
funSPI <- function(x, scale=12, na.rm=TRUE,...) as.numeric((spi(x, scale=scale, na.rm=na.rm, ...))$fitted)
#rstSPEI <- calc(wtrBal, fun = funSPEI)
# parallell calc
ptm <- proc.time()
beginCluster(7)
spiTemp <- clusterR(prec, calc, args=list(fun=funSPI))
endCluster()
proc.time() - ptm
# plot grids
# my.at <- seq(-3, 3, 0.5)
# mapTheme <- rasterTheme(region=brewer.pal(11,"Spectral"))
# levelplot(spiTemp[[1212]], par.settings = mapTheme, at=my.at, margin=FALSE, main="SPI", colorkey = list(space='right'))+
# layer(sp.polygons(states))
# # check stats of dist fitting
# meanSPI<-calc(spiTemp, mean, na.rm=TRUE)
# sdSPI<-calc(spiTemp, sd, na.rm=TRUE)
# write out files
writeRaster(spiTemp,filename="/scratch/crimmins/PRISM/monthly/processed/west/resampled/resampledWESTmonthlyPRISM_SPI12_1915_2015.grd", overwrite=TRUE )