def plot_linefits(y, ff):
        global plot_arr
        pl.figure(3)
        pl.subplot(*plot_arr)
        ps = Fit.do_fit(y, Fit.residual_single)[0]

        xx = np.arange(len(y))
        xxx = np.arange(0,len(y)-1,.1)
        fun = Fit.fit_single(ps,xxx)
        pl.plot(xx,y, '*')
        pl.plot(xxx, fun)
        x0 = ff(0)
        x1 = ff(0) + ps[4]
        pl.plot([x0, x0], [0, Fit.fit_single(ps, x0)])
        pl.plot([x1, x1], [0, Fit.fit_single(ps, x1)])

        pl.figure(4)
        pl.subplot(*plot_arr)
        fun = Fit.fit_single(ps,xx)
        r = (y - fun)/np.sqrt(np.abs(fun))
        pl.plot(xx, r, '*')
        pl.ylim([-5,5])
Example #2
0
def find_edge_pair(data, y, roi_width, edgeThreshold=450):
    '''
    find_edge_pair finds the edge of a slit pair in a flat

    data[2048x2048]: a well illuminated flat field [DN]
    y: guess of slit edge position [pix]

    Keywords:
    
    edgeThreshold: the pixel value below which we should ignore using
    to calculate edges.
    
    Moves along the edge of a slit image
            - At each location along the slit edge, determines
            the position of the demarcations between two slits

    Outputs:
    xposs []: Array of x positions along the slit edge [pix]
    yposs []: The fitted y positions of the "top" edge of the slit [pix]
    widths []: The fitted delta from the top edge of the bottom [pix]
    scatters []: The amount of light between slits


    The procedure is as follows
    1: starting from a guess spatial position (parameter y), march
        along the spectral direction in some chunk of pixels
    2: At each spectral location, construct a cross cut across the
        spatial direction; select_roi is used for this.
    3: Fit a two-sided error function Fit.residual_disjoint_pair
        on the vertical cross cut derived in step 2.
    4: If the fit fails, store it in the missing list
        - else if the top fit is good, store the top values in top vector
        - else if the bottom fit is good, store the bottom values in bottom
          vector.
    5: In the vertical cross-cut, there is a minimum value. This minimum
        value is stored as a measure of scattered light.

    Another procedure is used to fit polynomials to these fitted values.
    '''
    def select_roi(data, roi_width):
        v = data[y - roi_width:y + roi_width, xp - 2:xp + 2]
        v = np.median(v, axis=1)  # Axis = 1 is spatial direction

        return v

    xposs_top = []
    yposs_top = []
    xposs_top_missing = []

    xposs_bot = []
    yposs_bot = []
    xposs_bot_missing = []
    yposs_bot_scatters = []

    #1
    rng = np.linspace(10, 2040, 50).astype(np.int)
    for i in rng:
        xp = i
        #2
        v = select_roi(data, roi_width)
        xs = np.arange(len(v))

        # Modified from 450 as the hard coded threshold to one that
        # can be controlled by a keyword
        if (np.median(v) < edgeThreshold):
            xposs_top_missing.append(xp)
            xposs_bot_missing.append(xp)
            continue

        #3
        ff = Fit.do_fit(v, residual_fun=Fit.residual_disjoint_pair)
        fit_ok = 0 < ff[4] < 4

        if fit_ok:
            (sigma, offset, mult1, mult2, add, width) = ff[0]

            xposs_top.append(xp)
            yposs_top.append(y - roi_width + offset + width)

            xposs_bot.append(xp)
            yposs_bot.append(y - roi_width + offset)

            between = offset + width / 2
            if 0 < between < len(v) - 1:
                start = np.max([0, between - 2])
                stop = np.min([len(v), between + 2])
                yposs_bot_scatters.append(np.min(v[start:stop]))  # 5

                if False:
                    pl.figure(2)
                    pl.clf()
                    tmppix = np.arange(y - roi_width, y + roi_width)
                    tmpx = np.arange(len(v))
                    pl.axvline(y - roi_width + offset + width, color='red')
                    pl.axvline(y - roi_width + offset, color='red')
                    pl.scatter(tmppix, v)
                    pl.plot(tmppix, Fit.fit_disjoint_pair(ff[0], tmpx))
                    pl.axhline(yposs_bot_scatters[-1])
                    pl.draw()

            else:
                yposs_bot_scatters.append(np.nan)

        else:
            xposs_bot_missing.append(xp)
            xposs_top_missing.append(xp)
            info("Skipping wavelength pixel): %i" % (xp))

    return map(np.array, (xposs_bot, xposs_bot_missing, yposs_bot, xposs_top,
                          xposs_top_missing, yposs_top, yposs_bot_scatters))
Example #3
0
def find_edge_pair(data, y, roi_width, edgeThreshold=450):
    '''
    find_edge_pair finds the edge of a slit pair in a flat

    data[2048x2048]: a well illuminated flat field [DN]
    y: guess of slit edge position [pix]

    Keywords:
    
    edgeThreshold: the pixel value below which we should ignore using
    to calculate edges.
    
    Moves along the edge of a slit image
            - At each location along the slit edge, determines
            the position of the demarcations between two slits

    Outputs:
    xposs []: Array of x positions along the slit edge [pix]
    yposs []: The fitted y positions of the "top" edge of the slit [pix]
    widths []: The fitted delta from the top edge of the bottom [pix]
    scatters []: The amount of light between slits


    The procedure is as follows
    1: starting from a guess spatial position (parameter y), march
        along the spectral direction in some chunk of pixels
    2: At each spectral location, construct a cross cut across the
        spatial direction; select_roi is used for this.
    3: Fit a two-sided error function Fit.residual_disjoint_pair
        on the vertical cross cut derived in step 2.
    4: If the fit fails, store it in the missing list
        - else if the top fit is good, store the top values in top vector
        - else if the bottom fit is good, store the bottom values in bottom
          vector.
    5: In the vertical cross-cut, there is a minimum value. This minimum
        value is stored as a measure of scattered light.

    Another procedure is used to fit polynomials to these fitted values.
    '''

    def select_roi(data, roi_width):
        v = data[y-roi_width:y+roi_width, xp-2:xp+2]
        v = np.median(v, axis=1) # Axis = 1 is spatial direction

        return v



    xposs_top = []
    yposs_top = []
    xposs_top_missing = []

    xposs_bot = []
    yposs_bot = []
    xposs_bot_missing = []
    yposs_bot_scatters = []

    #1 
    rng = np.linspace(10, 2040, 50).astype(np.int)
    for i in rng:
        xp = i
        #2
        v = select_roi(data, roi_width)
        xs = np.arange(len(v))

        # Modified from 450 as the hard coded threshold to one that
        # can be controlled by a keyword
        if (np.median(v) < edgeThreshold):
            xposs_top_missing.append(xp)
            xposs_bot_missing.append(xp)
            continue

        #3
        ff = Fit.do_fit(v, residual_fun=Fit.residual_disjoint_pair)
        fit_ok = 0 < ff[4] < 4

        if fit_ok:
            (sigma, offset, mult1, mult2, add, width) = ff[0]

            xposs_top.append(xp)
            yposs_top.append(y - roi_width + offset + width)

            xposs_bot.append(xp)
            yposs_bot.append(y - roi_width + offset)

            between = offset + width/2
            if 0 < between < len(v)-1:
                start = np.max([0, between-2])
                stop = np.min([len(v),between+2])
                yposs_bot_scatters.append(np.min(v[start:stop])) # 5

                if False:
                    pl.figure(2)
                    pl.clf()
                    tmppix = np.arange(y-roi_width, y+roi_width)
                    tmpx = np.arange(len(v))
                    pl.axvline(y - roi_width + offset + width, color='red')
                    pl.axvline(y - roi_width + offset, color='red')
                    pl.scatter(tmppix, v)
                    pl.plot(tmppix, Fit.fit_disjoint_pair(ff[0], tmpx))
                    pl.axhline(yposs_bot_scatters[-1])
                    pl.draw()

            else:
                yposs_bot_scatters.append(np.nan)

        else:
            xposs_bot_missing.append(xp)
            xposs_top_missing.append(xp)
            info("Skipping wavelength pixel): %i" % (xp))

    
    return map(np.array, (xposs_bot, xposs_bot_missing, yposs_bot, xposs_top,
        xposs_top_missing, yposs_top, yposs_bot_scatters))
Example #4
0
def go(fname):
        global plot_arr

        print fname
        
        (header, data) = IO.readfits(fname)
        bs = CSU.Barset()
        bs.set_header(header)

        bars = []
        means = []
        sds = []
        deltas = []
        deltas_mm = []
        poss = []
        poss_mm = []
        poss_y = []
        request = []
        qs = []
        bars = range(1, 93)
        fit_fun = Fit.residual_single

        for bar in bars:
                pos = bs.get_bar_pix(bar)
                if bar % 8 == 0:
                        print "%2.0i: (%7.2f, %7.2f)" % (bar, pos[0], pos[1])


                if is_odd(bar):
                        if (bs.pos[bar] - bs.pos[bar-1]) < 2.7:
                                fit_fun = Fit.residual_pair
                        else:
                                fit_fun = Fit.residual_single

                width = 19 
                [[xslice, yslice],extent,ystart] = make_slice(pos,0,width,30)

                

                if not is_in_bounds(extent):
                        fits = [0,0,0,0,0]
                        [ff,ok] = [np.poly1d(0,0), []]
                        means.append(fits)
                        sds.append(fits)
                        drop_this = True
                else:
                        drop_this = False
                        fits = []
                        ys = np.arange(-10,10, dtype=np.float32)
                        for i in ys:
                                tofit = data[ystart-i, xslice]
                                y = median_tails(tofit)

                                ps = Fit.do_fit(y, fit_fun)
                                fits.append(ps[0])
                        
                        fits = np.array(fits)
                        fits[:,1] += 1

                        # fit to the ridgeline
                        [ff, ok] = fit_line_with_sigclip(ys, fits[:,1])
                        m = [np.mean(fits[:,i]) for i in range(5)]
                        s = [np.std(fits[:,i]) for i in range(5)]
                        means.append(m)
                        sds.append(s)


                slit_center_offset = pos[1] - ystart
                fc = ff(slit_center_offset)
                slit_center_pos = np.float(extent[0] + fc )

                if drop_this: 
                        poss.append(NaN)
                        poss_y.append(NaN)
                        poss_mm.append(NaN)
                else: 
                        poss.append(slit_center_pos)
                        poss_y.append(ystart)
                        poss_mm.append(CSU.csu_pix_to_mm_poly(slit_center_pos, ystart)[0])

                delta = np.float(slit_center_pos - pos[0])
                if drop_this: 
                        deltas.append(NaN)
                        deltas_mm.append(NaN)
                else: 
                        deltas.append(delta)
                        b = CSU.csu_pix_to_mm_poly(slit_center_pos + delta, ystart)[0]
                        deltas_mm.append(b - poss_mm[-1])

                q = np.float(np.degrees(np.tan(ff(1)-ff(0))))
                if drop_this: qs.append(NaN)
                qs.append(q)


        means = np.array(means)
        f = lambda x: np.array(x).ravel()
        sds = f(sds)
        deltas = f(deltas)
        poss = f(poss)
        poss_y = f(poss_y)
        poss_mm = f(poss_mm)
        deltas_mm = f(deltas_mm)
        qs  = f(qs)
        bars = f(bars)



        fout = "/users/npk/dropbox/mosfire/cooldown 9/csu_meas/" + fname.split("/")[-1] + ".sav"
        print "saving"
        tosav = {"bars": bars, "request": bs.pos, "deltas_mm": deltas_mm, "poss": poss, "poss_mm": poss_mm, "deltas": deltas, "means": means, "qs": qs}
        scipy.io.savemat(fout, tosav)
        save_region(bs, "/users/npk/dropbox/mosfire/cooldown 9/csu_meas/" + fname.split("/")[-1] + ".reg")
        print "saved"

        regout = "/users/npk/dropbox/mosfire/cooldown 9/csu_meas/" + fname.split("/")[-1] + ".meas.reg"
        pairs = np.array([poss,poss_y]).transpose()
        s = CSU.to_ds9_region(pairs, dash=0, color="blue", label=False)
        try:
                f = open(regout, "w")
                f.writelines(s)
                f.close()
        except:
                print "Couldn't write: %s" % regout


        return [tosav, bs]
Example #5
0
    [[xslice, yslice], extent] = make_slice(pos, 6, 25)
    if extent[0] == extent[1]:
        cnt += 1
        continue
    if extent[2] == extent[3]:
        cnt += 1
        continue
    cnt += 1

    fits = []
    xs = np.arange(-10, 10)
    for i in xs:
        tofit = data[pos[1] - i, xslice]
        y = median_tails(tofit)

        ps = Fit.do_fit(y, Fit.residual_pair)
        fits.append(ps[0])

    fits = np.array(fits)

    m = [np.mean(fits[:, i]) for i in range(5)]
    s = [np.std(fits[:, i]) for i in range(5)]
    means.append(m)
    sds.append(s)

    [ff, ok] = fit_line_with_sigclip(xs, fits[:, 1])

    pl.figure(5)
    pl.subplot(7, 7, cntfit)
    pl.plot(xs, fits[:, 1] - ff(xs), '*-')
    pl.plot(xs[ok], fits[ok, 1] - ff(xs[ok]), 'or-')
def go(fname):
        global plot_arr

        print fname
        
        (header, data) = IO.readfits(fname)
        bs = CSU.Barset()
        bs.set_header(header)

        bars = []
        means = []
        sds = []
        deltas = []
        deltas_mm = []
        poss = []
        poss_mm = []
        poss_y = []
        request = []
        qs = []
        bars = range(1, 93)
        fit_fun = Fit.residual_single

        for bar in bars:
                pos = bs.get_bar_pix(bar)
                if bar % 8 == 0:
                        print "%2.0i: (%7.2f, %7.2f)" % (bar, pos[0], pos[1])


                if is_odd(bar):
                        if (bs.pos[bar] - bs.pos[bar-1]) < 2.7:
                                fit_fun = Fit.residual_pair
                        else:
                                fit_fun = Fit.residual_single

                width = 19 
                [[xslice, yslice],extent,ystart] = make_slice(pos,0,width,30)

                

                if not is_in_bounds(extent):
                        fits = [0,0,0,0,0]
                        [ff,ok] = [np.poly1d(0,0), []]
                        means.append(fits)
                        sds.append(fits)
                        drop_this = True
                else:
                        drop_this = False
                        fits = []
                        ys = np.arange(-10,10, dtype=np.float32)
                        for i in ys:
                                tofit = data[ystart-i, xslice]
                                y = median_tails(tofit)

                                ps = Fit.do_fit(y, fit_fun)
                                fits.append(ps[0])
                        
                        fits = np.array(fits)
                        fits[:,1] += 1

                        # fit to the ridgeline
                        [ff, ok] = fit_line_with_sigclip(ys, fits[:,1])
                        m = [np.mean(fits[:,i]) for i in range(5)]
                        s = [np.std(fits[:,i]) for i in range(5)]
                        means.append(m)
                        sds.append(s)


                slit_center_offset = pos[1] - ystart
                fc = ff(slit_center_offset)
                slit_center_pos = np.float(extent[0] + fc )

                if drop_this: 
                        poss.append(NaN)
                        poss_y.append(NaN)
                        poss_mm.append(NaN)
                else: 
                        poss.append(slit_center_pos)
                        poss_y.append(ystart)
                        poss_mm.append(CSU.csu_pix_to_mm_poly(slit_center_pos, ystart)[0])

                delta = np.float(slit_center_pos - pos[0])
                if drop_this: 
                        deltas.append(NaN)
                        deltas_mm.append(NaN)
                else: 
                        deltas.append(delta)
                        b = CSU.csu_pix_to_mm_poly(slit_center_pos + delta, ystart)[0]
                        deltas_mm.append(b - poss_mm[-1])

                q = np.float(np.degrees(np.tan(ff(1)-ff(0))))
                if drop_this: qs.append(NaN)
                qs.append(q)


        means = np.array(means)
        f = lambda x: np.array(x).ravel()
        sds = f(sds)
        deltas = f(deltas)
        poss = f(poss)
        poss_y = f(poss_y)
        poss_mm = f(poss_mm)
        deltas_mm = f(deltas_mm)
        qs  = f(qs)
        bars = f(bars)



        fout = "/users/npk/dropbox/mosfire/cooldown 9/csu_meas/" + fname.split("/")[-1] + ".sav"
        print "saving"
        tosav = {"bars": bars, "request": bs.pos, "deltas_mm": deltas_mm, "poss": poss, "poss_mm": poss_mm, "deltas": deltas, "means": means, "qs": qs}
        scipy.io.savemat(fout, tosav)
        save_region(bs, "/users/npk/dropbox/mosfire/cooldown 9/csu_meas/" + fname.split("/")[-1] + ".reg")
        print "saved"

        regout = "/users/npk/dropbox/mosfire/cooldown 9/csu_meas/" + fname.split("/")[-1] + ".meas.reg"
        pairs = np.array([poss,poss_y]).transpose()
        s = CSU.to_ds9_region(pairs, dash=0, color="blue", label=False)
        try:
                f = open(regout, "w")
                f.writelines(s)
                f.close()
        except:
                print "Couldn't write: %s" % regout


        return [tosav, bs]
def go(fname):
        global plot_arr

        print fname
        
        (header, data) = IO.readfits(fname)
        bs = CSU.Barset()
        bs.set_header(header)

        cntfit = 1
        for i in range(1,8):
                continue
                pl.figure(i)
                pl.clf()

        first_time = True

        bars = []
        means = []
        sds = []
        deltas = []
        deltas_mm = []
        poss = []
        poss_mm = []
        poss_y = []
        request = []
        qs = []
        bars = range(1, 93)
        for bar in bars:
                pos = bs.get_bar_pix(bar)
                if bar % 8 == 0:
                        print "%2.0i: (%7.2f, %7.2f)" % (bar, pos[0], pos[1])

                width = 19 
                [[xslice, yslice],extent,ystart] = make_slice(pos,0,width,30)


                if not is_in_bounds(extent):
                        fits = [0,0,0,0,0]
                        [ff,ok] = [np.poly1d(0,0), []]
                        means.append(fits)
                        sds.append(fits)
                        drop_this = True
                else:
                        drop_this = False
                        fits = []
                        ys = np.arange(-10,10, dtype=np.float32)
                        for i in ys:
                                tofit = data[ystart-i, xslice]
                                y = median_tails(tofit)

                                ps = Fit.do_fit(y, Fit.residual_single)
                                fits.append(ps[0])
                        
                        fits = np.array(fits)
                        fits[:,1] += 1

                        # fit to the ridgeline
                        [ff, ok] = fit_line_with_sigclip(ys, fits[:,1])
                        m = [np.mean(fits[:,i]) for i in range(5)]
                        s = [np.std(fits[:,i]) for i in range(5)]
                        means.append(m)
                        sds.append(s)

                        #if bar == 44: start_shell()
                        #if bar == 43: start_shell()

                        # End of measurement logic, PLOTS

                        plot_arr = [12, 8, cntfit]
                        cntfit += 1
                        if False:
                                plot_stamps(data[yslice, xslice], ps[0])
                                y0 = median_tails(data[ystart, xslice])
                                plot_linefits(y0, ff)
                                plot_ridgeline(fits, ok, ys, ff, bar)
                                plot_widths(fits, ok, ys)

                        # End of plots, BOOKEEPING


                slit_center_offset = pos[1] - ystart
                fc = ff(slit_center_offset)
                slit_center_pos = np.float(extent[0] + fc )

                if drop_this: 
                        poss.append(NaN)
                        poss_y.append(NaN)
                        poss_mm.append(NaN)
                else: 
                        poss.append(slit_center_pos)
                        poss_y.append(ystart)
                        poss_mm.append(CSU.csu_pix_to_mm_poly(slit_center_pos, ystart)[0])

                delta = np.float(slit_center_pos - pos[0])
                if drop_this: 
                        deltas.append(NaN)
                        deltas_mm.append(NaN)
                else: 
                        deltas.append(delta)
                        b = CSU.csu_pix_to_mm_poly(slit_center_pos + delta, ystart)[0]
                        deltas_mm.append(b - poss_mm[-1])

                q = np.float(np.degrees(np.tan(ff(1)-ff(0))))
                if drop_this: qs.append(NaN)
                qs.append(q)

                if False: #is_in_bounds(extent):
                        pl.figure(2)
                        pl.text(pos[0], pos[1], 'b%2.0i: w=%3.2f p=%5.2f q=%3.2f d=%1.3f' % (bar, np.mean(fits[:,4]), extent[0]+ff(0), q, delta), fontsize=11, family='monospace', horizontalalignment='center')
                        


        means = np.array(means)
        f = lambda x: np.array(x).ravel()
        sds = f(sds)
        deltas = f(deltas)
        poss = f(poss)
        poss_y = f(poss_y)
        poss_mm = f(poss_mm)
        deltas_mm = f(deltas_mm)
        qs  = f(qs)
        bars = f(bars)
        pl.draw()


        if False:
                [pf, ok] = fit_line_with_sigclip(bars, deltas)
                print "** Residual: %4.3f [pix]" % np.std(deltas[ok]-pf(bars[ok]))
                evens = range(0,92,2)
                odds = range(1,92,2)

                pl.plot(bars[evens], deltas[evens],'r*-')
                pl.plot(bars[odds], deltas[odds],'b*-')
                pl.title("Red: even, blue: odd")
                pl.xlabel("Bar #")
                pl.ylabel("Delta pixel (Measured - Predicted)")
                pl.plot(bars, pf(bars))


        fout = "/users/npk/dropbox/mosfire/cooldown 9/csu_meas/" + fname.split("/")[-1] + ".sav"
        print "saving"
        tosav = {"bars": bars, "request": bs.pos, "deltas_mm": deltas_mm, "poss": poss, "poss_mm": poss_mm, "deltas": deltas, "means": means, "qs": qs}
        scipy.io.savemat(fout, tosav)
        save_region(bs, "/users/npk/dropbox/mosfire/cooldown 9/csu_meas/" + fname.split("/")[-1] + ".reg")
        print "saved"

        regout = "/users/npk/dropbox/mosfire/cooldown 9/csu_meas/" + fname.split("/")[-1] + ".meas.reg"
        pairs = np.array([poss,poss_y]).transpose()
        s = CSU.to_ds9_region(pairs, dash=0, color="blue", label=False)
        try:
                f = open(regout, "w")
                f.writelines(s)
                f.close()
        except:
                print "Couldn't write: %s" % regout


        return [tosav, bs]
        [[xslice, yslice],extent] = make_slice(pos, 6,25)
        if extent[0] == extent[1]: 
                cnt += 1
                continue
        if extent[2] == extent[3]: 
                cnt += 1
                continue
        cnt+=1

        fits = []
        xs = np.arange(-10,10)
        for i in xs:
                tofit = data[pos[1]-i, xslice]
                y = median_tails(tofit)

                ps = Fit.do_fit(y, Fit.residual_pair)
                fits.append(ps[0])

        fits = np.array(fits)

        m = [np.mean(fits[:,i]) for i in range(5)]
        s = [np.std(fits[:,i]) for i in range(5)]
        means.append(m)
        sds.append(s)

        [ff, ok] = fit_line_with_sigclip(xs, fits[:,1])

        pl.figure(5)
        pl.subplot(7,7,cntfit)
        pl.plot(xs, fits[:,1] - ff(xs), '*-')
        pl.plot(xs[ok], fits[ok,1] - ff(xs[ok]), 'or-')
        print "-------------==========================------------------"
        print "Finding Slit Edges for %s starting at %4.0i" % (ssl[target]["Target_Name"], y)
        tock = time.clock()

        yposs = []
        widths = []
        xposs = []

        x = 936
        for i in range(-49, 50):
                delt = 1900/100.
                xp = x+delt*i
                v = data[y-roi_width:y+roi_width, xp-2:xp+2]
                v = np.median(v, axis=1)

                ff = Fit.do_fit(v, residual_fun=Fit.residual_disjoint_pair)
                if (0 < ff[4] < 4):
                        xposs.append(x+delt*i)
                        xs = np.arange(len(v))

                        yposs.append(ff[0][1] - roi_width)
                        widths.append(ff[0][5])
                else:
                        print "Skipping: %i" % (x+delt*i)

        (xposs, yposs, widths) = map(np.array, (xposs, yposs, widths))

        fun = np.poly1d(Fit.polyfit_clip(xposs, yposs, 3))
        wfun = np.poly1d(Fit.polyfit_clip(xposs, widths, 3))
        res = fun(xposs) - yposs
        sd = np.std(res)