# coding=utf-8
"""Data frequency management tools"""
[docs]class Frequency(object):
"""Time frequency"""
_recognized = {'f': 'fx', 'fx': 'fx', 'fixed': 'fx',
'c': 'clim', 'clim': 'clim', 'climatology': 'clim', 'monclim': 'clim', '1hrclimmon': 'clim',
'dec': 'dec', 'decadal': 'dec',
'y': 'year', 'yr': 'year', 'year': 'year', 'yearly': 'year',
'm': 'mon', '1m': 'mon', 'mon': 'mon', 'monthly': 'mon', 'mm': 'mon',
'w': 'week', '1w': 'week', 'week': 'week', 'weekly': 'week',
'd': 'day', '1d': 'day', 'daily': 'day', 'day': 'day',
'15': '15hr', '15h': '15hr', '15hr': '15hr', '15_hourly': '15hr', '15hourly': '15hr',
'15 hourly': '15hr',
'6': '6hr', '6h': '6hr', '6hr': '6hr', '6_hourly': '6hr', '6hourly': '6hr', '6 hourly': '6hr',
'3': '3hr', '3h': '3hr', '3hr': '3hr', '3_hourly': '3hr', '3hourly': '3hr', '3 hourly': '3hr',
'1': '1hr', 'hr': '1hr', '1h': '1hr', 'hourly': '1hr', '1hr': '1hr', '1 hourly': '1hr',
'450mn': '450mn',
'subhr': 'subhr'}
def __init__(self, freq):
freq = freq.lower()
try:
self.frequency = Frequency._recognized[freq]
except KeyError:
raise ValueError('Frequency {0} not supported'.format(freq))
def __eq__(self, other):
return self.frequency == other.frequency
def __ne__(self, other):
return not self == other
def __str__(self):
return self.frequency
[docs] def folder_name(self, vartype):
"""
Get foder name associated to this frequency
Parameters
----------
vartype: VariableType
Returns
-------
str
"""
from earthdiagnostics.variable import VariableType
if self == Frequencies.daily:
freq_str = 'daily_{0}'.format(VariableType.to_str(vartype))
elif self == Frequencies.weekly:
freq_str = 'weekly_{0}'.format(VariableType.to_str(vartype))
elif self == Frequencies.climatology:
freq_str = 'clim'
elif self in (Frequencies.three_hourly, Frequencies.six_hourly, Frequencies.hourly):
freq_str = self.frequency[:-2] + 'hourly'
if vartype != VariableType.MEAN:
freq_str = '{0}_{1}'.format(freq_str, VariableType.to_str(vartype))
else:
freq_str = 'monthly_{0}'.format(VariableType.to_str(vartype))
return freq_str
[docs] @staticmethod
def parse(freq):
"""
Get frequency instance from str
If a Frequency object is passed, it is returned as usual
Parameters
----------
freq: str or Frequency
Returns
-------
Frequency
"""
if isinstance(freq, Frequency):
return freq
return Frequency(freq)
[docs]class Frequencies(object):
"""Enumeration of supported frequencies"""
fixed = Frequency('fx')
climatology = Frequency('clim')
yearly = Frequency('year')
monthly = Frequency('mon')
weekly = Frequency('week')
daily = Frequency('day')
six_hourly = Frequency('6hr')
three_hourly = Frequency('3hr')
hourly = Frequency('hr')
subhourly = Frequency('subhr')