Skip to content

Commit ec3fbdc

Browse files
committed
feat: TimeSeriesAPI module
1 parent 3b6e7e5 commit ec3fbdc

File tree

7 files changed

+60
-35
lines changed

7 files changed

+60
-35
lines changed

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# SpaceDataModel
22

3-
[![Documentation](https://img.shields.io/badge/docs-dev-blue.svg)](https://JuliaSpacePhysics.github.io/SpaceDataModel.jl/dev/)
43
[![DOI](https://zenodo.org/badge/958430775.svg)](https://doi.org/10.5281/zenodo.15207556)
4+
[![version](https://juliahub.com/docs/General/SpaceDataModel/stable/version.svg)](https://juliahub.com/ui/Packages/General/SpaceDataModel)
55

66
[![Build Status](https://github.com/JuliaSpacePhysics/SpaceDataModel.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/JuliaSpacePhysics/SpaceDataModel.jl/actions/workflows/CI.yml?query=branch%3Amain)
77
[![](https://img.shields.io/badge/%F0%9F%9B%A9%EF%B8%8F_tested_with-JET.jl-233f9a)](https://github.com/aviatesk/JET.jl)
@@ -14,12 +14,9 @@ SpaceDataModel.jl is a lightweight Julia package providing a flexible data model
1414

1515
For information on using the package, see the documentation available at https://JuliaSpacePhysics.github.io/SpaceDataModel.jl/dev/.
1616

17-
## Installation
17+
**Installation**: at the Julia REPL, run `using Pkg; Pkg.add("SpaceDataModel")`
1818

19-
```julia
20-
using Pkg
21-
Pkg.add("SpaceDataModel")
22-
```
19+
**Documentation**: [![Dev](https://img.shields.io/badge/docs-dev-blue.svg?logo=julia)](https://JuliaSpacePhysics.github.io/SpaceDataModel.jl/dev/)
2320

2421
## Usage
2522

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
module SpaceDataModelDimensionalDataExt
22

33
import SpaceDataModel
4-
using DimensionalData: AbstractDimArray, TimeDim, dims, Dimension
4+
using DimensionalData: AbstractDimArray, TimeDim, dims, Dimension, Dim
55
import DimensionalData as DD
6-
import SpaceDataModel: meta, _merge, timedim, unwrap, name
6+
import SpaceDataModel: meta, _merge, tdimnum, timedim, unwrap, name
77

88
_merge(::DD.NoMetadata, d, rest...) = merge(d, rest...)
9-
meta(A::AbstractDimArray) = DD.metadata(A)
10-
name(x::Dimension) = DD.name(x)
11-
unwrap(x::Dimension) = parent(x)
9+
SpaceDataModel.getmeta(A::AbstractDimArray) = DD.metadata(A)
10+
SpaceDataModel.name(x::Dimension) = DD.name(x)
11+
SpaceDataModel.unwrap(x::Dimension) = parent(x)
12+
13+
# A no-error version of `dimnum`
14+
_dimnum(x, dim) = DD.hasdim(x, dim) ? DD.dimnum(x, dim) : nothing
15+
16+
SpaceDataModel.tdimnum(x::AbstractDimArray) = @something(
17+
_dimnum(x, TimeDim),
18+
_dimnum(x, Dim{:time}),
19+
tdimnum(parent(x))
20+
)
1221

13-
function SpaceDataModel.timedim(x::AbstractDimArray, query = nothing)
14-
query = something(query, TimeDim)
15-
qdim = dims(x, query)
16-
return isnothing(qdim) ? dims(x, 1) : qdim
17-
end
1822
end

src/SpaceDataModel.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ include("workload.jl")
2727

2828
include("variable_interface.jl")
2929

30-
include("times.jl")
31-
using .Times
30+
include("times.jl"); using .Times
31+
include("timeseries.jl"); using .TimeSeriesAPI
3232

3333
end

src/timeseries.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""A time series-focused namespace for packages to share functions"""
2+
module TimeSeriesAPI
3+
using ..SpaceDataModel: dim, @getfield, unwrap
4+
export tdimnum, timedim, times, tmin, tmax
5+
"""
6+
tdimnum(x)
7+
8+
Get the time dimension number of object `x`.
9+
"""
10+
function tdimnum(x)
11+
@warn "Could not guess the time dimension number, assuming last dimension"
12+
return ndims(x)
13+
end
14+
15+
timedim(x) = dim(x, tdimnum(x))
16+
17+
times(v) = @getfield v (:times, :time) unwrap(timedim(v))
18+
19+
tmin(v) = minimum(times(v))
20+
tmax(v) = maximum(times(v))
21+
22+
end

src/variable.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ Base.setindex!(var::AbstractDataVariable, v, s::Union{String, Symbol}) = setinde
3232
Base.get(var::AbstractDataVariable, s::Union{String, Symbol}, d = nothing) = _get(meta(var), s, d)
3333
Base.get(f::Function, var::AbstractDataVariable, s::Union{String, Symbol}) = get(f, meta(var), s)
3434

35-
tmin(v) = minimum(times(v))
36-
tmax(v) = maximum(times(v))
37-
3835
_timerange_str(times) = "Time Range: $(minimum(times)) to $(maximum(times))"
3936

4037
function Base.show(io::IO, var::T) where {T <: AbstractDataVariable}

src/variable_interface.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,14 @@ function dim(x, s::Union{String, Symbol})
2929
error("Dimension $s not found")
3030
end
3131

32-
timedim(x) = dim(x, ndims(x))
33-
3432
"""
3533
getmeta(x)
3634
3735
Get metadata for object `x`. If `x` does not have metadata, return `NoMetadata()`.
3836
3937
"""
4038
getmeta(x) = @getfield x (:meta, :metadata) NoMetadata()
39+
getmeta(x::AbstractDict) = x
4140

4241
# like get, but handles NamedTuple
4342
_get(x, key, default) = get(x, key, default)
@@ -50,10 +49,9 @@ Get metadata value associated with `key` for object `x`, or `default` if `key` i
5049
"""
5150
getmeta(x, key, default = nothing) = _get(meta(x), key, default)
5251

53-
meta(x) = getmeta(x) # not exported (to be removed)
52+
const meta = getmeta # not exported (to be removed)
5453

5554
units(v) = @get(v, "units", nothing)
56-
times(v) = @getfield v (:times, :time) unwrap(timedim(v))
5755

5856
function unit(v)
5957
us = units(v)

test/ext/DimensionalData.jl

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,23 @@ end
3131
@test dim(x, :X) == X(1:3)
3232
@test_throws ErrorException dim(x, :time)
3333

34-
@test timedim(x) == Ti([1, 2, 3, 4, 5])
35-
@test times(x) == [1, 2, 3, 4, 5]
36-
@test tmin(x) == 1
37-
@test tmax(x) == 5
38-
@test unwrap(timedim(x)) == 1:5
39-
40-
@test name(timedim(x)) == :Ti
4134
@test string(name(x)) == ""
42-
43-
@test unwrap(Ti(1:5)) == 1:5
44-
@test Ti(1:5) isa DimensionalData.Dimension
4535
@test unwrap(Ti(view([1, 2, 3, 4, 5], 2:3))) == view(1:5, 2:3)
36+
37+
@testset "TimeSeriesAPI" begin
38+
using Chairmarks
39+
using SpaceDataModel: tdimnum
40+
x = rand(X(3), Ti(5), Z(2))
41+
@test tdimnum(x) == 2
42+
@test timedim(x) == Ti([1, 2, 3, 4, 5])
43+
@test times(x) == [1, 2, 3, 4, 5]
44+
@test tmin(x) == 1
45+
@test tmax(x) == 5
46+
@test unwrap(timedim(x)) == 1:5
47+
@test name(timedim(x)) == :Ti
48+
49+
x = rand(X(3), Dim{:time}(5), Z(2))
50+
@test tdimnum(x) == 2
51+
@test (@allocated tdimnum(x)) == 0
52+
end
4653
end

0 commit comments

Comments
 (0)