Example #1
0
 def photautocal(self,reference,reflimit=30,selflimit=30):
     """Find the rough WCS for this image, based on reference star_phootmetry,
     by cross-identifying stars by ID in DS9"""
     iraf.display(self.image,1)
     temp = iraf.pdump(self.photfile,"xcen,ycen,id","MAG<%f"%selflimit,Stdout=1)
     iraf.tvmark(1,"STDIN",Stdin=temp)
     iraf.display(reference.image,2)
     temp = iraf.pdump(reference.photfile,"xcen,ycen,id","MAG<%f"%reflimit,Stdout=1)
     iraf.tvmark(2,"STDIN",Stdin=temp)
     fred = open('tempautocal','w')
     input1=None
     input2=None
     print "Cross identify at least five stars, <q> to continue"
     while (input1!='q')and(input2!='q'):
         print
         input1 = raw_input("Star ID on Frame 1: ")
         input2 = raw_input("Matches star ID on Frame 2: ")
         try:
             star1 = self[int(input1)]
             star2 = reference[int(input2)]
         except:
             print "Known star integers required"
             continue
         fred.write("%f %f %f %f\n"%(star1[1],star1[2],star2[7],star2[8]))
     fred.close()
     iraf.ccmap(input='tempautocal',images=self.image,lngunits='degrees',
                database='database',fit='rxyscale',update='yes',inter='yes')
     print "Recalculating data"
     self.calcradec()
     print "If happy, now run ast_by_photometry"
Example #2
0
def recentre(image,refcoordfile):
    """Returns improved shift by centroiding
on the reference star using phot. This can be VERY
sensitive to the parameters in centerpars."""
    xin,yin = load(refcoordfile,unpack=True)
    try:
        iraf.phot(image,refcoordfile,'temp.mag',inter="no",calgorithm='centroid',
              mode='h',verify='no',update='no',verbose='no')
        xout,yout=iraf.pdump('temp.mag','xcen,ycen','yes',Stdout=1)[0].split()
    except:
        print "Recentring failed on", image
        return 0.,0.
    xout,yout = float(xout),float(yout)
    return xout-xin,yout-yin
Example #3
0
 def autoast(self,infile='asu.fit',RAfield='RAJ2000',decfield='DEJ2000',magfield='Vmag',magoff=None,
             markmax=30,crosses=False):
     """Calibrate image from file of data (2mass etc) by making initial astrometry
     by selecting a handful of stars, them using the other cal routines
     RAfield: column with RA (decimal!).
     decfield: column with dec (decimal!).
     magfield: column to calibrate magnitude by"""
     global xpoint,ypoint
     if self.image is None:
         print "No image to calibrate data by"
         return
     RA,dec,mag = load_data(infile,RAfield,decfield,magfield)
     fig = pylab.figure(figsize=(8,8))
     pylab.clf()
     try:
         magoff = magoff or mag.max()
         pylab.scatter(RA[mag>0],dec[mag>0],s=10**(0.4*(magoff-mag[mag>0])),hold=0,alpha=0.5)
     except:
         crosses=True
     if crosses: pylab.plot(RA,dec,'k+')
     pylab.axis([RA.max(),RA.min(),dec.min(),dec.max()])
     pylab.draw()
     cid = fig.canvas.mpl_connect('button_press_event',self._on_press)
     iraf.display(self.image,1)
     iraf.tvmark(1,"STDIN",Stdin=iraf.pdump(self.photfile,"xcen,ycen,id","MAG<%f"%markmax,Stdout=1))
     print "Pick five stars with middle button, then enter their IDs in order,\n<q> to continue"
     ids = []; self.xpoint=[]; self.ypoint=[]
     select = None
     while not(select=='q'):        
         select=raw_input('Star ID: ')
         try: ids.append(int(select))
         except: pass
     fig.canvas.mpl_disconnect('button_press_event')
     fred = open('autocalcoords','w')
     for i in range(min(len(ids),len(self.xpoint))):
         try:
             x,y = self[ids[i]][[1,2]]
             locmin = ((self.xpoint[i]-RA)**2+(self.ypoint[i]-dec)**2).argmin()
             fred.write('%f %f %15.12e %15.12e\n'%(x,y,RA[locmin],dec[locmin]))
         except:
             print "Coord pair %i failed"%i
     fred.close()
     iraf.ccmap(input='autocalcoords',images=self.image,lngunits='degrees',
                database='database',fit='rxyscale',update='yes',inter='yes')
     print "Recalculating..."
     self.calcradec()
     print "If happy, now run ast_by_file"        
def detect_stars(f,params):
    print 'Detecting stars in',f.name
    print 'Current directory is', os.getcwd()
    fp = params.loc_output+os.path.sep
    fn = f.fullname
    iraf.digiphot()
    iraf.daophot()
    print 'FWHM = ',f.fw
    nstars = 0
    thresh = 100
    while (nstars < 2*params.nstamps) and (thresh > 1.5):
        print 'thresh = ',thresh
        for d in ['temp.stars','temp.phot']:
            if os.path.exists(fp+d):
                os.system('/bin/rm '+fp+d)
        iraf.daofind(image=fn,output=fp+'temp.stars',interactive='no',verify='no',
                     threshold=thresh,sigma=30,fwhmpsf=f.fw,
                     datamin=params.pixel_min,datamax=params.pixel_max,
                     epadu=params.gain,readnoise=params.readnoise,
                     noise='poisson')
        iraf.phot(image=fn,output=fp+'temp.phot',coords=fp+'temp.stars',interactive='no',
                  verify='no',
                  sigma=30,fwhmpsf=f.fw,datamin=params.pixel_min,
                  datamax=params.pixel_max,epadu=params.gain,
                  readnoise=params.readnoise,noise='poisson',Stdout='/dev/null')
        nstars = 0
        if os.path.exists(fp+'temp.phot'):
            iraf.psort(infiles=fp+'temp.phot',field='MAG')   
            iraf.prenumber(infile=fp+'temp.phot')
            s = iraf.pdump(infiles=fp+'temp.phot',Stdout=1,fields='ID,XCENTER,YCENTER,MAG',
                           expr='yes')
            stars = np.zeros([len(s),3])
            i = 0
            for line in s:
                mag = line.split()[3]
                if not(mag == 'INDEF'):
                    stars[i,:] = np.array(map(float,line.split()[1:4]))
                    i += 1
            nstars = i
        thresh = thresh*0.5
    if nstars == 0:
        print 'Error: could not detect stars in',fn
        return None
    stars = stars[:i,:].copy()
    sys.old_stdout = sys.stdout
    return stars
Example #5
0
def simplepsfphot(image,coords,psf,refstar,centre=True,vary=False):
    """PSF photometry, with a given PSF file in psf used for every image"""
    iraf.dele('temp.mag*')
    iraf.dele('temp.als')
    iraf.dele('temp.sub.fits')
    if centre:
        xsh,ysh = recentre(image,refstar) 
        print "Fine Centring: ", xsh,ysh
    else: xsh,ysh = 0,0
    if vary:
        setaperture(image,refstar)
    shift_file_coords(coords,xsh,ysh,'tempcoords2',sort='als')
    iraf.phot(image,'tempcoords2','temp.mag2',inter="no",calgorithm='none',
                  mode='h',verify='no',update='no',verbose='no')
    iraf.allstar(image,'temp.mag2',psf,'temp.als','temp.mag.arj','temp.sub.fits',
                 mode='h',verify='no',update='no',verbose='no')
    out = iraf.pdump('temp.als','id,mag,merr,msky','yes',Stdout=1)
    return out   
Example #6
0
def apphot(image,coords,refstar=None,centre=False,vary=False):
    """Apperture photometry with centering based on a reference star.
NB: centre refers to shifting the coordinates by centroiding on the
reference star; recentering on the final phot depends on
centerpars.calgorithm ."""
    iraf.dele('temp.mag*')
    if centre:
        xsh,ysh = recentre(image,refstar)
        print "Fine centring: ", xsh,ysh
    else: #no recentreing by reference star (but could still have calgorithm!=none)
        xsh,ysh = 0,0
    if vary:
        setaperture(image,refstar)
    shift_file_coords(coords,xsh,ysh,'tempcoords')
    iraf.phot(image,'tempcoords','temp.mag2',inter="no",
                  mode='h',verify='no',update='no',verbose='no')
    out = iraf.pdump('temp.mag2','id,flux,msky,stdev','yes',Stdout=1)
    return out
Example #7
0
    def daocog(self, tolerance = 0.01):
        """
        Curve of growth to determine nominal aperture for photometry using DAOPHOT.

        Parameters
        ----------
        tolerance : float
            Magnitude difference tolerance between different apertures

        Returns
        -------
        aperture : float
            Nominal aperture radius for photmetry
        """
        # load iraf packages
        self.setiraf()

        # Randomly peform curve of growth on 5 frames
        framenum = np.random.randint(1, self.nframes, 5)

        apertures = np.linspace(2,20,19)

        # Iterate through the frames and determine nominal aperture
        nom_aper = np.zeros(5, dtype = np.float32)
        cnt = 0
        for val in framenum:
            outfile = self.sci_file.replace(".fits", "." + str(val) + ".cog.phot.1")
            iraf.delete(outfile)
            self.daophot(val, self.coords, outfile, apertures = "2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20")
            mags = iraf.pdump(outfile, "mag", "yes", Stdout = 1)
            mags = [mag if mag != 'INDEF' else 30.0 for mag in mags]
            mags_arr = np.array(mags[1].split(),dtype = np.float32)
            mags_diff = np.diff(mags_arr)
            idx = np.where((np.abs(mags_diff) < tolerance) & (np.abs(mags_diff) != 0.0))
            if len(idx[0]) != 0:
                nom_aper[cnt] = apertures[idx[0][0]]
            else:
                nom_aper[cnt] = 10.0
            cnt += 1
            iraf.delete(outfile)

        return np.median(nom_aper)
Example #8
0
 def daocog(self, tolerance = 0.01):
     """
     Curve of growth to determine nominal aperture for photometry using DAOPHOT.
     
     Parameters
     ----------
     tolerance : float
         Magnitude difference tolerance between different apertures
     
     Returns
     -------
     aperture : float
         Nominal aperture radius for photmetry
     """
     # load iraf packages
     self.setiraf()
     
     # Randomly peform curve of growth on 5 frames
     framenum = np.random.randint(1, self.nframes, 5)
     
     apertures = np.linspace(2,20,19)
     
     # Iterate through the frames and determine nominal aperture
     nom_aper = np.zeros(5, dtype = np.float32)
     cnt = 0
     for val in framenum:
         outfile = self.sci_file.replace(".fits", "." + str(val) + ".cog.phot.1")
         iraf.delete(outfile)
         self.daophot(val, self.coords, outfile, apertures = "2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20")
         mags = iraf.pdump(outfile, "mag", "yes", Stdout = 1)
         mags = [mag if mag != 'INDEF' else 30.0 for mag in mags]
         mags_arr = np.array(mags[1].split(),dtype = np.float32)
         mags_diff = np.diff(mags_arr)
         idx = np.where((np.abs(mags_diff) < 0.01) & (np.abs(mags_diff) != 0.0))
         if len(idx[0]) != 0:
             nom_aper[cnt] = apertures[idx[0][0]]
         else:
             nom_aper[cnt] = 10.0
         cnt += 1
         iraf.delete(outfile)
         
     return np.median(nom_aper)
Example #9
0
def dump(infile, keywords):
    """
    Dump keyword data from DAOPHOT output photometry file.
        
    Parameters
    ----------
    keywords : string
        Comma separated fields that have to be extracted from phot file
            
    Returns
    -------
    indata : numpy array
        Photometry data array    
    """
    # Load iraf packages
    iraf.noao(_doprint=0)
    iraf.noao.digiphot(_doprint=0)
    iraf.noao.digiphot.ptools(_doprint=0)

    indata = iraf.pdump(infile, keywords, "yes", Stdout=1)

    return indata[1]
Example #10
0
def dump(infile, keywords):
    """
    Dump keyword data from DAOPHOT output photometry file.
        
    Parameters
    ----------
    keywords : string
        Comma separated fields that have to be extracted from phot file
            
    Returns
    -------
    indata : numpy array
        Photometry data array    
    """
    # Load iraf packages
    iraf.noao(_doprint=0)
    iraf.noao.digiphot(_doprint=0)
    iraf.noao.digiphot.ptools(_doprint=0)

    indata = iraf.pdump(infile, keywords, "yes", Stdout=1)

    return indata[1]
Example #11
0
def psfphot(image,coords,pststars,refstar,centre=True,vary=False):
    """PSF photometry. Centering is through phot on refstar.
Assume coords is a .als file for now. Recentering is always done
for the reference star, never for the targets."""
    iraf.dele('temp.mag*')
    iraf.dele('temp.psf.fits')
    iraf.dele('temp.als')
    if centre:
        xsh,ysh = recentre(image,refstar) 
        print "Fine Centring: ", xsh,ysh
    else: xsh,ysh = 0,0
    if vary:
        setaperture(image,refstar)
    shift_file_coords(coords,xsh,ysh,'tempcoords2',sort='als')
    shift_file_coords(pststars,xsh,ysh,'temppst2',sort='pst')
    iraf.phot(image,'tempcoords2','temp.mag2',inter="no",calgorithm='none',
                  mode='h',verify='no',update='no',verbose='no')
    iraf.psf(image,'temp.mag2','temppst2','temp.psf','temp.mag.pst','temp.mag.psg',
             inter='no',mode='h',verify='no',update='no',verbose='no')
    iraf.allstar(image,'temp.mag2','temp.psf','temp.als','temp.mag.arj',"default",
                 mode='h',verify='no',update='no',verbose='no')
    out = iraf.pdump('temp.als','id,mag,merr,msky','yes',Stdout=1)
    return out   
Example #12
0
def findsolution(image,alsfile,datafile,source="GSC"):
    stars = CMD.star_photometry(alsfile,image,False)
    datfile = pyfits.open(datafile)
    mydata=datfile[-1].data
    datfile.close()
    iraf.display(image,1)
    temp=iraf.pdump(alsfile,'xcen,ycen,id','yes',Stdout=1)
    iraf.tvmark(1,"STDIN",Stdin=temp)
    rafield,decfield = fields[source]
    ra = mydata.field(rafield)
    dec= mydata.field(decfield)
    raint = int(min(ra))
    decint= int(min(dec))
    print "ra offset by %i, dec by %i\n"%(raint,decint)
    clf()
    plot(ra-raint,dec-decint,'k+')
    print "Require 4 stars matched to make simple solution"
    print "Enter co-ordinates as acurately as you can:-"
    fred = open('ccmapfile','w')
    for i in range(4):
        idin=input("Star %i ID:  "%i)
        mystar=stars[idin]
        rain=input("Star %i RA:  " %i) + raint
        decin=input("Star %i DEC: "%i) + decint
        radiff = ra-rain
        decdiff = dec-decin
        radecdiff = radiff**2+decdiff**2
        locmin = radecdiff.argmin()
        raout = ra[locmin]
        decout = dec[locmin]
        fred.write("%7.2f %7.2f %10.6f %10.6f\n"%(mystar[1],mystar[2],raout,decout))
    fred.close()
    iraf.ccmap('ccmapfile','database',images=image,xcolumn=1,ycolumn=2,lngcolumn=3,
               latcolumn=4,lngunits='degrees',latunits='degrees',insystem='j2000',
               fitgeometry='rxyscale')
    findbettersolution(image,alsfile,datafile,source=source)
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
Example #14
0
File: center.py Project: OSSOS/MOP
if os.access('zeropoint.used',os.R_OK):
	f=file('zeropoint.used')
	zmag=float(f.read())

if os.access(opt.output,os.R_OK):
    os.unlink(opt.output)

iraf.photpars.apertures=int(opt.aperture)
iraf.photpars.zmag=zmag
iraf.datapars.datamin=0
iraf.datapars.datamax=maxlin
iraf.datapars.exposur="EXPTIME"
iraf.fitskypars.annulus=int(opt.aperture)+5
iraf.fitskypars.dannulus=5
iraf.centerpars.calgori="centroid"
iraf.centerpars.cbox=5.
iraf.centerpars.cthreshold=0.
iraf.centerpars.maxshift=2.
iraf.centerpars.clean='no'
iraf.phot.update='no'
iraf.phot.verbose='no'
iraf.phot.verify='no'
iraf.phot.interactive='no'
import tempfile
magfile=tempfile.mktemp(suffix='mag')
print(magfile)
iraf.phot(opt.image, opt.input, magfile)
iraf.pdump(magfile,"XCENTER,YCENTER,MAG,MERR,ID","MERR < 0.1 && MAG != INDEF && PIER==0", header='no', parameters='yes',  Stdout=opt.output)

os.unlink(magfile)
Example #15
0
def phot(fits_filename, x_in, y_in, aperture=15, sky=20, swidth=10, apcor=0.3,
         maxcount=30000.0, exptime=1.0):
    """
    Compute the centroids and magnitudes of a bunch sources detected on
    CFHT-MEGAPRIME images.

    Args:
      fits_filename: str
        The name of the file containing the image to be processed.

    Returns a MOPfiles data structure.
    """

    if (not os.path.exists(fits_filename) and
            not fits_filename.endswith(".fits")):
        # For convenience, see if we just forgot to provide the extension
        fits_filename += ".fits"

    try:
        input_hdulist = fits.open(fits_filename)
    except Exception as err:
        raise TaskError("Failed to open input image: %s" % err.message)

    ## get the filter for this image
    filter = input_hdulist[0].header.get('FILTER', 'DEFAULT')

    ### Some CFHT zeropoints that might be useful
    zeropoints = {"I": 25.77,
                  "R": 26.07,
                  "V": 26.07,
                  "B": 25.92,
                  "DEFAULT": 26.0,
                  "g.MP9401": 26.4
    }

    ### load the
    if not filter in zeropoints:
        filter = "DEFAULT"

    zmag = input_hdulist[0].header.get('PHOTZP', zeropoints[filter])

    ### setup IRAF to do the magnitude/centroid measurements
    iraf.set(uparm="./")
    iraf.digiphot()
    iraf.apphot()
    iraf.daophot(_doprint=0)

    ### check for the magical 'zeropoint.used' file
    zpu_file = "zeropoint.used"
    if os.access(zpu_file, os.R_OK):
        with open(zpu_file) as zpu_fh:
            zmag = float(zpu_fh.read())

    iraf.photpars.apertures = int(aperture)
    iraf.photpars.zmag = zmag
    iraf.datapars.datamin = 0
    iraf.datapars.datamax = maxcount
    #iraf.datapars.exposur="EXPTIME"
    iraf.datapars.exposur = ""
    iraf.datapars.itime = exptime
    iraf.fitskypars.annulus = sky
    iraf.fitskypars.dannulus = swidth
    iraf.centerpars.calgori = "centroid"
    iraf.centerpars.cbox = 5.
    iraf.centerpars.cthreshold = 0.
    iraf.centerpars.maxshift = 2.
    iraf.centerpars.clean = 'no'
    iraf.phot.update = 'no'
    iraf.phot.verbose = 'no'
    iraf.phot.verify = 'no'
    iraf.phot.interactive = 'no'

    # Used for passing the input coordinates
    coofile = tempfile.NamedTemporaryFile(suffix=".coo", delete=False)
    coofile.write("%f %f \n" % (x_in, y_in))

    # Used for receiving the results of the task
    # mag_fd, mag_path = tempfile.mkstemp(suffix=".mag")
    magfile = tempfile.NamedTemporaryFile(suffix=".mag", delete=False)

    # Close the temp files before sending to IRAF due to docstring:
    # "Whether the name can be used to open the file a second time, while
    # the named temporary file is still open, varies across platforms"
    coofile.close()
    magfile.close()
    os.remove(magfile.name)

    iraf.phot(fits_filename, coofile.name, magfile.name)
    pdump_out = iraf.pdump(magfile.name, "XCENTER,YCENTER,MAG,MERR,ID,XSHIFT,YSHIFT,LID",
                           "MERR < 0.4 && MERR != INDEF && MAG != INDEF && PIER==0", header='no', parameters='yes',
                           Stdout=1)

    os.remove(coofile.name)
    os.remove(magfile.name)

    ### setup the mop output file structure
    hdu = {}
    hdu['header'] = {'image': input_hdulist,
                     'aper': aperture,
                     's_aper': sky,
                     'd_s_aper': swidth,
                     'aper_cor': apcor,
                     'zeropoint': zmag}
    hdu['order'] = ['X', 'Y', 'MAG', 'MERR', 'ID', 'XSHIFT', 'YSHIFT', 'LID']
    hdu['format'] = {'X': '%10.2f',
                     'Y': '%10.2f',
                     'MAG': '%10.2f',
                     'MERR': '%10.2f',
                     'ID': '%8d',
                     'XSHIFT': '%10.2f',
                     'YSHIFT': '%10.2f',
                     'LID': '%8d'}
    hdu['data'] = {}
    for col in hdu['order']:
        hdu['data'][col] = []

    for line in pdump_out:
        values = line.split()
        for col in hdu['order']:
            if re.match('\%.*f', hdu['format'][col]):
                if col == 'MAG':
                    values[0] = float(values[0]) - float(apcor)
                hdu['data'][col].append(float(values.pop(0)))
            elif re.match('\%.*d', hdu['format'][col]):
                hdu['data'][col].append(int(values.pop(0)))
            else:
                hdu['data'][col].append(values.pop(0))

    # Clean up temporary files generated by IRAF
    os.remove("datistabe.par")
    os.remove("datpdump.par")

    return hdu
Example #16
0
def skySub(imageName):
    #Set up iraf packages and variables for filenames
    skyIm = imageName + '.sky'
    outfile = 'mSky' + imageName + '.file'
    avgValLog = 'mSkyLog' + imageName + '.file'
    stDevLog = 'stDevLog' + imageName + '.file'
    outputIm = imageName + '.sky'
    iraf.apphot(_doprint=0)

    #Delete exisiting files
    for f in (skyIm, outfile, 'mSky.file', outputIm + '.fits', avgValLog):
        silentDelete(f)

    #Configure fitsky parameters
    iraf.fitsky.coords = "coordslist"
    iraf.fitsky.interactive = 'no'
    iraf.fitsky.verify = 'no'
    iraf.datapars.datamax = 150000
    iraf.datapars.datamin = -200
    iraf.fitskypars.salgorithm = "mode"
    iraf.fitskypars.annulus = 0
    iraf.fitskypars.dannulus = 40
    iraf.fitskypars.shireject = 2
    iraf.fitskypars.sloreject = 2
    iraf.fitsky.verbose = 'no'

    #Call fitsky to compute the mean of the sky
    iraf.fitsky(imageName, output=outfile)

    #Use 'PDUMP" to extract only the mean sky values
    sys.stdout = open(avgValLog, "w")
    iraf.pdump(outfile, 'mSky', "yes")
    sys.stdout = sys.__stdout__

    #Use pdump to get the standard deviation from the sky
    sys.stdout = open(stDevLog, "w")
    iraf.pdump(outfile, 'stDev', "yes")
    sys.stdout = sys.__stdout__

    #Read in the mSky values file
    skyValues = open(avgValLog, 'r')
    valueList = skyValues.readlines()

    #convert the value array to a numpy array
    valueListNp = np.asfarray(valueList)

    #compute average
    average = np.mean(valueListNp)

    #Print output
    print(str(average) + " is the average")

    #Read in the stDev log, take the average
    stDevs = open(stDevLog, 'r')
    stDevList = stDevs.readlines()
    stDevListNp = np.asfarray(stDevList)
    stDevAvg = np.mean(stDevListNp)

    #Configure imarith
    iraf.imarith.pixtype = "real"
    iraf.imarith.calctype = "real"

    #Use imarith to subtract the average sky value from the image
    iraf.imarith(imageName, "-", average, outputIm)

    #Add the average sky value and stDev to the image header
    iraf.hedit(outputIm, "SKY", average, add='yes', verify='no')
    iraf.hedit(outputIm, "SKYSIGMA", stDevAvg, add='yes', verify='no')

    #Print an output image
    print("Subtracted " + str(average) + " to create a new image called " +
          outputIm)

    #Delete acillary output files
    silentDelete(outfile)
Example #17
0
    os.unlink(opt.output)

iraf.photpars.apertures = int(opt.aperture)
iraf.photpars.zmag = zmag
iraf.datapars.datamin = 0
iraf.datapars.datamax = maxlin
iraf.datapars.exposur = "EXPTIME"
iraf.fitskypars.annulus = int(opt.aperture) + 5
iraf.fitskypars.dannulus = 5
iraf.centerpars.calgori = "centroid"
iraf.centerpars.cbox = 5.
iraf.centerpars.cthreshold = 0.
iraf.centerpars.maxshift = 2.
iraf.centerpars.clean = 'no'
iraf.phot.update = 'no'
iraf.phot.verbose = 'no'
iraf.phot.verify = 'no'
iraf.phot.interactive = 'no'
import tempfile
magfile = tempfile.mktemp(suffix='mag')
print(magfile)
iraf.phot(opt.image, opt.input, magfile)
iraf.pdump(magfile,
           "XCENTER,YCENTER,MAG,MERR,ID",
           "MERR < 0.1 && MAG != INDEF && PIER==0",
           header='no',
           parameters='yes',
           Stdout=opt.output)

os.unlink(magfile)
Example #18
0
def Photometry(path):
    print "Starting Photometry..."
    print "This can take a long time. Go drink some coffee!"
    filename = path #raw_input("Path to the directory of the images: ") 
    os.chdir(filename)   
    
    iraf.noao(_doprint=0)
    iraf.digiphot(_doprint=0)
    iraf.apphot(_doprint=0)

    #Parameter settings
    iraf.centerpars.setParam('calgorithm', 'centroid', check=0, exact=0)
    iraf.centerpars.setParam('cbox', '10.0', check=0, exact=0) 
    iraf.centerpars.saveParList(filename="center.par") 
    iraf.centerpars.setParList(ParList="center.par")

    iraf.fitskypars.setParam('salgorithm', 'mode', check=0, exact=0)
    iraf.fitskypars.setParam('annulus', '7.0') 
    iraf.fitskypars.setParam('dannulus', '2.0', check=0, exact=0)
    iraf.fitskypars.saveParList(filename='fits.par')
    iraf.fitskypars.setParList(ParList='fits.par')

    iraf.photpars.setParam('apertures', '4.5', check=0, exact=0) 
    iraf.photpars.saveParList(filename='phot.par')
    iraf.photpars.setParList(ParList='phot.par')

    iraf.datapars.setParam('fwhm', 'INDEF', check=0, exact=0)
    iraf.datapars.setParam('sigma', 'INDEF', check=0, exact=0)
    iraf.datapars.setParam('datamin', 'INDEF', check=0, exact=0)
    iraf.datapars.setParam('datamax','INDEF', check=0, exact=0)
    iraf.datapars.setParam('ccdread','RESPONSE', check=0, exact=0)
    iraf.datapars.setParam('gain','EGAIN', check=0, exact=0)
    iraf.datapars.setParam('exposur','EXPTIME', check=0, exact=0)
    iraf.datapars.setParam('airmass','AIRMASS', check=0, exact=0)
    #iraf.datapars.setParam('xairmas','1.', check=0, exact=0)
    iraf.datapars.setParam('obstime','UTSHUT', check=0, exact=0)
    iraf.datapars.setParam('filter','FILTER', check=0, exact=0)
    iraf.datapars.saveParList(filename='data.par')
    iraf.datapars.setParList(ParList='data.par')

    #Takes the necessary parameters
    iraf.centerpars.getParam('calgorithm', native=1, mode="h", exact=0, prompt=0)
    iraf.centerpars.getParam('cbox', native=1, exact=0, prompt=0)
    
    iraf.fitskypars.getParam('salgorithm', native=1, mode="h", exact=0, prompt=0)
    iraf.fitskypars.getParam('annulus', native=1, exact=0, prompt=0)
    iraf.fitskypars.getParam('dannulus', native=1, exact=0, prompt=0)

    iraf.photpars.getParam('apertures', native=1, mode="h", exact=0, prompt=0)

    iraf.datapars.getParam('fwhm', native=0, mode='h', exact=0, prompt=0)
    iraf.datapars.getParam('sigma', native=0, mode="h", exact=0, prompt=0)
    iraf.datapars.getParam('datamin', native=0, mode="h", exact=0, prompt=0)
    iraf.datapars.getParam('datamax', native=0, mode="h", exact=0, prompt=0)
    iraf.datapars.getParam('ccdread', native=0, mode="h", exact=0, prompt=0)
    iraf.datapars.getParam('gain', native=0, mode="h", exact=0, prompt=0)
    iraf.datapars.getParam('exposur', native=0, mode="h", exact=0, prompt=0)
    iraf.datapars.getParam('airmass', native=0, mode="h", exact=0, prompt=0)
    iraf.datapars.getParam('xairmas', native=0, mode="h", exact=0, prompt=0)
    iraf.datapars.getParam('obstime', native=0, mode="h", exact=0, prompt=0)
    iraf.datapars.getParam('filter', native=0, mode="h", exact=0, prompt=0)
                
    
    #Removing existing files
    filelist1 = glob.glob("*.mag")
    filelist2 = glob.glob("*.magnitude")

    for f in filelist1:
        os.remove(f)
    for g in filelist2:
        os.remove(g)
        
    #Photometry
    print "Extracting magnitude and magnitude error from the files created by Pyraf"
    images = glob.glob("*.fits")
    for i in images:
        tr_coord = i + '.out.xy.done'
        iraf.phot(image = i, output = i + '.mag', coords = tr_coord, interactive = 'no' , wcsin = 'tv', skyfile = '', verify='no', verbose='no' )


    #uses IRAF's pdump to take MAG values from the PHOT's output file
    photometry = glob.glob("*.mag")                  
    for j in photometry:
        iraf.pdump(j, 'MAG, MERR', 'yes', Stdout = j + 'nitude')
Example #19
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)
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)
Example #21
0
def continuumReduce(imageName):
  
  #Read the fwhm from the Ha image file
  imageHeader = fits.open(imageName+".fits")[0]
  fwhm = imageHeader.header['SEEING']
  annul = 5.0*fwhm
  aper=3.0*fwhm
  
  #Configure 'datapars', 'findpars', 'phot' and 'daofind'
  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'
  iraf.phot.verbose='no'
  
 #Delete old photometry and old Ha images
  for f in ('mag_Ha','psel_Ha',"Hacs_final.fits","Hacs_finalplus.fits","Hacs_finalminus.fits",imageName+"_scaledminus.fits",imageName+"_scaledplus.fits",imageName+"_scaled.fits"):
    silentDelete(f)
  
  #Call 'phot' to get the data of the stars from the aligned Ha image
  iraf.phot("Ha_final.fits",'pdump.file','mag_Ha')

  #Pick out only the required data of stars from the .25 file
  sys.stdout=open("pdump_Ha.file","w")
  iraf.pdump ("mag_Ha","xcenter,ycenter,flux","yes")
  sys.stdout = sys.__stdout__
  
  print("The Ha coordinates and flux are stored in pdump_Ha.file")
  
  #Read the data from the pdump files
  r_Data = open('pdump.file')
  r_DataList = r_Data.readlines()
  r_Data.close()
  Ha_Data = open('pdump_Ha.file')
  Ha_DataList = Ha_Data.readlines()
  Ha_Data.close()
  
  #Compute the average scale factor between the R and Ha images
  scaleFactors=[]
  print('Scale factors: ')
  for index in range(0,len(r_DataList)):
    rLine = r_DataList[index].split('  ')
    rFlux = float(rLine[2])
    haLine = Ha_DataList[index].split('  ')
    haFlux = float(haLine[2])
    scaleFactor = haFlux/rFlux
    print(str(scaleFactor))
    scaleFactors.append(scaleFactor)
  
  print(" ")
  scaleFactors=np.array(scaleFactors)
  
  #Compute the avg scale factor and standard deviation and ask if it is acceptable, changing the value if it is not.
  avgScaleFactor = np.mean(scaleFactors)
  stDev = np.std(scaleFactors)
  print("The scale factor is "+str(avgScaleFactor))
  decision = raw_input('Is this acceptable (y/n)? ')
  avgChanged=False
  if (not((decision=='yes') or (decision=='y'))):
    avgScaleFactor=float(raw_input('Enter a new scalefactor: '))
    avgChanged=True
    print("Average scale factor is now: "+str(avgScaleFactor))
  
  #configure hedit
  iraf.hedit.add='yes'
  iraf.hedit.verify='no'
  
  if (avgChanged):
    #No adjustment
    iraf.imarith('R_final',"*",avgScaleFactor,imageName+"_scaled")
    iraf.imarith('Ha_final',"-",imageName+"_scaled","Hacs_final")
    iraf.hedit("Hacs_final",'Rscale',avgScaleFactor)
    #plus .01
    iraf.imarith('R_final',"*",avgScaleFactor+.01,imageName+"_scaledplus")
    iraf.imarith('Ha_final',"-",imageName+"_scaledplus","Hacs_finalplus")
    iraf.hedit("Hacs_finalplus",'Rscale',avgScaleFactor+.01)
    #minus .01
    iraf.imarith('R_final',"*",avgScaleFactor-.01,imageName+"_scaledminus")
    iraf.imarith('Ha_final',"-",imageName+"_scaledminus","Hacs_finalminus")
    iraf.hedit("Hacs_finalminus",'Rscale',avgScaleFactor-.01)
  else:
    #No stdev adjustment
    iraf.imarith('R_final',"*",avgScaleFactor,imageName+"_scaled")
    iraf.imarith('Ha_final',"-",imageName+"_scaled","Hacs_final")
    iraf.hedit("Hacs_final",'Rscale',avgScaleFactor)
    #plus one stdev
    iraf.imarith('R_final',"*",avgScaleFactor+stDev,imageName+"_scaledplus")
    iraf.imarith('Ha_final',"-",imageName+"_scaledplus","Hacs_finalplus")
    iraf.hedit("Hacs_finalplus",'Rscale',avgScaleFactor+stDev)
    #minus one stdev
    iraf.imarith('R_final',"*",avgScaleFactor-stDev,imageName+"_scaledminus")
    iraf.imarith('Ha_final',"-",imageName+"_scaledminus","Hacs_finalminus")
    iraf.hedit("Hacs_finalminus",'Rscale',avgScaleFactor-stDev)
  
  #clear up superfluous images
  for f in (imageName+"_scaledminus.fits",imageName+"_scaledplus.fits",imageName+"_scaled.fits",'rIn.fits','haIn.fits'):
    silentDelete(f)

  print('The continuum subtracted images are Hacs_final.fits, Hacs_finalminus.fits, and Hacs_finalplus.fits')
Example #22
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")
def continuumReduce(imageName):

    #Read the fwhm from the Ha image file
    imageHeader = fits.open(imageName + ".fits")[0]
    fwhm = imageHeader.header['SEEING']
    annul = 5.0 * fwhm
    aper = 3.0 * fwhm

    #Configure 'datapars', 'findpars', 'phot' and 'daofind'
    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'
    iraf.phot.verbose = 'no'

    #Delete old photometry and old Ha images
    for f in ('mag_Ha', 'psel_Ha', "Hacs_final.fits", "Hacs_finalplus.fits",
              "Hacs_finalminus.fits", imageName + "_scaledminus.fits",
              imageName + "_scaledplus.fits", imageName + "_scaled.fits"):
        silentDelete(f)

    #Call 'phot' to get the data of the stars from the aligned Ha image
    iraf.phot("Ha_final.fits", 'pdump.file', 'mag_Ha')

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

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

    #Read the data from the pdump files
    r_Data = open('pdump.file')
    r_DataList = r_Data.readlines()
    r_Data.close()
    Ha_Data = open('pdump_Ha.file')
    Ha_DataList = Ha_Data.readlines()
    Ha_Data.close()

    #Compute the average scale factor between the R and Ha images
    scaleFactors = []
    print('Scale factors: ')
    for index in range(0, len(r_DataList)):
        rLine = r_DataList[index].split('  ')
        rFlux = float(rLine[2])
        haLine = Ha_DataList[index].split('  ')
        haFlux = float(haLine[2])
        scaleFactor = haFlux / rFlux
        print(str(scaleFactor))
        scaleFactors.append(scaleFactor)

    print(" ")
    scaleFactors = np.array(scaleFactors)

    #Compute the avg scale factor and standard deviation and ask if it is acceptable, changing the value if it is not.
    avgScaleFactor = np.mean(scaleFactors)
    stDev = np.std(scaleFactors)
    print("The scale factor is " + str(avgScaleFactor))
    decision = raw_input('Is this acceptable (y/n)? ')
    avgChanged = False
    if (not ((decision == 'yes') or (decision == 'y'))):
        avgScaleFactor = float(raw_input('Enter a new scalefactor: '))
        avgChanged = True
        print("Average scale factor is now: " + str(avgScaleFactor))

    #configure hedit
    iraf.hedit.add = 'yes'
    iraf.hedit.verify = 'no'

    if (avgChanged):
        #No adjustment
        iraf.imarith('R_final', "*", avgScaleFactor, imageName + "_scaled")
        iraf.imarith('Ha_final', "-", imageName + "_scaled", "Hacs_final")
        iraf.hedit("Hacs_final", 'Rscale', avgScaleFactor)
        #plus .01
        iraf.imarith('R_final', "*", avgScaleFactor + .01,
                     imageName + "_scaledplus")
        iraf.imarith('Ha_final', "-", imageName + "_scaledplus",
                     "Hacs_finalplus")
        iraf.hedit("Hacs_finalplus", 'Rscale', avgScaleFactor + .01)
        #minus .01
        iraf.imarith('R_final', "*", avgScaleFactor - .01,
                     imageName + "_scaledminus")
        iraf.imarith('Ha_final', "-", imageName + "_scaledminus",
                     "Hacs_finalminus")
        iraf.hedit("Hacs_finalminus", 'Rscale', avgScaleFactor - .01)
    else:
        #No stdev adjustment
        iraf.imarith('R_final', "*", avgScaleFactor, imageName + "_scaled")
        iraf.imarith('Ha_final', "-", imageName + "_scaled", "Hacs_final")
        iraf.hedit("Hacs_final", 'Rscale', avgScaleFactor)
        #plus one stdev
        iraf.imarith('R_final', "*", avgScaleFactor + stDev,
                     imageName + "_scaledplus")
        iraf.imarith('Ha_final', "-", imageName + "_scaledplus",
                     "Hacs_finalplus")
        iraf.hedit("Hacs_finalplus", 'Rscale', avgScaleFactor + stDev)
        #minus one stdev
        iraf.imarith('R_final', "*", avgScaleFactor - stDev,
                     imageName + "_scaledminus")
        iraf.imarith('Ha_final', "-", imageName + "_scaledminus",
                     "Hacs_finalminus")
        iraf.hedit("Hacs_finalminus", 'Rscale', avgScaleFactor - stDev)

    #clear up superfluous images
    for f in (imageName + "_scaledminus.fits", imageName + "_scaledplus.fits",
              imageName + "_scaled.fits", 'rIn.fits', 'haIn.fits'):
        silentDelete(f)

    print(
        'The continuum subtracted images are Hacs_final.fits, Hacs_finalminus.fits, and Hacs_finalplus.fits'
    )
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")
Example #25
0
def skySub(imageName):
  #Set up iraf packages and variables for filenames
  skyIm = imageName+'.sky'
  outfile = 'mSky'+imageName+'.file'
  avgValLog = 'mSkyLog'+imageName+'.file'
  stDevLog = 'stDevLog'+imageName+'.file'
  outputIm = imageName+'.sky'
  iraf.apphot(_doprint=0)
  
  #Delete exisiting files 
  for f in (skyIm,outfile,'mSky.file',outputIm+'.fits',avgValLog):
    silentDelete(f)
  
  #Configure fitsky parameters
  iraf.fitsky.coords="coordslist"
  iraf.fitsky.interactive='no'
  iraf.fitsky.verify='no'
  iraf.datapars.datamax=150000
  iraf.datapars.datamin=-200
  iraf.fitskypars.salgorithm="mode"
  iraf.fitskypars.annulus=0
  iraf.fitskypars.dannulus=40
  iraf.fitskypars.shireject=2
  iraf.fitskypars.sloreject=2
  iraf.fitsky.verbose='no'
  
  #Call fitsky to compute the mean of the sky
  iraf.fitsky(imageName,output=outfile)
  
  #Use 'PDUMP" to extract only the mean sky values
  sys.stdout=open(avgValLog,"w")
  iraf.pdump(outfile,'mSky',"yes")
  sys.stdout=sys.__stdout__

  #Use pdump to get the standard deviation from the sky
  sys.stdout=open(stDevLog,"w")
  iraf.pdump(outfile,'stDev',"yes")
  sys.stdout=sys.__stdout__

  #Read in the mSky values file
  skyValues = open(avgValLog,'r')
  valueList = skyValues.readlines()
  
  #convert the value array to a numpy array
  valueListNp = np.asfarray(valueList)

  #compute average
  average = np.mean(valueListNp)
  
  #Print output 
  print(str(average)+" is the average")
  
  #Read in the stDev log, take the average
  stDevs = open(stDevLog,'r')
  stDevList = stDevs.readlines()
  stDevListNp = np.asfarray(stDevList)
  stDevAvg = np.mean(stDevListNp)
  
  #Configure imarith
  iraf.imarith.pixtype="real"
  iraf.imarith.calctype="real"
  
  #Use imarith to subtract the average sky value from the image
  iraf.imarith(imageName,"-",average,outputIm)
  
  #Add the average sky value and stDev to the image header
  iraf.hedit(outputIm,"SKY",average,add='yes',verify='no')
  iraf.hedit(outputIm,"SKYSIGMA",stDevAvg,add='yes',verify='no')
  
  #Print an output image
  print("Subtracted "+ str(average)+ " to create a new image called "+outputIm)
  
  #Delete acillary output files
  silentDelete(outfile)