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)
评论列表
文章目录