source: trunk/imports/G2img_HDF5.py @ 2095

Last change on this file since 2095 was 2095, checked in by bfrosik, 7 years ago

Refactored the reader for better performance

File size: 4.2 KB
Line 
1# -*- coding: utf-8 -*-
2########### SVN repository information ###################
3# $Date: 2014-12-27 11:14:59 -0600 (Sat, 27 Dec 2014) $
4# $Author: $
5# $Revision: $
6# $URL: $
7# $Id: $
8########### SVN repository information ###################
9'''
10*Module G2img_HDF5: summed HDF5 image file*
11---------------------------------------
12
13This shows an example of an importer that will handle files with
14more than a single image.
15
16'''
17
18import h5py
19import GSASIIIO as G2IO
20import GSASIIpath
21GSASIIpath.SetVersionNumber("$Revision: $")
22
23class Hdf5_Reader(G2IO.ImportImage):
24    '''Routine to read a HD5 image, typically from APS Sector 6.
25    B. Frosik/SDM. Initial version.
26                   Refactored.       
27    '''
28    dsetlist = []
29    buffer = {}
30    init = False
31
32    def __init__(self):
33        print 'start'
34        super(self.__class__,self).__init__( # fancy way to self-reference
35                                             extensionlist=('.hdf5','.hd5','.h5','.hdf'),
36                                             strictExtension=True,
37                                             formatName = 'HDF5 image',
38                                             longFormatName = 'HDF5 image file'
39                                             )
40
41    def ContentsValidator(self, filepointer):
42        '''no test at this time
43        '''
44        try:
45            # the following does not work, filepointer is not a filename
46            f = h5py.File(filepointer.name, 'r')
47            f.close()
48            return True
49        except IOError:
50            return False               
51
52    def Reader(self,filename,filepointer, ParentFrame=None, **kwarg):
53        '''Read using HDF5 file reader, :func:`ReadData`
54        '''
55        imagenum = kwarg.get('blocknum')
56        #self.buffer = kwarg.get('buffer')
57       
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']
65        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
Note: See TracBrowser for help on using the repository browser.