Esempio n. 1
0
def arma_soar(imagen_proc,out_name):
    
    """creates a single image for SOAR SAMI
    It add to the file the exposure time
    and the filter. It also copies the header
    of the PRIMARY extenssion."""
    
    output_tmp=imagen_proc[:-5]+'_tmp.fits'
    iraf.blkrep(input=imagen_proc+'[1]',output=output_tmp,b1=2,b2=2)
    iraf.imcopy(input=imagen_proc+'[1]',output=output_tmp+'[1:1024,1:1028]',verbose='yes')
    iraf.imcopy(input=imagen_proc+'[2]',output=output_tmp+'[1025:2048,1:1028]',verbose='yes')
    iraf.imcopy(input=imagen_proc+'[3]',output=output_tmp+'[1:1024,1029:2056]',verbose='yes')
    iraf.imcopy(input=imagen_proc+'[4]',output=output_tmp+'[1025:2048,1029:2056]',verbose='yes')
    iraf.imcopy(input=output_tmp,output=out_name,verbose='yes')
    
    iraf.imutil.imdelete(output_tmp)
    
    return
Esempio n. 2
0
def lacos(_input0,
          output='clean.fits',
          outmask='mask.fits',
          gain=1.3,
          readn=9,
          xorder=9,
          yorder=9,
          sigclip=4.5,
          sigfrac=0.5,
          objlim=1,
          verbose=True,
          interactive=False):
    import floyds
    from floyds.util import delete
    from pyraf import iraf
    import numpy as np

    oldoutput, galaxy, skymod, med5 = 'oldoutput.fits', 'galaxy.fits', 'skymod.fits', 'med5.fits'
    blk, lapla, med3, med7, sub5, sigima, finalsel = 'blk.fits', 'lapla.fits', 'med3.fits', 'med7.fits', 'sub5.fits',\
                                                     'sigima.fits', 'finalsel.fits'
    deriv2, noise, sigmap, firstsel, starreject = 'deriv2.fits', 'noise.fits', 'sigmap.fits', 'firstsel.fits',\
                                                  'starreject.fits'
    inputmask = 'inputmask.fits'
    # set some parameters in standard IRAF tasks
    iraf.convolve.bilinear = 'no'
    iraf.convolve.radsym = 'no'
    # create Laplacian kernel
    # laplkernel = np.array([[0.0, -1.0, 0.0], [-1.0, 4.0, -1.0], [0.0, -1.0, 0.0]])
    f = open('_kernel', 'w')
    f.write('0 -1 0;\n-1 4 -1;\n0 -1 0')
    f.close()
    # create growth kernel
    f = open('_gkernel', 'w')
    f.write('1 1 1;\n1 1 1;\n1 1 1')
    f.close()
    gkernel = np.array([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]])
    delete(galaxy)
    delete(skymod)
    delete(oldoutput)

    if not output:
        output = _input0
    else:
        delete(output)
        iraf.imcopy(_input0, output, verbose='no')

    delete('_xxx.fits,_yyy.fits')
    iraf.imcopy(_input0 + '[*,10:80]', '_xxx.fits', verbose='no')
    _input = '_xxx.fits'

    arrayinput, headerinput = floyds.cosmics.fromfits(_input, verbose=False)
    floyds.cosmics.tofits(outmask,
                          np.float32(arrayinput - arrayinput),
                          headerinput,
                          verbose=False)

    # subtract object spectra if desired
    #    iraf.fit1d(_input,galaxy,"fit",axis=2,order=5,func="leg",low=4.,
    #            high=4.,nav=1,inter='yes',sample="*",niter=3,grow=0,cursor="")
    #    iraf.imarith(_input,"-",galaxy,oldoutput)
    #    iraf.display(oldoutput,1,fill='yes') #######
    #Subtract sky lines
    #    iraf.fit1d(oldoutput,skymod,"fit",axis=2,order=5,func="leg",low=4.,high=4.,
    #            inter='no',sample="*",nav=1,niter=3,grow=0,cursor="")
    #    iraf.imarith(oldoutput,"-",skymod,oldoutput)
    #    iraf.display(oldoutput,2,fill='yes')  #####
    iraf.imcopy(_input, oldoutput)
    arrayoldoutput, headeroldoutput = floyds.cosmics.fromfits(oldoutput,
                                                              verbose=False)
    # add object spectra to sky model
    #    iraf.imarith(skymod,"+",galaxy,skymod)
    delete(med5)
    # add median of residuals to sky model
    iraf.median(oldoutput,
                med5,
                5,
                5,
                zlor='INDEF',
                zhir='INDEF',
                verbose='no')
    #    m5 = ndimage.filters.median_filter(_inputarray, size=5, mode='mirror')
    #    iraf.imarith(skymod,"+",med5,med5)
    # take second-order derivative (Laplacian) of input image
    # kernel is convolved with subsampled image, in order to remove negative
    # pattern around high pixels
    delete(blk)
    delete(lapla)
    iraf.blkrep(oldoutput, blk, 2, 2)
    iraf.convolve(blk, lapla, '_kernel')
    iraf.imreplace(lapla, 0, upper=0, lower='INDEF')
    delete(deriv2)
    delete(noise)
    iraf.blkavg(lapla, deriv2, 2, 2, option="average")
    # create noise model
    iraf.imutil.imexpr(expr='sqrt(a*' + str(gain) + '+' + str(readn) +
                       '**2)/' + str(gain),
                       a=med5,
                       output=noise,
                       verbose='no')
    iraf.imreplace(med5, 0.00001, upper=0, lower='INDEF')
    # divide Laplacian by noise model
    delete(sigmap)
    iraf.imutil.imexpr(expr='(a/b)/2',
                       a=deriv2,
                       b=noise,
                       output=sigmap,
                       verbose='no')
    # removal of large structure (bright, extended objects)
    delete(med5)
    iraf.median(sigmap, med5, 5, 5, zlo='INDEF', zhi='INDEF', verbose='no')
    iraf.imarith(sigmap, "-", med5, sigmap)
    # find all candidate cosmic rays
    # this selection includes sharp features such as stars and HII regions

    arraysigmap, headersigmap = floyds.cosmics.fromfits(sigmap, verbose=False)
    arrayf = np.where(arraysigmap < sigclip, 0, arraysigmap)
    arrayf = np.where(arrayf > 0.1, 1, arrayf)
    floyds.cosmics.tofits(firstsel,
                          np.float32(arrayf),
                          headersigmap,
                          verbose=False)

    # compare candidate CRs to median filtered image
    # this step rejects bright, compact sources from the initial CR list
    # subtract background and smooth component of objects
    delete(med3)
    iraf.median(oldoutput, med3, 3, 3, zlo='INDEF', zhi='INDEF', verbose='no')
    delete(med7)
    delete('_' + med3)
    iraf.median(med3, med7, 7, 7, zlo='INDEF', zhi='INDEF', verbose='no')
    iraf.imutil.imexpr(expr='(a-b)/c',
                       a=med3,
                       b=med7,
                       c=noise,
                       output='_' + med3,
                       verbose='no')
    iraf.imreplace('_' + med3, 0.01, upper=0.01, lower='INDEF')
    # compare CR flux to object flux
    delete(starreject)
    iraf.imutil.imexpr(expr='a+b+c',
                       a=firstsel,
                       b=sigmap,
                       c="_" + med3,
                       output=starreject,
                       verbose='no')
    # discard if CR flux <= objlim * object flux
    iraf.imreplace(starreject, 0, upper=objlim, lower='INDEF')
    iraf.imreplace(starreject, 1, lower=0.5, upper='INDEF')
    iraf.imarith(firstsel, "*", starreject, firstsel)

    # grow CRs by one pixel and check in original sigma map
    arrayfirst, headerfirst = floyds.cosmics.fromfits(firstsel, verbose=False)
    arraygfirst = floyds.cosmics.my_convolve_with_FFT2(arrayfirst, gkernel)

    arraygfirst = np.where(arraygfirst > 0.5, 1, arraygfirst)
    arraygfirst = arraygfirst * arraysigmap
    arraygfirst = np.where(arraygfirst < sigclip, 0, arraygfirst)
    arraygfirst = np.where(arraygfirst > 0.1, 1, arraygfirst)

    # grow CRs by one pixel and lower detection limit
    sigcliplow = sigfrac * sigclip
    # Finding neighbouring pixels affected by cosmic rays
    arrayfinal = floyds.cosmics.my_convolve_with_FFT2(arraygfirst, gkernel)
    arrayfinal = np.where(arrayfinal > 0.5, 1, arrayfinal)
    arrayfinal = arrayfinal * arraysigmap
    arrayfinal = np.where(arrayfinal < sigcliplow, 0, arrayfinal)
    arrayfinal = np.where(arrayfinal > 0.1, 1, arrayfinal)

    # determine number of CRs found in this iteration
    arraygfirst = (1 - (arrayfinal - arrayfinal)) * arrayfinal
    npix = [str(int(np.size(np.where(arraygfirst > 0.5)) / 2.))]
    # create cleaned output image; use 3x3 median with CRs excluded
    arrayoutmask = np.where(arrayfinal > 1, 1, arrayfinal)
    floyds.cosmics.tofits(outmask,
                          np.float32(arrayoutmask),
                          headerfirst,
                          verbose=False)
    delete(inputmask)
    arrayinputmask = (1 - (10000 * arrayoutmask)) * arrayoldoutput
    floyds.cosmics.tofits(inputmask,
                          np.float32(arrayinputmask),
                          headerfirst,
                          verbose=False)
    delete(med5)
    iraf.median(inputmask,
                med5,
                5,
                5,
                zloreject=-9999,
                zhi='INDEF',
                verbose='no')
    iraf.imarith(outmask, "*", med5, med5)
    delete('_yyy.fits')
    iraf.imutil.imexpr(expr='(1-a)*b+c',
                       a=outmask,
                       b=oldoutput,
                       c=med5,
                       output='_yyy.fits',
                       verbose='no')
    # add sky and object spectra back in
    #iraf.imarith('_yyy.fits',"+",skymod,'_yyy.fits')
    # cleanup and get ready for next iteration
    if npix == 0: stop = yes
    # delete temp files
    iraf.imcopy('_yyy.fits', output + '[*,10:80]', verbose='no')
    delete(blk + "," + lapla + "," + deriv2 + "," + med5)
    delete(med3 + "," + med7 + "," + noise + "," + sigmap)
    delete(firstsel + "," + starreject)
    delete(finalsel + "," + inputmask)
    delete(oldoutput + "," + skymod + "," + galaxy)
    delete("_" + med3 + ",_" + sigmap)
    delete('_kernel' + "," + '_gkernel')
    delete(outmask)
    delete('_xxx.fits,_yyy.fits')
Esempio n. 3
0
def lacos_im(_input,
             _output='clean.fits',
             outmask='mask.fits',
             gain=1.3,
             readn=9,
             xorder=9,
             yorder=9,
             sigclip=4.5,
             sigfrac=0.5,
             objlim=1,
             skyval=0,
             niter=2,
             verbose=True,
             interactive=False):
    import floyds
    from floyds.util import delete
    import sys, re, os, string
    from pyraf import iraf
    import numpy as np

    iraf.convolve.bilinear = 'no'
    iraf.convolve.radsym = 'no'
    # make temporary files
    oldoutput, galaxy, skymod, med5 = 'oldoutput.fits', 'galaxy.fits', 'skymod.fits', 'med5.fits'
    blk, lapla, med3, med7, sub5, sigima, finalsel = 'blk.fits', 'lapla.fits', 'med3.fits', 'med7.fits', 'sub5.fits',\
                                                     'sigima.fits', 'finalsel.fits'
    deriv2, noise, sigmap, firstsel, starreject = 'deriv2.fits', 'noise.fits', 'sigmap.fits', 'firstsel.fits',\
                                                  'starreject.fits'
    inputmask, gfirstsel = 'inputmask.fits', 'gfirstsel.fits'
    f = open('_kernel', 'w')
    f.write('0 -1 0;\n-1 4 -1;\n0 -1 0')
    f.close()
    # create growth kernel
    f = open('_gkernel', 'w')
    f.write('1 1 1;\n1 1 1;\n1 1 1')
    f.close()
    gkernel = np.array([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]])
    # initialize loop
    usegain = gain
    i = 1
    stop = 'no'
    previous = 0
    if not _output: _output = _input

    arrayinput, headerinput = floyds.cosmics.fromfits(_input, verbose=False)
    floyds.cosmics.tofits(outmask,
                          np.float32(arrayinput - arrayinput),
                          headerinput,
                          verbose=False)

    delete(oldoutput)
    if skyval > 0:
        arrayoldoutput = arrayinput + skyval
    else:
        arrayoldoutput = arrayinput
    floyds.cosmics.tofits(oldoutput,
                          np.float32(arrayoldoutput),
                          headerinput,
                          verbose=False)
    # start iterations
    while stop == 'no':
        # take second-order derivative (Laplacian) of input image
        # kernel is convolved with subsampled image, in order to remove negative
        # pattern around high pixels
        delete(blk)
        delete(lapla)
        delete(deriv2)
        iraf.blkrep(oldoutput, blk, 2, 2)
        iraf.convolve(blk, lapla, '_kernel')
        iraf.imreplace(lapla, 0, upper=0, lower='INDEF', radius=0)
        iraf.blkavg(lapla, deriv2, 2, 2, option="average")
        delete(med5)
        # create model of background flux - 5x5 box should exclude all CRs
        iraf.median(oldoutput,
                    med5,
                    5,
                    5,
                    zlo='INDEF',
                    zhi='INDEF',
                    verbose='no')
        iraf.imreplace(med5, 0.0001, upper=0, lower='INDEF', radius=0)
        # create noise model
        delete(noise)
        iraf.imutil.imexpr(expr='sqrt(a*' + str(usegain) + '+' + str(readn) +
                           '**2)/' + str(usegain),
                           a=med5,
                           output=noise,
                           verbose='no')
        # divide Laplacian by noise model
        delete(sigmap)
        iraf.imarith(deriv2, "/", noise, sigmap)
        # Laplacian of blkreplicated image counts edges twice:
        iraf.imarith(sigmap, "/", 2., sigmap)
        # removal of large structure (bright, extended objects)
        delete(med5)
        iraf.median(sigmap, med5, 5, 5, zlo='INDEF', zhi='INDEF', verbose='no')
        arraysigmap, headersigmap = floyds.cosmics.fromfits(sigmap,
                                                            verbose=False)
        arraymed5, headermed5 = floyds.cosmics.fromfits(med5, verbose=False)
        arraysigmap = arraysigmap - arraymed5
        iraf.imarith(sigmap, "-", med5, sigmap)
        # find all candidate cosmic rays
        # this selection includes sharp features such as stars and HII regions

        delete(firstsel)
        iraf.imcopy(sigmap, firstsel, verbose='no')
        iraf.imreplace(firstsel, 0, upper=sigclip, lower='INDEF', radius=0)
        iraf.imreplace(firstsel, 1, lower=0.1, upper='INDEF', radius=0)
        #		arraygfirst=arraysigmap
        #		arraygfirst = np.where(arraygfirst<sigclip,0,arraygfirst)
        #		arraygfirst = np.where(arraygfirst>0.1,1,arraygfirst)

        # compare candidate CRs to median filtered image
        # this step rejects bright, compact sources from the initial CR list
        # subtract background and smooth component of objects
        delete(med3)
        delete(med7)
        iraf.median(oldoutput,
                    med3,
                    3,
                    3,
                    zlo='INDEF',
                    zhi='INDEF',
                    verbose='no')
        iraf.median(med3, med7, 7, 7, zlo='INDEF', zhi='INDEF', verbose='no')
        iraf.imarith(med3, "-", med7, med3)
        iraf.imarith(med3, "/", noise, med3)
        iraf.imreplace(med3, 0.01, upper=0.01, lower='INDEF', radius=0)
        # compare CR flux to object flux
        delete(starreject)
        iraf.imutil.imexpr(expr="(a*b)/c",
                           a=firstsel,
                           b=sigmap,
                           c=med3,
                           output=starreject,
                           verbose='no')
        # discard if CR flux <= objlim * object flux
        iraf.imreplace(starreject, 0, upper=objlim, lower='INDEF', radius=0)
        iraf.imreplace(starreject, 1, lower=0.5, upper='INDEF', radius=0)
        iraf.imarith(firstsel, "*", starreject, firstsel)
        # grow CRs by one pixel and check in original sigma map
        delete(gfirstsel)
        iraf.convolve(firstsel, gfirstsel, '_gkernel')
        iraf.imreplace(gfirstsel, 1, lower=0.5, upper='INDEF', radius=0)
        iraf.imarith(gfirstsel, "*", sigmap, gfirstsel)
        iraf.imreplace(gfirstsel, 0, upper=sigclip, lower='INDEF', radius=0)
        iraf.imreplace(gfirstsel, 1, lower=0.1, upper='INDEF', radius=0)
        # grow CRs by one pixel and lower detection limit
        sigcliplow = sigfrac * sigclip
        delete(finalsel)
        iraf.convolve(gfirstsel, finalsel, '_gkernel')
        iraf.imreplace(finalsel, 1, lower=0.5, upper='INDEF', radius=0)
        iraf.imarith(finalsel, "*", sigmap, finalsel)
        iraf.imreplace(finalsel, 0, upper=sigcliplow, lower='INDEF', radius=0)
        iraf.imreplace(finalsel, 1, lower=0.1, upper='INDEF', radius=0)
        # determine number of CRs found in this iteration
        delete(gfirstsel)
        iraf.imutil.imexpr(expr="(1-b)*a",
                           a=outmask,
                           b=finalsel,
                           output=gfirstsel,
                           verbose='no')

        npix = iraf.imstat(gfirstsel,
                           fields="npix",
                           lower=0.5,
                           upper='INDEF',
                           Stdout=1)
        # create cleaned output image; use 3x3 median with CRs excluded
        delete(med5)
        iraf.imarith(outmask, "+", finalsel, outmask)
        iraf.imreplace(outmask, 1, lower=1, upper='INDEF', radius=0)

        delete(inputmask)
        iraf.imutil.imexpr(expr="(1-10000*a)",
                           a=outmask,
                           output=inputmask,
                           verbose='no')
        iraf.imarith(oldoutput, "*", inputmask, inputmask)
        delete(med5)
        iraf.median(inputmask,
                    med5,
                    5,
                    5,
                    zloreject=-9999,
                    zhi='INDEF',
                    verbose='no')
        iraf.imarith(outmask, "*", med5, med5)
        if i > 1: delete(_output)

        delete(_output)
        iraf.imutil.imexpr(expr="(1.-b)*a+c",
                           a=oldoutput,
                           b=outmask,
                           c=med5,
                           output=_output,
                           verbose='no')

        # cleanup and get ready for next iteration
        delete(oldoutput)
        iraf.imcopy(_output, oldoutput, verbose='no')

        if npix == 0: stop = 'yes'
        i = i + 1
        if i > niter: stop = 'yes'
        # delete temp files
        delete(blk + "," + lapla + "," + deriv2 + "," + med5)
        delete(med3 + "," + med7 + "," + noise + "," + sigmap)
        delete(firstsel + "," + starreject + "," + gfirstsel)
        delete(finalsel + "," + inputmask)

    if skyval > 0: iraf.imarith(_output, "-", skyval, _output)
    delete('_kernel' + "," + '_gkernel')
    delete(oldoutput)
Esempio n. 4
0
def lacos(_input0, output='clean.fits', outmask='mask.fits', gain=1.3, readn=9, xorder=9, yorder=9, sigclip=4.5, sigfrac=0.5, objlim=1, verbose=True, interactive=False):
    # print "LOGX:: Entering `lacos` method/function in %(__file__)s" %
    # globals()
    import ntt
    from ntt.util import delete
    import sys
    import re
    import os
    import string
    from pyraf import iraf
    import numpy as np

    oldoutput, galaxy, skymod, med5 = 'oldoutput.fits', 'galaxy.fits', 'skymod.fits', 'med5.fits'
    blk, lapla, med3, med7, sub5, sigima, finalsel = 'blk.fits', 'lapla.fits', 'med3.fits', 'med7.fits', 'sub5.fits', 'sigima.fits', 'finalsel.fits'
    deriv2, noise, sigmap, firstsel, starreject = 'deriv2.fits', 'noise.fits', 'sigmap.fits', 'firstsel.fits', 'starreject.fits'
    inputmask = 'inputmask.fits'
    # set some parameters in standard IRAF tasks
    iraf.convolve.bilinear = 'no'
    iraf.convolve.radsym = 'no'
    # create Laplacian kernel
    # laplkernel = np.array([[0.0, -1.0, 0.0], [-1.0, 4.0, -1.0], [0.0, -1.0, 0.0]])
    f = open('_kernel', 'w')
    f.write('0 -1 0;\n-1 4 -1;\n0 -1 0')
    f.close()
    # create growth kernel
    f = open('_gkernel', 'w')
    f.write('1 1 1;\n1 1 1;\n1 1 1')
    f.close()
    gkernel = np.array([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]])
    delete(galaxy)
    delete(skymod)
    delete(oldoutput)

    if not output:
        output = _input0
    else:
        delete(output)
        iraf.imcopy(_input0, output, verbose='no')

    delete('_xxx.fits,_yyy.fits')
    iraf.imcopy(_input0 + '[350:550,*]', '_xxx.fits', verbose='no')
    _input = '_xxx.fits'

    arrayinput, headerinput = ntt.cosmics.fromfits(_input, verbose=False)
    ntt.cosmics.tofits(outmask, np.float32(
        arrayinput - arrayinput), headerinput, verbose=False)

    # subtract object spectra if desired
    iraf.fit1d(_input, galaxy, "fit", axis=2, order=9, func="leg", low=4.,
               high=4., nav=1, inter='no', sample="*", niter=3, grow=0, cursor="")
    iraf.imarith(_input, "-", galaxy, oldoutput)
    # Subtract sky lines
    iraf.fit1d(oldoutput, skymod, "fit", axis=1, order=5, func="leg", low=4., high=4.,
               inter='no', sample="*", nav=1, niter=3, grow=0, cursor="")
    iraf.imarith(oldoutput, "-", skymod, oldoutput)

    arrayoldoutput, headeroldoutput = ntt.cosmics.fromfits(
        oldoutput, verbose=False)
    # add object spectra to sky model
    iraf.imarith(skymod, "+", galaxy, skymod)
    delete(med5)
    # add median of residuals to sky model
    iraf.median(oldoutput, med5, 5, 5, zlor='INDEF',
                zhir='INDEF', verbose='no')
#    m5 = ndimage.filters.median_filter(_inputarray, size=5, mode='mirror')
    iraf.imarith(skymod, "+", med5, med5)
    # take second-order derivative (Laplacian) of input image
    # kernel is convolved with subsampled image, in order to remove negative
    # pattern around high pixels
    delete(blk)
    delete(lapla)
    iraf.blkrep(oldoutput, blk, 2, 2)
    iraf.convolve(blk, lapla, '_kernel')
    iraf.imreplace(lapla, 0, upper=0, lower='INDEF')
    delete(deriv2)
    delete(noise)
    iraf.blkavg(lapla, deriv2, 2, 2, option="average")
    # create noise model
    iraf.imutil.imexpr(expr='sqrt(a*' + str(gain) + '+' + str(readn) +
                       '**2)/' + str(gain), a=med5, output=noise, verbose='no')
    iraf.imreplace(med5, 0.00001, upper=0, lower='INDEF')
    # divide Laplacian by noise model
    delete(sigmap)
    iraf.imutil.imexpr(expr='(a/b)/2', a=deriv2, b=noise,
                       output=sigmap, verbose='no')
    # removal of large structure (bright, extended objects)
    delete(med5)
    iraf.median(sigmap, med5, 5, 5, zlo='INDEF', zhi='INDEF', verbose='no')
    iraf.imarith(sigmap, "-", med5, sigmap)
    # find all candidate cosmic rays
    # this selection includes sharp features such as stars and HII regions

    arraysigmap, headersigmap = ntt.cosmics.fromfits(sigmap, verbose=False)
    arrayf = np.where(arraysigmap < sigclip, 0, arraysigmap)
    arrayf = np.where(arrayf > 0.1, 1, arrayf)
    ntt.cosmics.tofits(firstsel, np.float32(
        arrayf), headersigmap, verbose=False)

    # compare candidate CRs to median filtered image
    # this step rejects bright, compact sources from the initial CR list
    # subtract background and smooth component of objects
    delete(med3)
    iraf.median(oldoutput, med3, 3, 3, zlo='INDEF', zhi='INDEF', verbose='no')
    delete(med7)
    delete('_' + med3)
    iraf.median(med3, med7, 7, 7, zlo='INDEF', zhi='INDEF', verbose='no')
    iraf.imutil.imexpr(expr='(a-b)/c', a=med3, b=med7,
                       c=noise, output='_' + med3, verbose='no')
    iraf.imreplace('_' + med3, 0.01, upper=0.01, lower='INDEF')
    # compare CR flux to object flux
    delete(starreject)
    iraf.imutil.imexpr(expr='a+b+c', a=firstsel, b=sigmap,
                       c="_" + med3, output=starreject, verbose='no')
    # discard if CR flux <= objlim * object flux
    iraf.imreplace(starreject, 0, upper=objlim, lower='INDEF')
    iraf.imreplace(starreject, 1, lower=0.5, upper='INDEF')
    iraf.imarith(firstsel, "*", starreject, firstsel)

    # grow CRs by one pixel and check in original sigma map
    arrayfirst, headerfirst = ntt.cosmics.fromfits(firstsel, verbose=False)
    arraygfirst = ntt.cosmics.my_convolve_with_FFT2(arrayfirst, gkernel)

    arraygfirst = np.where(arraygfirst > 0.5, 1, arraygfirst)
    arraygfirst = arraygfirst * arraysigmap
    arraygfirst = np.where(arraygfirst < sigclip, 0, arraygfirst)
    arraygfirst = np.where(arraygfirst > 0.1, 1, arraygfirst)

    # grow CRs by one pixel and lower detection limit
    sigcliplow = sigfrac * sigclip
    # Finding neighbouring pixels affected by cosmic rays
    arrayfinal = ntt.cosmics.my_convolve_with_FFT2(arraygfirst, gkernel)
    arrayfinal = np.where(arrayfinal > 0.5, 1, arrayfinal)
    arrayfinal = arrayfinal * arraysigmap
    arrayfinal = np.where(arrayfinal < sigcliplow, 0, arrayfinal)
    arrayfinal = np.where(arrayfinal > 0.1, 1, arrayfinal)

    # determine number of CRs found in this iteration
    arraygfirst = (1 - (arrayfinal - arrayfinal)) * arrayfinal
    npix = [str(int(np.size(np.where(arraygfirst > 0.5)) / 2.))]
    # create cleaned output image; use 3x3 median with CRs excluded
    arrayoutmask = np.where(arrayfinal > 1, 1, arrayfinal)
    ntt.cosmics.tofits(outmask, np.float32(
        arrayoutmask), headerfirst, verbose=False)
    delete(inputmask)
    arrayinputmask = (1 - (10000 * arrayoutmask)) * arrayoldoutput
    ntt.cosmics.tofits(inputmask, np.float32(
        arrayinputmask), headerfirst, verbose=False)
    delete(med5)
    iraf.median(inputmask, med5, 5, 5, zloreject=-
                9999, zhi='INDEF', verbose='no')
    iraf.imarith(outmask, "*", med5, med5)
    delete('_yyy.fits')
    iraf.imutil.imexpr(expr='(1-a)*b+c', a=outmask, b=oldoutput,
                       c=med5, output='_yyy.fits', verbose='no')
    # add sky and object spectra back in
    iraf.imarith('_yyy.fits', "+", skymod, '_yyy.fits')
    # cleanup and get ready for next iteration
    if npix == 0:
        stop = yes
      # delete temp files
    iraf.imcopy('_yyy.fits', output + '[350:550,*]', verbose='no')
    delete(blk + "," + lapla + "," + deriv2 + "," + med5)
    delete(med3 + "," + med7 + "," + noise + "," + sigmap)
    delete(firstsel + "," + starreject)
    delete(finalsel + "," + inputmask)
    delete(oldoutput + "," + skymod + "," + galaxy)
    delete("_" + med3 + ",_" + sigmap)
    delete('_kernel' + "," + '_gkernel')
    delete(outmask)
    delete('_xxx.fits,_yyy.fits')
Esempio n. 5
0
def lacos_im(_input, _output='clean.fits', outmask='mask.fits', gain=1.3, readn=9, xorder=9, yorder=9, sigclip=4.5, sigfrac=0.5, objlim=1, skyval=0, niter=2, verbose=True, interactive=False):
    # print "LOGX:: Entering `lacos_im` method/function in %(__file__)s" %
    # globals()
    import ntt
    from ntt.util import delete
    import sys
    import re
    import os
    import string
    from pyraf import iraf
    import numpy as np
    iraf.convolve.bilinear = 'no'
    iraf.convolve.radsym = 'no'
    # make temporary files
    oldoutput, galaxy, skymod, med5 = 'oldoutput.fits', 'galaxy.fits', 'skymod.fits', 'med5.fits'
    blk, lapla, med3, med7, sub5, sigima, finalsel = 'blk.fits', 'lapla.fits', 'med3.fits', 'med7.fits', 'sub5.fits', 'sigima.fits', 'finalsel.fits'
    deriv2, noise, sigmap, firstsel, starreject = 'deriv2.fits', 'noise.fits', 'sigmap.fits', 'firstsel.fits', 'starreject.fits'
    inputmask, gfirstsel = 'inputmask.fits', 'gfirstsel.fits'
    f = open('_kernel', 'w')
    f.write('0 -1 0;\n-1 4 -1;\n0 -1 0')
    f.close()
    # create growth kernel
    f = open('_gkernel', 'w')
    f.write('1 1 1;\n1 1 1;\n1 1 1')
    f.close()
    gkernel = np.array([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]])
    # initialize loop
    usegain = gain
    i = 1
    stop = 'no'
    previous = 0
    if not _output:
        _output = _input

    arrayinput, headerinput = ntt.cosmics.fromfits(_input, verbose=False)
    ntt.cosmics.tofits(outmask, np.float32(
        arrayinput - arrayinput), headerinput, verbose=False)

    delete(oldoutput)
    if skyval > 0:
        arrayoldoutput = arrayinput + skyval
    else:
        arrayoldoutput = arrayinput
    ntt.cosmics.tofits(oldoutput, np.float32(
        arrayoldoutput), headerinput, verbose=False)
    # start iterations
    while stop == 'no':
        # take second-order derivative (Laplacian) of input image
        # kernel is convolved with subsampled image, in order to remove negative
        # pattern around high pixels
        delete(blk)
        delete(lapla)
        delete(deriv2)
        iraf.blkrep(oldoutput, blk, 2, 2)
        iraf.convolve(blk, lapla, '_kernel')
        iraf.imreplace(lapla, 0, upper=0, lower='INDEF', radius=0)
        iraf.blkavg(lapla, deriv2, 2, 2, option="average")
        delete(med5)
        # create model of background flux - 5x5 box should exclude all CRs
        iraf.median(oldoutput, med5, 5, 5, zlo='INDEF',
                    zhi='INDEF', verbose='no')
        iraf.imreplace(med5, 0.0001, upper=0, lower='INDEF', radius=0)
        # create noise model
        delete(noise)
        iraf.imutil.imexpr(expr='sqrt(a*' + str(usegain) + '+' + str(readn) +
                           '**2)/' + str(usegain), a=med5, output=noise, verbose='no')
        # divide Laplacian by noise model
        delete(sigmap)
        iraf.imarith(deriv2, "/", noise, sigmap)
        # Laplacian of blkreplicated image counts edges twice:
        iraf.imarith(sigmap, "/", 2., sigmap)
        # removal of large structure (bright, extended objects)
        delete(med5)
        iraf.median(sigmap, med5, 5, 5, zlo='INDEF', zhi='INDEF', verbose='no')
        arraysigmap, headersigmap = ntt.cosmics.fromfits(sigmap, verbose=False)
        arraymed5, headermed5 = ntt.cosmics.fromfits(med5, verbose=False)
        arraysigmap = arraysigmap - arraymed5
        iraf.imarith(sigmap, "-", med5, sigmap)
        # find all candidate cosmic rays
        # this selection includes sharp features such as stars and HII regions

        delete(firstsel)
        iraf.imcopy(sigmap, firstsel, verbose='no')
        iraf.imreplace(firstsel, 0, upper=sigclip, lower='INDEF', radius=0)
        iraf.imreplace(firstsel, 1, lower=0.1, upper='INDEF', radius=0)
#		arraygfirst=arraysigmap
#		arraygfirst = np.where(arraygfirst<sigclip,0,arraygfirst)
#		arraygfirst = np.where(arraygfirst>0.1,1,arraygfirst)

        # compare candidate CRs to median filtered image
        # this step rejects bright, compact sources from the initial CR list
        # subtract background and smooth component of objects
        delete(med3)
        delete(med7)
        iraf.median(oldoutput, med3, 3, 3, zlo='INDEF',
                    zhi='INDEF', verbose='no')
        iraf.median(med3, med7, 7, 7, zlo='INDEF', zhi='INDEF', verbose='no')
        iraf.imarith(med3, "-", med7, med3)
        iraf.imarith(med3, "/", noise, med3)
        iraf.imreplace(med3, 0.01, upper=0.01, lower='INDEF', radius=0)
        # compare CR flux to object flux
        delete(starreject)
        iraf.imutil.imexpr(expr="(a*b)/c", a=firstsel, b=sigmap,
                           c=med3, output=starreject, verbose='no')
        # discard if CR flux <= objlim * object flux
        iraf.imreplace(starreject, 0, upper=objlim, lower='INDEF', radius=0)
        iraf.imreplace(starreject, 1, lower=0.5, upper='INDEF', radius=0)
        iraf.imarith(firstsel, "*", starreject, firstsel)
        # grow CRs by one pixel and check in original sigma map
        delete(gfirstsel)
        iraf.convolve(firstsel, gfirstsel, '_gkernel')
        iraf.imreplace(gfirstsel, 1, lower=0.5, upper='INDEF', radius=0)
        iraf.imarith(gfirstsel, "*", sigmap, gfirstsel)
        iraf.imreplace(gfirstsel, 0, upper=sigclip, lower='INDEF', radius=0)
        iraf.imreplace(gfirstsel, 1, lower=0.1, upper='INDEF', radius=0)
        # grow CRs by one pixel and lower detection limit
        sigcliplow = sigfrac * sigclip
        delete(finalsel)
        iraf.convolve(gfirstsel, finalsel, '_gkernel')
        iraf.imreplace(finalsel, 1, lower=0.5, upper='INDEF', radius=0)
        iraf.imarith(finalsel, "*", sigmap, finalsel)
        iraf.imreplace(finalsel, 0, upper=sigcliplow, lower='INDEF', radius=0)
        iraf.imreplace(finalsel, 1, lower=0.1, upper='INDEF', radius=0)
        # determine number of CRs found in this iteration
        delete(gfirstsel)
        iraf.imutil.imexpr(expr="(1-b)*a", a=outmask,
                           b=finalsel, output=gfirstsel, verbose='no')

        npix = iraf.imstat(gfirstsel, fields="npix",
                           lower=0.5, upper='INDEF', Stdout=1)
        # create cleaned output image; use 3x3 median with CRs excluded
        delete(med5)
        iraf.imarith(outmask, "+", finalsel, outmask)
        iraf.imreplace(outmask, 1, lower=1, upper='INDEF', radius=0)

        delete(inputmask)
        iraf.imutil.imexpr(expr="(1-10000*a)", a=outmask,
                           output=inputmask, verbose='no')
        iraf.imarith(oldoutput, "*", inputmask, inputmask)
        delete(med5)
        iraf.median(inputmask, med5, 5, 5, zloreject=-
                    9999, zhi='INDEF', verbose='no')
        iraf.imarith(outmask, "*", med5, med5)
        if i > 1:
            delete(_output)

        delete(_output)
        iraf.imutil.imexpr(expr="(1.-b)*a+c", a=oldoutput,
                           b=outmask, c=med5, output=_output, verbose='no')

        # cleanup and get ready for next iteration
        delete(oldoutput)
        iraf.imcopy(_output, oldoutput, verbose='no')

        if npix == 0:
            stop = 'yes'
        i = i + 1
        if i > niter:
            stop = 'yes'
        # delete temp files
        delete(blk + "," + lapla + "," + deriv2 + "," + med5)
        delete(med3 + "," + med7 + "," + noise + "," + sigmap)
        delete(firstsel + "," + starreject + "," + gfirstsel)
        delete(finalsel + "," + inputmask)

    if skyval > 0:
        iraf.imarith(_output, "-", skyval, _output)
    delete('_kernel' + "," + '_gkernel')
    delete(oldoutput)
def prep(df_image,hi_res_image,width_mask=1.5,unmaskgal=False,galvalues = None):
    print "\n************ Running the preparation steps ************\n"
    iraf.imdel('_mask.fits')
    iraf.imdel('_fluxmod_cfht*.fits')
    iraf.imdel('_df_4*.fits')
    
    'run SExtractor to get bright sources that are easily detected in the high resolution data'
    subprocess.call('sex %s'%hi_res_image,shell=True)    #####  Add in option to change sextractor threshold detect_thresh and analysis_thresh 2/3
    'copy the segmentation map to a mask'
    iraf.imcopy('seg.fits','_mask.fits')

    ##### Add step to get rid of diffraction spikes

    if unmaskgal:
        'Run SExtractor to get values for the central galaxy'
        print '\nDoing a second sextractor run to unmask central galaxy. \n'
        analysis_thresh_lg=2;back_size_lg=128;detect_thresh_lg=2;detect_minarea_lg=60
        segname = run_SExtractor(hi_res_image,detect_thresh=detect_thresh_lg,analysis_thresh=analysis_thresh_lg,back_size=back_size_lg,detect_minarea=detect_minarea_lg)
        print '\nSegmentation map is named: '+segname
        
        'Open up the data'
        seg2data = fits.getdata(segname)
        segdata,segheader = fits.getdata('_mask.fits',header=True)
        
        'Detect sources from the segmentation map'
        from photutils import detect_sources
        segrefdata = detect_sources(seg2data, 3, npixels=5)#, filter_kernel=kernel) 
        segrefdata = segrefdata.data
        segrefname = re.sub('.fits','_ds.fits',segname)
        writeFITS(segrefdata,segheader,segrefname)    
        print '\nSource separated segmentation map is named: '+segrefname
        
        'Use detected source seg map to mask galaxies' #galvalues = [3531,5444,5496]
        print 'Unmask the galaxies in the mask from the original segmap. \n'
        segdatanew = unmaskgalaxy(segdata,'_mask.fits',segrefname=segrefname,segref=segrefdata,galvalues=galvalues)        
        writeFITS(segdatanew,segheader,'_mask.fits')
        
    if verbose:
        print_verbose_string('Carrying on')
        
    'replace the values in the segments in the segmentation map (i.e. stars) all to 1, all the background is still 0'
    iraf.imreplace('_mask.fits',1,lower=0.5)
    'multiply the mask (with 1s at the stars) by the high res image to get the star flux back - now have the flux model'
    iraf.imarith('_mask.fits','*','%s'%hi_res_image,'_fluxmod_cfht')
    
    'smooth the flux model'
    'increase size of mask, so more is subtracted'
    'the "1.5" in the next line should be a user-defined parameter that is given'
    'to the script; it controls how much of the low surface brightness'
    'emission in the outskirts of galaxies is subtracted. This choice'
    'depends on the science application'
    iraf.gauss('_fluxmod_cfht','_fluxmod_cfht_smoothed',width_mask,nsigma=4.)

    iraf.imreplace('_fluxmod_cfht_smoothed', -1, lower=0, upper=0)
    iraf.imreplace('_fluxmod_cfht_smoothed', 1, lower=-0.5)
    iraf.imreplace('_fluxmod_cfht_smoothed', 0, upper=-0.5)
    iraf.imarith('%s'%hi_res_image, '*','_fluxmod_cfht_smoothed', '_fluxmod_cfht_new')
    
    ' this is the key new step! we"re registering the CFHT image'
    ' to a frame that is 4x finer sampled than the Dragonfly image.'
    ' this avoids all the pixelation effects we had before.'
    '  (4x seems enough; but we could have it as a free parameter - need'
    ' to be careful as it occurs elsewhere in the scripts too) '
    iraf.blkrep('%s'%df_image,'_df_4',4,4)
    
    'register the flux model onto the same pixel scale as the dragonfly image'
    iraf.wregister('_fluxmod_cfht_new','_df_4','_fluxmod_dragonfly',interpo='linear',fluxcon='yes')

    return None
Esempio n. 7
0
#!/usr/bin/env python

import sys, os, string
import pyraf
from pyraf import iraf
from iraf import images, tv, sleep, imutil, imgeom, blkrep, imcopy, imdelete

imagen = sys.argv[1]

iraf.imcopy(input=imagen + '[1]', output="test.fits")
iraf.blkrep(input="test.fits", output=imagen + '_GMOS', b1=12, b2=1)

for i in range(1, 13):
    #    print i
    if i < 2:
        #        print i
        iraf.imcopy(input=imagen + '[' + str(i) + ']',
                    output=imagen + '_GMOS.fits[1:' + str(288) + ',*]')
        #    if i>1 and i<12:
    else:
        #        print i,i
        iraf.imcopy(input=imagen + '[' + str(i) + ']',
                    output=imagen + '_GMOS.fits[' + str(
                        (i - 1) * 288) + ':' + str((i) * 288) + ',*]')
#    if i>12:
#        iraf.imcopy(input=imagen+'['+str(i)+']',output=imagen+'_GMOS.fits['+str(i*288)+':'+str((i+1)*288)+',*]')

iraf.imdelete("test.fits")
Esempio n. 8
0
#!/usr/bin/env python

import sys,os,string
import pyraf
from pyraf import iraf
from iraf import images,tv,sleep,imutil,imgeom,blkrep,imcopy,imdelete

imagen=sys.argv[1]

iraf.imcopy(input=imagen+'[1]',output="test.fits")
iraf.blkrep(input="test.fits",output=imagen+'_GMOS',b1=12,b2=1)

for i in range (1,13):
#    print i
   if i < 2:
#        print i
       iraf.imcopy(input=imagen+'['+str(i)+']',output=imagen+'_GMOS.fits[1:'+str(288)+',*]')
       #    if i>1 and i<12:
   else:
#        print i,i
       iraf.imcopy(input=imagen+'['+str(i)+']',output=imagen+'_GMOS.fits['+str((i-1)*288)+':'+str((i)*288)+',*]')
#    if i>12:    
#        iraf.imcopy(input=imagen+'['+str(i)+']',output=imagen+'_GMOS.fits['+str(i*288)+':'+str((i+1)*288)+',*]')

iraf.imdelete("test.fits")
Esempio n. 9
0
def lacos_spec(_input, output='clean.fits', outmask='mask.fits', gain=1.3, readn=9,\
               xorder=9, yorder=3, sigclip=4.5, sigfrac=0.5, objlim=1, niter=4, instrument='kastr', verbose=True, interactive=False):
    # print "LOGX:: Entering `lacos` method/function in %(__file__)s" %
    # globals()
    import lickshane
    import sys
    import re
    import os
    import string
    from pyraf import iraf
    import numpy as np

    oldoutput, galaxy, skymod, med5 = 'oldoutput.fits', 'galaxy.fits', 'skymod.fits', 'med5.fits'
    blk, lapla, med3, med7, sub5, sigima, finalsel = 'blk.fits', 'lapla.fits', 'med3.fits', 'med7.fits', 'sub5.fits', 'sigima.fits', 'finalsel.fits'
    deriv2, noise, sigmap, firstsel, starreject = 'deriv2.fits', 'noise.fits', 'sigmap.fits', 'firstsel.fits', 'starreject.fits'
    inputmask = 'inputmask.fits'
    # set some parameters in standard IRAF tasks
    iraf.convolve.bilinear = 'no'
    iraf.convolve.radsym = 'no'
    # create Laplacian kernel
    # laplkernel = np.array([[0.0, -1.0, 0.0], [-1.0, 4.0, -1.0], [0.0, -1.0, 0.0]])
    f = open('_kernel', 'w')
    f.write('0 -1 0;\n-1 4 -1;\n0 -1 0')
    f.close()
    # create growth kernel
    f = open('_gkernel', 'w')
    f.write('1 1 1;\n1 1 1;\n1 1 1')
    f.close()
    gkernel = np.array([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]])
    lickshane.util.delete(galaxy)
    lickshane.util.delete(skymod)
    lickshane.util.delete(oldoutput)

    if not output:
        output = _input
    else:
        os.system('cp ' + _input + ' ' + output)
    os.system('cp ' + _input + ' ' + oldoutput)

    arrayinput, headerinput = lickshane.cosmics.fromfits(oldoutput,
                                                         verbose=False)
    lickshane.cosmics.tofits(outmask,
                             np.float32(arrayinput - arrayinput),
                             headerinput,
                             verbose=False)

    if instrument in ['kastr']:
        axis1 = 1
        axis2 = 2
    elif instrument in ['kastb']:
        axis1 = 2
        axis2 = 1

    # subtract object spectra if desired
    if xorder > 0:
        iraf.fit1d(oldoutput,
                   galaxy,
                   "fit",
                   axis=axis1,
                   order=xorder,
                   func="leg",
                   low=4.,
                   high=4.,
                   nav=1,
                   inter='no',
                   sample="*",
                   niter=3,
                   grow=0,
                   cursor="")
        iraf.imarith(oldoutput, "-", galaxy, oldoutput)
    else:
        lickshane.cosmics.tofits(galaxy,
                                 np.float32(arrayinput - arrayinput),
                                 headerinput,
                                 verbose=False)

    # Subtract sky lines
    if yorder > 0:
        iraf.fit1d(oldoutput,
                   skymod,
                   "fit",
                   axis=axis2,
                   order=yorder,
                   func="leg",
                   low=4.,
                   high=4.,
                   inter='no',
                   sample="*",
                   nav=1,
                   niter=3,
                   grow=0,
                   cursor="")
        iraf.imarith(oldoutput, "-", skymod, oldoutput)
    else:
        lickshane.cosmics.tofits(skymod,
                                 np.float32(arrayinput - arrayinput),
                                 headerinput,
                                 verbose=False)

    arrayoldoutput, headeroldoutput = lickshane.cosmics.fromfits(oldoutput,
                                                                 verbose=False)

    # add object spectra to sky model
    iraf.imarith(skymod, "+", galaxy, skymod)

    ###########
    ##  start iteration
    ###########
    ii = 0
    while ii < niter:
        print ii
        # add median of residuals to sky model
        lickshane.util.delete(med5)
        iraf.median(oldoutput,
                    med5,
                    5,
                    5,
                    zlor='INDEF',
                    zhir='INDEF',
                    verbose='no')
        #          m5 = ndimage.filters.median_filter(_inputarray, size=5, mode='mirror')
        iraf.imarith(skymod, "+", med5, med5)

        # take second-order derivative (Laplacian) of input image
        # kernel is convolved with subsampled image, in order to remove negative
        # pattern around high pixels
        lickshane.util.delete(blk)
        lickshane.util.delete(lapla)
        lickshane.util.delete(deriv2)
        lickshane.util.delete(noise)
        lickshane.util.delete(sigmap)

        iraf.blkrep(oldoutput, blk, 2, 2)
        iraf.convolve(blk, lapla, '_kernel')
        iraf.imreplace(lapla, 0, upper=0, lower='INDEF')
        iraf.blkavg(lapla, deriv2, 2, 2, option="average")

        # create noise model
        iraf.imutil.imexpr(expr='sqrt(a*' + str(gain) + '+' + str(readn) +
                           '**2)/' + str(gain),
                           a=med5,
                           output=noise,
                           verbose='no')
        iraf.imreplace(med5, 0.00001, upper=0, lower='INDEF')

        # divide Laplacian by noise model
        iraf.imutil.imexpr(expr='(a/b)/2',
                           a=deriv2,
                           b=noise,
                           output=sigmap,
                           verbose='no')

        # removal of large structure (bright, extended objects)
        lickshane.util.delete(med5)
        iraf.median(sigmap, med5, 5, 5, zlo='INDEF', zhi='INDEF', verbose='no')
        iraf.imarith(sigmap, "-", med5, sigmap)

        # find all candidate cosmic rays
        # this selection includes sharp features such as stars and HII regions
        arraysigmap, headersigmap = lickshane.cosmics.fromfits(sigmap,
                                                               verbose=False)
        arrayf = np.where(arraysigmap < sigclip, 0, arraysigmap)
        arrayf = np.where(arrayf > 0.1, 1, arrayf)
        lickshane.cosmics.tofits(firstsel,
                                 np.float32(arrayf),
                                 headersigmap,
                                 verbose=False)

        # compare candidate CRs to median filtered image
        # this step rejects bright, compact sources from the initial CR list
        # subtract background and smooth component of objects
        lickshane.util.delete(med3)

        iraf.median(oldoutput,
                    med3,
                    3,
                    3,
                    zlo='INDEF',
                    zhi='INDEF',
                    verbose='no')
        lickshane.util.delete(med7)
        lickshane.util.delete('_' + med3)
        iraf.median(med3, med7, 7, 7, zlo='INDEF', zhi='INDEF', verbose='no')
        iraf.imutil.imexpr(expr='(a-b)/c',
                           a=med3,
                           b=med7,
                           c=noise,
                           output='_' + med3,
                           verbose='no')
        iraf.imreplace('_' + med3, 0.01, upper=0.01, lower='INDEF')

        # compare CR flux to object flux
        lickshane.util.delete(starreject)
        iraf.imutil.imexpr(expr='(a*b)/c',
                           a=firstsel,
                           b=sigmap,
                           c="_" + med3,
                           output=starreject,
                           verbose='no')
        #   ######         #####        ######          #####       ######  FOUND A BUG ?
        #          iraf.imutil.imexpr(expr='a+b+c', a=firstsel, b=sigmap,
        #                             c="_" + med3, output=starreject, verbose='no')

        # discard if CR flux <= objlim * object flux
        iraf.imreplace(starreject, 0, upper=objlim, lower='INDEF')
        iraf.imreplace(starreject, 1, lower=0.5, upper='INDEF')
        iraf.imarith(firstsel, "*", starreject, firstsel)

        # grow CRs by one pixel and check in original sigma map
        arrayfirst, headerfirst = lickshane.cosmics.fromfits(firstsel,
                                                             verbose=False)
        arraygfirst = lickshane.cosmics.my_convolve_with_FFT2(
            arrayfirst, gkernel)

        arraygfirst = np.where(arraygfirst > 0.5, 1, arraygfirst)
        arraygfirst = arraygfirst * arraysigmap
        arraygfirst = np.where(arraygfirst < sigclip, 0, arraygfirst)
        arraygfirst = np.where(arraygfirst > 0.1, 1, arraygfirst)

        # grow CRs by one pixel and lower detection limit
        sigcliplow = sigfrac * sigclip

        # Finding neighbouring pixels affected by cosmic rays
        arrayfinal = lickshane.cosmics.my_convolve_with_FFT2(
            arraygfirst, gkernel)
        arrayfinal = np.where(arrayfinal > 0.5, 1, arrayfinal)
        arrayfinal = arrayfinal * arraysigmap
        arrayfinal = np.where(arrayfinal < sigcliplow, 0, arrayfinal)
        arrayfinal = np.where(arrayfinal > 0.1, 1, arrayfinal)

        # determine number of CRs found in this iteration
        arraygfirst = (1 - (arrayfinal - arrayfinal)) * arrayfinal
        npix = [str(int(np.size(np.where(arraygfirst > 0.5)) / 2.))]

        # create cleaned output image; use 3x3 median with CRs excluded
        arrayoutmask = np.where(arrayfinal > 1, 1, arrayfinal)
        lickshane.cosmics.tofits(outmask,
                                 np.float32(arrayoutmask),
                                 headerfirst,
                                 verbose=False)
        lickshane.util.delete(inputmask)
        arrayinputmask = (1 - (10000 * arrayoutmask)) * arrayoldoutput
        lickshane.cosmics.tofits(inputmask,
                                 np.float32(arrayinputmask),
                                 headerfirst,
                                 verbose=False)
        lickshane.util.delete(med5)
        iraf.median(inputmask,
                    med5,
                    5,
                    5,
                    zloreject=-9999,
                    zhi='INDEF',
                    verbose='no')
        iraf.imarith(outmask, "*", med5, med5)
        lickshane.util.delete(output)
        iraf.imutil.imexpr(expr='(1-a)*b+c',
                           a=outmask,
                           b=oldoutput,
                           c=med5,
                           output=output,
                           verbose='no')

        lickshane.util.delete(oldoutput)
        os.system('cp ' + output + ' ' + oldoutput)

        # add sky and object spectra back in
        iraf.imarith(output, "+", skymod, output)
        # cleanup and get ready for next iteration
        ii = ii + 1
        if npix == 0:
            ii = niter
    # delete temp files

    lickshane.util.delete(blk + "," + lapla + "," + deriv2 + "," + med5)
    lickshane.util.delete(med3 + "," + med7 + "," + noise + "," + sigmap)
    lickshane.util.delete(firstsel + "," + starreject)
    lickshane.util.delete(finalsel + "," + inputmask)
    lickshane.util.delete(oldoutput + "," + skymod + "," + galaxy)
    lickshane.util.delete("_" + med3 + ",_" + sigmap)
    lickshane.util.delete('_kernel' + "," + '_gkernel')
    lickshane.util.delete(outmask)
Esempio n. 10
0

		#finding out which slits need the tilt correction
		if slit_tilt != 0.0:

			#here I'm using IRAF's block replicate function, confusing way of saying "enlarging", to turn 1 pixel into more
			#this will allow me to make corrections to the tilt in increments smaller than a pixel (imcopy only takes integers) 
			#this will trick it so an integer is now less than one pixel - how small determined by how much I enlarge it by
			y_exp = 5
			x_exp = 5
			
			#I have to put the formatted strings into smaller variables otherwise iraf tasks break with the long names
			#orig_image = '%s[%d]' % (filename[:-5], ext_num)
			#rep_image = '%s[%d, overwrite]' % (filename[:-5], ext_num)

			iraf.blkrep('%s[%d]' % (filename[:-5], ext_num), '%s[%d, overwrite]' % (filename[:-5], ext_num), x_exp, y_exp)
			#this is block replicating the slits in the final corrected_file so I can put the expanded slits into the final image
			iraf.blkrep('%s[%d]' % (corrected_file[:-5], ext_num), '%s[%d,overwrite]' % (corrected_file[:-5], ext_num), x_exp, y_exp)

			#now I'm renaming the dimensions from the MDF file to reflect the enlargement
			#I only need to change the ending dimensions (x2 and y2) because the beginning will still be 0's or 1's
			mod_secy2 = secy2 * y_exp
			mod_secx2 = secx2 * x_exp
			mod_slit_width = slit_width * 5			

			
			#setting this slit to have values of zero so when the tilt is corrected the extra space is filled w/ 0's 
			#this way the uncorrected image won't show through areas that corrected image doesn't cover
			iraf.imarith('%s[%d]' % (corrected_file, ext_num), '*', '0', '%s[%d,overwrite]' % (corrected_file, ext_num))

Esempio n. 11
0
	mdfinfo = pyfits.getdata(filename, 'MDF')

	#loading the slit tilt info from the MDF of the file
	for i in range(len(mdfinfo)):
		slit_tilt = mdfinfo[i]['slittilt']
		ext_num = i-1

		#finding out which slits had the tilt correction so we can read the imcopy logs and put the tilt back
		if slit_tilt != 0.0:

			#I'm using "blkrep" again to enlarge the slits that were enlarged before and retilting them using the imcopy logs
			#this allows me to reverse the corrections that were made in smaller incrementsthan a pixel 
			y_exp = 5
			x_exp = 5

			iraf.blkrep('%s[%d]' % (filename[:-5], ext_num), '%s[%d, overwrite]' % (filename[:-5], ext_num), x_exp, y_exp)
			#this is block replicating the slits in the final retilted_file so the dimensions will be correct for me to copy them over to that file and then I can "compact" them with block averaging in that file
			iraf.blkrep('retilted_%s[%d]' % (filename[:-5], ext_num), 'retilted_%s[%d,overwrite]' % (filename[:-5], ext_num), x_exp, y_exp)

			#setting this slit to have values of 0 so when it's retilted the extra space is filled w/ 0's 
			#this way the old image won't appear through the edges
			iraf.imarith('retilted_%s[%d]' % (filename, ext_num), '*', '0', 'retilted_%s[%d,overwrite]' % (filename, ext_num))

			#the reason the [18:5] is there is to get the root name of the filename and load that filename's imcopy log
			imcopy_log = [line.strip() for line in open('%s[%d]_imcopy_log.txt' % (filename[18:-5], ext_num))]
			
			#now going through each line in the imcopy log and pulling out the bits I need	
			for line in imcopy_log:

				#I need to break up the imcopy log into more variables, can't pass imcopy that whole string, thinks its literal
				#I'll first start with the by splitting up the first command in the result log line