Purpose
pynci is a Python wrapper for your
netcdf.h file and gives you access to the C netCDF interface from the Python prompt.
Limitation
pynci provides full access to netCDF but without any Python support. It is meant to be distributed with and used by other Python packages that use netCDF I/O. If you intend to do a lot of interactive netCDF I/O, you are better off with a more sophisticated package. However, if you are familiar with the C netCDF interface you will get started with pynci right away.
Description
The pynci package is a pythonized version of the netCDF interface. The interface is used in much the same way as the C version, namely it accepts C pointers. pynci comes with the
cPtr package that lets you create the C pointers.
Here is a simple example that dumps an
array to a netCDF file
foo.nc using pynci and cPtr.
>>> # import cPtr and pynci
>>> from cPtr.pointer import pointer
>>> from pynci.pyncilib import *
>>>
>>> # create netCDF file
>>> ncid = pointer(1, type='int')
>>> ierr = nc_create('foo.nc', NC_CLOBBER, ncid.ptr)
>>>
>>> # define netCDF dimensions
>>> dimid = pointer(1, type='int')
>>> ierr = nc_def_dim(ncid, 'dim', len(array.flat), dimid.ptr)
>>>
>>> # define netCDF variables
>>> varid = pointer(1, type='int')
>>> ierr = nc_def_var(ncid, 'var', NC_DOUBLE, dimid.len, dimid.ptr, varid.ptr)
>>>
>>> # leave define mode
>>> ierr = nc_enddef(ncid)
>>>
>>> # dump array to netCDF file
>>> vector = pointer(len(array.flat), type='double')
>>> vector[:] = array.flat
>>> ierr = nc_put_var_double(ncid, varid, vector.ptr)
>>>
>>> # close netCDF file
>>> ierr = nc_close(ncid)
Installation
To build pynci a
C compiler, a
netCDF installation and
SWIG is required.
Download the latest version and unpack it
> gzip -d pynci-0.4.tar.gz
> tar xf pynci-0.4.tar
To install the package go to the pynci directory and run the setup script.
> cd pynci-0.4
> python setup.py install
If your
netcdf.h file is in a standard location this should install the pynci package in the default path. If SWIG doesn't find the netCDF header file or if you want to use a specific one you can tell the setup script where it is using the
--include-dirs or
-I switch with the build_ext command.
> python setup.py build_ext --include-dirs=/usr/local/netcdf/include install
To install pynci in your home directory under
lib/python?.?/site-packages, use the
--prefix or
--home switch with the install command.
> python setup.py install --prefix=~
If you are using a Python version older than 2.1 the build_ext command of the setup process is likely to fail. Here are the necessary steps to proceed anyway.
- copy your
netcdf.h file into the src directory
- comment out the following line in the
setup.py file
from dist_with_swig import Distribution_with_swig
- run
python setup.py build
- copy
src/pynci.py to build/lib.my-system-my-python/pynci
- run
python setup.py install --prefix=~
This page will be installed under the prefix directory in
share/doc/pynci. pynci includes the
cPtr package which is installed along with pynci.
Documentation
There is no documentation for pynci, since it is used in much the same way as the C netCDF interface. You can get the netCDf documentation from
http://www.unidata.ucar.edu/software/netcdf/.
Known Bugs
pynci versions prior to 0.4 don't work as advertised with SWIG versions greater than 1.3.21. To make older pynci versions work with newer SWIG versions, the pointers returned by cPtr have to be passed as integers (or whatever is appropriate) to the netCDF interface, i.e. use
>>> ierr = nc_def_dim(int(ncid), 'dim', len(array.flat), dimid.ptr)
instead of
>>> ierr = nc_def_dim(ncid, 'dim', len(array.flat), dimid.ptr)
See details.
License
pynci - Python netCDF interface
Copyright (C) 2004 Christian Dieterich
This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA