如何从R中的netCDF文件中提取变量名?
我正在R中编写一个函数,从netCDF文件中提取一些空气质量建模数据。我已经安装了“ncdf”软件包 为了允许其他用户或我自己选择从netCDF文件中提取哪些变量,我想提取文件中所有变量的名称,这样我就可以在一个简单的列表中显示,而不仅仅是如何从R中的netCDF文件中提取变量名?,r,netcdf,variable-names,R,Netcdf,Variable Names,我正在R中编写一个函数,从netCDF文件中提取一些空气质量建模数据。我已经安装了“ncdf”软件包 为了允许其他用户或我自己选择从netCDF文件中提取哪些变量,我想提取文件中所有变量的名称,这样我就可以在一个简单的列表中显示,而不仅仅是print.ncdf()。有什么办法吗 我尝试了unlist()到ncdf对象的var字段,但它似乎也返回了内容 我在谷歌上搜索了stack*overflow*,但似乎没有找到答案,所以非常感谢您的帮助 非常感谢。如果您的ncdf对象被称为nc,那么非常简单:
print.ncdf()。有什么办法吗
我尝试了unlist()
到ncdf对象的var
字段,但它似乎也返回了内容
我在谷歌上搜索了stack*overflow*,但似乎没有找到答案,所以非常感谢您的帮助
非常感谢。如果您的ncdf对象被称为nc
,那么非常简单:
names(nc$var)
例如,使用下载的数据集(因为您没有提供数据集):
nc现在是2016年。ncdf包已弃用。
与SE用户plannapus的答案相同的代码现在是:
library(ncdf4)
netcdf.file <- "flux.nc"
nc = ncdf4::nc_open(netcdf.file)
variables = names(nc[['var']])
#print(nc)
非常感谢,@plannapus-我应该想到这一点!很抱歉没有提供一个示例文件,因为我对R和netCDF非常陌生,我想我无法复制我的工作文件,该文件的大小为~35 GB以说明…;-)我们如何知道“default”变量ncvar\u get
将返回的名称?它不是名称(nc$var)
列表中的第一个。
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.