示例#1
0
    def get(self, attr, unit=None):
        '''
        return value in desired unit. If None, then return the value in SI
        units. The user_unit are given in 'units' attribute and each attribute
        carries the value in as given in these user_units.
        '''
        val = getattr(self, attr)

        if unit is None:
            # Note: salinity only have one units since we don't
            # have any conversions for them in unit_conversion yet - revisit
            # this per requirements
            if (attr not in self._si_units
                    or self._si_units[attr] == self._units[attr]):
                return val
            else:
                unit = self._si_units[attr]

        if unit in self._units_type[attr][1]:
            return uc.convert(self._units_type[attr][0], self.units[attr],
                              unit, val)
        else:
            # log to file if we have logger
            ex = uc.InvalidUnitError((unit, self._units_type[attr][0]))
            self.logger.error(str(ex))
            raise ex
示例#2
0
 def area_units(self, value):
     '''
     value must be one of the valid units given in valid_area_units
     '''
     if value not in self.valid_area_units:
         e = uc.InvalidUnitError((value, 'Area'))
         self.logger.error(str(e))
         raise e
     else:
         self._area_units = value
示例#3
0
    def set(self, attr, value, unit):
        '''
        provide a corresponding set method that requires value and units
        The attributes can be directly set. This function just sets the
        desired property and also updates the units dict
        '''
        if unit not in self._units_type[attr][1]:
            raise uc.InvalidUnitError((unit, self._units_type[attr][0]))

        setattr(self, attr, value)
        self.units[attr] = unit
示例#4
0
    def units(self, u_dict):
        for prop, unit in u_dict.iteritems():
            if prop in self._units_type:
                if unit not in self._units_type[prop][1]:
                    msg = ("{0} are invalid units for {1}."
                           "Ignore it".format(unit, prop))
                    self.logger.error(msg)
                    # should we raise error?
                    raise uc.InvalidUnitError(msg)

            # allow user to add new keys to units dict.
            # also update prop if unit is valid
            self._units[prop] = unit
示例#5
0
    def thickness_units(self, value):
        '''
        value must be one of the valid units given in valid_length_units
        also reset active stop
        '''
        if value not in self.valid_length_units:
            e = uc.InvalidUnitError((value, 'Length'))
            self.logger.error(str(e))
            raise e

        self._thickness_units = value
        self._init_rate_duration()

        # if thickness in these units is < min required, log a warning
        self._log_thickness_warning()
示例#6
0
    def _check_units(self, units):
        """
        Checks the user provided units are in list of valid volume
        or mass units
        """

        if (units in self.valid_vol_units or units in self.valid_mass_units):
            return True
        else:
            msg = ('Units for amount spilled must be in volume or mass units. '
                   'Valid units for volume: {0}, for mass: {1} ').format(
                       self.valid_vol_units, self.valid_mass_units)
            ex = uc.InvalidUnitError(msg)
            self.logger.exception(ex, exc_info=True)
            raise ex  # this should be raised since run will fail otherwise
示例#7
0
 def _check_units(self, units):
     '''
     Checks the user provided units are in list Wind.valid_vel_units
     '''
     if units not in Wind.valid_vel_units:
         raise uc.InvalidUnitError((units, 'Velocity'))
示例#8
0
def test_InvalidUnitError():
    "just testing the __str__..."
    err = str(unit_conversion.InvalidUnitError(('feet', 'volume')))

    assert err == 'The unit: feet is not in the list for Unit Type: volume'