Ejemplo n.º 1
0
                            savefile=True,
                            saveall=False,
                            diffimg=False,
                            path=None,
                            timit=False)
#####################################################################################################################################################

# (3) ORDER TRACING #################################################################################################################################
# find orders roughly
P, tempmask = find_stripes(MW,
                           deg_polynomial=2,
                           min_peak=0.05,
                           gauss_filter_sigma=3.,
                           simu=False)
# assign physical diffraction order numbers (this is only a dummy function for now) to order-fit polynomials and bad-region masks
P_id = make_P_id(P)
mask = make_mask_dict(tempmask)
# extract stripes of user-defined width from the science image, centred on the polynomial fits defined in step (1)
MW_stripes, MW_indices = extract_stripes(MW,
                                         P_id,
                                         return_indices=True,
                                         slit_height=5)
#####################################################################################################################################################

###
#if we want to determine spatial profiles, then we should remove cosmics and background from MW like so:

# cosmic_cleaned_MW = remove_cosmics(MW, ronmask, obsname, path, Flim=3.0, siglim=5.0, maxiter=1, savemask=False, savefile=False, save_err=False, verbose=True, timit=True)
# bg_corrected_MW = remove_background(cosmic_cleaned_MW, P_id, obsname, path, degpol=5, slit_height=5, save_bg=False, savefile=False, save_err=False, exclude_top_and_bottom=True, verbose=True, timit=True)
#before doing the following:
MW_stripes, MW_stripe_indices = extract_stripes(MW,
choice = 'r'
if os.path.isfile(path + 'P_id.npy') and os.path.isfile(path + 'mask.npy'):
    choice = raw_input("INITIAL ORDER TRACING has already been done for " + date + " ! Do you want to skip this step or recreate it? ['s' / 'r']")
if choice.lower() == 's':
    print('Loading initial order traces for ' + date + '...')
    P_id = np.load(path + 'P_id.npy').item()
    mask = np.load(path + 'mask.npy').item()
else:
    # find rough order locations
    #P,tempmask = find_stripes(MW, deg_polynomial=2, min_peak=0.05, gauss_filter_sigma=3., simu=False)
    P,tempmask = find_stripes(MW, deg_polynomial=2, min_peak=0.05, gauss_filter_sigma=10., simu=False, maskthresh = 400)
    # if the bad pixel column is found as an order:
    # del P[5]
    # tempmask = tempmask[np.r_[0:5, 6:40],:]
    # assign physical diffraction order numbers (this is only a dummy function for now) to order-fit polynomials and bad-region masks
    P_id_dum = make_P_id(P)
    assert len(P_id_dum) == 39, 'ERROR: not exactly 39 orders found!!!'
    mask = make_mask_dict(tempmask)
    P_id = copy.deepcopy(P_id_dum)
    # for the dates where fibre 8 had a ~20-30% drop in throughput, do NOT subtract 2
    if int(date) == 20190414:
        for o in P_id.keys():
            P_id[o][0] -= 1.
    elif (int(date) <= 20190203) or (int(date) >= 20190619):
        for o in P_id.keys():
            P_id[o][0] -= 2.
    np.save(path + 'P_id.npy', P_id)
    np.save(path + 'mask.npy', mask)
    

# now redo the master white properly, incl background removal, and save to file
def make_fibparms_by_fib(savefile=True):

    path = '/Users/christoph/OneDrive - UNSW/fibre_profiles/'
    fp_files = glob.glob(path + "sim/" + "fibre_profiles*.npy")
    #mask_files = glob.glob(path+"masks/"+"mask*.npy")

    fibparms = {}

    for file in fp_files:
        fibnum = file[-6:-4]
        fib = 'fibre_' + fibnum
        fp = np.load(file).item()
        mask = np.load(path + "masks/" + "mask_" + fibnum + ".npy").item()
        flatname = '/Volumes/BERGRAID/data/simu/veloce_flat_t70000_single_fib' + fibnum + '.fit'
        flat = pyfits.getdata(flatname)
        img = flat + 1.

        #we need the flux later only to weight the fits
        P, tempmask = find_stripes(flat, deg_polynomial=2)
        P_id = make_P_id(P)
        #mask = make_mask_dict(tempmask)
        stripes = extract_stripes(img, P_id, slit_height=10)

        fibparms[fib] = {}
        for ord in sorted(fp.keys()):

            fibparms[fib][ord] = {}

            mu = np.array(fp[ord]['mu'])
            #amp = np.array(fp[ord]['amp'])
            sigma = np.array(fp[ord]['sigma'])
            beta = np.array(fp[ord]['beta'])
            #offset = np.array(fp[ord]['offset'])
            #slope = np.array(fp[ord]['slope'])

            onchip = mu >= 0
            good = np.logical_and(mu >= 0, mask[ord])

            stripe = stripes[ord]
            sc, sr = flatten_single_stripe(stripe, slit_height=10, timit=False)
            fluxsum = np.sum(sc, axis=0)
            w = np.sqrt(
                fluxsum
            )  #use relative error so that the order centres receive the largest weight-contribution

            xx = np.arange(len(mu))
            mu_fit = np.poly1d(np.polyfit(xx[good], mu[good], 5, w=w[good]))
            # xarr = xx*4*np.pi/np.max(xx) - 2*np.pi
            # amp_popt,amp_pcov = curve_fit(blaze,xarr,amp,sigma=w,p0=(0.2,np.max(amp),0.))
            sigma_fit = np.poly1d(
                np.polyfit(xx[good], sigma[good], 2, w=w[good]))
            beta_fit = np.poly1d(np.polyfit(xx[good], beta[good], 2,
                                            w=w[good]))
            #offset_fit = np.poly1d(np.polyfit(xx[good], offset[good], 0, w=w[good]))
            #offset_fit = np.average(offset, weights=w)

            fibparms[fib][ord]['mu_fit'] = mu_fit
            fibparms[fib][ord]['sigma_fit'] = sigma_fit
            fibparms[fib][ord]['beta_fit'] = beta_fit
            #fibparms[fib][ord]['offset_fit'] = offset_fit
            fibparms[fib][ord]['onchip'] = onchip

    if savefile:
        np.save(
            '/Users/christoph/OneDrive - UNSW/fibre_profiles/sim/fibparms_by_fib.npy',
            fibparms)

    return fibparms