diff options
Diffstat (limited to 'R')
-rw-r--r-- | R/PEC_soil.R | 3 | ||||
-rw-r--r-- | R/PEC_sw_drainage_UK.R | 36 | ||||
-rw-r--r-- | R/PEC_sw_drift.R | 75 | ||||
-rw-r--r-- | R/PEC_sw_exposit.R (renamed from R/PEC_sw_exposit_runoff.R) | 91 | ||||
-rw-r--r-- | R/PEC_sw_sed.R | 22 | ||||
-rw-r--r-- | R/endpoint.R | 2 | ||||
-rw-r--r-- | R/twa.R | 7 |
7 files changed, 156 insertions, 80 deletions
diff --git a/R/PEC_soil.R b/R/PEC_soil.R index ac551a7..d6356b9 100644 --- a/R/PEC_soil.R +++ b/R/PEC_soil.R @@ -68,7 +68,8 @@ if(getRversion() >= '2.15.1') utils::globalVariables(c("destination", "study_typ #' as Kom here #' @param t_avg Averaging times for time weighted average concentrations #' @param t_act Time series for actual concentrations -#' @param scenarios If this is 'default', the DT50 will be used without correction +#' @param scenarios If this is 'default', a soil bulk density of 1.5 kg/L will +#' be used. The DT50 will be used without correction #' and soil properties as specified in the REACH guidance (R.16, Table #' R.16-9) are used for porewater PEC calculations. If this is "EFSA_2015", #' the DT50 is taken to be a modelling half-life at 20°C and pF2 (for when diff --git a/R/PEC_sw_drainage_UK.R b/R/PEC_sw_drainage_UK.R index d773f40..4b3111e 100644 --- a/R/PEC_sw_drainage_UK.R +++ b/R/PEC_sw_drainage_UK.R @@ -1,7 +1,8 @@ #' Calculate initial predicted environmental concentrations in surface water due to drainage using the UK method #' #' This implements the method specified in the UK data requirements handbook and was checked against the spreadsheet -#' published on the CRC website +#' published on the CRC website. Degradation before the start of the drainage period is taken into account if +#' `latest_application` is specified and the degradation parameters are given either as a `soil_DT50` or a `model`. #' #' @param rate Application rate in g/ha #' @param interception The fraction of the application rate that does not reach the soil @@ -24,6 +25,9 @@ #' @author Johannes Ranke #' @examples #' PEC_sw_drainage_UK(150, Koc = 100) +#' PEC_sw_drainage_UK(60, interception = 0.5, Koc = 550, +#' latest_application = "01 July", soil_DT50 = 200) + PEC_sw_drainage_UK <- function(rate, interception = 0, Koc, latest_application = NULL, soil_DT50 = NULL, model = NULL, model_parms = NULL) @@ -34,20 +38,26 @@ PEC_sw_drainage_UK <- function(rate, interception = 0, Koc, if (!missing(latest_application)) { lct <- Sys.getlocale("LC_TIME") tmp <- Sys.setlocale("LC_TIME", "C") - latest <- as.Date(paste(latest_application, "1999"), "%d %b %Y") + if (latest_application == "29 February") { + ref_year <- 2000 + } else { ref_year <- 1999} + latest <- as.Date(paste(latest_application, ref_year), "%d %b %Y") + if (is.na(latest)) stop("Please specify the latest application in the format '%d %b', e.g. '01 July'") tmp <- Sys.setlocale("LC_TIME", lct) - degradation_time <- as.numeric(difftime(as.Date("1999-10-01"), units = "days", latest)) - if (!missing(soil_DT50)) { - k = log(2)/soil_DT50 - as.Date(paste(latest_application, "1999"), "%d %B %Y") + degradation_time <- as.numeric(difftime(as.Date(paste0(ref_year,"-10-01")), units = "days", latest)) + if (degradation_time > 0) { + if (!missing(soil_DT50)) { + k = log(2)/soil_DT50 + as.Date(paste(latest_application, "1999"), "%d %B %Y") - amount_available <- amount_available * exp(-k * degradation_time) - if (!missing(model)) stop("You already supplied a soil_DT50 value, implying SFO kinetics") - } - if (!missing(model)) { - fraction_left <- pfm_degradation(model, parms = model_parms, - times = degradation_time)[1, "parent"] - amount_available <- fraction_left * amount_available + amount_available <- amount_available * exp(-k * degradation_time) + if (!missing(model)) stop("You already supplied a soil_DT50 value, implying SFO kinetics") + } + if (!missing(model)) { + fraction_left <- pfm_degradation(model, parms = model_parms, + times = degradation_time)[1, "parent"] + amount_available <- fraction_left * amount_available + } } } diff --git a/R/PEC_sw_drift.R b/R/PEC_sw_drift.R index 22aa012..5c7fff4 100644 --- a/R/PEC_sw_drift.R +++ b/R/PEC_sw_drift.R @@ -4,9 +4,16 @@ #' concentration in surface water based on complete, instantaneous mixing #' with input via spray drift. #' +#' It is recommened to specify the arguments `rate`, `water_depth` and +#' `water_width` using [units::units] from the `units` package. +#' #' @inheritParams drift_percentages_rautmann +#' @importFrom units as_units set_units #' @seealso [drift_parameters_focus], [drift_percentages_rautmann] -#' @param rate Application rate in units specified below +#' @param rate Application rate in units specified below, or with units defined via the +#' `units` package. +#' @param rate_units Defaults to g/ha. For backwards compatibility, only used +#' if the specified rate does not have [units::units]]. #' @param drift_percentages Percentage drift values for which to calculate PECsw. #' Overrides 'drift_data' and 'distances' if not NULL. #' @param drift_data Source of drift percentage data. If 'JKI', the [drift_data_JKI] @@ -14,9 +21,8 @@ #' either in the original form or integrated over the width of the water body, depending #' on the 'formula' argument. #' @param crop_group_JKI When using the 'JKI' drift data, one of the German names -#' as used in [drift_parameters_focus]. +#' as used in [drift_data_JKI]. Will only be used if drift_data is 'JKI'. #' @param water_depth Depth of the water body in cm -#' @param rate_units Defaults to g/ha #' @param PEC_units Requested units for the calculated PEC. Only µg/L currently supported #' @param water_width Width of the water body in cm #' @param side_angle The angle of the side of the water relative to the bottom which @@ -34,13 +40,13 @@ #' PEC_sw_drift(100, distances = c(1, 3, 5, 6, 10, 20, 50, 100), drift_data = "RF") #' #' # or consider aerial application -#' PEC_sw_drift(100, distances = c(1, 3, 5, 6, 10, 20, 50, 100), drift_data = "RF", -#' crop_group_focus = "aerial") +#' PEC_sw_drift(100, distances = c(1, 3, 5, 6, 10, 20, 50, 100), drift_data = "RF", +#' crop_group_RF = "aerial") #' #' # Using custom drift percentages is also supported #' PEC_sw_drift(100, drift_percentages = c(2.77, 0.95, 0.57, 0.48, 0.29, 0.15, 0.06, 0.03)) #' -#' # The influence of assuming a 45° angle of the sides of the waterbody and the width of the +#' # The influence of assuming a 45° angle of the sides of the waterbody and the width of the #' # waterbody can be illustrated #' PEC_sw_drift(100) #' PEC_sw_drift(100, drift_data = "RF") @@ -49,40 +55,51 @@ #' PEC_sw_drift(100, drift_data = "RF", formula = "FOCUS", side_angle = 45, water_width = 200) PEC_sw_drift <- function(rate, applications = 1, - water_depth = 30, + water_depth = as_units("30 cm"), drift_percentages = NULL, drift_data = c("JKI", "RF"), - crop_group_JKI = c("Ackerbau", + crop_group_JKI = c("Ackerbau", "Obstbau frueh", "Obstbau spaet", "Weinbau frueh", "Weinbau spaet", "Hopfenbau", "Flaechenkulturen > 900 l/ha", "Gleisanlagen"), - crop_group_focus = c("arable", "hops", "vines, late", "vines, early", + crop_group_RF = c("arable", "hops", "vines, late", "vines, early", "fruit, late", "fruit, early", "aerial"), distances = c(1, 5, 10, 20), formula = c("Rautmann", "FOCUS"), - water_width = 100, + water_width = as_units("100 cm"), side_angle = 90, rate_units = "g/ha", PEC_units = "\u00B5g/L") { rate_units <- match.arg(rate_units) PEC_units <- match.arg(PEC_units) + # Set default units if not specified + if (!inherits(rate, "units")) rate <- set_units(rate, rate_units, mode = "symbolic") + if (!inherits(water_width, "units")) water_width <- set_units(water_width, "cm") + if (!inherits(water_depth, "units")) water_depth <- set_units(water_depth, "cm") drift_data <- match.arg(drift_data) crop_group_JKI <- match.arg(crop_group_JKI) - crop_group_focus <- match.arg(crop_group_focus) + crop_group_RF <- match.arg(crop_group_RF) + if (drift_data == "JKI" & crop_group_RF != "arable") { + stop("Specifying crop_group_RF only makes sense if 'RF' is used for 'drift_data'") + } + if (drift_data == "RF" & crop_group_JKI != "Ackerbau") { + stop("Specifying crop_group_JKI only makes sense if 'JKI' is used for 'drift_data'") + } formula <- match.arg(formula) if (side_angle < 0 | side_angle > 90) stop("The side anglemust be between 0 and 90 degrees") - mean_water_width <- if (side_angle == 90) water_width + mean_water_width <- if (side_angle == 90) water_width # Mean water width over waterbody depth else water_width - (water_depth / tanpi(side_angle/180)) - water_volume <- 100 * mean_water_width * (water_depth/100) * 1000 # in L (for 1 ha) - PEC_sw_overspray <- rate * 1e6 / water_volume # in µg/L + if (as.numeric(mean_water_width) < 0) stop("Undefined geometry") + relative_mean_water_width <- mean_water_width / water_width # Always <= 1 + PEC_sw_overspray <- set_units(rate / (relative_mean_water_width * water_depth), PEC_units, mode = "symbolic") dist_index <- as.character(distances) if (is.null(drift_percentages)) { drift_percentages <- switch(drift_data, JKI = pfm::drift_data_JKI[[applications]][dist_index, crop_group_JKI], - RF = drift_percentages_rautmann(distances, applications, + RF = drift_percentages_rautmann(distances, applications, formula = formula, - crop_group_focus, widths = water_width/100) + crop_group_RF, widths = as.numeric(set_units(water_width, "m"))) ) names(drift_percentages) <- paste(dist_index, "m") } else { @@ -101,14 +118,14 @@ PEC_sw_drift <- function(rate, #' @param distances The distances in m for which to get PEC values #' @param widths The widths of the water bodies (only used in the FOCUS formula) #' @param applications Number of applications for selection of drift percentile -#' @param crop_group_focus One of the crop groups as used in [drift_parameters_focus] +#' @param crop_group_RF One of the crop groups as used in [drift_parameters_focus] #' @seealso [drift_parameters_focus], [PEC_sw_drift] #' @references FOCUS (2014) Generic guidance for Surface Water Scenarios (version 1.4). #' FOrum for the Co-ordination of pesticde fate models and their USe. #' <http://esdac.jrc.ec.europa.eu/public_path/projects_data/focus/sw/docs/Generic%20FOCUS_SWS_vc1.4.pdf> #' @export #' @examples -#' # Compare JKI data with Rautmann formula +#' # Compare JKI data with Rautmann and FOCUS formulas for arable crops (default) #' # One application on field crops, for 1 m, 3 m and 5 m distance #' drift_data_JKI[[1]][as.character(c(1, 3, 5)), "Ackerbau"] #' drift_percentages_rautmann(c(1, 3, 5)) @@ -116,31 +133,31 @@ PEC_sw_drift <- function(rate, #' #' # One application to early or late fruit crops #' drift_data_JKI[[1]][as.character(c(3, 5, 20, 50)), "Obstbau frueh"] -#' drift_percentages_rautmann(c(3, 5, 20, 50), crop_group = "fruit, early") -#' drift_percentages_rautmann(c(3, 5, 20, 50), crop_group = "fruit, early", +#' drift_percentages_rautmann(c(3, 5, 20, 50), crop_group_RF = "fruit, early") +#' drift_percentages_rautmann(c(3, 5, 20, 50), crop_group_RF = "fruit, early", #' formula = "FOCUS") #' drift_data_JKI[[1]][as.character(c(3, 5, 20, 50)), "Obstbau spaet"] -#' drift_percentages_rautmann(c(3, 5, 20, 50), crop_group = "fruit, late") -#' drift_percentages_rautmann(c(3, 5, 20, 50), crop_group = "fruit, late", +#' drift_percentages_rautmann(c(3, 5, 20, 50), crop_group_RF = "fruit, late") +#' drift_percentages_rautmann(c(3, 5, 20, 50), crop_group_RF = "fruit, late", #' formula = "FOCUS") -#' +#' #' # We get a continuum if the waterbody covers the hinge distance #' # (11.4 m for 1 early app to fruit) #' x <- seq(3, 30, by = 0.1) -#' d <- drift_percentages_rautmann(x, crop_group = "fruit, early", formula = "FOCUS") +#' d <- drift_percentages_rautmann(x, crop_group_RF = "fruit, early", formula = "FOCUS") #' plot(x, d, type = "l", -#' xlab = "Distance of near edge [m]", +#' xlab = "Distance of near edge [m]", #' ylab = "Mean drift percentage over waterbody width", #' main = "One application to fruit, early") #' abline(v = 11.4, lty = 2) -drift_percentages_rautmann <- function(distances, applications = 1, - crop_group_focus = c("arable", "hops", "vines, late", "vines, early", "fruit, late", +drift_percentages_rautmann <- function(distances, applications = 1, + crop_group_RF = c("arable", "hops", "vines, late", "vines, early", "fruit, late", "fruit, early", "aerial"), formula = c("Rautmann", "FOCUS"), widths = 1 ) { - cg <- match.arg(crop_group_focus) + cg <- match.arg(crop_group_RF) if (!applications %in% 1:8) stop("Only 1 to 8 applications are supported") formula <- match.arg(formula) @@ -161,7 +178,7 @@ drift_percentages_rautmann <- function(distances, applications = 1, ifelse(z2 < hinge, # farther edge closer than hinge distance A/(widths * (B + 1)) * (z2^(B + 1) - z1^(B + 1)), - ifelse(z1 < hinge, + ifelse(z1 < hinge, # hinge distance in waterbody (between z1 and z2) (A/(B + 1) * (H^(B + 1) - z1^(B + 1)) + C/(D + 1) * (z2^(D + 1) - H^(D + 1)))/widths, # z1 >= hinge, i.e. near edge farther than hinge distance diff --git a/R/PEC_sw_exposit_runoff.R b/R/PEC_sw_exposit.R index 8b89cd9..282a1ac 100644 --- a/R/PEC_sw_exposit_runoff.R +++ b/R/PEC_sw_exposit.R @@ -47,6 +47,11 @@ #' This is a reimplementation of the calculation described in the Exposit 3.02 spreadsheet file, #' in the worksheet "Konzept Runoff". #' +#' It is recommened to specify the arguments `rate`, `Koc`, `DT50`, `t_runoff`, `V_ditch` and `V_event` +#' using [units::units] from the `units` package. +#' +#' @importFrom units as_units set_units drop_units +#' @importFrom dplyr across mutate #' @param rate The application rate in g/ha #' @param interception The fraction intercepted by the crop #' @param Koc The sorption coefficient to soil organic carbon @@ -74,43 +79,61 @@ #' @examples #' PEC_sw_exposit_runoff(500, Koc = 150) #' PEC_sw_exposit_runoff(600, Koc = 10000, DT50 = 195, exposit = "3.01a") -PEC_sw_exposit_runoff <- function(rate, interception = 0, Koc, DT50 = Inf, t_runoff = 3, +PEC_sw_exposit_runoff <- function(rate, interception = 0, Koc, + DT50 = set_units(Inf, "d"), + t_runoff = set_units(3, "days"), exposit_reduction_version = c("3.02", "3.01a", "3.01a2", "2.0"), - V_ditch = 30, V_event = 100, dilution = 2) + V_ditch = set_units(30, "m3"), V_event = set_units(100, "m3"), dilution = 2) { + # Set default units if not specified + if (!inherits(rate, "units")) rate <- set_units(rate, "g/ha") + if (!inherits(Koc, "units")) Koc <- set_units(Koc, "L/kg") + if (!inherits(DT50, "units")) DT50 <- set_units(DT50, "d") + if (!inherits(t_runoff, "units")) t_runoff <- set_units(t_runoff, "d") + if (!inherits(V_ditch, "units")) V_ditch <- set_units(V_ditch, "m3") + if (!inherits(V_event, "units")) V_event <- set_units(V_event, "m3") + k_deg <- log(2)/DT50 - input <- rate * (1 - interception) * 1 * exp(-k_deg * t_runoff) # assumes 1 ha treated area + + # The input is calculated for an area of 1 ha + input <- rate * as_units(1, "ha") * (1 - interception) * exp(as.numeric(-k_deg * t_runoff)) + input_units <- units(input) + input_numeric <- drop_units(input) if (length(Koc) > 1) stop("Only one compound at a time supported") exposit_reduction_version <- match.arg(exposit_reduction_version) - red_water <- pfm::perc_runoff_reduction_exposit[[exposit_reduction_version]]["dissolved"] / 100 - red_bound <- pfm::perc_runoff_reduction_exposit[[exposit_reduction_version]]["bound"] / 100 reduction_runoff <- pfm::perc_runoff_reduction_exposit[[exposit_reduction_version]] / 100 + transfer_runoff <- 1 - reduction_runoff - V_runoff <- V_event * (1 - reduction_runoff[["dissolved"]]) # m3 + V_runoff <- V_event * (1 - reduction_runoff[["dissolved"]]) V_flowing_ditch_runoff <- dilution * (V_ditch + V_runoff) - + f_runoff_exposit <- function(Koc) { - Koc_breaks <- c(pfm::perc_runoff_exposit$Koc_lower_bound, Inf) + Koc_breaks <- c(pfm::perc_runoff_exposit$Koc_lower_bound, set_units(Inf, "L/kg")) Koc_classes <- as.character(cut(Koc, Koc_breaks, labels = rownames(pfm::perc_runoff_exposit))) perc_runoff <- pfm::perc_runoff_exposit[Koc_classes, c("dissolved", "bound")] if (identical(Koc, 0)) perc_runoff <- c(dissolved = 0, bound = 0) return(unlist(perc_runoff) / 100) } f_runoff <- f_runoff_exposit(Koc) - runoff_dissolved <- input * f_runoff["dissolved"] * transfer_runoff["dissolved"] - runoff_bound <- input * f_runoff["bound"] * transfer_runoff["bound"] - runoff_input <- cbind(runoff_dissolved, runoff_bound) - runoff_input$total <- runoff_input$dissolved + runoff_input$bound - PEC_sw_runoff <- 1000 * runoff_input / V_flowing_ditch_runoff + runoff_dissolved <- input_numeric * f_runoff["dissolved"] * transfer_runoff[, "dissolved"] + runoff_bound <- input_numeric * f_runoff["bound"] * transfer_runoff[, "bound"] + runoff_input <- cbind(dissolved = runoff_dissolved, bound = runoff_bound, + total = runoff_dissolved + runoff_bound) + rownames(runoff_input) <- rownames(reduction_runoff) + units(runoff_input) <- input_units + + dn <- dimnames(runoff_input) + PEC_sw_runoff <- set_units(runoff_input / V_flowing_ditch_runoff, "\u00B5g/L") + dimnames(PEC_sw_runoff) <- dn result <- list( perc_runoff = 100 * f_runoff, - runoff = runoff_input, - PEC_sw_runoff = PEC_sw_runoff) + runoff = as.data.frame(runoff_input), + PEC_sw_runoff = as.data.frame(PEC_sw_runoff)) return(result) } @@ -123,6 +146,9 @@ PEC_sw_exposit_runoff <- function(rate, interception = 0, Koc, DT50 = Inf, t_run #' with modest to high mobility (groups 2, 3 and 4). In this implementation, #' the group is derived only from the Koc, if not given explicitly. For #' details, see the discussion of the function arguments below. + +#' It is recommened to specify the arguments `rate`, `Koc`, `DT50`, `t_drainage`, +#' `V_ditch` and `V_drainage` using [units::units] from the `units` package. #' #' @param rate The application rate in g/ha #' @param interception The fraction intercepted by the crop @@ -137,10 +163,10 @@ PEC_sw_exposit_runoff <- function(rate, interception = 0, Koc, DT50 = Inf, t_run #' @param dilution The dilution factor #' @return A list containing the following components #' \describe{ -#' \item{perc_runoff}{The runoff percentages for dissolved and bound substance} -#' \item{runoff}{A matrix containing dissolved and bound input for the different distances} -#' \item{PEC_sw_runoff}{A matrix containing PEC values for dissolved and bound substance -#' for the different distances. If the rate was given in g/ha, the PECsw are in microg/L.} +#' \item{perc_drainage_total}{Gesamtaustrag (total fraction of the residue drained)} +#' \item{perc_peak}{Stoßbelastung (fraction drained at event)} +#' \item{PEC_sw_drainage}{A matrix containing PEC values for the spring and autumn +#' scenarios. If the rate was given in g/ha, the PECsw are in microg/L.} #' } #' @export #' @source Excel 3.02 spreadsheet available from @@ -148,18 +174,31 @@ PEC_sw_exposit_runoff <- function(rate, interception = 0, Koc, DT50 = Inf, t_run #' @seealso \code{\link{perc_runoff_exposit}} for runoff loss percentages and \code{\link{perc_runoff_reduction_exposit}} for runoff reduction percentages used #' @examples #' PEC_sw_exposit_drainage(500, Koc = 150) -PEC_sw_exposit_drainage <- function(rate, interception = 0, Koc = NA, mobility = c(NA, "low", "high"), DT50 = Inf, t_drainage = 3, - V_ditch = 30, V_drainage = c(spring = 10, autumn = 100), dilution = 2) +PEC_sw_exposit_drainage <- function(rate, interception = 0, + Koc = NA, mobility = c(NA, "low", "high"), + DT50 = set_units(Inf, "d"), + t_drainage = set_units(3, "days"), + V_ditch = set_units(30, "m3"), + V_drainage = set_units(c(spring = 10, autumn = 100), "m3"), dilution = 2) { - # Rückstand zum Zeitpunkt des Niederschlagsereignisses (residue at the time of the drainage event) + # Set default units if not specified + if (!inherits(rate, "units")) rate <- set_units(rate, "g/ha") + if (!inherits(Koc, "units")) Koc <- set_units(Koc, "L/kg") + if (!inherits(DT50, "units")) DT50 <- set_units(DT50, "d") + if (!inherits(t_drainage, "units")) t_runoff <- set_units(t_drainage, "d") + if (!inherits(V_ditch, "units")) V_ditch <- set_units(V_ditch, "m3") + if (!inherits(V_drainage, "units")) V_event <- set_units(V_drainage, "m3") + k_deg <- log(2)/DT50 - residue <- rate * (1 - interception) * 1 * exp(-k_deg * t_drainage) # assumes 1 ha treated area + + # Total residue at the time of the drainage event, assumes 1 ha treated area + residue <- rate * as_units(1, "ha") * (1 - interception) * exp(as.numeric(-k_deg * t_drainage)) mobility <- match.arg(mobility) if (is.na(mobility)) { if (is.na(Koc)) stop("Koc is needed if the mobility is not specified") else { - if (Koc > 550) mobility = "low" + if (Koc > set_units(550, "L/kg")) mobility = "low" else mobility = "high" } } @@ -178,11 +217,11 @@ PEC_sw_exposit_drainage <- function(rate, interception = 0, Koc = NA, mobility = f_peak = c(spring = 0.125, autumn = 0.25) # Stoßbelastung (fraction drained at event) - PEC_sw_drainage <- 1000 * residue * f_drainage_total * f_peak / V_flowing_ditch_drainage + PEC_sw_drainage <- residue * f_drainage_total * f_peak / V_flowing_ditch_drainage result <- list( perc_drainage_total = 100 * f_drainage_total, perc_peak = 100 * f_peak, - PEC_sw_drainage = PEC_sw_drainage) + PEC_sw_drainage = set_units(PEC_sw_drainage, "\u00B5g/L")) return(result) } diff --git a/R/PEC_sw_sed.R b/R/PEC_sw_sed.R index 2865ab7..28e12d8 100644 --- a/R/PEC_sw_sed.R +++ b/R/PEC_sw_sed.R @@ -10,24 +10,30 @@ #' @param method The method used for the calculation #' @param sediment_depth Depth of the sediment layer #' @param water_depth Depth of the water body in cm -#' @param sediment_density The density of the sediment in L/kg (equivalent to +#' @param sediment_density The density of the sediment in kg/L (equivalent to #' g/cm3) #' @param PEC_sed_units The units of the estimated sediment PEC value #' @return The predicted concentration in sediment #' @export #' @author Johannes Ranke #' @examples +#' library(pfm) +#' library(units) #' PEC_sw_sed(PEC_sw_drift(100, distances = 1), percentage = 50) PEC_sw_sed <- function(PEC_sw, percentage = 100, method = "percentage", - sediment_depth = 5, water_depth = 30, - sediment_density = 1.3, + sediment_depth = set_units(5, "cm"), + water_depth = set_units(30, "cm"), + sediment_density = set_units(1.3, "kg/L"), PEC_sed_units = c("\u00B5g/kg", "mg/kg")) { - method = match.arg(method) - PEC_sed_units = match.arg(PEC_sed_units) + if (!inherits(PEC_sw, "units")) PEC_sw <- set_units(PEC_sw, "\u00B5g/L") + if (!inherits(sediment_depth, "units")) PEC_sw <- set_units(sediment_depth, "cm") + if (!inherits(water_depth, "units")) PEC_sw <- set_units(water_depth, "cm") + if (!inherits(sediment_density, "units")) PEC_sw <- set_units(sediment_density, "cm") + method <- match.arg(method) + PEC_sed_units <- match.arg(PEC_sed_units) if (method == "percentage") { - PEC_sed = PEC_sw * (percentage/100) * (water_depth / sediment_depth) * (1 / sediment_density) - if (PEC_sed_units == "mg/kg") PEC_sed <- PEC_sed / 1000 + PEC_sed <- PEC_sw * (percentage/100) * as.numeric((water_depth / sediment_depth)) * (1 / sediment_density) } - return(PEC_sed) + return(set_units(PEC_sed, PEC_sed_units, mode = "symbolic")) } diff --git a/R/endpoint.R b/R/endpoint.R index 08856f9..5415d84 100644 --- a/R/endpoint.R +++ b/R/endpoint.R @@ -41,7 +41,7 @@ endpoint <- function(chent, signif = 3) { if (!is(chent, "chent")) { - stop("Please supply a chent object as created using the package 'chents' available from jrwb.de") + stop("Please supply a chent object as created using the package 'chents' available from github") } ep_list <- chent$chyaml[[medium]][[type]] if (!is.na(lab_field[1])) { @@ -131,6 +131,7 @@ one_box.mkinfit <- function(x, ini = "model", ..., t_end = 100, res = 0.01) { #' fit_2 <- mkinfit(m_2, FOCUS_2006_D, quiet = TRUE) #' pred_2 <- one_box(fit_2, ini = 1) #' pred_2_saw <- sawtooth(pred_2, 2, 7) +#' plot(pred_2_saw) #' plot(pred_2_saw, max_twa = 21, max_twa_var = "m1") plot.one_box <- function(x, xlim = range(time(x)), ylim = c(0, max(x)), @@ -148,7 +149,9 @@ plot.one_box <- function(x, x_twa$window_end[max_twa_var], value, col = "grey") text(x_twa$window_end[max_twa_var], value, paste("Maximum:", signif(value, 3)), pos = 4) # Plot a second time to cover the grey rectangle - matlines(time(x), as.matrix(x), lty = 1:length(obs_vars), col = 1:length(obs_vars)) + plot.ts(x, plot.type = "single", xlab = xlab, ylab = ylab, + lty = 1:length(obs_vars), col = 1:length(obs_vars), + las = 1, xlim = xlim, ylim = ylim) } } @@ -229,7 +232,7 @@ twa.one_box <- function(x, window = 21) resolution = 1/frequency(x) n_filter = window/resolution - result = filter(x, rep(1/n_filter, n_filter), method = "convolution", sides = 1) + result = stats::filter(x, rep(1/n_filter, n_filter), method = "convolution", sides = 1) class(result) = c("one_box", "ts") dimnames(result) <- dimnames(x) return(result) |