15

我正在用 R 编写一个函数来从 netCDF 文件中提取一些空气质量建模数据。我安装了包“ncdf”。

为了允许其他用户或我自己选择从 netCDF 文件中提取哪些变量,我想提取文件中所有变量的名称,以便我可以在一个简单的列表中呈现,而不仅仅是print.ncdf()给出的文件很多信息。有什么办法吗?

我尝试了 ncdf 对象unlist()var字段,但它似乎也返回了内容......

我用谷歌搜索并搜索了 stack* overflow * 但似乎没有找到答案,因此非常感谢您的帮助。

提前谢谢了。

4

2 回答 2

25

如果您的 ncdf 对象被调用nc,那么很简单:

names(nc$var)

举个例子,例如使用在这里下载的数据集(因为您没有提供):

nc <- open.ncdf("20130128-ABOM-L4HRfnd-AUS-v01-fv01_0-RAMSSA_09km.nc")
names(nc$var)
[1] "analysed_sst"     "analysis_error"   "sea_ice_fraction" "mask"   
于 2013-01-29T15:47:19.580 回答
8

现在是 2016 年。 ncdf 包已弃用。与 SE 用户 plannapus 的答案相同的代码现在是:

library(ncdf4)
netcdf.file <- "flux.nc"
nc = ncdf4::nc_open(netcdf.file)
variables = names(nc[['var']])
#print(nc)

文档中的注释:

Package: ncdf
Title: Interface to Unidata netCDF Data Files
Maintainer: Brian Ripley <ripley@stats.ox.ac.uk>
Version: 1.6.9
Author: David Pierce <dpierce@ucsd.edu>
Description: This is deprecated and will be removed
   from CRAN in early 2016: use 'RNetCDF' or 'ncdf4' instead.

Newer package "ncdf4" is designed to work with the netcdf library 
version 4, and supports features such as compression and 
chunking.Unfortunately, for various reasons the ncdf4 package must have
a different API than the ncdf package.

维护者主页的注释:

Package ncdf4 -- use this for new code

The "ncdf4" package is designed to work with the netcdf library, version 4. 
It includes the ability to use compression and chunking, 
which seem to be some of the most anticipated benefits of the version 4 
library. Note that the API of ncdf4 has to be different 
from the API of ncdf, unfortunately. New code should use ncdf4, not ncdf. 

http://cirrus.ucsd.edu/~pierce/ncdf/

于 2016-02-08T14:51:36.390 回答