![](/img/trans.png)
[英]how to prevent netcdf4 in python from loading entire variable when using datetime
[英]Python3.7 function to plot datetime from timestep of netCDF4
我需要来自 netcdf 文件的 plot bg 变量,但是时间表示为时间步长。 我阅读了 netcdf 文件:
data =netCDF4.Dataset (r'D:\Users\NOAA\\CT2019B.molefrac_components_glb3x2_2014-2018.nc')
time = data.variables['time'][:]
bg = data.variables['bg']
时间是:
masked_array(data=[5114.0625, 5114.0625, 5114.1875, ..., 6939.8125,
6939.9375, 6939.9375],
mask=False,
fill_value=1e+20)
单位:
'days since 2000-1-1 00:00:00'
plot(time,bg)
我需要将 x.axis 从时间步数转换为日期(2014-01-01 ....)
我试图将它们转换为
time = netCDF4.num2date(data.variables['time'][:],data.variables['time'].units)
但是,当我到 plot 时
plot(time,bg)
我收到了这个错误信息
TypeError: float() 参数必须是字符串或数字,而不是 'cftime._cftime.DatetimeGregorian'
有谁知道如何解决这个问题? 非常感谢!
您可以强制num2date
给您 Python datetime
时间对象:
time = netCDF4.num2date(data.variables['time'][:], data.variables['time'].units,
only_use_python_datetimes=True)
有关更多信息,请参阅cftime
的文档(netCDF4 在后台使用的)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.