Package fabio :: Module openimage
[hide private]
[frames] | no frames]

Source Code for Module fabio.openimage

  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      # "\42\5a" : 'bzipped' 
 39      # "\1f\8b" : 'gzipped' 
 40      ("FORMAT :        86" , 'bruker'), 
 41      ("\x4d\x4d\x00\x2a"   , 'tif') , 
 42      # The marCCD and Pilatus formats are both standard tif with a header 
 43      # hopefully these byte patterns are unique for the formats 
 44      # If not the image will be read, but the is missing  
 45      ("\x49\x49\x2a\x00\x08\x00"   , 'marccd') , 
 46      ("\x49\x49\x2a\x00\x82\x00"   , 'pilatus') , 
 47      ("\x49\x49\x2a\x00"   , 'tif') , 
 48      # ADSC must come before edf 
 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'), #some machines may need byteswapping 
 58      # hint : MASK in 32 bit 
 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   
65 -def do_magic(byts):
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: # debugging - bruker needed 18 bytes below 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
79 -def openimage(filename):
80 """ Try to open an image """ 81 if isinstance(filename, filename_object): 82 try: 83 obj = _openimage(filename.tostring()) 84 obj.read(filename.tostring()) 85 except: 86 # multiframe file 87 #print "DEBUG: multiframe file, start # %d"%( 88 # filename.num) 89 obj = _openimage(filename.stem) 90 obj.read(filename.stem, frame=filename.num) 91 else: 92 obj = _openimage(filename) 93 obj.read(filename) 94 return obj
95 96
97 -def openheader(filename):
98 """ return only the header""" 99 obj = _openimage(filename) 100 obj.readheader(filename) 101 return obj
102 103
104 -def _openimage(filename):
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 # print filetype 114 if filetype == "marccd" and filename.find("mccd") == -1: 115 # Cannot see a way around this. Need to find something 116 # to distinguish mccd from regular tif... 117 filetype = "tif" 118 #UNUSED filenumber = getnum(filename) 119 except IOError: 120 # File probably does not exist 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 # one of OXD/ ADSC - should have got in previous 130 raise Exception("openimage failed on magic bytes & name guess") 131 filetype = file_obj.format 132 #UNUSED filenumber = file_obj.num 133 except: 134 #import traceback 135 #traceback.print_exc() 136 raise Exception("Fabio could not identify " + filename) 137 klass_name = "".join(filetype) + 'image' 138 # print "looking for %s in" % klass_name 139 # for i in sys.modules: 140 # if klass_name in i: 141 # print "%s\t%s" % (i, sys.modules[i]) 142 module = sys.modules.get("fabio." + klass_name, None) 143 # if hasattr(__init__, klass_name): 144 # module = getattr(__init__, klass_name) 145 if module is not None: 146 if hasattr(module, klass_name): 147 klass = getattr(module, klass_name) 148 # print klass 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 # skip the read for read header 155 return obj
156