Example #1
0
def get_anns(cal, nbright=0):
    wcsfn = cal.get_wcs_file()
    from astrometry.util.util import anwcs
    wcs = anwcs(wcsfn,0)
    catdir = settings.CAT_DIR
    uzcfn = os.path.join(catdir, 'uzc2000.fits')
    abellfn = os.path.join(catdir, 'abell-all.fits')
    ngcfn = os.path.join(catdir, 'ngc2000.fits')
    ngcnamesfn = os.path.join(catdir, 'ngc2000name.fits')
    icfn = os.path.join(catdir, 'ic2000.fits')
    brightfn = os.path.join(catdir, 'brightstars.fits')
    hdfn = settings.HENRY_DRAPER_CAT
    tycho2fn = settings.TYCHO2_KD
    
    import astrometry.blind.plotann as plotann
    opt = plotann.get_empty_opts()
    if nbright:
        opt.nbright = nbright
    rad = cal.get_radius()
    # These are the same limits used in views/image.py for annotations
    if rad < 1.:
        opt.abellcat = abellfn
        opt.hdcat = hdfn
    if rad < 0.25:
        opt.t2cat = tycho2fn
    if rad < 10:
        opt.ngc = True
        opt.ngccat = ngcfn
        opt.ngcname = ngcnamesfn
        opt.iccat = icfn
    opt.brightcat = brightfn
    
    jobjs = plotann.get_annotations_for_wcs(wcs, opt)
    return jobjs
Example #2
0
def skyplot():
    plot = Plotstuff(size=(800, 800), rdw=(103.1, 37.45, 0.8), outformat='png')
    plot.color = 'verydarkblue'
    plot.plot('fill')

    for ext in range(1, 17):
        fn = 'mos3.68488.fits'
        hdr = fitsio.read_header(fn, ext=ext)
        wcs = wcs_pv2sip_hdr(hdr)
        plot.color = 'red'
        plot.outline.wcs = anwcs_new_sip(wcs)
        plot.plot('outline')

        plot.color = 'white'
        plot.apply_settings()
        rc, dc = wcs.radec_center()
        plot.text_radec(rc, dc, hdr['EXTNAME'])

    plot.color = 'white'
    for ext in range(1, 17):
        fn = 'an2/mos3.68488.ext%02i.wcs' % ext
        plot.outline.wcs = anwcs(fn)
        plot.plot('outline')

    plot.rgb = (0.2, 0.2, 0.2)
    plot.plot_grid(0.1, 0.1)
    plot.color = 'gray'
    plot.plot_grid(0.5, 0.5, 0.5, 0.5)
    plot.write('plot.png')
Example #3
0
def get_anns(cal, nbright=0):
    wcsfn = cal.get_wcs_file()
    from astrometry.util.util import anwcs
    wcs = anwcs(wcsfn,0)
    catdir = settings.CAT_DIR
    uzcfn = os.path.join(catdir, 'uzc2000.fits')
    abellfn = os.path.join(catdir, 'abell-all.fits')
    ngcfn = os.path.join(catdir, 'ngc2000.fits')
    ngcnamesfn = os.path.join(catdir, 'ngc2000name.fits')
    icfn = os.path.join(catdir, 'ic2000.fits')
    brightfn = os.path.join(catdir, 'brightstars.fits')
    hdfn = settings.HENRY_DRAPER_CAT
    tycho2fn = settings.TYCHO2_KD
    
    import astrometry.blind.plotann as plotann
    opt = plotann.get_empty_opts()
    if nbright:
        opt.nbright = nbright
    rad = cal.get_radius()
    # These are the same limits used in views/image.py for annotations
    if rad < 1.:
        opt.abellcat = abellfn
        opt.hdcat = hdfn
    if rad < 0.25:
        opt.t2cat = tycho2fn
    if rad < 10:
        opt.ngc = True
        opt.ngccat = ngcfn
        opt.ngcname = ngcnamesfn
        opt.iccat = icfn
    opt.brightcat = brightfn
    
    jobjs = plotann.get_annotations_for_wcs(wcs, opt)
    return jobjs
Example #4
0
 def __init__(self, filename, hdu=0, wcs=None):
     self.x0 = 0.
     self.y0 = 0.
     if wcs is not None:
         self.wcs = wcs
     else:
         from astrometry.util.util import anwcs
         wcs = anwcs(filename, hdu)
         self.wcs = wcs
Example #5
0
 def __init__(self, filename, hdu=0, wcs=None):
     self.x0 = 0.
     self.y0 = 0.
     if wcs is not None:
         self.wcs = wcs
     else:
         from astrometry.util.util import anwcs
         wcs = anwcs(filename, hdu)
         self.wcs = wcs
Example #6
0
    import logging
    logformat = '%(message)s'
    from astrometry.util.util import log_init #, LOG_VERB, LOG_MSG
    loglvl = 2
    if opt.verbose:
        logging.basicConfig(level=logging.DEBUG, format=logformat)
        loglvl += 1
    else:
        logging.basicConfig(level=logging.INFO, format=logformat)
    log_init(loglvl)

    wcsfn = args[0]

    if dojson:
        from astrometry.util.util import anwcs
        wcs = anwcs(wcsfn,0)
        jobjs = get_annotations_for_wcs(wcs, opt)
        import json
        j = json.dumps(jobjs)
        print(j)
        sys.exit(0)

    fmt = PLOTSTUFF_FORMAT_JPG
    s = outfn.split('.')
    if len(s):
        s = s[-1].lower()
        if s in Plotstuff.format_map:
            fmt = s
    plot = Plotstuff(outformat=fmt, wcsfn=wcsfn)
    #plot.wcs_file = wcsfn
    #plot.outformat = fmt
Example #7
0
    from astrometry.util.util import log_init  # , LOG_VERB, LOG_MSG

    loglvl = 2
    if opt.verbose:
        logging.basicConfig(level=logging.DEBUG, format=logformat)
        loglvl += 1
    else:
        logging.basicConfig(level=logging.INFO, format=logformat)
    log_init(loglvl)

    wcsfn = args[0]

    if dojson:
        from astrometry.util.util import anwcs

        wcs = anwcs(wcsfn, 0)
        jobjs = get_annotations_for_wcs(wcs, opt)
        import simplejson

        json = simplejson.dumps(jobjs)
        print json
        sys.exit(0)

    fmt = PLOTSTUFF_FORMAT_JPG
    s = outfn.split(".")
    if len(s):
        s = s[-1].lower()
        if s in Plotstuff.format_map:
            fmt = s
    plot = Plotstuff(outformat=fmt, wcsfn=wcsfn)
    # plot.wcs_file = wcsfn
Example #8
0
 def __init__(self, filename, hdu=0):
     self.x0 = 0.
     self.y0 = 0.
     from astrometry.util.util import anwcs
     wcs = anwcs(filename, hdu)
     self.wcs = wcs
Example #9
0
def plot_unmatched():
    from bigboss_test import radecroi
    '''
    select
      run, rerun, camcol, field, nChild, probPSF,
      psfFlux_u, psfFlux_g, psfFlux_r, psfFlux_i, psfFlux_z,
      deVRad_u, deVRad_g, deVRad_r, deVRad_i, deVRad_z,
      deVAB_u, deVAB_g, deVAB_r, deVAB_i, deVAB_z,
      deVPhi_u, deVPhi_g, deVPhi_r, deVPhi_i, deVPhi_z,
      deVFlux_u, deVFlux_g, deVFlux_r, deVFlux_i, deVFlux_z,
      expRad_u, expRad_g, expRad_r, expRad_i, expRad_z,
      expAB_u, expAB_g, expAB_r, expAB_i, expAB_z,
      expPhi_u, expPhi_g, expPhi_r, expPhi_i, expPhi_z,
      expFlux_u, expFlux_g, expFlux_r, expFlux_i, expFlux_z,
      fracDeV_u, fracDeV_g, fracDeV_r, fracDeV_i, fracDeV_z,
      flags_u, flags_g, flags_r, flags_i, flags_z,
      probPSF_u, probPSF_g, probPSF_r, probPSF_i, probPSF_z,
      ra, dec
      from PhotoPrimary
      where ra between 333.5 and 335.5 and dec between -0.5 and 1.5
        '''

    ''' -> 124k rows.  (sdss-cas-testarea.fits)  Distinct runs:
    2585, 2728, 7712, 4203, 2583, 4192, 4207, 4184, 2662, 7717

    Run  #sources
    2583 663
    2585 675
    2662 4
    2728 156
    4184 762
    4192 36135
    4203 5
    4207 44078
    7712 12047
    7717 29911
    '''

    '''
    select
      run, rerun, camcol, field, nChild, probPSF,
      psfFlux_u, psfFlux_g, psfFlux_r, psfFlux_i, psfFlux_z,
      deVRad_u, deVRad_g, deVRad_r, deVRad_i, deVRad_z,
      deVAB_u, deVAB_g, deVAB_r, deVAB_i, deVAB_z,
      deVPhi_u, deVPhi_g, deVPhi_r, deVPhi_i, deVPhi_z,
      deVFlux_u, deVFlux_g, deVFlux_r, deVFlux_i, deVFlux_z,
      expRad_u, expRad_g, expRad_r, expRad_i, expRad_z,
      expAB_u, expAB_g, expAB_r, expAB_i, expAB_z,
      expPhi_u, expPhi_g, expPhi_r, expPhi_i, expPhi_z,
      expFlux_u, expFlux_g, expFlux_r, expFlux_i, expFlux_z,
      fracDeV_u, fracDeV_g, fracDeV_r, fracDeV_i, fracDeV_z,
      flags_u, flags_g, flags_r, flags_i, flags_z,
      probPSF_u, probPSF_g, probPSF_r, probPSF_i, probPSF_z,
      ra, dec, resolveStatus, score into mydb.wisetest from PhotoObjAll
      where ra between 333.5 and 335.5 and dec between -0.5 and 1.5
        and (resolveStatus & (
                dbo.fResolveStatus('SURVEY_PRIMARY') |
                dbo.fResolveStatus('SURVEY_BADFIELD') |
                dbo.fResolveStatus('SURVEY_EDGE'))) != 0

        --> sdss-cas-testarea-2.fits

    select
    run, rerun, camcol, field, nChild, probPSF,
    psfFlux_u, psfFlux_g, psfFlux_r, psfFlux_i, psfFlux_z,
    deVRad_u, deVRad_g, deVRad_r, deVRad_i, deVRad_z,
    deVAB_u, deVAB_g, deVAB_r, deVAB_i, deVAB_z,
    deVPhi_u, deVPhi_g, deVPhi_r, deVPhi_i, deVPhi_z,
    deVFlux_u, deVFlux_g, deVFlux_r, deVFlux_i, deVFlux_z,
    expRad_u, expRad_g, expRad_r, expRad_i, expRad_z,
    expAB_u, expAB_g, expAB_r, expAB_i, expAB_z,
    expPhi_u, expPhi_g, expPhi_r, expPhi_i, expPhi_z,
    expFlux_u, expFlux_g, expFlux_r, expFlux_i, expFlux_z,
    fracDeV_u, fracDeV_g, fracDeV_r, fracDeV_i, fracDeV_z,
    flags_u, flags_g, flags_r, flags_i, flags_z,
    probPSF_u, probPSF_g, probPSF_r, probPSF_i, probPSF_z,
    ra, dec, resolveStatus, score into mydb.wisetest from PhotoObjAll
    where ra between 333.5 and 335.5 and dec between -0.5 and 1.5
    and (resolveStatus & (
        dbo.fResolveStatus('SURVEY_PRIMARY') |
    dbo.fResolveStatus('SURVEY_BADFIELD') |
    dbo.fResolveStatus('SURVEY_EDGE') |
    dbo.fResolveStatus('SURVEY_BEST')
    )) != 0

    --> sdss-cas-testarea-3.fits
    '''

    ''' Check it out: spatial source density looks fine.  No overlap between runs.
    '''

    rng = ((333.5, 335.5), (-0.5, 1.5))
    from astrometry.util.plotutils import PlotSequence
    ps = PlotSequence('sdss')

    if False:
        r, d = np.mean(rng[0]), np.mean(rng[1])

        RCF = radec_to_sdss_rcf(r, d, radius=2. * 60.,
                                tablefn='window_flist-DR9.fits')
        print('Found', len(RCF), 'fields in range')
        for run, c, f, ra, dec in RCF:
            print('  ', run, c, f, 'at', ra, dec)

        from astrometry.blind.plotstuff import PlotSequence
        plot = Plotstuff(rdw=(r, d, 10), size=(1000, 1000), outformat='png')
        plot.color = 'white'
        plot.alpha = 0.5
        plot.apply_settings()
        T = fits_table('window_flist-DR9.fits')
        I, J, d = match_radec(T.ra, T.dec, r, d, 10)
        T.cut(I)
        print('Plotting', len(T))
        for i, (m0, m1, n0, n1, node, incl) in enumerate(zip(T.mu_start, T.mu_end, T.nu_start, T.nu_end, T.node, T.incl)):
            #rr,dd = [],[]
            for j, (m, n) in enumerate([(m0, n0), (m0, n1), (m1, n1), (m1, n0), (m0, n0)]):
                ri, di = munu_to_radec_deg(m, n, node, incl)
                # rr.append(ri)
                # dd.append(di)
                if j == 0:
                    plot.move_to_radec(ri, di)
                else:
                    plot.line_to_radec(ri, di)
            plot.stroke()
        plot.plot_grid(2, 2, 5, 5)
        plot.write('fields.png')

    # CAS PhotoObjAll.resolveStatus bits
    sprim = 0x100
    sbad = 0x800
    sedge = 0x1000
    sbest = 0x200

    if False:
        T = fits_table('sdss-cas-testarea.fits')
        plt.clf()
        plothist(T.ra, T.dec, 200, range=rng)
        plt.title('PhotoPrimary')
        ps.savefig()

        T = fits_table('sdss-cas-testarea-2.fits')
        plt.clf()
        plothist(T.ra, T.dec, 200, range=rng)
        plt.title('PhotoObjAll: SURVEY_PRIMARY | SURVEY_BADFIELD | SURVEY_EDGE')
        ps.savefig()

        T = fits_table('sdss-cas-testarea-3.fits')
        plt.clf()
        plothist(T.ra, T.dec, 200, range=rng)
        plt.title(
            'PhotoObjAll: SURVEY_PRIMARY | SURVEY_BADFIELD | SURVEY_EDGE | SURVEY_BEST')
        ps.savefig()

        for j, (flags, txt) in enumerate([(sprim, 'PRIM'), (sbad, 'BAD'), (sedge, 'EDGE'),
                                          (sbest, 'BEST')]):
            I = np.flatnonzero(
                (T.resolvestatus & (sprim | sbad | sedge | sbest)) == flags)
            print(len(I), 'with', txt)
            if len(I) == 0:
                continue
            plt.clf()
            plothist(T.ra[I], T.dec[I], 200, range=rng)
            plt.title('%i with %s' % (len(I), txt))
            ps.savefig()

        for j, (flags, txt) in enumerate([(sprim | sbad, 'PRIM + BAD'),
                                          (sprim | sedge, 'PRIM + EDGE'),
                                          (sprim | sbest, 'PRIM + BEST')]):
            I = np.flatnonzero((T.resolvestatus & flags) > 0)
            print(len(I), 'with', txt)
            if len(I) == 0:
                continue
            plt.clf()
            plothist(T.ra[I], T.dec[I], 200, range=rng)
            plt.title('%i with %s' % (len(I), txt))
            ps.savefig()

        # for run in np.unique(T.run):
        #   I = (T.run == run)
        #   plt.clf()
        #   plothist(T.ra[I], T.dec[I], 200, range=rng)
        #   plt.title('Run %i' % run)
        #   ps.savefig()

        R = 1. / 3600.
        I, J, d = match_radec(T.ra, T.dec, T.ra, T.dec, R, notself=True)
        print(len(I), 'matches')
        plt.clf()
        loghist((T.ra[I] - T.ra[J]) * 3600., (T.dec[I] - T.dec[J])
                * 3600., 200, range=((-1, 1), (-1, 1)))
        ps.savefig()

    ps = PlotSequence('forced')

    basedir = os.environ.get('BIGBOSS_DATA', '/project/projectdirs/bigboss')
    wisedatadir = os.path.join(basedir, 'data', 'wise')

    wisecat = fits_table(os.path.join(
        wisedatadir, 'catalogs', 'wisecat2.fits'))
    # plt.clf()
    #plothist(wisecat.ra, wisecat.dec, 200, range=rng)
    # plt.savefig('wisecat.png')

    (ra0, ra1, dec0, dec1) = radecroi
    ra = (ra0 + ra1) / 2.
    dec = (dec0 + dec1) / 2.

    #cas = fits_table('sdss-cas-testarea.fits')
    #cas = fits_table('sdss-cas-testarea-2.fits')
    cas = fits_table('sdss-cas-testarea-3.fits')
    print('Read', len(cas), 'CAS sources')

    cas.cut((cas.resolvestatus & sedge) == 0)
    print('Cut to ', len(cas), 'without SURVEY_EDGE set')

    # Check out WISE / SDSS matches.
    wise = wisecat
    sdss = cas
    print(len(sdss), 'SDSS sources')
    print(len(wise), 'WISE sources')
    R = 10.
    I, J, d = match_radec(wise.ra, wise.dec, sdss.ra, sdss.dec,
                          R / 3600., nearest=True)
    print(len(I), 'matches')

    print('max dist:', d.max())

    plt.clf()
    plt.hist(d * 3600., 100, range=(0, R), log=True)
    plt.xlabel('Match distance (arcsec)')
    plt.ylabel('Number of matches')
    plt.title('SDSS-WISE astrometric matches')
    ps.savefig()

    plt.clf()
    loghist((wise.ra[I] - sdss.ra[J]) * 3600., (wise.dec[I] - sdss.dec[J]) * 3600.,
            200, range=((-R, R), (-R, R)))
    plt.title('SDSS-WISE astrometric matches')
    plt.xlabel('dRA (arcsec)')
    plt.ylabel('dDec (arcsec)')
    ps.savefig()

    R = 4.

    I, J, d = match_radec(wise.ra, wise.dec, sdss.ra, sdss.dec,
                          R / 3600., nearest=True)
    print(len(I), 'matches')

    unmatched = np.ones(len(wise), bool)
    unmatched[I] = False
    wun = wise[unmatched]

    plt.clf()
    plothist(sdss.ra, sdss.dec, 200, range=rng)
    plt.title('SDSS source density')
    ps.savefig()

    plt.clf()
    plothist(wise.ra, wise.dec, 200, range=rng)
    plt.title('WISE source density')
    ps.savefig()

    plt.clf()
    #plt.plot(wun.ra, wun.dec, 'r.')
    #plt.axis(rng[0] + rng[1])
    plothist(wun.ra, wun.dec, 200, range=rng)
    plt.title('Unmatched WISE sources')
    ps.savefig()

    for band in 'ugriz':
        sdss.set('psfmag_' + band,
                 NanoMaggies.nanomaggiesToMag(sdss.get('psfflux_' + band)))

    # plt.clf()
    # loghist(wise.w1mpro[I], sdss.psfmag_r[J], 200)
    # plt.xlabel('WISE w1mpro')
    # plt.ylabel('SDSS psfflux_r')
    # ps.savefig()

    for band in 'riz':
        ax = [0, 10, 25, 5]
        plt.clf()
        mag = sdss.get('psfmag_' + band)[J]
        loghist(mag - wise.w1mpro[I], mag, 200,
                range=((ax[0], ax[1]), (ax[3], ax[2])))
        plt.xlabel('SDSS %s - WISE w1' % band)
        plt.ylabel('SDSS ' + band)
        plt.axis(ax)
        ps.savefig()

    for w, t in [(wise[I], 'Matched'), (wun, 'Unmatched')]:
        plt.clf()
        w1 = w.get('w1mpro')
        w2 = w.get('w2mpro')
        ax = [-1, 3, 18, 6]
        loghist(w1 - w2, w1, 200,
                range=((ax[0], ax[1]), (ax[3], ax[2])))
        plt.xlabel('W1 - W2')
        plt.ylabel('W1')
        plt.title('WISE CMD for %s sources' % t)
        plt.axis(ax)
        ps.savefig()

    sdssobj = DR9()
    band = 'r'
    RCF = np.unique(zip(sdss.run, sdss.camcol, sdss.field))
    wcses = []
    fns = []

    pfn = 'wcses.pickle'
    if os.path.exists(pfn):
        print('Reading', pfn)
        wcses, fns = unpickle_from_file(pfn)
    else:
        for r, c, f in RCF:
            fn = sdssobj.retrieve('frame', r, c, f, band)
            print('got', fn)
            fns.append(fn)
            wcs = Tan(fn, 0)
            print('got wcs', wcs)
            wcses.append(wcs)
        pickle_to_file((wcses, fns), pfn)
        print('Wrote to', pfn)

    wisefns = glob(os.path.join(wisedatadir, 'level3', '*w1-int-3.fits'))
    wisewcs = []
    for fn in wisefns:
        print('Reading', fn)
        wcs = anwcs(fn, 0)
        print('Got', wcs)
        wisewcs.append(wcs)

    I = np.argsort(wun.w1mpro)
    wun.cut(I)
    for i in range(len(wun)):
        ra, dec = wun.ra[i], wun.dec[i]
        insdss = -1
        for j, wcs in enumerate(wcses):
            if wcs.is_inside(ra, dec):
                insdss = j
                break
        inwise = -1
        for j, wcs in enumerate(wisewcs):
            if wcs.is_inside(ra, dec):
                inwise = j
                break
        N = 0
        if insdss != -1:
            N += 1
        if inwise != -1:
            N += 1
        if N == 0:
            continue

        if N != 2:
            continue

        plt.clf()
        ss = 1
        plt.subplot(2, N, ss)
        ss += 1
        M = 0.02
        I = np.flatnonzero((sdss.ra > (ra - M)) * (sdss.ra < (ra + M)) *
                           (sdss.dec > (dec - M)) * (sdss.dec < (dec + M)))
        sdssnear = sdss[I]
        plt.plot(sdss.ra[I], sdss.dec[I], 'b.', alpha=0.7)
        I = np.flatnonzero((wise.ra > (ra - M)) * (wise.ra < (ra + M)) *
                           (wise.dec > (dec - M)) * (wise.dec < (dec + M)))
        wisenear = wise[I]
        plt.plot(wise.ra[I], wise.dec[I], 'rx', alpha=0.7)
        if insdss:
            wcs = wcses[j]
            w, h = wcs.imagew, wcs.imageh
            rd = np.array([wcs.pixelxy2radec(x, y) for x, y in
                           [(1, 1), (w, 1), (w, h), (1, h), (1, 1)]])
            plt.plot(rd[:, 0], rd[:, 1], 'b-', alpha=0.5)
        if inwise:
            wcs = wisewcs[j]
            w, h = wcs.imagew, wcs.imageh
            rd = np.array([wcs.pixelxy2radec(x, y) for x, y in
                           [(1, 1), (w, 1), (w, h), (1, h), (1, 1)]])
            plt.plot(rd[:, 0], rd[:, 1], 'r-', alpha=0.5)
        plt.plot([ra], [dec], 'o', mec='k',
                 mfc='none', mew=3, ms=20, alpha=0.5)
        #plt.axis([ra+M, ra-M, dec-M, dec+M])
        plt.axis([ra - M, ra + M, dec - M, dec + M])
        plt.xticks([ra], ['RA = %0.3f' % ra])
        plt.yticks([dec], ['Dec = %0.3f' % dec])

        SW = 20

        ss = N + 1
        plt.subplot(2, N, ss)
        if insdss != -1:
            ss += 1
            j = insdss
            wcs = wcses[j]
            xc, yc = wcs.radec2pixelxy(ra, dec)
            r, c, f = RCF[j]
            frame = sdssobj.readFrame(r, c, f, band)
            #S = 50
            S = SW * 3.472
            im = frame.image
            H, W = im.shape
            y0, x0 = max(0, yc - S), max(0, xc - S)
            y1, x1 = min(H, yc + S), min(W, xc + S)
            subim = im[y0:y1, x0:x1]

            # plt.imshow(subim, interpolation='nearest', origin='lower',
            #          vmax=0.3, extent=[x0,x1,y0,y1])
            plt.imshow(subim.T, interpolation='nearest', origin='lower',
                       vmax=0.3, extent=[y0, y1, x0, x1])
            # vmax=subim.max()*1.01)
            ax = plt.axis()
            sx, sy = wcs.radec2pixelxy(sdssnear.ra, sdssnear.dec)
            #plt.plot(x, y, 'o', mec='b', mfc='none', ms=15)
            plt.plot(sy, sx, 'o', mec='b', mfc='none', ms=15)

            x, y = wcs.radec2pixelxy(wisenear.ra, wisenear.dec)
            #plt.plot(x, y, 'rx', ms=10)
            plt.plot(y, x, 'rx', ms=10)

            # Which way up?
            x, y = wcs.radec2pixelxy(np.array([ra, ra]), np.array(
                [0.5, 2.0]) * S * 0.396 / 3600. + dec)
            #plt.plot(x, y, 'b-', alpha=0.5, lw=2)
            plt.plot(y, x, 'b-', alpha=0.5, lw=2)
            plt.axis(ax)
            plt.gray()
            plt.title('SDSS %s (%i/%i/%i)' % (band, r, c, f))

            # Try to guess the PRIMARY run from the nearest object.
            for I in np.argsort((sx - xc)**2 + (sy - yc)**2):
                r, c, f = sdssnear.run[I], sdssnear.camcol[I], sdssnear.field[I]
                jj = None
                for j, (ri, ci, fi) in enumerate(RCF):
                    if ri == r and ci == c and fi == f:
                        jj = j
                        break
                assert(jj is not None)
                wcs = wcses[jj]
                xc, yc = wcs.radec2pixelxy(ra, dec)
                frame = sdssobj.readFrame(r, c, f, band)
                S = SW * 3.472
                im = frame.image
                H, W = im.shape
                y0, x0 = max(0, yc - S), max(0, xc - S)
                y1, x1 = min(H, yc + S), min(W, xc + S)
                subim = im[y0:y1, x0:x1]
                if np.prod(subim.shape) == 0:
                    continue
                print('subim shape', subim.shape)
                plt.subplot(2, N, 2)
                plt.imshow(subim.T, interpolation='nearest', origin='lower',
                           vmax=0.3, extent=[y0, y1, x0, x1])
                plt.gray()
                plt.title('SDSS %s (%i/%i/%i)' % (band, r, c, f))
                break

        if inwise != -1:
            plt.subplot(2, N, ss)
            ss += 1
            j = inwise
            wcs = wisewcs[j]
            ok, x, y = wcs.radec2pixelxy(ra, dec)
            im = pyfits.open(wisefns[j])[0].data
            S = SW
            H, W = im.shape
            y0, x0 = max(0, y - S), max(0, x - S)
            subim = im[y0: min(H, y + S), x0: min(W, x + S)]

            plt.imshow(subim, interpolation='nearest', origin='lower',
                       vmax=subim.max() * 1.01)
            ax = plt.axis()
            x, y = [], []
            for r, d in zip(wisenear.ra, wisenear.dec):
                ok, xi, yi = wcs.radec2pixelxy(r, d)
                x.append(xi)
                y.append(yi)
            x = np.array(x)
            y = np.array(y)
            plt.plot(x - x0, y - y0, 'rx', ms=15)

            x, y = [], []
            for r, d in zip(sdssnear.ra, sdssnear.dec):
                ok, xi, yi = wcs.radec2pixelxy(r, d)
                x.append(xi)
                y.append(yi)
            x = np.array(x)
            y = np.array(y)
            plt.plot(x - x0, y - y0, 'o', mec='b', mfc='none', ms=10)

            # Which way up?
            pixscale = 1.375 / 3600.
            ok, x1, y1 = wcs.radec2pixelxy(ra, dec + 0.5 * S * pixscale)
            ok, x2, y2 = wcs.radec2pixelxy(ra, dec + 2.0 * S * pixscale)
            plt.plot([x1 - x0, x2 - x0], [y1 - y0, y2 - y0],
                     'r-', alpha=0.5, lw=2)

            plt.axis([ax[1], ax[0], ax[2], ax[3]])
            # plt.axis(ax)
            plt.gray()
            plt.title('WISE W1 (coadd)')

        plt.suptitle('WISE unmatched source: w1=%.1f, RA,Dec = (%.3f, %.3f)' %
                     (wun.w1mpro[i], ra, dec))

        ps.savefig()

        rcfs = zip(sdssnear.run, sdssnear.camcol, sdssnear.field)
        print('Nearby SDSS sources are from:', np.unique(rcfs))

    return