Skip to contents

Fills missing values (`NA`) in daily weather variables, such as the data downloaded with [download_AWS_INMET_daily()], so they can be used by functions that do not accept missing values, like [water_balance()]. Short gaps are filled by linear interpolation between the neighbouring observations, and, optionally, remaining gaps are filled with the mean of the same day of the year across the available years (climatology).

Precipitation must not be filled with this function: it is intermittent, so interpolation or averaging would create rainfall that did not occur. A column of flags (`<variable>_filled`) identifies every value that was filled, so filled data are never mistaken for observations.

Usage

fill_gaps(
  data,
  vars = c("tair_mean_c", "tair_min_c", "tair_max_c", "rh_max_porc", "rh_min_porc",
    "ws_2_m_s", "patm_mb", "sr_mj_m2"),
  date = "date",
  method = c("linear", "climatology", "both"),
  max_gap = 3,
  group = NULL,
  flag = TRUE
)

Arguments

data

A data.frame with one row per day.

vars

A character vector with the names of the columns to fill. The default is the set of INMET variables used by [daily_eto_FAO56()]. Rainfall columns are not allowed.

date

Name of the column with the dates (`Date` or `POSIXct`). Within each group, dates must be unique and sorted in ascending order.

method

`"linear"` (default) interpolates gaps of at most `max_gap` days between two observed values; short gaps at the beginning or end of the series take the nearest observation. `"climatology"` fills every gap with the mean of the same day of the year in the other years (needs more than one year of data). `"both"` applies linear interpolation first and climatology to what remains.

max_gap

Maximum length, in days, of a gap filled by linear interpolation. Longer gaps are left as `NA` (or filled by climatology when `method = "both"`). Default is 3.

group

Optional name of a column (e.g. the station code) used to fill each series independently when the data have more than one station.

flag

If `TRUE` (default), adds a logical column `<variable>_filled` for each filled variable.

Value

The input data.frame with the missing values filled and, if `flag = TRUE`, one extra `<variable>_filled` column per variable. A message reports how many values were filled and how many remain missing.

Author

Roberto Filgueiras

Examples

if (FALSE) { # \dontrun{
df <- download_AWS_INMET_daily("A001", "2023-01-01", "2024-12-31")

# Short gaps only
df_filled <- fill_gaps(df)

# Short gaps by interpolation, the rest by climatology
df_filled <- fill_gaps(df, method = "both", max_gap = 3)
} # }