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. spacepy.pycdf.Attr.__len__() will return the number of gEntries; use
max_idx()
to find the highest defined gEntry number andhas_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 raiseIndexError
. AssigningNone
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):
existing gEntry of the same number in this gAttribute
other gEntries in this gAttribute
data-matching constraints described in
CDF.new()
.
See also
- __init__(cdf_file, attr_name, create=False)¶
Initialize this attribute
- Parameters:
- cdf_file
CDF
CDF file containing this attribute
- attr_namestr
Name of this attribute
- createbool
True to create attribute, False to look up existing
- cdf_file
Methods
Attributes
- ATTR_MAXENTRY_ = c_long(89)¶
- ATTR_NUMENTRIES_ = c_long(90)¶
- ENTRY_ = c_long(96)¶
- ENTRY_DATASPEC_ = c_long(100)¶
- ENTRY_DATATYPE_ = c_long(98)¶
- ENTRY_DATA_ = c_long(101)¶
- ENTRY_EXISTENCE_ = c_long(97)¶
- ENTRY_NUMELEMS_ = c_long(99)¶
- SCOPE = c_long(1)¶