Ejemplo n.º 1
0
def rmcrimg(obj):
    iraf.lacos_im(input='f' + obj,
                  output='crf' + obj,
                  outmask='mask' + obj,
                  niter=4,
                  verbose='No')
    print(obj + ' finished.')
Ejemplo n.º 2
0
def run_lacosmic(filename, sigclip, sigfrac, objlim, niter, sigclip_pf):
    """Runs ``IRAF/LACosmic`` over an FLT file.

    Parameters:
        filename : string
            Name of the FITS file, including the path.
        sigclip : float
            Detection limit for cosmic rays.
        sigfrac : float
            Detection limit for adjacent pixels.
        objlim : int
            Max number of objects desired in image.
        niter : int
            Number of iterations of cosmic ray finder.
        sigclip_pf : float
            Detection limit for cosmic rays in Post-Flashed images.
            Set to 0.0 if no Post-Flashed data.

    Returns:
        nothing

    Outputs:
        ``IRAF/LACosmic`` cleaned FITS file,
        ``<file rootname>.clean.fits``.
    """
    filename = str(filename)
    sigclip = float(sigclip)
    sigfrac = float(sigfrac)
    objlim = int(objlim)
    niter = int(niter)
    sigclip_pf = float(sigclip_pf)

    # Read the header for whether the image is post-flashed.
    fits_file = fits.open(filename)
    flshcorr = fits_file[0].header['FLSHCORR']
    fits_file.close()

    if sigclip_pf == 0.0 or flshcorr == 'OMIT':
        sigclip = sigclip
        print 'FLSHCORR set to OMIT.'
    elif flshcorr == 'COMPLETE':
        sigclip = sigclip_pf
        print 'FLSHCORR set to COMPLETE.'
    iraf.lacos_im(filename+'[1]', \
                  filename.split('.fits')[0]+'.clean.fits', \
                  filename.split('.fits')[0]+'.mask.fits', \
                  gain=1.5, \
                  readn=3.0, \
                  sigclip=sigclip, \
                  sigfrac=sigfrac, \
                  objlim=objlim, \
                  niter=niter)
Ejemplo n.º 3
0
def p60sub(refimage, inlist, ra=-1, dec=-1, dx=-1, dy=-1, \
 nsx=5, nsy=5, sx=1, sy=1, sat1=60000, sat2=60000, min=5, ms=130, dback=1, \
 dspace=2, hms=9, hss=15, thresh=20, seeing=5.0, scale=0.0, tol=0.005, \
 matchlist="", goodmatch=2.0, aorder=2, nstars=30, outim="", MAX=900, \
 order=3, clobber=globclob):
    """ subtraction pipeline for one object/filter P60 data """

    # Parse inputs
    infiles = iraffiles(inlist)

    # Find location (in pixels) of central object if specified
    [nx, ny] = get_head(refimage, ['NAXIS1', 'NAXIS2'])
    if not ((ra == -1) and (dec == -1)):
        [xcenpix, ycenpix] = imwcs2pix(refimage, ra, dec)
    else:
        xcenpix = (nx / 2.0)
        ycenpix = (ny / 2.0)
        [ra, dec] = impix2wcs(refimage, xcenpix, ycenpix)

    # Trim the reference image
    if (dx == -1):
        xl = 1
        xu = nx
    else:
        xl = int(xcenpix - (dx / 2.0))
        xu = int(xcenpix + (dx / 2.0))
    if (dy == -1):
        yl = 1
        yu = ny
    else:
        yl = int(ycenpix - (dy / 2.0))
        yu = int(ycenpix + (dy / 2.0))

    check_exist('t' + refimage, 'w', clobber)
    iraf.imcopy(refimage + '[%i:%i,%i:%i]' % (xl, xu, yl, yu), \
  't' + refimage)

    # Big Loop
    for image in infiles:

        # Separate image rootname from extension
        imageroot = image.split('.')[0]

        # Check to make sure the WCS fit was successful
        if not check_head(image, 'IQWCS'):
            print 'Skipping image %s: IQWCS not successful' % image
            infiles.remove(image)
            continue
        # If so, then shift all the images appropriately
        else:
            xsh = xcenpix - imwcs2pix(image, ra, dec)[0]
            ysh = ycenpix - imwcs2pix(image, ra, dec)[1]
            check_exist('s' + image, 'w', clobber)
            iraf.imshift(image, 's' + image, xsh, ysh)

        # Make sure sky subtraction is not done
        [skysub, skybkg] = get_head(image, ['SKYSUB', 'SKYBKG'])
        if (skysub == 1):
            iraf.imarith('s' + image, '+', skybkg, 's' + image)

        # Trim the images
        check_exist('ts' + image, 'w', clobber)
        iraf.imcopy('s' + image + '[%i:%i,%i:%i]' % (xl, xu, yl, yu), \
         'ts' + image)

        # Cosmic Ray Cleaning
        iraf.lacos_im('ts%s' % image, 'cts%s' % image, 'ts%s_crmask' % \
         imageroot, gain=2.2, readn=5.3, niter=3)

        # Register the images
        os.system(
            '$REDUCTION/imregister_new.pl t%s cts%s -sat %i -thresh %i -s %f -scale %f -tol %f %s -o %i -g %i -n %i -out cts%s.shift.fits -max %i'
            % (refimage, image, sat1, thresh, seeing, scale, tol, matchlist,
               order, goodmatch, nstars, imageroot, MAX))

        # Write the configuration parameters for subtraction
        pfile = open('default_config', 'w+')
        pfile.write('nstamps_x		%i\n' % nsx)
        pfile.write('nstamps_y		%i\n' % nsy)
        pfile.write('sub_x		%i\n' % sx)
        pfile.write('sub_y		%i\n' % sy)
        pfile.write('half_mesh_size	%i\n' % hms)
        pfile.write('half_stamp_size	%i\n' % hss)
        pfile.write('deg_bg		%i\n' % dback)
        pfile.write('saturation1	%f\n' % sat1)
        pfile.write('saturation2        %f\n' % sat2)
        pfile.write('pix_min		%i\n' % min)
        pfile.write('min_stamp_center  	%i\n' % ms)
        pfile.write('ngauss		3\n')
        pfile.write('deg_gauss1		6\n')
        pfile.write('deg_gauss2		4\n')
        pfile.write('deg_gauss3		2\n')
        pfile.write('sigma_gauss1	0.7\n')
        pfile.write('sigma_gauss2	1.5\n')
        pfile.write('sigma_gauss3	2.0\n')
        pfile.write('deg_spatial	%i\n' % dspace)
        pfile.write('reverse		0\n')
        pfile.write('stampsbyxy		0\n')
        pfile.close()

        # Run the subtraction
        os.system("$REDUCTION/ISIS/bin/mrj_phot t%s cts%s.shift.fits" \
         % (refimage, imageroot))

        # Clean up files
        os.system("mv conv.fits cts%s.shift.sub.fits" % imageroot)
        os.system("mv conv0.fits cts%s.shift.conv.fits" % imageroot)

    # Return
    print 'Exiting successfully'
    return
Ejemplo n.º 4
0
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)
Ejemplo n.º 5
0
                               zerocor=1,
                               darkcor=0,
                               flatcor=1)

#remove cosmic ray automatically
iraf.stsdas()
iraf.unlearn('lacos_im')
image_spectrum = [
    inf_file[j][0] for j in range(len(inf_file))
    if inf_file[j][-1] in ['o', 'c']
]
[
    iraf.lacos_im(image_spectrum[j],
                  image_spectrum[j].replace('.fit', '_clean.fit'),
                  image_spectrum[j].replace('.fit', '_clean.pl'),
                  gain=Gain,
                  readn=Ron,
                  sigclip=4,
                  sigfrac=2,
                  niter=3) for j in range(len(image_spectrum))
]
pl_files = find_files(Path_Folder_Obj, '_clean.pl')
[os.remove(pl_files[j]) for j in range(len(pl_files))]
[os.remove(image_spectrum[j]) for j in range(len(image_spectrum))]
[
    iraf.imcopy(image_spectrum[j].replace('.fit', '_clean.fit'),
                image_spectrum[j]) for j in range(len(image_spectrum))
]
clean_files = find_files(Path_Folder_Obj, '_clean.fit')
[os.remove(clean_files[j]) for j in range(len(clean_files))]
fits_file = find_files(Path_Folder_Obj, '.fits')
[os.remove(fits_file[j]) for j in range(len(fits_file))]
Ejemplo n.º 6
0
def iq_ratir(chip="C1", filt="i", reflist="sdss.reg"):
    '''Process RATIR data for given chip and filter'''

    # Check and see if bias frame exists.  If not, create one
    if not os.path.exists("Bias-%s.fits" % chip):

        imlist = glob.glob("[0-9]*T[0-9]*%sb.fits" % chip)
        hdr = pyfits.getheader(imlist[0])

        # Setup zerocombine
        zerocombine = iraf.ccdred.zerocombine
        zerocombine.combine = 'median'
        zerocombine.reject = 'avsigclip'
        zerocombine.ccdtype = ''
        zerocombine.process = no
        zerocombine.delete = no
        zerocombine.clobber = no
        zerocombine.scale = 'none'
        zerocombine.statsec = '*'
        zerocombine.nlow = 0
        zerocombine.nhigh = 1
        zerocombine.nkeep = 1
        zerocombine.mclip = yes
        zerocombine.lsigma = 3.0
        zerocombine.hsigma = 3.0
        zerocombine.rdnoise = 0.0
        zerocombine.gain = hdr['SOFTGAIN']
        zerocombine.snoise = 0
        zerocombine.pclip = -0.5
        zerocombine.blank = 0.0

        # Run zerocombine
        bstr = ",".join(imlist)
        zerocombine(input=bstr, output="Bias-%s.fits" % chip)

    # Bias subtract flat frames
    imlist = glob.glob("[0-9]*T[0-9]*%sf.fits" % chip)
    flis = []
    for im in imlist:
        hdr = pyfits.getheader(im)
        if (hdr["FILTER"] == filt) and (not os.path.exists("b%s" % im)):

            # Subtract bias frame
            ccdproc = iraf.ccdred.ccdproc
            ccdproc.ccdtype = ""
            ccdproc.noproc = no
            ccdproc.fixpix = no
            ccdproc.overscan = no
            ccdproc.trim = no
            ccdproc.zerocor = yes
            ccdproc.darkcor = no
            ccdproc.flatcor = no
            ccdproc.illumcor = no
            ccdproc.fringecor = no
            ccdproc.readcor = no
            ccdproc.scancor = no
            ccdproc.readaxis = 'line'
            ccdproc.fixfile = ""
            ccdproc.zero = "Bias-%s.fits" % chip
            ccdproc.interactive = no
            ccdproc.function = "legendre"
            ccdproc.order = 1
            ccdproc.sample = "*"
            ccdproc.naverage = 1
            ccdproc.niterate = 1
            ccdproc.low_reject = 3.0
            ccdproc.high_reject = 3.0
            ccdproc.grow = 0
            ccdproc(images=im, output="b%s" % im)

            flis.append("b%s" % im)
        elif (hdr["FILTER"] == filt):
            flis.append("b%s" % im)

    # Create flat field
    if not os.path.exists("Flat-%s-%s.fits" % (chip, filt)):

        glis = []
        for im in flis:
            iraf.iterstat.nsigrej = 5.0
            iraf.iterstat.maxiter = 10
            iraf.iterstat.verbose = globver
            iraf.iterstat.lower = INDEF
            iraf.iterstat.upper = INDEF
            iraf.iterstat(im)
            if (iraf.iterstat.median <
                    RATIRSAT[chip]) and (iraf.iterstat.median > 1000.0):
                glis.append(im)

        # Set up flatcombine
        flatcombine = iraf.ccdred.flatcombine
        flatcombine.combine = 'median'
        flatcombine.reject = 'avsigclip'
        flatcombine.ccdtype = ''
        flatcombine.scale = 'median'
        flatcombine.statsec = ''
        flatcombine.nlow = 1
        flatcombine.nhigh = 1
        flatcombine.nkeep = 1
        flatcombine.mclip = yes
        flatcombine.lsigma = 3.0
        flatcombine.hsigma = 3.0
        flatcombine.rdnoise = 0.0
        flatcombine.gain = hdr["SOFTGAIN"]
        flatcombine.snoise = 0.0
        flatcombine.pclip = -0.5

        # Run flatcombine
        fstr = ",".join(glis[:10])
        flatcombine(fstr, output="Flat-%s-%s.fits" % (chip, filt))

        # Normalize
        iraf.iterstat.nsigrej = 5.0
        iraf.iterstat.maxiter = 10
        iraf.iterstat.verbose = globver
        iraf.iterstat.lower = INDEF
        iraf.iterstat.upper = INDEF
        iraf.iterstat("Flat-%s-%s.fits" % (chip, filt))
        iraf.imarith("Flat-%s-%s.fits" % (chip, filt), "/",
                     iraf.iterstat.median, "Flat-%s-%s.fits" % (chip, filt))

    # Bias subtract and flat-field science images
    imlist = glob.glob("[0-9]*T[0-9]*%so_img_1.fits" % chip)
    for im in imlist:
        hdr = pyfits.getheader(im)
        if (hdr["FILTER"] == filt) and (not os.path.exists("fb%s" % im)):

            hdr = pyfits.getheader(im)

            # Subtract bias frame
            ccdproc = iraf.ccdred.ccdproc
            ccdproc.ccdtype = ""
            ccdproc.noproc = no
            ccdproc.fixpix = no
            ccdproc.overscan = no
            ccdproc.trim = no
            ccdproc.zerocor = yes
            ccdproc.darkcor = no
            ccdproc.flatcor = yes
            ccdproc.illumcor = no
            ccdproc.fringecor = no
            ccdproc.readcor = no
            ccdproc.scancor = no
            ccdproc.readaxis = 'line'
            ccdproc.fixfile = ""
            ccdproc.zero = "Bias-%s.fits" % chip
            ccdproc.flat = "Flat-%s-%s.fits" % (chip, filt)
            ccdproc.interactive = no
            ccdproc.function = "legendre"
            ccdproc.order = 1
            ccdproc.sample = "*"
            ccdproc.naverage = 1
            ccdproc.niterate = 1
            ccdproc.low_reject = 3.0
            ccdproc.high_reject = 3.0
            ccdproc.grow = 0
            ccdproc(images=im, output="fb%s" % im)

    # Create sky frame (including dark current!)
    if not os.path.exists("Sky-%s-%s.fits" % (chip, filt)):
        imlist = glob.glob("fb[0-9]*T[0-9]*%so_img_1.fits" % chip)
        hdr = pyfits.getheader(imlist[0])
        slis = []
        for im in imlist:
            if (hdr["FILTER"] == filt):
                slis.append(im)

        # Set up combine
        imcombine = iraf.immatch.imcombine
        imcombine.headers = ""
        imcombine.bpmasks = ""
        imcombine.rejmasks = ""
        imcombine.nrejmasks = ""
        imcombine.expmasks = ""
        imcombine.sigmas = ""
        imcombine.combine = "median"
        imcombine.reject = "avsigclip"
        imcombine.project = no
        imcombine.outtype = "real"
        imcombine.offsets = "none"
        imcombine.masktype = "none"
        imcombine.scale = "median"
        imcombine.zero = "none"
        imcombine.weight = "none"
        imcombine.statsec = ""
        imcombine.lsigma = 3.0
        imcombine.hsigma = 3.0
        imcombine.rdnoise = 0.0
        imcombine.gain = hdr["SOFTGAIN"]

        sstr = ",".join(slis[:10])
        imcombine(sstr, "Sky-%s-%s.fits" % (chip, filt))

        # Normalize
        iraf.iterstat("Sky-%s-%s.fits" % (chip, filt))
        iraf.imarith("Sky-%s-%s.fits" % (chip, filt), "/",
                     iraf.iterstat.median, "Sky-%s-%s.fits" % (chip, filt))

    # Subtract sky frame (and dark!)
    imlist = glob.glob("fb[0-9]*T[0-9]*%so_img_1.fits" % chip)
    for im in imlist:
        if not os.path.exists("s%s" % im):

            iraf.iterstat(im)
            iraf.imarith("Sky-%s-%s.fits" % (chip, filt), "*",
                         iraf.iterstat.median, "temp.fits")
            iraf.imarith(im, "-", "temp.fits", "s%s" % im)
            os.remove("temp.fits")
            fimg = pyfits.open("s%s" % im, "update")
            fimg[0].header["SKYMED"] = iraf.iterstat.median
            fimg[0].header["SKYSIG"] = iraf.iterstat.sigma
            fimg[0].header["SKYSUB"] = 1
            fimg.flush()

    # Remove cosmic rays
    imlist = glob.glob("sfb[0-9]*T[0-9]*%so_img_1.fits" % chip)
    for im in imlist:
        if not os.path.exists("c%s" % im):
            fimg = pyfits.open(im)
            iraf.lacos_im(im,
                          "c%s" % im,
                          "c%s.mask.fits" % im[:-5],
                          gain=hdr['SOFTGAIN'],
                          readn=0.0,
                          statsec="*,*",
                          skyval=fimg[0].header["SKYMED"],
                          sigclip=4.5,
                          sigfrac=0.5,
                          objlim=1.0,
                          niter=3,
                          verbose=no)

    # Add WCS keywords
    clis = glob.glob("csfb[0-9]*T[0-9]*%so_img_1.fits" % chip)
    for im in clis:

        fimg = pyfits.open(im, mode='update')
        if fimg[0].header.get("RA") == None:

            ra0 = fimg[0].header["STRCURA"]
            dec0 = fimg[0].header["STRCUDE"]
            nax = [fimg[0].header["NAXIS1"], fimg[0].header["NAXIS2"]]
            fimg[0].header["RA"] = ra0
            fimg[0].header["DEC"] = dec0
            fimg[0].header["PIXSCALE"] = RATIRPIXSCALE[chip]
            fimg[0].header["PIXSCAL1"] = RATIRPIXSCALE[chip]
            fimg[0].header["PIXSCAL2"] = RATIRPIXSCALE[chip]
            fimg[0].header["CTYPE1"] = "RA---TAN"
            fimg[0].header["CTYPE2"] = "DEC--TAN"
            fimg[0].header["WCSDIM"] = 2
            fimg[0].header["WAT0_001"] = "system=image"
            fimg[0].header["WAT1_001"] = "wtype=tan axtype=ra"
            fimg[0].header["WAT2_001"] = "wtype=tan axtype=dec"
            fimg[0].header["LTM1_1"] = 1.0
            fimg[0].header["LTM2_2"] = 1.0
            fimg[0].header["CRPIX1"] = nax[0] / 2.0
            fimg[0].header["CRPIX2"] = nax[1] / 2.0
            fimg[0].header["CRVAL1"] = ra0
            fimg[0].header["CRVAL2"] = dec0
            fimg[0].header["CD1_1"] = -RATIRPIXSCALE[chip] / 3600.0
            fimg[0].header["CD1_2"] = 0.0
            fimg[0].header["CD2_1"] = 0.0
            fimg[0].header["CD2_2"] = RATIRPIXSCALE[chip] / 3600.0

        fimg.flush()

    # Crude astrometry
    for im in clis:

        if not os.path.exists("a%s" % im):
            os.system("python %s %s" % (PYASTROM, im))

    # Refine Astrometry
    o1 = open("daofind.param", "w")
    o1.write(
        "NUMBER\nXWIN_IMAGE\nYWIN_IMAGE\nMAG_AUTO\nFLAGS\nA_IMAGE\nB_IMAGE\n")
    o1.write("ELONGATION\nFWHM_IMAGE\nXWIN_WORLD\nYWIN_WORLD\n")
    o1.write(
        "ERRAWIN_IMAGE\nERRBWIN_IMAGE\nERRTHETAWIN_IMAGE\nERRAWIN_WORLD\n")
    o1.write(
        "ERRBWIN_WORLD\nERRTHETAWIN_WORLD\nFLUX_AUTO\nFLUX_RADIUS\nFLUXERR_AUTO"
    )
    o1.close()

    o2 = open("default.conv", "w")
    o2.write(
        "CONV NORM\n# 5x5 convolution mask of a gaussian PSF with FWHM = 3.0 pixels.\n0.092163 0.221178 0.296069 0.221178 0.092163\n0.221178 0.530797 0.710525 0.530797 0.221178\n0.296069 0.710525 0.951108 0.710525 0.296069\n0.221178 0.530797 0.710525 0.530797 0.221178\n0.092163 0.221178 0.296069 0.221178 0.092163"
    )
    o2.close()

    alis = glob.glob("acsfb[0-9]*T[0-9]*%so_img_1.fits" % chip)
    for im in alis:

        # Detect sources
        os.system(
            "sex -CATALOG_NAME %s.cat -CATALOG_TYPE FITS_LDAC -PARAMETERS_NAME daofind.param -DETECT_THRESH 2.0 -ANALYSIS_THRESH 2.0 -GAIN_KEY SOFTGAIN -PIXEL_SCALE 0 %s"
            % (im[:-5], im))

    # Run scamp for alignment
    imlist = " ".join(alis)
    os.system(
        "scamp -ASTREF_CATALOG SDSS-R7 -DISTORTDEG 1 -SOLVE_PHOTOM N -SN_THRESHOLDS 3.0,10.0 -CHECKPLOT_DEV NULL %s"
        % imlist.replace(".fits", ".cat"))

    # Update header
    for im in alis:
        os.system("missfits %s" % im)
        os.system("rm %s.head %s.back" % (im[:-5], im))

    # Find good images for coadd
    slis = []
    for im in alis:
        hdr = pyfits.getheader(im)
        rms1 = hdr["ASTRRMS1"]
        rms2 = hdr["ASTRRMS2"]
        if (rms1 < 1.0e-4) and (rms1 > 5.0e-6) and (rms2 < 1.0e-4) and (
                rms2 > 5.0e-6):
            slis.append(im)

    # Initial (unweighted) coadd
    sstr = " ".join(slis)
    os.system("swarp -GAIN_KEYWORD SOFTGAIN %s" % sstr)

    # Identify sources in the coadded frame
    hdr = pyfits.getheader("coadd.fits")
    iqpkg.iqobjs("coadd.fits",
                 10.0,
                 RATIRSAT[chip],
                 skyval="0.0",
                 pix=RATIRPIXSCALE[chip],
                 gain=hdr["GAIN"],
                 aperture=20.0,
                 wtimage="coadd.weight.fits")
    hdr = pyfits.getheader("coadd.fits")
    cpsfdiam = 1.34 * float(hdr["SEEPIX"])
    iqpkg.iqobjs("coadd.fits",
                 10.0,
                 RATIRSAT[chip],
                 skyval="0.0",
                 pix=RATIRPIXSCALE[chip],
                 gain=hdr["GAIN"],
                 aperture=cpsfdiam,
                 wtimage="coadd.weight.fits")

    refstars1 = Starlist("coadd.fits.stars")
    refstars1.pix2wcs("coadd.fits")
    truemags = np.array(refstars1.mags())
    maglist = []
    errlist = []

    # (Relative) Zeropoints for individual images
    for im in slis:
        hdr = pyfits.getheader(im)
        iqpkg.iqobjs(im,
                     3.0,
                     RATIRSAT[chip],
                     skyval="!SKYMED",
                     pix=RATIRPIXSCALE[chip],
                     gain=hdr["SOFTGAIN"],
                     aperture=20.0)
        hdr = pyfits.getheader(im)
        psfdiam = 1.34 * float(hdr["SEEPIX"])
        iqpkg.iqobjs(im,
                     3.0,
                     RATIRSAT[chip],
                     skyval="!SKYMED",
                     pix=RATIRPIXSCALE[chip],
                     gain=hdr["SOFTGAIN"],
                     aperture=psfdiam)
        stars = Starlist("%s.stars" % im)
        refstars1.wcs2pix(im)
        a, b = stars.match(refstars1, tol=10.0, maxnum=1000)
        newmags = np.zeros(len(refstars1))
        newwts = np.zeros(len(refstars1))
        for i in range(len(refstars1)):
            for j in range(len(a)):
                if b[j].mag == truemags[i]:
                    newmags[i] = a[j].mag
                    newwts[i] = 1.0 / np.power(np.maximum(a[j].magu, 0.01), 2)
                    continue
        maglist.append(newmags)
        errlist.append(newwts)

    obsmags = np.array(maglist)
    wts = np.array(errlist)

    [zpts, scatt, rms] = calc_zpts(truemags, obsmags, wts, sigma=3.0)

    # Update headers
    medzp = np.median(zpts)
    for i in range(len(slis)):
        im = slis[i]
        fimg = pyfits.open(im, mode="update")
        fimg[0].header["RELZPT"] = zpts[i]
        fimg[0].header["RELZPTSC"] = scatt[i]
        fimg[0].header["RELZPRMS"] = rms[i]
        fimg[0].header["FLXSCALE"] = 1.0 / np.power(10,
                                                    (zpts[i] - medzp) / 2.5)
        fimg.flush()

    # Final coadd
    os.system("swarp -GAIN_KEYWORD SOFTGAIN %s" % sstr)

    # Calibration
    iqpkg.iqobjs("coadd.fits",
                 10.0,
                 RATIRSAT[chip],
                 skyval="0.0",
                 pix=RATIRPIXSCALE[chip],
                 gain=hdr["GAIN"],
                 aperture=cpsfdiam,
                 wtimage="coadd.weight.fits")
    stars = Starlist("coadd.fits.stars")
    refstars = Starlist(reflist)
    refstars.wcs2pix("coadd.fits")
    refstars.set_mag("%sMAG" % filt.upper())
    truemags = np.array(refstars.mags())
    maglist = []
    errlist = []

    a, b = stars.match(refstars, tol=10.0, maxnum=1000)
    newmags = np.zeros(len(refstars))
    newwts = np.zeros(len(refstars))
    for i in range(len(refstars)):
        for j in range(len(a)):
            if b[j].mag == truemags[i]:
                newmags[i] = a[j].mag
                newwts[i] = 1.0 / np.power(np.maximum(a[j].magu, 0.01), 2)
                continue
    maglist.append(newmags)
    errlist.append(newwts)

    obsmags = np.array(maglist)
    wts = np.array(errlist)

    [zpts, scatt, rms] = calc_zpts(truemags, obsmags, wts, sigma=3.0)
    fimg = pyfits.open("coadd.fits", mode="update")
    fimg[0].header["ABSZPT"] = zpts[0] + 25
    fimg[0].header["ABSZPTSC"] = scatt[0]
    fimg[0].header["ABZPRMS"] = rms[0]

    print "Zeropoint for coadded image: %.3f" % (25.0 + zpts[0])
    print "Robust scatter for coadded image: %.3f" % scatt[0]
    print "RMS for coadded image: %.3f" % rms[0]
Ejemplo n.º 7
0
def p60sub(refimage, inlist, ra=-1, dec=-1, dx=-1, dy=-1, \
 nsx=5, nsy=5, sx=1, sy=1, sat1=60000, sat2=60000, min=5, ms=130, dback=1, \
 dspace=2, hms=9, hss=15, thresh=20, seeing=5.0, scale=0.0, tol=0.005, \
 matchlist="", goodmatch=2.0, aorder=2, nstars=30, outim="", MAX=900, \
 order=3, clobber=globclob):

    """ subtraction pipeline for one object/filter P60 data """

    # Parse inputs
    infiles = iraffiles(inlist)

    # Find location (in pixels) of central object if specified
    [nx, ny] = get_head(refimage, ['NAXIS1', 'NAXIS2'])
    if not ((ra == -1) and (dec == -1)):
	[xcenpix, ycenpix] = imwcs2pix(refimage, ra, dec)
    else:
	xcenpix = (nx / 2.0)
	ycenpix = (ny / 2.0)
	[ra, dec] = impix2wcs(refimage, xcenpix, ycenpix)

    # Trim the reference image 
    if (dx == -1):
	xl = 1
	xu = nx
    else:
        xl = int(xcenpix - (dx / 2.0))
        xu = int(xcenpix + (dx / 2.0))
    if (dy == -1):
	yl = 1
	yu = ny
    else:
	yl = int(ycenpix - (dy / 2.0))
        yu = int(ycenpix + (dy / 2.0))
        
    check_exist('t' + refimage, 'w', clobber)
    iraf.imcopy(refimage + '[%i:%i,%i:%i]' % (xl, xu, yl, yu), \
	 't' + refimage)

    # Big Loop
    for image in infiles:

	# Separate image rootname from extension
	imageroot = image.split('.')[0]

     	# Check to make sure the WCS fit was successful
	if not check_head(image, 'IQWCS'):
	    print 'Skipping image %s: IQWCS not successful' % image
	    infiles.remove(image)
	    continue
	# If so, then shift all the images appropriately
	else:
	    xsh = xcenpix - imwcs2pix(image, ra, dec)[0]
	    ysh = ycenpix - imwcs2pix(image, ra, dec)[1]
	    check_exist('s' + image, 'w', clobber)
            iraf.imshift(image, 's' + image, xsh, ysh)
	
	# Make sure sky subtraction is not done
	[skysub, skybkg] = get_head(image, ['SKYSUB', 'SKYBKG'])
	if (skysub == 1):
	    iraf.imarith('s'+image,'+',skybkg,'s'+image)

	# Trim the images
	check_exist('ts' + image, 'w', clobber)
	iraf.imcopy('s' + image + '[%i:%i,%i:%i]' % (xl, xu, yl, yu), \
	 'ts' + image)

	# Cosmic Ray Cleaning
	iraf.lacos_im('ts%s' % image, 'cts%s' % image, 'ts%s_crmask' % \
	 imageroot, gain=2.2, readn=5.3, niter=3)

	# Register the images
	os.system('$REDUCTION/imregister_new.pl t%s cts%s -sat %i -thresh %i -s %f -scale %f -tol %f %s -o %i -g %i -n %i -out cts%s.shift.fits -max %i' % (refimage, image, sat1, thresh, seeing, scale, tol, matchlist, order, goodmatch, nstars, imageroot, MAX))
	
	# Write the configuration parameters for subtraction
	pfile = open('default_config', 'w+')
	pfile.write('nstamps_x		%i\n' % nsx)
	pfile.write('nstamps_y		%i\n' % nsy)
	pfile.write('sub_x		%i\n' % sx)
	pfile.write('sub_y		%i\n' % sy)
	pfile.write('half_mesh_size	%i\n' % hms)
	pfile.write('half_stamp_size	%i\n' % hss)
	pfile.write('deg_bg		%i\n' % dback)
	pfile.write('saturation1	%f\n' % sat1)
	pfile.write('saturation2        %f\n' % sat2)
	pfile.write('pix_min		%i\n' % min)
	pfile.write('min_stamp_center  	%i\n' % ms)
	pfile.write('ngauss		3\n')
	pfile.write('deg_gauss1		6\n')
	pfile.write('deg_gauss2		4\n')
	pfile.write('deg_gauss3		2\n')
	pfile.write('sigma_gauss1	0.7\n')
	pfile.write('sigma_gauss2	1.5\n')
	pfile.write('sigma_gauss3	2.0\n')
	pfile.write('deg_spatial	%i\n' % dspace)
	pfile.write('reverse		0\n')
	pfile.write('stampsbyxy		0\n')
	pfile.close()

	# Run the subtraction
	os.system("$REDUCTION/ISIS/bin/mrj_phot t%s cts%s.shift.fits" \
	 % (refimage, imageroot))

	# Clean up files
	os.system("mv conv.fits cts%s.shift.sub.fits" % imageroot)
	os.system("mv conv0.fits cts%s.shift.conv.fits" % imageroot)

    # Return
    print 'Exiting successfully'
    return
Ejemplo n.º 8
0
def iq_ratir(chip="C1", filt="i", reflist="sdss.reg"):

	'''Process RATIR data for given chip and filter'''
	
	# Check and see if bias frame exists.  If not, create one
	if not os.path.exists("Bias-%s.fits" % chip):
	
		imlist = glob.glob("[0-9]*T[0-9]*%sb.fits" % chip)
		hdr = pyfits.getheader(imlist[0])
		
		# Setup zerocombine
		zerocombine = iraf.ccdred.zerocombine
		zerocombine.combine = 'median'
		zerocombine.reject = 'avsigclip'
		zerocombine.ccdtype = ''
		zerocombine.process = no
		zerocombine.delete = no
		zerocombine.clobber = no
		zerocombine.scale = 'none'
		zerocombine.statsec = '*'
		zerocombine.nlow = 0
		zerocombine.nhigh = 1
		zerocombine.nkeep = 1
		zerocombine.mclip = yes
		zerocombine.lsigma = 3.0
		zerocombine.hsigma = 3.0
		zerocombine.rdnoise = 0.0
		zerocombine.gain = hdr['SOFTGAIN']
		zerocombine.snoise = 0
		zerocombine.pclip = -0.5
		zerocombine.blank = 0.0
		
		# Run zerocombine
		bstr = ",".join(imlist)
		zerocombine(input=bstr, output="Bias-%s.fits" % chip)
		
	# Bias subtract flat frames
	imlist = glob.glob("[0-9]*T[0-9]*%sf.fits" % chip)
	flis = []
	for im in imlist:
		hdr = pyfits.getheader(im)
		if (hdr["FILTER"] == filt) and (not os.path.exists("b%s" % im)):
			
			# Subtract bias frame
			ccdproc = iraf.ccdred.ccdproc	
			ccdproc.ccdtype = ""
			ccdproc.noproc = no
			ccdproc.fixpix = no
			ccdproc.overscan = no
			ccdproc.trim = no
			ccdproc.zerocor = yes
			ccdproc.darkcor = no
			ccdproc.flatcor = no
			ccdproc.illumcor = no
			ccdproc.fringecor = no
			ccdproc.readcor = no
			ccdproc.scancor = no
			ccdproc.readaxis = 'line'
			ccdproc.fixfile = ""
			ccdproc.zero = "Bias-%s.fits" % chip
			ccdproc.interactive = no
			ccdproc.function = "legendre"
			ccdproc.order = 1
			ccdproc.sample = "*"
			ccdproc.naverage = 1
			ccdproc.niterate = 1
			ccdproc.low_reject = 3.0
			ccdproc.high_reject = 3.0
			ccdproc.grow = 0
			ccdproc(images=im, output="b%s" % im)
				
			flis.append("b%s" % im)
		elif (hdr["FILTER"] == filt):
			flis.append("b%s" % im)

	# Create flat field
	if not os.path.exists("Flat-%s-%s.fits" % (chip, filt)):
	
		glis = []
		for im in flis:
			iraf.iterstat.nsigrej = 5.0
			iraf.iterstat.maxiter = 10
			iraf.iterstat.verbose = globver
			iraf.iterstat.lower = INDEF
			iraf.iterstat.upper = INDEF
			iraf.iterstat(im)
			if (iraf.iterstat.median < RATIRSAT[chip]) and (iraf.iterstat.median > 1000.0):
				glis.append(im)
		     	
		# Set up flatcombine
		flatcombine = iraf.ccdred.flatcombine
		flatcombine.combine = 'median'
		flatcombine.reject = 'avsigclip'
		flatcombine.ccdtype = ''
		flatcombine.scale = 'median'
		flatcombine.statsec = ''
		flatcombine.nlow = 1
		flatcombine.nhigh = 1
		flatcombine.nkeep = 1
		flatcombine.mclip = yes
		flatcombine.lsigma = 3.0
		flatcombine.hsigma = 3.0
		flatcombine.rdnoise = 0.0
		flatcombine.gain = hdr["SOFTGAIN"]
		flatcombine.snoise = 0.0
		flatcombine.pclip = -0.5
			
		# Run flatcombine
		fstr = ",".join(glis[:10])
		flatcombine(fstr, output="Flat-%s-%s.fits" % (chip, filt))
			
		# Normalize
		iraf.iterstat.nsigrej = 5.0
		iraf.iterstat.maxiter = 10
		iraf.iterstat.verbose = globver
		iraf.iterstat.lower = INDEF
		iraf.iterstat.upper = INDEF
		iraf.iterstat("Flat-%s-%s.fits" % (chip, filt))
		iraf.imarith("Flat-%s-%s.fits" % (chip, filt), "/", 
			         iraf.iterstat.median, "Flat-%s-%s.fits" % (chip, filt))
			         	
	# Bias subtract and flat-field science images
	imlist = glob.glob("[0-9]*T[0-9]*%so_img_1.fits" % chip)
	for im in imlist:
		hdr = pyfits.getheader(im)
		if (hdr["FILTER"] == filt) and (not os.path.exists("fb%s" % im)):
			
			hdr = pyfits.getheader(im)
			
			# Subtract bias frame
			ccdproc = iraf.ccdred.ccdproc	
			ccdproc.ccdtype = ""
			ccdproc.noproc = no
			ccdproc.fixpix = no
			ccdproc.overscan = no
			ccdproc.trim = no
			ccdproc.zerocor = yes
			ccdproc.darkcor = no
			ccdproc.flatcor = yes
			ccdproc.illumcor = no
			ccdproc.fringecor = no
			ccdproc.readcor = no
			ccdproc.scancor = no
			ccdproc.readaxis = 'line'
			ccdproc.fixfile = ""
			ccdproc.zero = "Bias-%s.fits" % chip
			ccdproc.flat = "Flat-%s-%s.fits" % (chip, filt)
			ccdproc.interactive = no
			ccdproc.function = "legendre"
			ccdproc.order = 1
			ccdproc.sample = "*"
			ccdproc.naverage = 1
			ccdproc.niterate = 1
			ccdproc.low_reject = 3.0
			ccdproc.high_reject = 3.0
			ccdproc.grow = 0
			ccdproc(images=im, output="fb%s" % im)
			
	# Create sky frame (including dark current!)
	if not os.path.exists("Sky-%s-%s.fits" % (chip, filt)):
		imlist = glob.glob("fb[0-9]*T[0-9]*%so_img_1.fits" % chip)
		hdr = pyfits.getheader(imlist[0])
		slis = []
		for im in imlist:
			if (hdr["FILTER"] == filt):
				slis.append(im)
				
		# Set up combine
		imcombine = iraf.immatch.imcombine
		imcombine.headers = ""
		imcombine.bpmasks = ""
		imcombine.rejmasks = ""
		imcombine.nrejmasks = ""
		imcombine.expmasks = ""
		imcombine.sigmas = ""
		imcombine.combine = "median"
		imcombine.reject = "avsigclip"
		imcombine.project = no
		imcombine.outtype = "real"
		imcombine.offsets = "none"
		imcombine.masktype = "none"
		imcombine.scale = "median"
		imcombine.zero = "none"
		imcombine.weight = "none"
		imcombine.statsec = ""
		imcombine.lsigma = 3.0
		imcombine.hsigma = 3.0
		imcombine.rdnoise = 0.0
		imcombine.gain = hdr["SOFTGAIN"]
		
		sstr = 	",".join(slis[:10])
		imcombine(sstr, "Sky-%s-%s.fits" % (chip, filt))
		
		# Normalize
		iraf.iterstat("Sky-%s-%s.fits" % (chip, filt))
		iraf.imarith("Sky-%s-%s.fits" % (chip, filt), "/", 
			         iraf.iterstat.median, "Sky-%s-%s.fits" % (chip, filt))
			         
	# Subtract sky frame (and dark!)
	imlist = glob.glob("fb[0-9]*T[0-9]*%so_img_1.fits" % chip)
	for im in imlist:
		if not os.path.exists("s%s" % im):
		
			iraf.iterstat(im)
			iraf.imarith("Sky-%s-%s.fits" % (chip, filt), "*", iraf.iterstat.median,
			             "temp.fits")
			iraf.imarith(im, "-", "temp.fits", "s%s" % im)
			os.remove("temp.fits")
			fimg = pyfits.open("s%s" % im, "update")
			fimg[0].header["SKYMED"] = iraf.iterstat.median
			fimg[0].header["SKYSIG"] = iraf.iterstat.sigma
			fimg[0].header["SKYSUB"] = 1
			fimg.flush()
								
	# Remove cosmic rays
	imlist = glob.glob("sfb[0-9]*T[0-9]*%so_img_1.fits" % chip)
	for im in imlist:
		if not os.path.exists("c%s" % im):
			fimg = pyfits.open(im)
			iraf.lacos_im(im, "c%s" % im, "c%s.mask.fits" % im[:-5],
			              gain=hdr['SOFTGAIN'], readn=0.0, statsec="*,*",
			              skyval=fimg[0].header["SKYMED"], sigclip=4.5, 
			              sigfrac=0.5, objlim=1.0, niter=3, verbose=no)

	# Add WCS keywords
	clis = glob.glob("csfb[0-9]*T[0-9]*%so_img_1.fits" % chip)
	for im in clis:
	
		fimg = pyfits.open(im, mode='update')
		if fimg[0].header.get("RA")==None:
		
			ra0 = fimg[0].header["STRCURA"]; dec0 = fimg[0].header["STRCUDE"]
			nax = [fimg[0].header["NAXIS1"], fimg[0].header["NAXIS2"]]
			fimg[0].header["RA"] = ra0
			fimg[0].header["DEC"] = dec0		
			fimg[0].header["PIXSCALE"] = RATIRPIXSCALE[chip]
			fimg[0].header["PIXSCAL1"] = RATIRPIXSCALE[chip]
			fimg[0].header["PIXSCAL2"] = RATIRPIXSCALE[chip]
			fimg[0].header["CTYPE1"] = "RA---TAN"
			fimg[0].header["CTYPE2"] = "DEC--TAN"
			fimg[0].header["WCSDIM"] = 2
			fimg[0].header["WAT0_001"] = "system=image"
			fimg[0].header["WAT1_001"] = "wtype=tan axtype=ra"
			fimg[0].header["WAT2_001"] = "wtype=tan axtype=dec"
			fimg[0].header["LTM1_1"] = 1.0
			fimg[0].header["LTM2_2"] = 1.0	
			fimg[0].header["CRPIX1"] = nax[0] / 2.0
			fimg[0].header["CRPIX2"] = nax[1] / 2.0
			fimg[0].header["CRVAL1"] = ra0
			fimg[0].header["CRVAL2"] = dec0
			fimg[0].header["CD1_1"] = -RATIRPIXSCALE[chip] / 3600.0
			fimg[0].header["CD1_2"] = 0.0
			fimg[0].header["CD2_1"] = 0.0
			fimg[0].header["CD2_2"] = RATIRPIXSCALE[chip] / 3600.0
			
		fimg.flush()
		
	# Crude astrometry
	for im in clis:
	
		if not os.path.exists("a%s" % im):
			os.system("python %s %s" % (PYASTROM, im))
				
	# Refine Astrometry
	o1 = open("daofind.param", "w")
	o1.write("NUMBER\nXWIN_IMAGE\nYWIN_IMAGE\nMAG_AUTO\nFLAGS\nA_IMAGE\nB_IMAGE\n")
	o1.write("ELONGATION\nFWHM_IMAGE\nXWIN_WORLD\nYWIN_WORLD\n")
	o1.write("ERRAWIN_IMAGE\nERRBWIN_IMAGE\nERRTHETAWIN_IMAGE\nERRAWIN_WORLD\n")
	o1.write("ERRBWIN_WORLD\nERRTHETAWIN_WORLD\nFLUX_AUTO\nFLUX_RADIUS\nFLUXERR_AUTO")
	o1.close()
	
	o2 = open("default.conv", "w")
	o2.write("CONV NORM\n# 5x5 convolution mask of a gaussian PSF with FWHM = 3.0 pixels.\n0.092163 0.221178 0.296069 0.221178 0.092163\n0.221178 0.530797 0.710525 0.530797 0.221178\n0.296069 0.710525 0.951108 0.710525 0.296069\n0.221178 0.530797 0.710525 0.530797 0.221178\n0.092163 0.221178 0.296069 0.221178 0.092163")
	o2.close()
	
	alis = glob.glob("acsfb[0-9]*T[0-9]*%so_img_1.fits" % chip)
	for im in alis:
	
		# Detect sources
		os.system("sex -CATALOG_NAME %s.cat -CATALOG_TYPE FITS_LDAC -PARAMETERS_NAME daofind.param -DETECT_THRESH 2.0 -ANALYSIS_THRESH 2.0 -GAIN_KEY SOFTGAIN -PIXEL_SCALE 0 %s" % (im[:-5], im))
		
	# Run scamp for alignment
	imlist = " ".join(alis)
	os.system("scamp -ASTREF_CATALOG SDSS-R7 -DISTORTDEG 1 -SOLVE_PHOTOM N -SN_THRESHOLDS 3.0,10.0 -CHECKPLOT_DEV NULL %s" % imlist.replace(".fits", ".cat"))
	
	# Update header
	for im in alis:
		os.system("missfits %s" % im)
		os.system("rm %s.head %s.back" % (im[:-5], im))
		
	# Find good images for coadd
	slis = []
	for im in alis:
		hdr = pyfits.getheader(im)
		rms1 = hdr["ASTRRMS1"]; rms2 = hdr["ASTRRMS2"]
		if (rms1 < 1.0e-4) and (rms1 > 5.0e-6) and (rms2 < 1.0e-4) and (rms2 > 5.0e-6):
			slis.append(im)
			
	# Initial (unweighted) coadd
	sstr = " ".join(slis)
	os.system("swarp -GAIN_KEYWORD SOFTGAIN %s" % sstr)
	
	# Identify sources in the coadded frame
	hdr = pyfits.getheader("coadd.fits")
	iqpkg.iqobjs("coadd.fits", 10.0, RATIRSAT[chip], skyval="0.0",
	             pix=RATIRPIXSCALE[chip], gain=hdr["GAIN"], aperture=20.0,
	             wtimage="coadd.weight.fits")
	hdr = pyfits.getheader("coadd.fits")
	cpsfdiam = 1.34 * float(hdr["SEEPIX"])
	iqpkg.iqobjs("coadd.fits", 10.0, RATIRSAT[chip], skyval="0.0",
	             pix=RATIRPIXSCALE[chip], gain=hdr["GAIN"], aperture=cpsfdiam,
	             wtimage="coadd.weight.fits")
	             
	refstars1 = Starlist("coadd.fits.stars")
	refstars1.pix2wcs("coadd.fits")
	truemags = np.array(refstars1.mags())
	maglist = []; errlist = []
	
	# (Relative) Zeropoints for individual images
	for im in slis:
		hdr = pyfits.getheader(im)
		iqpkg.iqobjs(im, 3.0, RATIRSAT[chip], skyval="!SKYMED", 
		       pix=RATIRPIXSCALE[chip], gain=hdr["SOFTGAIN"], aperture=20.0)
		hdr = pyfits.getheader(im)
		psfdiam = 1.34 * float(hdr["SEEPIX"])
		iqpkg.iqobjs(im, 3.0, RATIRSAT[chip], skyval="!SKYMED", 
		       pix=RATIRPIXSCALE[chip], gain=hdr["SOFTGAIN"], aperture=psfdiam)
		stars = Starlist("%s.stars" % im)
		refstars1.wcs2pix(im)
		a,b = stars.match(refstars1,tol=10.0,maxnum=1000)
		newmags = np.zeros(len(refstars1)); newwts = np.zeros(len(refstars1))
		for i in range(len(refstars1)):
			for j in range(len(a)):
				if b[j].mag == truemags[i]:
					newmags[i] = a[j].mag
					newwts[i] = 1.0 / np.power(np.maximum(a[j].magu, 0.01),2)
					continue
		maglist.append(newmags); errlist.append(newwts)
		
	obsmags = np.array(maglist)
	wts = np.array(errlist)
	
	[zpts, scatt, rms] = calc_zpts(truemags, obsmags, wts, sigma=3.0)
	
	# Update headers
	medzp = np.median(zpts)
	for i in range(len(slis)):
		im = slis[i]
		fimg = pyfits.open(im, mode="update")
		fimg[0].header["RELZPT"] = zpts[i]
		fimg[0].header["RELZPTSC"] = scatt[i]
		fimg[0].header["RELZPRMS"] = rms[i]
		fimg[0].header["FLXSCALE"] = 1.0 / np.power(10, (zpts[i] - medzp) / 2.5)
		fimg.flush()
		
	# Final coadd
	os.system("swarp -GAIN_KEYWORD SOFTGAIN %s" % sstr)
	
	# Calibration
	iqpkg.iqobjs("coadd.fits", 10.0, RATIRSAT[chip], skyval="0.0",
	             pix=RATIRPIXSCALE[chip], gain=hdr["GAIN"], aperture=cpsfdiam,
	             wtimage="coadd.weight.fits")
	stars = Starlist("coadd.fits.stars")
	refstars = Starlist(reflist)
	refstars.wcs2pix("coadd.fits")
	refstars.set_mag("%sMAG" % filt.upper())
	truemags = np.array(refstars.mags())
	maglist = []; errlist = []
	
	a,b = stars.match(refstars, tol=10.0, maxnum=1000)
	newmags = np.zeros(len(refstars)); newwts = np.zeros(len(refstars))
	for i in range(len(refstars)):
		for j in range(len(a)):
			if b[j].mag == truemags[i]:
				newmags[i] = a[j].mag
				newwts[i] = 1.0 / np.power(np.maximum(a[j].magu, 0.01),2)
				continue
	maglist.append(newmags); errlist.append(newwts)
	
	obsmags = np.array(maglist)
	wts = np.array(errlist)
	
	[zpts, scatt, rms] = calc_zpts(truemags, obsmags, wts, sigma=3.0)
	fimg = pyfits.open("coadd.fits", mode="update")
	fimg[0].header["ABSZPT"] = zpts[0] + 25
	fimg[0].header["ABSZPTSC"] = scatt[0]
	fimg[0].header["ABZPRMS"] = rms[0]
	
	print "Zeropoint for coadded image: %.3f" % (25.0+zpts[0])
	print "Robust scatter for coadded image: %.3f" % scatt[0]
	print "RMS for coadded image: %.3f" % rms[0]