Generating STICS XML files from tabulated data

library(SticsRFiles)
library(dplyr)
suppressWarnings(library(SticsRFiles))
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = FALSE,
  workspace_path <- tempdir(),
  stics_version <- SticsRFiles::get_stics_versions_compat()$latest_version
)

Introduction

This document has been generated using the latest STICS version r stics_version.

Package functions to be used {#pkg-funs}

There is not at the moment an integrated function which is able to produce all needed parameters files. Individual functions are dedicated to each parameter file kind and may be called individually or gathered in a single script for doing all tasks at once. Use examples are given in next parts below.

Getting a table file example {#download-excel-example}

Getting inputs Excel example file from the SticsRFiles package {#get-xl}

In the following example /path/to/target/dir must be replaced with a real path depending on the operating system (i.e. starting with c:/ for windows for example)

# Getting the file in the current directory
usm_xl_file <- download_usm_xl()

# Getting the file in a specic directory
xl_dir <- "/path/to/xl/dir" # or something like C:/path/to/xl/dir" for Windows

usm_xl_file <- download_usm_xl(out_dir = xl_dir)

#> [1] "inputs_stics_example.xlsx  has been copied in directory
#> "/path/to/xl/dir
usm_xl_file <- download_usm_xl(file = "inputs_stics_example.xlsx",
                               out_dir = workspace_path)

Getting an USMs inputs CSV example file from the SticsRFiles package {#get-csv}

# Getting the file in the current directory
usm_csv_file <- download_usm_csv(file = "inputs_stics_example_USMs.csv")

# Getting the file in a specific directory
csv_dir <- "/path/to/csv/dir" # or something like C:/path/to/xl/dir" for Windows

usm_csv_file <- download_usm_csv(file = "inputs_stics_example_USMs.csv",
                                 out_dir = csv_dir)

#> [1] inputs_stics_example_USMs.csv  has been copied in directory
#> "/path/to/csv/dir
usm_csv_file <- download_usm_csv(file = "inputs_stics_example_USMs.csv",
                                 out_dir = workspace_path)

Description of tables expected content and use

As told in the previous section, parameters data may be stored either in Excel files containing several sheets or in several equivalent individual CSV files.

Sheets content and CSV files content is the same, only their use is different. But, CSV files are easily traced under version control systems and they also can be generated programmatically with the help of functions dedicated for example to generating crop management parameters like irrigation, fertilization (interventions and doses calculations).

Important information

The USMs data which are necessary for generating the usms.xml must be all provided either through an Excel sheet or a CSV file; as shown in the USMs sheet example.

Other data stored in Excel or CSV files must be adapted to the use cases here after:

Specificities of Excel sheets

Excel sheet names are completely free, but must be meaningful regarding the kind of parameters set they contain.

In the example, useful sheets for generating XML parameters files are named: USMs, Ini, Soils, Tec, Station

Common tables specifications

The first column is used for storing identifiers or files names (or prefix) and the USMs table contains identifiers of the other linked tables in columns names finit, nomsol, fstation, ftec_1, ftec_2 with respect to initialization files, soil name, station files and crop management files (for main crops and optionally associated crops).

The first column names of data tables are automatically detected and must contain keywords prefix to do so as listed: usm_, ini_, soil_, sta_, tec_

These first columns may contain either names identifiers or files names according to data type:

Other columns of tables are named with parameters names according to the following rules:

Here is the actual correspondence table of keywords to be used in parameters tables (i.e excel files for example) in replacement of real names in XML files:

l <- SticsRFiles:::get_params_dict()
df <- data.frame(keyword = names(l), realname = unlist(l, use.names = FALSE),
                 stringsAsFactors = FALSE)
rmarkdown::paged_table(df)

As an example, looking in the Tec sheet of the previously downloaded Excel file, irrigation parameters for setting days and amount of water supplied, are respectively named julapI_1, julapI_2,… and doseI_1, doseI_2,… instead of using absolute_value/% and amount.

Data consistency checks in tables

For the moment, no consistency checks (between Excel sheets or CSV equivalents files) functionalities have been included in the package in order to prevent missing generated files or misspelled file names or identifiers.

So, one must take care of links between xml files names for example files names defined for usms related to names declared for example in Excel sheets Tec, Station, and so on.

In future developments, checks will be included to run be run prior to files generations, for preventing errors.

USMs table example {#usms-table-example}

The Excel file which contains parameters values to take into account for STICS inputs may contain specific sheets for each kind of parameters groups, i.e. USMs, crop management, soil, weather station, initializations (except plant parameters and general parameters).

The following table contains data extracted from a sheet named USMs containing a list of USMS parameters or from his equivalent CSV example file.

library(readxl)
xl_param <- read_params_table(usm_xl_file, sheet = "USMs")
knitr::kable(xl_param)

Generating the usms XML file (usms.xml)

The gen_usms_xml function is working on a data.frame object that can be loaded either from an Excel file sheet or from a csv file. Here after code shows how to get data from the downloaded examples Excel and CSV files.

Getting data

# Reading the Excel file
usms_param <- read_params_table(usm_xl_file, sheet_name = "USMs")
# Or
# Reading the CSV file
usms_param <- read_params_table(usm_csv_file)
library(readxl)

# Reading the Excel file
xl_param <- read_params_table(usm_xl_file, sheet_name = "USMs")

xl_param <- read_params_table(usm_xl_file, sheet_name = "USMs")

rmarkdown::paged_table(xl_param)

Generating the file

# Output file path
out_file <- "/path/to/file/usms.xml"
# or something like C:/path/to/file/usms.xml" for Windows

# Generating a new usms.xml file, for all xl_param lines
gen_usms_xml(file = out_file, param_df = usms_param)
out_file <- file.path(workspace_path, "usms.xml")

# Generating a new usms.xml file, for all xl_param lines
gen_usms_xml(file = out_file, param_df = xl_param)

usms.xml file content

Example of the first usm parameters:

l <- readLines(con = out_file)
cat(paste(c(l[1:24], "...", l[length(l) - 1]), collapse = "\n"))

Generating the sols XML file (sols.xml)

As for gen_usms_xml, gen_sols_xml function is working on a data.frame object loaded from an Excel file sheet. The following code only shows here after the sols.xml file generation section.

Getting data

# Reading the Excel file
soils_param <- read_params_table(usm_xl_file, sheet_name = "Soils")
library(readxl)

# Reading the Excel file
soils_param <- read_params_table(usm_xl_file, sheet_name = "Soils")

rmarkdown::paged_table(soils_param)

Generating the file

# Output file path
out_file <- "/path/to/file/sols.xml"
# or something like C:/path/to/file/usms.xml" for Windows

# Generating a new sols.xml file, for all xl_param lines
gen_sols_xml(file = out_file, param_df = soils_param)
out_file <- file.path(workspace_path, "sols.xml")

# Generating a new sols.xml file, for all xl_param lines
gen_sols_xml(file = out_file, param_df = soils_param)

sols.xml file content

Example of the first sol parameters sub-list:

l <- readLines(con = out_file)
cat(paste(c(l[1:30], "...", l[110], "...", l[length(l) - 1]), collapse = "\n"))

Generating multiple crop management XML files (*_tec.xml)

Getting data

# Reading the Excel file
tec_param <- read_params_table(usm_xl_file, sheet_name = "Tec")
library(readxl)

# Reading the Excel file
tec_param <- read_params_table(usm_xl_file, sheet_name = "Tec")

rmarkdown::paged_table(tec_param)

Generating files

# *_tec.xml files, one for each xl_param line
gen_tec_xml(param_df = tec_param, out_dir = workspace_path)
# *_tec.xml files, one for each xl_param line
gen_tec_xml(param_df = tec_param, out_dir = workspace_path)

Example of a tec parameters file content sub-list:

l <- readLines(con = file.path(workspace_path,
                              "BIN_CAN_05_SEC_220-0-0_34K_CANPC05T3_Q_tec.xml"))
cat(paste(c(l[1:30], "...", l[length(l) - 1]), collapse = "\n"))

Generating multiple initialization XML files (*_ini.xml)

Getting data

# Reading the Excel file
ini_param <- read_params_table(usm_xl_file, sheet_name = "Ini")
library(readxl)

# Reading the Excel file
ini_param <- read_params_table(usm_xl_file, sheet_name = "Ini")

rmarkdown::paged_table(ini_param)

Generating files

# *_ini.xml files, one for each xl_param line
gen_ini_xml(param_df = ini_param, out_dir = workspace_path)
# *_ini.xml files, one for each xl_param line
gen_ini_xml(param_df = ini_param, out_dir = workspace_path)

XML file content

Example of a ini parameters file content sub-list:

l <- readLines(con = file.path(workspace_path, "USM_2017_T1_ini.xml"))
cat(paste(c(l[1:19], "...", l[length(l) - 1]), collapse = "\n"))

Generating multiple meteorological station XML files (*_sta.xml)

Getting data

# Reading the Excel file
sta_param <- read_params_table(usm_xl_file, sheet_name = "Station")
library(readxl)

# Reading the Excel file
sta_param <- read_params_table(usm_xl_file, sheet_name = "Station")

rmarkdown::paged_table(sta_param)

Generating files

# *_sta.xml files, one for each xl_param line
gen_sta_xml(param_df = sta_param, out_dir = workspace_path)
# *_sta.xml files, one for each xl_param line
gen_sta_xml(param_df = sta_param, out_dir = workspace_path)

XML file content

Example of a sta parameters file content sub-list:

l <- readLines(con = file.path(workspace_path, "climatex_sta.xml"))
cat(paste(c(l[1:41], "...", l[length(l) - 1]), collapse = "\n"))


Try the SticsRFiles package in your browser

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

SticsRFiles documentation built on May 29, 2024, 4:18 a.m.