Skip to content

About

A simple allocation free temporal aggregation and resampling

Resources

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

 

History

336 Commits

Folders and files

Repository files navigation

TimeSamplers.jl

Julia License: EUPL-1.2

Synthetic hourly temperature resampled to 6-hour and daily bins, to 8-day and monthly bins, and to mean seasonal cycles

A Julia package for temporal sampling, aggregation, and resampling of N-dimensional arrays along their time dimension.

Features

  • Flexible Time Aggregation: Aggregate data to hourly, daily, monthly, or yearly time steps
  • n-unit Resampling: Resample to any period such as "30-sec", "6-hour", "8-day" or "3-month", including anomalies, MSC and IAV
  • Anomaly Calculations: Compute anomalies at various time scales
  • Climatological Statistics: Calculate multi-year seasonal cycles (MSC) and inter-annual variability (IAV)
  • Efficient Views: Uses array views to avoid unnecessary data copying
  • Multiple Sampling Methods: Support for various temporal operations including year shuffling and random selection

Installation

using Pkg
Pkg.add("TimeSamplers")

Quick Start

using TimeSamplers
using Dates

# Create a date vector
dates = collect(Date(2000, 1, 1):Day(1):Date(2000, 12, 31))

# Create time samplers for daily aggregation (returns a Vector{TimeSample})
daily_samplers = create_TimeSampler(dates, TimeDay())

# Example data (time is the first dimension, so size(data, 1) == length(dates))
data = rand(length(dates), 2)

# Apply sampling/aggregation (2-arg form defaults to `TimeNoDiff()`)
sampled_data = do_time_sampling(data, daily_samplers)

Resampling to n-unit periods

time_resample resamples data to any period given as a base unit and a count. Supported units are second, minute, hour, day, week, month and year, with aliases such as sec, min, hr, mon and yr.

hourly = collect(DateTime(2000, 1, 1):Hour(1):DateTime(2001, 12, 31, 23))
data = rand(length(hourly), 2)

time_resample(data, hourly, "6-hour")                    # 6-hourly means
time_resample(data, hourly, Hour(6))                     # same, from a Dates.Period
time_resample(data, hourly, :day; sampler_func=sum)      # daily sums
time_resample(data, hourly, "8-day"; origin=:year)       # 8-day bins restarting each Jan 1
time_resample(data, hourly, "3-hour_msc")                # seasonal cycle at 3-hour resolution
time_resample(data, hourly, "3-hour_msc_day")            # mean diurnal cycle, 8 values
time_resample(data, hourly, "8-day_iav")                 # 8-day values minus their seasonal cycle
time_resample(data, hourly, :mean)                       # mean over the whole record
time_resample_dates(hourly, "6-hour")                    # bin start dates of the output

A period spec has the form <n>-<unit>[_<kind>[_<cycle>]], where the separator may be _ or -. The count can also be joined to the unit, so "3-hour", "3hour", "3h" and "3 hour" are the same, and "3h_msc_day" works too. The kind is one of anomaly, msc, msc_anomaly or iav, and the cycle defaults to year. The same specs work with create_TimeSampler, and the types TimePeriodSample, TimePeriodAnomaly, TimePeriodMSC, TimePeriodMSCAnomaly and TimePeriodIAV can be used directly.

Bins are aligned to the calendar by default. Use origin=:start to count bins from the first date, or origin=:year to restart bins every Jan 1. MSC and IAV bins always restart at the start of each cycle, and the MSC pools all timesteps at a cycle position across all cycles. In a yearly cycle, days are matched by calendar date, so Mar 1 is always averaged with Mar 1, and Feb 29 only has values from leap years.

Build once, apply many times

When many arrays share one time axis, such as the model realizations of an optimization, build a TimeResampler once and reuse it. Applying it only allocates the output.

rs = TimeResampler(hourly, "8-day_iav"; sampler_func=sum)
for realization in realizations
    iav = time_resample(realization, rs)
end
time_resample_dates(rs)                                  # dates of the output

time_sampling_type(rs.method) tells how the samplers are combined, and time_sampling_period("day_MSC") gives the length of the bins, here one day. Snake case names such as "first_year" or "day_MSC" resolve to the method types TimeFirstYear and TimeDayMSC.

Available Time Sampling Methods

Period methods

The period methods are the main API. Each takes any count of any unit, and the spec string gives the same method.

Method Spec Output
TimePeriodSample(period; origin=:calendar) "<n>-<unit>" one value per n-unit bin
TimePeriodAnomaly(period; origin=:calendar) "<n>-<unit>_anomaly" n-unit bins minus the mean of the record
TimePeriodMSC(period, cycle=Year(1)) "<n>-<unit>_msc[_<cycle>]" mean seasonal or diurnal cycle of n-unit bins
TimePeriodMSCAnomaly(period, cycle=Year(1)) "<n>-<unit>_msc_anomaly[_<cycle>]" mean cycle minus the mean of the record
TimePeriodIAV(period, cycle=Year(1)) "<n>-<unit>_iav[_<cycle>]" n-unit bins minus their mean cycle
TimeMean() "mean" one mean over the whole record

Calendar methods

The calendar methods are legacy aliases kept for backward compatibility. Each one is the period method in the same row, and gives the same samplers, values and labels, including for skip_sampling. They resolve from their snake case names, which existing configurations use. New code should use the period methods.

Calendar method Name Period method Spec
TimeHour() "hour" TimePeriodSample(Hour(1)) "1-hour"
TimeDay() "day" TimePeriodSample(Day(1)) "1-day"
TimeMonth() "month" TimePeriodSample(Month(1)) "1-month"
TimeYear() "year" TimePeriodSample(Year(1)) "1-year"
TimeHourAnomaly() "hour_anomaly" TimePeriodAnomaly(Hour(1)) "1-hour_anomaly"
TimeDayAnomaly() "day_anomaly" TimePeriodAnomaly(Day(1)) "1-day_anomaly"
TimeMonthAnomaly() "month_anomaly" TimePeriodAnomaly(Month(1)) "1-month_anomaly"
TimeYearAnomaly() "year_anomaly" TimePeriodAnomaly(Year(1)) "1-year_anomaly"
TimeHourDayMean() "hour_day_mean" TimePeriodMSC(Hour(1), Day(1)) "1-hour_msc_day"
TimeDayMSC() "day_MSC" TimePeriodMSC(Day(1)) "1-day_msc"
TimeMonthMSC() "month_MSC" TimePeriodMSC(Month(1)) "1-month_msc"
TimeDayMSCAnomaly() "day_MSC_anomaly" TimePeriodMSCAnomaly(Day(1)) "1-day_msc_anomaly"
TimeMonthMSCAnomaly() "month_MSC_anomaly" TimePeriodMSCAnomaly(Month(1)) "1-month_msc_anomaly"
TimeDayIAV() "day_IAV" TimePeriodIAV(Day(1)) "1-day_iav"
TimeMonthIAV() "month_IAV" TimePeriodIAV(Month(1)) "1-month_iav"

examples/calendar_vs_nstep.jl checks every row of this table and plots the outputs of both methods.

Time selection

The selection methods pick time steps instead of resampling them, so they have no period equivalent and are not accepted by TimeResampler. Apply them with do_time_sampling(dat, samplers, TimeIndexed()), which selects along the first dimension.

Method Name Selects
TimeAllYears() "all_years" every time step
TimeFirstYear() "first_year" the time steps of the first year
TimeRandomYear() "random_year" the time steps of one random year
TimeShuffleYears() "shuffle_years" whole years in a random order

Documentation

For detailed documentation, see the TimeSamplers.jl documentation.

License

This package is licensed under the EUPL-1.2 (European Union Public Licence v. 1.2). See the LICENSE file for details.

Contributing

Contributions are welcome! Please open an issue or pull request in this repository.

Authors

TimeSamplers.jl Contributors

About

A simple allocation free temporal aggregation and resampling

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages