generated from BCDA-APS/tiled-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec_data.py
145 lines (128 loc) · 4.11 KB
/
spec_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""Read the SPEC data file format."""
from spec2nexus import spec
from tiled.adapters.array import ArrayAdapter
from tiled.adapters.mapping import MapAdapter
import datetime
import numpy
EXTENSIONS = [] # no uniform standard exists, many common patterns
MIMETYPE = "text/spec_data"
def read_diffractometer_metadata(diffractometer):
simple_attrs = """
UB
geometry_name
geometry_name_full
mode
sector
variant
wavelength
""".split()
# fmt: off
md = {
k: getattr(diffractometer, k)
for k in simple_attrs
if hasattr(diffractometer, k)
}
# special cases
def has(parent, attr):
return hasattr(parent, attr) and len(getattr(parent, attr)) > 0
if has(diffractometer, "reflections"):
md["reflections"] = {
f"R{r}": refl._asdict()
for r, refl in enumerate(diffractometer.reflections)
}
if has(diffractometer, "geometry_parameters"):
md["geometry_parameters"] = {
k: dict(
key=v.key,
description=v.description,
value=v.value,
)
for k, v in diffractometer.geometry_parameters.items()
}
if has(diffractometer, "lattice"):
md["lattice"] = diffractometer.lattice._asdict()
# fmt: on
return md
def read_spec_scan(scan):
try:
arrays = {
k: ArrayAdapter.from_array(numpy.array(v))
# TODO: xref name as metadata?
for k, v in scan.data.items()
}
# fmt: off
attrs = """
G L M S
column_first column_last
date epoch metadata positioner
scanCmd scanNum time_name
""".split()
md = {
k: getattr(scan, k)
for k in attrs
if hasattr(scan, k)
}
if hasattr(scan, "diffractometer"):
md.update(read_diffractometer_metadata(scan.diffractometer))
# fmt: on
except ValueError as exc:
arrays = {}
md = dict(ValueError=exc, disposition="skipping")
return MapAdapter(arrays, metadata=md)
def read_spec_data(filename):
if not spec.is_spec_file_with_header(filename):
raise spec.NotASpecDataFile(str(filename))
sdf = spec.SpecDataFile(str(filename))
md = dict(
fileName=str(sdf.fileName),
specFile=str(sdf.specFile),
)
# header metadata (sdf.headers is a list)
if hasattr(sdf, "headers") and len(sdf.headers) > 0:
md["headers"] = {}
for h, header in enumerate(sdf.headers, start=1):
h_md = md["headers"][f"H{h}"] = {}
for key in "date epoch counter_xref positioner_xref".split():
if hasattr(header, key):
h_md[key] = getattr(header, key)
if hasattr(header, "file"):
h_md["file"] = str(header.file)
if hasattr(header, "epoch"):
h_md["iso8601"] = f"{datetime.datetime.fromtimestamp(header.epoch)}"
if hasattr(header, "comments") and len(header.comments) > 0:
h_md["comments"] = {
f"C{c}": comment
for c, comment in enumerate(header.comments, start=1)
}
# fmt: off
scans = {
f"S{scan_number}": read_spec_scan(scan)
for scan_number, scan in sdf.scans.items()
}
# fmt: on
return MapAdapter(scans, metadata=md)
def main():
import pathlib
spec2nexus_data_path = (
pathlib.Path().home()
/ "Documents"
/ "projects"
/ "prjemian"
/ "spec2nexus"
/ "src"
/ "spec2nexus"
/ "data"
)
test_data_path = pathlib.Path(__file__).parent / "data"
sixc_data_path = test_data_path / "diffractometer" / "sixc"
usaxs_data_path = test_data_path / "usaxs" / "2019"
path = usaxs_data_path
for filename in sorted(path.iterdir()):
print(f"{filename.name=}")
try:
structure = read_spec_data(filename)
print(f"{structure}")
except spec.NotASpecDataFile:
pass
if __name__ == "__main__":
main()