Manipulating STICS text files

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
options(tibble.print_min = 5, tibble.print_max = 5)
library(SticsRFiles)
suppressWarnings(library(SticsRFiles))
stics_version <- get_stics_versions_compat()$latest_version

Overview

This package allows the user to programmatically manipulate either the XML files used to store more information, or the text files directly used as inputs by the STICS model. This vignette explains the second one. For more details on how to manipulate XML files, please refer to the vignette Manipulating STICS XML files.

Disclaimer: These functionalities are more oriented toward advanced users and/or developers.

Using this package, it is possible to:

To run a STICS simulation on these input files, please refer to the run_stics() function from SticsOnR.

Some example data are available from the package and can be retrieved using the following function call for the STICS version r stics_version:

example_txt_dir <-
  get_examples_path(file_type = "txt", stics_version = stics_version)

The example_txt_dir directory is: r example_txt_dir. By default, the latest STICS version is taken into account. For getting an example directory for another version, an additional argument must be given stics_version according to an existing version included in the package. The version list can be retrieved using:

get_stics_versions_compat()$versions_list

Getting parameter value

get_param_txt() helps to get the value of a given parameter in a USM. It has two main arguments, the directory of the USM (workspace, where the text files lives), and the parameter names (param) to be searched. But, if param is not used, the returned named list contains all the STICS parameters for a given USM.

Additional arguments can be set for specific extractions:

For example to get the value of the atmospheric pressure:

get_param_txt(workspace = example_txt_dir, param = "patm")

The function returns a named list according to the file where it found the parameter value (here "station") containing all the values found for the patm parameter (here 1000), along with the parameter name as found in the file. The function performs a fuzzy search, so here after the name of the parameter is only partially given, e.g.: atm

get_param_txt(workspace = example_txt_dir, param = "atm")

Note that the function does not require the user to know in which file the parameter is because it search the parameter in all files. So if it finds several parameter with the same given name, it will return all. The next example shows how to get parameters from the soil file containing the letter "a":

get_param_txt(workspace = example_txt_dir, param = "a")$soil

For an exact search of parameters, a specific argument exact must be set to TRUE (the default value is FALSE if not provided).

Here we see another peculiarity: the name associated to the value has more information for the plant file, e.g. " plant$plant1$P_Nmeta" instead of "plant$P_Nmeta". This is because a USM can potentially have two plant files if it is an intercrop, so the function returns the plant file index where the parameter was found.

It is the same case for the tec file:

get_param_txt(workspace = example_txt_dir, param = "interrang")

A particular search in plant parameters for specific varieties, can be done using the variety argument:

get_param_txt(workspace = example_txt_dir, param = "stlevamf",
              variety = c("Pactol", "Cecilia", "clarica"))
get_param_txt(workspace = example_txt_dir, param = "stlevamf",
              variety = c(1, 2, 5))

The get_param_txt() function is a wrapper around other lower-level functions that reads the parameter values in a given file:

All these functions are exported for convenience, but get_param_txt() is easier to use.

Getting the meteorological data

The get_climate_txt() function helps to get the data from the climat.txt file, and is used as:

get_climate_txt(workspace = example_txt_dir)
library(dplyr)
get_climate_txt(workspace = example_txt_dir) %>%
  rmarkdown::paged_table()

The function adds a Date column that is at the standard POSIXct format for convenience.

Setting parameter value

set_param_txt() is used to set the value of a given parameter in a USM. It has three main arguments, the directory of the USM (workspace, where the text files lives), the parameter name (param), and the new value of the parameter.

The actual value of of the atmospheric pressure read above is:

get_param_txt(workspace = example_txt_dir, param = "patm")

To set a new value of patm to 900:

set_param_txt(workspace = example_txt_dir, param = "patm", value = 900)

Now we can check that the value is changed:

get_param_txt(workspace = example_txt_dir, param = "patm")
# resetting the value:
set_param_txt(workspace = example_txt_dir, param = "patm", value = 1000)

There are four more arguments to the function:

Specific replacements may be performed combining additional arguments as plant_id, layer:

get_param_txt(workspace = example_txt_dir, param = "densinitial")
set_param_txt(workspace = example_txt_dir,
              param = "densinitial",
              plant_id = 1,
              layer = c(1, 4),
              value = c(0.5, 0.1))
get_param_txt(workspace = example_txt_dir, param = "densinitial")

Note that as for get_param_txt(), set_param_txt() finds automatically the file where the parameter is. It is also a wrapper around lower-level functions that set the parameter values in a given file:

All these functions are exported for convenience, but set_param_txt() is easier to use. These low-level functions may be used when a parameter name is replicated between files and the user wants to change the value of one only, or if the user need to replace the values for a particular variety in the plant file.

Set the output variables

The gen_varmod() function is used to set the required output variables in the var.mod file. For example if the user need the LAI and the dry mass:

gen_varmod(workspace = example_txt_dir, var = c("lai(n)", "masec(n)"))

Controlling if the values where written:

get_varmod(example_txt_dir)

Alternatively, the user can append a variable to the pre-existing vector of values using the append argument:

gen_varmod(workspace = example_txt_dir, var = c("hauteur"), append = TRUE)
get_varmod(example_txt_dir)

To get the possible output variables from the model, the user can use the get_var_info() function that provide a fuzzy search:

get_var_info("lai")


Try the SticsRFiles package in your browser

Any scripts or data that you put into this service are public.

SticsRFiles documentation built on Sept. 12, 2024, 7:41 a.m.