3 from __future__
import print_function
8 p = argparse.ArgumentParser(
9 description=
"Process output data file saved as dictionaries. "
10 "It has two modalities: print selected fields for all "
11 "lines or print a particular line where a field has a "
12 "given value. Example of usage: process_output.py "
13 "--soft -s To E S -f log.3.native-2-no-red. "
14 "process_output.py --soft --search_field EV0 "
15 "--search_value 5.67750116023 "
16 "-f log.3.native-2-no-red")
17 p.add_argument(
'-f', action=
"store", dest=
"filename",
18 help=
"file name to process")
19 p.add_argument(
'-s', dest=
"fields", nargs=
"+",
20 help=
"Specify all fields to be printed. Multiple flags "
21 "will append a list of fields to be printed")
22 p.add_argument(
'-t', dest=
"single_column_field",
23 help=
"Specify a single column field to be printed. It "
24 "will be printed as a column. If the field name is "
25 "not complete, it will print all fields whose name "
26 "contain the queried string.")
27 p.add_argument(
'-p', action=
"store_true", dest=
"print_fields",
28 default=
False, help=
"print the fields contained in the file")
29 p.add_argument(
'--head', action=
"store_true", dest=
"print_header",
31 help=
"print the fields contained in the file (only stat2)")
32 p.add_argument(
'-n', action=
"store", dest=
"print_raw_number",
33 help=
"print the selected raw")
34 p.add_argument(
'--soft', action=
"store_true", dest=
"soft_match", default=
False,
35 help=
"Soft match. Closest matching field will be printed, "
36 "e.g. S will give Step_Number, En will give energy, etc. ")
37 p.add_argument(
'--search_field', dest=
"search_field",
38 help=
"Search a line from the file. Specify the field to "
40 p.add_argument(
'--search_value', dest=
"search_value",
41 help=
"Search a line from the file. Specify the value to "
43 p.add_argument(
'--nframe', action=
"store_true", dest=
"nframe", default=
False,
44 help=
"Print the frame number as initial column")
46 result = p.parse_args()
52 if result.filename
is not None:
53 f = open(result.filename,
"r")
55 raise ValueError(
"No file name provided. Use -h for help")
58 for line
in f.readlines():
60 klist = list(d.keys())
62 if "STAT2HEADER" in klist:
66 if "STAT2HEADER" in str(k):
67 if result.print_header:
73 for k
in sorted(stat2_dict.items(),
74 key=operator.itemgetter(1))]
76 for k
in sorted(stat2_dict.items(),
77 key=operator.itemgetter(1))]
80 invstat2_dict.update({stat2_dict[k]: k})
89 if result.print_fields:
95 "... omitting the rest of the string (>100 characters)")
100 match_strictness = 1.0
101 if result.soft_match:
102 match_strictness = 0.1
105 if result.fields
is not None:
109 for field
in result.fields:
110 found_entries = difflib.get_close_matches(
115 if len(found_entries) == 0:
116 raise ValueError(
"field " + field +
" non found")
118 field_list.append(found_entries[0])
121 s0 =
' '.join([
"%20s" % (field)
for field
in field_list])
125 f = open(result.filename,
"r")
127 for line
in f.readlines():
132 print(
"# Warning: skipped line number " +
133 str(line_number) +
" not a valid line")
136 s0 =
' '.join([
"%20s" % (str(d[field]))
for field
in field_list])
140 s0 =
' '.join([
"%20s" % (str(d[invstat2_dict[field]]))
141 for field
in field_list])
142 if not result.nframe:
145 print(str(line_number) +
" > " + s0)
149 if result.single_column_field
is not None:
152 if result.single_column_field
in k:
155 f = open(result.filename,
"r")
157 for line
in f.readlines():
162 print(
"# Warning: skipped line number " +
163 str(line_number) +
" not a valid line")
166 for key
in field_list:
171 for key
in field_list:
172 print(key, d[invstat2_dict[key]])
176 if (result.search_field
is not None)
and (result.search_value
is not None):
179 found_entries = difflib.get_close_matches(
184 if len(found_entries) == 0:
185 raise ValueError(
"field " + result.search_field +
" non found")
187 corrected_field = found_entries[0]
189 f = open(result.filename,
"r")
191 for line
in f.readlines():
196 print(
"# Warning: skipped line number " +
197 str(line_number) +
" not a valid line")
201 if (str(d[corrected_field]) == result.search_value):
207 if (str(d[invstat2_dict[corrected_field]]) == result.search_value):
209 print(key, d[invstat2_dict[key]])
212 if result.print_raw_number
is not None:
215 f = open(result.filename,
"r")
217 for line
in f.readlines():
220 if (line_number == int(result.print_raw_number)):
224 print(
"# Warning: skipped line number "
225 + str(line_number) +
" not a valid line")
231 if (line_number == int(result.print_raw_number) + 1):
235 print(
"# Warning: skipped line number "
236 + str(line_number) +
" not a valid line")
239 print(key, d[invstat2_dict[key]])