timesteps

A library for handling discrete time series in constant increments. The main purpose of this library is to describe the time axis when dealing with time series of observational data and climate data.

Features

  • TimeStep consists of a pair of origin time and a time interval.
  • An instance of a TimeStep can be constructed by parsing an expression like "hours since 2001-01-01 00:00:00" (originate from udunits library)
  • DateTime object can be obtained from the index value (0 for the origin time).
  • The index value can be calculated from the date and time.
  • TimeStep can handle special calendar-type time series such as noleap, allleap, and 360_day.
  • "24:00:00" can be parsed as a valid timestamp.
  • You can pairert the index values between different TimeStep instances using TimeStepConv.

Usage

TimeStep

require "timesteps"

ts = TimeStep.new("3 hours since 2001-01-01 09:00:00", calendar: "standard")

p ts                ### => #<TimeStep definition='3 hours since 2001-01-01 09:00:00.000000000 +00:00' calendar='standard'>

p ts.origin         ### => #<DateTime: 2001-01-01T09:00:00+00:00 ...>

p ts.time_at(1)     ### => #<DateTime: 2001-01-01T12:00:00+00:00 ...>
p ts.time_at(8)     ### => #<DateTime: 2001-01-02T09:00:00+00:00 ...>

time = DateTime.parse("2001-01-02 09:00:00 +00:00")
p ts.index_at(time) ### => (8/1)    <Rational>

TimeStepPair

require "timesteps"

ts1 = TimeStep.new("3 hours since 2001-01-01 21:00:00")
ts2 = TimeStep.new("hour since 2001-01-01 09:00:00")
pair = TimeStep::Pair.new(ts1, ts2)

p pair            ### => <TimeStepConv from='<TimeSteps unit='3 hour since 2001-01-01 21:00:00.000000000' calendar='standard'>' to='<TimeSteps unit='hour since 2001-01-01 09:00:00.000000000' calendar='standard'>'>

p pair.forward(0) ### 12/1  (12)
p pair.forward(2) ### 18/1  (12 + 2*3h/1h)

p pair.inverse(0) ### -4/1  ((-12h)/3h)
p pair.inverse(2) ### -10/3 ((-12h+2*1h)/3h)