Skip to content

Commit 8ce84cf

Browse files
authored
Merge pull request #40 from JamesMcClung/pr/fixes-for-numpy
Fix for unwrapping 1d numpy arrays
2 parents 0d64e77 + ce01305 commit 8ce84cf

7 files changed

Lines changed: 22 additions & 2 deletions

File tree

‎src/pscpy/psc.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ def __init__(
3737
self.x = self._get_coord(0)
3838
self.y = self._get_coord(1)
3939
self.z = self._get_coord(2)
40-
self.t = float(ds.attrs["time"])
4140

4241
def _get_coord(self, coord_idx: int) -> NDArray[Any]:
4342
return np.linspace(
@@ -63,6 +62,12 @@ def iter_components(field: Hashable, species_names: Iterable[str]) -> Generator[
6362
yield f"{moment}_{species_name}"
6463

6564

65+
def unwrap_float(arr: np.ndarray) -> float:
66+
if arr.ndim == 0:
67+
return float(arr)
68+
return float(arr[0])
69+
70+
6671
def decode_psc(
6772
ds: xr.Dataset,
6873
species_names: Iterable[str],
@@ -96,7 +101,7 @@ def decode_psc(
96101
"x": ("x", run_info.x),
97102
"y": ("y", run_info.y),
98103
"z": ("z", run_info.z),
99-
"t": run_info.t,
104+
"t": unwrap_float(ds.attrs["time"]),
100105
}
101106
ds = ds.assign_coords(coords)
102107

1.13 KB
Binary file not shown.
1.27 KB
Binary file not shown.
162 Bytes
Binary file not shown.
1.18 KB
Binary file not shown.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[
2+
{ "rank":0, "start":"Fri_Apr_10_11:45:56_2026","PP_mus": 0, "PP":{"mus":0, "nCalls":2},"ES_close_mus": 65, "ES_close":{"mus":65, "nCalls":1},"ES_AWD_mus": 403, "ES_AWD":{"mus":403, "nCalls":1},"ES_mus": 872, "ES":{"mus":872, "nCalls":1},"ES_meta2_mus": 398, "ES_meta2":{"mus":398, "nCalls":1},"ES_meta1_mus": 4, "ES_meta1":{"mus":4, "nCalls":1},"ES_meta1_gather_mus": 2, "ES_meta1_gather":{"mus":2, "nCalls":1}, "databytes":0, "metadatabytes":0, "metametadatabytes":0, "transport_0":{"type":"File_POSIX", "wbytes":1152, "close":{"mus":15, "nCalls":1}, "write":{"mus":19, "nCalls":2}, "open":{"mus":36, "nCalls":1}}, "transport_1":{"type":"File_POSIX", "wbytes":1304, "close":{"mus":9, "nCalls":1}, "write":{"mus":78, "nCalls":7}, "open":{"mus":101, "nCalls":1}} },
3+
{ "rank":1, "start":"Fri_Apr_10_11:45:56_2026","PP_mus": 0, "PP":{"mus":0, "nCalls":2},"ES_close_mus": 61, "ES_close":{"mus":61, "nCalls":1},"ES_AWD_mus": 591, "ES_AWD":{"mus":591, "nCalls":1},"ES_mus": 668, "ES":{"mus":668, "nCalls":1},"ES_meta2_mus": 0, "ES_meta2":{"mus":0, "nCalls":1},"ES_meta1_mus": 14, "ES_meta1":{"mus":14, "nCalls":1},"ES_meta1_gather_mus": 13, "ES_meta1_gather":{"mus":13, "nCalls":1}, "databytes":0, "metadatabytes":0, "metametadatabytes":0 }
4+
]

‎tests/test_xarray_adios2.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ def ds_pfd_moments_decoded(ds_pfd_moments_raw) -> xr.Dataset:
9696
return _decode_dataset(ds_pfd_moments_raw)
9797

9898

99+
@pytest.fixture
100+
def ds_with_time_arr():
101+
return pscpy.decode_psc(
102+
xr.open_dataset(pscpy.sample_dir / "pfd.000000000.bp"), ["e", "i"]
103+
)
104+
105+
99106
def test_open_dataset(ds_pfd_decoded):
100107
assert "jx_ec" in ds_pfd_decoded
101108
assert ds_pfd_decoded.coords.keys() == set({"x", "y", "z", "t"})
@@ -105,6 +112,10 @@ def test_open_dataset(ds_pfd_decoded):
105112
)
106113

107114

115+
def test_time_arr(ds_with_time_arr):
116+
assert np.isclose(ds_with_time_arr.time[0], 0.0)
117+
118+
108119
def test_component(ds_pfd_raw, ds_pfd_decoded):
109120
assert np.all(ds_pfd_raw.jeh.isel(dim_1_9=0).data == ds_pfd_decoded.jx_ec.data)
110121

0 commit comments

Comments
 (0)