From 71b384140da20049de77048559f938fb3e97f0e9 Mon Sep 17 00:00:00 2001 From: Thomas Fischer <thomas.fischer@ufz.de> Date: Sun, 2 Aug 2020 09:32:42 +0200 Subject: [PATCH] Script to add offset in ExponentialProperty. --- xml/addOffsetToExponentProperty.py | 34 ++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 xml/addOffsetToExponentProperty.py diff --git a/xml/addOffsetToExponentProperty.py b/xml/addOffsetToExponentProperty.py new file mode 100644 index 0000000..38961e6 --- /dev/null +++ b/xml/addOffsetToExponentProperty.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python + +from lxml import etree as ET +import re +import shutil +import sys + +def writeResult(filename, tree): + tree.write(open(filename, 'wb'), encoding=tree.docinfo.encoding, pretty_print=True, xml_declaration=True) + +def addOffsetToExponentialProperty(exponential_property): + exponential_property.append(ET.XML("<offset>0</offset>")) + +def addOffsetToExponentialProperties(medium): + for p in medium.find("./properties"): + if p.find("./type").text == "Exponential": + if p.find("./offset") is None: + addOffsetToExponentialProperty(p) + +def processFile(filename): + shutil.copy2(filename, filename+'.bak') + parser = ET.XMLParser(remove_blank_text=True) + tree = ET.parse(filename, parser) + root = tree.getroot() + + for medium in tree.xpath('./media/medium'): + addOffsetToExponentialProperties(medium) + phase_path = "./phases/phase" + for phase in medium.xpath(phase_path): + addOffsetToExponentialProperties(phase) + + writeResult(filename, tree) + +processFile(sys.argv[1]) -- GitLab