util.py 文件源码

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

项目:ocean-regrid 作者: nicjhan 项目源码 文件源码
def get_time_origin(filename):
    """
    Parse time.units to find the start/origin date of the file. Return a
    datetime.date object.
    """

    date_search_strings = ['\d{4}-\d{2}-\d{2}','\d{4}-\d{1}-\d{2}',
                            '\d{4}-\d{2}-\d{1}','\d{4}-\d{1}-\d{1}']

    with nc.Dataset(filename) as f:
        time_var = f.variables['time']
        assert 'months since' in time_var.units or \
               'days since' in time_var.units or \
               'hours since' in time_var.units, \
            "Time units doesn't have expected format: {}".format(time_var.units)
        for ds in date_search_strings:
            m = re.search(ds, time_var.units)
            if m is not None:
                break
        assert m is not None
        date = dt.datetime.strptime(m.group(0), '%Y-%m-%d')

    return dt.date(date.year, date.month, date.day)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号