Ejemplo n.º 1
0
    def writeNewWcs(self, dataRefList):
        self.log.info("Write New WCS ...")
        for dataRef in dataRefList:
            iexp = dataRef.dataId["visit"]
            ichip = dataRef.dataId["ccd"]
            c = measMosaic.convertCoeff(self.coeffSet[iexp],
                                        self.ccdSet[ichip])
            wcs = measMosaic.wcsFromCoeff(c)
            calexp_md = dataRef.get("calexp_md", immediate=True)
            hscRun = mosaicUtils.checkHscStack(calexp_md)
            if hscRun is None:
                detector = dataRef.get("camera")[dataRef.dataId["ccd"]]
                nQuarter = detector.getOrientation().getNQuarter()
                if nQuarter % 4 != 0:
                    dimensions = afwImage.bboxFromMetadata(
                        calexp_md).getDimensions()
                    if nQuarter % 2 != 0:
                        dimensions = afwGeom.Extent2I(dimensions.getY(),
                                                      dimensions.getX())
                    wcs = measAstrom.rotateWcsPixelsBy90(
                        wcs, 4 - nQuarter, dimensions)

            try:
                dataRef.put(wcs, "jointcal_wcs")
            except Exception as e:
                print("failed to write wcs: %s" % (e))
Ejemplo n.º 2
0
def getFluxFitParams(dataRef):
    """Retrieve the flux correction parameters determined by meas_mosaic

    If the flux correction parameters do not exist, an exception will
    be raised.
    """
    calexp_md = dataRef.get("calexp_md", immediate=True)
    hscRun = mosaicUtils.checkHscStack(calexp_md)
    if hscRun is not None:
        ffpHeader = dataRef.get("fcr_hsc_md", immediate=True)
    else:
        ffpHeader = dataRef.get("fcr_md", immediate=True)
    photoCalib = dataRef.get("fcr_photoCalib")
    ffp = FluxFitParams(ffpHeader)

    wcs = getWcs(dataRef)

    if hscRun is None:
        detector = dataRef.get("camera")[dataRef.dataId["ccd"]]
        nQuarter = detector.getOrientation().getNQuarter()
        if nQuarter%4 != 0:
            # Have to put this import here due to circular dependence in forcedPhotCcd.py in meas_base
            import lsst.meas.astrom as measAstrom
            dimensions = dataRef.get("calexp_bbox").getDimensions()
            wcs = measAstrom.rotateWcsPixelsBy90(wcs, nQuarter, dimensions)
    return Struct(ffp=ffp, calib=photoCalib, wcs=wcs)
Ejemplo n.º 3
0
def getFluxFitParams(dataRef):
    """Retrieve the flux correction parameters determined by meas_mosaic

    If the flux correction parameters do not exist, an exception will
    be raised.
    """
    calexp_md = dataRef.get("calexp_md", immediate=True)
    hscRun = mosaicUtils.checkHscStack(calexp_md)
    if hscRun is not None:
        ffpHeader = dataRef.get("fcr_hsc_md", immediate=True)
    else:
        ffpHeader = dataRef.get("fcr_md", immediate=True)
    photoCalib = dataRef.get("fcr_photoCalib")
    ffp = FluxFitParams(ffpHeader)

    wcs = getWcs(dataRef)

    if hscRun is None:
        detector = dataRef.get("camera")[dataRef.dataId["ccd"]]
        nQuarter = detector.getOrientation().getNQuarter()
        if nQuarter % 4 != 0:
            # Have to put this import here due to circular dependence in forcedPhotCcd.py in meas_base
            import lsst.meas.astrom as measAstrom
            dimensions = dataRef.get("calexp_bbox").getDimensions()
            wcs = measAstrom.rotateWcsPixelsBy90(wcs, nQuarter, dimensions)
    return Struct(ffp=ffp, calib=photoCalib, wcs=wcs)
    def testRotateWcsPixelsBy90(self):
        filename = os.path.join(os.path.dirname(__file__),
                                'imgCharSources-v85501867-R01-S00.sipheader')
        wcs0 = lsst.afw.geom.makeSkyWcs(readMetadata(filename))
        w, h = 11, 12
        image0 = lsst.afw.image.ImageD(w, h)
        x, y = np.meshgrid(np.arange(w), np.arange(h))
        # Make a slowly-varying image of an asymmetric function
        image0.getArray()[:, :] = (x/w)**2 + 0.5*(x/w)*(y/h) - 3.0*(y/h)**2
        dimensions = image0.getBBox().getDimensions()

        image1 = lsst.afw.math.rotateImageBy90(image0, 1)
        wcs1 = rotateWcsPixelsBy90(wcs0, 1, dimensions)
        image2 = lsst.afw.math.rotateImageBy90(image0, 2)
        wcs2 = rotateWcsPixelsBy90(wcs0, 2, dimensions)
        image3 = lsst.afw.math.rotateImageBy90(image0, 3)
        wcs3 = rotateWcsPixelsBy90(wcs0, 3, dimensions)

        bbox = image0.getBBox()
        image0r = lsst.afw.image.ImageD(bbox)
        image1r = lsst.afw.image.ImageD(bbox)
        image2r = lsst.afw.image.ImageD(bbox)
        image3r = lsst.afw.image.ImageD(bbox)

        ctrl = lsst.afw.math.WarpingControl("nearest")
        lsst.afw.math.warpImage(image0r, wcs0, image0, wcs0, ctrl)
        lsst.afw.math.warpImage(image1r, wcs0, image1, wcs1, ctrl)
        lsst.afw.math.warpImage(image2r, wcs0, image2, wcs2, ctrl)
        lsst.afw.math.warpImage(image3r, wcs0, image3, wcs3, ctrl)

        # warpImage doesn't seem to handle the first row and column,
        # even with nearest-neighbor interpolation, so we have to
        # ignore pixels it didn't know how to populate.
        def compareFinite(ref, target):
            finitPixels = np.isfinite(target.getArray())
            self.assertGreater(finitPixels.sum(), 0.7*target.getArray().size)
            self.assertFloatsAlmostEqual(
                ref.getArray()[finitPixels],
                target.getArray()[finitPixels],
                rtol=1E-6
            )

        compareFinite(image0, image0r)
        compareFinite(image0, image1r)
        compareFinite(image0, image2r)
        compareFinite(image0, image3r)
    def testRotateWcsPixelsBy90(self):
        filename = os.path.join(os.path.dirname(__file__),
                                'imgCharSources-v85501867-R01-S00.sipheader')
        wcs0 = lsst.afw.geom.makeSkyWcs(readMetadata(filename))
        w, h = 11, 12
        image0 = lsst.afw.image.ImageD(w, h)
        x, y = np.meshgrid(np.arange(w), np.arange(h))
        # Make a slowly-varying image of an asymmetric function
        image0.getArray()[:, :] = (x/w)**2 + 0.5*(x/w)*(y/h) - 3.0*(y/h)**2
        dimensions = image0.getBBox().getDimensions()

        image1 = lsst.afw.math.rotateImageBy90(image0, 1)
        wcs1 = rotateWcsPixelsBy90(wcs0, 1, dimensions)
        image2 = lsst.afw.math.rotateImageBy90(image0, 2)
        wcs2 = rotateWcsPixelsBy90(wcs0, 2, dimensions)
        image3 = lsst.afw.math.rotateImageBy90(image0, 3)
        wcs3 = rotateWcsPixelsBy90(wcs0, 3, dimensions)

        bbox = image0.getBBox()
        image0r = lsst.afw.image.ImageD(bbox)
        image1r = lsst.afw.image.ImageD(bbox)
        image2r = lsst.afw.image.ImageD(bbox)
        image3r = lsst.afw.image.ImageD(bbox)

        ctrl = lsst.afw.math.WarpingControl("nearest")
        lsst.afw.math.warpImage(image0r, wcs0, image0, wcs0, ctrl)
        lsst.afw.math.warpImage(image1r, wcs0, image1, wcs1, ctrl)
        lsst.afw.math.warpImage(image2r, wcs0, image2, wcs2, ctrl)
        lsst.afw.math.warpImage(image3r, wcs0, image3, wcs3, ctrl)

        # warpImage doesn't seem to handle the first row and column,
        # even with nearest-neighbor interpolation, so we have to
        # ignore pixels it didn't know how to populate.
        def compareFinite(ref, target):
            finitPixels = np.isfinite(target.getArray())
            self.assertGreater(finitPixels.sum(), 0.7*target.getArray().size)
            self.assertFloatsAlmostEqual(
                ref.getArray()[finitPixels],
                target.getArray()[finitPixels],
                rtol=1E-6
            )

        compareFinite(image0, image0r)
        compareFinite(image0, image1r)
        compareFinite(image0, image2r)
        compareFinite(image0, image3r)
Ejemplo n.º 6
0
def applyMosaicResultsExposure(dataRef, calexp=None):
    """Update an Exposure with the Wcs, Calib, and flux scaling from meas_mosaic.

    If None, the calexp will be loaded from the dataRef.  Otherwise it is
    updated in-place.

    This assumes that the mosaic solution exists; an exception will be raised
    in the event that it does not.
    """
    if calexp is None:
        calexp = dataRef.get("calexp", immediate=True)

    nQuarter = calexp.getDetector().getOrientation().getNQuarter()
    dims = calexp.getDimensions()
    hscRun = mosaicUtils.checkHscStack(calexp.getMetadata())

    # Need the dimensions in coordinates used by meas_mosaic which defines 0,0 as the
    # lower-left hand corner on the sky
    if hscRun is None:
        if nQuarter % 2 != 0:
            width, height = calexp.getDimensions()
            dims = afwGeom.Extent2I(height, width)

    # return results in meas_mosaic coordinate system
    mosaic = getMosaicResults(dataRef, dims)

    # rotate wcs back to LSST coordinate system
    if nQuarter % 4 != 0 and hscRun is None:
        import lsst.meas.astrom as measAstrom
        mosaic.wcs = measAstrom.rotateWcsPixelsBy90(mosaic.wcs, 4 - nQuarter,
                                                    dims)
    calexp.setWcs(mosaic.wcs)

    fluxMag0 = mosaic.calib.getInstFluxAtZeroMagnitude()
    calexp.setPhotoCalib(
        afwImage.makePhotoCalibFromCalibZeroPoint(fluxMag0, 0.0))

    mi = calexp.getMaskedImage()
    # rotate photometric correction to LSST coordiantes
    if nQuarter % 4 != 0 and hscRun is None:
        mosaic.fcor = afwMath.rotateImageBy90(mosaic.fcor, 4 - nQuarter)
    mi *= mosaic.fcor

    return Struct(exposure=calexp, mosaic=mosaic)
Ejemplo n.º 7
0
def applyMosaicResultsExposure(dataRef, calexp=None):
    """Update an Exposure with the Wcs, Calib, and flux scaling from meas_mosaic.

    If None, the calexp will be loaded from the dataRef.  Otherwise it is
    updated in-place.

    This assumes that the mosaic solution exists; an exception will be raised
    in the event that it does not.
    """
    if calexp is None:
        calexp = dataRef.get("calexp", immediate=True)

    nQuarter = calexp.getDetector().getOrientation().getNQuarter()
    dims = calexp.getDimensions()
    hscRun = mosaicUtils.checkHscStack(calexp.getMetadata())

    # Need the dimensions in coordinates used by meas_mosaic which defines 0,0 as the
    # lower-left hand corner on the sky
    if hscRun is None:
        if nQuarter%2 != 0:
            width, height = calexp.getDimensions()
            dims = afwGeom.Extent2I(height, width)

    # return results in meas_mosaic coordinate system
    mosaic = getMosaicResults(dataRef, dims)

    # rotate wcs back to LSST coordinate system
    if nQuarter%4 != 0 and hscRun is None:
        import lsst.meas.astrom as measAstrom
        mosaic.wcs = measAstrom.rotateWcsPixelsBy90(mosaic.wcs, 4 - nQuarter, dims)
    calexp.setWcs(mosaic.wcs)

    fluxMag0 = mosaic.calib.getInstFluxAtZeroMagnitude()
    calexp.setPhotoCalib(afwImage.makePhotoCalibFromCalibZeroPoint(fluxMag0, 0.0))

    mi = calexp.getMaskedImage()
    # rotate photometric correction to LSST coordiantes
    if nQuarter%4 != 0 and hscRun is None:
        mosaic.fcor = afwMath.rotateImageBy90(mosaic.fcor, 4 - nQuarter)
    mi *= mosaic.fcor

    return Struct(exposure=calexp, mosaic=mosaic)
Ejemplo n.º 8
0
    def writeNewWcs(self, dataRefList):
        self.log.info("Write New WCS ...")
        for dataRef in dataRefList:
            iexp = dataRef.dataId["visit"]
            ichip = dataRef.dataId["ccd"]
            c = measMosaic.convertCoeff(self.coeffSet[iexp], self.ccdSet[ichip]);
            wcs = measMosaic.wcsFromCoeff(c);
            calexp_md = dataRef.get("calexp_md", immediate=True)
            hscRun = mosaicUtils.checkHscStack(calexp_md)
            if hscRun is None:
                detector = dataRef.get("camera")[dataRef.dataId["ccd"]]
                nQuarter = detector.getOrientation().getNQuarter()
                if nQuarter%4 != 0:
                    dimensions = afwImage.bboxFromMetadata(calexp_md).getDimensions()
                    if nQuarter%2 != 0:
                        dimensions = afwGeom.Extent2I(dimensions.getY(), dimensions.getX())
                    wcs = measAstrom.rotateWcsPixelsBy90(wcs, 4 - nQuarter, dimensions)

            try:
                dataRef.put(wcs, "jointcal_wcs")
            except Exception as e:
                print("failed to write wcs: %s" % (e))
Ejemplo n.º 9
0
    def getCcdImage(self, ccd, imageFactory=afwImage.ImageF, binSize=1):
        bbox = ccd.getBBox()
        try:
            ffp = self.ffp[ccd.getId()]
            wcs = self.wcs[ccd.getId()]
        except KeyError:
            result = imageFactory(bbox)
            return afwMath.binImage(result, binSize), ccd

        nQuarter = ccd.getOrientation().getNQuarter()
        # Rotate WCS from persisted LSST coords to meas_mosaic coords
        if nQuarter % 4 != 0:
            # Have to put this import here due to circular dependencies
            import lsst.meas.astrom as measAstrom
            wcs = measAstrom.rotateWcsPixelsBy90(wcs, nQuarter,
                                                 bbox.getDimensions())

        if nQuarter % 2:
            width, height = bbox.getHeight(), bbox.getWidth()
        else:
            width, height = bbox.getWidth(), bbox.getHeight()
        if self.fcor:
            result = getFCorImg(ffp, width, height)
            if self.jacobian:
                result *= getJImg(wcs, width, height)
        elif self.jacobian:
            result = getJImg(wcs, width, height)
        else:
            result = imageFactory(bbox)
            return afwMath.binImage(result, binSize), ccd

        # Rotate images to LSST coords
        if nQuarter % 4 != 0:
            result = afwMath.rotateImageBy90(result, 4 - nQuarter)
        result.setXY0(bbox.getMin())
        assert bbox == result.getBBox(), "%s != %s" % (bbox, result.getBBox())
        assert type(result) == imageFactory
        return afwMath.binImage(result, binSize), ccd
Ejemplo n.º 10
0
    def getCcdImage(self, ccd, imageFactory=afwImage.ImageF, binSize=1):
        bbox = ccd.getBBox()
        try:
            ffp = self.ffp[ccd.getId()]
            wcs = self.wcs[ccd.getId()]
        except KeyError:
            result = imageFactory(bbox)
            return afwMath.binImage(result, binSize), ccd

        nQuarter = ccd.getOrientation().getNQuarter()
        # Rotate WCS from persisted LSST coords to meas_mosaic coords
        if nQuarter%4 != 0:
            # Have to put this import here due to circular dependencies
            import lsst.meas.astrom as measAstrom
            wcs = measAstrom.rotateWcsPixelsBy90(wcs, nQuarter, bbox.getDimensions())

        if nQuarter%2:
            width, height = bbox.getHeight(), bbox.getWidth()
        else:
            width, height = bbox.getWidth(), bbox.getHeight()
        if self.fcor:
            result = getFCorImg(ffp, width, height)
            if self.jacobian:
                result *= getJImg(wcs, width, height)
        elif self.jacobian:
            result = getJImg(wcs, width, height)
        else:
            result = imageFactory(bbox)
            return afwMath.binImage(result, binSize), ccd

        # Rotate images to LSST coords
        if nQuarter%4 != 0:
            result = afwMath.rotateImageBy90(result, 4 - nQuarter)
        result.setXY0(bbox.getMin())
        assert bbox == result.getBBox(), "%s != %s" % (bbox, result.getBBox())
        assert type(result) == imageFactory
        return afwMath.binImage(result, binSize), ccd