Ignore:
Timestamp:
Dec 21, 2015 11:03:08 AM (6 years ago)
Author:
toby
Message:

Drop #1 from header in one-image files; revise HDF5 importer to read in slices; add .csv PWDR importer

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/imports/G2img_HDF5.py

    r2095 r2101  
    1111---------------------------------------
    1212
    13 This shows an example of an importer that will handle files with
    14 more than a single image.
     13Reads all images found in a HDF5 file.
    1514
    1615'''
    1716
    18 import h5py
     17try:
     18    import h5py
     19except ImportError:
     20    h5py = None
    1921import GSASIIIO as G2IO
    2022import GSASIIpath
    2123GSASIIpath.SetVersionNumber("$Revision: $")
    2224
    23 class Hdf5_Reader(G2IO.ImportImage):
     25class HDF5_Reader(G2IO.ImportImage):
    2426    '''Routine to read a HD5 image, typically from APS Sector 6.
    2527    B. Frosik/SDM. Initial version.
     
    3133
    3234    def __init__(self):
    33         print 'start'
     35        if h5py is None:
     36            self.UseReader = False
     37            print('HDF5 Reader skipped because h5py library is not installed')
    3438        super(self.__class__,self).__init__( # fancy way to self-reference
    3539                                             extensionlist=('.hdf5','.hd5','.h5','.hdf'),
     
    4044
    4145    def ContentsValidator(self, filepointer):
    42         '''no test at this time
     46        '''Test if valid by seeing if the HDF5 library recognizes the file.
    4347        '''
    4448        try:
    45             # the following does not work, filepointer is not a filename
    4649            f = h5py.File(filepointer.name, 'r')
    4750            f.close()
     
    5053            return False               
    5154
    52     def Reader(self,filename,filepointer, ParentFrame=None, **kwarg):
    53         '''Read using HDF5 file reader, :func:`ReadData`
     55    def Reader(self, filename, filepointer, ParentFrame=None, **kwarg):
     56        '''Scan file structure using :meth:`visit` and map out locations of image(s)
     57        then read one image using :meth:`readDataset`. Save map of file structure in
     58        buffer arg, if used.
    5459        '''
    5560        imagenum = kwarg.get('blocknum')
    56         #self.buffer = kwarg.get('buffer')
     61        if imagenum is None: imagenum = 1
     62        self.buffer = kwarg.get('buffer',{})
     63        try:
     64            fp = h5py.File(filename, 'r')
     65            if not self.buffer.get('init'):
     66                self.buffer['init'] = True
     67                self.visit(fp)
     68            self.Comments,self.Data,self.Npix,self.Image = self.readDataset(fp,imagenum)
     69            if self.Npix == 0 or not self.Comments:
     70                return False
     71            self.LoadImage(ParentFrame,filename,imagenum)
     72            self.repeatcount = imagenum
     73            self.repeat = imagenum < len(self.buffer['imagemap'])
     74            if GSASIIpath.GetConfigValue('debug'): print('Read image #'+str(imagenum)+' from file '+filename)
     75            return True
     76        except IOError:
     77            print 'cannot open file ', filename
     78            return False
     79        finally:
     80            fp.close()
     81
     82    def visit(self, fp):
     83        '''Recursively visit each node in an HDF5 file. For nodes
     84        ending in 'data' look at dimensions of contents. If the shape is
     85        length 2 or 4 assume an image and index in self.buffer['imagemap']
     86        '''
     87        datakeyword = 'data'
     88        def func(name, dset):
     89            if not hasattr(dset,'shape'): return # not array, can't be image
     90            if isinstance(dset, h5py.Dataset):
     91                if dset.name.endswith(datakeyword):
     92                    dims = dset.shape
     93                    if len(dims) == 4:
     94                        self.buffer['imagemap'] += [
     95                            (dset.name,i) for i in range(dims[1])]
     96                    elif len(dims) == 2:
     97                        self.buffer['imagemap'] += [(dset.name,None)]
     98        if GSASIIpath.GetConfigValue('debug'): print 'visit'
     99        self.buffer['imagemap'] = []
     100        fp.visititems(func)
    57101       
    58         if self.init is False:
    59             self.init = True
    60             self.readDataset(filename)
    61 
    62         len = self.buffer['len']
    63         if len is 1:
    64             image = self.buffer['image']
     102    def readDataset(self,fp,imagenum=1):
     103        '''Read a specified image number from a file
     104        '''
     105        name,num = self.buffer['imagemap'][imagenum-1] # look up in map
     106        dset = fp[name]
     107        if num is None:
     108            image = dset[()]
    65109        else:
    66             images = self.buffer['image']
    67             image = images[imagenum-1]
    68 
    69         self.Comments,self.Data,self.Npix,self.Image = self.read_set(image,imagenum=imagenum)
    70         if self.Npix == 0 or not self.Comments:
    71             return False
    72         self.LoadImage(ParentFrame,filename,imagenum)
    73         self.repeatcount = imagenum
    74         self.repeat = imagenum < len
    75         #print('Read image #'+str(imagenum)+' from file '+filename)
    76         return True
    77        
    78     def readDataset(self,filename):
    79             imagenum = 1
    80             try:
    81                 f = h5py.File(filename, 'r')
    82                 self.visit(f, imagenum)
    83             except IOError:
    84                 print 'cannot open file ', filename
    85                 return False
    86             finally:
    87                 if f is not None:
    88                    f.close()
    89        
    90          
    91     def visit(self, f, imagenum):
    92         #GSASIIpath.IPyBreak()   
    93         print 'visiting'     
    94         def func(name, dset):
    95             datakeyword = 'data'
    96             if isinstance(dset, h5py.Dataset):
    97                 self.dsetlist.append(dset.name)
    98                 lastindex = dset.name.rfind(datakeyword)
    99                 if lastindex is not -1:
    100                     index = len(dset.name)-len(datakeyword)
    101                     if index == lastindex:
    102                         if self.buffer is not None:
    103                             if len(dset.shape) > 2:
    104                                     image = dset[0,...]
    105                                     self.buffer['image'] = dset[0,...]
    106                                     self.buffer['len'] = image.shape[0]
    107                             else:
    108                                 self.buffer['image'] = dset[()]
    109                                 self.buffer['len'] = 1
    110                             self.buffer['name'] = dset.name
    111                         #GSASIIpath.IPyBreak()
    112         f.visititems(func)
    113        
    114     def read_set(self,image,imagenum=1):
    115          #more = False
    116          head = ['raw data']
    117          x_dim = image.shape[0]
    118          y_dim = image.shape[1]
    119          sizexy = [x_dim,y_dim]
    120          Npix = sizexy[0]*sizexy[1]             
    121          data = {'pixelSize':[200,200],'wavelength':0.15,'distance':250.0,'center':[204.8,204.8],'size':sizexy}
    122          return head,data,Npix,image
     110            image = dset[0,num,...]
     111        head = ['raw data']
     112        sizexy = list(image.shape)
     113        Npix = sizexy[0]*sizexy[1]             
     114        data = {'pixelSize':[200,200],'wavelength':0.15,'distance':250.0,
     115                'center':[204.8,204.8],'size':sizexy}
     116        return head,data,Npix,image
Note: See TracChangeset for help on using the changeset viewer.