def mask_create(images, no_unlearn=False):
    output_files = []
    # If the output file list doesn't exsist, create it.
    if len(output_files) == 0:
        output_files = [os.path.join(os.path.dirname(i), "mask_" + os.path.basename(i)) for i in images]
    # Create a temporary file to hold the input image list
    tfile = tempfile.mkstemp()
    os.write(tfile[0], "\n".join(images) + "\n")
    os.close(tfile[0])

    # Create a temporary file to hold the input image list
    tofile = tempfile.mkstemp()
    os.write(tfile[0], "\n".join(output_files) + "\n")
    os.close(tfile[0])

    try:

        # Set up to call the task
        iraf.set(clobber="yes")
        iraf.nproto()

        if no_unlearn == False:
            print "Objmasks is unlearning!!"
            iraf.objmasks.unlearn()

        # set parameters
        iraf.objmasks.images = "@" + tfile[1]
        iraf.objmasks.objmasks = "@" + tofile[1]
        iraf.objmasks.omtype = "boolean"
        iraf.objmasks()

    except iraf.IrafError, e:
        print "objmasks failed"
        print "error #" + str(e.errno)
        print "Msg: " + e.errmsg
        print "Task: " + e.errtask
        raise e
Exemple #2
0
def mkobjmask(inlist,
              inpref='',
              outpref='m',
              bpm='none',
              minpix=250,
              hsigma=3,
              lsigma=10,
              conv='block 3 3'):

    # load IRAF package
    iraf.nproto()

    # open input list and check if it exists
    inimg_arr = check_input(inlist, inpref)
    if isinstance(inimg_arr, int):
        return 1

    # check output images
    outimg_arr = check_outpref(outpref, inimg_arr)
    if isinstance(outimg_arr, int):
        return 1

    # check optional parameters
    if bpm.lower() != 'none':
        if not os.access(bpm, os.R_OK):
            print >> sys.stderr, 'cannot read bad pixel mask (%s)' % bpm
            return 1
        bpmex = 1
    else:
        bpmex = 0

    if lsigma <= 0:
        print >> sy.stderr, 'invalid lsigma value (%f)' % lsigma
        return 1

    if hsigma <= 0:
        print >> sys.stderr, 'invalid hsigma value (%f)' % hsigma
        return 1

    # check convolution kernel name
    ret = 1
    param = conv.split()
    if len(param) == 1:
        if os.access(param[0], os.R_OK):
            conv = '@' + conv
            ret = 0
        else:
            print >> sys.stderr, 'convolution kernel file (%s) does not exist' % param[
                0]
    elif param[0] == 'block' and len(param) == 3:
        if param[1].isdigit() and param[2].isdigit():
            ret = 0
    elif param[0] == 'bilinear' and len(param) == 3:
        if param[1].isdigit() and param[2].isdigit() and int(
                param[1]) > 0 and int(param[2]) > 0:
            ret = 0
    elif param[0] == 'gauss' and len(param) == 5:
        if param[1].isdigit() and param[2].isdigit() and int(
                param[1]) > 0 and int(param[2]) > 0:
            if isfloat(param[3]) and isfloat(
                    param[4]) and float(param[3]) > 0 and float(param[4]) > 0:
                ret = 0

    if ret > 0:
        print >> sys.stderr, 'invalid convolve parameter (%s)' % conv
        return 1

    # prefix for temoprary images
    tmp = tempfile.NamedTemporaryFile(suffix='', prefix='', dir='/tmp')
    tmp_prefix = tmp.name
    tmp.close()

    # check if input images exist and fix bad pixel if requested
    for i in range(len(inimg_arr)):

        tmp_inimg = tmp_prefix + os.path.basename(inimg_arr[i])
        tmp_outimg = tmp_prefix + 'mask' + os.path.basename(outimg_arr[i])
        iraf.unlearn('imcopy')
        iraf.imcopy(inimg_arr[i], tmp_inimg, verbose='no')
        if bpmex == 1:
            iraf.unlearn('fixpix')
            iraf.fixpix(tmp_inimg, bpm)
            iraf.fixpix(tmp_inimg, bpm)
            iraf.fixpix(tmp_inimg, bpm)
        iraf.unlearn('objmasks')
        iraf.objmasks(tmp_inimg,
                      tmp_outimg,
                      omtype='boolean',
                      masks='',
                      convolve=conv,
                      lsigma=lsigma,
                      hsigma=hsigma,
                      hdetect='yes',
                      ldetect='yes',
                      minpix=minpix)
        ret = []
        ret = iraf.objmasks(tmp_inimg,
                            outimg_arr[i],
                            omtype='boolean',
                            masks=tmp_outimg + '[pl]',
                            convolve=conv,
                            lsigma=lsigma,
                            hsigma=hsigma,
                            hdetect='yes',
                            ldetect='yes',
                            minpix=minpix,
                            Stdout=1)
        if len(ret) != 0:
            iraf.unlearn('hedit')
            iraf.hedit(inimg_arr[i],
                       'objmask',
                       outimg_arr[i] + '[pl]',
                       add='yes',
                       verify='no')
        os.remove(tmp_inimg)
        os.remove(tmp_outimg)
        if len(ret) == 0:
            print >> sys.stderr, 'failed to execute iraf objmasks task for %s' % inimg_arr[
                i]
            return 1

    return 0
Exemple #3
0
def mkcombmask(combimg,
               inlist,
               sspref='sdfr',
               outpref='mc',
               minpix=250,
               hsigma=3,
               lsigma=10,
               conv='block 3 3',
               bpm='none',
               sigmap='none'):

    # load IRAF package
    iraf.nproto()

    # chceck combined image
    if not os.access(combimg, os.R_OK):
        print >> sys.stderr, 'cannot open combined image (%s)' % combimg
        return 1

    # open input list and check if it exists
    inimg_arr = check_input(inlist, sspref)
    if isinstance(inimg_arr, int):
        return 1

    # check output images
    outimg_arr = check_outpref(outpref, inimg_arr)
    if isinstance(outimg_arr, int):
        return 1

    if lsigma <= 0:
        print >> sy.stderr, 'invalid lsigma value (%f)' % lsigma
        return 1

    if hsigma <= 0:
        print >> sys.stderr, 'invalid hsigma value (%f)' % hsigma
        return 1

    # check convolution kernel name
    ret = 1
    param = conv.split()
    if len(param) == 1:
        if os.access(param[0], os.R_OK):
            conv = '@' + conv
            ret = 0
        else:
            print >> sys.stderr, 'convolution kernel file (%s) does not exist' % param[
                0]
    elif param[0] == 'block' and len(param) == 3:
        if param[1].isdigit() and param[2].isdigit():
            ret = 0
    elif param[0] == 'bilinear' and len(param) == 3:
        if param[1].isdigit() and param[2].isdigit() and int(
                param[1]) > 0 and int(param[2]) > 0:
            ret = 0
    elif param[0] == 'gauss' and len(param) == 5:
        if param[1].isdigit() and param[2].isdigit() and int(
                param[1]) > 0 and int(param[2]) > 0:
            if isfloat(param[3]) and isfloat(
                    param[4]) and float(param[3]) > 0 and float(param[4]) > 0:
                ret = 0

    if ret > 0:
        print >> sys.stderr, 'invalid convolve parameter (%s)' % conv
        return 1

    # check bad pixel mask
    if bpm.lower() != 'none':
        bpmex = 1
        if os.access(bpm, os.R_OK) == False:
            print >> sys.stderr, 'cannot read bad pixel mask (%s)' % bpm
            return 1
    else:
        bpmex = 0

    # prefix for temoprary images
    tmp = tempfile.NamedTemporaryFile(suffix='', prefix='', dir='/tmp')
    tmp_prefix = tmp.name
    tmp.close()

    # check sigma map
    if sigmap.lower() == 'none':
        tmp_sigma = ''
        tmp_mask = ''
    else:
        if os.access(sigmap, os.R_OK) == False:
            print >> sys.stderr, 'cannot read sigma map image (%s)' % sigmap
            return 1
        else:
            tmp_mask = tmp_prefix + 'mask' + os.path.basename(sigmap)
            tmp_sigma = tmp_prefix + 'sigma' + os.path.basename(sigmap)
            iraf.imcopy(sigmap, tmp_sigma, verbose='no')
            ret = iraf.imstat(sigmap,
                              nclip=50,
                              field='mode',
                              format='no',
                              Stdout=1)
            sigma_mode = float(ret[0])
            iraf.imreplace(tmp_sigma, sigma_mode, upper=0)
            iraf.imarith(sigmap, '*', -1, tmp_mask, verbose='no')
            iraf.imreplace(tmp_mask, 1, lower=0)
            iraf.imreplace(tmp_mask, 0, upper=0)
            iraf.imarith(tmp_mask, '*', -1, tmp_mask)
            iraf.imarith(tmp_mask, '+', 1, tmp_mask)
    #
    tmp_precombmask = tmp_prefix + 'premask' + os.path.basename(combimg)
    tmp_combmask = tmp_prefix + 'mask' + os.path.basename(combimg)
    iraf.unlearn('objmasks')
    try:
        iraf.objmasks(combimg,
                      tmp_precombmask,
                      omtype='boolean',
                      masks='',
                      convolve=conv,
                      lsigma=lsigma,
                      hsigma=hsigma,
                      hdetect='yes',
                      ldetect='yes',
                      minpix=minpix,
                      sigmas=tmp_sigma)
        iraf.objmasks(combimg,
                      tmp_combmask,
                      omtype='boolean',
                      masks=tmp_precombmask + '[pl]',
                      convolve=conv,
                      lsigma=lsigma,
                      hsigma=hsigma,
                      hdetect='yes',
                      ldetect='yes',
                      minpix=minpix,
                      sigmas=tmp_sigma)
        os.remove(tmp_precombmask)
    except:
        print >> sys.stderr, 'failed to execute iraf objmasks task for %s' % combimg
        remove_temp_all(tmp_prefix)
        return 1

    # remove the area with no sigma value
    tmp_combmask2 = tmp_prefix + 'mask2' + os.path.basename(combimg)
    if sigmap.lower() == 'none':
        iraf.imcopy(tmp_combmask + '[pl]', tmp_combmask2, verbose='no')
    else:
        iraf.imarith(tmp_combmask + '[pl]',
                     '*',
                     tmp_mask,
                     tmp_combmask2,
                     verbose='no')
    os.remove(tmp_combmask)

    # check if input images exist and fix bad pixel if requested
    iraf.unlearn('imcopy')
    iraf.unlearn('imshift')
    for i in range(len(inimg_arr)):

        # get offset values from header
        im = pyfits.open(inimg_arr[i])
        try:
            dx = float(im[0].header['dx'])
            dy = float(im[0].header['dy'])
            dxc = float(im[0].header['dxc'])
            dyc = float(im[0].header['dyc'])
            nx = int(im[0].header['NAXIS1'])
            ny = int(im[0].header['NAXIS2'])
        except:
            print >> sys.stderr, 'failed to get offset values from the header of %s' % inimg_arr[
                i]
            remove_temp_all(tmp_prefix)
            return 1
        im.close()

        # shift combined mask
        tmp_shiftimg = tmp_prefix + os.path.basename(outimg_arr[i])
        iraf.imshift(tmp_combmask2,
                     tmp_shiftimg,
                     dxc,
                     dyc,
                     boundary_typ='constant',
                     constant=0.0)
        tmp_shiftimg_reg = '%s[1:%d,1:%d]' % (tmp_shiftimg, nx, ny)
        #iraf.imcopy(tmp_shiftimg_reg, outimg_arr[i]+'[pl]',verbose='no')
        iraf.imcopy(tmp_shiftimg_reg, outimg_arr[i], verbose='no')
        if bpmex == 1:
            iraf.imarith(outimg_arr[i], '+', bpm, outimg_arr[i], verbose='no')
        convert_maskfits_int(outimg_arr[i], outimg_arr[i])
        os.remove(tmp_shiftimg)

    # remove all temporary files
    remove_temp_all(tmp_prefix)

    return 0
def mkskyflat_with_mask(inlist,
                        output,
                        inpref='',
                        mskpref='m',
                        bpm='none',
                        iternum=50,
                        nsigma=3):

    # load IRAF package
    iraf.proto()
    iraf.nproto()

    # open input list and check if it exists
    inimg_arr = check_input(inlist, inpref)
    if len(inimg_arr) == 1 and inimg_arr[0] == 1:
        return 1

    # check if output image is already exist
    if os.access(output, os.R_OK):
        print >> sys.stderr, 'operation would overwrite existing file (%s)' % output
        return 1

    # check mask images
    if mskpref.lower() == 'none' or mskpref == '':
        print >> sys.stderr, 'mask frames are required for this task'
        return 1
    else:
        mskimg_arr = check_inpref(mskpref, inimg_arr)
        if isinstance(mskimg_arr, int):
            return 1

    # check optional parameters
    if bpm.lower() == 'none' or bpm == '':
        bpmex = 0
    else:
        bpmex = 1
        if not os.access(bpm, os.R_OK):
            print >> sys.stderr, 'cannot read bad pixel mask (%s)' % bpm
            return 1

    if iternum <= 0:
        print >> sys.stderr, 'invalid iternum value (%d)' % iternum
        return 1

    if nsigma <= 0:
        print >> sys.stderr, 'invalid nsigma value (%f)' % nsigma
        return 1

    # prefix for temoprary images
    tmp = tempfile.NamedTemporaryFile(suffix='', prefix='', dir='/tmp')
    tmp_prefix = tmp.name
    tmp.close()

    # nomalize by object masked average
    tmp_list = tmp_prefix + '.lst'
    fout = open(tmp_list, 'w')
    for i in range(len(inimg_arr)):

        im = pyfits.open(inimg_arr[i], mode='update')
        try:
            pattern = im[0].header['PATTERN']
        except KeyError:
            pattern = 0
        im.close()

        if pattern == 0:
            tmpimg = tmp_prefix + os.path.basename(inimg_arr[i])
            maskimg = mskimg_arr[i]
            tmpmask = tmp_prefix + maskimg
            fout.write('%s\n' % tmpimg)

            if os.access(tmpimg, os.R_OK):
                os.remove(tmpimg)

            if os.access(tmpmask, os.R_OK):
                os.remove(tmpmask)

            if bpmex == 1:
                iraf.unlearn('imarith')
                #iraf.imarith(maskimg+'[pl]','+',bpm,tmpmask,verbose='no')
                iraf.imarith(maskimg, '+', bpm, tmpmask, verbose='no')
            else:
                iraf.unlearn('imcopy')
                #iraf.imcopy(maskimg+'[pl]',tmpmask,verbose='no')
                iraf.imcopy(maskimg, tmpmask, verbose='no')

            iraf.unlearn('mimstat')
            res = iraf.mimstat(inimg_arr[i],
                               imasks=tmpmask,
                               format='no',
                               fields='mean',
                               nclip=iternum,
                               lsigma=nsigma,
                               usigma=nsigma,
                               Stdout=1)
            if len(res) == 1:
                ave = float(res[0])
            else:
                print >> sys.stderr, 'failed to calculate masked avarage from the frame (%s)' % inimg_arr[
                    i]
                remove_temp_all(tmp_prefix)
                return 1

            # normalize each frame by average
            iraf.unlearn('imarith')
            iraf.imarith(inimg_arr[i], '/', ave, tmpimg, verbose='no')

            # add mask into header
            iraf.unlearn('hedit')
            if mskpref[0] != ':':
                iraf.hedit(tmpimg, 'objmask', maskimg, add='yes', verify='no')

    # close output file handler
    fout.close()

    # combine
    ret = zcombine(tmp_list,
                   output,
                   combine='median',
                   reject='sigclip',
                   masktype='!objmask')
    if ret != 0:
        print >> sys.stderr, 'failed to combine normalized frames'
        remove_temp_all(tmp_prefix)
        return 1

    # remove temoprary files
    remove_temp_all(tmp_prefix)

    return 0
Exemple #5
0
def makefringingmask(listimg, _output, _interactive, _combine='average', _rejection='avsigclip'):
    # print "LOGX:: Entering `makefringingmask` method/function in
    # %(__file__)s" % globals()
    import ntt
    from ntt.util import readhdr, readkey3, delete, updateheader
    import glob
    import os
    import sys
    import re
    import string
    from pyraf import iraf
    iraf.noao(_doprint=0)
    iraf.immatch(_doprint=0)
    iraf.imutil(_doprint=0)
    iraf.nproto(_doprint=0)
    iraf.proto(_doprint=0)
    toforget = ['nproto.objmasks', 'proto.fixpix']
    for t in toforget:
        iraf.unlearn(t)

    if _interactive == True:
        listimg2 = []
        for img in listimg:
            _exptime = readkey3(readhdr(img), 'exptime')
            if float(_exptime) >= 10:
                answ = 'xxx'
                while answ.lower() not in ['y', 'n', 's', 'a']:
                    iraf.display(img, frame=1, fill='yes')
                    answ = raw_input(
                        'use this image (yes,no,stop (not more images),all) [[y]/n/s/a] ? ')
                    if not answ:
                        answ = 'y'
                    if answ.lower() == 'y':
                        listimg2.append(img)
                    elif answ.lower() == 'a':
                        listimg2 = listimg[:]
                if answ.lower() in ['a', 's']:
                    break
        listimg = listimg2[:]

    iraf.nproto.objmasks1.fitxord = 1
    iraf.nproto.objmasks1.fityord = 1
    hdr0 = readhdr(listimg[0])
    _date = readkey3(hdr0, 'date-obs')
    _filter = readkey3(hdr0, 'filter')
    _exptime = readkey3(hdr0, 'exptime')
    _instrume = readkey3(hdr0, 'instrume')
    _ron = readkey3(hdr0, 'ron')
    _gain = readkey3(hdr0, 'gain')
    badpixelmask = 'bad_pixel_mask.pl'
    if not os.path.isfile(badpixelmask):
        os.system('cp ' + ntt.__path__[0] + '/archive/' + _instrume +
                  '/badpixels/badpixel_20100210.pl ' + badpixelmask)
    ff = open('_listmask', 'w')
    hh = open('_listobgz', 'w')
    for img in listimg:
        _exptime = readkey3(readhdr(img), 'exptime')
        hh.write('z_' + img + '\n')
        ff.write('mask_' + img + '\n')
        delete('mask_' + img)
        aaa = iraf.hedit(img, delete='yes', field='OBJMASK',
                         up='yes', verify='no', Stdout=1)
        aaa = iraf.hedit(img, delete='yes', field='BPM',
                         up='yes', verify='no', Stdout=1)
        delete('z_' + img)
        iraf.imutil.imexpr(expr='(a - median(a))/' + str(_exptime),
                           a=img, output='z_' + img, verbose='no')
        ntt.util.updateheader('z_' + img, 0, {'EXPTIME': [1, '']})
    ff.close()
    hh.close()
    if not _output:
        _output = 'fringing_' + str(_date) + '_' + str(_filter) + '.fits'
    delete(_output)
    print ' making mask for each frame .......'
    ccc = iraf.nproto.objmasks(images='@_listobgz', objmasks='@_listmask', omtype='boolean',
                               blksize=-16, convolv='block 3 3', hsigma=5, lsigma=3, minpix=10, ngrow=2, agrow=4., Stdout=1)
    print 'combining all frames, masking the objects .....'
    iraf.imcombine('@_listobgz', output=_output, masktyp='!OBJMASK', maskval=0, combine=_combine, reject=_rejection,
                   scale='none', statsec='[100:800,100:800]', rdnoise='', gain='', nlow=1, nhigh=1, logfile='imcombinelog')

    ntt.util.phase3header(_output)
    ntt.util.updateheader(
        _output, 0, {'BUNIT': ['ADU', 'pixel units(ADU,electrons)']})
    ntt.util.updateheader(_output, 0, {'FILETYPE': [11231, 'fringing frame']})
    return _output
Exemple #6
0
def makefringing(listimg, _output, _xorder, _yorder, _interactive, combine='average', rejection='avsigclip'):
    # print "LOGX:: Entering `makefringing` method/function in %(__file__)s" %
    # globals()
    import os
    import string
    import re
    import ntt
    from ntt.util import readhdr, readkey3, delete, updateheader
    from pyraf import iraf
    from pyfits import open as popen
    from numpy import median, where, mean
    iraf.noao(_doprint=0)
    iraf.nproto(_doprint=0)
    if _interactive == True:
        listimg2 = []
        for img in listimg:
            _exptime = readkey3(readhdr(img), 'exptime')
            if float(_exptime) >= 100:
                answ = 'xxx'
                while answ.lower() not in ['y', 'n', 's', 'a']:
                    iraf.display(img, frame=1, fill='yes')
                    answ = raw_input(
                        'use this image (yes,no,stop (not more images),all) [[y]/n/s/a] ? ')
                    if not answ:
                        answ = 'y'
                    if answ.lower() == 'y':
                        listimg2.append(img)
                    elif answ.lower() == 'a':
                        listimg2 = listimg[:]
                if answ.lower() in ['a', 's']:
                    break
        listimg = listimg2[:]

    iraf.nproto.objmasks1.fitxord = 1
    iraf.nproto.objmasks1.fityord = 1
    listmask = []
    ff = open('_listmask', 'w')
    gg = open('_listobg', 'w')
    hh = open('_listobgz', 'w')
    for img in listimg:
        _exptime = readkey3(readhdr(img), 'exptime')
        gg.write(img + '\n')
        hh.write('z_' + img + '\n')
        ff.write('mask_' + img + '\n')
        listmask.append('mask_' + img)
        os.system('rm -rf mask_' + img)
        os.system('rm -rf t_' + img)
        os.system('rm -rf z_' + img)

        iraf.imsurfit(img, 't_' + img, xorder=_xorder,
                      yorder=_yorder, type_ou='residual', regions='all')
        iraf.imarith('t_' + img, '/', _exptime, 'z_' + img)
        os.system('rm -rf t_' + img)
    gg.close()
    ff.close()
    hh.close()

    os.system('rm ' + _output)
    iraf.objmasks(images='@_listobgz', objmasks='@_listmask', omtype='boolean',
                  blksize=-16, convolv='block 3 3', hsigma=5, lsigma=3, minpix=10, ngrow=2, agrow=4.)
    iraf.imcombine('@_listobgz', output=_output, masktyp='!OBJMASK', maskval=0, combine=_combine, reject=_rejection,
                   scale='none', statsec='[100:800,100:800]', offsets='', rdnoise='', gain='', nlow=1, nhigh=1, logfile='imcombinelog')

    for img in listimg:
        os.system('rm -rf mask_' + img)
        os.system('rm -rf z_' + img)

    os.system('rm _listmask')
    os.system('rm _listobg')
    iraf.display(_output, frame=2, fill='yes')
Exemple #7
0
def makefringing(listimg,
                 _output,
                 _xorder,
                 _yorder,
                 _interactive,
                 combine='average',
                 rejection='avsigclip'):
    # print "LOGX:: Entering `makefringing` method/function in %(__file__)s" %
    # globals()
    import os
    import string
    import re
    import ntt
    from ntt.util import readhdr, readkey3, delete, updateheader
    from pyraf import iraf
    from pyfits import open as popen
    from numpy import median, where, mean
    iraf.noao(_doprint=0)
    iraf.nproto(_doprint=0)
    if _interactive == True:
        listimg2 = []
        for img in listimg:
            _exptime = readkey3(readhdr(img), 'exptime')
            if float(_exptime) >= 100:
                answ = 'xxx'
                while answ.lower() not in ['y', 'n', 's', 'a']:
                    iraf.display(img, frame=1, fill='yes')
                    answ = raw_input(
                        'use this image (yes,no,stop (not more images),all) [[y]/n/s/a] ? '
                    )
                    if not answ:
                        answ = 'y'
                    if answ.lower() == 'y':
                        listimg2.append(img)
                    elif answ.lower() == 'a':
                        listimg2 = listimg[:]
                if answ.lower() in ['a', 's']:
                    break
        listimg = listimg2[:]

    iraf.nproto.objmasks1.fitxord = 1
    iraf.nproto.objmasks1.fityord = 1
    listmask = []
    ff = open('_listmask', 'w')
    gg = open('_listobg', 'w')
    hh = open('_listobgz', 'w')
    for img in listimg:
        _exptime = readkey3(readhdr(img), 'exptime')
        gg.write(img + '\n')
        hh.write('z_' + img + '\n')
        ff.write('mask_' + img + '\n')
        listmask.append('mask_' + img)
        os.system('rm -rf mask_' + img)
        os.system('rm -rf t_' + img)
        os.system('rm -rf z_' + img)

        iraf.imsurfit(img,
                      't_' + img,
                      xorder=_xorder,
                      yorder=_yorder,
                      type_ou='residual',
                      regions='all')
        iraf.imarith('t_' + img, '/', _exptime, 'z_' + img)
        os.system('rm -rf t_' + img)
    gg.close()
    ff.close()
    hh.close()

    os.system('rm ' + _output)
    iraf.objmasks(images='@_listobgz',
                  objmasks='@_listmask',
                  omtype='boolean',
                  blksize=-16,
                  convolv='block 3 3',
                  hsigma=5,
                  lsigma=3,
                  minpix=10,
                  ngrow=2,
                  agrow=4.)
    iraf.imcombine('@_listobgz',
                   output=_output,
                   masktyp='!OBJMASK',
                   maskval=0,
                   combine=_combine,
                   reject=_rejection,
                   scale='none',
                   statsec='[100:800,100:800]',
                   offsets='',
                   rdnoise='',
                   gain='',
                   nlow=1,
                   nhigh=1,
                   logfile='imcombinelog')

    for img in listimg:
        os.system('rm -rf mask_' + img)
        os.system('rm -rf z_' + img)

    os.system('rm _listmask')
    os.system('rm _listobg')
    iraf.display(_output, frame=2, fill='yes')
Exemple #8
0
def makefringingmask(listimg,
                     _output,
                     _interactive,
                     _combine='average',
                     _rejection='avsigclip'):
    # print "LOGX:: Entering `makefringingmask` method/function in
    # %(__file__)s" % globals()
    import ntt
    from ntt.util import readhdr, readkey3, delete, updateheader
    import glob
    import os
    import sys
    import re
    import string
    from pyraf import iraf
    iraf.noao(_doprint=0)
    iraf.immatch(_doprint=0)
    iraf.imutil(_doprint=0)
    iraf.nproto(_doprint=0)
    iraf.proto(_doprint=0)
    toforget = ['nproto.objmasks', 'proto.fixpix']
    for t in toforget:
        iraf.unlearn(t)

    if _interactive == True:
        listimg2 = []
        for img in listimg:
            _exptime = readkey3(readhdr(img), 'exptime')
            if float(_exptime) >= 10:
                answ = 'xxx'
                while answ.lower() not in ['y', 'n', 's', 'a']:
                    iraf.display(img, frame=1, fill='yes')
                    answ = raw_input(
                        'use this image (yes,no,stop (not more images),all) [[y]/n/s/a] ? '
                    )
                    if not answ:
                        answ = 'y'
                    if answ.lower() == 'y':
                        listimg2.append(img)
                    elif answ.lower() == 'a':
                        listimg2 = listimg[:]
                if answ.lower() in ['a', 's']:
                    break
        listimg = listimg2[:]

    iraf.nproto.objmasks1.fitxord = 1
    iraf.nproto.objmasks1.fityord = 1
    hdr0 = readhdr(listimg[0])
    _date = readkey3(hdr0, 'date-obs')
    _filter = readkey3(hdr0, 'filter')
    _exptime = readkey3(hdr0, 'exptime')
    _instrume = readkey3(hdr0, 'instrume')
    _ron = readkey3(hdr0, 'ron')
    _gain = readkey3(hdr0, 'gain')
    badpixelmask = 'bad_pixel_mask.pl'
    if not os.path.isfile(badpixelmask):
        os.system('cp ' + ntt.__path__[0] + '/archive/' + _instrume +
                  '/badpixels/badpixel_20100210.pl ' + badpixelmask)
    ff = open('_listmask', 'w')
    hh = open('_listobgz', 'w')
    for img in listimg:
        _exptime = readkey3(readhdr(img), 'exptime')
        hh.write('z_' + img + '\n')
        ff.write('mask_' + img + '\n')
        delete('mask_' + img)
        aaa = iraf.hedit(img,
                         delete='yes',
                         field='OBJMASK',
                         up='yes',
                         verify='no',
                         Stdout=1)
        aaa = iraf.hedit(img,
                         delete='yes',
                         field='BPM',
                         up='yes',
                         verify='no',
                         Stdout=1)
        delete('z_' + img)
        iraf.imutil.imexpr(expr='(a - median(a))/' + str(_exptime),
                           a=img,
                           output='z_' + img,
                           verbose='no')
        ntt.util.updateheader('z_' + img, 0, {'EXPTIME': [1, '']})
    ff.close()
    hh.close()
    if not _output:
        _output = 'fringing_' + str(_date) + '_' + str(_filter) + '.fits'
    delete(_output)
    print ' making mask for each frame .......'
    ccc = iraf.nproto.objmasks(images='@_listobgz',
                               objmasks='@_listmask',
                               omtype='boolean',
                               blksize=-16,
                               convolv='block 3 3',
                               hsigma=5,
                               lsigma=3,
                               minpix=10,
                               ngrow=2,
                               agrow=4.,
                               Stdout=1)
    print 'combining all frames, masking the objects .....'
    iraf.imcombine('@_listobgz',
                   output=_output,
                   masktyp='!OBJMASK',
                   maskval=0,
                   combine=_combine,
                   reject=_rejection,
                   scale='none',
                   statsec='[100:800,100:800]',
                   rdnoise='',
                   gain='',
                   nlow=1,
                   nhigh=1,
                   logfile='imcombinelog')

    ntt.util.phase3header(_output)
    ntt.util.updateheader(_output, 0,
                          {'BUNIT': ['ADU', 'pixel units(ADU,electrons)']})
    ntt.util.updateheader(_output, 0, {'FILETYPE': [11231, 'fringing frame']})
    return _output
Exemple #9
0
def mkskyflat(inlist, output, inpref='', mskpref='m', bpm='none', iternum=50, nsigma=3, minpix=250, hsigma=3, lsigma=10, conv='block 3 3'):
    
    # load IRAF package
    iraf.proto()
    iraf.nproto()

    # open input list and check if it exists
    inimg_arr = check_input(inlist, inpref)
    if len(inimg_arr) == 1 and inimg_arr[0] == 1:
        return 1

    # check if output image is already exist
    if os.access(output, os.R_OK) : 
        print >> sys.stderr, 'operation would overwrite existing file (%s)' % output
        return 1

    # check optional parameters 
    if bpm.lower() == 'none' or bpm == '':
        bpmex = 0
    else:
        bpmex = 1
        if not os.access(bpm, os.R_OK):
            print >> sys.stderr, 'cannot read bad pixel mask (%s)' % bpm
            return 1

    if iternum <= 0:
        print >> sys.stderr, 'invalid iternum value (%d)' % iternum
        return 1

    if nsigma <= 0:
        print >> sys.stderr, 'invalid nsigma value (%f)' % nsigma
        return 1

    # prefix for temoprary images
    tmp = tempfile.NamedTemporaryFile(suffix='', prefix='', dir='/tmp')
    tmp_prefix = tmp.name
    tmp.close()
    
    # temporary normalize raw frames by average
    tmp_list = tmp_prefix+'.lst'
    fout = open(tmp_list, 'w')
    for i in range(len(inimg_arr)):

        tmpimg = tmp_prefix+os.path.basename(inimg_arr[i])
        fout.write('%s\n' % tmpimg)

        # remove temporary files if it exists
        if os.access(tmpimg, os.R_OK):
            os.remove(tmpimg)

        # calculate average counts in each frame
        if bpmex == 0:
            iraf.unlearn('imstat')
            res = iraf.imstat(inimg_arr[i], format='no', fields='mean', nclip=iternum, lsigma=nsigma, usigma=nsigma, Stdout=1)
            if len(res) == 1:
                ave = float(res[0])
            else:
                print >> sys.stderr, 'failed to calculate avarage from the frame (%s)' % inimg
                remove_temp_all(tmp_prefix)
                fout.close()
                return 1
        else:
            iraf.unlearn('mimstat')
            res = iraf.mimstat(inimg_arr[i], imasks=bpm, format='no', fields='mean', nclip=iternum, lsigma=nsigma, usigma=nsigma, Stdout=1)
            if len(res) == 1:
                ave = float(res[0])
            else:
                print >> sys.stderr, 'failed to calculate masked avarage from the frame (%s)'% inimg
                remove_temp_all(tmp_prefix)
                fout.close()
                return 1

        # normalize each frame by average
        iraf.unlearn('imarith')
        iraf.imarith(inimg_arr[i], '/', ave, tmpimg, verbose='no')

    # close output file handler 
    fout.close()

    # make temporary sky flat frame
    tmp_flat = tmp_prefix+'_sflat.fits'
    ret = zcombine(tmp_list, tmp_flat, combine='median', reject='sigclip')
    if ret != 0:
        remove_temp_all(tmp_prefix)
        return 1

    # temporary flat field 
    iraf.unlearn('imarith')
    tmpff_list = tmp_prefix+'_ff.lst'
    fout = open(tmpff_list, 'w')
    for i in range(len(inimg_arr)):

        tmpimg = tmp_prefix+'_ff_'+os.path.basename(inimg_arr[i])
        fout.write('%s\n' % tmpimg)

        # remove temporary files if it exists
        if os.access(tmpimg, os.R_OK):
            os.remove(tmpimg)

        # divided by temporary flat 
        iraf.imarith(inimg_arr[i], '/', tmp_flat, tmpimg, verbose='no')

        # read image fits header
        im = pyfits.open(inimg_arr[i])
        exptime = float(im[0].header['EXP1TIME'])
        coadds = int(im[0].header['COADDS'])
        ndr = int(im[0].header['NDR'])
        im.close()

        # normalize by coadds, ndr, and exptime values
        iraf.imarith(tmpimg, '/', (coadds*ndr*exptime), tmpimg, verbose='no')


    # close output file handler 
    fout.close()

    # remove temporary flat
    os.remove(tmp_flat)

    # calculate standard deviation of the temporary flat divided images
    try:
        iraf.unlearn('imstat')
        stddev = iraf.imstat('@'+tmpff_list, format='no', fields='stddev', nclip=50, Stdout=1)
    except:
        remove_temp_all(tmp_prefix)
        return 1

    # calculate mean and sigma of the stddev of the image
    y = map(float, stddev)
    mean, sigma = meanclip(np.array(y, dtype=float))
    #print mean, sigma 

    # flag bad frames
    frame_flag = []
    for i in range(len(stddev)):
        im = pyfits.open(inimg_arr[i], mode='update')                
        if math.fabs(y[i] - mean) < nsigma * sigma:
            frame_flag.append(0)
            im[0].header['PATTERN'] = 0
        else:
            frame_flag.append(1)
            im[0].header['PATTERN'] = 1
        im.close()

    # make object mask 
    ret = mkobjmask(tmpff_list, inpref='', outpref=mskpref, bpm=bpm, minpix=minpix, hsigma=hsigma, lsigma=lsigma, conv=conv)

    # rename mask file 
    mskimg_arr = []
    for i in range(len(inimg_arr)):
        base = os.path.basename(inimg_arr[i])
        tmpimg = tmp_prefix+'_ff_'+base
        maskimg = mskpref+base
        tmpmask = mskpref+os.path.basename(tmpimg)
        move(tmpmask, maskimg)
        mskimg_arr.append(maskimg)

    # delete temporary flat fielded files 
    remove_temp_all(tmp_prefix)
    
    # check mkobjmask result 
    if ret != 0:
        return 1

    # nomalize by object masked average 
    fout = open(tmp_list, 'w')
    for i in range(len(inimg_arr)):

        if frame_flag[i] == 0:
        
            tmpimg = tmp_prefix+os.path.basename(inimg_arr[i])
            maskimg = mskimg_arr[i]
            tmpmask = tmp_prefix+maskimg
            fout.write('%s\n' % tmpimg)

            if os.access(tmpimg, os.R_OK):
                os.remove(tmpimg)
                
            if os.access(tmpmask, os.R_OK):
                os.remove(tmpmask)

            if bpmex == 1:
                iraf.unlearn('imarith')
                iraf.imarith(maskimg+'[pl]','+',bpm,tmpmask,verbose='no')
            else :
                iraf.unlearn('imcopy')
                iraf.imcopy(maskimg+'[pl]',tmpmask,verbose='no')

            iraf.unlearn('mimstat')
        
            # calculate average of the raw images with mask
            res = iraf.mimstat(inimg_arr[i], imasks=tmpmask, format='no', fields='mean', nclip=iternum, lsigma=nsigma, usigma=nsigma, Stdout=1)
            if len(res) == 1:
                ave = float(res[0])
            else:
                print >> sys.stderr, 'failed to calculate masked avarage from the frame (%s)' % inimg_arr[i]
                remove_temp_all(tmp_prefix)
                return 1
        
            # normalize each frame by average
            iraf.unlearn('imarith')
            iraf.imarith(inimg_arr[i], '/', ave, tmpimg, verbose='no')
            
            # add mask into header 
            iraf.unlearn('hedit')
            iraf.hedit(tmpimg, 'objmask', maskimg+'[pl]', add='yes', verify='no')

    # close output file handler 
    fout.close()
        
    # combine
    ret = zcombine(tmp_list, output, combine='median', reject='sigclip', masktype='!objmask')
    if ret != 0:
        print >> sys.stderr, 'failed to combine normalized frames'
        remove_temp_all(tmp_prefix)
        return 1   

    # remove temoprary files
    remove_temp_all(tmp_prefix)

    return 0
Exemple #10
0
def fringing2(img, fmask, _interactive, _verbose=False):
    # print "LOGX:: Entering `fringing2` method/function in %(__file__)s" %
    # globals()
    from ntt.util import delete, display_image, updateheader, readhdr, readkey3
    from ntt.efoscphotredudef import searchfringe
    import datetime

    MJDtoday = 55927 + (datetime.date.today() - datetime.date(2012, 01, 01)).days
    import ntt
    import os
    import string
    import re
    from numpy import median, where
    from pyraf import iraf

    iraf.nproto(_doprint=0)
    iraf.unlearn('nproto.objmasks')
    if _verbose:
        ver = 'yes'
    else:
        ver = 'no'
    hdr = readhdr(img)
    _filter = readkey3(hdr, 'filter')
    _exptime = readkey3(hdr, 'exptime')
    _date = readkey3(hdr, 'date-night')
    imgout = img
    maskname = ''
    if not readkey3(hdr, 'FRICOR'):
        if fmask:
            fmask = searchfringe(img, fmask)[0]
        if fmask:
            print '###### use fringing mask from user ' + fmask
        else:
            fmask = searchfringe(img, '')[0]
            if fmask:
                print '###### use fringing mask from archive ' + fmask
        if fmask:
            imgout = re.sub('.fits', '_fr.fits', img)
            _trim = readkey3(hdr, 'TRIM')
            _trimmask = readkey3(readhdr(fmask), 'TRIM')
            maskname = 'fmask_' + str(_date) + '_' + \
                _filter + '_' + str(MJDtoday) + '.fits'
            delete(maskname)
            if _trim and not _trimmask:
                _trim = '[' + string.split(_trim, '[')[1]
                iraf.ccdred.ccdproc(fmask, output=maskname, overscan="no", trim="yes", zerocor="no",
                                    trimsec=_trim, flatcor="no", zero="", Stdout=1)
            elif _trim and _trimmask:
                if _trim == _trimmask:
                    os.system('cp ' + str(fmask) + ' ' + str(maskname))
                else:
                    sys.exit('ERROR: fringing correction can be applied only to UNTRIMMED images or '
                             'images with this trim ' + str(_trimmask))
            elif not _trim and _trimmask:
                sys.exit(
                    'ERROR: image is not trimmed while selected fringing mask is trimmed: ' + str(_trimmask))
            else:
                os.system('cp ' + str(fmask) + ' ' + str(maskname))

            iraf.nproto.objmasks1.fitxord = 1
            iraf.nproto.objmasks1.fityord = 1
            delete('mask_' + img)
            imgcut = re.sub('.fits', '', img)
            xxx = iraf.nproto.objmasks(images=imgcut, objmasks='mask_' + img, omtype='boolean',
                                       blksize=-16, convolv='block 3 3', hsigma=5, lsigma=3, minpix=10, ngrow=2,
                                       agrow=4., Stdout=1)
            matimg = pyfits.open(img)[0].data
            matmask = pyfits.open('mask_' + img)[1].data
            matfrin = pyfits.open(maskname)[0].data
            indices = where(matmask < 1)
            scalevalue = median(
                (matimg[indices] - median(matimg[indices])) / (matfrin[indices] - median(matfrin)))
            delete(imgout)
            iraf.imutil.imexpr(expr='a - (' + str(scalevalue) + '* (b - ' + str(median(matfrin)) + ') )', a=img,
                               b=maskname, output=imgout, verbose='no')
            print '\n### fringing correction  ..... done'
            iraf.hedit(images=imgout, fields='OBJMASK', value='',
                       delete='yes', update='yes', verify='no')
            ntt.util.updateheader(imgout, 0, {
                'FRICOR': [str(scalevalue) + ' * (' + str(maskname) + ' - ' + str(median(matfrin)) + ')', '']})
            ntt.util.updateheader(
                imgout, 0, {'FILETYPE': [12205, 'pre-reduced image fringing corrected']})
            ntt.util.updateheader(imgout, 0, {'PROV1': [readkey3(
                readhdr(imgout), 'ARCFILE'), 'Originating file']})
            ntt.util.updateheader(
                imgout, 0, {'TRACE1': [img, 'Originating file']})
            stringa = '%7.7s  * (fmask_%8s.fits - %5.5s )' % (
                str(scalevalue), _date, str(median(matfrin)))
        else:
            print '\n### fringing mask not available for this filter'
    else:
        print '\n### fringing correction already applyed to this image'
    return imgout, maskname
Exemple #11
0
def mkskyflat_quick(inlist,
                    output,
                    inpref='',
                    bpm='none',
                    iternum=50,
                    nsigma=3):

    # load IRAF package
    iraf.proto()
    iraf.nproto()

    # open input list and check if it exists
    inimg_arr = check_input(inlist, inpref)
    if len(inimg_arr) == 1 and inimg_arr[0] == 1:
        return 1

    # check if output image is already exist
    if os.access(output, os.R_OK):
        print >> sys.stderr, 'operation would overwrite existing file (%s)' % output
        return 1

    # check optional parameters
    if bpm.lower() == 'none' or bpm == '':
        bpmex = 0
    else:
        bpmex = 1
        if not os.access(bpm, os.R_OK):
            print >> sys.stderr, 'cannot read bad pixel mask (%s)' % bpm
            return 1

    if iternum <= 0:
        print >> sys.stderr, 'invalid iternum value (%d)' % iternum
        return 1

    if nsigma <= 0:
        print >> sys.stderr, 'invalid nsigma value (%f)' % nsigma
        return 1

    # prefix for temoprary images
    tmp = tempfile.NamedTemporaryFile(suffix='', prefix='', dir='/tmp')
    tmp_prefix = tmp.name
    tmp.close()

    # temporary normalize raw frames by average
    tmp_list = tmp_prefix + '.lst'
    fout = open(tmp_list, 'w')
    for i in range(len(inimg_arr)):

        tmpimg = tmp_prefix + os.path.basename(inimg_arr[i])
        fout.write('%s\n' % tmpimg)

        # remove temporary files if it exists
        if os.access(tmpimg, os.R_OK):
            os.remove(tmpimg)

        # calculate average counts in each frame
        if bpmex == 0:
            iraf.unlearn('imstat')
            res = iraf.imstat(inimg_arr[i],
                              format='no',
                              fields='mean',
                              nclip=iternum,
                              lsigma=nsigma,
                              usigma=nsigma,
                              Stdout=1)
            if len(res) == 1:
                ave = float(res[0])
            else:
                print >> sys.stderr, 'failed to calculate avarage from the frame (%s)' % inimg
                remove_temp_all(tmp_prefix)
                fout.close()
                return 1
        else:
            iraf.unlearn('mimstat')
            res = iraf.mimstat(inimg_arr[i],
                               imasks=bpm,
                               format='no',
                               fields='mean',
                               nclip=iternum,
                               lsigma=nsigma,
                               usigma=nsigma,
                               Stdout=1)
            if len(res) == 1:
                ave = float(res[0])
            else:
                print >> sys.stderr, 'failed to calculate masked avarage from the frame (%s)' % inimg
                remove_temp_all(tmp_prefix)
                fout.close()
                return 1

        # normalize each frame by average
        iraf.unlearn('imarith')
        iraf.imarith(inimg_arr[i], '/', ave, tmpimg, verbose='no')

    # close output file handler
    fout.close()

    # make temporary sky flat frame
    tmp_flat = tmp_prefix + '_sflat.fits'
    ret = zcombine(tmp_list, output, combine='median', reject='sigclip')
    if ret != 0:
        remove_temp_all(tmp_prefix)
        return 1

    # remove temoprary files
    remove_temp_all(tmp_prefix)

    return 0