-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathimage_readers.py
352 lines (277 loc) · 10.7 KB
/
image_readers.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
from __future__ import annotations
import os
import time
from dxtbx.model.experiment_list import ExperimentListFactory
from fast_dp.image_names import image2template_directory
def check_file_readable(filename):
"""Check that the file filename exists and that it can be read. Returns
only if everything is OK.
"""
if not os.path.exists(filename):
raise RuntimeError("file %s not found" % filename)
if not os.access(filename, os.R_OK):
raise RuntimeError("file %s not readable" % filename)
def get_dectris_serial_no(record):
if "S/N" not in record:
return "0"
tokens = record.split()
return tokens[tokens.index("S/N") + 1]
def find_hdf5_lib(lib_name=None):
if not hasattr(find_hdf5_lib, "cache"):
lib_name = lib_name or "durin-plugin.so"
for d in os.environ["PATH"].split(os.pathsep):
if os.path.exists(os.path.join(d, lib_name)):
library = os.path.join(d, lib_name)
break
else:
library = ""
find_hdf5_lib.cache = library
return find_hdf5_lib.cache
try:
import bz2
except ImportError:
bz2 = None
try:
import gzip
except ImportError:
gzip = None
def is_bz2(filename):
if ".bz2" not in filename[-4:]:
return False
return "BZh" in open(filename, "rb").read(3)
def is_gzip(filename):
if ".gz" not in filename[-3:]:
return False
magic = open(filename, "rb").read(2)
return ord(magic[0]) == 0x1F and ord(magic[1]) == 0x8B
def open_file(filename, mode="rb", url=False):
if is_bz2(filename):
if bz2 is None:
raise RuntimeError("bz2 file provided without bz2 module")
def fh_func():
return bz2.BZ2File(filename, mode)
elif is_gzip(filename):
if gzip is None:
raise RuntimeError("gz file provided without gzip module")
def fh_func():
return gzip.GzipFile(filename, mode)
else:
def fh_func():
return open(filename, mode)
return fh_func()
def XDS_INP_to_dict(inp_text):
result = {}
for record in inp_text.split("\n"):
useful = record.split("!")[0].strip()
if not useful:
continue
if useful.count("=") > 1:
# assume tokens of form key=value key=value
tokens = useful.replace("=", " ").split()
for j in range(useful.count("=")):
key = tokens[2 * j].strip()
value = tokens[2 * j + 1].strip()
# handle multiples gracelessly
if key in result:
if not isinstance(result[key], list):
result[key] = [result[key]]
result[key].append(value)
else:
result[key].append(value)
else:
result[key] = value
else:
tokens = useful.split("=")
key = tokens[0].strip()
value = tokens[1].strip()
# handle multiples gracelessly
if key in result:
if not isinstance(result[key], list):
result[key] = [result[key]]
result[key].append(value)
else:
result[key].append(value)
else:
result[key] = value
# add neggia-type plugin if needed
if result["NAME_TEMPLATE_OF_DATA_FRAMES"].endswith(".h5"):
global __lib_name
result["LIB"] = find_hdf5_lib(lib_name=__lib_name)
return result
def failover_cbf(cbf_file):
"""CBF files from the latest update to the PILATUS detector cause a
segmentation fault in diffdump. This is a workaround.
"""
header = {}
header["two_theta"] = 0.0
for record in open_file(cbf_file):
if "_array_data.data" in record:
break
if "EIGER 1M" in record.upper():
header["detector_class"] = "eiger 1M"
header["detector"] = "dectris"
header["size"] = (1065, 1030)
header["serial_number"] = get_dectris_serial_no(record)
continue
if "EIGER 4M" in record.upper():
header["detector_class"] = "eiger 4M"
header["detector"] = "dectris"
header["size"] = (2176, 2070)
header["serial_number"] = get_dectris_serial_no(record)
continue
if "EIGER 9M" in record.upper():
header["detector_class"] = "eiger 9M"
header["detector"] = "dectris"
header["size"] = (3269, 3110)
header["serial_number"] = get_dectris_serial_no(record)
continue
if "EIGER 16M" in record.upper():
header["detector_class"] = "eiger 16M"
header["detector"] = "dectris"
header["size"] = (4371, 4150)
header["serial_number"] = get_dectris_serial_no(record)
continue
if "PILATUS 2M" in record:
header["detector_class"] = "pilatus 2M"
header["detector"] = "dectris"
header["size"] = (1679, 1475)
header["serial_number"] = get_dectris_serial_no(record)
continue
if "PILATUS 6M" in record:
header["detector_class"] = "pilatus 6M"
header["detector"] = "dectris"
header["size"] = (2527, 2463)
header["serial_number"] = get_dectris_serial_no(record)
continue
if "PILATUS3 6M" in record:
header["detector_class"] = "pilatus 6M"
header["detector"] = "dectris"
header["size"] = (2527, 2463)
header["serial_number"] = get_dectris_serial_no(record)
continue
if "PILATUS 12M" in record:
header["detector_class"] = "pilatus 12M"
header["detector"] = "dectris"
header["size"] = (5071, 2463)
header["serial_number"] = get_dectris_serial_no(record)
continue
if "Detector: ADSC HF-4M" in record:
header["detector_class"] = "adsc 4M"
header["detector"] = "adsc-pad"
header["size"] = (2290, 2100)
header["serial_number"] = record.replace(",", "").split()[-1]
continue
if "Start_angle" in record:
header["phi_start"] = float(record.split()[-2])
continue
if "Angle_increment" in record:
header["phi_width"] = float(record.split()[-2])
header["phi_end"] = header["phi_start"] + header["phi_width"]
header["oscillation"] = header["phi_start"], header["phi_width"]
continue
if "Exposure_period" in record:
header["exposure_time"] = float(record.split()[-2])
continue
if "Detector_distance" in record:
header["distance"] = 1000 * float(record.split()[2])
continue
if "Oscillation_axis" in record:
header["oscillation_axis"] = record.split("axis")[-1].strip()
if "Wavelength" in record:
header["wavelength"] = float(record.split()[-2])
continue
if "Pixel_size" in record:
header["pixel"] = (
1000 * float(record.split()[2]),
1000 * float(record.split()[5]),
)
continue
if "Count_cutoff" in record:
header["saturation"] = int(record.split()[2])
if "Silicon sensor" in record:
header["sensor"] = 1000 * float(record.split()[4])
if "Beam_xy" in record:
beam_pixels = map(
float,
record.replace("(", "").replace(")", "").replace(",", "").split()[2:4],
)
# for CBF images need to swap these to put in XDS mosflm
# coordinate frame...
header["beam"] = (
beam_pixels[0] * header["pixel"][0],
beam_pixels[1] * header["pixel"][1],
)
continue
# try to get the date etc. literally.
try:
datestring = record.split()[-1].split(".")[0]
format = "%Y-%b-%dT%H:%M:%S"
struct_time = time.strptime(datestring, format)
header["date"] = time.asctime(struct_time)
header["epoch"] = time.mktime(struct_time)
except Exception:
pass
try:
datestring = record.split()[-1].split(".")[0]
format = "%Y-%m-%dT%H:%M:%S"
struct_time = time.strptime(datestring, format)
header["date"] = time.asctime(struct_time)
header["epoch"] = time.mktime(struct_time)
except Exception:
pass
try:
datestring = record.replace("#", "").strip().split(".")[0]
format = "%Y/%b/%d %H:%M:%S"
struct_time = time.strptime(datestring, format)
header["date"] = time.asctime(struct_time)
header["epoch"] = time.mktime(struct_time)
except Exception:
pass
# cope with vertical goniometer on I24 @ DLS from 2015/1/1
# and possible alternative horizontal gonio if +FAST
if (
header.get("serial_number", "0") == "60-0119"
and int(header["date"].split()[-1]) >= 2015
):
if "+FAST" in header.get("oscillation_axis", ""):
header["goniometer_is_vertical"] = False
else:
header["goniometer_is_vertical"] = True
else:
if "goniometer_is_vertical" not in header:
header["goniometer_is_vertical"] = False
return header
__lib_name = None
def set_lib_name(lib_name):
global __lib_name
__lib_name = lib_name
def read_image_metadata_dxtbx(image):
"""Read the image header and send back the resulting metadata in a
dictionary. Read this using dxtbx - for a sequence of images use the
first image in the sequence to derive the metadata, for HDF5 files
just get on an read.
"""
check_file_readable(image)
if image.endswith(".h5"):
# XDS can literally only handle master files called (prefix)_master.h5
assert "master" in image
expt = ExperimentListFactory.from_filenames([image])[0]
else:
template, directory = image2template_directory(image)
full_template = os.path.join(directory, template)
expt = ExperimentListFactory.from_templates(
[full_template], allow_incomplete_sweeps=True
)[0]
from dxtbx.serialize.xds import to_xds
XDS_INP = to_xds(expt.imageset).XDS_INP()
params = XDS_INP_to_dict(XDS_INP)
# remove things we will want to guarantee we set in fast_dp
for name in ["BACKGROUND_RANGE", "SPOT_RANGE", "JOB"]:
if name in params:
del params[name]
return params
if __name__ == "__main__":
import sys
md = read_image_metadata_dxtbx(sys.argv[1])
for name in sorted(md):
print(name, md[name])