def testRaft(self): """Test if we can build a Raft out of Ccds""" #print >> sys.stderr, "Skipping testRaft"; return raftId = cameraGeom.Id("Raft") raftInfo = {"ampSerial" : CameraGeomTestCase.ampSerial} raft = cameraGeomUtils.makeRaft(self.geomPolicy, raftId, raftInfo=raftInfo) if display: cameraGeomUtils.showRaft(raft) ds9.incrDefaultFrame() if False: print cameraGeomUtils.describeRaft(raft) self.assertEqual(raft.getAllPixels().getWidth(), raftInfo["width"]) self.assertEqual(raft.getAllPixels().getHeight(), raftInfo["height"]) for x, y, serial, cen in [( 0, 0, 5, (-1.01, -2.02)), (150, 250, 21, (-1.01, 0.0 )), (250, 250, 29, ( 1.01, 0.0 )), (300, 500, 42, ( 1.01, 2.02))]: det = raft.findDetectorPixel(afwGeom.Point2D(x, y)) ccd = cameraGeom.cast_Ccd(det) if False: print x, y, det.getId().getName(), \ ccd.findAmp(afwGeom.Point2I(150, 152), True).getId().getSerial() if False: # XXX self.assertEqual(ccd.findAmp(afwGeom.PointI(150, 152), True).getId().getSerial(), serial) for i in range(2): self.assertAlmostEqual(ccd.getCenter()[i], cen[i]) name = "C:0,2" self.assertEqual(raft.findDetector(cameraGeom.Id(name)).getId().getName(), name) self.assertEqual(raft.getSize()[0], raftInfo["widthMm"]) self.assertEqual(raft.getSize()[1], raftInfo["heightMm"]) # # Test mapping pixel <--> mm # ps = raft.getPixelSize() for ix, iy, x, y in [(102, 500, -1.01, 2.02), (306, 100, 1.01, -2.02), (306, 500, 1.01, 2.02), (356, 525, 1.51, 2.27), ]: pix = afwGeom.Point2I(ix, iy) # wrt raft LLC #position of pixel center pos = afwGeom.Point2D(x+ps/2., y+ps/2.) # wrt raft center #position of pixel lower left corner which is returned by getPositionFromPixel() posll = afwGeom.Point2D(x, y) # wrt raft center rpos = raft.getPixelFromPosition(pos) rpos = afwGeom.PointI(int(rpos.getX()), int(rpos.getY())) self.assertEqual(rpos, pix) self.assertEqual(raft.getPositionFromPixel(afwGeom.Point2D(pix[0], pix[1])), posll)
def testCamera(self): """Test if we can build a Camera out of Rafts""" #print >> sys.stderr, "Skipping testCamera"; return cameraInfo = {"ampSerial" : CameraGeomTestCase.ampSerial} camera = cameraGeomUtils.makeCamera(self.geomPolicy, cameraInfo=cameraInfo) if display: cameraGeomUtils.showCamera(camera, ) ds9.incrDefaultFrame() if False: print cameraGeomUtils.describeCamera(camera) self.assertEqual(camera.getAllPixels().getWidth(), cameraInfo["width"]) self.assertEqual(camera.getAllPixels().getHeight(), cameraInfo["height"]) for rx, ry, cx, cy, serial, cen in [(0, 0, 0, 0, 4, (-3.12, -2.02)), (0, 0, 150, 250, 20, (-3.12, 0.00)), (600, 300, 0, 0, 52, ( 1.10, -2.02)), (600, 300, 150, 250, 68, ( 1.10, 0.00)), ]: raft = cameraGeom.cast_Raft(camera.findDetectorPixel(afwGeom.PointD(rx, ry))) ccd = cameraGeom.cast_Ccd(raft.findDetectorPixel(afwGeom.Point2D(cx, cy))) if False: self.assertEqual(ccd.findAmp(afwGeom.PointI(153, 152), True).getId().getSerial(), serial) for i in range(2): self.assertAlmostEqual(ccd.getCenter()[i], cen[i]) name = "R:1,0" self.assertEqual(camera.findDetector(cameraGeom.Id(name)).getId().getName(), name) self.assertEqual(camera.getSize()[0], cameraInfo["widthMm"]) self.assertEqual(camera.getSize()[1], cameraInfo["heightMm"]) ps = raft.getPixelSize() # # Test mapping pixel <--> mm # for ix, iy, x, y in [(102, 500, -3.12, 2.02), (152, 525, -2.62, 2.27), (714, 500, 3.12, 2.02), ]: pix = afwGeom.PointD(ix, iy) # wrt raft LLC pos = afwGeom.PointD(x, y) # center of pixel wrt raft center posll = afwGeom.PointD(x, y) # llc of pixel wrt raft center self.assertEqual(camera.getPixelFromPosition(pos), pix) self.assertEqual(camera.getPositionFromPixel(pix), posll) # Check that we can find an Amp in the bowels of the camera ccdName = "C:0,0" amp = cameraGeomUtils.findAmp(camera, cameraGeom.Id(ccdName), 1, 2) self.assertEqual(amp.getId().getName(), "ID6") self.assertEqual(amp.getParent().getId().getName(), ccdName)
def testDefectBase(self): """Test DefectBases""" #print >> sys.stderr, "Skipping testDefectBase"; return defectsDict = cameraGeomUtils.makeDefects(self.geomPolicy) for ccdName in ("Defective", "Defective II"): ccd = cameraGeomUtils.makeCcd(self.geomPolicy, cameraGeom.Id(ccdName)) ccdImage = cameraGeomUtils.makeImageFromCcd(ccd) if ccdName == "Defective": # # Insert some defects into the Ccd # for x0, y0, x1, y1 in [ (34, 0, 35, 80 ), (34, 81, 34, 100), (180, 100, 182, 130), ]: bbox = afwGeom.Box2I(afwGeom.Point2I(x0, y0), afwGeom.Point2I(x1, y1)) bad = ccdImage.Factory(ccdImage, bbox, afwImage.LOCAL) bad.set(100) if display: ds9.mtv(ccdImage, title="Defects") cameraGeomUtils.showCcd(ccd, None) defects = [v for (k, v) in defectsDict.items() if k == ccd.getId()] if len(defects) == 0: contine elif len(defects) == 1: defects = defects[0] else: raise RuntimeError, ("Found more than one defect set for CCD %s" % ccd.getId()) ccd.setDefects(defects) if False: print "CCD (%s)" % ccd.getId() for a in ccd: print " ", a.getId(), [str(d.getBBox()) for d in a.getDefects()] if ccdName == "Defective" and display: for d in ccd.getDefects(): displayUtils.drawBBox(d.getBBox(), ctype=ds9.CYAN, borderWidth=1.5) for a in ccd: for d in a.getDefects(): displayUtils.drawBBox(d.getBBox(), ctype=ds9.YELLOW, borderWidth=1.0) ds9.incrDefaultFrame()
def cosmicRay(self, exposure, keepCRs=None): """Mask cosmic rays \param[in,out] exposure Exposure to process \param[in] keepCRs Don't interpolate over the CR pixels (defer to pex_config if None) """ import lsstDebug display = lsstDebug.Info(__name__).display displayCR = lsstDebug.Info(__name__).displayCR assert exposure, "No exposure provided" psf = exposure.getPsf() assert psf, "No psf provided" # Blow away old mask try: mask = exposure.getMaskedImage().getMask() crBit = mask.getMaskPlane("CR") mask.clearMaskPlane(crBit) except Exception: pass mi = exposure.getMaskedImage() bg = afwMath.makeStatistics(mi, afwMath.MEDIAN).getValue() if keepCRs is None: keepCRs = self.config.cosmicray.keepCRs try: crs = measAlg.findCosmicRays(mi, psf, bg, pexConfig.makePolicy(self.config.cosmicray), keepCRs) except Exception: if display: import lsst.afw.display.ds9 as ds9 ds9.mtv(exposure, title="Failed CR") raise num = 0 if crs is not None: mask = mi.getMask() crBit = mask.getPlaneBitMask("CR") afwDet.setMaskFromFootprintList(mask, crs, crBit) num = len(crs) if display and displayCR: import lsst.afw.display.ds9 as ds9 import lsst.afw.display.utils as displayUtils ds9.incrDefaultFrame() ds9.mtv(exposure, title="Post-CR") with ds9.Buffering(): for cr in crs: displayUtils.drawBBox(cr.getBBox(), borderWidth=0.55) self.log.info("Identified %s cosmic rays." % (num,))
def testCamera(self): """Test if we can build a Camera out of Rafts""" #print >> sys.stderr, "Skipping testCamera"; return cameraInfo = {"ampSerial" : CameraGeomTestCase.ampSerial} camera = cameraGeomUtils.makeCamera(self.geomPolicy, cameraInfo=cameraInfo) if display: cameraGeomUtils.showCamera(camera, ) ds9.incrDefaultFrame() if False: print cameraGeomUtils.describeCamera(camera) self.assertEqual(camera.getAllPixels().getWidth(), cameraInfo["width"]) self.assertEqual(camera.getAllPixels().getHeight(), cameraInfo["height"]) name = "R:1,0" self.assertEqual(camera.findDetector(cameraGeom.Id(name)).getId().getName(), name) self.assertEqual(camera.getSize().getMm()[0], cameraInfo["widthMm"]) self.assertEqual(camera.getSize().getMm()[1], cameraInfo["heightMm"]) # # Test mapping pixel <--> mm # for ix, iy, x, y in [(102, 500, -3.12, 2.02), (152, 525, -2.62, 2.27), (714, 500, 3.12, 2.02), ]: pix = afwGeom.PointD(ix, iy) # wrt raft LLC pos = cameraGeom.FpPoint(x, y) # center of pixel wrt raft center posll = cameraGeom.FpPoint(x, y) # llc of pixel wrt raft center # may need to restructure this since adding FpPoint if False: self.assertEqual(camera.getPixelFromPosition(pos), pix) # there is no unique mapping from a pixel to a focal plane position # ... the pixel could be on any ccd if False: self.assertEqual(camera.getPositionFromPixel(pix).getMm(), posll.getMm()) # Check that we can find an Amp in the bowels of the camera ccdName = "C:1,0" amp = cameraGeomUtils.findAmp(camera, cameraGeom.Id(ccdName), 1, 2) self.assertFalse(amp is None) self.assertEqual(amp.getId().getName(), "ID7") self.assertEqual(amp.getParent().getId().getName(), ccdName)
def testDefectBase(self): """Test DefectBases""" defectList = [] ccdImage = afwImage.MaskedImageF(250, 225) ccdImage.set(self.setVal, 0, self.setVal) # # Insert some defects into the Ccd # for x0, y0, x1, y1 in [ (34, 0, 35, 80), (34, 81, 34, 100), (180, 100, 182, 130), ]: bbox = afwGeom.Box2I(afwGeom.Point2I(x0, y0), afwGeom.Point2I(x1, y1)) defectList.append(measAlg.Defect(bbox)) bad = ccdImage.Factory(ccdImage, bbox, afwImage.LOCAL) bad.set(100) ipIsr.maskPixelsFromDefectList(ccdImage, defectList, maskName='BAD') mask = ccdImage.getMask() bitMask = mask.getPlaneBitMask('BAD') for d in defectList: bad = mask.Factory(mask, d.getBBox(), afwImage.LOCAL) self.assertTrue((bad.getArray() & bitMask == bitMask).all()) if display: ds9.mtv(ccdImage.getImage(), title="Defects") for d in defectList: displayUtils.drawBBox(d.getBBox(), ctype=ds9.CYAN, borderWidth=.5) ds9.incrDefaultFrame() ipIsr.interpolateDefectList(ccdImage, defectList, 2.) im = ccdImage.getImage() for d in defectList: intrp = im.Factory(im, d.getBBox()) expect = np.empty_like(intrp.getArray()) expect[:] = self.setVal self.assertImagesEqual(intrp, expect) if display: ds9.mtv(ccdImage.getImage(), title="Defects Interpolated") for d in defectList: displayUtils.drawBBox(d.getBBox(), ctype=ds9.CYAN, borderWidth=.5) ds9.incrDefaultFrame()
def testRaft(self): """Test if we can build a Raft out of Ccds""" #print >> sys.stderr, "Skipping testRaft"; return raftId = cameraGeom.Id("Raft") raftInfo = {"ampSerial" : CameraGeomTestCase.ampSerial} raft = cameraGeomUtils.makeRaft(self.geomPolicy, raftId, raftInfo=raftInfo) if display: cameraGeomUtils.showRaft(raft) ds9.incrDefaultFrame() if False: print cameraGeomUtils.describeRaft(raft) self.assertEqual(raft.getAllPixels().getWidth(), raftInfo["width"]) self.assertEqual(raft.getAllPixels().getHeight(), raftInfo["height"]) name = "C:0,2" self.assertEqual(raft.findDetector(cameraGeom.Id(name)).getId().getName(), name) self.assertEqual(raft.getSize().getMm()[0], raftInfo["widthMm"]) self.assertEqual(raft.getSize().getMm()[1], raftInfo["heightMm"]) # # Test mapping pixel <--> mm # ps = raft.getPixelSize() for ix, iy, x, y in [(102, 500, -1.01, 2.02), (306, 100, 1.01, -2.02), (306, 500, 1.01, 2.02), (356, 525, 1.51, 2.27), ]: pix = afwGeom.Point2I(ix, iy) # wrt raft LLC #position of pixel center pos = cameraGeom.FpPoint(x+ps/2., y+ps/2.) # wrt raft center #position of pixel lower left corner which is returned by getPositionFromPixel() posll = cameraGeom.FpPoint(x, y) # wrt raft center rpos = raft.getPixelFromPosition(pos) rpos = afwGeom.PointI(int(rpos.getX()), int(rpos.getY())) # need to rework cameraGeom since FpPoint changes. disable this for now if False: self.assertEqual(rpos, pix) # this test is no longer meaningful as pixel is specific to a detector xy0 if False: self.assertEqual(raft.getPositionFromPixel(afwGeom.Point2D(pix[0], pix[1])).getMm(), posll.getMm())
def cosmicray(self, exposure, psf): """Cosmic ray masking @param exposure Exposure to process @param psf PSF """ import lsstDebug display = lsstDebug.Info(__name__).display displayCR = lsstDebug.Info(__name__).displayCR assert exposure, "No exposure provided" assert psf, "No psf provided" # Blow away old mask try: mask = exposure.getMaskedImage().getMask() crBit = mask.getMaskPlane("CR") mask.clearMaskPlane(crBit) except: pass if display and displayCR: ds9.incrDefaultFrame() ds9.mtv(exposure, title="Pre-CR") policy = self.config['cosmicray'].getPolicy() mi = exposure.getMaskedImage() bg = afwMath.makeStatistics(mi, afwMath.MEDIAN).getValue() crs = measAlg.findCosmicRays(mi, psf, bg, policy, self._keepCRs) num = 0 if crs is not None: mask = mi.getMask() crBit = mask.getPlaneBitMask("CR") afwDet.setMaskFromFootprintList(mask, crs, crBit) num = len(crs) if display and displayCR: ds9.incrDefaultFrame() ds9.mtv(exposure, title="Post-CR") ds9.cmdBuffer.pushSize() for cr in crs: displayUtils.drawBBox(cr.getBBox(), borderWidth=0.55) ds9.cmdBuffer.popSize() self.log.log(self.log.INFO, "Identified %d cosmic rays." % num) return
def testDefectBase(self): """Test DefectBases""" defectList = measAlg.DefectListT() ccdImage = afwImage.MaskedImageF(250,225) ccdImage.set(self.setVal, 0, self.setVal) # # Insert some defects into the Ccd # for x0, y0, x1, y1 in [ (34, 0, 35, 80 ), (34, 81, 34, 100), (180, 100, 182, 130), ]: bbox = afwGeom.Box2I(afwGeom.Point2I(x0, y0), afwGeom.Point2I(x1, y1)) defectList.append(measAlg.Defect(bbox)) bad = ccdImage.Factory(ccdImage, bbox, afwImage.LOCAL) bad.set(100) ipIsr.maskPixelsFromDefectList(ccdImage, defectList, maskName='BAD') mask = ccdImage.getMask() bitMask = mask.getPlaneBitMask('BAD') for d in defectList: bad = mask.Factory(mask, d.getBBox(), afwImage.LOCAL) self.assertTrue((bad.getArray()&bitMask == bitMask).all()) if display: ds9.mtv(ccdImage.getImage(), title="Defects") for d in defectList: displayUtils.drawBBox(d.getBBox(), ctype=ds9.CYAN, borderWidth=.5) ds9.incrDefaultFrame() ipIsr.interpolateDefectList(ccdImage, defectList, 2.) im = ccdImage.getImage() for d in defectList: intrp = im.Factory(im, d.getBBox()) self.assertTrue((intrp.getArray() == self.setVal).all()) if display: ds9.mtv(ccdImage.getImage(), title="Defects Interpolated") for d in defectList: displayUtils.drawBBox(d.getBBox(), ctype=ds9.CYAN, borderWidth=.5) ds9.incrDefaultFrame()
def testRotatedCcd(self): """Test if we can build a Ccd out of Amps""" #print >> sys.stderr, "Skipping testRotatedCcd"; return ccdId = cameraGeom.Id("Rot. CCD") ccdInfo = {"ampSerial" : CameraGeomTestCase.ampSerial} ccd = cameraGeomUtils.makeCcd(self.geomPolicy, ccdId, ccdInfo=ccdInfo) ccd.setOrientation(cameraGeom.Orientation(1, 0.0, 0.0, 0.0)) if display: cameraGeomUtils.showCcd(ccd) ds9.incrDefaultFrame() # # Trim the CCD and try again # trimmedImage = trimCcd(ccd) if display: cameraGeomUtils.showCcd(ccd, trimmedImage) ds9.incrDefaultFrame()
def testRotatedCcd(self): """Test if we can build a Ccd out of Amps""" #print >> sys.stderr, "Skipping testRotatedCcd"; return ccdId = cameraGeom.Id(1, "Rot. CCD") ccdInfo = {"ampSerial" : CameraGeomTestCase.ampSerial} ccd = cameraGeomUtils.makeCcd(self.geomPolicy, ccdId, ccdInfo=ccdInfo) zero = 0.0*afwGeom.radians ccd.setOrientation(cameraGeom.Orientation(1, zero, zero, zero)) if display: cameraGeomUtils.showCcd(ccd) ds9.incrDefaultFrame() # # Trim the CCD and try again # trimmedImage = trimCcd(ccd) if display: cameraGeomUtils.showCcd(ccd, trimmedImage) ds9.incrDefaultFrame()
def testAssembleCcd(self): """Test if we can build a Ccd out of Amps""" #print >> sys.stderr, "Skipping testRotatedCcd"; return compImage = afwImage.ImageI(os.path.join(eups.productDir("afw"), "tests", "test_comp.fits.gz")) compImageTrimmed = afwImage.ImageI(os.path.join(eups.productDir("afw"), "tests", "test_comp_trimmed.fits.gz")) ccdId = cameraGeom.Id(1, "LsstLike") ccdInfo = {"ampSerial" : CameraGeomTestCase.ampSerial} ccd = cameraGeomUtils.makeCcd(self.geomPolicy, ccdId, ccdInfo=ccdInfo) # # Test assembly of images that require preparation for assembly (like # LSST images) # outImage = cameraGeomUtils.makeImageFromCcd(ccd, imageSource=LsstLikeImage(), isTrimmed=False, imageFactory=afwImage.ImageU) self.assertTrue(outImage==compImage) if display: cameraGeomUtils.showCcd(ccd, outImage) ds9.incrDefaultFrame() ccdId = cameraGeom.Id(1, "ScLike") ccdInfo = {"ampSerial" : CameraGeomTestCase.ampSerial} ccd = cameraGeomUtils.makeCcd(self.geomPolicy, ccdId, ccdInfo=ccdInfo) outImage = cameraGeomUtils.makeImageFromCcd(ccd, imageSource=ScLikeImage(), isTrimmed=False, imageFactory=afwImage.ImageU) self.assertTrue(outImage==compImage) if display: cameraGeomUtils.showCcd(ccd, outImage) ds9.incrDefaultFrame() # # Trim the CCD and try again # ccd.setTrimmed(True) ccdId = cameraGeom.Id(1, "LsstLike Trimmed") ccdInfo = {"ampSerial" : CameraGeomTestCase.ampSerial} ccd = cameraGeomUtils.makeCcd(self.geomPolicy, ccdId, ccdInfo=ccdInfo) outImage = cameraGeomUtils.makeImageFromCcd(ccd, imageSource=LsstLikeImage(), isTrimmed=True, imageFactory=afwImage.ImageU) self.assertTrue(outImage==compImage) if display: cameraGeomUtils.showCcd(ccd, outImage) ds9.incrDefaultFrame()
def testCcd(self): """Test if we can build a Ccd out of Amps""" #print >> sys.stderr, "Skipping testCcd"; return ccdId = cameraGeom.Id("CCD") ccdInfo = {"ampSerial" : CameraGeomTestCase.ampSerial} ccd = cameraGeomUtils.makeCcd(self.geomPolicy, ccdId, ccdInfo=ccdInfo) if display: cameraGeomUtils.showCcd(ccd) ds9.incrDefaultFrame() trimmedImage = cameraGeomUtils.makeImageFromCcd(ccd, isTrimmed=True) cameraGeomUtils.showCcd(ccd, trimmedImage, isTrimmed=True) ds9.incrDefaultFrame() for i in range(2): self.assertEqual(ccd.getSize().getMm()[i], ccdInfo["pixelSize"]*ccd.getAllPixels(True).getDimensions()[i]) self.assertEqual(ccd.getId().getName(), ccdInfo["name"]) self.assertEqual(ccd.getAllPixels().getWidth(), ccdInfo["width"]) self.assertEqual(ccd.getAllPixels().getHeight(), ccdInfo["height"]) self.assertEqual([a.getId().getSerial() for a in ccd], range(ccdInfo["ampIdMin"], ccdInfo["ampIdMax"] + 1)) id = cameraGeom.Id("ID%d" % ccdInfo["ampIdMax"]) self.assertTrue(ccd.findAmp(id), id) self.assertEqual(ccd.findAmp(afwGeom.Point2I(10, 10)).getId().getSerial(), ccdInfo["ampIdMin"]) self.assertEqual(ccd.getAllPixels().getMin(), ccd.findAmp(afwGeom.Point2I(10, 10)).getAllPixels().getMin()) self.assertEqual(ccd.getAllPixels().getMax(), ccd.findAmp(afwGeom.Point2I(ccdInfo["width"] - 1, ccdInfo["height"] - 1)).getAllPixels().getMax()) ps = ccd.getPixelSize() # # Test mapping pixel <--> mm. Use a pixel at the middle of the top of the CCD # pix = afwGeom.Point2D(99.5, 203.5) # wrt bottom left pos = cameraGeom.FpPoint(0.00, 1.02) # pixel center wrt CCD center posll = cameraGeom.FpPoint(0.00, 1.02) # llc of pixel wrt CCD center # # Map pix into untrimmed coordinates # amp = ccd.findAmp(afwGeom.Point2I(int(pix[0]), int(pix[1]))) corrI = amp.getDataSec(False).getMin() - amp.getDataSec(True).getMin() corr = afwGeom.Extent2D(corrI.getX(), corrI.getY()) pix += corr self.assertEqual(amp.getDiskCoordSys(), cameraGeom.Amp.AMP) self.assertEqual(ccd.getPixelFromPosition(pos) + corr, pix) # # Trim the CCD and try again # trimmedImage = trimCcd(ccd) if display: ds9.mtv(trimmedImage, title='Trimmed') cameraGeomUtils.showCcd(ccd, trimmedImage) ds9.incrDefaultFrame() a = ccd.findAmp(cameraGeom.Id("ID%d" % ccdInfo["ampIdMin"])) self.assertEqual(a.getDataSec(), afwGeom.Box2I(afwGeom.Point2I(0, 0), afwGeom.Extent2I(ccdInfo["ampWidth"], ccdInfo["ampHeight"]))) self.assertEqual(ccd.getSize().getMm()[0], ccdInfo["pixelSize"]*ccdInfo["trimmedWidth"]) self.assertEqual(ccd.getSize().getMm()[1], ccdInfo["pixelSize"]*ccdInfo["trimmedHeight"]) # # Test mapping pixel <--> mm # pix = afwGeom.Point2D(99.5, 203.5) # wrt bottom left pos = cameraGeom.FpPoint(0.00, 1.02) # pixel center wrt CCD center posll = cameraGeom.FpPoint(0.00, 1.02) # llc of pixel wrt CCD center self.assertEqual(ccd.getPixelFromPosition(pos), pix) self.assertEqual(ccd.getPositionFromPixel(pix).getMm(), posll.getMm())
def testCameraGeomUtils(self): if not display: print "display variable not set; skipping cameraGeomUtils test" return for cw in self.cameraList: camera = cw.camera cameraGeomUtils.showCamera(camera, referenceDetectorName=camera[0].getName()) ds9.incrDefaultFrame() for det in (camera[0], camera[1]): cameraGeomUtils.showCcd(det, isTrimmed=True, inCameraCoords=False) ds9.incrDefaultFrame() cameraGeomUtils.showCcd(det, isTrimmed=True, inCameraCoords=True) ds9.incrDefaultFrame() cameraGeomUtils.showCcd(det, isTrimmed=False, inCameraCoords=False) ds9.incrDefaultFrame() cameraGeomUtils.showCcd(det, isTrimmed=False, inCameraCoords=True) ds9.incrDefaultFrame() for amp in det: cameraGeomUtils.showAmp(amp) ds9.incrDefaultFrame()
def cosmicRay(self, exposure, keepCRs=None): """Mask cosmic rays \param[in,out] exposure Exposure to process \param[in] keepCRs Don't interpolate over the CR pixels (defer to pex_config if None) """ import lsstDebug display = lsstDebug.Info(__name__).display displayCR = lsstDebug.Info(__name__).displayCR assert exposure, "No exposure provided" psf = exposure.getPsf() assert psf, "No psf provided" # Blow away old mask try: mask = exposure.getMaskedImage().getMask() crBit = mask.getMaskPlane("CR") mask.clearMaskPlane(crBit) except Exception: pass exposure0 = exposure # initial value of exposure binSize = self.config.cosmicray.background.binSize nx, ny = exposure.getWidth() / binSize, exposure.getHeight() / binSize # Treat constant background as a special case to avoid the extra complexity in calling # measAlg.SubtractBackgroundTask(). if nx * ny <= 1: medianBg = afwMath.makeStatistics(exposure.getMaskedImage(), afwMath.MEDIAN).getValue() modelBg = None else: # make a deep copy of the exposure before subtracting its background, # because this routine is only allowed to modify the exposure by setting mask planes # and interpolating over defects, not changing the background level exposure = exposure.Factory(exposure, True) subtractBackgroundTask = measAlg.SubtractBackgroundTask( config=self.config.cosmicray.background) modelBg = subtractBackgroundTask.run(exposure).background medianBg = 0.0 if keepCRs is None: keepCRs = self.config.cosmicray.keepCRs try: crs = measAlg.findCosmicRays( exposure.getMaskedImage(), psf, medianBg, pexConfig.makePolicy(self.config.cosmicray), keepCRs) if modelBg: # Add back background image img = exposure.getMaskedImage() img += modelBg.getImageF() del img # Replace original image with CR subtracted image exposure0.setMaskedImage(exposure.getMaskedImage()) except Exception: if display: import lsst.afw.display.ds9 as ds9 ds9.mtv(exposure0, title="Failed CR") raise num = 0 if crs is not None: mask = exposure0.getMaskedImage().getMask() crBit = mask.getPlaneBitMask("CR") afwDet.setMaskFromFootprintList(mask, crs, crBit) num = len(crs) if display and displayCR: import lsst.afw.display.ds9 as ds9 import lsst.afw.display.utils as displayUtils ds9.incrDefaultFrame() ds9.mtv(exposure0, title="Post-CR") with ds9.Buffering(): for cr in crs: displayUtils.drawBBox(cr.getBBox(), borderWidth=0.55) self.log.info("Identified %s cosmic rays." % (num, ))
def testAssembleCcd(self): """Test if we can build a Ccd out of Amps""" compImage = afwImage.ImageU(os.path.join(eups.productDir("afw"), "tests", "test_comp.fits.gz")) compImageTrimmed = afwImage.ImageU(os.path.join(eups.productDir("afw"), "tests", "test_comp_trimmed.fits.gz")) ccdId = cameraGeom.Id(1, "LsstLike") ccdInfo = {"ampSerial" : CameraGeomTestCase.ampSerial} ccd = cameraGeomUtils.makeCcd(self.geomPolicy, ccdId, ccdInfo=ccdInfo) # # Test assembly of images that require preparation for assembly (like # LSST images) # outImage = cameraGeomUtils.makeImageFromCcd(ccd, imageSource=LsstLikeImage(), isTrimmed=False, imageFactory=afwImage.ImageU) self.assertImagesAreEqual(outImage, compImage) if display: cameraGeomUtils.showCcd(ccd, outImage) ds9.incrDefaultFrame() # # Test assembly of images that reside in a pre-assembled state from # the DAQ (like Suprime-Cam images) # ccdId = cameraGeom.Id(1, "ScLike") ccdInfo = {"ampSerial" : CameraGeomTestCase.ampSerial} ccd = cameraGeomUtils.makeCcd(self.geomPolicy, ccdId, ccdInfo=ccdInfo) outImage = cameraGeomUtils.makeImageFromCcd(ccd, imageSource=ScLikeImage(), isTrimmed=False, imageFactory=afwImage.ImageU) self.assertImagesAreEqual(outImage, compImage) if display: cameraGeomUtils.showCcd(ccd, outImage) ds9.incrDefaultFrame() # # Do the same tests for trimmed ccds. # ccdId = cameraGeom.Id(1, "LsstLike") ccdInfo = {"ampSerial" : CameraGeomTestCase.ampSerial} ccd = cameraGeomUtils.makeCcd(self.geomPolicy, ccdId, ccdInfo=ccdInfo) outImage = cameraGeomUtils.makeImageFromCcd(ccd, imageSource=LsstLikeImage(), isTrimmed=True, imageFactory=afwImage.ImageU) ccd.setTrimmed(True) self.assertImagesAreEqual(outImage, compImageTrimmed) if display: cameraGeomUtils.showCcd(ccd, outImage) ds9.incrDefaultFrame() ccdId = cameraGeom.Id(1, "ScLike") ccdInfo = {"ampSerial" : CameraGeomTestCase.ampSerial} ccd = cameraGeomUtils.makeCcd(self.geomPolicy, ccdId, ccdInfo=ccdInfo) outImage = cameraGeomUtils.makeImageFromCcd(ccd, imageSource=ScLikeImage(), isTrimmed=True, imageFactory=afwImage.ImageU) ccd.setTrimmed(True) self.assertImagesAreEqual(outImage, compImageTrimmed) if display: cameraGeomUtils.showCcd(ccd, outImage) ds9.incrDefaultFrame()
def testCameraGeomUtils(self): for cw in self.cameraList: camera = cw.camera cameraGeomUtils.showCamera( camera, referenceDetectorName=camera[0].getName()) ds9.incrDefaultFrame() for det in (camera[0], camera[1]): cameraGeomUtils.showCcd( det, isTrimmed=True, inCameraCoords=False) ds9.incrDefaultFrame() cameraGeomUtils.showCcd( det, isTrimmed=True, inCameraCoords=True) ds9.incrDefaultFrame() cameraGeomUtils.showCcd( det, isTrimmed=False, inCameraCoords=False) ds9.incrDefaultFrame() cameraGeomUtils.showCcd( det, isTrimmed=False, inCameraCoords=True) ds9.incrDefaultFrame() for amp in det: cameraGeomUtils.showAmp(amp) ds9.incrDefaultFrame()
def cosmicRay(self, exposure, keepCRs=None): """Mask cosmic rays \param[in,out] exposure Exposure to process \param[in] keepCRs Don't interpolate over the CR pixels (defer to pex_config if None) """ import lsstDebug display = lsstDebug.Info(__name__).display displayCR = lsstDebug.Info(__name__).displayCR assert exposure, "No exposure provided" psf = exposure.getPsf() assert psf, "No psf provided" # Blow away old mask try: mask = exposure.getMaskedImage().getMask() crBit = mask.getMaskPlane("CR") mask.clearMaskPlane(crBit) except Exception: pass exposure0 = exposure # initial value of exposure binSize = self.config.cosmicray.background.binSize nx, ny = exposure.getWidth()/binSize, exposure.getHeight()/binSize # Treat constant background as a special case to avoid the extra complexity in calling # measAlg.SubtractBackgroundTask(). if nx*ny <= 1: medianBg = afwMath.makeStatistics(exposure.getMaskedImage(), afwMath.MEDIAN).getValue() modelBg = None else: # make a deep copy of the exposure before subtracting its background, # because this routine is only allowed to modify the exposure by setting mask planes # and interpolating over defects, not changing the background level exposure = exposure.Factory(exposure, True) subtractBackgroundTask = measAlg.SubtractBackgroundTask(config=self.config.cosmicray.background) modelBg = subtractBackgroundTask.run(exposure).background medianBg = 0.0 if keepCRs is None: keepCRs = self.config.cosmicray.keepCRs try: crs = measAlg.findCosmicRays(exposure.getMaskedImage(), psf, medianBg, pexConfig.makePolicy(self.config.cosmicray), keepCRs) if modelBg: # Add back background image img = exposure.getMaskedImage() img += modelBg.getImageF() del img # Replace original image with CR subtracted image exposure0.setMaskedImage(exposure.getMaskedImage()) except Exception: if display: import lsst.afw.display.ds9 as ds9 ds9.mtv(exposure0, title="Failed CR") raise num = 0 if crs is not None: mask = exposure0.getMaskedImage().getMask() crBit = mask.getPlaneBitMask("CR") afwDet.setMaskFromFootprintList(mask, crs, crBit) num = len(crs) if display and displayCR: import lsst.afw.display.ds9 as ds9 import lsst.afw.display.utils as displayUtils ds9.incrDefaultFrame() ds9.mtv(exposure0, title="Post-CR") with ds9.Buffering(): for cr in crs: displayUtils.drawBBox(cr.getBBox(), borderWidth=0.55) self.log.info("Identified %s cosmic rays." % (num,))