def read_cbf(filename, have_cbflib=1): """Read CBF output from the Pilatus detector at cSAXS. """ import os, re MAXHEADER = 4096 CBF_SIGNATURE = "###CBF: VERSION" BINARY_SIGNATURE = '\x0c\x1a\x04\xd5' (fname, file_is_temporary) = maybe_uncompress(filename) fid = open(fname) hdr = fid.read(MAXHEADER) if(hdr[0:15] != "###CBF: VERSION"): print("Warning: CBF header not present, trying to read anyway") d = det_struct() m = re.search('# Exposure_time ([0-9.]*) s', hdr) d.time = float(m.group(1)) m = re.search('conversions="([^"]*)"', hdr) compression = m.group(1) if compression != "x-CBF_BYTE_OFFSET": raise ValueError("Compression types other than x-CBF_BYTE_OFFSET are not supported") m = re.search('X-Binary-Size: ([0-9]*)', hdr) bsize = int(m.group(1)) m = re.search('X-Binary-Element-Type: "([^"]*)"', hdr) eltype = m.group(1) if eltype != "signed 32-bit integer": raise ValueError("Values types other than signed 32-bit integer are not supported") m = re.search('X-Binary-Size-Fastest-Dimension: ([0-9]*)', hdr) ydim = int(m.group(1)) m = re.search('X-Binary-Size-Second-Dimension: ([0-9]*)', hdr) xdim = int(m.group(1)) m = re.search('X-Binary-Element-Byte-Order: ([A-Z\_]*)', hdr) byteorder = m.group(1) if byteorder != "LITTLE_ENDIAN": raise ValueError("Byte orders other than little endian are not supported") m = re.search(BINARY_SIGNATURE, hdr) datastart = int(m.end(0)) if have_cbflib: fid.close() d.im = offset_decompress_cbflib(fname, [xdim, ydim]) else: fid.seek(datastart) dstr = fid.read(bsize) fid.close() d.im = offset_decompress_python(dstr, [xdim, ydim]) if file_is_temporary: os.remove(fname) return d
def read_eiger(filename, imno=None): """Read the HDF5 output of the Eiger prototype module at cSAXS. If an integer keyword argument `imno` is given, return only a single frame at that given index. Otherwise return the full image stack. """ import h5py, os (fname, file_is_temporary) = maybe_uncompress(filename) f = h5py.File(fname, "r") d = det_struct() if imno: d.im = np.array(f['eh5']['images'][imno]) else: d.im = np.array(f['eh5']['images']).squeeze() if file_is_temporary: os.remove(fname) return d