|
2 | 2 |
|
3 | 3 |
|
4 | 4 | class NexusEntry(dict):
|
| 5 | + """Read and write NeXus data files. |
| 6 | +
|
| 7 | + Methods: |
| 8 | + from_nexus |
| 9 | + to_nexus |
| 10 | + get |
| 11 | + """ |
5 | 12 |
|
6 | 13 | @staticmethod
|
7 | 14 | def _getitem_recursively(obj: dict, key: str, ATTRS: bool):
|
@@ -86,27 +93,51 @@ def _read_recursively(nexus_entry, items=None):
|
86 | 93 |
|
87 | 94 | return items
|
88 | 95 |
|
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 |
93 | 99 |
|
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 |
97 | 102 | """
|
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()]) |
103 | 106 |
|
104 | 107 | @classmethod
|
105 | 108 | 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 | + """ |
106 | 114 | with h5py.File(path_to_nexus, "r") as nexus_file:
|
107 | 115 | nexus_dict = NexusEntry._read_recursively(nexus_file)
|
108 | 116 | return cls([(key, val) for key, val in nexus_dict.items()])
|
109 | 117 |
|
110 | 118 | 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 | + """ |
111 | 124 | with h5py.File(path_to_nexus, "w") as nexus_file:
|
112 | 125 | 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