Exemple #1
0
def mjd_fromheader(h):
    """
    return modified Julian date from header
    """

    datetu = h[0].header['DATE-OBS']
    if len(datetu) < 23:
        mjd_start = h[0].header['MJD-OBS']
        mjd0 = 2400000.5
    else:
        #print datetu
        mjd0, mjd, i = GLOBALutils.iau_cal2jd(int(datetu[:4]),
                                              int(datetu[5:7]),
                                              int(datetu[8:10]))
        ho = int(datetu[11:13])
        mi = int(datetu[14:16])
        se = float(datetu[17:])
        ut = float(ho) + float(mi) / 60.0 + float(se) / 3600.0
        mjd_start = mjd + ut / 24.0

    secinday = 24 * 3600.0
    fraction = 0.5
    texp = h[0].header['EXPTIME']  #sec

    mjd = mjd_start + (fraction * texp) / secinday

    return mjd, mjd0
Exemple #2
0
def mjd_fromheader2(h):
    """
    return modified Julian date from header
    """
    rtime = 90.

    secinday = 24 * 3600.0

    date = h[0].header['DATE']
    hour = date[11:]
    date = date[:10]

    datetu = date.replace('-', ':')
    ut = hour.replace('-', ':')
    datetu = date.replace(' ', '')
    ut = hour.replace(' ', '')
    #print datetu
    #print ut
    mjd0, mjd, i = GLOBALutils.iau_cal2jd(int(datetu[0:4]), int(datetu[5:7]),
                                          int(datetu[8:10]))
    ut = (float(ut[:2]) * 3600. + float(ut[3:5]) * 60. + float(ut[6:]))
    mjd_start = mjd + ut / secinday

    fraction = 0.5
    texp = float(h[0].header['EXPTIME'])  #sec

    mjd = mjd_start - (fraction * texp + rtime) / secinday

    return mjd, mjd0
Exemple #3
0
def mjd_from_data(datetu, ut, texp, fraction):
    secinday = 24 * 3600.0
    mjd0, mjd, i = GLOBALutils.iau_cal2jd(int(datetu[0:4]), int(datetu[5:7]),
                                          int(datetu[8:10]))

    ut = (float(ut[:2]) * 3600. + float(ut[3:5]) * 60. + float(ut[6:]))
    mjd_start = mjd + ut / secinday

    mjd = mjd_start + (fraction * texp) / secinday
    return mjd, mjd0
Exemple #4
0
def get_thar_offsets(lines_thar, order_dir='wavcals/', pref='order_', suf='.iwdat', ior=20,fior=45, delt_or=3, del_width=200.,binning=1):
	xcs = []
	for ii in range(ior,fior):
			thar_order = lines_thar[ii]
			xct = []
			for order in range(ii-delt_or,ii+delt_or):
				order_s = str(order)
				if (order < 10):
					order_s = '0' + order_s
				if os.access(order_dir+pref+order_s+suf,os.F_OK):
					f = open(order_dir+pref+order_s+suf,'r')
					llins = f.readlines()
					if len(llins)>5:
						pixel_centers_0 = []
						for line in llins:
							w = line.split()
							nlines = int(w[0])
							for j in range(nlines):
								pixel_centers_0.append(float(w[2*j+1])*1./float(binning))
						pixel_centers_0 = np.array(pixel_centers_0).astype('int')
						#plot(thar_order)
						#plot(pixel_centers_0,thar_order[pixel_centers_0],'ro')
						#print order, order_s
						#show()
						ml = np.array(pixel_centers_0) - 2
						mh = np.array(pixel_centers_0) + 2
						if len(ml)>0:
							xc,offs = GLOBALutils.XCorPix( thar_order, ml, mh, del_width=del_width)
						else:
							xc = np.zeros(len(offs))
					else:
						xc = np.array([])
				if len(xct) == 0:
					xct = xc.copy()
				else:
					xct = np.vstack((xct,xc))
						
			if len(xcs) == 0:
				xcs = xct.copy()
			else:
				xcs += xct
	maxes, maxvels = [],[]
	for i in range(xcs.shape[0]):
		maxes.append(xcs[i].max())
		maxvels.append(offs[np.argmax(xcs[i])])
		#plot(offs,xcs[i])
	#show()

	maxes,maxvels = np.array(maxes),np.array(maxvels)
	orders_offset = -delt_or + np.argmax(maxes)
	rough_shift = maxvels[np.argmax(maxes)]

	return orders_offset, rough_shift
Exemple #5
0
def mjd_fromheader(h):
    """
    return modified Julian date from header
    """

    datetu = h[0].header['UT-DATE']
    ut     = h[0].header['UT-TIME']
    mjd0,mjd,i = GLOBALutils.iau_cal2jd(int(datetu[0:4]),int(datetu[5:7]),int(datetu[8:]))
    ut = float(ut[:2]) + float(ut[3:5])/60. + float(ut[6:])/3600.
    mjd_start = mjd + ut/24.0
    secinday = 24*3600.0
    fraction = .5
    texp     = h[0].header['EXPTIME'] #sec
    mjd = mjd_start + (fraction * texp) / secinday

    return mjd, mjd0
Exemple #6
0
def mjd_fromheader(h):
    """
    return modified Julian date from header
    """
    
    datetu = h[0].header['DATE-OBS'] 
    mjd0,mjd,i = GLOBALutils.iau_cal2jd(int(datetu[:4]),int(datetu[5:7]),int(datetu[8:10]))
    ut = float(datetu[11:13]) + float(datetu[14:16])/60. + float(datetu[17:])/3600.
    mjd_start = mjd + ut/24.0

    secinday = 24*3600.0
    fraction = 0.5
    texp     = h[0].header['EXPTIME'] #sec

    mjd = mjd_start + (fraction * texp) / secinday

    return mjd, mjd0
Exemple #7
0
def mjd_fromheader(h):
    """
    return modified Julian date from header
    """
    
    datetu = h[0].header['HIERARCH ESO CORA SHUTTER START DATE'] 
    ut     = h[0].header['HIERARCH ESO CORA SHUTTER START HOUR']
    mjd0,mjd,i = GLOBALutils.iau_cal2jd(int(datetu[0:4]),int(datetu[4:6]),int(datetu[6:8]))

    mjd_start = mjd + ut/24.0

    secinday = 24*3600.0
    fraction = h[0].header['HIERARCH ESO CORA PM FLUX TMMEAN']
    texp     = h[0].header['HIERARCH ESO OBS TEXP'] #sec

    mjd = mjd_start + (fraction * texp) / secinday

    return mjd, mjd0
Exemple #8
0
def mjd_fromheader(h):
    """
    return modified Julian date from header
    """
    ih = 0
    if len(h)>1:
	ih = 1
    datetu = h[ih].header['UT-DATE'] 
    ut     = h[ih].header['UT-TIME']
    mjd0,mjd,i = GLOBALutils.iau_cal2jd(int(datetu[0:4]),int(datetu[5:7]),int(datetu[8:]))
    ut = ut/3600.
    mjd_start = mjd + ut/24.0
    secinday = 24*3600.0
    fraction = .5
    texp     = h[ih].header['EXPTIME'] #sec
    mjd = mjd_start + (fraction * texp) / secinday

    return mjd, mjd0
def mjd_fromheader(h):
    """
    return modified Julian date from header
    """
    secinday = 24*3600.0

    datetu   = h[0].header['DATE-OBS'][:10] 
    ut       = h[0].header['DATE-OBS'][11:]

    mjd0,mjd,i = GLOBALutils.iau_cal2jd(int(datetu[0:4]),int(datetu[5:7]),int(datetu[8:10]))

    ut        = (float(ut[:2])*3600. + float(ut[3:5])*60. + float(ut[6:]))
    mjd_start = mjd + ut / secinday

    fraction = h[0].header['HIERARCH ESO INS DET1 TMMEAN']
    texp     = h[0].header['EXPTIME'] #sec

    mjd = mjd_start + (fraction * texp) / secinday

    return mjd, mjd0
Exemple #10
0
def mjd_fromheader(h):
    """
    return modified Julian date from header
    """  
    datetu = h['DATE-OBS'].split('T')[0]
    timetu = h['DATE-OBS'].split('T')[1]
    mjd0,mjd,i = GLOBALutils.iau_cal2jd(int(datetu[:4]),int(datetu[5:7]),int(datetu[8:]))
    ho = int(timetu[:2])
    mi = int(timetu[3:5])
    se = float(timetu[7:])
    ut = float(ho) + float(mi)/60.0 + float(se)/3600.0
    mjd_start = mjd + ut/24.0

    secinday = 24*3600.0
    fraction = 0.5
    texp     = h['EXPTIME'] #sec

    mjd = mjd_start + (fraction * texp) / secinday

    return mjd, mjd0
Exemple #11
0
def mjd_fromheader2(hd):
    """
    return modified Julian date from header
    """

    secinday = 24*3600.0

    date    = hd['DATE-OBS']
    ut      = hd['UTC']		    
    
    datetu = date.replace('-',':')

    mjd0,mjd,i = GLOBALutils.iau_cal2jd(int(datetu[0:4]),int(datetu[5:7]),int(datetu[8:10]))
    ut        = (float(ut[:2])*3600. + float(ut[3:5])*60. + float(ut[6:]))
    mjd_start = mjd + ut / secinday

    fraction = 0.5
    texp     = float(hd['EXPTIME']) #sec

    mjd = mjd_start + (fraction * texp) / secinday

    return mjd, mjd0
Exemple #12
0
def mjd_fromheader(h):
    """
    return modified Julian date from header
    """
    secinday = 24 * 3600.0

    date = h[0].header['DATE']
    stime = h[0].header['UT_START']
    etime = h[0].header['UT_END']
    date = date[:10]

    if stime > etime:
        date = yesterday(date)
    hour = ra_from_sec(stime)

    datetu = date.replace('-', ':')
    ut = hour.replace('-', ':')
    datetu = date.replace(' ', '')
    ut = hour.replace(' ', '')
    mjd0, mjd, i = GLOBALutils.iau_cal2jd(int(datetu[0:4]), int(datetu[5:7]),
                                          int(datetu[8:10]))
    ut = (float(ut[:2]) * 3600. + float(ut[3:5]) * 60. + float(ut[6:]))
    mjd_start = mjd + ut / secinday
Exemple #13
0
def mjd_fromheader(h):
    """
    return modified Julian date from header
    """
    datetu = h[0].header['DATE-OBS'][:10]
    ut = h[0].header['DATE-OBS'][11:]
    mjd0, mjd, i = GLOBALutils.iau_cal2jd(int(datetu[0:4]), int(datetu[5:7]),
                                          int(datetu[8:10]))
    ut = float(ut[:2]) + float(ut[3:5]) / 60. + float(ut[6:]) / 3600.
    mjd_start = mjd + ut / 24.0

    #print 'Warning!!! adding 5 hrs to comute MJD due to problem in header! CHECK in future!!'
    #mjd_start += 5./24.

    secinday = 24 * 3600.0
    fraction = 0.5
    texp = h[0].header['EXPTIME']  #sec

    #print 'Warning!!! assuming that the provided date is at the end of the exposure!!! Check in the future!!'
    #mjd = mjd_start - (fraction * texp) / secinday

    mjd = mjd_start + (fraction * texp) / secinday

    return mjd, mjd0
Exemple #14
0
    pre_process = 1
else:
    print "\tPre-processing files found, going straight to extraction"
    pre_process = 0

if pre_process == 1:
    # Find orders & traces
    print "\tTracing echelle orders..."

    h = pyfits.open(dirin + stst)
    if len(h) > 1:
        h[0].data = h[1].data
    d = h[0].data
    d = mikeutils.OverscanTrim(d, bias_sec, ov_sec)
    d = d.T
    c_all, nord = GLOBALutils.get_them(d, ext_aperture, 4, mode=1)
    c_all = c_all[1:-1]
    nord = len(c_all)
    print '\t\t', nord, 'orders found in red CCD'

    hb = pyfits.open(dirin + 'b' + stst[1:])
    if len(h) > 1:
        hb[0].data = hb[1].data
    db = hb[0].data
    db = mikeutils.OverscanTrim(db, bias_sec, ov_sec)
    db = db.T
    c_all_b, nord_b = GLOBALutils.get_them(db, ext_aperture, 4, mode=1)
    print '\t\t', nord_b, 'orders found in blue CCD'

    # median combine list of ob flats
    print "\tMedian combining flat frames"
Exemple #15
0
    hdu = pyfits.PrimaryHDU( MasterBias )
    if (os.access(dirout+'MasterBias.fits',os.F_OK)):
        os.remove(dirout+'MasterBias.fits')
    hdu.writeto(dirout+'MasterBias.fits')
    print("\t\t-> Masterbias: done!")

    # median combine list of flats
    Flat, RO_fl, GA_fl = cafeutils.MedianCombine(flats, zero=dirout+'MasterBias.fits')
    hdu = pyfits.PrimaryHDU(Flat)
    if (os.access(dirout+'Flat.fits',os.F_OK)):
        os.remove(dirout+'Flat.fits')
    hdu.writeto(dirout+'Flat.fits')
    print("\t\t-> Masterflats: done!")

    print("\tTracing echelle orders...")
    c_all,nord = GLOBALutils.get_them(Flat, ext_aperture, trace_degree, maxords=-1, mode=1)
    c_all, nord = GLOBALutils.good_orders(c_all,nord,Flat.shape[0],Flat.shape[1],ext_aperture)
    if nord >= 90:
        c_all = c_all[:90]
        nord=90
    print('\t\t'+ str(nord)+' orders found.')

    # pickle traces
    trace_dict = {'c_all':c_all,
                  'nord':nord,
                  'GA_bias': GA_bias, 'RO_bias' : RO_bias,
                  'GA_fl': GA_fl, 'RO_fl': RO_fl}
    pickle.dump( trace_dict, open( dirout+"trace.pkl", 'w' ) )

else:
    trace_dict = pickle.load( open( dirout+"trace.pkl", 'r' ) )
Exemple #16
0
        os.remove(dirout + 'Flat.fits')
    hdu.writeto(dirout + 'Flat.fits')
    print "\t\t-> Masterflat: done!"
    #Flat = pyfits.getdata(dirout+'Flat.fits')
    #RO_flat, GA_flat = 1.,1.

    # Find orders & traces
    print "\tTracing echelle orders..."
    h = pyfits.open(dirin + stst)[0]
    hth = pyfits.getheader(dirin + stst)
    d = h.data
    d = pfsutils.OverscanTrim(d, bias_section, over_section)
    d -= MasterBias
    c_all, nord = GLOBALutils.get_them(d,
                                       ext_aperture - 1,
                                       trace_degree,
                                       mode=1,
                                       nsigmas=5)

    trace_dict = {
        'c_all': c_all,
        'nord': nord,
        'GA_bias': GA_bias,
        'RO_bias': RO_bias,
        'GA_flat': GA_flat,
        'RO_flat': RO_flat
    }
    pickle.dump(trace_dict, open(dirout + 'trace.pkl', 'w'))

else:
    print '\t\tLoading Masterbias, Masterflat and traces'
Exemple #17
0
if (pre_process == 1):
    # median combine list of flats
    print "\t\tGenerating Master calibration frames..."
    Flat, RO_fl, GA_fl = hiresutils.MedianCombine(flats,chip=chip)
    hdu = pyfits.PrimaryHDU(Flat)
    if (os.access(dirout+'Flat_' + str(int(chip)) + '.fits',os.F_OK)):
        os.remove(dirout+'Flat_' + str(int(chip)) + '.fits')
    hdu.writeto(dirout+'Flat_' + str(int(chip)) + '.fits')
    print "\t\t-> Masterflat: done!"

    hdu = pyfits.open(dirin+stst)
    d = hdu[chip].data
    d = hiresutils.OverscanTrim(d,hdu[chip].header['DATASEC'])
    d = d.T
    print "\tTracing echelle orders..."
    c_all, nord = GLOBALutils.get_them(d,ext_aperture+5,trace_degree,mode=1,nsigmas=10,nc2=1)
    print '\t\t'+str(nord)+' orders found...'

    # pickle traces
    trace_dict = {'c_all':c_all,
                  'nord':nord,
                  'GA_fl': GA_fl, 'RO_fl': RO_fl}
    pickle.dump( trace_dict, open( dirout+'trace_' + str(int(chip)) + '.pkl', 'w' ) )

else:
    trace_dict = pickle.load( open( dirout+'trace_' + str(int(chip)) + '.pkl' ) )
    c_all = trace_dict['c_all']
    nord  = trace_dict['nord']
    # recover GA*, RO*
    GA_fl = trace_dict['GA_fl']
    RO_fl = trace_dict['RO_fl']
Exemple #18
0
        for time in dark_times:
            group = []
            for dark in darks:
                hd = pyfits.getheader(dark)
                if hd['EXPTIME'] == time:
                    group.append(dark)
            if len(group)>2:
                dark_groups.append(group)
                ndark_times.append(hd['EXPTIME'])

        dark_times = ndark_times
        i = 0
        while i < len(dark_times):
            DARK, RON, GAIN = vbtutils.MedianCombine(dark_groups[i], zero_bo=True, zero=dirout+'MasterBias.fits',dark_bo=False)
            hdu = pyfits.PrimaryHDU( DARK )
            hdu = GLOBALutils.update_header(hdu,'EXPTIME',dark_times[i])
            if os.access(dirout+'DARK_'+str(int(dark_times[i]))+'s.fits',os.F_OK):
                os.remove(dirout+'DARK_'+str(int(dark_times[i]))+'s.fits')
            hdu.writeto( dirout+'DARK_'+str(int(dark_times[i]))+'s.fits' )
            MDARKS.append(dirout+'DARK_'+str(int(dark_times[i]))+'s.fits')
            i+=1
    print "\t\t-> Masterdarks: done!"

    Flat, RO_flat, GA_flat = vbtutils.MedianCombine(flats, zero_bo=True, dark_bo=False, flat_bo=False,zero=dirout+'MasterBias.fits')
    hdu = pyfits.PrimaryHDU( Flat )

    if (os.access(dirout+'MasterFlat.fits',os.F_OK)):
        os.remove(dirout+'MasterFlat.fits')
    hdu.writeto(dirout+'MasterFlat.fits')
    print "\t\t-> Masterflat: done!"
Exemple #19
0
        MasDarl = []

        for t in dark_times:
            sirven = []
            i = 0
            while i < len(darks):
                if each_time[i] == t:
                    sirven.append(darks[i])
                i += 1
            Mdark, roD, gaF = pucherosutils.MedianCombine(
                sirven, True, dirout + 'MasterBias.fits')
            nMdark = 'MasterDark_' + str(t) + 's.fits'
            if (os.access(dirout + nMdark, os.F_OK)):
                os.remove(dirout + nMdark)
            hdu = pyfits.PrimaryHDU(Mdark)
            hdu = GLOBALutils.update_header(hdu, 'EXPTIME', t)
            hdu.writeto(dirout + nMdark)
            MasDarl.append(dirout + nMdark)

        darks_dict = {'d_names': MasDarl, 'd_times': dark_times}
        pickle.dump(darks_dict, open(dirout + "darks.pkl", 'w'))
        print "\t\t-> Masterdarks: done!"

    else:
        dark_times = []
        MasDarl = []
        darks_dict = {'d_names': MasDarl, 'd_times': dark_times}
        pickle.dump(darks_dict, open(dirout + "darks.pkl", 'w'))
        print "\t\t-> 0 Masterdarks found!"

# median combine list of flats
Exemple #20
0
     (force_pre_process) ):
    print "\tNo previous pre-processing files or found"
    pre_process = 1
else:
    print "\tPre-processing files found, going straight to extraction"
    pre_process = 0

trace_file = dirout + 'trace.pkl'
if (pre_process == 1):
    #c_ob, nord_ob = GLOBALutils.get_them(Flat_ob, 10.,trace_degree,maxords=-1)
    #c_ob, nord_ob = FIDEOSutils.good_orders(c_ob,nord_ob,Flat_ob.shape[0],Flat_ob.shape[1],ext_aperture)
    #print nord_ob, 'object orders found'
    #c_co, nord_co = GLOBALutils.get_them(Flat_co, 10.,trace_degree,maxords=-1)
    #c_co, nord_co = FIDEOSutils.good_orders(c_co,nord_co,Flat_co.shape[0],Flat_co.shape[1],ext_aperture)
    #print nord_co, 'comparison orders found'
    MasterBias = GLOBALutils.MedianCombine_simple(biases, ZF=0.)
    hdu = pyfits.PrimaryHDU(MasterBias)
    if (os.access(dirout + 'MasterBias.fits', os.F_OK)):
        os.remove(dirout + 'MasterBias.fits')
    hdu.writeto(dirout + 'MasterBias.fits')

    MasterFlat = GLOBALutils.MedianCombine_simple(flats, ZF=MasterBias)
    MasterFlat = np.fliplr(MasterFlat.T)
    hdu = pyfits.PrimaryHDU(MasterFlat)
    if (os.access(dirout + 'MasterFlat.fits', os.F_OK)):
        os.remove(dirout + 'MasterFlat.fits')
    hdu.writeto(dirout + 'MasterFlat.fits')

    MasterFlat_co = GLOBALutils.MedianCombine_simple(flats_co, ZF=MasterBias)
    MasterFlat_co = np.fliplr(MasterFlat_co.T)
    hdu = pyfits.PrimaryHDU(MasterFlat_co)
Exemple #21
0
def fill_list_header(hdu, ofiles):
    for i in range(len(ofiles)):
        hdu = GLOBALutils.update_header(hdu, 'RAW' + str(i),
                                        ofiles[i].split('/')[-1])
    return hdu
Exemple #22
0
for i in range(nspec.shape[1]):
    npix = np.arange(nspec.shape[2])
    wav = np.polyval(coefs[i][1:][::-1], npix) * 10
    nspec[0, i, :] = wav
    spl = interpolate.splrep(np.arange(wav.shape[0]), wav, k=3)
    dlambda_dx = interpolate.splev(np.arange(wav.shape[0]), spl, der=1)
    NN = np.average(dlambda_dx)
    dlambda_dx /= NN
    nspec[9, i, :] = nspec[5, i, :] * (dlambda_dx**1)
    nspec[8, i, :] = 100.

ml_v = tlines - 0.01
mh_v = tlines + 0.01
weight = np.ones(len(tlines))
vels, xc_full, sn, nlines_ccf, W_ccf = \
                    GLOBALutils.XCor(nspec, ml_v, mh_v, weight, 0, 1., vel_width=300,vel_step=3.,\
                                          spec_order=9,iv_order=10,sn_order=8,max_vel_rough=300)
xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=3., Simple=True, W=W_ccf)
#plot(vels,xc_av)
#show()
j = np.median(xc_av)
im = np.argmax(xc_av)
rv = vels[im]

vels, xc_full, sn, nlines_ccf, W_ccf = \
                    GLOBALutils.XCor(nspec, ml_v, mh_v, weight, rv, 1., vel_width=20,vel_step=0.3,\
                                          spec_order=9,iv_order=10,sn_order=8,max_vel_rough=300)
xc_av = GLOBALutils.Average_CCF(xc_full, sn, sn_min=3., Simple=True, W=W_ccf)
cent = np.sum(xc_av * vels) / np.sum(xc_av)
print cent
#axvline(cent)
#plot(vels,xc_av-j)
Exemple #23
0
    Flat, RO_fl, GA_fl = uvesutils.MedianCombine(flats, bias=MasterBias)
    hdu = pyfits.PrimaryHDU(Flat)
    if (os.access(dirout + 'Flat.fits', os.F_OK)):
        os.remove(dirout + 'Flat.fits')
    hdu.writeto(dirout + 'Flat.fits')
    print "\t\t-> MasterFlat: done!"

    hdu = pyfits.open(orderref[0])
    d1 = hdu[1].data - MasterBias[:, :, 0]
    d2 = hdu[2].data - MasterBias[:, :, 1]
    d1 = d1.T
    d2 = d2.T
    print "\tTracing echelle orders..."
    c_all1, nord1 = GLOBALutils.get_them(d1,
                                         int(0.2 * ext_aperture),
                                         trace_degree,
                                         mode=1,
                                         nsigmas=10)
    print '\t\t' + str(nord1) + ' orders for chip1 found...'
    c_all2, nord2 = GLOBALutils.get_them(d2,
                                         int(0.2 * ext_aperture),
                                         trace_degree,
                                         mode=1,
                                         nsigmas=10,
                                         endat=2030)
    print '\t\t' + str(nord2) + ' orders for chip2 found...'

    trace_dict = {'c_all1':c_all1,'nord1':nord1,\
                  'c_all2':c_all2,'nord2':nord2,\
                  'GA_bias': GA_bias, 'RO_bias': RO_bias,\
                  'GA_fl': GA_fl, 'RO_fl': RO_fl}
Exemple #24
0
    # save this file for later reference
    hdu = pyfits.PrimaryHDU(FlatR)
    if (os.access(dirout + 'FlatR.fits', os.F_OK)):
        os.remove(dirout + 'FlatR.fits')
    hdu.writeto(dirout + 'FlatR.fits')
    hdu = pyfits.PrimaryHDU(FlatB)
    if (os.access(dirout + 'FlatB.fits', os.F_OK)):
        os.remove(dirout + 'FlatB.fits')
    hdu.writeto(dirout + 'FlatB.fits')

    # Find orders & traces
    print "\tTracing echelle orders..."
    c_all, nord = GLOBALutils.get_them(FlatB + FlatR,
                                       ext_aperture,
                                       trace_degree,
                                       maxords=-1,
                                       mode=1)
    print "\t\t\t", nord, 'orders found ...'

    trace_dict = {'c_all':c_all, 'nord':nord, 'GA_bias': GA_bias, 'RO_bias': RO_bias, \
                  'GA_flatB': GA_flatB, 'RO_flatB': RO_flatB, 'GA_flatR': GA_flatR, 'RO_flatR': RO_flatR}
    pickle.dump(trace_dict, open(dirout + "trace.pkl", 'w'))

else:
    print '\tLoading Masterbias, Masterflat and traces'
    trace_dict = pickle.load(open(dirout + "trace.pkl", 'r'))
    c_all = trace_dict['c_all']
    nord = trace_dict['nord']
    GA_bias = trace_dict['GA_bias']
    RO_bias = trace_dict['RO_bias']