-
Notifications
You must be signed in to change notification settings - Fork 1
/
usrtrack2ascii.py
executable file
·201 lines (173 loc) · 6.9 KB
/
usrtrack2ascii.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
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
#!/usr/bin/env python
#########################################################################
# Copyright (C) 2010 Niels Bassler, [email protected]
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, see <http://www.gnu.org/licenses/>.
#
# usrbin2ascii.py
#
# Converting usrbin files to ascii.
#
#
# NB: Niels Bassler, [email protected],
#
# change log
# 18.2.2010, NB
# - code cleanup
# - adding stuff for handeling multiple detectors, does it work?
# - removed flair code, which is now imported instead.
# 22.11.2008, NB
# - added fortran block length 128 support from upstream release
# - added -F option
# 23.9.2008, NB
# - added help text and option parser
# - output suffix can now be set optionally and cleanup option
# - added glob.glob
# - fix exception if datafiles only contains zeros
#
#
# TODO:
# - check if input files are binary before processing, improve file checking
# - allow usrbin2ascii foo* instead of usrbin2ascii "foo*"
# - less verbose option ?
# - merge usrbin2ascii wtih usrtrack2ascii
# - convert to shell scripts which sets flair path for python instead of this solution ?
# - multiple detectors
#########################################################################
# DEPRECATED, should be merged with usrbin2ascii.py to new usr2ascii.py file.
#
# Development will until then happen in usrbin2ascii.py
#######################################################################
import os
import sys
import glob
import re
from optparse import OptionParser
version = "1.2"
# ----------------------------------------------
from flair.Data import *
parser = OptionParser()
parser.add_option("-s", "--suffix", dest="suffix",
help="add this suffix to generated data. Default is \"_ascii.dat\".",
metavar="string")
parser.add_option("-c", "--clean", action="store_true", dest="clean",
default=False,
help="Remove all files containing the suffix. Use carefully.")
parser.add_option("-d", "--detector", dest="detector",
help="If more detectors in binary file present, convert only this one.",
metavar="int")
parser.add_option("-F", "--FLUKA", action="store_true", dest="FLUKA",
default=False,
help="Fluka style fortran output, 10 data points per line.")
(options, args) = parser.parse_args()
if options.detector is not None:
detector_select = int(options.detector)
else:
detector_select = -1
if len(args) < 1:
print("")
print("usrbin2ascii.py Version", version)
print("")
print("Usage: usrbin2ascii.py [options] binaryforfile")
print("See: -h for all options.")
print("File lists are supported when using quotations:")
print("usrbin2ascii.py \"foo*\"")
raise IOError("Error: no input file(s) stated.")
filename = args[0]
filename_list = glob.glob(filename)
if options.suffix is None:
suffix = "_ascii.dat"
else:
suffix = options.suffix
# cleanup all files with suffix
if options.clean:
filename_list_suffix = glob.glob(filename+suffix)
for filename_temp in filename_list_suffix:
print("Removing filename", filename_temp)
os.remove( filename_temp )
# reread filename list
# filename_list = glob.glob(filename)
# print filename_list
if len(filename_list) < 1:
raise IOError("Error: %s does not exist." % filename)
for filename in filename_list:
print("opening binary file:", filename)
mymatch = re.search(suffix,filename)
if mymatch is not None:
print("")
print("Hmmm. It seems you have not deleted previous ascii output.")
print("I will exit now. You may want to wish to delete all files ending with",suffix)
print("You can do this easily by rerunning the program and using the -c option.")
sys.exit(0)
print("="*80)
usr = Usrbin(filename)
usr.say() # file,title,time,weight,ncase,nbatch
for i in range(len(usr.detector)):
print("-"*20,"Detector number %i" %i,"-"*20)
usr.say(i) # details for each detector
data = usr.readData(0)
print("len(data):", len(data))
fdata = unpackArray(data)
print("len(fdata):", len(fdata))
if len([x for x in fdata if x>0.0]) > 0:
fmin = min([x for x in fdata if x>0.0])
print("Min=",fmin)
else:
print("How sad. Your data contains only zeros.")
print("Converting anyway.")
if len(fdata) > 0:
fmax = max(fdata)
print("Max=",fmax)
print("="*80)
# TODO: handle multiple detectors.
outfile = filename+suffix
print("Writing output to", outfile)
f = open(outfile,'w')
det_number = 0
pos = 0 # counter for data
# TODO, check with multiple detectors. Are the data sequentially ordered?
# At least we assume this here.
if (detector_select + 1) > len(usr.detector):
raise IOError("Selected detctor number %i is not available." %detector_select)
for det in usr.detector:
# det = usr.detector[0]
# this header is only needed, if more detectors are requested
if (len(usr.detector) > 1) and (detector_select == -1):
f.write("# Detector number %i\n" % det_number )
print("nx,ny,nz:", int(det.nx), int(det.ny), int(det.nz))
ifort = 0 # counter for fortran format
for iz in range(int(det.nz)):
for iy in range(int(det.ny)):
for ix in range(int(det.nx)):
fx = (ix/float(det.nx) * (det.xhigh-det.xlow)) + det.xlow
fy = (iy/float(det.ny) * (det.yhigh-det.ylow)) + det.ylow
fz = (iz/float(det.nz) * (det.zhigh-det.zlow)) + det.zlow
# middle of bins
fx += det.dx /2.0
fy += det.dy /2.0
fz += det.dz /2.0
if options.FLUKA == True:
output_string = "%.3e " % (fdata[pos])
ifort +=1
if ifort >= 10:
ifort = 0
output_string += "\n" # CR after 10 data points
else:
output_string = "%e %e %e %e\n" % (fx,fy,fz,fdata[pos])
pos += 1
if (det_number == detector_select) or (detector_select == -1):
f.write(output_string)
det_number += 1
f.write("\n")
f.close()