Exemplo n.º 1
0
def applywavelengths(wavefile, applyfile, newname):
    #Read in file with wavelength solution and get header info
    wave = fits.open(wavefile)
    n_fr = float(wave[0].header['LINDEN'])
    n_fd = float(wave[0].header['CAMFUD'])
    fl = float(wave[0].header['FOCLEN'])
    zPnt = float(wave[0].header['ZPOINT'])

    #Read in file to apply wavelength solution and update header
    spec_data = fits.getdata(applyfile)
    spec_header = fits.getheader(applyfile)
    rt.Fix_Header(spec_header)
    spec_header.append(('LINDEN', n_fr, 'Line Desity for Grating Eq.'),
                       useblanks=True,
                       bottom=True)
    spec_header.append(
        ('CAMFUD', n_fd, 'Camera Angle Correction Factor for Grat. Eq.'),
        useblanks=True,
        bottom=True)
    spec_header.append(('FOCLEN', fl, 'Focal Length for Grat Eq.'),
                       useblanks=True,
                       bottom=True)
    spec_header.append(('ZPOINT', zPnt, 'Zero Point Pixel for Grat Eq.'),
                       useblanks=True,
                       bottom=True)
    NewspecHdu = fits.PrimaryHDU(data=spec_data, header=spec_header)

    #See if new file already exists
    mylist = [True for f in os.listdir('.') if f == newname]
    exists = bool(mylist)
    clob = False
    if exists:
        print 'File %s already exists.' % newname
        nextstep = raw_input(
            'Do you want to overwrite or designate a new name (overwrite/new)? '
        )
        if nextstep == 'overwrite':
            clob = True
            exists = False
        elif nextstep == 'new':
            newname = raw_input('New file name: ')
            exists = False
        else:
            exists = False
    NewspecHdu.writeto(newname, output_verify='warn', clobber=clob)
Exemplo n.º 2
0
complete task

'''

import numpy as np
import ReduceSpec_tools as rt
import spectools as st
import warnings
#import pyfits as fits
import astropy.io.fits as fits

task = raw_input('What would you like to do? (bias, flat, normalize, lacosmic, combine, trim, wavelength, details) ')

if task == 'combine':
    files = raw_input('Name of file containing images to combine: ')
    filelist = rt.Read_List(files)
    output_file = raw_input('Output file name: ')
    method = raw_input('Method to combine (median,average,sum): ')
    low_sig = float(raw_input('Low sigma clipping threshold: '))
    low_sig = np.abs(low_sig) #Ensure that this value is positive
    high_sig = float(raw_input('High sigma clipping threshold: '))

    rt.imcombine(filelist,output_file,method,lo_sig = low_sig, hi_sig = high_sig)

if task == 'trim':
    files = raw_input('Name of file to trim: ')
    rt.Trim_Spec(files)

if task == 'lacosmic':
    files = raw_input('Name of file containing images to combine: ')
    filelist = rt.Read_List(files)
Exemplo n.º 3
0
import ReduceSpec_tools as rt
import spectools as st
import warnings

task = raw_input('What would you like to do? (bias, flat, normalize, combine, trim, wavelength) ')

if task == 'combine':
    files = raw_input('Name of file containing images to combine: ')
    filelist = rt.Read_List(files)
    output_file = raw_input('Output file name: ')
    method = raw_input('Method to combine (median,average,sum): ')
    low_sig = float(raw_input('Low sigma clipping threshold: '))
    low_sig = np.abs(low_sig) #Ensure that this value is positive
    high_sig = float(raw_input('High sigma clipping threshold: '))

    rt.imcombine(filelist,output_file,method,lo_sig = low_sig, hi_sig = high_sig)

if task == 'trim':
    files = raw_input('Name of file to trim: ')
    rt.Trim_Spec(files)

if task == 'wavelength':
    Wavefile = raw_input('Name of file with wavelength solution: ')
    files = raw_input('Name of file to apply wavelength solution to: ')
    outputfile = raw_input('Output name of file: ')

    st.applywavelengths(Wavefile,files,outputfile)

if task == 'normalize':
    files = raw_input('Name of flat file to normalize: ')
    rt.Norm_Flat_Poly(files)
Exemplo n.º 4
0
 method = 'median' # method used to combine images 
 
 # If overwrite special comand is given # 
 if nargs >= 6:
     overwrite = args[5]
     warnings.filterwarnings('ignore', category=UserWarning, append=True)
 # If low_sigma and high_sigma values are given # 
 if nargs >= 8: 
     lo_sig = float(args[6])
     hi_sig = float(args[7]) 
 # If method is given #  
 if nargs >= 9:
     method = args[8]
     
 #Set up array to save for diagnostics. This is defined in rt.init()
 rt.init()
 
 # The rest of the code runs the reduction procces up to apall #  =========
 # Combine Zeros # 
 comb_zero = rt.imcombine(zero_lists[0], zero_names[0], 'average', lo_sig= 10, 
                     hi_sig= 3, overwrite= overwrite)
 
 # Bias Subtract Flats # 
 nf= len(flat_lists) # number of flats
 b_flat_lists= []
 i= 0
 while i < nf:
     b_flat_lists.append( rt.Bias_Subtract(flat_lists[i], comb_zero ) )
     i= i+1
 
 # Combine Bias Subtracted Flats # 
Exemplo n.º 5
0
def reduce_now(args):
    nargs = len(args)
    if (nargs < 4):
        print("\n====================\n")
        print("\nNot Enough Inputs.")
        print("Need at least 4 inputs: listZero, listFlat, listSpec, listFe")
        print("Optional inputs: overwrite= , low_sig= , high_sig=  ")
        print("Example:")
        print("\n>>> python imcombine.py listZero listFlat listSpec listFe \n")
        print("\n====================\n")

    # Unpack list from command line and combe trough them for diffrent observations #
    scriptname = args[0]
    zero_lists = rt.List_Combe(rt.Read_List(args[1]))
    flat_lists = rt.List_Combe(rt.Read_List(args[2]))
    spec_lists = rt.List_Combe(rt.Read_List(args[3]))

    # Select names from the first image of each observation #
    zero_names = []
    for zero in zero_lists:
        zero_names.append(zero[0][5:])
    flat_names = []
    for flat in flat_lists:
        flat_names.append(flat[0][5:])
    spec_names = []
    for spec in spec_lists:
        spec_names.append(spec[0][5:])

    # Default values for special commands if none are given these dont change #
    overwrite = True  # dont give imcombine permision to overwrite files #
    lo_sig = 10
    hi_sig = 3
    method = 'median'  # method used to combine images

    # If overwrite special comand is given #
    if nargs >= 6:
        overwrite = args[5]
        warnings.filterwarnings('ignore', category=UserWarning, append=True)
    # If low_sigma and high_sigma values are given #
    if nargs >= 8:
        lo_sig = float(args[6])
        hi_sig = float(args[7])
    # If method is given #
    if nargs >= 9:
        method = args[8]

    #Set up array to save for diagnostics. This is defined in rt.init()
    rt.init()

    # The rest of the code runs the reduction procces up to apall #  =========
    # Combine Zeros #
    comb_zero = rt.imcombine(zero_lists[0],
                             zero_names[0],
                             'average',
                             lo_sig=10,
                             hi_sig=3,
                             overwrite=overwrite)

    # Bias Subtract Flats #
    nf = len(flat_lists)  # number of flats
    b_flat_lists = []
    i = 0
    while i < nf:
        b_flat_lists.append(rt.Bias_Subtract(flat_lists[i], comb_zero))
        i = i + 1

    # Combine Bias Subtracted Flats #
    i = 0
    comb_flat = []
    while i < nf:
        comb_flat.append(
            rt.imcombine(b_flat_lists[i],
                         'b.' + flat_names[i],
                         'median',
                         lo_sig=10,
                         hi_sig=3,
                         overwrite=overwrite))
        i = i + 1

    # Normalize Flat #
    i = 0
    nb_flat = []
    while i < nf:
        nb_flat.append(rt.Norm_Flat_Avg(
            comb_flat[i]))  # (divide by average of counts)
        i = i + 1

    print('tennisten')
    # Bias Subtract Spec #
    i = 0
    b_spec_list = []
    nsp = len(spec_lists)
    # number of spectra
    while i < nsp:
        b_spec_list.append(rt.Bias_Subtract(spec_lists[i], comb_zero))
        i = i + 1

    # Flat Field Individual Spectra #
    i = 0
    ftb_spec_list = []

    tb_spec_list = rt.List_Combe(b_spec_list)

    print(tb_spec_list[i])
    print(type(tb_spec_list[i]))

    #tb_spec_list = rt.List_Combe(b_spec_list)
    tb_spec_list = b_spec_list

    while i < nsp:
        ftb_spec_list.append(rt.Flat_Field(tb_spec_list[i], nb_flat[0]))
        i = i + 1
    '''
    blueindex = [i for i, s in enumerate(nb_flat) if 'blue' in s.lower()]
    nbflatblue = nb_flat[blueindex[0]]
    redindex = [i for i, s in enumerate(nb_flat) if 'red' in s.lower()]
    if len(redindex) > 0:
        nbflatred = nb_flat[redindex[0]]
    i= 0
    ftb_spec_list = []
    tb_spec_list = rt.List_Combe(b_spec_list)
    while i < nsp:
        if tb_spec_list[i][0].lower().__contains__('blue') == True:
            ftb_spec_list.append( rt.Flat_Field(tb_spec_list[i], nbflatblue) )
        elif tb_spec_list[i][0].lower().__contains__('red') == True:
            ftb_spec_list.append( rt.Flat_Field(tb_spec_list[i], nbflatred) )
        else: 
            print("Problem applying the Flats.")
            print("Could not identify blue or red setup.")
        i= i+1
         tb_spec_list = rt.List_Combe(b_spec_list)
  
    print(tb_spec_list[i])
    print(type(tb_spec_list[i]))
    '''

    # Save all diagnostic info
    rt.save_diagnostic()

    #LA Cosmic
    i = 0
    cftb_spec = []
    cftb_mask = []
    while i < nsp:
        m = 0
        while m < len(ftb_spec_list[i]):
            lacos_spec, lacos_mask = rt.lacosmic(ftb_spec_list[i][m])
            cftb_spec.append(lacos_spec)
            cftb_mask.append(lacos_mask)
            m += 1
        i += 1

    cftb_spec_list = rt.List_Combe(cftb_spec)
    cftb_mask_list = rt.List_Combe(cftb_mask)

    print("Done. Ready for Apeture Extraction.\n")
def calibrate_now(lamp,
                  zz_specname,
                  fit_zpoint,
                  zzceti,
                  offset_file,
                  plotall=True):
    # Read Lamp Data and Header #
    lamp_data = fits.getdata(lamp)
    lamp_header = fits.getheader(lamp)

    # Check number of image slices, and select the spectra #
    if lamp_header["NAXIS"] == 2:
        lamp_spec = lamp_data[0]
    elif lamp_header["NAXIS"] == 3:
        lamp_spec = lamp_data[0][0]
    else:
        print("\nDont know which data to unpack.")
        print("Check the array dimensions\n")

    # plt.figure(1)
    # plt.plot(lamp_spec)
    # plt.title('Raw')
    # plt.show()

    # Find the pixel number offset due to trim reindexing #
    trim_sec = lamp_header["CCDSEC"]
    trim_offset = float(trim_sec[1:len(trim_sec) - 1].split(':')[0]) - 1

    # Find Bining #
    try:
        bining = float(lamp_header["PARAM18"])
    except:
        bining = float(lamp_header["PG3_2"])
    # Get Pixel Numbers #
    nx = np.size(lamp_spec)
    Pixels = bining * (np.arange(0, nx, 1) + trim_offset)

    # Select Set of Parameters to use #
    global parm
    if lamp.lower().__contains__('red'):
        parm = Param_930_20_40
        line_list = WaveList_Fe_930_20_40
    elif lamp.lower().__contains__('blue'):
        parm = Param_930_12_24
        line_list = WaveList_Fe_930_12_24
    else:
        print "Could not detect setup!"

    # Calculate Initial Guess Solution # ========================================

    alpha = float(lamp_header["GRT_TARG"])
    theta = float(lamp_header["CAM_TARG"])
    Wavelengths = DispCalc(Pixels, alpha, theta, parm[0], parm[1], parm[2],
                           parm[3])

    # Ask for offset # ===========================================================
    print offset_file
    if offset_file:
        print 'Using offset file: ', offset_file
        offsets = np.genfromtxt(offset_file, dtype='d')
        if offsets.size == 1:
            offsets = np.array([offsets])
        #print offsets
        if 'blue' in lamp.lower():
            offset = offsets[0]
        elif 'red' in lamp.lower():
            offset = offsets[1]
        Wavelengths = [w + offset for w in Wavelengths]
    else:
        # Plot Dispersion #
        plt.figure(1)
        plt.plot(Wavelengths, lamp_spec)
        plt.hold('on')
        for line in line_list[1]:
            if (Wavelengths[0] <= line <= Wavelengths[-1]):
                plt.axvline(line, color='r', linestyle='--')
        plt.title(
            "Initial Dispersion Inspection Graph. \nClose to Calculate Offset")
        plt.xlabel("Wavelengths")
        plt.ylabel("Counts")
        plt.hold('off')
        plt.show()

        print "\nWould You like to set Offset?"
        yn = raw_input('yes/no? >>> ')

        #yn= 'yes'
        if yn == 'yes':
            global ax, fig, coords
            fig = plt.figure(1)
            ax = fig.add_subplot(111)
            ax.plot(Wavelengths, lamp_spec)
            plt.hold('on')
            for line in line_list[1]:
                if (Wavelengths[0] <= line <= Wavelengths[-1]):
                    plt.axvline(line, color='r', linestyle='--')
            plt.title(
                "First click known line(red), then click coresponding peak near center\n Then close graph."
            )
            plt.xlabel("Wavelengths (Ang.)")
            plt.ylabel("Counts")
            if lamp.__contains__('blue'):
                plt.xlim(4700., 4900.)
            elif lamp.__contains__('red'):
                plt.xlim(6920., 7170.)
            plt.hold('off')
            coords = []
            cid = fig.canvas.mpl_connect('button_press_event', onclick)
            plt.show()

            k_line = find_near(
                coords[0][0], line_list[1])  # Nearest line to click cordinates
            k_peak = find_near(coords[1][0],
                               Wavelengths)  # Nearest Peak to click cordinates
            i_peak = Wavelengths.index(k_peak)
            X = Wavelengths[i_peak - 7:i_peak + 7]
            Y = lamp_spec[i_peak - 7:i_peak + 7]
            amp, center, width, b = fit_Gauss(X, Y)
            offset = (k_line - center)
            ##########
            #Save the offset
            print '\n Would you like to save the offset?'
            save_offset = raw_input('yes/no? >>> ')
            if save_offset == 'yes':
                print 'Saving offset to offsets.txt'
                g = open('offsets.txt', 'a')
                g.write(str(offset) + '\n')
                g.close()
            ##########
            Wavelengths = [w + offset for w in Wavelengths]

            plt.figure(1)
            plt.plot(Wavelengths, lamp_spec)
            plt.hold('on')
            for line in line_list[1]:
                if (Wavelengths[0] <= line <= Wavelengths[-1]):
                    plt.axvline(line, color='r', linestyle='--')
            plt.title("Offset Applied.")
            plt.xlabel("Wavelengths (Ang.)")
            plt.ylabel("Counts")
            plt.hold('off')
            plt.show()
        else:
            offset = 0.

    # Ask Refit # ===============================================================
    yn = 'yes'
    while yn == 'yes':

        #print "\nWould you like to refit and recalculate dispersion?"
        #yn= raw_input('yes/no? >>> ')
        yn = 'yes'
        if yn == 'yes':
            #print "\nOffset to apply to Grating Angle?"
            #alpha_offset= float( raw_input('Offset Value? >>>') )
            alpha_offset = 0.
            #alpha= alpha + alpha_offset
            '''
            #Uncomment this part if you would like to select lines to use by hand. Otherwise, all lines in the above line lists are used.
            fig = plt.figure(1)
            ax = fig.add_subplot(111)
            ax.plot(Wavelengths, lamp_spec)
            plt.hold('on')
            lines_in_range= []
            for line in line_list[1]:
                if (Wavelengths[0] <= line <= Wavelengths[-1]):
                    lines_in_range.append(line)
                    plt.axvline(line, color= 'r', linestyle= '--')
            plt.title("Click on The Peaks You Want to Use to Refit \n Then close graph.")
            plt.xlim([np.min(lines_in_range)-50, np.max(lines_in_range)+50])
            plt.ylim([np.min(lamp_spec)-100, np.max(lamp_spec)/2])
            plt.xlabel("Wavelengths (Ang.)")
            plt.ylabel("Counts")
            plt.hold('off')
            coords= [] 
            cid = fig.canvas.mpl_connect('button_press_event', onclick)
            plt.show()    
            '''
            ###n_pnt, n_cor= np.shape(coords)
            ###coord_x= [coords[i][0] for i in range(0,n_pnt)]
            coord_x = line_list[
                1]  #Use all lines in the line lists for the refitting.
            n_pnt = len(coord_x)

            peak_x = []
            for i in range(0, n_pnt):
                x = find_near(coord_x[i], Wavelengths)
                peak_x.append(x)
            centers_in_wave = find_peak_centers(peak_x, Wavelengths, lamp_spec)
            centers_in_wave = [w - offset for w in centers_in_wave]
            centers_in_pix = PixCalc(centers_in_wave, alpha, theta, parm[0],
                                     parm[1], parm[2], parm[3])

            known_waves = []
            for i in range(0, n_pnt):
                x = find_near(coord_x[i], line_list[1])
                known_waves.append(x)

            #Create array to save data for diagnostic purposes
            global savearray, n_fr, n_fd, n_zPnt
            savearray = np.zeros([len(Wavelengths), 8])
            #n_fr, n_fd, n_zPnt= fit_Grating_Eq(centers_in_pix, known_waves, alpha, theta, parm)
            par, rmsfit = fit_Grating_Eq(centers_in_pix,
                                         known_waves,
                                         alpha,
                                         theta,
                                         parm,
                                         plotalot=plotall)
            n_fr, n_fd, n_zPnt = par
            n_Wavelengths = DispCalc(Pixels, alpha - alpha_offset, theta, n_fr,
                                     n_fd, parm[2], n_zPnt)

            if plotall:
                plt.figure(1)
                plt.plot(n_Wavelengths, lamp_spec)
                plt.hold('on')
                for line in line_list[1]:
                    if (n_Wavelengths[0] <= line <= n_Wavelengths[-1]):
                        plt.axvline(line, color='r', linestyle='--')
                plt.title("Refitted Solution")
                plt.xlabel("Wavelengths (Ang.)")
                plt.ylabel("Counts")
                plt.hold('off')

            savearray[0:len(n_Wavelengths), 2] = n_Wavelengths
            savearray[0:len(lamp_spec), 3] = lamp_spec
            savearray[0:len(np.array(line_list[1])),
                      4] = np.array(line_list[1])
            '''   
            plt.figure(2)
            Diff= [ (Wavelengths[i]-n_Wavelengths[i]) for i in range(0,np.size(Wavelengths)) ]
            plt.plot(Diff, '.')
            plt.title("Diffence between old and new solution.")
            plt.xlabel("Pixel")
            plt.ylabel("old-new Wavelength (Ang.)")
            '''

            plt.show()
        if ('blue' in lamp.lower()) and (rmsfit > 1.0):
            coord_list_short = line_list[0][1:]
            wave_list_short = line_list[1][1:]
            line_list = np.array([coord_list_short, wave_list_short])
            print 'Refitting without first line.'
            yn = 'yes'
        else:
            yn = 'no'  #Don't refit again

    # Save parameters in header and write file #
    #print "\nWrite solution to header?"
    #yn= raw_input("yes/no? >>>")
    print '\n Writing solution to header'
    yn = 'yes'
    if yn == "yes":
        newname = 'w' + lamp
        mylist = [True for f in os.listdir('.') if f == newname]
        exists = bool(mylist)
        clob = False
        if exists:
            print 'File %s already exists.' % newname
            nextstep = raw_input(
                'Do you want to overwrite or designate a new name (overwrite/new)? '
            )
            if nextstep == 'overwrite':
                clob = True
                exists = False
            elif nextstep == 'new':
                newname = raw_input('New file name: ')
                exists = False
            else:
                exists = False

        rt.Fix_Header(lamp_header)
        lamp_header.append(('LINDEN', n_fr, 'Line Desity for Grating Eq.'),
                           useblanks=True,
                           bottom=True)
        lamp_header.append(
            ('CAMFUD', n_fd, 'Camera Angle Correction Factor for Grat. Eq.'),
            useblanks=True,
            bottom=True)
        lamp_header.append(('FOCLEN', parm[2], 'Focal Length for Grat Eq.'),
                           useblanks=True,
                           bottom=True)
        lamp_header.append(('ZPOINT', n_zPnt, 'Zero Point Pixel for Grat Eq.'),
                           useblanks=True,
                           bottom=True)
        lamp_header.append(('RMSWAVE', rmsfit, 'RMS from Wavelength Calib.'),
                           useblanks=True,
                           bottom=True)
        NewHdu = fits.PrimaryHDU(data=lamp_data, header=lamp_header)
        NewHdu.writeto(newname, output_verify='warn', clobber=clob)

    #Save parameters to ZZ Ceti spectrum#
    #print "\nWrite solution to header of another spectrum?"
    #yn= raw_input("yes/no? >>>")
    if zz_specname:
        #specname = raw_input("Filename: ")
        #fitspectrum = raw_input('Would you like to fit a new zero point using a spectral line? (yes/no) ')
        if fit_zpoint == 'yes':
            newzeropoint = WaveShift(zz_specname, zzceti, plotall)
        else:
            newzeropoint = n_zPnt
        spec_data = fits.getdata(zz_specname)
        spec_header = fits.getheader(zz_specname)
        rt.Fix_Header(spec_header)
        spec_header.append(('LINDEN', n_fr, 'Line Desity for Grating Eq.'),
                           useblanks=True,
                           bottom=True)
        spec_header.append(
            ('CAMFUD', n_fd, 'Camera Angle Correction Factor for Grat. Eq.'),
            useblanks=True,
            bottom=True)
        spec_header.append(('FOCLEN', parm[2], 'Focal Length for Grat Eq.'),
                           useblanks=True,
                           bottom=True)
        spec_header.append(
            ('ZPOINT', newzeropoint, 'Zero Point Pixel for Grat Eq.'),
            useblanks=True,
            bottom=True)
        spec_header.append(('RMSWAVE', rmsfit, 'RMS from Wavelength Calib.'),
                           useblanks=True,
                           bottom=True)
        NewspecHdu = fits.PrimaryHDU(data=spec_data, header=spec_header)

        newname = 'w' + zz_specname
        mylist = [True for f in os.listdir('.') if f == newname]
        exists = bool(mylist)
        clob = False
        if exists:
            print 'File %s already exists.' % newname
            nextstep = raw_input(
                'Do you want to overwrite or designate a new name (overwrite/new)? '
            )
            if nextstep == 'overwrite':
                clob = True
                exists = False
            elif nextstep == 'new':
                newname = raw_input('New file name: ')
                exists = False
            else:
                exists = False
        NewspecHdu.writeto(newname, output_verify='warn', clobber=clob)

    #Save arrays for diagnostics
    now = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M")
    endpoint = '.ms'
    with open(
            'wavecal_' + zz_specname[4:zz_specname.find(endpoint)] + '_' +
            now + '.txt', 'a') as handle:
        header = lamp + ',' + zz_specname + '\n First 2 columns: fitted wavelengths, residuals \n Next 3 columns: wavelengths, flux, lambdas fit \n Final 3 columns: wavelengths, sky flux, fit to line for recentering'
        np.savetxt(handle, savearray, fmt='%f', header=header)
Exemplo n.º 7
0
def reduce_now(args):
    nargs = len(args)
    if (nargs < 5):
        print "\n====================\n"
        print "\nNot Enough Inputs."
        print "Need at least 4 inputs: listZero, listFlat, listSpec, listFe"
        print "Optional inputs: overwrite= , low_sig= , high_sig=  "
        print "Example:"
        print "\n>>> python imcombine.py listZero listFlat listSpec listFe \n"
        print "\n====================\n"

    # Unpack list from command line and combe trough them for diffrent observations #
    scriptname = args[0]
    zero_lists = rt.List_Combe(rt.Read_List(args[1]))
    flat_lists = rt.List_Combe(rt.Read_List(args[2]))
    spec_lists = rt.List_Combe(rt.Read_List(args[3]))
    fe_lists = rt.List_Combe(rt.Read_List(args[4]))

    # Select names from the first image of each observation #
    zero_names = []
    for zero in zero_lists:
        zero_names.append(zero[0][5:])
    flat_names = []
    for flat in flat_lists:
        flat_names.append(flat[0][5:])
    spec_names = []
    for spec in spec_lists:
        spec_names.append(spec[0][5:])
    fe_names = []
    for lamp in fe_lists:
        fe_names.append(lamp[0][5:])

    # Default values for special commands if none are given these dont change #
    overwrite = False  # dont give imcombine permision to overwrite files #
    lo_sig = 10
    hi_sig = 3
    method = 'median'  # method used to combine images

    # If overwrite special comand is given #
    if nargs >= 6:
        overwrite = args[5]
        warnings.filterwarnings('ignore', category=UserWarning, append=True)
    # If low_sigma and high_sigma values are given #
    if nargs >= 8:
        lo_sig = float(args[6])
        hi_sig = float(args[7])
    # If method is given #
    if nargs >= 9:
        method = args[8]

    #Set up array to save for diagnostics. This is defined in rt.init()
    rt.init()

    #Check ADC status during observations
    adc_status = rt.adcstat(spec_lists[0][0])

    # The rest of the code runs the reduction procces up to apall #  =========
    # Combine Zeros #
    comb_zero = rt.imcombine(zero_lists[0],
                             zero_names[0],
                             'average',
                             lo_sig=10,
                             hi_sig=3,
                             overwrite=overwrite)

    # Bias Subtract Flats #
    nf = len(flat_lists)  # number of flats
    b_flat_lists = []
    i = 0
    while i < nf:
        b_flat_lists.append(rt.Bias_Subtract(flat_lists[i], comb_zero))
        i = i + 1

    # Combine Bias Subtracted Flats #
    i = 0
    comb_flat = []
    while i < nf:
        comb_flat.append(
            rt.imcombine(b_flat_lists[i],
                         'b.' + flat_names[i],
                         'median',
                         lo_sig=10,
                         hi_sig=3,
                         overwrite=overwrite))
        i = i + 1

    #Trim flats#
    tcomb_flat = []
    i = 0
    while i < nf:
        tcomb_flat.append(rt.Trim_Spec(comb_flat[i]))
        i = i + 1
    '''
    # Normalize Flat # 
    i= 0
    nb_flat1= []
    nb_flat= []
    while i < nf:
        nb_flat.append( rt.Norm_Flat_Poly(tcomb_flat[i], 4.) ) # (divide by average of counts)
        #nb_flat.append(rt.Norm_Flat_Boxcar(nb_flat1[0]))
        i= i+1
    '''
    # Normalize Flat #
    i = 0
    nb_flat = []
    while i < nf:
        if 'blue' in tcomb_flat[i].lower():
            nb_flat.append(
                rt.Norm_Flat_Boxcar_Multiples(tcomb_flat[i],
                                              adc_stat=adc_status))
        else:
            if 'quartz' in tcomb_flat[i].lower():
                nb_flat.append(rt.Norm_Flat_Poly(tcomb_flat[i], 4.))
            else:
                flat_temp = []
                flat_temp.append(rt.Norm_Flat_Poly(tcomb_flat[i], 3.))
                nb_flat.append(rt.Norm_Flat_Boxcar(flat_temp[0]))
        #nb_flat.append( rt.Norm_Flat_Poly(tcomb_flat[i]) ) # (divide by average of counts)
        #nb_flat.append(rt.Norm_Flat_Boxcar(nb_flat1[i]))
        #nb_flat.append(rt.Norm_Flat_Boxcar_Multiples(tcomb_flat[i]))
        i = i + 1

    # Bias Subtract Spec #
    i = 0
    b_spec_list = []
    nsp = len(spec_lists)
    # number of spectra
    while i < nsp:
        b_spec_list.append(rt.Bias_Subtract(spec_lists[i], comb_zero))
        i = i + 1

    #Trim Spectra#
    tb_spec_list = []
    i = 0
    while i < nsp:
        for x in range(0, len(b_spec_list[i])):
            tb_spec_list.append(rt.Trim_Spec(b_spec_list[i][x]))
        i = i + 1

    # Flat Field Individual Spectra #
    blueindex = [i for i, s in enumerate(nb_flat) if 'blue' in s.lower()]
    nbflatblue = nb_flat[blueindex[0]]
    redindex = [i for i, s in enumerate(nb_flat) if 'red' in s.lower()]
    if len(redindex) > 0:
        nbflatred = nb_flat[redindex[0]]
    i = 0
    ftb_spec_list = []
    tb_spec_list = rt.List_Combe(tb_spec_list)
    while i < nsp:
        if tb_spec_list[i][0].lower().__contains__('blue') == True:
            ftb_spec_list.append(rt.Flat_Field(tb_spec_list[i], nbflatblue))
        elif tb_spec_list[i][0].lower().__contains__('red') == True:
            ftb_spec_list.append(rt.Flat_Field(tb_spec_list[i], nbflatred))
        else:
            print("Problem applying the Flats.")
            print("Could not identify blue or red setup.")
        i = i + 1

    # Save all diagnostic info
    rt.save_diagnostic()

    #LA Cosmic
    i = 0
    cftb_spec = []
    cftb_mask = []
    while i < nsp:
        m = 0
        while m < len(ftb_spec_list[i]):
            lacos_spec, lacos_mask = rt.lacosmic(ftb_spec_list[i][m])
            cftb_spec.append(lacos_spec)
            cftb_mask.append(lacos_mask)
            m += 1
        i += 1

    cftb_spec_list = rt.List_Combe(cftb_spec)
    cftb_mask_list = rt.List_Combe(cftb_mask)

    # Combine Spectra #
    i = 0
    comb_fb_spec = []
    while i < nsp:
        rt.checkspec(cftb_spec_list[i])
        comb_fb_spec.append(
            rt.imcombine(cftb_spec_list[i],
                         'cftb.' + spec_names[i],
                         'average',
                         lo_sig=10,
                         hi_sig=3,
                         overwrite=overwrite,
                         mask=cftb_mask_list[i]))
        i = i + 1

    print "\n====================\n"

    #########################################
    # Combine Fe lamps #
    print "Combining and trimming Fe lamps."
    nf = len(fe_lists)  #number of fe lamps
    i = 0
    comb_lamp = []
    while i < nf:
        comb_lamp.append(
            rt.imcombine(fe_lists[i],
                         fe_names[i],
                         'average',
                         lo_sig=lo_sig,
                         hi_sig=hi_sig,
                         overwrite=overwrite))
        i = i + 1

    # Trim lamps #
    i = 0
    while i < nf:
        rt.Trim_Spec(comb_lamp[i])
        i = i + 1

    ########################################

    print "Done. Ready for Apeture Extraction.\n"