Esempio n. 1
0
def main(rerun,
         visit1,
         visit2,
         op,
         ccdno,
         datatype='calexp',
         scale=None,
         root=None,
         invert=False,
         cmap='gray',
         vmax=None,
         annotate=None,
         bins=16,
         rerun2=None,
         showCbar=False,
         vsig=None,
         showAnnotate=False,
         percent=None,
         hilite=None):

    vsigDefault = 5.0
    visit1 = int(visit1)
    if visit2:
        visit2 = int(visit2)
    ccdno = set([int(x) for x in hscUtil.idSplit(ccdno)])

    butler1 = hscUtil.getButler(rerun, root=root)
    if rerun2:
        butler2 = hscUtil.getButler(rerun2, root=root)
    else:
        butler2 = butler1

    if datatype not in calibTypes:
        dataIds1 = butler1.queryMetadata(datatype,
                                         "ccd",
                                         format=["visit", "ccd"],
                                         dataId={'visit': visit1})
        dataIds1 = [{'visit': x[0], 'ccd': x[1]} for x in dataIds1]
    else:
        dataIds1 = [{'visit': visit1, 'ccd': x} for x in range(104)]

    dataRef1 = None
    dataRef2 = None

    if visit2:
        dataIds2 = butler2.queryMetadata(datatype,
                                         "ccd",
                                         format=["visit", "ccd"],
                                         dataId={'visit': visit2})
        dataIds2 = [{'visit': x[0], 'ccd': x[1]} for x in dataIds2]

    # flip the color map
    if invert:
        cmap = re.sub("_r$", "", cmap) if re.match('_r$', cmap) else '_r'

    # sleezy, but if vmax isn't set and we're linear, just use ccd in the middle to norm
    if vmax is None and scale != 'histeq':
        vmax = 'c049'

    # handle the gray scale normalization
    vmin = 0.0
    mdRef1 = None
    mdRef2 = None
    if vmax:
        if scale == 'histeq':
            raise ValueError("Cannot specify vmax with histeq scaling.")

        # if it identifies a CCD to use
        if re.match("^c", vmax):
            vmaxCcd = int(re.sub("c", "", vmax))
            try:
                dataRef1 = hscButler.getDataRef(butler1, {
                    'visit': visit1,
                    'ccd': vmaxCcd
                })
                imgRef1 = dataRef1.get(
                    datatype).getMaskedImage().getImage().getArray()
                if datatype not in calibTypes:
                    mdRef1 = dataRef1.get(datatype + '_md', immediate=True)
                if visit2:
                    dataRef2 = hscButler.getDataRef(butler2, {
                        'visit': visit2,
                        'ccd': vmaxCcd
                    })
                    imgRef2 = dataRef2.get(
                        datatype).getMaskedImage().getImage().getArray()
                    mdRef2 = dataRef2.get(datatype + '_md', immediate=True)
                    img_op = hscUtil.rebin(compute(imgRef1, imgRef2, op), bins)
                    med = numpy.median(img_op)
                    std = numpy.std(img_op)
                    if not vsig:
                        vsig = vsigDefault
                    delta = vsig * std
                    if percent:
                        delta = percent * med
                    vmin = med - abs(delta)
                    vmax = med + abs(delta)
                else:
                    if showAnnotate:
                        exp1 = dataRef1.get(datatype)
                        aval = float(exp1.getMetadata().get(annotate))
                        med = aval
                        delta = vsig
                        if not vsig:
                            delta = 0.5 * aval
                        if percent:
                            delta = percent * aval
                        vmin = med - abs(delta)
                        vmax = med + abs(delta)
                    else:
                        img_op = imgRef1
                        med = numpy.median(img_op)
                        sig = numpy.sqrt(med)
                        delta = vsigDefault * sig
                        if vsig:
                            delta = vsig * sig
                        if percent:
                            delta = percent * med
                        vmin = med - abs(delta)
                        vmax = med + abs(delta)
                        if not vsig and not percent:
                            vmin = 0.5 * med
                            vmax = med + 5.0 * sig

            except Exception, e:
                raise RuntimeError("Could not get stats on vmax CCD" +
                                   str(vmax) + "  Exiting." + str(e))

        elif re.search(":", vmax):
            vmaxCcd = None
            vmin, vmax = [float(x) for x in vmax.split(":")]
            med = 0.5 * (vmax + vmin)
        else:
            vmaxCcd = None
            vmax = float(vmax)
            med = 0.5 * (vmax + vmin)
Esempio n. 2
0
def main(visit,
         rerun,
         out,
         ccd="0..103",
         root=None,
         point=None,
         minchips=1,
         showLabels=False,
         boresightOnly=False,
         edge=0.9):

    pcen, rad, pcenVisit = None, None, None
    if point:
        pcenArgs = map(float, point.split(":"))
        if len(pcenArgs) == 2:
            pcenVisit, rad = pcenArgs
        elif len(pcenArgs) == 3:
            cx, cy, rad = pcenArgs
            pcen = afwCoord.Fk5Coord(afwGeom.Point2D(cx, cy))
        else:
            raise ValueError(
                "Unable to parse center 'point', must be ra:dec:rad or visit:rad"
            )

    print visit, rerun
    butler = hscUtil.getButler(rerun, root=root)

    visitWidths = []
    visitHeights = []

    dataIds = visitCcdToDataId(visit, ccd)
    corners = {}
    boresights = {}
    for dataId in dataIds:
        visit, ccd = dataId['visit'], dataId['ccd']

        if boresightOnly and visit in boresights:
            continue

        print "Getting ", visit, ccd
        try:
            dataRef = hscButler.getDataRef(butler, dataId)
        except Exception, e:
            print "getDataRef() failed for ", visit, ccd, str(e)
            continue

        calfile = dataRef.get("calexp_filename", immediate=True)[0]
        noCal = False
        if not os.path.exists(calfile):
            print calfile + " missing."
            noCal = True
        else:
            # get the calexp
            calmd = dataRef.get("calexp_md", immediate=True)
            wcs = afwImage.makeWcs(calmd)
            w, h = calmd.get("NAXIS1"), calmd.get("NAXIS2")

            boresights[dataId['visit']] = [
                calmd.get("RA2000"), calmd.get("DEC2000")
            ]

            # get the corners in RA,Dec
            ll = wcs.pixelToSky(afwGeom.Point2D(1, 1)).toFk5()
            lr = wcs.pixelToSky(afwGeom.Point2D(w, 1)).toFk5()
            ul = wcs.pixelToSky(afwGeom.Point2D(1, h)).toFk5()
            ur = wcs.pixelToSky(afwGeom.Point2D(w, h)).toFk5()

        if noCal:

            rawfile = dataRef.get("raw_filename", immediate=True)[0]
            if not os.path.exists(rawfile):
                print rawfile + " missing too.  continuing."
                continue
            else:
                rawmd = dataRef.get("raw_md", immediate=True)
                raS, decS = rawmd.get("RA2000"), rawmd.get("DEC2000")
                boresights[dataId['visit']] = [raS, decS]
                #ra, dec = c.getRa().asDegrees(), c.getDec().asDegrees()
                crv1, crv2 = map(float,
                                 [rawmd.get("CRVAL1"),
                                  rawmd.get("CRVAL2")])
                crp1, crp2 = map(float,
                                 [rawmd.get("CRPIX1"),
                                  rawmd.get("CRPIX2")])

                useDelt = True
                try:
                    cdelt1, cdelt2 = map(
                        float, [rawmd.get("CDELT1"),
                                rawmd.get("CDELT1")])
                except:
                    useDelt = False
                if not useDelt:
                    cd11, cd12, cd21, cd22 = map(float, [
                        rawmd.get("CD1_1"),
                        rawmd.get("CD1_2"),
                        rawmd.get("CD2_1"),
                        rawmd.get("CD2_2")
                    ])
                    cdelt1 = 3600 * (cd11 + cd12) / 206265
                    cdelt2 = 3600 * (cd21 + cd22) / 206265

                    #cdelt1, cdelt2 = 1.0, 1.0

                nx, ny = map(int, [rawmd.get("NAXIS1"), rawmd.get("NAXIS2")])

                def trans(x, y):
                    ra = crv1 + (crp1 - x) * cdelt1
                    dec = crv2 + (crp2 - y) * cdelt2
                    return ra, dec

                ll = afwCoord.Fk5Coord(afwGeom.Point2D(*trans(1, 1)))
                lr = afwCoord.Fk5Coord(afwGeom.Point2D(*trans(nx, 1)))
                ul = afwCoord.Fk5Coord(afwGeom.Point2D(*trans(1, ny)))
                ur = afwCoord.Fk5Coord(afwGeom.Point2D(*trans(nx, ny)))

        if pcenVisit and not pcen:
            if int(pcenVisit) == int(visit):
                cx, cy = boresights[pcenVisit]
                pcen = afwCoord.Fk5Coord(cx, cy)

        if visit not in corners:
            corners[visit] = []

        corners[visit].append([ll, lr, ur, ul, ccd])
Esempio n. 3
0
def main(visit, rerun, out, ccd="0..103", root=None, 
         point=None, minchips=1, showLabels=False, boresightOnly=False, edge=0.9):

    pcen, rad, pcenVisit = None, None, None
    if point:
        pcenArgs = map(float, point.split(":"))
        if len(pcenArgs) == 2:
            pcenVisit, rad = pcenArgs
        elif len(pcenArgs) == 3:
            cx, cy, rad = pcenArgs
            pcen = afwCoord.Fk5Coord(afwGeom.Point2D(cx, cy))
        else:
            raise ValueError("Unable to parse center 'point', must be ra:dec:rad or visit:rad")

        
    print visit, rerun
    butler = hscUtil.getButler(rerun, root=root)

    visitWidths = []
    visitHeights = []
    
    dataIds = visitCcdToDataId(visit, ccd)
    corners = {}
    boresights = {}
    for dataId in dataIds:
        visit, ccd = dataId['visit'], dataId['ccd']

        if boresightOnly and visit in boresights:
            continue
        
        print "Getting ", visit, ccd
        try:
            dataRef = hscButler.getDataRef(butler, dataId)
        except Exception, e:
            print "getDataRef() failed for ", visit, ccd, str(e)
            continue

        calfile = dataRef.get("calexp_filename", immediate=True)[0]
        noCal = False
        if not os.path.exists(calfile):
            print calfile+" missing."
            noCal = True
        else:
            # get the calexp
            calmd = dataRef.get("calexp_md", immediate=True)
            wcs = afwImage.makeWcs(calmd)
            w, h = calmd.get("NAXIS1"), calmd.get("NAXIS2")

            boresights[dataId['visit']] = [calmd.get("RA2000"), calmd.get("DEC2000")]
            
            # get the corners in RA,Dec
            ll = wcs.pixelToSky(afwGeom.Point2D(1,1)).toFk5()
            lr = wcs.pixelToSky(afwGeom.Point2D(w,1)).toFk5()
            ul = wcs.pixelToSky(afwGeom.Point2D(1,h)).toFk5()
            ur = wcs.pixelToSky(afwGeom.Point2D(w,h)).toFk5()
            
        if noCal:

            rawfile = dataRef.get("raw_filename", immediate=True)[0]
            if not os.path.exists(rawfile):
                print rawfile+" missing too.  continuing."
                continue
            else:
                rawmd = dataRef.get("raw_md", immediate=True)
                raS, decS = rawmd.get("RA2000"), rawmd.get("DEC2000")
                boresights[dataId['visit']] = [raS, decS]
                #ra, dec = c.getRa().asDegrees(), c.getDec().asDegrees()
                crv1, crv2 = map(float, [rawmd.get("CRVAL1"), rawmd.get("CRVAL2")])
                crp1, crp2 = map(float, [rawmd.get("CRPIX1"), rawmd.get("CRPIX2")])

                useDelt = True
                try:
                    cdelt1, cdelt2  = map(float, [rawmd.get("CDELT1"), rawmd.get("CDELT1")])
                except:
                    useDelt= False
                if not useDelt:
                    cd11, cd12, cd21, cd22 = map(float, [rawmd.get("CD1_1"),
                                                         rawmd.get("CD1_2"),
                                                         rawmd.get("CD2_1"),
                                                         rawmd.get("CD2_2")])
                    cdelt1 = 3600*(cd11 + cd12)/206265
                    cdelt2 = 3600*(cd21 + cd22)/206265

                    #cdelt1, cdelt2 = 1.0, 1.0

                nx, ny  = map(int, [rawmd.get("NAXIS1"), rawmd.get("NAXIS2")])

                def trans(x, y):
                    ra = crv1 + (crp1-x)*cdelt1
                    dec = crv2 + (crp2-y)*cdelt2
                    return ra, dec

                ll = afwCoord.Fk5Coord(afwGeom.Point2D(*trans(1, 1)))
                lr = afwCoord.Fk5Coord(afwGeom.Point2D(*trans(nx, 1)))
                ul = afwCoord.Fk5Coord(afwGeom.Point2D(*trans(1, ny)))
                ur = afwCoord.Fk5Coord(afwGeom.Point2D(*trans(nx, ny)))

        if pcenVisit and not pcen:
            if int(pcenVisit) == int(visit):
                cx, cy = boresights[pcenVisit]
                pcen = afwCoord.Fk5Coord(cx, cy)

        if visit not in corners:
            corners[visit] = []
            
        corners[visit].append([ll, lr, ur, ul, ccd])
Esempio n. 4
0
def main(rerun, visit1, visit2, op, ccdno,
         datatype='calexp', scale=None, root=None, invert=False, cmap='gray',
         vmax=None, annotate=None, bins=16, rerun2=None, showCbar=False, vsig=None,
         showAnnotate=False, percent=None, hilite=None):

    vsigDefault = 5.0
    visit1 = int(visit1)
    if visit2:
        visit2 = int(visit2)
    ccdno = set([int(x) for x in hscUtil.idSplit(ccdno)])
    
    butler1 = hscUtil.getButler(rerun, root=root)
    if rerun2:
        butler2 = hscUtil.getButler(rerun2, root=root)
    else:
        butler2 = butler1

    if datatype not in calibTypes:
        dataIds1 = butler1.queryMetadata(datatype, "ccd", format=["visit", "ccd"], dataId={'visit':visit1})
        dataIds1 = [{'visit':x[0], 'ccd':x[1]} for x in dataIds1]
    else:
        dataIds1 = [{'visit': visit1, 'ccd':x} for x in range(104)]
        
    dataRef1 = None
    dataRef2 = None
    
    if visit2:
        dataIds2 = butler2.queryMetadata(datatype, "ccd", format=["visit", "ccd"], dataId={'visit':visit2})
        dataIds2 = [{'visit':x[0], 'ccd':x[1]} for x in dataIds2]
    
    # flip the color map
    if invert:
        cmap = re.sub("_r$", "", cmap) if re.match('_r$', cmap) else '_r'

    # sleezy, but if vmax isn't set and we're linear, just use ccd in the middle to norm 
    if vmax is None and scale != 'histeq':
        vmax = 'c049'

    # handle the gray scale normalization
    vmin = 0.0
    mdRef1 = None
    mdRef2 = None
    if vmax:
        if scale == 'histeq':
            raise ValueError("Cannot specify vmax with histeq scaling.")
        
        # if it identifies a CCD to use
        if re.match("^c", vmax):
            vmaxCcd = int(re.sub("c", "", vmax))
            try:
                dataRef1 = hscButler.getDataRef(butler1, {'visit':visit1, 'ccd':vmaxCcd})
                imgRef1  = dataRef1.get(datatype).getMaskedImage().getImage().getArray()
                if datatype not in calibTypes:
                    mdRef1   = dataRef1.get(datatype+'_md', immediate=True)
                if visit2:
                    dataRef2 = hscButler.getDataRef(butler2, {'visit':visit2, 'ccd':vmaxCcd})
                    imgRef2  = dataRef2.get(datatype).getMaskedImage().getImage().getArray()
                    mdRef2   = dataRef2.get(datatype+'_md', immediate=True)
                    img_op  = hscUtil.rebin(compute(imgRef1, imgRef2, op), bins)
                    med     = numpy.median(img_op)
                    std     = numpy.std(img_op)
                    if not vsig:
                        vsig = vsigDefault
                    delta = vsig*std
                    if percent:
                        delta = percent*med
                    vmin    = med - abs(delta)
                    vmax    = med + abs(delta)
                else:
                    if showAnnotate:
                        exp1 = dataRef1.get(datatype)
                        aval   = float(exp1.getMetadata().get(annotate))
                        med = aval
                        delta = vsig
                        if not vsig:
                            delta = 0.5*aval
                        if percent:
                            delta = percent*aval
                        vmin    = med - abs(delta)
                        vmax    = med + abs(delta)
                    else:
                        img_op  = imgRef1
                        med     = numpy.median(img_op)
                        sig     = numpy.sqrt(med)
                        delta   = vsigDefault*sig
                        if vsig:
                            delta = vsig*sig
                        if percent:
                            delta = percent*med
                        vmin    = med - abs(delta)
                        vmax    = med + abs(delta)
                        if not vsig and not percent:
                            vmin    = 0.5*med
                            vmax    = med + 5.0*sig

                        
            except Exception, e:
                raise RuntimeError("Could not get stats on vmax CCD" + str(vmax)+ "  Exiting." + str(e))

        elif re.search(":", vmax):
            vmaxCcd = None
            vmin, vmax = [float(x) for x in vmax.split(":")]
            med = 0.5*(vmax + vmin)
        else:
            vmaxCcd = None
            vmax = float(vmax)
            med = 0.5*(vmax + vmin)