Created
December 5, 2016 22:58
-
-
Save nluetzge/549cf6737a7256c6cbb88e75ff299b0f to your computer and use it in GitHub Desktop.
APT_Transforms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
########################################################################################## | |
NAME: apt2list.py | |
AUTHOR: Nora Luetzgendorf | |
EXAMPLE: ./apt2list.py 'NS 4.7.2-7 NIRSpec Model (gratings)' '1132' 'Model' | |
DESCRIPTION: | |
Transforming an observation Folder of an APT file into a csv file. | |
INPUTS: | |
obs_name: Name of the Observation Folder) | |
input_xml: Name of the Input xml file. The original APT file. This can be extracted | |
by decompressing the .aptx file | |
OUTPUTS: | |
output_csv: Name of the Output csv which can be opened and altered with Excel and | |
transformed back to a xml file with list2apt.py | |
########################################################################################## | |
""" | |
from lxml import etree | |
from astropy.table import Table, Column | |
from astropy.io import ascii | |
import argparse | |
########################################################################################## | |
# Main | |
########################################################################################## | |
# | |
# INPUT | |
parser = argparse.ArgumentParser() | |
parser.add_argument('obs_name', type=str, help='Name of the Observation Folder') | |
parser.add_argument('input_xml', type=str, help='Name of the Input xml file') | |
parser.add_argument('output_csv', type=str, help='Name of the Input csv file') | |
args = parser.parse_args() | |
obs_name = args.obs_name | |
input_xml = args.input_xml | |
output_csv = args.output_csv | |
# CONSTANTS | |
path = "//apt:Observation[apt:Label[contains(string(), '{}')]]/apt:Template/nsil:NirspecInternalLamp/nsil:ExposureList".format(obs_name) | |
Mylist = {'OperatingMode': [], 'Subarray': [], 'MsaConfigFile': [], 'ReadoutPattern': [], 'Groups': [], 'Integrations': [], 'Lamp': [], 'Grating': []} | |
# READ | |
# XML file | |
with open('%s.xml' % input_xml) as f: | |
tree = etree.parse(f) | |
# APT makes extensive use of XML namespaces | |
# (e.g. 'xmlns:nsmsasd="http://www.stsci.edu/JWST/APT/Template/NirspecMSAShortDetect"') | |
# so we have to as well | |
namespaces = tree.getroot().nsmap.copy() | |
# There is no 'default' namespace for XPath (used below), but the lxml parser | |
# does respect a default namespace, so we have to update its name from | |
# 'None' to 'apt' | |
namespaces['apt'] = namespaces[None] | |
del namespaces[None] | |
# Find your specific Observation | |
results = tree.xpath(path, namespaces=namespaces) | |
ExposureList = results[0] | |
# Find the Entries of the Observation | |
for item in Mylist: | |
entryList = ExposureList.xpath('nsil:Exposure/nsil:%s' % item,namespaces=namespaces) | |
for entry in entryList: | |
Mylist[item].append(entry.text) | |
# WRITE csv file | |
ascii.write(Table(Mylist), '%s.csv' % output_csv, format='csv') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
########################################################################################## | |
NAME: list2apt.py | |
AUTHOR: Nora Luetzgendorf | |
EXAMPLE: ./list2apt.py 'NS 4.7.2-7 NIRSpec Model (gratings)' 'Model_new' '1132' '1132_v2' | |
DESCRIPTION: | |
Transforms a csv list into an xml file that can be loaded into APT. It needs an already | |
existing APT file with at least one Observation to load it in. | |
INPUTS: | |
obs_name: Name of the Observation Folder | |
input_cvs: Name of the Input csv file (the table) | |
input_xml: Name of the Input xml file. The original APT file. This can be extracted | |
by decompressing the .aptx file | |
OUTPUTS: | |
output_xml: Name of the Output xml file. This can be loaded into APT and saved as an | |
.aptx file. | |
########################################################################################## | |
""" | |
from lxml import etree | |
from astropy.table import Table, Column | |
from astropy.io import ascii | |
import argparse | |
########################################################################################## | |
# Main | |
########################################################################################## | |
# | |
# INPUT | |
parser = argparse.ArgumentParser() | |
parser.add_argument('obs_name', type=str, help='Name of the Observation Folder') | |
parser.add_argument('input_csv', type=str, help='Name of the Input csv file') | |
parser.add_argument('input_xml', type=str, help='Name of the Input xml file') | |
parser.add_argument('output_xml', type=str, help='Name of the Output xml file') | |
args = parser.parse_args() | |
obs_name = args.obs_name | |
input_csv = args.input_csv | |
input_xml = args.input_xml | |
output_xml = args.output_xml | |
# CONSTANTS | |
path = "//apt:Observation[apt:Label[contains(string(), '{}')]]/apt:Template/nsil:NirspecInternalLamp/nsil:ExposureList".format(obs_name) | |
Mylist = ['OperatingMode', 'Subarray', 'MsaConfigFile', 'ReadoutPattern', 'Groups', 'Integrations', 'Lamp','Grating'] | |
# READ | |
# XML file | |
with open('./%s.xml' % input_xml) as f: | |
tree = etree.parse(f) | |
# CSV file | |
t = ascii.read('%s.csv'% input_csv, format='csv') | |
# APT makes extensive use of XML namespaces | |
# (e.g. 'xmlns:nsmsasd="http://www.stsci.edu/JWST/APT/Template/NirspecMSAShortDetect"') | |
# so we have to as well | |
namespaces = tree.getroot().nsmap.copy() | |
# There is no 'default' namespace folr XPath (used below), but the lxml parser | |
# does respect a default namespace, so we have to update its name from | |
# 'None' to 'apt' | |
namespaces['apt'] = namespaces[None] | |
del namespaces[None] | |
# Find your specific Observation | |
results = tree.xpath(path, namespaces=namespaces) | |
ExposureList = results[0] | |
nExp = len(ExposureList.getchildren()) | |
# Fill in the new values and add entries if needed | |
for i in range(len(t)): | |
if i >= nExp: | |
new_element = etree.fromstring(etree.tostring(ExposureList.xpath('//nsil:Exposure',namespaces=namespaces)[0])) | |
ExposureList.append(new_element) | |
for item in Mylist: | |
entry = ExposureList.xpath('nsil:Exposure/nsil:%s' % item,namespaces=namespaces)[i] | |
entry.text = str(t[item][i]) | |
# WRITE Output file | |
tree.write('%s.xml' % output_xml) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment