def integrate(self, diffractionData, calibrationData, lower, upper, num,
                  constraintLower, constraintUpper, doConstraint,
                  doPolarizationCorrection, P, typeOfIntegration,
                  typeOfConstraint, maskedPixelInfo):

        if typeOfIntegration == 'Q':
            if lower >= upper:
                raise Exception(
                    "Unable to integrate the intensity. The lower Q value must be less then the upper Q value"
                )

            if lower < 0:
                raise Exception(
                    "Unable to integrate intensity. The lower Q value must be larger then 0."
                )

            if upper > Transform.getMaxQ(calibrationData):
                raise Exception(
                    "Unable to integrate intensity. The upper Q value must be less then the largest possible Q value."
                )

            if num < 1:
                raise Exception(
                    "Unable to integrate intensity. The number of Q must be at least 1."
                )

        elif typeOfIntegration == '2theta':
            if lower >= upper:
                raise Exception(
                    "Unable to integrate the intensity. The lower 2theta value must be less then the upper 2theta value"
                )

            if lower < 0:
                raise Exception(
                    "Unable to integrate intensity. The lower 2theta value must be larger then 0."
                )

            if upper > 90:
                raise Exception(
                    "Unable to integrate intensity. The upper 2theta value must be smaller then 90."
                )

            if num < 1:
                raise Exception(
                    "Unable to integrate intensity. The number of 2theta must be at least 1."
                )

        elif typeOfIntegration == 'chi':
            if lower >= upper:
                raise Exception(
                    "Unable to integrate the intensity. The lower chi value must be less then the upper chi value"
                )

            if lower < -360:
                raise Exception(
                    "Unable to integrate intensity. The lower chi value must be larger then -360 degrees."
                )

            if upper > +360:
                raise Exception(
                    "Unable to integrate intensity. The upper chi value must be lower then 360 degrees."
                )

            if upper - lower > 360:
                raise Exception(
                    "Unable to integrate intensity. The chi range can be at most 360 degrees."
                )

            if num < 1:
                raise Exception(
                    "Unable to integrate intensity. The number of chi must be at least 1."
                )
        else:
            raise Exception(
                "Unable to integrate intensity. The function integrate must be passed for the parameter typeOfIntegration either 'Q', '2theta', or 'chi'"
            )

        if doConstraint:
            if (typeOfIntegration == 'Q' or typeOfIntegration == '2theta') and \
                typeOfConstraint == 'chi':

                if constraintLower >= constraintUpper:
                    raise Exception(
                        "Unable to integrate the intensity. The constraint lower chi value must be less then the upper chi value"
                    )

                if constraintLower < -360:
                    raise Exception(
                        "Unable to integrate intensity. The constraint lower chi value must be larger then -360 degrees."
                    )

                if constraintUpper > +360:
                    raise Exception(
                        "Unable to integrate intensity. The constraint upper chi value must be lower then 360 degrees."
                    )

                if constraintUpper - constraintLower > 360:
                    raise Exception(
                        "Unable to integrate intensity. The constraint chi range can be at most 360 degrees."
                    )
            elif (typeOfIntegration == 'chi' and typeOfConstraint == 'Q'):

                if constraintLower >= constraintUpper:
                    raise Exception(
                        "Unable to integrate the intensity. The constraint lower Q value must be less then the upper Q value"
                    )

                if constraintLower < 0:
                    raise Exception(
                        "Unable to integrate intensity. The constraint lower Q value must be larger then 0."
                    )

                if constraintUpper > Transform.getMaxQ(calibrationData):
                    raise Exception(
                        "Unable to integrate intensity. The constraint upper Q value must be less then the largest possible Q value."
                    )

            elif (typeOfIntegration == 'chi' and typeOfConstraint == '2theta'):

                if constraintLower >= constraintUpper:
                    raise Exception(
                        "Unable to integrate the intensity. The constraint lower 2theta value must be less then the upper 2theta value"
                    )

                if constraintLower < 0:
                    raise Exception(
                        "Unable to integrate intensity. The constraint lower 2theta value must be larger then 0."
                    )

                if constraintUpper > 90:
                    raise Exception(
                        "Unable to integrate intensity. The constraint upper 2theta value must be smaller then 90."
                    )

            else:
                raise Exception(
                    "Constraint must be of the from chi, 2theta, or Q. Also, the constraint must be different from the type of integration."
                )

        if maskedPixelInfo.doLessThanMask:
            lessThanMask = maskedPixelInfo.lessThanMask
        else:
            # We can just send the function a bunch of junk since it won't be used
            lessThanMask = -1

        if maskedPixelInfo.doGreaterThanMask:
            greaterThanMask = maskedPixelInfo.greaterThanMask
        else:
            greaterThanMask = -1

        if maskedPixelInfo.doPolygonMask:
            polygonsX = maskedPixelInfo.polygonsX
            polygonsY = maskedPixelInfo.polygonsY
            polygonBeginningsIndex = maskedPixelInfo.polygonBeginningsIndex
            polygonNumberOfItems = maskedPixelInfo.polygonNumberOfItems
        else:
            polygonsX = Numeric.array([])
            polygonsY = Numeric.array([])
            polygonBeginningsIndex = Numeric.array([])
            polygonNumberOfItems = Numeric.array([])

        # use the wraped C code to do the caking
        self.values, self.intensityData = DiffractionAnalysisWrap.integrate(
            diffractionData,
            calibrationData.getCenterX()['val'],
            calibrationData.getCenterY()['val'],
            calibrationData.getDistance()['val'],
            calibrationData.getEnergy()['val'],
            calibrationData.getAlpha()['val'],
            calibrationData.getBeta()['val'],
            calibrationData.getRotation()['val'], lower, upper, num,
            constraintLower, constraintUpper, doConstraint,
            doPolarizationCorrection, P, maskedPixelInfo.doGreaterThanMask,
            greaterThanMask, maskedPixelInfo.doLessThanMask, lessThanMask,
            maskedPixelInfo.doPolygonMask, polygonsX, polygonsY,
            polygonBeginningsIndex, polygonNumberOfItems,
            calibrationData.getPixelLength()['val'],
            calibrationData.getPixelHeight()['val'], typeOfIntegration,
            typeOfConstraint)

        if type(self.intensityData) == type(None):
            raise Exception(
                "Unspecified Error occured while integrating the data")

        self.typeOfIntegration = typeOfIntegration
        self.lower = lower
        self.upper = upper
        self.num = num
        self.constraintLower = constraintLower
        self.constraintUpper = constraintUpper
        self.doConstraint = doConstraint
        self.doPolarizationCorrection = doPolarizationCorrection
        self.P = P

        # We should make a shallow copy so that the widget dose
        # not get copied over. Nevertheless, all the stuff we
        # care about are single values so they will get really copied over
        self.maskedPixelInfo = copy.copy(maskedPixelInfo)

        self.typeOfConstraint = typeOfConstraint
        self.calibrationData = copy.deepcopy(calibrationData)
    def integrate(self,diffractionData,calibrationData,lower,
            upper,num,constraintLower,constraintUpper,doConstraint,
            doPolarizationCorrection,P,
            typeOfIntegration,typeOfConstraint,maskedPixelInfo):

        if typeOfIntegration == 'Q':
            if lower >= upper:
                raise Exception("Unable to integrate the intensity. The lower Q value must be less then the upper Q value")

            if lower < 0: 
                raise Exception("Unable to integrate intensity. The lower Q value must be larger then 0.")

            if upper > Transform.getMaxQ(calibrationData):
                raise Exception("Unable to integrate intensity. The upper Q value must be less then the largest possible Q value.")

            if num < 1:
                raise Exception("Unable to integrate intensity. The number of Q must be at least 1.")

        elif typeOfIntegration == '2theta':
            if lower >= upper:
                raise Exception("Unable to integrate the intensity. The lower 2theta value must be less then the upper 2theta value")

            if lower < 0: 
                raise Exception("Unable to integrate intensity. The lower 2theta value must be larger then 0.")

            if upper > 90:
                raise Exception("Unable to integrate intensity. The upper 2theta value must be smaller then 90.")

            if num < 1:
                raise Exception("Unable to integrate intensity. The number of 2theta must be at least 1.")

        elif typeOfIntegration == 'chi':
            if lower >= upper:
                raise Exception("Unable to integrate the intensity. The lower chi value must be less then the upper chi value")

            if lower < -360: 
                raise Exception("Unable to integrate intensity. The lower chi value must be larger then -360 degrees.")

            if upper > +360: 
                raise Exception("Unable to integrate intensity. The upper chi value must be lower then 360 degrees.")

            if upper - lower > 360:
                raise Exception("Unable to integrate intensity. The chi range can be at most 360 degrees.")

            if num < 1:
                raise Exception("Unable to integrate intensity. The number of chi must be at least 1.")
        else:
            raise Exception("Unable to integrate intensity. The function integrate must be passed for the parameter typeOfIntegration either 'Q', '2theta', or 'chi'")

        if doConstraint: 
            if (typeOfIntegration == 'Q' or typeOfIntegration == '2theta') and \
                typeOfConstraint == 'chi':

                if constraintLower >= constraintUpper:
                    raise Exception("Unable to integrate the intensity. The constraint lower chi value must be less then the upper chi value")

                if constraintLower < -360: 
                    raise Exception("Unable to integrate intensity. The constraint lower chi value must be larger then -360 degrees.")

                if constraintUpper > +360: 
                    raise Exception("Unable to integrate intensity. The constraint upper chi value must be lower then 360 degrees.")

                if constraintUpper - constraintLower > 360:
                    raise Exception("Unable to integrate intensity. The constraint chi range can be at most 360 degrees.")
            elif (typeOfIntegration == 'chi' and typeOfConstraint == 'Q'):

                if constraintLower >= constraintUpper:
                    raise Exception("Unable to integrate the intensity. The constraint lower Q value must be less then the upper Q value")

                if constraintLower < 0: 
                    raise Exception("Unable to integrate intensity. The constraint lower Q value must be larger then 0.")

                if constraintUpper > Transform.getMaxQ(calibrationData):
                    raise Exception("Unable to integrate intensity. The constraint upper Q value must be less then the largest possible Q value.")

            elif (typeOfIntegration == 'chi' and typeOfConstraint == '2theta'):

                if constraintLower >= constraintUpper:
                    raise Exception("Unable to integrate the intensity. The constraint lower 2theta value must be less then the upper 2theta value")

                if constraintLower < 0: 
                    raise Exception("Unable to integrate intensity. The constraint lower 2theta value must be larger then 0.")

                if constraintUpper > 90:
                    raise Exception("Unable to integrate intensity. The constraint upper 2theta value must be smaller then 90.")

            else:
                raise Exception("Constraint must be of the from chi, 2theta, or Q. Also, the constraint must be different from the type of integration.")


        if maskedPixelInfo.doLessThanMask:
            lessThanMask = maskedPixelInfo.lessThanMask
        else:
            # We can just send the function a bunch of junk since it won't be used
            lessThanMask = -1

        if maskedPixelInfo.doGreaterThanMask:
            greaterThanMask = maskedPixelInfo.greaterThanMask
        else:
            greaterThanMask = -1

        if maskedPixelInfo.doPolygonMask:
            polygonsX = maskedPixelInfo.polygonsX
            polygonsY = maskedPixelInfo.polygonsY
            polygonBeginningsIndex = maskedPixelInfo.polygonBeginningsIndex
            polygonNumberOfItems = maskedPixelInfo.polygonNumberOfItems
        else:
            polygonsX = Numeric.array([])
            polygonsY = Numeric.array([])
            polygonBeginningsIndex = Numeric.array([])
            polygonNumberOfItems = Numeric.array([])

        # use the wraped C code to do the caking
        self.values,self.intensityData = DiffractionAnalysisWrap.integrate(
                diffractionData,
                calibrationData.getCenterX()['val'],
                calibrationData.getCenterY()['val'],
                calibrationData.getDistance()['val'],
                calibrationData.getEnergy()['val'],
                calibrationData.getAlpha()['val'],
                calibrationData.getBeta()['val'],
                calibrationData.getRotation()['val'],
                lower, upper, num,
                constraintLower, constraintUpper,
                doConstraint,
                doPolarizationCorrection, P,
                maskedPixelInfo.doGreaterThanMask, greaterThanMask,
                maskedPixelInfo.doLessThanMask, lessThanMask,
                maskedPixelInfo.doPolygonMask,
                polygonsX,
                polygonsY,
                polygonBeginningsIndex,
                polygonNumberOfItems,
                calibrationData.getPixelLength()['val'],
                calibrationData.getPixelHeight()['val'],
                typeOfIntegration,
                typeOfConstraint)

        if type(self.intensityData) == type(None):
            raise Exception("Unspecified Error occured while integrating the data")

        self.typeOfIntegration = typeOfIntegration
        self.lower=lower
        self.upper=upper
        self.num=num
        self.constraintLower = constraintLower
        self.constraintUpper = constraintUpper
        self.doConstraint = doConstraint
        self.doPolarizationCorrection=doPolarizationCorrection
        self.P=P

        # We should make a shallow copy so that the widget dose
        # not get copied over. Nevertheless, all the stuff we
        # care about are single values so they will get really copied over
        self.maskedPixelInfo = copy.copy(maskedPixelInfo)

        self.typeOfConstraint = typeOfConstraint
        self.calibrationData=copy.deepcopy(calibrationData)