aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--R/PEC_sw_focus.R59
-rw-r--r--man/PEC_sw_focus.Rd12
-rw-r--r--tests/testthat/test_step_1.R15
3 files changed, 80 insertions, 6 deletions
diff --git a/R/PEC_sw_focus.R b/R/PEC_sw_focus.R
index c098fb2..e6f2689 100644
--- a/R/PEC_sw_focus.R
+++ b/R/PEC_sw_focus.R
@@ -27,6 +27,7 @@
#' applications are given explicitly
#' @param n The number of applications
#' @param i The application interval
+#' @param comment A comment for the input file
#' @param met A list containing metabolite specific parameters. e.g.
#' conveniently generated by \code{\link{chent_focus_sw}}. If not NULL,
#' the PEC is calculated for this compound, not the parent.
@@ -39,6 +40,12 @@
#' parent or a metabolite
#' @param scenario The name of the scenario. Must be one of the scenario
#' names given in \code{\link{FOCUS_Step_12_scenarios}}
+#' @param txt_file the name, and potentially the full path to the
+#' Steps.12 input text file to which the specification of the run(s)
+#' should be written
+#' @param overwrite Should an existing file a the location specified in
+#' \code{txt_file} be overwritten?
+#' @param append Should the input text file be appended?
#' @examples
#' # Parent only
#' dummy_1 <- chent_focus_sw(cwsat = 6000, DT50_ws = 6, Koc = 344.8)
@@ -49,9 +56,11 @@
#' M1 <- chent_focus_sw(mw = 100, cwsat = 100, DT50_ws = 100, Koc = 50, max_ws = 0, max_soil = 0.5)
#' PEC_sw_focus(new_dummy, 1000, scenario = "cereals, winter", met = M1)
PEC_sw_focus <- function(parent, rate, n = 1, i = NA,
+ comment = "",
met = NULL,
f_drift = NA, f_rd = 0.1,
- scenario = FOCUS_Step_12_scenarios$names)
+ scenario = FOCUS_Step_12_scenarios$names,
+ txt_file = "pesticide.txt", overwrite = FALSE, append = TRUE)
{
if (n > 1 & is.na(i)) stop("Please specify the interval i if n > 1")
@@ -66,7 +75,37 @@ PEC_sw_focus <- function(parent, rate, n = 1, i = NA,
# }
}
+ # Write to txt file if requested
+ add_line <- function(x) cat(paste0(x, "\r\n"), file = txt, append = TRUE)
+ add <- function(x) cat(paste(x, "\t"), file = txt, append = TRUE)
+ if (file.exists(txt_file)) {
+ if (append) {
+ txt <- file(txt_file, "a")
+ } else {
+ if (overwrite) {
+ txt <- file(txt_file, "w")
+ } else {
+ stop("The file", txt_file, "already exists, and you did not request",
+ "appending or overwriting it")
+ }
+ }
+ }
+ on.exit(close(txt))
+
+ # Write header to txt file
+ header <- c("Active Substance", "Compound", "Comment", "Mol mass a.i.",
+ "Mol mass met.", "Water solubility", "KOC assessed compound",
+ "KOC parent compound", "DT50", "Max. in Water",
+ "Max. in Soil asessed compound", # we reproduce the typo...
+ "App. Rate", "Number of App.", "Time between app.",
+ "App. Type", "DT50 soil parent compound", "DT50 soil",
+ "DT50 water", "DT50 sediment", "Region / Season",
+ "Interception class")
+ add_line(paste(header, collapse = "\t"))
+
+
if (is.null(met)) {
+ compound = parent$name
cwsat = parent$cwsat
mw_ratio = 1
max_soil = 1
@@ -74,6 +113,7 @@ PEC_sw_focus <- function(parent, rate, n = 1, i = NA,
Koc = parent$Koc
DT50_ws = parent$DT50_ws
} else {
+ compound = met$name
cwsat = met$cwsat
mw_ratio = met$mw / parent$mw
max_soil = met$max_soil
@@ -82,6 +122,18 @@ PEC_sw_focus <- function(parent, rate, n = 1, i = NA,
DT50_ws = met$DT50_ws
}
+ add(parent$name)
+ add(compound)
+ add(comment)
+ if (is.na(parent$mw)) add("-99.00")
+ else add(sprintf("%.2f", parent$mw))
+ if (is.na(met$mw)) add("-99.00")
+ else add(sprintf("%.2f", met$mw))
+ add(sprintf("%.2f", cwsat))
+ add(sprintf("%.2f", Koc))
+ if (is.null(met)) add("0.00E+00")
+ else add(sprintf("%.2f", parent$Koc))
+
# Rates for a single application
eq_rate_drift_s = mw_ratio * max_ws * rate
# Parent only, or metabolite formed in soil:
@@ -182,16 +234,17 @@ PEC_sw_focus <- function(parent, rate, n = 1, i = NA,
#' Create a chemical compound object for FOCUS Step 1 calculations
#'
#' @export
+#' @param name Length one character vector containing the name
#' @param cwsat Water solubility in mg/L
#' @param DT50_ws Half-life in water/sediment systems in days
#' @param Koc Partition coefficient between organic carbon and water
#' in L/kg.
-#' @param mw Molar weight in g/mol
+#' @param mw Molar weight in g/mol.
#' @param max_soil Maximum observed fraction (dimensionless) in soil
#' @param max_ws Maximum observed fraction (dimensionless) in water/sediment
#' systems
#' @return A list with the substance specific properties
-chent_focus_sw <- function(Koc, DT50_ws = NA, cwsat = 1000, mw = NA, max_soil = 1, max_ws = 1)
+chent_focus_sw <- function(name, Koc, DT50_ws = NA, cwsat = 1000, mw = NA, max_soil = 1, max_ws = 1)
{
list(Koc = Koc, DT50_ws = DT50_ws, cwsat = cwsat,
mw = mw, max_soil = max_soil, max_ws = max_ws)
diff --git a/man/PEC_sw_focus.Rd b/man/PEC_sw_focus.Rd
index 7562b88..a5628d7 100644
--- a/man/PEC_sw_focus.Rd
+++ b/man/PEC_sw_focus.Rd
@@ -5,7 +5,8 @@
\title{Calculate FOCUS Step 1 PEC surface water}
\usage{
PEC_sw_focus(parent, rate, n = 1, i = NA, met = NULL, f_drift = NA,
- f_rd = 0.1, scenario = FOCUS_Step_12_scenarios$names)
+ f_rd = 0.1, scenario = FOCUS_Step_12_scenarios$names,
+ txt_file = "pesticide.txt", overwrite = FALSE, append = TRUE)
}
\arguments{
\item{parent}{A list containing substance specific parameters, e.g.
@@ -33,6 +34,15 @@ parent or a metabolite}
\item{scenario}{The name of the scenario. Must be one of the scenario
names given in \code{\link{FOCUS_Step_12_scenarios}}}
+
+\item{txt_file}{the name, and potentially the full path to the
+Steps.12 input text file to which the specification of the run(s)
+should be written}
+
+\item{overwrite}{Should an existing file a the location specified in
+\code{txt_file} be overwritten?}
+
+\item{append}{Should the input text file be appended?}
}
\description{
This is reimplementation of Step 1 of the FOCUS Step 1 and 2 calculator
diff --git a/tests/testthat/test_step_1.R b/tests/testthat/test_step_1.R
index 2be5df8..0c8710b 100644
--- a/tests/testthat/test_step_1.R
+++ b/tests/testthat/test_step_1.R
@@ -1,10 +1,20 @@
context("FOCUS Step 1 calculations")
t_out <- c(0, 1, 2, 4) # Checking the first four days is sufficient for Step 1
+test_txt <- readLines(
+ system.file("testdata/Steps_12_pesticide.txt", package = "pfm")
+)
test_that("Results of Steps 1/2 calculator for Dummy 1 are reproduced", {
- dummy_1 <- chent_focus_sw(cwsat = 6000, DT50_ws = 6, Koc = 344.8)
- res_dummy_1 <- PEC_sw_focus(dummy_1, 3000, f_drift = 0)
+ dummy_1 <- chent_focus_sw("Dummy 1", cwsat = 6000, DT50_ws = 6, Koc = 344.8)
+ res_dummy_1 <- PEC_sw_focus(dummy_1, 3000,
+ comment = "Potatoes, Southern Europe, spring, 1 app/season, soil incorporation",
+ f_drift = 0,
+ append = FALSE, overwrite = TRUE)
+
+ pest_txt <- readLines("pesticide.txt")
+ expect_equal(test_txt[1], pest_txt[1])
+ strsplit(test_txt[2], "\t")[[1]]
PEC_orig_1 = matrix(NA, nrow = length(t_out), ncol = 4,
dimnames = list(Time = t_out, type = c("PECsw", "TWAECsw", "PECsed", "TWAECsed")))
@@ -112,3 +122,4 @@ test_that("Results of Steps 1/2 calculator for New Dummy (M1-M3) are reproduced"
expect_equal(res_M2$PEC[1:4, ], PEC_orig_M2[, ], tolerance = 0.01, scale = 1)
})
+unlink("pesticide.txt")

Contact - Imprint