From b38055278d4a801598ece9d2c93716a9bf67134a Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Fri, 27 Jan 2017 01:00:07 +0100 Subject: Set up FOCUS PELMO runs and run them in parallel - This works on Linux using wine - PELMO runs (including pelmo.inp files) are correctly generated - The PLM files for FOCUS Pesticide_D in the test data archive are correctly reproduced - The data files (including FOCUS groundwater scenario data) are now created and documented in R files --- tests/testthat/test_PELMO.R | 101 +++++++++++++++++++++++++++++++++++++++ tests/testthat/test_TOXSWA.R | 56 ++++++++++++++++++++++ tests/testthat/test_TOXSWA_cwa.R | 56 ---------------------- 3 files changed, 157 insertions(+), 56 deletions(-) create mode 100644 tests/testthat/test_PELMO.R create mode 100644 tests/testthat/test_TOXSWA.R delete mode 100644 tests/testthat/test_TOXSWA_cwa.R (limited to 'tests') diff --git a/tests/testthat/test_PELMO.R b/tests/testthat/test_PELMO.R new file mode 100644 index 0000000..4bd36ef --- /dev/null +++ b/tests/testthat/test_PELMO.R @@ -0,0 +1,101 @@ +# Remove when the test is ready to be run by testthat +#library(testthat) +#test_dir <- "/tmp/Rtmp8DFCT5" +#psm_name <- "D_rel_1" +#source("~/git/pfm/inst/extdata/FOCUS_PELMO_data.R") +#source("~/git/pfm/R/PELMO_runs.R") + +library(pfm) +context("Create PELMO runs from psm files and execute them") +PELMO_base <- system.file("FOCUSPELMO.553", package = "PELMO.installeR") + +test_archive <- system.file("testdata/FOCUS_PELMO.tar.bz2", package = "pfm") +test_dir <- tempdir() +untar(test_archive, exdir = test_dir, compressed = "bzip2") + +runs <- list( + list( + psm = "Pesticide_D", + fbe = c("Por"), + vbe = c("Por")), + list( + psm = "Pesticide_D_1_day_pre_em_every_third_year", + pot = c("Cha", "Ham")), + list( + psm = "Pesticide_D_1_May_every_other_year", + mai = c("Cha"))) + +psm_paths = c( + D_rel_1 = PELMO_path(runs[1]$psm, "fbe", "Por"), + D_rel_3 = PELMO_path(runs[2]$psm, "pot", "Ham"), + D_abs_2 = PELMO_path(runs[3]$psm, "mai", "Cha")) + +# Get psm files and put them into PELMO_base +psm_new_locations <- character(0) +for (psm_name in names(psm_paths)) { + psm_file <- file.path(test_dir, psm_paths[psm_name], paste0(runs[1]$psm, ".psm")) + psm_new_location <- file.path(PELMO_base, basename(psm_file)) + psm_new_locations[psm_name] <- psm_new_location + file.copy(psm_file, psm_new_location) +} + +test_that("PELMO runs are correctly set up", { + + # Prepare runs in analogy to the test archive + PELMO_runs(runs, psm_dir = PELMO_base, execute = FALSE, overwrite = TRUE) + + # Check that input files are correctly generated in the right location + for (run in runs) { + psm <- run$psm + message(psm) + crops <- setdiff(names(run), "psm") + for (crop in crops) { + message(crop) + for (scenario in run[[crop]]) { + message(scenario) + pp <- PELMO_path(psm, crop, scenario) + + input_new <- readLines(file.path(PELMO_base, "FOCUS", pp, "pelmo.inp")) + input_test <- readLines(file.path(test_dir, pp, "pelmo.inp")) + + # Check if the input files are correctly reproduced + expect_identical(input_new, input_test) + } + } + } +}) + +test_that("PELMO runs can be run and give the expected result files", { + run_PELMO(runs, psm_dir = PELMO_base, cores = 5) + + plm_files <- c("CHEM.PLM", "ECHO.PLM", "KONZCHEM.PLM", "PLNTPEST.plm", + "PLOT.PLM", "WASSER.PLM") + + # Check that if output is the same as in the test archive + for (run in runs) { + psm <- run$psm + crops <- setdiff(names(run), "psm") + for (crop in crops) { + # message(crop) + for (scenario in run[[crop]]) { + # message(scenario) + pp <- PELMO_path(psm, crop, scenario) + + for (plm in plm_files) { + new <- readLines(file.path(PELMO_base, "FOCUS", pp, plm)) + test <- readLines(file.path(test_dir, pp, plm)) + + # Check if the ouput files are correctly reproduced + expect_identical(new, test) + } + } + } + } +}) + +test_that("PELMO runs are correctly evaluated", { + +}) + +# Clean up +unlink(psm_new_locations) diff --git a/tests/testthat/test_TOXSWA.R b/tests/testthat/test_TOXSWA.R new file mode 100644 index 0000000..7c9a73c --- /dev/null +++ b/tests/testthat/test_TOXSWA.R @@ -0,0 +1,56 @@ +library(pfm) +context("Read and analyse TOXSWA cwa files") + +zipfile_test = system.file("testdata/SwashProjects.zip", package = "pfm") +basedir_test = "SwashProjects/project_H_sw/TOXSWA" + +H_sw_D4_pond <- read.TOXSWA_cwa("00001p_pa.cwa", + basedir = basedir_test, + zipfile = zipfile_test) + +H_sw_R1_stream <- read.TOXSWA_cwa("00003s_pa.cwa", + basedir = basedir_test, + zipfile = zipfile_test) + +basedir_test_2 = "SwashProjects/Project_1/TOXSWA" + +EXSW2_R1_stream <- read.TOXSWA_cwa("3.out", + basedir = basedir_test_2, + zipfile = zipfile_test) + + + +test_that("TOXSWA cwa file is correctly read and printed", { + + # This was the setting when printing the output into text files + options(width = 100) + + # Most content of the R6 object is at least partially printed + + H_sw_D4_pond_printed <- capture.output(print(H_sw_D4_pond)) + + expect_equal(H_sw_D4_pond_printed, readLines("H_sw_D4_pond_printed.txt")) + + H_sw_R1_stream_printed <- capture.output(print(H_sw_R1_stream)) + expect_equal(H_sw_R1_stream_printed, readLines("H_sw_R1_stream_printed.txt")) + + # The basedir is not printed, therefore tested separately + expect_equal(H_sw_D4_pond$basedir, basedir_test) + + EXSW2_R1_stream_printed <- capture.output(print(EXSW2_R1_stream)) + expect_equal(EXSW2_R1_stream_printed, readLines("EXSW2_R1_stream_printed.txt")) + + # The basedir is not printed, therefore tested separately + expect_equal(H_sw_D4_pond$basedir, basedir_test) +}) + +test_that("Getting events and moving window analysis works", { + + # 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") +}) diff --git a/tests/testthat/test_TOXSWA_cwa.R b/tests/testthat/test_TOXSWA_cwa.R deleted file mode 100644 index 7c9a73c..0000000 --- a/tests/testthat/test_TOXSWA_cwa.R +++ /dev/null @@ -1,56 +0,0 @@ -library(pfm) -context("Read and analyse TOXSWA cwa files") - -zipfile_test = system.file("testdata/SwashProjects.zip", package = "pfm") -basedir_test = "SwashProjects/project_H_sw/TOXSWA" - -H_sw_D4_pond <- read.TOXSWA_cwa("00001p_pa.cwa", - basedir = basedir_test, - zipfile = zipfile_test) - -H_sw_R1_stream <- read.TOXSWA_cwa("00003s_pa.cwa", - basedir = basedir_test, - zipfile = zipfile_test) - -basedir_test_2 = "SwashProjects/Project_1/TOXSWA" - -EXSW2_R1_stream <- read.TOXSWA_cwa("3.out", - basedir = basedir_test_2, - zipfile = zipfile_test) - - - -test_that("TOXSWA cwa file is correctly read and printed", { - - # This was the setting when printing the output into text files - options(width = 100) - - # Most content of the R6 object is at least partially printed - - H_sw_D4_pond_printed <- capture.output(print(H_sw_D4_pond)) - - expect_equal(H_sw_D4_pond_printed, readLines("H_sw_D4_pond_printed.txt")) - - H_sw_R1_stream_printed <- capture.output(print(H_sw_R1_stream)) - expect_equal(H_sw_R1_stream_printed, readLines("H_sw_R1_stream_printed.txt")) - - # The basedir is not printed, therefore tested separately - expect_equal(H_sw_D4_pond$basedir, basedir_test) - - EXSW2_R1_stream_printed <- capture.output(print(EXSW2_R1_stream)) - expect_equal(EXSW2_R1_stream_printed, readLines("EXSW2_R1_stream_printed.txt")) - - # The basedir is not printed, therefore tested separately - expect_equal(H_sw_D4_pond$basedir, basedir_test) -}) - -test_that("Getting events and moving window analysis works", { - - # 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