def match_xmm_to_ukirt():
    """ 
    A function that performs an "inverse" cross-match between 
    X-ray selected sources from XMM, and our UKIRT stars.

    """

    # Produces a table of cross-match IDs and indices.
    mated_xmm = tablemater(XMM_north, ukirt_list)
    
    mated_c1 =  tablemater(XMM_north_c1, ukirt_list)
    mated_c2 =  tablemater(XMM_north_c2, ukirt_list)
    mated_c3 =  tablemater(XMM_north_c3, ukirt_list)

    mated_list = [mated_xmm, mated_c1, mated_c2, mated_c3]

    # What kinds of plots do we want?
    # Histogram of S, for each xray category (incl. "all")
    fig = plt.figure()

    uks_i = 'UKIRT_autocan_strict_allstars_index'
    uks_d = autocan_strict

    uka_i = 'UKIRT_autocan_true_allstars_index'
    uka_d = autocan_true

    s1 = plt.subplot(4,1,1)
    s2 = plt.subplot(4,1,2)
    s3 = plt.subplot(4,1,3)
    s4 = plt.subplot(4,1,4)

    subplot_list = [s1, s2, s3, s4]
    name_list = ["all XMM sources",
                 "Class 1 XMM sources",
                 "Class 2 XMM sources",
                 "Class 3 XMM sources"]

    for s, m, name in zip(subplot_list, mated_list, name_list):
        # in approximate english: "the stats table, where you take the row 
        # handed to you by the mated table, but only where there's a match"
        try:
            # AUTO
            s.hist( 
                uka_d.Stetson[ 
                    m.where(m[uka_i] != -1)[uka_i] 
                    ], 
                range=[0,5], bins=20, color='0.7', label="1-band pristine"
                )
        except: pass
        try:
            # STRICT
            s.hist( 
                uks_d.Stetson[ 
                    m.where(m[uks_i] != -1)[uks_i] 
                    ], 
                range=[0,5], bins=20, color='0.2', label="3-band pristine"
                )
        except: pass

        # annotate each subplot so they're readable
        if s == s1:
            s.text(0.35, 0.75, name, transform=s.transAxes)
        else:
            s.text(0.65, 0.75, name, transform=s.transAxes)
        

    s1.set_title("Histogram: Stetson indices of X-ray-selected stars in ONC")
    s1.legend()
    s4.set_xlabel("Stetson Index")
    plt.show()


    # Now, I want to print out some stats relevant to what we want.
    # Think of how "official_star_counter" works.

    # For each group, print the stuff.

    for s, m, name in zip(subplot_list, mated_list, name_list):
        # Median S and MAD?
        auto_stetson = uka_d.Stetson[m.where(m[uka_i] != -1)[uka_i]]
        strict_stetson = uks_d.Stetson[m.where(m[uks_i] != -1)[uks_i]]

        print ("%s Median Stetson for Q=1+2: %.3f +- %.2f" % 
               (name, np.median(auto_stetson), rb.mad(auto_stetson)))
        print "N = %d" % len(auto_stetson)        
        print ("%s Median Stetson for Q=2: %.3f +- %.2f" % 
               (name, np.median(strict_stetson), rb.mad(strict_stetson)))
        print "N = %d" % len(strict_stetson)        
        
    for s, m, name in zip(subplot_list, mated_list, name_list):
        # Median Delta Mag (and MAD)?
#        auto_delta = uka_d.[m.where(m[uka_i] != -1)[uka_i]]# doesn't quite work
        strict_delta = uks_d.k_range[m.where(m[uks_i] != -1)[uks_i]]

        print ("%s Median delta-K (max-min) for strict-stars: %.3f +- %.2f" % 
               (name, np.median(strict_delta), rb.mad(strict_delta)))
        print "N = %d" % len(strict_delta)


        # What fraction have good periods? Not answerable right now.

    return mated_xmm, mated_c1, mated_c2, mated_c3
def f_magnitude_hists_by_class(threepanels=True, onepanels=False):
    """
    Makes a series of multipanel histograms of variability.
    Uses "strict" sources only for these.
    
    """

    megeath_class_column = make_megeath_class_column()
    
    strict_protostars = ukvar_spread.where(
        (ukvar_spread.strict == 1) & (megeath_class_column == 'P'))

    strict_disks = ukvar_spread.where(
        (ukvar_spread.strict == 1) & (megeath_class_column == 'D'))

    strict_nondisks = ukvar_spread.where(
        (ukvar_spread.strict == 1) & (megeath_class_column == 'ND'))

    print "Protostars: %d, Disks: %d, Nondisks: %d" % (
        len(strict_protostars), len(strict_disks), len(strict_nondisks) )

    # Let's test the J mag aspect of this, and then define some dicts or forloops to iterate through all "5" bands.

    names = ['J mag', 'H mag', 'K mag', '(J-H) color', '(H-K) color']
    bands = ['j', 'h', 'k', 'jmh', 'hmk']
    text_xposition = [0.375, 0.375, 0.375, 0.275, 0.275]

    figs = []

    hist_kwargs = {'range':(0,2), 'bins':20}

    if threepanels:
        for b, n, x in zip(bands, names, text_xposition):

            j_fig = plt.figure(figsize=(5,6))
            figs.append(j_fig)

            jsub1 = plt.subplot(3,1,1)
            jsub1.hist(strict_protostars['%s_ranger' % b], color=color_dict['protostar'], 
                       **hist_kwargs)
            jsub1.text(x, 0.65, "protostars \n"
                       r"median $\Delta %s: $%.2f \pm %.2f$" % (
                    n.replace(' ', '$ '), 
                    np.median(strict_protostars['%s_ranger' % b]),
                    rb.mad(strict_protostars['%s_ranger' % b])),
                       transform = jsub1.transAxes)

            jsub2 = plt.subplot(3,1,2, sharex=jsub1)
            jsub2.hist(strict_disks['%s_ranger' % b], color=color_dict['disk'], **hist_kwargs)
            jsub2.text(x, 0.65, "disks \n"
                       r"median $\Delta %s: $%.2f \pm %.2f$" % (
                    n.replace(' ', '$ '), 
                    np.median(strict_disks['%s_ranger' % b]),
                    rb.mad(strict_disks['%s_ranger' % b])),
                       transform = jsub2.transAxes)

            jsub3 = plt.subplot(3,1,3, sharex=jsub1)
            jsub3.hist(strict_nondisks['%s_ranger' % b], color=color_dict['nondisk'], 
                       **hist_kwargs)
            jsub3.text(x, 0.65, "non-disks \n"
                       r"median $\Delta %s: $%.2f \pm %.2f$" % (
                    n.replace(' ', '$ '), 
                    np.median(strict_nondisks['%s_ranger' % b]),
                    rb.mad(strict_nondisks['%s_ranger' % b])),
                       transform = jsub3.transAxes)

            jsub1.set_title("%s range for $Q=2$ variables"%n)
            jsub3.set_xlabel(r"$\Delta %s (outlier-proof)" % 
                             n.replace(' ', '$ '))


    if onepanels:

        fig = plt.figure()
        figs.append(fig)
        
        plt.hist(strict_nondisks['k_ranger'], 
                 color=color_dict['nondisk'], hatch='/', label='Megeath Non-disks',
                 **hist_kwargs)
        plt.hist(strict_disks['k_ranger'], 
                 color=color_dict['disk'], alpha=0.5, hatch='\\', label='Megeath Disks',
                 **hist_kwargs)
        plt.hist(strict_protostars['k_ranger'], 
                 color=color_dict['protostar'], hatch='--', label='Megeath Protostars',
                 **hist_kwargs)

        plt.title("K magnitude range (robust) for pristine-data variables")
        plt.xlabel(r"$\Delta K$ magnitude (outlier-proof)")

        plt.legend()

            
    plt.show()
    return figs
def match_spitzer_to_ukirt():
    """ 
    A function that performs an "inverse" cross-match between 
    mid-IR selected sources from Spitzer/Megeath2012, and our UKIRT stars.

    """

    # Produces a table of cross-match IDs and indices.
    mated_spitzer = tablemater(Megeath2012, ukirt_list)
    
    # Clearly, we need to update this to use slices of the Megeath table
    # filtered on Class.
    mated_P =  tablemater(Megeath_P, ukirt_list)
    mated_D =  tablemater(Megeath_D, ukirt_list)
    mated_ND = tablemater(Megeath_ND, ukirt_list)

    mated_list = [mated_P, mated_D, mated_ND]

    # What kinds of plots do we want?
    # Histogram of S, for each IR category
    fig = plt.figure()

    uks_i = 'UKIRT_autocan_strict_allstars_index'
    uks_d = autocan_strict

    uka_i = 'UKIRT_autocan_true_allstars_index'
    uka_d = autocan_true

    s1 = plt.subplot(3,1,1)
    s2 = plt.subplot(3,1,2)
    s3 = plt.subplot(3,1,3)

    subplot_list = [s1, s2, s3]
    name_list = ["Spitzer Protostars",
                 "Spitzer Disked sources",
                 "Spitzer Non-Disked sources"]

    for s, m, name in zip(subplot_list, mated_list, name_list):
        # in approximate english: "the stats table, where you take the row 
        # handed to you by the mated table, but only where there's a match"
        try:
            # AUTO
            s.hist( 
                uka_d.Stetson[ 
                    m.where(m[uka_i] != -1)[uka_i] 
                    ], 
                range=[0,5], bins=20, color='0.7', label="Q=1"
                )
        except: pass
        array_in_question = uka_d.Stetson[ 
                    m.where(m[uka_i] != -1)[uka_i] 
                    ]
        print len(array_in_question[array_in_question>5])
        try:
            # STRICT
            s.hist( 
                uks_d.Stetson[ 
                    m.where(m[uks_i] != -1)[uks_i] 
                    ], 
                range=[0,5], bins=20, color='0.2', label="Q=2"
                )
        except: pass

        # Calculate medians.
        auto_stetson = uka_d.Stetson[m.where(m[uka_i] != -1)[uka_i]]
        strict_stetson = uks_d.Stetson[m.where(m[uks_i] != -1)[uks_i]]

        print ("%s Median Stetson for Q=1+2: %.3f +- %.2f" % 
               (name, np.median(auto_stetson), rb.mad(auto_stetson)))
        print "N = %d" % len(auto_stetson)
        print ("%s Median Stetson for Q=2: %.3f +- %.2f" % 
               (name, np.median(strict_stetson), rb.mad(strict_stetson)))
        print "N = %d" % len(strict_stetson)
        
        # annotate each subplot so they're readable
        if s == s1:
            xtext = 0.65
        else:
            xtext = 0.5
            
        s.text(xtext, 0.75,
               "%s\nMedian S: %.2f$\pm$%.2f" % (name, np.median(auto_stetson),
                                                rb.mad(auto_stetson)),
               transform=s.transAxes)
        

    s1.set_title("Histogram: Stetson indices of Spitzer-selected stars in ONC")
    s1.legend(loc="upper left")
    s3.set_xlabel("Stetson Index")
    plt.show()


    # Now, I want to print out some stats relevant to what we want.
    # Think of how "official_star_counter" works.

    # For each group, print the stuff.

    print ""
    for s, m, name in zip(subplot_list, mated_list, name_list):
        # Mean Delta Mag (and sigmas)?
        auto_delta = uka_d.k_ranger[m.where(m[uka_i] != -1)[uka_i]]# doesn't quite work
        strict_delta = uks_d.k_ranger[m.where(m[uks_i] != -1)[uks_i]]

        print ("%s Median delta-K (max-min) for Q=2: %.3f +- %.2f" % 
               (name, np.median(strict_delta), rb.mad(strict_delta)))
        print ("%s Median delta-K (max-min) for Q=1+2: %.3f +- %.2f" % 
               (name, np.median(auto_delta), rb.mad(auto_delta)))


        # What fraction have good periods? Not answerable right now.

    # Finally, let's make a plot of S vs alpha_irac.
    fig2 = plt.figure()

    # define the Stetson column and the alpha_irac column
    m = mated_spitzer

    meg_i = 'Megeath2012_index'
    meg_d = Megeath2012.data

    auto_alpha_irac = meg_d.alpha[m.where(
            m[uka_i] != -1)[meg_i] ]
    strict_alpha_irac = meg_d.alpha[m.where(
            m[uks_i] != -1)[meg_i] ]

    auto_stetson = uka_d.Stetson[m.where(m[uka_i] != -1)[uka_i]]
    strict_stetson = uks_d.Stetson[m.where(m[uks_i] != -1)[uks_i]]

    plt.plot( auto_alpha_irac, auto_stetson, 'o', color='0.7', label="Q=1")
    plt.plot( strict_alpha_irac, strict_stetson, 'o', color='0.2', label="Q=2")
        
    plt.legend()

    plt.xlabel(r"Spectral index $\alpha_{IRAC}$", fontsize=18)
    plt.ylabel("Stetson Index", fontsize=18)

    plt.title(r"Stetson vs $\alpha$ for Spitzer-selected sources")

    plt.semilogy()

    plt.show()

    return mated_spitzer, mated_P, mated_D, fig, fig2