fabio 0.1.1__zip
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- Library/Python/2.7/site-packages/fabio/GEimage.py +334 -0
- Library/Python/2.7/site-packages/fabio/GEimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/GEimage_old.py +501 -0
- Library/Python/2.7/site-packages/fabio/GEimage_old.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/HiPiCimage.py +106 -0
- Library/Python/2.7/site-packages/fabio/HiPiCimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/OXDimage.py +442 -0
- Library/Python/2.7/site-packages/fabio/OXDimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/TiffIO.py +1197 -0
- Library/Python/2.7/site-packages/fabio/TiffIO.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/__init__.py +10 -0
- Library/Python/2.7/site-packages/fabio/__init__.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/adscimage.py +137 -0
- Library/Python/2.7/site-packages/fabio/adscimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/binaryimage.py +87 -0
- Library/Python/2.7/site-packages/fabio/binaryimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/bruker100image.py +96 -0
- Library/Python/2.7/site-packages/fabio/bruker100image.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/brukerimage.py +195 -0
- Library/Python/2.7/site-packages/fabio/brukerimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/byte_offset.so +0 -0
- Library/Python/2.7/site-packages/fabio/cbfimage.py +758 -0
- Library/Python/2.7/site-packages/fabio/cbfimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/cf_io.so +0 -0
- Library/Python/2.7/site-packages/fabio/compression.py +388 -0
- Library/Python/2.7/site-packages/fabio/compression.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/converters.py +54 -0
- Library/Python/2.7/site-packages/fabio/converters.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/datIO.py +60 -0
- Library/Python/2.7/site-packages/fabio/datIO.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/dm3image.py +219 -0
- Library/Python/2.7/site-packages/fabio/dm3image.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/edfimage.py +924 -0
- Library/Python/2.7/site-packages/fabio/edfimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/fabioimage.py +556 -0
- Library/Python/2.7/site-packages/fabio/fabioimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/fabioutils.py +491 -0
- Library/Python/2.7/site-packages/fabio/fabioutils.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/file_series.py +313 -0
- Library/Python/2.7/site-packages/fabio/file_series.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/fit2dmaskimage.py +94 -0
- Library/Python/2.7/site-packages/fabio/fit2dmaskimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/fit2dspreadsheetimage.py +85 -0
- Library/Python/2.7/site-packages/fabio/fit2dspreadsheetimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/kcdimage.py +131 -0
- Library/Python/2.7/site-packages/fabio/kcdimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/mar345_IO.so +0 -0
- Library/Python/2.7/site-packages/fabio/mar345image.py +320 -0
- Library/Python/2.7/site-packages/fabio/mar345image.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/marccdimage.py +309 -0
- Library/Python/2.7/site-packages/fabio/marccdimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/openimage.py +162 -0
- Library/Python/2.7/site-packages/fabio/openimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/pilatusimage.py +81 -0
- Library/Python/2.7/site-packages/fabio/pilatusimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/pnmimage.py +165 -0
- Library/Python/2.7/site-packages/fabio/pnmimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/readbytestream.py +86 -0
- Library/Python/2.7/site-packages/fabio/readbytestream.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/tifimage.py +283 -0
- Library/Python/2.7/site-packages/fabio/tifimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/xsdimage.py +134 -0
- Library/Python/2.7/site-packages/fabio/xsdimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio-0.1.1-py2.7.egg-info/PKG-INFO +11 -0
- Library/Python/2.7/site-packages/fabio-0.1.1-py2.7.egg-info/SOURCES.txt +114 -0
- Library/Python/2.7/site-packages/fabio-0.1.1-py2.7.egg-info/dependency_links.txt +1 -0
- Library/Python/2.7/site-packages/fabio-0.1.1-py2.7.egg-info/top_level.txt +4 -0
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# coding: utf8
|
|
3
|
+
"""
|
|
4
|
+
Authors: Jérôme Kieffer, ESRF
|
|
5
|
+
email:jerome.kieffer@esrf.fr
|
|
6
|
+
|
|
7
|
+
FabIO library containing compression and decompression algorithm for various
|
|
8
|
+
"""
|
|
9
|
+
__author__ = "Jérôme Kieffer"
|
|
10
|
+
__contact__ = "jerome.kieffer@esrf.eu"
|
|
11
|
+
__license__ = "GPLv3+"
|
|
12
|
+
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
import os, logging, struct, hashlib, base64, StringIO, sys
|
|
16
|
+
if sys.version_info >= (3,):
|
|
17
|
+
str = bytes
|
|
18
|
+
logger = logging.getLogger("compression")
|
|
19
|
+
import numpy
|
|
20
|
+
|
|
21
|
+
try:
|
|
22
|
+
import gzip
|
|
23
|
+
except ImportError:
|
|
24
|
+
logger.error("Unable to import gzip module: disabling gzip compression")
|
|
25
|
+
gzip = None
|
|
26
|
+
|
|
27
|
+
try:
|
|
28
|
+
import bz2
|
|
29
|
+
except ImportError:
|
|
30
|
+
logger.error("Unable to import bz2 module: disabling bz2 compression")
|
|
31
|
+
bz2 = None
|
|
32
|
+
|
|
33
|
+
try:
|
|
34
|
+
import zlib
|
|
35
|
+
except ImportError:
|
|
36
|
+
logger.error("Unable to import zlib module: disabling zlib compression")
|
|
37
|
+
zlib = None
|
|
38
|
+
|
|
39
|
+
def md5sum(blob):
|
|
40
|
+
"""
|
|
41
|
+
returns the md5sum of an object...
|
|
42
|
+
"""
|
|
43
|
+
return base64.b64encode(hashlib.md5(blob).digest())
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def endianness():
|
|
47
|
+
if numpy.little_endian:
|
|
48
|
+
return "LITTLE_ENDIAN"
|
|
49
|
+
else:
|
|
50
|
+
return "BIG_ENDIAN"
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def decGzip(stream):
|
|
54
|
+
if gzip is None:
|
|
55
|
+
raise ImportError("gzip module is not available")
|
|
56
|
+
fileobj = StringIO.StringIO(stream)
|
|
57
|
+
try:
|
|
58
|
+
rawData = gzip.GzipFile(fileobj=fileobj).read()
|
|
59
|
+
except IOError:
|
|
60
|
+
logger.warning("Encounter the python-gzip bug with trailing garbage, trying subprocess gzip")
|
|
61
|
+
try:
|
|
62
|
+
#This is as an ugly hack against a bug in Python gzip
|
|
63
|
+
import subprocess
|
|
64
|
+
sub = subprocess.Popen(["gzip", "-d", "-f"], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
65
|
+
rawData, err = sub.communicate(input=stream)
|
|
66
|
+
logger.debug("Gzip subprocess ended with %s err= %s; I got %s bytes back" % (sub.wait(), err, len(rawData)))
|
|
67
|
+
except Exception, error: #IGNORE:W0703
|
|
68
|
+
logger.warning("Unable to use the subprocess gzip (%s). Is gzip available? " % error)
|
|
69
|
+
for i in range(1, 513):
|
|
70
|
+
try:
|
|
71
|
+
fileobj = StringIO.StringIO(stream[:-i])
|
|
72
|
+
rawData = gzip.GzipFile(fileobj=fileobj).read()
|
|
73
|
+
except IOError:
|
|
74
|
+
logger.debug("trying with %s bytes less, doesn't work" % i)
|
|
75
|
+
else:
|
|
76
|
+
break
|
|
77
|
+
else:
|
|
78
|
+
logger.error("I am totally unable to read this gzipped compressed data block, giving up")
|
|
79
|
+
return rawData
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def decBzip2(stream):
|
|
83
|
+
"""
|
|
84
|
+
decompress a chunk of data using the bzip2 algorithm
|
|
85
|
+
"""
|
|
86
|
+
if bz2 is None:
|
|
87
|
+
raise ImportError("bz2 module is not available")
|
|
88
|
+
return bz2.decompress(stream)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def decZlib(stream):
|
|
92
|
+
"""
|
|
93
|
+
decompress a chunk of data using the zlib algorithm
|
|
94
|
+
"""
|
|
95
|
+
if zlib is None:
|
|
96
|
+
raise ImportError("zlib module is not available")
|
|
97
|
+
return zlib.decompress(stream)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def decByteOffet_python(stream, size):
|
|
101
|
+
"""
|
|
102
|
+
Analyze a stream of char with any length of exception (2,4, or 8 bytes integers)
|
|
103
|
+
@param stream: string representing the compressed data
|
|
104
|
+
@param size: the size of the output array (of longInts)
|
|
105
|
+
@return :NParrays
|
|
106
|
+
"""
|
|
107
|
+
logger.debug("CBF decompression using Python with Cython loops")
|
|
108
|
+
dataOut = numpy.zeros((size), dtype=numpy.int64)
|
|
109
|
+
i = 0
|
|
110
|
+
j = 0
|
|
111
|
+
last = 0
|
|
112
|
+
current = 0
|
|
113
|
+
while ((i < len(stream)) and (j < size)):
|
|
114
|
+
if (stream[i] == '\x80'):
|
|
115
|
+
if (stream[i + 1:i + 3] == "\x00\x80"):
|
|
116
|
+
if (stream[i + 3:i + 7] == "\x00\x00\x00\x80"):
|
|
117
|
+
current = struct.unpack("<q", stream[i + 7:i + 15])[0]
|
|
118
|
+
i += 15
|
|
119
|
+
else:
|
|
120
|
+
current = struct.unpack("<i", stream[i + 3:i + 7])[0]
|
|
121
|
+
i += 7
|
|
122
|
+
else:
|
|
123
|
+
current = struct.unpack("<h", stream[i + 1:i + 3])[0]
|
|
124
|
+
i += 3
|
|
125
|
+
else:
|
|
126
|
+
current = struct.unpack("<b", stream[i])[0]
|
|
127
|
+
i += 1
|
|
128
|
+
last += current
|
|
129
|
+
dataOut[j] = last
|
|
130
|
+
j += 1
|
|
131
|
+
return dataOut
|
|
132
|
+
|
|
133
|
+
def decByteOffet_weave(stream, size):
|
|
134
|
+
"""
|
|
135
|
+
Analyze a stream of char with any length of exception (2,4, or 8 bytes integers)
|
|
136
|
+
|
|
137
|
+
@return list of NParrays
|
|
138
|
+
"""
|
|
139
|
+
logger.debug("CBF decompression using Weave")
|
|
140
|
+
from scipy import weave
|
|
141
|
+
from scipy.weave import converters
|
|
142
|
+
dataIn = numpy.fromstring(stream, dtype="uint8")
|
|
143
|
+
n = dataIn.size
|
|
144
|
+
dataOut = numpy.zeros(size, dtype="int64")
|
|
145
|
+
codeC = """
|
|
146
|
+
unsigned char key = 0x80;
|
|
147
|
+
long j = 0;
|
|
148
|
+
long last=0;
|
|
149
|
+
long current=0;
|
|
150
|
+
for (int i=0; i< n; i++){
|
|
151
|
+
if (j>=size){
|
|
152
|
+
//printf("i= %i<%i, j=%i < size= %i %i\\n",i,n,j,size,dataIn(i));
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
if (dataIn(i) == key){
|
|
156
|
+
if ( (dataIn(i+1)==0) and (dataIn(i+2)==key) ){
|
|
157
|
+
if ( (dataIn(i+3)==0) and (dataIn(i+4)==0) and (dataIn(i+5)==0) and (dataIn(i+6)==key) ) {
|
|
158
|
+
// 64 bits mode
|
|
159
|
+
char tmp = dataIn(i+14) ;
|
|
160
|
+
current = (long(tmp)<<56) | (long(dataIn(i+13))<<48) | (long(dataIn(i+12))<<40) | (long(dataIn(i+11))<<32) | (long(dataIn(i+10))<<24) | (long(dataIn(i+9))<<16) | (long(dataIn(i+8))<<8) | (long(dataIn(i+7)));
|
|
161
|
+
// printf("64 bit int at pos %i, %i, value=%ld \\n",i,j,current);
|
|
162
|
+
i+=14;
|
|
163
|
+
}else{
|
|
164
|
+
// 32 bits mode
|
|
165
|
+
char tmp = dataIn(i+6) ;
|
|
166
|
+
current = (long(tmp)<<24) | (long(dataIn(i+5))<<16) | (long(dataIn(i+4))<<8) | (long(dataIn(i+3)));
|
|
167
|
+
// printf("32 bit int at pos %i, %i, value=%ld was %i %i %i %i %i %i %i\\n",i,j,current,dataIn(i),dataIn(i+1),dataIn(i+2),dataIn(i+3),dataIn(i+4),dataIn(i+5),dataIn(i+6));
|
|
168
|
+
// printf("%ld %ld %ld %ld\\n",(long(tmp)<<24) , (long(dataIn(i+5))<<16) , (long(dataIn(i+4))<<8) ,long(dataIn(i+3)));
|
|
169
|
+
i+=6;
|
|
170
|
+
}
|
|
171
|
+
}else{
|
|
172
|
+
// 16 bit mode
|
|
173
|
+
char tmp = dataIn(i+2);
|
|
174
|
+
current = (long(tmp)<<8) | (long (dataIn(i+1)));
|
|
175
|
+
// printf("16 bit int at pos %i, %i, value=%ld was %i %i %i\\n",i,j,current,dataIn(i),dataIn(i+1),dataIn(i+2));
|
|
176
|
+
i+=2;
|
|
177
|
+
}
|
|
178
|
+
}else{
|
|
179
|
+
// 8 bit mode
|
|
180
|
+
char tmp = dataIn(i) ;
|
|
181
|
+
current= long(tmp) ;
|
|
182
|
+
}
|
|
183
|
+
last+=current;
|
|
184
|
+
dataOut(j)=last;
|
|
185
|
+
j++ ;
|
|
186
|
+
}
|
|
187
|
+
return_val=0;
|
|
188
|
+
"""
|
|
189
|
+
rc = weave.inline(codeC, ["dataIn", "dataOut", "n", "size" ], verbose=2, type_converters=converters.blitz)
|
|
190
|
+
if rc != 0:
|
|
191
|
+
logger.warning("weave binary module return error code %s" % rc)
|
|
192
|
+
return dataOut
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def decByteOffet_numpy(stream, size=None):
|
|
197
|
+
"""
|
|
198
|
+
Analyze a stream of char with any length of exception:
|
|
199
|
+
2, 4, or 8 bytes integers
|
|
200
|
+
|
|
201
|
+
@return list of NParrays
|
|
202
|
+
"""
|
|
203
|
+
logger.debug("CBF decompression using Numpy")
|
|
204
|
+
listnpa = []
|
|
205
|
+
key16 = "\x80"
|
|
206
|
+
key32 = "\x00\x80"
|
|
207
|
+
key64 = "\x00\x00\x00\x80"
|
|
208
|
+
shift = 1
|
|
209
|
+
while True:
|
|
210
|
+
idx = stream.find(key16)
|
|
211
|
+
if idx == -1:
|
|
212
|
+
listnpa.append(numpy.fromstring(stream, dtype="int8"))
|
|
213
|
+
break
|
|
214
|
+
listnpa.append(numpy.fromstring(stream[:idx], dtype="int8"))
|
|
215
|
+
|
|
216
|
+
if stream[idx + 1:idx + 3] == key32:
|
|
217
|
+
if stream[idx + 3:idx + 7] == key64:
|
|
218
|
+
# long int 64 bits
|
|
219
|
+
listnpa.append(numpy.fromstring(stream[idx + 7:idx + 15],
|
|
220
|
+
dtype="int64"))
|
|
221
|
+
shift = 15
|
|
222
|
+
else: #32 bit int
|
|
223
|
+
listnpa.append(numpy.fromstring(stream[idx + 3:idx + 7],
|
|
224
|
+
dtype="int32"))
|
|
225
|
+
shift = 7
|
|
226
|
+
else: #int16
|
|
227
|
+
listnpa.append(numpy.fromstring(stream[idx + 1:idx + 3],
|
|
228
|
+
dtype="int16"))
|
|
229
|
+
shift = 3
|
|
230
|
+
stream = stream[idx + shift:]
|
|
231
|
+
return (numpy.hstack(listnpa)).astype("int64").cumsum()
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def decByteOffet_cython(stream, size=None):
|
|
235
|
+
"""
|
|
236
|
+
Analyze a stream of char with any length of exception:
|
|
237
|
+
2, 4, or 8 bytes integers
|
|
238
|
+
|
|
239
|
+
@return list of NParrays
|
|
240
|
+
"""
|
|
241
|
+
logger.debug("CBF decompression using cython")
|
|
242
|
+
try:
|
|
243
|
+
from fabio.byte_offset import analyseCython
|
|
244
|
+
except ImportError, error:
|
|
245
|
+
logger.error("Failed to import byte_offset cython module, falling back on numpy method")
|
|
246
|
+
return decByteOffet_numpy(stream, size)
|
|
247
|
+
else:
|
|
248
|
+
return analyseCython(stream, size)
|
|
249
|
+
|
|
250
|
+
def compByteOffet_numpy(data):
|
|
251
|
+
"""
|
|
252
|
+
Compress a dataset into a string using the byte_offet algorithm
|
|
253
|
+
@param data: ndarray
|
|
254
|
+
@return: string/bytes with compressed data
|
|
255
|
+
|
|
256
|
+
test = numpy.array([0,1,2,127,0,1,2,128,0,1,2,32767,0,1,2,32768,0,1,2,2147483647,0,1,2,2147483648,0,1,2,128,129,130,32767,32768,128,129,130,32768,2147483647,2147483648])
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
"""
|
|
260
|
+
flat = data.astype("int64").ravel()
|
|
261
|
+
delta = numpy.zeros_like(flat)
|
|
262
|
+
delta[0] = flat[0]
|
|
263
|
+
delta[1:] = flat[1:] - flat[:-1]
|
|
264
|
+
mask = ((delta > 127) + (delta < -127))
|
|
265
|
+
exceptions = numpy.nonzero(mask)[0]
|
|
266
|
+
if numpy.little_endian:
|
|
267
|
+
byteswap = False
|
|
268
|
+
else:
|
|
269
|
+
byteswap = True
|
|
270
|
+
start = 0
|
|
271
|
+
binary_blob = ""
|
|
272
|
+
for stop in exceptions:
|
|
273
|
+
if stop - start > 0:
|
|
274
|
+
binary_blob += delta[start:stop].astype("int8").tostring()
|
|
275
|
+
exc = delta[stop]
|
|
276
|
+
if (exc > 2147483647) or (exc < -2147483647): #2**31-1
|
|
277
|
+
binary_blob += "\x80\x00\x80\x00\x00\x00\x80"
|
|
278
|
+
if byteswap:
|
|
279
|
+
binary_blob += delta[stop:stop + 1].byteswap().tostring()
|
|
280
|
+
else:
|
|
281
|
+
binary_blob += delta[stop:stop + 1].tostring()
|
|
282
|
+
elif (exc > 32767) or (exc < -32767): #2**15-1
|
|
283
|
+
binary_blob += "\x80\x00\x80"
|
|
284
|
+
if byteswap:
|
|
285
|
+
binary_blob += delta[stop:stop + 1].astype("int32").byteswap().tostring()
|
|
286
|
+
else:
|
|
287
|
+
binary_blob += delta[stop:stop + 1].astype("int32").tostring()
|
|
288
|
+
else: #>127
|
|
289
|
+
binary_blob += "\x80"
|
|
290
|
+
if byteswap:
|
|
291
|
+
binary_blob += delta[stop:stop + 1].astype("int16").byteswap().tostring()
|
|
292
|
+
else:
|
|
293
|
+
binary_blob += delta[stop:stop + 1].astype("int16").tostring()
|
|
294
|
+
start = stop + 1
|
|
295
|
+
if start < delta.size:
|
|
296
|
+
binary_blob += delta[start:].astype("int8").tostring()
|
|
297
|
+
return binary_blob
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
def decTY1(raw_8, raw_16=None, raw_32=None):
|
|
301
|
+
"""
|
|
302
|
+
Modified byte offset decompressor used in Oxford Diffraction images
|
|
303
|
+
@param raw_8,raw_16,raw_32: strings containing raw data with integer of the given size
|
|
304
|
+
@return numpy.ndarray
|
|
305
|
+
"""
|
|
306
|
+
data = numpy.fromstring(raw_8, dtype="uint8").astype(int)
|
|
307
|
+
data -= 127
|
|
308
|
+
if raw_32 is not None:
|
|
309
|
+
int32 = numpy.fromstring(raw_32, dtype="int32").astype(int)
|
|
310
|
+
exception32 = numpy.nonzero(data == 128)
|
|
311
|
+
if raw_16 is not None:
|
|
312
|
+
int16 = numpy.fromstring(raw_16, dtype="int16").astype(int)
|
|
313
|
+
exception16 = numpy.nonzero(data == 127)
|
|
314
|
+
data[exception16] = int16
|
|
315
|
+
if raw_32:
|
|
316
|
+
data[exception32] = int32
|
|
317
|
+
summed = data.cumsum()
|
|
318
|
+
smax = summed.max()
|
|
319
|
+
if (smax > (2 ** 31 - 1)):
|
|
320
|
+
bytecode = "int64"
|
|
321
|
+
elif (smax > (2 ** 15 - 1)):
|
|
322
|
+
bytecode = "int32"
|
|
323
|
+
elif (smax > (2 ** 7 - 1)):
|
|
324
|
+
bytecode = "int16"
|
|
325
|
+
else:
|
|
326
|
+
bytecode = "int8"
|
|
327
|
+
return summed.astype(bytecode)
|
|
328
|
+
decKM4CCD = decTY1
|
|
329
|
+
|
|
330
|
+
def compTY1(data):
|
|
331
|
+
"""
|
|
332
|
+
Modified byte offset compressor used in Oxford Diffraction images
|
|
333
|
+
|
|
334
|
+
@param data numpy.ndarray
|
|
335
|
+
@return raw_8,raw_16,raw_32: strings containing raw data with integer of the given size
|
|
336
|
+
"""
|
|
337
|
+
fdata = data.flatten()
|
|
338
|
+
diff = numpy.zeros_like(fdata)
|
|
339
|
+
diff[0] = fdata[0]
|
|
340
|
+
diff[1:] = fdata[1:] - fdata[:-1]
|
|
341
|
+
adiff = abs(diff)
|
|
342
|
+
exception32 = (adiff > 32767)#2**15-1
|
|
343
|
+
exception16 = (adiff >= 127) - exception32 #2**7-1)
|
|
344
|
+
we16 = numpy.where(exception16)
|
|
345
|
+
we32 = numpy.where(exception32)
|
|
346
|
+
raw_16 = diff[we16].astype("int16").tostring()
|
|
347
|
+
raw_32 = diff[we32].astype("int32").tostring()
|
|
348
|
+
diff[we16] = 127
|
|
349
|
+
diff[we32] = 128
|
|
350
|
+
diff += 127
|
|
351
|
+
raw_8 = diff.astype("uint8").tostring()
|
|
352
|
+
return raw_8, raw_16, raw_32
|
|
353
|
+
|
|
354
|
+
def decPCK(stream, dim1=None, dim2=None, overflowPix=None):
|
|
355
|
+
"""
|
|
356
|
+
Modified CCP4 pck decompressor used in MAR345 images
|
|
357
|
+
|
|
358
|
+
@param stream: string or file
|
|
359
|
+
@return numpy.ndarray (square array)
|
|
360
|
+
"""
|
|
361
|
+
|
|
362
|
+
try:
|
|
363
|
+
from mar345_IO import uncompress_pck
|
|
364
|
+
except ImportError, error:
|
|
365
|
+
raise RuntimeError("Unable to import mar345_IO to read compressed dataset")
|
|
366
|
+
if "seek" in dir(stream):
|
|
367
|
+
stream.seek(0)
|
|
368
|
+
raw = stream.read()
|
|
369
|
+
else:
|
|
370
|
+
raw = str(stream)
|
|
371
|
+
|
|
372
|
+
return uncompress_pck(raw, dim1, dim2, overflowPix)
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
def compPCK(data):
|
|
376
|
+
"""
|
|
377
|
+
Modified CCP4 pck compressor used in MAR345 images
|
|
378
|
+
|
|
379
|
+
@param data numpy.ndarray (square array)
|
|
380
|
+
@return compressed stream
|
|
381
|
+
"""
|
|
382
|
+
try:
|
|
383
|
+
from mar345_IO import compress_pck
|
|
384
|
+
except ImportError, error:
|
|
385
|
+
raise RuntimeError("Unable to import mar345_IO to write compressed dataset")
|
|
386
|
+
return compress_pck(data)
|
|
387
|
+
|
|
388
|
+
|
|
Binary file
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
#coding: utf8
|
|
3
|
+
"""
|
|
4
|
+
Converter module.
|
|
5
|
+
This is for the moment empty (populated only with almost pass through anonymous functions)
|
|
6
|
+
but aims to be populated with more sofisticated translators ...
|
|
7
|
+
|
|
8
|
+
"""
|
|
9
|
+
__author__ = "Jérôme Kieffer"
|
|
10
|
+
__contact__ = "jerome.kieffer@esrf.eu"
|
|
11
|
+
__license__ = "GPLv3+"
|
|
12
|
+
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
|
|
13
|
+
|
|
14
|
+
import types, logging
|
|
15
|
+
logger = logging.getLogger("converter")
|
|
16
|
+
|
|
17
|
+
def convert_data_integer(data):
|
|
18
|
+
"""
|
|
19
|
+
convert data to integer
|
|
20
|
+
"""
|
|
21
|
+
if data is not None:
|
|
22
|
+
return data.astype(int)
|
|
23
|
+
else:
|
|
24
|
+
return data
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
CONVERSION_HEADER = {
|
|
28
|
+
("edfimage", "edfimage"): lambda header:header,
|
|
29
|
+
}
|
|
30
|
+
CONVERSION_DATA = {
|
|
31
|
+
("edfimage", "edfimage"): lambda data:data,
|
|
32
|
+
("edfimage", "cbfimage"): convert_data_integer,
|
|
33
|
+
("edfimage", "mar345image"): convert_data_integer,
|
|
34
|
+
("edfimage", "fit2dmaskimage"): convert_data_integer,
|
|
35
|
+
("edfimage", "kcdimage"): convert_data_integer,
|
|
36
|
+
("edfimage", "OXDimage"): convert_data_integer,
|
|
37
|
+
("edfimage", "pnmimage"): convert_data_integer,
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
def convert_data(inp, outp, data):
|
|
41
|
+
"""
|
|
42
|
+
Return data converted to the output format ... over-simplistic implementation for the moment ...
|
|
43
|
+
@param inp,outp: input/output format like "cbfimage"
|
|
44
|
+
@param data(ndarray): the actual dataset to be transformed
|
|
45
|
+
"""
|
|
46
|
+
return CONVERSION_DATA.get((inp, outp), lambda data:data)(data)
|
|
47
|
+
|
|
48
|
+
def convert_header(inp, outp, header):
|
|
49
|
+
"""
|
|
50
|
+
return header converted to the output format
|
|
51
|
+
@param inp,outp: input/output format like "cbfimage"
|
|
52
|
+
@param header(dict):the actual set of headers to be transformed
|
|
53
|
+
"""
|
|
54
|
+
return CONVERSION_HEADER.get((inp, outp), lambda header:header)(header)
|
|
Binary file
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
#coding: utf8
|
|
3
|
+
"""
|
|
4
|
+
Authors: Henning O. Sorensen & Erik Knudsen
|
|
5
|
+
Center for Fundamental Research: Metal Structures in Four Dimensions
|
|
6
|
+
Risoe National Laboratory
|
|
7
|
+
Frederiksborgvej 399
|
|
8
|
+
DK-4000 Roskilde
|
|
9
|
+
email:erik.knudsen@risoe.dk
|
|
10
|
+
|
|
11
|
+
and Jon Wright, ESRF
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
class fabiodata(object):
|
|
15
|
+
"""
|
|
16
|
+
A common class for dataIO in fable
|
|
17
|
+
Contains a 2d numpy array for keeping data, and two lists (clabels and rlabels)
|
|
18
|
+
containing labels for columns and rows respectively
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
def __init__(self, data=None, clabels=None, rlabels=None, fname=None):
|
|
22
|
+
"""
|
|
23
|
+
set up initial values
|
|
24
|
+
"""
|
|
25
|
+
if type(data) == type("string"):
|
|
26
|
+
raise Exception("fabioimage.__init__ bad argument - " + \
|
|
27
|
+
"data should be numpy array")
|
|
28
|
+
self.data = data
|
|
29
|
+
if (self.data):
|
|
30
|
+
self.dims = self.data.shape
|
|
31
|
+
self.clabels = clabels
|
|
32
|
+
self.rlabels = rlabels
|
|
33
|
+
if (fname):
|
|
34
|
+
self.read(fname)
|
|
35
|
+
|
|
36
|
+
def read(self, fname=None, frame=None):
|
|
37
|
+
"""
|
|
38
|
+
To be overridden by format specific subclasses
|
|
39
|
+
"""
|
|
40
|
+
raise Exception("Class has not implemented read method yet")
|
|
41
|
+
|
|
42
|
+
#import stuff from Jon's columnfile things
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class columnfile(fabiodata):
|
|
46
|
+
"Concrete fabiodata class"
|
|
47
|
+
def read(self, fname, frame=None):
|
|
48
|
+
import cf_io
|
|
49
|
+
try:
|
|
50
|
+
infile = open(fname, 'rb')
|
|
51
|
+
except:
|
|
52
|
+
raise Exception("columnfile: file" + str(fname) + "not found.")
|
|
53
|
+
try:
|
|
54
|
+
(self.data, self.clabels) = cf_io.read(infile)
|
|
55
|
+
except:
|
|
56
|
+
raise Exception("columnfile: read error, file " + str(fname) + " possibly corrupt")
|
|
57
|
+
self.dims = self.data.shape
|
|
58
|
+
infile.close()
|
|
59
|
+
|
|
60
|
+
|
|
Binary file
|