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 getExposure(self): """Construct a test exposure. The test exposure has a simple WCS set, as well as a list of unlikely header keywords that can be removed during ISR processing to exercise that code. Returns ------- exposure : `lsst.afw.exposure.Exposure` Construct exposure containing masked image of the appropriate size. """ camera = self.getCamera() detector = camera[self.config.detectorIndex] image = afwUtils.makeImageFromCcd(detector, isTrimmed=self.config.isTrimmed, showAmpGain=False, rcMarkSize=0, binSize=1, imageFactory=afwImage.ImageF) var = afwImage.ImageF(image.getDimensions()) mask = afwImage.Mask(image.getDimensions()) image.assign(0.0) maskedImage = afwImage.makeMaskedImage(image, mask, var) exposure = afwImage.makeExposure(maskedImage) exposure.setDetector(detector) exposure.setWcs(self.getWcs()) visitInfo = afwImage.VisitInfo(exposureTime=self.config.expTime, darkTime=self.config.darkTime) exposure.getInfo().setVisitInfo(visitInfo) metadata = exposure.getMetadata() metadata.add("SHEEP", 7.3, "number of sheep on farm") metadata.add("MONKEYS", 155, "monkeys per tree") metadata.add("VAMPIRES", 4, "How scary are vampires.") ccd = exposure.getDetector() newCcd = ccd.rebuild() newCcd.clear() for amp in ccd: newAmp = amp.rebuild() newAmp.setLinearityCoeffs((0., 1., 0., 0.)) newAmp.setLinearityType("Polynomial") newAmp.setGain(self.config.gain) newAmp.setSuspectLevel(25000.0) newAmp.setSaturation(32000.0) newCcd.append(newAmp) exposure.setDetector(newCcd.finish()) exposure.image.array[:] = np.zeros(exposure.getImage().getDimensions()).transpose() exposure.mask.array[:] = np.zeros(exposure.getMask().getDimensions()).transpose() exposure.variance.array[:] = np.zeros(exposure.getVariance().getDimensions()).transpose() return exposure
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 getExposure(self): r"""Construct a test exposure. The test exposure has a simple WCS set, as well as a list of unlikely header keywords that can be removed during ISR processing to exercise that code. Returns ------- exposure : `lsst.afw.exposure.Exposure` Construct exposure containing masked image of the appropriate size. """ camera = self.getCamera() detector = camera[self.config.detectorIndex] image = afwUtils.makeImageFromCcd(detector, isTrimmed=self.config.isTrimmed, showAmpGain=False, rcMarkSize=0, binSize=1, imageFactory=afwImage.ImageF) var = afwImage.ImageF(image.getDimensions()) mask = afwImage.Mask(image.getDimensions()) image.assign(0.0) maskedImage = afwImage.makeMaskedImage(image, mask, var) exposure = afwImage.makeExposure(maskedImage) exposure.setDetector(detector) exposure.setWcs(self.getWcs()) visitInfo = afwImage.VisitInfo(exposureTime=self.config.expTime, darkTime=self.config.darkTime) exposure.getInfo().setVisitInfo(visitInfo) metadata = exposure.getMetadata() metadata.add("SHEEP", 7.3, "number of sheep on farm") metadata.add("MONKEYS", 155, "monkeys per tree") metadata.add("VAMPIRES", 4, "How scary are vampires.") for amp in exposure.getDetector(): amp.setLinearityCoeffs((0., 1., 0., 0.)) amp.setLinearityType("Polynomial") amp.setGain(self.config.gain) exposure.image.array[:] = np.zeros(exposure.getImage().getDimensions()).transpose() exposure.mask.array[:] = np.zeros(exposure.getMask().getDimensions()).transpose() exposure.variance.array[:] = np.zeros(exposure.getVariance().getDimensions()).transpose() return exposure
def makeAssemblyInput(isPerAmp, doTrim=False): '''!Make the input to pass to the assembly task \param[in] isPerAmp -- If True, return a dictionary of amp exposures keyed by amp name. If False, return a single exposure with amps mosaiced preserving non-science pixels (e.g. overscan) \param[in doTrim -- Trim out the non-data pixels (e.g. overscan)? Ignored if isPerAmp is True. \return Either a dictionary of amp exposures or an exposure contining the mosaiced amps. ''' #number of amps in x and y nAmpX = 3 nAmpY = 3 #number of science pixels in each amp in x and y nPixX = 512 nPixY = 1024 #number of prescan rows pre = 4 #number of horizontal overscan columns hOscan = 10 #number of vertical overscan rows vOscan = 15 #number of pixels in the extended register ext = 1 detector = createDetector(nAmpX, nAmpY, nPixX, nPixY, pre, hOscan, vOscan, ext, isPerAmp) print " \n\n *********** Inside makeAssemblyInput -- detector = ", detector #mssg if isPerAmp: print " \n ****************** isPerAmp = True, About to call makeAmpInput(detector) " # print " makeAmpInput(detector) = ", makeAmpInput(detector) return makeAmpInput(detector) else: print " \n ****************** isPerAmp = F, About to call camGeomUtils " im = cameraGeomUtils.makeImageFromCcd(detector, isTrimmed=doTrim, imageFactory=afwImage.ImageF) print " \n ********* img = ", im ccdAssemblyInput = makeExpFromIm(im, detector) print " \n ********* initial ccdAssemblyInput = ", ccdAssemblyInput ccdAssemblyInput.setDetector(detector) print " \n ********** final ccdAssemblyInput = ", ccdAssemblyInput return ccdAssemblyInput
def trimCcd(ccd, ccdImage=""): """Trim a Ccd and maybe the image of the untrimmed Ccd""" if ccdImage == "": ccdImage = cameraGeomUtils.makeImageFromCcd(ccd) if ccd.isTrimmed(): return ccdImage ccd.setTrimmed(True) if ccdImage is not None: trimmedImage = ccdImage.Factory(ccd.getAllPixels().getDimensions()) for a in ccd: data = ccdImage.Factory(ccdImage, a.getDataSec(False)) tdata = trimmedImage.Factory(trimmedImage, a.getDataSec()) tdata[:] = data else: trimmedImage = None return trimmedImage
def makeAssemblyInput(isPerAmp, doTrim=False): '''!Make the input to pass to the assembly task \param[in] isPerAmp -- If True, return a dictionary of amp exposures keyed by amp name. If False, return a single exposure with amps mosaiced preserving non-science pixels (e.g. overscan) \param[in doTrim -- Trim out the non-data pixels (e.g. overscan)? Ignored if isPerAmp is True. \return Either a dictionary of amp exposures or an exposure contining the mosaiced amps. ''' #number of amps in x and y nAmpX = 3 nAmpY = 2 #number of science pixels in each amp in x and y nPixX = 512 nPixY = 1024 #number of prescan rows pre = 4 #number of horizontal overscan columns hOscan = 10 #number of vertical overscan rows vOscan = 15 #number of pixels in the extended register ext = 1 detector = createDetector(nAmpX, nAmpY, nPixX, nPixY, pre, hOscan, vOscan, ext, isPerAmp) if isPerAmp: return makeAmpInput(detector) else: im = cameraGeomUtils.makeImageFromCcd(detector, isTrimmed=doTrim, imageFactory=afwImage.ImageF) ccdAssemblyInput = makeExpFromIm(im, detector) ccdAssemblyInput.setDetector(detector) return ccdAssemblyInput
def trimCcd(ccd, ccdImage=""): """Trim a Ccd and maybe the image of the untrimmed Ccd""" if ccdImage == "": ccdImage = cameraGeomUtils.makeImageFromCcd(ccd) if ccd.isTrimmed(): return ccdImage ccd.setTrimmed(True) if ccdImage is not None: trimmedImage = ccdImage.Factory(ccd.getAllPixels()) for a in ccd: data = ccdImage.Factory(ccdImage, a.getDataSec(False), afwImage.LOCAL) tdata = trimmedImage.Factory(trimmedImage, a.getDataSec(), afwImage.LOCAL) tdata <<= data else: trimmedImage = None """ if trimmedImage: trimmedImage = afwMath.rotateImageBy90(trimmedImage, ccd.getOrientation().getNQuarter()) """ return trimmedImage
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 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())