Пример #1
0
def writeThumbnail(dataRef, thumb, dataset):
    """Write snapshot thumbnail to disk.

    Parameters
    ----------
    dataRef : `daf.persistence.butlerSubset.ButlerDataRef`
        Butler dataref to use to construct the output filename.
    thumb : `numpy.ndarray`
        Binned and scaled image to be written as a PNG.
    dataset : `str`
        String containing the dataset for this thumbnail.

    Raises
    ------
    OSError
        Raised if the output directory cannot be created and does not
        exist.
    """
    filename = dataRef.get(dataset + "_filename")[0]
    directory = os.path.dirname(filename)
    if not os.path.exists(directory):
        try:
            os.makedirs(directory)
        except OSError as e:
            # Don't fail if directory exists due to race condition.
            if e.errno != errno.EEXIST:
                raise e
    afwRGB.writeRGB(filename, thumb)
Пример #2
0
 def testWriteStars(self):
     """Test writing RGB files to disk"""
     asinhMap = rgb.AsinhMapping(self.min, self.range, self.Q)
     rgbImage = asinhMap.makeRgbImage(self.images[R], self.images[G],
                                      self.images[B])
     with utilsTests.getTempFilePath(".png") as fileName:
         rgb.writeRGB(fileName, rgbImage)
Пример #3
0
 def testWriteStars(self):
     """Test writing RGB files to disk"""
     asinhMap = rgb.AsinhMapping(self.min, self.range, self.Q)
     rgbImage = asinhMap.makeRgbImage(self.images[R], self.images[G], self.images[B])
     with utilsTests.getTempFilePath(".png") as fileName:
         rgb.writeRGB(fileName, rgbImage)
         self.assertTrue(os.path.exists(fileName))
Пример #4
0
    def testWriteStars(self):
        """Test writing RGB files to disk"""
        asinhMap = rgb.AsinhMapping(self.min, self.range, self.Q)
        rgbImage = asinhMap.makeRgbImage(self.images[R], self.images[G], self.images[B])
        fileName = "rgb.png"
        with Tempfile(fileName, remove=True):
            rgb.writeRGB(fileName, rgbImage)

            if False:               # you'll also want to set remove=False in Tempfile manager
                os.system("open %s > /dev/null 2>&1" % fileName)

            self.assertTrue(os.path.exists(fileName))
Пример #5
0
    def run(self, expRef, butler):
        """Make summary plots of full focalplane images.
        """
        sbi = SimButlerImage(butler,
                             type=expRef.butlerSubset.datasetType,
                             visit=expRef.dataId['visit'])

        # Get the per ccd images
        def parse_name_to_dataId(name_str):
            raft, sensor = name_str.split()
            return {'raft': raft[-3:], 'sensor': sensor[-3:]}

        for ccd in butler.get('camera'):
            data_id = parse_name_to_dataId(ccd.getName())
            data_id.update(expRef.dataId)
            try:
                binned_im = sbi.getCcdImage(ccd,
                                            binSize=self.config.sensorBinSize,
                                            as_masked_image=True)[0]
                binned_im = rotateImageBy90(binned_im,
                                            ccd.getOrientation().getNQuarter())
                if self.config.putFullSensors:
                    butler.put(binned_im, 'binned_sensor_fits', **data_id)
            except (TypeError, RuntimeError):
                # butler couldn't put the image or there was no image to put
                continue
            (x, y) = binned_im.getDimensions()
            boxes = {
                'A':
                afwGeom.Box2I(afwGeom.PointI(0, y / 2),
                              afwGeom.ExtentI(x, y / 2)),
                'B':
                afwGeom.Box2I(afwGeom.PointI(0, 0), afwGeom.ExtentI(x, y / 2))
            }
            for half in ('A', 'B'):
                box = boxes[half]
                butler.put(afwImage.MaskedImageF(binned_im, box),
                           'binned_sensor_fits_halves',
                           half=half,
                           **data_id)

        im = cgu.showCamera(butler.get('camera'),
                            imageSource=sbi,
                            binSize=self.config.binSize)
        expRef.put(im, 'focalplane_summary_fits')
        im = flipImage(im, False, True)
        zmap = ZScaleMapping(im, contrast=self.config.contrast)
        rgb = zmap.makeRgbImage(im, im, im)
        file_name = expRef.get('focalplane_summary_png_filename')
        writeRGB(file_name[0], rgb)
Пример #6
0
def plotDeblendFamilyRGB(parent,
                         bands=['g', 'r', 'i'],
                         min=0.01,
                         max=0.5,
                         Q=8,
                         rgbFileFmt=None):
    x, y = parent.getX(), parent.getY()

    fams = {}
    imBbox = afwGeom.BoxI()
    for bandName in "GRI".upper():
        filterName = "HSC-%s" % bandName

        x0, y0 = coaddDict[filterName].getXY0()
        x0, y0 = 0, 0
        fams[filterName] = familiesDict[filterName].find((x + x0, y + y0),
                                                         matchRadius=20)
        if not fams[filterName]:
            return
        parent, kids = fams[filterName]

        bbox = parent.getFootprint().getBBox()
        for kid in kids:
            kim = footprintToImage(kid.getFootprint(),
                                   coaddDict[filterName].getMaskedImage())
            bbox.include(kim.getBBox(afwImage.PARENT))

        imBbox.include(bbox)

    images = {}
    for bandName in bands:
        filterName = "HSC-%s" % bandName.upper()

        images[bandName] = makeDeblendFamilyMosaic(
            coaddDict[filterName].getMaskedImage(),
            *fams[filterName],
            background=-0.1,
            imBbox=imBbox).makeMosaic(display=None)

    for bands in [bands]:
        B, G, R = bands
        rgb = afwRgb.makeRGB(images[R], images[G], images[B], min, max - min,
                             Q)

        afwRgb.displayRGB(rgb, show=True)

        if rgbFileFmt:
            afwRgb.writeRGB(rgbFileFmt % "".join(bands), rgb)
Пример #7
0
def plotDeblendFamilyRGB(parent, bands=['g', 'r', 'i'],
                         min=0.01, max=0.5, Q=8, rgbFileFmt=None):
    x, y = parent.getX(), parent.getY()

    fams = {}
    imBbox = afwGeom.BoxI()
    for bandName in "GRI".upper():
        filterName = "HSC-%s" % bandName

        x0, y0 = coaddDict[filterName].getXY0()
        x0, y0 = 0, 0
        fams[filterName] = familiesDict[filterName].find((x + x0, y + y0), matchRadius=20)
        if not fams[filterName]:
            return
        parent, kids = fams[filterName]

        bbox = parent.getFootprint().getBBox()
        for kid in kids:
            kim = footprintToImage(kid.getFootprint(), coaddDict[filterName].getMaskedImage())
            bbox.include(kim.getBBox(afwImage.PARENT))

        imBbox.include(bbox)

    images = {} 
    for bandName in bands:
        filterName = "HSC-%s" % bandName.upper()

        images[bandName] = makeDeblendFamilyMosaic(coaddDict[filterName].getMaskedImage(),
                                                   *fams[filterName],
                                                    background=-0.1, imBbox=imBbox).makeMosaic(display=None)

    for bands in [bands]:
        B, G, R = bands
        rgb = afwRgb.makeRGB(images[R], images[G], images[B], min, max - min, Q)

        afwRgb.displayRGB(rgb, show=True)

        if rgbFileFmt:
            afwRgb.writeRGB(rgbFileFmt % "".join(bands), rgb)
Пример #8
0
    def runDataRef(self, expRefList, butler):
        """Make summary plots of full focalplane images.
        """
        if len(expRefList) == 0:
            return pipeBase.Struct(exitStatus=1)

        lsst.afw.fits.setAllowImageCompression(
            self.config.allowFitsCompression)

        dstype = expRefList[0].butlerSubset.datasetType

        if dstype == "raw":

            def callback(im, ccd, imageSource):
                return cgu.rawCallback(im,
                                       ccd,
                                       imageSource,
                                       correctGain=True,
                                       subtractBias=True)
        elif dstype == "eimage":
            callback = eimageCallback
        elif self.config.doApplySkyCorr:
            callback = skyCorrCallback
        else:
            callback = None

        for visit in set([er.dataId["visit"] for er in expRefList]):
            self.log.info("Processing visit %d", visit)
            expRefListForVisit = [
                er for er in expRefList if er.dataId["visit"] == visit
            ]

            dataId = expRefListForVisit[0].dataId
            bi = cgu.ButlerImage(butler,
                                 dstype,
                                 visit=visit,
                                 callback=callback,
                                 verbose=True)

            if self.config.doSensorImages:
                for dataId in (er.dataId for er in expRefListForVisit):
                    ccd = butler.get('calexp_detector', **dataId)
                    try:
                        md = butler.get('calexp_md', **dataId)
                    except RuntimeError:
                        md = None
                    if md:
                        afwGeom.makeSkyWcs(
                            md, strip=True
                        )  # strip WCS cards; they're invalidated by binning
                    try:
                        binned_im = bi.getCcdImage(
                            ccd,
                            binSize=self.config.sensorBinSize,
                            asMaskedImage=True)[0]
                        binned_im = rotateImageBy90(
                            binned_im,
                            ccd.getOrientation().getNQuarter())
                        if self.config.putFullSensors:
                            binned_exp = afwImage.ExposureF(binned_im)
                            binned_exp.setMetadata(md)
                            butler.put(binned_exp,
                                       'binned_sensor_fits',
                                       **dataId,
                                       dstype=dstype)
                    except (TypeError, RuntimeError) as e:
                        # butler couldn't put the image or there was no image to put
                        self.log.warn("Unable to make binned image: %s", e)
                        continue

                    (x, y) = binned_im.getDimensions()
                    boxes = {
                        'A':
                        afwGeom.Box2I(afwGeom.PointI(0, y / 2),
                                      afwGeom.ExtentI(x, y / 2)),
                        'B':
                        afwGeom.Box2I(afwGeom.PointI(0, 0),
                                      afwGeom.ExtentI(x, y / 2))
                    }
                    for half in ('A', 'B'):
                        box = boxes[half]
                        binned_exp = afwImage.ExposureF(binned_im[box])
                        binned_exp.setMetadata(md)
                        butler.put(binned_exp,
                                   'binned_sensor_fits_halves',
                                   half=half,
                                   **dataId,
                                   dstype=dstype)

            im = cgu.showCamera(butler.get('camera'),
                                imageSource=bi,
                                binSize=self.config.binSize)

            dstypeName = "%s-%s" % (
                dstype, self.config.fpId) if self.config.fpId else dstype

            butler.put(im, 'focal_plane_fits', visit=visit, dstype=dstypeName)

            # Compute the zscale stretch for just the CCDs that have data.
            detectorNameList = [
                "%s_%s" % (er.dataId["raftName"], er.dataId["detectorName"])
                for er in expRefListForVisit
            ]
            im_scaling = cgu.showCamera(butler.get('camera'),
                                        imageSource=bi,
                                        binSize=self.config.binSize,
                                        detectorNameList=detectorNameList)
            zmap = ZScaleMapping(im_scaling, contrast=self.config.contrast)

            im = flipImage(im, False, True)
            rgb = zmap.makeRgbImage(im, im, im)
            file_name = butler.get('focal_plane_png_filename',
                                   visit=visit,
                                   dstype=dstypeName)
            writeRGB(file_name[0], rgb)

        return pipeBase.Struct(exitStatus=0)