简体   繁体   English

如何从R中的netCDF文件中提取变量名?

[英]How to extract variable names from a netCDF file in R?

I am writing a function in R to extract some air quality modelling data from netCDF files. 我在R中编写一个函数来从netCDF文件中提取一些空气质量建模数据。 I have the Package "ncdf" installed. 我安装了包“ncdf”。

In order to allow other users or myself to choose what variables to extract from a netCDF file, I would like to extract the names of all variables in the file, so that I can present in a simple list rather than just print.ncdf() the file to give too much information. 为了让其他用户或我自己选择从netCDF文件中提取哪些变量,我想提取文件中所有变量的名称,这样我就可以在一个简单的列表中而不仅仅是print.ncdf()该文件提供了太多信息。 Is there any way of doing it? 这有什么办法吗?

I tried unlist() to the var field of the ncdf object but it seemed that it returned the contents as well... 我尝试将unlist()发送到ncdf对象的var字段,但它似乎也返回了内容...

I googled and searched stack* overflow * but didn't seem to find an answer, so your help is very much appreciated. 我google搜索堆栈* 溢出 *但似乎没有找到答案,所以非常感谢你的帮助。

Many thanks in advance. 提前谢谢了。

If your ncdf object is called nc , then quite simply: 如果您的ncdf对象被称为nc ,那么很简单:

names(nc$var)

With an example, using the dataset downloaded here , for instance (since you didn't provide with one): 举个例子,使用这里下载的数据集(因为你没有提供一个):

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"   

It is now 2016. ncdf package is deprecated. 现在是2016年。不推荐使用ncdf包。 Same code as SE user plannapus' answer is now: 与SE用户plannapus的答案相同的代码现在是:

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

A note from the documentation: 文档中的注释:

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.

A note from the home page of the maintainer: 维护者主页上的注释:

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/ http://cirrus.ucsd.edu/~pierce/ncdf/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM