View source: R/swWeatherGenerator.R
dbW_imputeWeather | R Documentation |
Impute missing weather values first by the weather generator
(for supported variables, see weatherGenerator_dataColumns()
) and
second, if any missing values remain, using one of the imputation types
provided by rSW2utils::impute_df()
for each variable separately.
dbW_imputeWeather(
weatherData,
use_wg = TRUE,
seed = NULL,
method_after_wg = c("interp", "locf", "mean", "none", "fail"),
nmax_run = Inf,
return_weatherDF = FALSE
)
weatherData |
A list of elements of class |
use_wg |
A logical value. Should the weather generator be used first? |
seed |
An integer value or |
method_after_wg |
A string. The imputation type passed
to |
nmax_run |
An integer value. Runs (sets of consecutive missing values)
that are equal or shorter to |
return_weatherDF |
A logical value. See section "Value". |
An updated copy of weatherData
where missing values are imputed.
If return_weatherDF
is TRUE
, then a
data frame where columns represent weather variables is returned.
If return_weatherDF
is FALSE
, then the result is converted to a
a list of elements of class swWeatherData
.
The weather generator (see dbW_generateWeather()
) produces new values
for all implemented variables weatherGenerator_dataColumns()
on days
where at least one of these variables is missing; this is to maintain
physical consistency among those variables.
This differs from the approach employed by method_after_wg
which imputes missing variables for each variable separately
(see rSW2utils::impute_df()
).
rSW2utils::impute_df()
, dbW_generateWeather()
# Load example data
path_demo <- system.file("extdata", "example1", package = "rSOILWAT2")
dif <- c(rep(TRUE, 3L), rep(FALSE, 11L))
dif[13L] <- TRUE # ACTUAL_VP
dif[14L] <- TRUE # SHORT_WR, desc_rsds = 2
wdata <- getWeatherData_folders(
LookupWeatherFolder = file.path(path_demo, "Input"),
weatherDirName = "data_weather_daymet",
filebasename = "weath",
startYear = 1980,
endYear = 1981,
dailyInputFlags = dif,
method = "C"
)
x0 <- x <- dbW_weatherData_to_dataframe(wdata)
dif0 <- calc_dailyInputFlags(x0)
# Set June-August of 1980 as missing
ids_missing <- x[, "Year"] == 1980 & x[, "DOY"] >= 153 & x[, "DOY"] <= 244
x[ids_missing, -(1:2)] <- NA
x1 <- dbW_imputeWeather(x, return_weatherDF = TRUE)
x2 <- dbW_imputeWeather(x, method_after_wg = "none", return_weatherDF = TRUE)
x3 <- dbW_imputeWeather(
x,
use_wg = FALSE,
method_after_wg = "locf",
return_weatherDF = TRUE
)
if (requireNamespace("graphics")) {
## Compare original vs imputed values for May-Sep of 1980
ip <- x[, "Year"] == 1980 & x[, "DOY"] >= 123 & x[, "DOY"] <= 274
doy <- x[ip, "DOY"]
vars <- names(dif0)[dif0]
nr <- ceiling(sqrt(length(vars)))
par_prev <- graphics::par(mfrow = c(nr, ceiling(length(vars) / nr)))
for (k in seq_along(vars)) {
graphics::plot(doy, x0[ip, vars[[k]]], ylab = vars[[k]], type = "l")
graphics::lines(doy, x1[ip, vars[[k]]], col = "red", lty = 2L)
graphics::lines(doy, x2[ip, vars[[k]]], col = "purple", lty = 3L)
graphics::lines(doy, x3[ip, vars[[k]]], col = "darkgreen", lty = 3L)
graphics::lines(doy, x0[ip, vars[[k]]], col = "gray")
}
graphics::par(par_prev)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.