From c9bcd8e68db61515080ff377c6a04fa807337258 Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Sat, 17 Jun 2017 16:36:13 +0200 Subject: Start with the generation of an input file --- R/PEC_sw_focus.R | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 3 deletions(-) (limited to 'R/PEC_sw_focus.R') 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) -- cgit v1.2.1