ms_synchronize_timestep: Aggregate or interpolate MacroSheds data to specified time...

View source: R/ms_synchronize_timestep.R

ms_synchronize_timestepR Documentation

Aggregate or interpolate MacroSheds data to specified time intervals

Description

Set the sample interval of a data.frame in MacroSheds format (see MacroSheds EDI) by interpolation (for upsampling) or aggregation (for downsampling).

Usage

ms_synchronize_timestep(
  d,
  desired_interval,
  round_method = "floor",
  interpolate_method = "linear",
  interpolate_limit,
  summary_fun,
  quiet = FALSE
)

Arguments

d

data.frame. A data.frame in MacroSheds format (see MacroSheds EDI which can be generated by ms_load_product().

desired_interval

character. Input that can be parsed by the "by" parameter of base::seq.POSIXt, e.g. "5 mins", "1 day", "1 month".

round_method

character. If "floor" (the default), dates or datetimes will be shifted backward to the nearest desired_interval boundary by lubridate::floor_date(). For example, with desired_interval set to "1 day", 2020-01-01 23:59:00 would be adjusted to 2020-01-01 00:00:00. The other option is to "round" dates or datetimes to the nearest interval boundary via lubridate::round_date().

interpolate_method

character. One of:

  • "linear": the default. Linear interpolation.

  • "nocb": next observation carried backward. Useful for interpolating precipitation chemistry reported as concentration.

  • "nocb_mean": the next observation, divided by the number of preceding observations that are missing, carried backward. Useful for e.g. precipitation measured with a collector after x days.

  • "zero": replace missing values with 0. Useful for e.g. precipitation series where nothing is reported during periods of no rain/snow.

interpolate_limit

numeric. The maximum number of consecutive points to interpolate.

summary_fun

optional character. One of "mean" or "sum". The function that will be used to summarize data if desired_interval is larger than the input data in d.

quiet

logical. Should warnings be printed to console.

Value

returns a tibble with data in the new desired_interval with a ms_interp column indicating if a value was interpolated.

Author(s)

Spencer Rhea

Mike Vlah, vlahm13@gmail.com

Wes Slaughter

Examples

### Load some MacroSheds data:
ms_root = 'data/macrosheds'
ms_download_core_data(macrosheds_root = ms_root,
                      domains = 'hbef')
d <- ms_load_product(macrosheds_root = ms_root, 
                     prodname = 'stream_chemistry',
                     domains = 'hbef',
                     filter_vars = c('NO3_N', 'Na', 'Mg', 'SO4_S'))
                     
### interpolate to 15-minute intervals, but don't fill gaps larger than 24 hours
ms_synchronize_timestep(d, '15 min', 96)

### aggregate samples to weekly interval by mean, and fill gaps of < 3 weeks
ms_synchronize_timestep(d, '1 week', 3, summary_fun = 'mean')

MacroSHEDS/macrosheds documentation built on Oct. 30, 2024, 11:15 a.m.