コード例 #1
0
ファイル: EchelleReduce.py プロジェクト: sPaMFouR/PythonJunk
def rename_files(files, key, keypattern, oldpattern, newpattern, exact=0):
    '''Quick little function that lets you search a list of files for
      a particular key (e.g., OBJECT) and if it matches the keypattern
      (e.g., 'flat'), it applies imrename using the provided oldpattern
      and newpattern to do the renaming (e.g. 'a' and 'comp').  If you 
      want an exact match for the key pattern, specify exact=1'''
    list = get_header(files, key, str)
    if exact: keypattern = r"\A" + keypattern + r"\Z"
    for name in list:
        if re.search(keypattern, list[name]):
            iraf.imrename(oldnames=name,
                          newnames=re.sub(oldpattern, newpattern, name))
コード例 #2
0
    def _scale_image(self, in_image):
        """
        Converts from [e] to [e/s]

        @param in_image: the input image
        @type in_image: string
        """
        # unlearn some iraf tasks
        iraf.unlearn('imarith')

        # get a random filename
        tmpfile1 = get_random_filename('t', '.fits')

        # divide by exposure time
        iraf.imarith(operand1=in_image,
                     op="/",
                     operand2=self.exptime,
                     result=tmpfile1)

        # delete the old image;
        # rename the new to the old name
        os.unlink(in_image)
        iraf.imrename(oldnames=tmpfile1, newnames=in_image)
コード例 #3
0
ファイル: reduce24.py プロジェクト: rfinn/pythonCode
def irafflatten():
    os.system('cp /Users/rfinn/clusters/spitzer/flatsexfiles/* .')
    infile=open('InputImageList.txt','r')
    outfile=open('FlatImageList.txt','w')
    sky=[]
    for line in infile:
	im=line[0:(len(line)-1)]
	mask='mask'+im
	skyim='s'+im
	outline='f'+line
	iraf.imgets(im,'DRIBKGND')
	t=iraf.imgets.value
	sky.append(float(t))
	outfile.write(outline)
    #get object positions using sextractor
	iraf.imarith(im,'-',t,skyim)#subtract sky before running sextractor (otherwise it doesn't detect any objects - don't know why...)
	s='sex '+skyim
	os.system(s)
	x=[]
	y=[]
	catfile=open('test.cat','r')
	for line in catfile:
	    if line.find('#') > -1:
		continue
	    t=line.split()
	    x.append(float(t[10]))
	    y.append(float(t[11]))
	catfile.close()
	x=N.array(x,'f')
	y=N.array(y,'f')
        
	try:#create image of ones same size as image
	    iraf.imarith(im,'/',im,'mask')
	except:
	    iraf.imdelete('mask')
	    iraf.imarith(im,'/',im,'mask')
	print "masking objects"
	for j in range(len(x)): #mask objects and radius around each position using imreplace, radius=10 pix
	    for k in range(11):
		y1=int(y[j]+5-k)
		if y1 < 1:
		    continue
		if y1 > 128:
		    continue
		xmin=int(x[j]-5)
		if xmin < 1:
		    xmin=1
		xmax=int(x[j]+5)
		if xmax > 128:
		    xmax=128

		s='mask['+str(xmin)+':'+str(xmax)+","+str(y1)+":"+str(y1)+"]"
		iraf.imreplace(s,0.)
	iraf.imrename('mask',mask)
	print "updating BPM field in header"
	iraf.hedit(im,fields='BPM',value=mask,add='yes',verify='no',update='yes')
    outfile.close()
    infile.close()
    avesky=N.average(N.array(sky,'f'))
    lthresh=avesky-1.
    hthresh=avesky+.6
    iraf.imcombine('@InputImageList.txt','flat',combine='average',reject='ccdclip',scale='none',zero='mode',lthreshold=lthresh,hthreshold=hthresh,lsigma=2.,hsigma=2.,rdnoise=5.,gain=5.,blank=1.,grow=12.,masktype='badvalue',maskvalue='0')
    t=iraf.imstat('flat',fields='mean',format='no',Stdout=1)
    ave=float(t[0])
    iraf.imarith('flat','/',ave,'nflat')
    iraf.imarith('@InputImageList.txt','/','nflat','@FlatImageList.txt')
コード例 #4
0
ファイル: saofocus.py プロジェクト: cenko/python
def saofocus(inpat,prepfile=yes,endhow="never",endwhen="",
             clobber=globclob,verbose=globver):

    """ derive best focus from an SAOFOCUS run on P60 """

    # Defaults
    twait=30
    reduced={}
    bpmname="BPM.pl"
    saokey="IMGTYPE"
    saore="SAOFOCUS"
    fseqkey="SAOFVALS"
    sigma=2.0
    satval=50000.0
    masksfx="mask"
    pix=0.3787 # arcsec per pixel
    mindiff=2  # minimum tolerable diff b/ best and worst seeing (pix)

    # Parse end-condition "time"
    if endhow=="time":
        re1=re.search("^(\d+):(\d+)",endwhen)
        if re1:
            tnow=time.gmtime()
            # Force UT calculation w/ ignoring of DST issues
            reftime=time.mktime([tnow[0],tnow[1],tnow[2],
                                 int(re1.group(1)),int(re1.group(2)),0,
                                 tnow[6],tnow[7],0]) - time.timezone
            if reftime<time.time():
                reftime+=86400
            if verbose:
                print "Running until %s" % \
                      time.strftime("%d %b %H:%M",time.gmtime(reftime))
        else:
            print "Failed to parse %s as UT time" % endwhen
            print "Running until stopped..."
            endhow="never"

    ##################################################

    # Big Loop
    done=no
    while not done:

        # Parse inputs
        allfiles=glob.glob(inpat)

        newfiles=[]
        for image in allfiles:
            if not reduced.has_key(image):
                # Interested only in SAOFOCUS images
                saoval=get_head(image,saokey)
                if re.search(saore,saoval,re.I):
                    reduced[image]=no
                    newfiles.append(image)
                else:
                    # Skip
                    reduced[image]=yes

        for image in newfiles:

            if verbose:
                print "Processing new SAOFOCUS image %s" % image

            # Only attempt reduction once for each image
            reduced[image]=yes

            # Demosaic
            ccdproc=iraf.ccdproc
            ccdproc.overscan=yes
            ccdproc.trim=yes
            ccdproc.zerocor=no
            iraf.iqmosaic(image,outpfx="",biassec="!BIASSEC",
                          trimsec="!TRIMSEC",joinkeys="DETSEC",
                          splitkeys="AMPSEC,CCDSEC,TRIM,OVERSCAN",
                          clobber=yes,verbose=verbose)
            # Fix the WCS that IRAF has now clobbered
            iraf.add_wcs(image,instrument="p60new")

            # Get some important keywords
            [raoff,decoff,saonseq,saoshift] = \
                   get_head(image,['RAOFF','DECOFF','NUMFOC',
                                   'SAOSHIFT'])
            # Type conversions
            saonseq=int(saonseq)
            saoshift=float(saoshift)

            # No bias-subtraction or flat-fielding

            # Subtract stair-step background if requested
            if prepfile:
                qtmp=iraf.mktemp("saofoc")+".fits"
                iraf.imcopy(image,qtmp,verbose=no)
                for iamp in [-1,1]:
                    for istep in range(saonseq):
                        qsec=iraf.mktemp("saofsc")+".fits"
                        y1=1024+iamp*(1+istep*saoshift)+max(0,iamp)
                        y2=y1+iamp*(saoshift-1)
                        if istep==0:
                            y1-=iamp
                        elif istep==saonseq-1:
                            y2=1+max(0,iamp)*2047
                        ymin=min(y1,y2)
                        ymax=max(y1,y2)
                        statsec='[*,%d:%d]' % (ymin,ymax)
                        iraf.iterstat(image+statsec,nsigrej=5,
                                      maxiter=10,prin=no,verbose=no)
                        medreg=float(iraf.iterstat.median)
                        iraf.imarith(image+statsec,'-',medreg,
                                     qsec,verbose=no,noact=no)
                        iraf.imcopy(qsec,qtmp+statsec,verbose=no)
                        iraf.imdel(qsec,verify=no,go_ahead=yes)
                iraf.imdel(image,verify=no,go_ahead=yes)
                iraf.imrename(qtmp,image,verbose=no)
            
            # No renaming of the file

            # No Bad Pixel Masking or rudimentary WCS

            # Object-detection
            doobj=1
            if check_head(image,'STARFILE'):
                if os.path.exists(get_head(image,'STARFILE')):
                    doobj=0
            if doobj:
                iraf.iqobjs(image,sigma,satval,skyval="0.0",
                            masksfx=masksfx,wtimage="none",minlim=no,
                            clobber=yes,verbose=verbose)

            # Establish our "region of interest" for pixels
            pixrough=800
            pixminx=1024-float(decoff)/pix-pixrough/2
            pixmaxx=pixminx+pixrough
            pixmaxy=1023
            pixminy=pixmaxy-float(raoff)/pix-(1+saonseq)*saoshift-pixrough/2

            # The array of focus positions (from low Y to high Y)
            farrkeys=[]
            for i in range(saonseq):
                farrkeys.append("SAOFOC%02d" % (i+1))
            farr=get_head(image,farrkeys)

            # Starlist from Sextractor
            starfile=get_head(image,'STARFILE')
            stars=Starlist(starfile)
            stars.mag_sort()

            # Find some of the stars in the sequence
            xvals=[]
            yvals=[]
            good1=[]
            for star in stars:
                # We're going in order from bright to faint
                if star.xval>pixminx and star.xval<pixmaxx and \
                   star.yval<pixmaxy and star.yval>pixminy and \
                   star.elong<3:
                    good1.append(star)
                    xvals.append(star.xval)
                    yvals.append(star.yval)
                    if len(good1)>=1.2*saonseq:
                        break

            # Sanity checking
            if len(xvals)<1:
                print "No stars found in search region"
                update_head(image,'SAOFOCUS',0,
                            "saofocus processing failed for this image")
                continue

            # Guess for x is median of the xvals of the goodstars
            xctr=median(xvals,pick=1)

            # First guess for y is median of the goodstars that fall
            # within +/- (pixfine) pix of this X value
            pixfine=8
            yval2=[]
            good2=[]
            for star in good1:
                if abs(star.xval-xctr)<pixfine:
                    good2.append(star)
                    yval2.append(star.yval)

            # Sanity checking
            if len(yval2)<1:
                print "No stars found in refined search region"
                update_head(image,'SAOFOCUS',0,
                            "saofocus processing failed for this image")
                continue

            yctr=median(yval2,pick=1)

            # This will be our reference star
            usestar=good2[yval2.index(yctr)]
            [refx,refy]=[usestar.xval,usestar.yval]
            
            # Identify additional stars, working out from the
            # reference star
            usestars=[usestar]

            # Search increasing & decreasing in Y
            # (we use a bigger box in Y, and correct for the last star
            # position, because of potential for tracking errors)
            for isgn in [+1,-1]:
                shifty=refy
                for i in range(1,saonseq):
                    shifty += isgn*saoshift
                    tgtstars=stars.starsinbox([[refx-pixfine,refx+pixfine],
                                               [shifty-1.5*pixfine,
                                                shifty+1.5*pixfine]])
                    if len(tgtstars)>0:
                        minmag=99.99
                        # Pick the brightest in the box
                        for tstar in tgtstars:
                            if tstar.mag<minmag:
                                svstar=tstar
                                minmag=tstar.mag
                        usestars.append(svstar)
                        shifty=svstar.yval
                    else:
                        # Quit searching when there are no stars in box
                        break

            # Exclude faint stars if we have too many
            # (could also drop stars until we reach the number, but
            # then we would want to check that all the y-values are in
            # strict succession...)
            magrough=2.0
            if len(usestars)>saonseq:
                usemags=[]
                for star in usestars:
                    usemags.append(star.mag)
                minmag=min(usemags)
                use2=[]
                for star in usestars:
                    if star.mag<minmag+magrough:
                        use2.append(star)
                if len(use2)==saonseq:
                    usestars=use2

            # Check for the right number
            if len(usestars)!=saonseq:
                print "Failed to get right number of target stars"
                update_head(image,'SAOFOCUS',0,
                            "saofocus processing failed for this image")
                continue

            # Construct the array of x- and y-values
            xval3=[]
            yval3=[]
            for star in usestars:
                xval3.append(star.xval)
                yval3.append(star.yval)
            # Choose a single x-value for the run
            xctr2="%.3f" % median(xval3)
            # Increasing y-value means increasing index in farr
            yval3.sort()
            ylis=','.join(map(str,yval3))
            flis=','.join(map(str,farr))

            # Run iqfocus
            iraf.iqfocus(image,xctr2,ylis,flis,method="acorr",
                         boxsize=saoshift,focuskey="BESTFOC",
                         seekey="SEEING",fseekey="SAOSEE",
                         update=yes,clobber=yes,verbose=verbose)

            # Check for successful iqfocus run
            if not check_head(image,'BESTFOC'):
                print "iqfocus run failed to find good focus"
                update_head(image,'SAOFOCUS',0,
                            "saofocus processing failed for this image")
                continue

            # Grab focus positions and seeing values
            bestfoc,saosee=get_head(image,['BESTFOC','SAOSEE'])

            # Some information
            if verbose:
                print "Focus settings:  "+flis
                print "Seeing values:   "+saosee
                print "Best focus:      %.3f" % bestfoc

            # Check for best-focus "near edge" condition
            for i in range(saonseq):
                if ("%.3f" % bestfoc)==("%.3f" % float(farr[i])):
                    ibest=i
                    break
            if (saonseq>3 and (ibest==0 or ibest==saonseq-1)) or \
               (saonseq>5 and (ibest==1 or ibest==saonseq-2)):
                update_head(image,'SAOEDGE',1,
                            "Best seeing is near edge of focus run")

            # Check for insufficient variation across the focus run
            maxsee=0
            minsee=0.5*saoshift
            saoseevals=eval(saosee)
            for seeval in saoseevals:
                minsee=min(minsee,seeval)
                maxsee=max(maxsee,seeval)
            if (maxsee-minsee) < mindiff:
                print "Failed to observe significant variation "+\
                      "across focus run"
                update_head(image,'SAOFOCUS',0,
                            "saofocus processing failed for this image")
                continue

            # Convert seeing value to arcsec
            seepix=get_head(image,'SEEPIX')
            update_head(image,'BESTSEE',seepix,
                        "Best seeing in pixels for this series")
            try:
                seeasec=float(seepix)*pix
                update_head(image,'SEEING',seeasec,
                            "Best seeing in arcsec for this series")
            except:
                print "Failed to convert seeing to arcsec for %s" % image

            # Successful processing
            update_head(image,'SAOFOCUS',1,
                        "saofocus processing succeeded for this image")

        ##############################

        # Test end conditions
        if endhow=="never":
            done=no
        elif endhow=="once":
            done=yes
        elif endhow=="time":
            if time.time()>reftime:
                done=yes
        
        # Wait a little while
        if not done:
            time.sleep(twait)
コード例 #5
0
ファイル: iqnirc2.py プロジェクト: cenko/python
    def flatten(self,qombos,flat=None,flatscale=no):

        if type(qombos) is StringType:
            qombo=qombos
            qombos=[qombo]

        # Loop over input list of qombos
        for qombo in qombos:

            # Check for presence of calibration files
            obj,filt=qombo_split(qombo)
            if not self.check_calib(filt):
                self.calib()

            # Corresponding files
            offiles=self.science_images(qombo)
            if len(offiles)==0:
                continue

            # Make a text ".lis" file to record this
            putlines(qombo+'.lis',offiles,clobber=yes)

            # Rescale flatfield if requested
            inflat=self.flatpre+filt+'.fits'
            outflat=self.flatpre+qombo+'.fits'
            check_exist(outflat,"w",clobber=self.clobber)

            if flatscale:
                nirc_flatscale(offiles,inflat,outflat,clipfrac=0.02,
                               clobber=yes)
            else:
                iraf.imcopy(inflat,outflat,verbose=no)
            check_exist(outflat,"r")

            # Clip flatfield and update BPM
            iraf.iqclip(outflat,lthresh=self.flatlow,
                        hthresh=self.flathigh,bookend=no,
                        replace=1.0,maskin='!BPM',maskval=1,
                        maskout=self.bpmsfx)

            # Rename the updated BPM
            newbpm1=get_head(outflat,'BPM')
            newbpm2=newbpm1.replace(self.flatpre,'')
            check_exist(newbpm2,'w',clobber=self.clobber)
            iraf.imrename(newbpm1,newbpm2,verbose=no)
            update_head(outflat,'BPM',newbpm2)

            # Further preprocessing
            for image in offiles:

                # Keyword updates
                update_head(image,[self.filtset,'BPM'],
                                  [filt,newbpm2])

                # Flatfielding
                iraf.iqflatten(image,outflat,outpfx=self.flatpfx,
                               normflat=no,statsec=self.statsec,
                               subsky=yes,vignflat=no,clipflat=no,
                               clobber=yes,verbose=no)

                # Set bad pixels to zero
                image2=self.flatpfx+image
                iraf.iqmask(image2,mask='!BPM',method='constant',value=0.0,
                            clobber=yes,verbose=no)

                # Write Useful Keywords
                update_head(image2,'PROCESSD',1,
                            "Image has been processed by iqnirc")
                update_head(image2,'PROCVER',version,
                            "Version number of iqnirc used")
                update_head(image2,'ZEROPT','INDEF',
                            "Zero-point relative to 2MASS or INDEF")
                update_head(image2,'PROCPROB','None',
                            "Problems encountered in iqnirc processing")

            # List of flatfielded images
            offiles2=pfx_list(offiles,self.flatpfx)
            oflist2=','.join(offiles2)

            # Object-detection
            iraf.iqobjs(oflist2,self.sigma,self.satval,
                        skyval="!SKYBKG",masksfx=self.masksfx,
                        wtimage="",minlim=no,
                        clobber=yes,verbose=no)

            # Attempt WCS refinement
            iraf.iqwcs(oflist2,objkey=self.objkey,rakey='RA',
                       deckey='DEC',pixscl=self.pix,pixtol=0.05,
                       starfile='!STARFILE',catalog='ir',
                       diffuse=yes,clobber=yes,verbose=self.verbose)

            # Done with Step #3
            update_head(offiles2,'IQWRCSTP',3,
                        "Stage of IQWIRC processing")
コード例 #6
0
ファイル: glob_example.py プロジェクト: rfinn/pythonCode
#! /usr/bin/env python

from pylab import *
from pyraf import iraf
import glob

files = glob.glob('*.fits')

for file in files:
    iraf.imgets(image=file, param='FILENAME')  #get RA of image
    originalName = iraf.imgets.value
    #print file, originalName
    iraf.imrename(file, originalName)

print "All Done!"
コード例 #7
0
ファイル: fastfocus.py プロジェクト: pradipgatkine/python
def fastfocus(inpat,
              prepfile=yes,
              endhow="never",
              endwhen="",
              clobber=globclob,
              verbose=globver):
    """ derive best focus from an SAOFOCUS run on P60 """

    # Defaults
    twait = 30
    reduced = {}
    bpmname = "BPM.pl"
    saokey = "IMGTYPE"
    saore = "SAOFOCUS"
    fseqkey = "SAOFVALS"
    sigma = 10.0
    satval = 50000.0
    masksfx = "mask"
    pix = 0.3787  # arcsec per pixel
    mindiff = 2  # minimum tolerable diff b/ best and worst seeing (pix)

    # Parse end-condition "time"
    if endhow == "time":
        re1 = re.search("^(\d+):(\d+)", endwhen)
        if re1:
            tnow = time.gmtime()
            # Force UT calculation w/ ignoring of DST issues
            reftime = time.mktime([
                tnow[0], tnow[1], tnow[2],
                int(re1.group(1)),
                int(re1.group(2)), 0, tnow[6], tnow[7], 0
            ]) - time.timezone
            if reftime < time.time():
                reftime += 86400
            if verbose:
                print "Running until %s" % \
                      time.strftime("%d %b %H:%M",time.gmtime(reftime))
        else:
            print "Failed to parse %s as UT time" % endwhen
            print "Running until stopped..."
            endhow = "never"

    # Open fake OCS
    fakeocs = ocs_ooriented(TEST=1)
    fakeocs.read_foctable()

    ##################################################

    # Big Loop
    done = no
    while not done:

        # Parse inputs
        allfiles = glob.glob(inpat)

        newfiles = []
        for image in allfiles:
            if not reduced.has_key(image):
                # Interested only in SAOFOCUS images
                saoval = get_head(image, saokey)
                if re.search(saore, saoval, re.I):
                    reduced[image] = no
                    newfiles.append(image)
                else:
                    # Skip
                    reduced[image] = yes

        for image in newfiles:

            if verbose:
                print "Processing new SAOFOCUS image %s" % image

            # Only attempt reduction once for each image
            reduced[image] = yes

            # Demosaic
            ccdproc = iraf.ccdproc
            ccdproc.overscan = yes
            ccdproc.trim = yes
            ccdproc.zerocor = no
            iraf.iqmosaic(image,
                          outpfx="",
                          biassec="!BIASSEC",
                          trimsec="!TRIMSEC",
                          joinkeys="DETSEC",
                          splitkeys="AMPSEC,CCDSEC,TRIM,OVERSCAN",
                          clobber=yes,
                          verbose=verbose)
            # Fix the WCS that IRAF has now clobbered
            iraf.add_wcs(image, instrument="p60new")

            # Get some important keywords
            [raoff,decoff,saonseq,saoshift] = \
                   get_head(image,['RAOFF','DECOFF','NUMFOC',
                                   'SAOSHIFT'])
            # Type conversions
            saonseq = int(saonseq)
            saoshift = float(saoshift)

            # No bias-subtraction or flat-fielding

            # Subtract stair-step background if requested
            if prepfile:
                qtmp = iraf.mktemp("saofoc") + ".fits"
                iraf.imcopy(image, qtmp, verbose=no)
                for iamp in [-1, 1]:
                    for istep in range(saonseq):
                        qsec = iraf.mktemp("saofsc") + ".fits"
                        y1 = 1024 + iamp * (1 + istep * saoshift) + max(
                            0, iamp)
                        y2 = y1 + iamp * (saoshift - 1)
                        if istep == 0:
                            y1 -= iamp
                        elif istep == saonseq - 1:
                            y2 = 1 + max(0, iamp) * 2047
                        ymin = min(y1, y2)
                        ymax = max(y1, y2)
                        statsec = '[*,%d:%d]' % (ymin, ymax)
                        iraf.iterstat(image + statsec,
                                      nsigrej=5,
                                      maxiter=10,
                                      prin=no,
                                      verbose=no)
                        medreg = float(iraf.iterstat.median)
                        iraf.imarith(image + statsec,
                                     '-',
                                     medreg,
                                     qsec,
                                     verbose=no,
                                     noact=no)
                        iraf.imcopy(qsec, qtmp + statsec, verbose=no)
                        iraf.imdel(qsec, verify=no, go_ahead=yes)
                iraf.imdel(image, verify=no, go_ahead=yes)
                iraf.imrename(qtmp, image, verbose=no)

            # No renaming of the file

            # No Bad Pixel Masking or rudimentary WCS

            # Object-detection
            doobj = 1
            if check_head(image, 'STARFILE'):
                if os.path.exists(get_head(image, 'STARFILE')):
                    doobj = 0
            if doobj:
                iraf.iqobjs(image,
                            sigma,
                            satval,
                            skyval="0.0",
                            masksfx=masksfx,
                            wtimage="none",
                            minlim=no,
                            clobber=yes,
                            verbose=verbose)

            # Establish our "region of interest" for pixels
            pixrough = 800
            pixminx = 1024 - float(decoff) / pix - pixrough / 2
            pixmaxx = pixminx + pixrough
            pixmaxy = 1023
            pixminy = pixmaxy - float(raoff) / pix - (
                1 + saonseq) * saoshift - pixrough / 2

            # The array of focus positions (from low Y to high Y)
            farrkeys = []
            for i in range(saonseq):
                farrkeys.append("SAOFOC%02d" % (i + 1))
            farr = get_head(image, farrkeys)

            # Starlist from Sextractor
            starfile = get_head(image, 'STARFILE')
            stars = Starlist(starfile)
            stars.mag_sort()

            # Find some of the stars in the sequence
            xvals = []
            yvals = []
            good1 = []
            for star in stars:
                # We're going in order from bright to faint
                if star.xval>pixminx and star.xval<pixmaxx and \
                   star.yval<pixmaxy and star.yval>pixminy and \
                   star.elong<3:
                    good1.append(star)
                    xvals.append(star.xval)
                    yvals.append(star.yval)
                    if len(good1) >= 1.2 * saonseq:
                        break

            # Sanity checking
            if len(xvals) < 1:
                print "No stars found in search region"
                update_head(image, 'SAOFOCUS', 0,
                            "saofocus processing failed for this image")
                continue

            # Guess for x is median of the xvals of the goodstars
            xctr = median(xvals, pick=1)

            # First guess for y is median of the goodstars that fall
            # within +/- (pixfine) pix of this X value
            #pixfine=10
            pixfine = 20
            yval2 = []
            good2 = []
            for star in good1:
                if abs(star.xval - xctr) < pixfine:
                    good2.append(star)
                    yval2.append(star.yval)

            # Sanity checking
            if len(yval2) < 1:
                print "No stars found in refined search region"
                update_head(image, 'SAOFOCUS', 0,
                            "saofocus processing failed for this image")
                continue

            yctr = median(yval2, pick=1)

            # This will be our reference star
            usestar = good2[yval2.index(yctr)]
            [refx, refy] = [usestar.xval, usestar.yval]

            # Identify additional stars, working out from the
            # reference star
            usestars = [usestar]

            # Search increasing & decreasing in Y
            # (we use a bigger box in Y, and correct for the last star
            # position, because of potential for tracking errors)
            for isgn in [+1, -1]:
                shifty = refy
                for i in range(1, saonseq):
                    shifty += isgn * saoshift
                    tgtstars = stars.starsinbox(
                        [[refx - pixfine, refx + pixfine],
                         [shifty - pixfine, shifty + pixfine]])
                    #[shifty-1.5*pixfine,
                    #shifty+1.5*pixfine]])
                    if len(tgtstars) > 0:
                        minmag = 99.99
                        # Pick the brightest in the box
                        for tstar in tgtstars:
                            if tstar.mag < minmag:
                                svstar = tstar
                                minmag = tstar.mag
                        usestars.append(svstar)
                        shifty = svstar.yval
                    else:
                        # Quit searching when there are no stars in box
                        break

            # Exclude faint stars if we have too many
            # (could also drop stars until we reach the number, but
            # then we would want to check that all the y-values are in
            # strict succession...)
            magrough = 2.0
            if len(usestars) > saonseq:
                usemags = []
                for star in usestars:
                    usemags.append(star.mag)
                minmag = min(usemags)
                use2 = []
                for star in usestars:
                    if star.mag < minmag + magrough:
                        use2.append(star)
                if len(use2) == saonseq:
                    usestars = use2

            # Check for the right number
            if len(usestars) != saonseq:
                print "Failed to get right number of target stars"
                update_head(image, 'SAOFOCUS', 0,
                            "saofocus processing failed for this image")
                continue

            # Construct the array of x- and y-values
            xval3 = []
            yval3 = []
            for star in usestars:
                xval3.append(star.xval)
                yval3.append(star.yval)
            # Choose a single x-value for the run
            xctr2 = "%.3f" % median(xval3)
            # Increasing y-value means increasing index in farr
            yval3.sort()
            ylis = ','.join(map(str, yval3))
            flis = ','.join(map(str, farr))

            # Run iqfocus
            iraf.iqfocus(image,
                         xctr2,
                         ylis,
                         flis,
                         method="acorr",
                         boxsize=saoshift,
                         focuskey="BESTFOC",
                         seekey="SEEPIX",
                         fseekey="SAOSEE",
                         update=yes,
                         clobber=yes,
                         verbose=verbose)

            # Check for successful iqfocus run
            if not check_head(image, 'BESTFOC'):
                print "iqfocus run failed to find good focus"
                update_head(image, 'SAOFOCUS', 0,
                            "saofocus processing failed for this image")
                continue

            # Grab focus positions and seeing values
            bestfoc, saosee = get_head(image, ['BESTFOC', 'SAOSEE'])

            # Some information
            if verbose:
                print "Focus settings:  " + flis
                print "Seeing values:   " + saosee
                print "Best focus:      %.3f" % bestfoc

            # Check for best-focus "near edge" condition
            for i in range(saonseq):
                if ("%.3f" % bestfoc) == ("%.3f" % float(farr[i])):
                    ibest = i
                    break
            if (saonseq>3 and (ibest==0 or ibest==saonseq-1)) or \
               (saonseq>5 and (ibest==1 or ibest==saonseq-2)):
                update_head(image, 'SAOEDGE', 1,
                            "Best seeing is near edge of focus run")

            # Check for insufficient variation across the focus run
            maxsee = 0
            minsee = 0.5 * saoshift
            saoseevals = eval(saosee)
            for seeval in saoseevals:
                minsee = min(minsee, seeval)
                maxsee = max(maxsee, seeval)
            if (maxsee - minsee) < mindiff:
                print "Failed to observe significant variation "+\
                      "across focus run"
                update_head(image, 'SAOFOCUS', 0,
                            "saofocus processing failed for this image")
                continue

            # Convert seeing value to arcsec
            seepix = get_head(image, 'SEEPIX')
            update_head(image, 'BESTSEE', seepix,
                        "Best seeing in pixels for this series")
            try:
                seeasec = float(seepix) * pix
                update_head(image, 'SEEING', seeasec,
                            "Best seeing in arcsec for this series")
            except:
                print "Failed to convert seeing to arcsec for %s" % image

            # Successful processing
            update_head(image, 'SAOFOCUS', 1,
                        "saofocus processing succeeded for this image")

            # Get other necessary keywords
            [filter, btubtemp, airmass] = get_head(image, \
                                           ['FILTER','BTUBTEMP','AIRMASS'])

            # Update focus file
            fakeocs.focus = [filter, bestfoc, float(btubtemp), float(airmass)]
            fakeocs.write_foctable()

        ##############################

        # Test end conditions
        if endhow == "never":
            done = no
        elif endhow == "once":
            done = yes
        elif endhow == "time":
            if time.time() > reftime:
                done = yes

        # Wait a little while
        if not done:
            time.sleep(twait)
コード例 #8
0
ファイル: iqp60.py プロジェクト: pradipgatkine/python
def iqp60(inpat,endhow="never",endwhen="",focmode="all",
          logfile="",clobber=globclob,verbose=globver):

    """ intelligent processing of P60 data files """

    # Defaults
    twait=30
    reduced={}
    filtkey="FILTER"
    biaskey="IMGTYPE"
    biasre="BIAS"
    biasroot="Bias"
    biaspfx="b"
    flatkey="IMGTYPE"
    flatre="DOMEFLAT"
    flatpre="Flat-"
    flatpfx="f"
    bpm0root="BPM0"
    bpmroot="BPM"
    binkey="CCDSUM"
    detseckey="DETSEC"
    binroikey="BINROI"
    binroidef="A"
    nfocus=10
    focuskey="IMGTYPE"
    focusre="^FOCUS"
    fseqkey="FOCUSSEQ"
    skipkey="IMGTYPE"
    skipre="SAOFOCUS"
    statsec=""
    reqkeys=['IMGTYPE','OBJECT','FILTER','BINROI',
             'CCDSUM','DETSEC','ROISEC']
    sigma=3.0
    minraw=0.0
    maxraw=65535.0
    satval=50000.0
    masksfx="mask"
    rawpix=0.3787
    zptkey="ZEROPT"
    zpukey="ZEROPTU"
    # For now:  
    #catmags={'B':'BMAG','V':'BMAG','g':'BMAG',
    #         'R':'RMAG','r':'RMAG','I':'IMAG',
    #         'i':'IMAG','ip':'IMAG','z':'IMAG',
    #         'zp':'IMAG','zl':'IMAG','zs':'IMAG'}
    ## In the future, once Sloan mags are written to the ubcone output:
    catmags={'B':'BMAG','V':'VMAG','g':'GPMAG',
             'R':'RMAG','r':'RPMAG','I':'IMAG',
             'i':'IMAG','ip':'IPMAG','z':'ZPMAG',
             'zp':'ZPMAG','zl':'ZPMAG','zs':'ZPMAG',
             'upr':'UPMAG','gpr':'GPMAG','rpr':'RPMAG',
             'ipr':'IPMAG','zpr':'ZPMAG'}
    exptmkey="EXPTIME"
    exptmstd=60.0
    minzpts={}
    minskys={}
    fwhm0=1.5
    gain=2.3
    readn=7.5
    crsfx="crm"
    statfile="Status_Science.txt"

    # Parse end-condition "time"
    if endhow=="time":
        re1=re.search("^(\d+):(\d+)",endwhen)
        if re1:
            tnow=time.gmtime()
            # Force UT calculation w/ ignoring of DST issues
            reftime=time.mktime([tnow[0],tnow[1],tnow[2],
                                 int(re1.group(1)),int(re1.group(2)),0,
                                 tnow[6],tnow[7],0]) - time.timezone
            if reftime<time.time():
                reftime+=86400
            if verbose:
                print "Running until %s" % \
                      time.strftime("%d %b %H:%M",time.gmtime(reftime))
        else:
            print "Failed to parse %s as UT time" % endwhen
            print "Running until stopped..."
            endhow="never"

    # Parse focmode setting
    fonly=0
    if focmode=="nofocus":
        skipre="FOCUS"
    elif focmode=="focus":
        fonly=1
        skipre="SCIENCE"

    # Parse logfile setting
    if len(logfile)>0:
        check_exist(logfile,'w',yes)
        try:
            log=open(logfile,'w')
            sys.stdout=log
        except:
            print "Failed to open logfile %s for writing" % logfile

    # Setup ccdproc options
    ccdproc=iraf.ccdred.ccdproc
    ccdproc.overscan=yes
    ccdproc.trim=yes
    ccdproc.zerocor=no

    ccdproc.interactive=no
    ccdproc.function="chebyshev"
    ccdproc.order=3
    ccdproc.sample="*"
    ccdproc.naverage=3
    ccdproc.niterate=2
    ccdproc.low_reject=3.0
    ccdproc.high_reject=3.0
    ccdproc.grow=0.0

    # Demosaic all the files
    allfiles=glob.glob(inpat)
    alllist=','.join(allfiles)
    iraf.iqmosaic(alllist,outpfx="",biassec="!BIASSEC",
                  trimsec="!TRIMSEC",joinkeys="CCDSIZE,DETSEC,ROISEC",
                  splitkeys="AMPSEC,CCDSEC,TRIM,OVERSCAN,CCDPROC",
                  clobber=yes,verbose=verbose)
    iraf.add_wcs(alllist,instrument="p60new",binkey=binkey)

    # Set ccdproc params for bias-subtraction only
    ccdproc.overscan=no
    ccdproc.trim=no
    ccdproc.zerocor=yes

    # Determine if we need to make calibration files
    dobias=no
    doflats=no
    
    for image in allfiles:

        # Criteria for bias image
        biasval,flatval=get_head(image,[biaskey,flatkey])
        if re.search(biasre,biasval,re.I):
            dobias=yes
            reduced[image]=yes
        elif re.search(flatre,flatval,re.I):
            doflats=yes
            reduced[image]=yes

    # Calibrations
    if (dobias or doflats) and not fonly:
        iraf.iqcals(alllist,dobias=dobias,biasproc=no,
                    biaskey=biaskey,biasre=biasre,biasroot=biasroot,
                    doflats=doflats,flatproc=yes,flatkey=flatkey,
                    flatre=flatre,filtkey=filtkey,flatpre=flatpre,
                    flatscale="mode",statsec="",normflat=yes,
                    dobpm=no,bpmroot=bpmroot,classkey=binroikey,
                    classdef=binroidef,mosaic=no,
                    clobber=clobber,verbose=verbose)

    # Delete binroi-specific BPMs and flatfields
    oldbpms=glob.glob(bpmroot+'-'+'*.pl')
    for oldbpm in oldbpms:
        iraf.imdel(oldbpm,verify=no,go_ahead=yes)
    oldflats=glob.glob(flatpre+'*-*.fits')
    for oldflat in oldflats:
        iraf.imdel(oldflat,verify=no,go_ahead=yes)

    # Clip all flatfields and update BPM as we go
    bpm0name=bpm0root+'.pl'
    bpmname=bpmroot+'.pl'
    if os.path.exists(bpmname):
        check_exist(bpm0name,'w',clobber=yes)
        iraf.imrename(bpmname,bpm0name,verbose=no)
        allflats=glob.glob(flatpre+'*.fits')
        for flatimg in allflats:
            iraf.iqclip(flatimg,lthresh=0.75,hthresh=1.25,bookend=no,
                        replace=1.0,maskin=bpm0name,maskout=bpmname,
                        maskval=1,clobber=yes,verbose=no)
            iraf.imdel(bpm0name,verify=no,go_ahead=yes)
            iraf.imrename(bpmname,bpm0name,verbose=no)
            update_head(flatimg,"CLIPFLAT",1,"Has flatfield been clipped?")
            update_head(flatimg,"CLIPREPL",1.0,
                        "Replacement value for clipped pixels")
        iraf.imrename(bpm0name,bpmname,verbose=no)

    ##################################################

    # Lists of focusfiles
    allfocus=[]
    focusfiles=[]
    # Should additional calibrations be taken later on...
    biasfiles=[]
    flatfiles=[]

    # Big Loop
    done=no
    while not done:

        # Parse inputs
        allfiles=glob.glob(inpat)

        newfiles=[]
        for image in allfiles:
            if not reduced.has_key(image):
                # Exclude Bias & Flats
                if re.search(biasroot,image,re.I) or \
                   re.search(flatpre,image,re.I):
                    reduced[image]=yes
                    continue
                # Exclude calibration files (for now)
                biasval,flatval=get_head(image,[biaskey,flatkey])
                if re.search(biasre,biasval,re.I):
                    biasfiles.append(image)
                    reduced[image]=yes
                elif re.search(flatre,flatval,re.I):
                    flatfiles.append(image)
                    reduced[image]=yes
                # Exclude "skippable" files
                skipval=get_head(image,skipkey)
                if re.search(skipre,skipval,re.I):
                    reduced[image]=yes
                # Queue file for processing
                else:
                    reduced[image]=no
                    newfiles.append(image)
            elif not reduced[image]:
                # Attempted reduction before, but failed?
                newfiles.append(image)

        if newfiles:

            # Brief pause to avoid file conflicts (?)
            time.sleep(3.0)

            # Demosaic
            newlist=','.join(newfiles)
            ccdproc.overscan=yes
            ccdproc.trim=yes
            ccdproc.zerocor=no
            iraf.iqmosaic(newlist,outpfx="",biassec="!BIASSEC",
                          trimsec="!TRIMSEC",joinkeys="DETSEC",
                          splitkeys="AMPSEC,CCDSEC,TRIM,OVERSCAN",
                          clobber=yes,verbose=verbose)

            iraf.add_wcs(newlist,instrument="p60new",binkey=binkey)

        # Reset ccdproc params for bias-subtraction only
        ccdproc.overscan=no
        ccdproc.biassec=""
        ccdproc.trim=no
        ccdproc.trimsec=""
        ccdproc.zerocor=yes
        ccdproc.flatcor=no

        ##############################

        # Process new images
        for image in newfiles:

            # Required keyword check
            if not check_head(image,reqkeys):
                print "Image %s is missing required keywords" % image
                continue

            # Attempt processing only once per image
            reduced[image]=yes
            if verbose:
                print "Reducing new image %s" % image

            # Track useful status information
            scistat={}

            # Prepare for processing
            pix=rawpix
            binroi=binroidef
            biasname=biasroot+'.fits'
            bpmname=bpmroot+'.pl'
            filt=get_head(image,filtkey)
            flatname=flatpre+filt+".fits"

            # Calibration files for BINROI setting
            if check_head(image,binroikey):
                binroi=get_head(image,binroikey)
                if binroi != binroidef:
                    # Get the right calibration files
                    fullbias=biasname
                    biasname=biasroot+'-'+binroi+'.fits'
                    fullbpm =bpmroot+'.pl'
                    bpmname =bpmroot+'-'+binroi+'.pl'
                    fullflat=flatname
                    flatname=flatpre+filt+'-'+binroi+".fits"
                    # Details of this binning/roi combo
                    [binval,detsec]=get_head(image,[binkey,detseckey])
                    binning=p60parsebin(binval)
                    roi=p60parseroi(detsec)
                    # Pixel scale (no anisotropic binning, sorry)
                    pix=binning[0]*rawpix
                    # Check for existance of bias; make it if we have to
                    if not os.path.exists(biasname):
                        print ("Failed to find bias for %s=%s; "+ \
                               "constructing one from full-frame bias") % \
                              (binroikey,binroi)
                        p60roiflat(fullbias,biasname,binning,roi)
                    # Produce the flatfield if necessary
                    if not os.path.exists(flatname):
                        p60roiflat(fullflat,flatname,binning,roi)
                    # Produce the bad-pixel mask if necessary
                    if not os.path.exists(bpmname):
                        # Get BPM into FITS format
                        bpm1=iraf.mktemp("iqbpm")+".fits"
                        iraf.imcopy(fullbpm,bpm1,verbose=no)
                        # Adjust BPM for binning/ROI
                        bpm2=iraf.mktemp("iqbpm")+".fits"
                        p60roibpm(bpm1,bpm2,binning,roi)
                        # Convert BPM to PL format
                        iraf.imcopy(bpm2,bpmname,verbose=no)
                        # Delete temporary files
                        iraf.imdel(bpm1,verify=no,go_ahead=yes)
                        iraf.imdel(bpm2,verify=no,go_ahead=yes)

            # Bias subtraction
            check_exist(biasname,"r")
            ccdproc.zero=biasname
            image1=biaspfx+image
            check_exist(image1,'w',clobber)
            iraf.ccdproc(image,output=image1,Stdout=1)

            # Flatfielding
            check_exist(flatname,"r")
            iraf.iqflatten(image1,flatname,outpfx=flatpfx,
                           normflat=no,clipflat=no,
                           statsec=statsec,subsky=yes,
                           vignflat=no,clobber=yes,verbose=no)
            imagef=flatpfx+image1

            # Clip (bookend) flattened & sky-subtracted image
            skybkg=float(get_head(image1,'SKYBKG'))
            iraf.iqclip(image1,lthresh=minraw-skybkg,hthresh=maxraw-skybkg,
                        bookend=yes,maskin="",maskout="",clobber=yes,
                        verbose=no)

            # Rename the file, if everything is well-behaved
            reim=re.search('^%s%s(.+[^r])r?\.fits' % (flatpfx,biaspfx),
                           imagef,re.I)
            if reim:
                image2=reim.group(1)+'p.fits'
                check_exist(image2,"w",yes)
                iraf.imrename(imagef,image2,verbose=verbose)
            else:
                image2=imagef

            # Set bad pixels to zero
            update_head(image2,'BPM',bpmname,"Bad Pixel Mask")
            iraf.iqmask(image2,mask='!BPM',method='constant',value='0.0',
                        clobber=yes,verbose=no)

            # Defringing happens later, in bunches

            # Cosmic Ray Rejection
            tmpimg=iraf.mktemp("iqcr")+".fits"
            check_exist("%s_%s.fits" % (image2[:15],crsfx),"w",clobber)
            iraf.lacos_im(image2,tmpimg,"%s_%s.fits" % (image2[:15],crsfx),
                          gain=gain,readn=readn,skyval=skybkg,sigclip=4.5,
                          sigfrac=0.5,objlim=1.0,niter=1)
            iraf.imdel(image2,verify=no,go_ahead=yes)
            iraf.imcopy(tmpimg,image2,verbose=no)
            iraf.imdel(tmpimg,verify=no,go_ahead=yes)

            # Object-detection
            if scistat.has_key("SEEING_%s" % file):
                try:
                    fwhm=float(scistat["SEEING_%s" % file])
                except:
                    fwhm=fwhm0
            else:
                fwhm=fwhm0
            iraf.iqobjs(image2,sigma,satval,skyval="!SKYBKG",masksfx=masksfx,
                        wtimage="",fwhm=fwhm,pix=rawpix,aperture=2*fwhm/rawpix,
                        gain=gain,minlim=no,clobber=yes,verbose=no)

            # Track some status information
            nstars=get_head(image2,'NSTARS')
            scistat['NSTARS']=nstars
            if minskys.has_key(filt):
                minsky=minskys[filt]
                scistat["SKYBKG_%s" % filt]=skybkg/minsky
            
            # Is it a focus frame?
            focusval=get_head(image2,focuskey)

            # Focus frame actions
            if re.search(focusre,focusval,re.I):

                focusfiles.append(image2)
                # Find its place in the sequence
                focusseq=get_head(image2,fseqkey)
                ref=re.search("(\d+)of(\d+)",focusseq,re.I)
                if ref:
                    ifocus=int(ref.group(1))
                    nfocus=int(ref.group(2))

            # Non-focus frame actions
            else:

                # Make a new keyword for mosaic objects
                objkey='OBJECT'
                if check_head(image2,'MSCSIZE'):
                    mscsize=get_head(image2,'MSCSIZE')
                    mscdims=mscsize.split(',')
                    if int(mscdims[0])>1 or int(mscdims[1])>1:
                        object=get_head(image2,'OBJECT')
                        mscposn=get_head(image2,'MSCPOSN')
                        objmsc=object+'-'+mscposn
                        update_head(image2,'OBJMSC',objmsc,
                                    'Object Name/Mosaic Position')
                        objkey='OBJMSC'

                # Refine WCS (also calculates seeing in arcsec)
                iraf.iqwcs(image2,objkey=objkey,rakey='RA',
                           deckey='DEC',pixtol=0.05,starfile='!STARFILE',
                           nstar=40,catalog='web',ubhost="localhost",
                           diffuse=yes,clobber=yes,verbose=verbose,
                           nstarmax=40)

                # Retry if unsuccessful
                if not check_head(image2,'IQWCS'):
                    iraf.iqwcs(image2,objkey=objkey,rakey='RA',
                               deckey='DEC',pixtol=0.05,starfile="!STARFILE",
                               nstar=40,catalog='web',ubhost="localhost",
                               diffuse=yes,clobber=yes,verbose=verbose,
                               nstarmax=1000)

                # Calculate further quantities if WCS was successful
                extinct='INDEF'
                if check_head(image2,'IQWCS'):

                    scistat['WCSGOOD'] = 1
                    scistat["SEEING_%s" % filt]=get_head(image2,'SEEING') 

                    [rapt,dcpt,lenx,leny]=get_head(image2,
                                            ['RA','DEC','NAXIS1','NAXIS2'])
                    [[ractr,dcctr]]=impix2wcs(image2,0.5*(1+float(lenx)),
                                            0.5*(1+float(leny)))
                    coopt=astrocoords(rapt,dcpt)
                    cooctr=astrocoords(ractr,dcctr)
                    [offra,offdc]=radecdiff(coopt.radeg(),coopt.dcdeg(),
                                            cooctr.radeg(),cooctr.dcdeg())
                    update_head(image2,'PTOFFSET',"%.2f %.2f" % \
                                (offra,offdc),
                                "Offset from nominal pointing (arcsec)")

                    scistat['PTOFFSET']="%.2f,%.2f" % (offra,offdc)

                    # Get zero-point against SDSS/NOMAD/USNO-B 
                    if catmags.has_key(filt):
                        
                        if check_head(image2,'OBJMSC'):
                            object=get_head(image2,'OBJMSC')
                        else:
                            object=get_head(image2,'OBJECT')
                        zpcat=""

                        # See if attempt already made for SDSS
                        if not (os.path.exists("%s.sdss" % object)):
                            getsdss(image2, "%s.sdss" % object)

                        # If successful, set to SDSS
                        sdssfile=open("%s.sdss" % object)
                        if (len(sdssfile.readlines())>2):
                            catalog="%s.sdss" % object
                            zpcat="SDSS"

                        # If SDSS unsuccessful, update USNO-B 
                        if not zpcat:
                            update_usnob("%s.cat" % object, 
                                         outfile="%s.reg" % object)
                            catalog="%s.reg" % object
                            zpcat="USNO-B"
                       
                        # Update image header
                        update_head(image2,"ZPCAT",zpcat)

                        # Run the zeropoint calculation
                        iraf.iqzeropt(image2,catmags[filt],
                                      starfile="!STARFILE",catalog=catalog,
                                      pixtol=3.0,useflags=yes,maxnum=50,
                                      method="mean",rejout=1,fencelim=0.50,
                                      sigma=1.5,maxfrac=0.25,
                                      zptkey=zptkey,zpukey=zpukey,
                                      clobber=yes,verbose=verbose)

                        # Use that to get an extinction estimate
                        zeropt,zeroptu,exptime=get_head(image2,[zptkey,
                                                        zpukey,exptmkey])
                        zeropt=float(zeropt)
                        zeroptu=float(zeroptu)
                        exptime=float(exptime)

                        scistat["ZEROPT_%s" % filt] = \
                            zeropt - 2.5*math.log10(exptime/exptmstd)
                        
                        if minzpts.has_key(filt):
                            extinct = zeropt - \
                                      2.5*math.log10(exptime/exptmstd) - \
                                      minzpts[filt]
                            scistat["EXTINCT_%s" % filt]=extinct

                # Fix the SEEING keyword if WCS fit was not successful
                else:
                    scistat['WCSGOOD'] = 0

                    try:
                        seepix=float(get_head(image2,'SEEPIX'))
                        seeing= "%.3f" % (pix*seepix)
                    except:
                        seeing='INDEF'

                    update_head(image2,'SEEING',seeing,
                                "Estimated seeing in arcsec or INDEF")

            # Write Archive Keywords
            update_head(image2,'PROCESSD',1,
                        "Image has been processed by iqp60")
            update_head(image2,'PROCVER',version,
                        "Version number of iqp60 used")
            update_head(image2,'EXTINCT',extinct,
                        "Estimated mags extinction or INDEF")
            update_head(image2,'PROCPROB','None',
                        "Problems encountered in iqp60 processing")

            # Update status file
            p60status(statfile,image2,scistat)

            # Clean up
            check_exist(image1,"w",yes)

        ##############################

        # If a set of focus images have been processed, find best focus
        if len(focusfiles)>=nfocus:

            print "%d focus images have been processed" % len(focusfiles)

            # Pick a starfile
            focusfiles.sort()
            medfile=focusfiles[len(focusfiles)/2]
            starfile=get_head(medfile,'STARFILE')

            # Update seeing values in all images based on good
            # stars in the starfile.   
            ffiles=','.join(focusfiles)
            iraf.iqseeing(ffiles,stars=starfile,
                          seekey="SEEING",method="acorr",useflags=yes,
                          skipstars=0,usestars=10,boxsize=64,
                          strictbox=no,imgsec="[940:1980,64:1980]",
                          update=yes,clobber=yes,verbose=no)

            # Grab focus positions and seeing values
            focx=[]
            focy=[]
            for image in focusfiles:
                [fpos,seep]=list2float(get_head(image,['FOCUSPOS','SEEING']))
                fpos=float(fpos)
                seep=float(seep)
                focx.append(fpos)
                pix=rawpix
                if check_head(image,binkey):
                    binval=get_head(image,binkey)
                    binels=binval.split()
                    binning=[int(binels[0]),int(binels[1])]
                    # Let's hope we don't ever do asymmetric binning
                    pix=binning[0]*rawpix
                focy.append(pix*seep)
                update_head(image,'SEEING',pix*seep,
                            "Estimated seeing in arcsec")

            # Choose best-seeing as minimum of these
            bestsee=min(focy)
            bestfoc=focx[focy.index(bestsee)]

            # This was too much verbosity for Brad
            if verbose and 0:
                print "Focus settings:  "+liststr(focx,"%.2f")
                print "Seeing values:   "+liststr(focy,"%.1f")
                print
                print "Best focus:  %.2f" % bestfoc

            # Expunge the focusfiles list
            allfocus.append(focusfiles)
            focusfiles=[]

        ##############################

        # Test end conditions
        if endhow=="never":
            done=no
        elif endhow=="once":
            done=yes
        elif endhow=="time":
            if time.time()>reftime:
                done=yes
        
        # Wait a little while
        if not done:
            time.sleep(twait)
コード例 #9
0
def floydsautoredu(files,
                   _interactive,
                   _dobias,
                   _doflat,
                   _listflat,
                   _listbias,
                   _listarc,
                   _cosmic,
                   _ext_trace,
                   _dispersionline,
                   liststandard,
                   listatmo,
                   _automaticex,
                   _classify=False,
                   _verbose=False,
                   smooth=1,
                   fringing=1):
    import floyds
    import string, re, os, glob, sys, pickle
    from numpy import array, arange, mean, pi, arccos, sin, cos, argmin
    import pyfits
    from pyraf import iraf
    import datetime
    os.environ["PYRAF_BETA_STATUS"] = "1"
    iraf.set(direc=floyds.__path__[0] + '/')
    _extinctdir = 'direc$standard/extinction/'
    _tel = floyds.util.readkey3(
        floyds.util.readhdr(re.sub('\n', '', files[0])), 'TELID')
    if _tel == 'fts':
        _extinction = 'ssoextinct.dat'
        _observatory = 'sso'
    elif _tel == 'ftn':
        _extinction = 'maua.dat'
        _observatory = 'cfht'
    else:
        sys.exit('ERROR: observatory not recognised')
    dv = floyds.util.dvex()
    scal = pi / 180.
    iraf.noao(_doprint=0)
    iraf.imred(_doprint=0)
    iraf.ccdred(_doprint=0)
    iraf.twodspec(_doprint=0)
    iraf.longslit(_doprint=0)
    iraf.specred(_doprint=0)
    toforget = ['ccdred.flatcombine','ccdred.zerocombine','ccdproc','specred.apall','longslit.identify','longslit.reidentify',\
                    'specred.standard','longslit.fitcoords','specred.transform','specred.response']
    for t in toforget:
        iraf.unlearn(t)
    iraf.longslit.dispaxi = 2
    iraf.longslit.mode = 'h'
    iraf.identify.fwidth = 7
    iraf.identify.order = 2
    iraf.specred.dispaxi = 2
    iraf.specred.mode = 'h'
    iraf.ccdproc.darkcor = 'no'
    iraf.ccdproc.fixpix = 'no'
    iraf.ccdproc.trim = 'no'
    iraf.ccdproc.flatcor = 'no'
    iraf.ccdproc.overscan = 'no'
    iraf.ccdproc.zerocor = 'no'
    iraf.ccdproc.biassec = ''
    iraf.ccdproc.ccdtype = ''
    iraf.ccdred.instrument = "/dev/null"
    if _verbose:
        iraf.ccdred.verbose = 'yes'
        iraf.specred.verbose = 'yes'
    else:
        iraf.specred.verbose = 'no'
        iraf.ccdred.verbose = 'no'
    now = datetime.datetime.now()
    datenow = now.strftime('20%y%m%d%H%M')
    MJDtoday = 55928 + (datetime.date.today() -
                        datetime.date(2012, 01, 01)).days
    outputlist = []
    hdra = floyds.util.readhdr(re.sub('\n', '', files[0]))
    _gain = floyds.util.readkey3(hdra, 'gain')
    _rdnoise = floyds.util.readkey3(hdra, 'ron')
    std, rastd, decstd, magstd = floyds.util.readstandard(
        'standard_floyds_mab.txt')
    _naxis2 = hdra.get('NAXIS2')
    _naxis1 = hdra.get('NAXIS1')
    if not _naxis1: _naxis1 = 2079
    if not _naxis2:
        if not hdr0.get('HDRVER'): _naxis1 = 511
        else: _naxis1 = 512
    _overscan = '[2049:' + str(_naxis1) + ',1:' + str(_naxis2) + ']'
    _biassecblu = '[380:2048,325:' + str(_naxis2) + ']'
    _biassecred = '[1:1800,1:350]'
    lista = {}
    objectlist = {}
    biaslist = {}
    flatlist = {}
    flatlistd = {}
    arclist = {}
    max_length = 14
    for img in files:
        hdr0 = floyds.util.readhdr(img)
        if floyds.util.readkey3(hdr0, 'naxis2') >= 500:
            if 'blu' not in lista: lista['blu'] = []
            if 'red' not in lista: lista['red'] = []
            _object0 = floyds.util.readkey3(hdr0, 'object')
            _object0 = re.sub(':', '', _object0)  # colon
            _object0 = re.sub('/', '', _object0)  # slash
            _object0 = re.sub('\s', '', _object0)  # any whitespace
            _object0 = re.sub('\(', '', _object0)  # open parenthesis
            _object0 = re.sub('\[', '', _object0)  # open square bracket
            _object0 = re.sub('\)', '', _object0)  # close parenthesis
            _object0 = re.sub('\]', '', _object0)  # close square bracket
            if len(_object0) > max_length:
                _object0 = _object0[:max_length]
            _date0 = floyds.util.readkey3(hdr0, 'date-night')
            _tel = floyds.util.readkey3(hdr0, 'TELID')
            _type = floyds.util.readkey3(hdr0, 'OBSTYPE')
            if not _type: _type = floyds.util.readkey3(hdr0, 'imagetyp')
            _slit = floyds.util.readkey3(hdr0, 'slit')
            if _type:
                _type = _type.lower()
                if _type in ['sky', 'spectrum', 'expose']:
                    nameoutb = str(_object0) + '_' + _tel + '_' + str(
                        _date0) + '_blue_' + str(_slit) + '_' + str(MJDtoday)
                    nameoutr = str(_object0) + '_' + _tel + '_' + str(
                        _date0) + '_red_' + str(_slit) + '_' + str(MJDtoday)
                elif _type in ['lamp', 'arc', 'l']:
                    nameoutb = 'arc_' + str(_object0) + '_' + _tel + '_' + str(
                        _date0) + '_blue_' + str(_slit) + '_' + str(MJDtoday)
                    nameoutr = 'arc_' + str(_object0) + '_' + _tel + '_' + str(
                        _date0) + '_red_' + str(_slit) + '_' + str(MJDtoday)
                elif _type in ['flat', 'f', 'lampflat', 'lamp-flat']:
                    nameoutb = 'flat_' + str(_object0) + '_' + _tel + '_' + str(
                        _date0) + '_blue_' + str(_slit) + '_' + str(MJDtoday)
                    nameoutr = 'flat_' + str(_object0) + '_' + _tel + '_' + str(
                        _date0) + '_red_' + str(_slit) + '_' + str(MJDtoday)
                else:
                    nameoutb = str(_type.lower(
                    )) + '_' + str(_object0) + '_' + _tel + '_' + str(
                        _date0) + '_blue_' + str(_slit) + '_' + str(MJDtoday)
                    nameoutr = str(_type.lower(
                    )) + '_' + str(_object0) + '_' + _tel + '_' + str(
                        _date0) + '_red_' + str(_slit) + '_' + str(MJDtoday)

                bimg = floyds.util.name_duplicate(img, nameoutb, '')
                rimg = floyds.util.name_duplicate(img, nameoutr, '')
                ####
                floyds.util.delete(bimg)
                floyds.util.delete(rimg)
                iraf.imcopy(img, bimg, verbose='no')
                iraf.imcopy(img, rimg, verbose='no')

                aaa = iraf.hedit(bimg,
                                 'CCDSEC',
                                 delete='yes',
                                 update='yes',
                                 verify='no',
                                 Stdout=1)
                aaa = iraf.hedit(bimg,
                                 'TRIMSEC',
                                 delete='yes',
                                 update='yes',
                                 verify='no',
                                 Stdout=1)
                aaa = iraf.hedit(rimg,
                                 'CCDSEC',
                                 delete='yes',
                                 update='yes',
                                 verify='no',
                                 Stdout=1)
                aaa = iraf.hedit(rimg,
                                 'TRIMSEC',
                                 delete='yes',
                                 update='yes',
                                 verify='no',
                                 Stdout=1)

                iraf.ccdproc(bimg,output='', overscan="yes", trim="yes", zerocor='no', flatcor='no', zero='', ccdtype='',\
                                 fixpix='no', trimsec=_biassecblu, biassec=_overscan, readaxi='line', Stdout=1)
                iraf.ccdproc(rimg,output='', overscan="yes", trim="yes", zerocor='no', flatcor='no', zero='', ccdtype='',\
                                 fixpix='no', trimsec=_biassecred, biassec=_overscan, readaxi='line', Stdout=1)
                floyds.util.updateheader(bimg, 0,
                                         {'GRISM': ['blu', ' blue order']})
                floyds.util.updateheader(rimg, 0,
                                         {'GRISM': ['red', ' blue order']})
                floyds.util.updateheader(
                    bimg, 0, {'arcfile': [img, 'file name in the archive']})
                floyds.util.updateheader(
                    rimg, 0, {'arcfile': [img, 'file name in the archive']})
                lista['blu'].append(bimg)
                lista['red'].append(rimg)
            else:
                print 'warning type not defined'
    for arm in lista.keys():
        for img in lista[arm]:
            print img
            hdr = floyds.util.readhdr(img)
            _type = floyds.util.readkey3(hdr, 'OBSTYPE')
            if _type == 'EXPOSE':
                _type = floyds.util.readkey3(hdr, 'imagetyp')
                if not _type: _type = 'EXPOSE'

            if _type == 'EXPOSE':
                print 'warning obstype still EXSPOSE, are this old data ?  run manually floydsfixheader'

            _slit = floyds.util.readkey3(hdr, 'slit')
            _grpid = floyds.util.readkey3(hdr, 'grpid')
            if _type.lower() in ['flat', 'f', 'lamp-flat', 'lampflat']:
                if (arm, _slit) not in flatlist: flatlist[arm, _slit] = {}
                if _grpid not in flatlist[arm, _slit]:
                    flatlist[arm, _slit][_grpid] = [img]
                else:
                    flatlist[arm, _slit][_grpid].append(img)
            elif _type.lower() in ['lamp', 'l', 'arc']:
                if (arm, _slit) not in arclist: arclist[arm, _slit] = {}
                if _grpid not in arclist[arm, _slit]:
                    arclist[arm, _slit][_grpid] = [img]
                else:
                    arclist[arm, _slit][_grpid].append(img)
            elif _type in ['bias', 'b']:
                if arm not in biaslist: biaslist[arm] = []
                biaslist[arm].append(img)
            elif _type.lower() in ['sky', 's', 'spectrum']:
                try:
                    _ra = float(floyds.util.readkey3(hdr, 'RA'))
                    _dec = float(floyds.util.readkey3(hdr, 'DEC'))
                except:
                    ra00 = string.split(floyds.util.readkey3(hdr, 'RA'), ':')
                    ra0, ra1, ra2 = float(ra00[0]), float(ra00[1]), float(
                        ra00[2])
                    _ra = ((ra2 / 60. + ra1) / 60. + ra0) * 15.
                    dec00 = string.split(floyds.util.readkey3(hdr, 'DEC'), ':')
                    dec0, dec1, dec2 = float(dec00[0]), float(dec00[1]), float(
                        dec00[2])
                    if '-' in str(dec0):
                        _dec = (-1) * ((dec2 / 60. + dec1) / 60. +
                                       ((-1) * dec0))
                    else:
                        _dec = (dec2 / 60. + dec1) / 60. + dec0
                dd = arccos(
                    sin(_dec * scal) * sin(decstd * scal) +
                    cos(_dec * scal) * cos(decstd * scal) * cos(
                        (_ra - rastd) * scal)) * ((180 / pi) * 3600)
                if _verbose:
                    print _ra, _dec
                    print std[argmin(dd)], min(dd)
                if min(dd) < 5200: _typeobj = 'std'
                else: _typeobj = 'obj'
                if min(dd) < 5200:
                    floyds.util.updateheader(
                        img, 0, {'stdname': [std[argmin(dd)], '']})
                    floyds.util.updateheader(
                        img, 0, {'magstd': [float(magstd[argmin(dd)]), '']})
                if _typeobj not in objectlist: objectlist[_typeobj] = {}

                if (arm, _slit) not in objectlist[_typeobj]:
                    objectlist[_typeobj][arm, _slit] = [img]
                else:
                    objectlist[_typeobj][arm, _slit].append(img)
    if _verbose:
        print 'object'
        print objectlist
        print 'flat'
        print flatlist
        print 'bias'
        print biaslist
        print 'arc'
        print arclist

    if liststandard and 'std' in objectlist.keys():
        print 'external standard, raw standard not used'
        del objectlist['std']

    sens = {}
    outputfile = {}
    atmo = {}
    for tpe in objectlist:
        if tpe not in outputfile: outputfile[tpe] = {}
        for setup in objectlist[tpe]:
            if setup not in sens: sens[setup] = []
            print '\n### setup= ', setup, '\n### objects= ', objectlist[tpe][
                setup], '\n'
            for img in objectlist[tpe][setup]:
                print '\n\n### next object= ', img, ' ', floyds.util.readkey3(
                    floyds.util.readhdr(img), 'object'), '\n'
                hdr = floyds.util.readhdr(img)
                archfile = floyds.util.readkey3(hdr, 'arcfile')
                _gain = floyds.util.readkey3(hdr, 'gain')
                _rdnoise = floyds.util.readkey3(hdr, 'ron')
                _grism = floyds.util.readkey3(hdr, 'grism')
                _grpid = floyds.util.readkey3(hdr, 'grpid')
                if archfile not in outputfile[tpe]:
                    outputfile[tpe][archfile] = []
                #####################      flat   ###############
                if _listflat: flatgood = _listflat  # flat list from reducer
                elif setup in flatlist:
                    if _grpid in flatlist[setup]:
                        print '\n###FLAT WITH SAME GRPID'
                        flatgood = flatlist[setup][
                            _grpid]  # flat in the  raw data
                    else:
                        flatgood = []
                        for _grpid0 in flatlist[setup].keys():
                            for ii in flatlist[setup][_grpid0]:
                                flatgood.append(ii)
                else:
                    flatgood = []
                if len(flatgood) != 0:
                    if len(flatgood) > 1:
                        f = open('_oflatlist', 'w')
                        for fimg in flatgood:
                            print fimg
                            f.write(fimg + '\n')
                        f.close()
                        floyds.util.delete('flat' + img)
                        iraf.ccdred.flatcombine('"@_oflatlist"',
                                                output='flat' + img,
                                                combine='average',
                                                reject='none',
                                                ccdtype=' ',
                                                rdnoise=_rdnoise,
                                                gain=_gain,
                                                process='no',
                                                Stdout=1)
                        floyds.util.delete('_oflatlist')
                        flatfile = 'flat' + img
                    elif len(flatgood) == 1:
                        os.system('cp ' + flatgood[0] + ' flat' + img)
                        flatfile = 'flat' + img
                else:
                    flatfile = ''
                ##########################   find arcfile            #######################
                arcfile = ''
                if _listarc:
                    arcfile = [floyds.util.searcharc(img, _listarc)[0]
                               ][0]  # take arc from list
                if not arcfile and setup in arclist.keys():
                    if _grpid in arclist[setup]:
                        print '\n###ARC WITH SAME GRPID'
                        arcfile = arclist[setup][
                            _grpid]  # flat in the  raw data
                    else:
                        arcfile = []
                        for _grpid0 in arclist[setup].keys():
                            for ii in arclist[setup][_grpid0]:
                                arcfile.append(ii)
                if arcfile:
                    if len(arcfile) > 1:  # more than one arc available
                        print arcfile
                        #                     _arcclose=floyds.util.searcharc(imgex,arcfile)[0]   # take the closest in time
                        _arcclose = floyds.sortbyJD(arcfile)[
                            -1]  #  take the last arc of the sequence
                        if _interactive.upper() in ['YES', 'Y']:
                            for ii in floyds.floydsspecdef.sortbyJD(arcfile):
                                print '\n### ', ii
                            arcfile = raw_input(
                                '\n### more than one arcfile available, which one to use ['
                                + str(_arcclose) + '] ? ')
                            if not arcfile: arcfile = _arcclose
                        else: arcfile = _arcclose
                    else: arcfile = arcfile[0]
                else: print '\n### Warning: no arc found'

                ###################################################################   rectify
                if setup[0] == 'red':
                    fcfile = floyds.__path__[
                        0] + '/standard/ident/fcrectify_' + _tel + '_red'
                    fcfile1 = floyds.__path__[
                        0] + '/standard/ident/fcrectify1_' + _tel + '_red'
                    print fcfile
                else:
                    fcfile = floyds.__path__[
                        0] + '/standard/ident/fcrectify_' + _tel + '_blue'
                    fcfile1 = floyds.__path__[
                        0] + '/standard/ident/fcrectify1_' + _tel + '_blue'
                    print fcfile
                print img, arcfile, flatfile
                img0 = img
                if img and not img in outputfile[tpe][archfile]:
                    outputfile[tpe][archfile].append(img)
                if arcfile and arcfile not in outputfile[tpe][archfile]:
                    outputfile[tpe][archfile].append(arcfile)
                if flatfile and flatfile not in outputfile[tpe][archfile]:
                    outputfile[tpe][archfile].append(flatfile)

                img, arcfile, flatfile = floyds.floydsspecdef.rectifyspectrum(
                    img, arcfile, flatfile, fcfile, fcfile1, 'no', _cosmic)
                print outputfile
                if img and not img in outputfile[tpe][archfile]:
                    outputfile[tpe][archfile].append(img)
                if arcfile and arcfile not in outputfile[tpe][archfile]:
                    outputfile[tpe][archfile].append(arcfile)
                if flatfile and flatfile not in outputfile[tpe][archfile]:
                    outputfile[tpe][archfile].append(flatfile)
                print outputfile
                ###################################################################         check wavecalib
                if tpe == 'std' or floyds.util.readkey3(
                        floyds.util.readhdr(img), 'exptime') < 300:
                    if setup[0] == 'red':
                        print '\n### check standard wave calib'
                        #print img
                        data, hdr = pyfits.getdata(img, 0, header=True)
                        y = data.mean(1)
                        import numpy as np
                        if np.argmax(y) < 80 and np.argmax(y) > 15:
                            y2 = data[np.argmax(y) - 3:np.argmax(y) +
                                      3].mean(0)
                            yy2 = data[np.argmax(y) - 9:np.argmax(y) -
                                       3].mean(0)
                            floyds.util.delete('_std.fits')
                            pyfits.writeto('_std.fits', np.float32(y2 - yy2),
                                           hdr)
                            #print '_std.fits',_interactive
                            shift = floyds.floydsspecdef.checkwavestd(
                                '_std.fits', _interactive, 2)
                            zro = hdr['CRVAL1']
                            floyds.util.updateheader(
                                img, 0, {'CRVAL1': [zro + int(shift), '']})
                            floyds.util.updateheader(
                                img, 0, {'shift': [float(shift), '']})
                            floyds.util.delete('_std.fits')
                        else:
                            print 'object not found'
                    else:
                        print '\n### warning check in wavelength not possible for short exposure in the blu range '
                else:
                    print '\n### check object wave calib'
                    _skyfile = floyds.__path__[
                        0] + '/standard/ident/sky_' + setup[0] + '.fits'
                    data, hdr = pyfits.getdata(img, 0, header=True)
                    y = data.mean(1)
                    import numpy as np
                    if np.argmax(y) < 80 and np.argmax(y) > 15:
                        yy1 = data[10:np.argmax(y) - 9].mean(0)
                        yy2 = data[np.argmax(y) + 9:-10].mean(0)
                        floyds.util.delete('_sky.fits')
                        pyfits.writeto('_sky.fits', np.float32(yy1 + yy2), hdr)
                        shift = floyds.floydsspecdef.checkwavelength_obj(
                            '_sky.fits', _skyfile, _interactive, 2)
                        floyds.util.delete('_sky.fits')
                        zro = hdr['CRVAL1']
                        floyds.util.updateheader(
                            img, 0, {'CRVAL1': [zro + int(shift), '']})
                        floyds.util.updateheader(img, 0,
                                                 {'shift': [float(shift), '']})
                    else:
                        print 'object not found'
####################################################     flat field
                if img and flatfile and setup[0] == 'red':
                    imgn = 'n' + img
                    hdr1 = floyds.readhdr(img)
                    hdr2 = floyds.readhdr(flatfile)
                    _grpid1 = floyds.util.readkey3(hdr1, 'grpid')
                    _grpid2 = floyds.util.readkey3(hdr2, 'grpid')
                    if _grpid1 == _grpid2:
                        print flatfile, img, setup[0]
                        imgn = floyds.fringing_classicmethod2(
                            flatfile, img, 'no', '*', 15, setup[0])
                    else:
                        print 'Warning flat not the same OB'
                        imgex = floyds.floydsspecdef.extractspectrum(
                            img,
                            dv,
                            _ext_trace,
                            _dispersionline,
                            _interactive,
                            tpe,
                            automaticex=_automaticex)
                        floyds.delete('flat' + imgex)
                        iraf.specred.apsum(flatfile,output='flat'+imgex,referen=img,interac='no',find='no',recente='no',resize='no',\
                                           edit='no',trace='no',fittrac='no',extract='yes',extras='no',review='no',backgro='none')
                        fringingmask = floyds.normflat('flat' + imgex)
                        print '\n### fringing correction'
                        print imgex, fringingmask
                        imgex, scale, shift = floyds.correctfringing_auto(
                            imgex, fringingmask)  #  automatic correction
                        shift = int(
                            .5 + float(shift) /
                            3.5)  # shift from correctfringing_auto in Angstrom
                        print '\n##### flat scaling: ', str(scale), str(shift)
                        ########################################################
                        datax, hdrx = pyfits.getdata(flatfile, 0, header=True)
                        xdim = hdrx['NAXIS1']
                        ydim = hdrx['NAXIS2']
                        iraf.specred.apedit.nsum = 15
                        iraf.specred.apedit.width = 100.
                        iraf.specred.apedit.line = 1024
                        iraf.specred.apfind.minsep = 20.
                        iraf.specred.apfind.maxsep = 1000.
                        iraf.specred.apresize.bkg = 'no'
                        iraf.specred.apresize.ylevel = 0.5
                        iraf.specred.aptrace.nsum = 10
                        iraf.specred.aptrace.step = 10
                        iraf.specred.aptrace.nlost = 10
                        floyds.util.delete('n' + flatfile)
                        floyds.util.delete('norm.fits')
                        floyds.util.delete('n' + img)
                        floyds.util.delete(re.sub('.fits', 'c.fits', flatfile))
                        iraf.imcopy(flatfile + '[500:' + str(xdim) + ',*]',
                                    re.sub('.fits', 'c.fits', flatfile),
                                    verbose='no')
                        iraf.imarith(flatfile,
                                     '/',
                                     flatfile,
                                     'norm.fits',
                                     verbose='no')
                        flatfile = re.sub('.fits', 'c.fits', flatfile)
                        floyds.util.delete('n' + flatfile)
                        iraf.unlearn(iraf.specred.apflatten)
                        floyds.floydsspecdef.aperture(flatfile)
                        iraf.specred.apflatten(flatfile,output='n'+flatfile,interac=_interactive,find='no',recenter='no', resize='no',edit='no',trace='no',\
                                               fittrac='no',fitspec='no', flatten='yes', aperture='',\
                                               pfit='fit2d',clean='no',function='legendre',order=15,sample = '*', mode='ql')
                        iraf.imcopy('n' + flatfile,
                                    'norm.fits[500:' + str(xdim) + ',*]',
                                    verbose='no')
                        floyds.util.delete('n' + flatfile)
                        floyds.util.delete('n' + img)
                        iraf.imrename('norm.fits',
                                      'n' + flatfile,
                                      verbose='no')
                        imgn = floyds.floydsspecdef.applyflat(
                            img, 'n' + flatfile, 'n' + img, scale, shift)
                else:
                    imgn = ''

                if imgn and not imgn in outputfile[tpe][archfile]:
                    outputfile[tpe][archfile].append(imgn)

                ###################################################      2D flux calib
                print '####### ' + imgn
                hdr = floyds.util.readhdr(img)
                _sens = ''
                if liststandard:
                    _sens = floyds.util.searchsens(
                        img,
                        liststandard)[0]  # search in the list from reducer
                if not _sens:
                    try:
                        _sens = floyds.util.searchsens(
                            img, sens[setup])[0]  # search in the reduced data
                    except:
                        _sens = floyds.util.searchsens(
                            img, '')[0]  # search in tha archive
                if _sens:
                    if _sens[0] == '/':
                        os.system('cp ' + _sens + ' .')
                        _sens = string.split(_sens, '/')[-1]
                    imgd = fluxcalib2d(img, _sens)
                    if imgn: imgdn = fluxcalib2d(imgn, _sens)
                    else: imgdn = ''
                    if _sens not in outputfile[tpe][archfile]:
                        outputfile[tpe][archfile].append(_sens)
                    else:
                        imgdn = ''
                    print '\n### do 2D calibration'
                else:
                    imgd = ''
                    imgdn = ''
################    extraction         ####################################
                if imgdn:
                    try:
                        imgdnex = floyds.floydsspecdef.extractspectrum(
                            imgdn,
                            dv,
                            _ext_trace,
                            _dispersionline,
                            _interactive,
                            tpe,
                            automaticex=_automaticex)
                    except:
                        imgdnex = ''
                else:
                    imgdnex = ''
                if imgd:
                    try:
                        imgdex = floyds.floydsspecdef.extractspectrum(
                            imgd,
                            dv,
                            _ext_trace,
                            _dispersionline,
                            _interactive,
                            tpe,
                            automaticex=_automaticex)
                    except:
                        imgdex = ''
                else:
                    imgdex = ''
                if imgd and not imgd in outputfile[tpe][archfile]:
                    outputfile[tpe][archfile].append(imgd)
                if imgdn and not imgdn in outputfile[tpe][archfile]:
                    outputfile[tpe][archfile].append(imgdn)
                if imgdnex and imgdnex not in outputfile[tpe][archfile]:
                    outputfile[tpe][archfile].append(imgdnex)
                if imgdex and imgdex not in outputfile[tpe][archfile]:
                    outputfile[tpe][archfile].append(imgdex)
                if tpe == 'std':
                    if imgn:
                        try:
                            imgnex = floyds.floydsspecdef.extractspectrum(
                                imgn,
                                dv,
                                _ext_trace,
                                _dispersionline,
                                _interactive,
                                tpe,
                                automaticex=_automaticex)
                        except:
                            imgnex = ''
                    elif img:
                        try:
                            imgnex = floyds.floydsspecdef.extractspectrum(
                                img,
                                dv,
                                _ext_trace,
                                _dispersionline,
                                _interactive,
                                tpe,
                                automaticex=_automaticex)
                        except:
                            imgnex = ''
                    if imgnex:
                        hdrs = floyds.util.readhdr(imgnex)
                        _tel = floyds.util.readkey3(hdrs, 'TELID')
                        try:
                            _outputsens2='sens_'+_tel+'_'+str(floyds.util.readkey3(hdrs,'date-night'))+'_'+str(floyds.util.readkey3(hdrs,'grism'))+\
                                '_'+re.sub('.dat','',floyds.util.readkey3(hdrs,'stdname'))+'_'+str(MJDtoday)
                        except:
                            sys.exit(
                                'Error: missing header -stdname- in standard '
                                + str(standardfile) + '  ')
                        print '\n### compute sensitivity function and atmofile'
                        if setup[0] == 'red':
                            atmofile = floyds.floydsspecdef.telluric_atmo(
                                imgnex)
                            #print atmofile
                            if atmofile and atmofile not in outputfile[tpe][
                                    archfile]:
                                outputfile[tpe][archfile].append(atmofile)
                            stdusedclean = re.sub('_ex.fits', '_clean.fits',
                                                  imgnex)
                            floyds.util.delete(stdusedclean)
                            _function = 'spline3'
                            iraf.specred.sarith(input1=imgnex,
                                                op='/',
                                                input2=atmofile,
                                                output=stdusedclean,
                                                format='multispec')
                            try:
                                _outputsens2 = floyds.floydsspecdef.sensfunction(
                                    stdusedclean, _outputsens2, _function, 8,
                                    _interactive)
                            except:
                                print 'Warning: problem computing sensitivity function'
                                _outputsens2 = ''
                            if setup not in atmo: atmo[setup] = [atmofile]
                            else: atmo[setup].append(atmofile)
                        else:
                            _function = 'spline3'
                            try:
                                _outputsens2 = floyds.floydsspecdef.sensfunction(
                                    imgnex, _outputsens2, _function, 12,
                                    _interactive, '3400:4700')  #,3600:4300')
                            except:
                                print 'Warning: problem computing sensitivity function'
                                _outputsens2 = ''
                        if _outputsens2 and _outputsens2 not in outputfile[
                                tpe][archfile]:
                            outputfile[tpe][archfile].append(_outputsens2)
    ###################################################
    print outputfile
    if 'obj' in outputfile:
        for imm in outputfile['obj']:
            lista = outputfile['obj'][imm]
            lista1 = []
            for i in lista:
                if '_ex.fits' in i:
                    lista1.append(i)
                    lista.pop(lista.index(i))
            done = []
            for img in lista1:
                _output = ''
                if img not in done:
                    if '_red_' in img:
                        redfile = img
                        if re.sub('_red_', '_blue_', redfile)[1:] in lista1:
                            bluefile = lista1[lista1.index(
                                re.sub('_red_', '_blue_', redfile)[1:])]
                            _output = re.sub('_red_', '_merge_', redfile)
                            try:
                                _output = floyds.floydsspecdef.combspec(
                                    bluefile,
                                    redfile,
                                    _output,
                                    scale=True,
                                    num=None)
                                lista.append(_output)
                                done.append(redfile)
                                done.append(bluefile)
                                floyds.util.delete(bluefile)
                                floyds.util.delete(redfile)
                                floyds.util.delete(redfile[1:])
                            except:
                                done.append(redfile)
                                lista.append(redfile)
                        else:
                            done.append(redfile)
                            lista.append(redfile)
                    elif '_blue_' in img:
                        bluefile = img
                        if 'n' + re.sub('_blue_', '_red_', bluefile) in lista1:
                            redfile = lista1[lista1.index(
                                'n' + re.sub('_blue_', '_red_', bluefile))]
                            _output = re.sub('_red_', '_merge_', redfile)
                            try:
                                _output = floyds.floydsspecdef.combspec(
                                    bluefile,
                                    redfile,
                                    _output,
                                    scale=True,
                                    num=None)
                                lista.append(_output)
                                done.append(redfile)
                                done.append(bluefile)
                                floyds.util.delete(bluefile)
                                floyds.util.delete(redfile)
                                floyds.util.delete(redfile[1:])
                            except:
                                done.append(bluefile)
                                lista.append(bluefile)
                        else:
                            done.append(bluefile)
                            lista.append(bluefile)
            outputfile['obj'][imm] = lista
    readme = floyds.floydsspecauto.writereadme()
    return outputfile, readme
コード例 #10
0
ファイル: iqnirc2.py プロジェクト: pradipgatkine/python
    def flatten(self, qombos, flat=None, flatscale=no):

        if type(qombos) is StringType:
            qombo = qombos
            qombos = [qombo]

        # Loop over input list of qombos
        for qombo in qombos:

            # Check for presence of calibration files
            obj, filt = qombo_split(qombo)
            if not self.check_calib(filt):
                self.calib()

            # Corresponding files
            offiles = self.science_images(qombo)
            if len(offiles) == 0:
                continue

            # Make a text ".lis" file to record this
            putlines(qombo + '.lis', offiles, clobber=yes)

            # Rescale flatfield if requested
            inflat = self.flatpre + filt + '.fits'
            outflat = self.flatpre + qombo + '.fits'
            check_exist(outflat, "w", clobber=self.clobber)

            if flatscale:
                nirc_flatscale(offiles,
                               inflat,
                               outflat,
                               clipfrac=0.02,
                               clobber=yes)
            else:
                iraf.imcopy(inflat, outflat, verbose=no)
            check_exist(outflat, "r")

            # Clip flatfield and update BPM
            iraf.iqclip(outflat,
                        lthresh=self.flatlow,
                        hthresh=self.flathigh,
                        bookend=no,
                        replace=1.0,
                        maskin='!BPM',
                        maskval=1,
                        maskout=self.bpmsfx)

            # Rename the updated BPM
            newbpm1 = get_head(outflat, 'BPM')
            newbpm2 = newbpm1.replace(self.flatpre, '')
            check_exist(newbpm2, 'w', clobber=self.clobber)
            iraf.imrename(newbpm1, newbpm2, verbose=no)
            update_head(outflat, 'BPM', newbpm2)

            # Further preprocessing
            for image in offiles:

                # Keyword updates
                update_head(image, [self.filtset, 'BPM'], [filt, newbpm2])

                # Flatfielding
                iraf.iqflatten(image,
                               outflat,
                               outpfx=self.flatpfx,
                               normflat=no,
                               statsec=self.statsec,
                               subsky=yes,
                               vignflat=no,
                               clipflat=no,
                               clobber=yes,
                               verbose=no)

                # Set bad pixels to zero
                image2 = self.flatpfx + image
                iraf.iqmask(image2,
                            mask='!BPM',
                            method='constant',
                            value=0.0,
                            clobber=yes,
                            verbose=no)

                # Write Useful Keywords
                update_head(image2, 'PROCESSD', 1,
                            "Image has been processed by iqnirc")
                update_head(image2, 'PROCVER', version,
                            "Version number of iqnirc used")
                update_head(image2, 'ZEROPT', 'INDEF',
                            "Zero-point relative to 2MASS or INDEF")
                update_head(image2, 'PROCPROB', 'None',
                            "Problems encountered in iqnirc processing")

            # List of flatfielded images
            offiles2 = pfx_list(offiles, self.flatpfx)
            oflist2 = ','.join(offiles2)

            # Object-detection
            iraf.iqobjs(oflist2,
                        self.sigma,
                        self.satval,
                        skyval="!SKYBKG",
                        masksfx=self.masksfx,
                        wtimage="",
                        minlim=no,
                        clobber=yes,
                        verbose=no)

            # Attempt WCS refinement
            iraf.iqwcs(oflist2,
                       objkey=self.objkey,
                       rakey='RA',
                       deckey='DEC',
                       pixscl=self.pix,
                       pixtol=0.05,
                       starfile='!STARFILE',
                       catalog='ir',
                       diffuse=yes,
                       clobber=yes,
                       verbose=self.verbose)

            # Done with Step #3
            update_head(offiles2, 'IQWRCSTP', 3, "Stage of IQWIRC processing")
コード例 #11
0
def floydsautoredu(files,_interactive,_dobias,_doflat,_listflat,_listbias,_listarc,_cosmic,_ext_trace,_dispersionline,liststandard,listatmo,_automaticex,_classify=False,_verbose=False,smooth=1,fringing=1):
    import floyds
    import string,re,os,glob,sys,pickle
    from numpy import array, arange, mean,pi,arccos,sin,cos,argmin
    from astropy.io import fits
    from pyraf import iraf
    import datetime
    os.environ["PYRAF_BETA_STATUS"] = "1"
    iraf.set(direc=floyds.__path__[0]+'/')
    _extinctdir='direc$standard/extinction/'
    _tel=floyds.util.readkey3(floyds.util.readhdr(re.sub('\n','',files[0])),'TELID')
    if _tel=='fts':
        _extinction='ssoextinct.dat'
        _observatory='sso'
    elif _tel=='ftn':
        _extinction='maua.dat' 
        _observatory='cfht'   
    else: sys.exit('ERROR: observatory not recognised')
    dv=floyds.util.dvex()
    scal=pi/180.
    iraf.noao(_doprint=0)
    iraf.imred(_doprint=0)
    iraf.ccdred(_doprint=0)
    iraf.twodspec(_doprint=0)
    iraf.longslit(_doprint=0)
    iraf.specred(_doprint=0)
    toforget = ['ccdred.flatcombine','ccdred.zerocombine','ccdproc','specred.apall','longslit.identify','longslit.reidentify',\
                    'specred.standard','longslit.fitcoords','specred.transform','specred.response']
    for t in toforget: iraf.unlearn(t)
    iraf.longslit.dispaxi=2
    iraf.longslit.mode='h'
    iraf.identify.fwidth=7 
    iraf.identify.order=2 
    iraf.specred.dispaxi=2
    iraf.specred.mode='h'
    iraf.ccdproc.darkcor='no'
    iraf.ccdproc.fixpix='no'
    iraf.ccdproc.trim='no'
    iraf.ccdproc.flatcor='no'
    iraf.ccdproc.overscan='no'
    iraf.ccdproc.zerocor='no'
    iraf.ccdproc.biassec=''
    iraf.ccdproc.ccdtype=''
    iraf.ccdred.instrument = "/dev/null"
    if _verbose: 
        iraf.ccdred.verbose='yes'
        iraf.specred.verbose='yes'
    else: 
        iraf.specred.verbose='no'
        iraf.ccdred.verbose='no'
    now=datetime.datetime.now()
    datenow=now.strftime('20%y%m%d%H%M')
    MJDtoday=55928+(datetime.date.today()-datetime.date(2012, 01, 01)).days
    outputlist=[]
    hdra=floyds.util.readhdr(re.sub('\n','',files[0]))
    _gain=floyds.util.readkey3(hdra,'gain')
    _rdnoise=floyds.util.readkey3(hdra,'ron')
    std,rastd,decstd,magstd=floyds.util.readstandard('standard_floyds_mab.txt')
    _naxis2=hdra.get('NAXIS2')
    _naxis1=hdra.get('NAXIS1')
    if not _naxis1: _naxis1=2079
    if not _naxis2: 
        if not hdr0.get('HDRVER'):   _naxis1=511
        else:                        _naxis1=512
    _overscan='[2049:'+str(_naxis1)+',1:'+str(_naxis2)+']'
    _biassecblu='[380:2048,325:'+str(_naxis2)+']'    
    _biassecred='[1:1800,1:350]'    
    lista={}
    objectlist={}
    biaslist={}
    flatlist={}
    flatlistd={}
    arclist={}
    max_length=14
    for img in files:
        hdr0=floyds.util.readhdr(img)
        if  floyds.util.readkey3(hdr0,'naxis2')>=500:
            if 'blu' not in lista: lista['blu']=[]
            if 'red' not in lista: lista['red']=[]
            _object0=floyds.util.readkey3(hdr0,'object')
            _object0 = re.sub(':', '', _object0) # colon
            _object0 = re.sub('/', '', _object0) # slash
            _object0 = re.sub('\s', '', _object0) # any whitespace
            _object0 = re.sub('\(', '', _object0) # open parenthesis
            _object0 = re.sub('\[', '', _object0) # open square bracket
            _object0 = re.sub('\)', '', _object0) # close parenthesis
            _object0 = re.sub('\]', '', _object0) # close square bracket
            _object0 = _object0.replace(r'\t', '') # Any tab characters
            _object0 = _object0.replace('*', '') # Any asterisks

            if len(_object0) > max_length:
                _object0 = _object0[:max_length]
            _date0=floyds.util.readkey3(hdr0,'date-night')
            _tel=floyds.util.readkey3(hdr0,'TELID')
            _type=floyds.util.readkey3(hdr0,'OBSTYPE')
            if not _type:    _type=floyds.util.readkey3(hdr0,'imagetyp')
            _slit=floyds.util.readkey3(hdr0,'slit')
            if _type:
                _type = _type.lower()
                if _type in ['sky','spectrum','expose']:
                    nameoutb=str(_object0)+'_'+_tel+'_'+str(_date0)+'_blue_'+str(_slit)+'_'+str(MJDtoday)
                    nameoutr=str(_object0)+'_'+_tel+'_'+str(_date0)+'_red_'+str(_slit)+'_'+str(MJDtoday)
                elif _type in ['lamp','arc','l']:
                    nameoutb='arc_'+str(_object0)+'_'+_tel+'_'+str(_date0)+'_blue_'+str(_slit)+'_'+str(MJDtoday)
                    nameoutr='arc_'+str(_object0)+'_'+_tel+'_'+str(_date0)+'_red_'+str(_slit)+'_'+str(MJDtoday)
                elif _type in ['flat','f','lampflat','lamp-flat']:
                    nameoutb='flat_'+str(_object0)+'_'+_tel+'_'+str(_date0)+'_blue_'+str(_slit)+'_'+str(MJDtoday)
                    nameoutr='flat_'+str(_object0)+'_'+_tel+'_'+str(_date0)+'_red_'+str(_slit)+'_'+str(MJDtoday)
                else:
                    nameoutb=str(_type.lower())+'_'+str(_object0)+'_'+_tel+'_'+str(_date0)+'_blue_'+str(_slit)+'_'+str(MJDtoday)
                    nameoutr=str(_type.lower())+'_'+str(_object0)+'_'+_tel+'_'+str(_date0)+'_red_'+str(_slit)+'_'+str(MJDtoday)

                bimg=floyds.util.name_duplicate(img,nameoutb,'')
                rimg=floyds.util.name_duplicate(img,nameoutr,'')
####
                floyds.util.delete(bimg)
                floyds.util.delete(rimg)
                iraf.imcopy(img,bimg,verbose='no')
                iraf.imcopy(img,rimg,verbose='no')

                aaa=iraf.hedit(bimg,'CCDSEC',delete='yes',update='yes',verify='no',Stdout=1)
                aaa=iraf.hedit(bimg,'TRIMSEC',delete='yes',update='yes',verify='no',Stdout=1)
                aaa=iraf.hedit(rimg,'CCDSEC',delete='yes',update='yes',verify='no',Stdout=1)
                aaa=iraf.hedit(rimg,'TRIMSEC',delete='yes',update='yes',verify='no',Stdout=1)

                iraf.ccdproc(bimg,output='', overscan="yes", trim="yes", zerocor='no', flatcor='no', zero='', ccdtype='',\
                                 fixpix='no', trimsec=_biassecblu, biassec=_overscan, readaxi='line', Stdout=1)
                iraf.ccdproc(rimg,output='', overscan="yes", trim="yes", zerocor='no', flatcor='no', zero='', ccdtype='',\
                                 fixpix='no', trimsec=_biassecred, biassec=_overscan, readaxi='line', Stdout=1)
                floyds.util.updateheader(bimg,0,{'GRISM':['blu',' blue order']})
                floyds.util.updateheader(rimg,0,{'GRISM':['red',' blue order']})
                floyds.util.updateheader(bimg,0,{'arcfile':[img,'file name in the archive']})
                floyds.util.updateheader(rimg,0,{'arcfile':[img,'file name in the archive']})
                lista['blu'].append(bimg)
                lista['red'].append(rimg)
            else: 
                print 'warning type not defined'
    for arm in lista.keys():
        for img in lista[arm]:
            print img
            hdr=floyds.util.readhdr(img)
            _type=floyds.util.readkey3(hdr,'OBSTYPE')
            if _type=='EXPOSE':  
                      _type=floyds.util.readkey3(hdr,'imagetyp')
                      if not _type: _type='EXPOSE'

            if _type=='EXPOSE':  
                print 'warning obstype still EXPOSE, are this old data ?  run manually floydsfixheader'

            _slit=floyds.util.readkey3(hdr,'slit')
            _grpid=floyds.util.readkey3(hdr,'grpid')
            if _type.lower() in ['flat','f','lamp-flat','lampflat'] :
                if (arm,_slit) not in flatlist:  flatlist[arm,_slit]={}
                if _grpid not in flatlist[arm,_slit]: flatlist[arm,_slit][_grpid]=[img]
                else: flatlist[arm,_slit][_grpid].append(img)
            elif _type.lower() in ['lamp','l','arc']:
                if (arm,_slit) not in arclist:  arclist[arm,_slit]={}
                if _grpid not in arclist[arm,_slit]: arclist[arm,_slit][_grpid]=[img]
                else: arclist[arm,_slit][_grpid].append(img)
            elif _type in ['bias','b']:
                if arm not in biaslist: biaslist[arm]=[]
                biaslist[arm].append(img)
            elif _type.lower() in ['sky','s','spectrum']:
                try:
                    _ra=float(floyds.util.readkey3(hdr,'RA'))
                    _dec=float(floyds.util.readkey3(hdr,'DEC'))
                except:
                    ra00=string.split(floyds.util.readkey3(hdr,'RA'),':')
                    ra0,ra1,ra2=float(ra00[0]),float(ra00[1]),float(ra00[2])
                    _ra=((ra2/60.+ra1)/60.+ra0)*15.
                    dec00=string.split(floyds.util.readkey3(hdr,'DEC'),':')
                    dec0,dec1,dec2=float(dec00[0]),float(dec00[1]),float(dec00[2])
                    if '-' in str(dec0):       _dec=(-1)*((dec2/60.+dec1)/60.+((-1)*dec0))
                    else:                      _dec=(dec2/60.+dec1)/60.+dec0
                dd=arccos(sin(_dec*scal)*sin(decstd*scal)+cos(_dec*scal)*cos(decstd*scal)*cos((_ra-rastd)*scal))*((180/pi)*3600)
                if _verbose:
                    print _ra,_dec
                    print std[argmin(dd)],min(dd)
                if min(dd)<5200: _typeobj='std'
                else: _typeobj='obj'
                if min(dd)<5200:
                    floyds.util.updateheader(img,0,{'stdname':[std[argmin(dd)],'']})
                    floyds.util.updateheader(img,0,{'magstd':[float(magstd[argmin(dd)]),'']})
                if _typeobj not in objectlist:      objectlist[_typeobj]={}

                if (arm,_slit) not in objectlist[_typeobj]:     objectlist[_typeobj][arm,_slit]=[img]
                else: objectlist[_typeobj][arm,_slit].append(img)
    if _verbose:
        print 'object'
        print objectlist
        print 'flat'
        print flatlist
        print 'bias'
        print biaslist
        print 'arc'
        print arclist

    if liststandard and 'std' in objectlist.keys():  
        print 'external standard, raw standard not used'
        del objectlist['std']

    sens={}
    outputfile={}
    atmo={}
    for tpe in objectlist:
      if tpe not in outputfile:  outputfile[tpe]={}
      for setup in objectlist[tpe]:
        if setup not in sens:   sens[setup]=[]
        print '\n### setup= ',setup,'\n### objects= ',objectlist[tpe][setup],'\n'
        for img in objectlist[tpe][setup]:
              print '\n\n### next object= ',img,' ',floyds.util.readkey3(floyds.util.readhdr(img),'object'),'\n'
              hdr=floyds.util.readhdr(img)
              archfile=floyds.util.readkey3(hdr,'arcfile')
              _gain=floyds.util.readkey3(hdr,'gain')
              _rdnoise=floyds.util.readkey3(hdr,'ron')
              _grism=floyds.util.readkey3(hdr,'grism')
              _grpid=floyds.util.readkey3(hdr,'grpid')
              if archfile not in outputfile[tpe]: outputfile[tpe][archfile]=[]
#####################      flat   ###############
              if _listflat:   flatgood=_listflat    # flat list from reducer
              elif setup in flatlist:  
                  if _grpid in flatlist[setup]:
                      print '\n###FLAT WITH SAME GRPID'
                      flatgood= flatlist[setup][_grpid]     # flat in the  raw data
                  else:  
                      flatgood=[]
                      for _grpid0 in flatlist[setup].keys():
                          for ii in flatlist[setup][_grpid0]:
                              flatgood.append(ii)
              else: flatgood=[]
              if len(flatgood)!=0:
                  if len(flatgood)>1:
                      f=open('_oflatlist','w')
                      for fimg in flatgood:
                          print fimg
                          f.write(fimg+'\n')
                      f.close()
                      floyds.util.delete('flat'+img)
                      iraf.ccdred.flatcombine('"@_oflatlist"',output='flat'+img,combine='average',reject='none',ccdtype=' ',rdnoise=_rdnoise,gain=_gain, process='no', Stdout=1)
                      floyds.util.delete('_oflatlist')
                      flatfile='flat'+img
                  elif len(flatgood)==1:
                      os.system('cp '+flatgood[0]+' flat'+img)
                      flatfile='flat'+img
              else: flatfile=''
##########################   find arcfile            #######################
              arcfile=''
              if _listarc:       arcfile= [floyds.util.searcharc(img,_listarc)[0]][0]   # take arc from list 
              if not arcfile and setup in arclist.keys():
                    if _grpid in arclist[setup]:  
                        print '\n###ARC WITH SAME GRPID'
                        arcfile= arclist[setup][_grpid]     # flat in the  raw data
                    else:  
                        arcfile=[]
                        for _grpid0 in arclist[setup].keys():
                            for ii in arclist[setup][_grpid0]:
                                arcfile.append(ii)                   
              if arcfile:
                  if len(arcfile)>1:                           # more than one arc available
                      print arcfile
#                     _arcclose=floyds.util.searcharc(imgex,arcfile)[0]   # take the closest in time 
                      _arcclose=floyds.sortbyJD(arcfile)[-1]               #  take the last arc of the sequence
                      if _interactive.upper() in ['YES','Y']:
                              for ii in floyds.floydsspecdef.sortbyJD(arcfile):
                                  print '\n### ',ii 
                              arcfile=raw_input('\n### more than one arcfile available, which one to use ['+str(_arcclose)+'] ? ')
                              if not arcfile: arcfile=_arcclose
                      else: arcfile=_arcclose
                  else: arcfile=arcfile[0]
              else:   print '\n### Warning: no arc found'

###################################################################   rectify 
              if setup[0]=='red':
                  fcfile=floyds.__path__[0]+'/standard/ident/fcrectify_'+_tel+'_red'
                  fcfile1=floyds.__path__[0]+'/standard/ident/fcrectify1_'+_tel+'_red'
                  print fcfile
              else:
                  fcfile=floyds.__path__[0]+'/standard/ident/fcrectify_'+_tel+'_blue'
                  fcfile1=floyds.__path__[0]+'/standard/ident/fcrectify1_'+_tel+'_blue'
                  print fcfile
              print img,arcfile,flatfile
              img0=img
              if img      and img not in outputfile[tpe][archfile]: outputfile[tpe][archfile].append(img)
              if arcfile  and arcfile not in outputfile[tpe][archfile]: outputfile[tpe][archfile].append(arcfile)
              if flatfile and flatfile not in outputfile[tpe][archfile]: outputfile[tpe][archfile].append(flatfile)

              img,arcfile,flatfile=floyds.floydsspecdef.rectifyspectrum(img,arcfile,flatfile,fcfile,fcfile1,'no',_cosmic)
              if img      and img not in outputfile[tpe][archfile]: outputfile[tpe][archfile].append(img)
              if arcfile  and arcfile not in outputfile[tpe][archfile]: outputfile[tpe][archfile].append(arcfile)
              if flatfile and flatfile not in outputfile[tpe][archfile]: outputfile[tpe][archfile].append(flatfile)
###################################################################         check wavecalib  
              if tpe=='std' or floyds.util.readkey3(floyds.util.readhdr(img),'exptime') < 300:
                  if setup[0]=='red':
                      print '\n### check standard wave calib'
                      data, hdr = fits.getdata(img, 0, header=True) 
                      y=data.mean(1)
                      import numpy as np
                      if np.argmax(y) < 80 and np.argmax(y) > 15:                      
                          y2=data[np.argmax(y)-3:np.argmax(y)+3].mean(0)
                          yy2=data[np.argmax(y)-9:np.argmax(y)-3].mean(0)
                          floyds.util.delete('_std.fits')
                          fits.writeto('_std.fits', np.float32(y2-yy2), hdr)
                          shift=floyds.floydsspecdef.checkwavestd('_std.fits',_interactive,2)
                          zro=hdr['CRVAL1']
                          floyds.util.updateheader(img,0,{'CRVAL1':[zro+int(shift),'']})
                          floyds.util.updateheader(img,0,{'shift':[float(shift),'']})
                          floyds.util.delete('_std.fits')
                      else:
                          print 'object not found'
                  else: 
                      print '\n### warning check in wavelength not possible for short exposure in the blu range '
              else:
                    print '\n### check object wave calib'
                    _skyfile=floyds.__path__[0]+'/standard/ident/sky_'+setup[0]+'.fits'
                    data, hdr = fits.getdata(img, 0, header=True) 
                    y=data.mean(1)
                    import numpy as np
                    if np.argmax(y) < 80 and np.argmax(y) > 15:
                        yy1=data[10:np.argmax(y)-9].mean(0)
                        yy2=data[np.argmax(y)+9:-10].mean(0)
                        floyds.util.delete('_sky.fits')
                        fits.writeto('_sky.fits', np.float32(yy1+yy2), hdr)
                        shift=floyds.floydsspecdef.checkwavelength_obj('_sky.fits',_skyfile,_interactive,usethirdlayer=False)
                        floyds.util.delete('_sky.fits')
                        zro=hdr['CRVAL1']
                        floyds.util.updateheader(img,0,{'CRVAL1':[zro+int(shift),'']})
                        floyds.util.updateheader(img,0,{'shift':[float(shift),'']})
                    else:  print 'object not found'
####################################################     flat field
              if img and flatfile and setup[0]=='red':
                      imgn='n'+img
                      hdr1 = floyds.readhdr(img)
                      hdr2 = floyds.readhdr(flatfile)
                      _grpid1=floyds.util.readkey3(hdr1,'grpid')
                      _grpid2=floyds.util.readkey3(hdr2,'grpid')
                      if _grpid1==_grpid2:
                          print flatfile,img,setup[0]
                          imgn=floyds.fringing_classicmethod2(flatfile,img,'no','*',15,setup[0])
                      else:
                          print 'Warning flat not the same OB'
                          imgex=floyds.floydsspecdef.extractspectrum(img,dv,_ext_trace,_dispersionline,_interactive,tpe,automaticex=_automaticex)
                          floyds.delete('flat'+imgex)
                          iraf.specred.apsum(flatfile,output='flat'+imgex,referen=img,interac='no',find='no',recente='no',resize='no',\
                                             edit='no',trace='no',fittrac='no',extract='yes',extras='no',review='no',backgro='none')
                          fringingmask=floyds.normflat('flat'+imgex)
                          print '\n### fringing correction'
                          print imgex,fringingmask
                          imgex,scale,shift=floyds.correctfringing_auto(imgex,fringingmask)  #  automatic correction
                          shift=int(.5+float(shift)/3.5)        # shift from correctfringing_auto in Angstrom
                          print '\n##### flat scaling: ',str(scale),str(shift)
########################################################
                          datax, hdrx = fits.getdata(flatfile, 0, header=True)
                          xdim=hdrx['NAXIS1']
                          ydim=hdrx['NAXIS2']
                          iraf.specred.apedit.nsum=15 
                          iraf.specred.apedit.width=100.  
                          iraf.specred.apedit.line=1024 
                          iraf.specred.apfind.minsep=20.  
                          iraf.specred.apfind.maxsep=1000.  
                          iraf.specred.apresize.bkg='no' 
                          iraf.specred.apresize.ylevel=0.5 
                          iraf.specred.aptrace.nsum=10
                          iraf.specred.aptrace.step=10
                          iraf.specred.aptrace.nlost=10
                          floyds.util.delete('n'+flatfile)
                          floyds.util.delete('norm.fits')
                          floyds.util.delete('n'+img)
                          floyds.util.delete(re.sub('.fits','c.fits',flatfile))
                          iraf.imcopy(flatfile+'[500:'+str(xdim)+',*]',re.sub('.fits','c.fits',flatfile),verbose='no')
                          iraf.imarith(flatfile,'/',flatfile,'norm.fits',verbose='no')
                          flatfile=re.sub('.fits','c.fits',flatfile)
                          floyds.util.delete('n'+flatfile)
                          iraf.unlearn(iraf.specred.apflatten)
                          floyds.floydsspecdef.aperture(flatfile)
                          iraf.specred.apflatten(flatfile,output='n'+flatfile,interac=_interactive,find='no',recenter='no', resize='no',edit='no',trace='no',\
                                                 fittrac='no',fitspec='no', flatten='yes', aperture='',\
                                                 pfit='fit2d',clean='no',function='legendre',order=15,sample = '*', mode='ql')
                          iraf.imcopy('n'+flatfile,'norm.fits[500:'+str(xdim)+',*]',verbose='no')
                          floyds.util.delete('n'+flatfile)
                          floyds.util.delete('n'+img)
                          iraf.imrename('norm.fits','n'+flatfile,verbose='no')
                          imgn=floyds.floydsspecdef.applyflat(img,'n'+flatfile,'n'+img,scale,shift)
              else:                  imgn=''

              if imgn and imgn not in outputfile[tpe][archfile]: outputfile[tpe][archfile].append(imgn)

###################################################      2D flux calib
              hdr=floyds.util.readhdr(img)
              _sens=''
              if liststandard:  _sens=floyds.util.searchsens(img,liststandard)[0]   # search in the list from reducer
              if not _sens:
                  try:      _sens=floyds.util.searchsens(img,sens[setup])[0]        # search in the reduced data
                  except:   _sens=floyds.util.searchsens(img,'')[0]              # search in tha archive
              if _sens:
                  if _sens[0]=='/': 
                      os.system('cp '+_sens+' .')
                      _sens=string.split(_sens,'/')[-1]
                  imgd=fluxcalib2d(img,_sens)
                  if imgn:     imgdn=fluxcalib2d(imgn,_sens)
                  else: imgdn=''
                  if _sens not in outputfile[tpe][archfile]: outputfile[tpe][archfile].append(_sens)
                  else:        imgdn='' 
                  print '\n### do 2D calibration'
              else:
                  imgd=''
                  imgdn=''
################    extraction         ####################################
              if imgdn:
                  try:
                      imgdnex=floyds.floydsspecdef.extractspectrum(imgdn,dv,_ext_trace,_dispersionline,_interactive,tpe,automaticex=_automaticex)
                  except Exception as e:
                      print 'failed to extract', imgdn
                      print e
                      imgdnex=''
              else:       
                  imgdnex=''
              if imgd:
                  try:
                      imgdex=floyds.floydsspecdef.extractspectrum(imgd,dv,_ext_trace,_dispersionline,_interactive,tpe,automaticex=_automaticex)  
                  except Exception as e:
                      print 'failed to extract', imgd
                      print e
                      imgdex=''
              else:
                  imgdex=''
              if imgd    and imgd    not in outputfile[tpe][archfile]: outputfile[tpe][archfile].append(imgd)
              if imgdn   and imgdn   not in outputfile[tpe][archfile]: outputfile[tpe][archfile].append(imgdn)
              if imgdnex and imgdnex not in outputfile[tpe][archfile]: outputfile[tpe][archfile].append(imgdnex)
              if imgdex  and imgdex  not in outputfile[tpe][archfile]: outputfile[tpe][archfile].append(imgdex)
              if tpe=='std':
                  if imgn:
                      try:
                          imgnex=floyds.floydsspecdef.extractspectrum(imgn,dv,_ext_trace,_dispersionline,_interactive,tpe,automaticex=_automaticex)  
                      except Exception as e:
                          print 'failed to extract', imgn
                          print e
                          imgnex=''
                  elif img:
                      try:
                          imgnex=floyds.floydsspecdef.extractspectrum(img,dv,_ext_trace,_dispersionline,_interactive,tpe,automaticex=_automaticex)  
                      except Exception as e:
                          print 'failed to extract', img
                          print e
                          imgnex=''
                  if imgnex:
                    hdrs=floyds.util.readhdr(imgnex)
                    _tel=floyds.util.readkey3(hdrs,'TELID')
                    try:
                      _outputsens2='sens_'+_tel+'_'+str(floyds.util.readkey3(hdrs,'date-night'))+'_'+str(floyds.util.readkey3(hdrs,'grism'))+\
                          '_'+re.sub('.dat','',floyds.util.readkey3(hdrs,'stdname'))+'_'+str(MJDtoday)
                    except:  sys.exit('Error: missing header -stdname- in standard '+str(standardfile)+'  ')                          
                    print '\n### compute sensitivity function and atmofile'
                    if setup[0]=='red':
                          atmofile=floyds.floydsspecdef.telluric_atmo(imgnex)
                          if atmofile and atmofile not in outputfile[tpe][archfile]:    outputfile[tpe][archfile].append(atmofile)
                          stdusedclean=re.sub('_ex.fits','_clean.fits',imgnex)
                          floyds.util.delete(stdusedclean)
                          _function='spline3'
                          iraf.specred.sarith(input1=imgnex,op='/',input2=atmofile,output=stdusedclean, format='multispec')
                          try:
                              _outputsens2=floyds.floydsspecdef.sensfunction(stdusedclean,_outputsens2,_function,8,_interactive)
                          except:
                              print 'Warning: problem computing sensitivity function'
                              _outputsens2=''
                          if setup not in atmo: atmo[setup]=[atmofile]
                          else: atmo[setup].append(atmofile)
                    else:
                          _function='spline3'
                          try:
                              _outputsens2=floyds.floydsspecdef.sensfunction(imgnex,_outputsens2,_function,12,_interactive,'3400:4700')#,3600:4300')
                          except:
                              print 'Warning: problem computing sensitivity function'
                              _outputsens2=''
                    if _outputsens2  and _outputsens2 not in outputfile[tpe][archfile]:    outputfile[tpe][archfile].append(_outputsens2)
    ###################################################
    if 'obj' in outputfile:
      for imm in outputfile['obj']:
        lista = []
        tt_red = ''
        ntt_red = ''
        tt_blue = ''
        for f in outputfile['obj'][imm]:
            if '_ex.fits' in f and '_blue_' in f:
                tt_blue = f
            elif '_ex.fits' in f and f[:3] == 'ntt':
                ntt_red = f
            elif '_ex.fits' in f and f[:2] == 'tt':
                tt_red = f
            else:
                lista.append(f)
        merged = ntt_red.replace('_red_', '_merge_')
        if tt_blue and ntt_red:
            floyds.floydsspecdef.combspec2(tt_blue, ntt_red, merged, scale=True, num=None)
        if os.path.isfile(merged):
            lista.append(merged)
            floyds.util.delete(tt_blue)
            floyds.util.delete(tt_red)
            floyds.util.delete(ntt_red)
        else:
            if tt_blue: lista.append(tt_blue)
            if tt_red:  lista.append(tt_red)
            if ntt_red: lista.append(ntt_red)
        outputfile['obj'][imm] = lista
    readme=floyds.floydsspecauto.writereadme()
    return outputfile,readme
コード例 #12
0
ファイル: mscimage.py プロジェクト: bjanesh/uchvc-tools
def mscimage(input=None, output=None, format='image', pixmask=no,verbose=')_.verbose',wcssource='image',reference='',ra=INDEF,dec=INDEF,scale=INDEF,rotation=INDEF,blank=0.0,interpolant='poly5',minterpolant='linear',boundary='reflect',constant=0.0,fluxconserve=no,ntrim=8,nxblock=INDEF,nyblock=INDEF,interactive=no,nx=10,ny=20,fitgeometry='general',xxorder=4,xyorder=4,xxterms='half',yxorder=4,yyorder=4,yxterms='half',fd_in='',fd_ext='',fd_coord='',mode='ql',DOLLARnargs=0,taskObj=None):

	Vars = IrafParList('mscimage')
	Vars.addParam(makeIrafPar(input, datatype='string', name='input', mode='a',prompt='List of input mosaic exposures'))
	Vars.addParam(makeIrafPar(output, datatype='string', name='output',mode='a',prompt='List of output images'))
	Vars.addParam(makeIrafPar(format, datatype='string', name='format',enum=['image', 'mef'],mode='h',prompt='Output format (image|mef)'))
	Vars.addParam(makeIrafPar(pixmask, datatype='bool', name='pixmask',mode='h',prompt='Create pixel mask?'))
	Vars.addParam(makeIrafPar(verbose, datatype='bool', name='verbose',mode='h',prompt='Verbose output?\n\n# Output WCS parameters'))
	Vars.addParam(makeIrafPar(wcssource, datatype='string', name='wcssource',enum=['image', 'parameters', 'match'],mode='h',prompt='Output WCS source (image|parameters|match)'))
	Vars.addParam(makeIrafPar(reference, datatype='file', name='reference',mode='h',prompt='Reference image'))
	Vars.addParam(makeIrafPar(ra, datatype='real', name='ra', max=24.0,min=0.0,mode='h',prompt='RA of tangent point (hours)'))
	Vars.addParam(makeIrafPar(dec, datatype='real', name='dec', max=90.0,min=-90.0,mode='h',prompt='DEC of tangent point (degrees)'))
	Vars.addParam(makeIrafPar(scale, datatype='real', name='scale', mode='h',prompt='Scale (arcsec/pixel)'))
	Vars.addParam(makeIrafPar(rotation, datatype='real', name='rotation',max=360.0,min=-360.0,mode='h',prompt='Rotation of DEC from N to E (degrees)\n\n# Resampling parmeters'))
	Vars.addParam(makeIrafPar(blank, datatype='real', name='blank', mode='h',prompt='Blank value'))
	Vars.addParam(makeIrafPar(interpolant, datatype='string',name='interpolant',mode='h',prompt='Interpolant for data'))
	Vars.addParam(makeIrafPar(minterpolant, datatype='string',name='minterpolant',mode='h',prompt='Interpolant for mask'))
	Vars.addParam(makeIrafPar(boundary, datatype='string', name='boundary',enum=['nearest', 'constant', 'reflect', 'wrap'],mode='h',prompt='Boundary extension'))
	Vars.addParam(makeIrafPar(constant, datatype='real', name='constant',mode='h',prompt='Constant boundary extension value'))
	Vars.addParam(makeIrafPar(fluxconserve, datatype='bool',name='fluxconserve',mode='h',prompt='Preserve flux per unit area?'))
	Vars.addParam(makeIrafPar(ntrim, datatype='int', name='ntrim', min=0,mode='h',prompt='Edge trim in each extension'))
	Vars.addParam(makeIrafPar(nxblock, datatype='int', name='nxblock',mode='h',prompt='X dimension of working block size in pixels'))
	Vars.addParam(makeIrafPar(nyblock, datatype='int', name='nyblock',mode='h',prompt='Y dimension of working block size in pixels\n\n# Geometric mapping parameters'))
	Vars.addParam(makeIrafPar(interactive, datatype='bool', name='interactive',mode='h',prompt='Fit mapping interactively?'))
	Vars.addParam(makeIrafPar(nx, datatype='int', name='nx', mode='h',prompt='Number of x grid points'))
	Vars.addParam(makeIrafPar(ny, datatype='int', name='ny', mode='h',prompt='Number of y grid points'))
	Vars.addParam(makeIrafPar(fitgeometry, datatype='string',name='fitgeometry',enum=['shift', 'xyscale', 'rotate', 'rscale', 'rxyscale', 'general'],mode='h',prompt='Fitting geometry'))
	Vars.addParam(makeIrafPar(xxorder, datatype='int', name='xxorder', min=2,mode='h',prompt='Order of x fit in x'))
	Vars.addParam(makeIrafPar(xyorder, datatype='int', name='xyorder', min=2,mode='h',prompt='Order of x fit in y'))
	Vars.addParam(makeIrafPar(xxterms, datatype='string', name='xxterms',mode='h',prompt='X fit cross terms type'))
	Vars.addParam(makeIrafPar(yxorder, datatype='int', name='yxorder', min=2,mode='h',prompt='Order of y fit in x'))
	Vars.addParam(makeIrafPar(yyorder, datatype='int', name='yyorder', min=2,mode='h',prompt='Order of y fit in y'))
	Vars.addParam(makeIrafPar(yxterms, datatype='string', name='yxterms',mode='h',prompt='Y fit cross terms type\n\n'))
	Vars.addParam(makeIrafPar(fd_in, datatype='struct', name='fd_in',list_flag=1,mode='h',prompt=''))
	Vars.addParam(makeIrafPar(fd_ext, datatype='struct', name='fd_ext',list_flag=1,mode='h',prompt=''))
	Vars.addParam(makeIrafPar(fd_coord, datatype='struct', name='fd_coord',list_flag=1,mode='h',prompt=''))
	Vars.addParam(makeIrafPar(mode, datatype='string', name='mode', mode='h',prompt=''))
	Vars.addParam(makeIrafPar(DOLLARnargs, datatype='int', name='$nargs',mode='h'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='in', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='out', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='ref', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='pl', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='image', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='trimsec', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='outsec', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='plsec', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='inlists', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='extlist', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='pllist', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='coord', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='db', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='wcsref', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='outtemp', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='pltemp', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='nc', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='nl', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='ncref', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='nlref', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='cmin', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='cmax', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='lmin', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='lmax', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='nimage', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='nimages', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='nxblk', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='nyblk', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='real', name='x', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='real', name='y', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='real', name='rval', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='real', name='xmin', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='real', name='xmax', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='real', name='ymin', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='real', name='ymax', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='real', name='crpix1', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='real', name='crpix2', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='string', name='extname',mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='string', name='str', mode='u'))

	iraf.cache('mscextensions', 'mscgmask')
	Vars.inlists = iraf.mktemp('tmp$iraf')
	Vars.extlist = iraf.mktemp('tmp$iraf')
	Vars.pllist = iraf.mktemp('tmp$iraf')
	Vars.coord = iraf.mktemp('tmp$iraf')
	Vars.db = iraf.mktemp('tmp$iraf')
	Vars.outtemp = iraf.mktemp('tmp')
	Vars.wcsref = iraf.mktemp('tmp')
	Vars.pltemp = iraf.mktemp('tmp')
	iraf.joinlists(Vars.input, Vars.output, output = Vars.inlists, delim = ' ',short=yes,type = 'image')
	Vars.fd_in = Vars.inlists
	while (iraf.fscan(locals(), 'Vars.fd_in', 'Vars.PYin', 'Vars.out') != EOF):
		if (iraf.imaccess(Vars.out)):
			iraf.printf('Warning: Image already exists (%s)\n', Vars.out)
			continue
		if (Vars.pixmask):
			Vars.pl = Vars.out
			Vars.nc = iraf.strlen(Vars.pl)
			if (Vars.nc > 5 and iraf.substr(Vars.pl, Vars.nc - 4, Vars.nc) == '.fits'):
				Vars.pl = iraf.substr(Vars.pl, 1, Vars.nc - 5)
			elif (Vars.nc > 4 and iraf.substr(Vars.out, Vars.nc - 3, Vars.nc) == '.imh'):
				Vars.pl = iraf.substr(Vars.pl, 1, Vars.nc - 4)
			Vars.pl = Vars.pl + '_bpm'
			if (Vars.format == 'image' and iraf.imaccess(Vars.pl)):
				iraf.printf('Warning: Mask already exists (%s)\n', Vars.pl)
				continue
		else:
			Vars.pl = ''
		iraf.mscextensions(Vars.PYin, output = 'file', index = '0-',extname = '',extver = '',lindex = no,lname = yes,lver = no,ikparams = '',Stdout=Vars.extlist)
		Vars.nimages = int(iraf.mscextensions.nimages)
		Vars.nimage = 0
		if (Vars.nimages < 1):
			iraf.printf("WARNING: No input image data found in `%s'.\
",Vars.PYin)
			iraf.delete(Vars.extlist, verify = no)
			continue
		if (not iraf.imaccess(Vars.wcsref)):
			Vars.ref = Vars.reference
			if (Vars.wcssource == 'match'):
				Vars.wcsref = Vars.ref
			else:
				iraf.mscwtemplate('@' + Vars.extlist, Vars.wcsref,wcssource = Vars.wcssource,reference = Vars.ref,ra = Vars.ra,dec = Vars.dec,scale = Vars.scale,rotation = Vars.rotation,projection = '',verbose = Vars.verbose)
		Vars.fd_ext = Vars.extlist
		while (iraf.fscan(locals(), 'Vars.fd_ext', 'Vars.image') != EOF):
			Vars.nimage = Vars.nimage + 1
			if (Vars.nimages > 1):
				Pipe1 = iraf.hselect(Vars.image, 'extname', yes, Stdout=1)
				iraf.scan(locals(), 'Vars.extname', Stdin=Pipe1)
				del Pipe1
				if (iraf.nscan() == 0):
					Vars.extname = 'im' + str(Vars.nimage)
				Pipe1 = iraf.printf('%s[%s,append]\n', Vars.outtemp,Vars.extname,Stdout=1)
				iraf.scan(locals(), 'Vars.outsec', Stdin=Pipe1)
				del Pipe1
				Pipe1 = iraf.printf('%s%s\n', Vars.pl, Vars.extname, Stdout=1)
				iraf.scan(locals(), 'Vars.plsec', Stdin=Pipe1)
				del Pipe1
			else:
				Vars.extname = ''
				Vars.outsec = Vars.outtemp
				Vars.plsec = Vars.pl
			if (Vars.pixmask and iraf.imaccess(Vars.plsec)):
				iraf.delete(Vars.coord, verify = no)
				iraf.delete(Vars.db, verify = no)
				iraf.printf('Warning: Mask already exists (%s)\n', Vars.plsec)
				continue
			if (Vars.verbose):
				iraf.printf('Resampling %s ...\n', Vars.image)
			Pipe1 = iraf.hselect(Vars.image, 'naxis1,naxis2', yes, Stdout=1)
			iraf.scan(locals(), 'Vars.nc', 'Vars.nl', Stdin=Pipe1)
			del Pipe1
			Vars.cmin = 1 + Vars.ntrim
			Vars.cmax = Vars.nc - Vars.ntrim
			Vars.lmin = 1 + Vars.ntrim
			Vars.lmax = Vars.nl - Vars.ntrim
			Pipe1 = iraf.printf('[%d:%d,%d:%d]\n', Vars.cmin, Vars.cmax,Vars.lmin,Vars.lmax,Stdout=1)
			iraf.scan(locals(), 'Vars.trimsec', Stdin=Pipe1)
			del Pipe1
			if (Vars.wcssource == 'match'):
				Pipe1 = iraf.hselect(Vars.ref, 'naxis1,naxis2', yes, Stdout=1)
				iraf.scan(locals(), 'Vars.ncref', 'Vars.nlref', Stdin=Pipe1)
				del Pipe1
				Vars.xmin = (Vars.ncref - 1.) / (Vars.nx - 1.)
				Vars.ymin = (Vars.nlref - 1.) / (Vars.ny - 1.)
				Vars.ymax = 1
				while (Vars.ymax <= Vars.nlref + 1):
					Vars.xmax = 1
					while (Vars.xmax <= Vars.ncref + 1):
						iraf.clPrint(Vars.xmax, Vars.ymax, Vars.xmax,Vars.ymax,StdoutAppend=Vars.coord)
						Vars.xmax = Vars.xmax + Vars.xmin
					Vars.ymax = Vars.ymax + Vars.ymin
				iraf.mscctran(Vars.coord, Vars.db, Vars.ref, 'logical','world',columns = '3 4',units = '',formats = '%.4H %.3h',min_sigdigit = 10,verbose = no)
				iraf.delete(Vars.coord, verify=no)
				iraf.wcsctran(Vars.db, Vars.coord, Vars.image + Vars.trimsec,inwcs = 'world',outwcs = 'logical',columns = '3 4',units = 'hours native',formats = '',min_sigdigit = 10,verbose = no)
				iraf.delete(Vars.db, verify=no)
			else:
				Vars.nc = Vars.cmax - Vars.cmin + 1
				Vars.nl = Vars.lmax - Vars.lmin + 1
				Vars.xmin = (Vars.nc - 1.) / (Vars.nx - 1.)
				Vars.ymin = (Vars.nl - 1.) / (Vars.ny - 1.)
				Vars.ymax = 1
				while (Vars.ymax <= Vars.nl + 1):
					Vars.xmax = 1
					while (Vars.xmax <= Vars.nc + 1):
						iraf.clPrint(Vars.xmax, Vars.ymax, Vars.xmax,Vars.ymax,StdoutAppend=Vars.coord)
						Vars.xmax = Vars.xmax + Vars.xmin
					Vars.ymax = Vars.ymax + Vars.ymin
				iraf.mscctran(Vars.coord, Vars.db, Vars.image + Vars.trimsec,'logical','world',columns = '1 2',units = '',formats = '%.4H %.3h',min_sigdigit = 10,verbose = no)
				iraf.delete(Vars.coord, verify=no)
				iraf.wcsctran(Vars.db, Vars.coord, Vars.wcsref,inwcs = 'world',outwcs = 'logical',columns = '1 2',units = 'hours native',formats = '',min_sigdigit = 10,verbose = no)
				iraf.delete(Vars.db, verify=no)
			Vars.xmax = 0.
			Vars.xmin = 1.
			Vars.ymax = 0.
			Vars.ymin = 1.
			Vars.fd_coord = Vars.coord
			while (iraf.fscan(locals(), 'Vars.fd_coord', 'Vars.x', 'Vars.y') != EOF):
				if (iraf.nscan() < 2):
					continue
				if (Vars.xmax < Vars.xmin):
					Vars.xmin = Vars.x
					Vars.xmax = Vars.x
					Vars.ymin = Vars.y
					Vars.ymax = Vars.y
				else:
					Vars.xmin = float(iraf.minimum(Vars.x, Vars.xmin))
					Vars.xmax = float(iraf.maximum(Vars.x, Vars.xmax))
					Vars.ymin = float(iraf.minimum(Vars.y, Vars.ymin))
					Vars.ymax = float(iraf.maximum(Vars.y, Vars.ymax))
			Vars.fd_coord = ''
			if (Vars.xmax <= Vars.xmin or Vars.ymax <= Vars.ymin):
				iraf.error(1, 'No overlap for matching reference')
			Vars.cmin = int(iraf.nint(Vars.xmin - 1.5))
			Vars.cmax = int(iraf.nint(Vars.xmax + 1.5))
			Vars.lmin = int(iraf.nint(Vars.ymin - 1.5))
			Vars.lmax = int(iraf.nint(Vars.ymax + 1.5))
			iraf.geomap(Vars.coord, Vars.db, Vars.cmin, Vars.cmax, Vars.lmin,Vars.lmax,transforms = '',results = '',fitgeometry = Vars.fitgeometry,function = 'chebyshev',xxorder = Vars.xxorder,xyorder = Vars.xyorder,xxterms = Vars.xxterms,yxorder = Vars.yxorder,yyorder = Vars.yyorder,yxterms = Vars.yxterms,reject = INDEF,calctype = 'double',verbose = no,interactive = Vars.interactive,graphics = 'stdgraph',cursor = '')
			if (Vars.wcssource == 'match'):
				Vars.cmin = 1
				Vars.lmin = 1
				Vars.cmax = Vars.ncref
				Vars.lmax = Vars.nlref
			if (Vars.nxblock == INDEF):
				Vars.nxblk = Vars.cmax - Vars.cmin + 3
			else:
				Vars.nxblk = Vars.nxblock
			if (Vars.nyblock == INDEF):
				Vars.nyblk = Vars.lmax - Vars.lmin + 3
			else:
				Vars.nyblk = Vars.nyblock
			iraf.geotran(Vars.image + Vars.trimsec, Vars.outsec, Vars.db,Vars.coord,geometry = 'geometric',xin = INDEF,yin = INDEF,xshift = INDEF,yshift = INDEF,xout = INDEF,yout = INDEF,xmag = INDEF,ymag = INDEF,xrotation = INDEF,yrotation = INDEF,xmin = Vars.cmin,xmax = Vars.cmax,ymin = Vars.lmin,ymax = Vars.lmax,xsample = 10.,ysample = 10.,xscale = 1.,yscale = 1.,ncols = INDEF,nlines = INDEF,interpolant = Vars.interpolant,boundary = 'constant',constant = Vars.constant,fluxconserve = Vars.fluxconserve,nxblock = Vars.nxblk,nyblock = Vars.nyblk,verbose = no)
			iraf.wcscopy(Vars.outsec, Vars.wcsref, verbose=no)
			Vars.xmin = 0.
			Vars.ymin = 0.
			Pipe1 = iraf.hselect(Vars.outsec, 'crpix1,crpix2', yes, Stdout=1)
			iraf.scan(locals(), 'Vars.xmin', 'Vars.ymin', Stdin=Pipe1)
			del Pipe1
			Vars.xmin = Vars.xmin - Vars.cmin + 1
			Vars.ymin = Vars.ymin - Vars.lmin + 1
			if (Vars.nimage == 1):
				Vars.crpix1 = Vars.xmin
				Vars.crpix2 = Vars.ymin
			else:
				Vars.crpix1 = float(iraf.maximum(Vars.crpix1, Vars.xmin))
				Vars.crpix2 = float(iraf.maximum(Vars.crpix2, Vars.ymin))
			iraf.hedit(Vars.outsec, 'crpix1', Vars.xmin, add=yes, verify=no,show=no,update=yes)
			iraf.hedit(Vars.outsec, 'crpix2', Vars.ymin, add=yes, verify=no,show=no,update=yes)
			if (Vars.pixmask):
				Pipe1 = iraf.printf('%s%s\n', Vars.pl, Vars.extname, Stdout=1)
				iraf.scan(locals(), 'Vars.plsec', Stdin=Pipe1)
				del Pipe1
				iraf.mscgmask(Vars.image + Vars.trimsec, Vars.pltemp + '.pl','BPM',mval = 10000)
				iraf.geotran(Vars.pltemp, Vars.plsec + '.fits', Vars.db,Vars.coord,geometry = 'geometric',xin = INDEF,yin = INDEF,xshift = INDEF,yshift = INDEF,xout = INDEF,yout = INDEF,xmag = INDEF,ymag = INDEF,xrotation = INDEF,yrotation = INDEF,xmin = Vars.cmin,xmax = Vars.cmax,ymin = Vars.lmin,ymax = Vars.lmax,xsample = 10.,ysample = 10.,interpolant = Vars.minterpolant,boundary = 'constant',constant = 20000.,fluxconserve = no,nxblock = Vars.nxblk,nyblock = Vars.nyblk,verbose = no)
				iraf.imdelete(Vars.pltemp, verify=no)
				iraf.mscpmask(Vars.plsec + '.fits', Vars.plsec + '.pl')
				iraf.imdelete(Vars.plsec + '.fits', verify=no)
				iraf.hedit(Vars.outsec, 'BPM', Vars.plsec + '.pl', add=yes,show=no,verify=no,update=yes)
				iraf.wcscopy(Vars.plsec, Vars.outsec, verbose=no)
				iraf.clPrint(Vars.plsec, StdoutAppend=Vars.pllist)
			else:
				iraf.hedit(Vars.outsec, 'BPM', PYdel=yes, add=no, addonly=no,show=no,verify=no,update=yes)
			iraf.delete(Vars.coord, verify = no)
			iraf.delete(Vars.db, verify = no)
		Vars.fd_ext = ''
		iraf.delete(Vars.extlist, verify = no)
		if (Vars.nimages > 1 and Vars.format == 'image'):
			if (Vars.verbose):
				iraf.printf('Creating image %s ...\n', Vars.out)
			iraf.mscextensions(Vars.outtemp, output = 'file', index = '',extname = '',extver = '',lindex = no,lname = yes,lver = no,ikparams = '',Stdout=Vars.extlist)
			if (Vars.pixmask):
				iraf.combine('@' + Vars.pllist, Vars.pltemp + '.pl',headers = '',bpmasks = Vars.pl,rejmasks = '',nrejmasks = '',expmasks = '',sigmas = '',imcmb = '',ccdtype = '',amps = no,subsets = no,delete = no,combine = 'average',reject = 'none',project = no,outtype = 'real',outlimits = '',offsets = 'wcs',masktype = 'none',maskvalue = '0',blank = 0.,scale = 'none',zero = 'none',weight = 'none',statsec = '',lthreshold = INDEF,hthreshold = 0.99,nlow = 1,nhigh = 1,nkeep = 1,mclip = yes,lsigma = 3.,hsigma = 3.,rdnoise = '0.',gain = '1.',snoise = '0.',sigscale = 0.1,pclip =  - 0.5,grow = 0.,Stdout='dev$null')
				iraf.imdelete(Vars.pltemp, verify=no)
				iraf.combine('@' + Vars.extlist, Vars.out, headers = '',bpmasks = '',rejmasks = '',nrejmasks = '',expmasks = '',sigmas = '',imcmb = '',ccdtype = '',amps = no,subsets = no,delete = no,combine = 'average',reject = 'none',project = no,outtype = 'real',outlimits = '',offsets = 'wcs',masktype = 'badvalue',maskvalue = '2',blank = 0.,scale = 'none',zero = 'none',weight = 'none',statsec = '',lthreshold = INDEF,hthreshold = INDEF,nlow = 1,nhigh = 1,nkeep = 1,mclip = yes,lsigma = 3.,hsigma = 3.,rdnoise = '0.',gain = '1.',snoise = '0.',sigscale = 0.1,pclip =  - 0.5,grow = 0.,Stdout='dev$null')
				iraf.hedit(Vars.out, 'BPM', Vars.pl, add=yes, verify=no,show=no,update=yes)
				iraf.hedit(Vars.pl, 'IMCMB???,PROCID??', add=no, addonly=no,PYdel=yes,update=yes,verify=no,show=no)
			else:
				iraf.combine('@' + Vars.extlist, Vars.out, headers = '',bpmasks = '',rejmasks = '',nrejmasks = '',expmasks = '',sigmas = '',imcmb = '',ccdtype = '',amps = no,subsets = no,delete = no,combine = 'average',reject = 'none',project = no,outtype = 'real',outlimits = '',offsets = 'wcs',masktype = 'none',maskvalue = '2',blank = 0.,scale = 'none',zero = 'none',weight = 'none',statsec = '',lthreshold = INDEF,hthreshold = INDEF,nlow = 1,nhigh = 1,nkeep = 1,mclip = yes,lsigma = 3.,hsigma = 3.,rdnoise = '0.',gain = '1.',snoise = '0.',sigscale = 0.1,pclip =  - 0.5,grow = 0.,Stdout='dev$null')
			Pipe2 = iraf.hselect('@' + Vars.extlist, 'gain', yes, Stdout=1)
			Pipe1 = iraf.average(data_value = 0., Stdin=Pipe2, Stdout=1)
			del Pipe2
			iraf.scan(locals(), 'Vars.rval', Stdin=Pipe1)
			del Pipe1
			iraf.hedit(Vars.out, 'gain', Vars.rval, add=yes, PYdel=no,update=yes,verify=no,show=no)
			Pipe2 = iraf.hselect('@' + Vars.extlist, 'rdnoise', yes, Stdout=1)
			Pipe1 = iraf.average(data_value = 0., Stdin=Pipe2, Stdout=1)
			del Pipe2
			iraf.scan(locals(), 'Vars.rval', Stdin=Pipe1)
			del Pipe1
			iraf.hedit(Vars.out, 'rdnoise', Vars.rval, add=yes, PYdel=no,update=yes,verify=no,show=no)
			iraf.hedit(Vars.out, 'IMCMB???,PROCID??', add=no, addonly=no,PYdel=yes,update=yes,verify=no,show=no)
			iraf.hedit(Vars.out,'NEXTEND,DETSEC,CCDSEC,AMPSEC,IMAGEID,DATASEC,TRIMSEC,BIASSEC',add=no,addonly=no,PYdel=yes,update=yes,verify=no,show=no)
			iraf.imdelete(Vars.outtemp, verify=no)
			if (iraf.access(Vars.pllist)):
				iraf.imdelete('@' + Vars.pllist, verify=no)
				iraf.delete(Vars.pllist, verify=no)
			iraf.delete(Vars.extlist, verify = no)
		elif (Vars.nimages > 1):
			iraf.imrename(Vars.outtemp, Vars.out, verbose=no)
			iraf.mscextensions(Vars.out, output = 'file', index = '',extname = '',extver = '',lindex = no,lname = yes,lver = no,ikparams = '',Stdout=Vars.extlist)
			Vars.fd_ext = Vars.extlist
			while (iraf.fscan(locals(), 'Vars.fd_ext', 'Vars.image') != EOF):
				Pipe1 = iraf.hselect(Vars.image, 'naxis1,naxis2,crpix1,crpix2',yes,Stdout=1)
				iraf.scan(locals(), 'Vars.nc', 'Vars.nl', 'Vars.xmin','Vars.ymin',Stdin=Pipe1)
				del Pipe1
				Vars.cmin = int(iraf.nint(Vars.crpix1 - Vars.xmin + 1))
				Vars.lmin = int(iraf.nint(Vars.crpix2 - Vars.ymin + 1))
				Vars.cmax = Vars.nc + Vars.cmin - 1
				Vars.lmax = Vars.nl + Vars.lmin - 1
				Pipe1 = iraf.printf('[%d:%d,%d:%d]\n', Vars.cmin, Vars.cmax,Vars.lmin,Vars.lmax,Stdout=1)
				iraf.scan(locals(), 'Vars.str', Stdin=Pipe1)
				del Pipe1
				iraf.hedit(Vars.image, 'DETSEC', Vars.str, add=yes, verify=no,show=no,update=yes)
				iraf.hedit(Vars.image, 'DTM1_1', 1., add=yes, verify=no,show=no,update=yes)
				iraf.hedit(Vars.image, 'DTM2_2', 1., add=yes, verify=no,show=no,update=yes)
				Vars.cmin = Vars.cmin - 1
				Vars.lmin = Vars.lmin - 1
				iraf.hedit(Vars.image, 'DTV1', Vars.cmin, add=yes, verify=no,show=no,update=yes)
				iraf.hedit(Vars.image, 'DTV2', Vars.lmin, add=yes, verify=no,show=no,update=yes)
				iraf.hedit(Vars.image,'CCDSUM,CCDSEC,AMPSEC,ATM1_1,ATM2_2,ATV1,ATV2',PYdel=yes,add=no,addonly=no,verify=no,show=no,update=yes)
			Vars.fd_ext = ''
			iraf.delete(Vars.extlist, verify=no)
		else:
			iraf.imrename(Vars.outsec, Vars.out, verbose=no)
		if (iraf.access(Vars.pllist)):
			iraf.delete(Vars.pllist, verify=no)
	Vars.fd_in = ''
	iraf.delete(Vars.inlists, verify = no)
	if (Vars.wcssource != 'match' and iraf.imaccess(Vars.wcsref)):
		iraf.imdelete(Vars.wcsref, verify=no)
コード例 #13
0
        src.delete('listtmp*')

    if glob.glob(out2):
        _date = src.date(out2, _header, _telescope)
        _ut = src.UT(out2, _header, _telescope)
        if not out:
            out = src.objects(out2, _header, _telescope)
            out = re.sub('-', '', out)
            if not out:
                out = 'SN'
        src.delete(out + '_' + _date + '_' +
                   str('0' + string.split(_ut, ':')[0])[-2:] + '_' + fil +
                   str(system0) + '.fits')
        iraf.imrename(out2,
                      out + '_' + _date + '_' +
                      str('0' + string.split(_ut, ':')[0])[-2:] + '_' + fil +
                      str(system0) + '.fits',
                      verbose='yes')

src.delete("tmp*")
src.delete("_templ.co*")
src.delete(coordinatelist + "_templ.coo")
src.delete(coordinatelist + ".tv")

src.close_program(logincl)

#x0,y0,z0=string.split(xycoo[0])
#hdulist = pyfits.open('tmp_imcomb.fits')
#wcs = pywcs.WCS(hdulist[0].header)
#pix = wcs.wcs_pix2sky(x0,y0, 1)
コード例 #14
0
def mscimage(input=None,
             output=None,
             format='image',
             pixmask=no,
             verbose=')_.verbose',
             wcssource='image',
             reference='',
             ra=INDEF,
             dec=INDEF,
             scale=INDEF,
             rotation=INDEF,
             blank=0.0,
             interpolant='poly5',
             minterpolant='linear',
             boundary='reflect',
             constant=0.0,
             fluxconserve=no,
             ntrim=8,
             nxblock=INDEF,
             nyblock=INDEF,
             interactive=no,
             nx=10,
             ny=20,
             fitgeometry='general',
             xxorder=4,
             xyorder=4,
             xxterms='half',
             yxorder=4,
             yyorder=4,
             yxterms='half',
             fd_in='',
             fd_ext='',
             fd_coord='',
             mode='ql',
             DOLLARnargs=0,
             taskObj=None):

    Vars = IrafParList('mscimage')
    Vars.addParam(
        makeIrafPar(input,
                    datatype='string',
                    name='input',
                    mode='a',
                    prompt='List of input mosaic exposures'))
    Vars.addParam(
        makeIrafPar(output,
                    datatype='string',
                    name='output',
                    mode='a',
                    prompt='List of output images'))
    Vars.addParam(
        makeIrafPar(format,
                    datatype='string',
                    name='format',
                    enum=['image', 'mef'],
                    mode='h',
                    prompt='Output format (image|mef)'))
    Vars.addParam(
        makeIrafPar(pixmask,
                    datatype='bool',
                    name='pixmask',
                    mode='h',
                    prompt='Create pixel mask?'))
    Vars.addParam(
        makeIrafPar(verbose,
                    datatype='bool',
                    name='verbose',
                    mode='h',
                    prompt='Verbose output?\n\n# Output WCS parameters'))
    Vars.addParam(
        makeIrafPar(wcssource,
                    datatype='string',
                    name='wcssource',
                    enum=['image', 'parameters', 'match'],
                    mode='h',
                    prompt='Output WCS source (image|parameters|match)'))
    Vars.addParam(
        makeIrafPar(reference,
                    datatype='file',
                    name='reference',
                    mode='h',
                    prompt='Reference image'))
    Vars.addParam(
        makeIrafPar(ra,
                    datatype='real',
                    name='ra',
                    max=24.0,
                    min=0.0,
                    mode='h',
                    prompt='RA of tangent point (hours)'))
    Vars.addParam(
        makeIrafPar(dec,
                    datatype='real',
                    name='dec',
                    max=90.0,
                    min=-90.0,
                    mode='h',
                    prompt='DEC of tangent point (degrees)'))
    Vars.addParam(
        makeIrafPar(scale,
                    datatype='real',
                    name='scale',
                    mode='h',
                    prompt='Scale (arcsec/pixel)'))
    Vars.addParam(
        makeIrafPar(
            rotation,
            datatype='real',
            name='rotation',
            max=360.0,
            min=-360.0,
            mode='h',
            prompt=
            'Rotation of DEC from N to E (degrees)\n\n# Resampling parmeters'))
    Vars.addParam(
        makeIrafPar(blank,
                    datatype='real',
                    name='blank',
                    mode='h',
                    prompt='Blank value'))
    Vars.addParam(
        makeIrafPar(interpolant,
                    datatype='string',
                    name='interpolant',
                    mode='h',
                    prompt='Interpolant for data'))
    Vars.addParam(
        makeIrafPar(minterpolant,
                    datatype='string',
                    name='minterpolant',
                    mode='h',
                    prompt='Interpolant for mask'))
    Vars.addParam(
        makeIrafPar(boundary,
                    datatype='string',
                    name='boundary',
                    enum=['nearest', 'constant', 'reflect', 'wrap'],
                    mode='h',
                    prompt='Boundary extension'))
    Vars.addParam(
        makeIrafPar(constant,
                    datatype='real',
                    name='constant',
                    mode='h',
                    prompt='Constant boundary extension value'))
    Vars.addParam(
        makeIrafPar(fluxconserve,
                    datatype='bool',
                    name='fluxconserve',
                    mode='h',
                    prompt='Preserve flux per unit area?'))
    Vars.addParam(
        makeIrafPar(ntrim,
                    datatype='int',
                    name='ntrim',
                    min=0,
                    mode='h',
                    prompt='Edge trim in each extension'))
    Vars.addParam(
        makeIrafPar(nxblock,
                    datatype='int',
                    name='nxblock',
                    mode='h',
                    prompt='X dimension of working block size in pixels'))
    Vars.addParam(
        makeIrafPar(
            nyblock,
            datatype='int',
            name='nyblock',
            mode='h',
            prompt=
            'Y dimension of working block size in pixels\n\n# Geometric mapping parameters'
        ))
    Vars.addParam(
        makeIrafPar(interactive,
                    datatype='bool',
                    name='interactive',
                    mode='h',
                    prompt='Fit mapping interactively?'))
    Vars.addParam(
        makeIrafPar(nx,
                    datatype='int',
                    name='nx',
                    mode='h',
                    prompt='Number of x grid points'))
    Vars.addParam(
        makeIrafPar(ny,
                    datatype='int',
                    name='ny',
                    mode='h',
                    prompt='Number of y grid points'))
    Vars.addParam(
        makeIrafPar(fitgeometry,
                    datatype='string',
                    name='fitgeometry',
                    enum=[
                        'shift', 'xyscale', 'rotate', 'rscale', 'rxyscale',
                        'general'
                    ],
                    mode='h',
                    prompt='Fitting geometry'))
    Vars.addParam(
        makeIrafPar(xxorder,
                    datatype='int',
                    name='xxorder',
                    min=2,
                    mode='h',
                    prompt='Order of x fit in x'))
    Vars.addParam(
        makeIrafPar(xyorder,
                    datatype='int',
                    name='xyorder',
                    min=2,
                    mode='h',
                    prompt='Order of x fit in y'))
    Vars.addParam(
        makeIrafPar(xxterms,
                    datatype='string',
                    name='xxterms',
                    mode='h',
                    prompt='X fit cross terms type'))
    Vars.addParam(
        makeIrafPar(yxorder,
                    datatype='int',
                    name='yxorder',
                    min=2,
                    mode='h',
                    prompt='Order of y fit in x'))
    Vars.addParam(
        makeIrafPar(yyorder,
                    datatype='int',
                    name='yyorder',
                    min=2,
                    mode='h',
                    prompt='Order of y fit in y'))
    Vars.addParam(
        makeIrafPar(yxterms,
                    datatype='string',
                    name='yxterms',
                    mode='h',
                    prompt='Y fit cross terms type\n\n'))
    Vars.addParam(
        makeIrafPar(fd_in,
                    datatype='struct',
                    name='fd_in',
                    list_flag=1,
                    mode='h',
                    prompt=''))
    Vars.addParam(
        makeIrafPar(fd_ext,
                    datatype='struct',
                    name='fd_ext',
                    list_flag=1,
                    mode='h',
                    prompt=''))
    Vars.addParam(
        makeIrafPar(fd_coord,
                    datatype='struct',
                    name='fd_coord',
                    list_flag=1,
                    mode='h',
                    prompt=''))
    Vars.addParam(
        makeIrafPar(mode, datatype='string', name='mode', mode='h', prompt=''))
    Vars.addParam(
        makeIrafPar(DOLLARnargs, datatype='int', name='$nargs', mode='h'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='in', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='out', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='ref', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='pl', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='image', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='trimsec', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='outsec', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='plsec', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='inlists', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='extlist', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='pllist', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='coord', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='db', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='wcsref', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='outtemp', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='pltemp', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='int', name='nc', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='int', name='nl', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='int', name='ncref', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='int', name='nlref', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='int', name='cmin', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='int', name='cmax', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='int', name='lmin', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='int', name='lmax', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='int', name='nimage', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='int', name='nimages', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='int', name='nxblk', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='int', name='nyblk', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='real', name='x', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='real', name='y', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='real', name='rval', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='real', name='xmin', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='real', name='xmax', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='real', name='ymin', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='real', name='ymax', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='real', name='crpix1', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='real', name='crpix2', mode='u'))
    Vars.addParam(
        makeIrafPar(None, datatype='string', name='extname', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='string', name='str', mode='u'))

    iraf.cache('mscextensions', 'mscgmask')
    Vars.inlists = iraf.mktemp('tmp$iraf')
    Vars.extlist = iraf.mktemp('tmp$iraf')
    Vars.pllist = iraf.mktemp('tmp$iraf')
    Vars.coord = iraf.mktemp('tmp$iraf')
    Vars.db = iraf.mktemp('tmp$iraf')
    Vars.outtemp = iraf.mktemp('tmp')
    Vars.wcsref = iraf.mktemp('tmp')
    Vars.pltemp = iraf.mktemp('tmp')
    iraf.joinlists(Vars.input,
                   Vars.output,
                   output=Vars.inlists,
                   delim=' ',
                   short=yes,
                   type='image')
    Vars.fd_in = Vars.inlists
    while (iraf.fscan(locals(), 'Vars.fd_in', 'Vars.PYin', 'Vars.out') != EOF):
        if (iraf.imaccess(Vars.out)):
            iraf.printf('Warning: Image already exists (%s)\n', Vars.out)
            continue
        if (Vars.pixmask):
            Vars.pl = Vars.out
            Vars.nc = iraf.strlen(Vars.pl)
            if (Vars.nc > 5
                    and iraf.substr(Vars.pl, Vars.nc - 4, Vars.nc) == '.fits'):
                Vars.pl = iraf.substr(Vars.pl, 1, Vars.nc - 5)
            elif (Vars.nc > 4
                  and iraf.substr(Vars.out, Vars.nc - 3, Vars.nc) == '.imh'):
                Vars.pl = iraf.substr(Vars.pl, 1, Vars.nc - 4)
            Vars.pl = Vars.pl + '_bpm'
            if (Vars.format == 'image' and iraf.imaccess(Vars.pl)):
                iraf.printf('Warning: Mask already exists (%s)\n', Vars.pl)
                continue
        else:
            Vars.pl = ''
        iraf.mscextensions(Vars.PYin,
                           output='file',
                           index='0-',
                           extname='',
                           extver='',
                           lindex=no,
                           lname=yes,
                           lver=no,
                           ikparams='',
                           Stdout=Vars.extlist)
        Vars.nimages = int(iraf.mscextensions.nimages)
        Vars.nimage = 0
        if (Vars.nimages < 1):
            iraf.printf("WARNING: No input image data found in `%s'.\
", Vars.PYin)
            iraf.delete(Vars.extlist, verify=no)
            continue
        if (not iraf.imaccess(Vars.wcsref)):
            Vars.ref = Vars.reference
            if (Vars.wcssource == 'match'):
                Vars.wcsref = Vars.ref
            else:
                iraf.mscwtemplate('@' + Vars.extlist,
                                  Vars.wcsref,
                                  wcssource=Vars.wcssource,
                                  reference=Vars.ref,
                                  ra=Vars.ra,
                                  dec=Vars.dec,
                                  scale=Vars.scale,
                                  rotation=Vars.rotation,
                                  projection='',
                                  verbose=Vars.verbose)
        Vars.fd_ext = Vars.extlist
        while (iraf.fscan(locals(), 'Vars.fd_ext', 'Vars.image') != EOF):
            Vars.nimage = Vars.nimage + 1
            if (Vars.nimages > 1):
                Pipe1 = iraf.hselect(Vars.image, 'extname', yes, Stdout=1)
                iraf.scan(locals(), 'Vars.extname', Stdin=Pipe1)
                del Pipe1
                if (iraf.nscan() == 0):
                    Vars.extname = 'im' + str(Vars.nimage)
                Pipe1 = iraf.printf('%s[%s,append]\n',
                                    Vars.outtemp,
                                    Vars.extname,
                                    Stdout=1)
                iraf.scan(locals(), 'Vars.outsec', Stdin=Pipe1)
                del Pipe1
                Pipe1 = iraf.printf('%s%s\n', Vars.pl, Vars.extname, Stdout=1)
                iraf.scan(locals(), 'Vars.plsec', Stdin=Pipe1)
                del Pipe1
            else:
                Vars.extname = ''
                Vars.outsec = Vars.outtemp
                Vars.plsec = Vars.pl
            if (Vars.pixmask and iraf.imaccess(Vars.plsec)):
                iraf.delete(Vars.coord, verify=no)
                iraf.delete(Vars.db, verify=no)
                iraf.printf('Warning: Mask already exists (%s)\n', Vars.plsec)
                continue
            if (Vars.verbose):
                iraf.printf('Resampling %s ...\n', Vars.image)
            Pipe1 = iraf.hselect(Vars.image, 'naxis1,naxis2', yes, Stdout=1)
            iraf.scan(locals(), 'Vars.nc', 'Vars.nl', Stdin=Pipe1)
            del Pipe1
            Vars.cmin = 1 + Vars.ntrim
            Vars.cmax = Vars.nc - Vars.ntrim
            Vars.lmin = 1 + Vars.ntrim
            Vars.lmax = Vars.nl - Vars.ntrim
            Pipe1 = iraf.printf('[%d:%d,%d:%d]\n',
                                Vars.cmin,
                                Vars.cmax,
                                Vars.lmin,
                                Vars.lmax,
                                Stdout=1)
            iraf.scan(locals(), 'Vars.trimsec', Stdin=Pipe1)
            del Pipe1
            if (Vars.wcssource == 'match'):
                Pipe1 = iraf.hselect(Vars.ref, 'naxis1,naxis2', yes, Stdout=1)
                iraf.scan(locals(), 'Vars.ncref', 'Vars.nlref', Stdin=Pipe1)
                del Pipe1
                Vars.xmin = (Vars.ncref - 1.) / (Vars.nx - 1.)
                Vars.ymin = (Vars.nlref - 1.) / (Vars.ny - 1.)
                Vars.ymax = 1
                while (Vars.ymax <= Vars.nlref + 1):
                    Vars.xmax = 1
                    while (Vars.xmax <= Vars.ncref + 1):
                        iraf.clPrint(Vars.xmax,
                                     Vars.ymax,
                                     Vars.xmax,
                                     Vars.ymax,
                                     StdoutAppend=Vars.coord)
                        Vars.xmax = Vars.xmax + Vars.xmin
                    Vars.ymax = Vars.ymax + Vars.ymin
                iraf.mscctran(Vars.coord,
                              Vars.db,
                              Vars.ref,
                              'logical',
                              'world',
                              columns='3 4',
                              units='',
                              formats='%.4H %.3h',
                              min_sigdigit=10,
                              verbose=no)
                iraf.delete(Vars.coord, verify=no)
                iraf.wcsctran(Vars.db,
                              Vars.coord,
                              Vars.image + Vars.trimsec,
                              inwcs='world',
                              outwcs='logical',
                              columns='3 4',
                              units='hours native',
                              formats='',
                              min_sigdigit=10,
                              verbose=no)
                iraf.delete(Vars.db, verify=no)
            else:
                Vars.nc = Vars.cmax - Vars.cmin + 1
                Vars.nl = Vars.lmax - Vars.lmin + 1
                Vars.xmin = (Vars.nc - 1.) / (Vars.nx - 1.)
                Vars.ymin = (Vars.nl - 1.) / (Vars.ny - 1.)
                Vars.ymax = 1
                while (Vars.ymax <= Vars.nl + 1):
                    Vars.xmax = 1
                    while (Vars.xmax <= Vars.nc + 1):
                        iraf.clPrint(Vars.xmax,
                                     Vars.ymax,
                                     Vars.xmax,
                                     Vars.ymax,
                                     StdoutAppend=Vars.coord)
                        Vars.xmax = Vars.xmax + Vars.xmin
                    Vars.ymax = Vars.ymax + Vars.ymin
                iraf.mscctran(Vars.coord,
                              Vars.db,
                              Vars.image + Vars.trimsec,
                              'logical',
                              'world',
                              columns='1 2',
                              units='',
                              formats='%.4H %.3h',
                              min_sigdigit=10,
                              verbose=no)
                iraf.delete(Vars.coord, verify=no)
                iraf.wcsctran(Vars.db,
                              Vars.coord,
                              Vars.wcsref,
                              inwcs='world',
                              outwcs='logical',
                              columns='1 2',
                              units='hours native',
                              formats='',
                              min_sigdigit=10,
                              verbose=no)
                iraf.delete(Vars.db, verify=no)
            Vars.xmax = 0.
            Vars.xmin = 1.
            Vars.ymax = 0.
            Vars.ymin = 1.
            Vars.fd_coord = Vars.coord
            while (iraf.fscan(locals(), 'Vars.fd_coord', 'Vars.x', 'Vars.y') !=
                   EOF):
                if (iraf.nscan() < 2):
                    continue
                if (Vars.xmax < Vars.xmin):
                    Vars.xmin = Vars.x
                    Vars.xmax = Vars.x
                    Vars.ymin = Vars.y
                    Vars.ymax = Vars.y
                else:
                    Vars.xmin = float(iraf.minimum(Vars.x, Vars.xmin))
                    Vars.xmax = float(iraf.maximum(Vars.x, Vars.xmax))
                    Vars.ymin = float(iraf.minimum(Vars.y, Vars.ymin))
                    Vars.ymax = float(iraf.maximum(Vars.y, Vars.ymax))
            Vars.fd_coord = ''
            if (Vars.xmax <= Vars.xmin or Vars.ymax <= Vars.ymin):
                iraf.error(1, 'No overlap for matching reference')
            Vars.cmin = int(iraf.nint(Vars.xmin - 1.5))
            Vars.cmax = int(iraf.nint(Vars.xmax + 1.5))
            Vars.lmin = int(iraf.nint(Vars.ymin - 1.5))
            Vars.lmax = int(iraf.nint(Vars.ymax + 1.5))
            iraf.geomap(Vars.coord,
                        Vars.db,
                        Vars.cmin,
                        Vars.cmax,
                        Vars.lmin,
                        Vars.lmax,
                        transforms='',
                        results='',
                        fitgeometry=Vars.fitgeometry,
                        function='chebyshev',
                        xxorder=Vars.xxorder,
                        xyorder=Vars.xyorder,
                        xxterms=Vars.xxterms,
                        yxorder=Vars.yxorder,
                        yyorder=Vars.yyorder,
                        yxterms=Vars.yxterms,
                        reject=INDEF,
                        calctype='double',
                        verbose=no,
                        interactive=Vars.interactive,
                        graphics='stdgraph',
                        cursor='')
            if (Vars.wcssource == 'match'):
                Vars.cmin = 1
                Vars.lmin = 1
                Vars.cmax = Vars.ncref
                Vars.lmax = Vars.nlref
            if (Vars.nxblock == INDEF):
                Vars.nxblk = Vars.cmax - Vars.cmin + 3
            else:
                Vars.nxblk = Vars.nxblock
            if (Vars.nyblock == INDEF):
                Vars.nyblk = Vars.lmax - Vars.lmin + 3
            else:
                Vars.nyblk = Vars.nyblock
            iraf.geotran(Vars.image + Vars.trimsec,
                         Vars.outsec,
                         Vars.db,
                         Vars.coord,
                         geometry='geometric',
                         xin=INDEF,
                         yin=INDEF,
                         xshift=INDEF,
                         yshift=INDEF,
                         xout=INDEF,
                         yout=INDEF,
                         xmag=INDEF,
                         ymag=INDEF,
                         xrotation=INDEF,
                         yrotation=INDEF,
                         xmin=Vars.cmin,
                         xmax=Vars.cmax,
                         ymin=Vars.lmin,
                         ymax=Vars.lmax,
                         xsample=10.,
                         ysample=10.,
                         xscale=1.,
                         yscale=1.,
                         ncols=INDEF,
                         nlines=INDEF,
                         interpolant=Vars.interpolant,
                         boundary='constant',
                         constant=Vars.constant,
                         fluxconserve=Vars.fluxconserve,
                         nxblock=Vars.nxblk,
                         nyblock=Vars.nyblk,
                         verbose=no)
            iraf.wcscopy(Vars.outsec, Vars.wcsref, verbose=no)
            Vars.xmin = 0.
            Vars.ymin = 0.
            Pipe1 = iraf.hselect(Vars.outsec, 'crpix1,crpix2', yes, Stdout=1)
            iraf.scan(locals(), 'Vars.xmin', 'Vars.ymin', Stdin=Pipe1)
            del Pipe1
            Vars.xmin = Vars.xmin - Vars.cmin + 1
            Vars.ymin = Vars.ymin - Vars.lmin + 1
            if (Vars.nimage == 1):
                Vars.crpix1 = Vars.xmin
                Vars.crpix2 = Vars.ymin
            else:
                Vars.crpix1 = float(iraf.maximum(Vars.crpix1, Vars.xmin))
                Vars.crpix2 = float(iraf.maximum(Vars.crpix2, Vars.ymin))
            iraf.hedit(Vars.outsec,
                       'crpix1',
                       Vars.xmin,
                       add=yes,
                       verify=no,
                       show=no,
                       update=yes)
            iraf.hedit(Vars.outsec,
                       'crpix2',
                       Vars.ymin,
                       add=yes,
                       verify=no,
                       show=no,
                       update=yes)
            if (Vars.pixmask):
                Pipe1 = iraf.printf('%s%s\n', Vars.pl, Vars.extname, Stdout=1)
                iraf.scan(locals(), 'Vars.plsec', Stdin=Pipe1)
                del Pipe1
                iraf.mscgmask(Vars.image + Vars.trimsec,
                              Vars.pltemp + '.pl',
                              'BPM',
                              mval=10000)
                iraf.geotran(Vars.pltemp,
                             Vars.plsec + '.fits',
                             Vars.db,
                             Vars.coord,
                             geometry='geometric',
                             xin=INDEF,
                             yin=INDEF,
                             xshift=INDEF,
                             yshift=INDEF,
                             xout=INDEF,
                             yout=INDEF,
                             xmag=INDEF,
                             ymag=INDEF,
                             xrotation=INDEF,
                             yrotation=INDEF,
                             xmin=Vars.cmin,
                             xmax=Vars.cmax,
                             ymin=Vars.lmin,
                             ymax=Vars.lmax,
                             xsample=10.,
                             ysample=10.,
                             interpolant=Vars.minterpolant,
                             boundary='constant',
                             constant=20000.,
                             fluxconserve=no,
                             nxblock=Vars.nxblk,
                             nyblock=Vars.nyblk,
                             verbose=no)
                iraf.imdelete(Vars.pltemp, verify=no)
                iraf.mscpmask(Vars.plsec + '.fits', Vars.plsec + '.pl')
                iraf.imdelete(Vars.plsec + '.fits', verify=no)
                iraf.hedit(Vars.outsec,
                           'BPM',
                           Vars.plsec + '.pl',
                           add=yes,
                           show=no,
                           verify=no,
                           update=yes)
                iraf.wcscopy(Vars.plsec, Vars.outsec, verbose=no)
                iraf.clPrint(Vars.plsec, StdoutAppend=Vars.pllist)
            else:
                iraf.hedit(Vars.outsec,
                           'BPM',
                           PYdel=yes,
                           add=no,
                           addonly=no,
                           show=no,
                           verify=no,
                           update=yes)
            iraf.delete(Vars.coord, verify=no)
            iraf.delete(Vars.db, verify=no)
        Vars.fd_ext = ''
        iraf.delete(Vars.extlist, verify=no)
        if (Vars.nimages > 1 and Vars.format == 'image'):
            if (Vars.verbose):
                iraf.printf('Creating image %s ...\n', Vars.out)
            iraf.mscextensions(Vars.outtemp,
                               output='file',
                               index='',
                               extname='',
                               extver='',
                               lindex=no,
                               lname=yes,
                               lver=no,
                               ikparams='',
                               Stdout=Vars.extlist)
            if (Vars.pixmask):
                iraf.combine('@' + Vars.pllist,
                             Vars.pltemp + '.pl',
                             headers='',
                             bpmasks=Vars.pl,
                             rejmasks='',
                             nrejmasks='',
                             expmasks='',
                             sigmas='',
                             imcmb='',
                             ccdtype='',
                             amps=no,
                             subsets=no,
                             delete=no,
                             combine='average',
                             reject='none',
                             project=no,
                             outtype='real',
                             outlimits='',
                             offsets='wcs',
                             masktype='none',
                             maskvalue='0',
                             blank=0.,
                             scale='none',
                             zero='none',
                             weight='none',
                             statsec='',
                             lthreshold=INDEF,
                             hthreshold=0.99,
                             nlow=1,
                             nhigh=1,
                             nkeep=1,
                             mclip=yes,
                             lsigma=3.,
                             hsigma=3.,
                             rdnoise='0.',
                             gain='1.',
                             snoise='0.',
                             sigscale=0.1,
                             pclip=-0.5,
                             grow=0.,
                             Stdout='dev$null')
                iraf.imdelete(Vars.pltemp, verify=no)
                iraf.combine('@' + Vars.extlist,
                             Vars.out,
                             headers='',
                             bpmasks='',
                             rejmasks='',
                             nrejmasks='',
                             expmasks='',
                             sigmas='',
                             imcmb='',
                             ccdtype='',
                             amps=no,
                             subsets=no,
                             delete=no,
                             combine='average',
                             reject='none',
                             project=no,
                             outtype='real',
                             outlimits='',
                             offsets='wcs',
                             masktype='badvalue',
                             maskvalue='2',
                             blank=0.,
                             scale='none',
                             zero='none',
                             weight='none',
                             statsec='',
                             lthreshold=INDEF,
                             hthreshold=INDEF,
                             nlow=1,
                             nhigh=1,
                             nkeep=1,
                             mclip=yes,
                             lsigma=3.,
                             hsigma=3.,
                             rdnoise='0.',
                             gain='1.',
                             snoise='0.',
                             sigscale=0.1,
                             pclip=-0.5,
                             grow=0.,
                             Stdout='dev$null')
                iraf.hedit(Vars.out,
                           'BPM',
                           Vars.pl,
                           add=yes,
                           verify=no,
                           show=no,
                           update=yes)
                iraf.hedit(Vars.pl,
                           'IMCMB???,PROCID??',
                           add=no,
                           addonly=no,
                           PYdel=yes,
                           update=yes,
                           verify=no,
                           show=no)
            else:
                iraf.combine('@' + Vars.extlist,
                             Vars.out,
                             headers='',
                             bpmasks='',
                             rejmasks='',
                             nrejmasks='',
                             expmasks='',
                             sigmas='',
                             imcmb='',
                             ccdtype='',
                             amps=no,
                             subsets=no,
                             delete=no,
                             combine='average',
                             reject='none',
                             project=no,
                             outtype='real',
                             outlimits='',
                             offsets='wcs',
                             masktype='none',
                             maskvalue='2',
                             blank=0.,
                             scale='none',
                             zero='none',
                             weight='none',
                             statsec='',
                             lthreshold=INDEF,
                             hthreshold=INDEF,
                             nlow=1,
                             nhigh=1,
                             nkeep=1,
                             mclip=yes,
                             lsigma=3.,
                             hsigma=3.,
                             rdnoise='0.',
                             gain='1.',
                             snoise='0.',
                             sigscale=0.1,
                             pclip=-0.5,
                             grow=0.,
                             Stdout='dev$null')
            Pipe2 = iraf.hselect('@' + Vars.extlist, 'gain', yes, Stdout=1)
            Pipe1 = iraf.average(data_value=0., Stdin=Pipe2, Stdout=1)
            del Pipe2
            iraf.scan(locals(), 'Vars.rval', Stdin=Pipe1)
            del Pipe1
            iraf.hedit(Vars.out,
                       'gain',
                       Vars.rval,
                       add=yes,
                       PYdel=no,
                       update=yes,
                       verify=no,
                       show=no)
            Pipe2 = iraf.hselect('@' + Vars.extlist, 'rdnoise', yes, Stdout=1)
            Pipe1 = iraf.average(data_value=0., Stdin=Pipe2, Stdout=1)
            del Pipe2
            iraf.scan(locals(), 'Vars.rval', Stdin=Pipe1)
            del Pipe1
            iraf.hedit(Vars.out,
                       'rdnoise',
                       Vars.rval,
                       add=yes,
                       PYdel=no,
                       update=yes,
                       verify=no,
                       show=no)
            iraf.hedit(Vars.out,
                       'IMCMB???,PROCID??',
                       add=no,
                       addonly=no,
                       PYdel=yes,
                       update=yes,
                       verify=no,
                       show=no)
            iraf.hedit(
                Vars.out,
                'NEXTEND,DETSEC,CCDSEC,AMPSEC,IMAGEID,DATASEC,TRIMSEC,BIASSEC',
                add=no,
                addonly=no,
                PYdel=yes,
                update=yes,
                verify=no,
                show=no)
            iraf.imdelete(Vars.outtemp, verify=no)
            if (iraf.access(Vars.pllist)):
                iraf.imdelete('@' + Vars.pllist, verify=no)
                iraf.delete(Vars.pllist, verify=no)
            iraf.delete(Vars.extlist, verify=no)
        elif (Vars.nimages > 1):
            iraf.imrename(Vars.outtemp, Vars.out, verbose=no)
            iraf.mscextensions(Vars.out,
                               output='file',
                               index='',
                               extname='',
                               extver='',
                               lindex=no,
                               lname=yes,
                               lver=no,
                               ikparams='',
                               Stdout=Vars.extlist)
            Vars.fd_ext = Vars.extlist
            while (iraf.fscan(locals(), 'Vars.fd_ext', 'Vars.image') != EOF):
                Pipe1 = iraf.hselect(Vars.image,
                                     'naxis1,naxis2,crpix1,crpix2',
                                     yes,
                                     Stdout=1)
                iraf.scan(locals(),
                          'Vars.nc',
                          'Vars.nl',
                          'Vars.xmin',
                          'Vars.ymin',
                          Stdin=Pipe1)
                del Pipe1
                Vars.cmin = int(iraf.nint(Vars.crpix1 - Vars.xmin + 1))
                Vars.lmin = int(iraf.nint(Vars.crpix2 - Vars.ymin + 1))
                Vars.cmax = Vars.nc + Vars.cmin - 1
                Vars.lmax = Vars.nl + Vars.lmin - 1
                Pipe1 = iraf.printf('[%d:%d,%d:%d]\n',
                                    Vars.cmin,
                                    Vars.cmax,
                                    Vars.lmin,
                                    Vars.lmax,
                                    Stdout=1)
                iraf.scan(locals(), 'Vars.str', Stdin=Pipe1)
                del Pipe1
                iraf.hedit(Vars.image,
                           'DETSEC',
                           Vars.str,
                           add=yes,
                           verify=no,
                           show=no,
                           update=yes)
                iraf.hedit(Vars.image,
                           'DTM1_1',
                           1.,
                           add=yes,
                           verify=no,
                           show=no,
                           update=yes)
                iraf.hedit(Vars.image,
                           'DTM2_2',
                           1.,
                           add=yes,
                           verify=no,
                           show=no,
                           update=yes)
                Vars.cmin = Vars.cmin - 1
                Vars.lmin = Vars.lmin - 1
                iraf.hedit(Vars.image,
                           'DTV1',
                           Vars.cmin,
                           add=yes,
                           verify=no,
                           show=no,
                           update=yes)
                iraf.hedit(Vars.image,
                           'DTV2',
                           Vars.lmin,
                           add=yes,
                           verify=no,
                           show=no,
                           update=yes)
                iraf.hedit(Vars.image,
                           'CCDSUM,CCDSEC,AMPSEC,ATM1_1,ATM2_2,ATV1,ATV2',
                           PYdel=yes,
                           add=no,
                           addonly=no,
                           verify=no,
                           show=no,
                           update=yes)
            Vars.fd_ext = ''
            iraf.delete(Vars.extlist, verify=no)
        else:
            iraf.imrename(Vars.outsec, Vars.out, verbose=no)
        if (iraf.access(Vars.pllist)):
            iraf.delete(Vars.pllist, verify=no)
    Vars.fd_in = ''
    iraf.delete(Vars.inlists, verify=no)
    if (Vars.wcssource != 'match' and iraf.imaccess(Vars.wcsref)):
        iraf.imdelete(Vars.wcsref, verify=no)