Reputation: 91
I need to plot bg variable from netcdf file, however the time is expressed as time-step. I read the netcdf file:
data =netCDF4.Dataset (r'D:\Users\NOAA\\CT2019B.molefrac_components_glb3x2_2014-2018.nc')
time = data.variables['time'][:]
bg = data.variables['bg']
time is :
masked_array(data=[5114.0625, 5114.0625, 5114.1875, ..., 6939.8125,
6939.9375, 6939.9375],
mask=False,
fill_value=1e+20)
units :
'days since 2000-1-1 00:00:00'
plot(time,bg)
I need to convert the x.axis from timestep number to date (2014-01-01 ....)
I tried to convert them as
time = netCDF4.num2date(data.variables['time'][:],data.variables['time'].units)
but, when I to plot it
plot(time,bg)
I received this of error message
TypeError: float() argument must be a string or a number, not 'cftime._cftime.DatetimeGregorian'
Does anyone knows how to solve this issue? Thanks a lot!
Upvotes: 1
Views: 756
Reputation: 5863
You can force num2date
to give you Python datetime
objects instead:
time = netCDF4.num2date(data.variables['time'][:], data.variables['time'].units,
only_use_python_datetimes=True)
See the docs for cftime
(which is what netCDF4 uses under the hood) for more information.
Upvotes: 0