Skip to content

Commit

Permalink
Feature improve weather (#244)
Browse files Browse the repository at this point in the history
- functionality to impute and 'fix' weather
- functionality to download weather and format for rSOILWAT2
  • Loading branch information
dschlaep authored Dec 4, 2023
2 parents 04870d4 + e0017bc commit 72d4bf2
Show file tree
Hide file tree
Showing 24 changed files with 2,018 additions and 360 deletions.
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export(dbW_deleteSiteData)
export(dbW_delete_duplicated_weatherData)
export(dbW_disconnectConnection)
export(dbW_estimate_WGen_coefs)
export(dbW_fixWeather)
export(dbW_generateWeather)
export(dbW_getIDs)
export(dbW_getScenarioId)
Expand All @@ -57,7 +58,9 @@ export(dbW_has_siteIDs)
export(dbW_has_sites)
export(dbW_has_weatherData)
export(dbW_have_sites_all_weatherData)
export(dbW_imputeWeather)
export(dbW_setConnection)
export(dbW_substituteWeather)
export(dbW_updateSites)
export(dbW_upgrade_to_rSOILWAT2)
export(dbW_upgrade_v1to2)
Expand Down Expand Up @@ -138,6 +141,8 @@ export(sw_dailyC4_TempVar)
export(sw_exec)
export(sw_inputData)
export(sw_inputDataFromFiles)
export(sw_meteo_obtain_DayMet)
export(sw_meteo_obtain_SCAN)
export(sw_out_flags)
export(sw_outputData)
export(sw_verbosity)
Expand All @@ -148,6 +153,7 @@ export(swrc_vwc_to_swp)
export(time_columns)
export(update_biomass)
export(update_requested_years)
export(upgrade_weatherDF)
export(upgrade_weatherHistory)
export(weatherGenerator_dataColumns)
export(weatherHistory)
Expand Down
34 changes: 34 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,38 @@
# rSOILWAT2 v6.0.4-9000
* This version produces the same output as the previous version.

## New features
* New `upgrade_weatherDF()` adds requested weather columns to a data frame
(@dschlaep).
* Improved rounding of weather functionality (@dschlaep):
* `dbW_weatherData_round()` now rounds both `"weatherList"` and
`"weatherDF"` objects; argument `"digits"` can now also be logical
(if `TRUE`, then digits takes the default value of 4) or not finite
(e.g., `NA`; not finite values return the input without rounding).
* Argument `"round"` of `dbW_dataframe_to_weatherData()` is deprecated and
changed the default value from rounding to 2 digits to no rounding (`NA`);
recommended replacement is a separate call to `dbW_weatherData_round()`.
* Argument `"digits"` of `dbW_generateWeather()` changed the default value
from rounding to 4 digits to no rounding (`NA`).
* `dbW_generateWeather()` gained `"return_weatherDF"` and now returns a
user requested weather object type (@dschlaep).
If `return_weatherDF` is `TRUE`, then the result is converted to a
data frame where columns represent weather variables; otherwise,
a list of elements of class `swWeatherData` is returned (as previously).
* New `dbW_imputeWeather()` replaces missing weather values using
using the weather generator and
using functionality by `rSW2utils::impute_df()` (@dschlaep).
* New `dbW_substituteWeather()` replaces missing weather values in one
weather data object with values from a second weather data object (@dschlaep).
* New `dbW_fixWeather()` fixes missing weather values using a sequence of
approaches including linear interpolation for short missing spells,
a fixed value for short spells of missing precipitation (optionally),
substitution from a second weather data object, and
replacement with long term daily mean values (@dschlaep).
* New family of functions `sw_meteo_obtain` that obtain (download) weather
data from external sources and prepare for use by `"rSOILWAT2"` (@dschlaep):
* New `sw_meteo_obtain_DayMet()` obtains and formats data from `"Daymet"`
* New `sw_meteo_obtain_SCAN()` obtains and formats data from `"SCAN"`


# rSOILWAT2 v6.0.3
Expand Down
2 changes: 1 addition & 1 deletion R/A_swGenericMethods.R
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ format_timestamp <- function(object) {
#' `"SWA"` added as `outkey` 8 for a new total of 30
#'
#' @examples
#' x <- sw_upgrade(rSOILWAT2::sw_exampleData, verbose = TRUE)
#' x <- sw_upgrade(rSOILWAT2::sw_exampleData, verbose = TRUE)
#'
#' @md
#' @exportMethod sw_upgrade
Expand Down
36 changes: 33 additions & 3 deletions R/D_swWeatherData.R
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,42 @@ swWeatherData <- function(...) {
do.call("new", args = c("swWeatherData", dots[dns %in% sns]))
}

#' @param weatherDF A data frame with weather variables.
#' @param template_weatherColumns A vector with requested weather variables.
#'
#' @return For [upgrade_weatherDF()]:
#' an updated `weatherDF` with requested columns.
#'
#' @examples
#' upgrade_weatherDF(
#' data.frame(DOY = 1:2, Tmax_C = runif(2), dummy = runif(2))
#' )
#'
#' @md
#' @rdname sw_upgrade
#' @export
upgrade_weatherDF <- function(
weatherDF,
template_weatherColumns = c("Year", "DOY", weather_dataColumns())
) {
template_data <- as.data.frame(
array(
dim = c(nrow(weatherDF), length(template_weatherColumns)),
dimnames = list(NULL, template_weatherColumns)
)
)

cns <- intersect(template_weatherColumns, colnames(weatherDF))
if (length(cns) < 1L) stop("Required weather variables not found.")

Check warning on line 217 in R/D_swWeatherData.R

View check run for this annotation

Codecov / codecov/patch

R/D_swWeatherData.R#L217

Added line #L217 was not covered by tests
template_data[, cns] <- weatherDF[, cns]
template_data
}

upgrade_swWeatherData <- function(data, year, template = new("swWeatherData")) {
stopifnot(colnames(data) %in% colnames(template@data))
template@year <- as.integer(year)
template@data <- template@data[seq_len(nrow(data)), , drop = FALSE]
template@data[, colnames(data)] <- data
template@data <- data.matrix(
upgrade_weatherDF(data, c("DOY", weather_dataColumns()))
)
template
}

Expand Down
Loading

0 comments on commit 72d4bf2

Please sign in to comment.