Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature improve weather #244

Merged
merged 7 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -137,7 +137,7 @@
expected[[c(1, 3)]] <- 0
}

if (level %in% "major") {

Check warning on line 140 in R/A_swGenericMethods.R

View workflow job for this annotation

GitHub Actions / lint

file=R/A_swGenericMethods.R,line=140,col=9,[scalar_in_linter] Use == to match length-1 scalars, not %in%. Note that == preserves NA where %in% does not.
# zero the minor-level
has[[c(1, 2)]] <- 0
expected[[c(1, 2)]] <- 0
Expand Down Expand Up @@ -230,7 +230,7 @@
#' `"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 @@ -44,11 +44,11 @@
#' @export
weather_dataAggFun <- function() {
c(
"Tmax_C" = mean,

Check warning on line 47 in R/D_swWeatherData.R

View workflow job for this annotation

GitHub Actions / lint

file=R/D_swWeatherData.R,line=47,col=5,[keyword_quote_linter] Only quote named arguments to functions if necessary, i.e., if the name is not a valid R symbol (see ?make.names).
"Tmin_C" = mean,

Check warning on line 48 in R/D_swWeatherData.R

View workflow job for this annotation

GitHub Actions / lint

file=R/D_swWeatherData.R,line=48,col=5,[keyword_quote_linter] Only quote named arguments to functions if necessary, i.e., if the name is not a valid R symbol (see ?make.names).
"PPT_cm" = sum,

Check warning on line 49 in R/D_swWeatherData.R

View workflow job for this annotation

GitHub Actions / lint

file=R/D_swWeatherData.R,line=49,col=5,[keyword_quote_linter] Only quote named arguments to functions if necessary, i.e., if the name is not a valid R symbol (see ?make.names).
"cloudCov_pct" = mean,

Check warning on line 50 in R/D_swWeatherData.R

View workflow job for this annotation

GitHub Actions / lint

file=R/D_swWeatherData.R,line=50,col=5,[keyword_quote_linter] Only quote named arguments to functions if necessary, i.e., if the name is not a valid R symbol (see ?make.names).
"windSpeed_mPERs" = mean,

Check warning on line 51 in R/D_swWeatherData.R

View workflow job for this annotation

GitHub Actions / lint

file=R/D_swWeatherData.R,line=51,col=5,[keyword_quote_linter] Only quote named arguments to functions if necessary, i.e., if the name is not a valid R symbol (see ?make.names).
"windSpeed_east_mPERs" = mean,
"windSpeed_north_mPERs" = mean,
"rHavg_pct" = mean,
Expand Down Expand Up @@ -188,12 +188,42 @@
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
Loading