Skip to content

Commit 5caee25

Browse files
authored
chore: update test and docs (#2)
1 parent 65d7455 commit 5caee25

File tree

8 files changed

+69
-94
lines changed

8 files changed

+69
-94
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
/Manifest.toml
1+
Manifest.toml
22
.CondaPkg
3+
themis_data/thd/l2/fgm/2020/thd_l2_fgm_20200420_v01.cdf
4+
omni_data

Project.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "PySPEDAS"
22
uuid = "668d2cef-03a7-4c36-a521-7d1c6ddc8846"
33
authors = ["Beforerr <[email protected]> and contributors"]
4-
version = "1.0.0-DEV"
4+
version = "0.1.0"
55

66
[deps]
77
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
@@ -10,5 +10,8 @@ PythonCall = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d"
1010
SpaceDataModel = "0b37b92c-f0c5-4a52-bd5c-390dec20857c"
1111

1212
[compat]
13+
Dates = "1"
1314
DimensionalData = "0.29"
15+
PythonCall = "0.9.23"
16+
SpaceDataModel = "0.1.8"
1417
julia = "1.10"

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
# PySPEDAS.jl
22

33
[![Build Status](https://github.com/Beforerr/PySPEDAS.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/Beforerr/PySPEDAS.jl/actions/workflows/CI.yml?query=branch%3Amain)
4+
[![Aqua QA](https://raw.githubusercontent.com/JuliaTesting/Aqua.jl/master/badge.svg)](https://github.com/JuliaTesting/Aqua.jl)
45

6+
A Julia wrapper around [PySPEDAS](https://github.com/spedas/pyspedas): Python-based Space Physics Environment Data Analysis Software.
57

6-
A simple Julia wrapper around [PySPEDAS](https://github.com/spedas/pyspedas): Python-based Space Physics Environment Data Analysis Software.
8+
## Installation
9+
10+
```julia
11+
using Pkg
12+
Pkg.add("PySPEDAS")
13+
```
714

815
## Demo
916

@@ -32,3 +39,5 @@ mms.fgm(trange, time_clip=true, probe=2)
3239
```
3340

3441
Each mission is represented as a `Project` type, which wraps the underlying Python module.
42+
43+
> [!NOTE] > [SPEDAS.jl](https://github.com/Beforerr/SPEDAS.jl) provides a native Julia counterpart with cross-language validation and comparison. See [SPEDAS.jl Documentation](https://beforerr.github.io/SPEDAS.jl/dev/validation/pyspedas/) for more details.

src/DimensionalData.jl

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ using DimensionalData
22
import DimensionalData: DimArray, dims
33
import DimensionalData.Lookups: NoLookup
44

5-
function get_xarray_dims(x; transpose=false)
5+
function get_xarray_dims(x; transpose = false)
66
dim_names = tuple(Symbol.(collect(x.dims))...)
77
dim_names = transpose ? reverse(dim_names) : dim_names
88
coord_names = Symbol.(collect(x.coords.keys()))
99
lookups_values = map(dim_names) do dim
1010
if dim in coord_names
1111
coord_py = getproperty(x, dim).data
1212
coord_type = string(coord_py.dtype.name)
13-
coord = coord_type == "datetime64[ns]" ? pyconvert_time(coord_py) : PyArray(coord_py; copy=false)
13+
coord = coord_type == "datetime64[ns]" ? pyconvert_time(coord_py) : PyArray(coord_py; copy = false)
1414
Dim{dim}(coord)
1515
else
1616
Dim{dim}(NoLookup())
@@ -21,8 +21,24 @@ end
2121

2222
DimensionalData.dims(v::TplotVariable) = Tuple(get_xarray_dims(v.py))
2323

24+
"""
25+
get_data(::Type{DimArray}, name; kwargs...)
26+
27+
Retrieve data from a tplot variable and convert it to a `DimensionalData.DimArray.
28+
"""
29+
get_data(::Type{DimArray}, name; kwargs...) = pyconvert_dataarray(get_data(name; kwargs...))
30+
31+
"""
32+
get_data(::Type{T<:AbstractDimStack}, names; kwargs...)
33+
34+
Retrieve multiple tplot variables and combine them into a DimensionalData stack.
35+
"""
36+
function get_data(::Type{T}, names; kwargs...) where {T <: AbstractDimStack}
37+
return T(pyconvert_dataarray.(get_data.(names; kwargs...)))
38+
end
39+
2440
function DimensionalData.DimArray(var::TplotVariable; kwargs...)
25-
pyconvert_dataarray(var.py; kwargs...)
41+
return pyconvert_dataarray(var.py; kwargs...)
2642
end
2743

2844
"""
@@ -33,13 +49,13 @@ Convert a `xarray.DataArray` to a `DimensionalData.DataArray`.
3349
# Reference:
3450
- https://github.com/rafaqz/DimensionalData.jl/blob/main/ext/DimensionalDataPythonCall.jl
3551
"""
36-
function pyconvert_dataarray(x; transpose=false)
52+
function pyconvert_dataarray(x; transpose = false)
3753
data_npy = transpose ? x.data.T : x.data
38-
data = PyArray(data_npy; copy=false)
54+
data = PyArray(data_npy; copy = false)
3955

4056
dims = get_xarray_dims(x; transpose)
41-
metadata = pyconvert(Dict{Any,Any}, x.attrs)
57+
metadata = pyconvert(Dict{Any, Any}, x.attrs)
4258
array_name = pyis(x.name, pybuiltins.None) ? nothing : string(x.name)
4359

44-
return DimArray(data, dims; name=array_name, metadata)
45-
end
60+
return DimArray(data, dims; name = array_name, metadata)
61+
end

src/PySPEDAS.jl

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ using SpaceDataModel
77
using SpaceDataModel: AbstractDataVariable
88
import SpaceDataModel: times
99

10-
export pyspedas, pytplot
11-
export tplot, get_data
10+
export pyspedas
11+
export pytplot, get_data
1212
export Project, TplotVariable
1313

1414
include("types.jl")
@@ -18,15 +18,13 @@ include("DimensionalData.jl")
1818

1919
using .Projects
2020

21-
const TnamesType = Union{AbstractArray,Tuple}
21+
const TnamesType = Union{AbstractArray, Tuple}
2222

2323
const pyspedas = pynew()
24-
const pytplot = pynew()
2524
const pyns = pynew()
2625

2726
function __init__()
2827
PythonCall.pycopy!(pyspedas, pyimport("pyspedas"))
29-
PythonCall.pycopy!(pytplot, pyimport("pytplot"))
3028
PythonCall.pycopy!(pyns, pyimport("numpy").timedelta64(1, "ns"))
3129
for p in PROJECTS
3230
try
@@ -37,10 +35,11 @@ function __init__()
3735
@warn "Failed to load project $(p): $e"
3836
end
3937
end
38+
return
4039
end
4140

42-
tplot(args...) = @pyconst(pyspedas.tplot)(args...)
43-
tplot(tnames::TnamesType, args...) = @pyconst(pyspedas.tplot)(pylist(tnames), args...)
41+
pytplot(args...) = @pyconst(pyspedas.tplot)(args...)
42+
pytplot(tnames::TnamesType, args...) = @pyconst(pyspedas.tplot)(pylist(tnames), args...)
4443

4544
"""
4645
get_data(name; xarray=true, kwargs...)
@@ -49,35 +48,19 @@ Retrieve data from a tplot variable by `name`.
4948
5049
By default, returns an xarray DataArray object. If `xarray` is set to false, returns a tuple of (times, data).
5150
"""
52-
get_data(name; xarray=true, kwargs...) = pyspedas.get_data(name; xarray, kwargs...)
51+
get_data(name; xarray = true, kwargs...) = pyspedas.get_data(name; xarray, kwargs...)
5352

54-
"""
55-
get_data(::Type{DimArray}, name; kwargs...)
56-
57-
Retrieve data from a tplot variable and convert it to a `DimensionalData.DimArray.
58-
"""
59-
get_data(::Type{DimArray}, name; kwargs...) = pyconvert_dataarray(get_data(name; kwargs...))
60-
61-
"""
62-
get_data(::Type{T<:AbstractDimStack}, names; kwargs...)
63-
64-
Retrieve multiple tplot variables and combine them into a DimensionalData stack.
65-
"""
66-
function get_data(::Type{T}, names; kwargs...) where {T<:AbstractDimStack}
67-
T(pyconvert_dataarray.(get_data.(names; kwargs...)))
68-
end
69-
70-
function demo_get_data(; trange=["2020-04-20/06:00", "2020-04-20/08:00"])
71-
pyspedas.projects.themis.fgm(; trange, time_clip=true, probe='d')
72-
get_data(DimArray, "thd_fgs_gsm")
53+
function demo_get_data(; trange = ["2017-03-23/00:00:00", "2017-04-23/23:59:59"])
54+
pyspedas.projects.omni.data(; trange)
55+
return get_data(DimArray, "SYM_H")
7356
end
7457

75-
function demo(; trange=["2020-04-20/06:00", "2020-04-20/08:00"])
76-
pyspedas.projects.solo.mag(; trange, time_clip=true)
77-
pyspedas.projects.psp.fields(; trange, time_clip=true)
78-
pyspedas.projects.mms.fgm(; trange, time_clip=true, probe=2)
79-
pyspedas.projects.themis.fgm(; trange, time_clip=true, probe='d')
80-
tplot(["B_RTN", "psp_fld_l2_mag_RTN", "mms2_fgm_b_gsm_srvy_l2_bvec", "thd_fgs_gsm"])
58+
function demo(; trange = ["2020-04-20/06:00", "2020-04-20/08:00"])
59+
pyspedas.projects.solo.mag(; trange, time_clip = true)
60+
pyspedas.projects.psp.fields(; trange, time_clip = true)
61+
pyspedas.projects.mms.fgm(; trange, time_clip = true, probe = 2)
62+
pyspedas.projects.themis.fgm(; trange, time_clip = true, probe = 'd')
63+
return pytplot(["B_RTN", "psp_fld_l2_mag_RTN", "mms2_fgm_b_gsm_srvy_l2_bvec", "thd_fgs_gsm"])
8164
end
8265

8366
end

src/types.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ 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{T,N} <: AbstractDataVariable{T,N}
18+
struct TplotVariable{T, N} <: AbstractDataVariable{T, N}
1919
name::Symbol
20-
data::PyArray{T,N}
20+
data::PyArray{T, N}
2121
py::Py
2222
end
2323

2424
function TplotVariable(name)
25-
py = pytplot.data_quants[String(name)]
26-
data = PyArray(py.data; copy=false)
27-
TplotVariable(Symbol(name), data, py)
25+
py = @pyconst(pyimport("pytplot").data_quants)[String(name)]
26+
data = PyArray(py.data; copy = false)
27+
return TplotVariable(Symbol(name), data, py)
2828
end
2929

3030
SpaceDataModel.times(var::TplotVariable) = pyconvert_time(var.py.time.data)
@@ -36,7 +36,7 @@ end
3636
function (f::LoadFunction)(args...; kwargs...)
3737
tvars_py = f.py(args...; kwargs...)
3838
tvars = pyconvert(Vector{Symbol}, tvars_py)
39-
NamedTuple{tuple(tvars...)}(TplotVariable.(tvars))
39+
return NamedTuple{tuple(tvars...)}(TplotVariable.(tvars))
4040
end
4141

4242
# Allow calling methods on the Python module
@@ -54,5 +54,5 @@ Base.show(io::IO, p::Project) = print(io, "SPEDAS Project: $(p.name)")
5454
Base.show(io::IO, var::TplotVariable) = print(io, var.py.data)
5555
function Base.show(io::IO, m::MIME"text/plain", var::TplotVariable)
5656
println(io, "Tplot Variable: $(var.name)")
57-
show(io, m, var.py)
58-
end
57+
return show(io, m, var.py)
58+
end

test/Manifest.toml

Lines changed: 0 additions & 41 deletions
This file was deleted.

test/runtests.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
using TestItems, TestItemRunner
22

3+
@run_package_tests filter = ti -> !(:skipci in ti.tags)
4+
35
@testitem "Aqua" begin
46
using Aqua
57
Aqua.test_all(PySPEDAS)
68
end
79

810
@testitem "PySPEDAS.jl" begin
9-
@test_nowarn PySPEDAS.demo_get_data()
11+
using PySPEDAS.DimensionalData
12+
@test PySPEDAS.demo_get_data() isa DimArray
1013
end

0 commit comments

Comments
 (0)