Beispiel #1
0
def main(rootDir, visit, ccd, filter=None,
         frame=1, title="", scale="zscale", zoom="to fit", trans=60,
         useEllipse=False, maskOnly=False):

    if ccd.find(",") < 0:
        ccd = int(ccd)

    pipeVersion = dafPersist.eupsVersions.EupsVersions().versions['hscPipe']
    if StrictVersion(pipeVersion) >= StrictVersion('3.9.0'):
        coaddData = "deepCoadd_calexp"
        coaddCat  = "meas"
    else:
        coaddData = "deepCoadd"
        coaddCat  = "src"

    print "Read in %s image"%coaddData

    # make a butler and specify your dataId
    butler = dafPersist.Butler(rootDir)
    if filter:
        dataId = {'tract': visit, 'patch':ccd, 'filter': filter}
        exposure = butler.get(coaddData, dataId, immediate=True)
        butlerTarget='deepCoadd_'
        sources = butler.get(butlerTarget + coaddCat, dataId)
    else:
        dataId = {'visit': visit, 'ccd':ccd}
        exposure = butler.get("calexp", dataId, immediate=True)
        butlerTarget=""
        sources = butler.get(butlerTarget + 'src', dataId)

    # put the settings in a dict object and call ds9.mtv()
    settings = {'scale':scale, 'zoom': zoom, 'mask' : 'transparency %d' %(trans)}
    ds9.setMaskPlaneColor('FAKE', color=ds9.CYAN)
    if not maskOnly:
        ds9.mtv(exposure, frame=frame, title=title, settings=settings)
    else:
        msk = exposure.getMaskedImage().getMask()
        msk &= msk.getPlaneBitMask('FAKE')
        ds9.mtv(msk, frame=frame, title=title, settings=settings)

    with ds9.Buffering():
        print len(sources)
        x0, y0 = exposure.getXY0()
        print x0, y0
        for i,source in enumerate(sources):
            color = ds9.RED
            size = 5.0

            if useEllipse:
                # show an ellipse symbol
                symbol = "@:{ixx},{ixy},{iyy}".format(ixx=source.getIxx(),
                                                      ixy=source.getIxy(),
                                                      iyy=source.getIyy())
            else:
                # just a simple point (symbols +, x, *, o are all accepted)
                symbol = "o"

            ds9.dot(symbol, source.getX()-x0, source.getY()-y0, ctype=color,
                    size=size, frame=frame, silent=True)
Beispiel #2
0
def subtractCrosstalkYagi(mi, coeffs1List, coeffs2List, gainsPreampSig, minPixelToMask=45000,
                          crosstalkStr="CROSSTALK"):
    """Subtract the crosstalk from MaskedImage mi given a set of coefficients
       based on procedure presented in Yagi et al. 2012, PASP in publication; arXiv:1210.8212
       The pixels affected by signal over minPixelToMask have the crosstalkStr bit set
    """

    ####sctrl = afwMath.StatisticsControl()
    ####sctrl.setAndMask(mi.getMask().getPlaneBitMask("DETECTED"))
    ####bkgd = afwMath.makeStatistics(mi, afwMath.MEDIAN, sctrl).getValue()
    #
    # These are the pixels that are bright enough to cause crosstalk (more precisely,
    # the ones that we label as causing crosstalk; in reality all pixels cause crosstalk)
    #
    if True:
        tempStr = "TEMP"                    # mask plane used to record the bright pixels that we need to mask
        mi.getMask().addMaskPlane(tempStr)
        fs = afwDetect.FootprintSet(mi, afwDetect.Threshold(minPixelToMask), tempStr)

        mi.getMask().addMaskPlane(crosstalkStr)
        ds9.setMaskPlaneColor(crosstalkStr, ds9.MAGENTA)
        fs.setMask(mi.getMask(), crosstalkStr)  # the crosstalkStr bit will now be set
        # whenever we subtract crosstalk
        crosstalk = mi.getMask().getPlaneBitMask(crosstalkStr)

    if True:
        # This python implementation is fairly fast
        image = mi.getImage()
        xtalk = afwImage.ImageF(image.getDimensions())
        xtalk.set(0)
        for i in range(4):
            xAmp, xOff = getAmplifier(xtalk, i, i)
            for j in range(4):
                if i == j:
                    continue
                gainRatio = gainsPreampSig[j] / gainsPreampSig[i]
                jAmp, jOff = getAmplifier(image, j, i)
                xAmp.scaledPlus(gainRatio*coeffs1List[i][j], jAmp)
                xOff.scaledPlus(gainRatio*coeffs2List[i][j], jOff)

        image -= xtalk
    else:
        nAmp = 4
        subtractCrosstalk(mi, nAmp, coeffs1List, coeffs2List, gainsPreampSig)

    #
    # Clear the crosstalkStr bit in the original bright pixels, where tempStr is set
    #
    msk = mi.getMask()
    temp = msk.getPlaneBitMask(tempStr)
    xtalk_temp = crosstalk | temp
    np_msk = msk.getArray()
    np_msk[np.where(np.bitwise_and(np_msk, xtalk_temp) == xtalk_temp)] &= ~crosstalk

    try:
        msk.removeAndClearMaskPlane(tempStr, True) # added in afw #1853
    except AttributeError:
        ds9.setMaskPlaneVisibility(tempStr, False)
Beispiel #3
0
def subtractCrosstalkYagi(mi, coeffs1List, coeffs2List, gainsPreampSig, minPixelToMask=45000, crosstalkStr="CROSSTALK"):
    """Subtract the crosstalk from MaskedImage mi given a set of coefficients
       based on procedure presented in Yagi et al. 2012, PASP in publication; arXiv:1210.8212
       The pixels affected by signal over minPixelToMask have the crosstalkStr bit set
    """


    ####sctrl = afwMath.StatisticsControl()
    ####sctrl.setAndMask(mi.getMask().getPlaneBitMask("DETECTED"))
    ####bkgd = afwMath.makeStatistics(mi, afwMath.MEDIAN, sctrl).getValue()
    #
    # These are the pixels that are bright enough to cause crosstalk (more precisely,
    # the ones that we label as causing crosstalk; in reality all pixels cause crosstalk)
    #
    if True:
        tempStr = "TEMP"                    # mask plane used to record the bright pixels that we need to mask
        mi.getMask().addMaskPlane(tempStr)
        fs = afwDetect.FootprintSet(mi, afwDetect.Threshold(minPixelToMask), tempStr)

        mi.getMask().addMaskPlane(crosstalkStr)
        ds9.setMaskPlaneColor(crosstalkStr, ds9.MAGENTA)
        fs.setMask(mi.getMask(), crosstalkStr) # the crosstalkStr bit will now be set whenever we subtract crosstalk
        crosstalk = mi.getMask().getPlaneBitMask(crosstalkStr)

    if True:
        # This python implementation is fairly fast
        image = mi.getImage()
        xtalk = afwImage.ImageF(image.getDimensions())
        xtalk.set(0)
        for i in range(4):
            xAmp, xOff = getAmplifier(xtalk, i, i)
            for j in range(4):
                if i == j:
                    continue
                gainRatio = gainsPreampSig[j] / gainsPreampSig[i]
                jAmp, jOff = getAmplifier(image, j, i)
                xAmp.scaledPlus(gainRatio*coeffs1List[i][j], jAmp)
                xOff.scaledPlus(gainRatio*coeffs2List[i][j], jOff)

        image -= xtalk
    else:
        nAmp = 4
        subaruLib.subtractCrosstalk(mi, nAmp, coeffs1List, coeffs2List, gainsPreampSig)

    #
    # Clear the crosstalkStr bit in the original bright pixels, where tempStr is set
    #
    msk = mi.getMask()
    temp = msk.getPlaneBitMask(tempStr)
    xtalk_temp = crosstalk | temp
    np_msk = msk.getArray()
    np_msk[np.where(np.bitwise_and(np_msk, xtalk_temp) == xtalk_temp)] &= ~crosstalk

    try:
        msk.removeAndClearMaskPlane(tempStr, True) # added in afw #1853
    except AttributeError:
        ds9.setMaskPlaneVisibility(tempStr, False)
Beispiel #4
0
    def testTwoDisplays(self):
        """Test that we can do things with two frames"""

        exp = afwImage.ExposureF(300, 350)

        for frame in (0, 1):
            ds9.setMaskTransparency(50, frame=frame)

            if frame == 1:
                ds9.setMaskPlaneColor("CROSSTALK", "ignore", frame=frame)
            ds9.mtv(exp, title="parent", frame=frame)

            ds9.erase(frame=frame)
            ds9.dot('o', 205, 180, size=6, ctype=ds9.RED, frame=frame)
Beispiel #5
0
    def testTwoDisplays(self):
        """Test that we can do things with two frames"""

        exp = afwImage.ExposureF(300, 350)

        for frame in (0, 1):
            ds9.setMaskTransparency(50, frame=frame)

            if frame == 1:
                ds9.setMaskPlaneColor("CROSSTALK", "ignore", frame=frame)
            ds9.mtv(exp, title="parent", frame=frame)

            ds9.erase(frame=frame)
            ds9.dot('o', 205, 180, size=6, ctype=ds9.RED, frame=frame)
Beispiel #6
0
 def testMaskPlanes(self):
     """Test basic image display"""
     ds9.setMaskTransparency(50)
     ds9.setMaskPlaneColor("CROSSTALK", "orange")
Beispiel #7
0
def disp_turnOffAllMasks(exceptFor=None):
    mpDict = afwImage.Mask().getMaskPlaneDict()
    for plane in mpDict.keys():
        if plane in exceptFor:
            continue
        ds9.setMaskPlaneColor(plane, afwDisplay.IGNORE)
def detectFootprints(exposure,
                     positiveThreshold,
                     psf=None,
                     negativeThreshold=None,
                     npixMin=1):
    """Detect sources above positiveThreshold in the provided exposure returning the
    detectionSet.  Only sources with at least npixMin are considered

    If negativeThreshold, return a pair of detectionSets, dsPositive, dsNegative
    """

    assert positiveThreshold or negativeThreshold  # or both

    if positiveThreshold:
        positiveThreshold = afwDetection.Threshold(positiveThreshold)
    if negativeThreshold:
        negativeThreshold = afwDetection.Threshold(negativeThreshold)
    #
    # Unpack variables
    #
    maskedImage = exposure.getMaskedImage()

    if not psf:  # no need to convolve if we don't know the PSF
        convolvedImage = maskedImage
        goodBBox = maskedImage.getBBox()
    else:
        convolvedImage = maskedImage.Factory(maskedImage.getBBox())

        if display:
            ds9.mtv(maskedImage)
        #
        # Smooth the Image
        #
        psf.convolve(convolvedImage, maskedImage,
                     convolvedImage.getMask().getMaskPlane("EDGE"))
        #
        # Only search psf-smooth part of frame
        #
        goodBBox = psf.getKernel().shrinkBBox(maskedImage.getBBox())

    middle = convolvedImage.Factory(convolvedImage, goodBBox)

    dsNegative = None
    if negativeThreshold != None:
        # detect negative sources
        dsNegative = afwDetection.makeDetectionSet(middle, negativeThreshold,
                                                   "DETECTED_NEGATIVE",
                                                   npixMin)
        if not ds9.getMaskPlaneColor("DETECTED_NEGATIVE"):
            ds9.setMaskPlaneColor("DETECTED_NEGATIVE", ds9.CYAN)

    dsPositive = None
    if positiveThreshold != None:
        dsPositive = afwDetection.makeFootprintSet(middle, positiveThreshold,
                                                   "DETECTED", npixMin)
    #
    # ds only searched the middle but it belongs to the entire MaskedImage
    #
    dsPositive.setRegion(maskedImage.getBBox())
    if dsNegative:
        dsNegative.setRegion(maskedImage.getBBox())
    #
    # We want to grow the detections into the edge by at least one pixel so that it sees the EDGE bit
    #
    grow, isotropic = 1, False
    dsPositive = afwDetection.FootprintSetF(dsPositive, grow, isotropic)
    dsPositive.setMask(maskedImage.getMask(), "DETECTED")

    if dsNegative:
        dsNegative = afwDetection.FootprintSetF(dsNegative, grow, isotropic)
        dsNegative.setMask(maskedImage.getMask(), "DETECTED_NEGATIVE")
    #
    # clean up
    #
    del middle

    if negativeThreshold:
        if positiveThreshold:
            return dsPositive, dsNegative
        return dsPositive, dsNegative
    else:
        return dsPositive
Beispiel #9
0
 def testMaskPlanes(self):
     """Test basic image display"""
     ds9.setMaskTransparency(50)
     ds9.setMaskPlaneColor("CROSSTALK", "orange")
def detectFootprints(exposure, positiveThreshold, psf=None, negativeThreshold=None, npixMin=1):
    """Detect sources above positiveThreshold in the provided exposure returning the
    detectionSet.  Only sources with at least npixMin are considered

    If negativeThreshold, return a pair of detectionSets, dsPositive, dsNegative
    """

    assert positiveThreshold or negativeThreshold # or both

    if positiveThreshold:
        positiveThreshold = afwDetection.Threshold(positiveThreshold)
    if negativeThreshold:
        negativeThreshold = afwDetection.Threshold(negativeThreshold)
    #
    # Unpack variables
    #
    maskedImage = exposure.getMaskedImage()

    if not psf:                         # no need to convolve if we don't know the PSF
        convolvedImage = maskedImage
        goodBBox = maskedImage.getBBox()
    else:
        convolvedImage = maskedImage.Factory(maskedImage.getBBox())
        
        if display:
            ds9.mtv(maskedImage)
        # 
        # Smooth the Image
        #
        psf.convolve(convolvedImage, 
                     maskedImage, 
                     convolvedImage.getMask().getMaskPlane("EDGE"))
        #
        # Only search psf-smooth part of frame
        #
        goodBBox = psf.getKernel().shrinkBBox(maskedImage.getBBox())

    middle = convolvedImage.Factory(convolvedImage, goodBBox)

    dsNegative = None 
    if negativeThreshold != None:
        #detect negative sources
        dsNegative = afwDetection.makeDetectionSet(middle, negativeThreshold, "DETECTED_NEGATIVE", npixMin)
        if not ds9.getMaskPlaneColor("DETECTED_NEGATIVE"):
            ds9.setMaskPlaneColor("DETECTED_NEGATIVE", ds9.CYAN)

    dsPositive = None
    if positiveThreshold != None:
        dsPositive = afwDetection.makeFootprintSet(middle, positiveThreshold, "DETECTED", npixMin)
    #
    # ds only searched the middle but it belongs to the entire MaskedImage
    #
    dsPositive.setRegion(maskedImage.getBBox())
    if dsNegative:
        dsNegative.setRegion(maskedImage.getBBox())
    #
    # We want to grow the detections into the edge by at least one pixel so that it sees the EDGE bit
    #
    grow, isotropic = 1, False
    dsPositive = afwDetection.FootprintSetF(dsPositive, grow, isotropic)
    dsPositive.setMask(maskedImage.getMask(), "DETECTED")

    if dsNegative:
        dsNegative = afwDetection.FootprintSetF(dsNegative, grow, isotropic)
        dsNegative.setMask(maskedImage.getMask(), "DETECTED_NEGATIVE")
    #
    # clean up
    #
    del middle

    if negativeThreshold:
        if positiveThreshold:
            return dsPositive, dsNegative
        return dsPositive, dsNegative
    else:
        return dsPositive