/usr/share/pyshared/fabio/raxisimage.py is in python-fabio 0.1.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 | #!/usr/bin/env python
#coding: utf8
"""
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
"""
# Get ready for python3:
from __future__ import with_statement, print_function, division
__authors__ = ["Brian R. Pauw"]
__contact__ = "brian@stack.nl"
__license__ = "GPLv3+"
__copyright__ = ""
__version__ = "23 Nov 2013"
import logging, struct, os
import numpy
from .fabioimage import fabioimage
logger = logging.getLogger("raxisimage")
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.
"""
def __init__(self, *arg, **kwargs):
"""
Generic constructor
"""
fabioimage.__init__(self, *arg, **kwargs)
self.data = None
self.header = {}
self.dim1 = self.dim2 = 0
self.m = self.maxval = self.stddev = self.minval = None
self.header_keys = self.header.keys()
self.bytecode = '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)
RKey, orderList = self.rigakuKeys()
self.header = {}
self.header_keys = orderList
#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
self.header = dict()
curByte = 0
for key in orderList:
if isinstance(RKey[key], int):
# read a number of bytes, convert to char.
#if -1, read remainder of header
if RKey[key] == -1:
rByte = len(rawHead) - curByte
self.header[key] = struct.unpack(fs + str(rByte) + 's',
rawHead[curByte : curByte + rByte])[0]
curByte += rByte
break
rByte = RKey[key]
self.header[key] = struct.unpack(fs + str(rByte) + 's',
rawHead[curByte : curByte + rByte])[0]
curByte += rByte
elif RKey[key] == 'float':
#read a float, 4 bytes
rByte = 4
self.header[key] = struct.unpack(fs + 'f',
rawHead[curByte : curByte + rByte])[0]
curByte += rByte
elif RKey[key] == 'long':
#read a long, 4 bytes
rByte = 4
self.header[key] = struct.unpack(fs + 'l',
rawHead[curByte : curByte + rByte])[0]
curByte += rByte
else:
logging.warn('special header data type {} not understood'
.format(Rkey[key]))
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:
"""
endianness = self.endianness
self.resetvals()
infile = self._open(fname, 'rb')
offset = -1 #read from EOF backward
try:
self._readheader(infile)
except:
raise
#we read the required bytes from the end of file, using code
#lifted from binaryimage
#read the image data
self.dim1 = self.header['X Pixels']
self.dim2 = self.header['Y Pixels']
self.bytecode = numpy.uint16
dims = [self.dim2, self.dim1]
bpp = numpy.dtype(self.bytecode).itemsize
size = dims[0] * dims[1] * bpp
if offset >= 0:
infile.seek(offset)
else:
try:
if "getSize" in dir(infile): #Handle specifically gzip ... works also for bzip2
filesize = infile.getSize()
infile.seek(filesize - size) #seek from EOF backwards
else:
infile.seek(-size + offset + 1 , os.SEEK_END) #seek from EOF backwards
# infile.seek(-size + offset + 1 , os.SEEK_END) #seek from EOF backwards
except IOError as error:
logger.warn('expected datablock too large, please check bytecode settings: %s, IOError: %s' % (self.bytecode, error))
except Exception as error:
logger.error('Uncommon error encountered when reading file: %s' % error)
rawData = infile.read(size)
if self.swap_needed():
data = numpy.fromstring(rawData, self.bytecode).byteswap().reshape(tuple(dims))
else:
data = numpy.fromstring(rawData, self.bytecode).reshape(tuple(dims))
# print(data)
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.bytecode = numpy.uint32
data = data.astype(self.bytecode)
#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
self.data = data
return self
def rigakuKeys(self):
#returns dict of keys and keyLengths
RKey = {
'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,
}
#make a list with the items in the right order
orderList = [
'InstrumentType',
'Version',
'Crystal Name',
'Crystal System',
'A',
'B',
'C',
'Alpha',
'Beta',
'Gamma',
'Space Group',
'Mosaicity',
'Memo',
'Date',
'Reserved Space 1',
'User',
'Xray Target',
'Wavelength',
'Monochromator',
'Monochromator 2theta',
'Collimator',
'Filter',
'Crystal-to-detector Distance',
'Generator Voltage',
'Generator Current',
'Focus',
'Xray Memo',
'IP shape',
'Oscillation Type',
'Reserved Space 2',
'Crystal Mount (spindle axis)',
'Crystal Mount (beam axis)',
'Phi Datum',
'Phi Oscillation Start',
'Phi Oscillation Stop',
'Frame Number',
'Exposure Time',
'Direct beam X position',
'Direct beam Y position',
'Omega Angle',
'Chi Angle',
'2Theta Angle',
'Mu Angle',
'Image Template',
'X Pixels',
'Y Pixels',
'X Pixel Length',
'Y Pixel Length',
'Record Length',
'Total',
'Starting Line',
'IP Number',
'Photomultiplier Ratio',
'Fade Time (to start of read)',
'Fade Time (to end of read)',
'Host Type/Endian',
'IP Type',
'Horizontal Scan',
'Vertical Scan',
'Front/Back Scan',
'Pixel Shift (RAXIS V)',
'Even/Odd Intensity Ratio (RAXIS V)',
'Magic number',
'Number of Axes',
'Goniometer Vector ax.1.1',
'Goniometer Vector ax.1.2',
'Goniometer Vector ax.1.3',
'Goniometer Vector ax.2.1',
'Goniometer Vector ax.2.2',
'Goniometer Vector ax.2.3',
'Goniometer Vector ax.3.1',
'Goniometer Vector ax.3.2',
'Goniometer Vector ax.3.3',
'Goniometer Vector ax.4.1',
'Goniometer Vector ax.4.2',
'Goniometer Vector ax.4.3',
'Goniometer Vector ax.5.1',
'Goniometer Vector ax.5.2',
'Goniometer Vector ax.5.3',
'Goniometer Start ax.1',
'Goniometer Start ax.2',
'Goniometer Start ax.3',
'Goniometer Start ax.4',
'Goniometer Start ax.5',
'Goniometer End ax.1',
'Goniometer End ax.2',
'Goniometer End ax.3',
'Goniometer End ax.4',
'Goniometer End ax.5',
'Goniometer Offset ax.1',
'Goniometer Offset ax.2',
'Goniometer Offset ax.3',
'Goniometer Offset ax.4',
'Goniometer Offset ax.5',
'Goniometer Scan Axis',
'Axes Names',
'file',
'cmnt',
'smpl',
'iext',
'reso',
'save',
'dint',
'byte',
'init',
'ipus',
'dexp',
'expn',
'posx',
'posy',
'xray',
'Header Leftovers'
]
return RKey, orderList
|