Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subdaily EGRET plots #223

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions R/convertToEGRET.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ convertToEGRETSample <- function(data = NULL, meta = NULL, dailydat = NULL) {
sample_data <- sample_data %>% # use transform b/c mutate breaks with smwrQW columns
mutate(Uncen = as.numeric(!is.na(ConcLow) & !is.na(ConcHigh) & ConcHigh == ConcLow)) %>%
populateSampleColumns() %>%
mutate(
dateTime = data[[date_col]],
Date = as.Date(Date))
mutate(Date = data[[date_col]])

# Format the flow info
flow_col <- getInfo(meta, 'flow', TRUE)
Expand All @@ -103,11 +101,11 @@ convertToEGRETSample <- function(data = NULL, meta = NULL, dailydat = NULL) {
flow.colname = flow_col,
date.colname = date_col,
flow.units = getUnits(meta, 'flow', 'EGRET')) %>%
select(Date, Q, dateTime)
select(Date, Q)

# Combine the sample and flow info
sample_df <- sample_data %>%
left_join(flow_data, by=c('dateTime', 'Date'))
left_join(flow_data, by=c('Date'))

# Add in predictions if available. The sample-specific model predictions
# created by EGRET are actually leave-one-out estimates where yHat, SE, and
Expand All @@ -120,8 +118,8 @@ convertToEGRETSample <- function(data = NULL, meta = NULL, dailydat = NULL) {
if(!is.null(dailydat) && all(c('yHat','SE','ConcDay') %in% names(dailydat))) {
sample_df <- left_join(
sample_df,
select(dailydat, dateTime, yHat, SE, ConcHat=ConcDay),
by='dateTime')
select(dailydat, Date, yHat, SE, ConcHat=ConcDay),
by='Date')
}

return(sample_df)
Expand Down Expand Up @@ -200,8 +198,8 @@ convertToEGRETDaily <- function(newdata, load.model = NULL, meta = NULL) {
# (3) is the estimated concentration (ConcHat).
fit <- se.pred <- '.dplyr.var'
daily_df <- daily_df %>%
left_join(select(preds_log, date, yHat = fit, SE = se.pred), by=c("dateTime" = "date")) %>%
left_join(select(preds_lin, date, ConcDay = fit), by=c("dateTime" = "date"))
left_join(select(preds_log, date, yHat = fit, SE = se.pred), by=c("Date" = "date")) %>%
left_join(select(preds_lin, date, ConcDay = fit), by=c("Date" = "date"))

# Fill in the Flux preds based on the Conc preds
meta.conv <- updateMetadata(meta, flow='Q', flow.units='cms', load.rate.units='kg d^-1')
Expand Down Expand Up @@ -241,8 +239,8 @@ expandFlowForEGRET <- function(flowdat, flow.colname, date.colname, flow.units)
rename_("value" = flow.colname,
"dateTime" = date.colname) %>%
mutate(code = "") %>%
populateDaily(qConvert = qconvert, interactive = FALSE) %>%
mutate(dateTime = flowdat[[date.colname]])
populateDaily(qConvert = qconvert, verbose = FALSE) %>%
mutate(Date = flowdat[[date.colname]])

return(flowdat_corrected)
}
14 changes: 7 additions & 7 deletions tests/testthat/test-51-EGRET.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ test_that("convertToEGRETSample returns NA without fitdat", {
test_that("convertToEGRETSample correctly converts", {
Sample <- loadflex:::convertToEGRETSample(meta = meta, data = fitdat)
expect_equal(nrow(Sample), nrow(fitdat))
expect_equal(ncol(Sample), 15)
expect_equal(ncol(Sample), 14)

expected_cols <- c("Date", "ConcLow", "ConcHigh", "Uncen", "ConcAve", "Julian",
"Month", "Day", "DecYear", "MonthSeq", "waterYear", "SinDY",
"CosDY", "dateTime", "Q")
"CosDY", "Q")
expect_equal(sort(names(Sample)), sort(expected_cols))
})

Expand All @@ -95,16 +95,16 @@ test_that("convertToEGRETDaily returns NA without estdat or preds", {

test_that("convertToEGRETDaily returns partial info without preds", {
Daily <- loadflex:::convertToEGRETDaily(meta = meta, newdata = estdat)
expect_equal(ncol(Daily), 14)
expect_equal(ncol(Daily), 13)
})

test_that("convertToEGRETDaily correctly converts", {
Daily <- loadflex:::convertToEGRETDaily(newdata = estdat, load.model = conc_lm)
expect_equal(nrow(Daily), nrow(estdat))
expect_equal(ncol(Daily), 18)
expect_equal(ncol(Daily), 17)

expected_cols <- c("Date", "Q", "Julian", "Month", "Day", "DecYear", "MonthSeq",
"waterYear", "Qualifier", "i", "LogQ", "Q7", "Q30", "dateTime",
"waterYear", "Qualifier", "i", "LogQ", "Q7", "Q30",
"ConcDay", "SE", "FluxDay", "yHat")
expect_equal(sort(names(Daily)), sort(expected_cols))
})
Expand All @@ -115,8 +115,8 @@ context("expandFlowForEGRET")
test_that("expandFlowForEGRET returns correct columns", {
corrected_flow_df <- loadflex:::expandFlowForEGRET(estdat, 'DISCHARGE', 'DATE', 'ft^3 s^-1')
expect_equal(nrow(corrected_flow_df), nrow(estdat))
expect_equal(ncol(corrected_flow_df), 14)
expect_equal(ncol(corrected_flow_df), 13)
expected_cols <- c("Date", "Q", "Julian", "Month", "Day", "DecYear", "MonthSeq",
"waterYear", "Qualifier", "i", "LogQ", "Q7", "Q30", "dateTime")
"waterYear", "Qualifier", "i", "LogQ", "Q7", "Q30")
expect_equal(sort(names(corrected_flow_df)), sort(expected_cols))
})