Skip to content

Commit ce41663

Browse files
committed
feat: add TimeRanges iterator for time window operations
1 parent bdc1c24 commit ce41663

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

src/SpaceDataModel.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ const getdim = dim
3333

3434
include("times.jl"); using .Times
3535
include("timeseries.jl"); using .TimeSeriesAPI
36+
include("timerange.jl"); export TimeRanges
3637

3738
end

src/timerange.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
abstract type AbstractTimeRanges end
2+
3+
# https://docs.sunpy.org/en/stable/generated/api/sunpy.time.TimeRange.html#sunpy.time.TimeRange.window
4+
"""An iterator over a series of time ranges that are window long, with a cadence of cadence."""
5+
struct TimeRanges{T, P1, P2} <: AbstractTimeRanges
6+
start::T
7+
stop::T
8+
cadence::P1
9+
window::P2
10+
end
11+
12+
TimeRanges(t0, t1, cadence = t1 - t0) = TimeRanges(t0, t1, cadence, cadence)
13+
14+
Base.length(iter::TimeRanges) = cld(iter.stop - iter.start, iter.cadence)
15+
16+
function Base.iterate(iter::TimeRanges, current=iter.start)
17+
current > iter.stop && return nothing
18+
tstop = current + iter.window
19+
next_start = current + iter.cadence
20+
return (current, tstop), next_start
21+
end

test/timerange.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
@testitem "TimeRanges" begin
2+
using Dates
3+
using SpaceDataModel: TimeRanges
4+
5+
start = DateTime("2010-03-04T00:10:00")
6+
stop = DateTime("2010-03-04T01:20:00")
7+
8+
tranges = TimeRanges(start, stop, Hour(1), Second(12))
9+
fragments = collect(tranges)
10+
11+
@test length(tranges) == 2
12+
@test length(fragments) == 2
13+
14+
@test first(tranges) == fragments[1] == (DateTime("2010-03-04T00:10:00"), DateTime("2010-03-04T00:10:12"))
15+
@test fragments[2] == (DateTime("2010-03-04T01:10:00"), DateTime("2010-03-04T01:10:12"))
16+
17+
tranges = TimeRanges(start, stop)
18+
@test length(tranges) == 1
19+
end

0 commit comments

Comments
 (0)