This repository has been archived by the owner on Aug 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
old-h5toathena.py
executable file
·98 lines (91 loc) · 3.16 KB
/
old-h5toathena.py
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
#! /usr/bin/python2.7
# convert from the h5 produced at SRX to something that Athena will understand
import h5py
import math
import sys
import os
import time
from optparse import OptionParser
global d
#Si 111
d=3.1355
global sddfactor
sddfactor=[9.9936]
def dcmtoeV(angle=0.):
return 12398. / (2. * d * math.sin(angle/180.*3.1416))
def chtoeV(ch=0):
global sddfactor
return (ch+1)*sddfactor[0]
def main(argv=None):
usage="usage: %prog [options]"
parser=OptionParser(usage)
parser.add_option("--input",action="store",type="string",dest="h5name",\
help="name of HDF5 to convert")
parser.add_option("--output",action="store",type="string",dest="ofile",\
help="base name of output file")
(options,args) = parser.parse_args()
try:
h5=h5py.File(options.h5name)
except OSError:
print '%s: file could not be opened!'%options.h5name
sys.exit()
datapath='/entry/instrument/detector/data'
sh=rawdata.shape
if len(sh) == 3:
#no virtual dimensions
try:
fp=open(options.ofile,mode='x')
except OSError:
print '%s: file could not be opened!'%options.ofile
sys.exit()
except FileExistsError:
print '%s: file exists!'%options.ofile
sys.exit()
rawdata=h5[datapath]
data=numpy.asarray(rawdata)
procdata=numpy.zeros(data[0][0][:].shape)
#sum over all exposures
for i in range(0,sh[0]):
#sum data in the first three channels
for j in range(0,3):
procdata=procdata+data[i][j]
s="# NSLS-II SRX\n# created from %(H5)s on %(T)s\n# ----------\n# energy counts\n"\
%{"H5":options.h5name, "T":time.asctime()}
fp.write(s)
for i in range(0,len(procdata)):
s="%(energy)8.2f\t%(ct)d\n"%{"energy":chtoeV(i),"ct":procdata[i]}
fp.write(s)
fp.close()
elif len(sh)==4:
#one virtual dimension
for k in range(0,sh[0]):
fname=options.ofile+"_%05d"%k
try:
fp=open(fname,mode='x')
except OSError:
print '%s: file could not be opened!'%options.ofile
sys.exit()
except FileExistsError:
print '%s: file exists!'%options.ofile
sys.exit()
rawdata=h5[datapath][k]
data=numpy.asarray(rawdata)
procdata=numpy.zeros(data[0][0][:].shape)
#sum over all exposures
for i in range(0,sh[0]):
#sum data in the first three channels
for j in range(0,3):
procdata=procdata+data[i][j]
s="# NSLS-II SRX\n# created from %(H5)s on %(T)s\n# ----------\n# energy counts\n"\
%{"H5":options.h5name, "T":time.asctime()}
fp.write(s)
for i in range(0,len(procdata)):
s="%(energy)8.2f\t%(ct)d\n"%{"energy":chtoeV(i),"ct":procdata[i]}
fp.write(s)
fp.close()
elif len(sh)==5:
#two virtual dimensions
pass
else:
print "Data has unknown shape: %s"%sh
sys.exit()