From 66a15549122c7e5f7aebaaab71f63ac6f97291ae Mon Sep 17 00:00:00 2001 From: MartinRoth Date: Wed, 21 Jun 2017 14:00:00 +0200 Subject: [PATCH] Use line_length_linter(90) --- R/Helpers.R | 5 +- R/MakkinkEvaporation.R | 4 +- R/PrecipitationTransformation.R | 51 ++++++---- R/RadiationTransformation.R | 6 +- R/TemperatureTransformation.R | 28 ++++-- inst/.lintr | 2 +- tests/testthat/test-evaporation.R | 3 +- tests/testthat/test-precipitation_centr.R | 38 +++++--- tests/testthat/test-precipitation_lower.R | 31 ++++--- tests/testthat/test-precipitation_upper.R | 27 ++++-- tests/testthat/test-readInput.R | 33 ++++--- tests/testthat/test-temperature.R | 108 ++++++++++++++-------- 12 files changed, 217 insertions(+), 119 deletions(-) diff --git a/R/Helpers.R b/R/Helpers.R index cd2095c..68f97eb 100644 --- a/R/Helpers.R +++ b/R/Helpers.R @@ -20,11 +20,12 @@ ReadInput <- function(var, ifile) { } flog.info("Reading reference data, file={%s}", ifile) - H.comments <- scan(ifile, character(0), sep = "\n", quiet = TRUE) # select lines with "#" from reference file + # select lines with "#" from reference file + H.comments <- scan(ifile, character(0), sep = "\n", quiet = TRUE) flog.debug("Scanning of the reference data returned n={%i} lines.", length(H.comments)) H.comments <- H.comments[grep("#", H.comments)] # (only necessary for output file) - obs <- read.table(ifile, header = F) # read reference data (header wordt niet apart ingelezen) + obs <- read.table(ifile, header = F) # read reference data header <- obs[which(obs[, 1] == 0), ] # header met stations meta-data etc. header[, 1] <- paste0(rep(0, 8), collapse = "") # "00000000" diff --git a/R/MakkinkEvaporation.R b/R/MakkinkEvaporation.R index 25adc37..dd33ed0 100644 --- a/R/MakkinkEvaporation.R +++ b/R/MakkinkEvaporation.R @@ -4,8 +4,8 @@ makkink <- function(Tg, Q) { rho <- 1000 # water mass density [kg/m3] vps <- 6.107 * 10^((7.5*Tg)/(237.3+Tg)) # saturated vapor pressure [hPa] #nolint delta <- ((7.5*237.3)/(237.3+Tg)^2) * log(10) * vps # vps gradient [hPa/K] #nolint - gamma <- PsychrometricConstant(Tg) #0.646 + 0.0006*Tg # psychrometric constant [hPa/K] - lambda <- WaterVaporizationEnthalpy(Tg) #1000 * (2501-2.38*Tg) # Enthalpy of vaporization [J/kg] + gamma <- PsychrometricConstant(Tg) # psychrometric constant [hPa/K] + lambda <- WaterVaporizationEnthalpy(Tg) # Enthalpy of vaporization [J/kg] evmk <- 1000*Q * 1000*0.65*delta / ((delta+gamma)*rho*lambda) # [mm/day] #nolint return(evmk) } diff --git a/R/PrecipitationTransformation.R b/R/PrecipitationTransformation.R index 2382e61..9b94047 100644 --- a/R/PrecipitationTransformation.R +++ b/R/PrecipitationTransformation.R @@ -148,28 +148,36 @@ WetDryDays <- function(fut, wdf, th, mm) { if (dwet > 0) { # select target values - step <- length(Xw) / dwet # step size to step through sorted step - target.values <- Xw[round(((1:dwet) - 0.5) * step)] # determine target.values for month #nolint + # step size to step through sorted step + step <- length(Xw) / dwet + # determine target.values for month + target.values <- Xw[round( ( (1:dwet) - 0.5) * step)] # (homogeneously selected from sorted subset) # select days to wet - preceding.wet <- cumsum(Xm >= th) + step / 2 # cumulative number of preceding wet days in month - add <- vector() # vector with days that should be wetted + # cumulative number of preceding wet days in month + preceding.wet <- cumsum(Xm >= th) + step / 2 + add <- vector() # vector with days that should be wetted for (id in 1:dwet) { + # select 'first' 'dry' day that succeeds a wet' day, + # for which exceeds the size + # and add this day(id) to vector add <- c(add, - which(Xm < th & # select 'first' 'dry' day that succeeds a wet' day, - X1m >= th & # for which exceeds the size - preceding.wet >= step)[1]) # and add this day(id) to vector + which(Xm < th & + X1m >= th & + preceding.wet >= step)[1]) if (is.na(add[id])) { add <- add[-id] } else { - preceding.wet <- preceding.wet - step # and decrease vector with + # and decrease vector with + preceding.wet <- preceding.wet - step preceding.wet[1:add[id]] <- 0 } } # Finally, target.values are assigned to selected days - # on the basis of the rank order of the precipitation amount of the preceding wet day + # on the basis of the rank order of the precipitation amount of the + # preceding wet day Y[rows[add]] <- target.values[rank(X1m[add], ties.method = "first")] } # dfwet > 0 @@ -184,9 +192,12 @@ CalculateClimatology <- function(obs, deltas, mm, th) { flog.debug("Calculate climatology") - # qq1 <- 0.99 # quantile of wet-day amounts that is used to estimate transformation coefficients - # qq2 <- 0.90 # quantile of wet-day amounts that is used to estimate qq1 (robustly) - # national median of monthly ratios between qq1 and qq2 for 240 precipitation stations + # qq1 <- 0.99 # quantile of wet-day amounts that is used to estimate + # transformation coefficients + # qq2 <- 0.90 # quantile of wet-day amounts that is used to estimate qq1 + # (robustly) + # national median of monthly ratios between qq1 and qq2 for 240 + # precipitation stations ratio <- c(2.22, 2.271, 2.366, @@ -205,10 +216,12 @@ CalculateClimatology <- function(obs, deltas, mm, th) { # mean (mean.obs), # # wet-day mean (mwet.obs), # wet-day 99th percentile (q1.obs) - wdf.obs <- as.matrix(aggregate(obs, by=list(mm), function(x) mean( x>=th )))[, -1, drop = FALSE] - mean.obs <- as.matrix(aggregate(obs, by=list(mm), function(x) mean(x )))[, -1, drop = FALSE] - #mwet.obs <- as.matrix(aggregate(obs, by=list(mm), function(x) mean(x[x>=th] )))[, -1, drop = FALSE] - q2.obs <- as.matrix(aggregate(obs, by=list(mm), function(x) quantile(x[x>=th], 0.90)))[, -1, drop = FALSE] + wdf.obs <- as.matrix(aggregate(obs, by=list(mm), + function(x) mean( x>=th )))[, -1, drop = FALSE] + mean.obs <- as.matrix(aggregate(obs, by=list(mm), + function(x) mean(x )))[, -1, drop = FALSE] + q2.obs <- as.matrix(aggregate(obs, by=list(mm), + function(x) quantile(x[x>=th], 0.90)))[, -1, drop = FALSE] q1.obs <- q2.obs*ratio # apply deltas to observed climatology to obtain future climatology @@ -239,7 +252,8 @@ TransformWetDayAmounts <- function(fut, climatology, mm, th) { Y <- fut[, is] for (im in 1:12) { - wet.im <- which(im == mm & Y >= th) # identify all wet days within calendar month + # identify all wet days within calendar month + wet.im <- which(im == mm & Y >= th) Xm <- Y[wet.im] # select all wet day amounts # get climatologies for reference and future period for the month at hand @@ -257,7 +271,8 @@ TransformWetDayAmounts <- function(fut, climatology, mm, th) { # actual transformation of wet-day amounts (application of transformation function) Y[wet.im] <- ifelse(Xm < qobs, a * Xm^b, c*Xm) - Y[wet.im][which(Y[wet.im] < th)] <- th # prevent days being dried by the wet-day transformation + # prevent days being dried by the wet-day transformation + Y[wet.im][which(Y[wet.im] < th)] <- th } # END TRANSFORMATION WET-DAY AMOUNTS fut[, is] <- Y diff --git a/R/RadiationTransformation.R b/R/RadiationTransformation.R index 6269c37..f1a91bf 100644 --- a/R/RadiationTransformation.R +++ b/R/RadiationTransformation.R @@ -38,14 +38,14 @@ rsds_trans_KNMI14 <- function(obs, for (is in 1:ns) { for (im in 1:12) { - days.im <- which(mm == im) # all days within in calendar month - X <- obs[days.im, is+1] # select all obs in month of station + days.im <- which(mm == im) # all days within in calendar month + X <- obs[days.im, is+1] # select all obs in month of station # clear sky radiation for all days in month Rx <- 0.75 * ObtainAngotRadiation(obs[days.im, 1], lat[is]) # determine coefficient a for transformation function - delta <- deltas[im, 2]/100 # relative change of average in month + delta <- deltas[im, 2]/100 # relative change of average in month if (delta > 0) { a <- BoundedScaling(X, Rx, delta) } else { diff --git a/R/TemperatureTransformation.R b/R/TemperatureTransformation.R index f614d45..47db08f 100644 --- a/R/TemperatureTransformation.R +++ b/R/TemperatureTransformation.R @@ -44,8 +44,10 @@ tm_trans_KNMI14 <- function(obs, fut[, -1] <- NA # future values (filled with NA) # region information - if (!("regio" %in% colnames(deltas))) deltas$regio <- "NLD" # add column to deltas if not provided - deltas <- deltas[c("regio", "maand", "P01", "P05", "P50", "P95", "P99")] # arrange deltas + # add column to deltas if not provided + if (!("regio" %in% colnames(deltas))) deltas$regio <- "NLD" + # arrange deltas + deltas <- deltas[c("regio", "maand", "P01", "P05", "P50", "P95", "P99")] # TRANSFORMATION # apply transformation per station / time series @@ -57,25 +59,30 @@ tm_trans_KNMI14 <- function(obs, percentile.changes <- as.numeric( deltas[which(deltas[, 1]==regio & deltas[, 2]==im), -1:-2] ) - days.im <- which(mm == im) # all days within in calendar month - X <- obs[days.im, is+1] # select all obs in month of station + days.im <- which(mm == im) # all days within in calendar month + X <- obs[days.im, is+1] # select all obs in month of station Y <- rep(NA, length(X)) - X.percentiles <- as.numeric(quantile(X, c(1, 5, 50, 95, 99)/100, na.rm=T)) # observed percentiles - Y.percentiles <- X.percentiles + percentile.changes # estimation of future percentiles + # observed percentiles + X.percentiles <- as.numeric(quantile(X, c(1, 5, 50, 95, 99)/100, na.rm=T)) + # estimation of future percentiles + Y.percentiles <- X.percentiles + percentile.changes # linear transformation: for intervals X= X.percentiles[ip-1] & X < X.percentiles[ip]) # id's all values within analysed percentile range + # id's all values within analysed percentile range + x.ids <- which(X >= X.percentiles[ip-1] & X < X.percentiles[ip]) a <- (Y.percentiles[ip] - Y.percentiles[ip-1]) / (X.percentiles[ip] - X.percentiles[ip-1]) b <- Y.percentiles[ip] - a * X.percentiles[ip] @@ -83,7 +90,8 @@ tm_trans_KNMI14 <- function(obs, } ip <- length(X.percentiles) - x.ids <- which(X >= X.percentiles[ip-1]) # id's for all values larger than second largest + # id's for all values larger than second largest + x.ids <- which(X >= X.percentiles[ip-1]) a <- (Y.percentiles[ip] - Y.percentiles[ip-1]) / (X.percentiles[ip] - X.percentiles[ip-1]) b <- Y.percentiles[ip] - a * X.percentiles[ip] diff --git a/inst/.lintr b/inst/.lintr index 4e4c07e..1491a75 100644 --- a/inst/.lintr +++ b/inst/.lintr @@ -1,6 +1,6 @@ linters: with_defaults( infix_spaces_linter = NULL, # 569 - line_length_linter = NULL, # 189 + line_length_linter(90), # 189 snake_case_linter = NULL, # should be incorporated camel_case_linter = NULL, # 60 commented_code_linter = NULL, # 52 diff --git a/tests/testthat/test-evaporation.R b/tests/testthat/test-evaporation.R index 28203d9..02c1bbe 100644 --- a/tests/testthat/test-evaporation.R +++ b/tests/testthat/test-evaporation.R @@ -54,7 +54,8 @@ test_that("2030 decadal prediction", { regions = regions, rounding = TRUE) - expect_equal_to_reference(tmp, "regressionOutput/evaporation/KNMI14___2030_evmk_rounded.rds") + expect_equal_to_reference(tmp, + "regressionOutput/evaporation/KNMI14___2030_evmk_rounded.rds") }) test_that("Scenario WL", { diff --git a/tests/testthat/test-precipitation_centr.R b/tests/testthat/test-precipitation_centr.R index b91f971..355434d 100644 --- a/tests/testthat/test-precipitation_centr.R +++ b/tests/testthat/test-precipitation_centr.R @@ -9,8 +9,8 @@ context("rr transformation (centr) - Entire station set") subscenario <- "centr" input <- ReadInput("rr", system.file("refdata", - "KNMI14____ref_rrcentr___19810101-20101231_v3.2.txt", - package="knmitransformer")) + "KNMI14____ref_rrcentr___19810101-20101231_v3.2.txt", + package="knmitransformer")) ofile <- NA rounding <- FALSE @@ -18,14 +18,15 @@ test_that("2030 decadal prediction", { scenario <- "GL" horizon <- 2030 tmp <- TransformPrecip(input=system.file("refdata", - "KNMI14____ref_rrcentr___19810101-20101231_v3.2.txt", - package="knmitransformer"), + "KNMI14____ref_rrcentr___19810101-20101231_v3.2.txt", + package="knmitransformer"), ofile="tmp.txt", scenario=scenario, horizon = horizon, subscenario=subscenario, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/precipitation/KNMI14___2030_rr_centr.rds") + expect_equal_to_reference(tmp, + "regressionOutput/precipitation/KNMI14___2030_rr_centr.rds") tmp <- TransformPrecip(input=input, ofile=ofile, @@ -33,7 +34,8 @@ test_that("2030 decadal prediction", { horizon = horizon, subscenario=subscenario, rounding = TRUE) - expect_equal_to_reference(tmp, "regressionOutput/precipitation/KNMI14___2030_rr_centr_rounded.rds") + expect_equal_to_reference(tmp, + "regressionOutput/precipitation/KNMI14___2030_rr_centr_rounded.rds") }) test_that("Scenario GL", { @@ -45,7 +47,8 @@ test_that("Scenario GL", { horizon = horizon, subscenario=subscenario, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/precipitation/KNMI14_GL_2050_rr_centr.rds") + expect_equal_to_reference(tmp, + "regressionOutput/precipitation/KNMI14_GL_2050_rr_centr.rds") horizon <- 2085 tmp <- TransformPrecip(input=input, @@ -54,7 +57,8 @@ test_that("Scenario GL", { horizon = horizon, subscenario=subscenario, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/precipitation/KNMI14_GL_2085_rr_centr.rds") + expect_equal_to_reference(tmp, + "regressionOutput/precipitation/KNMI14_GL_2085_rr_centr.rds") }) test_that("Scenario GH", { @@ -66,7 +70,8 @@ test_that("Scenario GH", { horizon = horizon, subscenario=subscenario, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/precipitation/KNMI14_GH_2050_rr_centr.rds") + expect_equal_to_reference(tmp, + "regressionOutput/precipitation/KNMI14_GH_2050_rr_centr.rds") horizon <- 2085 tmp <- TransformPrecip(input=input, @@ -75,7 +80,8 @@ test_that("Scenario GH", { horizon = horizon, subscenario=subscenario, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/precipitation/KNMI14_GH_2085_rr_centr.rds") + expect_equal_to_reference(tmp, + "regressionOutput/precipitation/KNMI14_GH_2085_rr_centr.rds") }) test_that("Scenario WH", { @@ -87,7 +93,8 @@ test_that("Scenario WH", { horizon = horizon, subscenario=subscenario, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/precipitation/KNMI14_WH_2050_rr_centr.rds") + expect_equal_to_reference(tmp, + "regressionOutput/precipitation/KNMI14_WH_2050_rr_centr.rds") horizon <- 2085 tmp <- TransformPrecip(input=input, @@ -96,7 +103,8 @@ test_that("Scenario WH", { horizon = horizon, subscenario=subscenario, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/precipitation/KNMI14_WH_2085_rr_centr.rds") + expect_equal_to_reference(tmp, + "regressionOutput/precipitation/KNMI14_WH_2085_rr_centr.rds") }) test_that("Scenario WL", { @@ -108,7 +116,8 @@ test_that("Scenario WL", { horizon = horizon, subscenario=subscenario, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/precipitation/KNMI14_WL_2050_rr_centr.rds") + expect_equal_to_reference(tmp, + "regressionOutput/precipitation/KNMI14_WL_2050_rr_centr.rds") horizon <- 2085 tmp <- TransformPrecip(input=input, @@ -117,5 +126,6 @@ test_that("Scenario WL", { horizon = horizon, subscenario=subscenario, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/precipitation/KNMI14_WL_2085_rr_centr.rds") + expect_equal_to_reference(tmp, + "regressionOutput/precipitation/KNMI14_WL_2085_rr_centr.rds") }) diff --git a/tests/testthat/test-precipitation_lower.R b/tests/testthat/test-precipitation_lower.R index 4b405ea..311ea75 100644 --- a/tests/testthat/test-precipitation_lower.R +++ b/tests/testthat/test-precipitation_lower.R @@ -9,8 +9,8 @@ context("rr transformation (lower) - Entire station set") subscenario <- "lower" input <- ReadInput("rr", system.file("refdata", - "KNMI14____ref_rrcentr___19810101-20101231_v3.2.txt", - package="knmitransformer")) + "KNMI14____ref_rrcentr___19810101-20101231_v3.2.txt", + package="knmitransformer")) ofile <- NA rounding <- FALSE @@ -24,7 +24,8 @@ test_that("2030 decadal prediction", { horizon = horizon, subscenario=subscenario, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/precipitation/KNMI14___2030_rr_lower.rds") + expect_equal_to_reference(tmp, + "regressionOutput/precipitation/KNMI14___2030_rr_lower.rds") }) test_that("Scenario GL", { @@ -36,7 +37,8 @@ test_that("Scenario GL", { horizon = horizon, subscenario=subscenario, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/precipitation/KNMI14_GL_2050_rr_lower.rds") + expect_equal_to_reference(tmp, + "regressionOutput/precipitation/KNMI14_GL_2050_rr_lower.rds") horizon <- 2085 tmp <- TransformPrecip(input=input, @@ -45,7 +47,8 @@ test_that("Scenario GL", { horizon = horizon, subscenario=subscenario, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/precipitation/KNMI14_GL_2085_rr_lower.rds") + expect_equal_to_reference(tmp, + "regressionOutput/precipitation/KNMI14_GL_2085_rr_lower.rds") }) test_that("Scenario GH", { @@ -57,7 +60,8 @@ test_that("Scenario GH", { horizon = horizon, subscenario=subscenario, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/precipitation/KNMI14_GH_2050_rr_lower.rds") + expect_equal_to_reference(tmp, + "regressionOutput/precipitation/KNMI14_GH_2050_rr_lower.rds") horizon <- 2085 tmp <- TransformPrecip(input=input, @@ -66,7 +70,8 @@ test_that("Scenario GH", { horizon = horizon, subscenario=subscenario, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/precipitation/KNMI14_GH_2085_rr_lower.rds") + expect_equal_to_reference(tmp, + "regressionOutput/precipitation/KNMI14_GH_2085_rr_lower.rds") }) test_that("Scenario WH", { @@ -78,7 +83,8 @@ test_that("Scenario WH", { horizon = horizon, subscenario=subscenario, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/precipitation/KNMI14_WH_2050_rr_lower.rds") + expect_equal_to_reference(tmp, + "regressionOutput/precipitation/KNMI14_WH_2050_rr_lower.rds") horizon <- 2085 tmp <- TransformPrecip(input=input, @@ -87,7 +93,8 @@ test_that("Scenario WH", { horizon = horizon, subscenario=subscenario, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/precipitation/KNMI14_WH_2085_rr_lower.rds") + expect_equal_to_reference(tmp, + "regressionOutput/precipitation/KNMI14_WH_2085_rr_lower.rds") }) test_that("Scenario WL", { @@ -99,7 +106,8 @@ test_that("Scenario WL", { horizon = horizon, subscenario=subscenario, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/precipitation/KNMI14_WL_2050_rr_lower.rds") + expect_equal_to_reference(tmp, + "regressionOutput/precipitation/KNMI14_WL_2050_rr_lower.rds") horizon <- 2085 tmp <- TransformPrecip(input=input, @@ -108,5 +116,6 @@ test_that("Scenario WL", { horizon = horizon, subscenario=subscenario, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/precipitation/KNMI14_WL_2085_rr_lower.rds") + expect_equal_to_reference(tmp, + "regressionOutput/precipitation/KNMI14_WL_2085_rr_lower.rds") }) diff --git a/tests/testthat/test-precipitation_upper.R b/tests/testthat/test-precipitation_upper.R index 3d4e482..afce918 100644 --- a/tests/testthat/test-precipitation_upper.R +++ b/tests/testthat/test-precipitation_upper.R @@ -24,7 +24,8 @@ test_that("2030 decadal prediction", { horizon = horizon, subscenario=subscenario, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/precipitation/KNMI14___2030_rr_upper.rds") + expect_equal_to_reference(tmp, + "regressionOutput/precipitation/KNMI14___2030_rr_upper.rds") }) test_that("Scenario GL", { @@ -36,7 +37,8 @@ test_that("Scenario GL", { horizon = horizon, subscenario=subscenario, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/precipitation/KNMI14_GL_2050_rr_upper.rds") + expect_equal_to_reference(tmp, + "regressionOutput/precipitation/KNMI14_GL_2050_rr_upper.rds") horizon <- 2085 tmp <- TransformPrecip(input=input, @@ -45,7 +47,8 @@ test_that("Scenario GL", { horizon = horizon, subscenario=subscenario, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/precipitation/KNMI14_GL_2085_rr_upper.rds") + expect_equal_to_reference(tmp, + "regressionOutput/precipitation/KNMI14_GL_2085_rr_upper.rds") }) @@ -58,7 +61,8 @@ test_that("Scenario GH", { horizon = horizon, subscenario=subscenario, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/precipitation/KNMI14_GH_2050_rr_upper.rds") + expect_equal_to_reference(tmp, + "regressionOutput/precipitation/KNMI14_GH_2050_rr_upper.rds") horizon <- 2085 tmp <- TransformPrecip(input=input, @@ -67,7 +71,8 @@ test_that("Scenario GH", { horizon = horizon, subscenario=subscenario, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/precipitation/KNMI14_GH_2085_rr_upper.rds") + expect_equal_to_reference(tmp, + "regressionOutput/precipitation/KNMI14_GH_2085_rr_upper.rds") }) test_that("Scenario WH", { @@ -79,7 +84,8 @@ test_that("Scenario WH", { horizon = horizon, subscenario=subscenario, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/precipitation/KNMI14_WH_2050_rr_upper.rds") + expect_equal_to_reference(tmp, + "regressionOutput/precipitation/KNMI14_WH_2050_rr_upper.rds") horizon <- 2085 tmp <- TransformPrecip(input=input, @@ -88,7 +94,8 @@ test_that("Scenario WH", { horizon = horizon, subscenario=subscenario, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/precipitation/KNMI14_WH_2085_rr_upper.rds") + expect_equal_to_reference(tmp, + "regressionOutput/precipitation/KNMI14_WH_2085_rr_upper.rds") }) test_that("Scenario WL", { @@ -100,7 +107,8 @@ test_that("Scenario WL", { horizon = horizon, subscenario=subscenario, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/precipitation/KNMI14_WL_2050_rr_upper.rds") + expect_equal_to_reference(tmp, + "regressionOutput/precipitation/KNMI14_WL_2050_rr_upper.rds") horizon <- 2085 tmp <- TransformPrecip(input=input, @@ -109,5 +117,6 @@ test_that("Scenario WL", { horizon = horizon, subscenario=subscenario, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/precipitation/KNMI14_WL_2085_rr_upper.rds") + expect_equal_to_reference(tmp, + "regressionOutput/precipitation/KNMI14_WL_2085_rr_upper.rds") }) diff --git a/tests/testthat/test-readInput.R b/tests/testthat/test-readInput.R index c68598d..36513d4 100644 --- a/tests/testthat/test-readInput.R +++ b/tests/testthat/test-readInput.R @@ -4,7 +4,7 @@ context("readInput") ## TODO: Add more tests test_that("read input works correctly", { - expect_error(ReadInput("rsds", "./regressionInput/KNMI14____ref_rsds___19810101-20101231_wrong.txt"), + expect_error(ReadInput("rsds", "./regressionInput/KNMI14____ref_rsds___19810101-20101231_wrong.txt"), #nolint "Check input file: Latitude NOT (or not properly) provided.", fixed = TRUE) expect_error(ReadInput("rr", "blah"), "Input file does not exist.") @@ -20,7 +20,8 @@ filename <- system.file("exampledata", package="knmitransformer") tgData <- fread(filename, skip = 6) -tgData[, date := format(as.Date(paste(V1, V2, V3, sep = "-"), format = "%Y-%m-%d"), format = "%Y%m%d")] +tgData[, date := format(as.Date(paste(V1, V2, V3, sep = "-"), + format = "%Y-%m-%d"), format = "%Y%m%d")] tgData <- tgData[, .(date, tg = V4, year = V1)] @@ -34,19 +35,25 @@ test_that("Input assertion", { "Data should contain only column date and column values") expect_error(CreateKnmiTFInput(tgData[, .(date, tg)], 1, lat, lon, comment), "Data should contain only column date and column values") - expect_error(CreateKnmiTFInput(tgData[, .(date, values = tg)], 1, lat, lon, comment), + expect_error(CreateKnmiTFInput(tgData[, .(date, values = tg)], 1, lat, lon, + comment), "Date col should contain date in integer format 'yyyymmdd'") tgData[, date := as.integer(date)] - expect_error(CreateKnmiTFInput(tgData[year == 2009, .(date, values = tg)], 1, lat, lon, comment), - "The date column should contain the full period from 19810101 to 20101231") - expect_error(CreateKnmiTFInput(tgData[, .(date, values = tg)], 1, lat, lon, comment), - "Currently the period is limited to the official thirty year period 1981-2010") + expect_error(CreateKnmiTFInput(tgData[year == 2009, .(date, values = tg)], 1, + lat, lon, comment), + "The date column should contain the full period from 19810101 to 20101231") + expect_error(CreateKnmiTFInput(tgData[, .(date, values = tg)], 1, lat, lon, + comment), + "Currently the period is limited to the official thirty year period 1981-2010") tgData <- tgData[year %in% 1981 : 2010] - expect_error(CreateKnmiTFInput(tgData[, .(date, values = tg)], 1, rep(lat, 2), lon, comment), + expect_error(CreateKnmiTFInput(tgData[, .(date, values = tg)], 1, rep(lat, 2), + lon, comment), "lat and lon should be of length 1") - expect_error(CreateKnmiTFInput(tgData[, .(date, values = as.character(tg))], 1, rep(lat, 2), lon, comment), + expect_error(CreateKnmiTFInput(tgData[, .(date, values = as.character(tg))], + 1, rep(lat, 2), lon, comment), "Values should be of class numeric") - expect_equal_to_reference(CreateKnmiTFInput(tgData[, .(date, values = tg)], 1, lat, lon, comment), + expect_equal_to_reference(CreateKnmiTFInput(tgData[, .(date, values = tg)], 1, + lat, lon, comment), file = "regressionOutput/additional/CreateInput.rds") }) @@ -60,8 +67,10 @@ test_that("Recognizes official files", { test_that("CheckRegions", { expect_error(CheckRegions("AB", 1), "Regions have to match the KNMI defined regions") - expect_error(CheckRegions("MON", 2), "regions should be `NLD` or vector of length equal to number of stations") + expect_error(CheckRegions("MON", 2), + "regions should be `NLD` or vector of length equal to number of stations") expect_equal(CheckRegions("NLD", 2), rep("NLD", 2)) expect_equal(CheckRegions(c("NLD", "MON"), 2), c("NLD", "MON")) - expect_error(CheckRegions(c("NLD", "MON"), 3), "regions should be `NLD` or vector of length equal to number of stations") + expect_error(CheckRegions(c("NLD", "MON"), 3), + "regions should be `NLD` or vector of length equal to number of stations") }) diff --git a/tests/testthat/test-temperature.R b/tests/testthat/test-temperature.R index 4772404..2c3c176 100644 --- a/tests/testthat/test-temperature.R +++ b/tests/testthat/test-temperature.R @@ -35,7 +35,8 @@ test_that("2030 decadal prediction", { var=var, regions=regions, rounding=TRUE) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14___2030_tg_rounded.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14___2030_tg_rounded.rds") }) test_that("Scenario WL", { @@ -49,7 +50,8 @@ test_that("Scenario WL", { regions=regions, rounding=rounding ) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_WL_2050_tg.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_WL_2050_tg.rds") # Regression test based only on the smaller subset used also for the other # scenarios - on the web there is an extended version @@ -62,7 +64,8 @@ test_that("Scenario WL", { regions=regions, rounding=rounding ) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_WL_2085_tg.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_WL_2085_tg.rds") }) test_that("Scenario WH", { @@ -76,7 +79,8 @@ test_that("Scenario WH", { regions=regions, rounding=rounding ) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_WH_2050_tg.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_WH_2050_tg.rds") horizon <- 2085 tmp <- TransformTemp(input=input, @@ -87,7 +91,8 @@ test_that("Scenario WH", { regions=regions, rounding=rounding ) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_WH_2085_tg.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_WH_2085_tg.rds") }) test_that("Scenario GH", { @@ -101,7 +106,8 @@ test_that("Scenario GH", { regions=regions, rounding=rounding ) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_GH_2050_tg.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_GH_2050_tg.rds") horizon <- 2085 tmp <- TransformTemp(input=input, @@ -112,7 +118,8 @@ test_that("Scenario GH", { regions=regions, rounding=rounding ) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_GH_2085_tg.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_GH_2085_tg.rds") }) test_that("Scenario GL", { @@ -126,7 +133,8 @@ test_that("Scenario GL", { regions=regions, rounding=rounding ) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_GL_2050_tg.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_GL_2050_tg.rds") horizon <- 2085 tmp <- TransformTemp(input=input, @@ -137,7 +145,8 @@ test_that("Scenario GL", { regions=regions, rounding=rounding ) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_GL_2085_tg.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_GL_2085_tg.rds") }) # ------------------------------------------------------------------------------ @@ -160,7 +169,8 @@ test_that("2030 decadal prediction", { regions=regions, rounding=rounding ) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14___2030_tx.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14___2030_tx.rds") }) test_that("Scenario WL", { @@ -174,7 +184,8 @@ test_that("Scenario WL", { regions=regions, rounding=rounding ) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_WL_2050_tx.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_WL_2050_tx.rds") # Regression test based only on the smaller subset used also for the other # scenarios - on the web there is an extended version @@ -187,7 +198,8 @@ test_that("Scenario WL", { regions=regions, rounding=rounding ) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_WL_2085_tx.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_WL_2085_tx.rds") }) test_that("Scenario WH", { @@ -201,7 +213,8 @@ test_that("Scenario WH", { regions=regions, rounding=rounding ) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_WH_2050_tx.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_WH_2050_tx.rds") horizon <- 2085 tmp <- TransformTemp(input=input, @@ -212,7 +225,8 @@ test_that("Scenario WH", { regions=regions, rounding=rounding ) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_WH_2085_tx.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_WH_2085_tx.rds") }) test_that("Scenario GH", { @@ -226,7 +240,8 @@ test_that("Scenario GH", { regions=regions, rounding=rounding ) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_GH_2050_tx.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_GH_2050_tx.rds") horizon <- 2085 tmp <- TransformTemp(input=input, @@ -237,7 +252,8 @@ test_that("Scenario GH", { regions=regions, rounding=rounding ) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_GH_2085_tx.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_GH_2085_tx.rds") }) test_that("Scenario GL", { @@ -251,7 +267,8 @@ test_that("Scenario GL", { regions=regions, rounding=rounding ) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_GL_2050_tx.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_GL_2050_tx.rds") horizon <- 2085 tmp <- TransformTemp(input=input, @@ -262,7 +279,8 @@ test_that("Scenario GL", { regions=regions, rounding=rounding ) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_GL_2085_tx.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_GL_2085_tx.rds") }) context("tn transformation - Entire station set") @@ -284,7 +302,8 @@ test_that("2030 decadal prediction", { regions=regions, rounding=rounding ) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14___2030_tn.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14___2030_tn.rds") }) test_that("Scenario WL", { @@ -298,7 +317,8 @@ test_that("Scenario WL", { regions=regions, rounding=rounding ) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_WL_2050_tn.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_WL_2050_tn.rds") # Regression test based only on the smaller subset used also for the other # scenarios - on the web there is an extended version @@ -311,7 +331,8 @@ test_that("Scenario WL", { regions=regions, rounding=rounding ) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_WL_2085_tn.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_WL_2085_tn.rds") }) test_that("Scenario WH", { @@ -325,7 +346,8 @@ test_that("Scenario WH", { regions=regions, rounding=rounding ) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_WH_2050_tn.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_WH_2050_tn.rds") horizon <- 2085 tmp <- TransformTemp(input=input, @@ -336,7 +358,8 @@ test_that("Scenario WH", { regions=regions, rounding=rounding ) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_WH_2085_tn.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_WH_2085_tn.rds") }) test_that("Scenario GH", { @@ -350,7 +373,8 @@ test_that("Scenario GH", { regions=regions, rounding=rounding ) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_GH_2050_tn.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_GH_2050_tn.rds") horizon <- 2085 tmp <- TransformTemp(input=input, @@ -361,7 +385,8 @@ test_that("Scenario GH", { regions=regions, rounding=rounding ) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_GH_2085_tn.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_GH_2085_tn.rds") }) test_that("Scenario GL", { @@ -375,7 +400,8 @@ test_that("Scenario GL", { regions=regions, rounding=rounding ) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_GL_2050_tn.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_GL_2050_tn.rds") horizon <- 2085 tmp <- TransformTemp(input=input, @@ -386,7 +412,8 @@ test_that("Scenario GL", { regions=regions, rounding=rounding ) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_GL_2085_tn.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_GL_2085_tn.rds") }) @@ -432,7 +459,8 @@ test_that("Procedure works for one station as well", { var=var, regions=regions, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14___2030_tg___DeBilt.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14___2030_tg___DeBilt.rds") }) test_that("Temperature regression test (with actual data) WH", { @@ -447,7 +475,8 @@ test_that("Temperature regression test (with actual data) WH", { var=var, regions=regions, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_WH_2050_tg___DeBilt.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_WH_2050_tg___DeBilt.rds") horizon <- 2085 tmp <- TransformTemp(input=input, @@ -457,7 +486,8 @@ test_that("Temperature regression test (with actual data) WH", { var=var, regions=regions, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_WH_2085_tg___DeBilt.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_WH_2085_tg___DeBilt.rds") }) test_that("Temperature regression test (with actual data) GL", { @@ -471,7 +501,8 @@ test_that("Temperature regression test (with actual data) GL", { var=var, regions=regions, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_GL_2050_tg___DeBilt.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_GL_2050_tg___DeBilt.rds") horizon <- 2085 tmp <- TransformTemp(input=input, @@ -481,7 +512,8 @@ test_that("Temperature regression test (with actual data) GL", { var=var, regions=regions, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_GL_2085_tg___DeBilt.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_GL_2085_tg___DeBilt.rds") }) test_that("Temperature regression test (with actual data) GH", { @@ -495,7 +527,8 @@ test_that("Temperature regression test (with actual data) GH", { var=var, regions=regions, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_GH_2050_tg___DeBilt.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_GH_2050_tg___DeBilt.rds") horizon <- 2085 tmp <- TransformTemp(input=input, @@ -505,7 +538,8 @@ test_that("Temperature regression test (with actual data) GH", { var=var, regions=regions, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_GH_2085_tg___DeBilt.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_GH_2085_tg___DeBilt.rds") }) test_that("Temperature regression test (with actual data) WL", { @@ -519,7 +553,8 @@ test_that("Temperature regression test (with actual data) WL", { var=var, regions=regions, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_WL_2050_tg___DeBilt.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_WL_2050_tg___DeBilt.rds") horizon <- 2085 tmp <- TransformTemp(input=input, @@ -529,5 +564,6 @@ test_that("Temperature regression test (with actual data) WL", { var=var, regions=regions, rounding = rounding) - expect_equal_to_reference(tmp, "regressionOutput/temperature/KNMI14_WL_2085_tg___DeBilt.rds") + expect_equal_to_reference(tmp, + "regressionOutput/temperature/KNMI14_WL_2085_tg___DeBilt.rds") })