Ejemplo n.º 1
0
def plot_calibrator_vis2(tgt_info, cal_folder="diagnostics/"):
    """
    """
    cal_oifits = glob.glob(cal_folder + "*fits")
    cal_oifits.sort()
    xy = np.ceil(len(cal_oifits)**0.5).astype(int)

    plt.close("all")
    with PdfPages("plots/calibrator_vis2.pdf") as pdf:
        # Initialise subplot
        fig, axes = plt.subplots(xy, xy)
        axes = axes.flatten()

        for cal_i, cal in enumerate(cal_oifits):
            cal_id = cal.split("SCI")[-1].split("oidata")[0].replace("_", "")
            date = cal.split("/")[-1].split("_")[0]
            #rplt.plot_vis2(cal, cal_id)

            # Check if we've previously flagged this calibrator as bad
            cal_hd = rutils.get_unique_key(tgt_info, [cal_id])[0]
            is_bad = tgt_info.loc[cal_hd]["Quality"] == "BAD"

            if is_bad:
                colour = "red"
            else:
                colour = "black"

            mjds, pairs, vis2, e_vis2, flags, baselines, wavelengths = rdiam.extract_vis2(
                cal)

            n_bl = len(baselines)
            n_wl = len(wavelengths)
            bl_grid = np.tile(baselines, n_wl).reshape([n_wl, n_bl]).T
            wl_grid = np.tile(wavelengths, n_bl).reshape([n_bl, n_wl])

            b_on_lambda = (bl_grid / wl_grid).flatten()

            axes[cal_i].errorbar(b_on_lambda,
                                 vis2.flatten(),
                                 yerr=e_vis2.flatten(),
                                 fmt=".",
                                 elinewidth=0.1,
                                 markersize=0.25)

            #axes[cal_i].set_xlabel(r"Spatial Frequency (rad$^{-1})$")
            #axes[cal_i].set_ylabel(r"Visibility$^2$")
            axes[cal_i].set_title(r"%s (%s)" % (cal_id, date),
                                  color=colour,
                                  fontsize="small")
            #plt.legend(loc="best")
            axes[cal_i].set_xlim([0.0, 25E7])
            axes[cal_i].set_ylim([0.0, 1.0])
            axes[cal_i].tick_params(axis="both", labelsize="xx-small")
            axes[cal_i].xaxis.get_offset_text().set_fontsize("xx-small")
            axes[cal_i].grid()

        #plt.tight_layout()
        plt.gcf().set_size_inches(32, 32)
        plt.subplots_adjust(hspace=0.3)
        pdf.savefig()  #bbox_inches="tight"
Ejemplo n.º 2
0
def merge_sampled_params_and_results(sampled_sci_params, bs_results, tgt_info):
    """For simplicity, merge sampled_sci_params and bs_results
    """
    # Get the IDs from bs_results
    prim_ids = bs_results.keys()

    # And the matching HD IDs
    hd_ids = rutils.get_unique_key(tgt_info, bs_results.keys())

    # Get the list of new columns. For now don't worry about the vectors
    #bs_cols = ['MJD', 'TEL_PAIR', 'VIS2', 'FLAG', 'BASELINE', 'WAVELENGTH',
    #           'LDD_FIT', 'LDD_PRED', 'C_SCALE']
    bs_cols = ["LDD_FIT"]

    for col in bs_cols:
        sampled_sci_params[col] = np.nan

    # Now go through and combine
    for prim_id, hd_id in zip(prim_ids, hd_ids):
        # Add to sampled_sci_params
        # Extra contingencies in case we're looking at results as we go
        n_total = len(sampled_sci_params.loc[hd_id])
        n_samples = len(bs_results[prim_id])

        results = np.zeros((n_total, len(bs_cols))) * np.nan
        results[:n_samples, :] = bs_results[prim_id][bs_cols].values

        sampled_sci_params.loc[hd_id, bs_cols] = results
Ejemplo n.º 3
0
def select_only_bad_target_durations(sequence_durations, tgt_info):
    """Takes the output of calculate_target_durations, and compares to the 
    target quality values in tgt_info, returning only durations for only those
    targets which we wish to exclude from the calibration process.
    
    Parameters
    ----------
    sequence_durations: dict
        Output from calculate_target_durations, a dict mapping nights to start
        and end times for each target: durations[night] = [target, start, end]
        
    tgt_info: pandas dataframe
        Pandas dataframe of all target info
        
    Returns
    -------
    bad_durations: dict
        Dict of same form as sequence_durations, but containing only the 
        calibrators we wish to exclude.
    """
    # Initialise results dict
    bad_durations = {}
    
    for night in sequence_durations:
        bad_durations[night] = []
        
        for star in sequence_durations[night]:
            # Get the star info, making sure to check primary, bayer, and HD
            # IDs given the non-unique IDs used
            prim_id = rutils.get_unique_key(tgt_info, star[0])
            
            # Check if it is a bad calibrator, and if so add to return dict
            if tgt_info.loc[prim_id[0]]["Quality"] == "BAD":
                bad_durations[night].append(star)
                
    return bad_durations
Ejemplo n.º 4
0
def calibrate_calibrators(sequences,
                          complete_sequences,
                          base_path,
                          tgt_info,
                          n_pred_ldd,
                          e_pred_ldd,
                          test_all_cals=False):
    """
    Assume that every sequence has three calibrators - we can simply do three
    loops of the calibration routine, turning the science off each time, and 
    flipping each calibrator to science in order going through.
    
    To do this, we'll need to flip all the science targets to "BAD" so that
    they're ignored. Hopefully this doesn't affect the kappa matrices. Should
    probably also flip them to calibrator status in case the pipeline complains
    that an entire science target is being ignored.
    """
    run_local = False
    already_calibrated = False
    do_random_ifg_sampling = False
    n_bootstraps = 1
    base_path = "/priv/mulga1/arains/pionier/complete_sequences/%s_v3.73_abcd/"
    results_path = "/home/arains/code/reach/diagnostics/"

    # Get a list of the calibrators, in order, turn one calibrator per sequence
    # to science per calibration run - should need three runs
    calibrators = np.vstack(sequences.values())[:, ::2]

    # Reset any currently "BAD" quality targets
    if test_all_cals:
        tgt_info["Quality"] = [None] * len(tgt_info)

    # Remove existing diagnostic files
    existing_files = glob.glob(results_path + "*.fits")

    for fits_file in existing_files:
        os.remove(fits_file)

    # Set all science targets to "BAD" so that we ignore them when calibrating
    science = rutils.get_unique_key(tgt_info,
                                    np.vstack(sequences.values())[:, 1])
    tgt_info.loc[science, "Quality"] = ["BAD"] * len(science)

    # Write nightly pndrs.i scripts
    for cal_i in np.arange(0, 3):
        print("Now running for set number %i of calibrators" % cal_i)

        # Set all science targets to "BAD" so that we ignore them when calibrating
        tgt_info["Science"] = [False] * len(tgt_info)

        # Get the cal_i-th column of calibrators, and get their unique IDs
        cal_ids = rutils.get_unique_key(tgt_info, calibrators[:, cal_i])

        # Flip each of the calibrators in cals_to_sci to be a science target
        tgt_info.loc[cal_ids, "Science"] = [True] * len(cal_ids)

        # Run calibration (no bootstrapping)
        rpndrs.run_n_bootstraps(sequences,
                                complete_sequences,
                                base_path,
                                tgt_info,
                                n_pred_ldd,
                                e_pred_ldd,
                                n_bootstraps,
                                run_local=run_local,
                                already_calibrated=already_calibrated,
                                do_random_ifg_sampling=do_random_ifg_sampling,
                                results_path=results_path)
Ejemplo n.º 5
0
def make_table_calibrators(tgt_info, sequences):
    """Summarise the calibrators used (or not).
    """
    # Column names and associated units
    columns = OrderedDict([("HD", ""), ("SpT$^a$", "(Actual)$^a$"),
                           ("SpT$^b$", "(Adopted)$^b$"),
                           (r"$V_{\rm T}^c$", "(mag)"), (r"$H^d$", "(mag)"),
                           (r"$E(B-V)$", "(mag)"),
                           ("$\\theta_{\\rm pred}$", "(mas)"),
                           ("$\\theta_{\\rm LD}$ Rel", ""), ("Used", ""),
                           ("Plx", "(mas)"), ("Target/s", "")])

    labels = ["index", "SpT", "VTmag", "Hmag", "Quality", "Target/s"]

    exclusion_codes = OrderedDict([("Binarity", "f"), ("IR excess", "g"),
                                   ("Inconsistent photometry", "h")])
    header = []
    table_rows = []
    footer = []
    notes = []

    # Construct the header of the table
    #header.append("\\begin{landscape}")
    header.append("\\begin{tabular}{%s}" % ("c" * len(columns)))
    header.append("\hline")
    header.append((r"HD & \multicolumn{2}{c}{SpT} & " +
                   ("%s & " * (len(columns) - 3))[:-2] + r"\\") %
                  tuple(columns.keys()[3:]))

    #header.append((("%s & "*len(columns))[:-2] + r"\\") % tuple(columns.keys()))
    header.append(
        (("%s & " * len(columns))[:-2] + r"\\") % tuple(columns.values()))
    header.append("\hline")

    # Populate the table for every science target
    for star_i, star in tgt_info[~tgt_info["Science"]].iterrows():
        table_row = ""

        # Only continue if we have data on this particular star
        if not star["in_paper"]:
            continue

        # Find which science target/s the calibrator is associated with
        scis = []
        for seq in sequences:
            cals = [
                target.replace("_", "").replace(".", "").replace(" ", "")
                for target in sequences[seq]
            ]

            if star.name in rutils.get_unique_key(tgt_info, cals):
                scis.append(rutils.format_id(seq[1]))

        scis = list(set(scis))
        scis.sort()

        # Step through column by column
        table_row += "%s & " % star.name.replace("HD", "")

        # Make SpT have a smaller font if it's long
        #if len(str(star["SpT"])) > 5:
        #    table_row += "{\\tiny %s } & " % star["SpT"]
        #else:
        table_row += "%s & " % star["SpT"]
        table_row += "%s & " % star["SpT_simple"]

        table_row += "%0.2f & " % star["VTmag"]
        table_row += "%0.2f & " % star["Hmag"]
        table_row += "%0.3f & " % star["eb_v"]

        # Remove placeholder LDD
        if star["LDD_pred"] == 1.0 or np.isnan(star["LDD_pred"]):
            table_row += "- & "
        else:
            table_row += r"%0.3f $\pm$ %0.2f & " % (star["LDD_pred"],
                                                    star["e_LDD_pred"])

        table_row += ("%s & " % star["LDD_rel"]).split("LDD_")[-1].replace(
            "_", "-")

        # Determine whether the star was used as a calibrator
        if star["Quality"] == "BAD":
            reason_code = exclusion_codes[star["exclusion_reason"]]
            table_row += r"N$^%s$ & " % reason_code
        else:
            table_row += "Y & "

        # Both parallaxes are nan, placeholder value
        if np.isnan(star["Plx"]) and np.isnan(star["Plx_alt"]):
            table_row += "- & "

        # If Gaia plx is nan, use Tycho
        elif np.isnan(star["Plx"]):
            table_row += r"%0.2f $\pm$ %0.2f$^c$ &" % (star["Plx_alt"],
                                                       star["e_Plx_alt"])

        # From Gaia DR2
        else:
            table_row += r"%0.2f $\pm$ %0.2f$^e$ &" % (star["Plx"],
                                                       star["e_Plx"])

        table_row += ("%s, " * len(scis) % tuple(scis))[:-2]

        # Replace any nans with '-'
        table_rows.append(table_row.replace("nan", "-") + r"\\")

    # Finish the table
    footer.append("\hline")
    footer.append("\end{tabular}")
    #footer.append("\\end{landscape}")

    # Add notes section
    notes.append("\\begin{minipage}{\linewidth}")
    notes.append("\\vspace{0.1cm}")

    notes.append("\\textbf{Notes:} $^a$SIMBAD, "
                 "$^b$Adopted for intrinsic colour grid interpolation,"
                 "$^c$Tycho \citet{hog_tycho-2_2000}, "
                 "$^d$2MASS \citet{skrutskie_two_2006}, "
                 "$^e$Gaia \citet{brown_gaia_2018}")

    for er in exclusion_codes.keys():
        notes[-1] += r", $^%s$%s" % (exclusion_codes[er], er)

    notes[-1] += "\\\\"

    notes.append("\\end{minipage}")
    #notes.append("\\end{table*}")
    #notes.append("\\end{landscape}")

    # Write the tables
    table_1 = header + table_rows[:45] + footer
    table_2 = header + table_rows[45:] + footer + notes

    np.savetxt("paper/table_calibrators_1.tex", table_1, fmt="%s")
    np.savetxt("paper/table_calibrators_2.tex", table_2, fmt="%s")
Ejemplo n.º 6
0
def make_table_observation_log(tgt_info, complete_sequences, sequences):
    """Make the table to summarise the observations, including what star was
    in what sequence.
    """
    columns = OrderedDict([
        ("Star", ""),
        ("UT Date", ""),
        ("ESO", "Period"),
        ("Sequence", "Type"),
        ("Baseline", ""),
        #("Spectral", "channels"),
        ("Calibrator", "HD"),
        ("Calibrators", "Used")
    ])

    header = []
    table_rows = {}
    footer = []

    # Construct the header of the table
    header.append("\\begin{tabular}{%s}" % ("c" * len(columns)))
    header.append("\hline")
    header.append(
        (("%s & " * len(columns))[:-2] + r"\\") % tuple(columns.keys()))
    header.append(
        (("%s & " * len(columns))[:-2] + r"\\") % tuple(columns.values()))
    header.append("\hline")

    # Populate the table for every science target
    for seq in complete_sequences:
        table_row = ""

        star_id = rutils.format_id(seq[1])
        ut_date = complete_sequences[seq][0]
        period = seq[0]
        seq_type = seq[2]
        baselines = complete_sequences[seq][2][0][9]
        cals = [
            target.replace("_", "").replace(".", "").replace(" ", "")
            for target in sequences[seq][::2]
        ]
        cals_hd = tuple(rutils.get_unique_key(tgt_info, cals))
        cals = ("%s, %s, %s" % cals_hd).replace("HD", "")

        # Figure out how many calibrators we used (i.e. weren't 'BAD')
        cal_quality = [tgt_info.loc[cal]["Quality"] for cal in cals_hd]
        cals_used = 3 - np.sum(np.array(cal_quality) == "BAD")

        table_row = ("%s & " * len(columns)) % (star_id, ut_date, period,
                                                seq_type, baselines, cals,
                                                str(cals_used))

        table_rows[(ut_date, star_id, seq_type)] = table_row[:-2] + r"\\"

    # Finish the table
    footer.append("\hline")
    footer.append("\end{tabular}")

    # Sort by UT
    ut_sorted = table_rows.keys()
    ut_sorted.sort()

    sorted_rows = [table_rows[row] for row in ut_sorted]

    # Write the table
    table = header + sorted_rows + footer
    np.savetxt("paper/table_observations.tex", table, fmt="%s")