Skip to content

Commit

Permalink
dbW_fixWeather() now handles data objects with all missing values
Browse files Browse the repository at this point in the history
- objects with all missing values are now passed through (instead of causing a cryptic error)
- tests now check dbW_fixWeather() for "normal" use and for the case with all missing values
  • Loading branch information
dschlaep committed Aug 28, 2024
1 parent c3ddd49 commit ae8876d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# rSOILWAT2 v6.1.1-9000
* This version produces the same output as the previous version.

## Bugfix
* `dbW_fixWeather()` now handles data objects with all missing values.


# rSOILWAT2 v6.1.0
Expand Down
16 changes: 8 additions & 8 deletions R/swWeatherGenerator.R
Original file line number Diff line number Diff line change
Expand Up @@ -1717,16 +1717,16 @@ dbW_fixWeather <- function(
tmp <- which(rowSums(!is_miss1) > 0L)
ids <- tmp[c(1L, length(tmp))]

ids_startend <-
ids_startend <- if (length(tmp) > 0L) {
# before start
(weatherData1[["Year"]] < weatherData1[ids[[1L]], "Year"]) |
(weatherData1[["Year"]] == weatherData1[ids[[1L]], "Year"] &
weatherData1[["DOY"]] < weatherData1[ids[[1L]], "DOY"]) |
# after end
(weatherData1[["Year"]] == weatherData1[ids[[2L]], "Year"] &
weatherData1[["DOY"]] > weatherData1[ids[[2L]], "DOY"]) |
(weatherData1[["Year"]] > weatherData1[ids[[2L]], "Year"])

(weatherData1[["Year"]] == weatherData1[ids[[1L]], "Year"] &
weatherData1[["DOY"]] < weatherData1[ids[[1L]], "DOY"]) |
# after end
(weatherData1[["Year"]] == weatherData1[ids[[2L]], "Year"] &
weatherData1[["DOY"]] > weatherData1[ids[[2L]], "DOY"]) |
(weatherData1[["Year"]] > weatherData1[ids[[2L]], "Year"])
}


#--- Interpolate short missing runs
Expand Down
37 changes: 37 additions & 0 deletions tests/testthat/test_WeatherGenerator_functionality.R
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,40 @@ test_that("Weather generator (integration tests): compare input/output", {
n = 5L
)
})


test_that("Weather: fix dataset", {
#--- Check: fixedValue, interpolatedLienar, substituteData
x0 <- x <- dbW_weatherData_to_dataframe(rSOILWAT2::weatherData)

tmp <- x[, "Year"] == 1981
ids_to_interp <- tmp & x[, "DOY"] >= 144 & x[, "DOY"] <= 145
x[ids_to_interp, -(1:2)] <- NA

tmp <- x[, "Year"] == 1980
ids_to_sub <- tmp & x[, "DOY"] >= 153 & x[, "DOY"] <= 244
x[ids_to_sub, -(1:2)] <- NA

xf <- dbW_fixWeather(x, x0, return_weatherDF = TRUE)

expect_identical(
xf[["weatherData"]][!ids_to_interp, ],
as.data.frame(x0)[!ids_to_interp, ]
)

expect_gt(sum(!is.na(xf[["meta"]])), 0L)

expect_setequal(
names(table(xf[["meta"]])),
c("fixedValue", "interpolateLinear (<= 7 days)", "substituteData")
)


#--- Check: all values missing in - all values missing out
x <- dbW_weatherData_to_dataframe(rSOILWAT2::weatherData)
x[, weather_dataColumns()] <- NA

xf <- dbW_fixWeather(x, return_weatherDF = TRUE)
expect_true(all(is.na(x[, weather_dataColumns()])))
expect_identical(sum(!is.na(xf[["meta"]])), 0L)
})

0 comments on commit ae8876d

Please sign in to comment.