Skip to content

Commit

Permalink
Merge branch '105-plot-griwrmoutputsmodel-handle-other-units-than-mm-…
Browse files Browse the repository at this point in the history
…time-step' into 'dev'

Resolve "`plot.GRiwrmOutputsModel`: handle other units than mm / time step"

Closes #105

See merge request in-wop/airGRiwrm!98
  • Loading branch information
Dorchies David committed Jul 31, 2024
2 parents 832876f + c2b04ea commit fbd7ff8
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 13 deletions.
2 changes: 2 additions & 0 deletions R/RunModel.GRiwrmInputsModel.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@ RunModel.GRiwrmInputsModel <- function(x, RunOptions, Param, ...) {
)
}
attr(OutputsModel, "Qm3s") <- OutputsModelQsim(x, OutputsModel, RunOptions[[1]]$IndPeriod_Run)
attr(OutputsModel, "GRiwrm") <- attr(x, "GRiwrm")
attr(OutputsModel, "TimeStep") <- attr(x, "TimeStep")
return(OutputsModel)
}
2 changes: 1 addition & 1 deletion R/RunModel_Reservoir.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' The reservoir model is a model combining a lag model and the calculation of
#' the water storage time series according to the released flow time series
#' from the `Qinf` parameter of [CreateInputsModel.GRiwrm].
#' from the `Qrelease` parameter of [CreateInputsModel.GRiwrm].
#'
#' @details
#' The simulated flow corresponds to the released flow except when the reservoir
Expand Down
22 changes: 17 additions & 5 deletions R/plot.GRiwrmOutputsModel.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#' @param Qobs (optional) [matrix] time series of observed flows
#' (for the same time steps than simulated) (mm/time step) with one column
#' by hydrological model output named with the node ID (See [CreateGRiwrm] for details)
#' @param unit (optional) [character] flows unit ("m3/s" or "mm")
#' @param ... Further arguments for [airGR::plot.OutputsModel] and [plot]
#'
#' @return [list] of plots.
Expand All @@ -13,16 +14,22 @@
#'
#' @example man-examples/RunModel.GRiwrmInputsModel.R
#'
plot.GRiwrmOutputsModel <- function(x, Qobs = NULL, ...) {
plot.GRiwrmOutputsModel <- function(x, Qobs = NULL, unit = "m3/s", ...) {

# Arguments checks
stopifnot(is.null(Qobs) || is.matrix(Qobs) || is.data.frame(Qobs),
is.character(unit),
unit %in% c("mm", "m3/s"))

griwrm <- attr(x, "GRiwrm")
## define outer margins and a title inside it
oldpar <- par(no.readonly = TRUE)
on.exit(par(oldpar))
par(oma = c(0, 0, 3, 0))

lapply(
names(x),
function(id, OutputsModels) {
function(id) {
Qobs_id <- NULL
if (!is.null(Qobs)) {
if (id %in% colnames(Qobs)) {
Expand All @@ -31,10 +38,15 @@ plot.GRiwrmOutputsModel <- function(x, Qobs = NULL, ...) {
warning("Column \"", id, "\" not found in Qobs")
}
}
plot(OutputsModels[[id]], Qobs = Qobs_id, ...)

BasinArea <- griwrm$area[griwrm$id == id & !is.na(griwrm$model) & griwrm$model != "Diversion"]
if (unit == "m3/s" && length(BasinArea) == 1 && !is.na(BasinArea)) {
plot(x[[id]], Qobs = Qobs_id, BasinArea = BasinArea, ...)
} else {
plot(x[[id]], Qobs = Qobs_id, ...)
}
title(main = id, outer = TRUE, line = 1.2, cex.main = 1.4)
},
OutputsModels = x
}
)
invisible(NULL)
}
10 changes: 9 additions & 1 deletion R/plot.OutputsModelReservoir.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
#' Plot simulated reservoir volume, inflows and released flows time series on a reservoir node
#'
#' @param x Object returned by [RunModel_Reservoir]
#' @param Qobs [numeric] time series of observed flow (for the same time steps than simulated) \[m3/time step\]
#' @param Qobs (optional) [numeric] time series of observed released flow \[m3/time step\]
#' @param ... Further arguments passed to [plot.Qm3s]
#'
#' @return Function used for side effect.
#' @export
#'
#' @example man-examples/RunModel_Reservoir.R
plot.OutputsModelReservoir <- function(x, Qobs = NULL, ...) {

# Argument checks
if (!is.null(Qobs)) {
stopifnot(is.numeric(Qobs),
length(Qobs) == length(x$Qsim_m3))

}

oldpar <- par(mfrow=c(2,1),
mar = c(2,3.3,1.2,0.5),
mgp = c(2,1,0))
Expand Down
2 changes: 1 addition & 1 deletion man-examples/RunModel_Reservoir.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@ plot(OutputsModel, Qobs = Qobs)
# N.B. "Observed releases" should be considered as "Target releases" here

# The plot for the reservoir can also be plotted alone
plot(OutputsModel$Reservoir)
plot(OutputsModel$Reservoir, Qobs = Qobs[, "Reservoir"])
4 changes: 2 additions & 2 deletions man/RunModel_Reservoir.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/plot.GRiwrmOutputsModel.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/plot.OutputsModelReservoir.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fbd7ff8

Please sign in to comment.