1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
A netCDF interface for Python
=============================
To use this module, you need:
- the Python interpreter, version 1.4 or higher
(see http://www.python.org)
- the numerics extension (NumPy)
(see http://www.sls.lcs.mit.edu/jjh/numpy/)
- the netCDF library, version 2.4 or later
(see http://www.unidata.ucar.edu/packages/netcdf/)
Installation (for Unix systems):
1) For Python versions before 1.5:
Copy the file Misc/Makefile.pre.in from the Python distribution to
this directory.
2) Type
python compile.py
3) Type
python install.py
For the last step you might need root permissions.
The netCDF interface has been compiled successfully under Windows 95,
but since I am not a Windows user myself, I can't give any
instructions. It should be compiled and installed just like any other
Python extension module.
This module is still in development, and new versions appear from time
to time. Check http://starship.skyport.net/crew/hinsen/netcdf.html for
updates. As usual, this module comes with ABSOLUTELY NO WARRANTY. If
it sends your hard drive into outer space, I'd be interested to hear
about it, but I don't accept any responsibility. See the file COPYRIGHT
for all legal details.
I welcome bug reports and general comments about this interface.
Konrad Hinsen <hinsen@ibs.ibs.fr>
Common installation problems
----------------------------
If the compiler complains about a missing file "arrayobject.h", make
sure that this file (which is part of NumPy) is in a directory searched
by the compiler (the best place is with the other Python include files,
i.e. /usr/local/include/python1.4 for most installations).
If "import netcdf" leads to error messages about missing or unresolved
symbols, add "-lnumpy" at the end of the last two lines in the file
Setup and recompile the netCDF module. Whether this must be done depends
on the linker and on the method used to install NumPy.
Access from Python programs
---------------------------
Prerequisites:
from netcdf import *
from Numeric import *
Opening an existing file for reading:
file = NetCDFFile('test.nc', 'r')
Opening an existing file for reading and writing:
file = NetCDFFile('test.nc', 'r+')
Opening a possibly existing file for reading and writing:
file = NetCDFFile('test.nc', 'a')
Creating a new file open for reading and writing:
file = NetCDFFile('test.nc', 'w', 'created today by me')
(The optional third argument gets added to the file attribute "history".)
Setting and accessing file attributes:
file.title = "And now for something completely different"
print file.title
Creating and accessing dimensions:
file.createDimension('n', 20)
file.createDimension('t', None) # unlimited dimension
print file.dimensions
Creating variables:
foo = file.createVariable('foo', Float, ('n', 't'))
Accessing existing variables:
print file.variables
foo = file.variables['foo']
Finding the current size of a variable:
print foo.shape
Finding the dimensions of a variable:
print foo.dimensions
Setting and accessing variable attributes:
foo.units = "kilograms of spam"
print foo.units
Reading from a variable (returns number or array):
print foo[0, 0]
a = foo[:,1]
the_whole_array = foo.getValue()
Writing to a variable:
foo[0,:] = [42., 42., 42.]
foo[:,1] = 1.
foo.assignValue(2.*foo.getValue())
Closing a file:
file.close()
Access from C programs
----------------------
To be documented Real Soon Now. Look at demomodule.c for an example.
Comments
--------
1) The unlimited dimension.
netCDF files can grow along one dimension, which is called the
"unlimited" dimension in the netCDF manual. In Python its size is
specified and returned as None (in the Python interface) or 0 (in the
C interface). To find the current value of the unlimited dimension,
get the shape of any variable that uses it.
If a file open for reading is simultaneously written by another program,
the size of the unlimited dimension may change. Every time the shape
of a variable is requested, the current size will be obtained from
the file. For reading and writing, the size obtained during the last
shape request is used. This ensures consistency: foo[-1] means the
same thing no matter how often it is evaluated, as long as the shape
is not re-read in between.
2) Efficiency considerations.
When new dimensions, variables, or attributes have been created, the
following data access (read or write) will essentially cause the whole
file to be copied to a new one, with a larger header containing the
new data. Therefore these operations should not be mixed at random.
3) Attribute conventions.
There are a few conventions about the use of attributes in netCDF
files. See the netCDF manual for a list.
Machine-specific notes
======================
Irix 6.2/6.3
------------
With Python 1.4 under Irix 6.2 and 6.3, the Python interpreter lacks
the code for handling CObjects, which are used to provide C-level
access to the netCDF module. As a consequence, importing the netCDF
module fails and warnings about unresolved symbols are printed.
To fix this problem, replace the file "configure" in the Python
distribution by the file "configure.IRIX6" from this archive
and reinstall the Python interpreter from scratch.
|