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. The .cdf file format is totally different from .nc NetCDF files, which are essentially special HDF5 files.

SpacePy Install Procedure

  1. install prereqs

     conda install numpy scipy h5py matplotlib networkx
    

    Linux

      apt install make gcc gfortran libncurses-dev
    

    Mac

      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:
    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

Leave a Comment