def getCoaddSecondMoments(coaddpsf, point, useValidPolygon=False): count = coaddpsf.getComponentCount() coaddWcs = coaddpsf.getCoaddWcs() weight_sum = 0.0 m1_sum = 0.0 m2_sum = 0.0 for i in range(count): wcs = coaddpsf.getWcs(i) psf = coaddpsf.getPsf(i) bbox = afwGeom.Box2D(coaddpsf.getBBox(i)) if useValidPolygon: validPolygon = coaddpsf.getValidPolygon(i) else: validPolygon = Polygon(bbox) point_rel = wcs.skyToPixel(coaddWcs.pixelToSky(afwGeom.Point2D(point))) if bbox.contains(point_rel) and validPolygon.contains(point_rel): weight = coaddpsf.getWeight(i) m0,xbar,ybar,mxx,myy,x0,y0 = getPsfMoments(psf, point) #, extent) m1_sum += mxx*weight m2_sum += myy*weight weight_sum += weight if weight_sum == 0.0: return 0,0 else: return m1_sum/weight_sum, m2_sum/weight_sum
def getCoaddSecondMoments(coaddpsf, point, useValidPolygon=False): count = coaddpsf.getComponentCount() coaddWcs = coaddpsf.getCoaddWcs() weight_sum = 0.0 m1_sum = 0.0 m2_sum = 0.0 for i in range(count): wcs = coaddpsf.getWcs(i) psf = coaddpsf.getPsf(i) bbox = afwGeom.Box2D(coaddpsf.getBBox(i)) if useValidPolygon: validPolygon = coaddpsf.getValidPolygon(i) else: validPolygon = Polygon(bbox) point_rel = wcs.skyToPixel(coaddWcs.pixelToSky(afwGeom.Point2D(point))) if bbox.contains(point_rel) and validPolygon.contains(point_rel): weight = coaddpsf.getWeight(i) m0, xbar, ybar, mxx, myy, x0, y0 = getPsfMoments(psf, point) #, extent) m1_sum += mxx * weight m2_sum += myy * weight weight_sum += weight if weight_sum == 0.0: return 0, 0 else: return m1_sum / weight_sum, m2_sum / weight_sum
def testSetPolygonIntersect(self): # Create a detector detector = DetectorWrapper().detector numPolygonPoints = 50 # Create an exposure with bounding box defined by detector exposure = afwImage.ExposureF(detector.getBBox()) exposure.setDetector(detector) pixelSizeMm = exposure.getDetector().getPixelSize()[0] pixX0 = exposure.getX0() pixY0 = exposure.getY0() pixX1 = pixX0 + exposure.getWidth() - 1 pixY1 = pixY0 + exposure.getHeight() - 1 fpCenter = exposure.getDetector().getCenter(FOCAL_PLANE).getPoint() fpCenterX = fpCenter[0] fpCenterY = fpCenter[1] pixCenter = exposure.getDetector().getCenter(PIXELS).getPoint() # Create an instance of IsrTask task = IsrTask() # Make a polygon that encompases entire ccd (radius of 2*max of width/height) fpRadius = 2.0*max(exposure.getWidth()*pixelSizeMm, exposure.getHeight()*pixelSizeMm) fpPolygon = makeCircularPolygon(fpCenterX, fpCenterY, fpRadius, numPolygonPoints) # Set the polygon that is the intersection of fpPolygon and ccd task.setValidPolygonIntersect(exposure, fpPolygon) # Since the ccd is fully contained in the fpPolygon, the intersection should be the ccdPolygon itself ccdPolygonPix = Polygon(exposure.getDetector().getCorners(PIXELS)) self.assertEqual(exposure.getInfo().getValidPolygon(), ccdPolygonPix) # Make a polygon that is entirely within, but smaller than, the ccd (radius of 0.2*min of width/height) fpRadius = 0.2*min(exposure.getWidth()*pixelSizeMm, exposure.getHeight()*pixelSizeMm) fpPolygon = makeCircularPolygon(fpCenterX, fpCenterY, fpRadius, numPolygonPoints) # Set the polygon that is the intersection of fpPolygon and ccd task.setValidPolygonIntersect(exposure, fpPolygon) # all vertices of polygon should be contained within the ccd for x in exposure.getInfo().getValidPolygon(): self.assertTrue(ccdPolygonPix.contains(afwGeom.Point2D(x))) # intersection is smaller than the ccd self.assertNotEqual(exposure.getInfo().getValidPolygon(), ccdPolygonPix) # make a simple square polygon that partly intersects the ccd, centered at ccd center fpPolygonSize = max(exposure.getWidth()*pixelSizeMm, exposure.getHeight()*pixelSizeMm) fpPolygon = makeSquarePolygon(fpCenterX, fpCenterY, fpPolygonSize) task.setValidPolygonIntersect(exposure, fpPolygon) # Check that the polygon contains the central pixel (offset by one to actually be "contained") pixCenterPlusOne = afwGeom.Point2D(pixCenter[0] + 1, pixCenter[1] + 1) self.assertTrue(exposure.getInfo().getValidPolygon().contains(afwGeom.Point2D(pixCenterPlusOne))) # Check that the polygon contains the upper right ccd edge self.assertTrue(exposure.getInfo().getValidPolygon().contains(afwGeom.Point2D(pixX1, pixY1)))