NetCDFShelf
NetCDFShelf.tar.gz
NetCDFShelf.py.html

Scientific.IO.NetCDF-like wrapper for python shelve.
 
I wrote this module because;
a) I wanted to use the familiar ScientificPython NetCDF interface 
   for I/O of Numeric arrays even where NetCDF was not installed, and
b) I was frustrated with the lack of support for built-in compression in
   the NetCDF library. 
   
This module uses bzip2 compression to create files that are
often more than 3 times smaller than true NetCDF files.
 
Shelve archives created with this module can easily be converted
to and from true NetCDF files using the shltonc and nctoshl class methods.
 
Differences with Scientific.IO.NetCDF:
 
1) data must be assigned to NetCDFVariable object along first
   dimension only. For example,
   x[0] = array[0,:,:] or x[0,:,:] = array[0,:,:] is OK 
   (where x is a NetCDFVariable object), but x[0,1,:] = array[0,1,:]
   will raise an exception (IndexError).
   However, data can be retrieved from a NetCDFVariable objects by slicing
   along any dimension.
2) only the first dimension can be unlimited.
3) Data is archived in a directory, with each variable
   in a separate file.  Global variable and attribute information
   is also stored in a separate file.
3) bzip2 compression is used for multi-dimensional variables
   (files are much smaller than netCDF, usually by a factor of 2-3)
 
Example code is in the 'examples' directory of the source distribution.
 
Requires Python >= 2.3 with bsddb and bz2 standard library modules.
 
Jeffrey Whitaker <jeffrey.s.whitaker@noaa.gov>
 
Version: 20050822

 
Modules
       
Scientific.IO.NetCDF
Numeric
bsddb
bz2
math
os
shelve
sys

 
Classes
       
NetCDFFile
NetCDFVariable

 
class NetCDFFile
    netCDF file Constructor: NetCDFFile(filename, mode="r",history=None)
 
Arguments:
 
filename -- Name of directory to hold data archive.
            Separate shelve files are created in this directory for
            each variable (plus one containing global attributes
            and variable info).
 
mode -- access mode. "r" means read-only; no data can be modified.
        "w" means write; a new file is created, an existing
        file with the same name is deleted. "a" means append
        (in analogy with serial files); an existing file is
        opened for reading and writing.
 
history -- a string that is used to define the global NetCDF
attribute 'history'.
 
NetCDFFile object has two standard attributes: 'dimensions' and
'variables'. The values of both are dictionaries, mapping dimension
names to their associated lengths and variable names to variables,
respectively. Application programs should never modify these
dictionaries.
 
All other attributes correspond to global attributes defined in the
netCDF file. Global file attributes are created by assigning to
an attribute of the NetCDFFile object.
 
  Methods defined here:
__getattr__(self, name)
__init__(self, dirname, mode='r', history=None)
__repr__(self)
produces output similar to 'ncdump -h'
__setattr__(self, name, value)
close(self)
Closes the file. Any read or write access to the file
or one of its variables after closing raises an exception.
createDimension(self, dimname, size)
Creates a new dimension with the given "dimname" and
"size". "size" must be a positive integer or 'None',
which stands for the unlimited dimension. There can
be only one unlimited dimension per dataset.
createVariable(self, varname, datatype, dimensions, least_significant_digit=None)
Creates a new variable with the given "varname", "datatype", and
"dimensions". The "datatype" is a one-letter string with the same
meaning as the typecodes for arrays in module Numeric; in
practice the predefined type constants from Numeric should
be used. "dimensions" must be a tuple containing dimension
names (strings) that have been defined previously.
An unlimited dimension must be the first (leftmost)
dimension of the variable.
 
If the optional keyword parameter 'least_significant_digit' is
specified, multidimensional variables will be truncated (quantized).
This can significantly improve compression.  For example, if 
least_significant_digit=1, data will be quantized using
Numeric.around(scale*data)/scale, where scale = 2**bits, and
bits is determined so that a precision of 0.1 is retained (in 
this case bits=4). 
From http://www.cdc.noaa.gov/cdc/conventions/cdc_netcdf_standard.shtml:
"least_significant_digit -- power of ten of the smallest decimal
place in unpacked data that is a reliable value."
 
The return value is the NetCDFVariable object describing the
new variable.
nctoshl(self, filename, unpackshort=True)
convert a true netcdf file (filename) to a NetCDFShelf.NetCDFFile.
Requires Scientific.IO.NetCDF module. If unpackshort=True, variables
stored as short integers with a scale and offset are unpacked to floating
point variables in the netCDF-shelve archive.  If the least_significant_digit
attribute is set, the data is quantized to improve compression.
shltonc(self, filename, packshort=False, scale_factor=None, add_offset=None)
convert NetCDFShelf.NetCDFFile to a true netcdf file (filename).
Requires Scientific.IO.NetCDF module. If packshort=True, 
variables are packed as short integers using the dictionaries
scale_factor and add_offset. The dictionary keys are the 
the variable names in the shelve archive to be packed as short
integers.
sync(self)
Writes all buffered data to the disk file.

 
class NetCDFVariable
    Variable in a netCDF file
 
NetCDFVariable objects are constructed by calling the method
'createVariable' on the NetCDFFile object.
 
NetCDFVariable objects behave much like array objects defined
in module Numeric, except that their data resides in a file.
Data is read by indexing and written by assigning to an
indexed subset; the entire array can be accessed by the index
'[:]'. Unlike Numeric arrays (and Scientific.IO.NetCDFVariable
objects), data can be assigned to NetCDFVariable objects only by 
indexing along the first (leftmost) dimension. Data may be
retrieved from a NetCDFVariable object by slicing along any
dimension.  Only 'extended slicing' of the the form i:j:k is 
supported, not fully general Numeric array slicing (for example,
a[::-1] won't return the contents of the variable a reversed).
 
Multidimensional variables are compressed on disk using bz2 compression,
after optionally truncating to a precision specified by the
least_significant_digit keyword argument to createVariable.
Truncation will signficantly improve compression.
 
NetCDFVariable objects also have attribute
"shape" with the same meaning as for arrays, but the shape
cannot be modified. There is another read-only attribute
"dimensions", whose value is the tuple of dimension names.
 
All other attributes correspond to variable attributes defined in the
netCDF file. Variable attributes are created by assigning to
an attribute of the NetCDFVariable object.
 
  Methods defined here:
__getattr__(self, name)
__getitem__(self, key)
__init__(self, varname, shelf, datatype, dimensions, least_significant_digit=None)
__len__(self)
__setattr__(self, name, value)
__setitem__(self, key, data)
append(self, data)
append data along unlimited dimension.
typecode(self)
Return the variable's type code (a string).

 
Data
        hasnetcdf = True