Esempio n. 1
0
def Geoxy(path):
    print "Starting Geoxy..."
    filename = path
    os.chdir(filename)   
    corrected = glob.glob("*.xy")
                   
    #Removing existing files    
    filelist = glob.glob("*.done")
    for f in filelist:
        os.remove(f)
    
    #Transforming coordinates
    print "Saving files with the new coordinates."
    for i in corrected:
        iraf.images(_doprint=0)
        iraf.immatch(_doprint=0)
        iraf.geoxy(input=i, output=i+'.done', database='data3', transforms='trans3')     
Esempio n. 2
0
def shift_align(filenames):
    if len(filenames) == 0:
        return ''
    fno = 1
    for i in filenames:
        iraf.display(image = i, frame = fno)
        fno += 1
    print 'The fits number is', len(filenames)
    if len(filenames) > 1:
        print 'Please check the fits pic and determine whether you need to shift the pic'
        print 'imexamine...'
        iraf.imexamine()
        valget = raw_input('Are you need to shift the fits picture?(y)')
        if valget == 'y' or valget == 'Y':
            nfilenames = [filenames[0]]
            for i2 in filenames:
                print 'implot ' + i2
                iraf.implot(image = i2)
            fno = 1
            iraf.immatch()
            while fno < len(filenames):
                valget = raw_input('Please input ' + filenames[fno] + ' xshift, yshift: ')
                valget = valget.split()
                fxshift = float(valget[0])
                fyshift = float(valget[1])
                ims_key = 1
                if os.path.isfile('shift_' + filenames[fno]):
                    valget = raw_input('shift_' + filenames[fno] + ' is already exist, are you want to delete the old one?(y):')
                    if valget == 'y' or valget == 'Y' or valget == 'yes' or valget == 'Yes':
                        os.remove('shift_' + filenames[fno])
                    else:
                        ims_key = 0
                if ims_key == 1:
                    print 'what happend????????????????'
                    iraf.imshift(input = filenames[fno]
                            , output = 'shift_' + filenames[fno], xshift = fxshift
                            , yshift = fyshift, shifts_file = '', interp_type = 'linear'
                            , boundary_type = 'nearest', constant = 0.0)
                nfilenames.append('shift_' + filenames[fno])
                fno += 1
            for i2 in xrange(len(nfilenames)):
                iraf.display(image = nfilenames[i2], frame = i2 + 1)
            iraf.imexamine()
            return cos_clear(nfilenames)
    return cos_clear(filenames)
Esempio n. 3
0
def shift_master(ref, master):
    """
    Find the shift between two images.
    returns the shift in y direction (along columns)
    """
    #make a narow copy of the reference flat and masterflat 
    iraf.imcopy(input="../"+ref+"[1990:2010,*]", output="tmp/masterflat_ref.fitscut",Stdout="/dev/null")
    iraf.imcopy(input="tmp/masterflat.fits[1990:2010,*]", output="tmp/masterflat.fitscut",Stdout="/dev/null")

    #find the shift with correlation
    iraf.images(_doprint=0,Stdout="/dev/null")
    iraf.immatch(_doprint=0,Stdout="/dev/null")

    shift=iraf.xregister(input="tmp/masterflat_ref.fitscut, tmp/masterflat.fitscut", reference="tmp/masterflat_ref.fitscut", regions='[*,*]', shifts="tmp/mastershift", output="", databasefmt="no", correlation="fourier", xwindow=3, ywindow=51, xcbox=21, ycbox=21, Stdout=1)

    shift=float(shift[-2].split()[-2])

    #Shift the list of apertures
    f=open(master, "r")
    o=open("database/aptmp_masterflat_s", "w")

    for line in f:
        l=line.split()
        if len(l)>0 and l[0]=='begin':
            l[-1]=str(float(l[-1])-shift)
            o.write(l[0]+"\t"+l[1]+"\t"+"tmp/masterflat_s"+"\t"+l[3]+"\t"+l[4]+"\t"+l[5]+"\n")
        elif len(l)>0 and l[0]=='center':
            l[-1]=str(float(l[-1])-shift)
            o.write("\t"+l[0]+"\t"+l[1]+"\t"+l[2]+"\n")
        elif len(l)>0 and l[0]=='image':
            o.write("\t"+"image"+"\t"+"tmp/masterflat_s"+"\n")
        else:
            o.write(line)

    f.close()
    o.close()
Esempio n. 4
0
import pyraf
from pyraf import iraf
import copy, os, shutil, glob, sys, string, re, math, operator, time
import pyfits
from types import *
from mx.DateTime import *

from iqpkg import *
import ephem

# Necessary packages
iraf.images()
iraf.immatch()
iraf.imfilter()
iraf.noao()
iraf.imred()
iraf.ccdred()
iraf.digiphot()
iraf.apphot()

yes=iraf.yes
no=iraf.no
INDEF=iraf.INDEF
hedit=iraf.hedit
imgets=iraf.imgets
imcombine=iraf.imcombine

pyrafdir="python/pyraf/"
pyrafdir_key='PYRAFPARS'

if os.environ.has_key(pyrafdir_key):
Esempio n. 5
0
import Numeric as N
#import scipy
from math import *
from pyraf import iraf
import pyfits
import mystuff as my
#import ppgplot
#pisces noise stats
READNOISE = 4.
GAIN = 4.35

prefix = ['D17s', 'EGSdJ', 'EGSdn', 'EGSsn', 'standA']

iraf.images()
iraf.images.imutil()
iraf.immatch()

for name in prefix:
    i = 0
    s = name + "*.fits"
    files = glob.glob(s)
    for file in files:
        (pre, junk) = file.split('.')
        index = pre[(len(pre) - 4):len(pre)]

        iraf.imgets(image=file, param='FILTER')  #get RA of image
        filter = iraf.imgets.value
        if my.beginsWith('D17s', file) > .1:
            outname = 'D17s' + str(
                file[7]) + "-" + str(filter) + "-" + str(index) + ".fits"
            s = "cp " + file + " " + outname + " \n"
import numpy as np
import matplotlib.pyplot as plt
from datetime import date
import operator
import time
import traceback
from lmfit import minimize, Parameters, report_fit

#import cosmics

iraf.noao(_doprint=0,Stdout="/dev/null")
iraf.rv(_doprint=0,Stdout="/dev/null")
iraf.imred(_doprint=0,Stdout="/dev/null")
iraf.ccdred(_doprint=0,Stdout="/dev/null")
iraf.images(_doprint=0,Stdout="/dev/null")
iraf.immatch(_doprint=0,Stdout="/dev/null")
iraf.onedspec(_doprint=0,Stdout="/dev/null")
iraf.twodspec(_doprint=0,Stdout="/dev/null")
iraf.apextract(_doprint=0,Stdout="/dev/null")
iraf.imutil(_doprint=0,Stdout="/dev/null")
iraf.echelle(_doprint=0,Stdout="/dev/null")
iraf.astutil(_doprint=0,Stdout="/dev/null")
iraf.apextract.dispaxi=1
iraf.echelle.dispaxi=1
#fixes a bug with latest versions of iraf
iraf.ccdred.instrum='blank.txt'


os.environ['PYRAF_BETA_STATUS'] = '1'

# REFS NEW: ecEC59550 ecEC59758 ecEC59795 ecEC59842 ecEC59844 ecEC59864 ecEC59866 ecEC59881 ecEC59883 ecEC59885
Esempio n. 7
0
def combineARCSpectra(args):

    import sys
    import glob
    import os
    from pyraf import iraf
    from shutil import copyfile
    from operator import itemgetter
    import pandas as pd

    # Setup relavent IRAF tasks...
    iraf.image()
    iraf.imutil()
    iraf.immatch()
    iraf.onedspec()

    # Grab list of file names from argument list
    #  and convert it into a python list...
    #fnameList = 'WDC0442-0536.0086b.ms.fits,WDC0442-0536.0087b.ms.fits,WDC0442-0536.0088b.ms.fits'
    fnameList = args.fileList
    fnameList = fnameList.split(',')

    # Clean up file name list to exclude any excess blank spaces...
    fileNameList = []
    for fname in fnameList:
        fileName = fname.strip()
        fileNameList.append(fileName)

    # Start a new file name list, for the imsliced
    #  files to be imcombined...
    newFileNameList = []

    # Loop through the input file name list...
    for fileName in fileNameList:

        print fileName

        # Create an output file name...
        baseFileName = os.path.splitext(fileName)[0] + '.tmp'
        outputFileName = baseFileName + '.fits'

        # Delete the any temporary files called tempA00?.fits,
        #  and then run imslice on fileName...
        iraf.imdel('tempA00?.fits')
        iraf.imslice(fileName, 'tempA', 2)

        # Delete the any temporary files called tempB00?.fits,
        #  and then run imslice on tempA001.fits (an output
        #  from the first imslice)...
        iraf.imdel('tempB00?.fits')
        iraf.imslice('tempA001.fits', 'tempB', 2)

        # Rename the output file tempB001.fits from the second
        #  imslice procedure; tempB001.fits contains the 1D
        #  version of the target spectrum. (The other file,
        #  tempB002.fits, which we ignore, contains the 1D
        #  version of the sky background spectrum.)
        os.rename('tempB001.fits', outputFileName)

        # Append the output file name to the new file name list...
        newFileNameList.append(outputFileName)

        # Clean up unneeded temporary files...
        iraf.imdel('tempA00?.fits')
        iraf.imdel('tempB00?.fits')

    # Print the list of file names that will be fed to imcombine...
    print newFileNameList

    # Here, we convert the python list into an IRAF list...
    inlist = ''
    for newFileName in newFileNameList:
        inlist = inlist + ',' + newFileName
    inlist = inlist[1:]
    print inlist

    # Run imcombine to create a median-combined spectrum
    #  and its sigma image...
    medianFile = newFileNameList[0].split('.')[0] + '.median.b.ms.fits'
    sigmaFile = 'sigma-' + newFileNameList[0].split(
        '.')[0] + '.median.b.ms.fits'
    iraf.imdel(medianFile)
    iraf.imdel(sigmaFile)
    iraf.imcombine(inlist,
                   medianFile,
                   combine='median',
                   scale='median',
                   sigma=sigmaFile)

    # Create ASCII text file equivalents...
    medianFileTxt = newFileNameList[0].split('.')[0] + '.median.b.ms.txt'
    sigmaFileTxt = 'sigma-' + newFileNameList[0].split(
        '.')[0] + '.median.b.ms.txt'
    iraf.onedspec.wspectext(medianFile, medianFileTxt)
    iraf.onedspec.wspectext(sigmaFile, sigmaFileTxt)

    # Combine median and sigma text files...
    df1 = pd.read_csv(medianFileTxt,
                      header=None,
                      names=['wave', 'flux'],
                      delim_whitespace=True)
    df2 = pd.read_csv(sigmaFileTxt,
                      header=None,
                      names=['wave', 'flux_err'],
                      delim_whitespace=True)
    df12 = df1.merge(df2, on='wave')
    outputFile = newFileNameList[0].split('.')[0] + '.median.flm'
    df12.to_csv(outputFile, index=False, sep=' ')

    # Run imcombine to create a mean-combined spectrum
    #  and its sigma image...
    meanFile = newFileNameList[0].split('.')[0] + '.mean.b.ms.fits'
    sigmaFile = 'sigma-' + newFileNameList[0].split('.')[0] + '.mean.b.ms.fits'
    iraf.imdel(meanFile)
    iraf.imdel(sigmaFile)
    iraf.imcombine(inlist,
                   meanFile,
                   combine='average',
                   scale='median',
                   sigma=sigmaFile)

    # Create ASCII text file equivalents...
    meanFileTxt = newFileNameList[0].split('.')[0] + '.mean.b.ms.txt'
    sigmaFileTxt = 'sigma-' + newFileNameList[0].split(
        '.')[0] + '.mean.b.ms.txt'
    iraf.onedspec.wspectext(meanFile, meanFileTxt)
    iraf.onedspec.wspectext(sigmaFile, sigmaFileTxt)

    # Combine mean and sigma text files...
    df1 = pd.read_csv(meanFileTxt,
                      header=None,
                      names=['wave', 'flux'],
                      delim_whitespace=True)
    df2 = pd.read_csv(sigmaFileTxt,
                      header=None,
                      names=['wave', 'flux_err'],
                      delim_whitespace=True)
    df12 = df1.merge(df2, on='wave')
    outputFile = newFileNameList[0].split('.')[0] + '.mean.flm'
    df12.to_csv(outputFile, index=False, sep=' ')

    return 0
Esempio n. 8
0
def image_combine(targetdir):

    """
    Combine all images. Need to run image_shift before.

    """
    print 'Target directory is ' + targetdir
    print 'Combining images...'


    innames = []

    for n in os.listdir(targetdir):
        if (n.endswith('.fit')) & (n.startswith('sdfcr')):
            innames.append(os.path.join(targetdir,n) )


    with open(os.path.join(targetdir,'input.list'), 'w') as f:
        for name in innames:
            print name.replace(targetdir + '/','')
            f.write( name + '\n' )

    if os.path.exists( os.path.join(targetdir,'imcomb.fit')):
        os.remove(os.path.join(targetdir,'imcomb.fit'))
        print 'Removing file ' + os.path.join(targetdir,'imcomb.fit')

    iraf.images(_doprint=0)
    iraf.immatch(_doprint=0)

    iraf.imcombine.setParam('input','@' + os.path.join(targetdir, 'input.list'))
    iraf.imcombine.setParam('output',os.path.join(targetdir,'imcomb.fit') )
    iraf.imcombine.setParam('combine','average')
    iraf.imcombine.setParam('reject','sigclip')
    iraf.imcombine.setParam('lsigma',3.0)
    iraf.imcombine.setParam('hsigma',3.0)

    iraf.imcombine()

    # Or, combine without background subtraction


    if os.path.exists( os.path.join(targetdir,'imcomb+bkgd.fit')):
        os.remove( os.path.join(targetdir, 'imcomb+bkgd.fit') )
        print 'Removing file ' + os.path.join(targetdir, 'imcomb+bkgd.fit')

    hdulist = fits.open( os.path.join( targetdir, 'imcomb.fit'))
    data = hdulist[0].data
    hdulist.close()

    # import matplotlib.pyplot as plt
    # from spectra.range_from_zscale import range_from_zscale
    # fig, ax = plt.subplots()
    # z1, z2, iteration = range_from_zscale(data)
    # ax.imshow(data,cmap='gray',vmin=z1,vmax=z2)
    # plt.gca().invert_yaxis()

    data = np.concatenate( ( data[32:345,:], data[520:650,:], data[740:940,:]))

    sigmaspec = np.std( data, axis = 0)

    # fig, ax = plt.subplots()
    # ax.plot( sigmaspec, lw=1 )

    # Convert sky from adu to electron
    skyspec = (4 * sigmaspec)**2
    sky_adu = skyspec / 4.0

    # add to each pixel

    hdulist = fits.open( os.path.join( targetdir, 'imcomb.fit'))
    data = hdulist[0].data
    hdulist.close()

    sky_adu = np.repeat(sky_adu, np.shape(data)[0] ).reshape( np.shape(data)[::-1] ).T

    data = data + sky_adu

    hdu = fits.PrimaryHDU(data)
    hdu.writeto( os.path.join( targetdir, 'imcomb+bkgd.fit') )


    return None
Esempio n. 9
0
# ------------------------------------------------------------------------------------------------------------------- #
# Telescope CCD Specifications
# ------------------------------------------------------------------------------------------------------------------- #
read_noise = 4.87
ccd_gain = 1.22
ccd_xsize = 2048
ccd_ysize = 2048
# ------------------------------------------------------------------------------------------------------------------- #

# ------------------------------------------------------------------------------------------------------------------- #
# Load Required IRAF Packages
# ------------------------------------------------------------------------------------------------------------------- #
iraf.noao(_doprint=0)
iraf.images(_doprint=0)
iraf.immatch(_doprint=0)
# ------------------------------------------------------------------------------------------------------------------- #

# ------------------------------------------------------------------------------------------------------------------- #
# Functions For File Handling
# ------------------------------------------------------------------------------------------------------------------- #


def remove_file(file_name):
    """
    Removes the file "file_name" in the constituent directory.
    Args:
         file_name  : Name of the file to be removed from the current directory
    Returns:
        None
    """
Esempio n. 10
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
Esempio n. 11
0
def sofispec1Dredu(files, _interactive, _ext_trace, _dispersionline, _automaticex, _verbose=False):
    # print "LOGX:: Entering `sofispec1Dredu` method/function in %(__file__)s"
    # % globals()
    import re
    import string
    import sys
    import os
    os.environ["PYRAF_BETA_STATUS"] = "1"
    import ntt
    try:     import pyfits
    except:  from astropy.io import fits as pyfits

    import numpy as np
    import datetime
    import pylab as pl
    from pyraf import iraf

    dv = ntt.dvex()
    now = datetime.datetime.now()
    datenow = now.strftime('20%y%m%d%H%M')
    MJDtoday = 55927 + (datetime.date.today() - datetime.date(2012, 01, 01)).days
    scal = np.pi / 180.
    hdr0 = ntt.util.readhdr(re.sub('\n', '', files[0]))
    _gain = ntt.util.readkey3(hdr0, 'gain')
    _rdnoise = ntt.util.readkey3(hdr0, 'ron')
    std_sun, rastd_sun, decstd_sun, magstd_sun = ntt.util.readstandard(
        'standard_sofi_sun.txt')
    std_vega, rastd_vega, decstd_vega, magstd_vega = ntt.util.readstandard(
        'standard_sofi_vega.txt')
    std_phot, rastd_phot, decstd_phot, magstd_phot = ntt.util.readstandard(
        'standard_sofi_phot.txt')
    outputfile = []
    objectlist, RA, DEC = {}, {}, {}
    for img in files:
        img = re.sub('\n', '', img)
        hdr = ntt.util.readhdr(img)
        _ra = ntt.util.readkey3(hdr, 'RA')
        _dec = ntt.util.readkey3(hdr, 'DEC')
        _grism = ntt.util.readkey3(hdr, 'grism')
        _filter = ntt.util.readkey3(hdr, 'filter')
        _slit = ntt.util.readkey3(hdr, 'slit')
        cc_sun = np.arccos(np.sin(_dec * scal) * np.sin(decstd_sun * scal) + np.cos(_dec * scal) *
                           np.cos(decstd_sun * scal) * np.cos((_ra - rastd_sun) * scal)) * ((180 / np.pi) * 3600)
        cc_vega = np.arccos(np.sin(_dec * scal) * np.sin(decstd_vega * scal) + np.cos(_dec * scal) *
                            np.cos(decstd_vega * scal) * np.cos((_ra - rastd_vega) * scal)) * ((180 / np.pi) * 3600)
        cc_phot = np.arccos(np.sin(_dec * scal) * np.sin(decstd_phot * scal) + np.cos(_dec * scal) *
                            np.cos(decstd_phot * scal) * np.cos((_ra - rastd_phot) * scal)) * ((180 / np.pi) * 3600)
        if min(cc_sun) < 100:
            _type = 'sun'
        elif min(cc_phot) < 100:
            _type = 'stdp'
        elif min(cc_vega) < 100:
            _type = 'vega'
        else:
            _type = 'obj'
        if min(cc_phot) < 100:
            if _verbose:
                print img, 'phot', str(min(cc_phot)), str(std_phot[np.argmin(cc_phot)])
            ntt.util.updateheader(img, 0, {'stdname': [std_phot[np.argmin(cc_phot)], ''],
                                           'magstd': [float(magstd_phot[np.argmin(cc_phot)]), '']})
        # ntt.util.updateheader(img,0,{'magstd':[float(magstd_phot[argmin(cc_phot)]),'']})
        elif min(cc_sun) < 100:
            if _verbose:
                print img, 'sun', str(min(cc_sun)), str(std_sun[np.argmin(cc_sun)])
            ntt.util.updateheader(img, 0, {'stdname': [std_sun[np.argmin(cc_sun)], ''],
                                           'magstd': [float(magstd_sun[np.argmin(cc_sun)]), '']})
        # ntt.util.updateheader(img,0,{'magstd':[float(magstd_sun[argmin(cc_sun)]),'']})
        elif min(cc_vega) < 100:
            if _verbose:
                print img, 'vega', str(min(cc_vega)), str(std_vega[np.argmin(cc_vega)])
            ntt.util.updateheader(img, 0, {'stdname': [std_vega[np.argmin(cc_vega)], ''],
                                           'magstd': [float(magstd_vega[np.argmin(cc_vega)]), '']})
        # ntt.util.updateheader(img,0,{'magstd':[float(magstd_vega[argmin(cc_vega)]),'']})
        else:
            if _verbose:
                print img, 'object'

        _OBID = (ntt.util.readkey3(hdr, 'esoid'))
        if _type not in objectlist:
            objectlist[_type] = {}
        if _grism not in objectlist[_type]:
            objectlist[_type][_grism] = {}
        if _OBID not in objectlist[_type][_grism]:
            objectlist[_type][_grism][_OBID] = []
        objectlist[_type][_grism][_OBID].append(img)

    if 'stdp' not in objectlist:
        print '###  warning: not photometric standard'
    else:
        print '### photometric standard in the list of object'
    if 'sun' not in objectlist:
        print '### warning: not telluric G standard (sun type)'
    else:
        print '### telluric G standard (sun type) in the list of object'
    if 'vega' not in objectlist:
        print '### warning: not telluric A standard (vega type)'
    else:
        print '### telluric A standard (vega type) in the list of object'

    iraf.noao(_doprint=0)
    iraf.imred(_doprint=0)
    iraf.specred(_doprint=0)
    iraf.immatch(_doprint=0)
    iraf.imutil(_doprint=0)
    toforget = ['specred.apall', 'specred.transform']
    for t in toforget:
        iraf.unlearn(t)
    iraf.specred.apall.readnoi = _rdnoise
    iraf.specred.apall.gain = _gain
    iraf.specred.dispaxi = 2
    for _type in objectlist:
        for setup in objectlist[_type]:
            for _ID in objectlist[_type][setup]:
                listmerge = objectlist[_type][setup][_ID]
                listmerge = ntt.sortbyJD(listmerge)
                _object = ntt.util.readkey3(
                    ntt.util.readhdr(listmerge[0]), 'object')
                if string.count(_object, '/') or string.count(_object, '.') or string.count(_object, ' '):
                    nameobj = string.split(_object, '/')[0]
                    nameobj = string.split(nameobj, ' ')[0]
                    nameobj = string.split(nameobj, '.')[0]
                else:
                    nameobj = _object
                _date = ntt.util.readkey3(
                    ntt.util.readhdr(listmerge[0]), 'date-night')
                outputimage = nameobj + '_' + _date + \
                    '_' + setup + '_merge_' + str(MJDtoday)
                outputimage = ntt.util.name_duplicate(
                    listmerge[0], outputimage, '')
                print '### setup= ', setup, ' name field= ', nameobj, ' merge image= ', outputimage, '\n'
#################
#  added to avoid crashing with a single frame
#  header will not be updated with all info
#################

                if len(listmerge)==1:
                    ntt.util.delete(outputimage)
                    iraf.imutil.imcopy(listmerge[0], output=outputimage, verbose='no')
                    answ= 'n'
                else:
                  if os.path.isfile(outputimage) and _interactive:
                    answ = raw_input(
                        'combine frame of dithered spectra already created. Do you want to make it again [[y]/n] ? ')
                    if not answ:
                        answ = 'y'
                  else:
                    answ = 'y'
#################
                if answ in ['Yes', 'y', 'Y', 'yes']:
                    if _interactive:
                        automaticmerge = raw_input(
                            '\n### Do you want to try to find the dither bethween frames automatically [[y]/n]')
                        if not automaticmerge:
                            automaticmerge = 'yes'
                        elif automaticmerge.lower() in ['y', 'yes']:
                            automaticmerge = 'yes'
                        else:
                            automaticmerge = 'no'
                    else:
                        automaticmerge = 'yes'
                    if automaticmerge == 'yes':
                        offset = 0
                        offsetvec = []
                        _center0 = ntt.sofispec1Ddef.findaperture(
                            listmerge[0], False)
                        _offset0 = ntt.util.readkey3(
                            ntt.util.readhdr(listmerge[0]), 'xcum')
                        print '\n### Try to merge spectra considering their offset along x axes .......'
                        f = open('_offset', 'w')
                        for img in listmerge:
                            _center = ntt.sofispec1Ddef.findaperture(
                                img, False)
                            _center2 = (
                                float(_center) + (float(_offset0) - float(_center0))) * (-1)
                            _offset = (-1) * \
                                ntt.util.readkey3(
                                    ntt.util.readhdr(img), 'xcum')
                            if abs(_center2 - _offset) >= 20:
                                automaticmerge = 'no'
                                break
                            else:
                                offset3 = _center2
                            offsetvec.append(offset3)
                            line = str(offset3) + '   0\n'
                            f.write(line)
                        f.close()
                    if automaticmerge == 'yes':
                        print '### automatic merge .......... done'
                    else:
                        print '\n### warning: try identification of spectra position in interactive way '
                        offset = 0
                        offsetvec = []
                        _z1, _z2, goon = ntt.util.display_image(
                            listmerge[0], 1, '', '', False)
                        print '\n### find aperture on first frame and use it as reference position of ' \
                              'the spectra (mark with ' + '"' + 'm' + '"' + ')'
                        _center0 = ntt.sofispec1Ddef.findaperture(
                            listmerge[0], True)
                        _offset0 = ntt.util.readkey3(
                            ntt.util.readhdr(listmerge[0]), 'xcum')
                        print '\n### find the aperture on all the spectra frames (mark with ' + '"' + 'm' + '"' + ')'
                        f = open('_offset', 'w')
                        for img in listmerge:
                            print '\n### ', img
                            _z1, _z2, goon = ntt.util.display_image(
                                img, 1, '', '', False)
                            _center = ntt.sofispec1Ddef.findaperture(img, True)
                            _center2 = (
                                float(_center) + (float(_offset0) - float(_center0))) * (-1)
                            _offset = (-1) * \
                                ntt.util.readkey3(
                                    ntt.util.readhdr(img), 'xcum')
                            print '\n### position from  dither header: ' + str(_offset)
                            print '### position identified interactively: ' + str(_center2)
                            offset3 = raw_input(
                                '\n### which is the right position [' + str(_center2) + '] ?')
                            if not offset3:
                                offset3 = _center2
                            offsetvec.append(offset3)
                            line = str(offset3) + '   0\n'
                            f.write(line)
                        f.close()
                    print offsetvec
                    start = int(max(offsetvec) - min(offsetvec))
                    print start
                    f = open('_goodlist', 'w')
                    print listmerge
                    for img in listmerge:
                        f.write(img + '\n')
                    f.close()
                    ntt.util.delete(outputimage)
                    ntt.util.delete('_output.fits')
                    yy1 = pyfits.open(listmerge[0])[0].data[:, 10]
                    iraf.immatch.imcombine('@_goodlist', '_output', combine='sum', reject='none', offset='_offset',
                                           masktyp='', rdnoise=_rdnoise, gain=_gain, zero='mode', Stdout=1)

                    _head = pyfits.open('_output.fits')[0].header
                    if _head['NAXIS1'] < 1024:
                        stop = str(_head['NAXIS1'])
                    else:
                        stop = '1024'

                    iraf.imutil.imcopy(
                            '_output[' + str(start) + ':'+stop+',*]', output=outputimage, verbose='no')

                    print outputimage
                    print len(listmerge)
                    hdr1 = ntt.util.readhdr(outputimage)
                    ntt.util.updateheader(outputimage, 0,
                                          {'SINGLEXP': [False, 'TRUE if resulting from single exposure'],
                                           'M_EPOCH': [False, 'TRUE if resulting from multiple epochs'],
                                           'EXPTIME': [ntt.util.readkey3(hdr1, 'EXPTIME') * len(listmerge),
                                                       'Total integration time per pixel (s)'],
                                           'TEXPTIME': [float(ntt.util.readkey3(hdr1, 'TEXPTIME')) * len(listmerge),
                                                        'Total integration time of all exposures (s)'],
                                           'APERTURE': [2.778e-4 * float(re.sub('long_slit_', '',
                                                                                ntt.util.readkey3(hdr1, 'slit'))),
                                                        '[deg] Aperture diameter'],
                                           'NOFFSETS': [2, 'Number of offset positions'],
                                           'NUSTEP': [0, 'Number of microstep positions'],
                                           'NJITTER': [int(ntt.util.readkey3(hdr1, 'NCOMBINE') / 2),
                                                       'Number of jitter positions']})
                    hdr = ntt.util.readhdr(outputimage)
                    matching = [s for s in hdr.keys() if "IMCMB" in s]
                    for imcmb in matching:
                        aaa = iraf.hedit(outputimage, imcmb, delete='yes', update='yes',
                                         verify='no', Stdout=1)
                    if 'SKYSUB' in hdr.keys():
                        aaa = iraf.hedit(outputimage, 'SKYSUB', delete='yes', update='yes',
                                         verify='no', Stdout=1)

                    mjdend = []
                    mjdstart = []
                    num = 0
                    for img in listmerge:
                        num = num + 1
                        hdrm = ntt.util.readhdr(img)
                        ntt.util.updateheader(outputimage, 0,
                                              {'PROV' + str(num):
                                               [ntt.util.readkey3(
                                                   hdrm, 'ARCFILE'), 'Originating file'],
                                               'TRACE' + str(num): [img, 'Originating file']})
                        mjdend.append(ntt.util.readkey3(hdrm, 'MJD-END'))
                        mjdstart.append(ntt.util.readkey3(hdrm, 'MJD-OBS'))
                    _dateobs = ntt.util.readkey3(ntt.util.readhdr(
                        listmerge[np.argmin(mjdstart)]), 'DATE-OBS')

                    _telapse = (max(mjdend) - min(mjdstart)) * \
                        60. * 60 * 24.  # *86400
                    _tmid = (max(mjdend) + min(mjdstart)) / 2

                    _title = str(_tmid)[0:9] + ' ' + str(ntt.util.readkey3(hdr, 'object')) + ' ' + str(
                        ntt.util.readkey3(hdr, 'grism')) + ' ' + \
                        str(ntt.util.readkey3(hdr, 'filter')) + \
                        ' ' + str(ntt.util.readkey3(hdr, 'slit'))
                    ntt.util.updateheader(outputimage, 0,
                                          {'MJD-OBS': [min(mjdstart), 'MJD start'],
                                           'MJD-END': [max(mjdend), 'MJD end'],
                                           'TELAPSE': [_telapse, 'Total elapsed time [days]'],
                                           'TMID': [_tmid, '[d] MJD mid exposure'],
                                           'TITLE': [_title, 'Dataset title'],
                                           'DATE-OBS': [_dateobs, 'Date of observation']})
                    # missing: merge airmass
                else:
                    print '\n### skip making again combined spectrum'
                objectlist[_type][setup][_ID] = [outputimage]
                print '\n### setup= ', setup, ' name field= ', nameobj, ' merge image= ', outputimage, '\n'
                if outputimage not in outputfile:
                    outputfile.append(outputimage)
                ntt.util.updateheader(outputimage, 0, {'FILETYPE': [
                                      42116, 'combine 2D spectra frame']})

    if _verbose:
        if 'obj' in objectlist:
            print objectlist['obj']
        if 'stdp' in objectlist:
            print objectlist['stdp']
        if 'sun' in objectlist:
            print objectlist['sun']
        if 'vega' in objectlist:
            print objectlist['vega']

    if 'obj' not in objectlist.keys():
        sys.exit('\n### error: no objects in the list')

    sens = {}
    print '\n############################################\n### extract the spectra  '
    # print objectlist
    for setup in objectlist['obj']:
        reduced = []
        for _ID in objectlist['obj'][setup]:
            for img in objectlist['obj'][setup][_ID]:
                hdr = ntt.util.readhdr(img)
                print '\n### next object\n ', img, ntt.util.readkey3(hdr, 'object')
                _grism = ntt.util.readkey3(hdr, 'grism')
                _exptimeimg = ntt.util.readkey3(hdr, 'exptime')
                _JDimg = ntt.util.readkey3(hdr, 'JD')

                imgex = ntt.util.extractspectrum(img, dv, _ext_trace, _dispersionline, _interactive, 'obj',
                                                 automaticex=_automaticex)
                if imgex not in outputfile:
                    outputfile.append(imgex)
                ntt.util.updateheader(imgex, 0, {'FILETYPE': [42107, 'extracted 1D wave calib'],
                                                 'PRODCATG': ['SCIENCE.' + ntt.util.readkey3(hdr, 'tech').upper(),
                                                              'Data product category']})
                hdr = ntt.util.readhdr(imgex)
                matching = [s for s in hdr.keys() if "TRACE" in s]
                for imcmb in matching:
                    aaa = iraf.hedit(imgex, imcmb, delete='yes',
                                     update='yes', verify='no', Stdout=1)
                ntt.util.updateheader(
                    imgex, 0, {'TRACE1': [img, 'Originating file']})

                if os.path.isfile('database/ap' + re.sub('_ex.fits', '', imgex)):
                    if 'database/ap' + re.sub('_ex.fits', '', imgex) not in outputfile:
                        outputfile.append(
                            'database/ap' + re.sub('_ex.fits', '', imgex))

                ###########################   telluric standard   #############
                if 'sun' in objectlist and setup in objectlist['sun']:
                    _type = 'sun'
                elif 'vega' in objectlist and setup in objectlist['vega']:
                    _type = 'vega'
                else:
                    _type = 'none'
                if _type in ['sun', 'vega']:
                    stdref = ntt.__path__[
                        0] + '/standard/fits/' + str(_type) + '.fits'
                    stdvec, airmassvec, JDvec = [], [], []
                    for _ID in objectlist[_type][setup]:
                        for std in objectlist[_type][setup][_ID]:
                            _airmassstd = ntt.util.readkey3(
                                ntt.util.readhdr(std), 'airmass')
                            _JDstd = ntt.util.readkey3(
                                ntt.util.readhdr(std), 'JD')
                            JDvec.append(abs(_JDstd - _JDimg))
                            stdvec.append(std)
                            airmassvec.append(_airmassstd)
                    stdtelluric = stdvec[np.argmin(JDvec)]
                    _exptimestd = ntt.util.readkey3(
                        ntt.util.readhdr(stdtelluric), 'exptime')
                    _magstd = ntt.util.readkey3(
                        ntt.util.readhdr(stdtelluric), 'magstd')
                    print '\n\n ##### closer standard for telluric corrections  #### \n\n'
                    print stdtelluric, airmassvec[np.argmin(JDvec)]
                    stdtelluric_ex = ntt.util.extractspectrum(stdtelluric, dv, False, False, _interactive, 'std',
                                                              automaticex=_automaticex)
                    if stdtelluric_ex not in outputfile:
                        outputfile.append(stdtelluric_ex)
                    ntt.util.updateheader(stdtelluric_ex, 0, {'FILETYPE': [
                                          42107, 'extracted 1D wave calib ']})
                    ntt.util.updateheader(stdtelluric_ex, 0, {'PRODCATG': [
                        'SCIENCE.' + ntt.util.readkey3(
                            ntt.util.readhdr(stdtelluric_ex), 'tech').upper(), 'Data product category']})

                    hdr = ntt.util.readhdr(stdtelluric_ex)
                    matching = [s for s in hdr.keys() if "TRACE" in s]
                    for imcmb in matching:
                        aaa = iraf.hedit(
                            stdtelluric_ex, imcmb, delete='yes', update='yes', verify='no', Stdout=1)
                    ntt.util.updateheader(stdtelluric_ex, 0, {'TRACE1': [
                                          stdtelluric, 'Originating file']})
                    ###########################################################
                    #               SN tellurich calibration
                    imgf = re.sub('_ex.fits', '_f.fits', imgex)
                    imgf, senstelluric = ntt.sofispec1Ddef.calibrationsofi(imgex, stdtelluric_ex, stdref, imgf,
                                                                           _interactive)
                    if imgf not in outputfile:
                        outputfile.append(imgf)
                    if senstelluric not in outputfile:
                        outputfile.append(senstelluric)
                    ntt.util.updateheader(imgf, 0, {'FILETYPE': [42208, '1D wave calib, tell cor.'],
                                                    #                                                    'SNR': [ntt.util.StoN(imgf, 50),
                                                    'SNR': [ntt.util.StoN2(imgf, False),
                                                            'Average signal to noise ratio per pixel'],
                                                    'TRACE1': [imgex, 'Originating file'],
                                                    'ASSON1': [re.sub('_f.fits', '_2df.fits', imgf),
                                                               'Name of associated file'],
                                                    'ASSOC1': ['ANCILLARY.2DSPECTRUM', 'Category of associated file']})
                    ###########################################################
                    imgd = ntt.efoscspec1Ddef.fluxcalib2d(
                        img, senstelluric)  # flux calibration 2d images
                    ntt.util.updateheader(
                        imgd, 0, {'FILETYPE': [42209, '2D wavelength and flux calibrated spectrum']})
                    iraf.hedit(imgd, 'PRODCATG', delete='yes',
                               update='yes', verify='no')
                    hdrd = ntt.util.readhdr(imgd)
                    matching = [s for s in hdrd.keys() if "TRACE" in s]
                    for imcmb in matching:
                        aaa = iraf.hedit(
                            imgd, imcmb, delete='yes', update='yes', verify='no', Stdout=1)
                    ntt.util.updateheader(
                        imgd, 0, {'TRACE1': [img, 'Originating file']})
                    if imgd not in outputfile:
                        outputfile.append(imgd)
                ###############################################################
                if 'stdp' in objectlist and setup in objectlist['stdp']:
                    print '\n #####  photometric calibration   ######\n '
                    standardfile = []
                    for _ID in objectlist['stdp'][setup]:
                        for stdp in objectlist['stdp'][setup][_ID]:
                            stdp_ex = ntt.util.extractspectrum(stdp, dv, False, _dispersionline, _interactive, 'std',
                                                               automaticex=_automaticex)
                            standardfile.append(stdp_ex)
                            if stdp_ex not in outputfile:
                                outputfile.append(stdp_ex)
                            ntt.util.updateheader(stdp_ex, 0, {
                                'FILETYPE': [42107, 'extracted 1D wave calib'],
                                'TRACE1': [stdp_ex, 'Originating file'],
                                'PRODCATG': ['SCIENCE.' + ntt.util.readkey3(ntt.util.readhdr(stdp_ex), 'tech').upper(),
                                             'Data product category']})
                    print '\n### ', standardfile, ' \n'
                    if len(standardfile) >= 2:
                        standardfile0 = raw_input(
                            'which one do you want to use [' + str(standardfile[0]) + '] ? ')
                        if not standardfile0:
                            standardfile0 = standardfile[0]
                    else:
                        standardfile0 = standardfile[0]
                    print standardfile0
                    stdpf = re.sub('_ex.fits', '_f.fits', standardfile0)
                    stdpf, senstelluric2 = ntt.sofispec1Ddef.calibrationsofi(standardfile0, stdtelluric_ex, stdref,
                                                                             stdpf, _interactive)
                    if stdpf not in outputfile:
                        outputfile.append(stdpf)
                    ntt.util.updateheader(stdpf, 0, {'FILETYPE': [42208, '1D wave calib, tell cor'],
                                                     'TRACE1': [stdp, 'Originating file']})
                    stdname = ntt.util.readkey3(
                        ntt.util.readhdr(standardfile0), 'stdname')
                    standardfile = ntt.__path__[
                        0] + '/standard/flux/' + stdname
                    xx, yy = ntt.util.ReadAscii2(standardfile)

                    crval1 = pyfits.open(stdpf)[0].header.get('CRVAL1')
                    cd1 = pyfits.open(stdpf)[0].header.get('CD1_1')
                    datastdpf, hdrstdpf = pyfits.getdata(stdpf, 0, header=True)
                    xx1 = np.arange(len(datastdpf[0][0]))
                    aa1 = crval1 + (xx1) * cd1
                    yystd = np.interp(aa1, xx, yy)
                    rcut = np.compress(
                        ((aa1 < 13000) | (aa1 > 15150)) & ((11700 < aa1) | (aa1 < 11000)) & (aa1 > 10000) &
                        ((aa1 < 17800) | (aa1 > 19600)) & (aa1 < 24000), datastdpf[0][0] / yystd)
                    aa11 = np.compress(
                        ((aa1 < 13000) | (aa1 > 15150)) & ((11700 < aa1) | (aa1 < 11000)) & (aa1 > 10000) &
                        ((aa1 < 17800) | (aa1 > 19600)) & (aa1 < 24000), aa1)
                    yy1clean = np.interp(aa1, aa11, rcut)
                    aa1 = np.array(aa1)
                    yy1clean = np.array(yy1clean)
                    A = np.ones((len(rcut), 2), dtype=float)
                    A[:, 0] = aa11
                    result = np.linalg.lstsq(A, rcut)  # result=[zero,slope]
                    p = [result[0][1], result[0][0]]
                    yfit = ntt.util.pval(aa1, p)

                    pl.clf()
                    pl.ion()
                    pl.plot(aa1, datastdpf[0][0] / yystd,
                            color='red', label='std')
                    pl.plot(aa1, yfit, color='blue', label='fit')
                    pl.legend(numpoints=1, markerscale=1.5)
                    #   sens function sofi spectra
                    outputsens = 'sens_' + stdpf
                    ntt.util.delete(outputsens)
                    datastdpf[0][0] = yfit
                    pyfits.writeto(outputsens, np.float32(datastdpf), hdrstdpf)
                    #################
                    imgsc = re.sub('_ex.fits', '_sc.fits', imgex)
                    ntt.util.delete(imgsc)
                    crval2 = pyfits.open(imgf)[0].header.get('CRVAL1')
                    cd2 = pyfits.open(imgf)[0].header.get('CD1_1')
                    dataf, hdrf = pyfits.getdata(imgf, 0, header=True)
                    xx2 = np.arange(len(dataf[0][0]))
                    aa2 = crval2 + (xx2) * cd2
                    yyscale = np.interp(aa2, aa1, yfit)

                    dataf[0][0] = dataf[0][0] / yyscale
                    dataf[1][0] = dataf[1][0] / yyscale
                    dataf[2][0] = dataf[2][0] / yyscale
                    dataf[3][0] = dataf[3][0] / yyscale

                    pyfits.writeto(imgsc, np.float32(dataf), hdrf)
                    ntt.util.updateheader(imgsc, 0, {'SENSPHOT': [outputsens, 'sens used to flux cal'],
                                                     'FILETYPE': [42208, '1D wave,flux calib, tell cor'],
                                                     'TRACE1': [imgf, 'Originating file']})
                    #                ntt.util.updateheader(imgsc,0,{'FILETYPE':[42208,'1D wave,flux calib, tell cor']})
                    #                ntt.util.updateheader(imgsc,0,{'TRACE1':[imgf,'']})
                    print '\n### flux calibrated spectrum= ', imgf, ' with the standard= ', stdpf
                    if imgsc not in outputfile:
                        outputfile.append(imgsc)
                else:
                    print '\n### photometric calibrated not performed \n'

    print '\n### adding keywords for phase 3 ....... '
    reduceddata = ntt.util.rangedata(outputfile)
    f = open('logfile_spec1d_' + str(reduceddata) +
             '_' + str(datenow) + '.raw.list', 'w')
    for img in outputfile:
        if str(img)[-5:] == '.fits':
            hdr = ntt.util.readhdr(img)
            # added for DR2
            if 'NCOMBINE' in hdr:
                _ncomb = ntt.util.readkey3(hdr, 'NCOMBINE')
            else:
                _ncomb = 1.0

            _effron = 12. * \
                (1 / np.sqrt(ntt.util.readkey3(hdr, 'ndit') * _ncomb)) * \
                np.sqrt(np.pi / 2)

            try:
                ntt.util.phase3header(img)  # phase 3 definitions
                ntt.util.updateheader(img, 0, {'quality': ['Final', ''],
                                               'EFFRON': [_effron, 'Effective readout noise per output (e-)']})
                f.write(ntt.util.readkey3(
                    ntt.util.readhdr(img), 'arcfile') + '\n')
            except:
                print 'Warning: ' + img + ' is not a fits file'
    f.close()
    return outputfile, 'logfile_spec1d_' + str(reduceddata) + '_' + str(datenow) + '.raw.list'
Esempio n. 12
0
def coaddImages(workdir, coaddfile=None, method='median'):
    '''
    Combine aligned images, masking out bad pixels using transformed
    masks

    @parameter coaddfile path to output coadded file to be produced by
    this method. None defaults to the workdir.
    
    @parameter method Method for combining images. See IRAF's imcombine
    for additional options
    '''

    print "Reading database..."
    db = readpickle(os.path.join(workdir, "db.pkl"))
    rejectimages = []
    for image in db:
        ## reject images with unphysical transformations.
        ##  e.g., those rotated too much
        if image['geomapangle'] > 15. and image['geomapangle'] < 345:
            image['okforalignment'] = False
            rejectimages.append(image['rawimgname'])

    db = [image for image in db if image['okforalignment'] == True]
    if len(db) == 0:
        print "Cannot continue, no matching images!"
        return

    print "Ok, we have %i images to combine." % len(db)

    curdir = os.getcwd()
    os.chdir(workdir)

    if coaddfile is None:
        coaddfile = 'coadd.fits'

    starttime = datetime.now()

    iraf.images()
    iraf.immatch()
    inputimages = []
    outnames = []

    ## sub-grouping the files
    numberofGroups = int(len(db) / 30.) + 1
    for i in range(numberofGroups - 1):
        inputimages.append(','.join(
            [image['aliimgname'] for image in db[i * 30:(i + 1) * 30]]))
        outnames.append(coaddfile + '_{0}.fits'.format(i))

    if len(db) % 30 != 0:
        ## handling the remaining not-grouped files
        inputimages.append(','.join(
            [image['aliimgname'] for image in db[(numberofGroups - 1) * 30:]]))
        outnames.append(coaddfile + '_{0}.fits'.format(numberofGroups - 1))
    print "divied the images in {} groups".format(numberofGroups)

    for i in range(len(inputimages)):
        iraf.flprcache()
        iraf.imcombine(input=inputimages[i],
                       output=outnames[i],
                       combine=method,
                       masktype='goodvalue')
    iraf.flprcache()
    iraf.imcombine(input=outnames[i],
                   output=coaddfile,
                   combine=method,
                   masktype='goodvalue')

    print "- " * 40
    endtime = datetime.now()
    timetaken = nicetimediff(endtime - starttime)

    os.chdir(curdir)

    print "Dear user, I'm done with the alignment. I did it in %s." % timetaken
    print "The following are rejected: \n{0}".format(rejectimages)
    ## Update the db :
    writepickle(db, os.path.join(workdir, "db.pkl"))
Esempio n. 13
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
Esempio n. 14
0


zp, sigma_zp = 37.7, 0.03

# Add zero point to the header of all the Halpha images
for im_name in list_images["filename"]:
    filter = utils.get_from_header(im_name, filterk)
    if filter == "H6678":
        utils.header_update_keyword(im_name, "ZP", zp, "AB magnitude zero point." )
        utils.header_update_keyword(im_name, "ZP_err", sigma_zp, "Zero point 1-sigma. ")


print "Combine images of same object and filter"
iraf.images(_doprint=0)
iraf.immatch(_doprint=0)

keywords = dict(filterk=filterk, objectk=objectk, gaink=gaink, datek=datek, exptimek=exptimek,
                fwhmk="seeing", airmassk=airmassk)
objects_list = defaultdict(list)
for obj in SciObj_set:
    obj_images = list(list_images["filename"][np.where(list_images["objname"] == obj)])
    obj_filters = [utils.get_from_header(im_name, filterk) for im_name in obj_images]
    for filt in set(obj_filters):
        input_images = [im_name for im_name, im_filt in zip(obj_images, obj_filters) if im_filt == filt]
        output_name = os.path.join(directory, obj + "_combined_" + filt + ".fits")
        input_names = ",".join(input_images)
        utilities.if_exists_remove(output_name)
        iraf.imcombine(input_names, output=output_name, combine="median", offsets="wcs")
        objects_list[obj].append(output_name)
Esempio n. 15
0
def align_norm(fnlist,uncertlist=None):
    """Aligns a set of images to each other, as well as normalizing the images
    to the same average brightness.
    
    Both the alignment and normalization are accomplished through stellar
    photometry using the IRAF routine 'daophot'. The centroids of a handful
    of stars are found and used to run the IRAF routine 'imalign'. The
    instrumental magnitudes of the stars are used to determine by how much
    each image must be scaled for the photometry to match across images.
    
    The images are simply updated with their rescaled, shifted selves. This
    overwrites the previous images and adds the header keyword 'fpphot' to
    the images.
    
    A handful of temporary files are created during this process, which should
    all be deleted by the routine at the end. But if it is interrupted, they
    might not be.
    
    If the uncertainty images exist, this routine also shifts them by the same
    amounts as the intensity images, as well as updating the uncertainty values
    for both the new normalization and the uncertainties in normalizing the
    images.
    
    Inputs:
    fnlist -> List of strings, each the path to a fits image.
    uncertlist (optional) -> List of paths to uncertainty images.
    
    """
    
    #Fit for the sky background level
    _skyavg, skysig = fit_sky_level(fnlist)
    
    #Get image FWHMs
    fwhm = np.empty(len(fnlist))
    firstimage = openfits(fnlist[0])
    toggle = firstimage[0].header.get("fpfwhm")
    axcen = firstimage[0].header.get("fpaxcen")
    aycen = firstimage[0].header.get("fpaycen")
    arad = firstimage[0].header.get("fparad")
    firstimage.close()
    if axcen == None:
        print "Error! Images have not yet been aperture-masked! Do this first!"
        crash()
    if toggle == None:
        print "Warning: FWHMs have not been measured! Assuming 5 pixel FWHM for all images."
        for i in range(len(fnlist)): fwhm[i] = 5
    else:
        for i in range(len(fnlist)):
            image = openfits(fnlist[i])
            fwhm[i] = image[0].header["fpfwhm"]
            image.close()
    
    #Identify objects in the fields
    coolist = identify_objects(fnlist,skysig,fwhm)
    
    #Match objects between fields
    coofile = match_objects(coolist)
    
    #Do aperture photometry on the matched objects
    photlist = do_phot(fnlist,coofile,fwhm,skysig)
    
    #Read the photometry files
    x, y, mag, dmag = read_phot(photlist)
    
    #Calculate the normalizations
    norm, dnorm = calc_norm(mag,dmag)
    
    #Normalize the images (and optionally, the uncertainty images)
    for i in range(len(fnlist)):
        print "Normalizing image "+fnlist[i]
        image = openfits(fnlist[i],mode="update")
        if not (uncertlist is None):
            uncimage = openfits(uncertlist[i],mode="update")
            uncimage[0].data = np.sqrt(norm[i]**2*uncimage[0].data**2 + dnorm[i]**2*image[0].data**2)
            uncimage.close()
        image[0].data *= norm[i]
        image.close()
    
    #Calculate the shifts
    for i in range(x.shape[1]):
        x[:,i] = -(x[:,i] - x[0,i])
        y[:,i] = -(y[:,i] - y[0,i])
    xshifts = np.average(x,axis=1)
    yshifts = np.average(y,axis=1)
    
    #Shift the images (and optionally, the uncertainty images)
    iraf.images(_doprint=0)
    iraf.immatch(_doprint=0)
    for i in range(len(fnlist)):
        print "Shifting image "+fnlist[i]
        iraf.geotran(input=fnlist[i],
                     output=fnlist[i],
                     geometry="linear",
                     xshift=xshifts[i],
                     yshift=yshifts[i],
                     database="",
                     verbose="no")
        if not (uncertlist is None):
            iraf.geotran(input=uncertlist[i],
                         output=uncertlist[i],
                         geometry="linear",
                         xshift=xshifts[i],
                         yshift=yshifts[i],
                         database="",
                         verbose="no")
    
    #Update the image headers
    for i in range(len(fnlist)):
        image = openfits(fnlist[i],mode="update")
        image[0].header["fpphot"]="True"
        image[0].header["fpxcen"]+=xshifts[i]
        image[0].header["fpycen"]+=yshifts[i]
        image[0].header["fpaxcen"]+=xshifts[i]
        image[0].header["fpaycen"]+=yshifts[i]
        image.close()
    
    #Clean up the coordinate file list
    clean_files(fnlist)
    remove(coofile)
    for i in range(len(photlist)):
        remove(photlist[i])
    
    return