Skip to content

Unexpected results due to inconsistency with date range lengths #42

Description

@jonbry

There are multiple functions in PWDGSI that modify the start and end dates of date ranges different. For example, marsFetchRainfallData and marsFetchRainEventData both add a single day to the end date when querying the database:

event_query <- paste(paste0("SELECT * FROM ", rainparams$eventtable),
"WHERE", rainparams$uidvar, "= CAST('", rainsource, "' as int)",
"AND eventdatastart >= Date('", start_date, "')",
"AND eventdataend <= Date('", end_date + lubridate::days(1), "');")
events <- DBI::dbGetQuery(con, event_query)

marsFetchLevelData on the other hand adds a day before and a day after:

# Add buffer to requested dates based on DB time zone
start_date <- lubridate::round_date(start_date) - lubridate::days(1)
end_date <- lubridate::round_date(end_date) + lubridate::days(1)

While this gives unexpected results (returns a range outside of the requested amount without notification), it is exasperated by functions that join the data or add their own changes to the date range. For example, the marsEventCombinedPlot adds an extra day before and after, which is then passed to marsFetchMonitoringData, which then is passed to marsFetchRainEventData and MarsFetchRainfallData.

#check for one day on either side of the event date
event_date %<>% as.POSIXct(format = '%Y-%m-%d')
start_date <- event_date - 86400
end_date <- event_date + 86400

This leads to unexpected results when with marsEventCombinedPlot. The following arguments returns an event for 2024-04-01 even though there was no rain on 3/31:

new_mars <- marsEventCombinedPlot(con = conn_sand,
                             event_date = "2024-03-31",
                             source = "gage",
                             smp_id = "1267-2-1",
                             ow_suffix = "CS2")

But the following shows no rain or event even though there was a rain event on 2024-03-27:

new_mars <- marsEventCombinedPlot(con = conn_sand,
                             event_date = "2024-03-26",
                             source = "gage",
                             smp_id = "1267-2-1",
                             ow_suffix = "CS2")

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions