Read CDF files in Python via SpacePy

SpacePy is useful for read/write Common Data Format .cdf files in Python, among other space science tasks. Here’s how to install SpacePy on Linux, Mac and Windows.

The .cdf file format is totally different from .nc NetCDF files, which are essentially special HDF5 files.

SpacePy Install Procedure

  1. download and extract SpacePy source

     wget -r -l1 -np -nd -nc -A cdf*-dist-all.tar.gz
     tar xf cdf*-dist-all.tar.gz -C ~
     cd ~/cdf*dist
  2. install Python prereqs (using conda or pip)

     conda install numpy scipy h5py matplotlib networkx
  3. Compile SpacePy. This step is distinct for Mac and Linux/Windows Subsystem for Linux.

Linux / Windows Subsystem for Linux

apt install build-essential gfortran ncurses-dev

make OS=linux ENV=gnu CURSES=yes FORTRAN=no UCOPTIONS=-O2 SHARED=yes -j4 all

make install #no sudo

Mac OS X

brew install gcc ncurses

make OS=macosx ENV=gnu CURSES=yes FORTRAN=no UCOPTIONS=-O2 SHARED=yes -j4 all

make install #no sudo

4. Complete SpacePy Path setup

suppose you installed to ~/cdf-dist. Add to your ~/.bashrc

export CDF_BASE=$HOME/cdf-dist
export CDF_INC=$CDF_BASE/include
export CDF_LIB=$CDF_BASE/lib

Then do

pip install spacepy

Read CDF with SpacePy on Python

use a test CDF file


with test program

If SpacePy Error

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

You need CDF_BASE and CDF_LIB in your ~/.bashrc

If you instead have a NetCDF file (*.nc)

conda install netcdf4

wget notes

For those curious about wget, the options used here are:

-np don’t go up in hierarchy (no parents)

-nd no directory structure on download (put all files in one directory commanded by -P)

-nc dint overwrite existing files on hard drive

-r recurse but -l1 don’t go below specified directory

-A accept files matching globbed pattern