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

Source Code for Module fabio.openimage

  1  """ 
  2   
  3  Authors: Henning O. Sorensen & Erik Knudsen 
  4           Center for Fundamental Research: Metal Structures in Four Dimensions 
  5           Risoe National Laboratory 
  6           Frederiksborgvej 399 
  7           DK-4000 Roskilde 
  8           email:henning.sorensen@risoe.dk 
  9   
 10  mods for fabio by JPW 
 11   
 12  """ 
 13  import sys, logging 
 14  from fabioutils  import deconstruct_filename, getnum, filename_object 
 15  from fabioimage import fabioimage 
 16  import edfimage 
 17  import adscimage 
 18  import tifimage 
 19  import marccdimage 
 20  import mar345image 
 21  import fit2dmaskimage 
 22  import brukerimage 
 23  import bruker100image 
 24  import pnmimage 
 25  import GEimage 
 26  import OXDimage 
 27  import dm3image 
 28  import HiPiCimage 
 29  import pilatusimage 
 30  import fit2dspreadsheetimage 
 31  import kcdimage 
 32  import cbfimage 
 33  import xsdimage 
 34   
 35  MAGIC_NUMBERS = [ 
 36      # "\42\5a" : 'bzipped' 
 37      # "\1f\8b" : 'gzipped' 
 38      ("FORMAT :        86" , 'bruker'), 
 39      ("\x4d\x4d\x00\x2a"   , 'tif') , 
 40      # The marCCD and Pilatus formats are both standard tif with a header 
 41      # hopefully these byte patterns are unique for the formats 
 42      # If not the image will be read, but the is missing  
 43      ("\x49\x49\x2a\x00\x08\x00"   , 'marccd') , 
 44      ("\x49\x49\x2a\x00\x82\x00"   , 'pilatus') , 
 45      ("\x49\x49\x2a\x00"   , 'tif') , 
 46      # ADSC must come before edf 
 47      ("{\nHEA"             , 'adsc'), 
 48      ("{"                  , 'edf'), 
 49      ("\r{"                , 'edf'), 
 50      ("\n{"                , 'edf'), 
 51      ("ADEPT"              , 'GE'), 
 52      ("OD"                 , 'OXD'), 
 53      ("IM"                 , 'HiPiC'), 
 54      ('\x2d\x04'           , 'mar345'), 
 55      ('\x04\x2d'           , 'mar345'), #some machines may need byteswapping 
 56      # hint : MASK in 32 bit 
 57      ('M\x00\x00\x00A\x00\x00\x00S\x00\x00\x00K\x00\x00\x00' , 'fit2dmask') , 
 58      ('\x00\x00\x00\x03'   , 'dm3'), 
 59      ("No"                 , "kcd"), 
 60      ("<"                  , "xsd") 
 61      ] 
 62   
63 -def do_magic(byts):
64 """ Try to interpret the bytes starting the file as a magic number """ 65 for magic, format in MAGIC_NUMBERS: 66 if byts.find(magic) == 0: 67 return format 68 if 0: # debugging - bruker needed 18 bytes below 69 print "m:", magic, "f:", format, 70 print "bytes:", magic, "len(bytes)", len(magic), 71 print "found:", byts.find(magic) 72 for i in range(len(magic)): 73 print ord(magic[i]), ord(byts[i]), magic[i], byts[i] 74 raise Exception("Could not interpret magic string")
75 76
77 -def openimage(filename):
78 """ Try to open an image """ 79 if isinstance(filename, filename_object): 80 try: 81 obj = _openimage(filename.tostring()) 82 obj.read(filename.tostring()) 83 except: 84 # multiframe file 85 #print "DEBUG: multiframe file, start # %d"%( 86 # filename.num) 87 obj = _openimage(filename.stem) 88 obj.read(filename.stem, frame=filename.num) 89 else: 90 obj = _openimage(filename) 91 obj.read(filename) 92 return obj
93 94
95 -def openheader(filename):
96 """ return only the header""" 97 obj = _openimage(filename) 98 obj.readheader(filename) 99 return obj
100 101
102 -def _openimage(filename):
103 """ 104 determine which format for a filename 105 and return appropriate class which can be used for opening the image 106 """ 107 try: 108 imo = fabioimage() 109 byts = imo._open(filename).read(18) 110 filetype = do_magic(byts) 111 # print filetype 112 if filetype == "marccd" and filename.find("mccd") == -1: 113 # Cannot see a way around this. Need to find something 114 # to distinguish mccd from regular tif... 115 filetype = "tif" 116 #UNUSED filenumber = getnum(filename) 117 except IOError: 118 # File probably does not exist 119 raise 120 except: 121 try: 122 file_obj = deconstruct_filename(filename) 123 if file_obj == None: 124 raise Exception 125 if len(file_obj.format) != 1 and \ 126 type(file_obj.format) != type(["list"]): 127 # one of OXD/ ADSC - should have got in previous 128 raise Exception("openimage failed on magic bytes & name guess") 129 filetype = file_obj.format 130 #UNUSED filenumber = file_obj.num 131 except: 132 #import traceback 133 #traceback.print_exc() 134 raise Exception("Fabio could not identify " + filename) 135 klass_name = "".join(filetype) + 'image' 136 # print "looking for %s in" % klass_name 137 # for i in sys.modules: 138 # if klass_name in i: 139 # print "%s\t%s" % (i, sys.modules[i]) 140 module = sys.modules.get("fabio." + klass_name, None) 141 # if hasattr(__init__, klass_name): 142 # module = getattr(__init__, klass_name) 143 if module is not None: 144 if hasattr(module, klass_name): 145 klass = getattr(module, klass_name) 146 # print klass 147 else: 148 raise Exception("Module %s has no image class" % module) 149 else: 150 raise Exception("Filetype not known %s %s" % (filename, klass_name)) 151 obj = klass() 152 # skip the read for read header 153 return obj
154