From 616009dc041b3dfe1b70531f6d00fd61d2a97233 Mon Sep 17 00:00:00 2001 From: Sterling Harper Date: Fri, 5 Sep 2014 22:55:42 -0400 Subject: [PATCH] Added a utility to update lattices in xml inputs. --- src/utils/update_lattices.py | 107 +++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100755 src/utils/update_lattices.py diff --git a/src/utils/update_lattices.py b/src/utils/update_lattices.py new file mode 100755 index 0000000000..38c325921b --- /dev/null +++ b/src/utils/update_lattices.py @@ -0,0 +1,107 @@ +#!/usr/bin/env python +"""Update lattices in geometry .xml files to the post-hex_lattice format. + +Usage information can be obtained by running 'track.py --help': + + usage: update_lattices.py [-h] IN [IN ...] + + Update lattices in geometry.xml files to the latest format. + + positional arguments: + IN Input geometry.xml file(s). + + optional arguments: + -h, --help show this help message and exit + +""" + +from __future__ import print_function + +import argparse + + +def parse_args(): + """Read the input files from the commandline.""" + # Create argument parser. + parser = argparse.ArgumentParser(description=\ + 'Update lattices in geometry.xml files to the latest format.') + parser.add_argument('input', metavar='IN', type=str, nargs='+', + help='Input geometry.xml file(s).') + + # Parse and return commandline arguments. + return parser.parse_args() + + +def read_file(fname): + """Open a file and return a list of lines.""" + fin = open(fname) + lines = fin.readlines() + fin.close() + return lines + + +def process_lattice(lines): + """Delete 'type' and replace 'width' with 'pitch' in lattice string.""" + assert 'type' in lines + assert 'width' in lines + + # Delete the 'type' attribute or tag. + if '' in lines: + # 'type' is a subelement. + start = lines.index('') + end = lines.index('') + 6 + lines = ''.join([lines[:start], lines[end+1:]]) + else: + # 'type' is an attribute. + start = lines.index('type') + end = lines.index('rectangular') + 11 # adjusted to include end quote + lines = ''.join([lines[:start], lines[end+1:]]) + + # Change 'width' to 'pitch'. + lines = lines.replace('width', 'pitch') + + return lines + + +def process_xml(file_lines): + """Update the lattices in the given geometry.xml lines.""" + # Join the list of lines into one big string to simplify the indexing of + # the xml tabs. + lines = ''.join(file_lines) + # Seperate the lattice elements. + lats = lines.split('