Skip to contents

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)
#> Installing Abimo to D:/a/_temp/Library/kwb.abimo/extdata/abimo_v3.3.0_windows ... The directory "D:/a/_temp/Library/kwb.abimo/extdata/abimo_v3.3.0_windows" was created.
#> ok. (0.88 secs)
output <- try(system2(command, args = "--version", stdout = TRUE))
kwb.abimo:::abimo_version()
#> [1] "Abimo 3.3.0 (cleaned by KWB)"

Basic model run

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

head(result)
#>               CODE       R     ROW      RI    RVOL  ROWVOL   RIVOL FLAECHE
#> 1 1000536281000000 290.063   0.000 290.063 585.425   0.000 585.425   63648
#> 2 0700221581000100 410.831 249.475 161.356 457.318 277.705 179.614   35104
#> 3 0700221391000100 422.356 261.911 160.445 502.827 311.813 191.014   37544
#> 4 0700221401000000 389.757 220.770 168.986 631.977 357.971 274.006   51134
#> 5 0700221581000200 361.886 211.796 150.089 212.424 124.323  88.102   18511
#> 6 0700221561000000 471.358 360.977 110.381 420.131 321.747  98.385   28109
#>   VERDUNSTUN
#> 1    356.307
#> 2    174.499
#> 3    164.064
#> 4    196.663
#> 5    224.535
#> 6    118.332

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)
#> 'data.frame':    25352 obs. of  34 variables:
#>  $ CODE      : chr  "1000536281000000" "0700221581000100" "0700221391000100" "0700221401000000" ...
#>  $ BEZIRK    : int  10 7 7 7 7 7 7 7 7 12 ...
#>  $ STAGEB    : int  53 22 22 22 22 22 22 22 22 62 ...
#>  $ BLOCK     : int  628 158 139 140 158 156 92 93 139 627 ...
#>  $ TEILBLOCK : int  0 1 1 0 2 0 2 1 3 2 ...
#>  $ NUTZUNG   : int  10 21 10 21 50 50 10 10 130 50 ...
#>  $ TYP       : int  23 7 7 7 13 45 11 11 53 47 ...
#>  $ FLGES     : num  53741 25316 24656 40647 15605 ...
#>  $ VG        : num  25.8 64.9 66.7 59.5 57.7 95.9 54.8 53.5 20.1 43.3 ...
#>  $ PROBAU    : num  18.3 42.1 46 34.1 33.4 81.9 35.9 29.6 0.7 17.7 ...
#>  $ PROVGU    : num  7.4 22.8 20.7 25.4 24.4 14 18.9 23.8 19.4 25.6 ...
#>  $ REGENJA   : int  593 537 538 538 538 541 536 534 538 573 ...
#>  $ REGENSO   : int  333 298 298 298 298 299 297 296 298 320 ...
#>  $ BELAG1    : int  18 41 41 41 38 41 49 49 30 7 ...
#>  $ BELAG2    : int  74 27 27 27 44 42 46 46 20 42 ...
#>  $ BELAG3    : int  2 4 4 4 2 15 3 3 5 5 ...
#>  $ BELAG4    : int  6 28 28 28 16 2 2 2 45 46 ...
#>  $ VGSTRASSE : num  95.1 97.4 97.4 97.4 97.4 97.4 97.4 97.4 97.4 95.1 ...
#>  $ STR_BELAG1: num  39.4 47.6 47.6 47.6 47.6 47.6 47.6 47.6 47.6 39.4 ...
#>  $ STR_BELAG2: num  24.8 23 23 23 23 23 23 23 23 24.8 ...
#>  $ STR_BELAG3: num  17.7 21.7 21.7 21.7 21.7 21.7 21.7 21.7 21.7 17.7 ...
#>  $ STR_BELAG4: num  18.1 7.7 7.7 7.7 7.7 7.7 7.7 7.7 7.7 18.1 ...
#>  $ KANAL     : int  0 1 1 1 1 1 1 1 0 1 ...
#>  $ KANART    : int  5 1 1 1 1 1 1 1 0 2 ...
#>  $ KAN_BEB   : int  0 94 94 94 90 89 86 86 0 89 ...
#>  $ KAN_VGU   : int  0 65 65 65 68 70 50 50 0 48 ...
#>  $ KAN_STR   : int  0 93 93 93 88 88 89 89 0 89 ...
#>  $ FELD_30   : int  13 10 10 10 10 10 10 10 10 17 ...
#>  $ FELD_150  : int  12 12 12 12 12 12 12 12 12 10 ...
#>  $ FLUR      : num  8.5 4.1 14.8 5.8 4.6 14.4 4.2 3.2 16.6 11.8 ...
#>  $ STR_FLGES : num  9907 9788 12889 10487 2906 ...
#>  $ KOR_FL_N  : num  1 1 0.84 0.87 1 0.82 1 1 0.82 0.56 ...
#>  $ AGEB1_NR  : int  NA NA NA NA NA NA NA NA NA 600 ...
#>  $ AGEB1     : chr  NA NA NA NA ...
#>  - attr(*, "data_types")= chr [1:34] "C" "N" "N" "N" ...

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

typeof(input_data$REGENJA) # integer
#> [1] "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
#> [1] "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)
#> [1] "integer"

Run Abimo with the modified data

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

head(result)
#>               CODE       R     ROW      RI    RVOL  ROWVOL   RIVOL FLAECHE
#> 1 1000536281000000 315.270   0.000 315.270 636.299   0.000 636.299   63648
#> 2 0700221581000100 434.706 262.339 172.366 483.895 292.024 191.870   35104
#> 3 0700221391000100 446.691 275.414 171.277 531.799 327.889 203.910   37544
#> 4 0700221401000000 412.824 232.126 180.698 669.380 376.385 292.996   51134
#> 5 0700221581000200 384.020 222.665 161.355 225.417 130.703  94.714   18511
#> 6 0700221561000000 496.796 380.013 116.784 442.805 338.713 104.092   28109
#>   VERDUNSTUN
#> 1    362.710
#> 2    178.964
#> 3    168.069
#> 4    201.936
#> 5    230.740
#> 6    122.324

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()
#> {xml_node}
#> <section name="Infiltrationsfaktoren">
#> [1] <item key="Dachflaechen" value="0.00"/>
#> [2] <item key="Belaglsklasse1" value="0.10"/>
#> [3] <item key="Belaglsklasse2" value="0.30"/>
#> [4] <item key="Belaglsklasse3" value="0.60"/>
#> [5] <item key="Belaglsklasse4" value="0.90"/>

# Get/set Infiltrationsfaktoren and check the change
config$section_Infiltrationsfaktoren$item_Dachflaechen
#> {xml_node}
#> <item key="Dachflaechen" value="0.00">
config$section_Infiltrationsfaktoren$item_Dachflaechen$set(value = 0.6)
config$section_Infiltrationsfaktoren$item_Dachflaechen
#> {xml_node}
#> <item key="Dachflaechen" value="0.6">

# Similar: other items "Belaglsklasse1", "Belaglsklasse2",
# "Belaglsklasse3", "Belaglsklasse4", e.g.
config$section_Infiltrationsfaktoren$item_Belaglsklasse2
#> {xml_node}
#> <item key="Belaglsklasse2" value="0.30">
config$section_Infiltrationsfaktoren$item_Belaglsklasse2$set(value = 0.99)
config$section_Infiltrationsfaktoren$item_Belaglsklasse2
#> {xml_node}
#> <item key="Belaglsklasse2" value="0.99">

# 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()
#> {xml_node}
#> <section name="PotentielleVerdunstung">
#> [1] <item bezirke="15,16,18,19,20" etp="620" etps="500"/>
#> [2] <item bezirke="8,9,10,12,21,22,23" etp="630" etps="505"/>
#> [3] <item bezirke="13,14,17" etp="640" etps="515"/>
#> [4] <item bezirke="2,3,4" etp="650" etps="520"/>
#> [5] <item bezirke="1,5,6,7,11" etp="660" etps="530"/>
#> [6] <item etp="660" etps="530"/>

# 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()
#> {xml_node}
#> <section name="PotentielleVerdunstung">
#> [1] <item bezirke="123-125" etp="123" etps="234"/>
#> [2] <item bezirke="8,9,10,12,21,22,23" etp="630" etps="505"/>
#> [3] <item bezirke="13,14,17" etp="640" etps="515"/>
#> [4] <item bezirke="2,3,4" etp="650" etps="520"/>
#> [5] <item bezirke="1,5,6,7,11" etp="660" etps="530"/>
#> [6] <item etp="660" etps="530"/>

# 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()
#> {xml_node}
#> <section name="PotentielleVerdunstung">
#> [1] <item etp="660" etps="530"/>

# 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
#> {xml_node}
#> <item bezirke="1,5,6,7,11" etp="660" etps="530">

# 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
#> NULL

# 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
#> {xml_node}
#> <item etp="666" etps="555">

Run Abimo with the modified configuration

result <- kwb.abimo::run_abimo(
  input_data = kwb.abimo::abimo_input_2019,
  config = config
)
#> Writing C:\Users\RUNNER~1\AppData\Local\Temp\RtmpspVEY0/config_20240322-160032.xml ... ok. (0.00 secs)

head(result)
#>               CODE       R     ROW      RI    RVOL  ROWVOL   RIVOL FLAECHE
#> 1 1000536281000000 293.946   0.000 293.946 593.261   0.000 593.261   63648
#> 2 0700221581000100 327.275 135.355 191.920 364.308 150.671 213.637   35104
#> 3 0700221391000100 338.859 145.362 193.497 403.421 173.058 230.364   37544
#> 4 0700221401000000 315.900 118.520 197.380 512.222 192.177 320.045   51134
#> 5 0700221581000200 288.330 105.502 182.829 169.248  61.929 107.319   18511
#> 6 0700221561000000 315.560 177.102 138.458 281.265 157.855 123.411   28109
#>   VERDUNSTUN
#> 1    352.425
#> 2    258.055
#> 3    247.561
#> 4    270.520
#> 5    298.090
#> 6    274.130

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

dir(tempdir())
#>  [1] "abimo_input.dbf"               "abimo_input_result.dbf"       
#>  [3] "abimo_input_result.log"        "abimo_v3.3.0_windows.zip"     
#>  [5] "config_20240322-160032.xml"    "file720258979ec"              
#>  [7] "file7202c4c3ad9"               "file7202e283aec"              
#>  [9] "file72030ee592d"               "file7203861599a"              
#> [11] "file72058ed7526"               "file7206acc1b02"              
#> [13] "file72071d44065"               "file7207f3c7128"              
#> [15] "file720cdd2ec0"                "rmarkdown-str7207e7f4e01.html"

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")
#> Writing D:/a/_temp/Library/kwb.abimo/extdata/config_tmp.xml ... ok. (0.00 secs)

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))