87from __future__
import print_function
96parser = argparse.ArgumentParser(
97 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
98 description=
'read a mille binary file and print its data'
100parser.add_argument(
'--type', choices=[
'c',
'fortran',
'autodetect'], default=
'autodetect',
101 help=
'type of binary file that will be read')
102parser.add_argument(
'filename', help=
'binary file to read')
103parser.add_argument(
'-n',
'--num-records', type=int, default=10,
104 help=
'Number of records (i.e. tracks) to print to terminal. Continue until the end of the file if negative.')
105parser.add_argument(
'-s',
'--skip-records', type=int, default=0,
106 help=
'number of records (tracks) to skip before starting to print.')
107parser.add_argument(
'--min-val', type=float,
108 help=
'minimum value to print derivatives')
109parser.add_argument(
'--quiet', action=
'store_true',
110 help=
'do not print any information to terminal')
112arg = parser.parse_args()
120elif arg.type ==
'fortran':
124 f = open(arg.filename,
"rb")
125 header_words = struct.unpack(
'ii', f.read(8))
128 if header_words[0] == 4 * (header_words[1] + 1):
130 print(
"Detected Fortran binary file")
133f = open(arg.filename,
"rb")
137 """unpack a certain number of the input type
139 We read from the file stored
in the variable `f`.
144 if there
is no data returned when the read
is done
146 if data
is returned but the length does
not match
147 the amount of data requested
152 single-character name of type to unpack:
'i',
'f',
or 'd'
153 number : int, optional
154 number of that type to unpack, default one
158 array of input length
and type, filled
with data unpacked
166 total_bytes = bytesper * number
167 bin_data = f.read(total_bytes)
168 if len(bin_data) != total_bytes:
169 if len(bin_data) == 0:
172 raise ValueError(
'Requested %d bytes but only got %d from the file.' % (total_bytes, len(bin_data)))
176 return struct.unpack(typechar * number, bin_data)
181 while (nrec < arg.num_records + arg.skip_records)
or (arg.num_records < 0):
185 lenf = struct.unpack(
'i', f.read(4))
198 nr = abs(length[0] >> 1)
212 if (nrec <= arg.skip_records):
218 print(
" === NR ", nrec, length[0] / 2)
221 if arg.num_records < 0:
230 while (i < (nr - 1)):
232 while (i < nr)
and (inder[i] != 0): i += 1
235 while (i < nr)
and (inder[i] != 0): i += 1
239 if (ja + 1 == jb)
and (glder[jb] < 0.):
241 nsp =
int(-glder[jb])
243 print(
' ### spec. ', nsp, inder[jsp + 1:i + 1], glder[jsp + 1:i + 1])
245 while (i < nr)
and (inder[i] != 0): i += 1
250 print(
' -g- meas. ', nh, inder[jb + 1], jb - ja - 1, i - jb, glder[ja], glder[jb])
253 print(
' -l- meas. ', nh, inder[ja + 1], jb - ja - 1, i - jb, glder[ja], glder[jb])
257 for k
in range(ja + 1, jb):
258 if arg.min_val
is None:
261 elif abs(glder[k]) >= arg.min_val:
264 print(
" local ", lab)
265 print(
" local ", val)
269 for k
in range(jb + 1, i + 1):
270 if arg.min_val
is None:
273 elif abs(glder[k]) >= arg.min_val:
276 print(
" global ", lab)
277 print(
" global ", val)
281 print(
" >>> error: end of file before end of record", nrec)
283except ValueError
as e:
284 print(
" >>> error: unable to unpack values before end of record", nrec, *(e.args))
287print(
" end of file after", nrec,
"records")
def unpack(typechar, number=1)