basins.py 3.72 KB
Newer Older
class Basin(object):
    """
    Class representing a given basin

    :param shortname: sfull basin's name
    :type shortname: str
    :param fullname: full basin's name
    :type fullname: str
    :param coordinates: list of coordinates for the basin in the order: min longitude, max longitude, min latitude,
     max latitude
    :type coordinates: tuple
    def __init__(self, shortname, fullname, coordinates=None):
        self._shortname = shortname
        self._fullname = fullname
        if coordinates:
            self._coordinates = coordinates
        else:
            self._coordinates = [0, 0, 0, 0]

    @property
    def shortname(self):
        """
        Basin's short name
        :rtype: str
        """
        return self._shortname

    @property
    def fullname(self):
        """
        Basin's full name
        :rtype: str
        """
        return self._fullname

    @property
    def min_lon(self):
        """
        Minimum longitude
        :rtype: int
        """
        return self._coordinates[0]

    @property
    def max_lon(self):
        """
        Maximum longitude
        :rtype: int
        """
        return self._coordinates[1]

    @property
    def min_lat(self):
        """
        Minimum latitude
        :rtype: int
        """
        return self._coordinates[2]

    @property
    def max_lat(self):
        """
        Maximum latitude
        :rtype: int
        """
        return self._coordinates[3]

    @property
    def lon(self):
        """
        Longitude limits of the basin
        :rtype: tuple
        """
        return self._coordinates[0:2]

    @property
    def lat(self):
        """
        Latitude limits of the basin
        :rtype: tuple
        """
        return self._coordinates[2:]


class Basins(object):
    """
    Predefined basins
    """
    Global = Basin('glob', 'Global_Ocean')
    """ Global ocean """
    Atlantic = Basin('Atl', 'Atlantic_Ocean')
    """ Atlantic ocean """
    NorthAtlantic = Basin('NAtl', 'North_Atlantic_Ocean')
    """ North Atlantic Ocean """
    TropicalAtlantic = Basin('TAtl', 'Tropical_Atlantic_Ocean')
    """ Tropical Atlantic Ocean """
    Pacific = Basin('Pac', 'Pacific_Ocean')
    """ Pacific Ocean """
    NorthPacific = Basin('NPac', 'North_Pacific_Ocean')
    """ North Pacific Ocean """
    TropicalPacific = Basin('TPac', 'Tropical_Pacific_Ocean')
    """ Tropical Pacific Ocean """
    IndoPacific = Basin('IndPac', 'Indo_Pacific_Ocean')
    """ Indo Pacific Ocean """
    Indian = Basin('Ind', 'Indian_Ocean')
    """ Indian Ocean """
    TropicalIndian = Basin('TInd', 'Tropical_Indian_Ocean')
    """ Tropical Indian Ocean """
    Antarctic = Basin('Anta', 'Antarctic_Ocean')
    """ Antarctic Ocean """
    Arctic = Basin('Arct', 'Arctic_Ocean')
    """ Arctic Ocean """
        """
        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 basin: basin name or basin instance
        :type basin: str | Basin
        :return: basin instance corresponding to the basin name
        :rtype: Basin
        """
        if isinstance(basin, Basin):
            return basin
        for name in cls.__dict__.keys():
            if name.startswith('_'):
                continue
            # noinspection PyCallByClass
            value = cls.__getattribute__(cls, name)
            if isinstance(value, Basin):
                if basin.lower() in [value.shortname.lower(), value.fullname.lower()]:
                    return value
        return None