-
Notifications
You must be signed in to change notification settings - Fork 1
/
seehdf
executable file
·287 lines (260 loc) · 12.7 KB
/
seehdf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/env python
'''
a program to view data array in hdf files as an image
:author: Stanislav Stoupin
:email: sstoupin@gmail.com
:copyright: Copyright 2014 by XSD, Advanced Photon Source, Argonne National Laboratory
:license: UChicago Argonne, LLC Open Source License, see LICENSE for details.
'''
import os, sys
from numpy import *
from pylab import *
from matplotlib.colors import LogNorm
#LOCAL
#from pyhdf import *
#from myio import coljoin, writeFile
#GLOBAL
from dtxrd.pyhdf import *
prog = os.path.basename(sys.argv[0])
############################################################
__version__='0.19'
proginfo = prog + __version__ + ', by Stanislav Stoupin <sstoupin@gmail.com>'
########################################################################################
# v 0.12 - after pyhdf modification and convention for image transformation is adopted
# v 0.13 - minor adaptation to run globally
# v 0.14 - add -r option to plot selected region
# v 0.15 - fix transformations for -s option
# v 0.16 - user_dir = os.getcwd() sys.path.append(user_dir) for ccd.py and publish.py
# v 0.17 - implemented argparse
# - slicing via integration similar to tiffview + data saving
# v 0.19 - add limits control on colorbar
def fatalError(msg):
sys.stderr.write('Error: ')
sys.stderr.write(str(msg))
sys.stderr.write('\n')
sys.exit(1)
def fatalIOError(err):
if issubclass(err.__class__, IOError) and err.strerror and err.filename:
err = '%s: %s' % (err.strerror, err.filename)
fatalError(err)
def ParseArguments():
import argparse # requires Python 2.7 or higher
msg = prog + ' version: ' + __version__ + '\n'*2 # + __doc__.strip()+'\n'
#
parser = argparse.ArgumentParser(prog=prog, description=msg, formatter_class = argparse.RawDescriptionHelpFormatter)
parser.add_argument('-v', '--version', action='version', version=__version__)
parser.add_argument('-o', '--output', action='store', dest='output', default=None, help='write calculated parameters to file F (defaults to stdout)', metavar='F')
parser.add_argument('-p', '--publish', action='store_const', const=1, dest='publish', default=0, help='generate figures for publication')
parser.add_argument('-s', '--transpose', action='store_const', const=1, dest='transpose', default=0, help='transpose image array')
parser.add_argument('-w', '--write', action='store_const', const=1, dest='write', default=0, help='write processed figures to their corresponding png files')
parser.add_argument('-i', '--instrument', action='store_const', const=1, dest='instrument', default=0, help='read parameters from instrument file ccd.py')
#parser.add_argument('-z', '--psize', action='store', dest='psize', default=1, metavar='psize', help='pixel size [mm]')
parser.add_argument('-n', '--name', action='store', dest='name', default='', metavar='name', help='sample name')
parser.add_argument('-r', '--range', action='store', dest='rng', default='-1 -1 -1 -1', metavar='rng', help='range of interest')
parser.add_argument('-x', '--xint', action='store_const', const=1, dest='xint', default=0, help='integrate the region of interest in x and plot distribution')
parser.add_argument('-y', '--yint', action='store_const', const=1, dest='yint', default=0, help='integrate the region of interest in y and plot distribution')
parser.add_argument('-c', '--calc', action='store_const', const=1, dest='calc', default=0, help='calculate curve statistics on the resulting distribution')
parser.add_argument('-z', '--zrange', action='store', dest='zrng', default='-1 -1', metavar='rng', help='z range of interest (limits on the colorbar)')
parser.add_argument('-l', '--log', action='store_const', const=1, dest='logp', default=0, help='plot image on log scale')
parser.add_argument('filenames', action='store', nargs='+')
return parser.parse_args()
def main():
cmd_opts = ParseArguments()
if cmd_opts.output is not None:
try:
outFile = open(cmd_opts.output, 'w')
except IOError as e:
fatalIOError(e)
else:
outFile = sys.stdout
#######
if cmd_opts.instrument == 1: # if instrument file ccd.py is used
# import parameters from ccd.py
user_dir = os.getcwd()
sys.path.append(user_dir)
from ccd import rbin,dx,dy #,tot_range,dyn_range,fwhm_0,bkg0
from ccd import data_path,th_path,chi_path
sys.path.pop()
else:
rbin=1
dx, dy = 1.0, 1.0 # CCD camera pixel size [mm]
tot_range=10000.0 # upper limit for threshold processing (to exclude "dead" pixels)
dyn_range=10000.0 # upper limit for threshold processing (gauss)
fwhm_0=30.0 # expected fwhm (for fitting)
bkg0=2750.0 # "reasonable" background - dark current count
# default paths in hdf or h5 file
data_path='/entry/instrument/detector/data'
th_path='/entry/instrument/detector/NDAttributes/theta'
chi_path='/entry/instrument/detector/NDAttributes/chi'
dx=float(rbin)*dx
dy=float(rbin)*dy
#
rangez=cmd_opts.zrng.split(' ')
zmin=float(rangez[0])
zmax=float(rangez[1])
#
outFile.write('%prog '+__version__+' by Stanislav Stoupin <sstoupin@gmail.com>\n')
count=1
########################################################################
for fn in cmd_opts.filenames:
fn_ext=fn.split(".")[-1]
if fn_ext == "hdf":
angle, size, im = read_hdf4(fn)
elif fn_ext == "h5":
angle, chi, size, im = read_hdf5(fn,rbin,data_path,th_path,chi_path)
else:
fatalError('Supported files are either hdf4 (extension .hdf) or hdf5 (extension h5)')
# need to do these transformations in the opposite sense to viewhdf for some reason
# then -s assignment matches in both programs
if cmd_opts.transpose == 1:
im=transpose(im)
im=fliplr(im)
nx=size[1]
ny=size[0]
else:
nx=size[0] #;print nx
ny=size[1] #;print ny
im=flipud(im)
#im=fliplr(im)
#nx=size[0] #;print nx
#ny=size[1] #;print ny
####################################################
## Analyzed Area (Range)
####################################################
step=1
#
rangexy=cmd_opts.rng.split(' ')
rngx1=float(rangexy[0]); rngy1=float(rangexy[2])
rngx2=float(rangexy[1]); rngy2=float(rangexy[3])
if rngx1<0:
indx1=0
else:
indx1=int(rngx1/dx)
if rngy1<0:
indy1=0
else:
indy1=int(rngy1/dy)
#---------------------------------------
if rngx2<0 or rngx2>nx*dx:
indx2=nx
else:
indx2=int(rngx2/dx)
if rngy2<0 or rngy2>ny*dy:
indy2=ny
else:
indy2=int(rngy2/dy)
#xrange=arange(nx-indx2,nx-indx1,step)
#yrange=arange(ny-indy2,ny-indy1,step) #;print yrange
#
nnx1=nx-indx2; nnx2=nx-indx1
nny1=ny-indy2; nny2=ny-indy1
im=im[nnx1:nnx2,nny1:nny2]
#############################################
# final transformation to match topohdf
im=transpose(im)
im=fliplr(im)
#############################################
if cmd_opts.publish !=1:
#xyrange=(0,dx*nx,0,dy*ny)
xyrange=(dx*indx1,dx*indx2,dy*indy1,dy*indy2) # for some reason the arrays should be transposed
#nx,ny = im.shape
#xyrange=(0,dy*ny,0,dx*nx)
#xyrange=(dx*nnx1,dx*nnx2,dy*nny1,dy*nny2)
#
fig1=plt.figure(count)
abs_max = im.max()
abs_min = im.min()
ax=fig1.add_subplot(111)
if cmd_opts.logp == 1:
if zmin<0: vmin0=1.0e0
else: vmin0=zmin
if zmax<0: vmax0=1.0e6
else: vmax0=zmax
imgplot = ax.imshow(im, aspect='equal', extent=xyrange, norm=LogNorm(), vmin=vmin0, vmax=vmax0)
else:
if zmin<0: vmin0=0.0
else: vmin0=zmin
if zmax<0: vmax0=abs_max
else: vmax0=zmax
imgplot = ax.imshow(im, aspect='equal', extent=xyrange, vmin=vmin0, vmax=vmax0)
imgplot.set_cmap('jet')
plt.colorbar(imgplot)
plt.title(fn)
plt.xlabel('x [mm]')
plt.ylabel('y [mm]')
#numrows, numcols = im.shape
#def format_coord(x, y):
# col = int(x/dx+0.5)
# row = int(y/dy+0.5)
#if col>=0 and col<numcols and row>=0 and row<numrows:
# z = im[numrows-row,col]
# return 'x=%1.4f, y=%1.4f, z=%1.4f'%(x, y, z)
#else:
# return 'x=%1.4f, y=%1.4f'%(x, y)
#ax.format_coord = format_coord
else:
user_dir = os.getcwd()
sys.path.append(user_dir)
from publish import figplot
from publish import a2urad
sys.path.pop()
theta = a2urad*angle
#indx1=0; indx2=nx
#indy1=0; indy2=ny
print("cmd_opts.transpose", cmd_opts.transpose)
figplot(cmd_opts.transpose,dx,dy,indx1,indx2,indy1,indy2,im,fn,theta)
############################################################################
## Do STUFF
############################################################################
if cmd_opts.name !='':
plt.title(cmd_opts.name)
else:
plt.title(fn) # + ' '+ "%.2f" % theta +' [urad]')
fn_ = fn.split(".h5")[0]
cnt = '%02d' %count
if cmd_opts.xint == 1 or cmd_opts.yint ==1:
fig2 = plt.figure(2)
if cmd_opts.xint == 1:
idata = average(im, axis=1)
plt.xlabel('y [mm]')
plt.ylabel('averaged intensity [counts]')
crange=arange(dy*indy1,dy*indy2,dy)
datafile='Ydist_'+'_'+fn_+'.dat'
if cmd_opts.yint == 1:
idata = average(im, axis=0)
plt.xlabel('x [mm]')
plt.ylabel('averaged intensity [counts]')
crange=arange(dx*indx1,dx*indx2,dx)
datafile='Xdist_'+'_'+fn_+'.dat'
plt.plot(crange,idata,'ko')
plt.plot(crange,idata,'k-')
if cmd_opts.calc == 1:
bkg0 = 0.25*(idata[0]+idata[1]+idata[len(idata)-1]+idata[len(idata)-2])
stat0=curvestat(crange,idata,bkg0)
fwhm0 = stat0[5]
outFile.write('FWHM [z units] = '+str(fwhm0)+' \n')
#######################################################################
## WRITE OUTPUT
#######################################################################
if cmd_opts.write == 1:
#imgfile=cnt+'_'+fn_+'.png'
imgfile=fn_+'.png'
fig1.savefig(imgfile)
if cmd_opts.xint == 1 or cmd_opts.yint ==1:
header = '# '+ str(sys.argv) +'\n' \
+'# version '+__version__+' by Stanislav Stoupin\n' \
+'# columns: \n' \
+ '#coordinate integr_distribution'+'\n'
try:
writeFile(datafile,header,crange,idata)
except IOError as e:
fatalIOError(e)
count = count + 1
outFile.write('# filename: '+fn+'\n')
outFile.write('# image: '+str(nx)+' x '+str(ny)+'\n')
outFile.write('angle [orig. units] = '+str(angle)+'\n')
outFile.write('chi [orig. units] = '+str(chi)+'\n')
outFile.close
plt.show()
if __name__ == '__main__':
main()