问题描述
我需要使用python读取CDF文件。我已经找到了库,但是我不知道如何使用它。例如,在此(),我需要下载CDF库,我没有不知道在哪里下载。有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(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.
推荐答案
。
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。安装所有依赖项:
和CDF库具有多个依赖项(如@Michal Dyzma所指出)。您可以使用或和
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 / ; 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
如果出于某些原因 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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!