Пример #1
0
def make_combined_image_sky(helper, band, obsids):
    """
    simple median combine with destripping. Suitable for sky.
    """
    filenames, basename, master_obsid = helper.get_base_info(band, obsids)

    hdu_list = [pyfits.open(fn)[0] for fn in filenames]
    _data = stsci_median([hdu.data for hdu in hdu_list])

    from get_destripe_mask import get_destripe_mask
    destripe_mask = get_destripe_mask(helper, band, obsids)

    from destriper import destriper
    from estimate_sky import estimate_background, get_interpolated_cubic

    xc, yc, v, std = estimate_background(_data, destripe_mask,
                                         di=48, min_pixel=40)
    nx = ny = 2048
    ZI3 = get_interpolated_cubic(nx, ny, xc, yc, v)
    ZI3 = np.nan_to_num(ZI3)

    d = _data - ZI3
    mask=destripe_mask | ~np.isfinite(d)
    stripes = destriper.get_stripe_pattern64(d, mask=mask,
                                             concatenate=True,
                                             remove_vertical=False)

    return d - stripes
Пример #2
0
    def get_stripe_pattern64(self,
                             d,
                             mask=None,
                             concatenate=True,
                             remove_vertical=True):
        """
        if concatenate is True, return 2048x2048 array.
        Otherwise, 128x2048 array.
        """
        dy = 64
        n_dy = 2048 / dy
        dy_slices = [slice(iy * dy, (iy + 1) * dy) for iy in range(2048 / dy)]
        from itertools import cycle
        if mask is not None:
            if remove_vertical:
                d = self._remove_vertical_smooth_bg(d, mask=mask)
            alt_sign = cycle([1, -1])
            dd = [d[sl][::next(alt_sign)] for sl in dy_slices]
            alt_sign = cycle([1, -1])
            msk = [mask[sl][::next(alt_sign)] for sl in dy_slices]
            ddm = stsci_median(dd, badmasks=msk)
            #dd1 = np.ma.array(dd, mask=msk)
            #ddm = np.ma.median(dd1, axis=0)
        else:
            alt_sign = cycle([1, -1])
            dd = [d[sl][::next(alt_sign)] for sl in dy_slices]
            ddm = np.median(dd, axis=0)

        if concatenate:
            return np.concatenate([ddm, ddm[::-1]] * (n_dy / 2))
        else:
            return ddm
Пример #3
0
    def get_stripe_pattern64(self, d, mask=None,
                             concatenate=True,
                             remove_vertical=True):
        """
        if concatenate is True, return 2048x2048 array.
        Otherwise, 128x2048 array.
        """
        dy = 64
        n_dy = 2048/dy
        dy_slices = [slice(iy*dy, (iy+1)*dy) for iy in range(2048/dy)]
        from itertools import cycle
        if mask is not None:
            if remove_vertical:
                d = self._remove_vertical_smooth_bg(d, mask=mask)
            alt_sign = cycle([1, -1])
            dd = [d[sl][::next(alt_sign)] for sl in dy_slices]
            alt_sign = cycle([1, -1])
            msk = [mask[sl][::next(alt_sign)] for sl in dy_slices]
            ddm = stsci_median(dd, badmasks=msk)
            #dd1 = np.ma.array(dd, mask=msk)
            #ddm = np.ma.median(dd1, axis=0)
        else:
            alt_sign = cycle([1, -1])
            dd = [d[sl][::next(alt_sign)] for sl in dy_slices]
            ddm = np.median(dd, axis=0)


        if concatenate:
            return np.concatenate([ddm, ddm[::-1]] * (n_dy/2))
        else:
            return ddm
Пример #4
0
def get_combined_image(hdus): #, destripe=True):
    # destripe=True):

    data_list = [hdu.data for hdu in hdus]

    from stsci_helper import stsci_median
    im = stsci_median(data_list)

    return im
Пример #5
0
def get_combined_image(hdus):  #, destripe=True):
    # destripe=True):

    data_list = [hdu.data for hdu in hdus]

    from stsci_helper import stsci_median
    im = stsci_median(data_list)

    return im
Пример #6
0
    def extract_spectra_from_ordermap(self, data, order_map):
        slices = ni.find_objects(order_map)
        s_list = []
        for o in self.orders:
            sl = slices[o - 1]
            msk = (order_map[sl] != o)
            s = stsci_median(data[sl], badmasks=msk)

            s_list.append(s)

        return s_list
Пример #7
0
    def extract_spectra_from_ordermap(self, data, order_map):
        slices = ni.find_objects(order_map)
        s_list = []
        for o in self.orders:
            sl = slices[o - 1]
            msk = (order_map[sl] != o)
            s = stsci_median(data[sl], badmasks=msk)

            s_list.append(s)

        return s_list
Пример #8
0
def make_combined_image_thar(helper, band, obsids):
    """
    simple median combine with destripping. Suitable for sky.
    """
    filenames, basename, master_obsid = helper.get_base_info(band, obsids)

    hdu_list = [pyfits.open(fn)[0] for fn in filenames]
    _data = stsci_median([hdu.data for hdu in hdu_list])

    from destriper import destriper
    data = destriper.get_destriped(_data)

    return data
Пример #9
0
def make_combined_image_sky_deprecated(helper, band, obsids):
    """
    simple median combine with destripping. Suitable for sky.
    """
    filenames, basename, master_obsid = helper.get_base_info(band, obsids)

    hdu_list = [pyfits.open(fn)[0] for fn in filenames]
    _data = stsci_median([hdu.data for hdu in hdu_list])

    from get_destripe_mask import get_destripe_mask
    destripe_mask = get_destripe_mask(helper, band, obsids)

    data = destripe_sky(_data, destripe_mask)

    return data
Пример #10
0
def get_1d_median_specs(fits_names, ap):
    #hdu_list = [pyfits.open(fn)[0] for fn in fits_names]
    from load_fits import load_fits_data
    hdu_list = [load_fits_data(fn) for fn in fits_names]
    _data = stsci_median([hdu.data for hdu in hdu_list])

    from destriper import destriper
    data = destriper.get_destriped(_data)

    s = ap.extract_spectra_v2(data)

    from storage_descriptions import (COMBINED_IMAGE_DESC, ONED_SPEC_JSON_DESC)

    r = PipelineProducts("1d median specs")
    r.add(COMBINED_IMAGE_DESC, PipelineImageBase([], data))
    r.add(ONED_SPEC_JSON_DESC, PipelineDict(orders=ap.orders, specs=s))

    return r
Пример #11
0
    def make_flatoff_hotpixmap(self, sigma_clip1=100, sigma_clip2=10,
                               medfilter_size=None,
                               destripe=True):

        flat_off_cards = []

        flat_off = stsci_median(self.data_list)

        if destripe:
            flat_offs = destriper.get_destriped(flat_off)
            flat_off_cards.append(Card(("HISTORY", "IGR: image destriped.")))

        hotpix_mask = bp.badpixel_mask(flat_offs,
                                       sigma_clip1=sigma_clip1,
                                       sigma_clip2=sigma_clip2,
                                       medfilter_size=medfilter_size)

        bg_std = flat_offs[~hotpix_mask].std()


        flat_off_cards.append(Card(("BG_STD", bg_std,
                                    "IGR: stddev of combined flat")))
        flat_off_image = PipelineImageBase(flat_off_cards,
                                           flat_offs)

        hotpix_mask_image = PipelineImageBase([],
                                              hotpix_mask)


        from storage_descriptions import (FLAT_OFF_DESC,
                                          FLATOFF_JSON_DESC,
                                          HOTPIX_MASK_DESC)

        r = PipelineProducts("flat off products")
        r.add(FLAT_OFF_DESC, flat_off_image)

        r.add(HOTPIX_MASK_DESC,
              hotpix_mask_image)

        r.add(FLATOFF_JSON_DESC,
              PipelineDict(bg_std=bg_std))

        return r
Пример #12
0
def get_1d_median_specs(fits_names, ap):
    hdu_list = [pyfits.open(fn)[0] for fn in fits_names]
    _data = stsci_median([hdu.data for hdu in hdu_list])

    from destriper import destriper
    data = destriper.get_destriped(_data)

    s = ap.extract_spectra_v2(data)

    from storage_descriptions import (COMBINED_IMAGE_DESC,
                                      ONED_SPEC_JSON_DESC)

    r = PipelineProducts("1d median specs")
    r.add(COMBINED_IMAGE_DESC,
          PipelineImage([], data))
    r.add(ONED_SPEC_JSON_DESC,
          PipelineDict(orders=ap.orders,
                       specs=s))

    return r
Пример #13
0
    def make_flatoff_hotpixmap(self,
                               sigma_clip1=100,
                               sigma_clip2=10,
                               medfilter_size=None,
                               destripe=True):

        flat_off_cards = []

        flat_off = stsci_median(self.data_list)

        if destripe:
            flat_offs = destriper.get_destriped(flat_off)
            flat_off_cards.append(Card(("HISTORY", "IGR: image destriped.")))

        hotpix_mask = bp.badpixel_mask(flat_offs,
                                       sigma_clip1=sigma_clip1,
                                       sigma_clip2=sigma_clip2,
                                       medfilter_size=medfilter_size)

        bg_std = flat_offs[~hotpix_mask].std()

        flat_off_cards.append(
            Card(("BG_STD", bg_std, "IGR: stddev of combined flat")))
        flat_off_image = PipelineImageBase(flat_off_cards, flat_offs)

        hotpix_mask_image = PipelineImageBase([], hotpix_mask)

        from storage_descriptions import (FLAT_OFF_DESC, FLATOFF_JSON_DESC,
                                          HOTPIX_MASK_DESC)

        r = PipelineProducts("flat off products")
        r.add(FLAT_OFF_DESC, flat_off_image)

        r.add(HOTPIX_MASK_DESC, hotpix_mask_image)

        r.add(FLATOFF_JSON_DESC, PipelineDict(bg_std=bg_std))

        return r
Пример #14
0
def mymain():
    from pipeline_jjlee import IGRINSLog, Destriper

    if 1:
        log_20140316 = dict(flat_off=range(2, 4),
                            flat_on=range(4, 7),
                            thar=range(1, 2),
                            HD3417=[15, 16])

        igrins_log = IGRINSLog("20140316", log_20140316)
    else:
        log_20140525 = dict(flat_off=range(64, 74),
                            flat_on=range(74, 84),
                            thar=range(3, 8))

        igrins_log = IGRINSLog("20140525", log_20140525)

    band = "H"

    import numpy as np
    destriper = Destriper()

    hdu_list = igrins_log.get_cal_hdus(band, "flat_off")
    flat_offs = [destriper.get_destriped(hdu.data) for hdu in hdu_list]

    hdu_list = igrins_log.get_cal_hdus(band, "flat_on")
    flat_ons = [hdu.data for hdu in hdu_list]

    #flat_off = np.median(flat_offs, axis=0)

    #import trace_flat
    #reload(trace_flat)
    #process_flat =  trace_flat.process_flat

    r = process_flat(ondata_list=flat_ons, offdata_list=flat_offs)

    flat_normed = r["flat_normed"].copy()
    flat_normed[r["flat_bpix_mask"]] = np.nan

    starting_order = 5
    orders = range(starting_order,
                   len(r["bottomup_solutions"]) + starting_order)
    order_map = make_order_map(flat_normed.shape,
                               r["bottomup_solutions"],
                               orders=orders,
                               mask_top_bottom=False)

    # get dead pixel mask
    flat_smoothed = ni.median_filter(flat_normed, [1, 9])
    flat_smoothed[order_map == 0] = np.nan
    flat_ratio = flat_normed / flat_smoothed
    flat_mask = r["flat_mask"]

    refpixel_mask = np.ones(flat_mask.shape, bool)
    refpixel_mask[4:-4, 4:-4] = False

    dead_pixel_mask = (flat_ratio < 0.3) & flat_mask & (~refpixel_mask)

    flat_normed[dead_pixel_mask] = np.nan

    import scipy.ndimage as ni
    slices = ni.find_objects(order_map)

    mean_order_spec = []
    mask_list = []
    for o in orders:
        sl = slices[o - 1]
        d_sl = flat_normed[sl].copy()
        d_sl[order_map[sl] != o] = np.nan

        f_sl = flat_mask[sl].copy()
        f_sl[order_map[sl] != o] = np.nan
        ff = np.nanmean(f_sl, axis=0)
        mask_list.append(ff)

        mmm = order_map[sl] == o
        ss = [
            np.nanmean(d_sl[3:-3][:, i][mmm[:, i][3:-3]]) for i in range(2048)
        ]
        mean_order_spec.append(ss)

    s_list = [get_smoothed_order_spec(s) for s in mean_order_spec]
    i1i2_list = [get_order_boundary_indices(s, s0) \
                 for s, s0 in zip(mean_order_spec, s_list)]
    p_list = [get_order_flat1d(s, i1, i2) for s, (i1, i2) \
              in zip(s_list, i1i2_list)]

    fig_list, ax_list = prepare_order_trace_plot(s_list)
    x = np.arange(2048)
    for s, i1i2, ax in zip(mean_order_spec, i1i2_list, ax_list):
        check_order_trace1(ax, x, s, i1i2)

    for s, p, ax in zip(mean_order_spec, p_list, ax_list):
        check_order_trace2(ax, x, p)

    # make flat
    x = np.arange(len(s))
    flat_im = np.empty(flat_normed.shape, "d")
    flat_im.fill(np.nan)

    for o, p in zip(orders, p_list):
        sl = slices[o - 1]
        d_sl = flat_normed[sl].copy()
        msk = (order_map[sl] == o)
        d_sl[~msk] = np.nan

        flat_im[sl][msk] = (d_sl / p(x))[msk]

    if 0:
        hdu_list = igrins_log.get_cal_hdus(band, "HD3417")
        hd_list = [destriper.get_destriped(hdu.data) for hdu in hdu_list]
        from stsci_helper import stsci_median
        hd_spec = stsci_median(hd_list)
Пример #15
0
def process_flat(ondata_list, offdata_list):

    from stsci_helper import stsci_median

    return_object = {}

    flat_on = stsci_median(ondata_list)
    flat_off = stsci_median(offdata_list)

    bpix_mask = bp.badpixel_mask(flat_off, sigma_clip1=100)

    bg_std = flat_off[~bpix_mask].std()

    flat_on_off = flat_on - flat_off

    norm_factor = get_flat_normalization(flat_on_off, bg_std, bpix_mask)

    flat_norm = flat_on_off / norm_factor
    bg_std_norm = bg_std / norm_factor

    flat_bpix = flat_norm.astype("d", copy=True)
    flat_bpix[bpix_mask] = np.nan

    flat_mask = get_flat_mask(flat_bpix, bg_std_norm, sigma=5)

    return_object["flat_on_off"] = flat_on_off
    return_object["flat_norm_factor"] = norm_factor
    return_object["flat_normed"] = flat_norm
    return_object["flat_bpix_mask"] = bpix_mask
    return_object["bg_std"] = bg_std
    return_object["bg_std_normed"] = bg_std_norm
    return_object["flat_mask"] = flat_mask

    flat_deriv_ = get_y_derivativemap(flat_norm,
                                      flat_bpix,
                                      bg_std_norm,
                                      max_sep_order=150,
                                      pad=50,
                                      flat_mask=flat_mask)

    flat_deriv, flat_deriv_pos_msk, flat_deriv_neg_msk = flat_deriv_[
        "data"], flat_deriv_["pos_mask"], flat_deriv_["neg_mask"]

    return_object["flat_deriv"] = flat_deriv
    return_object["flat_deriv_pos_mask"] = flat_deriv_pos_msk
    return_object["flat_deriv_neg_mask"] = flat_deriv_neg_msk

    ny, nx = flat_deriv.shape
    cent_bottom_list = identify_horizontal_line(flat_deriv,
                                                flat_deriv_pos_msk,
                                                pad=50,
                                                bg_std=bg_std_norm)

    cent_up_list = identify_horizontal_line(-flat_deriv,
                                            flat_deriv_neg_msk,
                                            pad=50,
                                            bg_std=bg_std_norm)

    if 1:  # chevyshev
        _ = trace_centroids_chevyshev(cent_bottom_list,
                                      cent_up_list,
                                      domain=[0, 2048],
                                      ref_x=nx / 2)

    if 0:
        order = 5
        func_fitter = get_line_fiiter(order)

        _ = trace_centroids(cent_bottom_list,
                            cent_up_list,
                            func_fitter=func_fitter,
                            ref_x=nx / 2)

    bottom_up_solutions, centroid_bottom_up_list = _

    if 0:
        plot_solutions(flat_norm, centroid_bottom_up_list, bottom_up_solutions)

    return_object["cent_up_list"] = cent_up_list
    return_object["cent_bottom_list"] = cent_bottom_list
    return_object["bottomup_solutions"] = bottom_up_solutions
    return_object["bottomup_centroids"] = centroid_bottom_up_list

    return return_object
Пример #16
0
    def make_flaton_deadpixmap(self,
                               flatoff_product,
                               deadpix_mask_old=None,
                               flat_mask_sigma=5.,
                               deadpix_thresh=0.6,
                               smooth_size=9):

        # load flat off data

        # flat_off = flatoff_product["flat_off"]
        # bg_std = flatoff_product["bg_std"]
        # hotpix_mask = flatoff_product["hotpix_mask"]

        from storage_descriptions import (FLAT_OFF_DESC, FLATOFF_JSON_DESC,
                                          HOTPIX_MASK_DESC)

        flat_off = flatoff_product[FLAT_OFF_DESC].data
        bg_std = flatoff_product[FLATOFF_JSON_DESC]["bg_std"]
        hotpix_mask = flatoff_product[HOTPIX_MASK_DESC].data

        flat_on = stsci_median(self.data_list)
        flat_on_off = flat_on - flat_off

        # normalize it
        norm_factor = get_flat_normalization(flat_on_off, bg_std, hotpix_mask)

        flat_normed = flat_on_off / norm_factor
        flat_std_normed = ni.median_filter(np.std(self.data_list, axis=0) /
                                           norm_factor,
                                           size=(3, 3))
        bg_std_norm = bg_std / norm_factor

        # mask out bpix
        flat_bpixed = flat_normed.astype("d")  # by default, astype
        # returns new array.
        flat_bpixed[hotpix_mask] = np.nan

        flat_mask = get_flat_mask_auto(flat_bpixed)
        # flat_mask = get_flat_mask(flat_bpixed, bg_std_norm,
        #                           sigma=flat_mask_sigma)

        # get dead pixel mask
        flat_smoothed = ni.median_filter(flat_normed,
                                         [smooth_size, smooth_size])
        #flat_smoothed[order_map==0] = np.nan
        flat_ratio = flat_normed / flat_smoothed
        flat_std_mask = (flat_smoothed - flat_normed) > 5 * flat_std_normed

        refpixel_mask = np.ones(flat_mask.shape, bool)
        # mask out outer boundaries
        refpixel_mask[4:-4, 4:-4] = False

        deadpix_mask = (flat_ratio < deadpix_thresh
                        ) & flat_std_mask & flat_mask & (~refpixel_mask)

        if deadpix_mask_old is not None:
            deadpix_mask = deadpix_mask | deadpix_mask_old

        flat_bpixed[deadpix_mask] = np.nan

        from storage_descriptions import (FLAT_NORMED_DESC, FLAT_BPIXED_DESC,
                                          FLAT_MASK_DESC, DEADPIX_MASK_DESC,
                                          FLATON_JSON_DESC)

        r = PipelineProducts("flat on products")

        r.add(FLAT_NORMED_DESC, PipelineImageBase([], flat_normed))
        r.add(FLAT_BPIXED_DESC, PipelineImageBase([], flat_bpixed))
        r.add(FLAT_MASK_DESC, PipelineImageBase([], flat_mask))
        r.add(DEADPIX_MASK_DESC, PipelineImageBase([], deadpix_mask))

        r.add(FLATON_JSON_DESC, PipelineDict(bg_std_normed=bg_std_norm))

        return r
Пример #17
0
def process_flat(ondata_list, offdata_list):

    from stsci_helper import stsci_median

    return_object = {}

    flat_on = stsci_median(ondata_list)
    flat_off = stsci_median(offdata_list)

    bpix_mask = bp.badpixel_mask(flat_off, sigma_clip1=100)

    bg_std = flat_off[~bpix_mask].std()

    flat_on_off = flat_on - flat_off

    norm_factor = get_flat_normalization(flat_on_off,
                                         bg_std, bpix_mask)

    flat_norm = flat_on_off / norm_factor
    bg_std_norm = bg_std/norm_factor


    flat_bpix = flat_norm.astype("d", copy=True)
    flat_bpix[bpix_mask] = np.nan

    flat_mask = get_flat_mask(flat_bpix, bg_std_norm, sigma=5)


    return_object["flat_on_off"] = flat_on_off
    return_object["flat_norm_factor"] = norm_factor
    return_object["flat_normed"] = flat_norm
    return_object["flat_bpix_mask"] = bpix_mask
    return_object["bg_std"] = bg_std
    return_object["bg_std_normed"] = bg_std_norm
    return_object["flat_mask"] = flat_mask


    flat_deriv_ = get_y_derivativemap(flat_norm, flat_bpix,
                                      bg_std_norm,
                                      max_sep_order=150, pad=50,
                                      flat_mask=flat_mask)

    flat_deriv, flat_deriv_pos_msk, flat_deriv_neg_msk = flat_deriv_["data"], flat_deriv_["pos_mask"], flat_deriv_["neg_mask"]


    return_object["flat_deriv"] = flat_deriv
    return_object["flat_deriv_pos_mask"] = flat_deriv_pos_msk
    return_object["flat_deriv_neg_mask"] = flat_deriv_neg_msk

    ny, nx = flat_deriv.shape
    cent_bottom_list = identify_horizontal_line(flat_deriv,
                                                flat_deriv_pos_msk,
                                                pad=50,
                                                bg_std=bg_std_norm)

    cent_up_list = identify_horizontal_line(-flat_deriv,
                                            flat_deriv_neg_msk,
                                            pad=50,
                                            bg_std=bg_std_norm)


    if 1: # chevyshev
        _ = trace_centroids_chevyshev(cent_bottom_list,
                                      cent_up_list,
                                      domain=[0, 2048],
                                      ref_x=nx/2)

    if 0:
        order = 5
        func_fitter = get_line_fiiter(order)

        _ = trace_centroids(cent_bottom_list,
                            cent_up_list,
                            func_fitter=func_fitter,
                            ref_x=nx/2)

    bottom_up_solutions, centroid_bottom_up_list = _

    if 0:
        plot_solutions(flat_norm,
                       centroid_bottom_up_list,
                       bottom_up_solutions)

    return_object["cent_up_list"] = cent_up_list
    return_object["cent_bottom_list"] = cent_bottom_list
    return_object["bottomup_solutions"] = bottom_up_solutions
    return_object["bottomup_centroids"] = centroid_bottom_up_list

    return return_object
Пример #18
0
    def make_flaton_deadpixmap(self, flatoff_product,
                               deadpix_mask_old=None,
                               flat_mask_sigma=5.,
                               deadpix_thresh=0.6,
                               smooth_size=9):

        # load flat off data

        # flat_off = flatoff_product["flat_off"]
        # bg_std = flatoff_product["bg_std"]
        # hotpix_mask = flatoff_product["hotpix_mask"]

        from storage_descriptions import (FLAT_OFF_DESC,
                                          FLATOFF_JSON_DESC,
                                          HOTPIX_MASK_DESC)


        flat_off = flatoff_product[FLAT_OFF_DESC].data
        bg_std = flatoff_product[FLATOFF_JSON_DESC]["bg_std"]
        hotpix_mask = flatoff_product[HOTPIX_MASK_DESC].data

        flat_on = stsci_median(self.data_list)
        flat_on_off = flat_on - flat_off

        # normalize it
        norm_factor = get_flat_normalization(flat_on_off,
                                             bg_std, hotpix_mask)

        flat_normed = flat_on_off / norm_factor
        flat_std_normed = ni.median_filter(np.std(self.data_list, axis=0) / norm_factor,
                                           size=(3,3))
        bg_std_norm = bg_std/norm_factor

        # mask out bpix
        flat_bpixed = flat_normed.astype("d") # by default, astype
                                              # returns new array.
        flat_bpixed[hotpix_mask] = np.nan

        flat_mask = get_flat_mask(flat_bpixed, bg_std_norm,
                                  sigma=flat_mask_sigma)


        # get dead pixel mask
        flat_smoothed = ni.median_filter(flat_normed,
                                         [smooth_size, smooth_size])
        #flat_smoothed[order_map==0] = np.nan
        flat_ratio = flat_normed/flat_smoothed
        flat_std_mask = (flat_smoothed - flat_normed) > 5*flat_std_normed

        refpixel_mask = np.ones(flat_mask.shape, bool)
        # mask out outer boundaries
        refpixel_mask[4:-4,4:-4] = False

        deadpix_mask = (flat_ratio<deadpix_thresh) & flat_std_mask & flat_mask & (~refpixel_mask)

        if deadpix_mask_old is not None:
            deadpix_mask = deadpix_mask | deadpix_mask_old

        flat_bpixed[deadpix_mask] = np.nan


        from storage_descriptions import (FLAT_NORMED_DESC,
                                          FLAT_BPIXED_DESC,
                                          FLAT_MASK_DESC,
                                          DEADPIX_MASK_DESC,
                                          FLATON_JSON_DESC)


        r = PipelineProducts("flat on products")

        r.add(FLAT_NORMED_DESC, PipelineImageBase([], flat_normed))
        r.add(FLAT_BPIXED_DESC, PipelineImageBase([], flat_bpixed))
        r.add(FLAT_MASK_DESC, PipelineImageBase([], flat_mask))
        r.add(DEADPIX_MASK_DESC, PipelineImageBase([], deadpix_mask))

        r.add(FLATON_JSON_DESC,
              PipelineDict(bg_std_normed=bg_std_norm))

        return r
Пример #19
0
def mymain():
    from pipeline_jjlee import IGRINSLog, Destriper

    if 1:
        log_20140316 = dict(flat_off=range(2, 4),
                            flat_on=range(4, 7),
                            thar=range(1, 2),
                            HD3417=[15, 16])


        igrins_log = IGRINSLog("20140316", log_20140316)
    else:
        log_20140525 = dict(flat_off=range(64, 74),
                            flat_on=range(74, 84),
                            thar=range(3, 8))

        igrins_log = IGRINSLog("20140525", log_20140525)

    band = "H"

    import numpy as np
    destriper = Destriper()

    hdu_list = igrins_log.get_cal_hdus(band, "flat_off")
    flat_offs = [destriper.get_destriped(hdu.data) for hdu in hdu_list]

    hdu_list = igrins_log.get_cal_hdus(band, "flat_on")
    flat_ons = [hdu.data for hdu in hdu_list]

    #flat_off = np.median(flat_offs, axis=0)

    #import trace_flat
    #reload(trace_flat)
    #process_flat =  trace_flat.process_flat

    r = process_flat(ondata_list=flat_ons, offdata_list=flat_offs)


    flat_normed = r["flat_normed"].copy()
    flat_normed[r["flat_bpix_mask"]] = np.nan

    starting_order = 5
    orders = range(starting_order,
                   len(r["bottomup_solutions"]) + starting_order)
    order_map = make_order_map(flat_normed.shape,
                               r["bottomup_solutions"],
                               orders=orders,
                               mask_top_bottom=False)

    # get dead pixel mask
    flat_smoothed = ni.median_filter(flat_normed, [1, 9])
    flat_smoothed[order_map==0] = np.nan
    flat_ratio = flat_normed/flat_smoothed
    flat_mask = r["flat_mask"]

    refpixel_mask = np.ones(flat_mask.shape, bool)
    refpixel_mask[4:-4,4:-4] = False

    dead_pixel_mask = (flat_ratio<0.3) & flat_mask & (~refpixel_mask)

    flat_normed[dead_pixel_mask] = np.nan

    import scipy.ndimage as ni
    slices = ni.find_objects(order_map)

    mean_order_spec = []
    mask_list = []
    for o in orders:
        sl = slices[o-1]
        d_sl = flat_normed[sl].copy()
        d_sl[order_map[sl] != o] = np.nan

        f_sl = flat_mask[sl].copy()
        f_sl[order_map[sl] != o] = np.nan
        ff = np.nanmean(f_sl, axis=0)
        mask_list.append(ff)

        mmm = order_map[sl] == o
        ss = [np.nanmean(d_sl[3:-3][:,i][mmm[:,i][3:-3]]) for i in range(2048)]
        mean_order_spec.append(ss)


    s_list = [get_smoothed_order_spec(s) for s in mean_order_spec]
    i1i2_list = [get_order_boundary_indices(s, s0) \
                 for s, s0 in zip(mean_order_spec, s_list)]
    p_list = [get_order_flat1d(s, i1, i2) for s, (i1, i2) \
              in zip(s_list, i1i2_list)]

    fig_list, ax_list = prepare_order_trace_plot(s_list)
    x = np.arange(2048)
    for s, i1i2, ax in zip(mean_order_spec, i1i2_list, ax_list):
        check_order_trace1(ax, x, s, i1i2)

    for s, p, ax in zip(mean_order_spec, p_list, ax_list):
        check_order_trace2(ax, x, p)


    # make flat
    x = np.arange(len(s))
    flat_im = np.empty(flat_normed.shape, "d")
    flat_im.fill(np.nan)

    for o, p in zip(orders, p_list):
        sl = slices[o-1]
        d_sl = flat_normed[sl].copy()
        msk = (order_map[sl] == o)
        d_sl[~msk] = np.nan

        flat_im[sl][msk] = (d_sl / p(x))[msk]

    if 0:
        hdu_list = igrins_log.get_cal_hdus(band, "HD3417")
        hd_list = [destriper.get_destriped(hdu.data) for hdu in hdu_list]
        from stsci_helper import stsci_median
        hd_spec = stsci_median(hd_list)