Ejemplo n.º 1
0
def find_initial_wvlsol(helper, band, obsids, thar_products,
                        thar_reidentified_products, new_orders):
    if 1:

        from igrins.libs.process_thar import (load_echelogram,
                                              align_echellogram_thar,
                                              check_thar_transorm,
                                              get_wavelength_solutions)

        from igrins.libs.master_calib import load_thar_ref_data

        #ref_date = "20140316"

        thar_ref_data = load_thar_ref_data(helper.refdate, band)

        ref_date = thar_ref_data["ref_date"]
        echel = load_echelogram(ref_date, band)

        thar_master_obsid = obsids[0]
        ap = get_simple_aperture(helper,
                                 band,
                                 thar_master_obsid,
                                 orders=new_orders)

        thar_aligned_echell_products = \
             align_echellogram_thar(thar_reidentified_products,
                                    echel, band, ap)

        # We do not save this product yet.
        # igr_storage.store(thar_aligned_echell_products,
        #                   mastername=thar_filenames[0],
        #                   masterhdu=hdu)

    # Make figures

    thar_filenames = helper.get_filenames(band, obsids)
    thar_basename = os.path.splitext(os.path.basename(thar_filenames[0]))[0]
    thar_master_obsid = obsids[0]

    if 1:

        fig_list = check_thar_transorm(thar_products,
                                       thar_aligned_echell_products)

        from igrins.libs.qa_helper import figlist_to_pngs
        igr_path = helper.igr_path
        thar_figs = igr_path.get_section_filename_base("QA_PATH", "thar",
                                                       "thar_" + thar_basename)
        figlist_to_pngs(thar_figs, fig_list)

        thar_wvl_sol = get_wavelength_solutions(thar_aligned_echell_products,
                                                echel, new_orders)

        hdu = pyfits.open(thar_filenames[0])[0]
        helper.igr_storage.store(thar_wvl_sol,
                                 mastername=thar_filenames[0],
                                 masterhdu=hdu)
Ejemplo n.º 2
0
def save_qa(obsset):

    df = obsset.load_data_frame("SKY_FITTED_PIXELS_JSON", orient="split")

    msk_ = df["slit_center"] == 0.5
    dfm_ = df[msk_]

    msk = np.isfinite(dfm_["pixels"])
    dfm = dfm_[msk]

    lines_map = dict((o, (_["pixels"].values, _["wavelength"].values))
                     for o, _ in dfm.groupby("order"))

    from matplotlib.figure import Figure
    from igrins.libs.ecfit import check_fit

    d = obsset.load_item("SKY_WVLSOL_JSON")

    orders = d["orders"]

    fit_results = obsset.load_item("SKY_WVLSOL_FIT_RESULT_JSON")

    # fit_results = dict(xyz=[xl[msk], yl[msk], zl[msk]],
    #                    fit_params=fit_params,
    #                    fitted_model=poly_2d)

    # xl, yl, zl = get_ordered_line_data(reidentified_lines_map)

    xl, yl, zlo = fit_results["xyz"]
    xl, yl, zlo = [np.array(_) for _ in [xl, yl, zlo]]
    zl = zlo

    m = np.array(fit_results["fitted_mask"])

    lines_map_filtered = dict((o, (_["pixels"].values, _["wavelength"].values))
                              for o, _ in dfm[m].groupby("order"))

    modeul_name, class_name, serialized = fit_results["fitted_model"]
    from igrins.libs.astropy_poly_helper import deserialize_poly_model

    p = deserialize_poly_model(modeul_name, class_name, serialized)

    if 1:
        fig1 = Figure(figsize=(12, 7))
        check_fit(fig1, xl, yl, zl, p, orders, lines_map)
        fig1.tight_layout()

        fig2 = Figure(figsize=(12, 7))
        check_fit(fig2, xl[m], yl[m], zl[m], p, orders, lines_map_filtered)
        fig2.tight_layout()

    from igrins.libs.qa_helper import figlist_to_pngs
    dest_dir = obsset.query_item_path("qa_sky_fit2d_dir", subdir="sky_fit2d")
    figlist_to_pngs(dest_dir, [fig1, fig2])
Ejemplo n.º 3
0
    def save_qa(helper, band, obsids,
                orders_w_solutions,
                reidentified_lines_map, p, m):
        # filter out the line indices not well fit by the surface


        keys = reidentified_lines_map.keys()
        di_list = [len(reidentified_lines_map[k_][0]) for k_ in keys]

        endi_list = np.add.accumulate(di_list)

        filter_mask = [m[endi-di:endi] for di, endi in zip(di_list, endi_list)]
        #from itertools import compress
        # _ = [list(compress(indices, mm)) for indices, mm \
        #      in zip(line_indices_list, filter_mask)]
        # line_indices_list_filtered = _

        reidentified_lines_ = [reidentified_lines_map[k_] for k_ in keys]
        _ = [(v_[0][mm], v_[1][mm]) for v_, mm \
             in zip(reidentified_lines_, filter_mask)]

        reidentified_lines_map_filtered = dict(zip(orders_w_solutions, _))


        if 1:
            from matplotlib.figure import Figure
            from igrins.libs.ecfit import get_ordered_line_data, check_fit

            xl, yl, zl = get_ordered_line_data(reidentified_lines_map)

            fig1 = Figure(figsize=(12, 7))
            check_fit(fig1, xl, yl, zl, p,
                      orders_w_solutions,
                      reidentified_lines_map)
            fig1.tight_layout()

            fig2 = Figure(figsize=(12, 7))
            check_fit(fig2, xl[m], yl[m], zl[m], p,
                      orders_w_solutions,
                      reidentified_lines_map_filtered)
            fig2.tight_layout()

        from igrins.libs.qa_helper import figlist_to_pngs
        igr_path = helper.igr_path
        sky_basename = helper.get_basename(band, obsids[0])
        sky_figs = igr_path.get_section_filename_base("QA_PATH",
                                                      "oh_fit2d",
                                                      "oh_fit2d_"+sky_basename)
        figlist_to_pngs(sky_figs, [fig1, fig2])
Ejemplo n.º 4
0
def save_figures(obsset):

    ### THIS NEEDS TO BE REFACTORED!

    from igrins.libs.process_flat import check_order_flat

    if 1:
        order_flat_json = obsset.load_item("order_flat_json")

        fig_list = check_order_flat(order_flat_json)

        from igrins.libs.qa_helper import figlist_to_pngs
        dest_dir = obsset.query_item_path("qa_orderflat_dir",
                                          subdir="orderflat")
        figlist_to_pngs(dest_dir, fig_list)
Ejemplo n.º 5
0
def save_figures(obsset):

    ### THIS NEEDS TO BE REFACTORED!

    from igrins.libs.process_flat import check_order_flat

    if 1:
        order_flat_json = obsset.load_item("order_flat_json")

        fig_list = check_order_flat(order_flat_json)

        from igrins.libs.qa_helper import figlist_to_pngs
        dest_dir = obsset.query_item_path("qa_orderflat_dir",
                                          subdir="orderflat")
        figlist_to_pngs(dest_dir, fig_list)
Ejemplo n.º 6
0
    def save_qa(helper, band, obsids, orders_w_solutions,
                reidentified_lines_map, p, m):
        # filter out the line indices not well fit by the surface

        keys = reidentified_lines_map.keys()
        di_list = [len(reidentified_lines_map[k_][0]) for k_ in keys]

        endi_list = np.add.accumulate(di_list)

        filter_mask = [
            m[endi - di:endi] for di, endi in zip(di_list, endi_list)
        ]
        #from itertools import compress
        # _ = [list(compress(indices, mm)) for indices, mm \
        #      in zip(line_indices_list, filter_mask)]
        # line_indices_list_filtered = _

        reidentified_lines_ = [reidentified_lines_map[k_] for k_ in keys]
        _ = [(v_[0][mm], v_[1][mm]) for v_, mm \
             in zip(reidentified_lines_, filter_mask)]

        reidentified_lines_map_filtered = dict(zip(orders_w_solutions, _))

        if 1:
            from matplotlib.figure import Figure
            from igrins.libs.ecfit import get_ordered_line_data, check_fit

            xl, yl, zl = get_ordered_line_data(reidentified_lines_map)

            fig1 = Figure(figsize=(12, 7))
            check_fit(fig1, xl, yl, zl, p, orders_w_solutions,
                      reidentified_lines_map)
            fig1.tight_layout()

            fig2 = Figure(figsize=(12, 7))
            check_fit(fig2, xl[m], yl[m], zl[m], p, orders_w_solutions,
                      reidentified_lines_map_filtered)
            fig2.tight_layout()

        from igrins.libs.qa_helper import figlist_to_pngs
        igr_path = helper.igr_path
        sky_basename = helper.get_basename(band, obsids[0])
        sky_figs = igr_path.get_section_filename_base(
            "QA_PATH", "oh_fit2d", "oh_fit2d_" + sky_basename)
        figlist_to_pngs(sky_figs, [fig1, fig2])
Ejemplo n.º 7
0
def store_qa(obsset_on, obsset_off):

    # caldb = helper.get_caldb()
    # basename = (band, obsids_on[0])

    # plot qa figures.

    if 1:
        from igrins.libs.process_flat import plot_trace_solutions
        from matplotlib.figure import Figure

        fig1 = Figure(figsize=[9, 4])

        flat_deriv = obsset_on.load_image("flat_deriv")
        trace_dict = obsset_on.load_item("flatcentroids_json")

        from igrins.libs.flat_qa import check_trace_order
        check_trace_order(flat_deriv, trace_dict, fig1)

        flat_normed = obsset_on.load_image("flat_normed")
        flatcentroid_sol_json = obsset_on.load_item("flatcentroid_sol_json")

        from igrins.libs.flat_qa import plot_trace_solutions
        fig2, fig3 = plot_trace_solutions(flat_normed,
                                          flatcentroid_sol_json)

    # flatoff_basename = os.path.splitext(os.path.basename(flat_off_filenames[0]))[0]
    # flaton_basename = os.path.splitext(os.path.basename(flat_on_filenames[0]))[0]

    # flatoff_basename = helper.get_basename(band, obsids_off[0])
    # flaton_basename = helper.get_basename(band, obsids_on[0])

    if 1:
        from igrins.libs.qa_helper import figlist_to_pngs
        # get_filename = helper.get_section_filename_base
        dest_dir = obsset_on.query_item_path("qa_flat_aperture_dir",
                                             subdir="aperture")
        # aperture_figs = get_filename("QA_PATH",
        #                              "aperture_"+flaton_basename,
        #                              "aperture_"+flaton_basename)

        figlist_to_pngs(dest_dir, [fig1, fig2, fig3])
Ejemplo n.º 8
0
def store_qa(obsset_on, obsset_off):

    # caldb = helper.get_caldb()
    # basename = (band, obsids_on[0])

    # plot qa figures.

    if 1:
        from igrins.libs.process_flat import plot_trace_solutions
        from matplotlib.figure import Figure

        fig1 = Figure(figsize=[9, 4])

        flat_deriv = obsset_on.load_image("flat_deriv")
        trace_dict = obsset_on.load_item("flatcentroids_json")

        from igrins.libs.flat_qa import check_trace_order
        check_trace_order(flat_deriv, trace_dict, fig1)

        flat_normed = obsset_on.load_image("flat_normed")
        flatcentroid_sol_json = obsset_on.load_item("flatcentroid_sol_json")

        from igrins.libs.flat_qa import plot_trace_solutions
        fig2, fig3 = plot_trace_solutions(flat_normed, flatcentroid_sol_json)

    # flatoff_basename = os.path.splitext(os.path.basename(flat_off_filenames[0]))[0]
    # flaton_basename = os.path.splitext(os.path.basename(flat_on_filenames[0]))[0]

    # flatoff_basename = helper.get_basename(band, obsids_off[0])
    # flaton_basename = helper.get_basename(band, obsids_on[0])

    if 1:
        from igrins.libs.qa_helper import figlist_to_pngs
        # get_filename = helper.get_section_filename_base
        dest_dir = obsset_on.query_item_path("qa_flat_aperture_dir",
                                             subdir="aperture")
        # aperture_figs = get_filename("QA_PATH",
        #                              "aperture_"+flaton_basename,
        #                              "aperture_"+flaton_basename)

        figlist_to_pngs(dest_dir, [fig1, fig2, fig3])
Ejemplo n.º 9
0
    def save_check_images(self, extractor, xl, yl, zl_list, pm_list):

        from igrins.libs.ecfit import check_fit_simple

        fig_list = []
        from matplotlib.figure import Figure

        orders = extractor.orders_w_solutions
        for zl, (p, m) in zip(zl_list, pm_list):
            fig = Figure()
            check_fit_simple(fig, xl[m], yl[m], zl[m], p, orders)
            fig_list.append(fig)

        igr_path = extractor.igr_path
        from igrins.libs.qa_helper import figlist_to_pngs
        sky_basename = extractor.tgt_basename
        sky_figs = igr_path.get_section_filename_base(
            "QA_PATH", "oh_distortion", "oh_distortion_" + sky_basename)
        print fig_list
        figlist_to_pngs(sky_figs, fig_list)

        return fig_list
Ejemplo n.º 10
0
    def save_check_images(self, extractor, xl, yl, zl_list, pm_list):

        from igrins.libs.ecfit import check_fit_simple

        fig_list = []
        from matplotlib.figure import Figure

        orders = extractor.orders_w_solutions
        for zl, (p, m)  in zip(zl_list, pm_list):
            fig = Figure()
            check_fit_simple(fig, xl[m], yl[m], zl[m], p, orders)
            fig_list.append(fig)

        igr_path = extractor.igr_path
        from igrins.libs.qa_helper import figlist_to_pngs
        sky_basename = extractor.tgt_basename
        sky_figs = igr_path.get_section_filename_base("QA_PATH",
                                                      "oh_distortion",
                                                      "oh_distortion_"+sky_basename)
        print fig_list
        figlist_to_pngs(sky_figs, fig_list)

        return fig_list
Ejemplo n.º 11
0
def process_abba_band(recipe, utdate, refdate, band, 
                      groupname,
                      obsids, frametypes,
                      config,
                      do_interactive_figure=False,
                      threshold_a0v=0.1,
                      objname="",
                      multiply_model_a0v=False,
                      html_output=False,
                      a0v_obsid=None,
                      basename_postfix=None):

    target_type, nodding_type = recipe.split("_")

    if target_type in ["A0V"]:
        FIX_TELLURIC=False
    elif target_type in ["STELLAR", "EXTENDED"]:
        FIX_TELLURIC=True
    else:
        raise ValueError("Unknown recipe : %s" % recipe)


    from recipe_extract_base import RecipeExtractPR
    extractor = RecipeExtractPR(utdate, band,
                                obsids, config)

    master_obsid = extractor.pr.master_obsid
    igr_path = extractor.pr.igr_path

    mastername = igr_path.get_basename(band, groupname)

    tgt = extractor.get_oned_spec_helper(mastername,
                                         basename_postfix=basename_postfix)

    orders_w_solutions = extractor.orders_w_solutions

    # if wavelengths list are sorted in increasing order of
    # wavelength, recerse the order list
    if tgt.um[0][0] < tgt.um[-1][0]:
        orders_w_solutions = orders_w_solutions[::-1]

    if FIX_TELLURIC:

        if (a0v_obsid is None) or (a0v_obsid == "1"):
            A0V_basename = extractor.basenames["a0v"]
        else:
            A0V_basename = "SDC%s_%s_%04d" % (band, utdate, int(a0v_obsid))
            print A0V_basename

        a0v = extractor.get_oned_spec_helper(A0V_basename,
                                             basename_postfix=basename_postfix)

        tgt_spec_cor = get_tgt_spec_cor(tgt, a0v,
                                        threshold_a0v,
                                        multiply_model_a0v,
                                        config)

    # prepare i1i2_list
    i1i2_list = get_i1i2_list(extractor,
                              orders_w_solutions)




    fig_list = []


    if 1:

        if do_interactive_figure:
            from matplotlib.pyplot import figure as Figure
        else:
            from matplotlib.figure import Figure
        fig1 = Figure(figsize=(12,6))
        fig_list.append(fig1)

        ax1a = fig1.add_subplot(211)
        ax1b = fig1.add_subplot(212, sharex=ax1a)

        for wvl, s, sn in zip(tgt.um,
                              tgt.spec, tgt.sn):
            #s[s<0] = np.nan
            #sn[sn<0] = np.nan

            ax1a.plot(wvl, s)
            ax1b.plot(wvl, sn)

        ax1a.set_ylabel("Counts [DN]")
        ax1b.set_ylabel("S/N per Res. Element")
        ax1b.set_xlabel("Wavelength [um]")

        ax1a.set_title(objname)



    if FIX_TELLURIC:

        fig2 = Figure(figsize=(12,6))
        fig_list.append(fig2)

        ax2a = fig2.add_subplot(211)
        ax2b = fig2.add_subplot(212, sharex=ax2a)

        #from igrins.libs.stddev_filter import window_stdev

        for wvl, s, t in zip(tgt.um,
                             tgt_spec_cor,
                             a0v.flattened):

            ax2a.plot(wvl, t, "0.8", zorder=0.5)
            ax2b.plot(wvl, s, zorder=0.5)


        s_max_list = []
        s_min_list = []
        for s in tgt_spec_cor[3:-3]:
            s_max_list.append(np.nanmax(s))
            s_min_list.append(np.nanmin(s))
        s_max = np.max(s_max_list)
        s_min = np.min(s_min_list)
        ds_pad = 0.05 * (s_max - s_min)

        ax2a.set_ylabel("A0V flattened")
        ax2a.set_ylim(-0.05, 1.1)
        ax2b.set_ylabel("Target / A0V")
        ax2b.set_xlabel("Wavelength [um]")

        ax2b.set_ylim(s_min-ds_pad, s_max+ds_pad)
        ax2a.set_title(objname)



    # save figures
    if fig_list:
        for fig in fig_list:
            fig.tight_layout()

        # tgt_basename = extractor.pr.tgt_basename
        tgt_basename = mastername

        dirname = "spec_"+tgt_basename
        basename_postfix_s = basename_postfix if basename_postfix is not None else ""
        filename_prefix = "spec_" + tgt_basename + basename_postfix_s
        figout = igr_path.get_section_filename_base("QA_PATH",
                                                    filename_prefix,
                                                    dirname)
        #figout = obj_path.get_secondary_path("spec", "spec_dir")
        from igrins.libs.qa_helper import figlist_to_pngs
        figlist_to_pngs(figout, fig_list)

    # save html

    if html_output:
        if basename_postfix is not None:
            igr_log.warn("For now, no html output is generated if basename-postfix option is used")
        else:
            dirname = config.get_value('HTML_PATH', utdate)
            from igrins.libs.path_info import get_zeropadded_groupname

            objroot = get_zeropadded_groupname(groupname)
            html_save(utdate, dirname, objroot, band,
                      orders_w_solutions, tgt.um,
                      tgt.spec, tgt.sn, i1i2_list)

            if FIX_TELLURIC:
                objroot = get_zeropadded_groupname(groupname)+"A0V"
                html_save(utdate, dirname, objroot, band,
                          orders_w_solutions, tgt.um,
                          a0v.flattened, tgt_spec_cor, i1i2_list,
                          spec_js_name="jj_a0v.js")


    if do_interactive_figure:
        import matplotlib.pyplot as plt
        plt.show()
Ejemplo n.º 12
0
def save_qa(obsset):


    df = obsset.load_data_frame("SKY_FITTED_PIXELS_JSON", orient="split")

    msk_ = df["slit_center"] == 0.5
    dfm_ = df[msk_]

    msk = np.isfinite(dfm_["pixels"])
    dfm = dfm_[msk]

    lines_map = dict((o, (_["pixels"].values, _["wavelength"].values))
                     for o, _ in dfm.groupby("order"))

    from matplotlib.figure import Figure
    from igrins.libs.ecfit import check_fit

    d = obsset.load_item("SKY_WVLSOL_JSON")

    orders = d["orders"]

    fit_results = obsset.load_item("SKY_WVLSOL_FIT_RESULT_JSON")

    # fit_results = dict(xyz=[xl[msk], yl[msk], zl[msk]],
    #                    fit_params=fit_params,
    #                    fitted_model=poly_2d)

    # xl, yl, zl = get_ordered_line_data(reidentified_lines_map)

    xl, yl, zlo = fit_results["xyz"]
    xl, yl, zlo = [np.array(_) for _ in [xl, yl, zlo]]
    zl = zlo

    m = np.array(fit_results["fitted_mask"])

    lines_map_filtered = dict((o, (_["pixels"].values,
                                   _["wavelength"].values))
                              for o, _ in dfm[m].groupby("order"))

    modeul_name, class_name, serialized = fit_results["fitted_model"]
    from igrins.libs.astropy_poly_helper import deserialize_poly_model
   
    p = deserialize_poly_model(modeul_name, class_name, serialized)

    if 1:
        fig1 = Figure(figsize=(12, 7))
        check_fit(fig1, xl, yl, zl, p,
                  orders,
                  lines_map)
        fig1.tight_layout()

        fig2 = Figure(figsize=(12, 7))
        check_fit(fig2, xl[m], yl[m], zl[m], p,
                  orders,
                  lines_map_filtered)
        fig2.tight_layout()

    from igrins.libs.qa_helper import figlist_to_pngs
    dest_dir = obsset.query_item_path("qa_sky_fit2d_dir",
                                      subdir="sky_fit2d")
    figlist_to_pngs(dest_dir, [fig1, fig2])
Ejemplo n.º 13
0
def process_abba_band(recipe,
                      utdate,
                      refdate,
                      band,
                      groupname,
                      obsids,
                      frametypes,
                      config,
                      do_interactive_figure=False,
                      threshold_a0v=0.1,
                      objname="",
                      multiply_model_a0v=False,
                      html_output=False,
                      a0v_obsid=None,
                      basename_postfix=None):

    target_type, nodding_type = recipe.split("_")

    if target_type in ["A0V"]:
        FIX_TELLURIC = False
    elif target_type in ["STELLAR", "EXTENDED"]:
        FIX_TELLURIC = True
    else:
        raise ValueError("Unknown recipe : %s" % recipe)

    from recipe_extract_base import RecipeExtractPR
    extractor = RecipeExtractPR(utdate, band, obsids, config)

    master_obsid = extractor.pr.master_obsid
    igr_path = extractor.pr.igr_path

    mastername = igr_path.get_basename(band, groupname)

    tgt = extractor.get_oned_spec_helper(mastername,
                                         basename_postfix=basename_postfix)

    orders_w_solutions = extractor.orders_w_solutions

    # if wavelengths list are sorted in increasing order of
    # wavelength, recerse the order list
    if tgt.um[0][0] < tgt.um[-1][0]:
        orders_w_solutions = orders_w_solutions[::-1]

    if FIX_TELLURIC:

        if (a0v_obsid is None) or (a0v_obsid == "1"):
            A0V_basename = extractor.basenames["a0v"]
        else:
            A0V_basename = "SDC%s_%s_%04d" % (band, utdate, int(a0v_obsid))
            print A0V_basename

        a0v = extractor.get_oned_spec_helper(A0V_basename,
                                             basename_postfix=basename_postfix)

        tgt_spec_cor = get_tgt_spec_cor(tgt, a0v, threshold_a0v,
                                        multiply_model_a0v, config)

    # prepare i1i2_list
    i1i2_list = get_i1i2_list(extractor, orders_w_solutions)

    fig_list = []

    if 1:

        if do_interactive_figure:
            from matplotlib.pyplot import figure as Figure
        else:
            from matplotlib.figure import Figure
        fig1 = Figure(figsize=(12, 6))
        fig_list.append(fig1)

        ax1a = fig1.add_subplot(211)
        ax1b = fig1.add_subplot(212, sharex=ax1a)

        for wvl, s, sn in zip(tgt.um, tgt.spec, tgt.sn):
            #s[s<0] = np.nan
            #sn[sn<0] = np.nan

            ax1a.plot(wvl, s)
            ax1b.plot(wvl, sn)

        ax1a.set_ylabel("Counts [DN]")
        ax1b.set_ylabel("S/N per Res. Element")
        ax1b.set_xlabel("Wavelength [um]")

        ax1a.set_title(objname)

    if FIX_TELLURIC:

        fig2 = Figure(figsize=(12, 6))
        fig_list.append(fig2)

        ax2a = fig2.add_subplot(211)
        ax2b = fig2.add_subplot(212, sharex=ax2a)

        #from igrins.libs.stddev_filter import window_stdev

        for wvl, s, t in zip(tgt.um, tgt_spec_cor, a0v.flattened):

            ax2a.plot(wvl, t, "0.8", zorder=0.5)
            ax2b.plot(wvl, s, zorder=0.5)

        s_max_list = []
        s_min_list = []
        for s in tgt_spec_cor[3:-3]:
            s_max_list.append(np.nanmax(s))
            s_min_list.append(np.nanmin(s))
        s_max = np.max(s_max_list)
        s_min = np.min(s_min_list)
        ds_pad = 0.05 * (s_max - s_min)

        ax2a.set_ylabel("A0V flattened")
        ax2a.set_ylim(-0.05, 1.1)
        ax2b.set_ylabel("Target / A0V")
        ax2b.set_xlabel("Wavelength [um]")

        ax2b.set_ylim(s_min - ds_pad, s_max + ds_pad)
        ax2a.set_title(objname)

    # save figures
    if fig_list:
        for fig in fig_list:
            fig.tight_layout()

        # tgt_basename = extractor.pr.tgt_basename
        tgt_basename = mastername

        dirname = "spec_" + tgt_basename
        basename_postfix_s = basename_postfix if basename_postfix is not None else ""
        filename_prefix = "spec_" + tgt_basename + basename_postfix_s
        figout = igr_path.get_section_filename_base("QA_PATH", filename_prefix,
                                                    dirname)
        #figout = obj_path.get_secondary_path("spec", "spec_dir")
        from igrins.libs.qa_helper import figlist_to_pngs
        figlist_to_pngs(figout, fig_list)

    # save html

    if html_output:
        if basename_postfix is not None:
            igr_log.warn(
                "For now, no html output is generated if basename-postfix option is used"
            )
        else:
            dirname = config.get_value('HTML_PATH', utdate)
            from igrins.libs.path_info import get_zeropadded_groupname

            objroot = get_zeropadded_groupname(groupname)
            html_save(utdate, dirname, objroot, band, orders_w_solutions,
                      tgt.um, tgt.spec, tgt.sn, i1i2_list)

            if FIX_TELLURIC:
                objroot = get_zeropadded_groupname(groupname) + "A0V"
                html_save(utdate,
                          dirname,
                          objroot,
                          band,
                          orders_w_solutions,
                          tgt.um,
                          a0v.flattened,
                          tgt_spec_cor,
                          i1i2_list,
                          spec_js_name="jj_a0v.js")

    if do_interactive_figure:
        import matplotlib.pyplot as plt
        plt.show()
Ejemplo n.º 14
0
def save_figures(helper, band, obsids, thar_products, new_orders):
    thar_filenames = helper.get_filenames(band, obsids)
    thar_basename = os.path.splitext(os.path.basename(thar_filenames[0]))[0]
    thar_master_obsid = obsids[0]

    if 1:  # make amp and order falt

        ap = get_simple_aperture(helper,
                                 band,
                                 thar_master_obsid,
                                 orders=new_orders)

        from igrins.libs.storage_descriptions import ONED_SPEC_JSON_DESC

        orders = thar_products[ONED_SPEC_JSON_DESC]["orders"]
        order_map = ap.make_order_map()
        #slitpos_map = ap.make_slitpos_map()

        # load flat on products
        #flat_on_params_name = flaton_path.get_secondary_path("flat_on_params")

        #flaton_products = PipelineProducts.load(flat_on_params_name)
        from igrins.libs.storage_descriptions import (FLAT_NORMED_DESC,
                                                      FLAT_MASK_DESC)

        flaton_db_name = helper.igr_path.get_section_filename_base(
            "PRIMARY_CALIB_PATH",
            "flat_on.db",
        )
        flaton_db = ProductDB(flaton_db_name)

        flaton_basename = flaton_db.query(band, thar_master_obsid)

        flaton_products = helper.igr_storage.load(
            [FLAT_NORMED_DESC, FLAT_MASK_DESC], flaton_basename)

        from igrins.libs.process_flat import make_order_flat, check_order_flat
        order_flat_products = make_order_flat(flaton_products, orders,
                                              order_map)

        #fn = thar_path.get_secondary_path("orderflat")
        #order_flat_products.save(fn, masterhdu=hdu)

        hdu = pyfits.open(thar_filenames[0])[0]
        helper.igr_storage.store(order_flat_products,
                                 mastername=flaton_basename,
                                 masterhdu=hdu)

        flat_mask = helper.igr_storage.load1(FLAT_MASK_DESC, flaton_basename)
        order_map2 = ap.make_order_map(mask_top_bottom=True)
        bias_mask = flat_mask.data & (order_map2 > 0)

        pp = PipelineProducts("")
        from igrins.libs.storage_descriptions import BIAS_MASK_DESC
        pp.add(BIAS_MASK_DESC, PipelineImageBase([], bias_mask))

        helper.igr_storage.store(pp, mastername=flaton_basename, masterhdu=hdu)

    if 1:
        fig_list = check_order_flat(order_flat_products)

        from igrins.libs.qa_helper import figlist_to_pngs
        orderflat_figs = helper.igr_path.get_section_filename_base(
            "QA_PATH", "orderflat", "orderflat_" + thar_basename)
        figlist_to_pngs(orderflat_figs, fig_list)
Ejemplo n.º 15
0
def process_distortion_sky_band(utdate, refdate, band, obsids, config):

    from igrins.libs.products import ProductDB, PipelineStorage

    igr_path = IGRINSPath(config, utdate)

    igr_storage = PipelineStorage(igr_path)

    sky_filenames = igr_path.get_filenames(band, obsids)


    sky_basename = os.path.splitext(os.path.basename(sky_filenames[0]))[0]

    master_obsid = obsids[0]


    flaton_db_name = igr_path.get_section_filename_base("PRIMARY_CALIB_PATH",
                                                        "flat_on.db",
                                                        )
    flaton_db = ProductDB(flaton_db_name)

    thar_db_name = igr_path.get_section_filename_base("PRIMARY_CALIB_PATH",
                                                        "thar.db",
                                                        )
    thar_db = ProductDB(thar_db_name)



    from igrins.libs.process_thar import COMBINED_IMAGE_DESC, ONED_SPEC_JSON
    raw_spec_products = igr_storage.load([COMBINED_IMAGE_DESC, ONED_SPEC_JSON],
                                         sky_basename)

    # raw_spec_products = PipelineProducts.load(sky_path.get_secondary_path("raw_spec"))

    SKY_WVLSOL_JSON_DESC = ("PRIMARY_CALIB_PATH", "SKY_", ".wvlsol_v1.json")

    wvlsol_products = igr_storage.load([SKY_WVLSOL_JSON_DESC],
                                       sky_basename)[SKY_WVLSOL_JSON_DESC]

    orders_w_solutions = wvlsol_products["orders"]
    wvl_solutions = wvlsol_products["wvl_sol"]

    ap = load_aperture(igr_storage, band, master_obsid,
                       flaton_db,
                       raw_spec_products[ONED_SPEC_JSON]["orders"],
                       orders_w_solutions)
    #orders_w_solutions = ap.orders


    if 1: # load reference data
        from igrins.libs.master_calib import load_sky_ref_data

        ref_utdate = config.get_value("REFDATE", utdate)

        sky_ref_data = load_sky_ref_data(ref_utdate, band)


        ohlines_db = sky_ref_data["ohlines_db"]
        ref_ohline_indices = sky_ref_data["ohline_indices"]


        orders_w_solutions = wvlsol_products["orders"]
        wvl_solutions = wvlsol_products["wvl_sol"]


    if 0:
        raw_spec_products = PipelineProducts.load(sky_path.get_secondary_path("raw_spec"))



        basename = flaton_db.query(band, sky_master_obsid)
        flaton_path = ProductPath(igr_path, basename)

        aperture_solution_products = PipelineProducts.load(flaton_path.get_secondary_path("aperture_solutions"))

        bottomup_solutions = aperture_solution_products["bottom_up_solutions"]


        basename = thar_db.query(band, sky_master_obsid)
        thar_path = ProductPath(igr_path, basename)
        fn = thar_path.get_secondary_path("median_spectra")
        # thar_products = PipelineProducts.load(fn)

        # basename = sky_db.query(band, sky_master_obsid)
        # sky_path = ProductPath(igr_path, basename)
        fn = sky_path.get_secondary_path("wvlsol_v1")
        wvlsol_products = PipelineProducts.load(fn)

        if 1:
            from igrins.libs.master_calib import load_sky_ref_data

            ref_utdate = "20140316"

            sky_ref_data = load_sky_ref_data(ref_utdate, band)


            ohlines_db = sky_ref_data["ohlines_db"]
            ref_ohline_indices = sky_ref_data["ohline_indices"]


            orders_w_solutions = wvlsol_products["orders"]
            wvl_solutions = wvlsol_products["wvl_sol"]

        if 1: # make aperture

            _o_s = dict(zip(raw_spec_products["orders"], bottomup_solutions))
            ap =  Apertures(orders_w_solutions,
                            [_o_s[o] for o in orders_w_solutions])


    if 1:

        n_slice_one_direction = 2
        n_slice = n_slice_one_direction*2 + 1
        i_center = n_slice_one_direction
        slit_slice = np.linspace(0., 1., n_slice+1)

        slice_center = (slit_slice[i_center], slit_slice[i_center+1])
        slice_up = [(slit_slice[i_center+i], slit_slice[i_center+i+1]) \
                    for i in range(1, n_slice_one_direction+1)]
        slice_down = [(slit_slice[i_center-i-1], slit_slice[i_center-i]) \
                      for i in range(n_slice_one_direction)]

        d = raw_spec_products[COMBINED_IMAGE_DESC].data
        s_center = ap.extract_spectra_v2(d, slice_center[0], slice_center[1])

        s_up, s_down = [], []
        for s1, s2 in slice_up:
            s = ap.extract_spectra_v2(d, s1, s2)
            s_up.append(s)
        for s1, s2 in slice_down:
            s = ap.extract_spectra_v2(d, s1, s2)
            s_down.append(s)


    if 1:
        # now fit

        #ohline_indices = [ref_ohline_indices[o] for o in orders_w_solutions]


        if 0:
            def test_order(oi):
                ax=subplot(111)
                ax.plot(wvl_solutions[oi], s_center[oi])
                #ax.plot(wvl_solutions[oi], raw_spec_products["specs"][oi])
                o = orders[oi]
                line_indices = ref_ohline_indices[o]
                for li in line_indices:
                    um = np.take(ohlines_db.um, li)
                    intensity = np.take(ohlines_db.intensity, li)
                    ax.vlines(um, ymin=0, ymax=-intensity)




        from igrins.libs.reidentify_ohlines import fit_ohlines, fit_ohlines_pixel

        def get_reidentified_lines_OH(orders_w_solutions,
                                      wvl_solutions, s_center):
            ref_pixel_list, reidentified_lines = \
                            fit_ohlines(ohlines_db, ref_ohline_indices,
                                        orders_w_solutions,
                                        wvl_solutions, s_center)

            reidentified_lines_map = dict(zip(orders_w_solutions,
                                              reidentified_lines))
            return reidentified_lines_map, ref_pixel_list

        if band == "H":
            reidentified_lines_map, ref_pixel_list_oh = \
                       get_reidentified_lines_OH(orders_w_solutions,
                                                 wvl_solutions,
                                                 s_center)

            def refit_centroid(s_center,
                               ref_pixel_list=ref_pixel_list_oh):
                centroids = fit_ohlines_pixel(s_center,
                                              ref_pixel_list)
                return centroids

        else: # band K
            reidentified_lines_map, ref_pixel_list_oh = \
                       get_reidentified_lines_OH(orders_w_solutions,
                                                 wvl_solutions,
                                                 s_center)

            import igrins.libs.master_calib as master_calib
            fn = "hitran_bootstrap_K_%s.json" % ref_utdate
            bootstrap_name = master_calib.get_master_calib_abspath(fn)
            import json
            bootstrap = json.load(open(bootstrap_name))

            import igrins.libs.hitran as hitran
            r, ref_pixel_dict_hitrans = hitran.reidentify(wvl_solutions,
                                                          s_center,
                                                          bootstrap)
            # for i, s in r.items():
            #     ss = reidentified_lines_map[int(i)]
            #     ss0 = np.concatenate([ss[0], s["pixel"]])
            #     ss1 = np.concatenate([ss[1], s["wavelength"]])
            #     reidentified_lines_map[int(i)] = (ss0, ss1)

            #reidentified_lines_map, ref_pixel_list

            def refit_centroid(s_center,
                               ref_pixel_list=ref_pixel_list_oh,
                               ref_pixel_dict_hitrans=ref_pixel_dict_hitrans):
                centroids_oh = fit_ohlines_pixel(s_center,
                                                 ref_pixel_list)

                s_dict = dict(zip(orders_w_solutions, s_center))
                centroids_dict_hitrans = hitran.fit_hitrans_pixel(s_dict,
                                                                  ref_pixel_dict_hitrans)
                centroids = []
                for o, c_oh in zip(orders_w_solutions, centroids_oh):
                    if o in centroids_dict_hitrans:
                        c = np.concatenate([c_oh,
                                            centroids_dict_hitrans[o]["pixel"]])
                        centroids.append(c)
                    else:
                        centroids.append(c_oh)

                return centroids

        # reidentified_lines_map = get_reidentified_lines(orders_w_solutions,
        #                                                 wvl_solutions,
        #                                                 s_center)


    if 1:
        # TODO: we should not need this, instead recycle from preivious step.
        fitted_centroid_center = refit_centroid(s_center)
        # fitted_centroid_center = fit_ohlines_pixel(s_center,
        #                                            ref_pixel_list)

        d_shift_up = []
        for s in s_up:
            # TODO: ref_pixel_list_filtered need to be updated with recent fit.
            fitted_centroid = refit_centroid(s)
            # fitted_centroid = fit_ohlines_pixel(s,
            #                                     ref_pixel_list)
            d_shift = [b-a for a, b in zip(fitted_centroid_center,
                                           fitted_centroid)]
            d_shift_up.append(d_shift)

        d_shift_down = []
        for s in s_down:
            # TODO: ref_pixel_list_filtered need to be updated with recent fit.
            fitted_centroid = refit_centroid(s)
            # fitted_centroid = fit_ohlines_pixel(s,
            #                                     ref_pixel_list)
            #fitted_centroid_center,
            d_shift = [b-a for a, b in zip(fitted_centroid_center,
                                           fitted_centroid)]
            d_shift_down.append(d_shift)


    if 1:
        # now fit
        orders = orders_w_solutions

        x_domain = [0, 2048]
        y_domain = [orders[0]-2, orders[-1]+2]


        xl = np.concatenate(fitted_centroid_center)

        yl_ = [o + np.zeros_like(x_) for o, x_ in zip(orders,
                                                      fitted_centroid_center)]
        yl = np.concatenate(yl_)

        from igrins.libs.ecfit import fit_2dspec, check_fit_simple

        zl_list = [np.concatenate(d_) for d_ \
                   in d_shift_down[::-1] + d_shift_up]

        pm_list = []
        for zl in zl_list:
            p, m = fit_2dspec(xl, yl, zl,
                              x_degree=1, y_degree=1,
                              x_domain=x_domain, y_domain=y_domain)
            pm_list.append((p,m))

        zz_std_list = []
        for zl, (p, m)  in zip(zl_list, pm_list):
            z_m = p(xl[m], yl[m])
            zz = z_m - zl[m]
            zz_std_list.append(zz.std())

        fig_list = []
        from matplotlib.figure import Figure
        for zl, (p, m)  in zip(zl_list, pm_list):
            fig = Figure()
            check_fit_simple(fig, xl[m], yl[m], zl[m], p, orders)
            fig_list.append(fig)


    if 1:
        xi = np.linspace(0, 2048, 128+1)
        from astropy.modeling import fitting
        from astropy.modeling.polynomial import Chebyshev2D
        x_domain = [0, 2048]
        y_domain = [0., 1.]

        p2_list = []
        for o in orders:
            oi = np.zeros_like(xi) + o
            shift_list = []
            for p,m in pm_list[:n_slice_one_direction]:
                shift_list.append(p(xi, oi))

            shift_list.append(np.zeros_like(xi))

            for p,m in pm_list[n_slice_one_direction:]:
                shift_list.append(p(xi, oi))


            p_init = Chebyshev2D(x_degree=1, y_degree=2,
                                 x_domain=x_domain, y_domain=y_domain)
            f = fitting.LinearLSQFitter()

            yi = 0.5*(slit_slice[:-1] + slit_slice[1:])
            xl, yl = np.meshgrid(xi, yi)
            zl = np.array(shift_list)
            p = f(p_init, xl, yl, zl)

            p2_list.append(p)

    if 1:
        p2_dict = dict(zip(orders, p2_list))

        order_map = ap.make_order_map()
        slitpos_map = ap.make_slitpos_map()

        slitoffset_map = np.empty_like(slitpos_map)
        slitoffset_map.fill(np.nan)
        for o in ap.orders:
            xi = np.arange(0, 2048)
            xl, yl = np.meshgrid(xi, xi)
            msk = order_map == o
            slitoffset_map[msk] = p2_dict[o](xl[msk], slitpos_map[msk])

        import astropy.io.fits as pyfits
        #fn = sky_path.get_secondary_path("slitoffset_map.fits")
        #pyfits.PrimaryHDU(data=slitoffset_map).writeto(fn, clobber=True)

        SLITOFFSET_FITS_DESC = ("PRIMARY_CALIB_PATH", "SKY_", ".slitoffset_map.fits")
        from igrins.libs.products import PipelineImageBase, PipelineProducts
        distortion_products = PipelineProducts("Distortion map")
        distortion_products.add(SLITOFFSET_FITS_DESC,
                                PipelineImageBase([],
                                              slitoffset_map))

        igr_storage.store(distortion_products,
                          mastername=sky_filenames[0],
                          masterhdu=None)


        from igrins.libs.qa_helper import figlist_to_pngs
        sky_figs = igr_path.get_section_filename_base("QA_PATH",
                                                      "oh_distortion",
                                                      "oh_distortion_dir")
        print fig_list
        figlist_to_pngs(sky_figs, fig_list)


    if 0:
        # test
        x = np.arange(2048, dtype="d")
        oi = 10
        o = orders[oi]

        yi = 0.5*(slit_slice[:-1] + slit_slice[1:])

        ax1 = subplot(211)
        s1 = s_up[-1][oi]
        s2 = s_down[-1][oi]

        ax1.plot(x, s1)
        ax1.plot(x, s2)

        ax2 = subplot(212, sharex=ax1, sharey=ax1)
        dx1 = p2_dict[o](x, yi[-1]+np.zeros_like(x))
        ax2.plot(x-dx1, s1)

        dx2 = p2_dict[o](x, yi[0]+np.zeros_like(x))
        ax2.plot(x-dx2, s2)
Ejemplo n.º 16
0
def process_distortion_sky_band(utdate, refdate, band, obsids, config):

    from igrins.libs.products import ProductDB, PipelineStorage

    igr_path = IGRINSPath(config, utdate)

    igr_storage = PipelineStorage(igr_path)

    sky_filenames = igr_path.get_filenames(band, obsids)

    sky_basename = os.path.splitext(os.path.basename(sky_filenames[0]))[0]

    master_obsid = obsids[0]

    flaton_db_name = igr_path.get_section_filename_base(
        "PRIMARY_CALIB_PATH",
        "flat_on.db",
    )
    flaton_db = ProductDB(flaton_db_name)

    thar_db_name = igr_path.get_section_filename_base(
        "PRIMARY_CALIB_PATH",
        "thar.db",
    )
    thar_db = ProductDB(thar_db_name)

    from igrins.libs.process_thar import COMBINED_IMAGE_DESC, ONED_SPEC_JSON
    raw_spec_products = igr_storage.load([COMBINED_IMAGE_DESC, ONED_SPEC_JSON],
                                         sky_basename)

    # raw_spec_products = PipelineProducts.load(sky_path.get_secondary_path("raw_spec"))

    SKY_WVLSOL_JSON_DESC = ("PRIMARY_CALIB_PATH", "SKY_", ".wvlsol_v1.json")

    wvlsol_products = igr_storage.load([SKY_WVLSOL_JSON_DESC],
                                       sky_basename)[SKY_WVLSOL_JSON_DESC]

    orders_w_solutions = wvlsol_products["orders"]
    wvl_solutions = wvlsol_products["wvl_sol"]

    ap = load_aperture(igr_storage, band, master_obsid, flaton_db,
                       raw_spec_products[ONED_SPEC_JSON]["orders"],
                       orders_w_solutions)
    #orders_w_solutions = ap.orders

    if 1:  # load reference data
        from igrins.libs.master_calib import load_sky_ref_data

        ref_utdate = config.get_value("REFDATE", utdate)

        sky_ref_data = load_sky_ref_data(ref_utdate, band)

        ohlines_db = sky_ref_data["ohlines_db"]
        ref_ohline_indices = sky_ref_data["ohline_indices"]

        orders_w_solutions = wvlsol_products["orders"]
        wvl_solutions = wvlsol_products["wvl_sol"]

    if 0:
        raw_spec_products = PipelineProducts.load(
            sky_path.get_secondary_path("raw_spec"))

        basename = flaton_db.query(band, sky_master_obsid)
        flaton_path = ProductPath(igr_path, basename)

        aperture_solution_products = PipelineProducts.load(
            flaton_path.get_secondary_path("aperture_solutions"))

        bottomup_solutions = aperture_solution_products["bottom_up_solutions"]

        basename = thar_db.query(band, sky_master_obsid)
        thar_path = ProductPath(igr_path, basename)
        fn = thar_path.get_secondary_path("median_spectra")
        # thar_products = PipelineProducts.load(fn)

        # basename = sky_db.query(band, sky_master_obsid)
        # sky_path = ProductPath(igr_path, basename)
        fn = sky_path.get_secondary_path("wvlsol_v1")
        wvlsol_products = PipelineProducts.load(fn)

        if 1:
            from igrins.libs.master_calib import load_sky_ref_data

            ref_utdate = "20140316"

            sky_ref_data = load_sky_ref_data(ref_utdate, band)

            ohlines_db = sky_ref_data["ohlines_db"]
            ref_ohline_indices = sky_ref_data["ohline_indices"]

            orders_w_solutions = wvlsol_products["orders"]
            wvl_solutions = wvlsol_products["wvl_sol"]

        if 1:  # make aperture

            _o_s = dict(zip(raw_spec_products["orders"], bottomup_solutions))
            ap = Apertures(orders_w_solutions,
                           [_o_s[o] for o in orders_w_solutions])

    if 1:

        n_slice_one_direction = 2
        n_slice = n_slice_one_direction * 2 + 1
        i_center = n_slice_one_direction
        slit_slice = np.linspace(0., 1., n_slice + 1)

        slice_center = (slit_slice[i_center], slit_slice[i_center + 1])
        slice_up = [(slit_slice[i_center+i], slit_slice[i_center+i+1]) \
                    for i in range(1, n_slice_one_direction+1)]
        slice_down = [(slit_slice[i_center-i-1], slit_slice[i_center-i]) \
                      for i in range(n_slice_one_direction)]

        d = raw_spec_products[COMBINED_IMAGE_DESC].data
        s_center = ap.extract_spectra_v2(d, slice_center[0], slice_center[1])

        s_up, s_down = [], []
        for s1, s2 in slice_up:
            s = ap.extract_spectra_v2(d, s1, s2)
            s_up.append(s)
        for s1, s2 in slice_down:
            s = ap.extract_spectra_v2(d, s1, s2)
            s_down.append(s)

    if 1:
        # now fit

        #ohline_indices = [ref_ohline_indices[o] for o in orders_w_solutions]

        if 0:

            def test_order(oi):
                ax = subplot(111)
                ax.plot(wvl_solutions[oi], s_center[oi])
                #ax.plot(wvl_solutions[oi], raw_spec_products["specs"][oi])
                o = orders[oi]
                line_indices = ref_ohline_indices[o]
                for li in line_indices:
                    um = np.take(ohlines_db.um, li)
                    intensity = np.take(ohlines_db.intensity, li)
                    ax.vlines(um, ymin=0, ymax=-intensity)

        from igrins.libs.reidentify_ohlines import fit_ohlines, fit_ohlines_pixel

        def get_reidentified_lines_OH(orders_w_solutions, wvl_solutions,
                                      s_center):
            ref_pixel_list, reidentified_lines = \
                            fit_ohlines(ohlines_db, ref_ohline_indices,
                                        orders_w_solutions,
                                        wvl_solutions, s_center)

            reidentified_lines_map = dict(
                zip(orders_w_solutions, reidentified_lines))
            return reidentified_lines_map, ref_pixel_list

        if band == "H":
            reidentified_lines_map, ref_pixel_list_oh = \
                       get_reidentified_lines_OH(orders_w_solutions,
                                                 wvl_solutions,
                                                 s_center)

            def refit_centroid(s_center, ref_pixel_list=ref_pixel_list_oh):
                centroids = fit_ohlines_pixel(s_center, ref_pixel_list)
                return centroids

        else:  # band K
            reidentified_lines_map, ref_pixel_list_oh = \
                       get_reidentified_lines_OH(orders_w_solutions,
                                                 wvl_solutions,
                                                 s_center)

            import igrins.libs.master_calib as master_calib
            fn = "hitran_bootstrap_K_%s.json" % ref_utdate
            bootstrap_name = master_calib.get_master_calib_abspath(fn)
            import json
            bootstrap = json.load(open(bootstrap_name))

            import igrins.libs.hitran as hitran
            r, ref_pixel_dict_hitrans = hitran.reidentify(
                wvl_solutions, s_center, bootstrap)

            # for i, s in r.items():
            #     ss = reidentified_lines_map[int(i)]
            #     ss0 = np.concatenate([ss[0], s["pixel"]])
            #     ss1 = np.concatenate([ss[1], s["wavelength"]])
            #     reidentified_lines_map[int(i)] = (ss0, ss1)

            #reidentified_lines_map, ref_pixel_list

            def refit_centroid(s_center,
                               ref_pixel_list=ref_pixel_list_oh,
                               ref_pixel_dict_hitrans=ref_pixel_dict_hitrans):
                centroids_oh = fit_ohlines_pixel(s_center, ref_pixel_list)

                s_dict = dict(zip(orders_w_solutions, s_center))
                centroids_dict_hitrans = hitran.fit_hitrans_pixel(
                    s_dict, ref_pixel_dict_hitrans)
                centroids = []
                for o, c_oh in zip(orders_w_solutions, centroids_oh):
                    if o in centroids_dict_hitrans:
                        c = np.concatenate(
                            [c_oh, centroids_dict_hitrans[o]["pixel"]])
                        centroids.append(c)
                    else:
                        centroids.append(c_oh)

                return centroids

        # reidentified_lines_map = get_reidentified_lines(orders_w_solutions,
        #                                                 wvl_solutions,
        #                                                 s_center)

    if 1:
        # TODO: we should not need this, instead recycle from preivious step.
        fitted_centroid_center = refit_centroid(s_center)
        # fitted_centroid_center = fit_ohlines_pixel(s_center,
        #                                            ref_pixel_list)

        d_shift_up = []
        for s in s_up:
            # TODO: ref_pixel_list_filtered need to be updated with recent fit.
            fitted_centroid = refit_centroid(s)
            # fitted_centroid = fit_ohlines_pixel(s,
            #                                     ref_pixel_list)
            d_shift = [
                b - a for a, b in zip(fitted_centroid_center, fitted_centroid)
            ]
            d_shift_up.append(d_shift)

        d_shift_down = []
        for s in s_down:
            # TODO: ref_pixel_list_filtered need to be updated with recent fit.
            fitted_centroid = refit_centroid(s)
            # fitted_centroid = fit_ohlines_pixel(s,
            #                                     ref_pixel_list)
            #fitted_centroid_center,
            d_shift = [
                b - a for a, b in zip(fitted_centroid_center, fitted_centroid)
            ]
            d_shift_down.append(d_shift)

    if 1:
        # now fit
        orders = orders_w_solutions

        x_domain = [0, 2048]
        y_domain = [orders[0] - 2, orders[-1] + 2]

        xl = np.concatenate(fitted_centroid_center)

        yl_ = [
            o + np.zeros_like(x_)
            for o, x_ in zip(orders, fitted_centroid_center)
        ]
        yl = np.concatenate(yl_)

        from igrins.libs.ecfit import fit_2dspec, check_fit_simple

        zl_list = [np.concatenate(d_) for d_ \
                   in d_shift_down[::-1] + d_shift_up]

        pm_list = []
        for zl in zl_list:
            p, m = fit_2dspec(xl,
                              yl,
                              zl,
                              x_degree=1,
                              y_degree=1,
                              x_domain=x_domain,
                              y_domain=y_domain)
            pm_list.append((p, m))

        zz_std_list = []
        for zl, (p, m) in zip(zl_list, pm_list):
            z_m = p(xl[m], yl[m])
            zz = z_m - zl[m]
            zz_std_list.append(zz.std())

        fig_list = []
        from matplotlib.figure import Figure
        for zl, (p, m) in zip(zl_list, pm_list):
            fig = Figure()
            check_fit_simple(fig, xl[m], yl[m], zl[m], p, orders)
            fig_list.append(fig)

    if 1:
        xi = np.linspace(0, 2048, 128 + 1)
        from astropy.modeling import fitting
        from astropy.modeling.polynomial import Chebyshev2D
        x_domain = [0, 2048]
        y_domain = [0., 1.]

        p2_list = []
        for o in orders:
            oi = np.zeros_like(xi) + o
            shift_list = []
            for p, m in pm_list[:n_slice_one_direction]:
                shift_list.append(p(xi, oi))

            shift_list.append(np.zeros_like(xi))

            for p, m in pm_list[n_slice_one_direction:]:
                shift_list.append(p(xi, oi))

            p_init = Chebyshev2D(x_degree=1,
                                 y_degree=2,
                                 x_domain=x_domain,
                                 y_domain=y_domain)
            f = fitting.LinearLSQFitter()

            yi = 0.5 * (slit_slice[:-1] + slit_slice[1:])
            xl, yl = np.meshgrid(xi, yi)
            zl = np.array(shift_list)
            p = f(p_init, xl, yl, zl)

            p2_list.append(p)

    if 1:
        p2_dict = dict(zip(orders, p2_list))

        order_map = ap.make_order_map()
        slitpos_map = ap.make_slitpos_map()

        slitoffset_map = np.empty_like(slitpos_map)
        slitoffset_map.fill(np.nan)
        for o in ap.orders:
            xi = np.arange(0, 2048)
            xl, yl = np.meshgrid(xi, xi)
            msk = order_map == o
            slitoffset_map[msk] = p2_dict[o](xl[msk], slitpos_map[msk])

        import astropy.io.fits as pyfits
        #fn = sky_path.get_secondary_path("slitoffset_map.fits")
        #pyfits.PrimaryHDU(data=slitoffset_map).writeto(fn, clobber=True)

        SLITOFFSET_FITS_DESC = ("PRIMARY_CALIB_PATH", "SKY_",
                                ".slitoffset_map.fits")
        from igrins.libs.products import PipelineImageBase, PipelineProducts
        distortion_products = PipelineProducts("Distortion map")
        distortion_products.add(SLITOFFSET_FITS_DESC,
                                PipelineImageBase([], slitoffset_map))

        igr_storage.store(distortion_products,
                          mastername=sky_filenames[0],
                          masterhdu=None)

        from igrins.libs.qa_helper import figlist_to_pngs
        sky_figs = igr_path.get_section_filename_base("QA_PATH",
                                                      "oh_distortion",
                                                      "oh_distortion_dir")
        print fig_list
        figlist_to_pngs(sky_figs, fig_list)

    if 0:
        # test
        x = np.arange(2048, dtype="d")
        oi = 10
        o = orders[oi]

        yi = 0.5 * (slit_slice[:-1] + slit_slice[1:])

        ax1 = subplot(211)
        s1 = s_up[-1][oi]
        s2 = s_down[-1][oi]

        ax1.plot(x, s1)
        ax1.plot(x, s2)

        ax2 = subplot(212, sharex=ax1, sharey=ax1)
        dx1 = p2_dict[o](x, yi[-1] + np.zeros_like(x))
        ax2.plot(x - dx1, s1)

        dx2 = p2_dict[o](x, yi[0] + np.zeros_like(x))
        ax2.plot(x - dx2, s2)