Exemplo n.º 1
0
def compute_psf_image(params,g,psf_deg=1,psf_rad=8,
                      star_file='phot.mags',psf_image='psf.fits',edge_dist=5):
    iraf.digiphot()
    iraf.daophot()
    fp = params.loc_output+os.path.sep

    f_im = g.image*g.mask
    f = fp+'temp.ref.fits'
    write_image(f_im,f)

    g.fw = np.max([1.5,g.fw])
    g.fw = np.min([0.5*params.psf_max_radius,g.fw])

    logfile = fp+'psf.log'

    fd = fits.getdata(f)
    xmax = fd.shape[0] - edge_dist
    ymax = fd.shape[1] - edge_dist
    

    for d in ['temp.stars','temp.phot','temp.phot1','temp.phot2','temp.pst',
              'temp.opst','temp.opst2',
              'temp.psf.fits','temp.psf1.fits','temp.psf2.fits','temp.psg',
              'temp.psg2','temp.psg3','temp.psg5','temp.rej','temp.rej2',
              'temp.sub.fits','temp.sub1.fits',
              'temp.sub2.fits','temp.opst1','temp.opst3','temp.rej3',
              'temp.nst','temp.stars1','ref.mags',psf_image,'temp.als',
              'temp.als2']:
            if os.path.exists(fp+d):
                os.remove(fp+d)


    # locate stars
    iraf.daofind(image=f,output=fp+'temp.stars',interactive='no',verify='no',
                 threshold=3,sigma=params.star_detect_sigma,fwhmpsf=g.fw,
                 datamin=1,datamax=params.pixel_max,
                 epadu=params.gain,readnoise=params.readnoise,
                 noise='poisson')

    if params.star_file:
        als_recenter = 'no'
        all_template_stars = np.genfromtxt(params.star_file)
        all_new_stars = np.genfromtxt(fp+'temp.stars')
        
        if all_new_stars.shape[0] > params.star_file_number_match:
            new_stars = all_new_stars[all_new_stars[:,2].argsort()][:params.star_file_number_match]
        else:
            new_stars = all_new_stars

        if all_template_stars.shape[0] > params.star_file_number_match:
            template_stars = all_template_stars[all_template_stars[:,3].argsort()][:params.star_file_number_match]
        else:
            template_stars = all_template_stars

        tx, ty = compute_xy_shift(new_stars,template_stars[:,1:3],0.5,
                                  degree=params.star_file_transform_degree)

        if params.star_file_has_magnitudes:
            star_positions = all_template_stars[:,1:4]
            xx = (star_positions[:,0]-np.mean(new_stars[:,0]))/np.mean(new_stars[:,0])
            yy = (star_positions[:,1]-np.mean(new_stars[:,1]))/np.mean(new_stars[:,1])
            for m in range(params.star_file_transform_degree+1):
                for n in range(params.star_file_transform_degree+1-m):
                    star_positions[:,0] += tx[m,n]* xx**m * yy**n
                    star_positions[:,1] += ty[m,n]* xx**m * yy**n
            np.savetxt(fp+'temp.stars.1',star_positions,fmt='%10.3f %10.3f %10.3f')
        else:
            star_positions = all_template_stars[:,1:3]
            xx = (star_positions[:,0]-np.mean(new_stars[:,0]))/np.mean(new_stars[:,0])
            yy = (star_positions[:,1]-np.mean(new_stars[:,1]))/np.mean(new_stars[:,1])
            for m in range(params.star_file_transform_degree+1):
                for n in range(params.star_file_transform_degree+1-m):
                    star_positions[:,0] += tx[m,n]* xx**m * yy**n
                    star_positions[:,1] += ty[m,n]* xx**m * yy**n
            np.savetxt(fp+'temp.stars.1',star_positions,fmt='%10.3f %10.3f')
        all_template_stars[:,1] = star_positions[:,0]
        all_template_stars[:,2] = star_positions[:,1]
            
    else:
        
        als_recenter = 'yes'
        star_positions = np.genfromtxt(fp+'temp.stars')
        np.savetxt(fp+'temp.stars.1',star_positions[:,:2],fmt='%10.3f %10.3f')

    iraf.phot(image=f,output=fp+'temp.phot',coords=fp+'temp.stars.1',interactive='no',
              verify='no',
              sigma=params.star_detect_sigma,fwhmpsf=g.fw,apertures=g.fw,
              datamin=1,
              datamax=2*params.pixel_max,epadu=params.gain,annulus=3*g.fw,
              dannulus=3.0,
              readnoise=params.readnoise,noise='poisson')

    print 'fw = ',g.fw
    #fw = np.max([4.0,fw])
    #print 'fw = ',fw


    # select PSF stars
    iraf.pstselect(image=f,photfile=fp+'temp.phot',pstfile=fp+'temp.pst',maxnpsf=40,
                   interactive='no',verify='no',datamin=1,fitrad=2.0,
                   datamax=params.pixel_max,epadu=params.gain,psfrad=np.max([3.0,g.fw]),
                   readnoise=params.readnoise,noise='poisson')

    if params.star_file and params.star_file_has_magnitudes:

        # We don't need to do the photometry - only make the PSF

        # Initial PSF estimate to generate PSF groups
        #psfrad=3*np.max([g.fw,1.8])
        iraf.psf(image=f,photfile=fp+'temp.phot',pstfile=fp+'temp.pst',psfimage=fp+'temp.psf',
                 function=params.psf_profile_type,opstfile=fp+'temp.opst',
                 groupfile=fp+'temp.psg',
                 interactive='no',
                 verify='no',varorder=0 ,psfrad=2*np.max([g.fw,1.8]),
                 datamin=-10000,datamax=0.95*params.pixel_max,
                 scale=1.0)

        # construct a file of the psf neighbour stars
        slist = []
        psf_stars = np.loadtxt(fp+'temp.opst',usecols=(0,1,2))

        for star in range(psf_stars.shape[0]):

            xp = psf_stars[star,1]
            yp = psf_stars[star,2]
            xmin = np.max([np.int(xp-10*g.fw),0])
            xmax = np.min([np.int(xp+10*g.fw),f_im.shape[0]])
            ymin = np.max([np.int(yp-10*g.fw),0])
            ymax = np.min([np.int(yp+10*g.fw),f_im.shape[1]])

            p = star_positions[np.logical_and(np.logical_and(star_positions[:,0]>xmin,
                                                             star_positions[:,0]<xmax),
                                              np.logical_and(star_positions[:,1]>ymin,
                                                             star_positions[:,1]<ymax))]
            slist.append(p)

        group_stars = np.concatenate(slist)
        np.savetxt(fp+'temp.nst',group_stars,fmt='%10.3f %10.3f %10.3f')
        
        
        # subtract PSF star neighbours
        iraf.substar(image=f,photfile=fp+'temp.nst',psfimage=fp+'temp.psf',
                     exfile=fp+'temp.opst',fitrad=2.0,
                     subimage=fp+'temp.sub1',verify='no',datamin=1,
                     datamax=params.pixel_max,epadu=params.gain,
                     readnoise=params.readnoise,noise='poisson')
        
        # final PSF
        iraf.psf(image=fp+'temp.sub1',photfile=fp+'temp.phot',pstfile=fp+'temp.opst',
                 psfimage=psf_image,psfrad=2*g.fw,
                 function=params.psf_profile_type,opstfile=fp+'temp.opst2',
                 groupfile=fp+'temp.psg2',
                 interactive='no',
                 verify='no',varorder=0,
                 datamin=1,datamax=0.95*params.pixel_max,
                 scale=1.0)

        np.savetxt(fp+'ref.mags',all_template_stars,fmt='%7d %10.3f %10.3f %10.3f')
        stars = all_template_stars

    else:




        # initial PSF estimate
        iraf.psf(image=f,photfile=fp+'temp.phot',pstfile=fp+'temp.pst',psfimage=fp+'temp.psf',
                 function=params.psf_profile_type,opstfile=fp+'temp.opst',
                 groupfile=fp+'temp.psg1',
                 interactive='no',
                 verify='no',varorder=0 ,psfrad=2*g.fw,
                 datamin=1,datamax=0.95*params.pixel_max,
                 scale=1.0)


        # separation distance of near neighbours
        separation = np.max([rewrite_psg(fp+'temp.psg1',fp+'temp.psg2'),3])
        print 'separation = ',separation

        # subtract all stars using truncated PSF
        iraf.allstar(image=f,photfile=fp+'temp.phot',psfimage=fp+'temp.psf',
                     allstarfile=fp+'temp.als',rejfile='',
                     subimage=fp+'temp.sub',verify='no',psfrad=2*g.fw,fitrad=2.0,
                     recenter='yes',groupsky='yes',fitsky='yes',sannulus=7,wsannulus=10,
                     datamin=1,datamax=params.pixel_max,
                     epadu=params.gain,readnoise=params.readnoise,
                     noise='poisson')

        if params.star_file:

            os.system('cp '+fp+'temp.phot '+fp+'temp.phot2') 

        else:
        
            # locate new stars
            iraf.daofind(image=fp+'temp.sub',output=fp+'temp.stars1',interactive='no',verify='no',
                         threshold=3,sigma=params.star_detect_sigma,fwhmpsf=2*g.fw,
                         datamin=1,datamax=params.pixel_max,
                         epadu=params.gain,readnoise=params.readnoise,
                         noise='poisson')


            # magnitudes for new stars
            iraf.phot(image=fp+'temp.sub',output=fp+'temp.phot1',coords=fp+'temp.stars1',
                      interactive='no',
                      verify='no',sigma=params.star_detect_sigma,
                      fwhmpsf=g.fw,datamin=1,
                      datamax=params.pixel_max,epadu=params.gain,
                      readnoise=params.readnoise,noise='poisson')

            # join star lists together
            iraf.pconcat(infiles=fp+'temp.phot,'+fp+'temp.phot1',outfile=fp+'temp.phot2')

        # new PSF estimate to generate PSF groups
        iraf.psf(image=f,photfile=fp+'temp.phot2',pstfile=fp+'temp.pst',psfimage=fp+'temp.psf2',
                 function=params.psf_profile_type,opstfile=fp+'temp.opst2',
                 groupfile=fp+'temp.psg3',
                 interactive='no',
                 verify='no',varorder=0 ,psfrad=2*g.fw,
                 datamin=-10000,datamax=0.95*params.pixel_max,
                 scale=1.0)

        # magnitudes for PSF group stars
        iraf.nstar(image=f,groupfile=fp+'temp.psg3',psfimage=fp+'temp.psf2',
                   nstarfile=fp+'temp.nst',
                   rejfile='',verify='no',psfrad=2*g.fw,fitrad=2.0,
                   recenter='no',
                   groupsky='yes',fitsky='yes',sannulus=7,wsannulus=10,
                   datamin=1,datamax=params.pixel_max,
                   epadu=params.gain,readnoise=params.readnoise,noise='poisson')

        # subtract PSF star neighbours
        iraf.substar(image=f,photfile=fp+'temp.nst',psfimage=fp+'temp.psf2',
                     exfile=fp+'temp.opst2',fitrad=2.0,
                     subimage=fp+'temp.sub1',verify='no',datamin=1,
                     datamax=params.pixel_max,epadu=params.gain,
                     readnoise=params.readnoise,noise='poisson')
        
        # final PSF
        iraf.psf(image=fp+'temp.sub1',photfile=fp+'temp.phot2',
                 pstfile=fp+'temp.opst2',
                 psfimage=psf_image,psfrad=2*g.fw,
                 function=params.psf_profile_type,opstfile=fp+'temp.opst3',
                 groupfile=fp+'temp.psg5',
                 interactive='no',
                 verify='no',varorder=0,
                 datamin=1,datamax=0.95*params.pixel_max,
                 scale=1.0)

        # final photometry

        
        iraf.allstar(image=g.fullname,photfile=fp+'temp.phot2',psfimage=psf_image,
                     allstarfile=fp+'temp.als2',rejfile='',
                     subimage=fp+'temp.sub2',verify='no',psfrad=2*g.fw,
                     recenter=als_recenter,groupsky='yes',fitsky='yes',sannulus=7,
                     wsannulus=10,fitrad=2.0,
                     datamin=params.pixel_min,datamax=params.pixel_max,
                     epadu=params.gain,readnoise=params.readnoise,
                     noise='poisson')

        psfmag = 10.0
        for line in open(fp+'temp.als2','r'):
            sline = line.split()
            if sline[1] == 'PSFMAG':
                psfmag = float(sline[3])
                break

        if params.star_file:
            
            iraf.psort(infiles=fp+'temp.als2',field='ID')
            os.system('cp '+fp+'temp.als2 '+fp+'temp.als3') 

        else:
        
            selection = 'XCE >= '+str(edge_dist)+' && XCE <= '+str(xmax)+' && YCE >= '+str(edge_dist)+' && YCE <= '+str(ymax)+' && MAG != INDEF'
            iraf.pselect(infiles=fp+'temp.als2',outfiles=fp+'temp.als3',expr=selection)
            iraf.psort(infiles=fp+'temp.als3',field='MAG')   
            iraf.prenumber(infile=fp+'temp.als3')
            
        s = iraf.pdump(infiles=fp+'temp.als3',Stdout=1,
                       fields='ID,XCENTER,YCENTER,MAG,MERR,MSKY,SHARPNESS,CHI',expr='yes')
        sf = [k.replace('INDEF','22.00') for k in s]
        stars = np.zeros([len(sf),5])
        for i, line in enumerate(sf):
            stars[i,:] = np.array(map(float,sf[i].split()[1:6]))

        s = iraf.pdump(infiles=fp+'temp.als3',Stdout=1,
                       fields='ID,XCENTER,YCENTER,MAG,MERR,SHARPNESS,CHI,MSKY',expr='yes')
        sf = [k.replace('INDEF','22.00') for k in s]
        with open(fp+'ref.mags','w') as fid:
            for s in sf:
                fid.write(s+'\n')

    return stars
Exemplo n.º 2
0
def alignImages(imageName, imageName_Ha):
    #Read the fwhm and seeing from the image file
    imageHeader = fits.open(imageName + ".fits")[0]
    fwhm = imageHeader.header['SEEING']
    annul = 5.0 * fwhm
    aper = 3.0 * fwhm
    sigma = imageHeader.header['SKYSIGMA']
    iraf.daophot(_doprint=0)
    #Do 'daofind' on the image to locate the stars
    print("1. Find stars using 'daofind'")

    #Configure 'datapars', 'findpars', and 'daofind'

    iraf.datapars.fwhmpsf = fwhm
    iraf.datapars.sigma = sigma
    iraf.datapars.datamin = -10
    iraf.datapars.ccdread = "RDNOISE"
    iraf.datapars.gain = "GAIN"
    iraf.datapars.exposure = "EXPTIME"
    iraf.datapars.airmass = "AIRMASS"
    iraf.datapars.filter = "FILTER"
    iraf.datapars.obstime = "TIME-OBS"

    iraf.findpars.threshold = 20 * sigma
    iraf.findpars.sharplo = 0.2
    iraf.findpars.sharphi = 1.0
    iraf.findpars.roundlo = -1.0
    iraf.findpars.roundhi = 1.0
    iraf.daofind.verify = 'no'
    iraf.daofind.verbose = 'no'

    #Delete exisiting coordinate,output files of 'phot',file containg data of 'good' stars, old images
    for f in ("coord", "mag", "psel", "R_final.fits", "Ha_final.fits"):
        silentDelete(f)

    iraf.daofind(imageName, 'coord')

    print("File containing the coordinates of the stars is coord")

    print(" ")
    #Configure 'centerpars', 'fitskypars','photpars'
    iraf.datapars.datamax = 150000
    iraf.centerpars.calgorithm = "centroid"
    iraf.centerpars.cbox = 16
    iraf.centerpars.maxshift = 3

    iraf.fitskypars.salgorithm = "mode"
    iraf.fitskypars.annulus = annul
    iraf.fitskypars.dannulus = 10

    iraf.photpars.apertures = aper

    iraf.phot.verify = 'no'
    iraf.phot.verbose = 'no'

    print("2. Obtain data of stars using 'phot'")
    #Call 'phot' to get the data of the stars
    iraf.phot(imageName, 'coord', 'mag')

    #sort in order of increasing magnitude of stars
    iraf.psort('mag', "mag")
    boundsig = sigma + 2
    boexpr = "CIER==0 && PIER==0 && STDEV <=" + str(boundsig)
    print(
        "File containing the data of the stars in order of decreasing brightness is mag"
    )

    print(" ")

    print("3. Select stars with low error, no bad pixels")
    #Select stars that have no centering error, skyerror <sig+2 and no bad pixels
    iraf.pselect("mag", "psel", boexpr)

    print(
        "File containing stars with low sky error,low centering error is psel")
    print(" ")

    #Renumber the ID number of the stars in order of increasing magnitude
    iraf.prenumber("psel")

    #Delete existing files
    for f in ("pdump.file", "stars25", 'alR.fits', 'rIn.fits', 'haIn.fits'):
        silentDelete(f)

    print("4. Select the 25 brightest stars")
    iraf.pselect("psel", "stars25", "ID <=25")

    print("File containing the brightest 25 'good' stars is stars25")
    print(" ")

    #Pick out only the required data of stars from the .25 file
    sys.stdout = open("pdump.file", "w")
    iraf.pdump("stars25", "xcenter,ycenter,flux", "yes")
    sys.stdout = sys.__stdout__

    print("The coordinates and flux are stored in pdump.file")

    #Align images
    iraf.imcopy(imageName, 'rIn')
    iraf.imcopy(imageName_Ha, 'haIn')
    print("Aligning images")
    #iraf.imalign.verbose='no'
    iraf.imalign("rIn,haIn", "rIn", "pdump.file", "R_final,Ha_final")
Exemplo n.º 3
0
def maskStars(imageName):
  #read in the fits data and header
  imageHeader = fits.open(imageName+".fits")[0]
  
  #Read in the skysigma, skyv, and seeing (fwhm) from the header and set up other relevant values
  sigma = imageHeader.header['SKYSIGMA']
  threshold = 3.0 * sigma
  
  skyv = imageHeader.header['SKY']
  
  fwhm = imageHeader.header['SEEING']
  aper = 3.0 * fwhm
  annul = 5 * fwhm
  
  #Read in the galcut file
  galcut = open("galcut.file")
  galcutString = file.read(galcut)
  galcut.close()
  
  #Parse the galcut file into its variables
  galList = galcutString.split(' ')
  xCenter = float(galList[0])
  yCenter = float(galList[1])
  a = float(galList[2]) #Semimajor Axis
  eccent = float(galList[3]) #Eccentricity
  posAngle = float(galList[4]) #Position angle
  
  posAngleRad = (posAngle+90)*3.14159/180 
  b = a * eccent #Semiminor Axis
  na = -1*a
  nb = -1*b
  
  #Create a version of the r image with the sky added back in for the daofind procedure
  iraf.imdelete("withsky")
  iraf.imarith(imageName,'+',skyv,"withsky")

  # Load daophot package
  iraf.digiphot()
  iraf.daophot()  

  print("A. Find stars using daofind")

#DAOFIND  searches  the  IRAF  images image for local density maxima,
#    with a  full-width  half-maxima  of  datapars.fwhmpsf,  and  a  peak
#    amplitude  greater  than  findpars.threshold  * datapars.sigma above
#    the local background, and writes a list of detected objects  in  the
#    file  output.  
# Here we set the fwhm and sigma equal to those listed in the header.
# We set the datamin to -3*sigma = -1*threshold defined above
# The findpars.threshold is set to 4.5*sigma by default. You may have
# to alter this value for images where too few/many stars are found.


  #Configure 'datapars', 'findpars', and 'daofind'
  iraf.datapars.fwhmpsf=fwhm
  iraf.datapars.sigma=sigma
  iraf.datapars.datamax='indef'
  iraf.datapars.datamin=(-1)*threshold
  iraf.datapars.ccdread='RDNOISE'
  iraf.datapars.gain="GAIN"
  iraf.datapars.exposure="EXPTIME"
  iraf.datapars.airmass="AIRMASS"
  iraf.datapars.filter="FILTER"
  iraf.datapars.obstime="TIME-OBS"

  iraf.findpars.threshold=4.5
  iraf.findpars.roundhi=3
  iraf.findpars.roundlo=-3

  iraf.daofind.verify='no'
  iraf.daofind.verbose='no'
  
  #create the variable for the coordinate file
  allStars = imageName+'.coo'
  
  #Remove a previous coordinate file if one exists
  silentDelete(imageName+'.coo')

  #Find the stars in the aligned R image
  iraf.daofind("withsky",allStars)
  print("The file containing the data of the stars in the aligned R-image is ",allStars)
  print(" ") 

  if os.path.getsize(allStars) > 2239 :

   print("B. Separate stars inside and outside galaxy")
  
   #Delete existing files
   for f in ("temp.file","maskfile","maskfile.sat"):
     silentDelete(f)
  
   #Extract the coordinates from the allStars file, redirecting stdout to do so
   sys.stdout=open("temp.file","w")
   iraf.pdump(allStars,"xcenter,ycenter",'yes')
   sys.stdout = sys.__stdout__
  


  #Read the file to get the xy coordinate pairs in a list
   coords = open("temp.file")
   coordList = list(coords)
   countout=0
  
  #Create strings of xy pairs to write to files
   outGalString = ""
   for pair in coordList:
     #for each entry in the list, parse it into xy value integer pairs
     values = pair.split("  ")
     x = float(values[0])
     y = float(values[1])
    
    #Determine if the star lies within the galaxy-centered ellipse
     inGalaxy = False
     deltaX = x - xCenter
     deltaY = y - yCenter
     cdx = abs(deltaX)
     cdy = abs(deltaY)
     if (cdx < a and cdy < a):
       xt=(deltaX*math.cos(posAngleRad))+(deltaY*math.sin(posAngleRad))
       yt=(deltaY*math.cos(posAngleRad))-(deltaX*math.sin(posAngleRad))
       if(xt <= a and xt >= na and yt <= b and yt >= nb):
         inGalaxy = True
      
     if (not inGalaxy):
       outGalString += str(x) + " " + str(y) + "\n"
       countout=countout+1
 
  #Write the coordinate list to a file
   mask = open("maskfile","w")
   mask.write(outGalString)
   mask.close()
   print('')
   print("  ",countout," stars found outside galaxy")
   print('')
   if countout!=0 :

    print("C. Do photometry to find saturated stars")
  
    for f in ("maskfile.mag","maskfile.satmag","maskfile.sat"):
     silentDelete(f)
  
  #Configure 'centerpars', 'fitskypars','photpars'
    iraf.datapars.datamax=150000
    iraf.datapars.datamin='indef'
    iraf.centerpars.calgorithm="none"

    iraf.fitskypars.salgorithm="mode"
    iraf.fitskypars.annulus=annul
    iraf.fitskypars.dannulus=10

    iraf.photpars.apertures=fwhm
    iraf.phot.verify='no'
    iraf.phot.verbose='no'
  
  #Call 'phot' to get the data of the stars 
    iraf.phot (imageName,"maskfile","maskfile.mag")

    boexpr="PIER==305"
    iraf.pselect("maskfile.mag","maskfile.satmag",boexpr)

    sys.stdout=open("maskfile.sat","w")
    iraf.pdump("maskfile.satmag","xcenter,ycenter","yes")
    sys.stdout = sys.__stdout__
  
    print("D. Make mask of stars outside galaxy")
  
  #Delete the rmask if one exists
    silentDelete("rmask.fits")
  
  #Configure 'imedit'
    iraf.imedit.cursor="maskfile"
    iraf.imedit.display='no'
    iraf.imedit.autodisplay='no'
    iraf.imedit.aperture="circular"
    iraf.imedit.value=-1000
    iraf.imedit.default="e"
  
  #Replace the pixel values near the star coordinates with value -1000 using imedit
    iraf.imedit(imageName,"redit",radius=aper)
 
    iraf.imcopy.verbose='no'
  #Do the same for the saturated stars, but with larger apertures
    satMaskSize = os.path.getsize("maskfile.sat")
    if satMaskSize < 1 : 
     print("   No saturated stars found")
    if (satMaskSize!=0):
     iraf.imedit.cursor="maskfile.sat"
     iraf.imedit.radius=2*aper
     iraf.imedit("redit","rmask")
    else:
     iraf.imcopy("redit","rmask")
  
  #Replace good pixels with value 0 in 'rmask'
    iraf.imreplace.lower=-999
    iraf.imreplace.upper='indef'
    iraf.imreplace("rmask",0)

  #Replace bad pixels with value 1 in 'rmask'
    iraf.imreplace.lower='indef'
    iraf.imreplace.upper=-1000
    iraf.imreplace("rmask",1)
  
   
  #Remove the mask file if one already exists
    silentDelete(imageName+"mask.fits")
    iraf.imcopy("rmask",imageName+"mask")

    print(" ")
    print("The mask image is ",imageName+"mask")
    print("The masked image is ",imageName+"Masked")
    print(" ")
 
   else :
    print("No stars found outside galaxy, no mask created.")

  if os.path.getsize(allStars) < 2314 :
   print("No stars found, no mask created.")



  #Delete intermediate images 
  for f in ("rmask.fits","redit.fits","maskimage.fits","maskfile.mag","maskfile.satmag","temp.file","withsky.fits","mask.fits"):
    silentDelete(f)
Exemplo n.º 4
0
def build(f):

    ### is this an MEF file
    current_ext = 0
    NEXTEND = 0

    EXTEND = f[0].header["EXTEND"]
    if EXTEND == "T":
        NEXTEND = f[0].header["NEXTEND"]
        current_ext = 1

    ### create the name of the output MEF psf file
    if not f[0].header.has_key("FILENAME"):
        os.unlink(opt.filename)
        sys.exit("The fits file " + opt.filename + " has no EXPNUM keyword\n")

    sexp = f[0].header["FILENAME"]
    mef_psf = sexp + "p_psf_iraf.fits"
    ### create an MEF file that will contian the PSF(s)
    import pyfits

    fitsobj = pyfits.HDUList()
    prihdu = pyfits.PrimaryHDU()
    import re

    prihdu.header.update("FILENAME", sexp, comment="CFHT Exposure Numebr")
    prihdu.header.update("NEXTEND", NEXTEND, comment="number of extensions")
    version = re.match(r"\$Rev.*: (\d*.\d*) \$", __Version__).group(1)
    prihdu.header.update("MKPSF_V", float(version), comment="Version number of mkpsf")
    fitsobj.append(prihdu)
    if os.access(mef_psf, os.F_OK):
        os.unlink(mef_psf)
    fitsobj.writeto(mef_psf)
    fitsobj.close()
    outfits = pyfits.open(mef_psf, "append")
    prihdr = outfits[0].header

    import jjkmode

    ### Get my python routines
    from pyraf import iraf
    from pyraf.irafpar import IrafParList

    ### keep all the parameters locally cached.
    iraf.set(uparm="./")

    ### Load the required IRAF packages
    iraf.digiphot()
    iraf.apphot()
    iraf.daophot()

    ### temp file name hash.
    tfile = {}

    while current_ext <= NEXTEND:

        ### this is a psf SCRIPT so the showplots and interactive are off by force

        print "Working on image section " + str(current_ext)

        iraf.findpars.sharplo = 0
        iraf.findpars.sharphi = 0.7
        iraf.findpars.roundlo = -0.7
        iraf.findpars.roundhi = 0.7

        iraf.datapars.datamax = 20000
        iraf.datapars.airmass = "AIRMASS"
        iraf.datapars.filter = "FILTER"
        iraf.datapars.obstime = "TIME-OBS"
        iraf.datapars.exposure = "EXPTIME"
        iraf.datapars.gain = "GAIN"
        iraf.datapars.ccdread = "RDNOISE"
        iraf.datapars.fwhmpsf = opt.fwhm

        iraf.daopars.nclean = 2
        iraf.daopars.psfrad = 5.0 * opt.fwhm
        iraf.daopars.fitrad = 0.85 * opt.fwhm
        iraf.daopars.function = "gauss"

        iraf.centerpars.calgorithm = "centroid"

        zero_mag = 26.19
        iraf.photpars.zmag = zero_mag
        iraf.photpars.apertures = int(0.85 * opt.fwhm)
        iraf.fitskypars.annulus = 2 + int(opt.fwhm * 4.00)
        iraf.fitskypars.dannulus = int(opt.fwhm * 2.0)
        iraf.daophot.verbose = no
        iraf.daophot.verify = no
        iraf.daophot.update = no

        iraf.psf.interactive = no
        iraf.pstselect.interactive = no

        iraf.datapars.saveParList()
        iraf.fitskypars.saveParList()
        iraf.centerpars.saveParList()
        iraf.findpars.saveParList()
        iraf.photpars.saveParList()

        tfiles = [
            "coo_bright",
            "coo_ok",
            "coo_faint",
            "mag_all",
            "mag_bright",
            "mag_ok",
            "mag_good",
            "mag_best",
            "pst_in",
            "pst_out",
            "pst_out2",
            "prf",
            "psg_org",
            "psg",
            "psf_1.fits",
            "psf_2.fits",
            "psf_final.fits",
            "psf_3.fits",
            "psf_4.fits",
            "mag_pst",
            "coo_pst",
            "nst",
            "nrj",
            "seepsf.fits",
            "sub.fits",
            "fwhm",
            "apcor",
        ]

        for file in tfiles:
            extname = "chip" + str(f[current_ext].header.get("IMAGEID", str(current_ext))).zfill(2)
            tfile[file] = sexp + "_" + extname + "." + file
            if os.access(tfile[file], os.F_OK):
                os.unlink(tfile[file])

        if EXTEND == "T":
            this_image = opt.filename + "[" + extname + "]"
        else:
            this_image = opt.filename
        gain = f[current_ext].header.get("GAIN", 1.0)
        #### set sky/sigma parameters specific to this frame.
        (skyvalue, sigma) = jjkmode.stats(f[current_ext].data)
        import math

        sigma = math.sqrt(skyvalue / gain)
        datamin = float(skyvalue) - 8.0 * float(sigma)
        print "Determined sky level to be " + str(skyvalue) + " +/-" + str(sigma)

        iraf.datapars.datamin = datamin
        iraf.datapars.sigma = float(sigma)
        iraf.datapars.saveParList()

        iraf.fitskypars.skyvalue = skyvalue
        iraf.fitskypars.saveParList()
        ### find the bright stars in the image.
        print "sextracting for stars in " + this_image

        ###iraf.daophot.daofind(image=this_image,
        ###                     output=tfile['coo_bright'],threshold=4.0)

        os.system(
            "sex -c /home/cadc/kavelaar/12kproc/config/default.sex -SATUR_LEVEL 25000 -CATALOG_NAME "
            + tfile["coo_bright"]
            + " "
            + this_image
        )

        ### print "finding stellar locus in sround/ground space"
        print "clipping using star_class > 0.85 "
        fcoo = open(tfile["coo_bright"], "r")
        lines = fcoo.readlines()
        fcoo.close()
        import numarray, math

        fout = open(tfile["coo_ok"], "w")
        for line in lines:
            if re.match(r"^#", line) or re.search(r"INDEF", line):
                continue
            values = line.split()
            star_class = float(values[2])
            if star_class > 0.75:
                fout.write(line)
        fout.close()

        print "Measuring photometry for psf candidate stars in " + tfile["coo_ok"]
        iraf.daophot.phot(image=this_image, coords=tfile["coo_ok"], output=tfile["mag_bright"])

        ### do this selection in 2 steps because of the way IRAF handles INDEFs
        print "Selecting stars that have good centroids and magnitudes "
        iraf.pselect(
            tfile["mag_bright"], tfile["mag_ok"], "(CIER==0)&&(PIER==0)&&(SIER==0)&&(MSKY>0)&&(MSKY<2e5)&&(MSKY!=INDEF)"
        )
        print "Selecting stars that have normal sky levels"
        condition = "(abs(MSKY -" + str(skyvalue) + ") < 5.0*" + str(sigma) + ")"
        iraf.pselect(tfile["mag_ok"], tfile["mag_good"], condition)

        a = iraf.txdump(tfile["mag_good"], "SSKEW", iraf.yes, Stdout=1)
        aa = []
        for v in a:
            aa.append(float(v))

        a = numarray.array(aa)
        mean = a.mean()
        aa = a * a

        stddev = math.sqrt(aa.sum() / len(aa) - mean ** 2)
        limit = mean + 2 * stddev

        os.unlink(tfile["mag_good"])
        condition = condition + " && SSKEW < " + str(limit)
        iraf.pselect(tfile["mag_ok"], tfile["mag_good"], condition)

        print "Choosing the psf stars"
        iraf.pstselect(image=this_image, photfile=tfile["mag_good"], pstfile=tfile["pst_in"], maxnpsf=25)

        ## construct an initial PSF image
        print "computing psf with neighbor stars based on complete star list"
        iraf.psf.mode = "a"
        iraf.psf(
            image=this_image,
            photfile=tfile["mag_bright"],
            pstfile=tfile["pst_in"],
            psfimage=tfile["psf_1.fits"],
            opstfile=tfile["pst_out"],
            groupfile=tfile["psg_org"],
            varorder=0,
        )

        try:
            print "subtracting the psf neighbors and placing the results in " + tfile["sub.fits"]
            iraf.daophot.nstar(
                image=this_image,
                groupfile=tfile["psg_org"],
                psfimage=tfile["psf_1.fits"],
                nstarfile=tfile["nst"],
                rejfile=tfile["nrj"],
            )

            iraf.daophot.substar(
                image=this_image,
                photfile=tfile["nst"],
                exfile=tfile["pst_in"],
                psfimage=tfile["psf_1.fits"],
                subimage=tfile["sub.fits"],
            )

            a = iraf.daophot.txdump(tfile["nst"], "chi", "yes", Stdout=1)
            aa = []
            for v in a:
                aa.append(float(v))

            a = numarray.array(aa)
            mean = a.mean()
            aa = a * a

            stddev = math.sqrt(aa.sum() / len(aa) - mean ** 2)
            limit = mean + 2.5 * stddev

            print "Selecting those psf stars with CHI^2 <" + str(limit) + " after fitting with trial psf"
            iraf.pselect(tfile["nst"], tfile["mag_best"], "CHI < " + str(limit))

            os.unlink(tfile["pst_out"])
            ##    os.unlink(tfile['psg'])
            ## rebuild the PSF file with the psf stars that fit well..
            ## using the neighbor subtracted image

            print "Rebuilding the PSF"

            iraf.daophot.psf(
                image=tfile["sub.fits"],
                photfile=tfile["mag_best"],
                pstfile=tfile["pst_in"],
                psfimage=tfile["psf_2.fits"],
                opstfile=tfile["pst_out"],
                groupfile=tfile["psg"],
                varorder=0,
            )

            print "re-subtracting with rebuilt psf"

            os.unlink(tfile["nst"])
            os.unlink(tfile["nrj"])
            iraf.daophot.nstar(
                image=this_image,
                groupfile=tfile["psg"],
                psfimage=tfile["psf_2.fits"],
                nstarfile=tfile["nst"],
                rejfile=tfile["nrj"],
            )

            os.unlink(tfile["sub.fits"])
            iraf.daophot.substar(
                image=this_image,
                photfile=tfile["nst"],
                exfile=tfile["pst_in"],
                psfimage=tfile["psf_2.fits"],
                subimage=tfile["sub.fits"],
            )

            os.unlink(tfile["psg"])
            os.unlink(tfile["pst_out"])
            iraf.daophot.psf(
                image=tfile["sub.fits"],
                photfile=tfile["mag_best"],
                pstfile=tfile["pst_in"],
                psfimage=tfile["psf_3.fits"],
                opstfile=tfile["pst_out"],
                groupfile=tfile["psg"],
                varorder=0,
            )

            os.unlink(tfile["nrj"])
            os.unlink(tfile["nst"])
            iraf.daophot.nstar(
                image=this_image,
                groupfile=tfile["psg"],
                psfimage=tfile["psf_3.fits"],
                nstarfile=tfile["nst"],
                rejfile=tfile["nrj"],
            )

            a = iraf.daophot.txdump(tfile["nst"], "chi", "yes", Stdout=1)
            aa = []
            for v in a:
                aa.append(float(v))

            a = numarray.array(aa)
            mean = a.mean()
            aa = a * a

            stddev = math.sqrt(aa.sum() / len(aa) - mean ** 2)
            limit = mean + 2 * stddev
            limit = 2.0

            # print "Selecting those psf stars with CHI^2 < "+str(limit)+" after fit with GOOD psf"

            os.unlink(tfile["mag_best"])
            iraf.pselect(tfile["nst"], tfile["mag_best"], "CHI < " + str(limit))

            print "Building final PSF.... "
            os.unlink(tfile["sub.fits"])
            iraf.daophot.substar(
                image=this_image,
                photfile=tfile["nst"],
                exfile=tfile["pst_in"],
                psfimage=tfile["psf_3.fits"],
                subimage=tfile["sub.fits"],
            )

            os.unlink(tfile["psg"])
            os.unlink(tfile["pst_out"])
            iraf.daophot.psf(
                image=tfile["sub.fits"],
                photfile=tfile["mag_best"],
                pstfile=tfile["pst_in"],
                psfimage=tfile["psf_final.fits"],
                opstfile=tfile["pst_out"],
                groupfile=tfile["psg"],
                varorder=0,
            )

            print "building an analytic psf for the FWHM calculations"
            os.unlink(tfile["pst_out"])
            os.unlink(tfile["psg"])
            iraf.daophot.psf(
                image=tfile["sub.fits"],
                photfile=tfile["mag_best"],
                pstfile=tfile["pst_in"],
                psfimage=tfile["psf_4.fits"],
                opstfile=tfile["pst_out"],
                groupfile=tfile["psg"],
                varorder=-1,
            )
        except:
            print sys.exc_info()[1]
            print "ERROR: Reverting to first pass psf"
            tfile["psf_final.fits"] = tfile["psf_1.fits"]

            iraf.daophot.psf(
                image=this_image,
                photfile=tfile["mag_best"],
                pstfile=tfile["pst_in"],
                psfimage=tfile["psf_4.fits"],
                opstfile=tfile["pst_out2"],
                groupfile=tfile["psg"],
                varorder=-1,
            )

        psf_ap = iraf.photpars.apertures
        ap1 = int(psf_ap)
        ap2 = int(4.0 * opt.fwhm)
        apcor = "INDEF"
        aperr = "INDEF"
        if 0:
            # try
            ### now that we have the psf use the output list of psf stars
            ### to compute the aperature correction
            lines = iraf.txdump(tfile["pst_out"], "xcen,ycen,mag,id", iraf.yes, Stdout=tfile["coo_pst"])

            ## set the lower ap value for the COG (normally set to 2)
            if ap1 < 3:
                smallap = 1
            else:
                smallap = 2 - ap1 + 1
                ap1 = 2

            ap2 = int(math.floor(4.0 * opt.fwhm))
            naperts = ap2 - ap1 + 1

            iraf.photpars.apertures = str(ap1) + ":" + str(ap2) + ":1"
            iraf.photpars.saveParList()
            iraf.daophot.phot(image=this_image, coords=tfile["coo_pst"], output=tfile["mag_pst"])
            iraf.photcal()
            iraf.photcal.mkapfile(
                tfile["mag_pst"],
                naperts=naperts,
                apercors=tfile["apcor"],
                smallap=smallap,
                verify="no",
                gcommands="",
                interactive=0,
            )
            fin = open(tfile["apcor"], "r")
            lines = fin.readlines()
            values = lines[2].split()
            apcor = values[1]
            aperr = values[2]
        # except:

        ## compute the FWHM of the PSF image using the analytic PSF (VarOrd=-1)
        psf_file = pyfits.open(tfile["psf_4.fits"])

        fwhm = psf_file[0].header.get("PAR1", 99.0) + psf_file[0].header.get("PAR2", 99.0)
        psf_file.close()
        #   ## Open the psf.fits
        infits = pyfits.open(tfile["psf_final.fits"])
        hdu = infits[0]
        inhdu = hdu.header
        inhdu.update("XTENSION", "IMAGE", before="SIMPLE")
        inhdu.update("PCOUNT", 0, after="NAXIS2")
        inhdu.update("GCOUNT", 1, after="PCOUNT")
        del hdu.header["SIMPLE"]
        del hdu.header["EXTEND"]
        inhdu.update("EXTNAME", extname, comment="image extension identifier")
        # inhdu.update("SLOW",slow,comment="SROUND low cutoff")
        # inhdu.update("SIGH",sigh,comment="SROUND high cutoff")
        inhdu.update("PFWHM", fwhm, comment="FWHM of stars based on PSF fitting")
        inhdu.update("ZMAG", zero_mag, comment="ZMAG of PSF ")
        inhdu.update("BCKG", skyvalue, comment="Mean sky level in counts")
        inhdu.update("BCKG_STD", sigma, comment="standard deviation of sky in counts")
        inhdu.update("AP1", psf_ap, comment="Apperture used for PSF flux")
        inhdu.update("AP2", ap2, comment="Full Flux aperture")
        inhdu.update("APCOR", apcor, comment="Apperture correction (ap1->ap2)")
        inhdu.update("APERR", apcor, comment="Uncertainty in APCOR")

        #    ### append this psf to the output images....
        print "Sticking this PSF onto the output file"
        f[current_ext].header.update("PFWHM", fwhm, comment="FWHM of stars based on PSF fitting")
        f[current_ext].header.update("BCKG", skyvalue, comment="Mean sky level in counts")
        f[current_ext].header.update("BCKG_STD", sigma, comment="Standard deviation of sky in counts")
        f.flush()
        outfits.append(hdu)
        outfits.flush()
        infits.close()

        ### remove the temp file we used for this computation.
        for tf in tfile.keys():
            if os.access(tfile[tf], os.F_OK):
                os.unlink(tfile[tf])

        current_ext = current_ext + 1

    outfits.close()
    return mef_psf
Exemplo n.º 5
0
def alignImages(imageName,imageName_Ha):
  #Read the fwhm and seeing from the image file
  imageHeader = fits.open(imageName+".fits")[0]
  fwhm = imageHeader.header['SEEING']
  annul = 5.0 * fwhm
  aper=3.0 * fwhm
  sigma = imageHeader.header['SKYSIGMA']
  iraf.daophot(_doprint=0)
  #Do 'daofind' on the image to locate the stars
  print("1. Find stars using 'daofind'")
  
  #Configure 'datapars', 'findpars', and 'daofind'
  
  iraf.datapars.fwhmpsf=fwhm
  iraf.datapars.sigma=sigma
  iraf.datapars.datamin=-10
  iraf.datapars.ccdread="RDNOISE"
  iraf.datapars.gain="GAIN"
  iraf.datapars.exposure="EXPTIME"
  iraf.datapars.airmass="AIRMASS"
  iraf.datapars.filter="FILTER"
  iraf.datapars.obstime="TIME-OBS"

  iraf.findpars.threshold=20*sigma
  iraf.findpars.sharplo=0.2
  iraf.findpars.sharphi=1.0
  iraf.findpars.roundlo=-1.0
  iraf.findpars.roundhi=1.0
  iraf.daofind.verify='no'
  iraf.daofind.verbose='no'

  #Delete exisiting coordinate,output files of 'phot',file containg data of 'good' stars, old images
  for f in("coord","mag","psel","R_final.fits","Ha_final.fits"):
    silentDelete(f)

  iraf.daofind(imageName,'coord')

  print("File containing the coordinates of the stars is coord")


  print(" ")
  #Configure 'centerpars', 'fitskypars','photpars'
  iraf.datapars.datamax=150000
  iraf.centerpars.calgorithm="centroid"
  iraf.centerpars.cbox=16
  iraf.centerpars.maxshift=3

  iraf.fitskypars.salgorithm="mode"
  iraf.fitskypars.annulus=annul
  iraf.fitskypars.dannulus=10

  iraf.photpars.apertures=aper

  iraf.phot.verify='no'
  iraf.phot.verbose='no'

  print("2. Obtain data of stars using 'phot'")
  #Call 'phot' to get the data of the stars 
  iraf.phot (imageName,'coord','mag')

  #sort in order of increasing magnitude of stars
  iraf.psort('mag',"mag")
  boundsig=sigma+2
  boexpr="CIER==0 && PIER==0 && STDEV <="+str(boundsig)  
  print("File containing the data of the stars in order of decreasing brightness is mag")

  print (" ")

  print("3. Select stars with low error, no bad pixels")
  #Select stars that have no centering error, skyerror <sig+2 and no bad pixels
  iraf.pselect ("mag" ,"psel" ,boexpr)

  print("File containing stars with low sky error,low centering error is psel")
  print(" ")

  #Renumber the ID number of the stars in order of increasing magnitude
  iraf.prenumber ("psel")


  #Delete existing files
  for f in ("pdump.file","stars25",'alR.fits','rIn.fits','haIn.fits'):
    silentDelete(f)

  print("4. Select the 25 brightest stars")
  iraf.pselect ("psel","stars25", "ID <=25")

  print("File containing the brightest 25 'good' stars is stars25")
  print(" ")

  #Pick out only the required data of stars from the .25 file
  sys.stdout=open("pdump.file","w")
  iraf.pdump ("stars25","xcenter,ycenter,flux","yes")
  sys.stdout = sys.__stdout__
  
  print("The coordinates and flux are stored in pdump.file")
  
  #Align images
  iraf.imcopy(imageName,'rIn')
  iraf.imcopy(imageName_Ha,'haIn')
  print("Aligning images")
  #iraf.imalign.verbose='no'
  iraf.imalign("rIn,haIn", "rIn","pdump.file","R_final,Ha_final")
Exemplo n.º 6
0
def build(f):

    ### is this an MEF file
    current_ext = 0
    NEXTEND = 0

    EXTEND = f[0].header['EXTEND']
    if (EXTEND == "T"):
        NEXTEND = f[0].header['NEXTEND']
        current_ext = 1

    ### create the name of the output MEF psf file
    if not f[0].header.has_key('FILENAME'):
        os.unlink(opt.filename)
        sys.exit('The fits file ' + opt.filename + ' has no EXPNUM keyword\n')

    sexp = f[0].header['FILENAME']
    mef_psf = sexp + "p_psf_iraf.fits"
    ### create an MEF file that will contian the PSF(s)
    import pyfits
    fitsobj = pyfits.HDUList()
    prihdu = pyfits.PrimaryHDU()
    import re
    prihdu.header.update('FILENAME', sexp, comment='CFHT Exposure Numebr')
    prihdu.header.update('NEXTEND', NEXTEND, comment='number of extensions')
    version = re.match(r'\$Rev.*: (\d*.\d*) \$', __Version__).group(1)
    prihdu.header.update('MKPSF_V',
                         float(version),
                         comment="Version number of mkpsf")
    fitsobj.append(prihdu)
    if os.access(mef_psf, os.F_OK):
        os.unlink(mef_psf)
    fitsobj.writeto(mef_psf)
    fitsobj.close()
    outfits = pyfits.open(mef_psf, "append")
    prihdr = outfits[0].header

    import jjkmode

    ### Get my python routines
    from pyraf import iraf
    from pyraf.irafpar import IrafParList

    ### keep all the parameters locally cached.
    iraf.set(uparm="./")

    ### Load the required IRAF packages
    iraf.digiphot()
    iraf.apphot()
    iraf.daophot()

    ### temp file name hash.
    tfile = {}

    while (current_ext <= NEXTEND):

        ### this is a psf SCRIPT so the showplots and interactive are off by force

        print "Working on image section " + str(current_ext)

        iraf.findpars.sharplo = 0
        iraf.findpars.sharphi = 0.7
        iraf.findpars.roundlo = -0.7
        iraf.findpars.roundhi = 0.7

        iraf.datapars.datamax = 20000
        iraf.datapars.airmass = 'AIRMASS'
        iraf.datapars.filter = 'FILTER'
        iraf.datapars.obstime = 'TIME-OBS'
        iraf.datapars.exposure = 'EXPTIME'
        iraf.datapars.gain = 'GAIN'
        iraf.datapars.ccdread = 'RDNOISE'
        iraf.datapars.fwhmpsf = opt.fwhm

        iraf.daopars.nclean = 2
        iraf.daopars.psfrad = 5.0 * opt.fwhm
        iraf.daopars.fitrad = 0.85 * opt.fwhm
        iraf.daopars.function = "gauss"

        iraf.centerpars.calgorithm = 'centroid'

        zero_mag = 26.19
        iraf.photpars.zmag = zero_mag
        iraf.photpars.apertures = int(0.85 * opt.fwhm)
        iraf.fitskypars.annulus = 2 + int(opt.fwhm * 4.00)
        iraf.fitskypars.dannulus = int(opt.fwhm * 2.0)
        iraf.daophot.verbose = no
        iraf.daophot.verify = no
        iraf.daophot.update = no

        iraf.psf.interactive = no
        iraf.pstselect.interactive = no

        iraf.datapars.saveParList()
        iraf.fitskypars.saveParList()
        iraf.centerpars.saveParList()
        iraf.findpars.saveParList()
        iraf.photpars.saveParList()

        tfiles = [
            'coo_bright', 'coo_ok', 'coo_faint', 'mag_all', 'mag_bright',
            'mag_ok', 'mag_good', 'mag_best', 'pst_in', 'pst_out', 'pst_out2',
            'prf', 'psg_org', 'psg', 'psf_1.fits', 'psf_2.fits',
            'psf_final.fits', 'psf_3.fits', 'psf_4.fits', 'mag_pst', 'coo_pst',
            'nst', 'nrj', 'seepsf.fits', 'sub.fits', 'fwhm', 'apcor'
        ]

        for file in tfiles:
            extname = "chip" + str(f[current_ext].header.get(
                'IMAGEID', str(current_ext))).zfill(2)
            tfile[file] = sexp + "_" + extname + "." + file
            if (os.access(tfile[file], os.F_OK)):
                os.unlink(tfile[file])

        if (EXTEND == "T"):
            this_image = opt.filename + "[" + extname + "]"
        else:
            this_image = opt.filename
        gain = f[current_ext].header.get('GAIN', 1.0)
        #### set sky/sigma parameters specific to this frame.
        (skyvalue, sigma) = jjkmode.stats(f[current_ext].data)
        import math
        sigma = math.sqrt(skyvalue / gain)
        datamin = float(skyvalue) - 8.0 * float(sigma)
        print "Determined sky level to be " + str(skyvalue) + " +/-" + str(
            sigma)

        iraf.datapars.datamin = datamin
        iraf.datapars.sigma = float(sigma)
        iraf.datapars.saveParList()

        iraf.fitskypars.skyvalue = skyvalue
        iraf.fitskypars.saveParList()
        ### find the bright stars in the image.
        print "sextracting for stars in " + this_image

        ###iraf.daophot.daofind(image=this_image,
        ###                     output=tfile['coo_bright'],threshold=4.0)

        os.system(
            "sex -c /home/cadc/kavelaar/12kproc/config/default.sex -SATUR_LEVEL 25000 -CATALOG_NAME "
            + tfile['coo_bright'] + " " + this_image)

        ### print "finding stellar locus in sround/ground space"
        print "clipping using star_class > 0.85 "
        fcoo = open(tfile['coo_bright'], 'r')
        lines = fcoo.readlines()
        fcoo.close()
        import numarray, math
        fout = open(tfile['coo_ok'], 'w')
        for line in lines:
            if re.match(r'^#', line) or re.search(r'INDEF', line):
                continue
            values = line.split()
            star_class = float(values[2])
            if star_class > 0.75:
                fout.write(line)
        fout.close()

        print "Measuring photometry for psf candidate stars in " + tfile[
            'coo_ok']
        iraf.daophot.phot(image=this_image,
                          coords=tfile['coo_ok'],
                          output=tfile['mag_bright'])

        ### do this selection in 2 steps because of the way IRAF handles INDEFs
        print "Selecting stars that have good centroids and magnitudes "
        iraf.pselect(
            tfile['mag_bright'], tfile['mag_ok'],
            "(CIER==0)&&(PIER==0)&&(SIER==0)&&(MSKY>0)&&(MSKY<2e5)&&(MSKY!=INDEF)"
        )
        print "Selecting stars that have normal sky levels"
        condition = "(abs(MSKY -" + str(skyvalue) + ") < 5.0*" + str(
            sigma) + ")"
        iraf.pselect(tfile['mag_ok'], tfile['mag_good'], condition)

        a = iraf.txdump(tfile['mag_good'], "SSKEW", iraf.yes, Stdout=1)
        aa = []
        for v in a:
            aa.append(float(v))

        a = numarray.array(aa)
        mean = a.mean()
        aa = a * a

        stddev = math.sqrt(aa.sum() / len(aa) - mean**2)
        limit = mean + 2 * stddev

        os.unlink(tfile['mag_good'])
        condition = condition + " && SSKEW < " + str(limit)
        iraf.pselect(tfile['mag_ok'], tfile['mag_good'], condition)

        print "Choosing the psf stars"
        iraf.pstselect(image=this_image,
                       photfile=tfile['mag_good'],
                       pstfile=tfile['pst_in'],
                       maxnpsf=25)

        ## construct an initial PSF image
        print "computing psf with neighbor stars based on complete star list"
        iraf.psf.mode = 'a'
        iraf.psf(image=this_image,
                 photfile=tfile['mag_bright'],
                 pstfile=tfile['pst_in'],
                 psfimage=tfile['psf_1.fits'],
                 opstfile=tfile['pst_out'],
                 groupfile=tfile['psg_org'],
                 varorder=0)

        try:
            print "subtracting the psf neighbors and placing the results in " + tfile[
                'sub.fits']
            iraf.daophot.nstar(image=this_image,
                               groupfile=tfile['psg_org'],
                               psfimage=tfile['psf_1.fits'],
                               nstarfile=tfile['nst'],
                               rejfile=tfile['nrj'])

            iraf.daophot.substar(image=this_image,
                                 photfile=tfile['nst'],
                                 exfile=tfile['pst_in'],
                                 psfimage=tfile['psf_1.fits'],
                                 subimage=tfile['sub.fits'])

            a = iraf.daophot.txdump(tfile['nst'], 'chi', 'yes', Stdout=1)
            aa = []
            for v in a:
                aa.append(float(v))

            a = numarray.array(aa)
            mean = a.mean()
            aa = a * a

            stddev = math.sqrt(aa.sum() / len(aa) - mean**2)
            limit = mean + 2.5 * stddev

            print "Selecting those psf stars with CHI^2 <" + str(
                limit) + " after fitting with trial psf"
            iraf.pselect(tfile['nst'], tfile['mag_best'],
                         "CHI < " + str(limit))

            os.unlink(tfile['pst_out'])
            ##    os.unlink(tfile['psg'])
            ## rebuild the PSF file with the psf stars that fit well..
            ## using the neighbor subtracted image

            print "Rebuilding the PSF"

            iraf.daophot.psf(image=tfile['sub.fits'],
                             photfile=tfile['mag_best'],
                             pstfile=tfile['pst_in'],
                             psfimage=tfile['psf_2.fits'],
                             opstfile=tfile['pst_out'],
                             groupfile=tfile['psg'],
                             varorder=0)

            print "re-subtracting with rebuilt psf"

            os.unlink(tfile['nst'])
            os.unlink(tfile['nrj'])
            iraf.daophot.nstar(image=this_image,
                               groupfile=tfile['psg'],
                               psfimage=tfile['psf_2.fits'],
                               nstarfile=tfile['nst'],
                               rejfile=tfile['nrj'])

            os.unlink(tfile['sub.fits'])
            iraf.daophot.substar(image=this_image,
                                 photfile=tfile['nst'],
                                 exfile=tfile['pst_in'],
                                 psfimage=tfile['psf_2.fits'],
                                 subimage=tfile['sub.fits'])

            os.unlink(tfile['psg'])
            os.unlink(tfile['pst_out'])
            iraf.daophot.psf(image=tfile['sub.fits'],
                             photfile=tfile['mag_best'],
                             pstfile=tfile['pst_in'],
                             psfimage=tfile['psf_3.fits'],
                             opstfile=tfile['pst_out'],
                             groupfile=tfile['psg'],
                             varorder=0)

            os.unlink(tfile['nrj'])
            os.unlink(tfile['nst'])
            iraf.daophot.nstar(image=this_image,
                               groupfile=tfile['psg'],
                               psfimage=tfile['psf_3.fits'],
                               nstarfile=tfile['nst'],
                               rejfile=tfile['nrj'])

            a = iraf.daophot.txdump(tfile['nst'], 'chi', 'yes', Stdout=1)
            aa = []
            for v in a:
                aa.append(float(v))

            a = numarray.array(aa)
            mean = a.mean()
            aa = a * a

            stddev = math.sqrt(aa.sum() / len(aa) - mean**2)
            limit = mean + 2 * stddev
            limit = 2.0

            #print "Selecting those psf stars with CHI^2 < "+str(limit)+" after fit with GOOD psf"

            os.unlink(tfile['mag_best'])
            iraf.pselect(tfile['nst'], tfile['mag_best'],
                         "CHI < " + str(limit))

            print "Building final PSF.... "
            os.unlink(tfile['sub.fits'])
            iraf.daophot.substar(image=this_image,
                                 photfile=tfile['nst'],
                                 exfile=tfile['pst_in'],
                                 psfimage=tfile['psf_3.fits'],
                                 subimage=tfile['sub.fits'])

            os.unlink(tfile['psg'])
            os.unlink(tfile['pst_out'])
            iraf.daophot.psf(image=tfile['sub.fits'],
                             photfile=tfile['mag_best'],
                             pstfile=tfile['pst_in'],
                             psfimage=tfile['psf_final.fits'],
                             opstfile=tfile['pst_out'],
                             groupfile=tfile['psg'],
                             varorder=0)

            print "building an analytic psf for the FWHM calculations"
            os.unlink(tfile['pst_out'])
            os.unlink(tfile['psg'])
            iraf.daophot.psf(image=tfile['sub.fits'],
                             photfile=tfile['mag_best'],
                             pstfile=tfile['pst_in'],
                             psfimage=tfile['psf_4.fits'],
                             opstfile=tfile['pst_out'],
                             groupfile=tfile['psg'],
                             varorder=-1)
        except:
            print sys.exc_info()[1]
            print "ERROR: Reverting to first pass psf"
            tfile['psf_final.fits'] = tfile['psf_1.fits']

            iraf.daophot.psf(image=this_image,
                             photfile=tfile['mag_best'],
                             pstfile=tfile['pst_in'],
                             psfimage=tfile['psf_4.fits'],
                             opstfile=tfile['pst_out2'],
                             groupfile=tfile['psg'],
                             varorder=-1)

        psf_ap = iraf.photpars.apertures
        ap1 = int(psf_ap)
        ap2 = int(4.0 * opt.fwhm)
        apcor = "INDEF"
        aperr = "INDEF"
        if (0):
            #try
            ### now that we have the psf use the output list of psf stars
            ### to compute the aperature correction
            lines = iraf.txdump(tfile['pst_out'],
                                'xcen,ycen,mag,id',
                                iraf.yes,
                                Stdout=tfile['coo_pst'])

            ## set the lower ap value for the COG (normally set to 2)
            if (ap1 < 3):
                smallap = 1
            else:
                smallap = 2 - ap1 + 1
                ap1 = 2

            ap2 = int(math.floor(4.0 * opt.fwhm))
            naperts = ap2 - ap1 + 1

            iraf.photpars.apertures = str(ap1) + ":" + str(ap2) + ":1"
            iraf.photpars.saveParList()
            iraf.daophot.phot(image=this_image,
                              coords=tfile['coo_pst'],
                              output=tfile['mag_pst'])
            iraf.photcal()
            iraf.photcal.mkapfile(tfile['mag_pst'],
                                  naperts=naperts,
                                  apercors=tfile['apcor'],
                                  smallap=smallap,
                                  verify='no',
                                  gcommands='',
                                  interactive=0)
            fin = open(tfile['apcor'], 'r')
            lines = fin.readlines()
            values = lines[2].split()
            apcor = values[1]
            aperr = values[2]
        #except:

        ## compute the FWHM of the PSF image using the analytic PSF (VarOrd=-1)
        psf_file = pyfits.open(tfile['psf_4.fits'])

        fwhm = (psf_file[0].header.get('PAR1', 99.0) +
                psf_file[0].header.get('PAR2', 99.0))
        psf_file.close()
        #   ## Open the psf.fits
        infits = pyfits.open(tfile['psf_final.fits'])
        hdu = infits[0]
        inhdu = hdu.header
        inhdu.update('XTENSION', 'IMAGE', before='SIMPLE')
        inhdu.update('PCOUNT', 0, after='NAXIS2')
        inhdu.update('GCOUNT', 1, after='PCOUNT')
        del hdu.header['SIMPLE']
        del hdu.header['EXTEND']
        inhdu.update("EXTNAME", extname, comment="image extension identifier")
        #inhdu.update("SLOW",slow,comment="SROUND low cutoff")
        #inhdu.update("SIGH",sigh,comment="SROUND high cutoff")
        inhdu.update("PFWHM",
                     fwhm,
                     comment="FWHM of stars based on PSF fitting")
        inhdu.update("ZMAG", zero_mag, comment="ZMAG of PSF ")
        inhdu.update("BCKG", skyvalue, comment="Mean sky level in counts")
        inhdu.update("BCKG_STD",
                     sigma,
                     comment="standard deviation of sky in counts")
        inhdu.update("AP1", psf_ap, comment="Apperture used for PSF flux")
        inhdu.update("AP2", ap2, comment="Full Flux aperture")
        inhdu.update("APCOR", apcor, comment="Apperture correction (ap1->ap2)")
        inhdu.update("APERR", apcor, comment="Uncertainty in APCOR")

        #    ### append this psf to the output images....
        print "Sticking this PSF onto the output file"
        f[current_ext].header.update(
            "PFWHM", fwhm, comment="FWHM of stars based on PSF fitting")
        f[current_ext].header.update("BCKG",
                                     skyvalue,
                                     comment="Mean sky level in counts")
        f[current_ext].header.update(
            "BCKG_STD", sigma, comment="Standard deviation of sky in counts")
        f.flush()
        outfits.append(hdu)
        outfits.flush()
        infits.close()

        ### remove the temp file we used for this computation.
        for tf in tfile.keys():
            if os.access(tfile[tf], os.F_OK):
                os.unlink(tfile[tf])

        current_ext = current_ext + 1

    outfits.close()
    return mef_psf
def maskStars(imageName):
    #read in the fits data and header
    imageHeader = fits.open(imageName + ".fits")[0]

    #Read in the skysigma, skyv, and seeing (fwhm) from the header and set up other relevant values
    sigma = imageHeader.header['SKYSIGMA']
    threshold = 3.0 * sigma

    skyv = imageHeader.header['SKY']

    fwhm = imageHeader.header['SEEING']
    aper = 3.0 * fwhm
    annul = 5 * fwhm

    #Read in the galcut file
    galcut = open("galcut.file")
    galcutString = file.read(galcut)
    galcut.close()

    #Parse the galcut file into its variables
    galList = galcutString.split(' ')
    xCenter = float(galList[0])
    yCenter = float(galList[1])
    a = float(galList[2])  #Semimajor Axis
    eccent = float(galList[3])  #Eccentricity
    posAngle = float(galList[4])  #Position angle

    posAngleRad = (posAngle + 90) * 3.14159 / 180
    b = a * eccent  #Semiminor Axis
    na = -1 * a
    nb = -1 * b

    #Create a version of the r image with the sky added back in for the daofind procedure
    iraf.imdelete("withsky")
    iraf.imarith(imageName, '+', skyv, "withsky")

    # Load daophot package
    iraf.digiphot()
    iraf.daophot()

    print("A. Find stars using daofind")

    #DAOFIND  searches  the  IRAF  images image for local density maxima,
    #    with a  full-width  half-maxima  of  datapars.fwhmpsf,  and  a  peak
    #    amplitude  greater  than  findpars.threshold  * datapars.sigma above
    #    the local background, and writes a list of detected objects  in  the
    #    file  output.
    # Here we set the fwhm and sigma equal to those listed in the header.
    # We set the datamin to -3*sigma = -1*threshold defined above
    # The findpars.threshold is set to 4.5*sigma by default. You may have
    # to alter this value for images where too few/many stars are found.

    #Configure 'datapars', 'findpars', and 'daofind'
    iraf.datapars.fwhmpsf = fwhm
    iraf.datapars.sigma = sigma
    iraf.datapars.datamax = 'indef'
    iraf.datapars.datamin = (-1) * threshold
    iraf.datapars.ccdread = 'RDNOISE'
    iraf.datapars.gain = "GAIN"
    iraf.datapars.exposure = "EXPTIME"
    iraf.datapars.airmass = "AIRMASS"
    iraf.datapars.filter = "FILTER"
    iraf.datapars.obstime = "TIME-OBS"

    iraf.findpars.threshold = 4.5
    iraf.findpars.roundhi = 3
    iraf.findpars.roundlo = -3

    iraf.daofind.verify = 'no'
    iraf.daofind.verbose = 'no'

    #create the variable for the coordinate file
    allStars = imageName + '.coo'

    #Remove a previous coordinate file if one exists
    silentDelete(imageName + '.coo')

    #Find the stars in the aligned R image
    iraf.daofind("withsky", allStars)
    print(
        "The file containing the data of the stars in the aligned R-image is ",
        allStars)
    print(" ")

    if os.path.getsize(allStars) > 2239:

        print("B. Separate stars inside and outside galaxy")

        #Delete existing files
        for f in ("temp.file", "maskfile", "maskfile.sat"):
            silentDelete(f)

        #Extract the coordinates from the allStars file, redirecting stdout to do so
        sys.stdout = open("temp.file", "w")
        iraf.pdump(allStars, "xcenter,ycenter", 'yes')
        sys.stdout = sys.__stdout__

        #Read the file to get the xy coordinate pairs in a list
        coords = open("temp.file")
        coordList = list(coords)
        countout = 0

        #Create strings of xy pairs to write to files
        outGalString = ""
        for pair in coordList:
            #for each entry in the list, parse it into xy value integer pairs
            values = pair.split("  ")
            x = float(values[0])
            y = float(values[1])

            #Determine if the star lies within the galaxy-centered ellipse
            inGalaxy = False
            deltaX = x - xCenter
            deltaY = y - yCenter
            cdx = abs(deltaX)
            cdy = abs(deltaY)
            if (cdx < a and cdy < a):
                xt = (deltaX * math.cos(posAngleRad)) + (deltaY *
                                                         math.sin(posAngleRad))
                yt = (deltaY * math.cos(posAngleRad)) - (deltaX *
                                                         math.sin(posAngleRad))
                if (xt <= a and xt >= na and yt <= b and yt >= nb):
                    inGalaxy = True

            if (not inGalaxy):
                outGalString += str(x) + " " + str(y) + "\n"
                countout = countout + 1

    #Write the coordinate list to a file
        mask = open("maskfile", "w")
        mask.write(outGalString)
        mask.close()
        print('')
        print("  ", countout, " stars found outside galaxy")
        print('')
        if countout != 0:

            print("C. Do photometry to find saturated stars")

            for f in ("maskfile.mag", "maskfile.satmag", "maskfile.sat"):
                silentDelete(f)

    #Configure 'centerpars', 'fitskypars','photpars'
            iraf.datapars.datamax = 150000
            iraf.datapars.datamin = 'indef'
            iraf.centerpars.calgorithm = "none"

            iraf.fitskypars.salgorithm = "mode"
            iraf.fitskypars.annulus = annul
            iraf.fitskypars.dannulus = 10

            iraf.photpars.apertures = fwhm
            iraf.phot.verify = 'no'
            iraf.phot.verbose = 'no'

            #Call 'phot' to get the data of the stars
            iraf.phot(imageName, "maskfile", "maskfile.mag")

            boexpr = "PIER==305"
            iraf.pselect("maskfile.mag", "maskfile.satmag", boexpr)

            sys.stdout = open("maskfile.sat", "w")
            iraf.pdump("maskfile.satmag", "xcenter,ycenter", "yes")
            sys.stdout = sys.__stdout__

            print("D. Make mask of stars outside galaxy")

            #Delete the rmask if one exists
            silentDelete("rmask.fits")

            #Configure 'imedit'
            iraf.imedit.cursor = "maskfile"
            iraf.imedit.display = 'no'
            iraf.imedit.autodisplay = 'no'
            iraf.imedit.aperture = "circular"
            iraf.imedit.value = -1000
            iraf.imedit.default = "e"

            #Replace the pixel values near the star coordinates with value -1000 using imedit
            iraf.imedit(imageName, "redit", radius=aper)

            iraf.imcopy.verbose = 'no'
            #Do the same for the saturated stars, but with larger apertures
            satMaskSize = os.path.getsize("maskfile.sat")
            if satMaskSize < 1:
                print("   No saturated stars found")
            if (satMaskSize != 0):
                iraf.imedit.cursor = "maskfile.sat"
                iraf.imedit.radius = 2 * aper
                iraf.imedit("redit", "rmask")
            else:
                iraf.imcopy("redit", "rmask")

    #Replace good pixels with value 0 in 'rmask'
            iraf.imreplace.lower = -999
            iraf.imreplace.upper = 'indef'
            iraf.imreplace("rmask", 0)

            #Replace bad pixels with value 1 in 'rmask'
            iraf.imreplace.lower = 'indef'
            iraf.imreplace.upper = -1000
            iraf.imreplace("rmask", 1)

            #Remove the mask file if one already exists
            silentDelete(imageName + "mask.fits")
            iraf.imcopy("rmask", imageName + "mask")

            print(" ")
            print("The mask image is ", imageName + "mask")
            print("The masked image is ", imageName + "Masked")
            print(" ")

        else:
            print("No stars found outside galaxy, no mask created.")

    if os.path.getsize(allStars) < 2314:
        print("No stars found, no mask created.")

    #Delete intermediate images
    for f in ("rmask.fits", "redit.fits", "maskimage.fits", "maskfile.mag",
              "maskfile.satmag", "temp.file", "withsky.fits", "mask.fits"):
        silentDelete(f)