如何在Python中读取通用数据格式(CDF) [英] How read Common Data Format(CDF) in Python
问题描述
我需要使用python读取CDF文件。我找到了库,但我不明白如何使用它。例如,在此( pythong lib ),我需要下载CDF lib,我不要知道在哪里下载。有CDF的下载页面,但它似乎与这个库无关。
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(pythong 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.
推荐答案
回答 @miraculixx是正确的,但它假设您已经安装了 CDF C库。
The answer by @miraculixx is correct, but it assumes that you have already installed the CDF C Library.
如果您在SO上发现此问题之前甚至不知道CDF文件格式是什么,这是一个易于理解的指南。
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。下载最新版本的CDF C库:
您可以在此处找到最新的稳定版本链接。使用 wget
获取源代码,然后解压缩。 注意:以下内容将在当前文件夹 ./
中创建一个目录,如果您想以不同的路径下载代码,请确保更改代码如下。
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。安装所有依赖项:
SpacePy 和CDF库有几个依赖关系(正如@Michal Dyzma所指出的那样)。您可以使用 conda 或 pip 和 apt 。
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。编译C库:
您应该已经下载了包含很多的 README.install
文件这个步骤的更多细节比我提供的更多。两美分是你要检查哪些编译变量是你的系统和需要的必需/可选。
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
我将使用GNU C编译器构建Linux发行版。我对FORTRAN接口不感兴趣,我的操作系统支持可共享库。我想安装基于Curses的工具包程序,允许使用基于命令行的交互式CDF工具(这就是为什么我们在步骤2中安装了 libncurses5-dev
依赖项)。因此,这是最终的make命令:
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
安装应该顺利进行添加 ./ bin
, ./ include
和 ./ lib中的所有文件
子目录。
Installation should run smooth and add all the files in the ./bin
, ./include
, and ./lib
sub-directories.
4。设置环境变量:
./ bin
中应该有一个名为<$ c的文件$ c> definitions.B 为您自动执行此操作,使其可执行 chmod + x
并将以下行添加到〜/ .bashrc
(注意: 1)我假设您在路径 $ HOME / Libraries / 中安装了库code>; 2)
之后有一个空格。
):
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
重要提示:
上面的文件在第68行有一个错误,而不是附加到环境变量 LD_LIBRARY_PATH
它会覆盖它。修复很简单,用以下内容替换第68行:
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
如果由于某种原因 definitions.B
不存在,只需添加以下内容:
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。你已经准备好了,去做好事:
假设你用pip安装 spacepy
以下应该开箱即用:
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)
这篇关于如何在Python中读取通用数据格式(CDF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!