Ejemplo n.º 1
0
 def _parse_(self,value):
     ## if this is not an eval function (a string to interpret as a function)
     ## then construct the function dictionaries. otherwise, pass through
     if '=' in value:
         self._is_eval_function = True
         if EvalFunction.is_multivariate(value):
             eval_klass = MultivariateEvalFunction
         else:
             eval_klass = EvalFunction
         value = {'func':value,'ref':eval_klass}
     ## if it is not an eval function, then do the standard argument parsing
     if not self._is_eval_function:
         fr = register.FunctionRegistry()
         
         ## get the function key string form the calculation definition dictionary
         function_key = value['func']
         ## this is the message for the DefinitionValidationError if this key
         ## may not be found.
         dve_msg = 'The function key "{0}" is not available in the function registry.'.format(function_key)
         
         ## retrieve the calculation class reference from the function registry
         try:
             value['ref'] = fr[function_key]
         ## if the function cannot be found, it may be part of a contributed
         ## library of calculations not registered by default as the external
         ## library is an optional dependency.
         except KeyError:
             ## this will register the icclim indices.
             if function_key.startswith('{0}_'.format(constants.prefix_icclim_function_key)):
                 register.register_icclim(fr)
             else:
                 raise(DefinitionValidationError(self,dve_msg))
         ## make another attempt to register the function
         try:
             value['ref'] = fr[function_key]
         except KeyError:
             raise(DefinitionValidationError(self,dve_msg))
         
         ## parameters will be set to empty if none are present in the calculation
         ## dictionary.
         if 'kwds' not in value:
             value['kwds'] = OrderedDict()
         ## make the keyword parameter definitions lowercase.
         else:
             value['kwds'] = OrderedDict(value['kwds'])
             for k,v in value['kwds'].iteritems():
                 try:
                     value['kwds'][k] = v.lower()
                 except AttributeError:
                     pass
     return(value)
Ejemplo n.º 2
0
from flyingpigeon.utils import archiveextract
from flyingpigeon.utils import rename_complexinputs
from flyingpigeon.utils import GROUPING
from flyingpigeon.log import init_process_logger

import ocgis
from ocgis.calc.library import register
from ocgis.contrib import library_icclim as libclim
from collections import OrderedDict

import logging
LOGGER = logging.getLogger("PYWPS")

# Register ocgis functions, including icclim
fr = register.FunctionRegistry()
register.register_icclim(fr)
icclim_classes = [
    k for k in fr.keys() if isinstance(k, str) and k.startswith('icclim')
]


class IndicatorProcess(Process, object):
    """A Process class wrapping OCGIS functions."""
    key = 'to_be_subclassed'
    version = ocgis.__version__

    #################
    # Common inputs #
    #################
    resource_inputs = [
        ComplexInput(
Ejemplo n.º 3
0
 def test_register_icclim(self):
     fr = FunctionRegistry()
     self.assertNotIn('icclim_TG',fr)
     register_icclim(fr)
     self.assertIn('icclim_TG',fr)
     self.assertIn('icclim_vDTR',fr)
Ejemplo n.º 4
0
    def _parse_(self, value):
        # test if the value is an eval function and set internal flag
        if '=' in value:
            self._is_eval_function = True
        elif isinstance(value, dict) and value.get('func') is not None and '=' in value['func']:
            self._is_eval_function = True
        else:
            self._is_eval_function = False

        # format the output dictionary
        if self._is_eval_function:
            # select the function reference...
            try:
                eval_string = value['func']
            except TypeError:
                eval_string = value

            # determine if the eval string is multivariate
            if EvalFunction.is_multivariate(eval_string):
                eval_klass = MultivariateEvalFunction
            else:
                eval_klass = EvalFunction

            # reset the output dictionary
            new_value = {'func': eval_string, 'ref': eval_klass, 'name': None, 'kwds': OrderedDict()}
            # attempt to update the meta_attrs if they are present
            try:
                new_value.update({'meta_attrs': value['meta_attrs']})
            # attempting to index a string incorrectly
            except TypeError:
                new_value.update({'meta_attrs': None})
            # adjust the reference
            value = new_value

        # if it is not an eval function, then do the standard argument parsing
        else:

            # check for required keys
            if isinstance(value, dict):
                for key in self._required_keys_initial:
                    if key not in value:
                        msg = 'The key "{0}" is required for calculation dictionaries.'.format(key)
                        raise DefinitionValidationError(self, msg)

            fr = register.FunctionRegistry()

            # get the function key string form the calculation definition dictionary
            function_key = value['func']
            # this is the message for the DefinitionValidationError if this key may not be found.
            dve_msg = 'The function key "{0}" is not available in the function registry.'.format(function_key)

            # retrieve the calculation class reference from the function registry
            try:
                value['ref'] = fr[function_key]
            # if the function cannot be found, it may be part of a contributed library of calculations not registered by
            # default as the external library is an optional dependency.
            except KeyError:
                # this will register the icclim indices.
                if function_key.startswith('{0}_'.format(constants.ICCLIM_PREFIX_FUNCTION_KEY)):
                    register.register_icclim(fr)
                else:
                    raise DefinitionValidationError(self, dve_msg)
            # make another attempt to register the function
            try:
                value['ref'] = fr[function_key]
            except KeyError:
                raise DefinitionValidationError(self, dve_msg)

            # parameters will be set to empty if none are present in the calculation dictionary.
            if 'kwds' not in value:
                value['kwds'] = OrderedDict()
            # make the keyword parameter definitions lowercase.
            else:
                value['kwds'] = OrderedDict(value['kwds'])
                for k, v in value['kwds'].iteritems():
                    try:
                        value['kwds'][k] = v.lower()
                    except AttributeError:
                        pass

        # add placeholder for meta_attrs if it is not present
        if 'meta_attrs' not in value:
            value['meta_attrs'] = None
        else:
            # replace with the metadata attributes class if the attributes are not none
            ma = value['meta_attrs']
            if ma is not None:
                value['meta_attrs'] = MetadataAttributes(ma)

        return value
Ejemplo n.º 5
0
    def _parse_(self, value):
        # test if the value is an eval function and set internal flag
        if '=' in value:
            self._is_eval_function = True
        elif isinstance(value, dict) and value.get(
                'func') is not None and '=' in value['func']:
            self._is_eval_function = True
        else:
            self._is_eval_function = False

        # format the output dictionary
        if self._is_eval_function:
            # select the function reference...
            try:
                eval_string = value['func']
            except TypeError:
                eval_string = value

            # determine if the eval string is multivariate
            if EvalFunction.is_multivariate(eval_string):
                eval_klass = MultivariateEvalFunction
            else:
                eval_klass = EvalFunction

            # reset the output dictionary
            new_value = {
                'func': eval_string,
                'ref': eval_klass,
                'name': None,
                'kwds': OrderedDict()
            }
            # attempt to update the meta_attrs if they are present
            try:
                new_value.update({'meta_attrs': value['meta_attrs']})
            # attempting to index a string incorrectly
            except TypeError:
                new_value.update({'meta_attrs': None})
            # adjust the reference
            value = new_value

        # if it is not an eval function, then do the standard argument parsing
        else:

            # check for required keys
            if isinstance(value, dict):
                for key in self._required_keys_initial:
                    if key not in value:
                        msg = 'The key "{0}" is required for calculation dictionaries.'.format(
                            key)
                        raise DefinitionValidationError(self, msg)

            fr = register.FunctionRegistry()

            # get the function key string form the calculation definition dictionary
            function_key = value['func']
            # this is the message for the DefinitionValidationError if this key may not be found.
            dve_msg = 'The function key "{0}" is not available in the function registry.'.format(
                function_key)

            # retrieve the calculation class reference from the function registry
            try:
                value['ref'] = fr[function_key]
            # if the function cannot be found, it may be part of a contributed library of calculations not registered by
            # default as the external library is an optional dependency.
            except KeyError:
                # this will register the icclim indices.
                if function_key.startswith('{0}_'.format(
                        constants.ICCLIM_PREFIX_FUNCTION_KEY)):
                    register.register_icclim(fr)
                else:
                    raise DefinitionValidationError(self, dve_msg)
            # make another attempt to register the function
            try:
                value['ref'] = fr[function_key]
            except KeyError:
                raise DefinitionValidationError(self, dve_msg)

            # parameters will be set to empty if none are present in the calculation dictionary.
            if 'kwds' not in value:
                value['kwds'] = OrderedDict()
            # make the keyword parameter definitions lowercase.
            else:
                value['kwds'] = OrderedDict(value['kwds'])
                for k, v in value['kwds'].items():
                    try:
                        value['kwds'][k] = v.lower()
                    except AttributeError:
                        pass

        # add placeholder for meta_attrs if it is not present
        if 'meta_attrs' not in value:
            value['meta_attrs'] = None
        else:
            # replace with the metadata attributes class if the attributes are not none
            ma = value['meta_attrs']
            if ma is not None:
                value['meta_attrs'] = MetadataAttributes(ma)

        return value
Ejemplo n.º 6
0
 def test_register_icclim(self):
     fr = FunctionRegistry()
     self.assertNotIn('icclim_TG', fr)
     register_icclim(fr)
     self.assertIn('icclim_TG', fr)
     self.assertIn('icclim_vDTR', fr)