netCDF 文件中的 uwnd 变量

uwnd variable in netCDF file

如何在 python 中读取 netCDF 文件中的 uwnd 变量?

from mpl_toolkits.basemap import Basemap
from netCDF4 import Dataset, date2index
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime
from IPython.display import Image
from IPython.core.display import HTML 
import netCDF4 as nc
plt.clf()
nc_file = '042711_V.nc'
nc_file2 = '042711_U.nc'
V = Dataset(nc_file, mode='r')
U = Dataset(nc_file2, mode='r')
print U.variables

这验证了uwnd确实是一个变量,但是为什么读不出来呢?

OrderedDict([(u'lat', <type 'netCDF4._netCDF4.Variable'>
float32 lat(lat)
    units: degrees_north
    actual_range: [ 90. -90.]
    long_name: Latitude
unlimited dimensions: 
current shape = (73,)
filling off
), (u'lon', <type 'netCDF4._netCDF4.Variable'>
float32 lon(lon)
    units: degrees_east
    long_name: Longitude
    actual_range: [   0.   357.5]
unlimited dimensions: 
current shape = (144,)
filling off
), (u'time', <type 'netCDF4._netCDF4.Variable'>
float64 time(time)
    units: hours since 1800-1-1 00:00:0.0
    long_name: Time
    actual_range: [ 71870952.  71870952.]
    delta_t: 0000-01-00 00:00:00
unlimited dimensions: time
current shape = (1,)
filling off
), (u'uwnd', <type 'netCDF4._netCDF4.Variable'>
float32 uwnd(time, lat, lon)
    long_name: u wind
    valid_range: [-99999.  99999.]
    actual_range: [ -26.84999084  377.20001221]
    units: m/s
    add_offset: 0.0
    scale_factor: 1.0
    missing_value: -9.96921e+36
    precision: 99
    least_significant_digit: 99
    var_desc: u wind
    dataset: CDC Derived NCEP Reanalysis Products
    level_desc:  500mb Pressure Level
    statistic: Composite
    parent_stat: Other
unlimited dimensions: time
current shape = (1, 73, 144)
filling off
)])

KeyError: 'uwnd'

我正在尝试在地图上绘制一些 widnbarbs。我已经成功地让它读取时间、经度和纬度,但是 uwnd 是什么意思,我如何将它读入 numpy 格式?

这是我找到的关于如何读取 netCDF 文件的网站: http://www.hydro.washington.edu/~jhamman/hydro-logic/blog/2013/10/12/plot-netcdf-data/

edit 之后 discussing 更详细了,原来 uwnd 在另一个调用了 vwnd (042711_U.nc) 文件,导致 KeyError


您是否从代码中的最后一个 print 语句中获得了 KeyError(或您显示的整个输出)?通常像这样的东西应该将 uwnd 读入 3D Numpy 数组:

from netCDF4 import Dataset
U = Dataset('042711_U.nc', 'r')
uwnd = U.variables["uwnd"][:,:,:]

例如,气象数据集略有不同:

import netCDF4 as nc4
nc = nc4.Dataset('drycblles.default.0000000.nc')
u = nc.variables["u"][:,:]
print(type(u), u.shape)

<class 'numpy.ndarray'> (8, 32)