dbW_fixWeather: Fix weather data

View source: R/swWeatherGenerator.R

dbW_fixWeatherR Documentation

Fix weather data

Description

Missing values are "fixed" with the following approach:

  1. weatherData is formatted for rSOILWAT2, i.e., converted to a Gregorian calendar and required but missing variables added.

  2. Short spells of missing values (consecutive days shorter than nmax_interp) are linearly interpolated from adjacent non-missing values (meta data tag "interpolateLinear (<= X days)")

  3. Short spells of missing precipitation values are

    • Linearly interpolated if precip_lt_nmax is NA

    • Set to a fixed numeric value of precip_lt_nmax (meta data tag "fixedValue")

    • Substituted with values from subData if precip_lt_nmax is Inf (see next point)

  4. Values from a second weather data object subData are used to replace (meta data tag ⁠substituteData"⁠):

    • Missing precipitation values (if precip_lt_nmax is Inf)

    • Values before first day with any non-missing values

    • Variables absent in weatherData and present in subData

  5. Long-term daily means are used to replace any remaining missing values (meta data tag "longTermDailyMean"); for instance, this approach may be applied for

    • Values of variables that are present in weatherData and absent in subData, before first day with any non-missing values

    • Values after end of available values in both weatherData and subData

Usage

dbW_fixWeather(
  weatherData,
  subData = NULL,
  new_startYear = NULL,
  new_endYear = NULL,
  nmax_interp = 7L,
  precip_lt_nmax = 0,
  return_weatherDF = FALSE
)

Arguments

weatherData

A list of elements of class swWeatherData or a data frame as returned by dbW_weatherData_to_dataframe().

subData

A weather data object.

new_startYear

An integer value. The first Calendar year of the new time period. If NULL, then the first year of weatherData.

new_endYear

An integer value. The last Calendar year of the new time period. If NULL, then the last year of weatherData.

nmax_interp

An integer value. Maximum spell length of missing values for which linear interpolation is applied.

precip_lt_nmax

A numeric value. Should short spells of missing precipitation values be linearly interpolated (if NA), substituted with values from subData (if Inf), or replaced by a fixed numeric value (default is 0)?

return_weatherDF

A logical value. See section "Value".

Value

A list with two named elements

  • "weatherData": An updated copy of the input weatherData where missing values have been replaced. If return_weatherDF is TRUE, then the result is converted to a data frame where columns represent weather variables. If return_weatherDF is FALSE, then the result is a list of elements of class swWeatherData.

  • "meta": a data frame with the same dimensions as "weatherData" with tags indicating which approach was used to replaced missing values in corresponding cells of weatherData (see section Details)

See Also

dbW_imputeWeather(), dbW_substituteWeather(), dbW_generateWeather()

Examples

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)
all.equal(
  xf[["weatherData"]][!ids_to_interp, ],
  as.data.frame(x0)[!ids_to_interp, ]
)
table(xf[["meta"]])


DrylandEcology/rSOILWAT2 documentation built on Jan. 12, 2024, 9:06 p.m.