コード例 #1
0
def sgr():
    """ Top-down plot of Sgr particles, with selected stars and then 
        re-observed
    """
    
    cached_data_file = os.path.join(plot_path, 'sgr_kde.pickle')
    
    fig,axes = plt.subplots(1, 2, figsize=(14,6.5), sharex=True, sharey=True)
    
    # read in all particles as a table
    pdata = particle_table(N=0, expr="(Pcol<7) & (abs(Lmflag)==1)")
    extent = {'x':(-90,55), 
              'y':(-58,68)}
    
    if not os.path.exists(cached_data_file):    
        Z = sgr_kde(pdata, extent=extent)
        
        with open(cached_data_file, 'w') as f:
            pickle.dump(Z, f)
    
    with open(cached_data_file, 'r') as f:
        Z = pickle.load(f)
    
    axes[1].imshow(Z**0.5, interpolation="nearest", 
              extent=extent['x']+extent['y'],
              cmap=cm.Blues, aspect=1)
    
    np.random.seed(42)
    particles = particles_today(N=100, expr="(Pcol<7) & (Pcol>0) & (abs(Lmflag)==1)")   
    
    # read in Stripe 82 RR Lyrae
    s82 = ascii.read(stripe82_catalog)
    stripe82_xyz = ra_dec_dist_to_xyz(s82['ra']*u.deg, s82['dec']*u.deg, 
                                      s82['dist']*u.kpc)
    
    catalina = ascii.read(os.path.join(project_root, "data/spitzer_sgr_sample.txt"))
    catalina = catalina[catalina['dist'] < 35.]
    catalina_xyz = ra_dec_dist_to_xyz(catalina['ra']*u.deg, 
                                      catalina['dec']*u.deg, 
                                      catalina['dist']*u.kpc)
    
    '''
    quest = ascii.read(quest_catalog)
    quest = quest[quest['dist'] < 35]
    quest = quest[quest['dist'] > 10]
    quest_xyz = ra_dec_dist_to_xyz(quest['ra']*u.deg, 
                                   quest['dec']*u.deg, 
                                   quest['dist']*u.kpc)
    '''
    
    rcparams = {'lines.linestyle' : 'none', 
                'lines.color' : 'k',
                'lines.marker' : 'o'}
    
    with rc_context(rc=rcparams): 
        axes[0].plot(pdata['x'], pdata['z'],
                     marker='.', alpha=0.1, ms=5)
        
        axes[1].plot(stripe82_xyz[:,0], stripe82_xyz[:,2], marker='.', 
                     color='#111111', alpha=0.85, markersize=12, 
                     label="Stripe 82", markeredgewidth=0)
        
        axes[1].plot(catalina_xyz[:,0], catalina_xyz[:,2], marker='^', 
                     color='#111111', alpha=0.85, markersize=7, 
                     label="Catalina", markeredgewidth=0)
        
        #axes[1].plot(quest_xyz[:,0], quest_xyz[:,2], marker='s', 
        #             color='#111111', alpha=0.85, markersize=6, 
        #             label="QUEST", markeredgewidth=0)
        
        # add solar symbol
        axes[0].text(-8., 0., s=r"$\odot$")
        axes[1].text(-8., 0., s=r"$\odot$")

        axes[0].set_xlabel("$X_{GC}$ [kpc]")
        axes[1].set_xlabel("$X_{GC}$ [kpc]")
        axes[0].set_ylabel("$Z_{GC}$ [kpc]")
    
    axes[1].legend(loc='upper left')
    plt.tight_layout()
    
    axes[0].set_xlim(extent['x'])
    axes[0].set_ylim(extent['y'])
    
    # turn off right, left ticks respectively
    axes[0].yaxis.tick_left()
    axes[1].yaxis.tick_right()
    
    # turn off top ticks
    axes[0].xaxis.tick_bottom()
    axes[1].xaxis.tick_bottom()
    
    fig.subplots_adjust(wspace=0.)
    fig.savefig(os.path.join(plot_path, "lm10.png"))
コード例 #2
0
ファイル: spitzer_targets.py プロジェクト: adrn/streams
def sgr(overwrite=False, seed=42):

    np.random.seed(seed)

    lm10_cache = os.path.join(project_root, "data", "spitzer_targets",
                              "lm10_cache.pickle")
    if os.path.exists(lm10_cache) and overwrite:
        os.remove(lm10_cache)

    if not os.path.exists(lm10_cache):
        # select particle data from the LM10 simulation
        lm10 = particle_table(N=0, expr="(Pcol>-1) & (Pcol<8) & "\
                                        "(abs(Lmflag)==1) & (dist<100)")
        fnpickle(np.array(lm10), lm10_cache)
    else:
        lm10 = Table(fnunpickle(lm10_cache))

    # read in the Catalina RR Lyrae data
    spatial_data = ascii.read(os.path.join(project_root,
                              "data/catalog/Catalina_all_RRLyr.txt"))
    velocity_data = ascii.read(os.path.join(project_root,
                               "data/catalog/Catalina_vgsr_RRLyr.txt"))
    catalina = join(spatial_data, velocity_data, join_type='outer', keys="ID")
    catalina.rename_column("RAdeg", "ra")
    catalina.rename_column("DEdeg", "dec")
    catalina.rename_column("dh", "dist")

    # add Sgr coordinates to the Catalina data
    catalina = add_sgr_coordinates(catalina)

    # add Galactocentric distance to the Catalina and LM10 data
    cat_gc_dist = tbl_to_gc_dist(catalina)
    lm10_gc_dist = tbl_to_gc_dist(lm10)
    catalina.add_column(Column(cat_gc_dist, name="gc_dist"))
    lm10.add_column(Column(lm10_gc_dist, name="gc_dist"))

    # 1) Select stars < 20 kpc from the orbital plane of Sgr
    sgr_catalina = catalina[np.fabs(catalina["Z_sgr"]) < 20.]
    x,y,z = tbl_to_xyz(sgr_catalina)

    # 2) Stars with D > 15 kpc from the Galactic center
    sgr_catalina = sgr_catalina[sgr_catalina["gc_dist"] > 15]
    sgr_catalina_rv = sgr_catalina[~sgr_catalina["Vgsr"].mask]
    print("{0} CSS RRLs have radial velocities.".format(len(sgr_catalina_rv)))

    # plot X-Z plane, data and lm10 particles
    fig,axes = plt.subplots(1,3,figsize=(15,6), sharex=True, sharey=True)
    x,y,z = tbl_to_xyz(catalina)
    axes[0].set_title("All RRL", fontsize=20)
    axes[0].plot(x, z, marker='.', alpha=0.2, linestyle='none')
    axes[0].set_xlabel("$X_{gc}$ kpc")
    axes[0].set_ylabel("$Z_{gc}$ kpc")

    x,y,z = tbl_to_xyz(sgr_catalina)
    axes[1].set_title(r"RRL $|Z-Z_{sgr}|$ $<$ $20$ kpc", fontsize=20)
    axes[1].plot(x, z, marker='.', alpha=0.2, linestyle='none')
    axes[2].plot(lm10["x"], lm10["z"], marker='.',
                 alpha=0.2, linestyle='none')
    axes[2].set_title("LM10", fontsize=20)
    axes[2].set_xlim(-60, 40)
    axes[2].set_ylim(-60, 60)

    fig.tight_layout()
    fig.savefig(os.path.join(notes_path, "catalina_all.pdf"))

    # plot Lambda-dist plane, data and lm10 particles
    fig,ax = plt.subplots(1,1,figsize=(6,6),
                          subplot_kw=dict(projection="polar"))

    ax.set_theta_direction(-1)
    ax.plot(np.radians(lm10["Lambda"]), lm10["dist"],
                       marker='.', alpha=0.2, linestyle='none')
    ax.plot(np.radians(sgr_catalina["Lambda"]), sgr_catalina["dist"],
                       marker='.', alpha=0.75, linestyle='none', ms=6)

    fig.tight_layout()
    fig.savefig(os.path.join(notes_path, "rrl_over_lm10.pdf"))

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # Select on radial velocities
    rv_selection_cache = os.path.join(project_root, "data",
                                      "spitzer_targets", "rv.pickle")
    if os.path.exists(rv_selection_cache) and overwrite:
        os.remove(rv_selection_cache)

    if not os.path.exists(rv_selection_cache):
        lmflag_rv_idx = sgr_rv(sgr_catalina_rv, lm10, Nbins=40, sigma_cut=3.)
        fnpickle(lmflag_rv_idx, rv_selection_cache)
    else:
        lmflag_rv_idx = fnunpickle(rv_selection_cache)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # Select on distance
    _selection_cache = os.path.join(project_root, "data",
                                    "spitzer_targets", "dist.pickle")
    if os.path.exists(_selection_cache) and overwrite:
        os.remove(_selection_cache)

    if not os.path.exists(_selection_cache):
        lmflag_dist_idx = sgr_dist(sgr_catalina_rv, lm10,
                                 Nbins=40, sigma_cut=3.)
        fnpickle(lmflag_dist_idx, _selection_cache)
    else:
        lmflag_dist_idx = fnunpickle(_selection_cache)

    ################################################################

    # Make X-Z plot
    fig,ax = plt.subplots(1,1,figsize=(6,6))

    x,y,z = tbl_to_xyz(lm10)
    ax.plot(x, z, marker=',', alpha=0.2,
                linestyle='none')

    for lmflag in [1,-1]:
        ix = lmflag_dist_idx[lmflag] & lmflag_rv_idx[lmflag]
        x,y,z = tbl_to_xyz(sgr_catalina_rv[ix])
        ax.plot(x, z, marker='.', alpha=0.75,
                linestyle='none', ms=6, label="Lmflag={0}".format(lmflag))

    ax.legend(loc='lower right',\
              prop={'size':12})
    ax.set_title("RV-selected CSS RRLs", fontsize=20)
    ax.set_xlabel(r"$X_{\rm gc}$ kpc")
    ax.set_ylabel(r"$Z_{\rm gc}$ kpc")
    fig.tight_layout()
    fig.savefig(os.path.join(notes_path, "selected_xz.pdf"))

    ##############################################################
    # Finalize two samples:
    #   - 1 with 10 stars in the nearby trailing wrap
    #   - 1 without these stars

    L = sgr_catalina_rv["Lambda"]
    B = sgr_catalina_rv["Beta"]
    D = sgr_catalina_rv["dist"]
    X,Y = D*np.cos(np.radians(L)),D*np.sin(np.radians(L))

    lead_ix = lmflag_dist_idx[1] & lmflag_rv_idx[1] & (np.fabs(B) < 40)
    trail_ix = lmflag_dist_idx[-1] & lmflag_rv_idx[-1] & (np.fabs(B) < 40)
    trail_ix[L < 180] &= B[L < 180] > -5
    trail_ix = trail_ix & ( ((L > 230) & (L < 315)) | (L < 180) )

    #trail_ix &= np.logical_not((L > 50) & (L < 100) & (sgr_catalina_rv["dist"] < 40))

    # draw a box around some possible bifurcation members
    bif_ix  = (L > 200) & (L < 230) & (B < 2) & (B > -10) & lead_ix

    # deselect stars possibly associated with the bifurcation
    no_bif = (L > 180) & (L < 225) & (B < 20) & (B > 2)
    no_bif |= (L > 225) & (L < 360) & (B < 17) & (B > 2)
    no_bif |= ((L <= 180) & (B < 15) & (B > -8) & (L > 50))
    lead_ix &= no_bif

    print(sum(bif_ix), "bifurcation stars")
    print(sum(lead_ix), "leading arm stars")
    print(sum(trail_ix), "trailing arm stars ")

    # select 3 clumps in the leading arm
    Nclump = 11
    ix_lead_clumps = np.zeros_like(lead_ix).astype(bool)
    for clump in [(215,17), (245,25), (260,40)]:
        l,d = clump
        x,y = d*np.cos(np.radians(l)),d*np.sin(np.radians(l))

        # find N stars closest to the clump
        clump_dist = np.sqrt((X-x)**2 + (Y-y)**2)
        xxx = np.sort(clump_dist[lead_ix])[Nclump]

        this_ix = lead_ix & (clump_dist <= xxx)
        print("lead",sum(this_ix))
        ix_lead_clumps |= this_ix #select_only(this_ix, 10)
    ix_lead_clumps &= lead_ix

    # all southern leading
    lll = ((L > 45) & (L < 180) & lead_ix)
    print("southern leading", sum(lll))
    ix_lead_clumps |= lll

    ix_trail_clumps = np.zeros_like(trail_ix).astype(bool)
    for clump in [(260,19)]:
        l,d = clump
        x,y = d*np.cos(np.radians(l)),d*np.sin(np.radians(l))

        # find N stars closest to the clump
        clump_dist = np.sqrt((X-x)**2 + (Y-y)**2)
        xxx = np.sort(clump_dist[trail_ix])[10]

        this_ix = trail_ix & (clump_dist <= xxx)
        #bonus_trail_ix = trail_ix & (clump_dist > xxx)
        ix_trail_clumps |= this_ix #select_only(this_ix, 10)
    ix_trail_clumps &= trail_ix

    # all trailing southern
    ttt = (L > 45) & (L < 180) & (trail_ix)
    print("southern trailing", sum(ttt))
    ix_trail_clumps |= ttt

    i1 = integration_time(sgr_catalina_rv[bif_ix]["dist"])
    i2 = integration_time(sgr_catalina_rv[ix_lead_clumps]["dist"])
    i3 = integration_time(sgr_catalina_rv[ix_trail_clumps]["dist"])

    print("final num bifurcation", len(sgr_catalina_rv[bif_ix]))
    print("final num leading arm", len(sgr_catalina_rv[ix_lead_clumps]))
    print("final num trailing arm",len(sgr_catalina_rv[ix_trail_clumps]))
    print("final total", sum(ix_trail_clumps) + sum(ix_lead_clumps) + sum(bif_ix))
    print("final total", sum(ix_trail_clumps | ix_lead_clumps | bif_ix))

    targets = sgr_catalina_rv[ix_trail_clumps | ix_lead_clumps | bif_ix]
    bonus_targets = sgr_catalina_rv[(lead_ix | trail_ix) & \
                            ~(ix_trail_clumps | ix_lead_clumps | bif_ix)]

    # print()
    # print("bifurcation", np.sum(i1))
    # print("leading",np.sum(i2))
    # print("trailing",np.sum(i3))
    # print("Total:",np.sum(integration_time(targets["dist"])))

    tot_time = 0.
    for t in targets:
        Vmag = t["<Vmag>"]

        if Vmag < 16.6:
            tot_time += 1.286
        elif 16.6 < Vmag < 16.8:
            tot_time += 1.31
        elif 16.8 < Vmag < 17.2:
            tot_time += 1.83
        elif 17.2 < Vmag < 17.8:
            tot_time += 2.52
        elif Vmag > 17.8:
            tot_time += 4.34

    print ("total time: ", tot_time)

    output_file = "sgr.txt"
    output = targets.copy()
    output.rename_column("Eta", "hjd0")
    output.rename_column("<Vmag>", "VMagAvg")
    output.keep_columns(["ID", "ra", "dec", "VMagAvg", "Period", "hjd0"])
    ascii.write(output,
                os.path.join(project_root, "data", "spitzer_targets", output_file), Writer=ascii.Basic)

    output_file = "sgr_bonus.txt"
    output = bonus_targets.copy()
    output.rename_column("Eta", "hjd0")
    output.rename_column("<Vmag>", "VMagAvg")
    output.keep_columns(["ID", "ra", "dec", "VMagAvg", "Period", "hjd0"])
    ascii.write(output,
                os.path.join(project_root, "data", "spitzer_targets", output_file), Writer=ascii.Basic)

    # ----------------------------------
    # Make Lambda-D plot
    fig,ax = plt.subplots(1,1,figsize=(6,6),
                          subplot_kw=dict(projection="polar"))
    ax.set_theta_direction(-1)

    ax.plot(np.radians(lm10["Lambda"]), lm10["dist"],
            marker=',', alpha=0.2, linestyle='none')

    d = sgr_catalina_rv[lead_ix]
    ax.plot(np.radians(d["Lambda"]), d["dist"], marker='.',
            alpha=0.75, linestyle='none', ms=8, c="#CA0020", label="leading")

    d = sgr_catalina_rv[trail_ix]
    ax.plot(np.radians(d["Lambda"]), d["dist"], marker='.',
            alpha=0.75, linestyle='none', ms=8, c="#5E3C99", label="trailing")

    ax.plot(np.radians(targets["Lambda"]), targets["dist"], marker='.',
            alpha=0.7, linestyle='none', ms=10, c="#31A354", label="targets",
            mfc='none', mec='k', mew=1.5)

    ax.legend(loc="lower right")
    plt.setp(ax.get_legend().get_texts(), fontsize='12')
    ax.set_ylim(0,65)
    fig.tight_layout()
    fig.savefig(os.path.join(notes_path, "xz.pdf"))

    # ---------------------
    # Make Lambda-Beta plot
    fig,ax = plt.subplots(1,1,figsize=(12,5))

    ax.plot(lm10["Lambda"], lm10["Beta"], marker=',', alpha=0.2,
                linestyle='none')

    dd_bif = sgr_catalina_rv[bif_ix]
    ax.plot(dd_bif["Lambda"], dd_bif["Beta"], marker='.', alpha=0.75,
            linestyle='none', ms=6, c="#31A354", label="bifurcation")

    d = sgr_catalina_rv[lead_ix]
    ax.plot(d["Lambda"], d["Beta"], marker='.',
           alpha=0.75, linestyle='none', ms=8, c="#CA0020", label="leading")

    d = sgr_catalina_rv[trail_ix]
    ax.plot(d["Lambda"], d["Beta"], marker='.',
            alpha=0.75, linestyle='none', ms=8, c="#5E3C99", label="trailing")

    ax.plot(targets["Lambda"], targets["Beta"], marker='.',
            alpha=0.7, linestyle='none', ms=10, c="#31A354", label="targets",
            mfc='none', mec='k', mew=1.5)

    ax.legend(loc="lower right")
    plt.setp(ax.get_legend().get_texts(), fontsize='12')
    ax.set_title("RV-selected CSS RRLs", fontsize=20)
    ax.set_xlabel(r"$\Lambda$ [deg]")
    ax.set_ylabel(r"$B$ [deg]")
    ax.set_xlim(360, 0)
    ax.set_ylim(-45, 45)
    fig.tight_layout()
    fig.savefig(os.path.join(notes_path, "LB.pdf"))
コード例 #3
0
ファイル: spitzer_targets.py プロジェクト: adrn/streams
def sgr(overwrite=False, seed=42):

    np.random.seed(seed)

    lm10_cache = os.path.join(project_root, "data", "spitzer_targets",
                              "lm10_cache.pickle")
    if os.path.exists(lm10_cache) and overwrite:
        os.remove(lm10_cache)

    if not os.path.exists(lm10_cache):
        # select particle data from the LM10 simulation
        lm10 = particle_table(N=0, expr="(Pcol>-1) & (Pcol<8) & "\
                                        "(abs(Lmflag)==1) & (dist<100)")
        fnpickle(np.array(lm10), lm10_cache)
    else:
        lm10 = Table(fnunpickle(lm10_cache))

    # read in the Catalina RR Lyrae data
    spatial_data = ascii.read(
        os.path.join(project_root, "data/catalog/Catalina_all_RRLyr.txt"))
    velocity_data = ascii.read(
        os.path.join(project_root, "data/catalog/Catalina_vgsr_RRLyr.txt"))
    catalina = join(spatial_data, velocity_data, join_type='outer', keys="ID")
    catalina.rename_column("RAdeg", "ra")
    catalina.rename_column("DEdeg", "dec")
    catalina.rename_column("dh", "dist")

    # add Sgr coordinates to the Catalina data
    catalina = add_sgr_coordinates(catalina)

    # add Galactocentric distance to the Catalina and LM10 data
    cat_gc_dist = tbl_to_gc_dist(catalina)
    lm10_gc_dist = tbl_to_gc_dist(lm10)
    catalina.add_column(Column(cat_gc_dist, name="gc_dist"))
    lm10.add_column(Column(lm10_gc_dist, name="gc_dist"))

    # 1) Select stars < 20 kpc from the orbital plane of Sgr
    sgr_catalina = catalina[np.fabs(catalina["Z_sgr"]) < 20.]
    x, y, z = tbl_to_xyz(sgr_catalina)

    # 2) Stars with D > 15 kpc from the Galactic center
    sgr_catalina = sgr_catalina[sgr_catalina["gc_dist"] > 15]
    sgr_catalina_rv = sgr_catalina[~sgr_catalina["Vgsr"].mask]
    print("{0} CSS RRLs have radial velocities.".format(len(sgr_catalina_rv)))

    # plot X-Z plane, data and lm10 particles
    fig, axes = plt.subplots(1, 3, figsize=(15, 6), sharex=True, sharey=True)
    x, y, z = tbl_to_xyz(catalina)
    axes[0].set_title("All RRL", fontsize=20)
    axes[0].plot(x, z, marker='.', alpha=0.2, linestyle='none')
    axes[0].set_xlabel("$X_{gc}$ kpc")
    axes[0].set_ylabel("$Z_{gc}$ kpc")

    x, y, z = tbl_to_xyz(sgr_catalina)
    axes[1].set_title(r"RRL $|Z-Z_{sgr}|$ $<$ $20$ kpc", fontsize=20)
    axes[1].plot(x, z, marker='.', alpha=0.2, linestyle='none')
    axes[2].plot(lm10["x"], lm10["z"], marker='.', alpha=0.2, linestyle='none')
    axes[2].set_title("LM10", fontsize=20)
    axes[2].set_xlim(-60, 40)
    axes[2].set_ylim(-60, 60)

    fig.tight_layout()
    fig.savefig(os.path.join(notes_path, "catalina_all.pdf"))

    # plot Lambda-dist plane, data and lm10 particles
    fig, ax = plt.subplots(1,
                           1,
                           figsize=(6, 6),
                           subplot_kw=dict(projection="polar"))

    ax.set_theta_direction(-1)
    ax.plot(np.radians(lm10["Lambda"]),
            lm10["dist"],
            marker='.',
            alpha=0.2,
            linestyle='none')
    ax.plot(np.radians(sgr_catalina["Lambda"]),
            sgr_catalina["dist"],
            marker='.',
            alpha=0.75,
            linestyle='none',
            ms=6)

    fig.tight_layout()
    fig.savefig(os.path.join(notes_path, "rrl_over_lm10.pdf"))

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # Select on radial velocities
    rv_selection_cache = os.path.join(project_root, "data", "spitzer_targets",
                                      "rv.pickle")
    if os.path.exists(rv_selection_cache) and overwrite:
        os.remove(rv_selection_cache)

    if not os.path.exists(rv_selection_cache):
        lmflag_rv_idx = sgr_rv(sgr_catalina_rv, lm10, Nbins=40, sigma_cut=3.)
        fnpickle(lmflag_rv_idx, rv_selection_cache)
    else:
        lmflag_rv_idx = fnunpickle(rv_selection_cache)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # Select on distance
    _selection_cache = os.path.join(project_root, "data", "spitzer_targets",
                                    "dist.pickle")
    if os.path.exists(_selection_cache) and overwrite:
        os.remove(_selection_cache)

    if not os.path.exists(_selection_cache):
        lmflag_dist_idx = sgr_dist(sgr_catalina_rv,
                                   lm10,
                                   Nbins=40,
                                   sigma_cut=3.)
        fnpickle(lmflag_dist_idx, _selection_cache)
    else:
        lmflag_dist_idx = fnunpickle(_selection_cache)

    ################################################################

    # Make X-Z plot
    fig, ax = plt.subplots(1, 1, figsize=(6, 6))

    x, y, z = tbl_to_xyz(lm10)
    ax.plot(x, z, marker=',', alpha=0.2, linestyle='none')

    for lmflag in [1, -1]:
        ix = lmflag_dist_idx[lmflag] & lmflag_rv_idx[lmflag]
        x, y, z = tbl_to_xyz(sgr_catalina_rv[ix])
        ax.plot(x,
                z,
                marker='.',
                alpha=0.75,
                linestyle='none',
                ms=6,
                label="Lmflag={0}".format(lmflag))

    ax.legend(loc='lower right',\
              prop={'size':12})
    ax.set_title("RV-selected CSS RRLs", fontsize=20)
    ax.set_xlabel(r"$X_{\rm gc}$ kpc")
    ax.set_ylabel(r"$Z_{\rm gc}$ kpc")
    fig.tight_layout()
    fig.savefig(os.path.join(notes_path, "selected_xz.pdf"))

    ##############################################################
    # Finalize two samples:
    #   - 1 with 10 stars in the nearby trailing wrap
    #   - 1 without these stars

    L = sgr_catalina_rv["Lambda"]
    B = sgr_catalina_rv["Beta"]
    D = sgr_catalina_rv["dist"]
    X, Y = D * np.cos(np.radians(L)), D * np.sin(np.radians(L))

    lead_ix = lmflag_dist_idx[1] & lmflag_rv_idx[1] & (np.fabs(B) < 40)
    trail_ix = lmflag_dist_idx[-1] & lmflag_rv_idx[-1] & (np.fabs(B) < 40)
    trail_ix[L < 180] &= B[L < 180] > -5
    trail_ix = trail_ix & (((L > 230) & (L < 315)) | (L < 180))

    #trail_ix &= np.logical_not((L > 50) & (L < 100) & (sgr_catalina_rv["dist"] < 40))

    # draw a box around some possible bifurcation members
    bif_ix = (L > 200) & (L < 230) & (B < 2) & (B > -10) & lead_ix

    # deselect stars possibly associated with the bifurcation
    no_bif = (L > 180) & (L < 225) & (B < 20) & (B > 2)
    no_bif |= (L > 225) & (L < 360) & (B < 17) & (B > 2)
    no_bif |= ((L <= 180) & (B < 15) & (B > -8) & (L > 50))
    lead_ix &= no_bif

    print(sum(bif_ix), "bifurcation stars")
    print(sum(lead_ix), "leading arm stars")
    print(sum(trail_ix), "trailing arm stars ")

    # select 3 clumps in the leading arm
    Nclump = 11
    ix_lead_clumps = np.zeros_like(lead_ix).astype(bool)
    for clump in [(215, 17), (245, 25), (260, 40)]:
        l, d = clump
        x, y = d * np.cos(np.radians(l)), d * np.sin(np.radians(l))

        # find N stars closest to the clump
        clump_dist = np.sqrt((X - x)**2 + (Y - y)**2)
        xxx = np.sort(clump_dist[lead_ix])[Nclump]

        this_ix = lead_ix & (clump_dist <= xxx)
        print("lead", sum(this_ix))
        ix_lead_clumps |= this_ix  #select_only(this_ix, 10)
    ix_lead_clumps &= lead_ix

    # all southern leading
    lll = ((L > 45) & (L < 180) & lead_ix)
    print("southern leading", sum(lll))
    ix_lead_clumps |= lll

    ix_trail_clumps = np.zeros_like(trail_ix).astype(bool)
    for clump in [(260, 19)]:
        l, d = clump
        x, y = d * np.cos(np.radians(l)), d * np.sin(np.radians(l))

        # find N stars closest to the clump
        clump_dist = np.sqrt((X - x)**2 + (Y - y)**2)
        xxx = np.sort(clump_dist[trail_ix])[10]

        this_ix = trail_ix & (clump_dist <= xxx)
        #bonus_trail_ix = trail_ix & (clump_dist > xxx)
        ix_trail_clumps |= this_ix  #select_only(this_ix, 10)
    ix_trail_clumps &= trail_ix

    # all trailing southern
    ttt = (L > 45) & (L < 180) & (trail_ix)
    print("southern trailing", sum(ttt))
    ix_trail_clumps |= ttt

    i1 = integration_time(sgr_catalina_rv[bif_ix]["dist"])
    i2 = integration_time(sgr_catalina_rv[ix_lead_clumps]["dist"])
    i3 = integration_time(sgr_catalina_rv[ix_trail_clumps]["dist"])

    print("final num bifurcation", len(sgr_catalina_rv[bif_ix]))
    print("final num leading arm", len(sgr_catalina_rv[ix_lead_clumps]))
    print("final num trailing arm", len(sgr_catalina_rv[ix_trail_clumps]))
    print("final total",
          sum(ix_trail_clumps) + sum(ix_lead_clumps) + sum(bif_ix))
    print("final total", sum(ix_trail_clumps | ix_lead_clumps | bif_ix))

    targets = sgr_catalina_rv[ix_trail_clumps | ix_lead_clumps | bif_ix]
    bonus_targets = sgr_catalina_rv[(lead_ix | trail_ix) & \
                            ~(ix_trail_clumps | ix_lead_clumps | bif_ix)]

    # print()
    # print("bifurcation", np.sum(i1))
    # print("leading",np.sum(i2))
    # print("trailing",np.sum(i3))
    # print("Total:",np.sum(integration_time(targets["dist"])))

    tot_time = 0.
    for t in targets:
        Vmag = t["<Vmag>"]

        if Vmag < 16.6:
            tot_time += 1.286
        elif 16.6 < Vmag < 16.8:
            tot_time += 1.31
        elif 16.8 < Vmag < 17.2:
            tot_time += 1.83
        elif 17.2 < Vmag < 17.8:
            tot_time += 2.52
        elif Vmag > 17.8:
            tot_time += 4.34

    print("total time: ", tot_time)

    output_file = "sgr.txt"
    output = targets.copy()
    output.rename_column("Eta", "hjd0")
    output.rename_column("<Vmag>", "VMagAvg")
    output.keep_columns(["ID", "ra", "dec", "VMagAvg", "Period", "hjd0"])
    ascii.write(output,
                os.path.join(project_root, "data", "spitzer_targets",
                             output_file),
                Writer=ascii.Basic)

    output_file = "sgr_bonus.txt"
    output = bonus_targets.copy()
    output.rename_column("Eta", "hjd0")
    output.rename_column("<Vmag>", "VMagAvg")
    output.keep_columns(["ID", "ra", "dec", "VMagAvg", "Period", "hjd0"])
    ascii.write(output,
                os.path.join(project_root, "data", "spitzer_targets",
                             output_file),
                Writer=ascii.Basic)

    # ----------------------------------
    # Make Lambda-D plot
    fig, ax = plt.subplots(1,
                           1,
                           figsize=(6, 6),
                           subplot_kw=dict(projection="polar"))
    ax.set_theta_direction(-1)

    ax.plot(np.radians(lm10["Lambda"]),
            lm10["dist"],
            marker=',',
            alpha=0.2,
            linestyle='none')

    d = sgr_catalina_rv[lead_ix]
    ax.plot(np.radians(d["Lambda"]),
            d["dist"],
            marker='.',
            alpha=0.75,
            linestyle='none',
            ms=8,
            c="#CA0020",
            label="leading")

    d = sgr_catalina_rv[trail_ix]
    ax.plot(np.radians(d["Lambda"]),
            d["dist"],
            marker='.',
            alpha=0.75,
            linestyle='none',
            ms=8,
            c="#5E3C99",
            label="trailing")

    ax.plot(np.radians(targets["Lambda"]),
            targets["dist"],
            marker='.',
            alpha=0.7,
            linestyle='none',
            ms=10,
            c="#31A354",
            label="targets",
            mfc='none',
            mec='k',
            mew=1.5)

    ax.legend(loc="lower right")
    plt.setp(ax.get_legend().get_texts(), fontsize='12')
    ax.set_ylim(0, 65)
    fig.tight_layout()
    fig.savefig(os.path.join(notes_path, "xz.pdf"))

    # ---------------------
    # Make Lambda-Beta plot
    fig, ax = plt.subplots(1, 1, figsize=(12, 5))

    ax.plot(lm10["Lambda"],
            lm10["Beta"],
            marker=',',
            alpha=0.2,
            linestyle='none')

    dd_bif = sgr_catalina_rv[bif_ix]
    ax.plot(dd_bif["Lambda"],
            dd_bif["Beta"],
            marker='.',
            alpha=0.75,
            linestyle='none',
            ms=6,
            c="#31A354",
            label="bifurcation")

    d = sgr_catalina_rv[lead_ix]
    ax.plot(d["Lambda"],
            d["Beta"],
            marker='.',
            alpha=0.75,
            linestyle='none',
            ms=8,
            c="#CA0020",
            label="leading")

    d = sgr_catalina_rv[trail_ix]
    ax.plot(d["Lambda"],
            d["Beta"],
            marker='.',
            alpha=0.75,
            linestyle='none',
            ms=8,
            c="#5E3C99",
            label="trailing")

    ax.plot(targets["Lambda"],
            targets["Beta"],
            marker='.',
            alpha=0.7,
            linestyle='none',
            ms=10,
            c="#31A354",
            label="targets",
            mfc='none',
            mec='k',
            mew=1.5)

    ax.legend(loc="lower right")
    plt.setp(ax.get_legend().get_texts(), fontsize='12')
    ax.set_title("RV-selected CSS RRLs", fontsize=20)
    ax.set_xlabel(r"$\Lambda$ [deg]")
    ax.set_ylabel(r"$B$ [deg]")
    ax.set_xlim(360, 0)
    ax.set_ylim(-45, 45)
    fig.tight_layout()
    fig.savefig(os.path.join(notes_path, "LB.pdf"))