Esempio n. 1
0
 def __call__(self, *args, **kwargs):
     """The prefered way to pass values to |TInd| instances
     within parameter control files.
     """
     try:
         super().__call__(*args, **kwargs)
     except NotImplementedError:
         try:
             tal = float(kwargs['tal'])
             hot = float(kwargs['hot'])
             hut = float(kwargs['hut'])
         except KeyError:
             raise ValueError(
                 'For the alternative calculation of parameter `tind`, '
                 'values for all three keyword keyword arguments `tal`, '
                 '`hot`, and `hut` must be given.')
         if (tal <= 0.) or (hot <= hut):
             raise ValueError(
                 'For the alternative calculation of parameter '
                 '`tind`, the value assigned to keyword argument '
                 '`tal` must be greater then zero and the one of '
                 '`hot` must be greater than the one of `hut`.  '
                 'However, for element %s, the values `%s`, `%s` '
                 'and `%s` were given respectively.' %
                 (objecttools.devicename(self), tal, hot, hut))
         self.value = (.868 * tal**3 / (hot - hut))**.385
         if (self > 1000.) or (self < .001):
             warnings.warn(
                 'Due to the given values for the keyword arguments '
                 '`tal` (%s), `hot` (%s) and `hut` (%s), parameter '
                 '`tind` of element `%s` has been set to an '
                 'unrealistic value of `%s hours`.' %
                 (tal, hot, hut, objecttools.devicename(self),
                  objecttools.repr_(self.value)))
         self.value *= timetools.Period('1h') / self.simulationstep
Esempio n. 2
0
 def __call__(self, *args, **kwargs):
     self._coefs.clear()
     if len(args) > 1:
         raise ValueError(
             'For parameter `%s` of element `%s` at most one positional '
             'argument is allowed, but `%d` are given.' %
             (self.name, objecttools.devicename(self.subpars), len(args)))
     for (key, value) in kwargs.items():
         setattr(self, key, value)
     if len(args) == 1:
         setattr(self, 'th_0_0', args[0])
     if len(args) + len(kwargs) != len(self):
         raise ValueError(
             'For parameter `%s` of element `%s` `%d` arguments have been '
             'given but only `%s` response functions could be prepared.  '
             'Most probably, you defined the same threshold value(s) twice.'
             % (self.name, objecttools.devicename(
                 self.subpars), len(args) + len(kwargs), len(self)))
Esempio n. 3
0
def _trim_int_0d(self, lower, upper):
    if lower is None:
        lower = _INT_NAN
    if upper is None:
        upper = -_INT_NAN
    if (self != _INT_NAN) and ((self < lower) or (self > upper)):
        raise ValueError(
            'The value `%d` of parameter `%s` of element `%s` is not valid.  '
            % (self.value, self.name, objecttools.devicename(self)))
Esempio n. 4
0
 def __call__(self, *args, **kwargs):
     """The prefered way to pass values to :class:`TInd` instances
     within parameter control files.
     """
     try:
         parametertools.SingleParameter.__call__(self, *args, **kwargs)
     except NotImplementedError:
         counter = ('tal' in kwargs) + ('hot' in kwargs) + ('hut' in kwargs)
         if counter == 0:
             raise ValueError('For parameter `tind` a value can be set '
                              'directly by passing a single value or '
                              'indirectly by using the keyword arguments '
                              '`tal`, `hot`, and `hut`.')
         elif counter < 3:
             raise ValueError('For the alternative calculation of '
                              'parameter `tind`, values for all three '
                              'keyword keyword arguments `tal`, `hot`, '
                              'and `hut` must be given.')
         else:
             tal = float(kwargs['tal'])
             hot = float(kwargs['hot'])
             hut = float(kwargs['hut'])
             if (tal <= 0.) or (hot <= hut):
                 raise ValueError(
                     'For the alternative calculation of '
                     'parameter `tind`, the value assigned to '
                     'keyword argument `tal` must be greater '
                     'then zero and the one of `hot` must be '
                     'greater than the one of `hut`.  '
                     'However, for element %s, the values '
                     '`%s`, `%s` and `%s` were given '
                     'respectively.' %
                     (objecttools.devicename(self), tal, hot, hut))
             self.value = (.868 * tal**3 / (hot - hut))**.385
             if (self > 1000.) or (self < .001):
                 warnings.warn('Due to the given values for the keyword '
                               'arguments `tal` (%s), `hot` (%s) and `hut` '
                               '(%s), parameter `tind` of element `%s` has '
                               'been set to an unrealistic value of `%s '
                               'hours`.' %
                               (tal, hot, hut, objecttools.devicename(self),
                                round(self.value, 6)))
             self.value *= timetools.Period('1h') / self.simulationstep
Esempio n. 5
0
 def connect(self):
     """Connect the link sequences of the actual model."""
     try:
         for group in ('inlets', 'receivers', 'outlets', 'senders'):
             self._connect_subgroup(group)
     except BaseException:
         objecttools.augment_excmessage(
             'While trying to build the node connection of the `%s` '
             'sequences of the model handled by element `%s`' %
             (group[:-1], objecttools.devicename(self)))
Esempio n. 6
0
 def __call__(self, *args, **kwargs):
     try:
         args = [timetools.Period(args[0]).seconds]
     except BaseException:
         objecttools.augment_excmessage(
             'While trying the set the value of parameter `maxdt` '
             'of the lake model handled by element `%s`' %
             objecttools.devicename(self),
             '(An example: set `max dt` to 3600 seconds by writing '
             '`maxdt("1h"))')
     super().__call__(*args, **kwargs)
Esempio n. 7
0
 def __getattribute__(self, key):
     try:
         return object.__getattribute__(self, key)
     except AttributeError:
         pass
     try:
         std_key = self._standardize_key(key)
     except AttributeError:
         raise AttributeError(
             'Parameter `%s` of element `%s` does not have an attribute '
             'named `%s` and the name `%s` is also not a valid threshold '
             'value identifier.' %
             (self.name, objecttools.devicename(self.subpars), key, key))
     if std_key in self._coefs:
         return self._coefs[std_key]
     else:
         raise AttributeError(
             'Parameter `%s` of element `%s` does not have an attribute '
             'attribute named `%s` nor an arma model corresponding to a '
             'threshold value named `%s`.' %
             (self.name, objecttools.devicename(
                 self.subpars), key, std_key))
Esempio n. 8
0
def _trim_int_nd(self, lower, upper):
    if lower is None:
        lower = _INT_NAN
    lower = numpy.full(self.shape, lower, dtype=int)
    if upper is None:
        upper = -_INT_NAN
    upper = numpy.full(self.shape, upper, dtype=int)
    idxs = numpy.where(self.values == _INT_NAN)
    self[idxs] = lower[idxs]
    if numpy.any(self.values < lower) or numpy.any(self.values > upper):
        raise ValueError(
            'At least one value of parameter `%s` of element `%s` is not '
            'valid.' % (self.name, objecttools.devicename(self)))
    self[idxs] = _INT_NAN
Esempio n. 9
0
 def __setattr__(self, key, value):
     if self._has_predefined_attr(key):
         object.__setattr__(self, key, value)
     else:
         std_key = self._standardize_key(key)
         try:
             try:
                 self._coefs[std_key] = value.arma.coefs
             except AttributeError:
                 self._coefs[std_key] = (tuple(float(v) for v in value[0]),
                                         tuple(float(v) for v in value[1]))
         except BaseException:
             objecttools.augment_excmessage(
                 'While trying to set a new threshold (%s) coefficient '
                 'pair for parameter `%s` of element `%s`' %
                 (key, self.name, objecttools.devicename(self.subpars)))
Esempio n. 10
0
 def _standardize_key(self, key):
     try:
         tuple_ = str(key).split('_')
         if (len(tuple_) > 1) and tuple_[-2].isdigit():
             integer = int(tuple_[-2])
             decimal = int(tuple_[-1])
         else:
             integer = int(tuple_[-1])
             decimal = 0
         return '_'.join(('th', str(integer), str(decimal)))
     except BaseException:
         raise AttributeError(
             'To define different response functions for parameter `%s` of '
             'element `%s`, one has to pass them as keyword arguments or '
             'set them as additional attributes.  The used name must meet '
             'a specific format (see the documentation for further '
             'information).  The given name `%s` does not meet this format.'
             % (self.name, objecttools.devicename(self.subpars), key))
Esempio n. 11
0
 def comparison_function(self, other):
     try:
         method = getattr(self.value, method_string)
     except AttributeError:
         # in Python 2.7, `int` (but not `float`) defines
         # `__cmp__` instead of rich comparisons
         method = getattr(float(self.value), method_string)
     try:
         if isinstance(other, abctools.VariableABC):
             result = method(other.value)
         else:
             result = method(other)
         if result is NotImplemented:
             return result
         try:
             return aggregation_func(result)
         except TypeError:
             return result
     except BaseException:
         objecttools.augment_excmessage(
             'While trying to compare variable `{0!r}` of '
             'element `{1}` with object `{2}` of type `{3}`'
             .format(self, objecttools.devicename(self),
                     other, objecttools.classname(other)))
Esempio n. 12
0
 def _setshape(self, shape):
     raise AttributeError(
         'The shape of parameter `%s` cannot be '
         'changed, but this was attempted for element `%s`.' %
         (self.name, objecttools.devicename(self)))