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