Read CDF files in Python via SpacePy

For CDF file read / write, consider pure Python + Numpy cdflib as cdflib is OS-agnostic, easy to install and performant.


SpacePy can read / write Common Data Format .cdf files in Python, among other space science tasks. The .cdf file format is totally different from .nc NetCDF files, which are essentially special HDF5 files. In general if you’re just doing CDF file I/O consider cdflib instead of SpacePy. SpacePy isn’t well tested on Windows.

Install

Note: The procedure below is for Mac and Linux. Windows users should consider cdflib to read / write CDF files.

Install SpacePy prereqs

conda install numpy scipy h5py matplotlib networkx
  • Linux: apt install make gcc gfortran libncurses-dev
  • MacOS: brew install gcc ncurses

Download CDF source and extract

tar xf cdf*-dist-all.tar.gz -C ~
cd ~/cdf*dist

Compile CDF:

  • Linux
   make OS=linux ENV=gnu CURSES=yes FORTRAN=no UCOPTIONS=-O2 SHARED=yes -j4 all
   make install #no sudo
  • Mac
   make OS=macosx ENV=gnu CURSES=yes FORTRAN=no UCOPTIONS=-O2 SHARED=yes -j4 all
   make install #no sudo

CDF environment setup: assuming CDF install to ~/cdf-dist, add to ~/.bashrc

. $HOME/cdf*dist/bin/definitions.B

Complete SpacePy install:

python -m pip install spacepy

Read CDF with SpacePy on Python

Use a test CDF file with test program readTHEMIS.py.

Notes

Fix Error

spacepy.pycdf.CDFError: NO_SUCH_CDF: The specified CDF does not exist.

CDF_BASE and CDF_LIB must be defined in ~/.bashrc (Step #4)

Read NetCDF file *.nc

NetCDF is an HDF5 subset, read/write in Python with:

conda install netcdf4