def get_mpl_time(ds, basetime=None, freq=1):
"""Return a matplotlib usable time format from the faam core netCDF4.
>>> 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 hasattr(ds, 'variables'):
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'][:]
elif isinstance(ds, dict):
if ds.has_key('Time'):
vtime=ds['Time']
else:
return None
import numpy as np
rows = len(vtime)
vtime = vtime.repeat(freq).reshape((rows, freq)) + np.array(range(freq), dtype=np.float64)/freq
if not basetime:
basetime=get_base_time(ds)
result=np.float64(vtime/86400.) + np.float64(date2num(basetime))
return result
评论列表
文章目录