Beispiel #1
0
    def testMakeTanSipWcs(self):
        referenceWcs = makeSkyWcs(self.metadata, strip=False)

        crpix = Point2D(
            self.metadata.get("CRPIX1") - 1,
            self.metadata.get("CRPIX2") - 1)
        crval = SpherePoint(
            self.metadata.get("CRVAL1") * degrees,
            self.metadata.get("CRVAL2") * 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")
        skyWcs1 = makeTanSipWcs(crpix=crpix,
                                crval=crval,
                                cdMatrix=cdMatrix,
                                sipA=sipA,
                                sipB=sipB)
        skyWcs2 = makeTanSipWcs(crpix=crpix,
                                crval=crval,
                                cdMatrix=cdMatrix,
                                sipA=sipA,
                                sipB=sipB,
                                sipAp=sipAp,
                                sipBp=sipBp)

        self.assertWcsAlmostEqualOverBBox(referenceWcs, skyWcs1, self.bbox)
        self.assertWcsAlmostEqualOverBBox(referenceWcs, skyWcs2, self.bbox)
        self.checkMakeFlippedWcs(skyWcs1)
        self.checkMakeFlippedWcs(skyWcs2)
Beispiel #2
0
    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)
Beispiel #3
0
    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)
Beispiel #4
0
    def testGetSipMatrixFromMetadata(self):
        """Test getSipMatrixFromMetadata and makeSipMatrixMetadata
        """
        for badName in ("X", "AA"):
            self.assertFalse(hasSipMatrix(self.metadata, badName))
            with self.assertRaises(TypeError):
                getSipMatrixFromMetadata(self.metadata, badName)

        for name in ("A", "B", "AP", "BP"):
            self.assertTrue(hasSipMatrix(self.metadata, name))
            sipMatrix = getSipMatrixFromMetadata(self.metadata, name)
            width = self.metadata.getScalar("%s_ORDER" % (name, )) + 1
            self.assertEqual(sipMatrix.shape, (width, width))
            for i in range(width):
                for j in range(width):
                    # SIP matrix terms use 0-based indexing
                    cardName = "%s_%d_%d" % (name, i, j)
                    if self.metadata.exists(cardName):
                        self.assertEqual(sipMatrix[i, j],
                                         self.metadata.getScalar(cardName))
                    else:
                        self.assertEqual(sipMatrix[i, j], 0.0)

            metadata = makeSipMatrixMetadata(sipMatrix, name)
            for name in metadata.names(False):
                value = metadata.getScalar(name)
                if (name.endswith("ORDER")):
                    self.assertEqual(width, value + 1)
                else:
                    self.assertEqual(value, self.metadata.getScalar(name))
                    self.assertNotEqual(value, 0.0)  # 0s are omitted

        # try metadata with only the ORDER keyword; the matrix should be all zeros
        # except for the invalid case of order < 0
        for order in (-3, -1, 0, 3):
            metadata2 = PropertyList()
            metadata2.set("W_ORDER", order)
            if order < 0:
                # invalid order
                self.assertFalse(hasSipMatrix(metadata2, "W"))
                with self.assertRaises(TypeError):
                    getSipMatrixFromMetadata(metadata2, "W")
            else:
                self.assertTrue(hasSipMatrix(metadata2, "W"))
                zeroMatrix = getSipMatrixFromMetadata(metadata2, "W")
                self.assertEqual(zeroMatrix.shape, (order + 1, order + 1))
                for i in range(order + 1):
                    for j in range(order + 1):
                        self.assertEqual(zeroMatrix[i, j], 0.0)
Beispiel #5
0
    def testGetSipMatrixFromMetadata(self):
        """Test getSipMatrixFromMetadata and makeSipMatrixMetadata
        """
        for badName in ("X", "AA"):
            self.assertFalse(hasSipMatrix(self.metadata, badName))
            with self.assertRaises(TypeError):
                getSipMatrixFromMetadata(self.metadata, badName)

        for name in ("A", "B", "AP", "BP"):
            self.assertTrue(hasSipMatrix(self.metadata, name))
            sipMatrix = getSipMatrixFromMetadata(self.metadata, name)
            width = self.metadata.getScalar("%s_ORDER" % (name,)) + 1
            self.assertEqual(sipMatrix.shape, (width, width))
            for i in range(width):
                for j in range(width):
                    # SIP matrix terms use 0-based indexing
                    cardName = "%s_%d_%d" % (name, i, j)
                    if self.metadata.exists(cardName):
                        self.assertEqual(sipMatrix[i, j], self.metadata.getScalar(cardName))
                    else:
                        self.assertEqual(sipMatrix[i, j], 0.0)

            metadata = makeSipMatrixMetadata(sipMatrix, name)
            for name in metadata.names(False):
                value = metadata.getScalar(name)
                if (name.endswith("ORDER")):
                    self.assertEqual(width, value + 1)
                else:
                    self.assertEqual(value, self.metadata.getScalar(name))
                    self.assertNotEqual(value, 0.0)  # 0s are omitted

        # try metadata with only the ORDER keyword; the matrix should be all zeros
        # except for the invalid case of order < 0
        for order in (-3, -1, 0, 3):
            metadata2 = PropertyList()
            metadata2.set("W_ORDER", order)
            if order < 0:
                # invalid order
                self.assertFalse(hasSipMatrix(metadata2, "W"))
                with self.assertRaises(TypeError):
                    getSipMatrixFromMetadata(metadata2, "W")
            else:
                self.assertTrue(hasSipMatrix(metadata2, "W"))
                zeroMatrix = getSipMatrixFromMetadata(metadata2, "W")
                self.assertEqual(zeroMatrix.shape, (order + 1, order + 1))
                for i in range(order + 1):
                    for j in range(order + 1):
                        self.assertEqual(zeroMatrix[i, j], 0.0)
    def testMakeWcs(self):
        """Test SipForwardTransform, SipReverseTransform and makeWcs
        """
        filename = os.path.join(os.path.dirname(__file__),
                                'imgCharSources-v85501867-R01-S00.sipheader')
        sipMetadata = readMetadata(filename)
        # We're building an ICRS-based TAN-SIP using coefficients read from metadata
        # so ignore the RADESYS in metadata (which is missing anyway, falling back to FK5)
        sipMetadata.set("RADESYS", "ICRS")
        crpix = lsst.afw.geom.Point2D(
            sipMetadata.get("CRPIX1") - 1,
            sipMetadata.get("CRPIX2") - 1,
        )
        crval = lsst.afw.geom.SpherePoint(
            sipMetadata.get("CRVAL1"),
            sipMetadata.get("CRVAL2"),
            lsst.afw.geom.degrees,
        )
        cdLinearTransform = lsst.afw.geom.LinearTransform(
            getCdMatrixFromMetadata(sipMetadata))
        aArr = getSipMatrixFromMetadata(sipMetadata, "A")
        bArr = getSipMatrixFromMetadata(sipMetadata, "B")
        apArr = getSipMatrixFromMetadata(sipMetadata, "AP")
        bpArr = getSipMatrixFromMetadata(sipMetadata, "BP")
        abPoly = PolynomialTransform(aArr, bArr)
        abRevPoly = PolynomialTransform(apArr, bpArr)
        fwd = SipForwardTransform(crpix, cdLinearTransform, abPoly)
        rev = SipReverseTransform(crpix, cdLinearTransform, abRevPoly)
        wcsFromMakeWcs = lsst.meas.astrom.makeWcs(fwd, rev, crval)
        wcsFromMetadata = lsst.afw.geom.makeSkyWcs(sipMetadata, strip=False)

        # Check SipForwardTransform against a local implementation
        localPixelToIwc = makeSipPixelToIwc(sipMetadata)
        self.assertTransformsAlmostEqual(fwd,
                                         localPixelToIwc.applyForward,
                                         maxval=2000)

        # Compare SipReverseTransform against a local implementation
        # Use the forward direction first to get sensible inputs
        localIwcToPixel = makeSipIwcToPixel(sipMetadata)

        def fwdThenRev(p):
            return rev(fwd(p))

        def fwdThenLocalRev(p):
            return localIwcToPixel.applyForward(fwd(p))

        self.assertTransformsAlmostEqual(fwdThenRev,
                                         fwdThenLocalRev,
                                         maxval=2000)

        # Check that SipReverseTransform is the inverse of SipForwardTransform;
        # this is not perfect because the coefficients don't define a perfect inverse
        def nullTransform(p):
            return p

        self.assertTransformsAlmostEqual(fwdThenRev,
                                         nullTransform,
                                         maxval=2000,
                                         atol=1e-3)

        # Check SipForwardTransform against the one contained in wcsFromMakeWcs
        # (Don't bother with the other direction because the WCS transform is iterative,
        # so it doesn't tell us anything useful about SipReverseTransform
        pixelToIwc = lsst.afw.geom.getPixelToIntermediateWorldCoords(
            wcsFromMetadata)
        self.assertTransformsAlmostEqual(fwd,
                                         pixelToIwc.applyForward,
                                         maxval=2000)

        # Check a WCS constructed from SipForwardTransform, SipReverseTransform
        # against one constructed directly from the metadata
        bbox = lsst.afw.geom.Box2D(lsst.afw.geom.Point2D(0, 0),
                                   lsst.afw.geom.Extent2D(2000, 2000))
        self.assertWcsAlmostEqualOverBBox(wcsFromMakeWcs, wcsFromMetadata,
                                          bbox)
    def testMakeWcs(self):
        """Test SipForwardTransform, SipReverseTransform and makeWcs
        """
        filename = os.path.join(os.path.dirname(__file__),
                                'imgCharSources-v85501867-R01-S00.sipheader')
        sipMetadata = readMetadata(filename)
        # We're building an ICRS-based TAN-SIP using coefficients read from metadata
        # so ignore the RADESYS in metadata (which is missing anyway, falling back to FK5)
        sipMetadata.set("RADESYS", "ICRS")
        crpix = lsst.afw.geom.Point2D(
            sipMetadata.getScalar("CRPIX1") - 1,
            sipMetadata.getScalar("CRPIX2") - 1,
        )
        crval = lsst.afw.geom.SpherePoint(
            sipMetadata.getScalar("CRVAL1"),
            sipMetadata.getScalar("CRVAL2"), lsst.afw.geom.degrees,
        )
        cdLinearTransform = lsst.afw.geom.LinearTransform(getCdMatrixFromMetadata(sipMetadata))
        aArr = getSipMatrixFromMetadata(sipMetadata, "A")
        bArr = getSipMatrixFromMetadata(sipMetadata, "B")
        apArr = getSipMatrixFromMetadata(sipMetadata, "AP")
        bpArr = getSipMatrixFromMetadata(sipMetadata, "BP")
        abPoly = PolynomialTransform(aArr, bArr)
        abRevPoly = PolynomialTransform(apArr, bpArr)
        fwd = SipForwardTransform(crpix, cdLinearTransform, abPoly)
        rev = SipReverseTransform(crpix, cdLinearTransform, abRevPoly)
        wcsFromMakeWcs = lsst.meas.astrom.makeWcs(fwd, rev, crval)
        wcsFromMetadata = lsst.afw.geom.makeSkyWcs(sipMetadata, strip=False)

        # Check SipForwardTransform against a local implementation
        localPixelToIwc = makeSipPixelToIwc(sipMetadata)
        self.assertTransformsAlmostEqual(fwd, localPixelToIwc.applyForward, maxval=2000)

        # Compare SipReverseTransform against a local implementation
        # Use the forward direction first to get sensible inputs
        localIwcToPixel = makeSipIwcToPixel(sipMetadata)

        def fwdThenRev(p):
            return rev(fwd(p))

        def fwdThenLocalRev(p):
            return localIwcToPixel.applyForward(fwd(p))

        self.assertTransformsAlmostEqual(fwdThenRev, fwdThenLocalRev, maxval=2000)

        # Check that SipReverseTransform is the inverse of SipForwardTransform;
        # this is not perfect because the coefficients don't define a perfect inverse
        def nullTransform(p):
            return p

        self.assertTransformsAlmostEqual(fwdThenRev, nullTransform, maxval=2000, atol=1e-3)

        # Check SipForwardTransform against the one contained in wcsFromMakeWcs
        # (Don't bother with the other direction because the WCS transform is iterative,
        # so it doesn't tell us anything useful about SipReverseTransform
        pixelToIwc = lsst.afw.geom.getPixelToIntermediateWorldCoords(wcsFromMetadata)
        self.assertTransformsAlmostEqual(fwd, pixelToIwc.applyForward, maxval=2000)

        # Check a WCS constructed from SipForwardTransform, SipReverseTransform
        # against one constructed directly from the metadata
        bbox = lsst.afw.geom.Box2D(lsst.afw.geom.Point2D(0, 0), lsst.afw.geom.Extent2D(2000, 2000))
        self.assertWcsAlmostEqualOverBBox(wcsFromMakeWcs, wcsFromMetadata, bbox)