python3.x中的netcdf对某些函数无法正常工作

2024-04-19 13:36:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我注意到netcdf函数变量.键()for python3未按预期工作。它不像使用python2时那样返回键列表(dictionary),而是像调用不带括号的函数时那样返回整个信息块。 这并不是python3中唯一一个以这种方式运行的函数。我的理解是netcdf4库已经移植到py3。你知道吗

以下是函数的预期输出:

In [5]: nc.variables.keys()
Out[5]: 
[u'area',
 u'lat',
 u'lat_bnds',
 u'lon',
 u'lon_bnds',
 u'msk_rgn',
 u'plev',
 u'pr',
 u'tas',
 u'time',
 u'time_bnds',
 u'ua']

相反,这就是所产生的:

In [15]: nc.variables.keys()
Out[15]: 
KeysView(OrderedDict([('area', <class 'netCDF4.Variable'>
float32 area(lat, lon)
    long_name: Surface area
    units: meter2
unlimited dimensions: 
current shape = (128, 256)
filling off
), ('lat', <class 'netCDF4.Variable'>
float32 lat(lat)
    long_name: latitude
    units: degrees_north
    axis: Y
    standard_name: latitude
    bounds: lat_bnds
unlimited dimensions: 
current shape = (128,)
filling off
), ('lat_bnds', <class 'netCDF4.Variable'>
float64 lat_bnds(lat, bnds)
unlimited dimensions: 
current shape = (128, 2)
filling off
), ('lon', <class 'netCDF4.Variable'>
float32 lon(lon)
    long_name: longitude
    units: degrees_east
    axis: X
    standard_name: longitude
    bounds: lon_bnds
unlimited dimensions: 
current shape = (256,)
filling off
), ('lon_bnds', <class 'netCDF4.Variable'>
float64 lon_bnds(lon, bnds)
unlimited dimensions: 
current shape = (256, 2)
filling off
), ('msk_rgn', <class 'netCDF4.Variable'>
int32 msk_rgn(lat, lon)
    long_name: Mask region
    units: bool
unlimited dimensions: 
current shape = (128, 256)
filling off
), ('plev', <class 'netCDF4.Variable'>
float64 plev(plev)
    long_name: pressure
    units: Pa
    standard_name: air_pressure
    positive: down
    axis: Z
unlimited dimensions: 
current shape = (17,)
filling off
), ('pr', <class 'netCDF4.Variable'>
float32 pr(time, lat, lon)
    comment: Created using NCL code CCSM_atmm_2cf.ncl on
 machine eagle163s
    missing_value: 1e+20
    _FillValue: 1e+20
    cell_methods: time: mean (interval: 1 month)
    history: (PRECC+PRECL)*r[h2o]
    original_units: m-1 s-1
    original_name: PRECC, PRECL
    standard_name: precipitation_flux
    units: kg m-2 s-1
    long_name: precipitation_flux
    cell_method: time: mean
unlimited dimensions: time
current shape = (1, 128, 256)
filling off
), ('tas', <class 'netCDF4.Variable'>
float32 tas(time, lat, lon)
    comment: Created using NCL code CCSM_atmm_2cf.ncl on
 machine eagle163s
    missing_value: 1e+20
    _FillValue: 1e+20
    cell_methods: time: mean (interval: 1 month)
    history: Added height coordinate
    coordinates: height
    original_units: K
    original_name: TREFHT
    standard_name: air_temperature
    units: K
    long_name: air_temperature
    cell_method: time: mean
unlimited dimensions: time
current shape = (1, 128, 256)
filling off
), ('time', <class 'netCDF4.Variable'>
float64 time(time)
    calendar: noleap
    standard_name: time
    axis: T
    units: days since 0000-1-1
    bounds: time_bnds
    long_name: time
unlimited dimensions: time
current shape = (1,)
filling off
), ('time_bnds', <class 'netCDF4.Variable'>
float64 time_bnds(time, bnds)
unlimited dimensions: time
current shape = (1, 2)
filling off
), ('ua', <class 'netCDF4.Variable'>
float32 ua(time, plev, lat, lon)
    comment: Created using NCL code CCSM_atmm_2cf.ncl on
 machine eagle163s
    missing_value: 1e+20
    cell_methods: time: mean (interval: 1 month)
    long_name: eastward_wind
    history: Interpolated U with NCL 'vinth2p_ecmwf'
    units: m s-1
    original_units: m s-1
    original_name: U
    standard_name: eastward_wind
    _FillValue: 1e+20
unlimited dimensions: time
current shape = (1, 17, 128, 256)
filling off
)]))

Tags: nametimecurrentvariableclassdimensionsnetcdf4lon
1条回答
网友
1楼 · 发布于 2024-04-19 13:36:24

这不是一个bug,而是字典更改的结果,特别是在python3.x中的OrderedDict。在python3.x中,keys()方法不再返回键列表,而是返回迭代器对象。您看到的输出只是迭代器对象(KeysView)及其引用的原始字典,然后显示其表示形式。你知道吗

如果您需要一个密钥列表,您需要特别询问:list(nc.variables.keys())。你知道吗

相关问题 更多 >