简体   繁体   English

使用 netCDF4 从带有 Python 的 NETCDF 文件中提取名称

[英]Extracting a name from a NETCDF file with Python using netCDF4

I have been fiddling with the netCDF4 package for a while, and no matter what I try, I am unable to load the variable names.我一直在摆弄 netCDF4 package 一段时间,无论我尝试什么,我都无法加载变量名。 I will show you what I mean by it.我会告诉你我的意思。 The simple code I have written is as follows:我写的简单代码如下:

import netCDF4 as netcdf
import numpy as np

f = netcdf.Dataset("pressureOutput/QOUT_500.Netcdf","r")
   
variablesList = f.variables

print(variablesList)

The output on the console is:控制台上的output为:

{'variables0': <class 'netCDF4._netCDF4.Variable'>
float64 variables0(dim0)
    name: u
unlimited dimensions: 
current shape = (163840,)
filling on, default _FillValue of 9.969209968386869e+36 used, 'variables1': <class 'netCDF4._netCDF4.Variable'>
float64 variables1(dim0)
    name: v
unlimited dimensions: 
current shape = (163840,)
filling on, default _FillValue of 9.969209968386869e+36 used, 'variables2': <class 'netCDF4._netCDF4.Variable'>
float64 variables2(dim0)
    name: rho
unlimited dimensions: 
current shape = (163840,)
filling on, default _FillValue of 9.969209968386869e+36 used, 'variables3': <class 'netCDF4._netCDF4.Variable'>
float64 variables3(dim0)
    name: p
unlimited dimensions: 
current shape = (163840,)
filling on, default _FillValue of 9.969209968386869e+36 used, 'variables4': <class 'netCDF4._netCDF4.Variable'>
float64 variables4(dim0)
    name: YH2
unlimited dimensions: 
current shape = (163840,)
filling on, default _FillValue of 9.969209968386869e+36 used, 'variables5': <class 'netCDF4._netCDF4.Variable'>
( MORE OUTPUT)
(...)
(...)
filling on, default _FillValue of 9.969209968386869e+36 used, 'variables24': <class 'netCDF4._netCDF4.Variable'>
float64 variables24(dim0)
    name: heatRelease
unlimited dimensions: 
current shape = (163840,)
filling on, default _FillValue of 9.969209968386869e+36 used}

As one can see, the variable 'variables0' has a property named name, which should output u.可以看到,变量“variables0”有一个名为 name 的属性,应该是 output u。 I want to retrieve this information for every one of the variables (u, v, rho, p, etc.).我想为每个变量(u、v、rho、p 等)检索此信息。 However, if I try但是,如果我尝试

f['variables0'].name

I get 'variables0' as an output.我得到“variables0”作为 output。 How can I retrieve this information from the file?如何从文件中检索此信息?

You should be able to solve this problem by converting it to a list:您应该能够通过将其转换为列表来解决此问题:

list(variablesList)

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

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