utils.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:faampy 作者: ncasuk 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号