Skip to content

Commit c03504d

Browse files
committed
Added a touched arugment to RomFeature() and added an outaszoo argument to om_get_rundata to allow data frames to be returned
1 parent 5a4d9d6 commit c03504d

File tree

6 files changed

+60
-21
lines changed

6 files changed

+60
-21
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: hydrotools
22
Title: Data processing and accessors for VAHydro
3-
Version: 1.0.10
3+
Version: 1.0.11
44
Authors@R: person("Robert", "Burgholzer", email = "rburghol@vt.edu", role = c("aut", "cre"))
55
Description: Provides set of reference classes and functions to assemble a component based simulation for constructing time-based model simulations.
66
Depends:

R/RomFeature.R

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,23 +233,27 @@ RomFeature <- R6Class(
233233
#' meteorology varkeys include 'prism_mod_daily', 'daymet_mod_daily',
234234
#' 'nldas2_obs_hourly', 'nldas2_precip_hourly_tiled_16x16',
235235
#' 'nldas2_precip_daily', 'amalgamate_simple_lm', 'amalgamate_storm_vol'
236-
#' @param starttime begin tstime to limit query (default FALSE to remove a
236+
#' @param starttime begin date-time to limit query (default FALSE to remove a
237237
#' tstime filter)
238-
#' @param endtime last tsendtime to limit query (default FALSE to remove a
239-
#' tsendtime filter)
238+
#' @param endtime last date-time to limit query (default to Sys.Date())
240239
#' @param band which raster band to query (default = 1)?
241240
#' @param aggregate Should data be aggregated and if so, how? FALSE will
242241
#' return 1 record for every date. Otheriwse, can use basic aggregate
243242
#' functions like mean, min, max to summarize data
244243
#' @param metric default mean, which aspect of the ST_SummaryStats() to return
244+
#' @param touched Defaults to FALSE. Should PostGIS ST_Clip() use the
245+
#' touched argument to return all raster cells touched by the feature? Or,
246+
#' if FALSE, should only pixels that have centroids within the feature be
247+
#' considered?
245248
#' @return dataframe of timeseries values
246249
get_raster_ts = function(
247250
varkey = 'prism_mod_daily',
248251
starttime = FALSE,
249252
endtime = Sys.Date(),
250253
band = '1',
251254
aggregate = FALSE,
252-
metric = 'mean'
255+
metric = 'mean',
256+
touched = FALSE
253257
) {
254258
# looks at the table dh_timeseries_weather
255259
# coverage raster summary
@@ -269,14 +273,21 @@ RomFeature <- R6Class(
269273
}
270274
#If the user wishes to apply an aggregate function, group the data by the
271275
#featureid and apply the aggregate and metric arguments. Otherwise, just
272-
#get the metric from st_summarystats
276+
#get the metric from st_summarystats. The data will be clipped to the
277+
#feature and the user can choose to apply the touched argument from
278+
#postGIS st_Clip
279+
if(touched){
280+
clipSQL <- paste0("st_clip(met.rast, fgeo.dh_geofield_geom,touched => true)")
281+
}else{
282+
clipSQL <- paste0("st_clip(met.rast, fgeo.dh_geofield_geom,touched => false)")
283+
}
273284
if (!is.logical(aggregate)) {
274-
rastercalc = fn$paste0("$aggregate((ST_summarystats(st_clip(met.rast, fgeo.dh_geofield_geom), 1, TRUE)).$metric)")
285+
rastercalc = fn$paste0("$aggregate((ST_summarystats($clipSQL, 1, TRUE)).$metric)")
275286
groupby = "GROUP BY met.featureid"
276287
startcol = "to_timestamp(min(met.tsendtime))"
277288
endcol = "to_timestamp(max(met.tsendtime))"
278289
} else {
279-
rastercalc = fn$paste0("(ST_summarystats(st_clip(met.rast, fgeo.dh_geofield_geom), 1, TRUE)).$metric")
290+
rastercalc = fn$paste0("(ST_summarystats($clipSQL, 1, TRUE)).$metric")
280291
groupby = ""
281292
startcol = "to_timestamp(met.tsendtime)"
282293
endcol = "to_timestamp(met.tsendtime)"

R/om_get_rundata.R

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
#' particular model and run ID (scenario ID).
44
#' @details A wrapper of \code{hydrotools::fn_get_runfile()}. This function
55
#' returns the results in a zoo (see zoo::zoo()) that has the timestamp as the
6-
#' index. See \code{hydrotools::fn_get_runfile()} for more details.
7-
#'
6+
#' index. See \code{hydrotools::fn_get_runfile()} for more details. If
7+
#' outaszoo is TRUE, the output will be a zoo TS with numeric mode. Please note
8+
#' that zoo's are matrices and cannot hold more than one data type such that
9+
#' character and date fields will be set to NA.
810
#' @param elid integer OM element connection ID e.g. the original OM model ID
911
#' @param runid integer run id representing the scenario. Ask the modeling team
1012
#' for scenario IDs if you are unsure otherwise see the WSPA Shiny Dashboard
@@ -17,15 +19,18 @@
1719
#' @param cleanup Logical. Should the function delete the log file create for
1820
#' the cached argument? If this is TRUE, the .log files will be deleted after
1921
#' download from OM server
22+
#' @param outaszoo boolean return as a zoo timeseries with numeric mode if TRUE,
23+
#' or as data frame if FALSE. Default is TRUE.
2024
#' @return data.frame of model results
2125
#' @export om_get_rundata
2226
#' @examples #om_get_rundata(72446, 600, site=omsite)
2327
om_get_rundata <- function(elid, runid, site='http://deq2.bse.vt.edu',
2428
cached=FALSE, hydrowarmup=TRUE,
25-
cleanup = FALSE) {
29+
cleanup = FALSE, outaszoo = TRUE) {
2630
# replace this with a single function that grabs
2731
# a hydro model for summarization and slims it down
28-
dat <- fn_get_runfile(elid, runid, 37, site, cached = FALSE, cleanup = cleanup)
32+
dat <- fn_get_runfile(elid, runid, 37, site, cached = FALSE, cleanup = cleanup,
33+
outaszoo = outaszoo)
2934
syear = as.integer(min(dat$year))
3035
eyear = as.integer(max(dat$year))
3136
if ((hydrowarmup == TRUE) & (syear < (eyear - 2))) {
@@ -47,9 +52,13 @@ om_get_rundata <- function(elid, runid, site='http://deq2.bse.vt.edu',
4752
edate <- as.POSIXct(edate,tz = "EST")
4853

4954
#Get the window of interest from the timeseries
50-
dat <- stats::window(dat, start = sdate, end = edate);
51-
#Change mode of zoo to numeric e.g. Convert all fields to numeric
52-
mode(dat) <- 'numeric'
55+
if(outaszoo){
56+
dat <- stats::window(dat, start = sdate, end = edate)
57+
#Change mode of zoo to numeric e.g. Convert all fields to numeric
58+
mode(dat) <- 'numeric'
59+
}else{
60+
dat <- dat[dat$timestamp >= sdate & dat$timestamp <= edate,]
61+
}
5362

5463
return(dat)
5564
}

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ Rob Burgholzer ([robert.burgholzer\@deq.virginia.gov](mailto:robert.burgholzer@d
109109
This package is in active development.
110110

111111
## Release notes
112+
### 1.0.11 01/07/2026
113+
1. Updated the documentation on `RomFeature$get_raster_ts()` to be more accurate
114+
for the starttime and endtime arguments. Additionally, added a `touched` argument
115+
to leverage the new options in PostGIS `ST_Clip()`
116+
2. Added an `outaszoo` argument to `om_get_rundata()` to allow data frames of
117+
model data to be returned.
118+
112119
### 1.0.10 12/15/2025
113120
1. Update elfgen wrappers to allow for more dynamic generation of NHDPlus flows.
114121
2. Added an `na.rm = TRUE` to the mean flows call in `om_flow_table()`

man/RomFeature.Rd

Lines changed: 9 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/om_get_rundata.Rd

Lines changed: 9 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)