Read CDF files in Python via SpacePy

Note: The newer, pure Python + Numpy cdflib can be 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.

Install

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


We suggest convenient setup_spacepy.py SpacePy install script. Otherwise, the manual procedure to install SpacePy is:

  1. install prereqs

    conda install numpy scipy h5py matplotlib networkx
    • Linux:

      apt install make gcc gfortran libncurses-dev
    • MacOS:

      brew install gcc ncurses
  2. Download CDF source and extract

    tar xf cdf*-dist-all.tar.gz -C ~
    cd ~/cdf*dist
  3. 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
  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 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