-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_undefined.py
More file actions
60 lines (43 loc) · 1.67 KB
/
remove_undefined.py
File metadata and controls
60 lines (43 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from pathlib import Path
from pprint import pprint
def read_file(input_path):
with input_path.open() as fh:
lines = fh.readlines()
return lines
def write_file(output_path, lines):
with output_path.open(mode='w') as fh:
for line in lines:
fh.write(f'{line}\n')
def replace_undefined(lines):
modified_lines = [line.split() for line in lines]
first_defined_pos = get_starting_pos(modified_lines)
replace_leading_undefined(modified_lines, first_defined_pos)
replace_trailing_undefined(modified_lines, first_defined_pos)
output_lines = [' '.join(line) for line in modified_lines]
return output_lines
def get_starting_pos(lines):
for i, line in enumerate(lines):
if line[0].startswith('Time_s'):
continue
elif not line[0].startswith('--undefined--'):
return i
def replace_leading_undefined(lines, starting_pos):
current_pos = starting_pos
while current_pos > 0:
if lines[current_pos-1][0] == 'Time_S':
break
else:
old_time = float(lines[current_pos][0])
new_time = "{:.6f}".format(old_time - 0.01)
lines[current_pos-1][0] = new_time
current_pos -= 1
return lines
def replace_trailing_undefined(lines, starting_pos):
current_pos = starting_pos
while current_pos < len(lines) - 1:
if lines[current_pos+1][0] == '--undefined--':
old_time = float(lines[current_pos][0])
new_time = "{:.6f}".format(old_time + 0.01)
lines[current_pos+1][0] = new_time
current_pos += 1
return lines