library(SticsRFiles)
The goal of SticsRFiles is to perform manipulations of all types of files related to the input and outputs of the STICS model.
This article presents the design of the package and its basic features, i.e. the main functions to deal with the XML files. For a complete introduction to SticsRPacks (managing files, running simulations, plotting, optimization…), see the tutorial from the SticsRPacks package.
The executable of the STICS model reads text files with standard names and format to import the inputs describing a single unit of simulation (USM), e.g. one crop for one year.
Here’s a typical list of the input files for the STICS executable (without the optional files):
workspace
│
├── 📜climat.txt # The meteorological data for the USM
├── 📜ficini.txt # The initialization
├── 📜ficplt1.txt # Parameters for the plant to be simulated
├── 📜fictec1.txt # The management applied to the soil and crop
├── 📜new_travail.usm # The general configuration parameters for the USM
├── 📜param.sol # Soil parameters
├── 📜station.txt # The site parameters (*e.g.* altitude, latitude)
├── 📜tempopar.sti # More general parameters not in the other files
├── 📜tempoparv6.sti # Parameters for the custom versions of STICS
└── 📜var.mod # Variables to write in the outputs
Because these files only describe one USM at a time and can be tedious to explore and parameterize, we usually don’t interact with them directly, but through another program: JavaSTICS.
JavaSTICS is a graphical user interface used to easily create input text files for the STICS executable according to the user’s choices, and for managing STICS simulations. JavaSTICS saves the parameter values and options choices in XML files.
The XML files store more information than the text files: not only do they store the parameter values but also their description, maximum and minimum boundary values, all existing formalisms, the different choices allowed, and more importantly they allow for the management of several USMs in the same folder (called workspace), and can help make successive USMs.
It is important to note that only JavaSTICS interact with the XML files, not the STICS executable. The STICS input text files are then automatically created by JavaSTICS when running a simulation, just before calling the STICS executable.
SticsRFiles is an R package that uses JavaSTICS from the command line to manage the XML and the text files. We can generate XML or text files, get and set parameter values, import simulations outputs, and manage observation files.
Advanced features also include:
Here’s a simple example usage of SticsRFiles using an example workspace.
All the example data used in this article are available from the data
repository in the SticsRPacks
organization.
SticsRFiles provides a function to download it from the command line. Please execute the following command in R:
library(SticsRFiles)
<- SticsRFiles::download_data(example_dirs = "study_case_1",
example_data "V10.0")
The example data is downloaded by default in a temporary folder.
For the sake of readability, we’ll declare the workspace path and the path to the plant file here. But remember the functions can be applied to any XML files or workspaces.
<- file.path(example_data, "XmlFiles")
workspace <- file.path(workspace, "plant", "maisopti_plt.xml") plant_file
get_var_info()
helps to get any STICS variable name by
doing a fuzzy search. For example to get all variables with
lai
in their names, you would do:
::get_var_info("lai")
SticsRFiles#> name
#> 4 albedolai
#> 241 exolai
#> 337 innlai
#> 351 lai(n)
#> 352 lai_mx_av_cut
#> 353 laimax
#> 354 laisen(n)
#> 743 splai
#> 806 ulai(n)
#> definition
#> 4 albedo of the crop including soil and vegetation
#> 241 reduction factor on leaf growth due to water excess
#> 337 reduction factor on leaf growth due to NNI (nitrogen deficiency)
#> 351 leaf area index (table)
#> 352 LAI before cut (for cut crops , for others = lai(n) )
#> 353 maximum leaf area index
#> 354 leaf area index of senescent leaves (table)
#> 743 source to sink ratio of assimilates in the leaves
#> 806 relative development unit for LAI
#> unit type
#> 4 SD real
#> 241 0-1 real
#> 337 innmin to 1 real
#> 351 m2.m-2 real
#> 352 SD real
#> 353 m2.m-2 real
#> 354 m2.m-2 real
#> 743 SD real
#> 806 0-3 real
Sometimes it is also useful to search in the variable definition
instead of its name. To do so, you can use the keyword
argument like so:
::get_var_info(keyword = "lai")
SticsRFiles#> name
#> 4 albedolai
#> 170 diftemp1intercoupe
#> 171 diftemp2intercoupe
#> 241 exolai
#> 333 inn1intercoupe
#> 335 inn2intercoupe
#> 337 innlai
#> 351 lai(n)
#> 352 lai_mx_av_cut
#> 353 laimax
#> 354 laisen(n)
#> 358 leai
#> 743 splai
#> 746 str1intercoupe
#> 747 str2intercoupe
#> 748 stu1intercoupe
#> 749 stu2intercoupe
#> 802 tustress
#> 806 ulai(n)
#> definition
#> 4 albedo of the crop including soil and vegetation
#> 170 mean difference between crop and air temperatures during the vegetative phase (emergence - maximum LAI)
#> 171 mean difference between crop and air temperatures during the reproductive phase (maximum LAI - maturity)
#> 241 reduction factor on leaf growth due to water excess
#> 333 average NNI during the cut (cut crop vegetative phase: emergence to maximum LAI)
#> 335 average NNI during the cut (cut crop reproductive phase: maximum LAI to maturity)
#> 337 reduction factor on leaf growth due to NNI (nitrogen deficiency)
#> 351 leaf area index (table)
#> 352 LAI before cut (for cut crops , for others = lai(n) )
#> 353 maximum leaf area index
#> 354 leaf area index of senescent leaves (table)
#> 358 Leaf+ear area index = lai +eai
#> 743 source to sink ratio of assimilates in the leaves
#> 746 average stomatal water stress index during the vegetative phase (emergence - maximum LAI) of forage crops
#> 747 average stomatal water stress index during the reproductive phase (maximum LAI - maturity) of forage crops
#> 748 average turgescence water stress index during the vegetative phase (emergence - maximum LAI) of forage crops
#> 749 average turgescence water stress index during the reproductive phase (maximum LAI - maturity) of forage crops
#> 802 reduction factor on leaf growth due to the effective water stress (= min(turfac,innlai))
#> 806 relative development unit for LAI
#> unit type
#> 4 SD real
#> 170 degreeC real
#> 171 degreeC real
#> 241 0-1 real
#> 333 0-2 real
#> 335 0-2 real
#> 337 innmin to 1 real
#> 351 m2.m-2 real
#> 352 SD real
#> 353 m2.m-2 real
#> 354 m2.m-2 real
#> 358 m2.m-2 real
#> 743 SD real
#> 746 0-1 real
#> 747 0-1 real
#> 748 0-1 real
#> 749 0-1 real
#> 802 0-1 real
#> 806 0-3 real
get_param_info()
is used to get parameter names from XML
files:
get_param_info(param = "lai")
#> # A tibble: 21 × 5
#> name file min max formalism
#> <chr> <chr> <dbl> <dbl> <chr>
#> 1 lai0 file_ini.xml NA NA none
#> 2 codelaitr file_plt.xml 1 2 leaves
#> 3 codlainet file_plt.xml 1 2 cultivar parameters
#> 4 dlaimax file_plt.xml 0.000005 0.5 cultivar parameters
#> 5 dlaimaxbrut file_plt.xml 0.000005 0.5 cultivar parameters
#> # … with 16 more rows
We can also read the parameters inside a particular XML file. This helps users of custom or old versions of JavaSTICS still get the parameters:
get_param_info(file = plant_file)
#> # A tibble: 314 × 5
#> name file min max formalism
#> <chr> <chr> <dbl> <dbl> <chr>
#> 1 Efremobil maisopti_plt.xml 0 1 partitioning of biomass in organs
#> 2 INNimin maisopti_plt.xml 0 1 nitrogen
#> 3 INNmin maisopti_plt.xml 0 1 nitrogen
#> 4 Kmabs1 maisopti_plt.xml 20 200 nitrogen
#> 5 Kmabs2 maisopti_plt.xml 4000 40000 nitrogen
#> # … with 309 more rows
get_param_xml()
is used to get the values of a parameter
in an XML file. For example if we want to get dlaimax
, we
would do:
<- get_param_xml(plant_file, "dlaimax")
dlaimax
dlaimax#> $maisopti_plt.xml
#> $maisopti_plt.xml$dlaimax
#> [1] 0.00321 0.00321 0.00321 0.00321 0.00321
But this function is way more powerful than just that. You can also
get the values for all parameters in a given formalism
(formalisme
in French, yes some variables are still written
in French in STICS). To do so, use the select
argument like
so:
<- get_param_xml(plant_file, select = "formalisme",
values select_value = "radiation interception")
unlist(values) # For pretty-printing
#> maisopti_plt.xml.codetransrad maisopti_plt.xml.forme
#> 1 1
#> maisopti_plt.xml.rapforme maisopti_plt.xml.adfol
#> 4 1
#> maisopti_plt.xml.dfolbas maisopti_plt.xml.dfolhaut
#> 5 5
We can also change the value of a parameter programmatically using
set_param_xml()
. It is used similarly to
get_param_xml()
. For example if we want to increase
dlaimax
by 30%:
set_param_xml(plant_file, "dlaimax", unlist(dlaimax) * 1.3, overwrite = TRUE)
Don’t forget to use the overwrite
argument and set it to
TRUE
. It is FALSE
by default to avoid any
mishandling.
New values written in the file can be checked:
<- get_param_xml(plant_file, "dlaimax")
dlaimax
dlaimax#> $maisopti_plt.xml
#> $maisopti_plt.xml$dlaimax
#> [1] 0.004173 0.004173 0.004173 0.004173 0.004173
We can generate observation files from a data.frame
using gen_obs()
.
Lets create some dummy data.frame
first:
<- data.frame(usm_name = "Test", ian = 2021, mo = 3:10, jo = 1,
obs_df `masec(n)` = 0.1 * 3:10)
Then we can write the data to a file using
gen_obs()
:
gen_obs(df = obs_df, out_dir = "/path/to/dest/dir")
We can read the observation files in a workspace using
get_obs()
. Note that all observation files should be named
after the USM they are linked to. See the help page for more details,
e.g. about intercrops.
<- get_obs(workspace)
obs #> bo96iN+.obs
#> bou00t1.obs
#> bou00t3.obs
#> bou99t1.obs
#> bou99t3.obs
#> lu96iN+.obs
#> lu96iN6.obs
#> lu97iN+.obs
Likewise, we can read the observation files in a workspace using
get_sim()
:
<- get_sim(workspace)
sim #> Warning in get_file_(workspace = x, usm_name = usm_name, usms_filepath =
#> usms_path, : Not any sim file detected in
#> workspace/tmp/RtmpjkDYAq/data-master/
#> study_case_1/V10.0/XmlFiles
But as there aren’t any simulations yet in the workspace, the function will return an error. To make a simulation, head to SticsOnR. Then to plot both observations and simulations, you can use CroPlotR.