def get_mpl_time(ds, *freq):
"""Return a float wihich is usabale for plt.plot_date from matplotlib.
:param ds: core_faam dataset
:type param: netCDF4.Dataset
:param freq: frequency of the time stamp default=1; if freq > 1 a multidimensional array is returned
:return: array containing the matplotlib timestamps
:rtype: numpy.array
>>> ds = netCDF4.Dataset('core_faam_20130403_v004_r0_b768.nc', 'r')
>>> t_1hz = get_mpl_time(ds)
>>> t_1hz.shape
Out[1]: (37137,)
>>> t_32hz = get_mpl_time(ds, 32)
>>> t_32hz.shape
Out[1]: (37137, 32)
>>> plot_date(t_32hz.ravel(), ds.variables['U_C'][:].ravel(), 'b-')
>>>
"""
if 'Time' in ds.variables.keys():
vtime=ds.variables['Time'][:]
elif 'time' in ds.variables.keys():
vtime=ds.variables['time'][:]
elif 'TIME' in ds.variables.keys():
vtime=ds.variables['TIME'][:]
#in old core files the 'Time' variable was c2alled PARA0515
elif 'PARA0515' in ds.variables.keys():
vtime=ds.variables['PARA0515'][:]
else:
return None
vtime=np.array(vtime)
if freq:
rows = len(vtime)
vtime = vtime.repeat(freq[0]).reshape((rows, freq[0])) + np.array(range(freq[0]), dtype=np.float64)/freq[0]
result=np.float64(vtime/86400.) + np.float64(date2num(get_base_time(ds)))
return result
评论列表
文章目录