typhon.arts.xml

Reading / writing XML files

This module provides functionality for reading and writing ARTS XML files.

typhon.arts.xml.load(filename)[source]

Load a variable from an ARTS XML file.

The input file can be either a plain or gzipped XML file

Parameters:filename (str) – Name of ARTS XML file.
Returns:Data from the XML file. Type depends on data in file.

Example

>>> typhon.arts.xml.load('tests/reference/matrix.xml')
array([[ 0.,  1.],
       [ 2.,  3.]])
typhon.arts.xml.save(var, filename, precision='.7e', format='ascii', comment=None)[source]

Save a variable to an ARTS XML file.

Parameters:
  • var – Variable to be stored.
  • filename (str) – Name of output XML file. If the name ends in .gz, the file is compressed on the fly.
  • precision (str) – Format for output precision.
  • format (str) – Output format: ‘ascii’ (default) or ‘binary’.
  • comment (str) – Comment string included in a tag above data.

Note

Python’s gzip module is extremely slow in writing. Consider compressing files manually after writing them normally.

Example

>>> x = numpy.array([1.,2.,3.])
>>> typhon.arts.xml.save(x, 'myvector.xml')
typhon.arts.xml.load_directory(directory, exclude=None)[source]

Load all XML files in a given directory.

Search given directory for files with ‘.xml’-extension and try to load them using load().

Parameters:
  • directory (str) – Path to the directory.
  • exclude (list[str]) – Filenames to exlude.
Returns:

Dictionary, filenames without extension are used as key.

Return type:

dictionary

Example

Load all files in foo except for the lookup table in abs_lookup.xml.

>>> load_directory('foo', exclude=['abs_lookup.xml'])
typhon.arts.xml.load_indexed(filename)[source]

Load all indexed XML files matching the given filename.

The function searches all files matching the pattern <filename>.<file_index>.xml or <filename>.<file_index>.xml.gz.

A list with the loaded file contents is returned. The list indices are equivalent to the file indices.

Parameters:filename (str) – Filename.
Returns:List of file contents.
Return type:list

Example

Load all files matching the pattern foo.<file_index>.xml.

>>> load_indexed_xml('foo')

Internal functions

typhon.arts.xml.read

Read ARTS XML types

This packages contains the internal implementation for reading ARTS XML files.

typhon.arts.xml.read.parse(source, binaryfp=None)[source]

Parse ArtsXML file from source.

Parameters:source (str) – Filename or file pointer.
Returns:XML Tree of the ARTS data file.
Return type:xml.etree.ElementTree

typhon.arts.xml.write

Write ARTS XML types

This package contains the internal implementation for writing ARTS XML files.

class typhon.arts.xml.write.ARTSXMLWriter(fp, precision='.7e', binaryfp=None)[source]

Bases: object

Class to output a variable to an ARTS XML file.

binaryfilepointer

BufferedWriter – Binary output file.

close_tag(newline=True)[source]

Close current XML tag.

filepointer

TextIOWrapper – Output file.

open_tag(tag, attr=None, newline=True)[source]

Write opening tag with attributes.

Parameters:
  • tag (str) – Tag name.
  • attr (dict) – Optional XML attributes.
  • newline (bool) – Put newline after tag.
precision

str – Floating point output format.

write(s)[source]

Write string to XML file.

write_basic_type(name, var, attr=None, precision='')[source]

Write a basic ARTS type as XML.

Parameters:
write_comment(comment)[source]

Write comment tag to XML file.

Write closing tag for ARTS XML file.

write_header(version=1)[source]

Write XML file header.

Writes the XML header and the opening arts tag.

Parameters:version (int) – ARTS XML version.
write_ndarray(var, attr)[source]

Convert ndarray to ARTS XML representation.

For arguments see write_xml().

write_xml(var, attr=None, arraytype=None)[source]

Write a variable as XML.

Writing basic matpack types is implemented here. Custom types (e.g. GriddedFields) must implement a class member function called ‘write_xml’.

Tuples and list are mapped to ARTS Array types.