knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = TRUE # kwb.abimo:::on_windows()
)

This vignette aims at demonstrating how to work with the kwb.abimo package.

Install Abimo and check the version

tag <- kwb.abimo:::latest_abimo_version()
command <- kwb.abimo:::check_abimo_binary(tag)
output <- try(system2(command, args = "--version", stdout = TRUE))
kwb.abimo:::abimo_version()

Basic model run

result <- kwb.abimo::run_abimo(input_data = kwb.abimo::abimo_input_2019)

head(result)

Load and modify input data

input_data <- kwb.abimo::abimo_input_2019

# Have a look at the column types. IMPORTANT: Modifications must not change the
# types!
str(input_data)

# Assigning a value of e.g. 600 to an integer column converts the column
# type to double (floating point)!

typeof(input_data$REGENJA) # integer

input_data$REGENJA <- 600

# run_abimo() will complain now...
#kwb.abimo::run_abimo(input_data = input_data)
# Error: Column 'REGENJA' does not have the expected type (integer)!

# Really?
typeof(input_data$REGENJA) # now double

# Problem: By assigning 600, the type was implicitly converted to double.
# Use "L" to indicate integer values (or use as.integer())
input_data$REGENJA <- 600L

# Or e.g.
input_data$REGENJA <- as.integer(1.05 * kwb.abimo::abimo_input_2019$REGENJA)

typeof(input_data$REGENJA) # now integer (again)

Run Abimo with the modified data

result <- kwb.abimo::run_abimo(input_data = input_data)

head(result)

Load and modify a configuration (config.xml), updated for v0.2.0!

# Create a configuration object (from default config.xml stored in this package)
config <- kwb.abimo:::read_config()

# Show the content of one section of the configuration
config$section_Infiltrationsfaktoren$get_xml()

# Get/set Infiltrationsfaktoren and check the change
config$section_Infiltrationsfaktoren$item_Dachflaechen
config$section_Infiltrationsfaktoren$item_Dachflaechen$set(value = 0.6)
config$section_Infiltrationsfaktoren$item_Dachflaechen

# Similar: other items "Belaglsklasse1", "Belaglsklasse2",
# "Belaglsklasse3", "Belaglsklasse4", e.g.
config$section_Infiltrationsfaktoren$item_Belaglsklasse2
config$section_Infiltrationsfaktoren$item_Belaglsklasse2$set(value = 0.99)
config$section_Infiltrationsfaktoren$item_Belaglsklasse2

# Similar: Get/set item values in sections "Bagrovwerte" or 
# "ErgebnisNachkommaStellen", "Diverse"

# More complicated: sections "Gewaesserverdunstung" and "PotentielleVerdunstung". 
# Here we may want to keep only one entry for all "bezirke"

# Show section "PotentielleVerdunstung"
config$section_PotentielleVerdunstung$get_xml()

# Change values of an existing item
config$section_PotentielleVerdunstung$item_1$set(
  bezirke = "123-125", etp = 123, etps = 234
)

# Check the change
config$section_PotentielleVerdunstung$get_xml()

# This was just for demonstration. Now lets remove items 1 to 5
config$section_PotentielleVerdunstung$remove("item_1")
config$section_PotentielleVerdunstung$remove("item_2")
config$section_PotentielleVerdunstung$remove("item_3")
config$section_PotentielleVerdunstung$remove("item_4")
config$section_PotentielleVerdunstung$remove("item_5")

# Show the remaining item in section "Gewaesserverdunstung". 
# Only the last item remains
config$section_PotentielleVerdunstung$get_xml()

# Even though removed from the XML tree the config structure still contains
# the removed items. E.g. item 5 still exists:
config$section_PotentielleVerdunstung$item_5

# To remove the items also from the config structure, we need to update it
config <- config$update()

# Item 5 does not exist any more (is NULL):
config$section_PotentielleVerdunstung$item_5

# Note that the list elements have been renamed. As there is only
# one remaining item, it is just called "item" (without a number)

# Change the value in the remaining item (representing all "bezirke")
config$section_PotentielleVerdunstung$item$set(etp = 666, etps = 555)
config$section_PotentielleVerdunstung$item

Run Abimo with the modified configuration

result <- kwb.abimo::run_abimo(
  input_data = kwb.abimo::abimo_input_2019,
  config = config
)

head(result)

Internally, a new config_<date-time-string>.xml file was created in the temporary folder:

dir(tempdir())

Manually save configuration object to XML

You may create your own config files from config objects using save():

new_xml_file <- config$save(name = "config_tmp.xml")

If something goes wrong, you may compare the created xml file with the original file, e.g. using TortoiseSVN->Diff...

# Open the folder containing the xml file
kwb.utils::hsOpenWindowsExplorer(dirname(new_xml_file))


KWB-R/kwb.abimo documentation built on Feb. 14, 2024, 9:42 a.m.