def testBBox(self): """Check that we can measure a single Gaussian's attributes.""" print("BBoxTest") sigma0 = 5 size = [50, 60, 70, 80] for i in range(4): record = self.mycatalog.getTable().makeRecord() psf = measAlg.DoubleGaussianPsf(size[i], size[i], sigma0, 1.00, 0.0) record.setPsf(psf) wcs = afwImage.makeWcs(self.crval, self.crpix, self.cd11, self.cd12, self.cd21, self.cd22) record.setWcs(wcs) record['weight'] = 1.0 * (i + 1) record['id'] = i bbox = afwGeom.Box2I(afwGeom.Point2I(0, 0), afwGeom.Extent2I(2000, 2000)) record.setBBox(bbox) self.mycatalog.append(record) mypsf = measAlg.CoaddPsf(self.mycatalog, self.wcsref, 'weight') self.assertEqual(mypsf.computeKernelImage().getBBox(), mypsf.computeBBox())
def testTicket2872(self): """Test that CoaddPsf.getAveragePosition() is always a position at which we can call computeImage(). """ cdelt = (0.2 * afwGeom.arcseconds).asDegrees() wcs = afwImage.makeWcs( afwCoord.IcrsCoord(afwGeom.Point2D(45.0, 45.0), afwGeom.degrees), afwGeom.Point2D(50, 50), cdelt, 0.0, 0.0, cdelt) kernel = measAlg.DoubleGaussianPsf(7, 7, 2.0).getKernel() psf1 = measAlg.KernelPsf(kernel, afwGeom.Point2D(0, 50)) psf2 = measAlg.KernelPsf(kernel, afwGeom.Point2D(100, 50)) record1 = self.mycatalog.addNew() record1.setPsf(psf1) record1.setWcs(wcs) record1.setD(self.weightKey, 1.0) record1.setBBox( afwGeom.Box2I(afwGeom.Point2I(-40, 0), afwGeom.Point2I(40, 100))) record2 = self.mycatalog.addNew() record2.setPsf(psf2) record2.setWcs(wcs) record2.setD(self.weightKey, 1.0) record2.setBBox( afwGeom.Box2I(afwGeom.Point2I(60, 0), afwGeom.Point2I(140, 100))) coaddPsf = measAlg.CoaddPsf(self.mycatalog, wcs) naiveAvgPos = afwGeom.Point2D(50, 50) with self.assertRaises(pexExceptions.InvalidParameterError): coaddPsf.computeKernelImage(naiveAvgPos) # important test is that this doesn't throw: coaddPsf.computeKernelImage()
def testRotatePsf(self): """Check that we can create a CoaddPsf with 10 elements.""" print("RotatePsfTest") cdMatrix = afwGeom.makeCdMatrix( scale=5.55555555e-05 * lsst.geom.degrees, orientation=90 * lsst.geom.degrees, flipX=True, ) wcs = afwGeom.makeSkyWcs(crpix=self.crpix, crval=self.crval, cdMatrix=cdMatrix) # make a single record with an oblong Psf record = self.mycatalog.getTable().makeRecord() psf = makeBiaxialGaussianPsf(100, 100, 1.0, 6.0, 0.0) record.setPsf(psf) record.setWcs(wcs) record['weight'] = 1.0 record['id'] = 1 bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.Extent2I(2000, 2000)) record.setBBox(bbox) self.mycatalog.append(record) mypsf = measAlg.CoaddPsf(self.mycatalog, self.wcsref) m0, xbar, ybar, mxx, myy, x0, y0 = getPsfMoments( psf, lsst.geom.Point2D(0.25, 0.75)) cm0, cxbar, cybar, cmxx, cmyy, cx0, cy0 = getPsfMoments( mypsf, lsst.geom.Point2D(0.25, 0.75)) self.assertAlmostEqual(mxx, cmyy, delta=0.01) self.assertAlmostEqual(myy, cmxx, delta=0.01)
def testRotatePsf(self): """Check that we can create a CoaddPsf with 10 elements.""" print("RotatePsfTest") cd21 = 5.55555555e-05 cd12 = 5.55555555e-05 cd11 = 0.0 cd22 = 0.0 wcs = afwImage.makeWcs(self.crval, self.crpix, cd11, cd12, cd21, cd22) # make a single record with an oblong Psf record = self.mycatalog.getTable().makeRecord() psf = makeBiaxialGaussianPsf(100, 100, 1.0, 6.0, 0.0) record.setPsf(psf) record.setWcs(wcs) record['weight'] = 1.0 record['id'] = 1 bbox = afwGeom.Box2I(afwGeom.Point2I(0, 0), afwGeom.Extent2I(2000, 2000)) record.setBBox(bbox) self.mycatalog.append(record) mypsf = measAlg.CoaddPsf(self.mycatalog, self.wcsref) m0, xbar, ybar, mxx, myy, x0, y0 = getPsfMoments( psf, afwGeom.Point2D(0.25, 0.75)) cm0, cxbar, cybar, cmxx, cmyy, cx0, cy0 = getPsfMoments( mypsf, afwGeom.Point2D(0.25, 0.75)) self.assertAlmostEqual(mxx, cmyy, delta=0.01) self.assertAlmostEqual(myy, cmxx, delta=0.01)
def testBBox(self): """Check that computeBBox returns same BBox as realized Kernel Image and resized raises a Not Implemented Error""" sigma0 = 5 size = [50, 60, 70, 80] for i in range(4): record = self.mycatalog.getTable().makeRecord() psf = measAlg.DoubleGaussianPsf(size[i], size[i], sigma0, 1.00, 0.0) record.setPsf(psf) wcs = afwGeom.makeSkyWcs(crpix=self.crpix, crval=self.crval, cdMatrix=self.cdMatrix) record.setWcs(wcs) record['weight'] = 1.0 * (i + 1) record['id'] = i bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.Extent2I(2000, 2000)) record.setBBox(bbox) self.mycatalog.append(record) mypsf = measAlg.CoaddPsf(self.mycatalog, self.wcsref, 'weight') self.assertEqual(mypsf.computeKernelImage().getBBox(), mypsf.computeBBox()) with self.assertRaises(pexExceptions.LogicError): mypsf.resized(100, 100)
def testTicket2872(self): """Test that CoaddPsf.getAveragePosition() is always a position at which we can call computeImage(). """ scale = 0.2 * lsst.geom.arcseconds cdMatrix = afwGeom.makeCdMatrix(scale=scale) wcs = afwGeom.makeSkyWcs( crpix=lsst.geom.Point2D(50, 50), crval=lsst.geom.SpherePoint(45.0, 45.0, lsst.geom.degrees), cdMatrix=cdMatrix, ) kernel = measAlg.DoubleGaussianPsf(7, 7, 2.0).getKernel() psf1 = measAlg.KernelPsf(kernel, lsst.geom.Point2D(0, 50)) psf2 = measAlg.KernelPsf(kernel, lsst.geom.Point2D(100, 50)) record1 = self.mycatalog.addNew() record1.setPsf(psf1) record1.setWcs(wcs) record1.setD(self.weightKey, 1.0) record1.setBBox( lsst.geom.Box2I(lsst.geom.Point2I(-40, 0), lsst.geom.Point2I(40, 100))) record2 = self.mycatalog.addNew() record2.setPsf(psf2) record2.setWcs(wcs) record2.setD(self.weightKey, 1.0) record2.setBBox( lsst.geom.Box2I(lsst.geom.Point2I(60, 0), lsst.geom.Point2I(140, 100))) coaddPsf = measAlg.CoaddPsf(self.mycatalog, wcs) naiveAvgPos = lsst.geom.Point2D(50, 50) with self.assertRaises(pexExceptions.InvalidParameterError): coaddPsf.computeKernelImage(naiveAvgPos) # important test is that this doesn't throw: coaddPsf.computeKernelImage(coaddPsf.getAveragePosition())
def testDefaultSize(self): """Test of both default size and specified size.""" print("DefaultSizeTest") sigma0 = 5 # set the peak of the outer guassian to 0 so this is really a single gaussian. psf = measAlg.DoubleGaussianPsf(60, 60, 1.5 * sigma0, 1, 0.0) # Now make the catalog record = self.mycatalog.getTable().makeRecord() psf = measAlg.DoubleGaussianPsf(100, 100, 10.0, 1.00, 1.0) record.setPsf(psf) wcs = afwGeom.makeSkyWcs(crpix=self.crpix, crval=self.crval, cdMatrix=self.cdMatrix) record.setWcs(wcs) record['weight'] = 1.0 record['id'] = 1 bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.Extent2I(2000, 2000)) record.setBBox(bbox) self.mycatalog.append(record) mypsf = measAlg.CoaddPsf(self.mycatalog, self.wcsref) # , 'weight') m1coadd, m2coadd = getCoaddSecondMoments(mypsf, lsst.geom.Point2D(0, 0)) m1, m2 = getPsfSecondMoments(mypsf, lsst.geom.Point2D(1000, 1000)) self.assertAlmostEqual(m1, m1coadd, delta=.01) self.assertAlmostEqual(m2, m2coadd, delta=.01)
def testLargeTransform(self): """Test that images with bad astrometry are identified""" multiplier = 1000.0 # CD matrix multiplier for bad input badId = 1 # ID of bad input for ii in range(3): record = self.mycatalog.addNew() record.setPsf(measAlg.DoubleGaussianPsf(50, 50, 5.0, 1.00, 0.0)) cdMatrix = self.cdMatrix if ii == badId: # This image has bad astrometry: cdMatrix *= multiplier record['id'] = ii record['weight'] = 1.0 record.setWcs( afwGeom.makeSkyWcs(crpix=self.crpix, crval=self.crval, cdMatrix=cdMatrix)) record.setBBox( lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.Extent2I(2000, 2000))) coaddPsf = measAlg.CoaddPsf(self.mycatalog, self.wcsref) with self.assertRaises(pexExceptions.RangeError) as cm: coaddPsf.computeKernelImage() self.assertIn("id=%d" % (badId, ), str(cm.exception))
def test(self): schema = afwTable.ExposureTable.makeMinimalSchema() schema.addField("ccd", np.int32, doc="CCD number") schema.addField("visit", np.int32, doc="Visit number") schema.addField("goodpix", np.int32, doc="Number of good pixels") schema.addField("weight", float, doc="Weighting for this CCD") ccds = afwTable.ExposureCatalog(schema) wcs = afwImage.makeWcs(afwCoord.Coord(0.0*afwGeom.degrees, 0.0*afwGeom.degrees), afwGeom.Point2D(0.0, 0.0), 1.0e-4, 0.0, 0.0, 1.0e-4) new = ccds.addNew() new.set("id", 0) new.set("bbox_min_x", 0) new.set("bbox_min_y", 0) new.set("bbox_max_x", 1024) new.set("bbox_max_y", 1024) # The following lines are critical for reproducing the bug, because # the code is reading a double starting at the 'ccd' (offset 24), and # it sees a zero (from the zero in 'ccd' and the leading zeros in 'visit'). new.set("ccd", 0) new.set("visit", 6789) new.set("goodpix", 987654321) new.set("weight", 1.0) new.setPsf(measAlg.SingleGaussianPsf(23, 23, 2.345)) new.setWcs(wcs) # In the presence of the bug, the following fails with # lsst::pex::exceptions::RuntimeError thrown in src/CoaddPsf.cc # with message: "Could not find a valid average position for CoaddPsf" measAlg.CoaddPsf(ccds, wcs)
def test(self): schema = afwTable.ExposureTable.makeMinimalSchema() schema.addField("ccd", int, doc="CCD number") schema.addField("visit", long, doc="Visit number") schema.addField("goodpix", int, doc="Number of good pixels") schema.addField("weight", float, doc="Weighting for this CCD") ccds = afwTable.ExposureCatalog(schema) wcs = afwImage.makeWcs(afwCoord.Coord(0.0*afwGeom.degrees, 0.0*afwGeom.degrees), afwGeom.Point2D(0.0, 0.0), 1.0e-4, 0.0, 0.0, 1.0e-4) new = ccds.addNew() new.set("id", 0) new.set("bbox.min", afwGeom.Point2I(0,0)) new.set("bbox.max", afwGeom.Point2I(1024,1024)) # The following lines are critical for reproducing the bug, because # the code is reading a double starting at the 'ccd' (offset 24), and # it sees a zero (from the zero in 'ccd' and the leading zeros in 'visit'). new.set("ccd", 0) new.set("visit", 6789) new.set("goodpix", 987654321) new.set("weight", 1.0) new.setPsf(measAlg.SingleGaussianPsf(23, 23, 2.345)) new.setWcs(wcs) # In the presence of the bug, the following fails with: # LsstCppException: 0: lsst::pex::exceptions::RuntimeErrorException thrown at src/CoaddPsf.cc:134 in lsst::afw::geom::Point2D lsst::meas::algorithms::{anonymous}::computeAveragePosition(const ExposureCatalog&, const lsst::afw::image::Wcs&, lsst::afw::table::Key<double>) # 0: Message: Could not find a valid average position for CoaddPsf measAlg.CoaddPsf(ccds, wcs)
def testTicket2872(self): """Test that CoaddPsf.getAveragePosition() is always a position at which we can call computeImage(). """ schema = afwTable.ExposureTable.makeMinimalSchema() weightKey = schema.addField("weight", type=float, doc="photometric weight") catalog = afwTable.ExposureCatalog(schema) cdelt = (0.2 * afwGeom.arcseconds).asDegrees() wcs = afwImage.makeWcs( afwCoord.IcrsCoord(afwGeom.Point2D(45.0, 45.0), afwGeom.degrees), afwGeom.Point2D(50, 50), cdelt, 0.0, 0.0, cdelt) kernel = measAlg.DoubleGaussianPsf(7, 7, 2.0).getKernel() psf1 = measAlg.KernelPsf(kernel, afwGeom.Point2D(0, 50)) psf2 = measAlg.KernelPsf(kernel, afwGeom.Point2D(100, 50)) record1 = catalog.addNew() record1.setPsf(psf1) record1.setWcs(wcs) record1.setD(weightKey, 1.0) record1.setBBox( afwGeom.Box2I(afwGeom.Point2I(-40, 0), afwGeom.Point2I(40, 100))) record2 = catalog.addNew() record2.setPsf(psf2) record2.setWcs(wcs) record2.setD(weightKey, 1.0) record2.setBBox( afwGeom.Box2I(afwGeom.Point2I(60, 0), afwGeom.Point2I(140, 100))) coaddPsf = measAlg.CoaddPsf(catalog, wcs) naiveAvgPos = afwGeom.Point2D(50, 50) self.assertRaises(pexExceptions.InvalidParameterError, coaddPsf.computeKernelImage, naiveAvgPos) # important test is that this doesn't throw: coaddPsf.computeKernelImage()
def testValidPolygonPsf(self): """Demonstrate that we can use the validPolygon on Exposures in the CoaddPsf.""" # Create 9 separate records, each with its own peculiar Psf, Wcs, # weight, bounding box, and valid region. for i in range(1, 10): record = self.mycatalog.getTable().makeRecord() record.setPsf(measAlg.DoubleGaussianPsf(100, 100, i, 1.00, 0.0)) crpix = lsst.geom.PointD(1000 - 10.0 * i, 1000.0 - 10.0 * i) wcs = afwGeom.makeSkyWcs(crpix=crpix, crval=self.crval, cdMatrix=self.cdMatrix) record.setWcs(wcs) record['weight'] = 1.0 * (i + 1) record['id'] = i record.setBBox( lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.Extent2I(1000, 1000))) validPolygon = afwGeom.Polygon( lsst.geom.Box2D(lsst.geom.Point2D(0, 0), lsst.geom.Extent2D(i * 100, i * 100))) record.setValidPolygon(validPolygon) self.mycatalog.append(record) # Create the CoaddPsf and check at three different points to ensure that the validPolygon is working mypsf = measAlg.CoaddPsf(self.mycatalog, self.wcsref, 'weight') for position in [ lsst.geom.Point2D(50, 50), lsst.geom.Point2D(500, 500), lsst.geom.Point2D(850, 850) ]: m1coadd, m2coadd = getCoaddSecondMoments(mypsf, position, True) m1, m2 = getPsfSecondMoments(mypsf, position) self.assertAlmostEqual(m1, m1coadd, delta=0.01) self.assertAlmostEqual(m2, m2coadd, delta=0.01)
def testValidPolygonPsf(self): """Test that we can use the validPolygon on exposures in the coadd psf""" print "ValidPolygonTest" # this is the coadd Wcs we want cd11 = 5.55555555e-05 cd12 = 0.0 cd21 = 0.0 cd22 = 5.55555555e-05 crval1 = 0.0 crval2 = 0.0 crpix = afwGeom.PointD(1000, 1000) crval = afwCoord.Coord(afwGeom.Point2D(crval1, crval2)) wcsref = afwImage.makeWcs(crval, crpix, cd11, cd12, cd21, cd22) schema = afwTable.ExposureTable.makeMinimalSchema() schema.addField("weight", type="D", doc="Coadd weight") mycatalog = afwTable.ExposureCatalog(schema) # Each of the 9 has its peculiar Psf, Wcs, weight, bounding box, and valid region. for i in range(1, 10, 1): record = mycatalog.getTable().makeRecord() psf = measAlg.DoubleGaussianPsf(100, 100, i, 1.00, 0.0) record.setPsf(psf) crpix = afwGeom.PointD(1000 - 10.0 * i, 1000.0 - 10.0 * i) wcs = afwImage.makeWcs(crval, crpix, cd11, cd12, cd21, cd22) record.setWcs(wcs) record['weight'] = 1.0 * (i + 1) record['id'] = i bbox = afwGeom.Box2I(afwGeom.Point2I(0, 0), afwGeom.Extent2I(1000, 1000)) record.setBBox(bbox) validPolygon_bbox = afwGeom.Box2D( afwGeom.Point2D(0, 0), afwGeom.Extent2D(i * 100, i * 100)) validPolygon = Polygon(validPolygon_bbox) record.setValidPolygon(validPolygon) mycatalog.append(record) # Create the coaddpsf and check at three different points to ensure that the validPolygon is working mypsf = measAlg.CoaddPsf(mycatalog, wcsref, 'weight') m1coadd, m2coadd = getCoaddSecondMoments(mypsf, afwGeom.Point2D(50, 50), True) m1, m2 = getPsfSecondMoments(mypsf, afwGeom.Point2D(50, 50)) self.assertTrue(testRelDiff(m1, m1coadd, .01)) self.assertTrue(testRelDiff(m2, m2coadd, .01)) m1coadd, m2coadd = getCoaddSecondMoments(mypsf, afwGeom.Point2D(500, 500), True) m1, m2 = getPsfSecondMoments(mypsf, afwGeom.Point2D(500, 500)) self.assertTrue(testRelDiff(m1, m1coadd, .01)) self.assertTrue(testRelDiff(m2, m2coadd, .01)) m1coadd, m2coadd = getCoaddSecondMoments(mypsf, afwGeom.Point2D(850, 850), True) m1, m2 = getPsfSecondMoments(mypsf, afwGeom.Point2D(850, 850)) self.assertTrue(testRelDiff(m1, m1coadd, .01)) self.assertTrue(testRelDiff(m2, m2coadd, .01))
def testCreate(self): """Check that we can create a CoaddPsf with 9 elements""" print "CreatePsfTest" # this is the coadd Wcs we want cd11 = 5.55555555e-05 cd12 = 0.0 cd21 = 0.0 cd22 = 5.55555555e-05 crval1 = 0.0 crval2 = 0.0 crpix = afwGeom.PointD(1000, 1000) crval = afwCoord.Coord(afwGeom.Point2D(crval1, crval2)) wcsref = afwImage.makeWcs(crval, crpix, cd11, cd12, cd21, cd22) #also test that the weight field name is correctly observed schema = afwTable.ExposureTable.makeMinimalSchema() schema.addField("customweightname", type="D", doc="Coadd weight") mycatalog = afwTable.ExposureCatalog(schema) # Each of the 9 has its peculiar Psf, Wcs, weight, and bounding box. for i in range(1, 10, 1): record = mycatalog.getTable().makeRecord() psf = measAlg.DoubleGaussianPsf(100, 100, i, 1.00, 0.0) record.setPsf(psf) crpix = afwGeom.PointD(i * 1000.0, i * 1000.0) wcs = afwImage.makeWcs(crval, crpix, cd11, cd12, cd21, cd22) record.setWcs(wcs) record['customweightname'] = 1.0 * (i + 1) record['id'] = i bbox = afwGeom.Box2I(afwGeom.Point2I(0, 0), afwGeom.Extent2I(i * 1000, i * 1000)) record.setBBox(bbox) mycatalog.append(record) #create the coaddpsf mypsf = measAlg.CoaddPsf(mycatalog, wcsref, 'customweightname') # check to be sure that we got the right number of components, in the right order self.assertTrue(mypsf.getComponentCount() == 9) for i in range(1, 10, 1): wcs = mypsf.getWcs(i - 1) psf = mypsf.getPsf(i - 1) bbox = mypsf.getBBox(i - 1) weight = mypsf.getWeight(i - 1) id = mypsf.getId(i - 1) self.assertTrue(i == id) self.assertTrue(weight == 1.0 * (i + 1)) self.assertTrue(bbox.getBeginX() == 0) self.assertTrue(bbox.getBeginY() == 0) self.assertTrue(bbox.getEndX() == 1000 * i) self.assertTrue(bbox.getEndY() == 1000 * i) self.assertTrue(wcs.getPixelOrigin().getX() == (1000.0 * i)) self.assertTrue(wcs.getPixelOrigin().getY() == (1000.0 * i)) m0, xbar, ybar, mxx, myy, x0, y0 = getPsfMoments( psf, afwGeom.Point2D(0, 0)) self.assertTrue(testRelDiff(i * i, mxx, .01)) self.assertTrue(testRelDiff(i * i, myy, .01))
def _constructPsf(self, mapperResults, exposure): """Construct a CoaddPsf based on PSFs from individual subExposures Currently uses (and returns) a CoaddPsf. TBD if we want to create a custom subclass of CoaddPsf to differentiate it. Parameters ---------- mapperResults : `list` list of `pipeBase.Struct` returned by `ImageMapper.run`. For this to work, each element of `mapperResults` must contain a `subExposure` element, from which the component Psfs are extracted (thus the reducerTask cannot have `reduceOperation = 'none'`. exposure : `lsst.afw.image.Exposure` the original exposure which is used here solely for its bounding-box and WCS. Returns ------- psf : `lsst.meas.algorithms.CoaddPsf` A psf constructed from the PSFs of the individual subExposures. """ schema = afwTable.ExposureTable.makeMinimalSchema() schema.addField("weight", type="D", doc="Coadd weight") mycatalog = afwTable.ExposureCatalog(schema) # We're just using the exposure's WCS (assuming that the subExposures' # WCSs are the same, which they better be!). wcsref = exposure.getWcs() for i, res in enumerate(mapperResults): record = mycatalog.getTable().makeRecord() if 'subExposure' in res.getDict(): subExp = res.subExposure if subExp.getWcs() != wcsref: raise ValueError( 'Wcs of subExposure is different from exposure') record.setPsf(subExp.getPsf()) record.setWcs(subExp.getWcs()) record.setBBox(subExp.getBBox()) elif 'psf' in res.getDict(): record.setPsf(res.psf) record.setWcs(wcsref) record.setBBox(res.bbox) record['weight'] = 1.0 record['id'] = i mycatalog.append(record) # create the coaddpsf psf = measAlg.CoaddPsf(mycatalog, wcsref, 'weight') return psf
def testCreate(self): """Check that we can create a CoaddPsf with 9 elements.""" print("CreatePsfTest") # also test that the weight field name is correctly observed schema = afwTable.ExposureTable.makeMinimalSchema() schema.addField("customweightname", type="D", doc="Coadd weight") mycatalog = afwTable.ExposureCatalog(schema) # Each of the 9 has its peculiar Psf, Wcs, weight, and bounding box. for i in range(1, 10, 1): record = mycatalog.getTable().makeRecord() psf = measAlg.DoubleGaussianPsf(100, 100, i, 1.00, 0.0) record.setPsf(psf) crpix = lsst.geom.PointD(i * 1000.0, i * 1000.0) wcs = afwGeom.makeSkyWcs(crpix=crpix, crval=self.crval, cdMatrix=self.cdMatrix) record.setWcs(wcs) record['customweightname'] = 1.0 * (i + 1) record['id'] = i bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.Extent2I(i * 1000, i * 1000)) record.setBBox(bbox) mycatalog.append(record) # create the coaddpsf mypsf = measAlg.CoaddPsf(mycatalog, self.wcsref, 'customweightname') # check to be sure that we got the right number of components, in the right order self.assertEqual(mypsf.getComponentCount(), 9) for i in range(1, 10, 1): wcs = mypsf.getWcs(i - 1) psf = mypsf.getPsf(i - 1) bbox = mypsf.getBBox(i - 1) weight = mypsf.getWeight(i - 1) id = mypsf.getId(i - 1) self.assertEqual(i, id) self.assertEqual(weight, 1.0 * (i + 1)) self.assertEqual(bbox.getBeginX(), 0) self.assertEqual(bbox.getBeginY(), 0) self.assertEqual(bbox.getEndX(), 1000 * i) self.assertEqual(bbox.getEndY(), 1000 * i) self.assertAlmostEqual(wcs.getPixelOrigin().getX(), (1000.0 * i)) self.assertAlmostEqual(wcs.getPixelOrigin().getY(), (1000.0 * i)) m0, xbar, ybar, mxx, myy, x0, y0 = getPsfMoments( psf, lsst.geom.Point2D(0, 0)) self.assertAlmostEqual(i * i, mxx, delta=0.01) self.assertAlmostEqual(i * i, myy, delta=0.01)
def testSimpleGaussian(self): """Check that we can measure a single Gaussian's attributes.""" print("SimpleGaussianTest") sigma0 = 5 # set the peak of the outer guassian to 0 so this is really a single gaussian. psf = measAlg.DoubleGaussianPsf(60, 60, 1.5 * sigma0, 1, 0.0) sigma = [5, 6, 7, 8] # 5 pixels is the same as a sigma of 1 arcsec. # lay down a simple pattern of four ccds, set in a pattern of 1000 pixels around the center offsets = [(1999, 1999), (1999, 0), (0, 0), (0, 1999)] # Imagine a ccd in each of positions +-1000 pixels from the center for i in range(4): record = self.mycatalog.getTable().makeRecord() psf = measAlg.DoubleGaussianPsf(100, 100, sigma[i], 1.00, 1.0) record.setPsf(psf) crpix = afwGeom.PointD(offsets[i][0], offsets[i][1]) wcs = afwImage.makeWcs(self.crval, crpix, self.cd11, self.cd12, self.cd21, self.cd22) # print out the coorinates of this supposed 2000x2000 ccd in wcsref coordinates beginCoord = wcs.pixelToSky(0, 0) endCoord = wcs.pixelToSky(2000, 2000) self.wcsref.skyToPixel(beginCoord) self.wcsref.skyToPixel(endCoord) record.setWcs(wcs) record['weight'] = 1.0 record['id'] = i bbox = afwGeom.Box2I(afwGeom.Point2I(0, 0), afwGeom.Extent2I(2000, 2000)) record.setBBox(bbox) self.mycatalog.append(record) #img = psf.computeImage(afwGeom.Point2D(1000,1000), afwGeom.Extent2I(100,100), False, False) # img.writeFits("img%d.fits"%i) mypsf = measAlg.CoaddPsf(self.mycatalog, self.wcsref) # , 'weight') m1coadd, m2coadd = getCoaddSecondMoments(mypsf, afwGeom.Point2D(1000, 1000)) m1, m2 = getPsfSecondMoments(mypsf, afwGeom.Point2D(1000, 1000)) self.assertAlmostEqual(m1, m1coadd, delta=.01) m1, m2 = getPsfSecondMoments(mypsf, afwGeom.Point2D(1000, 1001)) m1coadd, m2coadd = getCoaddSecondMoments(mypsf, afwGeom.Point2D(1000, 1001)) self.assertAlmostEqual(m1, m1coadd, delta=0.01)
def testWeight(self): """Check that we can measure a single Gaussian's attributes.""" print("WeightTest") sigma0 = 5 # set the peak of the outer guassian to 0 so this is really a single gaussian. psf = measAlg.DoubleGaussianPsf(60, 60, 1.5 * sigma0, 1, 0.0) sigma = [5, 6, 7, 8] # 5 pixels is the same as a sigma of 1 arcsec. # lay down a simple pattern of four ccds, set in a pattern of 1000 pixels around the center offsets = [(1999, 1999), (1999, 0), (0, 0), (0, 1999)] # Imagine a ccd in each of positions +-1000 pixels from the center for i in range(4): record = self.mycatalog.getTable().makeRecord() psf = measAlg.DoubleGaussianPsf(100, 100, sigma[i], 1.00, 0.0) record.setPsf(psf) crpix = lsst.geom.PointD(offsets[i][0], offsets[i][1]) wcs = afwGeom.makeSkyWcs(crpix=crpix, crval=self.crval, cdMatrix=self.cdMatrix) # print out the coorinates of this supposed 2000x2000 ccd in wcsref coordinates record.setWcs(wcs) record['weight'] = 1.0 * (i + 1) record['id'] = i bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.Extent2I(2000, 2000)) record.setBBox(bbox) self.mycatalog.append(record) mypsf = measAlg.CoaddPsf(self.mycatalog, self.wcsref) # , 'weight') m1, m2 = getPsfSecondMoments(mypsf, lsst.geom.Point2D(1000, 1000)) m1coadd, m2coadd = getCoaddSecondMoments(mypsf, lsst.geom.Point2D(1000, 1000)) self.assertAlmostEqual(m1, m1coadd, delta=0.01) m1, m2 = getPsfSecondMoments(mypsf, lsst.geom.Point2D(1000, 1001)) m1coadd, m2coadd = getCoaddSecondMoments(mypsf, lsst.geom.Point2D(1000, 1001)) self.assertAlmostEqual(m1, m1coadd, delta=0.01) m1, m2 = getCoaddSecondMoments(mypsf, lsst.geom.Point2D(1001, 1000)) m1coadd, m2coadd = getCoaddSecondMoments(mypsf, lsst.geom.Point2D(1001, 1000)) self.assertAlmostEqual(m1, m1coadd, delta=0.01)
def getCoaddPsf(self, exposure): import lsst.afw.table as afwTable import lsst.afw.image as afwImage import lsst.afw.math as afwMath import lsst.afw.geom as afwGeom import lsst.meas.algorithms as measAlg schema = afwTable.ExposureTable.makeMinimalSchema() schema.addField("weight", type="D", doc="Coadd weight") mycatalog = afwTable.ExposureCatalog(schema) wcsref = exposure.getWcs() extentX = int(exposure.getWidth() * 0.05) extentY = int(exposure.getHeight() * 0.05) ind = 0 for x in np.linspace(extentX, exposure.getWidth() - extentX, 10): for y in np.linspace(extentY, exposure.getHeight() - extentY, 10): x = int(x) y = int(y) image = self.getImage(x, y) psf = afwImage.ImageD(image.shape[0], image.shape[1]) psf.getArray()[:, :] = image psfK = afwMath.FixedKernel(psf) psf = measAlg.KernelPsf(psfK) record = mycatalog.getTable().makeRecord() record.setPsf(psf) record.setWcs(wcsref) bbox = afwGeom.Box2I( afwGeom.Point2I( int(np.floor(x - extentX)) - 5, int(np.floor(y - extentY)) - 5), afwGeom.Point2I( int(np.floor(x + extentX)) + 5, int(np.floor(y + extentY)) + 5)) record.setBBox(bbox) record['weight'] = 1.0 record['id'] = ind ind += 1 mycatalog.append(record) # create the coaddpsf psf = measAlg.CoaddPsf(mycatalog, wcsref, 'weight') return psf
def testDefaultSize(self): """Test of both default size and specified size""" print "DefaultSizeTest" sigma0 = 5 # set the peak of the outer guassian to 0 so this is really a single gaussian. psf = measAlg.DoubleGaussianPsf(60, 60, 1.5 * sigma0, 1, 0.0) if False and display: im = psf.computeImage(afwGeom.PointD(xwid / 2, ywid / 2)) ds9.mtv(im, title="N(%g) psf" % sigma0, frame=0) # this is the coadd Wcs we want cd11 = 5.55555555e-05 cd12 = 0.0 cd21 = 0.0 cd22 = 5.55555555e-05 crval1 = 0.0 crval2 = 0.0 crpix = afwGeom.PointD(1000, 1000) crval = afwCoord.Coord(afwGeom.Point2D(crval1, crval2)) wcsref = afwImage.makeWcs(crval, crpix, cd11, cd12, cd21, cd22) # Now make the catalog schema = afwTable.ExposureTable.makeMinimalSchema() schema.addField("weight", type="D", doc="Coadd weight") mycatalog = afwTable.ExposureCatalog(schema) record = mycatalog.getTable().makeRecord() psf = measAlg.DoubleGaussianPsf(100, 100, 10.0, 1.00, 1.0) record.setPsf(psf) wcs = afwImage.makeWcs(crval, crpix, cd11, cd12, cd21, cd22) record.setWcs(wcs) record['weight'] = 1.0 record['id'] = 1 bbox = afwGeom.Box2I(afwGeom.Point2I(0, 0), afwGeom.Extent2I(2000, 2000)) record.setBBox(bbox) mycatalog.append(record) mypsf = measAlg.CoaddPsf(mycatalog, wcsref) #, 'weight') m1coadd, m2coadd = getCoaddSecondMoments(mypsf, afwGeom.Point2D(0, 0)) m1, m2 = getPsfSecondMoments(mypsf, afwGeom.Point2D(1000, 1000)) self.assertTrue(testRelDiff(m1, m1coadd, .01)) self.assertTrue(testRelDiff(m2, m2coadd, .01))
def testFractionalPixel(self): """Check that we can create a CoaddPsf with 10 elements""" print "FractionalPixelTest" # this is the coadd Wcs we want cd11 = 5.55555555e-05 cd12 = 0.0 cd21 = 0.0 cd22 = 5.55555555e-05 crval1 = 0.0 crval2 = 0.0 crpix = afwGeom.PointD(1000, 1000) crval = afwCoord.Coord(afwGeom.Point2D(crval1, crval2)) wcsref = afwImage.makeWcs(crval, crpix, cd11, cd12, cd21, cd22) cd21 = 5.55555555e-05 cd12 = 5.55555555e-05 cd11 = 0.0 cd22 = 0.0 wcs = afwImage.makeWcs(crval, crpix, cd11, cd12, cd21, cd22) schema = afwTable.ExposureTable.makeMinimalSchema() schema.addField("weight", type="D", doc="Coadd weight") mycatalog = afwTable.ExposureCatalog(schema) # make a single record with an oblong Psf record = mycatalog.getTable().makeRecord() psf = makeBiaxialGaussianPsf(100, 100, 6.0, 6.0, 0.0) record.setPsf(psf) record.setWcs(wcs) record['weight'] = 1.0 record['id'] = 1 bbox = afwGeom.Box2I(afwGeom.Point2I(0, 0), afwGeom.Extent2I(2000, 2000)) record.setBBox(bbox) mycatalog.append(record) mypsf = measAlg.CoaddPsf(mycatalog, wcsref) img = psf.computeImage(afwGeom.PointD(0.25, 0.75)) img = psf.computeImage(afwGeom.PointD(0.25, 0.75)) img = psf.computeImage(afwGeom.PointD(1000, 1000)) m0, xbar, ybar, mxx, myy, x0, y0 = getPsfMoments( psf, afwGeom.Point2D(0.25, 0.75)) cm0, cxbar, cybar, cmxx, cmyy, cx0, cy0 = getPsfMoments( mypsf, afwGeom.Point2D(0.25, 0.75)) self.assertTrue(testRelDiff(x0 + xbar, cx0 + cxbar, .01)) self.assertTrue(testRelDiff(y0 + ybar, cy0 + cybar, .01))
def testValidPolygonPsf(self): """Demonstrate that we can use the validPolygon on Exposures in the CoaddPsf""" cd11, cd12, cd21, cd22 = 5.55555555e-05, 0.0, 0.0, 5.55555555e-05 crval1, crval2 = 0.0, 0.0 crpix = afwGeom.PointD(1000, 1000) crval = afwCoord.Coord(afwGeom.Point2D(crval1, crval2)) wcsref = afwImage.makeWcs(crval, crpix, cd11, cd12, cd21, cd22) schema = afwTable.ExposureTable.makeMinimalSchema() schema.addField("weight", type="D", doc="Coadd weight") mycatalog = afwTable.ExposureCatalog(schema) # Create 9 separate records, each with its own peculiar Psf, Wcs, # weight, bounding box, and valid region. for i in range(1, 10): record = mycatalog.getTable().makeRecord() record.setPsf(measAlg.DoubleGaussianPsf(100, 100, i, 1.00, 0.0)) crpix = afwGeom.PointD(1000 - 10.0 * i, 1000.0 - 10.0 * i) wcs = afwImage.makeWcs(crval, crpix, cd11, cd12, cd21, cd22) record.setWcs(wcs) record['weight'] = 1.0 * (i + 1) record['id'] = i record.setBBox( afwGeom.Box2I(afwGeom.Point2I(0, 0), afwGeom.Extent2I(1000, 1000))) validPolygon = Polygon( afwGeom.Box2D(afwGeom.Point2D(0, 0), afwGeom.Extent2D(i * 100, i * 100))) record.setValidPolygon(validPolygon) mycatalog.append(record) # Create the CoaddPsf and check at three different points to ensure that the validPolygon is working mypsf = measAlg.CoaddPsf(mycatalog, wcsref, 'weight') for position in [ afwGeom.Point2D(50, 50), afwGeom.Point2D(500, 500), afwGeom.Point2D(850, 850) ]: m1coadd, m2coadd = getCoaddSecondMoments(mypsf, position, True) m1, m2 = getPsfSecondMoments(mypsf, position) self.assertAlmostEqual(m1, m1coadd, delta=0.01) self.assertAlmostEqual(m2, m2coadd, delta=0.01)
def testGoodPix(self): """Demonstrate that we can goodPix information in the CoaddPsf""" cd11, cd12, cd21, cd22 = 5.55555555e-05, 0.0, 0.0, 5.55555555e-05 crval1, crval2 = 0.0, 0.0 bboxSize = afwGeom.Extent2I(2000, 2000) crpix = afwGeom.PointD(bboxSize / 2.0) crval = afwCoord.Coord(afwGeom.Point2D(crval1, crval2)) wcsref = afwImage.makeWcs(crval, crpix, cd11, cd12, cd21, cd22) schema = afwTable.ExposureTable.makeMinimalSchema() schema.addField("weight", type="D", doc="Coadd weight") schema.addField("goodpix", type="I", doc="Number of good pixels") mycatalog = afwTable.ExposureCatalog(schema) # Create several records, each with its own peculiar center and numGoodPixels. # Each PSF has the same shape and size, and the position offsets are small # relative to the FWHM, in order to make it easy to predict the resulting # weighted mean position. xwsum = 0 ywsum = 0 wsum = 0 for i, (xOff, yOff, numGoodPix) in enumerate(( (30.0, -20.0, 25), (32.0, -21.0, 10), (28.0, -19.0, 30), )): xwsum -= xOff * numGoodPix ywsum -= yOff * numGoodPix wsum += numGoodPix record = mycatalog.getTable().makeRecord() record.setPsf(measAlg.DoubleGaussianPsf(25, 25, 10, 1.00, 0.0)) offPix = crpix + afwGeom.Extent2D(xOff, yOff) wcs = afwImage.makeWcs(crval, offPix, cd11, cd12, cd21, cd22) record.setWcs(wcs) record['weight'] = 1.0 record['id'] = i record['goodpix'] = numGoodPix record.setBBox(afwGeom.Box2I(afwGeom.Point2I(0, 0), bboxSize)) mycatalog.append(record) mypsf = measAlg.CoaddPsf(mycatalog, wcsref, 'weight') predPos = afwGeom.Point2D(xwsum / wsum, ywsum / wsum) self.assertPairsNearlyEqual(predPos, mypsf.getAveragePosition())
def testWeight(self): """Check that we can measure a single Gaussian's attributes""" print "WeightTest" sigma0 = 5 # set the peak of the outer guassian to 0 so this is really a single gaussian. psf = measAlg.DoubleGaussianPsf(60, 60, 1.5 * sigma0, 1, 0.0) if False and display: im = psf.computeImage(afwGeom.PointD(xwid / 2, ywid / 2)) ds9.mtv(im, title="N(%g) psf" % sigma0, frame=0) # this is the coadd Wcs we want cd11 = 5.55555555e-05 cd12 = 0.0 cd21 = 0.0 cd22 = 5.55555555e-05 crval1 = 0.0 crval2 = 0.0 crpix = afwGeom.PointD(1000, 1000) crval = afwCoord.Coord(afwGeom.Point2D(crval1, crval2)) wcsref = afwImage.makeWcs(crval, crpix, cd11, cd12, cd21, cd22) schema = afwTable.ExposureTable.makeMinimalSchema() schema.addField("weight", type="D", doc="Coadd weight") mycatalog = afwTable.ExposureCatalog(schema) sigma = [5, 6, 7, 8] # 5 pixels is the same as a sigma of 1 arcsec. # lay down a simple pattern of four ccds, set in a pattern of 1000 pixels around the center offsets = [(1999, 1999), (1999, 0), (0, 0), (0, 1999)] # Imagine a ccd in each of positions +-1000 pixels from the center for i in range(4): record = mycatalog.getTable().makeRecord() psf = measAlg.DoubleGaussianPsf(100, 100, sigma[i], 1.00, 0.0) record.setPsf(psf) crpix = afwGeom.PointD(offsets[i][0], offsets[i][1]) wcs = afwImage.makeWcs(crval, crpix, cd11, cd12, cd21, cd22) # print out the coorinates of this supposed 2000x2000 ccd in wcsref coordinates record.setWcs(wcs) record['weight'] = 1.0 * (i + 1) record['id'] = i bbox = afwGeom.Box2I(afwGeom.Point2I(0, 0), afwGeom.Extent2I(2000, 2000)) record.setBBox(bbox) mycatalog.append(record) mypsf = measAlg.CoaddPsf(mycatalog, wcsref) #, 'weight') m1, m2 = getPsfSecondMoments(mypsf, afwGeom.Point2D(1000, 1000)) m1coadd, m2coadd = getCoaddSecondMoments(mypsf, afwGeom.Point2D(1000, 1000)) self.assertTrue(testRelDiff(m1, m1coadd, .01)) m1, m2 = getPsfSecondMoments(mypsf, afwGeom.Point2D(1000, 1001)) m1coadd, m2coadd = getCoaddSecondMoments(mypsf, afwGeom.Point2D(1000, 1001)) self.assertTrue(testRelDiff(m1, m1coadd, .01)) m1, m2 = getCoaddSecondMoments(mypsf, afwGeom.Point2D(1001, 1000)) m1coadd, m2coadd = getCoaddSecondMoments(mypsf, afwGeom.Point2D(1001, 1000)) self.assertTrue(testRelDiff(m1, m1coadd, .01))
def mergeCoadd(a, dfgby_raveled, coaddTempExpDict_a, hits_skymap): from lsst.pipe.tasks.assembleCoadd import AssembleCoaddTask, AssembleCoaddConfig import lsst.afw.math as afwMath import lsst.afw.image as afwImage import lsst.coadd.utils as coaddUtils import lsst.meas.algorithms as measAlg config = AssembleCoaddConfig() assembleTask = AssembleCoaddTask(config=config) ccdsinPatch = len(dfgby_raveled) # len(dfgby.get_group(a)[0].ravel()) xInd = a[0] yInd = a[1] print xInd, yInd if xInd == 0 and yInd == 0: return None imageScalerRes = prepareInputs(coaddTempExpDict_a.values(), coaddTempExpDict_a.keys(), assembleTask) if imageScalerRes is None: return None mask = assembleTask.getBadPixelMask() statsCtrl = afwMath.StatisticsControl() statsCtrl.setNumSigmaClip(assembleTask.config.sigmaClip) statsCtrl.setNumIter(assembleTask.config.clipIter) statsCtrl.setAndMask(mask) statsCtrl.setNanSafe(True) statsCtrl.setWeighted(True) statsCtrl.setCalcErrorFromInputVariance(True) for plane, threshold in assembleTask.config.maskPropagationThresholds.items( ): bit = afwImage.MaskU.getMaskPlane(plane) statsCtrl.setMaskPropagationThreshold(bit, threshold) statsFlags = afwMath.MEAN skyInfo = getSkyInfo(hits_skymap, xInd, yInd) coaddExposure = afwImage.ExposureF(skyInfo.bbox, skyInfo.wcs) coaddExposure.setCalib(assembleTask.scaleZeroPoint.getCalib()) coaddExposure.getInfo().setCoaddInputs( assembleTask.inputRecorder.makeCoaddInputs()) # remember to set metadata if you want any hope of running detection and measurement on this coadd: # self.assembleMetadata(coaddExposure, tempExpRefList, weightList) # most important thing is the psf coaddExposure.setFilter(coaddTempExpDict_a.values()[0].getFilter()) coaddInputs = coaddExposure.getInfo().getCoaddInputs() for tempExp, weight in zip(coaddTempExpDict_a.values(), imageScalerRes.weightList): assembleTask.inputRecorder.addVisitToCoadd(coaddInputs, tempExp, weight) # takes numCcds as argument coaddInputs.ccds.reserve(ccdsinPatch) coaddInputs.visits.reserve(len(imageScalerRes.dataIdList)) psf = measAlg.CoaddPsf(coaddInputs.ccds, coaddExposure.getWcs()) coaddExposure.setPsf(psf) maskedImageList = afwImage.vectorMaskedImageF() coaddMaskedImage = coaddExposure.getMaskedImage() for dataId, imageScaler, exposure in zip(imageScalerRes.dataIdList, imageScalerRes.imageScalerList, coaddTempExpDict_a.values()): print dataId, imageScaler, exposure maskedImage = exposure.getMaskedImage() imageScaler.scaleMaskedImage(maskedImage) maskedImageList.append(maskedImage) maskedImage = afwMath.statisticsStack(maskedImageList, statsFlags, statsCtrl, imageScalerRes.weightList) coaddMaskedImage.assign(maskedImage, skyInfo.bbox) coaddUtils.setCoaddEdgeBits(coaddMaskedImage.getMask(), coaddMaskedImage.getVariance()) return coaddExposure
def main(): # try out one exposure #visits = ["0288935","0288976"] #,"0289893","0289913","0289931","0289614","0289818","0289820", "0289850","0289851","0289871","0289892", "0288935","0288976","0289016","0289056","0289161","0289202","0289243","0289284","0289368","0289409","0289450","0289493","0289573","0289656"] visits = ["0288976", "0288935"] ccds = [] exit(0) for i in range(1, 61): ccds.append(i) filterName = 'g' DATA_PATH = "/root/extra_home/lsst_data/" #spathprefix = "/home/dongfang/download/lsst_data/" spathprefix = DATA_PATH + "raw/" #calexpsloc = "/home/dongfang/download/lsst_data/calexps/" calexpsloc = DATA_PATH + "calexps/" #coaddloc = "/home/dongfang/download/lsst_data/coadds/" coaddloc = DATA_PATH + "coadds/" #mergecoaddloc = "/home/dongfang/download/lsst_data/merge/" mergecoaddloc = DATA_PATH + "merge/" # Characterize Image charImageConfig = CharacterizeImageConfig() charImage = CharacterizeImageTask() calibrateConfig = CalibrateConfig(doPhotoCal=False, doAstrometry=False) calibrateTask = CalibrateTask(config=calibrateConfig) makeCTEConfig = MakeCoaddTempExpConfig() makeCTE = MakeCoaddTempExpTask(config=makeCTEConfig) newSkyMapConfig = skymap.discreteSkyMap.DiscreteSkyMapConfig( projection='STG', decList=[-4.9325280994132905], patchInnerDimensions=[2000, 2000], radiusList=[4.488775723429071], pixelScale=0.333, rotation=0.0, patchBorder=100, raList=[154.10660740464786], tractOverlap=0.0) hits_skymap = skymap.discreteSkyMap.DiscreteSkyMap(config=newSkyMapConfig) tract = hits_skymap[0] coaddTempDict = {} calibResDict = {} f = open("log.txt", 'wb') start = datetime.datetime.now() #process CCDs to create calexps. for v in visits: for ccd in ccds: visit = int(v) filename = "instcal" + v + "." + str(ccd) + ".fits" calexpfn = calexpsloc + v + "/" + filename source = spathprefix + v + "/" + filename exposure = afwImg.ExposureF(source) try: # Characterize Image charRes = charImage.characterize(exposure, exposureIdInfo=None, background=None) except: f.write("DFZ DEBUG at charRes: errors in visit " + v + ", ccd " + str(ccd) + "\n") try: # Caliberate Image calibRes = calibrateTask.calibrate( charRes.exposure, exposureIdInfo=None, background=charRes.background, icSourceCat=None) except: f.write("DFZ DEBUG at calibRes: errors in visit " + v + ", ccd " + str(ccd) + "\n") try: #write out calexps calibRes.exposure.writeFits(calexpfn) #calbresDict.append((v,ccd),calibRes) except: f.write("DFZ DEBUG at calibRes.exposure: errors in visit " + v + ", ccd " + str(ccd) + "\n") end = datetime.datetime.now() d = end - start f.write("time for creating calexps: ") f.write(str(d.total_seconds())) f.write("\n") #time for creating co-add tempexps. start = datetime.datetime.now() # map calexps to patch-ids visit = visits[0] ccdsPerPatch = [] for ccd in ccds: filename = "instcal" + visit + "." + str(ccd) + ".fits" source = calexpsloc + visit + "/" + filename exposure = afwImg.ExposureF(source) bbox = exposure.getBBox() wcs = exposure.getWcs() corners = bbox.getCorners() xIndexMax, yIndexMax = tract.findPatch( wcs.pixelToSky(corners[0][0], corners[0][1])).getIndex() xIndexMin, yIndexMin = tract.findPatch( wcs.pixelToSky(corners[2][0], corners[2][1])).getIndex() yy = range(yIndexMin, yIndexMax + 1) xx = range(xIndexMin, xIndexMax + 1) for yIdx in yy: for xIdx in xx: ccdsPerPatch.append((ccd, (xIdx, yIdx))) print len(ccdsPerPatch) #import cPickle #cPickle.dump(open("ccdsinpatch.p",'wb'),ccdsPerPatch) # import cPickle # f = open("ccdsInPatch.p",'wb') # cPickle.dump(ccdsInPatch,f) #import cPickle #ccdsInPatch = cPickle.load(open("ccdsInPatch.p",'rb')) df = pd.DataFrame(ccdsPerPatch) dfgby = df.groupby(1) makeCTEConfig = MakeCoaddTempExpConfig() makeCTE = MakeCoaddTempExpTask(config=makeCTEConfig) coaddTempExpDict = {} for visit in visits: for a in dfgby.indices: coaddTempExpDict[a] = {} xInd = a[0] yInd = a[1] skyInfo = getSkyInfo(hits_skymap, xInd, yInd) v = int(visit) coaddTempExp = afwImage.ExposureF(skyInfo.bbox, skyInfo.wcs) coaddTempExp.getMaskedImage().set( numpy.nan, afwImage.MaskU.getPlaneBitMask("NO_DATA"), numpy.inf) totGoodPix = 0 didSetMetadata = False modelPsf = makeCTEConfig.modelPsf.apply( ) if makeCTEConfig.doPsfMatch else None setInputRecorder = False for b in dfgby.get_group(a)[0].ravel(): print a print b if not setInputRecorder: ccdsinPatch = len(dfgby.get_group(a)[0].ravel()) try: inputRecorder = makeCTE.inputRecorder.makeCoaddTempExpRecorder( v, ccdsinPatch) except: f.write("DFZ DEBUG at inputRecorder\n") setInputRecorder = True numGoodPix = 0 ccd = b filename = "instcal" + visit + "." + str(ccd) + ".fits" source = calexpsloc + visit + "/" + filename calExp = afwImg.ExposureF(source) ccdId = calExp.getId() warpedCcdExp = makeCTE.warpAndPsfMatch.run( calExp, modelPsf=modelPsf, wcs=skyInfo.wcs, maxBBox=skyInfo.bbox).exposure if didSetMetadata: mimg = calExp.getMaskedImage() mimg *= (coaddTempExp.getCalib().getFluxMag0()[0] / calExp.getCalib().getFluxMag0()[0]) del mimg numGoodPix = coaddUtils.copyGoodPixels( coaddTempExp.getMaskedImage(), warpedCcdExp.getMaskedImage(), makeCTE.getBadPixelMask()) totGoodPix += numGoodPix if numGoodPix > 0 and not didSetMetadata: coaddTempExp.setCalib(warpedCcdExp.getCalib()) coaddTempExp.setFilter(warpedCcdExp.getFilter()) didSetMetadata = True inputRecorder.addCalExp(calExp, ccdId, numGoodPix) ##### End loop over ccds here: inputRecorder.finish(coaddTempExp, totGoodPix) if totGoodPix > 0 and didSetMetadata: coaddTempExp.setPsf( modelPsf if makeCTEConfig.doPsfMatch else CoaddPsf( inputRecorder.coaddInputs.ccds, skyInfo.wcs)) coaddTempExpDict[a][v] = coaddTempExp coaddfilename = coaddloc + visit + "/" + "instcal" + visit + "." + str( xInd) + "_" + str(yInd) + ".fits" coaddTempExp.writeFits(coaddfilename) end = datetime.datetime.now() d = end - start f.write("time for creating co-add tempexps:\n ") f.write(str(d.total_seconds())) f.write("\n") #DFZ: stop here exit(0) start = datetime.datetime.now() config = AssembleCoaddConfig() assembleTask = AssembleCoaddTask(config=config) mergcoadds = {} for a in dfgby.indices: ccdsinPatch = len(dfgby.get_group(a)[0].ravel()) xInd = a[0] yInd = a[1] imageScalerRes = prepareInputs(coaddTempExpDict[a].values(), coaddTempExpDict[a].keys(), assembleTask) mask = None doClip = False if mask is None: mask = assembleTask.getBadPixelMask() statsCtrl = afwMath.StatisticsControl() statsCtrl.setNumSigmaClip(assembleTask.config.sigmaClip) statsCtrl.setNumIter(assembleTask.config.clipIter) statsCtrl.setAndMask(mask) statsCtrl.setNanSafe(True) statsCtrl.setWeighted(True) statsCtrl.setCalcErrorFromInputVariance(True) for plane, threshold in assembleTask.config.maskPropagationThresholds.items( ): bit = afwImage.MaskU.getMaskPlane(plane) statsCtrl.setMaskPropagationThreshold(bit, threshold) if doClip: statsFlags = afwMath.MEANCLIP else: statsFlags = afwMath.MEAN coaddExposure = afwImage.ExposureF(skyInfo.bbox, skyInfo.wcs) coaddExposure.setCalib(assembleTask.scaleZeroPoint.getCalib()) coaddExposure.getInfo().setCoaddInputs( assembleTask.inputRecorder.makeCoaddInputs()) #remember to set metadata if you want any hope of running detection and measurement on this coadd: #self.assembleMetadata(coaddExposure, tempExpRefList, weightList) #most important thing is the psf coaddExposure.setFilter(coaddTempExpDict[a].values()[0].getFilter()) coaddInputs = coaddExposure.getInfo().getCoaddInputs() for tempExp, weight in zip(coaddTempExpDict[a].values(), imageScalerRes.weightList): assembleTask.inputRecorder.addVisitToCoadd(coaddInputs, tempExp, weight) #takes numCcds as argument coaddInputs.ccds.reserve(ccdsinPatch) coaddInputs.visits.reserve(len(imageScalerRes.dataIdList)) psf = measAlg.CoaddPsf(coaddInputs.ccds, coaddExposure.getWcs()) coaddExposure.setPsf(psf) maskedImageList = afwImage.vectorMaskedImageF() coaddMaskedImage = coaddExposure.getMaskedImage() for dataId, imageScaler, exposure in zip( imageScalerRes.dataIdList, imageScalerRes.imageScalerList, coaddTempExpDict[a].values()): print dataId, imageScaler, exposure maskedImage = exposure.getMaskedImage() imageScaler.scaleMaskedImage(maskedImage) maskedImageList.append(maskedImage) maskedImage = afwMath.statisticsStack(maskedImageList, statsFlags, statsCtrl, imageScalerRes.weightList) coaddMaskedImage.assign(maskedImage, skyInfo.bbox) coaddUtils.setCoaddEdgeBits(coaddMaskedImage.getMask(), coaddMaskedImage.getVariance()) # write out Coadd! mergefilename = mergecoaddloc + str(xInd) + "_" + str(yInd) + ".fits" mergcoadds[a] = coaddExposure coaddExposure.writeFits(mergefilename) end = datetime.datetime.now() d = end - start f.write("time for creating merged co-adds:\n ") f.write(str(d.total_seconds())) f.write("\n") start = datetime.datetime.now() config = DetectCoaddSourcesConfig() detectCoaddSources = DetectCoaddSourcesTask(config=config) for a in dfgby.indices: # Detect on Coadd exp = mergcoadds[a] detRes = detectCoaddSources.runDetection(exp, idFactory=None) end = datetime.datetime.now() d = end - start f.write("time for detecting sources:\n ") f.write(str(d.total_seconds())) f.close()