<jupyter_start><jupyter_text>Create Control Table**Goals**: To create a Control Sample from an user-provided initial sample.**Methodology**:1. Cross-match the initial sample against the Gaia DR2 (via astroquery.gaia) using the 2MASS ID's of the initial sample. 2. Applying 3 quality selection criteria ($\varpi$ S/N, vissibility periods, RUWE)3. Identifying outliers (if any) in distance, $\mu_{\alpha}^*$, and $\mu_{\delta}$ via a $\sigma$-threshold criteria.**Inputs**: The IC 348 & NGC 1333 catalogues from Luhman, Esplin & Loutrel 2016 (https://ui.adsabs.harvard.edu/abs/2016ApJ...827...52L/abstract).Those include the most recent candidate members and supersedes previous (e.g., Luhman+2003, Lada+2006) catalogues, and contain the 2MASSIDs for the sources.**Products**: The Control sample.**Notes**:<jupyter_code># Python Basics ========================
import warnings, itertools, sys
from astropy.table            import Table
from astropy.utils.exceptions import AstropyWarning

# PanGaia libraries ====================
sys.path.append('../library')
from lib_utils       import LibUtils       as Utils
from lib_gaia_query  import LibGaiaQuery   as GaiaQuery
from lib_plotters    import LibPlotters    as Plotters

# Extra ================================
sys.path.append('../extra_library/')
from lib_control     import LibControl     as Control # This does not

# PyEsasKy =============================
from pyesasky.pyesasky import ESASkyWidget<jupyter_output>Created TAP+ (v1.2.1) - Connection:
	Host: gea.esac.esa.int
	Use HTTPS: True
	Port: 443
	SSL Port: 443
Created TAP+ (v1.2.1) - Connection:
	Host: geadata.esac.esa.int
	Use HTTPS: True
	Port: 443
	SSL Port: 443<jupyter_text>Create Initial Sample<jupyter_code># Read tables ==========================================
warnings.filterwarnings('ignore', category=AstropyWarning, append=True)
inp_cats = []
labels   = iter(['IC_348', 'NGC_1333'])
for path in ['table_1.dat', 'table_2.dat']:
    inp_cat            = Table.read('../extra_samples/' + path, format = 'ascii') # Read Table
    inp_cat.label      = next(labels)                                             # Add name
    inp_cat['twomass'] = ['N'] * len(inp_cat)                                     # Label sources included in 2MASS
    for i in range(len(inp_cat)):
        if inp_cat['Name'][i][0:5] == '2MASS':
            inp_cat['twomass'][i] = 'Y'
    inp_cats.append(inp_cat)
# Select targets with 2MASS coordinates/ID =============
out_cats = []
for inp_cat in inp_cats:
    out_cat             = inp_cat[inp_cat['twomass'] == 'Y']
    out_cat['col2mass'] = [inp[7:] for inp in out_cat['Name']] #This column is used when crossmatching Gaia * 2MASS
    out_cats.append(Table([out_cat['col2mass']]))
# Save Catalogues ======================================
for i in range(len(out_cats)):
    inp_cat = inp_cats[i]
    out_cat = out_cats[i]
    name_out = f'sample_ini_{inp_cat.label}.vot'
    out_cat.write(name_out, format = 'votable', overwrite = True)
    print(f'Saving Initial sample as: {name_out:>25s}. N_els ={len(out_cat):>4.0f}; input sample has {len(inp_cat)} elements')<jupyter_output>Saving Initial sample as:     sample_ini_IC_348.vot. N_els = 406; input sample has 478 elements
Saving Initial sample as:   sample_ini_NGC_1333.vot. N_els = 161; input sample has 203 elements<jupyter_text>Create Control Sample  Crossmatch Initial Sample against Gaia DR2<jupyter_code># Initial sample * Gaia DR2 using the 2MASS SourceID ===
upload_table = 'sample_ini_IC_348.vot'
# upload_table = 'sample_ini_NGC_1333.vot'
gquery       = GaiaQuery()
gquery.run_gaia2mass_cross_match(upload_table = upload_table, quality_par_SN  = '10', quality_par_vis = '7',quality_par_ruwe = '1.40')<jupyter_output>RUNNING ADQL SYNCRHRONOUS QUERY ==========================================================
Launched query: 'SELECT  TOP 2000 input_table.col2mass, gaia.source_id, gaia.ra, gaia.dec, gaia.parallax, gaia.parallax_error, gaia.pmra, gaia.pmra_error, gaia.pmdec, gaia.pmdec_error, gaia.phot_g_mean_mag, gaia.phot_bp_mean_mag, gaia.phot_rp_mean_mag, gaia.phot_g_mean_flux_over_error, gaia.phot_bp_mean_flux_over_error, gaia.phot_rp_mean_flux_over_error, gaia.l, gaia.b, gaia.visibility_periods_used, gaia.radial_velocity,sqrt(gaia.astrometric_chi2_al/(gaia.astrometric_n_good_obs_al-5)) as unit_weight_e, g_ruwe.ruwe as ruwe FROM tap_upload.input_table as input_table LEFT OUTER JOIN gaiadr2.tmass_best_neighbour AS xmatch ON input_table.col2mass = xmatch.original_ext_source_id LEFT OUTER JOIN gaiadr2.gaia_source          AS gaia   ON xmatch.source_id     = gaia.source_id LEFT OUTER JOIN gaiadr2.ruwe                 AS g_ruwe ON gaia.source_id       = g_ruwe.source_id WHERE gaia.parallax/gaia.parallax[...]<jupyter_text>Apply selection cuts<jupyter_code># Apply cuts ===========================================
control_0 = Utils(label = upload_table.replace('_ini_', '_control_').replace('.vot', ''))
control_0.read_catalogue(gquery.cat) # Read catalogue & add extra columns (e.g., distance)

# Generate histograms ==================================
figures    = Plotters()
figures.load_gaia_cat(control_0.cat)
histograms = figures.plot_3_hist(fig_nm = control_0.label + '_hist_astrometry.pdf',
                                 x1_bins=5, x2_bins=5, x3_bins=5, fontsize = 40, Gauss_fit = True)
# Remove distance, pmRA, pmDec Outliers ================
control_1 = Control(label = control_0.label)
control_1.load_cat(control_0.cat)
control_1.remove_hist_outliers(histograms_dat=histograms, verbose = True, N_sigma = 5)

# Generate new histograms ==============================
figures      = Plotters()
figures.load_gaia_cat(control_1.cat)
histograms_2 = figures.plot_3_hist(fig_nm = control_1.label + '_hist_astrometry_2.pdf', 
                                   x1_bins=3, x2_bins=5, x3_bins=5, fontsize = 40, Gauss_fit = True)

# Save Control Sample ==================================
control_1.save_control_sample()<jupyter_output>Removing outliers in: distance
Removing outliers in: pmra
Removing outliers in: pmdec
=================================================
Removing targets at 5 sigma from Gaussian Centers
Targets in Control Sample:                  144
=================================================<jupyter_text>Show Control Sample astrometric properties<jupyter_code>ccat      = control_1.cat
control_f = Utils(color = 'magenta', label = control_1.label)
control_f.read_catalogue(ccat)

print()
print(f'Showing {control_f.label} first 5 rows:')
display(control_f.cat[0:5])

# Examine Velocity & Histograms ====================
figures  = Plotters()
figures.load_gaia_cat(control_f.cat)
figures.plot_2d_and_hist(col_hist = 'distance', label = control_f.label,
                         color_2d = control_f.color, color_hist = control_f.color, mew = 1)    
# Run ESASKY ===========================================
esasky = ESASkyWidget()
display(esasky)

figures.send_to_ESASky(esasky, background='WISE', color=control_f.color, catalogueName = control_f.label)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup