spacepy.pycdf.gAttr

class spacepy.pycdf.gAttr(cdf_file, attr_name, create=False)[source]

Global Attribute for a CDF

Represents a CDF attribute, providing access to the gEntries in a format that looks like a Python list. General list information is available in the python docs: 1, 2, 3.

Normally accessed by providing a key to a gAttrList:

>>> attribute = cdffile.attrs['attribute_name']
>>> first_gentry = attribute[0]

Each element of the list is a single gEntry of the appropriate type. The index to the elements is the gEntry number.

A gEntry may be either a single string or a 1D array of numerical type. Entries of numerical type (everything but CDF_CHAR and CDF_UCHAR) with a single element are returned as scalars; multiple-element entries are returned as a list. No provision is made for accessing below the entry level; the whole list is returned at once (but Python’s slicing syntax can be used to extract individual items from that list.)

Multi-dimensional slicing is not supported; an entry with multiple elements will have all elements returned (and can thus be sliced itself). Example:

>>> first_three = attribute[5, 0:3] #will fail
>>> first_three = attribute[5][0:3] #first three elements of 5th Entry

gEntries are not necessarily contiguous; a gAttribute may have an entry 0 and entry 2 without an entry 1. len() will return the number of gEntries; use max_idx() to find the highest defined gEntry number and has_entry() to determine if a particular gEntry number exists. Iterating over all entries is also supported:

>>> entrylist = [entry for entry in attribute]

Deleting gEntries will leave a “hole”:

>>> attribute[0:3] = [1, 2, 3]
>>> del attribute[1]
>>> attribute.has_entry(1)
False
>>> attribute.has_entry(2)
True
>>> print attribute[0:3]
[1, None, 3]

Multi-element slices over nonexistent gEntries will return None where no entry exists. Single-element indices for nonexistent gEntries will raise IndexError. Assigning None to a gEntry will delete it.

When assigning to a gEntry, the type is chosen to match the data; subject to that constraint, it will try to match (in order):

  1. existing gEntry of the same number in this gAttribute

  2. other gEntries in this gAttribute

  3. data-matching constraints described in CDF.new().

See also

Attr