spacepy.pycdf

Interface to the Common Data Format (CDF) library

CDF available at http://cdf.gsfc.nasa.gov/.

The interface is intended to be ‘pythonic’ rather than reproducing the C interface. To open or close a CDF and access its variables, see the CDF class. Accessing data within the variables is via the Var class. The lib object provides access to some routines that affect the functionality of the library in general. The const module contains constants useful for accessing the underlying library.

The CDF C library must be properly installed in order to use this module. Installing SpacePy from a binary installation provides this requirement.

The CDF distribution provides scripts meant to be called in a user’s login scripts, definitions.B for bash and definitions.C for C-shell derivatives. (See the installation instructions which come with the CDF library.) These will set environment variables specifying the location of the library; pycdf will respect these variables if they are set. Otherwise it will search the standard system library path and the default installation locations for the CDF library, falling back to the version shipped with the SpacePy binary if the library is not found.

If pycdf has trouble finding the library, try setting CDF_LIB before importing the module, e.g. if the library is in CDF/lib in the user’s home directory:

>>> import os
>>> os.environ["CDF_LIB"] = "~/CDF/lib"
>>> from spacepy import pycdf

If this works, make the environment setting permanent. Note that on OSX, using plists to set the environment may not carry over to Python terminal sessions; use .cshrc or .bashrc instead.

Authors: Jon Niehof

Institution: University of New Hampshire

Contact: Jonathan.Niehof@unh.edu

For additional documentation pycdf - Python interface to CDF files

Copyright 2010-2015 Los Alamos National Security, LLC.

Submodules

const

Various constants defined in cdf.h and used in pycdf.

istp

Support for ISTP-compliant CDFs

Functions

concatCDF(cdfs[, varnames, raw])

Concatenate data from multiple CDFs

Classes

Attr(cdf_file, attr_name[, create])

An attribute, g or z, for a CDF

AttrList(cdf_file[, special_entry])

Object representing a list of attributes.

CDF(pathname[, masterpath, create, ...])

Python object representing a CDF file.

CDFCopy(cdf)

A dictionary-like copy of all data and attributes in a CDF

Library([libpath, library])

Abstraction of the base CDF C library and its state.

Var(cdf_file, var_name, *args)

A CDF variable.

VarCopy(zVar)

A list-like copy of the data and attributes in a Var

gAttr(cdf_file, attr_name[, create])

Global Attribute for a CDF

gAttrList(cdf_file[, special_entry])

Object representing all the gAttributes in a CDF.

zAttr(cdf_file, attr_name[, create])

zAttribute for zVariables within a CDF.

zAttrList(zvar)

Object representing all the zAttributes in a zVariable.

Attributes

Exceptions

CDFError(status)

Raised for an error in the CDF library.

CDFException(status)

Base class for errors or warnings in the CDF library.

CDFWarning(status)

Used for a warning in the CDF library.

EpochError

Used for errors in epoch routines

spacepy.pycdf.concatCDF(cdfs, varnames=None, raw=False)[source]

Concatenate data from multiple CDFs

Reads data from all specified CDFs in order and returns as if they were from a single CDF. The assumption is that the CDFs all have the same structure (same variables, each with the same dimensions and variance.)

Parameters:
cdfslist of Var

Open CDFs, will be read from in order. Must be a list (cannot be an iterable, as all files need to be open).

varnameslist of str

Names of variables to read (default: all variables in first CDF)

rawbool

If True, read variables as raw (don’t convert epochs, etc.) Default False.

Returns:
SpaceData

data concatenated from each CDF, with all attributes from first. Non-record-varying data is also only from first, and record variance is only checked on the first!

Examples

Read all data from all CDFs in the current directory. Note that CDFs are closed when their variable goes out of scope.

>>> import glob
>>> import spacepy.pycdf
>>> data = spacepy.pycdf.concatCDF([
...     spacepy.pycdf.CDF(f) for f in glob.glob('*.cdf')])
exception spacepy.pycdf.CDFError(status)[source]

Raised for an error in the CDF library.

exception spacepy.pycdf.CDFException(status)[source]

Base class for errors or warnings in the CDF library.

Not normally used directly, but in subclasses CDFError and CDFWarning.

Error messages provided by this class are looked up from the underlying C library.

exception spacepy.pycdf.CDFWarning(status)[source]

Used for a warning in the CDF library.

exception spacepy.pycdf.EpochError[source]

Used for errors in epoch routines

spacepy.pycdf.lib = <spacepy.pycdf.Library object>

Module global library object.

Initalized at module load time so all classes have ready access to the CDF library and a common state. E.g:

>>> from spacepy import pycdf
>>> pycdf.lib.version
    (3, 3, 0, ' ')