2"""OpenMD File Extractor
4Makes omd file from the last good frame of OpenMD dump file.
9 -h, --help show this help
10 -m, --meta-data=... use specified OpenMD (.dump) file
17__author__ = "Hemanta Bhattarai (hbhattar@nd.edu), Charles Vardeman (cvardema@nd.edu)"
18__copyright__ = "Copyright (c) 2004-present The University of Notre Dame. All Rights Reserved."
34def getLastFrame(mdFileName):
35 mdFile = open(mdFileName, 'r')
37 (filepath, filename) = os.path.split(mdFileName)
39 (outfilePrefix, ext) = os.path.splitext(filename)
46# Find OpenMD version info first
47 line = mdFile.readline()
49 if '<OOPSE version=' in line or '<OpenMD version=' in line:
52 line = mdFile.readline()
54# Rewind file and find start of MetaData block
58 line = mdFile.readline()
61 if '<MetaData>' in line:
63 metaDataFrame.append(line)
64 line = mdFile.readline()
65 if '</MetaData>' in line:
66 metaDataFrame.append(line)
71 line = mdFile.readline()
76 # reading last valid frame from dump file
80 lineReversed = reversed(mdFile.readlines())
81 SnapshotFrameReversed = []
82 writeSnapshotFrameReversed = False
83 for line in lineReversed:
84 splittedLine = line.split()
85 if '</Snapshot>' in splittedLine or writeSnapshotFrameReversed == True:
86 writeSnapshotFrameReversed = True
87 SnapshotFrameReversed.append(line)
89 if '<Snapshot>' in splittedLine and writeSnapshotFrameReversed:
94 # checking if there exist valid frame
96 SnapshotFrameReversed[0]
98 print("\t\tError: %s doesn't have any valid frames." %filename)
101 # writing out the valid frame
103 outfilename = outfilePrefix + "_lastframe" + ".omd"
104 outFile = open(outfilename, 'w')
105 outFile.write(OpenMDversion)
106 for metaline in metaDataFrame:
107 outFile.write(metaline)
109 for lineNumber in range(1,len(SnapshotFrameReversed)+1):
110 outFile.write(SnapshotFrameReversed[-lineNumber])
112 outFile.write("</OpenMD>")
120 opts, args = getopt.getopt(argv, "hm:", ["help", "meta-data=", ""])
121 except getopt.GetoptError:
124 for opt, arg in opts:
125 if opt in ("-h", "--help"):
128 elif opt in ("-m", "--meta-data"):
130 global _haveMDFileName
135 if (_haveMDFileName != 1):
137 print("No OpenMD file was specified")
140 getLastFrame(mdFileName)
143if __name__ == "__main__":
144 if len(sys.argv) == 1: