Search code examples
pythoniodataformatcommon-data-format

How read Common Data Format (CDF) in Python


I need to read CDF file with using python. I have found libraries but i did not understand how use it. For example at this(Python lib), i need to download CDF lib, i don't know where to download. There is download page for CDF but it seems irrelevant with this library.


Solution

  • The answer by @miraculixx is correct, but it assumes that you have already installed the CDF C Library.

    Here's an easy to follow guide if you didn't even know what the CDF file format was before you found this question on SO.

    1. Download the latest version of the CDF C Library:

    You can find the latest stable release at this link. Grab the source code using wget, and extract it. Note: the following will create a directory in the current folder ./ if you want to download the code in a different path make sure you change the code below.

    wget -r -l1 -np -nd -nc http://cdaweb.gsfc.nasa.gov/pub/software/cdf/dist/latest-release/linux/ -A cdf*-dist-all.tar.gz
    tar xf cdf*-dist-all.tar.gz -C ./
    cd cdf*dist
    

    2. Install all the dependencies:

    SpacePy and the CDF Library have several dependencies (as pointed out by @Michal Dyzma). You can install them all using conda or pip, and apt.

    pip install numpy scipy h5py matplotlib networkx
    apt install build-essential gfortran libncurses5-dev
    

    3. Compile the C Library:

    You should have downloaded a README.install file that contains a lot more details on this step than I'll provide. The two cents are that you want to check which compile variables are required/optional for your system and needs.

    make all.help
    

    I will be building the distribution for Linux using the GNU C compiler. I am not interested in the FORTRAN interface, and my operating system supports shareable libraries. I want to install the Curses-based toolkit programs that allow to use the command-line based interactive CDF tools (that's why we installed libncurses5-dev dependency in step 2). As a result this is the final make command:

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

    Installation should run smooth and add all the files in the ./bin, ./include, and ./lib sub-directories.

    4. Set the environment variables:

    There should be a file in ./bin called definitions.B that does this automatically for you, make it executable with chmod+x and add the following line to your ~/.bashrc (Note: 1) I'm assuming you installed the library at the path $HOME/Libraries/; 2) There is a space after the .):

    . $HOME/Libraries/cdf/cdf36_3-dist/bin/definitions.B
    

    IMPORTANT NOTE: The file above has a bug at line 68, instead of appending to environment variable LD_LIBRARY_PATH it overwrites it. The fix is easy, replace line 68 with the following:

    export LD_LIBRARY_PATH=$HOME/Libraries/cdf/cdf36_3-dist/lib:$LD_LIBRARY_PATH
    

    If for some reason definitions.B is not there, simply add the following:

    export CDF_BASE=$HOME/Libraries/cdf/cdf36_3-dist
    export CDF_INC=$CDF_BASE/include
    export CDF_LIB=$CDF_BASE/lib
    export CDF_BIN=$CDF_BASE/bin
    export LD_LIBRARY_PATH=$CDF_BASE/lib:$LD_LIBRARY_PATH
    

    5. You're all set, go and do good:

    Assuming you installed spacepy with pip the following should work out of the box:

    from spacepy import pycdf
    cdf = pycdf.CDF('/path/to/file.cdf')
    print(cdf)