OpenMD 3.1
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
omdShrink
1#!@Python3_EXECUTABLE@
2"""OpenMD File Trimmer
3
4Skips every n frames of an OpenMD dump file and loads it into new dump file
5
6Usage: omdShrink
7
8Options:
9 -h, --help show this help
10 -m, --meta-data=... use specified OpenMD (.dump) file
11 -o, --output-file=... use specified output file
12 -s, --split-frame=... split every n frames
13
14
15Example:
16 omdShrink -s 10 -m long.dump -o short.dump
17
18"""
19
20__copyright__ = "Copyright (c) 2004-present The University of Notre Dame. All Rights Reserved."
21__license__ = "OpenMD"
22
23import sys
24import getopt
25import string
26import os
27
28_haveMDFileName = 0
29_haveOutFileName = 0
30_haveSplitFrame = 0
31
32def usage():
33 print(__doc__)
34
35def shrinkFile(mdFileName, outputFileName, skipFrameNumber):
36 mdFile = open(mdFileName, 'r')
37 outFile = open(outputFileName, 'w')
38
39 metaDataFrame = []
40 haveMetaData = 0
41 haveMetaDataEnd = 0
42 # Find OpenMD version info first
43 line = mdFile.readline()
44 while True:
45 if '<OOPSE version=' in line or '<OpenMD version=' in line:
46 OpenMDversion = line
47 break
48 line = mdFile.readline()
49
50 # Rewind file and find start of MetaData block
51
52 mdFile.seek(0)
53 breakLoop = False
54 line = mdFile.readline()
55
56 while True:
57 if '<MetaData>' in line:
58 while 2:
59 metaDataFrame.append(line)
60 line = mdFile.readline()
61 if '</MetaData>' in line:
62 metaDataFrame.append(line)
63 breakLoop = True
64 break
65 if breakLoop:
66 break
67 line = mdFile.readline()
68
69 outFile.write(OpenMDversion)
70 for metaline in metaDataFrame:
71 if 'sampleTime' in metaline:
72 metasplit = metaline.split()
73 sampleTime = float(metasplit[2].strip(';'))
74 newSampleTime = sampleTime * int(skipFrameNumber)
75 outFile.write('sampleTime = %10d;\n' % (newSampleTime))
76 else:
77 outFile.write(metaline)
78
79 mdFile.seek(0)
80 framePos = 0
81 foundFrames = 0;
82 line = mdFile.readline()
83 while True:
84 if '<Snapshot>' in line:
85 division, remainder=divmod(foundFrames, int(skipFrameNumber))
86 foundFrames += 1
87 if (remainder == 0):
88 while not '</Snapshot>' in line:
89 outFile.write(line)
90 line = mdFile.readline()
91 outFile.write(line)
92 line = mdFile.readline()
93 if not line: break
94
95 outFile.write("</OpenMD>")
96 outFile.close()
97 mdFile.close()
98
99
100def main(argv):
101 try:
102 opts, args = getopt.getopt(argv, "hm:o:s:", ["help", "meta-data=", "output-file=", "split-frame", ""])
103 except getopt.GetoptError:
104 usage()
105 sys.exit(2)
106 for opt, arg in opts:
107 if opt in ("-h", "--help"):
108 usage()
109 sys.exit()
110 elif opt in ("-m", "--meta-data"):
111 mdFileName = arg
112 global _haveMDFileName
113 _haveMDFileName = 1
114 elif opt in ("-o", "--output-file"):
115 outputFileName = arg
116 global _haveOutFileName
117 _haveOutFileName = 1
118 elif opt in ("-s", "--split-frame"):
119 skipFrameNumber = arg
120 global _haveSplitFrame
121 _haveSplitFrame = 1
122 if (_haveMDFileName != 1):
123 usage()
124 print("No OpenMD input file was specified")
125 sys.exit()
126 if (_haveOutFileName != 1):
127 usage()
128 print("No output file was specified")
129 sys.exit()
130 if (_haveSplitFrame != 1):
131 usage()
132 print("Frames to split was not specified")
133 sys.exit()
134 shrinkFile(mdFileName, outputFileName, skipFrameNumber);
135
136if __name__ == "__main__":
137 if len(sys.argv) == 1:
138 usage()
139 sys.exit()
140 main(sys.argv[1:])