繁体   English   中英

使用 netCDF4 num2date 函数时出错

[英]Error using netCDF4 num2date function

我正在处理一个 nc 文件。该文件有一个维度数组,包括时间、数字、纬度、经度。 我试图通过num2date方法将时间从数字转换为日期。

time_unit = nc.variables["time"].getncattr('units') # first read the 'units' attributes from the variable time
print(time_unit)
time_cal = nc.variables["time"].getncattr('calendar') # read calendar type
local_time = nc.num2date(t, units=time_unit, calendar=time_cal) # convert time
print("Original time %s is now converted as %s" % (time[0], local_time[0]))

但我面临以下错误。

AttributeError                            Traceback (most recent call last)
<ipython-input-124-d9912be48299> in <module>()
      2 print(time_unit)
      3 time_cal = nc.variables["time"].getncattr('calendar') # read 
calendar type
----> 4 local_time = nc.num2date(t, units=time_unit, calendar=time_cal) # 
convert time
      5 print("Original time %s is now converted as %s" % (time[0], 
local_time[0]))

netCDF4\_netCDF4.pyx in netCDF4._netCDF4.Dataset.__getattr__ 
(netCDF4\_netCDF4.c:20474)()

netCDF4\_netCDF4.pyx in netCDF4._netCDF4.Dataset.getncattr 
(netCDF4\_netCDF4.c:19433)()

netCDF4\_netCDF4.pyx in netCDF4._netCDF4._get_att (netCDF4\_netCDF4.c:4451()

AttributeError: NetCDF: Attribute not found

任何解决方案?

nc是您打开的 netCDF 文件,但num2date位于netCDF4

>>> import netCDF4
>>> netCDF4.num2date
<function netCDF4._netCDF4.num2date>

因此,这样称呼它:

local_time = netCDF4.num2date(t, units=time_unit, calendar=time_cal) 

暂无
暂无

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

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