Source code for sunpy.io.ana
"""
This module provides an ANA file Reader.
This is a modified version of `pyana <https://github.com/tvwerkhoven/pyana>`__.
.. warning::
The reading and writing of ana files is not supported under Windows.
"""
import os
import collections
from sunpy.io.header import FileHeader
try:
from sunpy.io import _pyana
except ImportError:
_pyana = None
__all__ = ['read', 'get_header', 'write']
HDPair = collections.namedtuple('HDPair', ['data', 'header'])
[docs]def read(filename, debug=False, **kwargs):
"""
Loads an ANA file and returns the data and a header in a list of (data,
header) tuples.
Parameters
----------
filename : `str`
Name of file to be read.
debug : `bool`, optional
Prints verbose debug information.
Returns
-------
out : `list`
A list of (data, header) tuples
Examples
--------
>>> data = sunpy.io.ana.read(filename) # doctest: +SKIP
"""
if not os.path.isfile(filename):
raise OSError("File does not exist!")
if _pyana is None:
raise ImportError("C extension for ANA is missing, please rebuild.")
data = _pyana.fzread(filename, debug)
return [HDPair(data['data'], FileHeader(data['header']))]
[docs]def write(filename, data, comments=False, compress=True, debug=False):
"""
Saves a 2D `numpy.array` as an ANA file and returns the bytes written or
``NULL``.
Parameters
----------
filename : `str`
Name of file to be created.
data : `numpy.ndarray`
The data to be stored.
comments : `~sunpy.io.header.FileHeader`, optional
The comments to be stored as a header.
compress : `bool`, optional
Compress the data with `True` (the default).
debug : `bool`, optional
Prints verbose debug information, defaults to `False`.
Returns
-------
out: ANA compressed archive
A new ANA compressed archive containing the data and header.
Examples
--------
>>> written = sunpy.io.ana.write(filename, data, comments=False, compress=True) # doctest: +SKIP
"""
if _pyana is None:
raise ImportError("C extension for ANA is missing, please rebuild")
if comments:
return _pyana.fzwrite(filename, data, int(compress), comments, debug)
else:
return _pyana.fzwrite(filename, data, int(compress), '', debug)