Skip to content

Commit c248359

Browse files
committed
sorting of tsteps
1 parent e2f5e40 commit c248359

File tree

3 files changed

+42
-21
lines changed

3 files changed

+42
-21
lines changed

dist/nt2py-0.4.1.tar.gz

13.9 KB
Binary file not shown.

nt2/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.4.0"
1+
__version__ = "0.4.1"

nt2/read.py

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -630,11 +630,13 @@ def __init__(self, fname):
630630
else:
631631
self.metric = MinkowskiMetric()
632632
coords = list(CoordinateDict[coordinates].values())[::-1][-dimension:]
633-
633+
634634
for s in self.file.keys():
635635
if any([k.startswith("X") for k in self.file[s].keys()]):
636636
# cell-centered coords
637-
cc_coords = {c: self.file[s][f"X{i+1}"] for i, c in enumerate(coords[::-1])}
637+
cc_coords = {
638+
c: self.file[s][f"X{i+1}"] for i, c in enumerate(coords[::-1])
639+
}
638640
# cell edges
639641
cell_1 = {
640642
f"{c}_1": (
@@ -664,7 +666,7 @@ def __init__(self, fname):
664666
)
665667

666668
self.dataset = xr.Dataset()
667-
669+
668670
# -------------------------------- load fields ------------------------------- #
669671
fields = None
670672
f_outsteps = []
@@ -678,6 +680,10 @@ def __init__(self, fname):
678680
f_times.append(self.file[s]["Time"][()])
679681
f_steps.append(self.file[s]["Step"][()])
680682

683+
f_outsteps = sorted(f_outsteps, key=lambda x: int(x.replace("Step", "")))
684+
f_steps = sorted(f_steps)
685+
f_times = np.array(sorted(f_times), dtype=np.float64)
686+
681687
for k in self.file.attrs.keys():
682688
if (
683689
type(self.file.attrs[k]) == bytes
@@ -722,7 +728,7 @@ def __init__(self, fname):
722728
},
723729
)
724730
self.dataset[k_] = x
725-
731+
726732
# ------------------------------ load particles ------------------------------ #
727733
particles = None
728734
p_outsteps = []
@@ -735,19 +741,27 @@ def __init__(self, fname):
735741
p_outsteps.append(s)
736742
p_times.append(self.file[s]["Time"][()])
737743
p_steps.append(self.file[s]["Step"][()])
738-
744+
745+
p_outsteps = sorted(p_outsteps, key=lambda x: int(x.replace("Step", "")))
746+
p_steps = sorted(p_steps)
747+
p_times = np.array(sorted(p_times), dtype=np.float64)
748+
739749
self._particles = {}
740-
741750

742751
if len(p_outsteps) > 0:
743752
species = np.unique(
744-
[int(pq.split("_")[1]) for pq in self.file[p_outsteps[0]].keys() if pq.startswith("p")]
753+
[
754+
int(pq.split("_")[1])
755+
for pq in self.file[p_outsteps[0]].keys()
756+
if pq.startswith("p")
757+
]
745758
)
746759

747760
def list_to_ragged(arr):
748761
max_len = np.max([len(a) for a in arr])
749762
return map(
750-
lambda a: np.concatenate([a, np.full(max_len - len(a), np.nan)]), arr
763+
lambda a: np.concatenate([a, np.full(max_len - len(a), np.nan)]),
764+
arr,
751765
)
752766

753767
for s in species:
@@ -773,11 +787,16 @@ def list_to_ragged(arr):
773787
if "p" + q in self.file[step_k].keys():
774788
prtl_data[q_].append(self.file[step_k]["p" + q])
775789
else:
776-
prtl_data[q_].append(np.full_like(prtl_data[q_][-1], np.nan))
790+
prtl_data[q_].append(
791+
np.full_like(prtl_data[q_][-1], np.nan)
792+
)
777793
prtl_data[q_] = list_to_ragged(prtl_data[q_])
778794
prtl_data[q_] = da.from_array(list(prtl_data[q_]))
779795
prtl_data[q_] = xr.DataArray(
780-
prtl_data[q_], dims=["t", "id"], name=q_, coords={"t": p_times, "s": ("t", p_steps)}
796+
prtl_data[q_],
797+
dims=["t", "id"],
798+
name=q_,
799+
coords={"t": p_times, "s": ("t", p_steps)},
781800
)
782801
if coordinates == "sph":
783802
prtl_data["x"] = (
@@ -794,7 +813,7 @@ def list_to_ragged(arr):
794813
prtl_data[PrtlDict[coordinates]["X2"]]
795814
)
796815
self._particles[s] = xr.Dataset(prtl_data)
797-
816+
798817
# ------------------------------- load spectra ------------------------------- #
799818
spectra = None
800819
s_outsteps = []
@@ -807,13 +826,21 @@ def list_to_ragged(arr):
807826
s_outsteps.append(s)
808827
s_times.append(self.file[s]["Time"][()])
809828
s_steps.append(self.file[s]["Step"][()])
810-
829+
830+
s_outsteps = sorted(s_outsteps, key=lambda x: int(x.replace("Step", "")))
831+
s_steps = sorted(s_steps)
832+
s_times = np.array(sorted(s_times), dtype=np.float64)
833+
811834
self._spectra = xr.Dataset()
812835
log_bins = self.file.attrs["output.spectra.log_bins"]
813836

814837
if len(s_outsteps) > 0:
815838
species = np.unique(
816-
[int(pq.split("_")[1]) for pq in self.file[s_outsteps[0]].keys() if pq.startswith("sN")]
839+
[
840+
int(pq.split("_")[1])
841+
for pq in self.file[s_outsteps[0]].keys()
842+
if pq.startswith("sN")
843+
]
817844
)
818845
e_bins = self.file[s_outsteps[0]]["sEbn"]
819846
if log_bins:
@@ -839,8 +866,6 @@ def list_to_ragged(arr):
839866
)
840867
self._spectra[f"n_{sp}"] = x
841868

842-
843-
844869
def __del__(self):
845870
self.file.close()
846871

@@ -855,15 +880,11 @@ def __enter__(self):
855880

856881
def __exit__(self, exc_type, exc_value, traceback):
857882
self.file.close()
858-
self.close()
859-
for _, v in self._particles.items():
860-
del v
861-
del self
862883

863884
@property
864885
def particles(self):
865886
return self._particles
866-
887+
867888
@property
868889
def spectra(self):
869890
return self._spectra

0 commit comments

Comments
 (0)