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 23, 2024
2 parents 4fa644d + 4df2cee commit 389a53f
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 1 deletion.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ Collate:
'find_names.R'
'force_param_values.R'
'gen_climate.R'
'gen_general_param_xml.R'
'gen_ini_doc.R'
'gen_ini_xml.R'
'gen_new_travail.R'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export(download_data)
export(download_usm_csv)
export(download_usm_xl)
export(force_param_values)
export(gen_general_param_xml)
export(gen_ini_xml)
export(gen_obs)
export(gen_sols_xml)
Expand Down
56 changes: 56 additions & 0 deletions R/gen_general_param_xml.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#' @title Generate STICS general parameters xml file(s) from a template
#' file according to a STICS version
#'
# @param param_df A table (df, tibble) containing the values of the parameters
# to use (see details)
# @param file Path of a sta xml file to be used as a template. Optional,
# if not provided, the function will use a standard template depending
# on the STICS version.
#' @param out_dir Path of the directory where to generate the file(s).
#' @param stics_version Name of the STICS version. Optional, the latest one
#' is used as default
#' @param overwrite Optional logical, TRUE for overwriting files,
#' FALSE otherwise (default)
#'
#' @details Please see `get_stics_versions_compat()` for the full list of
#' STICS versions that can be used for the argument `stics_version`.
#'
#' @return None
#'
#' @examples
#' gen_general_param_xml(out_dir = tempdir())
#'
#' gen_general_param_xml(out_dir = tempdir(),
#' stics_version = "V10.0",
#' overwrite = TRUE)
#'
#' @export
#'
gen_general_param_xml <- function(out_dir,
stics_version = "latest",
overwrite = FALSE) {


# check/get version
stics_version <- get_xml_stics_version(
stics_version = stics_version
)

# getting dir path of templates
files_dir <- get_examples_path(file_type = "xml",
stics_version = stics_version)

# Copying files to out_dir
files_name <- c("param_gen.xml", "param_newform.xml")
xml_files <- file.path(files_dir, files_name)

copy_status <- file.copy(xml_files, out_dir, overwrite = overwrite)

if (!all(copy_status))
stop("Some error occured while copying files: \n",
paste(files_name[!copy_status], collapse = ", \n"),
"\nConsider setting overwrite to TRUE as function additional input")



}
3 changes: 2 additions & 1 deletion R/get_usms_files.R
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,9 @@ get_usms_files <- function(workspace,

# Keeping usms xml files, except plant files, obs, lai, null
useless_files_idx <- grep("\\.obs|\\.lai|null", usm_files)

if (length(useless_files_idx) > 0) usm_files <- usm_files[-useless_files_idx]
usm_files <- unique(usm_files)

usm_files_path <- file.path(workspace_path, usm_files)


Expand Down
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ reference:
- gen_sta_xml
- gen_tec_xml
- gen_usms_xml
- gen_general_param_xml

- title: "Upgrade JavaStics XML files from version 9.2 or (9.1) to 10.0"
desc: Functions for upgrading parameter files
Expand Down
37 changes: 37 additions & 0 deletions man/gen_general_param_xml.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions tests/testthat/test-gen_general_param_xml.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

context("Generating xml general parameters files")

gen_general_param_xml(out_dir = tempdir())

files <- c("param_gen.xml", "param_newform.xml")

test_that("latest version", {
expect_true(all(file.exists(file.path(tempdir(), files))))
expect_error(gen_general_param_xml(out_dir = tempdir()))
expect_no_error(gen_general_param_xml(out_dir = tempdir(), overwrite = TRUE))
})



test_that("other version", {
expect_no_error(gen_general_param_xml(out_dir = tempdir(),
stics_version = "V10.0",
overwrite = TRUE))
expect_true(all(file.exists(file.path(tempdir(), files))))
})
16 changes: 16 additions & 0 deletions vignettes/Generating_Stics_XML_files.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ There is not at the moment an integrated function which is able to produce all n
* gen_ini_xml: for generating simulations initialization files (`*_ini.xml`)
* gen_tec_xml: for generating crop management files (`*_tec.xml`)
* gen_sta_xml: for generating weather stations files (`*_sta.xml`)
* gen_general_param_xml: for generating general parameters files (`param_gen.xml and param_newform.xml`)



Expand Down Expand Up @@ -450,3 +451,18 @@ 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"))
```

# Generating general parameters XML files (param_gen.xml and param_newform.xml)

Nothing is to be done for specifying parameters values, the function is to be called with an optional argument for setting the desired version as follows.

<!-- Only for the show -->
```{r gen_gen_param, eval = FALSE}
# with the latest version
gen_general_param_xml(out_dir = workspace_path)
# or with a specific version
gen_general_param_xml(out_dir = workspace_path, stics_version = "V10.0")
```

0 comments on commit 389a53f

Please sign in to comment.