Read CDF files in Python via SpacePy

1 minute read

Note: The newer, pure Python + Numpy cdflib is a better choice for reading CDF than complicated SpacePy.

SpacePy is useful for 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.


Note: The procedure below is automated by for Mac and Linux. SpacePy isn’t well tested on Windows. If you need SpacePy on Windows, consider Windows Subsystem for Linux.

I suggest using the convenient SpacePy install script. Otherwise, the manual procedure to install SpacePy is:

  1. install prereqs
    conda install numpy scipy h5py matplotlib networkx


    apt install make gcc gfortran libncurses-dev


    brew install gcc ncurses
  2. Download CDF source and extract
    tar xf cdf*-dist-all.tar.gz -C ~
    cd ~/cdf*dist
  3. Compile CDF:


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


    make OS=macosx ENV=gnu CURSES=yes FORTRAN=no UCOPTIONS=-O2 SHARED=yes -j4 all
    make install #no sudo
  4. CDF environment setup: assuming CDF install to ~/cdf-dist, add to ~/.bashrc
    . $HOME/cdf*dist/bin/definitions.B
  5. Complete SpacePy install:
    python -m pip install spacepy

Read CDF with SpacePy on Python

Use a test CDF file with test program


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

Leave a comment