Пример #1
0
def psfconv(df_image,psf):
    print "\n************ Running the psf convolution steps ************\n"

    iraf.imdel('_model.fits')
    iraf.imdel('_model_4.fits')
    iraf.imdel('_res*.fits')
    iraf.imdel('_psf*.fits')
    iraf.imdel('_df_sub')
    
    'subtract the sky value from the dragonfly image header'
    try:
        df_backval = fits.getheader(df_image)['BACKVAL']
        iraf.imarith('%s'%df_image,'-','%s'%df_backval,'_df_sub')
    except:
        print "WARNING: No BACKVAL to subtract!  Skipping the background subtraction..."
        iraf.imcopy('%s'%df_image,'_df_sub.fits')
    
    ##### subtract the background from the cfht image?
    
    'convolve the model with the Dragonfly PSF'
    if usemodelpsf:
        makeallisonspsf()
        psf = './psf/psf_static_fullframe.fits'
        
    if verbose:
        print 'VERBOSE:  Using %s for the psf convolution.'%psf
        
    'resample the PSF by a factor of 4'
    iraf.magnify('%s'%psf,'_psf_4',4,4,interp="spline3")
    
    'this is just to retain the same total flux in the psf'
    iraf.imarith('_psf_4','*',16.,'_psf_4')
    
    'Convolve with the psf'
    # from scipy import signal
    # fluxmoddata,fluxmodheader = fits.getdata('_fluxmod_dragonfly.fits',header=True)
    # psfdata = fits.getdata('_psf_4.fits')
    # fluxmodheader['COMMENT']='convolved with '+'_psf_4.fits'
    # modeldata = signal.fftconvolve(fluxmoddata, psfdata)
    # print ""
    # print fluxmoddata.shape
    # print modeldata.shape
    # print ""
    # writeFITS(modeldata,fluxmodheader,'_model_4.fits')
    iraf.stsdas.analysis.fourier.fconvolve('_fluxmod_dragonfly','_psf_4','_model_4')
    
    'now after the convolution we can go back to the Dragonfly resolution'
    iraf.blkavg('_model_4','_model',4,4,option="average")
    
    
    return None
Пример #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')
Пример #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)
Пример #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')
Пример #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)
Пример #6
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)
Пример #7
0
def extract(arcs, objs,ccd,mode=1):
    """
    mode=1: 1D extraction
    mode=2: 2D extraction
    """
    #apall na vseh luckah
    print " + identifing arcs"

    for i in arcs:
        iraf.hedit(images='tmp/arcs/'+i[1], fields='DISPAXIS', value=1, add='yes', verify='no', update='yes',Stdout="/dev/null")
        iraf.apall(input='tmp/arcs/%s' % (i[1]), referen='tmp/masterflat.fits', format='multispec', interac='no', find='no', recenter='no', resize='no', edit='no', trace='no', fittrac='no', extract='yes', extras='no', review='yes', lower=-3.0, upper=3.0, nsubaps=7, pfit="fit1d", Stdout="/dev/null")

    if mode==1:
        pass
    else:
        iraf.cd("tmp/arcs")
        geometry_prep(arcs,ccd)
        os.system("cp transformations* ../objs/")
        #sys.exit(0)
        iraf.cd("../..")
        for i in arcs:
            os.system("rm tmp/arcs/%s.ms.fits" % (i[1][:-5]))
            os.system("rm tmp/arcs/%s_cut*.fits" % (i[1][:-5]))
            os.system("rm tmp/arcs/%s_t*.fits" % (i[1][:-5]))
            pass

        os.system("rm tmp/arcs/calibrations/idarcs_cut*")
        os.system("rm tmp/arcs/arcs_cut*")

        #extract 2d arcs and objects
        for i in arcs:
            iraf.hedit(images='tmp/arcs/'+i[1], fields='DISPAXIS', value=1, add='yes', verify='no', update='yes',Stdout="/dev/null")
            iraf.apall(input='tmp/arcs/%s' % (i[1]), referen='tmp/masterflat.fits', format='multispec', interac='no', find='no', recenter='no', resize='no', edit='no', trace='no', fittrac='no', extract='yes', extras='no', review='yes', lower=-3.0, upper=3.0, nsubaps=7, pfit="fit1d", Stdout="/dev/null")
            #forget apertures
            for j in range(8,1000):
                iraf.hedit(images='tmp/arcs/%s.ms.fits' % (i[1][:-5]), fields="APNUM%s" % (str(j)), value='', delete="yes", verify="no", Stdout="/dev/null")

        for i in objs:
            iraf.hedit(images='tmp/objs/'+i[1], fields='DISPAXIS', value=1, add='yes', verify='no', update='yes',Stdout="/dev/null")
            iraf.apall(input='tmp/objs/%s' % (i[1]), referen='tmp/masterflat.fits', format='multispec', interac='no', find='no', recenter='no', resize='no', edit='no', trace='no', fittrac='no', extract='yes', extras='no', review='yes', lower=-3.0, upper=3.0, nsubaps=7, pfit="fit1d", Stdout="/dev/null")
            #forget apertures
            for j in range(8,1000):
                iraf.hedit(images='tmp/objs/%s.ms.fits' % (i[1][:-5]), fields="APNUM%s" % (str(j)), value='', delete="yes", verify="no", Stdout="/dev/null")

        iraf.cd("tmp/arcs")
        for ii in arcs:
            geometry_transform(ii)
        iraf.cd("../..")

        iraf.cd("tmp/objs")
        for ii in objs:
            geometry_transform(ii)
        iraf.cd("../..")

    #make normal 1d extraction and copy results into it
    for i in arcs:
        os.system("rm -f tmp/arcs/%s" % (i[1][:-5]+".ms.fits"))
        iraf.apall(input='tmp/arcs/%s' % (i[1]), referen='tmp/masterflat.fits', format='multispec', interac='no', find='no', recenter='no', resize='no', edit='no', trace='no', fittrac='no', extract='yes', extras='no', review='yes', lower=-3.0, upper=3.0, nsubaps=1, pfit="fit1d", Stdout="/dev/null")
        os.system("cp tmp/arcs/%s" % (i[1][:-5])+".ms.fits tmp/arcs/%s" % (i[1][:-5])+".ms2.fits")

        if mode==1:
            pass
        else:
            for j in range(1,393):
                os.system("rm -f tmp/copy_tmp.fits")
                try:
                    iraf.blkavg(input="tmp/arcs/"+i[1][:-5]+"_t%s.fits" % (str(j)), output="tmp/copy_tmp", option='sum', b1=1, b2=7, Stdout="/dev/null")
                    iraf.imcopy(input="tmp/copy_tmp", output="tmp/arcs/"+i[1][:-5]+".ms.fits[*,%s]" % (j), Stdout="/dev/null")
                except:
                    pass

    for i in objs:
        os.system("rm -f tmp/objs/%s" % (i[1][:-5]+".ms.fits"))
        iraf.apall(input='tmp/objs/%s' % (i[1]), referen='tmp/masterflat.fits', format='multispec', interac='no', find='no', recenter='no', resize='no', edit='no', trace='no', fittrac='no', extract='yes', extras='no', review='yes', lower=-3.0, upper=3.0, nsubaps=1, pfit="fit1d", Stdout="/dev/null")
        os.system("cp tmp/objs/%s" % (i[1][:-5])+".ms.fits tmp/objs/%s" % (i[1][:-5])+".ms2.fits")

        if mode==1:
            pass
        else:
            for j in range(1,393):
                os.system("rm -f tmp/copy_tmp.fits")
                try:
                    iraf.blkavg(input="tmp/objs/"+i[1][:-5]+"_t%s.fits" % (str(j)), output="tmp/copy_tmp", option='sum', b1=1, b2=7, Stdout="/dev/null")
                    iraf.imcopy(input="tmp/copy_tmp", output="tmp/objs/"+i[1][:-5]+".ms.fits[*,%s]" % (j), Stdout="/dev/null")
                except:
                    pass
Пример #8
0
						imcopy_output = iraf.imcopy(image1,image2, Stdout=1)
						imcopy_log.write('%s\n' % imcopy_output)


				except:
					print "\n\nCorrection not performed. Check slit image, correlation results, and imcopy parameters for %s[%d]\n\n" % (filename, ext_num)
					IPython.embed()
					sys.exit()


			#here I'm reversing IRAF's 'block replicate' with 'block averaging' (re-binning basically)
			#The 5's in the parameters are the number of columns to be block averaged
			#as it sits in this for-loop, the blkavg should take place after each slit is finished being corrected

			#this is compressing the slits in the original image that has the tilts in it
			iraf.blkavg('%s[%d]' % (filename[:-5], ext_num), '%s[%d,overwrite]' % (filename[:-5], ext_num), x_exp, y_exp)
			#this is compressing the slits in the tilt-corrected image
			iraf.blkavg('corrected_%s[%d]' % (filename[:-5], ext_num), 'corrected_%s[%d,overwrite]' % (filename[:-5], ext_num), x_exp, y_exp)



			#removing the extra files after the correction has been done
			#os.system('rm %s_ext%d_line1.fits' % (filename[:-5], ext_num))
			#os.system('rm %s_ext%d_line2.fits' % (filename[:-5], ext_num))
			#os.system('rm %s_ext%d_corr_results.fits' % (filename[:-5], ext_num))
			#os.system('rm %s' % trimmed_xcorr_result)


	imcopy_log.close()

os.system('rm tmp*')
Пример #9
0
if step == 1:
    source = fits.open("mystars_smeared.fits")
    image = source[0]
    try:
        os.remove('sourcelist.txt')
    except OSError:
        pass
    source_list = open('sourcelist.txt', 'w')
    coordinates = []
    for n in xrange(0, 50):
        coordinates.append(makeSource(21))
    for coordinate in coordinates:
        temp_string = ('\t'.join(str(i) for i in coordinate) + '\n')
        source_list.writelines(temp_string)
    source_list.close()
    savenum = 21.2
    save(savenum)
    iraf.blkavg('mystars_smeared_' + str(savenum) + '.fits',
                'mystars_smeared_' + str(savenum) + '.fits', expansion_factor,
                expansion_factor)
#for i in xrange(2,3):
#	makeField()
#	smear()
#recondense()
#mkNoise()
#	for n in xrange(0,50):
#		makeSource(21)
#	save(i)
#	iraf.blkavg('mystars_smeared_'+str(i)+'.fits','mystars_smeared_'+str(i)+'.fits',expansion_factor,expansion_factor)
#iraf.mknoise('mystars_smeared.fits')
Пример #10
0
				cm2_root_file = command2_pieces[0].split('.')[0][10:]

				cm2_x1 = int(command2_pieces[1].split(':')[0][1:])
				#the reason the cm2_x2 value isn't hardcoded is because the correction method works differently on pos/neg tilt
				cm2_x2 = int(command2_pieces[1].split(':')[1].split(',')[0])

				cm2_y1 = int(command2_pieces[1].split(',')[1].split(':')[0])
				cm2_y2 = int(command2_pieces[1].split(',')[1].split(':')[1])


				#putting all the bits together to make a more usable string
				#I had to wait to put the new_command1 down here becuase I needed to adjust the cm1_x2 value by subtracting cm2_x1; I needed to define the cm2_x1 before using it. The reason I need it is because I can't tell a small image piece, say x1-100 fill a x1-125 image piece <- the computer fills that extra space with extreme random values and it crashes the scripts. What I'm doing is adjusting the cm1_x2 value to be the correct length of the image piece I'm copying.
				new_command1 = '%s[%d][%d:%d,%d:%d]' % (cm1_root_file, ext_num, cm1_x1, cm1_x2-cm2_x1, cm1_y1, cm1_y2)
				new_command2 = 	'%s[%d][%d:%d,%d:%d]' % (cm2_root_file, ext_num, cm2_x1, cm2_x2, cm2_y1, cm2_y2)			

				#IPython.embed()
				#sys.exit()
				#since I've got the usable bits from the imcopy log file, I need to reverse which ever way they were corrected
				iraf.imcopy('optimum_corrected_%s' % new_command2, 'retilted_optimum_corrected_%s' % new_command1) 

			iraf.blkavg('retilted_%s[%d]' % (filename[:-5], ext_num), 'retilted_%s[%d,overwrite]' % (filename[:-5], ext_num), x_exp, y_exp)
#log_removal = raw_input("Delete the imcopy logs created from tilt_correction.py (which takes ~4 hours)? 'yes' or 'no'.\n")
#if log_removal == 'yes':
#	os.system('rm *_imcopy_log.txt')


end = time.strftime("%H:%M:%S")
print "Start time: ", start
print "End time: ", end