-
Notifications
You must be signed in to change notification settings - Fork 4
/
example.R
75 lines (54 loc) · 2.77 KB
/
example.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
74
75
rm(list = ls())
source("./R/getOnsetCessation.R")
### point-station calculation ----------------------------------------------------------------
pr_data <- zoo::read.zoo("./data/example_zoo.csv",
header = T,
format = "%Y-%m-%d",
sep = ",")
est <- pr_data[, 3]
# setting hydrological year and removing "02-29" dates
est <- window(est, start = "1981-08-01", end = "2016-07-31")
est <- est[!(format(time(est), "%m-%d") %in% c("02-29"))]
# getting Onset Cessation dates
OC <- getOnsetCessation(zoo::coredata(est))
OC_dates <- zoo::index(est)[OC]
# length of wet season by OC (lengthOC)
lengthOC <- mapply(function(x, y){ y - x },
x = OC[seq(1, length(OC), 2)],
y = OC[seq(2, length(OC), 2)])
plot(lengthOC, type = "l")
# total precipitation by OC (PRCPTOT)
PRCPTOT <- mapply(function(x, y){ sum(est[x:y]) },
x = OC[seq(1, length(OC), 2)],
y = OC[seq(2, length(OC), 2)])
plot(PRCPTOT, type = "l")
# 1-day maximum precipitation by OC (Rx1day)
Rx1day <- mapply(function(x, y){ max(est[x:y]) },
x = OC[seq(1, length(OC), 2)],
y = OC[seq(2, length(OC), 2)])
plot(Rx1day, type = "l")
### gridded calculation ----------------------------------------------------------------
piscop <- raster::brick("./data/piscop_sample.nc")
# setting hydrological year and removing "02-29" dates
seq_ts = seq(as.Date("1981-01-01"), as.Date("2016-12-31"), by = "day")
piscop_ts = zoo::zoo(1:length(seq_ts), seq_ts)
piscop_ts = window(piscop_ts, start = "1981-08-01", end = "2016-07-31")
piscop_ts = piscop_ts[!(format(time(piscop_ts), "%m-%d") %in% c("02-29"))]
piscop = piscop[[zoo::coredata(piscop_ts)]] # this process is really slow!
piscop = piscop + 0 # to get numeric data (bug)
# getting Onset Cessation dates
OC_gridded <- raster::calc(piscop, fun = getOnsetCessation)
# length of wet season by OC (lengthOC)
lengthOC_gridded <- raster::calc(OC_gridded,
fun = function(z){
mapply(function(x, y){
# some years can not be computed because:
# 1) data is bad
# 2) the year does not follow the seasonality
# 3) an atypical year
ifelse(y - x < 0, NA, y - x) },
x = z[seq(1, length(z), 2)],
y = z[seq(2, length(z), 2)])
})
# spatial variability of the lengthOC in the year 1
sp::spplot(lengthOC_gridded[[1]])