Ejemplo n.º 1
0
def showCamera(camera, imageSource=SynthesizeCcdImage(), imageFactory=afwImage.ImageU,
               frame=None, overlay=True, bin=1):
    """Show a Camera on ds9 (with the specified frame); if overlay show the IDs and amplifier boundaries

If imageSource is provided its getImage method will be called to return a CCD image (e.g. a
cameraGeom.GetCcdImage object); if it is "", an image will be created based on the properties
of the detectors"""

    if imageSource is None:
        cameraImage = None
    elif isinstance(imageSource, GetCcdImage):
        cameraImage = makeImageFromCamera(camera, imageSource, bin=bin, imageFactory=imageFactory)
    else:
        cameraImage = imageSource

    if cameraImage:
        ds9.mtv(cameraImage, frame=frame, title=camera.getId().getName())

    for det in camera:
        raft = cameraGeom.cast_Raft(det)
        
        center = camera.getCenterPixel() + afwGeom.Extent2D(raft.getCenterPixel())

        if overlay:
            bbox = raft.getAllPixels()
            ds9.dot(raft.getId().getName(), center[0]/bin, center[1]/bin, frame=frame)

        showRaft(raft, None, frame=frame, overlay=overlay,
                 raftOrigin=center - afwGeom.makeExtentD(raft.getAllPixels().getWidth()/2,
                                                         raft.getAllPixels().getHeight()/2), 
                 bin=bin)
Ejemplo n.º 2
0
    def testRadialDistortion(self):
        for raft in self.camera:
            for ccdIndex, ccd in enumerate(cameraGeom.cast_Raft(raft)):
                dist = pipDist.createDistortion(ccd, self.config)
                size = ccd.getSize()
                height, width = size.getX(), size.getY()
                for x, y in ((0.0, 0.0), (0.0, height), (0.0, width),
                             (height, width), (height / 2.0, width / 2.0)):
                    src = afwDet.Source()
                    src.setXAstrom(x)
                    src.setYAstrom(y)
                    forward = dist.actualToIdeal(src)
                    backward = dist.idealToActual(forward)
                    trueForward = bickDistortion(x, y, ccdIndex)

                    self.assertTrue(
                        compare(backward.getXAstrom(), backward.getYAstrom(),
                                x, y, REVERSE_TOL),
                        "Undistorted distorted position is not original: %f,%f vs %f,%f for %d"
                        % (backward.getXAstrom(), backward.getYAstrom(), x, y,
                           ccdIndex))
                    self.assertTrue(
                        compare(forward.getXAstrom(), forward.getYAstrom(),
                                trueForward[0], trueForward[1], ABSOLUTE_TOL),
                        "Distorted position is not correct: %f,%f vs %f,%f for %d %f,%f"
                        % (forward.getXAstrom(), forward.getYAstrom(),
                           trueForward[0], trueForward[1], ccdIndex, x, y))
    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])
Ejemplo n.º 4
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',
            }
Ejemplo n.º 5
0
 def batchWallTime(cls, time, parsedCmd, numNodes, numProcs):
     """
     over-ridden method that gives the requested wall time for the method
     Probably not necessary here as this task is fast
     """
     numCcds = sum(1 for raft in parsedCmd.butler.get("camera")
                   for ccd in afwCg.cast_Raft(raft))
     numCycles = int(math.ceil(numCcds/float(numNodes*numProcs)))
     numExps = len(cls.RunnerClass.getTargetList(parsedCmd))
     return time*numExps*numCycles
Ejemplo n.º 6
0
 def batchWallTime(cls, time, parsedCmd, numNodes, numProcs):
     """
     over-ridden method that gives the requested wall time for the method
     Probably not necessary here as this task is fast
     """
     numCcds = sum(1 for raft in parsedCmd.butler.get("camera")
                   for ccd in afwCg.cast_Raft(raft))
     numCycles = int(math.ceil(numCcds / float(numNodes * numProcs)))
     numExps = len(cls.RunnerClass.getTargetList(parsedCmd))
     return time * numExps * numCycles
Ejemplo n.º 7
0
    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)
    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)
    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)
Ejemplo n.º 10
0
def findRaft(parent, id):
    """Find the Raft with the specified Id within the composite"""

    if isinstance(parent, cameraGeom.Camera):
        d = parent.findDetector(id)
        if d:
            return cameraGeom.cast_Raft(d)
    else:
        if parent.getId() == id:
            return raft

    return None
Ejemplo n.º 11
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")
Ejemplo n.º 12
0
    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 = {}
Ejemplo n.º 13
0
    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))
Ejemplo n.º 14
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
Ejemplo n.º 15
0
 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))
 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
Ejemplo n.º 17
0
    def getAxes(self, serial):
        ccd = self.detectors[serial]
        raft = camGeom.cast_Raft(ccd.getParent())

        # NOTE: all ccd coords are w.r.t. the *center* of the raft, not its LLC
        if False:
            rc = raft.getCenter().getPixels(raft.getPixelSize())
            rxc, ryc = rc.getX(), rc.getY()
            if rxc == 0 or ryc == 0:
                rbbox = raft.getAllPixels(True)
                rxc = rbbox.getWidth() / 2
                ryc = rbbox.getHeight() / 2
        else:
            rxc = self.camera_width / 2
            ryc = self.camera_height / 2

        #label = ccd.getId().getName()

        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()

        #print rxc, ryc, cxc, cyc

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

        # rescale according to rect
        sx = self.camera_width / self.fig_width
        sy = self.camera_height / self.fig_height
        l, w = self.fig_left + cx0 / sx, cwidth / sx
        b, h = self.fig_bottom + cy0 / sy, cheight / sy
        rect = (l, b, w, h)

        ax = self.figure.add_axes(rect)

        # kill the ticks
        for tick in ax.get_xticklines() + ax.get_yticklines(
        ) + ax.get_yticklabels() + ax.get_xticklabels():
            tick.set_visible(False)

        self.axes[serial] = ax
        return ax
Ejemplo n.º 18
0
    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 = {}
Ejemplo n.º 19
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]
Ejemplo n.º 20
0
    def getAxes(self, serial):
        ccd = self.detectors[serial]
        raft = camGeom.cast_Raft(ccd.getParent())

        # NOTE: all ccd coords are w.r.t. the *center* of the raft, not its LLC
        if False:
            rc        = raft.getCenter().getPixels(raft.getPixelSize())
            rxc, ryc  = rc.getX(), rc.getY()
            if rxc == 0 or ryc == 0:
                rbbox = raft.getAllPixels(True)
                rxc   = rbbox.getWidth()/2
                ryc   = rbbox.getHeight()/2
        else:
            rxc = self.camera_width/2
            ryc = self.camera_height/2

        #label = ccd.getId().getName()

        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()

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

        # rescale according to rect
        sx = self.camera_width/self.fig_width
        sy = self.camera_height/self.fig_height
        l, w = self.fig_left   + cx0/sx, cwidth/sx
        b, h = self.fig_bottom + cy0/sy, cheight/sy
        rect = (l, b, w, h)

        ax = self.figure.add_axes(rect)

        # kill the ticks
        for tick in ax.get_xticklines() + ax.get_yticklines() + ax.get_yticklabels() + ax.get_xticklabels():
            tick.set_visible(False)

        self.axes[serial] = ax
        return ax
 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]
Ejemplo n.º 22
0
    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))
Ejemplo n.º 23
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)
Ejemplo n.º 24
0
def describeRaft(raft, indent=""):
    """Describe an entire Raft"""
    descrip = []

    size = raft.getSize()
    descrip.append("%sRaft \"%s\",  %gx%g  BBox %s" % (indent, raft.getId(),
                                                        size[0], size[1], raft.getAllPixels()))
    
    for d in cameraGeom.cast_Raft(raft):
        cenPixel = d.getCenterPixel()
        cen = d.getCenter()

        descrip.append("%sCcd: %s (%5d, %5d) %s  (%7.1f, %7.1f)" % \
                       ((indent + "    "),
                        d.getAllPixels(True), cenPixel[0], cenPixel[1],
                        cameraGeom.ReadoutCorner(d.getOrientation().getNQuarter()), cen[0], cen[1]))
            
    return "\n".join(descrip)
Ejemplo n.º 25
0
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
Ejemplo n.º 26
0
    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
Ejemplo n.º 27
0
    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())        
 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)
Ejemplo n.º 29
0
    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
Ejemplo n.º 30
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])
Ejemplo n.º 31
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])
Ejemplo n.º 32
0
def makeImageFromCamera(camera, imageSource=None, imageFactory=afwImage.ImageU, bin=1):
    """Make an Image of a Camera"""

    cameraImage = imageFactory(camera.getAllPixels().getDimensions()/bin)
    for det in camera:
        raft = cameraGeom.cast_Raft(det);
        bbox = raft.getAllPixels()
        origin = camera.getCenterPixel() + afwGeom.Extent2D(raft.getCenterPixel()) - \
                 afwGeom.Extent2D(bbox.getWidth()/2, bbox.getHeight()/2)               
        bbox = afwGeom.Box2I(afwGeom.Point2I((origin.getX() + bbox.getMinX())//bin,
                                           (origin.getY() + bbox.getMinY())//bin),
                             bbox.getDimensions()//bin)

        im = cameraImage.Factory(cameraImage, bbox, afwImage.LOCAL)

        im <<= makeImageFromRaft(raft, imageSource,
                                 raftCenter=None,
                                 imageFactory=imageFactory, bin=bin)
        serial = raft.getId().getSerial()
        im += serial if serial > 0 else 0

    return cameraImage
Ejemplo n.º 33
0
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()
Ejemplo n.º 34
0
def main(
    dataDir,
    visit,
    title="",
    outputTxtFileName=None,
    showFwhm=False,
    minFwhm=None,
    maxFwhm=None,
    correctDistortion=False,
    showEllipticity=False,
    ellipticityDirection=False,
    showNdataFwhm=False,
    showNdataEll=False,
    minNdata=None,
    maxNdata=None,
    gridPoints=30,
    verbose=False,
):

    butler = dafPersist.ButlerFactory(mapper=hscSim.HscSimMapper(root=dataDir)).create()
    camera = butler.get("camera")

    if not (showFwhm or showEllipticity or showNdataFwhm or showNdataEll or outputTxtFileName):
        showFwhm = True
    #
    # Get a dict of cameraGeom::Ccd indexed by serial number
    #
    ccds = {}
    for raft in camera:
        raft = cameraGeom.cast_Raft(raft)
        for ccd in raft:
            ccd.setTrimmed(True)
            ccds[ccd.getId().getSerial()] = ccd
    #
    # Read all the tableSeeingMap files, converting their (x, y) to focal plane coordinates
    #
    xArr = []
    yArr = []
    ellArr = []
    fwhmArr = []
    paArr = []
    aArr = []
    bArr = []
    e1Arr = []
    e2Arr = []
    elle1e2Arr = []
    for tab in butler.subset("tableSeeingMap", visit=visit):
        # we could use tab.datasetExists() but it prints a rude message
        fileName = butler.get("tableSeeingMap_filename", **tab.dataId)[0]
        if not os.path.exists(fileName):
            continue

        with open(fileName) as fd:
            ccd = None
            for line in fd.readlines():
                if re.search(r"^\s*#", line):
                    continue
                fields = [float(_) for _ in line.split()]

                if ccd is None:
                    ccd = ccds[int(fields[0])]

                x, y, fwhm, ell, pa, a, b = fields[1:8]
                x, y = ccd.getPositionFromPixel(afwGeom.PointD(x, y)).getMm()
                xArr.append(x)
                yArr.append(y)
                ellArr.append(ell)
                fwhmArr.append(fwhm)
                paArr.append(pa)
                aArr.append(a)
                bArr.append(b)
                if len(fields) == 11:
                    e1 = fields[8]
                    e2 = fields[9]
                    elle1e2 = fields[10]
                else:
                    e1 = -9999.0
                    e2 = -9999.0
                    elle1e2 = -9999.0
                e1Arr.append(e1)
                e2Arr.append(e2)
                elle1e2Arr.append(elle1e2)

    xArr = np.array(xArr)
    yArr = np.array(yArr)
    ellArr = np.array(ellArr)
    fwhmArr = np.array(fwhmArr) * 0.168  # arcseconds
    paArr = np.radians(np.array(paArr))
    aArr = np.array(aArr)
    bArr = np.array(bArr)

    e1Arr = np.array(e1Arr)
    e2Arr = np.array(e2Arr)
    elle1e2Arr = np.array(elle1e2Arr)

    if correctDistortion:
        import lsst.afw.geom.ellipses as afwEllipses

        dist = camera.getDistortion()
        for i in range(len(aArr)):
            axes = afwEllipses.Axes(aArr[i], bArr[i], paArr[i])
            if False:  # testing only!
                axes = afwEllipses.Axes(1.0, 1.0, np.arctan2(yArr[i], xArr[i]))
            quad = afwEllipses.Quadrupole(axes)
            quad = quad.transform(dist.computeQuadrupoleTransform(afwGeom.PointD(xArr[i], yArr[i]), False))
            axes = afwEllipses.Axes(quad)
            aArr[i], bArr[i], paArr[i] = axes.getA(), axes.getB(), axes.getTheta()

        ellArr = 1 - bArr / aArr

    if len(xArr) == 0:
        gridPoints = 0
        xs, ys = [], []
    else:
        N = gridPoints * 1j
        extent = [min(xArr), max(xArr), min(yArr), max(yArr)]
        xs, ys = np.mgrid[extent[0] : extent[1] : N, extent[2] : extent[3] : N]

    title = [title]

    title.append("\n#")

    if outputTxtFileName:
        f = open(outputTxtFileName, "w")
        f.write("# %s visit %s\n" % (" ".join(title), visit))
        for x, y, ell, fwhm, pa, a, b, e1, e2, elle1e2 in zip(
            xArr, yArr, ellArr, fwhmArr, paArr, aArr, bArr, e1Arr, e2Arr, elle1e2Arr
        ):
            f.write("%f %f %f %f %f %f %f %f %f %f\n" % (x, y, ell, fwhm, pa, a, b, e1, e2, elle1e2))

    if showFwhm:
        title.append("FWHM (arcsec)")
        if len(xs) > 0:
            fwhmResampled = griddata(xArr, yArr, fwhmArr, xs, ys)
            plt.imshow(fwhmResampled.T, extent=extent, vmin=minFwhm, vmax=maxFwhm, origin="lower")
            plt.colorbar()

        if outputTxtFileName:

            ndataGrids = getNumDataGrids(xArr, yArr, fwhmArr, xs, ys)

            f = open(outputTxtFileName + "-fwhm-grid.txt", "w")
            f.write("# %s visit %s\n" % (" ".join(title), visit))
            for xline, yline, fwhmline, ndataline in zip(xs.tolist(), ys.tolist(), fwhmResampled.tolist(), ndataGrids):
                for xx, yy, fwhm, ndata in zip(xline, yline, fwhmline, ndataline):
                    if fwhm is None:
                        fwhm = -9999
                    f.write("%f %f %f %d\n" % (xx, yy, fwhm, ndata))

    elif showEllipticity:
        title.append("Ellipticity")
        scale = 4

        if ellipticityDirection:  # we don't care about the magnitude
            ellArr = 0.1

        u = -ellArr * np.cos(paArr)
        v = -ellArr * np.sin(paArr)
        if gridPoints > 0:
            u = griddata(xArr, yArr, u, xs, ys)
            v = griddata(xArr, yArr, v, xs, ys)
            x, y = xs, ys
        else:
            x, y = xArr, yArr

        Q = plt.quiver(x, y, u, v, scale=scale, pivot="middle", headwidth=0, headlength=0, headaxislength=0)
        keyLen = 0.10
        if not ellipticityDirection:  # we care about the magnitude
            plt.quiverkey(Q, 0.20, 0.95, keyLen, "e=%g" % keyLen, labelpos="W")

        if outputTxtFileName:
            ndataGrids = getNumDataGrids(xArr, yArr, ellArr, xs, ys)

            f = open(outputTxtFileName + "-ell-grid.txt", "w")
            f.write("# %s visit %s\n" % (" ".join(title), visit))
            # f.write('# %f %f %f %f %f %f %f\n' % (x, y, ell, fwhm, pa, a, b))
            for xline, yline, uline, vline, ndataline in zip(
                x.tolist(), y.tolist(), u.tolist(), v.tolist(), ndataGrids
            ):
                for xx, yy, uu, vv, ndata in zip(xline, yline, uline, vline, ndataline):
                    if uu is None:
                        uu = -9999
                    if vv is None:
                        vv = -9999
                    f.write("%f %f %f %f %d\n" % (xx, yy, uu, vv, ndata))

    elif showNdataFwhm:
        title.append("N per fwhm grid")
        if len(xs) > 0:
            ndataGrids = getNumDataGrids(xArr, yArr, fwhmArr, xs, ys)
            plt.imshow(ndataGrids, interpolation="nearest", extent=extent, vmin=minNdata, vmax=maxNdata, origin="lower")
            plt.colorbar()
        else:
            pass

    elif showNdataEll:
        title.append("N per ell grid")
        if len(xs) > 0:
            ndataGrids = getNumDataGrids(xArr, yArr, ellArr, xs, ys)
            plt.imshow(ndataGrids, interpolation="nearest", extent=extent, vmin=minNdata, vmax=maxNdata, origin="lower")
            plt.colorbar()
        else:
            pass

    # plt.plot(xArr, yArr, "r.")
    # plt.plot(xs, ys, "b.")
    plt.axes().set_aspect("equal")
    plt.axis([-20000, 20000, -20000, 20000])

    def frameInfoFrom(filepath):
        import pyfits

        with pyfits.open(filepath) as hdul:
            h = hdul[0].header
            "object=ABELL2163 filter=HSC-I exptime=360.0 alt=62.11143274 azm=202.32265181 hst=(23:40:08.363-23:40:48.546)"
            return "object=%s filter=%s exptime=%.1f azm=%.2f hst=%s" % (
                h["OBJECT"],
                h["FILTER01"],
                h["EXPTIME"],
                h["AZIMUTH"],
                h["HST"],
            )

    title.insert(0, frameInfoFrom(butler.get("raw_filename", {"visit": visit, "ccd": 0})[0]))
    title.append(r'$\langle$FWHM$\rangle %4.2f$"' % np.median(fwhmArr))
    plt.title("%s visit=%s" % (" ".join(title), visit), fontsize=9)

    return plt
Ejemplo n.º 35
0
def ditherDES(camera, scale=4.5,  names=False):
    positions = [
        (    0.0,     0.0),
        (   65.0,   100.0),
        (   57.0,   -99.0),
        ( -230.0,  -193.0),
        ( -136.0,   267.0),
        ( -430.0,   417.0),
        (  553.0,   233.0),
        (  628.0,   940.0),
        (  621.0,  -946.0),
        ( -857.0,  1425.0),
        (   10.0,     5.0),
        ( 1647.0,   231.0),
        ( 1305.0, -1764.0),
        (-2134.0,  -511.0),
        ( 2700.0,   370.0),
        ( -714.0, -2630.0),
        ( 2339.0, -2267.0),
        (-3001.0, -1267.0),
        ( -152.0, -3785.0),
        (-3425.0,  1619.0),
        ( 1862.0,  3038.0),
        (    5.0,    10.0),
        ]

    xdither, ydither = [x*scale for x, y in positions], [y*scale for x, y in positions]
    x0, y0 = xdither[0], ydither[0]
    for x, y in zip(xdither, ydither):
        print "%8.1f %8.1f   %8.1f %8.1f" % (x, y, x - x0, y - y0)
        x0, y0 = x, y

    pyplot.clf()
    pyplot.plot(xdither, ydither, "o")
    pyplot.axes().set_aspect('equal')

    for raft in camera:
        raft = afwCG.cast_Raft(raft)
        for ccd in raft:
            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 = [y0, y1]
                    xList = [x0]*len(yList)
                elif y0 == y1 and x0 != x1:
                    xList = [x0, x1]
                    yList = [y0]*len(xList)
                else:
                    raise RuntimeError("Should never get here")

                xOriginal = []; yOriginal = []
                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())

                pyplot.plot(xOriginal, yOriginal, 'k-')

            x,y = ccd.getPositionFromPixel(afwGeom.Point2D(width/2, height/2)).getMm()
            cid = ccd.getId()
            if names:
                pyplot.text(x, y, cid.getName(), ha='center', rotation=90 if height > width else 0,
                            fontsize="smaller")
            else:
                pyplot.text(x, y, cid.getSerial(), ha='center', va='center')
Ejemplo n.º 36
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)
Ejemplo n.º 37
0
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()
Ejemplo n.º 38
0
 def batchWallTime(cls, time, parsedCmd, numNodes, numProcs):
     numCcds = sum(1 for raft in parsedCmd.butler.get("camera")
                   for ccd in afwCg.cast_Raft(raft))
     numCycles = int(math.ceil(numCcds / float(numNodes * numProcs)))
     numExps = len(cls.RunnerClass.getTargetList(parsedCmd))
     return time * numExps * numCycles
Ejemplo n.º 39
0
def main(dataDir, visit, title="", outputTxtFileName=None,
         showFwhm=False, minFwhm=None, maxFwhm=None,
         correctDistortion=False,
         showEllipticity=False, ellipticityDirection=False,
         showNdataFwhm=False, showNdataEll=False,
         minNdata=None, maxNdata=None, 
         gridPoints=30, verbose=False):

    butler = dafPersist.ButlerFactory(mapper=hscSim.HscSimMapper(root=dataDir)).create()
    camera = butler.get("camera")

    if not (showFwhm or showEllipticity or showNdataFwhm or showNdataEll or outputTxtFileName):
        showFwhm = True
    #
    # Get a dict of cameraGeom::Ccd indexed by serial number
    #
    ccds = {}
    for raft in camera:
        raft = cameraGeom.cast_Raft(raft)
        for ccd in raft:
            ccd.setTrimmed(True)
            ccds[ccd.getId().getSerial()] = ccd
    #
    # Read all the tableSeeingMap files, converting their (x, y) to focal plane coordinates
    #
    xArr = []; yArr = []; ellArr = []; fwhmArr = []
    paArr = []; aArr = []; bArr = [];
    e1Arr=[]; e2Arr=[]; elle1e2Arr =[];
    for tab in butler.subset("tableSeeingMap", visit=visit):
        # we could use tab.datasetExists() but it prints a rude message
        fileName = butler.get("tableSeeingMap_filename", **tab.dataId)[0]
        if not os.path.exists(fileName):
            continue

        with open(fileName) as fd:
            ccd = None
            for line in fd.readlines():
                if re.search(r"^\s*#", line):
                    continue
                fields = [float(_) for _ in line.split()]

                if ccd is None:
                    ccd = ccds[int(fields[0])]

                x, y, fwhm, ell, pa, a, b  = fields[1:8]
                x, y = ccd.getPositionFromPixel(afwGeom.PointD(x, y)).getMm()
                xArr.append(x)
                yArr.append(y)
                ellArr.append(ell)
                fwhmArr.append(fwhm)
                paArr.append(pa)
                aArr.append(a)
                bArr.append(b)
                if len(fields) == 11:
                    e1 = fields[8]
                    e2 = fields[9]
                    elle1e2 = fields[10]
                else:
                    e1 = -9999.; e2 = -9999. ; elle1e2 = -9999.
                e1Arr.append(e1)
                e2Arr.append(e2)
                elle1e2Arr.append(elle1e2)

    xArr = np.array(xArr)
    yArr = np.array(yArr)
    ellArr = np.array(ellArr)
    fwhmArr = np.array(fwhmArr)*0.168   # arcseconds
    paArr = np.radians(np.array(paArr))
    aArr = np.array(aArr)
    bArr = np.array(bArr)

    e1Arr = np.array(e1Arr)
    e2Arr = np.array(e2Arr)
    elle1e2Arr = np.array(elle1e2Arr)


    if correctDistortion:
        import lsst.afw.geom.ellipses as afwEllipses

        dist = camera.getDistortion()
        for i in range(len(aArr)):
            axes = afwEllipses.Axes(aArr[i], bArr[i], paArr[i])
            if False:                                                       # testing only!
                axes = afwEllipses.Axes(1.0, 1.0, np.arctan2(yArr[i], xArr[i]))
            quad = afwEllipses.Quadrupole(axes)
            quad = quad.transform(dist.computeQuadrupoleTransform(afwGeom.PointD(xArr[i], yArr[i]), False))
            axes = afwEllipses.Axes(quad)
            aArr[i], bArr[i], paArr[i] = axes.getA(), axes.getB(), axes.getTheta()

        ellArr = 1 - bArr/aArr

    if len(xArr) == 0:
        gridPoints = 0
        xs, ys = [], []
    else:
        N = gridPoints*1j
        extent = [min(xArr), max(xArr), min(yArr), max(yArr)]
        xs,ys = np.mgrid[extent[0]:extent[1]:N, extent[2]:extent[3]:N]

    title = [title,]

    title.append("\n#")

    if outputTxtFileName:
        f = open(outputTxtFileName, 'w')
        f.write("# %s visit %s\n" % (" ".join(title), visit))
        for x, y, ell, fwhm, pa, a, b, e1, e2, elle1e2 in zip(xArr, yArr,  ellArr, fwhmArr, paArr, aArr, bArr, e1Arr, e2Arr, elle1e2Arr):
            f.write('%f %f %f %f %f %f %f %f %f %f\n' % (x, y, ell, fwhm, pa, a, b, e1, e2, elle1e2))

    if showFwhm:
        title.append("FWHM (arcsec)")
        if len(xs) > 0:
            fwhmResampled = griddata(xArr, yArr, fwhmArr, xs, ys)
            plt.imshow(fwhmResampled.T, extent=extent, vmin=minFwhm, vmax=maxFwhm, origin='lower')
            plt.colorbar()

        if outputTxtFileName:

            ndataGrids = getNumDataGrids(xArr, yArr, fwhmArr, xs, ys)

            f = open(outputTxtFileName+'-fwhm-grid.txt', 'w')
            f.write("# %s visit %s\n" % (" ".join(title), visit))
            for xline, yline, fwhmline, ndataline in zip(xs.tolist(), ys.tolist(), fwhmResampled.tolist(), ndataGrids):
                for xx, yy, fwhm, ndata in zip(xline, yline, fwhmline, ndataline):
                    if fwhm is None:
                        fwhm = -9999
                    f.write('%f %f %f %d\n' % (xx, yy, fwhm, ndata))

    elif showEllipticity:
        title.append("Ellipticity")
        scale = 4

        if ellipticityDirection:        # we don't care about the magnitude
            ellArr = 0.1

        u =  -ellArr*np.cos(paArr)
        v =  -ellArr*np.sin(paArr)
        if gridPoints > 0:
            u = griddata(xArr, yArr, u, xs, ys)
            v = griddata(xArr, yArr, v, xs, ys)
            x, y = xs, ys
        else:
            x, y = xArr, yArr

        Q = plt.quiver(x, y, u, v, scale=scale,
                       pivot="middle",
                       headwidth=0,
                       headlength=0,
                       headaxislength=0,
                       )
        keyLen = 0.10
        if not ellipticityDirection:    # we care about the magnitude
            plt.quiverkey(Q, 0.20, 0.95, keyLen, "e=%g" % keyLen, labelpos='W')

        if outputTxtFileName:
            ndataGrids = getNumDataGrids(xArr, yArr, ellArr, xs, ys)

            f = open(outputTxtFileName+'-ell-grid.txt', 'w')
            f.write("# %s visit %s\n" % (" ".join(title), visit))
            #f.write('# %f %f %f %f %f %f %f\n' % (x, y, ell, fwhm, pa, a, b))
            for xline, yline, uline, vline, ndataline in zip(x.tolist(), y.tolist(), u.tolist(), v.tolist(), ndataGrids):
                for xx, yy, uu, vv, ndata in zip(xline, yline, uline, vline, ndataline):
                    if uu is None:
                        uu = -9999
                    if vv is None:
                        vv = -9999
                    f.write('%f %f %f %f %d\n' % (xx, yy, uu, vv, ndata))

    elif showNdataFwhm:
        title.append("N per fwhm grid")
        if len(xs) > 0:
            ndataGrids = getNumDataGrids(xArr, yArr, fwhmArr, xs, ys)
            plt.imshow(ndataGrids, interpolation='nearest', extent=extent, vmin=minNdata, vmax=maxNdata, origin='lower')
            plt.colorbar()
        else:
            pass

    elif showNdataEll:
        title.append("N per ell grid")
        if len(xs) > 0:
            ndataGrids = getNumDataGrids(xArr, yArr, ellArr, xs, ys)
            plt.imshow(ndataGrids, interpolation='nearest', extent=extent, vmin=minNdata, vmax=maxNdata, origin='lower')
            plt.colorbar()
        else:
            pass

    #plt.plot(xArr, yArr, "r.")
    #plt.plot(xs, ys, "b.")
    plt.axes().set_aspect('equal')
    plt.axis([-20000, 20000, -20000, 20000])

    def frameInfoFrom(filepath):
        import pyfits
        with pyfits.open(filepath) as hdul:
            h = hdul[0].header
            'object=ABELL2163 filter=HSC-I exptime=360.0 alt=62.11143274 azm=202.32265181 hst=(23:40:08.363-23:40:48.546)'
            return 'object=%s filter=%s exptime=%.1f azm=%.2f hst=%s' % (h['OBJECT'], h['FILTER01'], h['EXPTIME'], h['AZIMUTH'], h['HST'])

    title.insert(0, frameInfoFrom(butler.get('raw_filename', {'visit':visit, 'ccd':0})[0]))
    title.append(r'$\langle$FWHM$\rangle %4.2f$"' % np.median(fwhmArr))
    plt.title("%s visit=%s" % (" ".join(title), visit), fontsize=9)

    return plt
    def plot(self, data, dataId, showUndefined=False):

        
        testSet = self.getTestSet(data, dataId)
        testSet.setUseCache(self.useCache)
        isFinalDataId = False
        if len(data.brokenDataIdList) == 0 or data.brokenDataIdList[-1] == dataId:
            isFinalDataId = True
        
        # make fpa figures - for all detections, and for matched detections
        emptyBase    = "emptySectors"
        emptyMatBase = "aa_emptySectorsMat"

        emptyFig    = qaFig.FpaQaFigure(data.cameraInfo, data=None, map=None)
        emptyFigMat = qaFig.FpaQaFigure(data.cameraInfo, data=None, map=None)

        if self.summaryProcessing != self.summOpt['summOnly']:
            for raft, ccdDict in emptyFig.data.items():
                for ccd, value in ccdDict.items():

                    # set values for data[raft][ccd] (color coding)
                    # set values for map[raft][ccd]  (tooltip text)
                    if self.emptySectors.get(raft, ccd) is not None:
                        nEmpty = self.emptySectors.get(raft, ccd)
                        emptyFig.data[raft][ccd] = nEmpty
                        emptyFig.map[raft][ccd] = "%dx%d,empty=%d" % (self.nx, self.ny, nEmpty)

                        nEmptyMat = self.emptySectorsMat.get(raft, ccd)
                        emptyFigMat.data[raft][ccd] = nEmptyMat
                        emptyFigMat.map[raft][ccd] = "%dx%d,empty=%d" % (self.nx, self.ny, nEmptyMat)
                        
                        label = data.cameraInfo.getDetectorName(raft, ccd)
                        
                        testSet.pickle(emptyBase + label, [emptyFig.data, emptyFig.map])
                        testSet.pickle(emptyMatBase + label, [emptyFigMat.data, emptyFigMat.map])


        # make the figures and add them to the testSet
        # sample colormaps at: http://www.scipy.org/Cookbook/Matplotlib/Show_colormaps


        if (self.summaryProcessing in [self.summOpt['summOnly'], self.summOpt['delay']]) and isFinalDataId:

            for raft, ccdDict in emptyFig.data.items():
                for ccd, value in ccdDict.items():
                    label = data.cameraInfo.getDetectorName(raft, ccd)
                    emptyDataTmp, emptyMapTmp       = testSet.unpickle(emptyBase+label, [None, None])
                    emptyFig.mergeValues(emptyDataTmp, emptyMapTmp)
                    emptyMatDataTmp, emptyMatMapTmp = testSet.unpickle(emptyMatBase+label, [None, None])
                    emptyFigMat.mergeValues(emptyMatDataTmp, emptyMatMapTmp)


            self.log.log(self.log.INFO, "plotting FPAs")
            emptyFig.makeFigure(showUndefined=showUndefined, cmap="gist_heat_r",
                                vlimits=[0, self.nx*self.ny],
                                title="Empty sectors (%dx%d grid)" % (self.nx, self.ny),
                                failLimits=self.limits)
            testSet.addFigure(emptyFig, emptyBase+".png",
                              "Empty Sectors in %dx%d grid." % (self.nx, self.ny), navMap=True)
            
            emptyFigMat.makeFigure(showUndefined=showUndefined, cmap="gist_heat_r",
                                   vlimits=[0, self.nx*self.ny],
                                   title="Empty sectors (matched, %dx%d grid)" % (self.nx, self.ny),
                                   failLimits=self.limits)
            testSet.addFigure(emptyFigMat, emptyMatBase+".png",
                              "Empty Sectors in %dx%d grid." % (self.nx, self.ny), navMap=True)


        del emptyFig, emptyFigMat


        cacheLabel = "pointPositions"

        l_cam, b_cam, r_cam, t_cam = 1.0e6, 1.0e6, -1.0e6, -1.0e6
        for r in data.cameraInfo.camera:
            raft = camGeom.cast_Raft(r)
            for c in raft:
                ccd = camGeom.cast_Ccd(c)
                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)
        xlo, xhi, ylo, yhi = l_cam, r_cam, b_cam, t_cam

        if self.summaryProcessing != self.summOpt['summOnly']:

            # make any individual (ie. per sensor) plots
            for raft, ccd in self.emptySectors.raftCcdKeys():

                # get the data we want for this sensor (we stored it here in test() method above)
                x, y       = self.x.get(raft, ccd), self.y.get(raft, ccd)
                xmat, ymat = self.xmat.get(raft, ccd), self.ymat.get(raft, ccd)
                xwid, ywid = self.size.get(raft, ccd)

                ra, dec    = self.ra.get(raft, ccd), self.dec.get(raft, ccd)
                ramat, decmat = self.ramat.get(raft, ccd), self.decmat.get(raft, ccd)
                
                if data.cameraInfo.name == 'coadd':
                    xmin, ymin, xmax, ymax = x.min(), y.min(), x.max(), y.max()
                    x -= xmin
                    y -= ymin
                    xmat -= xmin
                    ymat -= ymin
                    xxlo, yylo, xxhi, yyhi = xmin, ymin, xmax, ymax
                else:
                    xxlo, yylo, xxhi, yyhi = data.cameraInfo.getBbox(raft, ccd)

                dataDict = {'x' : x+xxlo, 'y' : y+yylo, 'xmat' : xmat+xxlo, 'ymat' : ymat+yylo,
                            'ra' : ra, 'dec': dec, 'ramat': ramat, 'decmat': decmat,
                            'limits' : [0, xwid, 0, ywid],
                            'summary' : False, 'alllimits' : [xlo, xhi, ylo, yhi],
                            'bbox' : [xxlo, xxhi, yylo, yyhi],
                            'nxn' : [self.nx, self.ny]}
                
                self.log.log(self.log.INFO, "plotting %s" % (ccd))
                import EmptySectorQaPlot as plotModule
                label = data.cameraInfo.getDetectorName(raft, ccd)
                caption = "Pixel coordinates of all (black) and matched (red) detections." + label
                pngFile = cacheLabel+".png"


                if self.lazyPlot.lower() in ['sensor', 'all']:
                    testSet.addLazyFigure(dataDict, pngFile, caption,
                                          plotModule, areaLabel=label, plotargs="")
                else:
                    testSet.cacheLazyData(dataDict, pngFile, areaLabel=label)
                    fig = plotModule.plot(dataDict)
                    testSet.addFigure(fig, pngFile, caption, areaLabel=label)
                    del fig

            
        if (self.summaryProcessing in [self.summOpt['summOnly'], self.summOpt['delay']]) and isFinalDataId:

            self.log.log(self.log.INFO, "plotting Summary figure")


            import EmptySectorQaPlot as plotModule
            label = 'all'
            caption = "Pixel coordinates of all (black) and matched (red) detections." + label
            pngFile = "pointPositions.png"

            if self.lazyPlot in ['all']:
                testSet.addLazyFigure({}, cacheLabel+".png", caption,
                                      plotModule, areaLabel=label, plotargs="")
            else:
                dataDict, isSummary = qaPlotUtil.unshelveGlob(cacheLabel+"-all.png", testSet=testSet)
                dataDict['summary'] = True
                if 'x' in dataDict:
                    dataDict['limits'] = dataDict['alllimits']
                    fig = plotModule.plot(dataDict)                
                    testSet.addFigure(fig, pngFile, caption, areaLabel=label)
                    del fig

            self.combineOutputs(data, dataId)
    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()
Ejemplo n.º 42
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)
Ejemplo n.º 43
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()
    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()
Ejemplo n.º 45
0
def ditherDES(camera, scale=4.5, names=False):
    positions = [
        (0.0, 0.0),
        (65.0, 100.0),
        (57.0, -99.0),
        (-230.0, -193.0),
        (-136.0, 267.0),
        (-430.0, 417.0),
        (553.0, 233.0),
        (628.0, 940.0),
        (621.0, -946.0),
        (-857.0, 1425.0),
        (10.0, 5.0),
        (1647.0, 231.0),
        (1305.0, -1764.0),
        (-2134.0, -511.0),
        (2700.0, 370.0),
        (-714.0, -2630.0),
        (2339.0, -2267.0),
        (-3001.0, -1267.0),
        (-152.0, -3785.0),
        (-3425.0, 1619.0),
        (1862.0, 3038.0),
        (5.0, 10.0),
    ]

    xdither, ydither = [x * scale for x, y in positions
                        ], [y * scale for x, y in positions]
    x0, y0 = xdither[0], ydither[0]
    for x, y in zip(xdither, ydither):
        print "%8.1f %8.1f   %8.1f %8.1f" % (x, y, x - x0, y - y0)
        x0, y0 = x, y

    pyplot.clf()
    pyplot.plot(xdither, ydither, "o")
    pyplot.axes().set_aspect('equal')

    for raft in camera:
        raft = afwCG.cast_Raft(raft)
        for ccd in raft:
            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 = [y0, y1]
                    xList = [x0] * len(yList)
                elif y0 == y1 and x0 != x1:
                    xList = [x0, x1]
                    yList = [y0] * len(xList)
                else:
                    raise RuntimeError("Should never get here")

                xOriginal = []
                yOriginal = []
                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())

                pyplot.plot(xOriginal, yOriginal, 'k-')

            x, y = ccd.getPositionFromPixel(
                afwGeom.Point2D(width / 2, height / 2)).getMm()
            cid = ccd.getId()
            if names:
                pyplot.text(x,
                            y,
                            cid.getName(),
                            ha='center',
                            rotation=90 if height > width else 0,
                            fontsize="smaller")
            else:
                pyplot.text(x, y, cid.getSerial(), ha='center', va='center')
Ejemplo n.º 46
0
def main(camera, sample=20, names=False, showDistortion=True, plot=True, outputFile=None):
    if plot:
        fig = plt.figure(1)
        fig.clf()
        ax = fig.add_axes((0.1, 0.1, 0.8, 0.8))
        ax.set_title('%s  Distorted CCDs' % camera.getId().getName())
    else:
        fig = None

    dist = camera.getDistortion()

    for raft in camera:
        raft = cameraGeom.cast_Raft(raft)
        for ccd in raft:
            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-')
                    ax.plot(xDistort, yDistort, 'r-')

            if fig:
                x,y = ccd.getPositionFromPixel(afwGeom.Point2D(width/2, height/2)).getMm()
                cid = ccd.getId()
                if names:
                    ax.text(x, y, cid.getName(), ha='center', rotation=90 if height > width else 0,
                            fontsize="smaller")
                else:
                    ax.text(x, y, cid.getSerial(), ha='center', va='center')

    if fig:
        if camera.getId().getName() == "HSC":
            from matplotlib.patches import Circle
            cen = (0, 0)
            ax.add_patch(Circle(cen, radius=18100, color='black', alpha=0.2))
            if showDistortion:
                ax.add_patch(Circle(cen, radius=19000, color='red', alpha=0.2))

            for x, y, t in ([-1, 0, "N"], [0, 1, "W"], [1, 0, "S"], [0, -1, "E"]):
                ax.text(19500*x, 19500*y, t, ha="center", va="center")

            plt.axis([-21000, 21000, -21000, 21000])

        ax.set_aspect('equal')
        if outputFile:
            plt.savefig(outputFile)
        else:
            plt.show()
Ejemplo n.º 47
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()
Ejemplo n.º 48
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()
Ejemplo n.º 49
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")
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