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 | |
---|
13 | This shows an example of an importer that will handle files with |
---|
14 | more than a single image. |
---|
15 | |
---|
16 | ''' |
---|
17 | |
---|
18 | import numpy as np |
---|
19 | import GSASIIIO as G2IO |
---|
20 | import GSASIIpath |
---|
21 | GSASIIpath.SetVersionNumber("$Revision: $") |
---|
22 | |
---|
23 | class HDF5_Reader(G2IO.ImportImage): |
---|
24 | '''Routine to read a HD5 image, typically from APS Sector 6. |
---|
25 | B. Frosik/SDM. Initial version. |
---|
26 | |
---|
27 | ''' |
---|
28 | dsetlist = [] |
---|
29 | |
---|
30 | def __init__(self): |
---|
31 | # check if HDF5 library is present, if not importer cannot be used |
---|
32 | try: |
---|
33 | import h5py |
---|
34 | except: |
---|
35 | self.UseReader = False |
---|
36 | print 'HDF5 Reader skipped because h5py library not installed' |
---|
37 | super(self.__class__,self).__init__( # fancy way to self-reference |
---|
38 | extensionlist=('.hdf5','.hd5','.h5','.hdf'), |
---|
39 | strictExtension=True, |
---|
40 | formatName = 'HDF5 image', |
---|
41 | longFormatName = 'HDF5 image file' |
---|
42 | ) |
---|
43 | |
---|
44 | def ContentsValidator(self, filepointer): |
---|
45 | '''test by using the HDF5 open |
---|
46 | ''' |
---|
47 | import h5py |
---|
48 | try: |
---|
49 | # the following does not work, filepointer is not a filename |
---|
50 | f = h5py.File(filepointer.name, 'r') |
---|
51 | f.close() |
---|
52 | return True |
---|
53 | except IOError: |
---|
54 | return False |
---|
55 | |
---|
56 | def Reader(self,filename,filepointer, ParentFrame=None, **kwarg): |
---|
57 | '''Read using HDF5 file reader, :func:`ReadData` |
---|
58 | ''' |
---|
59 | import h5py |
---|
60 | imagenum = kwarg.get('blocknum') |
---|
61 | if imagenum is None: imagenum = 1 |
---|
62 | f = None |
---|
63 | try: |
---|
64 | f = h5py.File(filename, 'r') |
---|
65 | #print f.keys() |
---|
66 | self.visit(f) |
---|
67 | self.Comments,self.Data,self.Npix,self.Image,more = self.read_set(f,imagenum=imagenum) |
---|
68 | self.LoadImage(ParentFrame,filename,imagenum) |
---|
69 | self.repeatcount = imagenum |
---|
70 | self.repeat = more |
---|
71 | print('Read image #'+str(imagenum)+' from file '+filename) |
---|
72 | return True |
---|
73 | except IOError: |
---|
74 | print 'cannot open file ', filename |
---|
75 | return False |
---|
76 | finally: |
---|
77 | if f is not None: |
---|
78 | f.close() |
---|
79 | |
---|
80 | def visit(self, f): |
---|
81 | import h5py |
---|
82 | def func(name, dset): |
---|
83 | datakeyword = 'data' |
---|
84 | if isinstance(dset, h5py.Dataset): |
---|
85 | self.dsetlist.append(dset.name) |
---|
86 | lastindex = dset.name.rfind(datakeyword) |
---|
87 | if lastindex is not -1: |
---|
88 | index = len(dset.name)-len(datakeyword) |
---|
89 | if index == lastindex: |
---|
90 | self.image = np.array(f[dset.name],dtype=np.int32) |
---|
91 | f.visititems(func) |
---|
92 | |
---|
93 | def read_set(self,f,imagenum=1): |
---|
94 | more = False |
---|
95 | head = ['raw data'] |
---|
96 | #if multiple frames the array size is 1 x number_frames x 2048 x 2048 |
---|
97 | num_dim = len(self.image.shape) |
---|
98 | #GSASIIpath.IPyBreak() |
---|
99 | if num_dim > 2: |
---|
100 | image = self.image[0,imagenum-1] |
---|
101 | more = self.image.shape[1] > imagenum |
---|
102 | else: |
---|
103 | image = self.image |
---|
104 | x_dim = image.shape[0] |
---|
105 | y_dim = image.shape[1] |
---|
106 | sizexy = [x_dim,y_dim] |
---|
107 | Npix = sizexy[0]*sizexy[1] |
---|
108 | data = {'pixelSize':[200,200],'wavelength':0.15,'distance':250.0,'center':[204.8,204.8],'size':sizexy} |
---|
109 | return head,data,Npix,image,more |
---|