spacepy.pycdf.CDF¶
- class spacepy.pycdf.CDF(pathname, masterpath=None, create=None, readonly=None, encoding='utf-8')[source]¶
Python object representing a CDF file.
Open or create a CDF file by creating an object of this class.
- Parameters:
- pathnamestring
name of the file to open or create
- masterpathstring
name of the master CDF file to use in creating a new file. If not provided, an existing file is opened; if provided but evaluates to
False
(e.g.,''
), an empty new CDF is created.- createbool
Create a new CDF even if masterpath isn’t provided
- readonlybool
Open the CDF read-only. Default True if opening an existing CDF; False if creating a new one. A readonly CDF with many variables may be slow to close on CDF library versions before 3.8.1. See
readonly()
.- encodingstr, optional
Text encoding to use when reading and writing strings. Default
'utf-8'
.
- Raises:
- CDFError
if CDF library reports an error
- Warns:
- CDFWarning
if CDF library reports a warning and interpreter is set to error on warnings.
Examples
Open a CDF by creating a CDF object, e.g.:
>>> cdffile = pycdf.CDF('cdf_filename.cdf')
Be sure to
close()
orsave()
when done.Note
Existing CDF files are opened read-only by default, see
readonly()
to change.CDF supports the with keyword, like other file objects, so:
>>> with pycdf.CDF('cdf_filename.cdf') as cdffile: ... #do brilliant things with the CDF
will open the CDF, execute the indented statements, and close the CDF when finished or when an error occurs. The python docs include more detail on this ‘context manager’ ability.
CDF objects behave like a python dictionary, where the keys are names of variables in the CDF, and the values,
Var
objects. As a dictionary, they are also iterable and it is easy to loop over all of the variables in a file. Some examples:List the names of all variables in the open CDF
cdffile
:>>> cdffile.keys() >>> for k in cdffile: #Alternate ... print(k)
Get a
Var
object for the variable namedEpoch
:>>> epoch = cdffile['Epoch']
Determine if a CDF contains a variable named
B_GSE
:>>> if 'B_GSE' in cdffile: ... print('B_GSE is in the file') ... else: ... print('B_GSE is not in the file')
Find how many variables are in the file:
>>> print(len(cdffile))
Delete the variable
Epoch
from the open CDF filecdffile
:>>> del cdffile['Epoch']
Display a summary of variables and types in open CDF file
cdffile
:>>> print(cdffile)
Open the CDF named
cdf_filename.cdf
, read all the data from all variables into dictionarydata
, and close it when done or if an error occurs:>>> with pycdf.CDF('cdf_filename.cdf') as cdffile: ... data = cdffile.copy()
This last example can be very inefficient as it reads the entire CDF. Normally it’s better to treat the CDF as a dictionary and access only the data needed, which will be pulled transparently from disc. See
Var
for more subtle examples.Potentially useful dictionary methods and related functions:
The CDF user’s guide section 2.2 has more background information on CDF files.
The
attrs
Python attribute acts as a dictionary referencing CDF attributes (do not confuse the two); all the dictionary methods above also work on the attribute dictionary. SeegAttrList
for more on the dictionary of global attributes.Creating a new CDF from a master (skeleton) CDF has similar syntax to opening one:
>>> cdffile = pycdf.CDF('cdf_filename.cdf', 'master_cdf_filename.cdf')
This creates and opens
cdf_filename.cdf
as a copy ofmaster_cdf_filename.cdf
.Using a skeleton CDF is recommended over making a CDF entirely from scratch, but this is possible by specifying a blank master:
>>> cdffile = pycdf.CDF('cdf_filename.cdf', '')
When CDFs are created in this way, they are opened read-write, see
readonly()
to change.By default, new CDFs (without a master) are created in version 3 format. To create a version 2 (backward-compatible) CDF, use
Library.set_backward()
:>>> pycdf.lib.set_backward(True) >>> cdffile = pycdf.CDF('cdf_filename.cdf', '')
Add variables by direct assignment, which will automatically set type and dimension based on the data provided:
>>> cdffile['new_variable_name'] = [1, 2, 3, 4]
or, if more control is needed over the type and dimensions, use
new()
.Although it is supported to assign Var objects to Python variables for convenience, there are some minor pitfalls that can arise when changing a CDF that will not affect most users. This is only a concern when assigning a zVar object to a Python variable, changing the CDF through some other variable, and then trying to use the zVar object via the originally assigned variable.
Deleting a variable:
>>> var = cdffile['Var1'] >>> del cdffile['Var1'] >>> var[0] #fail, no such variable
Renaming a variable:
>>> var = cdffile['Var1'] >>> cdffile['Var1'].rename('Var2') >>> var[0] #fail, no such variable
Renaming via the same variable works:
>>> var = cdffile['Var1'] >>> var.rename('Var2') >>> var[0] #succeeds, aware of new name
Deleting a variable and then creating another variable with the same name may lead to some surprises:
>>> var = cdffile['Var1'] >>> var[...] = [1, 2, 3, 4] >>> del cdffile['Var1'] >>> cdffile.new('Var1', data=[5, 6, 7, 8] >>> var[...] [5, 6, 7, 8]
- __init__(pathname, masterpath=None, create=None, readonly=None, encoding='utf-8')[source]¶
Open or create a CDF file.
- Parameters:
- pathnamestring
name of the file to open or create
- masterpathstring
name of the master CDF file to use in creating a new file. If not provided, an existing file is opened; if provided but evaluates to
False
(e.g.,''
), an empty new CDF is created.- createbool
Create a new CDF even if masterpath isn’t provided
- readonlybool
Open the CDF read-only. Default True if opening an existing CDF; False if creating a new one.
- encodingstr, optional
Text encoding to use when reading and writing strings. Default
'utf-8'
.
- Raises:
- CDFError
if CDF library reports an error
- CDFWarning
if CDF library reports a warning and interpreter is set to error on warnings.
Examples
Open a CDF by creating a CDF object, e.g.:
>>> cdffile = pycdf.CDF('cdf_filename.cdf')
Methods
add_attr_to_cache
(attrname, num, scope)Add an attribute to the name-to-number cache
add_to_cache
(varname, num)Add a variable to the name-to-number cache
attr_num
(attrname)Get the attribute number and scope by attribute name
checksum
([new_val])Set or check the checksum status of this CDF.
clear_attr_from_cache
(attrname)Mark an attribute deleted in the name-to-number cache
clear_from_cache
(varname)Mark a variable deleted in the name-to-number cache
clone
(zVar[, name, data])Clone a zVariable (from another CDF or this) into this CDF
close
()Closes the CDF file
col_major
([new_col])Finds the majority of this CDF file
compress
([comptype, param])Set or check the compression of this CDF
copy
()Make a copy of all data and attributes in this CDF
from_data
(filename, sd)Create a new CDF file from a SpaceData object or similar
new
(name[, data, type, recVary, dimVarys, ...])Create a new zVariable in this CDF
raw_var
(name)Get a "raw"
Var
object.readonly
([ro])Sets or check the readonly status of this CDF
save
()Saves the CDF file but leaves it open.
var_num
(varname)Get the variable number of a particular variable name
version
()Get version of library that created this CDF
Attributes
- add_attr_to_cache(attrname, num, scope)[source]¶
Add an attribute to the name-to-number cache
This maintains a cache of name-to-number mappings for attributes to keep from having to query the CDF library constantly. It’s mostly an internal function.
- Parameters:
- varnamebytes
name of the zVariable. Not this is NOT a string in Python 3!
- numint
number of the variable
- scopebool
True if global scope; False if variable scope.
- add_to_cache(varname, num)[source]¶
Add a variable to the name-to-number cache
This maintains a cache of name-to-number mappings for zVariables to keep from having to query the CDF library constantly. It’s mostly an internal function.
- Parameters:
- varnamebytes
name of the zVariable. Not this is NOT a string in Python 3!
- numint
number of the variable
- attr_num(attrname)[source]¶
Get the attribute number and scope by attribute name
This maintains a cache of name-to-number mappings for attributes to keep from having to query the CDF library constantly. It’s mostly an internal function.
- Parameters:
- attrnamebytes
name of the attribute. Not this is NOT a string in Python 3!
- Returns:
- outtuple
attribute number, scope (True for global) of this attribute
- Raises:
- CDFErrorif attribute is not found
- checksum(new_val=None)[source]¶
Set or check the checksum status of this CDF. If checksums are enabled, the checksum will be verified every time the file is opened.
- Returns:
- outboolean
True if the checksum is enabled or False if disabled
- Other Parameters:
- new_valboolean
True to enable checksum, False to disable, or leave out to simply check.
- clear_attr_from_cache(attrname)[source]¶
Mark an attribute deleted in the name-to-number cache
Will remove an attribute, and all attributes with higher numbers, from the attribute cache.
Does NOT delete the variable!
This maintains a cache of name-to-number mappings for attributes to keep from having to query the CDF library constantly. It’s mostly an internal function.
- Parameters:
- attrnamebytes
name of the attribute. Not this is NOT a string in Python 3!
- clear_from_cache(varname)[source]¶
Mark a variable deleted in the name-to-number cache
Will remove a variable, and all variables with higher numbers, from the variable cache.
Does NOT delete the variable!
This maintains a cache of name-to-number mappings for zVariables to keep from having to query the CDF library constantly. It’s mostly an internal function.
- Parameters:
- varnamebytes
name of the zVariable. Not this is NOT a string in Python 3!
- clone(zVar, name=None, data=True)[source]¶
Clone a zVariable (from another CDF or this) into this CDF
- close()[source]¶
Closes the CDF file
Although called on object destruction, to ensure all data are saved, the user should explicitly call
close()
orsave()
.- Raises:
- CDFErrorif CDF library reports an error
- Warns:
- CDFWarningif CDF library reports a warning
- col_major(new_col=None)[source]¶
Finds the majority of this CDF file
- Returns:
- outboolean
True if column-major, false if row-major
- Other Parameters:
- new_colboolean
Specify True to change to column-major, False to change to row major, or do not specify to check the majority rather than changing it. (default is check only)
- compress(comptype=None, param=None)[source]¶
Set or check the compression of this CDF
Sets compression on entire file, not per-variable.
See section 2.6 of the CDF user’s guide for more information on compression.
- Returns:
- outtuple
(comptype, param) currently in effect
- Other Parameters:
- comptypectypes.c_long
type of compression to change to, see CDF C reference manual section 4.10. Constants for this parameter are in
const
. If not specified, will not change compression.- paramctypes.c_long
Compression parameter, see CDF CRM 4.10 and
const
. If not specified, will choose reasonable default (5 for gzip; other types have only one possible parameter.)
See also
Examples
- Set file
cdffile
to gzip compression, compression level 9: >>> cdffile.compress(pycdf.const.GZIP_COMPRESSION, 9)
- classmethod from_data(filename, sd)[source]¶
Create a new CDF file from a SpaceData object or similar
The CDF named
filename
is created, opened, filled with the contents ofsd
(including attributes), and closed.sd
should be a dictionary-like object; each key will be made into a variable name. An attribute calledattrs
, if it exists, will be made into global attributes for the CDF.Each value of
sd
should be array-like and will be used as the contents of the variable; an attribute calledattrs
, if it exists, will be made into attributes for that variable.
- new(name, data=None, type=None, recVary=None, dimVarys=None, dims=None, n_elements=None, compress=None, compress_param=None, sparse=None, pad=None)[source]¶
Create a new zVariable in this CDF
Note
Either
data
ortype
must be specified. If type is not specified, it is guessed fromdata
.This creates a new variable. If using a “master CDF” with existing variables and no records, simply assign the new data to the variable, or the “whole variable” slice:
>>> cdf['ExistingVariable'] = data >>> cdf['ExistingVariable'][...] = data
- Parameters:
- namestr
name of the new variable
- Returns:
- out
Var
the newly-created zVariable
- out
- Other Parameters:
- data
data to store in the new variable. If this has a an
attrs
attribute (e.g.,dmarray
), it will be used to populate attributes of the new variable. Similarly the CDF type, record variance, etc. will, by default, be taken fromdata
if it is aVarCopy
. This can be overridden by specifying other keywords.- typectypes.c_long
CDF type of the variable, from
const
. See section 2.5 of the CDF user’s guide for more information on CDF data types.- recVaryboolean
record variance of the variable (default True)
- dimVaryslist of boolean
dimension variance of each dimension, default True for all dimensions.
- dimslist of int
size of each dimension of this variable, default zero-dimensional. Note this is the dimensionality as defined by CDF, i.e., for record-varying variables it excludes the leading record dimension. See
Var
.- n_elementsint
number of elements, should be 1 except for CDF_CHAR, for which it’s the length of the string.
- compressctypes.c_long
Compression to apply to this variable, default None. See
Var.compress()
.- compress_paramctypes.c_long
Compression parameter if compression used; reasonable default is chosen. See
Var.compress()
.- sparsectypes.c_long
Added in version 0.2.3.
Sparse records type for this variable, default None (no sparse records). See
Var.sparse()
.- pad
Added in version 0.2.3.
Pad value for this variable, default None (do not set). See
Var.pad()
.
- Raises:
- ValueErrorif neither data nor sufficient typing information
is provided.
Notes
Any given data may be representable by a range of CDF types; if the type is not specified, pycdf will guess which the CDF types which can represent this data. This breaks down to:
If input data is a numpy array, match the type of that array
Proper kind (numerical, string, time)
Proper range (stores highest and lowest number provided)
Sufficient resolution (EPOCH16 or TIME_TT2000 required if datetime has microseconds or below.)
If more than one value satisfies the requirements, types are returned in preferred order:
Type that matches precision of data first, then
integer type before float type, then
Smallest type first, then
signed type first, then
specifically-named (CDF_BYTE) vs. generically named (CDF_INT1)
TIME_TT2000 is always the preferred time type if it is available. Otherwise, EPOCH_16 is preferred over EPOCH if
data
specifies below the millisecond level (rule 1), but otherwise EPOCH is preferred (rule 2).Changed in version 0.3.0: Before 0.3.0, EPOCH or EPOCH_16 were used if not specified. Now TIME_TT2000 is always the preferred type.
For floats, four-byte is preferred unless eight-byte is required:
absolute values between 0 and 3e-39
absolute values greater than 1.7e38
This will switch to an eight-byte double in some cases where four bytes would be sufficient for IEEE 754 encoding, but where DEC formats would require eight.
- raw_var(name)[source]¶
Get a “raw”
Var
object.Normally a
Var
will perform translation of values for certain types (to/from Unicode for CHAR variables on Py3k, and to/from datetime for all time types). A “raw” object does not perform this translation, on read or write.This does not affect the data on disk, and in fact it is possible to maintain multiple Python objects with access to the same zVariable.
- Parameters:
- namestr
name or number of the zVariable
- readonly(ro=None)[source]¶
Sets or check the readonly status of this CDF
If the CDF has been changed since opening, setting readonly mode will have no effect.
Note
Before version 3.8.1 of the NASA CDF library, closing a CDF that has been opened readonly, or setting readonly False, may take a substantial amount of time if there are many variables in the CDF, as a (potentially large) cache needs to be cleared. If upgrading to a newer CDF library is not possible, specifying
readonly=False
when opening the file is an option. However, this may make some reading operations slower.- Returns:
- outBoolean
True if CDF is read-only, else False
- Other Parameters:
- roBoolean
True to set the CDF readonly, False to set it read/write, or leave out to check only.
- Raises:
- CDFErrorif bad mode is set
- save()[source]¶
Saves the CDF file but leaves it open.
If closing the CDF,
close()
is sufficient; there is no need to callsave()
beforeclose()
.Note
Relies on an undocumented call of the CDF C library, which is also used in the Java interface.
- Raises:
- CDFErrorif CDF library reports an error
- Warns:
- CDFWarningif CDF library reports a warning
- var_num(varname)[source]¶
Get the variable number of a particular variable name
This maintains a cache of name-to-number mappings for zVariables to keep from having to query the CDF library constantly. It’s mostly an internal function.
- Parameters:
- varnamebytes
name of the zVariable. Not this is NOT a string in Python 3!
- Returns:
- outint
Variable number of this zvariable.
- Raises:
- CDFErrorif variable is not found
- version()[source]¶
Get version of library that created this CDF
- Returns:
- outtuple
version of CDF library, in form (version, release, increment)
- backward = False¶
True if this CDF was created in backward-compatible mode.