From ec79637749d300ab4ca170805c673905e52d67dd Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Wed, 22 Apr 2015 13:42:10 +0200 Subject: Add simplest PEC soil calcs, use testthat --- pkg/DESCRIPTION | 11 ++++--- pkg/NAMESPACE | 3 +- pkg/R/PEC_soil.R | 45 ++++++++++++++++++++++++++ pkg/man/PEC_soil.Rd | 36 +++++++++++++++++++++ pkg/man/TOXSWA_cwa.Rd | 2 +- pkg/man/plot.TOXSWA_cwa.Rd | 2 +- pkg/man/read.TOXSWA_cwa.Rd | 2 +- pkg/tests/testthat.R | 4 +++ pkg/tests/testthat/H_sw_D4_pond.rds | Bin 0 -> 243113 bytes pkg/tests/testthat/H_sw_R1_stream_events.rds | Bin 0 -> 332 bytes pkg/tests/testthat/H_sw_R1_stream_windows.rds | Bin 0 -> 234 bytes pkg/tests/testthat/test_PEC_soil.R | 13 ++++++++ pkg/tests/testthat/test_TOXSWA_cwa.R | 25 ++++++++++++++ 13 files changed, 135 insertions(+), 8 deletions(-) create mode 100644 pkg/R/PEC_soil.R create mode 100644 pkg/man/PEC_soil.Rd create mode 100644 pkg/tests/testthat.R create mode 100644 pkg/tests/testthat/H_sw_D4_pond.rds create mode 100644 pkg/tests/testthat/H_sw_R1_stream_events.rds create mode 100644 pkg/tests/testthat/H_sw_R1_stream_windows.rds create mode 100644 pkg/tests/testthat/test_PEC_soil.R create mode 100644 pkg/tests/testthat/test_TOXSWA_cwa.R (limited to 'pkg') diff --git a/pkg/DESCRIPTION b/pkg/DESCRIPTION index 29e36e7..a327ae1 100644 --- a/pkg/DESCRIPTION +++ b/pkg/DESCRIPTION @@ -1,11 +1,14 @@ Package: pfm Type: Package Title: Utilities for pesticide fate modelling -Version: 0.1-1 -Date: 2015-01-23 -Authors@R: "Johannes Ranke [aut, cre, cph]" -Description: Utilities for dealing with data from FOCUS software tools. +Version: 0.1-2 +Date: 2015-04-22 +Authors@R: person("Johannes Ranke", email = "jranke@uni-bremen.de", + role = c("aut", "cre", "cph")) +Description: Utilities for simple PEC calculations and for dealing with data + from some FOCUS pesticide fate modelling software. Depends: R6 +Suggests: testthat License: GPL LazyLoad: yes LazyData: yes diff --git a/pkg/NAMESPACE b/pkg/NAMESPACE index 4b55df5..dd140d0 100644 --- a/pkg/NAMESPACE +++ b/pkg/NAMESPACE @@ -1,6 +1,7 @@ -# Generated by roxygen2 (4.1.0): do not edit by hand +# Generated by roxygen2 (4.1.0.9001): do not edit by hand S3method(plot,TOXSWA_cwa) +export(PEC_soil) export(TOXSWA_cwa) export(read.TOXSWA_cwa) importFrom(R6,R6Class) diff --git a/pkg/R/PEC_soil.R b/pkg/R/PEC_soil.R new file mode 100644 index 0000000..3dbc2eb --- /dev/null +++ b/pkg/R/PEC_soil.R @@ -0,0 +1,45 @@ +# Copyright (C) 2015 Johannes Ranke +# Contact: jranke@uni-bremen.de +# This file is part of the R package pfm + +# This program is free software: you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation, either version 3 of the License, or (at your option) any later +# version. + +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. + +# You should have received a copy of the GNU General Public License along with +# this program. If not, see + +#' Calculate predicted environmental concentrations in soil +#' +#' This is a basic, vectorised form of a simple calculation of a contaminant +#' concentration in bulk soil based on complete, instantaneous mixing. +#' +#' @param rate Application rate in units specified below +#' @param rate_units Defaults to g/ha +#' @param interception The fraction of the application rate that does not reach the soil +#' @param mixing_depth Mixing depth in cm +#' @param bulk_density Bulk density of the soil. Defaults to 1.5 kg/L, or 1500 kg/m3 +#' @param PEC_units Requested units for the calculated PEC. Only mg/kg currently supported +#' @return The predicted concentration in soil +#' @export +#' @author Johannes Ranke +#' @examples +#' PEC_soil(100, interception = 0.25) +PEC_soil <- function(rate, rate_units = "g/ha", interception = 0, + mixing_depth = 5, bulk_density = 1.5, + PEC_units = "mg/kg") +{ + rate_to_soil = (1 - interception) * rate + rate_units = match.arg(rate_units) + PEC_units = match.arg(PEC_units) + soil_volume = 100 * 100 * (mixing_depth/100) # in m3 + soil_mass = soil_volume * bulk_density * 1000 # in kg + PEC_soil = rate_to_soil * 1000 / soil_mass # in mg/kg + return(PEC_soil) +} diff --git a/pkg/man/PEC_soil.Rd b/pkg/man/PEC_soil.Rd new file mode 100644 index 0000000..76ffd4b --- /dev/null +++ b/pkg/man/PEC_soil.Rd @@ -0,0 +1,36 @@ +% Generated by roxygen2 (4.1.0.9001): do not edit by hand +% Please edit documentation in R/PEC_soil.R +\name{PEC_soil} +\alias{PEC_soil} +\title{Calculate predicted environmental concentrations in soil} +\usage{ +PEC_soil(rate, rate_units = "g/ha", interception = 0, mixing_depth = 5, + bulk_density = 1.5, PEC_units = "mg/kg") +} +\arguments{ +\item{rate}{Application rate in units specified below} + +\item{rate_units}{Defaults to g/ha} + +\item{interception}{The fraction of the application rate that does not reach the soil} + +\item{mixing_depth}{Mixing depth in cm} + +\item{bulk_density}{Bulk density of the soil. Defaults to 1.5 kg/L, or 1500 kg/m3} + +\item{PEC_units}{Requested units for the calculated PEC. Only mg/kg currently supported} +} +\value{ +The predicted concentration in soil +} +\description{ +This is a basic, vectorised form of a simple calculation of a contaminant +concentration in bulk soil based on complete, instantaneous mixing. +} +\examples{ +PEC_soil(100, interception = 0.25) +} +\author{ +Johannes Ranke +} + diff --git a/pkg/man/TOXSWA_cwa.Rd b/pkg/man/TOXSWA_cwa.Rd index c4d573b..5aa8bfc 100644 --- a/pkg/man/TOXSWA_cwa.Rd +++ b/pkg/man/TOXSWA_cwa.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.0.9001): do not edit by hand % Please edit documentation in R/TOXSWA_cwa.R \docType{class} \name{TOXSWA_cwa} diff --git a/pkg/man/plot.TOXSWA_cwa.Rd b/pkg/man/plot.TOXSWA_cwa.Rd index 9136c10..1dddb78 100644 --- a/pkg/man/plot.TOXSWA_cwa.Rd +++ b/pkg/man/plot.TOXSWA_cwa.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.0.9001): do not edit by hand % Please edit documentation in R/TOXSWA_cwa.R \name{plot.TOXSWA_cwa} \alias{plot.TOXSWA_cwa} diff --git a/pkg/man/read.TOXSWA_cwa.Rd b/pkg/man/read.TOXSWA_cwa.Rd index ded0a39..f67455f 100644 --- a/pkg/man/read.TOXSWA_cwa.Rd +++ b/pkg/man/read.TOXSWA_cwa.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.0.9001): do not edit by hand % Please edit documentation in R/TOXSWA_cwa.R \name{read.TOXSWA_cwa} \alias{read.TOXSWA_cwa} diff --git a/pkg/tests/testthat.R b/pkg/tests/testthat.R new file mode 100644 index 0000000..d2df1cc --- /dev/null +++ b/pkg/tests/testthat.R @@ -0,0 +1,4 @@ +library(testthat) +library(pfm) + +test_check("pfm") diff --git a/pkg/tests/testthat/H_sw_D4_pond.rds b/pkg/tests/testthat/H_sw_D4_pond.rds new file mode 100644 index 0000000..9ad8194 Binary files /dev/null and b/pkg/tests/testthat/H_sw_D4_pond.rds differ diff --git a/pkg/tests/testthat/H_sw_R1_stream_events.rds b/pkg/tests/testthat/H_sw_R1_stream_events.rds new file mode 100644 index 0000000..3e2828e Binary files /dev/null and b/pkg/tests/testthat/H_sw_R1_stream_events.rds differ diff --git a/pkg/tests/testthat/H_sw_R1_stream_windows.rds b/pkg/tests/testthat/H_sw_R1_stream_windows.rds new file mode 100644 index 0000000..29e13f2 Binary files /dev/null and b/pkg/tests/testthat/H_sw_R1_stream_windows.rds differ diff --git a/pkg/tests/testthat/test_PEC_soil.R b/pkg/tests/testthat/test_PEC_soil.R new file mode 100644 index 0000000..27b2eb7 --- /dev/null +++ b/pkg/tests/testthat/test_PEC_soil.R @@ -0,0 +1,13 @@ +library(pfm) +context("Simple PEC soil calculations") + +test_that("PEC_soil calculates correctly", { + # Application of 100 g/ha gives 0.133 mg/kg under default assumptions + expect_equal(PEC_soil(100), 0.1 * 4/3) + + # or 0.1 mg/kg assuming 25% interception + expect_equal(PEC_soil(100, interception = 0.25), 0.1) + + # Mixing depth of 1 cm gives five-fold PEC + expect_equal(PEC_soil(100, interception = 0.25, mixing_depth = 1), 0.5) +}) diff --git a/pkg/tests/testthat/test_TOXSWA_cwa.R b/pkg/tests/testthat/test_TOXSWA_cwa.R new file mode 100644 index 0000000..d91b79a --- /dev/null +++ b/pkg/tests/testthat/test_TOXSWA_cwa.R @@ -0,0 +1,25 @@ +library(pfm) +context("Read and analyse TOXSWA cwa files") + +test_that("TOXSWA cwa file is correctly read", { + H_sw_D4_pond <- read.TOXSWA_cwa("00001p_pa.cwa", + basedir = "SwashProjects/project_H_sw/TOXSWA", + zipfile = system.file("testdata/SwashProjects.zip", + package = "pfm")) + expect_equal_to_reference(H_sw_D4_pond, file = "H_sw_D4_pond.rds") +}) + +test_that("Getting events and moving window analysis works", { + H_sw_R1_stream <- read.TOXSWA_cwa("00003s_pa.cwa", + basedir = "SwashProjects/project_H_sw/TOXSWA", + zipfile = system.file("testdata/SwashProjects.zip", + package = "pfm")) + + # Event analysis with two different thresholds + H_sw_R1_stream$get_events(c(2, 10)) + expect_equal_to_reference(H_sw_R1_stream$events, file = "H_sw_R1_stream_events.rds") + + # Moving window analysis + H_sw_R1_stream$moving_windows(c(7, 21)) + expect_equal_to_reference(H_sw_R1_stream$windows, file = "H_sw_R1_stream_windows.rds") +}) -- cgit v1.2.1