# coding=utf-8 from earthdiagnostics.frequency import Frequencies class ModelingRealm(object): def __init__(self, domain_name): domain_name = domain_name.lower() if domain_name == 'seaice': self.name = 'seaIce' elif domain_name == 'landice': self.name = 'landIce' elif domain_name == 'atmoschem': self.name = 'atmosChem' elif domain_name == 'ocnbgchem': self.name = 'ocnBgchem' elif domain_name in ['ocean', 'atmos', 'land', 'aerosol']: self.name = domain_name else: raise ValueError('Modelling realm {0} not recognized!'.format(domain_name)) def __eq__(self, other): return other.__class__ == ModelingRealm and self.name == other.name def __ne__(self, other): return not (self == other) def __str__(self): return self.name def get_table_name(self, frequency, data_convention): """ Returns the table name for a domain-frequency pair :param data_convention: Data convention in use :type data_convention: str :param frequency: variable's frequency :type frequency: str :return: variable's table name :rtype: str """ if frequency in (Frequencies.monthly, Frequencies.climatology, Frequencies.daily): if self.name == 'seaIce': if data_convention in ('specs', 'preface'): prefix = 'OI' else: prefix = 'SI' elif self.name == 'landIce': prefix = 'LI' else: prefix = self.name[0].upper() table_name = prefix + str(frequency) elif frequency == Frequencies.six_hourly: table_name = '6hrPlev' else: table_name = frequency.frequency return table_name def get_table(self, frequency, data_convention): table_name = self.get_table_name(frequency, data_convention) from earthdiagnostics.variable import CMORTable return CMORTable(table_name, frequency, 'December 2013') class ModelingRealms(object): seaIce = ModelingRealm('seaice') ocean = ModelingRealm('ocean') landIce = ModelingRealm('landIce') atmos = ModelingRealm('atmos') land = ModelingRealm('land') aerosol = ModelingRealm('aerosol') atmosChem = ModelingRealm('atmosChem') ocnBgchem = ModelingRealm('ocnBgchem') @classmethod def parse(cls, modelling_realm): """ Return the basin matching the given name. If the parameter basin is a Basin instance, directly returns the same instance. This bahaviour is intended to facilitate the development of methods that can either accept a name or a Basin instance to characterize the basin. :param modelling_realm: basin name or basin instance :type modelling_realm: str | Basin :return: basin instance corresponding to the basin name :rtype: Basin """ if isinstance(modelling_realm, ModelingRealm): return modelling_realm for name in cls.__dict__.keys(): if name.startswith('_'): continue # noinspection PyCallByClass value = cls.__getattribute__(cls, name) if isinstance(value, ModelingRealm): if modelling_realm.lower() in [value.name.lower()]: return value raise ValueError('Modelling realm {0} not recognized!'.format(modelling_realm))