Vlasiator ebf0dd394 on dev (v5.4.0 + 1054 commits)
Loading...
Searching...
No Matches
logparser.py
Go to the documentation of this file.
1import re
2import os
3import argparse
4
5
6parser = argparse.ArgumentParser(
7 prog="Logparser",
8 description="For pruning restarts from the logfile such that the latest successful timestep is left in the log",
9)
10parser.add_argument("logfile", type=str)
11parser.add_argument("outputfile", type=str)
12args = parser.parse_args()
13input = args.logfile
14output = args.outputfile
15
16logdict = {}
17with open(input, "r") as logfile:
18 loglines = logfile.readlines()
19 iterator = iter(loglines)
20 n = 0
21 line = iterator.__next__()
22 logdict[-1] = []
23 while n < len(loglines):
24 try:
25 regex = re.search(r"^-{10}\Wtstep\W=\W(\d+).+?-{10}", line)
26 if regex:
27 tstep = regex.group(1)
28
29 logdict[tstep] = [line]
30 line = iterator.__next__()
31 n += 1
32 while not re.search(r"^-{10}\Wtstep\W=\W(\d+).+?-{10}", line):
33 logdict[tstep].append(line)
34 # we dont want to do this at last step
35 try:
36 line = iterator.__next__()
37 except StopIteration:
38 break
39 n += 1
40 else:
41 logdict[-1].append(line)
42 line = iterator.__next__()
43
44 except StopIteration:
45 break
46 logfile.close()
47
48with open(output, "w") as fileoutput:
49 for lines in logdict.values():
50 for line in lines:
51 fileoutput.write(line)