Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 68 additions & 30 deletions R/get_param_txt.R
Original file line number Diff line number Diff line change
Expand Up @@ -403,16 +403,29 @@ get_ini_txt <- function(
densinitial = params[[12]]
)

ini$plant$plant2 <- list(
stade0 = params[[14]],
lai0 = params[[15]],
masec0 = params[[16]],
QNplante0 = params[[17]],
magrain0 = params[[18]],
zrac0 = params[[19]],
resperenne0 = params[[20]],
densinitial = params[[22]]
)
if (ini$nbplantes > 1) {
ini$plant$plant2 <- list(
stade0 = params[[14]],
lai0 = params[[15]],
masec0 = params[[16]],
QNplante0 = params[[17]],
magrain0 = params[[18]],
zrac0 = params[[19]],
resperenne0 = params[[20]],
densinitial = params[[22]]
)
} else {
ini$plant$plant2 <- list(
stade0 = "",
lai0 = "0",
masec0 = "0",
QNplante0 = "0",
magrain0 = "0",
zrac0 = "0",
resperenne0 = "0",
densinitial = "0"
)
}

ini$hinit <- params[[24]]
ini$NO3init <- params[[26]]
Expand Down Expand Up @@ -442,34 +455,59 @@ get_ini_txt <- function(
densinitial = params[[18]]
)

ini$plant$plant2 <- list(
stade0 = params[[20]],
lai0 = params[[21]],
magrain0 = params[[22]],
zrac0 = params[[23]],
code_acti_reserve = params[[25]],
maperenne0 = params[[26]],
QNperenne0 = params[[27]],
masecnp0 = params[[28]],
QNplantenp0 = params[[29]],
masec0 = params[[30]],
QNplante0 = params[[31]],
restemp0 = params[[32]],
densinitial = params[[34]]
)
if (ini$nbplantes > 1) {
ini$plant$plant2 <- list(
stade0 = params[[20]],
lai0 = params[[21]],
magrain0 = params[[22]],
zrac0 = params[[23]],
code_acti_reserve = params[[25]],
maperenne0 = params[[26]],
QNperenne0 = params[[27]],
masecnp0 = params[[28]],
QNplantenp0 = params[[29]],
masec0 = params[[30]],
QNplante0 = params[[31]],
restemp0 = params[[32]],
densinitial = params[[34]]
)
} else {
ini$plant$plant2 <- list(
stade0 = "",
lai0 = "0",
magrain0 = "0",
zrac0 = "0",
code_acti_reserve = "0",
maperenne0 = "0",
QNperenne0 = "0",
masecnp0 = "0",
QNplantenp0 = "0",
masec0 = "0",
QNplante0 = "0",
restemp0 = "0",
densinitial = "0"
)
}

ini$Hinitf <- params[[36]]
ini$NO3initf <- params[[38]]
ini$NH4initf <- params[[40]]
ini$Sdepth0 <- params[[43]]
ini$Sdry0 <- params[[45]]
ini$Swet0 <- params[[47]]
ini$ps0 <- params[[49]]
if (any(grepl(":snow:", params, fixed = TRUE))) {
ini$Sdepth0 <- params[[43]]
ini$Sdry0 <- params[[45]]
ini$Swet0 <- params[[47]]
ini$ps0 <- params[[49]]
} else {
ini$Sdepth0 <- "0"
ini$Sdry0 <- "0"
ini$Swet0 <- "0"
ini$ps0 <- "0"
}
}

ini <- character_to_numeric_list(ini)

return(ini)
ini
}

#' @rdname get_param_txt
Expand Down
13 changes: 8 additions & 5 deletions R/set_param_txt.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ set_param_txt <- function(
param,
value,
append = FALSE,
plant_id = 1,
plant_id = NULL,
variety = NULL,
value_id = NULL,
stics_version = "latest"
Expand Down Expand Up @@ -107,6 +107,7 @@ set_param_txt <- function(
"\nPlease use the set_* functions directly to set the parameter value."
)
}

switch(file_type,
ini = {
set_ini_txt(
Expand Down Expand Up @@ -160,6 +161,7 @@ set_param_txt <- function(
)
},
tec = {
if (is.null(plant_id)) plant_id <- 1
lapply(plant_id, function(x) {
set_tec_txt(
file = file.path(workspace, paste0("fictec", x, ".txt")),
Expand All @@ -171,6 +173,7 @@ set_param_txt <- function(
})
},
plant = {
if (is.null(plant_id)) plant_id <- 1
lapply(plant_id, function(x) {
if (is.null(variety)) {
variety <-
Expand Down Expand Up @@ -251,7 +254,7 @@ set_ini_txt <- function(
param,
value,
append = FALSE,
plant_id = 1,
plant_id = NULL,
value_id = NULL,
stics_version = "latest"
) {
Expand Down Expand Up @@ -494,8 +497,8 @@ set_soil_txt <- function(
#' @param file Path to the parameter file
#' @param param Parameter name
#' @param value New parameter value
#' @param append Boolean. Append input to existing file
#' @param plant_id The plant identifier (main crop: 1 ; associated crop: 2).
#' @param append Boolean. Append input to existing file
#' @param plant_id The plant identifier (main crop: 1 ; associated crop: 2).
#' @param variety The plant variety to set the parameter value,
#' either the variety
#' name (`codevar` in the plant file) or the index
Expand Down Expand Up @@ -581,7 +584,7 @@ set_file_txt <- function(
value_id = value_id,
value = value
)
ref[[param]][[value_id]] <- value
ref[[param]][value_id] <- value
}
} else {
plt_tag <- paste0("plant", plant_id)
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ The package will install the packages for you at the latest release version.
```{r eval=FALSE, results='asis'}
install.packages("SticsRFiles")

or
or

pak::pkg_install("SticsRFiles")
```
Expand Down
4 changes: 2 additions & 2 deletions man/set_param_txt.Rd

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

46 changes: 46 additions & 0 deletions tests/testthat/test-set_get_param_txt.R
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,49 @@ test_that("get for NO3init, for a wrong version", {
)
)
})

path <- get_examples_path("txt", stics_version = "V9.2")
# Getting values for an unknown parameter
test_that("get for Hinitf, for a wrong version", {
expect_equal(
get_param_txt(workspace = path, param = "Hinitf", stics_version = "v9.2"),
list()
)
})

test_that("set/get for hinit", {
# Setting parameters
# all values
set_param_txt(
workspace = path,
param = "hinit",
value = 1:5,
stics_version = "v9.2"
)
expect_equal(
get_param_txt(
workspace = path,
param = "hinit",
stics_version = "v9.2"
)$ini$hinit,
1:5
)

# for a subset
set_param_txt(
workspace = path,
param = "hinit",
value = c(10, 15),
value_id = c(1, 5),
stics_version = "v9.2"
)
expect_equal(
get_param_txt(
workspace = path,
param = "hinit",
value_id = c(1, 5),
stics_version = "v9.2"
)$ini$hinit,
c(10, 15)
)
})
Loading