datamanager.py 48.2 KB
Newer Older
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
        :rtype: str
        """
        return 'fc{0}'.format(str(member).zfill(self.member_digits))


class Variable(object):
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
    """
    Class to characterize a CMOR variable. It also contains the static method to make the match between thje original
    name and the standard name. Requires cmor_table.csv to work.
    """

    def __init__(self, line):
        self.short_name = line[1]
        self.standard_name = line[2]
        self.long_name = line[3]
        self.domain = line[4]
        self.basin = Basins.parse(line[5])

    @classmethod
    def get_variable(cls, original_name):
        try:
            return Variable._dict_variables[original_name.lower()]

        except AttributeError:
            Variable._dict_variables = dict()
            with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'cmor_table.csv'), 'rb') as csvfile:
                reader = csv.reader(csvfile, dialect='excel')
                for line in reader:
                    if line[0] == 'variable':
                        continue
                    var = Variable(line)
                    if not var.short_name:
                        continue
                    for old_name in line[0].split(':'):
                        Variable._dict_variables[old_name] = var
                    Variable._dict_variables[var.short_name] = var
            return Variable.get_variable(original_name)

        except KeyError:
            Log.error('Variable {0} is not defined'.format(original_name))
            return None