7 p = argparse.ArgumentParser(
8 description=
"Process output data file saved as dictionaries. "
9 "It has two modalities: print selected fields for all "
10 "lines or print a particular line where a field has a "
11 "given value. Example of usage: process_output.py "
12 "--soft -s To E S -f log.3.native-2-no-red. "
13 "process_output.py --soft --search_field EV0 "
14 "--search_value 5.67750116023 "
15 "-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 result.filename
is not 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(),
73 key=operator.itemgetter(1))]
75 for k
in sorted(stat2_dict.items(),
76 key=operator.itemgetter(1))]
79 invstat2_dict.update({stat2_dict[k]: k})
88 if result.print_fields:
94 "... omitting the rest of the string (>100 characters)")
99 match_strictness = 1.0
100 if result.soft_match:
101 match_strictness = 0.1
104 if result.fields
is not None:
108 for field
in result.fields:
109 found_entries = difflib.get_close_matches(
114 if len(found_entries) == 0:
115 raise ValueError(
"field " + field +
" non found")
117 field_list.append(found_entries[0])
120 s0 =
' '.join([
"%20s" % (field)
for field
in field_list])
124 f = open(result.filename,
"r")
126 for line
in f.readlines():
131 print(
"# Warning: skipped line number " +
132 str(line_number) +
" not a valid line")
135 s0 =
' '.join([
"%20s" % (str(d[field]))
for field
in field_list])
139 s0 =
' '.join([
"%20s" % (str(d[invstat2_dict[field]]))
140 for field
in field_list])
141 if not result.nframe:
144 print(str(line_number) +
" > " + s0)
148 if result.single_column_field
is not None:
151 if result.single_column_field
in k:
154 f = open(result.filename,
"r")
156 for line
in f.readlines():
161 print(
"# Warning: skipped line number " +
162 str(line_number) +
" not a valid line")
165 for key
in field_list:
170 for key
in field_list:
171 print(key, d[invstat2_dict[key]])
175 if (result.search_field
is not None)
and (result.search_value
is not None):
178 found_entries = difflib.get_close_matches(
183 if len(found_entries) == 0:
184 raise ValueError(
"field " + result.search_field +
" non found")
186 corrected_field = found_entries[0]
188 f = open(result.filename,
"r")
190 for line
in f.readlines():
195 print(
"# Warning: skipped line number " +
196 str(line_number) +
" not a valid line")
200 if (str(d[corrected_field]) == result.search_value):
206 if (str(d[invstat2_dict[corrected_field]]) == result.search_value):
208 print(key, d[invstat2_dict[key]])
211 if result.print_raw_number
is not None:
214 f = open(result.filename,
"r")
216 for line
in f.readlines():
219 if (line_number == int(result.print_raw_number)):
223 print(
"# Warning: skipped line number "
224 + str(line_number) +
" not a valid line")
230 if (line_number == int(result.print_raw_number) + 1):
234 print(
"# Warning: skipped line number "
235 + str(line_number) +
" not a valid line")
238 print(key, d[invstat2_dict[key]])