def fitContinuum(continuuminter, tempInter, grating):
    """
    Fit a continuum to the telluric correction spectrum to normalize it. The continuum
    fitting regions were derived by eye and can be improved.

    Results are in fit<Grating>.fits
    """
    # These were found to fit the curves well by hand. You can probably improve them; feel free to fiddle around!
    if grating == "K":
        order = 5
        sample = "20279:20395,20953:24283"
    elif grating == "J":
        order = 5
        sample = "11561:12627,12745:12792,12893:13566"
    elif grating == "H":
        order = 5
        sample = "*"
    elif grating == "Z":
        order = 5
        sample = "9453:10015,10106:10893,10993:11553"
    if os.path.exists("fit.fits"):
        os.remove("fit.fits")
    iraf.continuum(input='final_tel_no_hlines_no_norm',output='fit',ask='yes',lines='*',bands='1',type="fit",replace='no',wavescale='yes',logscale='no',override='no',listonly='no',logfiles='',inter=continuuminter,sample=sample,naverage=1,func='spline3',order=order,low_rej=1.0,high_rej=3.0,niterate=2,grow=1.0,markrej='yes',graphics='stdgraph',cursor='',mode='ql')
    # Plot the telluric correction spectrum with the continuum fit.
    final_tel_no_hlines_no_norm = astropy.io.fits.open('final_tel_no_hlines_no_norm.fits')[0].data
    fit = astropy.io.fits.open('fit.fits')[0].data
    if continuuminter or tempInter:
        plt.title('Unnormalized Telluric Correction and Continuum fit Used to Normalize')
        plt.plot(final_tel_no_hlines_no_norm)
        plt.plot(fit)
        plt.show()
def normalize(fn):
    iraf.noao()
    iraf.onedspec()
    for i in range(len(fn)):
        if fn[i][-1] == 'L':
            nord = '10'
        else:
            nord = '7'
        iraf.continuum(fn[i], fn[i], order=nord, ask='no', logfile='norm_log')
def normalize(setup, filelist):
    iraf.noao()
    iraf.onedspec()
    for i in (filelist):
        name = setup + '/' + i
        iraf.continuum(name,
                       name,
                       order=1,
                       ask='no',
                       logfile=setup + '/norm_log')
def combine_normalize_images(WORK_DIR, combine=False):
  
  if combine:
    print '\n + Combine images\n'
    try: 
        os.remove('combined_sum.fits')       
    except: pass 
    iraf.scombine(input=','.join([obj+'.ec.vh' for obj in observations[WORK_DIR]['objects']]), output='combined_sum', group='apertures', combine='sum', reject='none', Stdout="/dev/null")

  # normalize spectra
  # This step will require you to manually normalize all the spectra.
  print '\n + Normalize spectra\n'

  if combine:
    try: 
        os.remove('combined_sum_cone_rv_echellet.fits')       
    except: pass 
    iraf.continuum(input='combined_sum', output='combined_sum_cont', type='fit', replace='no', listonly='no', functio='cheb', order=13, low_rej=2, high_rej=3, naverag=-3, niter=9, interac='no', markrej='no')
  else:  
    for obj in observations[WORK_DIR]['objects']:
      try: 
        os.remove(obj+'_cont.fits')
        os.remove(obj+'_vh_norm.fits')
      except: pass 
      iraf.continuum(input=obj+'.ec.vh', output=obj+'_cont', type='fit', replace='no', listonly='no', functio='cheb', order=13, low_rej=2, high_rej=3, naverag=-3, niter=9, interac='no', markrej='no', ask='yes')
      iraf.sarith(input1=obj+'.ec.vh', op='/', input2=obj+'_cont', output=obj+'_vh_norm', format='multispec', Stdout="/dev/null")

  #combine apertures
  print '\n + Combine apertures\n'

  if combine:
    try: 
        os.remove('combined_sum_data.fits')       
        os.remove('combined_sum_norm.fits')    
        os.remove('combined_final.0001.fits')       
    except: pass
    iraf.scombine(input='combined_sum', output='combined_sum_data', group='all', combine='sum', reject='none', Stdout="/dev/null")
    iraf.scombine(input='combined_sum_cont', output='combined_sum_norm', group='all', combine='sum', reject='none', Stdout="/dev/null")

    iraf.sarith(input1='combined_sum_data', op='/', input2='combined_sum_norm', output='combined_final', format='onedspec', Stdout="/dev/null")
  else:  
    for obj in observations[WORK_DIR]['objects']:
      try: 
        os.remove(obj+'_data1D.fits')
        os.remove(obj+'_cont1D.fits')
        os.remove(obj+'_1D_vh_norm.0001.fits')   
        os.remove(obj+'_1D_vh_norm.fits')        
      except: pass 
      iraf.scombine(input=obj+'.ec.vh', output=obj+'_data1D', group='all', combine='sum', reject='none', Stdout="/dev/null")
      iraf.scombine(input=obj+'_cont', output=obj+'_cont1D', group='all', combine='sum', reject='none', Stdout="/dev/null")

      iraf.sarith(input1=obj+'_data1D', op='/', input2=obj+'_cont1D', output=obj+'_1D_vh_norm', format='onedspec', Stdout="/dev/null")
def divide_smooth(input_file):
    os.system("rm smoothdiv*_"+input_file)

    print wave_w1,wave_w2
    
    iraf.sarith(
        input1 = "spec_" + input_file,\
        op = "/",\
        input2 = "master_smooth.fits",\
        output = "smoothdivtemp_"+input_file,\
        w1 = wave_w1,\
        w2 = wave_w2,\
        apertures = "*",\
        bands = "",\
        beams = "",\
        apmodulus = 0,\
        reverse = 0,\
        ignoreaps = 1,\
        format = "multispec",\
        renumber = 1,\
        offset = 0,\
        clobber = 1,\
        merge = 0,\
        rebin = 1,\
        errval = 0.0,\
        verbose = 1)

    ### Apply continuum to remove artifacts from the division
    iraf.continuum(
        input = "smoothdivtemp_" + input_file,\
        output = "smoothdiv_" + input_file,\
        ask = "no",\
        lines = "*",\
        bands = "1",\
        type = "data",\
        replace = 1,\
        wavescale = 1,\
        logscale = 0,\
        override = 1,\
        listonly = 0,\
        logfiles = "logfile",\
        interactive = 0,\
        sample = "*",\
        naverage = 1,\
        function = "spline3",\
        order = 10,\
        low_reject = 30.0,\
        high_reject = 30.0,\
        niterate = 1,\
        grow = 5.0)
Exemple #6
0
    def ContinuumTask(self, InputFile, OutputFile, Fits_Folder, Suffix = 'N'):
        
        #Incase no output name is given, we generate one with the provided "Suffix" (The defaul one is N)
        if OutputFile == None:
            OutputFile = self.outputNameGenerator(InputFile, Suffix)
            
        continuum_conf = self.ContinuumAttributes(InputFile, OutputFile, Fits_Folder)

        #Display the equivalent command in IRAF
        Command = self.printIrafCommand('continuum', continuum_conf, printindividually=False)
        print '--Using command'
        print Command

        
        iraf.continuum(**continuum_conf)
Exemple #7
0
def continumsub(imagefile, _order1, _order2):
    # print "LOGX:: Entering `continumsub` method/function in %(__file__)s" %
    # globals()
    from pyraf import iraf
    iraf.noao(_doprint=0)
    iraf.imred(_doprint=0)
    iraf.specred(_doprint=0)
    toforget = ['specred.continuum']
    for t in toforget:
        iraf.unlearn(t)
    delete('tsky.fits')
    iraf.specred.continuum(imagefile, output='tsky.fits', type='difference',
                           interact='no', function='legendre', niterat=300, low_rej=3, high_re=2, sample='*',
                           order=_order1, ask='YES')
    delete(imagefile)
    iraf.continuum('tsky.fits', output=imagefile, type='difference',
                   interact='no', function='spline1', overrid='yes', niterat=10, low_rej=3, high_re=1, sample='*',
                   order=_order2, ask='YES')
    delete('tsky.fits')
    return imagefile
Exemple #8
0
def icontinuum(fn):
    """
    call iraf command continuum.
    If out put file already exist, this function will delete the old one.
    fn : fits name
    type : string
    return : out put file name
    type : string
    """
    outname = 'c' + fn
    if os.path.isfile(outname):
        print('remove file ' + outname)
        os.remove(outname)
    iraf.continuum(input=fn,
                   output=outname,
                   lines='*',
                   bands=1,
                   type='ratio',
                   replace='no',
                   wavescale='Yes',
                   logscale='No',
                   override='No',
                   listonly='No',
                   logfiles='logfile',
                   interactive='Yes',
                   sample='*',
                   naverage=1,
                   function='spline3',
                   order=1,
                   low_reject=1.5,
                   high_reject=0.0,
                   niterate=20,
                   grow=2.0,
                   markrej='Yes',
                   graphics='stdgraph',
                   cursor='',
                   ask='yes')
    return outname  # return out fit name
Exemple #9
0
def normalise(input_spectrum):
    ### Apply normalisation with continuum
    print "Fitting continuum and normalising spectrum"
    os.system("rm temp.fits")
    iraf.continuum(
        input = input_spectrum,\
        output = "temp.fits",\
        lines = "*",\
        bands = "*",\
        type = "ratio",\
        replace = 0,\
        wavescale = 1,\
        logscale = 0,\
        override = 1,\
        listonly = 0,\
        logfiles = "logfile",\
        interactive = 0,\
        function  = "spline3",\
        order = 1,\
        low_reject = 1.5,\
        high_reject = 3.0,\
        niterate = 5,\
        grow = 2,\
        ask = "no",)
Exemple #10
0
#!/usr/bin/env python
#
# Try to normalize spectra
#
from pyraf import iraf

iraf.continuum.unlearn()

spec = str(raw_input("Enter spectra to normalize: "))
out = str(raw_input("Enter name of ouput normalized spectra: "))

iraf.continuum.function = 'spline3'
iraf.continuum.order = 1
iraf.continuum.low_reject = 3.0
iraf.continuum.high_reject = 3.0
iraf.continuum(input=spec, output=out)
Exemple #11
0
   bfmod = model[:-4]+'.bf.apogee.txt'

# Various outfiles, some of which will be deleted at the end
textcont = model[:-4]+'_short.txt'
fitscont = model[:-4]+'_short.fits'
fitsnocont = model[:-4]+'_nocont.fits'
convtext = model[:-4]+'.cnv'
convout = model[:-4]+'.cnv.out'

# Continuum fit the spectrum so we can use IRAF's continuum function to remove it
np.savetxt(textcont, np.array([AIR,newf]).transpose())
iraf.rspectext(input=textcont, output=fitscont, flux='No', dtype='interp')
print('iraf rspectext complete')
iraf.continuum(input=fitscont, output=fitsnocont, lines='*',
               type='ratio', replace=no, wavescale=yes, logscale=no,
               listonly=no, interactive=no, sample='*',
               naverage=-25, function='spline3', order=5, niterate=10,
               low_reject=1.5, high_reject=5.0, markrej=no, grow=0,
               override=yes)
print('iraf continuum complete')
print('friendly untouched by convspec FITS saved {0}'.format(fitsnocont))

# Create a text file correctly formatted for convspec
wave, data = read_fits(fitsnocont)
np.savetxt(convtext, np.array([wave, np.zeros(len(wave)), data]).transpose())

# Run convspec, the fortran code
call([convspecexe, convtext, convspecpar])
print('completed convspec')

# Read the output from convspec
a, b = np.loadtxt(convout, unpack=True, usecols=[0,2])
        apmodulus = "0",\
        format = "multispec",\
        renumber = 0,\
        offset = 0,\
        clobber = 1,\
        merge = 0,\
        rebin = 0,\
        verbose = 1)

    ### Then normalise this spectrum (rather than combining the normalised 
    ### spectra from each aperture... easier!)

    iraf.continuum(
        input = "spec_" + file_name,\
        output = "normspec_" + file_name,\
        type = "ratio",\
        replace = 0,\
        wavescale = 1,\
        logscale = 0,\
        override = 1,\
        listonly = 0,\
        logfiles = "logfile",\
        interactive = 0,\
        function  = "spline3",\
        order = 15,\
        low_reject = 5.0,\
        high_reject = 0.0,\
        niterate = 2,\
        grow = 1,\
        ask = "no",)
Exemple #13
0
os.system("rm " + "master_smooth.fits")
os.system("mv " + "temp_master_smooth.fits master_smooth.fits")

### Apply continuum 
os.system("rm continuum_fit.fits")

iraf.hedit(images="master_smooth",fields = "SFIT",value="",add=0,delete=1,verify=0, show=1, update=1)

iraf.continuum(
    input = "master_smooth.fits",\
    output = "continuum_fit.fits",\
    lines = "*",\
    bands = "*",\
    type = "fit",\
    replace = 0,\
    wavescale = 1,\
    logscale = 0,\
    override = 1,\
    listonly = 0,\
    logfiles = "logfile",\
    interactive = 1,\
    function  = "spline3",\
    order = 100,\
    low_reject = 5.0,\
    high_reject = 5.0,\
    niterate = 5,\
    grow = 1,\
    ask = "yes",)

os.system("mv continuum_fit.fits " + grating + "_" + dichroic + "_smooth.fits")
def contAndStack(directory='', fileTag=''):
# Function goes through all the files in the passed directory, breaks them
# into their component orders, and continuum-normalizes each order. It 
# then re-combines the continuum-ed orders into a single 1-D spectrum.
# It then performs a user-assisted Doppler correction.
    fluxFiles = sorted(glob.glob(directory+'/*_Flux.fits'))
    spectraCount = 0
    # Load the necessary IRAF packages
    iraf.onedspec()
    
    # We're going to keep track of the files for each object separately.
    # Note: With the potential for multiple exposures, we may have multiple
    # images to weight and stack for a given object.
    fileDict = {}
    
    for thisFile in fluxFiles:
        # Reason #93 as to why IRAF sucks...80 character limit for
        # file paths and names...
        if len(thisFile) >80:
            tempName = directory+'/a_{0}.fits'.format(spectraCount)
            if len(tempName)>80:
                # make the user rename their path...
                print('File path/name too long: {0}'.format(thisFile))
                print('Skipping this file.')
                continue
            os.rename(thisFile, tempName)
            thisFile = tempName

        # Resulting file names will use the self-reported object name
        # (if any) and an index based on the alphabetical order of
        # the original ("HI.xxxxx...") filename. Individual CCD files
        # (ie: HI.xxxx..._[1,2,3]_Flux.fits") will be left separate.
        objName = SD.GetObjectName(thisFile).strip().replace(' ','_')
        if objName not in fileDict.keys():
            fileDict[objName] = []
        # Using a "spectraCount" ensures that if multiple exposures of
        # the same object exist, they are given unique filenames for the
        # combining process
        starName = '{0}{1}_{2:02d}'.format(fileTag, objName, spectraCount)
        spectraCount += 1
        
        # makee 'linearize' breaks a 2-d spectrum into it's component
        # orders, one order per file.
        makeeCall = kMakeeLinearCall + thisFile
        sub.call(makeeCall, shell=True)
        orderFiles = sorted(glob.glob(directory+'/*_Flux-??.fits'))
        for order in orderFiles:
            orderNo = order[-7:-5]
            if starName == '':
                fileRoot = order.split('/')[-1]
                orderFileName = directory+'/temp_'+fileRoot
            else:
                orderFileName = directory+'/temp_'+starName+'_'+orderNo+'.fits'
            # Continuum the orders
            try:
                iraf.continuum(input=order, output=orderFileName,
                    functio='chebyshev', interac='no', order=5, low_rej=3.0, 
                    high_re=3.5, niterat=10)
            except:
                print('Error in continuum {0}. Skipping.'.format(order))
                u.clearFile(order)
                continue
            u.clearFile(order)
            fileDict[objName].append(orderFileName)
    completedList=['H027771','H027859','H028099','H028205','H028344','H028462',\
    'H028992','Hy_vB59=HD28034','Hy_vB64=HD28099','Hy_vB65=HD28205',\
    'Hy_vB73=HD28344','Hy_vB92=HD28805','Hy_vB93=HD28878','Hy_vB97=HD28992',\
    'vA_294','vB_173','vB_180','vB_183','vB__46','vB__49','vB__64','vB__65',\
    'vB__73','vB__92','vB__93','vB__97','RHy_281','VA294','28344','HD_028344'\
    'HD28394','08-U20187']
    
    for objName in fileDict.keys():
        if len(fileDict[objName]) == 0:
            continue
        if objName in completedList:
            print('Skipping {0}'.format(objName))
            continue
        # Create a temporary input file list:
        fpInput = open('input.dat','w')
        fpWeight = open('weight.dat','w')
        print('---- {0} ----'.format(objName))
        for fn in fileDict[objName]:
            fpInput.write(fn+'\n')
            sn = SD.GetSpectraSN(fn)
            fpWeight.write('{0:6.1f}\n'.format(sn))
            print('    {0:40}: {1:5.1f}'.\
                format(fn.split('/')[-1], sn))
        fpInput.close()
        fpWeight.close()
        contFileName = directory+'/wc_'+objName+'.fits'
        # Stack all the images/orders for this object.
        iraf.onedspec.scombine(input='@input.dat', output=contFileName,\
            weight='@weight.dat', combine='median', \
            lthreshold=0.05, hthreshold=2.0, group="all")
#        iraf.stsdas.hst_calib.tomultispec(input=contFileName, output='ms_'+contFileName)
        u.clearFile('input.dat')
        u.clearFile('weight.dat')
        for fn in fileDict[objName]:
            u.clearFile(fn)

        # For now, the automated Doppler correction is good only for very small
        # corrections (ie: <5km/s), so use the manual version.
        dopCorVelocity, dopWLs = DCL.dopCor(contFileName, interactive=True)
#        print('Doppler correction = {0:2.3f}km/s'.format(dopCorVelocity))
        dopCorFilename = directory+'/D'+contFileName.split('/')[-1][1:]
        iraf.dopcor(input=contFileName, output=dopCorFilename, redshift=dopCorVelocity, isveloc='yes', verbose='yes')
        u.clearFile(contFileName)
        
    return
Exemple #15
0
def fitContinuum(rawFrame, grating, continuumInter, tempInter, log, over):
    """
    Fit a continuum to the telluric correction spectrum to normalize it. The continuum
    fitting regions were derived by eye and can be improved.

    Results are in fit<Grating>.fits
    """
    # These were found to fit the curves well by hand. You can probably improve them; feel free to fiddle around!
    if grating == "K":
        order = 5
        sample = "20279:20395,20953:24283"
    elif grating == "J":
        order = 5
        sample = "11561:12627,12745:12792,12893:13566"
    elif grating == "H":
        order = 5
        sample = "*"
    elif grating == "Z":
        order = 5
        sample = "9453:10015,10106:10893,10993:11553"
    if os.path.exists('2_fit' + rawFrame + '.fits'):
        if over:
            os.remove('2_fit' + rawFrame + '.fits')
            iraf.continuum(input='1_htel' + rawFrame,
                           output='2_fit' + rawFrame,
                           ask='yes',
                           lines='*',
                           bands='1',
                           type="fit",
                           replace='no',
                           wavescale='yes',
                           logscale='no',
                           override='no',
                           listonly='no',
                           logfiles=log,
                           inter=continuumInter,
                           sample=sample,
                           naverage=1,
                           func='spline3',
                           order=order,
                           low_rej=1.0,
                           high_rej=3.0,
                           niterate=2,
                           grow=1.0,
                           markrej='yes',
                           graphics='stdgraph',
                           cursor='',
                           mode='ql')
        else:
            logging.info(
                "\nOutput exists and -over not set - skipping continuum fit to telluric correction"
            )
    else:
        iraf.continuum(input='1_htel' + rawFrame,
                       output='2_fit' + rawFrame,
                       ask='yes',
                       lines='*',
                       bands='1',
                       type="fit",
                       replace='no',
                       wavescale='yes',
                       logscale='no',
                       override='no',
                       listonly='no',
                       logfiles=log,
                       inter=continuumInter,
                       sample=sample,
                       naverage=1,
                       func='spline3',
                       order=order,
                       low_rej=1.0,
                       high_rej=3.0,
                       niterate=2,
                       grow=1.0,
                       markrej='yes',
                       graphics='stdgraph',
                       cursor='',
                       mode='ql')
    if os.path.exists('../products_fluxcal_AND_telluric_corrected/0_fit' +
                      rawFrame + '.fits'):
        if over:
            os.remove('../products_fluxcal_AND_telluric_corrected/0_fit' +
                      rawFrame + '.fits')
            shutil.copy(
                '2_fit' + rawFrame + '.fits',
                '../products_fluxcal_AND_telluric_corrected/0_fit' + rawFrame +
                '.fits')
        else:
            logging.info(
                "\nOutput exists and -over not set - skipping copy of fit to products_fluxcal_AND_telluric_corrected"
            )
    else:
        shutil.copy(
            '2_fit' + rawFrame + '.fits',
            '../products_fluxcal_AND_telluric_corrected/0_fit' + rawFrame +
            '.fits')

    if tempInter:
        # Plot the telluric correction spectrum with the continuum fit.
        final_tel_no_hLines_no_norm = astropy.io.fits.open('1_htel' +
                                                           rawFrame +
                                                           '.fits')[0].data
        fit = astropy.io.fits.open('2_fit' + rawFrame + 'fit.fits')[0].data
        plt.title(
            'Unnormalized Telluric Correction and Continuum fit Used to Normalize'
        )
        plt.plot(final_tel_no_hLines_no_norm)
        plt.plot(fit)
        plt.show()
Exemple #16
0
    fnu = 0,\
    mode = "al")

### Apply high/low rejection again to remove cosmic rays
os.system("rm temp.fits")
iraf.continuum(
    input = "fluxcal_"+ file_name,\
    output = "temp.fits",\
    ask = "no",\
    lines = "*",\
    bands = "*",\
    type = "data",\
    replace = 1,\
    wavescale = 1,\
    logscale = 0,\
    override = 1,\
    listonly = 0,\
    logfiles = "logfile",\
    interactive = 0,\
    sample = "*",\
    naverage = 1,\
    function = "spline3",\
    order = 15,\
    low_reject = 30.0,\
    high_reject = 30.0,\
    niterate = 10,\
    grow = 1.0)

os.system("rm fluxcal_" + file_name)
os.system("mv temp.fits fluxcal_" + file_name)

### Move spectrum to reduced/
Exemple #17
0
###
###  Continuum fitting and removal different orders for different parts...
###  Trial and error to get it working.
###
print 'Continuum fitting the spectra...'

iraf.continuum(input='@targets_extracted',
               output='fitcont//@targets_extracted',
               lines='*',
               type='fit',
               replace=no,
               wavescale=yes,
               logscale=no,
               listonly=no,
               interactive=no,
               sample='*',
               naverage=1,
               function='spline3',
               order=4,
               niterate=20,
               low_reject=1.15,
               high_reject=2.5,
               markrej=no,
               grow=0,
               override=no)

# Good fits for old 90-92 Ca H and K
#if grating == 'old':
#    iraf.continuum(input='@targets_extracted',
#                   output='fitcont//@targets_extracted',lines='90,91,92',
#                   type='fit',replace=no,wavescale=yes,logscale=no,listonly=no,
Exemple #18
0
def deimos_standard(standard):

    '''Extract standard star and calculate sensitivit function.'''
    
    bstd = "%s_01_B" % standard; rstd = "%s_01_R" % standard

    # Extract standard
    if get_head("%s.fits" % bstd, "SKYSUB"):
        iraf.apall(bstd, output="", inter=yes, find=yes, recenter=yes, resize=yes,
                   edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes,
                   review=yes, background="none")
        iraf.apall(rstd, output="", inter=yes, find=yes, recenter=yes, resize=yes,
                   edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes,
                   review=yes, background="none")
    else:
        iraf.apall(bstd, output="", inter=yes, find=yes, recenter=yes, resize=yes,
                   edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes,
                   review=yes, background="fit")
        iraf.apall(rstd, output="", inter=yes, find=yes, recenter=yes, resize=yes,
                   edit=yes, trace=yes, fittrace=yes, extract=yes, extras=yes,
                   review=yes, background="fit")

    # Fit the continuum
    iraf.continuum("%s.ms" % bstd, output="s%s.ms" % bstd, lines=1, bands=1,
                   type="fit", sample="*", naverage=1, function="chebyshev",
                   order=15, low_reject=3.0, high_reject=5.0, niterate=10, grow=1.0,
                   interac=yes)
    iraf.continuum("%s.ms" % rstd, output="s%s.ms" % rstd, lines=1, bands=1,
                   type="fit", sample="*", naverage=1, function="chebyshev",
                   order=15, low_reject=3.0, high_reject=5.0, niterate=10, grow=1.0,
                   interac=yes)
    
    # Divide through by smoothed standard
    iraf.sarith("%s.ms" % bstd, "/", "s%s.ms" % bstd, "ds%s.ms" % bstd)
    iraf.sarith("%s.ms" % rstd, "/", "s%s.ms" % rstd, "ds%s.ms" % rstd)

    # Create and apply telluric correction
    iraf.splot("%s.ms" % bstd) # Remove telluric, save as a%s.ms
    iraf.splot("%s.ms" % rstd) # Remove telluric, save as a%s.ms
    iraf.sarith("%s.ms" % bstd, "/", "a%s.ms" % bstd, "telluric.B.fits")
    iraf.sarith("%s.ms" % rstd, "/", "a%s.ms" % rstd, "telluric.R.fits")
    iraf.imreplace("telluric.B.fits", 1.0, lower=0.0, upper=0.0)
    iraf.imreplace("telluric.R.fits", 1.0, lower=0.0, upper=0.0)
    iraf.telluric("ds%s.ms" % bstd, "tds%s.ms" % bstd, "telluric.B.fits",
                  xcorr=no, tweakrms=no, interactive=no, sample='6850:6950,7575:7700')
    iraf.telluric("ds%s.ms" % rstd, "tds%s.ms" % rstd, "telluric.R.fits",
                  xcorr=no, tweakrms=no, interactive=no, sample='6850:6950,7575:7700')

    # Define bandpasses for standard star calculation
    iraf.standard("tds%s.ms" % bstd, "%s.B.std" % standard,
                  extinction='home$extinct/maunakeaextinct.dat',
                  caldir='home$standards/', observatory='Keck', interac=yes,
                  star_name=standard, airmass='', exptime='')
    iraf.standard("tds%s.ms" % rstd, "%s.R.std" % standard,
                  extinction='home$extinct/maunakeaextinct.dat',
                  caldir='home$standards/', observatory='Keck', interac=yes,
                  star_name=standard, airmass='', exptime='')

    # Determine sensitivity function
    iraf.sensfunc('%s.B.std' % standard, '%s.B.sens' % standard,
                  extinction='home$extinct/maunakeaextinct.dat',
                  newextinction='extinct.dat', observatory='Keck',
                  function='legendre', order=4, interactive=yes)
    iraf.sensfunc('%s.R.std' % standard, '%s.R.sens' % standard,
                  extinction='home$extinct/maunakeaextinct.dat',
                  newextinction='extinct.dat', observatory='Keck',
                  function='legendre', order=4, interactive=yes)

    return
### Apply continuum to normalise
os.system("rm norm_" + file_name)

iraf.continuum(
    input = "trim_" + file_name,\
    output = "norm_" + file_name,\
    lines = "*",\
    bands = "1",\
    type = "ratio",\
    replace = 0,\
    wavescale = 1,\
    logscale = 0,\
    override = 1,\
    listonly = 0,\
    logfiles = "logfile",\
    interactive = 0,\
    sample = "*",\
    naverage = 1,\
    function = "spline3",\
    order = 3,\
    low_reject = 1.5,\
    high_reject = 4.0,\
    niterate = 10,\
    markrej = 1,\
    graphics = "stdgraph",\
    cursor = "",\
    ask = "no",\
    mode = "ql")

#######################
### Convert to .dat ###
Exemple #20
0
flat

# <codecell>

#create flats with different aperture widths
iraf.apall(input=flat, output='flat_1', referen=flat, format='multispec', interac='no', find='no', recenter='no', resize='yes', edit='no', trace='no', fittrac='no', extract='yes', extras='no', review='no', line=2000, lower=-1, upper=1, bkg='no', nsum=-10, ylevel="INDEF", llimit=-1, ulimit=1,Stdout="/dev/null")
# iraf.apall(input='tmp/masterflat.fits', output='tmp/flat_2', referen='tmp/masterflat.fits', format='multispec', interac='no', find='no', recenter='no', resize='yes', edit='no', trace='no', fittrac='no', extract='yes', extras='no', review='no', line=2000, lower=-2, upper=2, bkg='no', nsum=-10, ylevel="INDEF", llimit=-2, ulimit=2,Stdout="/dev/null")
# iraf.apall(input='tmp/masterflat.fits', output='tmp/flat_3', referen='tmp/masterflat.fits', format='multispec', interac='no', find='no', recenter='no', resize='yes', edit='no', trace='no', fittrac='no', extract='yes', extras='no', review='no', line=2000, lower=-3, upper=3, bkg='no', nsum=-10, ylevel="INDEF", llimit=-3, ulimit=3,Stdout="/dev/null")

# <codecell>

#normalize different flats
iraf.noao(_doprint=0,Stdout="/dev/null")
iraf.onedspec(_doprint=0,Stdout="/dev/null")
iraf.continuum(input=flat, output='flat_1_norm', lines='*', bands='*', type='ratio', wavescale='no', interac='no', sample='1:4095', functio='spline3', order=13, low_rej=2, high_rej=2, niter=10,Stdout="/dev/null")
# iraf.continuum(input='tmp/flat_2', output='tmp/flat_2_norm', lines='*', bands='*', type='ratio', wavescale='no', interac='no', sample='1:4095', functio='spline3', order=13, low_rej=2, high_rej=2, niter=10,Stdout="/dev/null")
# iraf.continuum(input='tmp/flat_3', output='tmp/flat_3_norm', lines='*', bands='*', type='ratio', wavescale='no', interac='no', sample='1:4095', functio='spline3', order=13, low_rej=2, high_rej=2, niter=10,Stdout="/dev/null")

# <codecell>

#combine normalized flats
# iraf.images(_doprint=0,Stdout="/dev/null")
# iraf.imutil(_doprint=0,Stdout="/dev/null")
# iraf.imarith(operand1='tmp/flat_1_norm', op='*', operand2=0.43, result='tmp/flat_1_norm2',Stdout="/dev/null")
# iraf.imarith(operand1='tmp/flat_2_norm', op='*', operand2=0.41, result='tmp/flat_2_norm2',Stdout="/dev/null")
# iraf.imarith(operand1='tmp/flat_3_norm', op='*', operand2=0.16, result='tmp/flat_3_norm2',Stdout="/dev/null")

# <codecell>

iraf.imsum(input='*norm.fits', output='masterflat_norm.fits', option='sum',Stdout="/dev/null")
Exemple #21
0
        verbose = 1,\
        mode = "ql")

trim_spectra("synthetic.fits")

print "Fitting continuum and normalising synthetic spectrum"
iraf.continuum(
    input = "synthetic",\
    output = "n_synthetic",\
    lines = "*",\
    bands = 1,\
    type = "ratio",\
    replace = 0,\
    wavescale = 1,\
    logscale = 0,\
    override = 1,\
    listonly = 0,\
    logfiles = "logfile",\
    interactive = 0,\
    function  = "spline3",\
    order = 15,\
    low_reject = 2.0,\
    high_reject = 0.0,\
    niterate = 5,\
    grow = 1,\
    ask = "no")

os.system("mv -f n_synthetic.fits synthetic.fits")

#################
### Run fxcor ###
#################
            lowrej = 15.0
            highrej = 25.0
            nit = 2

        os.system("rm cray_" + im_slice + "_" + file_name)
        iraf.continuum(
            input = "dispcorr_" + im_slice + "_" + file_name,\
            output = "cray_" + im_slice + "_" + file_name,\
            ask = "no",\
            lines = "*",\
            bands = "*",\
            type = "data",\
            replace = 1,\
            wavescale = 1,\
            logscale = 0,\
            override = 1,\
            listonly = 0,\
            logfiles = "logfile",\
            interactive = 0,\
            sample = "*",\
            naverage = 1,\
            function = "spline3",\
            order = 15,\
            low_reject = lowrej,\
            high_reject = highrej,\
            niterate = nit,\
            grow = 1.0)

        iraf.hedit(images="cray_" + im_slice + "_" + file_name,fields="SFIT",value="1",add=0,delete=1,verify=0,show=1,update=1)
        iraf.hedit(images="cray_" + im_slice + "_" + file_name,fields="SFITB",value="1",add=0,delete=1,verify=0,show=1,update=1)

        ### Apply normalisation with continuum
Exemple #23
0
def extractSpectra():
    """
    Extract 1D spectra using IRAF interactively

    Interpolate across the two arcs either side to
    get the most accurate wavelength solution

    TODO: Finish docstring
          Add method of using super arc for inital
          identify
    """
    # load IRAF from the location of the login.cl file
    here = os.getcwd()
    os.chdir(loginCl_location)
    from pyraf import iraf
    os.chdir(here)
    time.sleep(2)

    # make a list of the science images to be analysed
    templist = g.glob('i_s*')
    # import IRAF packages for spectroscopy
    iraf.imred(_doprint=0)
    iraf.kpnoslit(_doprint=0)
    # apall parameters
    iraf.apall.setParam('format', 'multispec')
    iraf.apall.setParam('interac', 'yes')
    iraf.apall.setParam('find', 'yes')
    iraf.apall.setParam('recen', 'yes')
    iraf.apall.setParam('resize', 'yes')
    iraf.apall.setParam('trace', 'yes')
    iraf.apall.setParam('fittrac', 'yes')
    iraf.apall.setParam('extract', 'yes')
    iraf.apall.setParam('extras', 'yes')
    iraf.apall.setParam('review', 'yes')
    iraf.apall.setParam('line', 'INDEF')
    iraf.apall.setParam('nsum', '12')
    iraf.apall.setParam('lower', '-6')
    iraf.apall.setParam('upper', '6')
    iraf.apall.setParam('b_funct', 'chebyshev')
    iraf.apall.setParam('b_order', '1')
    iraf.apall.setParam('b_sampl', '-25:-15,15:25')
    iraf.apall.setParam('b_naver', '-100')
    iraf.apall.setParam('b_niter', '0')
    iraf.apall.setParam('b_low_r', '3')
    iraf.apall.setParam('b_high', '3')
    iraf.apall.setParam('b_grow', '0')
    iraf.apall.setParam('width', '10')
    iraf.apall.setParam('radius', '10')
    iraf.apall.setParam('threshold', '0')
    iraf.apall.setParam('nfind', '1')
    iraf.apall.setParam('t_nsum', '10')
    iraf.apall.setParam('t_step', '10')
    iraf.apall.setParam('t_nlost', '3')
    iraf.apall.setParam('t_niter', '7')
    iraf.apall.setParam('t_funct', 'spline3')
    iraf.apall.setParam('t_order', '3')
    iraf.apall.setParam('backgro', 'fit')
    iraf.apall.setParam('skybox', '1')
    iraf.apall.setParam('weights', 'variance')
    iraf.apall.setParam('pfit', 'fit1d')
    iraf.apall.setParam('clean', 'yes')
    iraf.apall.setParam('saturat', SATURATION)
    iraf.apall.setParam('readnoi', RDNOISE)
    iraf.apall.setParam('gain', GAIN)
    iraf.apall.setParam('lsigma', '4.0')
    iraf.apall.setParam('usigma', '4.0')
    iraf.apall.setParam('nsubaps', '1')
    iraf.apall.saveParList(filename="apall.pars")

    # make reference arc for reidentify
    if '.' in args.refarc:
        args.refarc = args.refarc.split('.')[0]
    refarc = "a_s_{}_t.fits".format(args.refarc)
    refarc_out = "a_s_{}_t.ms.fits".format(args.refarc)

    # loop over all the the spectra
    for i in range(0, len(templist)):
        hdulist = fits.open(templist[i])
        prihdr = hdulist[0].header
        target_id = prihdr['CAT-NAME']
        spectrum_id = int(templist[i].split('_')[2].split('r')[1])
        if args.ds9:
            os.system('xpaset fuckingds9 fits < {}'.format(templist[i]))
        # extract the object spectrum
        print("[{}/{}] Extracting spectrum of {} from image {}".format(i+1, len(templist), target_id, templist[i]))
        print("[{}/{}] Check aperture and background. Change if required".format(i+1, len(templist)))
        print("[{}/{}] AP: m = mark aperture, d = delete aperture".format(i+1, len(templist)))
        print("[{}/{}] SKY: s = mark sky, t = delete sky, f = refit".format(i+1, len(templist)))
        print("[{}/{}] q = continue".format(i+1, len(templist)))
        iraf.apall(input=templist[i])
        print("Spectrum extracted!")
        # find the arcs either side of the object
        arclist = []
        arc1 = "a_s_r{0:d}_t.fits".format(spectrum_id-1)
        arc2 = "a_s_r{0:d}_t.fits".format(spectrum_id+1)
        arc1_out = "a_s_r{0:d}_t.ms.fits".format(spectrum_id-1)
        arc2_out = "a_s_r{0:d}_t.ms.fits".format(spectrum_id+1)
        # predict the arc names
        print("\nPredicting arcs names...")
        print("Arc1: {}".format(arc1))
        print("Arc2: {}".format(arc2))
        # setup a reference filename for the arc conditions in database
        reffile = templist[i].split('.fits')[0]
        # extract the arcs
        print("\nExtracting arcs under the same conditions...")
        if os.path.exists(arc1):
            iraf.apall(input=arc1,
                       reference=reffile,
                       recente="no",
                       trace="no",
                       backgro="no",
                       interac="no")
            print("Arc1 {} extracted".format(arc1))
            arclist.append(arc1_out)
        else:
            print("\n\nArc1 {} FILE NOT FOUND\n\n".format(arc1))
        if os.path.exists(arc2):
            iraf.apall(input=arc2,
                       reference=reffile,
                       recente="no",
                       trace="no",
                       backgro="no",
                       interac="no")
            print("Arc2 {} extracted".format(arc2))
            arclist.append(arc2_out)
        else:
            print("\n\nArc2 {} FILE NOT FOUND\n\n".format(arc2))
        # get a list of the extracted arcs and objects
        spectrum_out = "i_s_r{0:d}_t.ms.fits".format(spectrum_id)
        if i == 0:
            # extract the master reference arc
            print("\nExtracting master arc {} under the same conditions...".format(refarc))
            iraf.apall(input=refarc,
                       reference=reffile,
                       recente="no",
                       trace="no",
                       backgro="no",
                       interac="no")
            print("Reference arc {} extracted".format(refarc))
            # identify the lines in it
            print("\nIdentify arc lines:")
            print("Enter the following in the splot window")
            print("\t:thres 500")
            print("\t:order 1, max = 3")
            print("\tfwidth 2")
            print("Select 3-5 arc lines from line atlas")
            print("Press 'm' to mark, then enter wavelength")
            print("Then press 'l' to automatically ID the other lines")
            print("Press 'f' to fit the dispersion correction")
            print("Use 'd' to remove bad points, 'f' to refit")
            print("'q' from fit, then 'q' from identify to continue\n")
            iraf.identify(images=refarc_out, coordlist=lineList_location)
        # use the refarc to ID all the subsequent arcs
        for arc in arclist:
            print("\nReidentifying arclines from {}".format(arc))
            iraf.reidentify(reference=refarc_out, images=arc)
        # add the refspec keywords to the image header for dispcor
        # refspec_factor tells IRAF how to interpolate the arcs
        refspec_factor = round((1./len(arclist)), 1)
        for i in range(0, len(arclist)):
            refspec = "{} {}".format(arclist[i].split(".fits")[0], refspec_factor)
            print("REFSPEC{}: {}".format(i+1, refspec))
            iraf.hedit(images=spectrum_out,
                       fields="REFSPEC{}".format(i+1),
                       value=refspec,
                       add="yes",
                       verify="no",
                       show="yes")
        print("Headers updated!\n")
        # apply the dispersion correction
        print("Applying the dispersion correction")
        iraf.dispcor(input=spectrum_out,
                     output=spectrum_out,
                     lineari="yes",
                     databas="database",
                     table="")
        print("Correction applied!")
        # normalize the spectrum using continuum
        normspec_out = "{}n.ms.fits".format(spectrum_out.split('.ms')[0])
        iraf.continuum(input=spectrum_out,
                       output=normspec_out,
                       logfile="logfile",
                       interac="yes",
                       functio="spline3",
                       order="5",
                       niterat="10",
                       markrej="yes")
        print("\n\n")
Exemple #24
0
             file_name[k] + "_blue.fits",
             star_folders[i] + "/reduction/" +
             file_name[k] + "_blue_d.fits",
             analyse['CCF_RVC'][k],
             isveloc="yes")
 iraf.dopcor(star_folders[i] + "/reduction/" +
             file_name[k] + "_red.fits",
             star_folders[i] + "/reduction/" +
             file_name[k] + "_red_d.fits",
             analyse['CCF_RVC'][k],
             isveloc="yes")
 iraf.continuum(star_folders[i] + "/reduction/" +
                file_name[k] + "_blue_d.fits",
                star_folders[i] + "/reduction/" +
                file_name[k] + "_blue_dn.fits",
                order=80,
                nit=7,
                low_rej=1.0,
                high_rej=3,
                interactive="no",
                wavescale="no")
 iraf.continuum(star_folders[i] + "/reduction/" +
                file_name[k] + "_red_d.fits",
                star_folders[i] + "/reduction/" +
                file_name[k] + "_red_dn.fits",
                order=80,
                nit=7,
                low_rej=1.0,
                high_rej=3,
                interactive="no",
                wavescale="no")
 norm_blue.append(file_name[k] + "_blue_dn.fits")
Exemple #25
0
pyfits.writeto("REGION_Amp5_resto0.fits", imagen_noC, header)

from pyraf import iraf
from iraf import onedspec, continuum, imutil, imarith

############# CONTINUUM###############

iraf.continuum(input="REGION_1_ORIG_resto0.fits",
               output="REGION_1_ORIG_CONT.fits",
               lines="*",
               band="*",
               type='fit',
               replace='no',
               interac='no',
               sample='1056:1309',
               naverag='3',
               functio='legendre',
               order='4',
               low_rej='3',
               high_rej='3',
               niterate='10',
               ask='no')

iraf.continuum(input="REGION_2_ORIG_resto0.fits",
               output="REGION_2_ORIG_CONT.fits",
               lines="*",
               band="*",
               type='fit',
               replace='no',
               interac='no',
Exemple #26
0
def remove_molecule(molecule,spec_name,outspec_name):

    telluric_region = eval("TELLURIC_REGION_"+molecule)
    telluric_region_list = string.split(telluric_region,",")
    for n in range(len(telluric_region_list)):
        region = telluric_region_list[n][1:]
        region = string.split(region,"-")
        temp = []
        for i in region:
            temp.append(float(i))
        telluric_region_list[n] = temp

    ### Copy telluric line spectra to working directory
    telluric_fits = grating + "_"+molecule+".fits"

    os.system("cp -f "+program_dir+"telluric_fits/" + telluric_fits + " telluric.fits")
    os.system("rm temp_norm*")
    os.system("rm temp_spec*")
    os.system("cp -f "+spec_name+" temp_spec.fits")

    iraf.continuum(
        input = spec_name,\
        output = "temp_norm.fits",\
        lines = "*",\
        bands = "*",\
        type = "ratio",\
        replace = 0,\
        wavescale = 1,\
        logscale = 0,\
        override = 1,\
        listonly = 0,\
        logfiles = "logfile",\
        interactive = 0,\
        sample = "*",\
        naverage = 1,\
        function = "spline3",\
        order = 20,\
        low_reject = 2.0,\
        high_reject = 5.0,\
        niterate = 5,\
        grow = 2.0,\
        ask = "yes")

    ##################
    ### Find shift ###
    ##################
    shift = run_fxcor("telluric.fits","temp_norm.fits",telluric_region)
    os.system("rm telluric.dat")
    iraf.dopcor(
        input = "telluric.fits",\
        output = "telluric.fits",\
        redshift = shift,\
        isvelocity = 1,\
        add = 1,\
        dispersion = 1,\
        flux = 0,\
        verbose = 0)

    iraf.wspectext(
        input = "telluric.fits",\
        output = "telluric.dat",\
        header = 0)

    ######################################
    ### Iterative telluric subtraction ###
    ######################################

    def iterate():
        os.system("rm temp_norm.dat")
        iraf.wspectext(
            input = "temp_norm.fits",\
            output = "temp_norm.dat",\
            header = 0)

        data_spec = transpose(loadtxt("temp_norm.dat"))
        wave = arange(min(data_spec[0]),max(data_spec[0]),1)
        data_flux = interpolate.splrep(data_spec[0],data_spec[1])
        data_flux = interpolate.splev(wave,data_flux)

        telluric_spec = transpose(loadtxt("telluric.dat"))
        telluric_flux = interpolate.splrep(telluric_spec[0],telluric_spec[1])
        telluric_flux = interpolate.splev(wave,telluric_flux)

        ### Loop through telluric scaling
        telluric_scaling = []
        rms_list = []

        maxscale = 1.08
        for scale in arange(0.0,maxscale,0.01):
            scatter_list = []
            for region in telluric_region_list:
                temp_flux = []
                temp_tel = []

                for i in range(len(wave)):
                    if wave[i] > region[0] and wave[i] < region[1]:
                        temp_flux.append(data_flux[i])
                        temp_tel.append(telluric_flux[i])
                    if wave[i] > region[1]:
                        break

                temp_flux = array(temp_flux)
                temp_tel = array(temp_tel)
                scatter = temp_flux / (temp_tel * scale + (1-scale))

                # plt.plot(scatter)
                # plt.show()

                scatter_list.append(std(scatter))

            telluric_scaling.append(scale)
            rms_list.append(average(scatter_list))

        telluric_scaling,rms_list = array(telluric_scaling),array(rms_list)
        best_scale = find_min(telluric_scaling,rms_list)
        if best_scale > maxscale:
            best_scale = maxscale
        if best_scale < 0:
            best_scale = 0

        print "scaling telluric by",best_scale

        # plt.plot(telluric_scaling,rms_list)
        # plt.show()

        os.system("cp telluric.fits telluric_temp.fits")

        iraf.sarith(
            input1 = "telluric_temp.fits",\
            op = "*",\
            input2 = best_scale,\
            output = "telluric_temp.fits",\
            w1 = "INDEF",\
            w2 = "INDEF",\
            apertures = "",\
            bands = "",\
            beams = "",\
            apmodulus = 0,\
            reverse = 0,\
            ignoreaps = 1,\
            format = "multispec",\
            renumber = 0,\
            offset = 0,\
            clobber = 1,\
            merge = 0,\
            rebin = 0,\
            verbose = 0)

        iraf.sarith(
            input1 = "telluric_temp.fits",\
            op = "+",\
            input2 = 1-best_scale,\
            output = "telluric_temp.fits",\
            w1 = "INDEF",\
            w2 = "INDEF",\
            apertures = "",\
            bands = "",\
            beams = "",\
            apmodulus = 0,\
            reverse = 0,\
            ignoreaps = 1,\
            format = "multispec",\
            renumber = 0,\
            offset = 0,\
            clobber = 1,\
            merge = 0,\
            rebin = 0,\
            verbose = 0)

        iraf.sarith(
            input1 = "temp_spec.fits",\
            op = "/",\
            input2 = "telluric_temp.fits",\
            output = "temp_spec",\
            w1 = "INDEF",\
            w2 = "INDEF",\
            apertures = "",\
            bands = "",\
            beams = "",\
            apmodulus = 0,\
            reverse = 0,\
            ignoreaps = 1,\
            format = "multispec",\
            renumber = 0,\
            offset = 0,\
            clobber = 1,\
            merge = 0,\
            rebin = 0,\
            verbose = 0)

        iraf.sarith(
            input1 = "temp_norm.fits",\
            op = "/",\
            input2 = "telluric_temp.fits",\
            output = "temp_norm",\
            w1 = "INDEF",\
            w2 = "INDEF",\
            apertures = "",\
            bands = "",\
            beams = "",\
            apmodulus = 0,\
            reverse = 0,\
            ignoreaps = 1,\
            format = "multispec",\
            renumber = 0,\
            offset = 0,\
            clobber = 1,\
            merge = 0,\
            rebin = 0,\
            verbose = 0)

        return best_scale

    best_scaling = 1.0
    while best_scaling > 0.05:
        best_scaling = iterate()

    os.system("cp temp_spec.fits "+outspec_name)