-
Notifications
You must be signed in to change notification settings - Fork 0
/
skip_lammpstrj.py
executable file
·74 lines (63 loc) · 2.08 KB
/
skip_lammpstrj.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python
"""Write a reduced lammpstrj file by skipping frames."""
import pathlib
import sys
import re
import io
import mmap
from tqdm import tqdm
def read_lammpstrj(lmp):
"""Iterate frames in a lammpstrj file."""
raw = []
with open(lmp, 'r') as infile:
for lines in infile:
if lines.startswith('ITEM: TIMESTEP'):
if raw:
yield raw
raw = []
raw.append(lines)
if raw:
yield raw
def count_frames(lmp):
"""Count the number of frames in a lammpstrj file."""
pattern = re.compile(b'ITEM: TIMESTEP')
with io.open(lmp, 'r', encoding='utf-8') as infile:
match = pattern.finditer(
mmap.mmap(infile.fileno(), 0, access=mmap.ACCESS_READ)
)
return sum(1 for _ in match)
return 0
def main(infile, skip=10):
"""Write a reduced lammpstrj file by skipping frames."""
print('Skip: {}'.format(skip))
print('Infile: {}'.format(infile))
infile_path = pathlib.Path(infile).resolve()
outfile = '{}-skip-{}.lammpstrj'.format(
str(pathlib.Path(infile).stem), skip
)
outfile_path = infile_path.parent.joinpath(outfile)
print('Outfile: {}'.format(outfile_path))
print('Getting number of frames in original file...')
frames_tot = count_frames(infile_path)
print('Frames in original file: {}'.format(frames_tot))
if frames_tot < 1:
print('No frames found, exiting...')
return
frames = 0
frames_read = 0
with tqdm(total=frames_tot) as pbar:
with open(outfile_path, 'w') as output:
for i, frame in enumerate(read_lammpstrj(infile_path)):
frames_read += 1
pbar.update(1)
if i % skip == 0:
frames += 1
output.write(''.join(frame))
print('Frames read: {}'.format(frames_read))
print('Frames written to new file: {}'.format(frames))
return
if __name__ == '__main__':
try:
main(sys.argv[1], skip=int(sys.argv[2]))
except IndexError:
main(sys.argv[1])