示例#1
0
    def compute(self):
        # *** IMPORTANT ***
        # Once someone figures out how to pass the tvariable object, to this
        # module none of the computation in this method is necessary

        # Check ports
        if not self.has_input('cdmsfile'):
            raise ModuleError(self, "'cdmsfile' is mandatory.")
        if not self.has_input('id'):
            raise ModuleError(self, "'id' is mandatory.")

        # Get input from ports
        cdmsfile = self.get_input('cdmsfile')
        id = self.get_input('id')
        axes = self.force_get_input('axes')  # None if no input
        axesOperations = self.force_get_input(
            'axesOperations')  # None if no input

        # Get the variable
        varType = self.getVarType(id, cdmsfile)
        if (varType == 'variable'):
            var = cdmsfile.__call__(id)
        elif (varType == 'axis'):
            varID = self.getAxisID(id)
            axis = getattr(cdmsfile, 'axes')[varID]
            var = MV2.array(axis)
            var.setAxis(0, axis)
        elif (varType == 'weighted-axis'):
            varID, axisID = self.getVarAndAxisID(id)
            var = cdmsfile.__call__(varID)
            var = genutil.getAxisWeightByName(var, axisID)
            var.id = varID + '_' + axisID + '_weight'
        else:
            var = None

        # Eval the variable with the axes
        if axes is not None and var is not None:
            try:
                kwargs = eval(axes)
                var = var(**kwargs)
            except:
                raise ModuleError(self, "Invalid 'axes' specification", axes)

        # Apply axes ops to the variable
        if axesOperations is not None:
            var = self.applyAxesOperations(var, axesOperations)

        self.set_output('variable', var)
示例#2
0
    def compute(self):
        # *** IMPORTANT ***
        # Once someone figures out how to pass the tvariable object, to this
        # module none of the computation in this method is necessary 
        
        # Check ports
        if not self.hasInputFromPort('cdmsfile'):
            raise ModuleError(self, "'cdmsfile' is mandatory.")
        if not self.hasInputFromPort('id'):
            raise ModuleError(self, "'id' is mandatory.")

        # Get input from ports
        cdmsfile = self.getInputFromPort('cdmsfile')
        id = self.getInputFromPort('id')
        axes = self.forceGetInputFromPort('axes') # None if no input
        axesOperations = self.forceGetInputFromPort('axesOperations') # None if no input

        # Get the variable
        varType = self.getVarType(id, cdmsfile)
        if (varType == 'variable'):
            var = cdmsfile.__call__(id)
        elif (varType == 'axis'):
            varID = self.getAxisID(id)            
            axis = getattr(cdmsfile, 'axes')[varID]
            var = MV2.array(axis)
            var.setAxis(0, axis)
        elif (varType == 'weighted-axis'):
            varID, axisID = self.getVarAndAxisID(id)
            var = cdmsfile.__call__(varID)            
            var = genutil.getAxisWeightByName(var, axisID)
            var.id = varID +'_' + axisID + '_weight'
        else:
            var = None

        # Eval the variable with the axes
        if axes is not None and var is not None:
            try:
                kwargs = eval(axes)
                var = var(**kwargs)
            except:
                raise ModuleError(self, "Invalid 'axes' specification", axes)

        # Apply axes ops to the variable
        if axesOperations is not None:
            var = self.applyAxesOperations(var, axesOperations)

        self.setResult('variable', var)
示例#3
0
    def compute(self):
        # *** IMPORTANT ***
        # Once someone figures out how to pass the tvariable object, to this
        # module none of the computation in this method is necessary

        # Check ports
        #        if not self.hasInputFromPort('cdmsfile'):
        #            raise ModuleError(self, "'cdmsfile' is mandatory.")
        #        if not self.hasInputFromPort('id'):
        #            raise ModuleError(self, "'id' is mandatory.")

        # Get input from ports
        if self.hasInputFromPort('inputVariable'):
            var = self.getInputFromPort('inputVariable')
        else:
            if self.hasInputFromPort('cdmsfile'):
                cdmsfile = self.getInputFromPort('cdmsfile')
            if self.hasInputFromPort('id'):
                id = self.getInputFromPort('id')
            # Get the variable
            varType = self.getVarType(id, cdmsfile)
            if (varType == 'variable'):
                var = cdmsfile.__call__(id)
            elif (varType == 'axis'):
                varID = self.getAxisID(id)
                axis = getattr(cdmsfile, 'axes')[varID]
                var = MV2.array(axis)
                var.setAxis(0, axis)
            elif (varType == 'weighted-axis'):
                varID, axisID = self.getVarAndAxisID(id)
                var = cdmsfile.__call__(varID)
                var = genutil.getAxisWeightByName(var, axisID)
                var.id = varID + '_' + axisID + '_weight'
            else:
                var = None

        axes = self.forceGetInputFromPort('axes')  # None if no input
        axesOperations = self.forceGetInputFromPort(
            'axesOperations')  # None if no input
        # Eval the variable with the axes
        if axes is not None and var is not None:
            try:
                var = eval("var(%s)" % axes)
            except Exception, e:
                raise ModuleError(self,
                                  "Invalid 'axes' specification: %s" % str(e))