[R] compounding precipitation based on whether falls within a day
Morway, Eric
emorway at usgs.gov
Wed Sep 13 18:52:30 CEST 2017
Using the small reproducible example below, I'm wondering how best to
complete the following task:
In the small reproducible example below, the 3D array prec has indexes that
correspond to time, x, y (i.e., prec[time, x, y]). In this case, the time
index is hours since some predefined start time. I'd like to add up all
the time indexes in 'prec' based on whether or not the corresponding hours
fall within a given day. So, at the end of the small example below, there
are two variables that I'm left with, prec_idx (an hourly sequence from
beg_time to end_time) whose length is equal to the first index (the time
index) of the 3D array of precipitation called prec. That is, I'd like to
get a 3D array called prec*_daily* that has dimension prec*_daily*[21, 3, 4],
where 21 is the number of days and the value in say prec*_daily*[1,x,y] is
equal to prec[1,x,y] + prec[2,x,y] + ... + prec[24,x,y]
ndays <- 21
base_time <- as.character('2001-12-31T23:00:00Z')
hrs_since_base <- 1
# adding an extra second to the end b/c I'm paranoid about the midnight
time stamp not being explicit
beg_time <- as.POSIXct(base_time, format = "%Y-%m-%dT%H:%M:%SZ") +
(hrs_since_base * 60 * 60) + 1
max_hr_since <- 24 * ndays
end_time <- as.POSIXct(base_time, format = "%Y-%m-%dT%H:%M:%SZ") +
(max_hr_since * 60 * 60) + 1
prec_idx <- seq(beg_time, end_time, by='hour')
prec <- array(abs(rnorm((24*ndays) * 3 * 4)) , dim=c(24*ndays, 3, 4))
length(prec_idx)
# 504
dim(prec)
# 504 3 4
# How do I aggregate prec to get daily sums of precipitation based on the
prec_idx array?
[[alternative HTML version deleted]]
More information about the R-help
mailing list