コード例 #1
0
    def _addDefects(self, dataId, amp=None, ccd=None):
        """Add the defects for an amplifier or a CCD to the detector object
        for that amplifier/CCD.
        @param dataId (dict) Dataset identifier
        @param[in,out] amp (lsst.afw.cameraGeom.Detector)
        @param[in,out] ccd (lsst.afw.cameraGeom.Detector)
        Exactly one of amp or ccd should be specified."""

        if ccd is None:
            ccd = afwCameraGeom.cast_Ccd(amp.getParent())
        if len(ccd.getDefects()) > 0:
            # Assume we have loaded them properly already
            return
        defectFits = self._defectLookup(dataId, ccd.getId().getSerial())
        if defectFits is not None:
            defectDict = cameraGeomUtils.makeDefectsFromFits(defectFits)
            ccdDefects = None
            for k in defectDict.keys():
                if k == ccd.getId():
                    ccdDefects = defectDict[k]
                    break
            if ccdDefects is None:
                raise RuntimeError, "No defects for ccd %s in %s" % \
                        (str(ccd.getId()), defectFits)
            ccd.setDefects(ccdDefects)
コード例 #2
0
ファイル: utils.py プロジェクト: RayPlante/DMS.afw
def makeImageFromRaft(raft, imageSource=SynthesizeCcdImage(), raftCenter=None,
                      imageFactory=afwImage.ImageU, bin=1):
    """Make an Image of a Raft"""

    if raftCenter is None:
        raftCenter = afwGeom.Point2I(raft.getAllPixels().getDimensions()//2)

    raftImage = imageFactory(raft.getAllPixels().Dimensions()//bin)

    for det in raft:
        ccd = cameraGeom.cast_Ccd(det)
        
        bbox = ccd.getAllPixels(True)
        cen = getCenterPixel()
        origin = afwGeom.PointI(cen[0], cen[1]) - \
                bbox.getDimensions()/2 + \
                afwGeom.Extent2I(raftCenter[0], raftCenter[1])

        bbox = afwGeom.Box2I(afwGeom.Point2I((origin.getX() + bbox.getMinX())//bin,
                                             (origin.getY() + bbox.getMinY())//bin),
                             bbox.getDimensions()//bin)

        ccdImage = raftImage.Factory(raftImage, bbox, afwImage.LOCAL)
        ccdImage <<= makeImageFromCcd(ccd, imageSource, imageFactory=imageFactory, isTrimmed=True, bin=bin)

    return raftImage
コード例 #3
0
    def skyMapToCamera(self, dataIds):
        """Make a minimal camera based on the skymap; ONLY DESIGNED TO WORK WITH 1 TRACT (sorry, future developer)"""
        tracts = set(x["tract"] for x in dataIds)
        assert(len(tracts) == 1)
        self.tract = tracts.pop()

        cols   = set([x["patch"][0] for x in dataIds]) # x
        rows   = set([x["patch"][1] for x in dataIds]) # y
        col0   = min(cols)
        row0   = min(rows)
        ncols  = max(cols) - col0 + 1
        nrows  = max(rows) - row0 + 1

        origBBox = afwGeom.Box2I()

        raftId = cameraGeom.Id(0, str(self.tract))
        raft   = cameraGeom.Raft(raftId, ncols, nrows)
        for nId, dataId in enumerate(dataIds):
            patch = self.skyMap[self.tract].getPatchInfo(dataId["patch"])
            detId = cameraGeom.Id(nId, "%d-%d,%d" % (self.tract, patch.getIndex()[0], patch.getIndex()[1]))
            bbox  = patch.getInnerBBox() # outer bbox overlaps, inner doesn't
            origBBox.include(bbox)       # capture the full extent of the skymap
            bbox.shift(-afwGeom.Extent2I(bbox.getBegin())) # need the bbox to be w.r.t. the raft coord system; i.e. 0
            ccd   = cameraGeomUtils.makeDefaultCcd(bbox, detId=detId) 

            col   = patch.getIndex()[0] - col0
            row   = patch.getIndex()[1] - row0

            xc    = bbox.getBeginX() + 0.5 * bbox.getWidth()
            yc    = bbox.getBeginY() + 0.5 * bbox.getHeight()

            raft.addDetector(afwGeom.Point2I(col, row), 
                             cameraGeom.FpPoint(afwGeom.Point2D(xc, yc)),
                             cameraGeom.Orientation(),
                             ccd)

        raftBbox  = raft.getAllPixels()
        xc        = origBBox.getBeginX() + 0.5 * origBBox.getWidth()
        yc        = origBBox.getBeginY() + 0.5 * origBBox.getHeight()
        cameraId = cameraGeom.Id(0, "Skymap")
        camera = cameraGeom.Camera(cameraId, 1, 1)
        camera.addDetector(afwGeom.Point2I(0, 0),
                           cameraGeom.FpPoint(afwGeom.Point2D(xc, yc)), 
                           cameraGeom.Orientation(), raft) 

        self.camera = camera
        # Now, redo the assignments in __init__
        for r in self.camera:
            raft = cameraGeom.cast_Raft(r)
            raftName = raft.getId().getName().strip()
            self.detectors[raftName] = raft
            self.rafts[raftName] = raft
            for c in raft:
                ccd = cameraGeom.cast_Ccd(c)
                ccdName = ccd.getId().getName().strip()
                self.detectors[ccdName] = ccd
                self.sensors[ccdName] = ccd
                self.nSensor += 1
                self.raftCcdKeys.append([raftName, ccdName])
コード例 #4
0
def detectorIsCcd(exposure):
    """Is the detector referred to by the exposure a Ccd?

    @param exposure Exposure to inspect
    @returns True if exposure's detector is a Ccd
    """
    det = exposure.getDetector()
    ccd = cameraGeom.cast_Ccd(det)
    return False if ccd is None else True
コード例 #5
0
ファイル: utils.py プロジェクト: RayPlante/DMS.afw
def findCcd(parent, id):
    """Find the Ccd with the specified Id within the composite"""

    if isinstance(parent, cameraGeom.Camera):
        for d in parent:
            ccd = findCcd(cameraGeom.cast_Raft(d), id)
            if ccd:
                return ccd
    elif isinstance(parent, cameraGeom.Raft):
        try:
            return cameraGeom.cast_Ccd(parent.findDetector(id))
        except:
            pass
    else:
        if parent.getId() == id:
            return cameraGeom.cast_Ccd(parent)
        
    return None
コード例 #6
0
ファイル: detectorUtil.py プロジェクト: laurenam/pipe_tasks
def detectorIsCcd(exposure):
    """Is the detector referred to by the exposure a Ccd?

    @param exposure Exposure to inspect
    @returns True if exposure's detector is a Ccd
    """
    det = exposure.getDetector()
    ccd = cameraGeom.cast_Ccd(det)
    return False if ccd is None else True
コード例 #7
0
ファイル: cameraGeom.py プロジェクト: RayPlante/DMS.afw
    def testRaft(self):
        """Test if we can build a Raft out of Ccds"""

        #print >> sys.stderr, "Skipping testRaft"; return
        raftId = cameraGeom.Id("Raft")
        raftInfo = {"ampSerial" : CameraGeomTestCase.ampSerial}
        raft = cameraGeomUtils.makeRaft(self.geomPolicy, raftId, raftInfo=raftInfo)

        if display:
            cameraGeomUtils.showRaft(raft)
            ds9.incrDefaultFrame()

        if False:
            print cameraGeomUtils.describeRaft(raft)

        self.assertEqual(raft.getAllPixels().getWidth(), raftInfo["width"])
        self.assertEqual(raft.getAllPixels().getHeight(), raftInfo["height"])

        for x, y, serial, cen in [(  0,   0,  5, (-1.01, -2.02)),
                                  (150, 250, 21, (-1.01,  0.0 )),
                                  (250, 250, 29, ( 1.01,  0.0 )),
                                  (300, 500, 42, ( 1.01,  2.02))]:
            det = raft.findDetectorPixel(afwGeom.Point2D(x, y))
            ccd = cameraGeom.cast_Ccd(det)
            if False:
                print x, y, det.getId().getName(), \
                      ccd.findAmp(afwGeom.Point2I(150, 152), True).getId().getSerial()
            if False:                   # XXX
                self.assertEqual(ccd.findAmp(afwGeom.PointI(150, 152), True).getId().getSerial(), serial)
            for i in range(2):
                self.assertAlmostEqual(ccd.getCenter()[i], cen[i])

        name = "C:0,2"
        self.assertEqual(raft.findDetector(cameraGeom.Id(name)).getId().getName(), name)

        self.assertEqual(raft.getSize()[0], raftInfo["widthMm"])
        self.assertEqual(raft.getSize()[1], raftInfo["heightMm"])
        #
        # Test mapping pixel <--> mm
        #
        ps = raft.getPixelSize()
        for ix, iy, x, y in [(102, 500, -1.01,  2.02),
                             (306, 100,  1.01, -2.02),
                             (306, 500,  1.01,  2.02),
                             (356, 525,  1.51,  2.27),
                             ]:
            pix = afwGeom.Point2I(ix, iy) # wrt raft LLC
            #position of pixel center
            pos = afwGeom.Point2D(x+ps/2., y+ps/2.) # wrt raft center
            #position of pixel lower left corner which is returned by getPositionFromPixel()
            posll = afwGeom.Point2D(x, y) # wrt raft center

            rpos = raft.getPixelFromPosition(pos)
            rpos = afwGeom.PointI(int(rpos.getX()), int(rpos.getY()))
            self.assertEqual(rpos, pix)
            self.assertEqual(raft.getPositionFromPixel(afwGeom.Point2D(pix[0], pix[1])), posll)
コード例 #8
0
    def __init__(self, name, dataInfo, camera=None):
        """
        @param name         A name for this camera
        @param dataInfo     List of dataId [name,distinguisher] pairs (distinguisher now depricated)
        @param camera       LSST camera object for this camera
        """
        
        self.name        = name
        self.dataInfo    = dataInfo
        self.camera      = camera

        self.rafts       = {}
        self.sensors     = {}
        self.detectors   = {}
        self.nSensor     = 0

        self.raftCcdKeys = []
        
        if self.camera is None:
            return

        self.rawName = "raw"

        self.ccdNamesBySerial = {}

        for r in self.camera:
            raft = cameraGeom.cast_Raft(r)
            raftName = raft.getId().getName().strip()
            self.detectors[raftName] = raft
            self.rafts[raftName] = raft
            for c in raft:
                ccd       = cameraGeom.cast_Ccd(c)
                ccdId     = ccd.getId()
                ccdName   = ccdId.getName().strip()
                ccdSerial = ccdId.getSerial()
                
                self.detectors[ccdName] = ccd
                self.sensors[ccdName] = ccd
                self.nSensor += 1
                self.raftCcdKeys.append([raftName, ccdName])
                self.ccdNamesBySerial[ccdSerial] = ccdName

                
        self.dataIdTranslationMap = {
            # standard  : camera
            'visit'  : 'visit',
            'snap'   : 'snap',
            'raft'   : 'raft',
            'sensor' : 'sensor',
            }
        self.dataIdDbNames = {
            'visit' : 'visit',
            'raft'  : 'raftName',
            'sensor' : 'ccdName',
            'snap'  : 'snap',
            }
コード例 #9
0
ファイル: cameraGeom.py プロジェクト: RayPlante/DMS.afw
    def testCamera(self):
        """Test if we can build a Camera out of Rafts"""

        #print >> sys.stderr, "Skipping testCamera"; return

        cameraInfo = {"ampSerial" : CameraGeomTestCase.ampSerial}
        camera = cameraGeomUtils.makeCamera(self.geomPolicy, cameraInfo=cameraInfo)

        if display:
            cameraGeomUtils.showCamera(camera, )
            ds9.incrDefaultFrame()

        if False:
            print cameraGeomUtils.describeCamera(camera)

        self.assertEqual(camera.getAllPixels().getWidth(), cameraInfo["width"])
        self.assertEqual(camera.getAllPixels().getHeight(), cameraInfo["height"])

        for rx, ry, cx, cy, serial, cen in [(0, 0,     0,   0,    4, (-3.12, -2.02)),
                                            (0,   0,   150, 250, 20, (-3.12,  0.00)),
                                            (600, 300, 0,   0,   52, ( 1.10,  -2.02)),
                                            (600, 300, 150, 250, 68, ( 1.10,  0.00)),
                                            ]:
            raft = cameraGeom.cast_Raft(camera.findDetectorPixel(afwGeom.PointD(rx, ry)))

            ccd = cameraGeom.cast_Ccd(raft.findDetectorPixel(afwGeom.Point2D(cx, cy)))
            if False:
                self.assertEqual(ccd.findAmp(afwGeom.PointI(153, 152), True).getId().getSerial(), serial)
            for i in range(2):
                self.assertAlmostEqual(ccd.getCenter()[i], cen[i])

        name = "R:1,0"
        self.assertEqual(camera.findDetector(cameraGeom.Id(name)).getId().getName(), name)

        self.assertEqual(camera.getSize()[0], cameraInfo["widthMm"])
        self.assertEqual(camera.getSize()[1], cameraInfo["heightMm"])
        ps = raft.getPixelSize()
        #
        # Test mapping pixel <--> mm
        #
        for ix, iy, x, y in [(102, 500, -3.12, 2.02),
                             (152, 525, -2.62, 2.27),
                             (714, 500,  3.12, 2.02),
                             ]:
            pix = afwGeom.PointD(ix, iy) # wrt raft LLC
            pos = afwGeom.PointD(x, y) # center of pixel wrt raft center
            posll = afwGeom.PointD(x, y) # llc of pixel wrt raft center

            self.assertEqual(camera.getPixelFromPosition(pos), pix)
            self.assertEqual(camera.getPositionFromPixel(pix), posll)
        # Check that we can find an Amp in the bowels of the camera
        ccdName = "C:0,0"
        amp = cameraGeomUtils.findAmp(camera, cameraGeom.Id(ccdName), 1, 2)
        self.assertEqual(amp.getId().getName(), "ID6")
        self.assertEqual(amp.getParent().getId().getName(), ccdName)
コード例 #10
0
    def getDataArray(self):

        arr = []
        for r in self.camera:
            raft   = cameraGeom.cast_Raft(r)
            rlabel = raft.getId().getName()
            for c in raft:
                ccd    = cameraGeom.cast_Ccd(c)
                clabel = ccd.getId().getName()
                arr.append(self.data[rlabel][clabel][2])
        return numpy.array(arr)
コード例 #11
0
    def getDataArray(self):

        arr = []
        for r in self.camera:
            raft = cameraGeom.cast_Raft(r)
            rlabel = raft.getId().getName()
            for c in raft:
                ccd = cameraGeom.cast_Ccd(c)
                clabel = ccd.getId().getName()
                arr.append(self.data[rlabel][clabel][2])
        return numpy.array(arr)
コード例 #12
0
 def process(self, clipboard):
     """
     """
     self.log.log(Log.INFO, "Doing Saturation correction.")
     
     #grab exposure from clipboard
     exposure = clipboard.get(self.policy.getString("inputKeys.exposure"))
     exposure = ipIsr.convertImageForIsr(exposure)
     fwhm = self.policy.getDouble("parameters.defaultFwhm")
     amp = cameraGeom.cast_Amp(exposure.getDetector())
     dataBbox = amp.getDataSec(True)
     x = dataBbox.getMinX()
     y = dataBbox.getMinY()
     height = dataBbox.getMaxY() - dataBbox.getMinY()
     width = dataBbox.getMaxY() - dataBbox.getMinX()
     dl = measAlg.DefectListT()
     defectList = cameraGeom.cast_Ccd(amp.getParent()).getDefects()
     if amp.getId().getIndex()[1] == 1:
         for d in defectList:
             d1 = measAlg.Defect(d.getBBox())
             d1.shift(-x, -y)
             bbox = d1.getBBox()
             if bbox.getMinX() - 4 > width or bbox.getMaxY() - 4 < 0 or \
                 height - bbox.getMinY() - 1 > height or height - bbox.getMaxY() - 1 < 0:
                 pass
             else:
                 nd = measAlg.Defect(afwGeom.Box2I(
                     afwGeom.Point2I(bbox.getMinX() + 4, height - bbox.getMinY() + 1),
                     bbox.getDimensions()))
                 dl.append(nd)
     else:
         for d in defectList:
             d1 = measAlg.Defect(d.getBBox())
             d1.shift(-x, -y)
             bbox = d1.getBBox()
             if bbox.getMinX() - 4 > width or bbox.getMaxY() - 4 < 0 or \
                 bbox.getMinY() - 1 > height or bbox.getMaxY() - 1 < 0:
                 pass
             else:
                 nd = measAlg.Defect(afwGeom.Box2I(
                     bbox.getMin() + afwGeom.Extent2I(4, 1),
                     bbox.getDimensions()))
                 dl.append(nd)
     saturation = amp.getElectronicParams().getSaturationLevel()
     ipIsr.maskBadPixelsDef(exposure, dl,
             fwhm, interpolate=True, maskName='BAD')
     ipIsr.saturationCorrection(exposure, int(saturation), fwhm,
             growSaturated=False, interpolate=True)
     #ds9.mtv(exposure, frame = 0, title = "my Amp")
     #exposure.writeFits("Amp.fits")
     #output products
     clipboard.put(self.policy.get("outputKeys.saturationCorrectedExposure"),
             exposure)
コード例 #13
0
ファイル: detectorUtil.py プロジェクト: laurenam/pipe_tasks
def getCcd(exposure, allowRaise=True):
    """Get the Ccd referred to by an exposure

    @param exposure Exposure to inspect
    @param allowRaise  If False, return None if the CCD can't be found rather than raising an exception
    @returns Ccd
    """
    det = exposure.getDetector()
    ccd = cameraGeom.cast_Ccd(det)
    if ccd is not None:
        return ccd
    amp = cameraGeom.cast_Amp(det)
    if amp is not None:
        det = amp.getParent()
        ccd = cameraGeom.cast_Ccd(det)
        return ccd

    if allowRaise:
        raise RuntimeError("Can't find Ccd from detector.")
    else:
        return None
コード例 #14
0
def main(rootDir, tract, visits, ccds=None, showPatch=False):

    butler = dafPersist.Butler(rootDir)

    ##################
    ###  draw the CCDs
    ras, decs = [], []
    for i_v, visit in enumerate(visits):
        print i_v, visit
        ccdList = [camGeom.cast_Ccd(ccd) for ccd in camGeom.cast_Raft(butler.get("camera")[0])]
        for ccd in ccdList:
            bbox = ccd.getAllPixels()
            ccdId = ccd.getId().getSerial()

            if (ccds is None or ccdId in ccds) and ccdId < 104:
                dataId = {'tract': 0, 'visit': visit, 'ccd': ccdId}
                try:
                    wcs = afwImage.makeWcs(butler.get("calexp_md", dataId))
                except:
                    pass
                ra, dec = bboxToRaDec(bbox, wcs)
                ras += ra
                decs += dec
                color = ('r', 'b', 'c', 'g', 'm')[i_v%5]
                pyplot.fill(ra, dec, fill=True, alpha=0.2, color=color, edgecolor=color)

    buff = 0.1
    xlim = max(ras)+buff, min(ras)-buff
    ylim = min(decs)-buff, max(decs)+buff

    ###################
    ### draw the skymap
    if showPatch:
        skymap = butler.get('deepCoadd_skyMap', {'tract':0})
        for tract in skymap:
            for patch in tract:
                ra, dec = bboxToRaDec(patch.getInnerBBox(), tract.getWcs())
                pyplot.fill(ra, dec, fill=False, edgecolor='k', lw=1, linestyle='dashed')
                if xlim[1] < percent(ra) < xlim[0] and ylim[0] < percent(dec) < ylim[1]:
                    pyplot.text(percent(ra), percent(dec, 0.9), str(patch.getIndex()),
                                fontsize=6, horizontalalignment='center', verticalalignment='top')


    ######################
    ### add labels as save
    ax = pyplot.gca()
    ax.set_xlabel("R.A. (deg)")
    ax.set_ylabel("Decl. (deg)")
    ax.set_xlim(xlim)
    ax.set_ylim(ylim)
    fig = pyplot.gcf()
    fig.savefig("patches.png")
コード例 #15
0
    def process(self, clipboard):
        """
        """
        self.log.log(Log.INFO, "Doing CCD defect mask.")
        
        #grab exposure from clipboard
        exposure = clipboard.get(self.policy.getString("inputKeys.ccdExposure"))
        #get known defects from camera class
        defectBaseList = cameraGeom.cast_Ccd(exposure.getDetector()).getDefects()
        id = exposure.getDetector().getId()
        defectList = measAlg.DefectListT()
        #create master list of defects and add those from the camera class
        for d in defectBaseList:
            bbox = d.getBBox()
            nd = measAlg.Defect(bbox)
            defectList.append(nd)
        fwhm = self.policy.getDouble("parameters.defaultFwhm")
        #get saturated pixels from the mask
        sdefects = ipIsr.defectListFromMask(exposure, growFootprints=1, maskName='SAT')
        #mask bad pixels in the camera class
        ipIsr.maskBadPixelsDef(exposure, defectList,
            fwhm, interpolate=False, maskName='BAD')
        #add saturated pixels to master defect list
	for d in sdefects:
            bbox = d.getBBox()
	    nd = measAlg.Defect(bbox)
	    defectList.append(nd)
        #find unmasked bad pixels and mask them
        exposure.getMaskedImage().getMask().addMaskPlane("UNMASKEDNAN")	
        unc = ipIsr.UnmaskedNanCounterF()
        unc.apply(exposure.getMaskedImage())
        nnans = unc.getNpix()
        metadata = exposure.getMetadata()
        metadata.set("NUMNANS", nnans)
        if nnans == 0:
            self.log.log(Log.INFO, "Zero unmasked nans/infs were found, which is good.")
        else:
            self.log.log(Log.INFO, "%i unmasked nans/infs found in ccd exposure: %s"%(nnans, id.__str__()))
        #get footprints of bad pixels not in the camera class
        undefects = ipIsr.defectListFromMask(exposure, growFootprints=0, maskName='UNMASKEDNAN')
	for d in undefects:
            bbox = d.getBBox()
	    nd = measAlg.Defect(bbox)
	    defectList.append(nd)
        #interpolate all bad pixels
	ipIsr.interpolateDefectList(exposure, defectList, fwhm)
	
        #output products
        clipboard.put(self.policy.get("outputKeys.defectMaskedCcdExposure"),
                exposure)
コード例 #16
0
ファイル: utils.py プロジェクト: surhudm/hscTools
    def __init__(self, figure, camera, rect=(0.08, 0.08, 0.84, 0.84)):
        self.figure  = figure
        self.camera  = camera
        self.rect    = rect
        self.detectors = {}
        self.amps      = {}
        
        # use same rect as figure.add_axes(rect) to avoid confusion
        l, b, w, h = self.rect
        self.fig_left   = l
        self.fig_bottom = b
        self.fig_width  = w
        self.fig_height = h

        l_cam, b_cam, r_cam, t_cam = 1.0e6, 1.0e6, -1.0e6, -1.0e6
        for r in self.camera:
            raft = camGeom.cast_Raft(r)
            for c in raft:
                ccd = camGeom.cast_Ccd(c)
                serial = ccd.getId().getSerial()
                self.detectors[serial] = ccd

                cc       = ccd.getCenter().getPixels(ccd.getPixelSize())
                cxc, cyc = cc.getX(), cc.getY()
                cbbox    = ccd.getAllPixels(True)
                cwidth   = cbbox.getMaxX() - cbbox.getMinX()
                cheight  = cbbox.getMaxY() - cbbox.getMinY()

                l_cam = min(l_cam, cxc - cwidth/2)
                b_cam = min(b_cam, cyc - cheight/2)
                r_cam = max(r_cam, cxc + cwidth/2)
                t_cam = max(t_cam, cyc + cheight/2)
                
                self.amps[serial] = []
                for a in ccd:
                    amp = camGeom.cast_Amp(a)
                    self.amps[serial].append(amp)


        self.camera_width  = r_cam - l_cam
        self.camera_height = t_cam - b_cam

        extent = afwGeom.Extent2D(self.camera_width, self.camera_height)
        self.camera_bbox   = afwGeom.Box2D(afwGeom.Point2D(0.0, 0.0), extent)

        #print "Camera", self.camera_bbox
        
                    
        self.axes = {}
コード例 #17
0
ファイル: utils.py プロジェクト: surhudm/hscTools
    def __init__(self, camera, scale=16.0):
        self.camera = camera
        self.detectors = {}
        self.amps = {}

        self.xy0 = {}
        self.dims = {}
        self.used = set()
        self.nQuarter = {}

        l_cam, b_cam, r_cam, t_cam = 1.0e6, 1.0e6, -1.0e6, -1.0e6
        for r in self.camera:
            raft = camGeom.cast_Raft(r)
            for c in raft:
                ccd = camGeom.cast_Ccd(c)
                serial = ccd.getId().getSerial()
                self.detectors[serial] = ccd

                cc = ccd.getCenter().getPixels(ccd.getPixelSize())
                cxc, cyc = cc.getX(), cc.getY()
                cbbox = ccd.getAllPixels(True)
                cwidth = cbbox.getMaxX() - cbbox.getMinX()
                cheight = cbbox.getMaxY() - cbbox.getMinY()

                x0, y0 = int(cxc - cwidth / 2.0), int(cyc - cheight / 2.0)

                self.xy0[serial] = (x0, y0)
                self.dims[serial] = (cwidth / scale, cheight / scale)
                self.nQuarter[serial] = ccd.getOrientation().getNQuarter()

                l_cam = min(l_cam, x0)
                b_cam = min(b_cam, y0)
                r_cam = max(r_cam, cxc + cwidth / 2)
                t_cam = max(t_cam, cyc + cheight / 2)

        for k, v in self.xy0.items():
            self.xy0[k] = [
                int(x)
                for x in ((v[0] - l_cam) / scale, (v[1] - b_cam) / scale)
            ]

        self.camera_width = r_cam - l_cam
        self.camera_height = t_cam - b_cam

        extent = afwGeom.Extent2D(self.camera_width, self.camera_height)
        self.camera_bbox = afwGeom.Box2D(afwGeom.Point2D(0.0, 0.0), extent)

        self.image = numpy.zeros(
            (self.camera_height / scale, self.camera_width / scale))
コード例 #18
0
ファイル: getRaw.py プロジェクト: jonathansick-shadow/obs_sst
    def testRaw(self):
        """Test retrieval of raw image"""
        for ccd in range(12):
            raw = self.butler.get("raw", self.dataId, ccd=ccd)

            self.assertExposure(raw, ccd)

            if display:
                ccd = cameraGeom.cast_Ccd(raw.getDetector())
                for amp in ccd:
                    amp = cameraGeom.cast_Amp(amp)
                    print ccd.getId(), amp.getId(), amp.getDataSec().toString(), \
                          amp.getBiasSec().toString(), amp.getElectronicParams().getGain()
                cameraGeomUtils.showCcd(ccd, ccdImage=raw, frame=frame)
                frame += 1
コード例 #19
0
ファイル: getRaw.py プロジェクト: lsst/obs_wiyn
    def testRaw(self):
        """Test retrieval of raw image"""
        ccd = 'VIRGO1'
        raw = self.butler.get("raw", self.dataId, ccd=ccd)

        self.assertExposure(raw, ccd)

        if display:
            ccd = cameraGeom.cast_Ccd(raw.getDetector())
            for amp in ccd:
                amp = cameraGeom.cast_Amp(amp)
                print ccd.getId(), amp.getId(), amp.getDataSec().toString(), \
                      amp.getBiasSec().toString(), amp.getElectronicParams().getGain()
            cameraGeomUtils.showCcd(ccd, ccdImage=raw, frame=frame)
            frame += 1
コード例 #20
0
 def validate(self):
     # Since we establish the structure of data in __init__, it
     # should always be valid.  Unless someone mucks with self.data
     # directly...
     for r in self.camera:
         raft   = cameraGeom.cast_Raft(r)
         rlabel = raft.getId().getName()
         if not self.data.has_key(rlabel):
             return False
         for c in raft:
             ccd    = cameraGeom.cast_Ccd(c)
             clabel = ccd.getId().getName()
             if not self.data[rlabel].has_key(clabel):
                 return False
     return True
コード例 #21
0
ファイル: utils.py プロジェクト: RayPlante/DMS.afw
def trimExposure(ccdImage, ccd=None):
    """Trim a raw CCD Exposure"""

    if not ccd:
        ccd = cameraGeom.cast_Ccd(ccdImage.getDetector())
    
    dim = ccd.getAllPixelsNoRotation(True).getDimensions()
    trimmedImage = ccdImage.Factory(dim)
    for a in ccd:
        data = ccdImage.Factory(ccdImage, a.getDataSec(False), afwImage.LOCAL).getMaskedImage()
        tdata = trimmedImage.Factory(trimmedImage, a.getDataSec(True), afwImage.LOCAL).getMaskedImage()
        tdata <<= data

    ccd.setTrimmed(True)
    return trimmedImage
コード例 #22
0
 def validate(self):
     # Since we establish the structure of data in __init__, it
     # should always be valid.  Unless someone mucks with self.data
     # directly...
     for r in self.camera:
         raft = cameraGeom.cast_Raft(r)
         rlabel = raft.getId().getName()
         if not self.data.has_key(rlabel):
             return False
         for c in raft:
             ccd = cameraGeom.cast_Ccd(c)
             clabel = ccd.getId().getName()
             if not self.data[rlabel].has_key(clabel):
                 return False
     return True
コード例 #23
0
ファイル: pipette.py プロジェクト: HyperSuprime-Cam/distEst
 def testInvariance(self):
     for raft in self.camera:
         raft = cameraGeom.cast_Raft(raft)
         for ccd in raft:
             ccd = cameraGeom.cast_Ccd(ccd)
             dist = pipDist.createDistortion(ccd, self.config)
             self.assertTrue(isinstance(dist, hscDist.HscDistortion))
             size = ccd.getSize()
             height, width = size.getX(), size.getY()
             for x, y in ((0.0,0.0), (0.0, height), (width, 0.0), (width, height), (width/2.0,height/2.0)):
                 forward = dist.actualToIdeal(afwGeom.PointD(x, y))
                 backward = dist.idealToActual(forward)
                 diff = math.hypot(backward.getX() - x, backward.getY() - y)
                 self.assertLess(diff, TOLERANCE, "Not invariant: %s %f,%f --> %s --> %s (%f > %f)" % \
                                 (ccd.getId().getSerial(), x, y, forward, backward, diff, TOLERANCE))
コード例 #24
0
 def process(self, clipboard):
     """
     """
     self.log.log(Log.INFO, "Doing CCD assembly.")
     
     #grab exposure from clipboard
     exposureList = clipboard.get(self.policy.getString("inputKeys.exposureList"))
     rmKeys = self.policy.getArray("parameters.deleteFieldsList")
     amp = cameraGeom.cast_Amp(exposureList[0].getDetector())
     ccdId = amp.getParent().getId()
     ccd = cameraGeom.cast_Ccd(amp.getParent())
     exposure = ipIsr.ccdAssemble.assembleCcd(exposureList, ccd,
             keysToRemove=rmKeys)
     #output products
     clipboard.put(self.policy.get("outputKeys.assembledCcdExposure"),
             exposure)
コード例 #25
0
ファイル: utils.py プロジェクト: surhudm/hscTools
    def __init__(self, figure, camera, rect=(0.08, 0.08, 0.84, 0.84)):
        self.figure = figure
        self.camera = camera
        self.rect = rect
        self.detectors = {}
        self.amps = {}

        # use same rect as figure.add_axes(rect) to avoid confusion
        l, b, w, h = self.rect
        self.fig_left = l
        self.fig_bottom = b
        self.fig_width = w
        self.fig_height = h

        l_cam, b_cam, r_cam, t_cam = 1.0e6, 1.0e6, -1.0e6, -1.0e6
        for r in self.camera:
            raft = camGeom.cast_Raft(r)
            for c in raft:
                ccd = camGeom.cast_Ccd(c)
                serial = ccd.getId().getSerial()
                self.detectors[serial] = ccd

                cc = ccd.getCenter().getPixels(ccd.getPixelSize())
                cxc, cyc = cc.getX(), cc.getY()
                cbbox = ccd.getAllPixels(True)
                cwidth = cbbox.getMaxX() - cbbox.getMinX()
                cheight = cbbox.getMaxY() - cbbox.getMinY()

                l_cam = min(l_cam, cxc - cwidth / 2)
                b_cam = min(b_cam, cyc - cheight / 2)
                r_cam = max(r_cam, cxc + cwidth / 2)
                t_cam = max(t_cam, cyc + cheight / 2)

                self.amps[serial] = []
                for a in ccd:
                    amp = camGeom.cast_Amp(a)
                    self.amps[serial].append(amp)

        self.camera_width = r_cam - l_cam
        self.camera_height = t_cam - b_cam

        extent = afwGeom.Extent2D(self.camera_width, self.camera_height)
        self.camera_bbox = afwGeom.Box2D(afwGeom.Point2D(0.0, 0.0), extent)

        #print "Camera", self.camera_bbox

        self.axes = {}
コード例 #26
0
 def reset(self, data=None):
     """Set all values in data dictionary to None."""
     
     if data is None:
         data = self.data
         
     for r in self.camera:
         raft   = cameraGeom.cast_Raft(r)
         rlabel = raft.getId().getName()
         data[rlabel] = {}
         for c in raft:
             ccd    = cameraGeom.cast_Ccd(c)
             clabel = ccd.getId().getName()
             self.idByName[clabel] = ccd.getId()
             data[rlabel][clabel] = None
             areaLabel = self.getAreaLabel(rlabel, clabel)
             self.raftCcdByAreaLabel[areaLabel] = [rlabel, clabel]
コード例 #27
0
    def reset(self, data=None):
        """Set all values in data dictionary to None."""

        if data is None:
            data = self.data

        for r in self.camera:
            raft = cameraGeom.cast_Raft(r)
            rlabel = raft.getId().getName()
            data[rlabel] = {}
            for c in raft:
                ccd = cameraGeom.cast_Ccd(c)
                clabel = ccd.getId().getName()
                self.idByName[clabel] = ccd.getId()
                data[rlabel][clabel] = None
                areaLabel = self.getAreaLabel(rlabel, clabel)
                self.raftCcdByAreaLabel[areaLabel] = [rlabel, clabel]
コード例 #28
0
ファイル: utils.py プロジェクト: surhudm/hscTools
    def __init__(self, camera, scale=16.0):
        self.camera  = camera
        self.detectors = {}
        self.amps      = {}

        self.xy0 = {}
        self.dims = {}
        self.used = set()
        self.nQuarter = {}
        
        l_cam, b_cam, r_cam, t_cam = 1.0e6, 1.0e6, -1.0e6, -1.0e6
        for r in self.camera:
            raft = camGeom.cast_Raft(r)
            for c in raft:
                ccd = camGeom.cast_Ccd(c)
                serial = ccd.getId().getSerial()
                self.detectors[serial] = ccd

                cc       = ccd.getCenter().getPixels(ccd.getPixelSize())
                cxc, cyc = cc.getX(), cc.getY()
                cbbox    = ccd.getAllPixels(True)
                cwidth   = cbbox.getMaxX() - cbbox.getMinX()
                cheight  = cbbox.getMaxY() - cbbox.getMinY()

                x0, y0 = int(cxc - cwidth/2.0), int(cyc - cheight/2.0)

                self.xy0[serial] = (x0, y0)
                self.dims[serial] = (cwidth/scale, cheight/scale)
                self.nQuarter[serial] = ccd.getOrientation().getNQuarter()
                
                l_cam = min(l_cam, x0)
                b_cam = min(b_cam, y0)
                r_cam = max(r_cam, cxc + cwidth/2)
                t_cam = max(t_cam, cyc + cheight/2)

        for k,v in self.xy0.items():
            self.xy0[k] = [int(x) for x in ((v[0] - l_cam)/scale, (v[1] - b_cam)/scale)]
            
        self.camera_width  = r_cam - l_cam
        self.camera_height = t_cam - b_cam

        extent = afwGeom.Extent2D(self.camera_width, self.camera_height)
        self.camera_bbox   = afwGeom.Box2D(afwGeom.Point2D(0.0, 0.0), extent)

        self.image = numpy.zeros((self.camera_height/scale, self.camera_width/scale))
コード例 #29
0
    def setMapInfo(self):
        """Establish map areas for any defined CCDs"""

        for r in self.camera:
            raft = cameraGeom.cast_Raft(r)
            rlabel = raft.getId().getName()
            for c in raft:
                ccd = cameraGeom.cast_Ccd(c)
                clabel = ccd.getId().getName()
                info = self.map[rlabel][clabel]

                if ((info is not None) and self.ccdBoundaries.has_key(clabel)
                        and (self.ccdBoundaries[clabel] is not None)):
                    bound = self.ccdBoundaries[clabel]
                    x0, x1 = bound[0]
                    y0, y1 = bound[1]
                    label = self.getAreaLabel(rlabel, clabel)
                    self.addMapArea(label, [x0, y0, x1, y1], info)
コード例 #30
0
    def process(self, clipboard):
        """
        """
        self.log.log(Log.INFO, "Doing CCD assembly.")

        #grab exposure from clipboard
        exposureList = clipboard.get(
            self.policy.getString("inputKeys.exposureList"))
        rmKeys = self.policy.getArray("parameters.deleteFieldsList")
        amp = cameraGeom.cast_Amp(exposureList[0].getDetector())
        ccdId = amp.getParent().getId()
        ccd = cameraGeom.cast_Ccd(amp.getParent())
        exposure = ipIsr.ccdAssemble.assembleCcd(exposureList,
                                                 ccd,
                                                 keysToRemove=rmKeys)
        #output products
        clipboard.put(self.policy.get("outputKeys.assembledCcdExposure"),
                      exposure)
コード例 #31
0
ファイル: cameraGeom.py プロジェクト: RayPlante/DMS.afw
    def testParent(self):
        """Test that we can find our parent"""

        cameraInfo = {"ampSerial" : CameraGeomTestCase.ampSerial}
        camera = cameraGeomUtils.makeCamera(self.geomPolicy, cameraInfo=cameraInfo)

        for rx, ry, cx, cy, serial in [(0, 0,     0,   0,   4),
                                       (0,   0,   150, 250, 20),
                                       (600, 300, 0,   0,   52),
                                       (600, 300, 150, 250, 68),
                                       ]:
            raft = cameraGeom.cast_Raft(camera.findDetectorPixel(afwGeom.Point2D(rx, ry)))
            ccd = cameraGeom.cast_Ccd(raft.findDetectorPixel(afwGeom.Point2D(cx, cy)))

            amp = ccd[0]
            self.assertEqual(ccd.getId(),    amp.getParent().getId())
            self.assertEqual(raft.getId(),   ccd.getParent().getId())
            self.assertEqual(camera.getId(), ccd.getParent().getParent().getId())
            self.assertEqual(None,           ccd.getParent().getParent().getParent())        
コード例 #32
0
ファイル: findTract.py プロジェクト: surhudm/hscTools
    def run(self, dataRef):

        calexp_md = dataRef.get("calexp_md")
        wcs = afwImage.makeWcs(calexp_md)
        skymap = dataRef.get("deepCoadd_skyMap", immediate=True)

        # all this, just to get the center pixel coordinate
        camera = dataRef.get("camera")
        raft = camGeom.cast_Raft(camera.findDetector(camGeom.Id(0)))
        detId = camGeom.Id(calexp_md.get("DET-ID"))
        ccd = camGeom.cast_Ccd(raft.findDetector(detId))
        size = ccd.getSize().getPixels(ccd.getPixelSize())

        coord = wcs.pixelToSky(size.getX() / 2, size.getY() / 2)
        tract = skymap.findTract(coord).getId()

        d = dataRef.dataId
        print "%-6d %3d  %5d" % (d["visit"], d["ccd"], tract)
        return tract
コード例 #33
0
ファイル: findTract.py プロジェクト: surhudm/hscTools
    def run(self, dataRef):

        calexp_md = dataRef.get("calexp_md")
        wcs = afwImage.makeWcs(calexp_md)
        skymap = dataRef.get("deepCoadd_skyMap", immediate=True)

        # all this, just to get the center pixel coordinate
        camera = dataRef.get("camera")
        raft = camGeom.cast_Raft(camera.findDetector(camGeom.Id(0)))
        detId = camGeom.Id(calexp_md.get("DET-ID"))
        ccd = camGeom.cast_Ccd(raft.findDetector(detId))
        size = ccd.getSize().getPixels(ccd.getPixelSize())

        coord = wcs.pixelToSky(size.getX() / 2, size.getY() / 2)
        tract = skymap.findTract(coord).getId()

        d = dataRef.dataId
        print "%-6d %3d  %5d" % (d['visit'], d['ccd'], tract)
        return tract
コード例 #34
0
 def setMapInfo(self):
     """Establish map areas for any defined CCDs"""
     
     for r in self.camera:
         raft = cameraGeom.cast_Raft(r)
         rlabel = raft.getId().getName()
         for c in raft:
             ccd = cameraGeom.cast_Ccd(c)
             clabel = ccd.getId().getName()
             info = self.map[rlabel][clabel]
             
             if ((info is not None) and
                 self.ccdBoundaries.has_key(clabel) and
                 (self.ccdBoundaries[clabel] is not None)):
                 bound = self.ccdBoundaries[clabel]
                 x0, x1 = bound[0]
                 y0, y1 = bound[1]
                 label = self.getAreaLabel(rlabel, clabel)
                 self.addMapArea(label, [x0, y0, x1, y1], info)
コード例 #35
0
ファイル: distort.py プロジェクト: RobertLuptonTheGood/afw
    def testDistortionInACamera(self):

	policyFile = cameraGeomUtils.getGeomPolicy(os.path.join(eups.productDir("afw"),
								"tests", "TestCameraGeom.paf"))
	pol = pexPolicy.Policy(policyFile)
	pol = cameraGeomUtils.getGeomPolicy(pol)
	cam = cameraGeomUtils.makeCamera(pol)

	# see if the distortion object made it into the camera object
	dist = cam.getDistortion()
	self.tryAFewCoords(dist, [1.0, 1.0, 0.1])

	# see if the distortion object is accessible in the ccds
	for raft in cam:
	    for ccd in cameraGeom.cast_Raft(raft):
		ccd = cameraGeom.cast_Ccd(ccd)
		if self.prynt:
		    print "CCD id: ", ccd.getId()
		ccdDist = ccd.getDistortion()
		self.tryAFewCoords(dist, [1.0, 1.0, 0.1])
コード例 #36
0
ファイル: getRaw.py プロジェクト: jchiang87/obs_decam
    def testRaw(self):
        """Test retrieval of raw image"""
        frame = 0
        if display:
            cameraGeomUtils.showCamera(self.butler.mapper.camera, frame=frame)

        for side in ("N", "S"):
            for ccd in range(1, 32, 1):
                raw = self.butler.get("raw", self.dataId, side=side, ccd=ccd)

                self.assertExposure(raw, side, ccd)

                if display:
                    frame += 1
                    ccd = cameraGeom.cast_Ccd(raw.getDetector())
                    for amp in ccd:
                        amp = cameraGeom.cast_Amp(amp)
                        print ccd.getId(), amp.getId(), amp.getDataSec().toString(), \
                              amp.getBiasSec().toString(), amp.getElectronicParams().getGain()
                    cameraGeomUtils.showCcd(ccd, ccdImage=raw, frame=frame)
コード例 #37
0
    def testDistortionInACamera(self):

        policyFile = cameraGeomUtils.getGeomPolicy(
            os.path.join(eups.productDir("afw"), "tests",
                         "TestCameraGeom.paf"))
        pol = pexPolicy.Policy(policyFile)
        pol = cameraGeomUtils.getGeomPolicy(pol)
        cam = cameraGeomUtils.makeCamera(pol)

        # see if the distortion object made it into the camera object
        dist = cam.getDistortion()
        self.tryAFewCoords(dist, [1.0, 1.0, 0.1])

        # see if the distortion object is accessible in the ccds
        for raft in cam:
            for ccd in cameraGeom.cast_Raft(raft):
                ccd = cameraGeom.cast_Ccd(ccd)
                if self.prynt:
                    print "CCD id: ", ccd.getId()
                ccdDist = ccd.getDistortion()
                self.tryAFewCoords(dist, [1.0, 1.0, 0.1])
コード例 #38
0
ファイル: utils.py プロジェクト: RobertLuptonTheGood/afw
def showRaft(raft, imageSource=SynthesizeCcdImage(), raftOrigin=None, frame=None, overlay=True, bin=1):
    """Show a Raft on ds9.

If imageSource isn't None, create an image using the images specified by imageSource"""

    raftCenter = afwGeom.Point2I(raft.getAllPixels().getDimensions()/2)
    if raftOrigin:
        raftCenter += afwGeom.ExtentI(int(raftOrigin[0]), int(raftOrigin[1]))

    if imageSource is None:
        raftImage = None
    elif isinstance(imageSource, GetCcdImage):
        raftImage = makeImageFromRaft(raft, imageSource=imageSource, raftCenter=raftCenter, bin=bin)
    else:
        raftImage = imageSource

    if raftImage:
        ds9.mtv(raftImage, frame=frame, title=raft.getId().getName())

    if not raftImage and not overlay:
        return

    with ds9.Buffering():
        for det in raft:
            ccd = cameraGeom.cast_Ccd(det)

            bbox = ccd.getAllPixels(True)
            origin = ccd.getCenterPixel() - \
                    afwGeom.ExtentD(bbox.getWidth()/2 - raftCenter.getX(), 
                                    bbox.getHeight()/2 - raftCenter.getY())

            if True:
                name = ccd.getId().getName()
            else:
                name = str(ccd.getCenter())

            ds9.dot(name, (origin[0] + 0.5*bbox.getWidth())/bin,
                          (origin[1] + 0.4*bbox.getHeight())/bin, frame=frame)

            showCcd(ccd, None, isTrimmed=True, frame=frame, ccdOrigin=origin, overlay=overlay, bin=bin)
コード例 #39
0
 def fixDefectsAndSat(self, masterFrame, detector):
     fwhm = self.config.fwhm
     dataBbox = detector.getDataSec(True)
     #Reversing the x and y is essentially a hack since we have to apply the defects in Amp coordinates and they are recorded in chip coordinates
     #This should go away when the data from imSim is all in chip coordinates
     y = dataBbox.getMinX()
     x = dataBbox.getMinY()
     height = dataBbox.getDimensions()[0]
     #Should when at detector level, there will not be the need to go through the step of getting the parent
     defectList = cameraGeom.cast_Ccd(detector.getParent()).getDefects() 
     dl = self.transposeDefectList(defectList, dataBbox)
     for d in dl:
         d.shift(-x, -y)
         if detector.getId()>8:
             d.shift(0, height - 2*d.getBBox().getMinY()-d.getBBox().getHeight())
     #Should saturation be interpolated as well?
     #sdl = self.isr.getDefectListFromMask(masterFrame, 'SAT', growFootprints=0)
     #for d in sdl:
     #    dl.push_back(d)
     self.isr.maskPixelsFromDefectList(masterFrame, dl, maskName='BAD')
     self.isr.interpolateDefectList(masterFrame, dl, fwhm)
     return masterFrame
コード例 #40
0
ファイル: testButler.py プロジェクト: fjammes/obs_cfht
    def assertExposure(self, exp, ccd, checkFilter=True):
        print "dataId: ", self.dataId
        print "ccd: ", ccd
        print "width: ", exp.getWidth()
        print "height: ", exp.getHeight()
        print "detector name: ", exp.getDetector().getName()
        print "filter name: ", exp.getFilter().getFilterProperty().getName()

        self.assertEqual(exp.getWidth(), self.size[0])
        self.assertEqual(exp.getHeight(), self.size[1])
        self.assertEqual(exp.getDetector().getName(), "ccd%02d" % ccd)
        if checkFilter:
            self.assertEqual(exp.getFilter().getFilterProperty().getName(), self.filter)

        if display and ccd % 18 == 0:
            global frame
            frame += 1
            ccd = cameraGeom.cast_Ccd(exp.getDetector())
            for amp in ccd:
                amp = cameraGeom.cast_Amp(amp)
                print ccd.getId(), amp.getId(), amp.getDataSec().toString(), \
                      amp.getBiasSec().toString(), amp.getElectronicParams().getGain()
            cameraGeomUtils.showCcd(ccd, ccdImage=exp, frame=frame)
コード例 #41
0
ファイル: display.py プロジェクト: HyperSuprime-Cam/distEst
def main(camera, distortionConfig):
    fig = plt.figure(1)
    fig.clf()
    ax = fig.add_axes((0.1, 0.1, 0.8, 0.8))
#    ax.set_autoscale_on(False)
#    ax.set_ybound(lower=-0.2, upper=0.2)
#    ax.set_xbound(lower=-17, upper=-7)
    ax.set_title('Distorted CCDs')
    
    for raft in camera:
        raft = cameraGeom.cast_Raft(raft)
        for ccd in raft:
            ccd = cameraGeom.cast_Ccd(ccd)
            size = ccd.getSize()
            width, height = 2048, 4096
            dist = pipDist.createDistortion(ccd, distortionConfig)
            corners = ((0.0,0.0), (0.0, height), (width, height), (width, 0.0), (0.0, 0.0))
            for (x0, y0), (x1, y1) in zip(corners[0:4],corners[1:5]):
                if x0 == x1 and y0 != y1:
                    yList = numpy.linspace(y0, y1, num=SAMPLE)
                    xList = [x0] * len(yList)
                elif y0 == y1 and x0 != x1:
                    xList = numpy.linspace(x0, x1, num=SAMPLE)
                    yList = [y0] * len(xList)
                else:
                    raise RuntimeError("Should never get here")

                xDistort = []; yDistort = []
                for x, y in zip(xList, yList):
                    distorted = dist.actualToIdeal(afwGeom.Point2D(x, y))
                    xDistort.append(distorted.getX())
                    yDistort.append(distorted.getY())

                ax.plot(xDistort, yDistort, 'k-')

    plt.show()
コード例 #42
0
def cameraToRectangles(camera):
    rectangles = {}
    centers = {}
    raftBoundaries = []
    ccdBoundaries = {}

    
    for r in camera:
        raft = cameraGeom.cast_Raft(r)

        # NOTE: all ccd coords are w.r.t. the *center* of the raft, not its LLC
        rxc     = raft.getCenterPixel().getX()
        ryc     = raft.getCenterPixel().getY()

        xmin = +1e10
        ymin = +1e10
        xmax = -1e10
        ymax = -1e10
        for c in raft:
            ccd   = cameraGeom.cast_Ccd(c)
            label = ccd.getId().getName()

            cxc     = ccd.getCenterPixel().getX()
            cyc     = ccd.getCenterPixel().getY()
            orient  = ccd.getOrientation()
            nQuart  = ccd.getOrientation().getNQuarter()
            yaw     = orient.getYaw()

            cbbox   = ccd.getAllPixels(True)
            cwidth  = cbbox.getMaxX() - cbbox.getMinX()
            cheight = cbbox.getMaxY() - cbbox.getMinY()
            if abs(yaw.asRadians() - numpy.pi/2.0) < 1.0e-3:  # nQuart == 1 or nQuart == 3:
                ctmp = cwidth
                cwidth = cheight
                cheight = ctmp

            cx0     = rxc + cxc - cwidth/2
            cy0     = ryc + cyc - cheight/2
            cx1     = cx0 + cwidth
            cy1     = cy0 + cheight

            if cx0 < xmin:
                xmin = cx0
            if cx1 > xmax:
                xmax = cx1
            if cy0 < ymin:
                ymin = cy0
            if cy1 > ymax:
                ymax = cy1

            crectangle = Rectangle((cx0, cy0), cwidth, cheight, fill = False, label = label)
            rectangles[label] = crectangle
            centers[label] = (rxc+cxc, ryc+cyc)

            ccdBoundaries[label] = ((cx0, cx1), (cy0, cy1))
            

        raftBoundaries.append(((xmin, xmin), (ymin, ymax)))
        raftBoundaries.append(((xmin, xmax), (ymin, ymin)))
        raftBoundaries.append(((xmin, xmax), (ymax, ymax)))
        raftBoundaries.append(((xmax, xmax), (ymin, ymax)))

    return centers, rectangles, raftBoundaries, ccdBoundaries
コード例 #43
0
def main(rootDir, tract, visits, ccds=None, showPatch=False):

    butler = dafPersist.Butler(rootDir)

    ##########################
    ###  CCDs データを呼び込む
    ras, decs = [], []
    for i_v, visit in enumerate(visits):
        print i_v, visit
        ccdList = [
            camGeom.cast_Ccd(ccd)
            for ccd in camGeom.cast_Raft(butler.get("camera")[0])
        ]
        for ccd in ccdList:
            bbox = ccd.getAllPixels()
            ccdId = ccd.getId().getSerial()

            if (ccds is None or ccdId in ccds) and ccdId < 104:
                dataId = {'tract': 0, 'visit': visit, 'ccd': ccdId}
                try:
                    wcs = afwImage.makeWcs(butler.get("calexp_md", dataId))
                except:
                    pass
                ra, dec = bboxToRaDec(bbox, wcs)
                ras += ra
                decs += dec
                color = ('r', 'b', 'c', 'g', 'm')[i_v % 5]
                pyplot.fill(ra,
                            dec,
                            fill=True,
                            alpha=0.2,
                            color=color,
                            edgecolor=color)

    buff = 0.1
    xlim = max(ras) + buff, min(ras) - buff
    ylim = min(decs) - buff, max(decs) + buff

    ######################
    ### skymap を呼び込む
    if showPatch:
        skymap = butler.get('deepCoadd_skyMap', {'tract': 0})
        for tract in skymap:
            for patch in tract:
                ra, dec = bboxToRaDec(patch.getInnerBBox(), tract.getWcs())
                pyplot.fill(ra,
                            dec,
                            fill=False,
                            edgecolor='k',
                            lw=1,
                            linestyle='dashed')
                if xlim[1] < percent(ra) < xlim[0] and ylim[0] < percent(
                        dec) < ylim[1]:
                    pyplot.text(percent(ra),
                                percent(dec, 0.9),
                                str(patch.getIndex()),
                                fontsize=6,
                                horizontalalignment='center',
                                verticalalignment='top')

    ############################################
    ### png データとして保存するためにラベルをつける
    ax = pyplot.gca()
    ax.set_xlabel("R.A. (deg)")
    ax.set_ylabel("Decl. (deg)")
    ax.set_xlim(xlim)
    ax.set_ylim(ylim)
    fig = pyplot.gcf()
    fig.savefig("patches.png")
コード例 #44
0
    def process(self, clipboard):
        """
        """
        self.log.log(Log.INFO, "Doing CCD defect mask.")

        #grab exposure from clipboard
        exposure = clipboard.get(
            self.policy.getString("inputKeys.ccdExposure"))
        #get known defects from camera class
        defectBaseList = cameraGeom.cast_Ccd(
            exposure.getDetector()).getDefects()
        id = exposure.getDetector().getId()
        defectList = measAlg.DefectListT()
        #create master list of defects and add those from the camera class
        for d in defectBaseList:
            bbox = d.getBBox()
            nd = measAlg.Defect(bbox)
            defectList.append(nd)
        fwhm = self.policy.getDouble("parameters.defaultFwhm")
        #get saturated pixels from the mask
        sdefects = ipIsr.defectListFromMask(exposure,
                                            growFootprints=1,
                                            maskName='SAT')
        #mask bad pixels in the camera class
        ipIsr.maskBadPixelsDef(exposure,
                               defectList,
                               fwhm,
                               interpolate=False,
                               maskName='BAD')
        #add saturated pixels to master defect list
        for d in sdefects:
            bbox = d.getBBox()
            nd = measAlg.Defect(bbox)
            defectList.append(nd)
        #find unmasked bad pixels and mask them
        exposure.getMaskedImage().getMask().addMaskPlane("UNMASKEDNAN")
        unc = ipIsr.UnmaskedNanCounterF()
        unc.apply(exposure.getMaskedImage())
        nnans = unc.getNpix()
        metadata = exposure.getMetadata()
        metadata.set("NUMNANS", nnans)
        if nnans == 0:
            self.log.log(Log.INFO,
                         "Zero unmasked nans/infs were found, which is good.")
        else:
            self.log.log(
                Log.INFO, "%i unmasked nans/infs found in ccd exposure: %s" %
                (nnans, id.__str__()))
        #get footprints of bad pixels not in the camera class
        undefects = ipIsr.defectListFromMask(exposure,
                                             growFootprints=0,
                                             maskName='UNMASKEDNAN')
        for d in undefects:
            bbox = d.getBBox()
            nd = measAlg.Defect(bbox)
            defectList.append(nd)
        #interpolate all bad pixels
        ipIsr.interpolateDefectList(exposure, defectList, fwhm)

        #output products
        clipboard.put(self.policy.get("outputKeys.defectMaskedCcdExposure"),
                      exposure)
コード例 #45
0
class PolypixPsfDeterminer(object):
    ConfigClass = PolypixPsfDeterminerConfig

    def __init__(self, config):
        """
        @param[in] config: instance of PolypixPsfDeterminerConfig
        """
        self.config = config
        # N.b. name of component is meas.algorithms.psfDeterminer so you can turn on psf debugging
        # independent of which determiner is active
        self.debugLog = pexLog.Debug("meas.algorithms.psfDeterminer")
        self.warnLog = pexLog.Log(pexLog.getDefaultLog(), "meas.algorithms.psfDeterminer")

    def determinePsf(self, exposure, psfCandidateList, metadata=None, flagKey=None):
        """
        @param[in] exposure: exposure containing the psf candidates (lsst.afw.image.Exposure)
        @param[in] psfCandidateList: a sequence of PSF candidates (each an lsst.meas.algorithms.PsfCandidate);
            typically obtained by detecting sources and then running them through a star selector
        @param[in,out] metadata  a home for interesting tidbits of information
        @param[in] flagKey: schema key used to mark sources actually used in PSF determination
    
        @return psf
        """

        import lsstDebug
        display = lsstDebug.Info(__name__).display 
        displayExposure = display and \
            lsstDebug.Info(__name__).displayExposure     # display the Exposure + spatialCells 
        displayPsfCandidates = display and \
            lsstDebug.Info(__name__).displayPsfCandidates # show the viable candidates 
        displayPsfComponents = display and \
            lsstDebug.Info(__name__).displayPsfComponents # show the basis functions
        showBadCandidates = display and \
            lsstDebug.Info(__name__).showBadCandidates # Include bad candidates (meaningless, methinks)
        displayResiduals = display and \
            lsstDebug.Info(__name__).displayResiduals         # show residuals
        displayPsfMosaic = display and \
            lsstDebug.Info(__name__).displayPsfMosaic   # show mosaic of reconstructed PSF(x,y)
        matchKernelAmplitudes = lsstDebug.Info(__name__).matchKernelAmplitudes # match Kernel amplitudes for spatial plots
        normalizeResiduals = lsstDebug.Info(__name__).normalizeResiduals # Normalise residuals by object amplitude 

        mi = exposure.getMaskedImage()
        
        nCand = len(psfCandidateList)
        if nCand == 0:
            raise RuntimeError("No PSF candidates supplied.")

        #
        # How big should our PSF models be?
        #
        if display:                     # only needed for debug plots
            # construct and populate a spatial cell set
            bbox = mi.getBBox(afwImage.PARENT)
            psfCellSet = afwMath.SpatialCellSet(bbox, self.config.sizeCellX, self.config.sizeCellY)
        else:
            psfCellSet = None
        
        sizes = np.empty(nCand)
        for i, psfCandidate in enumerate(psfCandidateList):
            try:
                if psfCellSet:
                    psfCellSet.insertCandidate(psfCandidate)
            except Exception, e:
                self.debugLog.debug(2, "Skipping PSF candidate %d of %d: %s" % (i, len(psfCandidateList), e))
                continue

            source = psfCandidate.getSource()
            quad = afwEll.Quadrupole(source.getIxx(), source.getIyy(), source.getIxy())
            rmsSize = quad.getTraceRadius()
            sizes[i] = rmsSize

        if self.config.kernelSize >= 15:
            self.debugLog.debug(1, \
                "WARNING: NOT scaling kernelSize by stellar quadrupole moment, but using absolute value")
            actualKernelSize = int(self.config.kernelSize)
        else:
            actualKernelSize = 2 * int(self.config.kernelSize * np.sqrt(np.median(sizes)) + 0.5) + 1
            if actualKernelSize < self.config.kernelSizeMin:
                actualKernelSize = self.config.kernelSizeMin
            if actualKernelSize > self.config.kernelSizeMax:
                actualKernelSize = self.config.kernelSizeMax
            if display:
                rms = np.median(sizes)
                print "Median PSF RMS size=%.2f pixels (\"FWHM\"=%.2f)" % (rms, 2*np.sqrt(2*np.log(2))*rms)

        # If we manually set the resolution then we need the size in pixel units
        pixKernelSize = actualKernelSize
        if self.config.samplingSize > 0:
            pixKernelSize = int(actualKernelSize*self.config.samplingSize)
            if pixKernelSize % 2 == 0: pixKernelSize += 1 
        self.debugLog.debug(3, "PSF Kernel size=%.2f, Image Kernel Size=%.2f" % 
                            (actualKernelSize,pixKernelSize))

        nside = pixKernelSize // 2
        assert pixKernelSize == (2*nside+1)

        # Set size of image returned around candidate
        psfCandidateList[0].setHeight(pixKernelSize)
        psfCandidateList[0].setWidth(pixKernelSize)

        mask_bits = afwImage.MaskU_getPlaneBitMask(self.config.badMaskBits)            
        fluxName = 'initial.flux.sinc'    # FIXME should be in config? (meas_extensions_psfex has it in a config file)
        fluxFlagName = fluxName + ".flags"

        cs = hscpsfLib.HscCandidateSet(mask_bits, pixKernelSize, pixKernelSize)

        with ds9.Buffering():
            xpos = []; ypos = []
            for i, psfCandidate in enumerate(psfCandidateList):
                source = psfCandidate.getSource()
                xc, yc = source.getX(), source.getY()

                if fluxFlagName in source.schema and source.get(fluxFlagName):
                    continue

                flux = source.get(fluxName)
                if flux < 0 or np.isnan(flux):
                    continue

                cs.add(psfCandidate, i, flux, sizes[i])

                xpos.append(xc); ypos.append(yc) # for QA

                if displayExposure:
                    ds9.dot("o", xc, yc, ctype=ds9.CYAN, size=4, frame=frame)

        if cs.getNcand() == 0:
            raise RuntimeError("No good PSF candidates")

        # Calculate fwhm, backnoise2 and gain
        fwhm = 2*np.sqrt(2*np.log(2))*np.median(sizes)
        backnoise2 = afwMath.makeStatistics(mi.getImage(), afwMath.VARIANCECLIP).getValue()
        ccd = afwCG.cast_Ccd(exposure.getDetector())
        if ccd:
            gain = np.mean(np.array([a.getElectronicParams().getGain() for a in ccd]))
        else:
            gain = 1.0
            self.warnLog.log(pexLog.Log.WARN, "Setting gain to %g" % gain)

        xvec = np.array([ cand.getSource().get('initial.centroid.sdss.x') for cand in psfCandidateList ])
        yvec = np.array([ cand.getSource().get('initial.centroid.sdss.y') for cand in psfCandidateList ])
        spatialModel = hscpsfLib.HscSpatialModelPolynomial(self.config.spatialOrder, min(xvec), max(xvec), min(yvec), max(yvec))

        #
        # This sequence of PSF fits, including "magic numbers" such as psf_make(0.2, 1000.0)
        # matches internal logic in psfex!
        #
        psf = hscpsfLib.PolypixPsf(cs, nside, actualKernelSize, self.config.samplingSize, spatialModel, fwhm, backnoise2, gain)
        psf.psf_make(0.2, 1000.0)
        cs = psf.psf_clean(0.2)

        psf = hscpsfLib.PolypixPsf(cs, psf)
        psf.psf_make(0.1, 1000.0)
        cs = psf.psf_clean(0.1)

        psf = hscpsfLib.PolypixPsf(cs, psf)
        psf.psf_make(0.05, 1000.0)
        cs = psf.psf_clean(0.05)

        psf = hscpsfLib.PolypixPsf(cs, psf)
        psf.psf_make(0.01, 1000.0)
        psf.psf_clip()
        cs = psf.psf_clean(0.01)

        good_indices = [ cs.getId(icand) for icand in xrange(cs.getNcand()) ]
        if flagKey is not None:
            for (icand, cand) in enumerate(psfCandidateList):
                if icand in good_indices:
                    cand.getSource().set(flagKey, True)

        numGoodStars = len(good_indices)
        avgX, avgY = np.mean(xpos), np.mean(ypos)

        #
        # Generate some QA information
        #
        # Count PSF stars
        #
        if metadata != None:
            metadata.set("spatialFitChi2", np.nan)
            metadata.set("numAvailStars", nCand)
            metadata.set("numGoodStars", numGoodStars)
            metadata.set("avgX", avgX)
            metadata.set("avgY", avgY)

        psfCellSet = None
        return psf, psfCellSet
コード例 #46
0
def main(infile, ccds=None, camera="hsc", cmap="copper", cols=[0,1],
         nsig=3.0, percent=False, textcolor='k', out=None):

    ###########################
    # build the camera
    policy_file = {
        'hsc': os.path.join(os.getenv("OBS_SUBARU_DIR"), "hsc", "hsc_geom.paf"),
        'sc' : os.path.join(os.getenv("OBS_SUBARU_DIR"), "suprimecam", "Full_Suprimecam_geom.paf")
        }
    geomPolicy = afwCGU.getGeomPolicy(policy_file[camera.lower()])
    camera     = afwCGU.makeCamera(geomPolicy)

    
    ###########################
    # load the data
    data = {}
    if infile:
        load = numpy.loadtxt(infile)
        vals = load[:,cols[1]]
        for i in range(len(vals)):
            ccd = int(load[i,cols[0]])
            if len(cols) == 3:
                amp = int(load[i,cols[2]])
            else:
                amp = 0
            if ccd not in data:
                data[ccd] = {}
            data[ccd][amp] = vals[i]
    else:
        vals = []
        for r in camera:
            for c in afwCG.cast_Raft(r):
                ccd = afwCG.cast_Ccd(c)
                ccdId = ccd.getId().getSerial()
                data[ccdId] = {}
                val = 1.0
                if ccdId > 103:
                    val = 0.8
                for a in ccd:
                    amp = afwCG.cast_Amp(a)
                    ampId = amp.getId().getSerial() - 1
                    data[ccdId][ampId] = val
                    vals.append(val)
                if len(data[ccdId]) == 0:
                    data[ccdId][0] = val
                    vals.append(val)
        vals = numpy.array(vals)
        
    mean = vals.mean()
    med  = numpy.median(vals)
    std  = vals.std()
    vmin, vmax = med - nsig*std, med+nsig*std
    

    ###########################
    # make the plot
    fig = figure.Figure(figsize=(7,7))
    canvas = FigCanvas(fig)

    if infile:
        rect = (0.06, 0.12, 0.76, 0.76)
    else:
        rect = (0.06, 0.06, 0.88, 0.88)
        
    fpa_fig = hscUtil.FpaFigure(fig, camera, rect=rect)
    
    for i_ccd, amplist in data.items():

        hide = False
        if ccds and (i_ccd not in ccds):
            hide = True
        
        ax = fpa_fig.getAxes(i_ccd)
        fpa_fig.highlightAmp(i_ccd, 0)
        nq = fpa_fig.detectors[i_ccd].getOrientation().getNQuarter()
        nx, ny = 4, 8
        if nq % 2:
            ny, nx = nx, ny
        firstAmp = sorted(amplist.keys())[0]
        im = numpy.zeros((ny, nx)) + amplist[firstAmp]

        print i_ccd
        for amp, val in amplist.items():
            useVal = val
            if hide:
                useVal = 0.0
            if nq == 0:
                im[:,amp] = useVal
            if nq == 1 or nq == -3:
                im[3-amp,:] = useVal
            if nq == 2:
                im[:,3-amp] = useVal
            if nq == -1 or nq == 3:
                im[amp,:] = useVal
            
        im_ax = ax.imshow(im, cmap=cmap, vmax=vmax, vmin=vmin, interpolation='nearest')

        fpa_fig.addLabel(i_ccd, [str(i_ccd)], color=textcolor)


    #############################
    # the colorbar
    ylo, yhi = vmin, vmax

    if infile:
        rect = (0.91, 0.25, 0.02, 0.6)
        
        # real units
        cax  = fig.add_axes(rect)        
        cax.get_yaxis().get_major_formatter().set_useOffset(False)        
        cax.set_ylim([ylo, yhi])
        cax.get_xaxis().set_ticks([])
        cbar = fig.colorbar(im_ax, cax=cax)

        # mirror the values.  std or in percent (fractional) if requested
        caxp = cax.twinx()
        caxp.get_xaxis().set_ticks([])
        caxp.get_yaxis().get_major_formatter().set_useOffset(False)        
        if percent:
            caxp.set_ylim([(med-ylo)/(yhi-ylo), (yhi-med)/(yhi-ylo)])
        else:
            caxp.set_ylim([-nsig*std, nsig*std])


        for t in cax.get_yticklabels():
            t.set_size('small')
        for t in caxp.get_yticklabels():
            t.set_size("small")


    #################################
    # add stats
    if infile:
        stats = {"Mean":mean, "Med":med, "Std":std, "Max":vals.max(), "Min":vals.min()}
        order = ["Mean", "Med", "Std", "Min", "Max"]
        i = 0
        for k in order:
            v = stats[k]
            ax.text(0.8, 0.03+0.02*i, "%-10s %.3g"%(k+":",v), fontsize=9,
                    horizontalalignment='left', verticalalignment='center', transform=fig.transFigure)
            i += 1

    #################################
    # title and write it
    date = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
    title = "File: %s  Cols: %s  %s" % (infile, ":".join([str(x+1) for x in cols]), date) if infile else ""
    fig.suptitle(title)

    outfile = out if out else "camview-%s.png" % infile
    fig.savefig(outfile)
コード例 #47
0
ファイル: showCamera.py プロジェクト: rnikutta/afw
def main(camera, sample=20, showDistortion=True):
    if True:
        fig = plt.figure(1)
        fig.clf()
        ax = fig.add_axes((0.1, 0.1, 0.8, 0.8))

        title = camera.getId().getName()
        if showDistortion:
            title += ' (Distorted)'

        ax.set_title(title)
    else:
        fig = None

    if showDistortion:
        dist = camera.getDistortion()

    for raft in camera:
        raft = cameraGeom.cast_Raft(raft)
        for ccd in raft:
            if False and ccd.getId().getSerial() not in (0, 3):
                continue

            ccd = cameraGeom.cast_Ccd(ccd)
            ccd.setTrimmed(True)

            width, height = ccd.getAllPixels(True).getDimensions()

            corners = ((0.0, 0.0), (0.0, height), (width, height),
                       (width, 0.0), (0.0, 0.0))
            for (x0, y0), (x1, y1) in zip(corners[0:4], corners[1:5]):
                if x0 == x1 and y0 != y1:
                    yList = numpy.linspace(y0, y1, num=sample)
                    xList = [x0] * len(yList)
                elif y0 == y1 and x0 != x1:
                    xList = numpy.linspace(x0, x1, num=sample)
                    yList = [y0] * len(xList)
                else:
                    raise RuntimeError("Should never get here")

                xOriginal = []
                yOriginal = []
                xDistort = []
                yDistort = []
                for x, y in zip(xList, yList):
                    position = ccd.getPositionFromPixel(afwGeom.Point2D(
                        x, y))  # focal plane position

                    xOriginal.append(position.getMm().getX())
                    yOriginal.append(position.getMm().getY())

                    if not showDistortion:
                        continue

                    # Calculate offset (in CCD pixels) due to distortion
                    distortion = dist.distort(afwGeom.Point2D(x, y),
                                              ccd) - afwGeom.Extent2D(x, y)

                    # Calculate the distorted position
                    distorted = position + cameraGeom.FpPoint(
                        distortion) * ccd.getPixelSize()

                    xDistort.append(distorted.getMm().getX())
                    yDistort.append(distorted.getMm().getY())

                if fig:
                    ax.plot(xOriginal, yOriginal, 'k-')
                    if showDistortion:
                        ax.plot(xDistort, yDistort, 'r-')

            if fig:
                x, y = ccd.getPositionFromPixel(
                    afwGeom.Point2D(width / 2, height / 2)).getMm()
                ax.text(x, y, ccd.getId().getSerial(), ha='center')

    if fig:
        plt.show()
コード例 #48
0
def main(rootDir,
         tracts,
         visitsIn=None,
         ccds=None,
         patches=None,
         showPatch=False,
         showTract=False,
         showFootprint=False,
         filter=None,
         out=None):

    butler = dafPersist.Butler(rootDir)

    if not patches:
        patches = ["%d,%d" % (ix, iy) for ix in range(11) for iy in range(11)]

    #####################################
    # get the visits and ccds
    #####################################

    visits = set()
    ccdsInVisits = dict()
    files = {}
    for tract in tracts:
        for patch in patches:
            print "getting patch", tract, patch

            try:
                coadd_file = butler.get("deepCoadd_filename", {
                    'tract': tract,
                    "filter": filter,
                    "patch": patch
                },
                                        immediate=True)[0]
            except:
                continue

            if re.search("_parent", coadd_file):
                continue

            #print coadd_file
            files[patch] = coadd_file
            try:
                coadd = butler.get("deepCoadd", {
                    'tract': tract,
                    "filter": filter,
                    "patch": patch
                })
                coaddIn = coadd.getInfo().getCoaddInputs()
            except:
                continue
            ccdInputs = coaddIn.ccds

            for v, ccd in zip(ccdInputs.get("visit"), ccdInputs.get("ccd")):
                if v not in ccdsInVisits:
                    ccdsInVisits[v] = []
                ccdsInVisits[v].append(ccd)

            for v in coaddIn.visits:
                visits.add(int(v.getId()))

    if visitsIn:
        visits = visits & set(visitsIn)

    nv = len(visits)

    nvals = {1: (1, 1), 2: (2, 1), 3: (3, 1), 4: (2, 2)}
    sizes = {1: (7, 7), 2: (10, 5), 3: (12, 4), 4: (8, 8)}
    if nv in nvals:
        nx, ny = nvals[nv]
        figsize = sizes[nv]
    else:
        nx = int(math.floor(numpy.sqrt(nv))) + 1
        ny = nv / nx
        if ny == 0:
            ny = 1
        if nv % nx > 0:
            ny += 1
        if nv < 20:
            figsize = (10, 8)
        else:
            figsize = (2 * nx, 2 * ny)

    print ny, nx
    fig, axes = pyplot.subplots(ny,
                                nx,
                                squeeze=False,
                                sharex=True,
                                sharey=True,
                                figsize=figsize)

    ########################
    ###  draw the CCDs
    ########################
    ras, decs = [], []
    for i_v, visit in enumerate(sorted(visits)):
        print i_v, visit,
        allCcds = [
            camGeom.cast_Ccd(ccd)
            for ccd in camGeom.cast_Raft(butler.get("camera")[0])
        ]
        ccdSet = ccds if ccds else set(ccdsInVisits[visit])
        ccdList = [c for c in allCcds if c.getId().getSerial() in ccdSet]

        color = ('r', 'b', 'c', 'g', 'm')  #[0] #[i_v%5]
        i_y, i_x = i_v / nx, i_v % nx

        ras = []
        decs = []
        for ccd in ccdList:
            bbox = ccd.getAllPixels()
            ccdId = ccd.getId().getSerial()
            nq = ccd.getOrientation().getNQuarter()

            print "  ", ccdId
            # if it's odd (chips 100..103) color it like it's even counterparts
            nq = nq - nq % 2
            clr = color[0]

            # show the halves of the camera, and make chip 0 brighter to break the 180deg symmetry
            alpha = 0.4 if (nq == 0 or ccdId == 0 or ccdId == 50) else 0.2

            if ccdId < 104:
                #dataId = {'tract': tracts, 'visit': visit, 'ccd': ccdId}
                dataId = {'visit': visit, 'ccd': ccdId}
                try:
                    wcs = afwImage.makeWcs(butler.get("calexp_md", dataId))
                except:
                    wcs = None

                if wcs:
                    ra, dec = bboxToRaDec(bbox, wcs)
                    ras += ra
                    decs += dec
                    if not showFootprint:
                        axes[i_y][i_x].fill(ra,
                                            dec,
                                            fill=True,
                                            alpha=alpha,
                                            color=clr,
                                            edgecolor=clr)

        axes[i_y][i_x].set_title(str(visit), size='x-small')

        if showFootprint:
            perim = convex_hull(zip(ras, decs))
            pra, pdec = zip(*perim)
            axes[i_y][i_x].fill(pra, pdec, fill=False, edgecolor=color[0])

        print ""

    ##############################
    # draw the skymap
    ###############################
    minRa = min(ras)
    maxRa = max(ras)
    minDec = min(decs)
    maxDec = max(decs)
    tras = []
    tdecs = []
    if showTract:
        skymap = butler.get('deepCoadd_skyMap', {'tract': 0})
        tractObjs = [t for t in skymap if (t.getId() in tracts)]

        for tractObj in tractObjs:

            print tractObj.getId()
            ra, dec = bboxToRaDec(tractObj.getBBox(), tractObj.getWcs())
            tras += ra
            tdecs += dec

            # add tract,patch grids to all panels
            for i_x in range(nx):
                for i_y in range(ny):
                    axes[i_y][i_x].fill(ra,
                                        dec,
                                        fill=False,
                                        edgecolor='k',
                                        lw=1,
                                        linestyle='dashed')
                    axes[i_y][i_x].text(percent(ra, 0.03),
                                        percent(dec, 0.95),
                                        str(tractObj.getId()),
                                        fontsize=6,
                                        horizontalalignment='right',
                                        verticalalignment='top')
                    if showPatch:
                        for patch in tractObj:
                            ra, dec = bboxToRaDec(patch.getInnerBBox(),
                                                  tractObj.getWcs())
                            axes[i_y][i_x].fill(ra,
                                                dec,
                                                fill=False,
                                                edgecolor='k',
                                                lw=1,
                                                linestyle='dashed')
                            if min(ra) > maxRa or max(ra) < minRa or min(
                                    dec) > maxDec or max(dec) < minDec:
                                continue
                            patch_str = "%d,%d" % patch.getIndex()
                            axes[i_y][i_x].text(percent(ra),
                                                percent(dec, 0.9),
                                                patch_str,
                                                fontsize=6,
                                                horizontalalignment='center',
                                                verticalalignment='top')

    ######################
    # final plot stuff
    ######################

    buff = 0.2
    if showTract:
        minRa = max(min(tras), min(ras))
        maxRa = min(max(tras), max(ras))
        minDec = max(min(tdecs), min(decs))
        maxDec = min(max(tdecs), max(decs))
        xlim = maxRa + buff, minRa - buff
        ylim = minDec - buff, maxDec + buff
    else:
        xlim = max(ras) + buff, min(ras) - buff
        ylim = min(decs) - buff, max(decs) + buff

    for i_x in range(nx):
        for i_y in range(ny):
            axes[i_y][i_x].set_xlim(xlim)
            axes[i_y][i_x].set_ylim(ylim)
            for tic in axes[i_y][i_x].get_xticklabels(
            ) + axes[i_y][i_x].get_yticklabels():
                tic.set_size("x-small")
            for tic in axes[i_y][i_x].get_xticklabels():
                tic.set_rotation(33.0)

    if rootDir[-1] == "/":
        rootDir = rootDir[:-1]
    rerun = os.path.basename(rootDir)
    tractsort = sorted(tracts)
    if len(tracts) == 1:
        tract_str = str(tracts[0])
    else:
        tract_str = str(tractsort[0]) + "-" + str(tractsort[-1])
    fig.suptitle("Tract %s inputs" % (tract_str))
    outfile = out or "coaddIn-%s-%s.png" % (rerun, tract_str)
    fig.savefig(outfile)
コード例 #49
0
    def makeFigure(
        self,
        borderPix=100,
        boundaryColors='r',
        doLabel=False,
        showUndefined=False,
        vlimits=None,
        cmap="jet",
        title=None,
        cmapOver=None,
        cmapUnder=None,
        failLimits=None,
        failColor='k',
    ):
        """Make the figure.

        @param borderPix        width of border in pixels
        @param boundaryColors   matplotlib color specifier for border
        @param doLabel          add ccd labels to the figure
        @param showUndefined    show sensors even if their value is None
        @param vlimits          [low, high] limits for colormap
        @param cmap             string name of an matplotlib.cm colormap
        @param title            title of the figure
        @param cmapOver         Color to use if above cmap high limit.
        @param cmapUnder        Color to use if below cmap low limit.
        @param failLimits       Limits to mark failure.
        @param failColor        Color to use to mark failed sensors.
        """

        if vlimits is None:
            arr = self.getDataArray()
            vlimits = [arr.min(), arr.max()]

        if failLimits is None:
            failLimits = vlimits

        histWidth = 0.05
        useHist = True
        if useHist:
            left, right, bottom, top = 0.195, 0.95 - 2.0 * histWidth, 0.15, 0.9
        else:
            left, right, bottom, top = 0.195, 0.95, 0.15, 0.9
        self.fig.subplots_adjust(left=left,
                                 right=right,
                                 bottom=bottom,
                                 top=top)

        sp = self.fig.gca()

        values = []  # needs to be synchronized with self.rectangles
        patches = []
        allValues = []
        missingCcds = {}
        failedCcds = {}
        for r in self.camera:
            raft = cameraGeom.cast_Raft(r)
            rlabel = raft.getId().getName()
            for c in raft:
                ccd = cameraGeom.cast_Ccd(c)
                clabel = ccd.getId().getName()
                value = self.data[rlabel][clabel]
                allValues.append(value)
                #if (not value is None) or (showUndefined):
                if value is None:
                    value = numpy.NaN
                    missingCcds[clabel] = self.ccdBoundaries[clabel]
                if value < failLimits[0] or value > failLimits[1]:
                    failedCcds[clabel] = value
                #if value > failLimits[1]:
                #    failedCcds[clabel] = 1
                values.append(value)
                patches.append(self.rectangles[clabel])

        if vlimits is not None:
            norm = colors.Normalize(vmin=vlimits[0],
                                    vmax=vlimits[1],
                                    clip=False)
        else:
            norm = colors.Normalize()

        if len(patches) == 0:
            patches = self.rectangles.values()
            values = allValues

        cmap = getattr(cm, cmap)
        cmap.set_bad('k', 0.2)
        if cmapOver is not None:
            cmap.set_over(cmapOver, 1.0)
        if cmapUnder is not None:
            cmap.set_under(cmapUnder, 1.0)
        p = PatchCollection(patches, norm=norm, cmap=cmap)
        value_array = numpy.array(values)
        masked_value_array = numpyMa.masked_where(numpy.isnan(value_array),
                                                  value_array)
        p.set_array(masked_value_array)
        try:
            cb = self.fig.colorbar(p)
        except Exception:
            cb = None
        sp.add_collection(p)

        ##############################
        # put a histogram on the side
        if useHist:
            nbins = 30
            histValues = numpy.array(values)
            finiteValues = histValues[numpy.where(
                numpy.isfinite(histValues))[0]]
            left = right + 0.05
            axH = self.fig.add_axes([left, bottom, histWidth, top - bottom])
            binwid = float(vlimits[1] - vlimits[0]) / nbins
            if binwid > 0.0 and len(finiteValues) > 0:
                eps = 1.0e-4 * binwid
                #print finiteValues, nbins, vlimits
                nu, bu, pu = axH.hist(
                    finiteValues,
                    bins=nbins,
                    range=[vlimits[0] - eps, vlimits[1] + eps],
                    orientation='horizontal',
                    color='#aaaaaa',
                    fill=True)
                #nu = numpy.array([1.0])
                axH.set_ylim(vlimits)
                xmax = 1.2 * nu.max()
                if xmax <= 0.0:
                    xmax = 1.0
                axH.set_xlim([0, xmax])

            axH.set_xlabel('')
            axH.set_ylabel('')
            axH.set_xticklabels([])
            axH.set_yticklabels([])
            self.fig.sca(sp)

        self.plotRaftBoundaries(sp, boundaryColors)
        self.plotCcdBoundaries(sp)
        self.markMissingCcds(sp, missingCcds)
        self.markFailedCcds(sp, failedCcds, cmap, vlimits)
        if self.cameraInfo.doLabel or doLabel:
            self.labelSensors(sp)

        if title is not None:
            self.fig.text(0.5,
                          0.94,
                          title,
                          horizontalalignment="center",
                          fontsize=10)
            #sp.set_title(title, fontsize=12)
        sp.set_xlabel("Focal Plane X", fontsize=9)
        sp.set_ylabel("Focal Plane Y", fontsize=9)

        self.adjustTickLabels(sp, cb)

        x0, y0, x1, y1 = self.getFpaLimits()
        sp.set_xlim((x0 - borderPix, x1 + borderPix))
        sp.set_ylim((y0 - borderPix, y1 + borderPix))

        self.setMapInfo()
コード例 #50
0
    def makeFigure(
        self,
        borderPix=100,
        boundaryColors='r',
        doLabel=False,
        showUndefined=False,
        vlimits=None,
        cmap="jet",
        title=None,
        cmapOver=None,
        cmapUnder=None,
        failLimits=None,
        failColor='k',
    ):
        """Make the figure.

        @param borderPix        width of border in pixels
        @param boundaryColors   matplotlib color specifier for border
        @param doLabel          add ccd labels to the figure
        @param showUndefined    show sensors even if their value is None
        @param vlimits          [low, high] limits for colormap
        @param cmap             string name of an matplotlib.cm colormap
        @param title            title of the figure
        @param cmapOver         Color to use if above cmap high limit.
        @param cmapUnder        Color to use if below cmap low limit.
        @param failLimits       Limits to mark failure.
        @param failColor        Color to use to mark failed sensors.
        """

        if vlimits is None:
            arr = self.getDataArray()
            vlimits = [arr.min(), arr.max()]
        if failLimits is None:
            failLimits = vlimits

        histWidth = 0.05
        useHist = True
        if useHist:
            left, right, bottom, top = 0.195, 0.95 - 2.0 * histWidth, 0.15, 0.9
        else:
            left, right, bottom, top = 0.195, 0.95, 0.15, 0.9

        self.fig.subplots_adjust(left=left,
                                 right=right,
                                 bottom=bottom,
                                 top=top)

        colorValues = []  # needs to be synchronized with self.rectangles
        patches = []
        allValues = []

        radiansWrtX = {}
        lenInPix = {}
        colorScalar = {}
        haveColors = False

        missingCcds = {}
        failedCcds = {}
        for r in self.camera:
            raft = cameraGeom.cast_Raft(r)
            rlabel = raft.getId().getName()
            for c in raft:
                ccd = cameraGeom.cast_Ccd(c)
                clabel = ccd.getId().getName()
                values = self.data[rlabel][clabel]
                defaultLen = 1500.0
                if isinstance(values, list):
                    if len(values) == 3:
                        radiansWrtXtmp, lenInPixtmp, colorScalartmp = values
                    elif len(values) == 2:
                        radiansWrtXtmp, lenInPixtmp = values
                        colorScalartmp = 0.0
                    else:
                        raise Exception(
                            "values for Vector must be float or [radians, lenInPix, [colorFloat]]."
                        )
                    if lenInPixtmp is None:
                        lenInPixtmp = defaultLen

                else:
                    if values is not None:
                        values = float(values)
                        defaultLen = 1500.0  # this works for most ccds, but should change to eg. ccdwidth/2
                    else:
                        values = 0.0
                        defaultLen = 0.0
                    radiansWrtXtmp, lenInPixtmp, colorScalartmp = values, defaultLen, None

                if lenInPixtmp < 0:
                    lenInPixtmp = 0
                #print clabel, radiansWrtXtmp, lenInPixtmp, colorScalartmp

                #print clabel, radiansWrtXtmp, lenInPixtmp, colorScalartmp
                lenInPix[clabel] = lenInPixtmp
                allValues.append(radiansWrtXtmp)
                if (radiansWrtXtmp is not None):  # or (showUndefined):
                    if colorScalartmp is not None:
                        colorValues.append(colorScalartmp)
                        patches.append(self.rectangles[clabel])
                        colorScalar[clabel] = colorScalartmp
                        haveColors = True
                        if colorScalartmp < failLimits[
                                0] or colorScalartmp > failLimits[1]:
                            failedCcds[clabel] = colorScalartmp
                        #if colorScalartmp > failLimits[1]:
                        #    failedCcds[clabel] = 1
                    else:
                        colorValues.append(numpy.NaN)
                        patches.append(self.rectangles[clabel])
                        missingCcds[clabel] = self.ccdBoundaries[clabel]
                    radiansWrtX[clabel] = radiansWrtXtmp

                else:
                    colorValues.append(numpy.NaN)
                    patches.append(self.rectangles[clabel])
                    missingCcds[clabel] = self.ccdBoundaries[clabel]

        if vlimits is not None:
            norm = colors.Normalize(vmin=vlimits[0],
                                    vmax=vlimits[1],
                                    clip=False)
        else:
            norm = colors.Normalize()

        sp = self.fig.gca(
        )  #add_axes([left, bottom, right-left, top-bottom]) #gca()

        ##############################
        # put a histogram on the side
        if useHist:
            nbins = 30
            histValues = numpy.array(colorValues)
            finiteValues = histValues[numpy.where(
                numpy.isfinite(histValues))[0]]
            left = right + 0.05
            axH = self.fig.add_axes([left, bottom, histWidth, top - bottom])
            binwid = float(vlimits[1] - vlimits[0]) / nbins
            if binwid > 0.0 and len(finiteValues) > 0:
                eps = 1.0e-4 * binwid
                nu, bu, pu = axH.hist(
                    finiteValues,
                    bins=nbins,
                    range=[vlimits[0] - eps, vlimits[1] + eps],
                    orientation='horizontal',
                    color='#aaaaaa',
                    fill=True)
                axH.set_ylim(vlimits)
                axH.set_xlim([0, 1.2 * nu.max()])
            axH.set_xlabel('')
            axH.set_ylabel('')
            axH.set_xticklabels([])
            axH.set_yticklabels([])
            self.fig.sca(sp)

        if len(patches) > 0:

            cmap = getattr(cm, cmap)
            cmap.set_bad('k', 0.2)
            if cmapOver is not None:
                cmap.set_over(cmapOver, 1.0)
            if cmapUnder is not None:
                cmap.set_under(cmapUnder, 1.0)

            p = PatchCollection(patches, norm=norm, cmap=cmap)
            value_array = numpy.array(colorValues)
            masked_value_array = numpyMa.masked_where(numpy.isnan(value_array),
                                                      value_array)
            p.set_array(masked_value_array)
            if haveColors or (vlimits is not None):
                try:
                    cb = self.fig.colorbar(p)
                except Exception:
                    cb = None
            sp.add_collection(p)

        for label, angle in radiansWrtX.items():
            xc, yc = self.centers[label]
            arrowLen = lenInPix[label]
            x = xc - 0.5 * arrowLen * numpy.cos(angle)
            y = yc - 0.5 * arrowLen * numpy.sin(angle)
            dx = arrowLen * numpy.cos(angle)
            dy = arrowLen * numpy.sin(angle)
            if numpy.abs(dx) < 1.0e-15 or numpy.abs(dy) < 1.0e-15:
                continue
            sp.arrow(x, y, dx, dy)  #, ec="k", lw=3)

        self.plotRaftBoundaries(sp, boundaryColors)
        self.plotCcdBoundaries(sp)
        self.markMissingCcds(sp, missingCcds)
        self.markFailedCcds(sp, failedCcds, cmap, vlimits)
        if self.cameraInfo.doLabel or doLabel:
            self.labelSensors(sp)

        if title is not None:
            self.fig.text(0.5,
                          0.94,
                          title,
                          horizontalalignment="center",
                          fontsize=10)
            #sp.set_title(title, fontsize=12)
        sp.set_xlabel("Focal Plane X", fontsize=9)
        sp.set_ylabel("Focal Plane Y", fontsize=9)

        if (not haveColors) and (vlimits is None):
            cb = None
        self.adjustTickLabels(sp, cb)

        x0, y0, x1, y1 = self.getFpaLimits()
        sp.set_xlim((x0 - borderPix, x1 + borderPix))
        sp.set_ylim((y0 - borderPix, y1 + borderPix))

        self.setMapInfo()
コード例 #51
0
    def process(self, clipboard):
        """
        """
        self.log.log(Log.INFO, "Doing Saturation correction.")

        #grab exposure from clipboard
        exposure = clipboard.get(self.policy.getString("inputKeys.exposure"))
        exposure = ipIsr.convertImageForIsr(exposure)
        fwhm = self.policy.getDouble("parameters.defaultFwhm")
        amp = cameraGeom.cast_Amp(exposure.getDetector())
        dataBbox = amp.getDataSec(True)
        x = dataBbox.getMinX()
        y = dataBbox.getMinY()
        height = dataBbox.getMaxY() - dataBbox.getMinY()
        width = dataBbox.getMaxY() - dataBbox.getMinX()
        dl = measAlg.DefectListT()
        defectList = cameraGeom.cast_Ccd(amp.getParent()).getDefects()
        if amp.getId().getIndex()[1] == 1:
            for d in defectList:
                d1 = measAlg.Defect(d.getBBox())
                d1.shift(-x, -y)
                bbox = d1.getBBox()
                if bbox.getMinX() - 4 > width or bbox.getMaxY() - 4 < 0 or \
                    height - bbox.getMinY() - 1 > height or height - bbox.getMaxY() - 1 < 0:
                    pass
                else:
                    nd = measAlg.Defect(
                        afwGeom.Box2I(
                            afwGeom.Point2I(bbox.getMinX() + 4,
                                            height - bbox.getMinY() + 1),
                            bbox.getDimensions()))
                    dl.append(nd)
        else:
            for d in defectList:
                d1 = measAlg.Defect(d.getBBox())
                d1.shift(-x, -y)
                bbox = d1.getBBox()
                if bbox.getMinX() - 4 > width or bbox.getMaxY() - 4 < 0 or \
                    bbox.getMinY() - 1 > height or bbox.getMaxY() - 1 < 0:
                    pass
                else:
                    nd = measAlg.Defect(
                        afwGeom.Box2I(bbox.getMin() + afwGeom.Extent2I(4, 1),
                                      bbox.getDimensions()))
                    dl.append(nd)
        saturation = amp.getElectronicParams().getSaturationLevel()
        ipIsr.maskBadPixelsDef(exposure,
                               dl,
                               fwhm,
                               interpolate=True,
                               maskName='BAD')
        ipIsr.saturationCorrection(exposure,
                                   int(saturation),
                                   fwhm,
                                   growSaturated=False,
                                   interpolate=True)
        #ds9.mtv(exposure, frame = 0, title = "my Amp")
        #exposure.writeFits("Amp.fits")
        #output products
        clipboard.put(
            self.policy.get("outputKeys.saturationCorrectedExposure"),
            exposure)
コード例 #52
0
def main(butler, tract, visits, ccds=None, showPatch=False, singleVisit=False):
    """Plot the visits/CCDs belong to certain Tract."""
    #  draw the CCDs
    ras, decs = [], []
    for i_v, visit in enumerate(visits):
        print i_v, visit
        visitColor = "#%06x" % random.randint(0, 0xFFFFFF)
        ccdList = [camGeom.cast_Ccd(ccd) for ccd in
                   camGeom.cast_Raft(butler.get("camera")[0])]
        for ccd in ccdList:
            bbox = ccd.getAllPixels()
            ccdId = ccd.getId().getSerial()

            if (ccds is None or ccdId in ccds) and ccdId < 104:
                dataId = {'tract': tract, 'visit': visit, 'ccd': ccdId}
                try:
                    wcs = afwImage.makeWcs(butler.get("calexp_md", dataId))
                except:
                    pass
                ra, dec = bboxToRaDec(bbox, wcs)
                ras += ra
                decs += dec
                if singleVisit:
                    color = 'r'
                else:
                    color = visitColor
                pyplot.fill(ra, dec, fill=True, alpha=0.3,
                            color=color, edgecolor=color)

    buff = 0.1
    xlim = max(ras)+buff, min(ras)-buff
    ylim = min(decs)-buff, max(decs)+buff

    # draw the skymap
    if showPatch:
        skymap = butler.get('deepCoadd_skyMap', {'tract': tract})
        tt = skymap[tract]
        for patch in tt:
            ra, dec = bboxToRaDec(patch.getInnerBBox(), tt.getWcs())
            pyplot.fill(ra, dec, fill=False, edgecolor='k', lw=1,
                        linestyle='dashed')
            if (xlim[1] < percent(ra) < xlim[0]) and (
               ylim[0] < percent(dec) < ylim[1]):
                        pyplot.text(percent(ra), percent(dec, 0.9),
                                    str(patch.getIndex()),
                                    fontsize=6,
                                    horizontalalignment='center',
                                    verticalalignment='top')

    # add labels as save
    ax = pyplot.gca()
    ax.set_xlabel("R.A. (deg)")
    ax.set_ylabel("Decl. (deg)")
    ax.set_xlim(xlim)
    ax.set_ylim(ylim)
    fig = pyplot.gcf()

    if singleVisit:
        fig.savefig("%s_patches_%s.png" % (tract, visit))
    else:
        fig.savefig("%s_patches.png" % tract)
    fig.clear()