Define the SWAT output variables that should be extracted after the SWAT
model execution and be returned to R. It is required to use this function to
pass the desired outputs with the variable output in the function calls
run_swat2012 and run_swatplus. See
the examples how to use the output definition together with
run_swat2012 or run_swatplus. Further, more comprehensive
examples are provided on the package's 'Get Started' page in the section
'First SWAT model runs.
define_output(file, variable = NULL, unit = NULL, expression = NULL)Character string. The SWAT output file to read.
(Valid inputs are 'rch', 'sub', 'hru', and 'sed' for
the respective SWAT2012 output files 'output.rch', 'output.sub',
output.hru', and 'output.sed'. For the respective SWAT+ output files see
the available options in the 'print.prt' file of your SWAT+ project).
Character string. Output variable that is extracted from the
respective SWAT output file defined with file. For a correct
definition of SWAT2012 output variables please use the
variable documented in the
SWAT Output Data
Documentation. For SWAT+ output variables please use the header names from
the respective output table, without the units! Optionally, the
column number of a variable in the respective output file can be provided.
CAUTION: spaces (e.g. in P TOT) must be replaced with underscores
(P_TOT).
Numeric vector. The spatial unit (e.g. the reach, subbasin, or HRU) defined by the columns 'RCH', 'SUB', 'HRU' in the respective SWAT2012 output file or the 'unit' column in the SWAT+ output file for which the outputs should be extracted.
As an alternative to variable and unit an
expression can be defined as a string to perform an individual extraction of outputs.
This is still experimental and is not necessary in most cases!
# A single variable can be defined as follows (e.g. "FLOW_OUT" for
# the reaches 1 and 5):
out_flow <- define_output(file = "rch",
variable = "FLOW_OUT",
unit = c(1,5))
# In this case the the variable name of the returned output is then
# the same as defined with 'variable', here "FLOW_OUT"
# If a custom variable name is preferred for the returned output,
# the output must be defined as named list:
out_flow <- list(discharge = define_output(file = "rch",
variable = "FLOW_OUT",
unit = c(1,5)))
# Define the discharge for the RCH units 1 to 5 and the
# nitrate-nitrogen load and ET for the unit 5:
out_def <- list(flow = define_output(file = "rch",
variable = "FLOW_OUT",
unit = 1:5),
no3 = define_output(file = "rch",
variable = "NO3_OUT",
unit = 5),
et_a = define_output(file = "sub",
variable = "ET",
unit = 5))
# Define output with an expression:
# E.g. directly extract P_TOT concentrations for reach 5 from
# daily simulations:
expr <- paste0("dplyr::filter(., RCH == 5) %>% ",
"dplyr::filter(., MON %in% 1:12) %>% ",
"dplyr::select(., FLOW_OUT, P_TOT) %>% ",
"dplyr::mutate(., P_CONC = P_TOT/FLOW_OUT/86.4) %>% ",
"dplyr::select(., P_CONC)")
out_def <- list(p_conc = define_output(file ="rch", expression = expr))
#> Error in define_output(file = "rch", expression = expr): If 'expression' is given, 'unit' must be provided as well.