Skip to content

Instantly share code, notes, and snippets.

@paulromano
Created July 30, 2012 21:22
Show Gist options
  • Save paulromano/3210387 to your computer and use it in GitHub Desktop.
Save paulromano/3210387 to your computer and use it in GitHub Desktop.
Convert <source> element to new format for OpenMC settings.xml files
#!/usr/bin/env python
import sys
# Check that path was supplied
if len(sys.argv) < 2:
print("Must supply path to settings.xml!")
sys.exit(1)
# Get path to settings.xml and name of new file
inputfile = sys.argv[1]
if len(sys.argv) == 3:
outputfile = sys.argv[2]
else:
outputfile = None
# Parse settings.xml
lines = open(inputfile,'r').readlines()
slines = [line.strip() for line in lines]
# Determine where <source> block is
index_source_start = slines.index('<source>')
index_source_end = slines.index('</source>')
index_position = lines[index_source_start].index('<')
# Indent lines inside <source>
for i in range(index_source_start+1,index_source_end):
lines[i] = ' ' + lines[i].replace('coeffs', 'parameters')
# Add <space> and </space>
lines.insert(index_source_start + 1, ' '*(index_position + 2) + '<space>\n')
lines.insert(index_source_end + 1, ' '*(index_position + 2) + '</space>\n')
# Print modified settings.xml
if outputfile:
open(outputfile,'w').writelines(lines)
else:
for line in lines:
print(line.rstrip())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment