Skip to content

Commit 65d7455

Browse files
committed
feat: subtype TplotVariable from AbstractDataVariable
1 parent 5489160 commit 65d7455

File tree

4 files changed

+35
-18
lines changed

4 files changed

+35
-18
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ version = "1.0.0-DEV"
77
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
88
DimensionalData = "0703355e-b756-11e9-17c0-8b28908087d0"
99
PythonCall = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d"
10+
SpaceDataModel = "0b37b92c-f0c5-4a52-bd5c-390dec20857c"
1011

1112
[compat]
1213
DimensionalData = "0.29"

src/DimensionalData.jl

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
11
using DimensionalData
2-
import DimensionalData: DimArray
2+
import DimensionalData: DimArray, dims
33
import DimensionalData.Lookups: NoLookup
44

5+
function get_xarray_dims(x; transpose=false)
6+
dim_names = tuple(Symbol.(collect(x.dims))...)
7+
dim_names = transpose ? reverse(dim_names) : dim_names
8+
coord_names = Symbol.(collect(x.coords.keys()))
9+
lookups_values = map(dim_names) do dim
10+
if dim in coord_names
11+
coord_py = getproperty(x, dim).data
12+
coord_type = string(coord_py.dtype.name)
13+
coord = coord_type == "datetime64[ns]" ? pyconvert_time(coord_py) : PyArray(coord_py; copy=false)
14+
Dim{dim}(coord)
15+
else
16+
Dim{dim}(NoLookup())
17+
end
18+
end
19+
return lookups_values
20+
end
21+
22+
DimensionalData.dims(v::TplotVariable) = Tuple(get_xarray_dims(v.py))
23+
524
function DimensionalData.DimArray(var::TplotVariable; kwargs...)
625
pyconvert_dataarray(var.py; kwargs...)
726
end
@@ -18,22 +37,9 @@ function pyconvert_dataarray(x; transpose=false)
1837
data_npy = transpose ? x.data.T : x.data
1938
data = PyArray(data_npy; copy=false)
2039

21-
dim_names = tuple(Symbol.(collect(x.dims))...)
22-
dim_names = transpose ? reverse(dim_names) : dim_names
23-
coord_names = Symbol.(collect(x.coords.keys()))
24-
lookups_values = map(dim_names) do dim
25-
if dim in coord_names
26-
coord_py = getproperty(x, dim).data
27-
coord_type = string(coord_py.dtype.name)
28-
coord_type == "datetime64[ns]" ? pyconvert_time(coord_py) : PyArray(coord_py; copy=false)
29-
else
30-
NoLookup()
31-
end
32-
end
33-
34-
lookups = NamedTuple{dim_names}(lookups_values)
40+
dims = get_xarray_dims(x; transpose)
3541
metadata = pyconvert(Dict{Any,Any}, x.attrs)
3642
array_name = pyis(x.name, pybuiltins.None) ? nothing : string(x.name)
3743

38-
return DimArray(data, lookups; name=array_name, metadata)
44+
return DimArray(data, dims; name=array_name, metadata)
3945
end

src/PySPEDAS.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ module PySPEDAS
33
using PythonCall
44
using PythonCall: pynew
55
using Dates
6+
using SpaceDataModel
7+
using SpaceDataModel: AbstractDataVariable
8+
import SpaceDataModel: times
69

710
export pyspedas, pytplot
811
export tplot, get_data

src/types.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,19 @@ Project(name) = Project(name, pynew(), Ref{Vector{Symbol}}())
1515
# This somehow could prevent the Segmentation fault, see also https://github.com/JuliaPy/PythonCall.jl/issues/586
1616
attributes(p::Project) = p.attributes[]
1717

18-
struct TplotVariable
18+
struct TplotVariable{T,N} <: AbstractDataVariable{T,N}
1919
name::Symbol
20+
data::PyArray{T,N}
2021
py::Py
2122
end
2223

23-
TplotVariable(name) = TplotVariable(Symbol(name), pytplot.data_quants[String(name)])
24+
function TplotVariable(name)
25+
py = pytplot.data_quants[String(name)]
26+
data = PyArray(py.data; copy=false)
27+
TplotVariable(Symbol(name), data, py)
28+
end
29+
30+
SpaceDataModel.times(var::TplotVariable) = pyconvert_time(var.py.time.data)
2431

2532
struct LoadFunction
2633
py::Py

0 commit comments

Comments
 (0)