Skip to content

Commit 00b9271

Browse files
author
Bing Li
committed
implementing spice reader
1 parent 1029dc3 commit 00b9271

17 files changed

+759
-430
lines changed

scripts/HoV6Sn6_contour.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import matplotlib.pyplot as plt
22

33
from tavi.data.plotter import Plot2DManager
4-
from tavi.data.spice_to_nexus import convert_spice_to_nexus
4+
from tavi.data.scan_old.spice_to_nexus import convert_spice_to_nexus
55
from tavi.data.tavi import TAVI
66

77
spice_folder = "./test_data/exp1031/"

scripts/MnTe_contour.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import matplotlib.pylab as plt
22

33
from tavi.data.plotter import Plot2DManager
4-
from tavi.data.spice_to_nexus import convert_spice_to_nexus
4+
from tavi.data.scan_old.spice_to_nexus import convert_spice_to_nexus
55
from tavi.data.tavi import TAVI
66
from tavi.instrument.resolution.cooper_nathans import CN
77
from tavi.sample.xtal import Xtal

src/tavi/data/nxentry.py

+43-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33

44
class NexusEntry(dict):
5+
"""Read and write NeXus data files.
6+
7+
Methods:
8+
from_nexus
9+
to_nexus
10+
get
11+
"""
512

613
@staticmethod
714
def _getitem_recursively(obj: dict, key: str, ATTRS: bool):
@@ -86,27 +93,51 @@ def _read_recursively(nexus_entry, items=None):
8693

8794
return items
8895

89-
def get(self, key, ATTRS=False, default=None):
90-
"""
91-
Return dataset spicified by key regardless of the hierarchy.
92-
Return attributes instead if ATTRS is True.
96+
@classmethod
97+
def from_spice(cls, path_to_spice_folder: str) -> dict:
98+
"""return a NexusEntry instance from loading a SPICE file
9399
94-
Note:
95-
Unique keys like 's1' or 'm2' can be found straight forwardly.
96-
To find monitor or detecor data use monitor/data or detector/data
100+
Args:
101+
path_to_spice_folder (str): path to a SPICE folder
97102
"""
98-
value = NexusEntry._getitem_recursively(self, key, ATTRS)
99-
if value is not None:
100-
return value
101-
else:
102-
return default
103+
104+
nexus_dict = {}
105+
return cls([(key, val) for key, val in nexus_dict.items()])
103106

104107
@classmethod
105108
def from_nexus(cls, path_to_nexus: str) -> dict:
109+
"""return a NexusEntry instance from loading a NeXus file
110+
111+
Args:
112+
path_to_nexus (str): path to a NeXus file with the extension .h5
113+
"""
106114
with h5py.File(path_to_nexus, "r") as nexus_file:
107115
nexus_dict = NexusEntry._read_recursively(nexus_file)
108116
return cls([(key, val) for key, val in nexus_dict.items()])
109117

110118
def to_nexus(self, path_to_nexus: str) -> None:
119+
"""write a NexueEntry instance to a NeXus file
120+
121+
Args:
122+
path_to_nexus (str): path to a NeXus file with the extention .h5
123+
"""
111124
with h5py.File(path_to_nexus, "w") as nexus_file:
112125
NexusEntry._write_recursively(self, nexus_file)
126+
127+
# TODO need to catch errors when multiple IPTS are loaded in a tavi file
128+
def get(self, key, ATTRS=False, default=None):
129+
"""
130+
Return dataset spicified by key regardless of the hierarchy.
131+
Return attributes instead if ATTRS is True.
132+
133+
Args:
134+
key (str): keyword or path. e.g. "s1" or "detector/data"
135+
ATTRS (bool): return attributes if ture, dataset if false
136+
137+
Note:
138+
Unique keys like 's1' or 'm2' can be found straight forwardly.
139+
To find monitor or detecor data use monitor/data or detector/data
140+
"""
141+
value = NexusEntry._getitem_recursively(self, key, ATTRS)
142+
143+
return value if value is not None else default

0 commit comments

Comments
 (0)