1
2
3 """
4
5 Authors: Henning O. Sorensen & Erik Knudsen
6 Center for Fundamental Research: Metal Structures in Four Dimensions
7 Risoe National Laboratory
8 Frederiksborgvej 399
9 DK-4000 Roskilde
10 email:henning.sorensen@risoe.dk
11
12 mods for fabio by JPW
13
14 """
15
16 import fabio
17 from fabio import deconstruct_filename, getnum
18 from fabio.fabioimage import fabioimage
19
20
21 from fabio import edfimage
22 from fabio import adscimage
23 from fabio import tifimage
24 from fabio import marccdimage
25 from fabio import mar345image
26 from fabio import fit2dmaskimage
27 from fabio import brukerimage
28 from fabio import bruker100image
29 from fabio import pnmimage
30 from fabio import GEimage
31 from fabio import OXDimage
32 from fabio import dm3image
33 from fabio import HiPiCimage
34 from fabio import pilatusimage
35 from fabio import fit2dspreadsheetimage
36 from fabio import kcdimage
37 from fabio import cbfimage
38
39
40 MAGIC_NUMBERS = [
41
42
43 ("FORMAT : 86" , 'bruker'),
44 ("\x4d\x4d\x00\x2a" , 'tif') ,
45
46
47
48 ("\x49\x49\x2a\x00\x08\x00" , 'marccd') ,
49 ("\x49\x49\x2a\x00\x82\x00" , 'pilatus') ,
50 ("\x49\x49\x2a\x00" , 'tif') ,
51
52 ("{\nHEA" , 'adsc'),
53 ("{" , 'edf'),
54 ("\r{" , 'edf'),
55 ("\n{" , 'edf'),
56 ("ADEPT" , 'GE'),
57 ("OD" , 'OXD'),
58 ("IM" , 'HiPiC'),
59 ('\x2d\x04' , 'mar345'),
60 ('\x04\x2d' , 'mar345'),
61
62 ('M\x00\x00\x00A\x00\x00\x00S\x00\x00\x00K\x00\x00\x00' , 'fit2dmask') ,
63 ('\x00\x00\x00\x03' , 'dm3'),
64 ("No" , "kcd")
65 ]
66
68 """ Try to interpret the bytes starting the file as a magic number """
69 for magic, format in MAGIC_NUMBERS:
70 if byts.find(magic) == 0:
71 return format
72 if 0:
73 print "m:", magic, "f:", format,
74 print "bytes:", magic, "len(bytes)", len(magic),
75 print "found:", byts.find(magic)
76 for i in range(len(magic)):
77 print ord(magic[i]), ord(byts[i]), magic[i], byts[i]
78 raise Exception("Could not interpret magic string")
79
80
98
99
101 """ return only the header"""
102 obj = _openimage(filename)
103 obj.readheader(filename)
104 return obj
105
106
108 """
109 determine which format for a filename
110 and return appropriate class which can be used for opening the image
111 """
112 try:
113 imo = fabioimage()
114 byts = imo._open(filename).read(18)
115 filetype = do_magic(byts)
116
117 if filetype == "marccd" and filename.find("mccd") == -1:
118
119
120 filetype = "tif"
121
122 except IOError:
123
124 raise
125 except:
126 try:
127 file_obj = deconstruct_filename(filename)
128 if file_obj == None:
129 raise Exception
130 if len(file_obj.format) != 1 and \
131 type(file_obj.format) != type(["list"]):
132
133 raise Exception("openimage failed on magic bytes & name guess")
134 filetype = file_obj.format
135
136 except:
137
138
139 raise Exception("Fabio could not identify " + filename)
140 klass_name = "".join(filetype) + 'image'
141
142 if hasattr(fabio, klass_name):
143 module = getattr(fabio, klass_name)
144 if hasattr(module, klass_name):
145 klass = getattr(module, klass_name)
146
147 else:
148 raise Exception("Module " + module + "has no image class")
149 else:
150 raise Exception("Filetype not known " + filename + " " +
151 klass_name)
152 obj = klass()
153
154 return obj
155