Saturday 18 May 2013

Using python to access netcdf files

I needed to write out some arrays in a binary format from a c program. I decided to use the netcdf libraries to store the files in a more structured format.

I also wanted to use python to do dome additional analysis. So I need a way to read netcdf files from the python scripting language. There are a number of different python libraries which can read netcdf files. After some exploration I decided to use Module netCDF4.

There are some good instructions on how to install the netcdf4 python module on Ubuntu.

First the hdf5 library needs to be installed
 wget http://www.hdfgroup.org/ftp/HDF5/current/src/hdf5-1.8.11.tar.gz

 gunzip  hdf5-1.8.11.tar.gz 
 tar xvf hdf5-1.8.11.tar 
 cd hdf5-1.8.11/

 ./configure --prefix=/usr/local --enable-shared --enable-hl
 make
 sudo make install


Next the netcdf library needs to be installed.

 wget ftp://ftp.unidata.ucar.edu/pub/netcdf/netcdf-4.3.0.tar.gz
 gunzip  netcdf-4.3.0.tar.gz
 tar xvf netcdf-4.3.0.tar 
 
 cd netcdf-4.3.0/
 LDFLAGS="-L/usr/local/lib CPPFLAGS=-I/usr/local/include"
 ./configure --enable-netcdf-4 --enable-dap --enable-shared --prefix=/usr/local
 make
 sudo make install

Now finally we can install the python library. However first I needed to install a python library to compile against c code.
sudo apt-get install python-dev
sudo ldconfig
Now we need to install the python library.
 wget https://netcdf4-python.googlecode.com/files/netCDF4-1.0.4.tar.gz

gunzip netCDF4-1.0.4.tar.gz

tar xvf netCDF4-1.0.4.tar
cd netCDF4-1.0.4/

cp setup.cfg.template  setup.cfg

Then I needed to edit setup.cfg
python setup.py build
sudo python setup.py install

No comments:

Post a Comment