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