diff options
author | Johannes Ranke <jranke@uni-bremen.de> | 2015-04-22 13:42:10 +0200 |
---|---|---|
committer | Johannes Ranke <jranke@uni-bremen.de> | 2015-04-22 13:42:10 +0200 |
commit | ec79637749d300ab4ca170805c673905e52d67dd (patch) | |
tree | 6eabf636f3012daecbe7ee446b1083da86d64e63 | |
parent | 8ffbc49b3f01deac6f9e83aaa6d318d4e2f8552b (diff) |
Add simplest PEC soil calcs, use testthat
-rw-r--r-- | GNUmakefile (renamed from Makefile) | 14 | ||||
-rw-r--r-- | README.rmd | 16 | ||||
-rw-r--r-- | pkg/DESCRIPTION | 11 | ||||
-rw-r--r-- | pkg/NAMESPACE | 3 | ||||
-rw-r--r-- | pkg/R/PEC_soil.R | 45 | ||||
-rw-r--r-- | pkg/man/PEC_soil.Rd | 36 | ||||
-rw-r--r-- | pkg/man/TOXSWA_cwa.Rd | 2 | ||||
-rw-r--r-- | pkg/man/plot.TOXSWA_cwa.Rd | 2 | ||||
-rw-r--r-- | pkg/man/read.TOXSWA_cwa.Rd | 2 | ||||
-rw-r--r-- | pkg/tests/testthat.R | 4 | ||||
-rw-r--r-- | pkg/tests/testthat/H_sw_D4_pond.rds | bin | 0 -> 243113 bytes | |||
-rw-r--r-- | pkg/tests/testthat/H_sw_R1_stream_events.rds | bin | 0 -> 332 bytes | |||
-rw-r--r-- | pkg/tests/testthat/H_sw_R1_stream_windows.rds | bin | 0 -> 234 bytes | |||
-rw-r--r-- | pkg/tests/testthat/test_PEC_soil.R | 13 | ||||
-rw-r--r-- | pkg/tests/testthat/test_TOXSWA_cwa.R | 25 |
15 files changed, 163 insertions, 10 deletions
@@ -18,6 +18,8 @@ usage: pkgfiles = pkg/DESCRIPTION \ pkg/inst/testdata/* \ + pkg/tests/testthat.R \ + pkg/tests/testthat/* \ pkg/R/* clean: @@ -33,14 +35,24 @@ roxygen: $(TGZ): $(pkgfiles) @echo "Building package..." sed -i -e "s/Date:.*/Date: $(DATE)/" pkg/DESCRIPTION - "$(R_HOME)/bin/Rscript" -e 'library(roxygen2); roxygenize("pkg")' > roxygen.log 2>&1 || cat roxygen.log + "$(R_HOME)/bin/Rscript" -e 'library(roxygen2); roxygenize("pkg")' 2>&1 | tee roxygen.log git log --no-merges -M --date=iso pkg/ > pkg/ChangeLog "$(R_HOME)/bin/R" CMD build pkg > build.log 2>&1 @echo "DONE." build: $(TGZ) +test: build + @echo "Running testthat tests..." + "$(R_HOME)/bin/Rscript" -e 'library(devtools); devtools::test("pkg")' 2>&1 | tee roxygen.log + @echo "DONE." + check: build + @echo "Running check..." + "$(R_HOME)/bin/R" CMD check $(TGZ) + @echo "DONE." + +crancheck: build @echo "Running CRAN check..." "$(R_HOME)/bin/R" CMD check --as-cran $(TGZ) @echo "DONE." @@ -33,7 +33,11 @@ library(devtools) install_github("jranke/pfm", subdir = "pkg", quick = TRUE) ``` -To read in and analyse a cwa file: +## Use + +# Analyse TOXSWA output + +Read in and analyse a cwa file: ```{r} library(pfm) @@ -43,3 +47,13 @@ example_cwa$get_events(c(20, 100)) example_cwa$moving_windows(c(7, 21)) print(example_cwa) ``` + +# Calculate PEC soil + +Simple PEC soil calculation for an application rate of 100 g/ha and +25% interception, assuming complete mixing into 5 cm and a soil bulk +density of 1.5 kg/L, output in mg/kg: + +```{r} +PEC_soil(100, int = 0.25) +``` 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 <jranke@uni-bremen.de> [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 <http://www.gnu.org/licenses/> + +#' 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 Binary files differnew file mode 100644 index 0000000..9ad8194 --- /dev/null +++ b/pkg/tests/testthat/H_sw_D4_pond.rds diff --git a/pkg/tests/testthat/H_sw_R1_stream_events.rds b/pkg/tests/testthat/H_sw_R1_stream_events.rds Binary files differnew file mode 100644 index 0000000..3e2828e --- /dev/null +++ b/pkg/tests/testthat/H_sw_R1_stream_events.rds diff --git a/pkg/tests/testthat/H_sw_R1_stream_windows.rds b/pkg/tests/testthat/H_sw_R1_stream_windows.rds Binary files differnew file mode 100644 index 0000000..29e13f2 --- /dev/null +++ b/pkg/tests/testthat/H_sw_R1_stream_windows.rds 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") +}) |