def checkDipoles(diaSrc):
    dxdyp = []
    dxdyn = []
    for src in diaSrc:
        centPosNaive     = src.get("centroid.dipole.naive.pos")
        centPosNaiveErr  = src.get("centroid.dipole.naive.pos.err")
        centPosNaiveFlag = src.get("centroid.dipole.naive.pos.flags")
        fluxPosNaive     = src.get("flux.dipole.naive.pos")
        fluxPosNaiveErr  = src.get("flux.dipole.naive.pos.err")
        fluxPosNaiveFlag = src.get("flux.dipole.naive.pos.flags")

        centNegNaive     = src.get("centroid.dipole.naive.neg")
        centNegNaiveErr  = src.get("centroid.dipole.naive.neg.err")
        centNegNaiveFlag = src.get("centroid.dipole.naive.neg.flags")
        fluxNegNaive     = src.get("flux.dipole.naive.neg")
        fluxNegNaiveErr  = src.get("flux.dipole.naive.neg.err")
        fluxNegNaiveFlag = src.get("flux.dipole.naive.neg.flags")

        ######

        centPosPsf     = src.get("flux.dipole.psf.pos.centroid")
        centPosPsfErr  = src.get("flux.dipole.psf.pos.centroid.err")
        centPosPsfFlag = src.get("flux.dipole.psf.pos.centroid.flags")
        fluxPosPsf     = src.get("flux.dipole.psf.pos")
        fluxPosPsfErr  = src.get("flux.dipole.psf.pos.err")
        fluxPosPsfFlag = src.get("flux.dipole.psf.pos.flags")

        centNegPsf     = src.get("flux.dipole.psf.neg.centroid")
        centNegPsfErr  = src.get("flux.dipole.psf.neg.centroid.err")
        centNegPsfFlag = src.get("flux.dipole.psf.neg.centroid.flags")
        fluxNegPsf     = src.get("flux.dipole.psf.neg")
        fluxNegPsfErr  = src.get("flux.dipole.psf.neg.err")
        fluxNegPsfFlag = src.get("flux.dipole.psf.neg.flags")

        chi2dof        = src.get("flux.dipole.psf.chi2dof")

        #print centPosPsf, centNegPsf, fluxPosPsf, fluxNegPsf, centPosPsfFlag, fluxPosPsfFlag, centNegPsfFlag, fluxNegPsfFlag
        if not (centPosPsfFlag or fluxPosPsfFlag or centNegPsfFlag or fluxNegPsfFlag):
            dx = centPosPsf[0] - centNegPsf[0]
            dy = centPosPsf[1] - centNegPsf[1]
            totalFlux = fluxPosPsf + fluxNegPsf
            sigmaFlux = totalFlux / np.sqrt(fluxPosPsfErr**2 + fluxNegPsfErr**2)
            if False:
                print centNegPsf, centPosPsf, dx, dy, totalFlux, sigmaFlux, chi2dof
                cmd = "regions command {line %f %f %f %f # color=magenta width=3}" % (centNegPsf[0], centNegPsf[1], centPosPsf[0], centPosPsf[1])
                ds9.ds9Cmd(cmd, silent=False)

            # Lets look at dy, since dx~0 for visits D and E
            if dy < 0.0:
                dxdyn.append((dx, dy))
            else:
                dxdyp.append((dx, dy))

    dxn = np.array([x[0] for x in dxdyn])
    dyn = np.array([x[1] for x in dxdyn])
    dxp = np.array([x[0] for x in dxdyp])
    dyp = np.array([x[1] for x in dxdyp])

    return dxn,dyn,dxp,dyp
def ViewIntensityArrayInDs9(intensity_array, savefile=None):
    import lsst.afw.display.ds9 as ds9
    ds9.mtv(
        makeImageFromArray(100 * intensity_array /
                           float(intensity_array.max())))
    if savefile is not None:
        arg = 'saveimage jpeg ' + str(savefile) + ' 100'
        ds9.ds9Cmd(arg)
Beispiel #3
0
def imagePca(mosaics,
             visits=None,
             nComponent=3,
             log=False,
             rng=30,
             showEigen=True,
             showResiduals=False,
             showOriginal=True,
             showRecon=False,
             normalizeEimages=True,
             scale=False,
             frame0=0,
             verbose=False):

    if showOriginal and showRecon:
        raise RuntimeError(
            "You may not specify both showOriginal and showRecon")

    try:
        rng[0]
    except TypeError:
        rng = [rng, rng]

    if not visits:
        visits = sorted(mosaics.keys())

    mosaics = mosaics.copy()
    for v in visits:
        mosaics[v] = mosaics[v].clone()

    pca = afwImage.ImagePcaF()
    mask = None
    for v in visits:
        im = mosaics[v]
        if not mask:
            mask = im.Factory(im.getDimensions())
            maska = mask.getArray()
            X, Y = np.meshgrid(np.arange(mask.getWidth()),
                               np.arange(mask.getHeight()))
            maska[np.where(np.hypot(X - 571, Y - 552) > 531)] = np.nan
            del maska

            mask[168:184, 701:838] = np.nan
            mask[667:733, 420:556] = np.nan
            mask[653:677, 548:570] = np.nan
            mask[1031:1047, 274:414] = np.nan

            if False:
                mask[866:931, 697:828] = np.nan
                mask[267:334, 145:276] = np.nan

        im += mask

        pca.addImage(im, afwMath.makeStatistics(im, afwMath.SUM).getValue())
    pca.analyze()

    eValues = np.array(pca.getEigenValues())
    eValues /= eValues[0]
    eImages = pca.getEigenImages()
    #
    # Fiddle eigen images (we don't care about orthogonality)
    #
    if False:
        f10 = 0.1
        f20 = -0.3
        f30 = 0.55
        eImages[1].getArray()[:] += f10 * eImages[0].getArray()
        eImages[2].getArray()[:] += f20 * eImages[0].getArray()
        eImages[3].getArray()[:] += f30 * eImages[0].getArray()

    if nComponent:
        eImages = eImages[:nComponent]
    else:
        nComponent = len(eImages)
    #
    # Normalize
    #
    good = np.where(np.isfinite(eImages[0].getArray()))
    if normalizeEimages:
        for eim in eImages:
            eima = eim.getArray()
            eima /= 1e-3 * np.sqrt(np.sum(eima[good]**2))
    #
    # Plot/display eigen values/images
    #
    frame = frame0
    if showEigen:
        for i in range(len(eImages)):
            ds9.mtv(eImages[i], frame=frame, title="%d %.2g" % (i, eValues[i]))
            ds9.ds9Cmd("scale linear; scale mode zscale")
            frame += 1

        pyplot.clf()
        if log:
            eValues = np.log10(eValues)
        pyplot.plot(eValues)
        pyplot.plot(eValues, marker='o')
        pyplot.xlim(-0.5, len(eValues))
        pyplot.ylim(max(-5, pyplot.ylim()[0]), 0.05 if log else 1.05)
        pyplot.xlabel("n")
        pyplot.ylabel(r"$lg(\lambda)$" if log else r"$\lambda$")
        pyplot.show()

    if showOriginal or showRecon or showResiduals:
        #
        # Expand input images in the (modified) eImages
        #
        A = np.empty(nComponent * nComponent).reshape((nComponent, nComponent))
        b = np.empty(nComponent)
        for v in visits:
            im = mosaics[v]
            if scale:
                im /= afwMath.makeStatistics(im, afwMath.MEANCLIP).getValue()

            for i in range(nComponent):
                b[i] = np.dot(eImages[i].getArray()[good], im.getArray()[good])

                for j in range(i, nComponent):
                    A[i, j] = np.dot(eImages[i].getArray()[good],
                                     eImages[j].getArray()[good])
                    A[j, i] = A[i, j]

            x = np.linalg.solve(A, b)
            #print v, A, b, x
            print "%d [%s] %s" % (v, ", ".join(["%9.2e" % _ for _ in x / x[0]
                                                ]), labels.get(v, ""))

            recon = eImages[0].clone()
            recon *= x[0]
            recona = recon.getArray()
            for i in range(1, nComponent):
                recona += x[i] * eImages[i].getArray()

            mtv = True
            if showOriginal or showRecon:
                if mtv:
                    ds9.mtv(im if showOriginal else recon,
                            frame=frame,
                            title=v)
                else:
                    ds9.erase(frame=frame)
                s0 = afwMath.makeStatistics(recon, afwMath.MEDIAN).getValue()
                s0 -= 0.5 * rng[0]
                ds9.ds9Cmd("scale linear; scale limits %g %g" %
                           (s0, s0 + rng[0]),
                           frame=frame)
                ds9.dot("%s %d" % ("orig" if showOriginal else "resid", v),
                        int(0.5 * im.getWidth()),
                        int(0.15 * im.getHeight()),
                        frame=frame,
                        ctype=ds9.RED)
                if labels.has_key(v):
                    ds9.dot(labels[v],
                            int(0.5 * im.getWidth()),
                            int(0.85 * im.getHeight()),
                            frame=frame,
                            ctype=ds9.RED)

                frame += 1

            if showResiduals:
                recon -= im
                if mtv:
                    ds9.mtv(recon, frame=frame)
                else:
                    ds9.erase(frame=frame)
                s0 = 0
                s0 -= 0.5 * rng[1]
                ds9.ds9Cmd("scale linear; scale limits %g %g" %
                           (s0, s0 + rng[1]),
                           frame=frame)

                frame += 1

    return eImages
Beispiel #4
0
def diffs(mosaics,
          visits,
          refVisit=None,
          scale=True,
          raw=None,
          rng=20,
          IRatioMax=1.0,
          frame0=0,
          verbose=False):
    """Display a series of differences and/or scaled differences
    (scale = True, False, (True, False), ...)
    """

    visits = list(visits)  # in case it's an iterator, and to get a copy

    if refVisit is None:
        refVisit = visits[0]

    ref = mosaics[refVisit]
    nameRef = str(refVisit)
    refMedian = float(np.median(ref.getArray()))

    visits = [v for v in visits if v != refVisit]

    try:
        scale[0]
    except TypeError:
        scale = (scale, )

    frame = frame0
    goodVisits = [refVisit]
    for v2 in visits:
        for s in scale:
            nameA, nameB = str(v2), nameRef
            diff = mosaics[v2].clone()

            IRatio = np.median(mosaics[v2].getArray()) / refMedian
            if IRatioMax and IRatio > IRatioMax and IRatio < 1 / IRatioMax:
                if verbose:
                    print "Skipping %d [ratio = %.2f]" % (v2, IRatio)
                break

            if s == scale[0]:
                goodVisits.append(v2)

            if s:
                diff /= IRatio
                nameA = "%s/%.2f" % (nameA, IRatio)

            diffa = diff.getArray()
            good = np.where(np.isfinite(diffa))
            v2Median = np.mean(diffa[good])

            if raw:
                title = nameA
            else:
                diff -= ref
                if IRatio < 1:
                    diff *= -1
                    nameA, nameB = nameB, nameA

                if verbose:
                    print "%s %5.1f%% %-s" % (v2, 100 * np.mean(diffa[good]) /
                                              v2Median, labels.get(v2, ""))

                title = "%s - %s" % (nameA, nameB)
                if not s:
                    title += " [rat=%.2f]" % (IRatio)

            if True:
                ds9.mtv(diff, title=title, frame=frame)
            else:
                ds9.erase(frame=frame)

            ds9.dot(title,
                    int(0.5 * diff.getWidth()),
                    int(0.05 * diff.getHeight()),
                    frame=frame,
                    ctype=ds9.RED)
            if labels.has_key(v2):
                ds9.dot(labels[v2],
                        int(0.5 * diff.getWidth()),
                        int(0.95 * diff.getHeight()),
                        frame=frame,
                        ctype=ds9.RED)

            s0 = 0 if (s and not raw) else afwMath.makeStatistics(
                diff, afwMath.MEDIAN).getValue()
            s0 -= 0.5 * rng
            ds9.ds9Cmd("scale linear; scale limits %g %g" % (s0, s0 + rng),
                       frame=frame)

            frame += 1

    return list(sorted(goodVisits))
Beispiel #5
0
def imagePca(mosaics, visits=None, nComponent=3, log=False, rng=30,
             showEigen=True, showResiduals=False, showOriginal=True, showRecon=False,
             normalizeEimages=True, scale=False, frame0=0, verbose=False):

    if showOriginal and showRecon:
        raise RuntimeError("You may not specify both showOriginal and showRecon")

    try:
        rng[0]
    except TypeError:
        rng = [rng, rng]

    if not visits:
        visits = sorted(mosaics.keys())

    mosaics = mosaics.copy()
    for v in visits:
        mosaics[v] = mosaics[v].clone()

    pca = afwImage.ImagePcaF()
    mask = None
    for v in visits:
        im = mosaics[v]
        if not mask:
            mask = im.Factory(im.getDimensions())
            maska = mask.getArray()
            X, Y = np.meshgrid(np.arange(mask.getWidth()), np.arange(mask.getHeight()))
            maska[np.where(np.hypot(X - 571, Y - 552) > 531)] = np.nan
            del maska

            mask[ 168: 184, 701:838] = np.nan
            mask[ 667: 733, 420:556] = np.nan
            mask[ 653: 677, 548:570] = np.nan
            mask[1031:1047, 274:414] = np.nan

            if False:
                mask[866:931, 697:828] = np.nan
                mask[267:334, 145:276] = np.nan

        im += mask

        pca.addImage(im, afwMath.makeStatistics(im, afwMath.SUM).getValue())
    pca.analyze()

    eValues = np.array(pca.getEigenValues())
    eValues /= eValues[0]
    eImages = pca.getEigenImages()
    #
    # Fiddle eigen images (we don't care about orthogonality)
    #
    if False:
        f10 =  0.1
        f20 = -0.3
        f30 =  0.55
        eImages[1].getArray()[:] += f10*eImages[0].getArray()
        eImages[2].getArray()[:] += f20*eImages[0].getArray()
        eImages[3].getArray()[:] += f30*eImages[0].getArray()

    if nComponent:
        eImages = eImages[:nComponent]
    else:
        nComponent = len(eImages)
    #
    # Normalize
    #
    good = np.where(np.isfinite(eImages[0].getArray()))
    if normalizeEimages:
        for eim in eImages:
            eima = eim.getArray()
            eima /= 1e-3*np.sqrt(np.sum(eima[good]**2))
    #
    # Plot/display eigen values/images
    #
    frame = frame0
    if showEigen:
        for i in range(len(eImages)):
            ds9.mtv(eImages[i], frame=frame, title="%d %.2g" % (i, eValues[i]))
            ds9.ds9Cmd("scale linear; scale mode zscale")
            frame += 1

        pyplot.clf()
        if log:
            eValues = np.log10(eValues)
        pyplot.plot(eValues)
        pyplot.plot(eValues, marker='o')
        pyplot.xlim(-0.5, len(eValues))
        pyplot.ylim(max(-5, pyplot.ylim()[0]), 0.05 if log else 1.05)
        pyplot.xlabel("n")
        pyplot.ylabel(r"$lg(\lambda)$" if log else r"$\lambda$")
        pyplot.show()

    if showOriginal or showRecon or showResiduals:
        #
        # Expand input images in the (modified) eImages
        #
        A = np.empty(nComponent*nComponent).reshape((nComponent, nComponent))
        b = np.empty(nComponent)
        for v in visits:
            im = mosaics[v]
            if scale:
                im /= afwMath.makeStatistics(im, afwMath.MEANCLIP).getValue()

            for i in range(nComponent):
                b[i] = np.dot(eImages[i].getArray()[good], im.getArray()[good])

                for j in range(i, nComponent):
                    A[i, j] = np.dot(eImages[i].getArray()[good], eImages[j].getArray()[good])
                    A[j, i] = A[i, j]

            x = np.linalg.solve(A, b)
            #print v, A, b, x
            print "%d [%s] %s" % (v, ", ".join(["%9.2e" % _ for _ in x/x[0]]), labels.get(v, ""))

            recon = eImages[0].clone(); recon *= x[0]
            recona = recon.getArray()
            for i in range(1, nComponent):
                recona += x[i]*eImages[i].getArray()

            mtv = True
            if showOriginal or showRecon:
                if mtv:
                    ds9.mtv(im if showOriginal else recon, frame=frame, title=v)
                else:
                    ds9.erase(frame=frame)
                s0 = afwMath.makeStatistics(recon, afwMath.MEDIAN).getValue()
                s0 -= 0.5*rng[0]
                ds9.ds9Cmd("scale linear; scale limits %g %g" % (s0, s0 + rng[0]), frame=frame)
                ds9.dot("%s %d" % ("orig" if showOriginal else "resid", v),
                        int(0.5*im.getWidth()), int(0.15*im.getHeight()), frame=frame, ctype=ds9.RED)
                if labels.has_key(v):
                    ds9.dot(labels[v], int(0.5*im.getWidth()), int(0.85*im.getHeight()),
                            frame=frame, ctype=ds9.RED)

                frame += 1

            if showResiduals:
                recon -= im
                if mtv:
                    ds9.mtv(recon, frame=frame)
                else:
                    ds9.erase(frame=frame)
                s0 = 0
                s0 -= 0.5*rng[1]
                ds9.ds9Cmd("scale linear; scale limits %g %g" % (s0, s0 + rng[1]), frame=frame)

                frame += 1

    return eImages
Beispiel #6
0
def diffs(mosaics, visits, refVisit=None, scale=True, raw=None,
          rng=20, IRatioMax=1.0, frame0=0, verbose=False):
    """Display a series of differences and/or scaled differences
    (scale = True, False, (True, False), ...)
    """

    visits = list(visits)               # in case it's an iterator, and to get a copy

    if refVisit is None:
        refVisit = visits[0]

    ref = mosaics[refVisit]
    nameRef = str(refVisit)
    refMedian = float(np.median(ref.getArray()))

    visits = [v for v in visits if v != refVisit]

    try:
        scale[0]
    except TypeError:
        scale = (scale,)

    frame = frame0
    goodVisits = [refVisit]
    for v2 in visits:
        for s in scale:
            nameA, nameB = str(v2), nameRef
            diff = mosaics[v2].clone()

            IRatio = np.median(mosaics[v2].getArray())/refMedian
            if IRatioMax and IRatio > IRatioMax and IRatio < 1/IRatioMax:
                if verbose:
                    print "Skipping %d [ratio = %.2f]" % (v2, IRatio)
                break

            if s == scale[0]:
                goodVisits.append(v2)

            if s:
                diff /= IRatio
                nameA = "%s/%.2f" % (nameA, IRatio)

            diffa = diff.getArray()
            good = np.where(np.isfinite(diffa))
            v2Median = np.mean(diffa[good])

            if raw:
                title = nameA
            else:
                diff -= ref
                if IRatio < 1:
                    diff *= -1
                    nameA, nameB = nameB, nameA

                if verbose:
                    print "%s %5.1f%% %-s" % (v2, 100*np.mean(diffa[good])/v2Median, labels.get(v2, ""))

                title = "%s - %s" % (nameA, nameB)
                if not s:
                    title += " [rat=%.2f]" % (IRatio)

            if True:
                ds9.mtv(diff, title=title, frame=frame)
            else:
                ds9.erase(frame=frame)

            ds9.dot(title, int(0.5*diff.getWidth()), int(0.05*diff.getHeight()),
                    frame=frame, ctype=ds9.RED)
            if labels.has_key(v2):
                ds9.dot(labels[v2], int(0.5*diff.getWidth()), int(0.95*diff.getHeight()),
                        frame=frame, ctype=ds9.RED)

            s0 = 0 if (s and not raw) else afwMath.makeStatistics(diff, afwMath.MEDIAN).getValue()
            s0 -= 0.5*rng
            ds9.ds9Cmd("scale linear; scale limits %g %g" % (s0, s0 + rng), frame=frame)

            frame += 1

    return list(sorted(goodVisits))
                                                      showBadCandidates=showBadCandidates)
                            maUtils.showPsfCandidates(exposure, psfCellSet, psf=psf, frame=5,
                                                      normalize=normalizeResiduals,
                                                      showBadCandidates=showBadCandidates,
                                                      variance=True)
                        except:
                            if not showBadCandidates:
                                showBadCandidates = True
                                continue
                        break

                if displayPsfComponents:
                    maUtils.showPsf(psf, eigenValues, frame=6)
                if displayPsfMosaic:
                    maUtils.showPsfMosaic(exposure, psf, frame=7, showFwhm=True)
                    ds9.ds9Cmd(ds9.selectFrame(frame=7) + " ;scale limits 0 1")
                if displayPsfSpatialModel:
                    maUtils.plotPsfSpatialModel(exposure, psf, psfCellSet, showBadCandidates=True,
                                                matchKernelAmplitudes=matchKernelAmplitudes,
                                                keepPlots=keepMatplotlibPlots)

                if pause:
                    while True:
                        try:
                            reply = raw_input("Next iteration? [ynchpqQs] ").strip()
                        except EOFError:
                            reply = "n"

                        reply = reply.split()
                        if reply:
                            reply, args = reply[0], reply[1:]
Beispiel #8
0
    import os
    import numpy as np
    import lsst.afw.display.ds9 as ds9

    try:
        ds9.initDS9(False)
    except ds9.Ds9Error:
        print 'DS9 launch bug error thrown away (probably)'
    
    from my_functions import OpenTimepixInDS9
#     OpenTimepixInDS9('/mnt/hgfs/VMShared/Data/new_sensors/oxford/march_2015/E404_50nm_newsample/420@133_1.txt')
#     OpenTimepixInDS9('/mnt/hgfs/VMShared/Data/new_sensors/suny_01042015/50nm_big_runs/422_big_only_electrons/1@1_1.txt')
#     OpenTimepixInDS9('/mnt/hgfs/VMShared/Data/new_sensors/suny_01042015/50nm_big_runs/422_big_only_ions/1@1_1.txt')
    OpenTimepixInDS9('/mnt/hgfs/VMShared/Data/new_sensors/bnl/24_03_2015/A2(300nm)/Run4/1_0001.txt')
 
    ds9.ds9Cmd('-scale limits 6400 6600')
    ds9.ds9Cmd('-scale limits 6400 6600')
    
    exit()
    
    
    expids,durations = np.loadtxt('/mnt/hgfs/VMShared/temp/input.txt', delimiter = '\t', skiprows = 1, usecols = (0,1), unpack = True)
    
    for i in range(len(expids)):
        print i, str(expids[i]), str(durations[i])
    
        
    dl = []
    files = os.listdir('/mnt/hgfs/VMShared/Data/des_darks/')
    for filename in files:
        dl.append(filename[8:14])
Beispiel #9
0
         footPrints = footPrintSet.getFootprints()
         
         footprint_pixels = 0
         
         ions_per_frame.append(len(footPrints))
         for footprint in footPrints:
             if DISPLAY and filenum == display_num: displayUtils.drawBBox(footprint.getBBox(), borderWidth=0.5) # border to fully encompass the bbox and no more
             npix = afwDetect.Footprint.getNpix(footprint)
             cluster_sizes.append(npix)
             footprint_pixels += npix
         
         footprint_pixels_per_frame_list.append(footprint_pixels)
 #         if filenum == display_num: exit()
         if DISPLAY and filenum == display_num:
             arg = 'saveimage jpeg ' + str(OUTPUT_PATH + str(val) + '_example_frame.jpeg') + ' 100'
             ds9.ds9Cmd(arg)
     
     
     histmax = 30
     name = 'cluster_size'
     h1 = ListToHist(cluster_sizes, OUTPUT_PATH + ID + '_cluster_size.png', log_z = False, nbins = histmax-1, histmin = 1, histmax = histmax, name = name)
     
     histmax = 20001#201
     name = 'pixels_per_frame'
     h2 = ListToHist(pixels_per_frame_list, OUTPUT_PATH + ID + '_pixels_per_frame.png', log_z = False, nbins = 100, histmin = 1, histmax = histmax, name = name)
     
     histmax = 20001#20001
     name = 'footprint_pixels_per_frame'
     h4 = ListToHist(footprint_pixels_per_frame_list, OUTPUT_PATH + ID + '_footprint_pixels_per_frame.png', log_z = False, nbins = 100, histmin = 1, histmax = histmax, name = name)
     name = 'footprint_pixels_per_frame_big_bins'
     h5 = ListToHist(footprint_pixels_per_frame_list, OUTPUT_PATH + ID + '_footprint_pixels_per_frame_big_bins.png', log_z = False, nbins = 50, histmin = 1, histmax = histmax, name = name)
        mapper  = Mapper(root=os.path.join(indir, "outputs8%s%s_doPreConvolveTrue" % (suffixT, suffixI)), calibRoot = None, outputRoot = None)
        butler  = dafPersist.ButlerFactory(mapper = mapper).create()
        dataId  = {"filter": filterName, "visit": visit, "raft": "2,2", "sensor": "1,1"}
        calexp  = butler.get(datasetType="calexp", dataId=dataId)
        rotTel  = calexp.getMetadata().get("SPIDANG")
        zenith  = calexp.getMetadata().get("ZENITH")
        diffim  = butler.get(datasetType="deepDiff_differenceExp", dataId=dataId)
        src     = butler.get(datasetType="src", dataId=dataId)
        matches = astrometer.useKnownWcs(src, exposure=calexp).matches

        if filterId == 1 and airmassIdI == 0:
            # Make sure ds9 has all the frames initialized
            for i in range(1, 10): ds9.mtv(calexp, frame=i)

        ds9.mtv(calexp, frame=frameId)
        ds9.ds9Cmd("mask transparency 80")
        if suffixI != "C":
            for match in matches:
                refObj = match.first
                gr     = -2.5 * np.log10(refObj["g"]) - -2.5 * np.log10(refObj["r"])
                amp    = getDcrAmp(zenith, filterName, gr)
                if amp == 0:
                    continue
                if gr < 0: color="blue"
                elif gr < 1: color="green"
                else: color="red"
                src    = match.second
                x1, y1 = src.getCentroid() # End of the Dcr vector
                dx     = amp * -np.sin(np.pi * -rotTel / 180.) # Minus since we are rotating clockwise from y-axis
                dy     = amp *  np.cos(np.pi * -rotTel / 180.) 
                x0     = x1 - dx
                        frameId = 1
                        dxns = np.array(())
                        dyns = np.array(())
                        dxps = np.array(())
                        dyps = np.array(())
                        for sx in range(3):
                            for sy in range(3):
                                sensor    = "%d,%d" % (sx, sy)
                                dataId    = {"visit": visit, "raft": "2,2", "sensor": sensor}
                                if not butler.datasetExists(datasetType="deepDiff_differenceExp", dataId=dataId):
                                    continue
                                src       = butler.get(datasetType="deepDiff_diaSrc", dataId = dataId)
                                if False:
                                    diffim    = butler.get(datasetType="deepDiff_differenceExp", dataId=dataId)
                                    ds9.mtv(diffim, frame=frameId); frameId += 1
                                    ds9.ds9Cmd("mask transparency 80")

                                #print "#", suffixT, suffixI, dataId
                                dxn, dyn, dxp, dyp = checkDipoles(src)
                                dxns = np.append(dxns, dxn)
                                dyns = np.append(dyns, dyn)
                                dxps = np.append(dxps, dxp)
                                dyps = np.append(dyps, dyp)

                        distn = np.sqrt(dxns**2 + dyns**2) * 0.2
                        angn  = np.arctan(dyns/dxns) * 180 / np.pi
                        distp = np.sqrt(dxps**2 + dyps**2) * 0.2
                        angp  = np.arctan(dyps/dxps) * 180 / np.pi
                        # OK, so for dy < 0, this is being measured w.r.t. x axis; in our coordinate system take abs and add 90.
                        angn  = 90 + np.abs(angn)
                        # And for dy > 0 its 270 + np.abs
                                showBadCandidates=showBadCandidates,
                                variance=True)
                        except:
                            if not showBadCandidates:
                                showBadCandidates = True
                                continue
                        break

                if displayPsfComponents:
                    maUtils.showPsf(psf, eigenValues, frame=6)
                if displayPsfMosaic:
                    maUtils.showPsfMosaic(exposure,
                                          psf,
                                          frame=7,
                                          showFwhm=True)
                    ds9.ds9Cmd(ds9.selectFrame(frame=7) + " ;scale limits 0 1")
                if displayPsfSpatialModel:
                    maUtils.plotPsfSpatialModel(
                        exposure,
                        psf,
                        psfCellSet,
                        showBadCandidates=True,
                        matchKernelAmplitudes=matchKernelAmplitudes,
                        keepPlots=keepMatplotlibPlots)

                if pause:
                    while True:
                        try:
                            reply = raw_input(
                                "Next iteration? [ynchpqQs] ").strip()
                        except EOFError: