Skip to content

Commit

Permalink
Merge branch 'main' into add/xml-upgrades/v11
Browse files Browse the repository at this point in the history
  • Loading branch information
plecharpent committed Jul 19, 2024
2 parents a377a6d + dcab493 commit b474082
Show file tree
Hide file tree
Showing 24 changed files with 503 additions and 111 deletions.
6 changes: 3 additions & 3 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ message: 'To cite package "SticsRFiles" in publications use:'
type: software
license: LGPL-3.0-or-later
title: 'SticsRFiles: Read and Modify ''STICS'' Input/Output Files'
version: 1.3.0
version: 1.4.0
abstract: Manipulating input and output files of the 'STICS' crop model. Files are
either 'JavaSTICS' XML files or text files used by the model 'fortran' executable.
Most basic functionalities are reading or writing parameter names and values in
Expand All @@ -35,7 +35,7 @@ authors:
repository: https://CRAN.R-project.org/package=SticsRFiles
repository-code: https://github.com/SticsRPacks/SticsRFiles
url: https://doi.org/10.5281/zenodo.4443206
date-released: '2024-04-10'
date-released: '2024-07-16'
contact:
- family-names: Lecharpentier
given-names: Patrice
Expand Down Expand Up @@ -68,7 +68,7 @@ references:
title: crayon
abstract: 'crayon: Colored Terminal Output'
notes: Imports
url: https://github.com/r-lib/crayon#readme
url: https://r-lib.github.io/crayon/
repository: https://CRAN.R-project.org/package=crayon
authors:
- family-names: Csárdi
Expand Down
7 changes: 5 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: SticsRFiles
Title: Read and Modify 'STICS' Input/Output Files
Version: 1.3.0
Date: 2024-04-10
Version: 1.4.0
Date: 2024-07-16
Authors@R: c(
person("Patrice", "Lecharpentier", , "patrice.lecharpentier@inrae.fr", role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-4044-4322")),
Expand Down Expand Up @@ -83,6 +83,7 @@ Collate:
'check_output_files.R'
'check_param_names.R'
'col_names_to_var.R'
'compute_date_from_day.R'
'compute_day_number.R'
'convert_xml2txt.R'
'convert_xml2txt_int.R'
Expand Down Expand Up @@ -114,6 +115,8 @@ Collate:
'gen_usms_xml2txt.R'
'gen_varmod.R'
'get_climate_txt.R'
'get_cultivars_list.R'
'get_cultivars_param.R'
'get_file.R'
'get_file_int.R'
'get_formalisms_xml.R'
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

S3method("[",cropr_simulation)
export(compute_date_from_day)
export(compute_day_from_date)
export(convert_xml2txt)
export(download_data)
Expand All @@ -16,6 +17,8 @@ export(gen_usms_xml)
export(gen_usms_xml2txt)
export(gen_varmod)
export(get_climate_txt)
export(get_cultivars_list)
export(get_cultivars_param)
export(get_examples_path)
export(get_general_txt)
export(get_ini_txt)
Expand Down
18 changes: 18 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# SticsRFiles 1.4.0 _2024-07-16_

* FUNCTIONS
* New functions for computing dates from days numbers with reference to
starting year
* compute_day_from_date now uses a start year instead of a date, and date may
of type character
* get_param_txt, set_param_txt, get_param_xml, set_param_xml now use values
ids for getting or setting specific parameter vector values
specific values
* New functions for getting cultivars list and extracting cultivars parameters
as a data.frame

* FIXES
* management of STICS version consistency with text files content using
get_param_txt or set_param_txt


# SticsRFiles 1.3.0 _2024-04-10_

* FUNCTIONS
Expand Down
25 changes: 25 additions & 0 deletions R/compute_date_from_day.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#' Convert day number into date
#'
#' Computes the date corresponding to a given day number (or vector of)
#' with reference to a start year. Typically, the start year should
#' be the year of a STICS simulation start, but not necessarily.
#' @param day day number(s) to be converted
#' @param start_year year to be used as time reference (simulation start year).
#'
#' @return Date vector
#' @author Timothee Flutre
#' @examples
#'
#' compute_date_from_day(day = 290, start_year = 1994)
#'
#' compute_date_from_day(day = 700, start_year = 1994)
#'
#' @export
#'
compute_date_from_day <- function(day, start_year){
stopifnot(all(is.numeric(day)),
length(start_year) == 1,
is.numeric(start_year))

return(as.Date(day - 1, origin = paste0(start_year, "-01-01")))
}
38 changes: 38 additions & 0 deletions R/get_cultivars_list.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#' @title Get the cultivar names for an xml plant file (*_plt.xml)
#'
#' @description Extracts the cultivar names from a plant file
#'
#' @param file The path of a plant file.
#'
#' @return A vector of cultivar names
#'
#' @examples
#' path <- get_examples_path(file_type = "xml")
#'
#' # Read from a plant file (all cultivars available in a plant file)
#' cv_list <- get_cultivars_list(file = file.path(path, "file_plt.xml"))
#'
#' @export
#'
get_cultivars_list <- function(file) {

xml_doc <- xmldocument(file)

stopifnot(is_stics_plt(xml_doc))

xml_name <- "variete"

cv_list <- unique(
unlist(
lapply(
XML::getNodeSet(doc = xml_doc@content,
path = paste0("//", xml_name)),
function(x) {
XML::xmlGetAttr(x, "nom")
}
)
)
)

return(cv_list)
}
28 changes: 28 additions & 0 deletions R/get_cultivars_param.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#' @title Get the values of cultivar-specific parameters from an xml
#' plant file (*_plt.xml)
#'
#' @description Extracts the values of cultivar-specific parameters from a
#' plant file
#'
#' @param file The path of a plant file.
#'
#' @return A data.frame with one row per cultivar and one column per parameter
#'
#' @examples
#' path <- get_examples_path(file_type = "xml")
#'
#' # Read from a plant file (all cultivars available in a plant file)
#' cv_param_df <- get_cultivars_param(file = file.path(path, "file_plt.xml"))
#'
#' @export
#'
get_cultivars_param <- function(file) {

cv_list <- get_cultivars_list(file)

out <- get_param_xml(file, select = "variete", select_value = cv_list)
out <- as.data.frame(do.call(cbind, out[[1]]))
rownames(out) <- cv_list

return(out)
}
4 changes: 2 additions & 2 deletions R/get_param_txt.R
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ get_ini_txt <- function(file = "ficini.txt",

if (stics_version_num < 10) {

if (length(params) > 28)
if (length(params) > 48)
stop("The used STICS version ",
stics_version_num,
" does not correspond to the file content (STICS version >= 10)")
Expand Down Expand Up @@ -399,7 +399,7 @@ get_ini_txt <- function(file = "ficini.txt",
ini$NH4init <- params[[28]]
} else {

if (length(params) < 49)
if (length(params) < 48)
stop("The used STICS version ",
stics_version_num,
" does not correspond to the file content (STICS version < 10)")
Expand Down
4 changes: 4 additions & 0 deletions R/get_param_xml.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#' (optional, default to no selection)
#' @param select_value Vector of values used for select (see examples).
#' Optional, should be provided only if select is provided.
#' @param value_id Vector of ids of the parameters values to be retrieved
#' from the parameter values vector
#' @param value `r lifecycle::badge("deprecated")` `value` is no
#' longer supported, use `select_value` instead.
#' @param ... Pass further arguments to `get_param_value()`
Expand Down Expand Up @@ -48,6 +50,7 @@ get_param_xml <- function(file,
param = NULL,
select = NULL,
select_value = NULL,
value_id = NULL,
xml_file = lifecycle::deprecated(),
param_name = lifecycle::deprecated(),
value = lifecycle::deprecated(),
Expand Down Expand Up @@ -97,6 +100,7 @@ get_param_xml <- function(file,
param_name = param_name,
parent_name = select,
parent_sel_attr = value,
ids = value_id,
...
)
xml_names <- lapply(xml_file, basename) %>% unlist()
Expand Down
25 changes: 23 additions & 2 deletions R/set_param_xml.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#' (optional, default to no selection)
#' @param select_value Vector of values used for select (see examples).
#' Optional, should be provided only if select is provided.
#' @param value_id Vector of ids of the parameters values to be retrieved
#' from the parameter values vector
#' @param overwrite Logical TRUE for overwriting the output file,
#' FALSE otherwise (default)
#' @param xml_file `r lifecycle::badge("deprecated")` `xml_file` is no
Expand Down Expand Up @@ -71,22 +73,39 @@
#' set_param_xml(sol_path, c("epc", "HCCF"),
#' select = "sol",
#' select_value = c("solcanne", "solbanane"),
#' param_value = list(c(20:24, 10:14), c(50:54, 40:44)), overwrite = TRUE
#' values = list(c(20:24, 10:14), c(50:54, 40:44)),
#' overwrite = TRUE
#' )
#'
#' # Getting changed values
#' # get_param_xml(sol_path, c("epc", "HCCF"),
#' # select = "sol",
#' # select_value = c("solcanne", "solbanane")
#' # )
#'
#' # For specific values of vector parameters
#' set_param_xml(sol_path, "HCCF",
#' select = "sol",
#' select_value = "solcanne",
#' values = c(46.8, 48.5, 50.1),
#' value_id = c(1,3,5),
#' overwrite = TRUE
#' )
#'
#' # Getting changed values
#' # get_param_xml(sol_path, "HCCF",
#' # select = "sol",
#' # select_value = "solcanne",
#' # value_id = c(1,3,5)
#' # )
#'
#' # Crop management file
#'
#' tec_path <- file.path(ex_path, "file_tec.xml")
#'
#' # Modifying irrigations parameters
#' set_param_xml(tec_path, c("julapI_or_sum_upvt", "amount"),
#' param_value = list(200:215, 20:35), overwrite = TRUE
#' values = list(200:215, 20:35), overwrite = TRUE
#' )
#' # Getting changed values
#' # get_param_xml(tec_path, c("julapI_or_sum_upvt", "amount"))
Expand All @@ -99,6 +118,7 @@ set_param_xml <- function(file,
save_as = NULL,
select = NULL,
select_value = NULL,
value_id = NULL,
overwrite = FALSE,
xml_file = lifecycle::deprecated(),
out_path = lifecycle::deprecated(),
Expand Down Expand Up @@ -188,6 +208,7 @@ set_param_xml <- function(file,
param_value = param_value,
parent_name = select,
parent_sel_attr = value,
ids = value_id,
...
)

Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ knitr::opts_chunk$set(

<!-- badges: end -->

The goal of SticsRFiles is to perform manipulations of the [STICS](https://eng-stics.paca.hub.inrae.fr/) model files either on XML files (used by the JavaSTICS GUI) or on text files used by the model fortran executable.
The goal of SticsRFiles is to perform manipulations of the [STICS](https://stics.inrae.fr/eng/) model files either on XML files (used by the JavaSTICS GUI) or on text files used by the model fortran executable.

The basic functionalities allows to read parameters names and values through XML queries and replace parameters values in files. A starting guide is available on the [Get started page](https://sticsrpacks.github.io/SticsRFiles/articles/SticsRFiles.html).

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ coverage](https://codecov.io/gh/SticsRPacks/SticsRFiles/branch/master/graph/badg
<!-- badges: end -->

The goal of SticsRFiles is to perform manipulations of the
[STICS](https://eng-stics.paca.hub.inrae.fr/) model files either on XML
files (used by the JavaSTICS GUI) or on text files used by the model
fortran executable.
[STICS](https://stics.inrae.fr/eng/) model files either on XML files
(used by the JavaSTICS GUI) or on text files used by the model fortran
executable.

The basic functionalities allows to read parameters names and values
through XML queries and replace parameters values in files. A starting
Expand Down
3 changes: 3 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ reference:
- get_usms_files
- get_usms_list
- get_soils_list
- get_cultivars_list
- get_cultivars_param

- title: "Generate Stics Input files (Text format)"
desc: Input parameters files, outputs definition files or observations files
Expand Down Expand Up @@ -117,3 +119,4 @@ reference:
- is_windows
- "`[.cropr_simulation`"
- compute_day_from_date
- compute_date_from_day
Loading

0 comments on commit b474082

Please sign in to comment.