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 -f log.3.native-2-no-red")
16 p.add_argument(
'-f', action=
"store", dest=
"filename",
17 help=
"file name to process")
18 p.add_argument(
'-s', dest=
"fields", nargs=
"+",
19 help=
"Specify all fields to be printed. Multiple flags "
20 "will append a list of fields to be printed")
21 p.add_argument(
'-t', dest=
"single_column_field",
22 help=
"Specify a single column field to be printed. It "
23 "will be printed as a column. If the field name is "
24 "not complete, it will print all fields whose name "
25 "contain the queried string.")
26 p.add_argument(
'-p', action=
"store_true", dest=
"print_fields",
27 default=
False, help=
"print the fields contained in the file")
28 p.add_argument(
'--head', action=
"store_true", dest=
"print_header",
30 help=
"print the fields contained in the file (only stat2)")
31 p.add_argument(
'-n', action=
"store", dest=
"print_raw_number",
32 help=
"print the selected raw")
33 p.add_argument(
'--soft', action=
"store_true", dest=
"soft_match", default=
False,
34 help=
"Soft match. Closest matching field will be printed, "
35 "e.g. S will give Step_Number, En will give energy, etc. ")
36 p.add_argument(
'--search_field', dest=
"search_field",
37 help=
"Search a line from the file. Specify the field to "
39 p.add_argument(
'--search_value', dest=
"search_value",
40 help=
"Search a line from the file. Specify the value to "
42 p.add_argument(
'--nframe', action=
"store_true", dest=
"nframe", default=
False,
43 help=
"Print the frame number as initial column")
45 result = p.parse_args()
51 if not result.filename
is None:
52 f = open(result.filename,
"r")
54 raise ValueError(
"No file name provided. Use -h for help")
57 for line
in f.readlines():
59 klist = list(d.keys())
61 if "STAT2HEADER" in klist:
65 if "STAT2HEADER" in str(k):
66 if result.print_header:
72 for k
in sorted(stat2_dict.items(), key=operator.itemgetter(1))]
74 for k
in sorted(stat2_dict.items(), key=operator.itemgetter(1))]
77 invstat2_dict.update({stat2_dict[k]: k})
86 if result.print_fields:
91 print(key[0:100],
"... omitting the rest of the string (>100 characters)")
96 match_strictness = 1.0
98 match_strictness = 0.1
101 if not result.fields
is None:
105 for field
in result.fields:
106 found_entries = difflib.get_close_matches(
111 if len(found_entries) == 0:
112 raise ValueError(
"field " + field +
" non found")
114 field_list.append(found_entries[0])
117 s0 =
' '.join([
"%20s" % (field)
for field
in field_list])
121 f = open(result.filename,
"r")
123 for line
in f.readlines():
128 print(
"# Warning: skipped line number " + str(line_number) +
" not a valid line")
131 s0 =
' '.join([
"%20s" % (str(d[field]))
for field
in field_list])
135 s0 =
' '.join([
"%20s" % (str(d[invstat2_dict[field]]))
136 for field
in field_list])
137 if not result.nframe:
140 print(str(line_number)+
" > " + s0)
144 if not result.single_column_field
is None:
147 if result.single_column_field
in k:
150 f = open(result.filename,
"r")
152 for line
in f.readlines():
157 print(
"# Warning: skipped line number " + str(line_number) +
" not a valid line")
160 for key
in field_list:
165 for key
in field_list:
166 print(key, d[invstat2_dict[key]])
170 if (
not result.search_field
is None)
and (
not result.search_value
is None):
173 found_entries = difflib.get_close_matches(
178 if len(found_entries) == 0:
179 raise ValueError(
"field " + results.search_field +
" non found")
181 corrected_field = found_entries[0]
183 f = open(result.filename,
"r")
185 for line
in f.readlines():
190 print(
"# Warning: skipped line number " + str(line_number) +
" not a valid line")
194 if (str(d[corrected_field]) == result.search_value):
200 if (str(d[invstat2_dict[corrected_field]]) == result.search_value):
202 print(key, d[invstat2_dict[key]])
205 if not result.print_raw_number
is None:
208 f = open(result.filename,
"r")
210 for line
in f.readlines():
213 if (line_number == int(result.print_raw_number)):
217 print(
"# Warning: skipped line number " + str(line_number) +
" not a valid line")
223 if (line_number == int(result.print_raw_number) + 1):
227 print(
"# Warning: skipped line number " + str(line_number) +
" not a valid line")
230 print(key, d[invstat2_dict[key]])