OpenMD 3.1
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
omdSplit
1#!@Python3_EXECUTABLE@
2"""OpenMD File Splitter
3
4Splits OpenMD dump file frames into seperate omd files
5
6Usage: omdSplit
7
8Options:
9 -h, --help show this help
10 -m, --meta-data=... use specified OpenMD (.omd) file
11 -s, --split-frame=... split every n frames
12
13
14Example:
15 omdSplit -m Ar.dump
16
17"""
18
19__author__ = "Charles Vardeman (cvardema@nd.edu)"
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
29_haveMDFileName = 0
30_haveSplitFrame = 0
31
32
33def usage():
34 print(__doc__)
35
36
37def splitFile(mdFileName, skipFrameNumber):
38 mdFile = open(mdFileName, 'r')
39
40 (filepath, filename) = os.path.split(mdFileName)
41
42 (outfilePrefix, ext) = os.path.splitext(filename)
43
44
45
46 metaDataFrame = []
47 haveMetaData = 0
48 haveMetaDataEnd = 0
49# Find OpenMD version info first
50 line = mdFile.readline()
51 while True:
52 if '<OOPSE version=' in line or '<OpenMD version=' in line:
53 OpenMDversion = line
54 break
55 line = mdFile.readline()
56
57# Rewind file and find start of MetaData block
58
59 mdFile.seek(0)
60 breakLoop = False
61 line = mdFile.readline()
62
63 while True:
64 if '<MetaData>' in line:
65 while 2:
66 metaDataFrame.append(line)
67 line = mdFile.readline()
68 if '</MetaData>' in line:
69 metaDataFrame.append(line)
70 breakLoop = True
71 break
72 if breakLoop:
73 break
74 line = mdFile.readline()
75
76
77 mdFile.seek(0)
78 framePos = 0
79 foundFrames = 0;
80 line = mdFile.readline()
81 while True:
82 if '<Snapshot>' in line:
83 foundFrames += 1
84 division, remainder=divmod(foundFrames, int(skipFrameNumber))
85 if (remainder == 0):
86 outfilename = outfilePrefix + "_" + "%04d" % foundFrames + ext
87 outFile = open(outfilename, 'w')
88 outFile.write(OpenMDversion)
89 for metaline in metaDataFrame:
90 outFile.write(metaline)
91 while not '</Snapshot>' in line:
92 outFile.write(line)
93 line = mdFile.readline()
94 outFile.write(line)
95 outFile.write("</OpenMD>")
96 outFile.close()
97 line = mdFile.readline()
98 if not line: break
99
100 mdFile.close()
101
102
103def main(argv):
104 try:
105 opts, args = getopt.getopt(argv, "hm:s:", ["help", "meta-data=", "split-frame", ""])
106 except getopt.GetoptError:
107 usage()
108 sys.exit(2)
109 for opt, arg in opts:
110 if opt in ("-h", "--help"):
111 usage()
112 sys.exit()
113 elif opt in ("-m", "--meta-data"):
114 mdFileName = arg
115 global _haveMDFileName
116 _haveMDFileName = 1
117 elif opt in ("-s", "--split-frame"):
118 skipFrameNumber = arg
119 global _haveSplitFrame
120 _haveSplitFrame = 1
121 if (_haveMDFileName != 1):
122 usage()
123 print("No OpenMD file was specified")
124 sys.exit()
125 if (_haveSplitFrame != 1):
126 usage()
127 print("Frames to split was not specified")
128 sys.exit()
129 splitFile(mdFileName, skipFrameNumber);
130
131if __name__ == "__main__":
132 if len(sys.argv) == 1:
133 usage()
134 sys.exit()
135 main(sys.argv[1:])
136