def testMakeTanSipMetadata(self): """Test makeTanSipMetadata """ crpix = lsst.geom.Point2D( self.metadata.getScalar("CRPIX1") - 1, self.metadata.getScalar("CRPIX2") - 1) crval = lsst.geom.SpherePoint(self.metadata.getScalar("CRVAL1"), self.metadata.getScalar("CRVAL2"), lsst.geom.degrees) cdMatrix = getCdMatrixFromMetadata(self.metadata) sipA = getSipMatrixFromMetadata(self.metadata, "A") sipB = getSipMatrixFromMetadata(self.metadata, "B") sipAp = getSipMatrixFromMetadata(self.metadata, "AP") sipBp = getSipMatrixFromMetadata(self.metadata, "BP") forwardMetadata = makeTanSipMetadata( crpix=crpix, crval=crval, cdMatrix=cdMatrix, sipA=sipA, sipB=sipB, ) self.assertFalse(forwardMetadata.exists("AP_ORDER")) self.assertFalse(forwardMetadata.exists("BP_ORDER")) fullMetadata = makeTanSipMetadata( crpix=crpix, crval=crval, cdMatrix=cdMatrix, sipA=sipA, sipB=sipB, sipAp=sipAp, sipBp=sipBp, ) for cardName in ("CRPIX1", "CRPIX2", "CRVAL1", "CRVAL2", "CTYPE1", "CTYPE2", "CUNIT1", "CUNIT2", "RADESYS"): self.assertTrue(forwardMetadata.exists(cardName)) self.assertTrue(fullMetadata.exists(cardName)) for name, matrix in (("A", sipA), ("B", sipB)): self.checkSipMetadata(name, matrix, forwardMetadata) self.checkSipMetadata(name, matrix, fullMetadata) for name, matrix in (("AP", sipAp), ("BP", sipBp)): self.checkSipMetadata(name, matrix, fullMetadata)
def testMakeTanSipMetadata(self): """Test makeTanSipMetadata """ crpix = lsst.geom.Point2D(self.metadata.getScalar("CRPIX1") - 1, self.metadata.getScalar("CRPIX2") - 1) crval = lsst.geom.SpherePoint(self.metadata.getScalar("CRVAL1"), self.metadata.getScalar("CRVAL2"), lsst.geom.degrees) cdMatrix = getCdMatrixFromMetadata(self.metadata) sipA = getSipMatrixFromMetadata(self.metadata, "A") sipB = getSipMatrixFromMetadata(self.metadata, "B") sipAp = getSipMatrixFromMetadata(self.metadata, "AP") sipBp = getSipMatrixFromMetadata(self.metadata, "BP") forwardMetadata = makeTanSipMetadata( crpix=crpix, crval=crval, cdMatrix=cdMatrix, sipA=sipA, sipB=sipB, ) self.assertFalse(forwardMetadata.exists("AP_ORDER")) self.assertFalse(forwardMetadata.exists("BP_ORDER")) fullMetadata = makeTanSipMetadata( crpix=crpix, crval=crval, cdMatrix=cdMatrix, sipA=sipA, sipB=sipB, sipAp=sipAp, sipBp=sipBp, ) for cardName in ("CRPIX1", "CRPIX2", "CRVAL1", "CRVAL2", "CTYPE1", "CTYPE2", "CUNIT1", "CUNIT2", "RADESYS"): self.assertTrue(forwardMetadata.exists(cardName)) self.assertTrue(fullMetadata.exists(cardName)) for name, matrix in (("A", sipA), ("B", sipB)): self.checkSipMetadata(name, matrix, forwardMetadata) self.checkSipMetadata(name, matrix, fullMetadata) for name, matrix in (("AP", sipAp), ("BP", sipBp)): self.checkSipMetadata(name, matrix, fullMetadata)
def calculateSipWcsHeader(wcs, order, bbox, spacing, header=None): """Generate a SIP WCS header approximating a given ``SkyWcs`` Parameters ---------- wcs : `lsst.afw.geom.SkyWcs` World Coordinate System to approximate as SIP. order : `int` SIP order (equal to the maximum sum of the polynomial exponents). bbox : `lsst.geom.Box2I` Bounding box over which to approximate the ``wcs``. spacing : `float` Spacing between sample points. header : `lsst.daf.base.PropertyList`, optional Header to which to add SIP WCS keywords. Returns ------- header : `lsst.daf.base.PropertyList` Header including SIP WCS keywords. Examples -------- >>> header = calculateSipWcsHeader(exposure.getWcs(), 3, exposure.getBBox(), 20) >>> sipWcs = SkyWcs(header) """ transform = getPixelToIntermediateWorldCoords(wcs) crpix = wcs.getPixelOrigin() cdMatrix = wcs.getCdMatrix() crval = wcs.getSkyOrigin() gridNum = Extent2I(int(bbox.getWidth() / spacing + 0.5), int(bbox.getHeight() / spacing + 0.5)) sip = SipApproximation(transform, crpix, cdMatrix, Box2D(bbox), gridNum, order) md = makeTanSipMetadata(sip.getPixelOrigin(), crval, sip.getCdMatrix(), sip.getA(), sip.getB(), sip.getAP(), sip.getBP()) if header is not None: header.combine(md) else: header = md return header
def doTest(self, name, func, order=3, numIter=4, specifyBBox=False, doPlot=False, doPrint=False): """Apply func(x, y) to each source in self.sourceCat, then fit and check the resulting WCS """ bbox = lsst.geom.Box2I() for refObj, src, d in self.matches: origPos = src.get(self.srcCentroidKey) x, y = func(*origPos) distortedPos = lsst.geom.Point2D(*func(*origPos)) src.set(self.srcCentroidKey, distortedPos) bbox.include(lsst.geom.Point2I(lsst.geom.Point2I(distortedPos))) tanSipWcs = self.tanWcs for i in range(numIter): if specifyBBox: sipObject = makeCreateWcsWithSip(self.matches, tanSipWcs, order, bbox) else: sipObject = makeCreateWcsWithSip(self.matches, tanSipWcs, order) tanSipWcs = sipObject.getNewWcs() setMatchDistance(self.matches) fitRes = lsst.pipe.base.Struct( wcs=tanSipWcs, scatterOnSky=sipObject.getScatterOnSky(), ) if doPrint: print("TAN-SIP metadata fit over bbox=", bbox) metadata = makeTanSipMetadata( crpix=tanSipWcs.getPixelOrigin(), crval=tanSipWcs.getSkyOrigin(), cdMatrix=tanSipWcs.getCdMatrix(), sipA=sipObject.getSipA(), sipB=sipObject.getSipB(), sipAp=sipObject.getSipAp(), sipBp=sipObject.getSipBp(), ) print(metadata.toString()) if doPlot: self.plotWcs(tanSipWcs, name=name) self.checkResults(fitRes, catsUpdated=False) if self.MatchClass == afwTable.ReferenceMatch: # reset source coord and reference centroid based on initial WCS afwTable.updateRefCentroids(wcs=self.tanWcs, refList=self.refCat) afwTable.updateSourceCoords(wcs=self.tanWcs, sourceList=self.sourceCat) fitterConfig = FitTanSipWcsTask.ConfigClass() fitterConfig.order = order fitterConfig.numIter = numIter fitter = FitTanSipWcsTask(config=fitterConfig) self.loadData() if specifyBBox: fitRes = fitter.fitWcs( matches=self.matches, initWcs=self.tanWcs, bbox=bbox, refCat=self.refCat, sourceCat=self.sourceCat, ) else: fitRes = fitter.fitWcs( matches=self.matches, initWcs=self.tanWcs, bbox=bbox, refCat=self.refCat, sourceCat=self.sourceCat, ) self.checkResults(fitRes, catsUpdated=True)