1 | # -*- coding: utf-8 -*- |
---|
2 | ########### SVN repository information ################### |
---|
3 | # $Date: 2016-01-22 19:05:12 +0000 (Fri, 22 Jan 2016) $ |
---|
4 | # $Author: toby $ |
---|
5 | # $Revision: 2133 $ |
---|
6 | # $URL: trunk/imports/G2img_HDF5.py $ |
---|
7 | # $Id: G2img_HDF5.py 2133 2016-01-22 19:05:12Z toby $ |
---|
8 | ########### SVN repository information ################### |
---|
9 | ''' |
---|
10 | *Module G2img_HDF5: summed HDF5 image file* |
---|
11 | --------------------------------------- |
---|
12 | |
---|
13 | Reads all images found in a HDF5 file. |
---|
14 | |
---|
15 | ''' |
---|
16 | |
---|
17 | try: |
---|
18 | import h5py |
---|
19 | except ImportError: |
---|
20 | h5py = None |
---|
21 | import GSASIIIO as G2IO |
---|
22 | import GSASIIpath |
---|
23 | GSASIIpath.SetVersionNumber("$Revision: 2133 $") |
---|
24 | |
---|
25 | class HDF5_Reader(G2IO.ImportImage): |
---|
26 | '''Routine to read a HD5 image, typically from APS Sector 6. |
---|
27 | B. Frosik/SDM. Initial version. |
---|
28 | Refactored. |
---|
29 | ''' |
---|
30 | dsetlist = [] |
---|
31 | buffer = {} |
---|
32 | init = False |
---|
33 | |
---|
34 | def __init__(self): |
---|
35 | if h5py is None: |
---|
36 | self.UseReader = False |
---|
37 | print('HDF5 Reader skipped because h5py library is not installed') |
---|
38 | super(self.__class__,self).__init__( # fancy way to self-reference |
---|
39 | extensionlist=('.hdf5','.hd5','.h5','.hdf'), |
---|
40 | strictExtension=True, |
---|
41 | formatName = 'HDF5 image', |
---|
42 | longFormatName = 'HDF5 image file' |
---|
43 | ) |
---|
44 | |
---|
45 | def ContentsValidator(self, filepointer): |
---|
46 | '''Test if valid by seeing if the HDF5 library recognizes the file. |
---|
47 | ''' |
---|
48 | try: |
---|
49 | f = h5py.File(filepointer.name, 'r') |
---|
50 | f.close() |
---|
51 | return True |
---|
52 | except IOError: |
---|
53 | return False |
---|
54 | |
---|
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. |
---|
59 | ''' |
---|
60 | imagenum = kwarg.get('blocknum') |
---|
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) |
---|
101 | |
---|
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[()] |
---|
109 | else: |
---|
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 |
---|