How to read general data format (CDF) in Python

I need to read a CDF file using Python. I found libraries, but I did not understand how to use them. For example, in this ( Python lib ) I need to download CDF lib, I donโ€™t know where to download. There is a download page for CDF, but it is not related to this library.

+6
source share
4 answers

The answer from @miraculixx is correct, but it is assumed that you have already installed the CDF C Library .

Itโ€™s easy to follow the guide here if you donโ€™t even know what format the CDF file was before you found this question in SO.

1. Download the latest CDF C library:

You can find the latest stable version on this link . Take the source code with 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 way, 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 there are several dependencies in the CDF library (as @Michal Dyzma points out). You can install them all with 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 the README.install file, which contains a lot more details in this step than I provided. Two cents is that you want to check which compilation variables are necessary / optional for your system and needs.

 make all.help 

I will build a Linux distribution using the GNU C compiler. I am not interested in the FORTRAN interface, and my operating system supports shared libraries. I want to install Curses-based tools that allow you to use interactive command-line CDF tools (why we installed the 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 

The installation should run smoothly and add all the files to the ./bin , ./include and ./lib .

4. Set the environment variables:

A definitions.B file must be specified in ./bin , which will do this automatically for you, make it executable with chmod+x and add the following line to ~/.bashrc ( Note: 1) I assume that you installed the library along the path $HOME/Libraries/ ; 2) After . ) there is a space:

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

IMPORTANT NOTE: In the above file, there is an error in line line 68 , instead of adding to the environment variable LD_LIBRARY_PATH it overwrites it. Easy to fix, replace line line 68 as follows:

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

If for some reason definitions.B does not exist, 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 are 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) 
+5
source

If you have the Python tool package pip installed, you can get the capspy cdf library as follows:

 $ pip install git+https://github.com/spacepy/spacepy.git 

Note that this will install many dependencies, including numpy and scipy. They can be a little harder to install from scratch. First you can install the finished package, for example. anaconda . Once this is done, just use the command above, and spacepy should be installed as a breeze.

Once the installation of spacepy was successful, in accordance with this example, it should work something like this:

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

So far I have had the same problem. I suppose you are running Windows ...

According to the Spacepy documentation, you need several dependencies to use your cdf module.

The first of all that SpacePy officially supports only the 32-bit version of python, so you must have python in 323bit.

Second , it requires the NASF CDF library installed on your system (also a 32-bit version). You can grab it from here .

The third works with Spacepy dependencies:

  • Numpy
  • Scipy
  • Matplotlib
  • h5py
  • Networkx
  • ffnet

Most of them are part of the Anaconda package. If it is not, and you just need to install them pip install <package name> .

If you have trouble compiling from sources, I suggest going to the Christoph Gohlke website and grab the pre-created binaries for Windows that match your version of python. http://www.lfd.uci.edu/~gohlke/pythonlibs/

This should lead you to use the Spacepy CDF module.

You can also try a different approach. Download the CDF-to-netCDF converter from the NASA page and run it in your CDF file.

Python has a nice netCDF module that can be installed from GitHub or python repo. In this case, you also need several dependencies, such as HDF5, netCDF-4, numpy, cython.

Once you have the netCDF file, you can access it with the netCDF module or scipy.io module.

+3
source

Maven CDFLib is a pure alternative to Python. https://github.com/MAVENSDC/cdflib

pip install cdflib

In your code:

 import cdflib cdf_file = cdflib.CDF('/path/to/cdf_file.cdf') 
0
source

All Articles