File: raxisimage.py

package info (click to toggle)
python-fabio 0.11.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,092 kB
  • sloc: python: 19,244; ansic: 1,085; makefile: 219; sh: 215
file content (333 lines) | stat: -rw-r--r-- 12,576 bytes parent folder | download | duplicates (4)
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
# coding: utf-8
#
#    Project: X-ray image reader
#             https://github.com/silx-kit/fabio
#
#    Principal author:       "Brian R. Pauw" "brian@stack.nl"
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE

"""

Authors: Brian R. Pauw
email:  brian@stack.nl

Written using information gleaned from the ReadRAXISImage program
written by T. L. Hendrixson, made available by Rigaku Americas.
Available at: http://www.rigaku.com/downloads/software/readimage.html


"""

__authors__ = ["Brian R. Pauw"]
__contact__ = "brian@stack.nl"
__license__ = "MIT"
__copyright__ = "Brian R. Pauw"
__date__ = "03/04/2020"

import logging
import struct
import os
import numpy
from .fabioimage import FabioImage
from .fabioutils import OrderedDict
logger = logging.getLogger(__name__)

RIGAKU_KEYS = OrderedDict([
    ('InstrumentType', 10),
    ('Version', 10),
    ('Crystal Name', 20),
    ('Crystal System', 12),
    ('A', 'float'),
    ('B', 'float'),
    ('C', 'float'),
    ('Alpha', 'float'),
    ('Beta', 'float'),
    ('Gamma', 'float'),
    ('Space Group', 12),
    ('Mosaicity', 'float'),
    ('Memo', 80),
    ('Date', 12),
    ('Reserved Space 1', 84),
    ('User', 20),
    ('Xray Target', 4),
    ('Wavelength', 'float'),
    ('Monochromator', 20),
    ('Monochromator 2theta', 'float'),
    ('Collimator', 20),
    ('Filter', 4),
    ('Crystal-to-detector Distance', 'float'),
    ('Generator Voltage', 'float'),
    ('Generator Current', 'float'),
    ('Focus', 12),
    ('Xray Memo', 80),
    ('IP shape', 'long'),  # 1= cylindrical, 0=flat. A "long" is overkill.
    ('Oscillation Type', 'float'),  # 1=weissenberg. else regular. "float"? really?
    ('Reserved Space 2', 56),
    ('Crystal Mount (spindle axis)', 4),
    ('Crystal Mount (beam axis)', 4),
    ('Phi Datum', 'float'),  # degrees
    ('Phi Oscillation Start', 'float'),  # deg
    ('Phi Oscillation Stop', 'float'),  # deg
    ('Frame Number', 'long'),
    ('Exposure Time', 'float'),  # minutes
    ('Direct beam X position', 'float'),  # special, x,y
    ('Direct beam Y position', 'float'),  # special, x,y
    ('Omega Angle', 'float'),  # omega angle
    ('Chi Angle', 'float'),  # omega angle
    ('2Theta Angle', 'float'),  # omega angle
    ('Mu Angle', 'float'),  # omega angle
    ('Image Template', 204),  # used for storing scan template..
    ('X Pixels', 'long'),
    ('Y Pixels', 'long'),
    ('X Pixel Length', 'float'),  # mm
    ('Y Pixel Length', 'float'),  # mm
    ('Record Length', 'long'),
    ('Total', 'long'),
    ('Starting Line', 'long'),
    ('IP Number', 'long'),
    ('Photomultiplier Ratio', 'float'),
    ('Fade Time (to start of read)', 'float'),
    ('Fade Time (to end of read)', 'float'),  # good that they thought of this, but is it applied?
    ('Host Type/Endian', 10),
    ('IP Type', 10),
    ('Horizontal Scan', 'long'),  # 0=left->Right, 1=Rigth->Left
    ('Vertical Scan', 'long'),  # 0=down->up, 1=up->down
    ('Front/Back Scan', 'long'),  # 0=front, 1=back
    ('Pixel Shift (RAXIS V)', 'float'),
    ('Even/Odd Intensity Ratio (RAXIS V)', 'float'),
    ('Magic number', 'long'),  # 'RAPID'-specific
    ('Number of Axes', 'long'),
    ('Goniometer Vector ax.1.1', 'float'),
    ('Goniometer Vector ax.1.2', 'float'),
    ('Goniometer Vector ax.1.3', 'float'),
    ('Goniometer Vector ax.2.1', 'float'),
    ('Goniometer Vector ax.2.2', 'float'),
    ('Goniometer Vector ax.2.3', 'float'),
    ('Goniometer Vector ax.3.1', 'float'),
    ('Goniometer Vector ax.3.2', 'float'),
    ('Goniometer Vector ax.3.3', 'float'),
    ('Goniometer Vector ax.4.1', 'float'),
    ('Goniometer Vector ax.4.2', 'float'),
    ('Goniometer Vector ax.4.3', 'float'),
    ('Goniometer Vector ax.5.1', 'float'),
    ('Goniometer Vector ax.5.2', 'float'),
    ('Goniometer Vector ax.5.3', 'float'),
    ('Goniometer Start ax.1', 'float'),
    ('Goniometer Start ax.2', 'float'),
    ('Goniometer Start ax.3', 'float'),
    ('Goniometer Start ax.4', 'float'),
    ('Goniometer Start ax.5', 'float'),
    ('Goniometer End ax.1', 'float'),
    ('Goniometer End ax.2', 'float'),
    ('Goniometer End ax.3', 'float'),
    ('Goniometer End ax.4', 'float'),
    ('Goniometer End ax.5', 'float'),
    ('Goniometer Offset ax.1', 'float'),
    ('Goniometer Offset ax.2', 'float'),
    ('Goniometer Offset ax.3', 'float'),
    ('Goniometer Offset ax.4', 'float'),
    ('Goniometer Offset ax.5', 'float'),
    ('Goniometer Scan Axis', 'long'),
    ('Axes Names', 40),
    ('file', 16),
    ('cmnt', 20),
    ('smpl', 20),
    ('iext', 'long'),
    ('reso', 'long'),
    ('save', 'long'),
    ('dint', 'long'),
    ('byte', 'long'),
    ('init', 'long'),
    ('ipus', 'long'),
    ('dexp', 'long'),
    ('expn', 'long'),
    ('posx', 20),
    ('posy', 20),
    ('xray', 'long'),
    # more values can be added here
    ('Header Leftovers', -1)
])


class RaxisImage(FabioImage):
    """
    FabIO image class to read Rigaku RAXIS image files.
    Write functions are not planned as there are plenty of more suitable
    file formats available for storing detector data.
    In particular, the MSB used in Rigaku files is used in an uncommon way:
    it is used as a *multiply-by* flag rather than a normal image value bit.
    While it is said to multiply by the value specified in the header, there
    is at least one case where this is found not to hold, so YMMV and be careful.
    """

    DESCRIPTION = "Rigaku RAXIS file format"

    DEFAULT_EXTENSIONS = ["img"]

    def __init__(self, *arg, **kwargs):
        """
        Generic constructor
        """
        FabioImage.__init__(self, *arg, **kwargs)
        self._dtype = numpy.dtype('uint16')  # same for all RAXIS images AFAICT
        self.endianness = '>'  # this may be tested for.

    def swap_needed(self):
        """not sure if this function is needed"""
        endian = self.endianness
        # Decide if we need to byteswap
        if (endian == '<' and numpy.little_endian) or (endian == '>' and not numpy.little_endian):
            return False
        if (endian == '>' and numpy.little_endian) or (endian == '<' and not numpy.little_endian):
            return True

    def _readheader(self, infile):
        """
        Read and decode the header of a Rigaku RAXIS image.
        The Rigaku format uses a block of (at least) 1400 bytes for storing
        information. The information has a fixed structure, but endianness
        can be flipped for non-char values. Header items which are not
        capitalised form part of a non-standardized data block and may not
        be accurate.

        TODO: It would be useful to have an automatic endianness test in here.

        :param infile: Opened python file (can be stringIO or bzipped file)
        """
        endianness = self.endianness
        # list of header key to keep the order (when writing)
        self.header = self.check_header()

        # swapBool=False
        fs = endianness
        minHeaderLength = 1400  # from rigaku's def
        # if (numpy.little_endian and endianness=='>'):
        #    swapBool=True
        # file should be open already
        # fh=open(filename,'rb')
        infile.seek(0)  # hopefully seeking works.
        rawHead = infile.read(minHeaderLength)
        # fh.close() #don't like open files in case of intermediate crash

        curByte = 0
        for key, kind in RIGAKU_KEYS.items():
            if isinstance(kind, int):
                # read a number of bytes, convert to char.
                # if -1, read remainder of header
                if kind == -1:
                    rByte = len(rawHead) - curByte
                    self.header[key] = struct.unpack(fs + str(rByte) + 's',
                                                     rawHead[curByte: curByte + rByte])[0]
                    curByte += rByte
                    break

                rByte = kind
                self.header[key] = struct.unpack(fs + str(rByte) + 's',
                                                 rawHead[curByte: curByte + rByte])[0]
                curByte += rByte
            elif kind == 'float':
                # read a float, 4 bytes
                rByte = 4
                self.header[key] = struct.unpack(fs + 'f',
                                                 rawHead[curByte: curByte + rByte])[0]
                curByte += rByte
            elif kind == 'long':
                # read a long, 4 bytes
                rByte = 4
                self.header[key] = struct.unpack(fs + 'l',
                                                 rawHead[curByte: curByte + rByte])[0]
                curByte += rByte
            else:
                logger.warning('special header data type %s not understood', kind)
            if len(rawHead) == curByte:
                # "end reached"
                break

    def read(self, fname, frame=None):
        """
        try to read image
        :param fname: name of the file
        :param frame:
        """
        self.resetvals()
        infile = self._open(fname, 'rb')
        offset = -1  # read from EOF backward
        self._readheader(infile)

        # we read the required bytes from the end of file, using code
        # lifted from binaryimage
        # read the image data

        dim1 = self.header['X Pixels']
        dim2 = self.header['Y Pixels']
        self._shape = dim2, dim1

        self._dtype = numpy.dtype(numpy.uint16)
        shape = self.shape
        size = shape[0] * shape[1] * self._dtype.itemsize
        if offset >= 0:
            infile.seek(offset)
        else:
            try:
                attrs = dir(infile)
                if "measure_size" in attrs:  # Handle specifically gzip
                    infile.seek(infile.measure_size() - size)  # seek from EOF backwards
                elif "size" in attrs:
                    infile.seek(infile.size - size)  # seek from EOF backwards
                if "len" in attrs:
                    infile.seek(infile.len - size)  # seek from EOF backwards
                else:
                    infile.seek(-size + offset + 1, os.SEEK_END)  # seek from EOF backwards
            except IOError as error:
                logger.warning('expected datablock too large, please check bytecode settings: %s, IOError: %s' % (self._dtype.type, error))
            except Exception as error:
                logger.error('Uncommon error encountered when reading file: %s' % error)
        rawData = infile.read(size)
        data = numpy.frombuffer(rawData, self._dtype).copy().reshape(shape)
        if self.swap_needed():
            data.byteswap(True)
        di = (data >> 15) != 0  # greater than 2^15
        if di.sum() >= 1:
            # find indices for which we need to do the correction (for which
            # the 16th bit is set):

            logger.debug("Correct for PM: %s" % di.sum())
            data = data << 1 >> 1  # reset bit #15 to zero
            self._dtype = numpy.dtype(numpy.uint32)
            data = data.astype(self._dtype)
            # Now we do some fixing for Rigaku's refusal to adhere to standards:
            sf = self.header['Photomultiplier Ratio']
            # multiply by the ratio  defined in the header
            # data[di] *= sf
            data[di] = (sf * data[di]).astype(self._dtype)

        self.data = data
        self._shape = None
        self._dtype = None
        return self

    def rigakuKeys(self):
        # returns dict of keys and keyLengths
        RKey = RIGAKU_KEYS
        orderList = list(RIGAKU_KEYS.keys())
        return RKey, orderList


raxisimage = RaxisImage