Exemple #1
0
def daofind(filelist):
	iraf.datapars.setParam("exposure", "EXPTIME")
	iraf.datapars.setParam("airmass", "AIRMASS")
	iraf.datapars.setParam("filter", "INSFILTE")
	iraf.datapars.setParam("ccdread", "RDNOISE")
	iraf.datapars.setParam("gain", "GAIN")

	iraf.centerpars.setParam("calgorithm", "centroid")

	iraf.photpars.setParam("apertures", "17.56")
	
	iraf.fitskypars.setParam("annulus", "20.56")
	iraf.fitskypars.setParam("dannulus", "5")
	
	

	with open(filelist) as file1:
		for line in file1:
			filename = line.strip()
			print("Filename %s"  % filename)
			y,z = np.loadtxt(filename + "-daoedit", usecols=[3,4], unpack=True)
			sigma = np.mean(y) 
			fwhm = np.mean(z)
			iraf.datapars.setParam("fwhmpsf", "%s" % str(fwhm)  )
			iraf.datapars.setParam("sigma", "%s" % str(sigma)  )
			iraf.daofind.setParam("mode", "h")
			iraf.phot.setParam("mode", "h")
			iraf.datapars.setParam("mode", "h")
			iraf.centerpars.setParam("mode", "h")
			iraf.fitskypars.setParam("mode", "h")
			iraf.photpars.setParam("mode", "h")
	
			iraf.daofind(filename, filename + "-daofind", mode='h')
			
			iraf.phot(filename, coords=(filename + "-daofind"), output=(filename+".mag"), mode='h')
Exemple #2
0
def daophot(inp, fwhm):
    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)

    iraf.datapars.fwhmpsf = float(fwhm)
    iraf.datapars.datamin = 10
    iraf.datapars.datamax = 60000
    iraf.datapars.sigma = 50
    iraf.datapars.readnoise = 2.3
    iraf.datapars.epadu = 0.8

    iraf.centerpars.calgorithm = 'centroid'
    iraf.centerpars.cbox = 6

    iraf.fitskypars.annulus = 20
    iraf.fitskypars.dannulus = 10

    app = str(fwhm) + ":" + str(4 * float(fwhm)) + ":" + "1"
    iraf.photpars.apertures = app
    iraf.photpars.zmag = 25

    iraf.phot(image=inp,
              output='default',
              verify='No',
              verbose='No',
              coords='default')
Exemple #3
0
def run_phot(imageRoot,
             silent=False,
             apertures=[25, 50, 75, 100, 125, 150, 175, 200],
             sky_annulus=200,
             sky_dannulus=50,
             zmag=0):

    setup_phot(imageRoot,
               apertures=apertures,
               zmag=zmag,
               silent=silent,
               sky_annulus=sky_annulus,
               sky_dannulus=sky_dannulus)

    image = imageRoot + '.fits'
    coords = imageRoot + '.coo'

    # Output into current directory, not data directory
    rootSplit = imageRoot.split('/')
    output = rootSplit[-1] + '.phot.mag'

    ir.phot(image, coords, output)

    (radius, flux, mag, merr) = get_phot_output(output, silent=silent)

    return (radius, flux, mag, merr)
Exemple #4
0
def do_photometry(filename, science_image, aperture_rad=20., use_iraf=True):
    """Do aperture photometry on the difference image"""

    image_data = fits.getdata(filename)
    image_header = fits.getheader(science_image)
    sexa_ra, sexa_dec = image_header['RA'], image_header['DEC']
    ra, dec = sexa2deg(sexa_ra, sexa_dec)
    x, y = degs2coords(ra, dec, image_header)

    if use_iraf:
        coords = open('coords.txt', 'w')
        coords.write('{0} {1}'.format(x, y))
        coords.close()
        iraf.datapars.sigma = 20.
        iraf.datapars.fwhmpsf = 7.
        iraf.photpars.apertures = 15.
        outname = filename.replace('.fits', '.mag')
        iraf.phot(filename,
                  coords='coords.txt',
                  output=outname,
                  verify='no',
                  interactive='no')
        os.system('rm coords.txt')
        mag = open(outname, 'r')
        flux = float(mag.readlines()[-1].split()[3])

    else:
        x, y = [round(float(coord)) for coord in (x, y)]
        xind, yind = np.indices(image_data.shape)
        phot_pix = np.hypot(x - xind, y - yind) <= aperture_rad
        flux = np.sum(image_data[phot_pix])
    return flux
def getSkyMeanSDinAnnulus(ann,delta=5):
	iraf.noao()
	iraf.digiphot()
	iraf.apphot()
	iraf.photpars.setParam('apertures','1')
	iraf.phot.setParam('interactive','no')
	iraf.phot.setParam('image',fitsDir+fitsFile)
	iraf.phot.setParam('coords',fitsDir+fitsFile+".coo")
	outmag=".maglim"
	try:	
		os.remove(fitsDir+fitsFile+outmag)
	except:
		print "File does not exist BEFORE running phot, so no need to delete."
	iraf.phot.setParam('output',fitsDir+fitsFile+outmag)
	iraf.phot.setParam('interac','no')	
	iraf.fitskypars.setParam('annulus',str(ann))	
	iraf.fitskypars.setParam('dannulus',str(delta))
	## NO SIGMA CLIPPING! JUST TO BE SAFE: (6/2013)
	iraf.fitskypars.setParam('sloclip',"0")
	iraf.fitskypars.setParam('shiclip',"0")

	iraf.phot(fitsDir+fitsFile,Stdin=cr)	
	aa, nn, xx, ss = ao.readPhotMagFile(fitsDir,fitsFile,outmag)
	try:	
		os.remove(fitsDir+fitsFile+outmag)
	except:
		print "File not found to delete AFTER running phot; that's odd."
	return xx
Exemple #6
0
    def _get_photometry(self):
        """ Get the photometry for the target.

        If the target is a standard star, aperture photometry will be performed. For the moment nothing is done with
        the others, but in due time (TODO) photometry.py will be included here. """

        basename = "standards"
        fd, coords_file = tempfile.mkstemp(prefix=basename, suffix=".coords")
        os.write(fd, "{0} {1} \n".format(self.RA, self.DEC))
        os.close(fd)

        if self.objtype == "standard":
            iraf.noao(_doprint=0)
            iraf.digiphot(_doprint=0)
            iraf.apphot(_doprint=0)
            seeing = self.header.hdr[self.header.seeingk]
            photfile_name = self.header.im_name + ".mag.1"
            utilities.if_exists_remove(photfile_name)
            kwargs =  dict(output=photfile_name, coords=coords_file,
                      wcsin='world', fwhm=seeing, gain=self.header.gaink, exposure=self.header.exptimek,
                      airmass=self.header.airmassk, annulus=6*seeing, dannulus=3*seeing,
                      apert=2*seeing, verbose="no", verify="no", interac="no")
            iraf.phot(self.header.im_name, **kwargs)
            [counts] = iraf.module.txdump(photfile_name, 'FLUX', 'yes', Stdout=subprocess.PIPE)
            utilities.if_exists_remove(coords_file)
            return float(counts)
Exemple #7
0
def p60photsub(ofile, inlis, refimage, refstars, ot, filter):
   
    refstars.set_mag(filter)
    coofile=open('ot.coo','w')
    ot.wcs2pix(refimage)
    coofile.write('%10.3f%10.3f\n' % (ot[0].xval, ot[0].yval))
    coofile.close()

    outfile=open(ofile, 'a+')
  
    p60photutils.p60zeropt(inlis, refstars, filter)

    subimage=iraffiles('@%s' % inlis)
    for image in subimage:

        root,ext=image.split('.')
        iraf.phot('%s.sub' % root, coo='ot.coo', output='%s.grb' % root,
                  interac=no)

        stars=Starlist('%s.mag' % root)
        refstars.wcs2pix(image)
        zp,zpu=stars.zeropt(refstars,method='mean',rejout=0)

        otstars=Starlist('%s.grb' % root)    
        
        [mjd,utshut]=get_head(image,['OBSMJD','UTSHUT'])

        if (len(stars)>0):
            outfile.write('%s\t%s\t%.3f\t%.3f\t%.3f\t%.3f\n' % (mjd, utshut, otstars[0].mag+zp, sqrt(pow(zpu,2)+pow(otstars[0].magu,2)), otstars[0].magu, zpu))

    outfile.close()
def do_iraf_phot():
    """ Runs IRAF photometry with `iraf.phot`.
    """ 
    coofile = os.path.join(path_to_out, 'coo_files', 'teststar_iraf.coo')
    outname = os.path.join(path_to_out, 'mag_files', 'teststar_iraf_centroid.mag')
    #apertures='1,2,3,4,5,10,12,15,20'
    aperture_list = np.arange(0.5,15.5,0.5) 
    # Make the aperture list IRAF-compatible.
    apertures = ''
    for ap in aperture_list:
        apertures += str(ap) + ','
    apertures[:-1]    

    print(" ")
    print(image)
    iraf.phot.unlearn()         
    iraf.phot(image=image+'[0]', 
              interactive='no', 
                verify='no', 
                coords=coofile, 
                output=outname,
                #fwhmpsf=2.5,
                #itime=exptime, 
                calgorithm='centroid', 
                #cbox=5.0,
                #annulus=12.0,
                #dannulus=10.0,
                apertures=apertures)    
Exemple #9
0
def Photometry(setn, flist, band):
    # J-band
    os.system("head -1 -q %s/xylist/%sf*pos | awk '{print $3}'> tmp" %
              (setn, band))
    ff = open("tmp")
    fr = ff.readlines()
    ff.close
    r = []
    for x in fr:
        r.append(float(x))
    radi = np.nanmean(r)
    print s, band, radi

    # aperture,annulus,dannulus
    iraf.apphot.photpars.apertures = radi
    iraf.apphot.fitskypars.annulus = radi + 4
    iraf.apphot.fitskypars.dannulus = 10

    for x in flist:
        if x[0] in s:
            infile = "%s/xylist/%sf%s.pos" % (setn, band, x[1])
            outfile = "%s/photometry/%sf%s.mag" % (setn, band, x[1])
            dumped = "%s/photometry/%sf%s.txt" % (setn, band, x[1])
            fits = "%s/%sf%smasked.fits" % (setn, band, x[1])
            print infile, fits
            iraf.phot(fits, coords=infile, output=outfile, wcsin="physical")
            iraf.txdump(outfile,
                        fields="xc,yc,mag,merr,flux,msky",
                        expr="yes",
                        Stdout=dumped)
Exemple #10
0
 def daophot(self, framenum, coords, outfile, apertures, verbose = "no"):
     """
     Aperture photometry of stars in the coords file using IRAF PHOT routine.
     
     Parameters
     ----------
     framenum : int
         Frame number in the image cube to perform aperture photometry on.
         
     coords : string
         Text file with coordinate of the stars
         
     outfile : string
         Text file to which photometry results are written
     
     Returns
     -------
     None
     """
     # load iraf packages
     self.setiraf()
     
     iraf.delete(outfile)
     iraf.phot(image = self.sci_file + "[,," + str(framenum) + "]", coords = coords, output = outfile, fwhmpsf = self.fwhmpsf, sigma = self.sigma, readnoise = self.readnoise, epadu = self.epadu, exposure = self.exposure, calgorithm = self.calgorithm, cbox = self.cbox, maxshift = self.maxshift, salgorithm = self.salgorithm, annulus = self.annulus, dannulus = self.dannulus, apertures = apertures, zmag = self.zmag, interactive = "no", verify = "no", verbose = verbose)        
     
     return
Exemple #11
0
    def daophot(self, framenum, coords, outfile, apertures, verbose = "no"):
        """
        Aperture photometry of stars in the coords file using IRAF PHOT routine.

        Parameters
        ----------
        framenum : int
            Frame number in the image cube to perform aperture photometry on.

        coords : string
            Text file with coordinate of the stars

        outfile : string
            Text file to which photometry results are written

        Returns
        -------
        None
        """
        # load iraf packages
        self.setiraf()

        iraf.delete(outfile)
        iraf.phot(image = self.sci_file + "[,," + str(framenum) + "]", coords = coords, output = outfile, fwhmpsf = self.fwhmpsf, sigma = self.sigma, readnoise = self.readnoise, epadu = self.epadu, exposure = self.exposure, calgorithm = self.calgorithm, cbox = self.cbox, maxshift = self.maxshift, salgorithm = self.salgorithm, annulus = self.annulus, dannulus = self.dannulus, apertures = apertures, zmag = self.zmag, interactive = "no", verify = "no", verbose = verbose)

        return
Exemple #12
0
def psffit2(img, fwhm, psfstars, hdr, _datamax=45000, psffun='gauss',  fixaperture=False):
    ''' 
    giving an image, a psffile,  calculate the magnitudes of strs in the file _psf.coo
    '''
    import lsc
    _ron = lsc.util.readkey3(hdr, 'ron')
    _gain = lsc.util.readkey3(hdr, 'gain')
    if not _ron:
        _ron = 1
        print 'warning ron not defined'
    if not _gain:
        _gain = 1
        print 'warning ron not defined'

    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)
    zmag = 0.
    varord = 0  # -1 analitic 0 - numeric

    if fixaperture:
        print 'use fix aperture 5 8 10'
        hdr = lsc.util.readhdr(img+'.fits')
        _pixelscale = lsc.util.readkey3(hdr, 'PIXSCALE')
        a1, a2, a3, a4, = float(5. / _pixelscale), float(5. / _pixelscale), float(8. / _pixelscale), float(
                    10. / _pixelscale)
    else:
        a1, a2, a3, a4, = int(fwhm + 0.5), int(fwhm * 2 + 0.5), int(fwhm * 3 + 0.5), int(fwhm * 4 + 0.5)

    iraf.fitskypars.annulus = a4
    iraf.fitskypars.salgori = 'mean'  #mode,mean,gaussian
    iraf.photpars.apertures = '%d,%d,%d' % (a2, a3, a4)
    iraf.datapars.datamin = -100
    iraf.datapars.datamax = _datamax
    iraf.datapars.readnoise = _ron
    iraf.datapars.epadu = _gain
    iraf.datapars.exposure = 'EXPTIME'  
    iraf.datapars.airmass = ''
    iraf.datapars.filter = ''
    iraf.photpars.zmag = zmag
    iraf.centerpars.calgori = 'centroid'
    iraf.centerpars.cbox = a2
    iraf.daopars.recenter = 'yes'
    iraf.delete('_psf2.ma*', verify=False)

    iraf.phot(img+'[0]', '_psf2.coo', '_psf2.mag', interac=False, verify=False, verbose=False)

    iraf.daopars.psfrad = a4
    iraf.daopars.functio = psffun
    iraf.daopars.fitrad = a1
    iraf.daopars.fitsky = 'yes'
    iraf.daopars.sannulus = a4
    iraf.daopars.recenter = 'yes'
    iraf.daopars.varorder = varord
    iraf.delete("_als2,_psf.grp,_psf.nrj", verify=False)
    iraf.group(img + '[0]', '_psf2.mag', img + '.psf', '_psf.grp', verify=False, verbose=False)
    iraf.nstar(img + '[0]', '_psf.grp', img + '.psf', '_als2', '_psf.nrj', verify=False, verbose=False)
    photmag = iraf.txdump("_psf2.mag", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1)
    fitmag = iraf.txdump("_als2", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1)
    return photmag, fitmag
Exemple #13
0
def psffit2(img, fwhm, psfstars, hdr, _datamax, psffun='gauss',  fixaperture=False):
    ''' 
    giving an image, a psffile,  calculate the magnitudes of strs in the file _psf.coo
    '''
    import lsc
    _ron = lsc.util.readkey3(hdr, 'ron')
    _gain = lsc.util.readkey3(hdr, 'gain')
    if not _ron:
        _ron = 1
        print 'warning ron not defined'
    if not _gain:
        _gain = 1
        print 'warning ron not defined'

    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)
    zmag = 0.
    varord = 0  # -1 analitic 0 - numeric

    if fixaperture:
        print 'use fix aperture 5 8 10'
        hdr = lsc.util.readhdr(img+'.fits')
        _pixelscale = lsc.util.readkey3(hdr, 'PIXSCALE')
        a1, a2, a3, a4, = float(5. / _pixelscale), float(5. / _pixelscale), float(8. / _pixelscale), float(
                    10. / _pixelscale)
    else:
        a1, a2, a3, a4, = int(fwhm + 0.5), int(fwhm * 2 + 0.5), int(fwhm * 3 + 0.5), int(fwhm * 4 + 0.5)

    iraf.fitskypars.annulus = a4
    iraf.fitskypars.salgori = 'mean'  #mode,mean,gaussian
    iraf.photpars.apertures = '%d,%d,%d' % (a2, a3, a4)
    iraf.datapars.datamin = -100
    iraf.datapars.datamax = _datamax
    iraf.datapars.readnoise = _ron
    iraf.datapars.epadu = _gain
    iraf.datapars.exposure = 'EXPTIME'  
    iraf.datapars.airmass = ''
    iraf.datapars.filter = ''
    iraf.photpars.zmag = zmag
    iraf.centerpars.calgori = 'centroid'
    iraf.centerpars.cbox = a2
    iraf.daopars.recenter = 'yes'
    iraf.delete('_psf2.ma*', verify=False)

    iraf.phot(img+'[0]', '_psf2.coo', '_psf2.mag', interac=False, verify=False, verbose=False)

    iraf.daopars.psfrad = a4
    iraf.daopars.functio = psffun
    iraf.daopars.fitrad = a1
    iraf.daopars.fitsky = 'yes'
    iraf.daopars.sannulus = a4
    iraf.daopars.recenter = 'yes'
    iraf.daopars.varorder = varord
    iraf.delete("_als2,_psf.grp,_psf.nrj", verify=False)
    iraf.group(img + '[0]', '_psf2.mag', img + '.psf', '_psf.grp', verify=False, verbose=False)
    iraf.nstar(img + '[0]', '_psf.grp', img + '.psf', '_als2', '_psf.nrj', verify=False, verbose=False)
    photmag = iraf.txdump("_psf2.mag", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1)
    fitmag = iraf.txdump("_als2", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1)
    return photmag, fitmag
Exemple #14
0
def get_fake_centroid(filename,x,y,instrument,filt):
    """
    Locate the center of a fake psf

    INPUTS: The fake-SN psf image in filename, the expected x,y position
    of the center of the psf, the instrument and filter being modeled.
    RETURNS: xcentroid, ycentroid, fluxcorr
    """
    from pyraf import iraf

    iraf.digiphot(_doprint=0)
    iraf.apphot(_doprint=0)
    iraf.unlearn(iraf.apphot.phot)
    iraf.unlearn(iraf.datapars)
    iraf.unlearn(iraf.centerpars)
    #Use the centroid algorithm right now as it seems more robust to geometric distortion.
    iraf.centerpars.calgorithm = 'centroid'
    iraf.centerpars.cbox = 5.0

    iraf.unlearn(iraf.fitskypars)
    iraf.unlearn(iraf.photpars)
    photparams = {
        'interac':False,
        'radplot':False,
        }
    iraf.datapars.readnoise = 0.0
    iraf.datapars.itime = 1.0
    iraf.datapars.epadu = 1.0

    # iraf.digiphot.apphot.fitskypars :
    iraf.unlearn(iraf.fitskypars)
    iraf.fitskypars.salgorithm = 'constant'
    iraf.fitskypars.skyvalue = 0.0

    # iraf.digiphot.apphot.photpars :
    iraf.unlearn(iraf.photpars)
    iraf.photpars.weighting = 'constant'
    iraf.photpars.apertures = 20   # TODO : set this more intelligently !
    iraf.photpars.zmag = 25
    iraf.photpars.mkapert = False

    #Write the coordinate file starting as position x and y
    coxyfile = 'centroid.xycoo'
    coxy = open(coxyfile, 'w')
    print >> coxy, "%10.2f  %10.2f" % (x,y)
    coxy.close()
    if os.path.exists('centroid.mag'): os.remove('centroid.mag')
    iraf.phot(image=filename, skyfile='', coords=coxyfile, output='centroid.mag',
              verify=False, verbose=True, Stdout=1, **photparams)
    f = open('centroid.mag', 'r')
    maglines = f.readlines()
    f.close()
    xcentroid = float(maglines[76].split()[0])
    ycentroid = float(maglines[76].split()[1])

    return xcentroid,ycentroid
Exemple #15
0
def photometry(color, images):
	#grab the aligned images
	#images=sorted(glob.glob('reduced/'+color+'align/*fits'))

	for i in images:
		iraf.phot(i, coords='reduced/'+color+'align/coords.lis',
			fwhmpsf=3,datamin=-500.,datamax=10000.,readnoise=20.,epadu=7.2,
			  exposur="EXPTIME",airmass="SECZ",obstime="JD",filter="IRFLTID",annulus=15,
		  	  dannulu=7, apertur="9",verify='no',output=i+".MC")
	return
Exemple #16
0
def psffit2(img, fwhm, psfstars, hdr, _datamax=45000, psffun='gauss', fixaperture=False):
    import agnkey

    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)
    zmag = 0.
    varord = 0  # -1 analitic 0 - numeric
    if fixaperture:
        print 'use fix aperture 5 8 10'
        hdr = agnkey.util.readhdr(img+'.fits')
        _pixelscale = agnkey.util.readkey3(hdr, 'PIXSCALE')
        a1, a2, a3, a4, = float(5. / _pixelscale), float(5. / _pixelscale), float(8. / _pixelscale), float(
                    10. / _pixelscale)
    else:
        a1, a2, a3, a4, = int(fwhm + 0.5), int(fwhm * 2 + 0.5), int(fwhm * 3 + 0.5), int(fwhm * 4 + 0.5)

    _center='no'
    iraf.fitskypars.annulus = a4
    iraf.fitskypars.dannulus = a4
    iraf.noao.digiphot.daophot.daopars.sannulus = int(a4)
    iraf.noao.digiphot.daophot.daopars.wsannul = int(a4)
    iraf.fitskypars.salgori = 'mean'  #mode,mean,gaussian
    iraf.photpars.apertures = '%d,%d,%d' % (a2, a3, a4)
    #    iraf.photpars.apertures = '%d,%d,%d'%(a2,a3,a4)
    iraf.datapars.datamin = -100
    iraf.datapars.datamax = _datamax
    iraf.datapars.readnoise = agnkey.util.readkey3(hdr, 'ron')
    iraf.datapars.epadu = agnkey.util.readkey3(hdr, 'gain')
    iraf.datapars.exposure = 'exptime'  #agnkey.util.readkey3(hdr,'exptime')
    iraf.datapars.airmass = 'airmass'
    iraf.datapars.filter = 'filter2'
    iraf.centerpars.calgori = 'gauss'
    iraf.centerpars.cbox = 1
    iraf.daopars.recenter = _center
    iraf.photpars.zmag = zmag

    iraf.delete('_psf2.ma*', verify=False)

    iraf.phot(img+'[0]', '_psf2.coo', '_psf2.mag', interac=False, verify=False, verbose=False)

    iraf.daopars.psfrad = a4
    iraf.daopars.functio = psffun
    iraf.daopars.fitrad = a1
    iraf.daopars.fitsky = 'yes'
    iraf.daopars.sannulus = int(a4)
    iraf.daopars.wsannul = int(a4)
    iraf.daopars.recenter = _center
    iraf.daopars.varorder = varord
    iraf.delete("_als,_psf.grp,_psf.nrj", verify=False)
    iraf.group(img+'[0]', '_psf2.mag', img + '.psf', '_psf.grp', verify=False, verbose=False)
    iraf.nstar(img+'[0]', '_psf.grp', img + '.psf', '_als', '_psf.nrj', verify=False, verbose=False)
    photmag = iraf.txdump("_psf2.mag", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1)
    fitmag = iraf.txdump("_als", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1)
    return photmag, fitmag
Exemple #17
0
def psffit2(img, fwhm, psfstars, hdr, _datamax=45000, psffun='gauss', fixaperture=False):
    import agnkey

    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)
    zmag = 0.
    varord = 0  # -1 analitic 0 - numeric
    if fixaperture:
        print 'use fix aperture 5 8 10'
        hdr = agnkey.util.readhdr(img+'.fits')
        _pixelscale = agnkey.util.readkey3(hdr, 'PIXSCALE')
        a1, a2, a3, a4, = float(5. / _pixelscale), float(5. / _pixelscale), float(8. / _pixelscale), float(
                    10. / _pixelscale)
    else:
        a1, a2, a3, a4, = int(fwhm + 0.5), int(fwhm * 2 + 0.5), int(fwhm * 3 + 0.5), int(fwhm * 4 + 0.5)

    _center='no'
    iraf.fitskypars.annulus = a4
    iraf.fitskypars.dannulus = a4
    iraf.noao.digiphot.daophot.daopars.sannulus = int(a4)
    iraf.noao.digiphot.daophot.daopars.wsannul = int(a4)
    iraf.fitskypars.salgori = 'mean'  #mode,mean,gaussian
    iraf.photpars.apertures = '%d,%d,%d' % (a2, a3, a4)
    #    iraf.photpars.apertures = '%d,%d,%d'%(a2,a3,a4)
    iraf.datapars.datamin = -100
    iraf.datapars.datamax = _datamax
    iraf.datapars.readnoise = agnkey.util.readkey3(hdr, 'ron')
    iraf.datapars.epadu = agnkey.util.readkey3(hdr, 'gain')
    iraf.datapars.exposure = 'exptime'  #agnkey.util.readkey3(hdr,'exptime')
    iraf.datapars.airmass = 'airmass'
    iraf.datapars.filter = 'filter2'
    iraf.centerpars.calgori = 'gauss'
    iraf.centerpars.cbox = 1
    iraf.daopars.recenter = _center
    iraf.photpars.zmag = zmag

    iraf.delete('_psf2.ma*', verify=False)

    iraf.phot(img+'[0]', '_psf2.coo', '_psf2.mag', interac=False, verify=False, verbose=False)

    iraf.daopars.psfrad = a4
    iraf.daopars.functio = psffun
    iraf.daopars.fitrad = a1
    iraf.daopars.fitsky = 'yes'
    iraf.daopars.sannulus = int(a4)
    iraf.daopars.wsannul = int(a4)
    iraf.daopars.recenter = _center
    iraf.daopars.varorder = varord
    iraf.delete("_als,_psf.grp,_psf.nrj", verify=False)
    iraf.group(img+'[0]', '_psf2.mag', img + '.psf', '_psf.grp', verify=False, verbose=False)
    iraf.nstar(img+'[0]', '_psf.grp', img + '.psf', '_als', '_psf.nrj', verify=False, verbose=False)
    photmag = iraf.txdump("_psf2.mag", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1)
    fitmag = iraf.txdump("_als", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1)
    return photmag, fitmag
Exemple #18
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
Exemple #19
0
def psf(args):
    """ Calculate the PSF of an image.
    """
    # Read the seeing and sigma of the sky from the header
    seeing, sigma = utils.get_from_header(args.input, args.FWHM_key, args.sigma)
    
    # Do photometry on the image
    #print "photometry: \n"
    photfile_name = args.input + ".mag.1"
    utils.if_exists_remove(photfile_name)
    iraf.phot(args.input, output=photfile_name, coords=args.stars,
                     wcsin=args.coords, fwhm=seeing, 
                     sigma=sigma, datamax=args.maxval, datamin=args.minval,
                     ccdread=args.ron_key, gain=args.gain_key, exposure=args.expt_key,
                     airmass=args.airm_key, annulus=36, dannulus=18,
                     apert=18, verbose="no", verify="no", interac="no")


    # Select stars on the image                 
    #print "pstselect: \n"
    pstfile_name = args.input + ".pst.1"
    utils.if_exists_remove(pstfile_name)
    iraf.pstselect(args.input, photfile=photfile_name, pstfile=pstfile_name,
                          maxnpsf=20, fwhm=seeing, sigma=sigma,
                       datamax=args.maxval, ccdread=args.ron_key, gain=args.gain_key,
                       exposure=args.expt_key,  function="auto", nclean=1, 
                       psfrad=36, fitrad=18, maxnstar=20, verbose="no", interac="no",
                       verify="no")

    # Build psf of the stars
    #print "psf: \n"
    psffile_table = args.input + ".psf.1.fits"  # iraf keeps adding the .fits :(
    psgfile_name = args.input + ".psg.1"
    pstfile_name2 = args.input + ".pst.2"   
    utils.if_exists_remove(psffile_table,psgfile_name, pstfile_name2)
    iraf.psf( args.input, photfile=photfile_name, pstfile=pstfile_name,
                     groupfile=psgfile_name, opstfile=pstfile_name2,    
                     psfimage=psffile_table,fwhm=seeing, sigma=sigma, datamax=args.maxval, 
                     datamin=args.minval, ccdread=args.ron_key, gain=args.gain_key, 
                     exposure=args.expt_key, function="moffat25", nclean=1, 
                     psfrad=36, fitrad=18, maxnstar=20, interactive="no",
                     varorder=args.varorder, verbose="no",verify="no")

    # Use seepsf to build the image of the psf
    psffile_name = args.input + ".psf.fits" 
    utils.if_exists_remove(psffile_name)
    iraf.seepsf(psffile_table, psffile_name)
     
    return psffile_name
Exemple #20
0
def aperphot(
    imgs,
    posname,
    aper1,
    aper2,
    aper3,
):
    postfix = '.obs'
    for i in xrange(len(imgs)):

        basename, exten = os.path.splitext(imgs[i])
        photname = basename + postfix

        meanpsf = 2.0
        std = 70.0

        iraf.display(imgs[i], frame=1)
        iraf.tvmark(frame=1,
                    coords=posname,
                    autolog='No',
                    mark='circle',
                    radii=30,
                    lengths=3,
                    font='raster',
                    color=204,
                    label='No',
                    number='Yes',
                    txsize=3)

        iraf.datapars.ccdread = 'CCDRON'
        iraf.datapars.gain = 'CCDGAIN'
        iraf.datapars.scale = 1.0
        iraf.datapars.fwhmpsf = meanpsf
        iraf.datapars.sigma = std
        iraf.datapars.filter = 'INSFLNAM'
        iraf.datapars.datamax = 1000000.0
        iraf.centerpars.cbox = 8.0
        iraf.centerpars.calgorithm = 'centroid'
        iraf.fitskypars.salgorithm = 'median'
        iraf.fitskypars.annulus = aper2  # ------ important ------
        iraf.fitskypars.dannulus = aper3  # ------ important ------
        #iraf.photpars.apertures = meanpsf  # ------ important ------
        iraf.photpars.apertures = aper1  # ------ important ------

        iraf.phot(image=imgs[i],
                  coords=posname,
                  output=photname,
                  interactive=False,
                  verify=False)
Exemple #21
0
def apply_phot(args):
    # Temporary file
    with tempfile.NamedTemporaryFile(suffix=".mag.1") as fd:
        phot_modstars = fd.name

    with tempfile.NamedTemporaryFile(suffix=".mag.1") as fd2:
        phot_subtstars = fd2.name

    hdr = args.image.header  # for short
    seeing, sigma = utils.get_from_header(args.image.im_name, hdr.seeingk, hdr.sigmak)
    iraf.phot(
        args.image.im_name,
        output=phot_modstars,
        coords=args.model_stars,
        wcsin=args.coords,
        fwhmpsf=seeing,
        sigma=sigma,
        ccdread=hdr.ccdronk,
        gain=hdr.gaink,
        exposure=hdr.exptimek,
        airmass=hdr.airmassk,
        annulus=6 * seeing,
        dannulus=3 * seeing,
        apert=3 * seeing,
        verbose="no",
        verify="no",
        interac="no",
    )

    iraf.phot(
        args.image.im_name,
        output=phot_subtstars,
        coords=args.subt_stars,
        wcsin=args.coords,
        fwhmpsf=seeing,
        sigma=sigma,
        ccdread=hdr.ccdronk,
        gain=hdr.gaink,
        exposure=hdr.exptimek,
        airmass=hdr.airmassk,
        annulus=6 * seeing,
        dannulus=3 * seeing,
        apert=3 * seeing,
        verbose="no",
        verify="no",
        interac="no",
    )
    return phot_modstars, phot_subtstars
Exemple #22
0
def p48coaddphot(inlis, refstars, ot, filter, outfile):

    outf = open(outfile, "w")
    
    for image in inlis:

        root = image.split(".")[0]
        
        # Get the seeing in the image
        iqpkg.iqobjs(image, SIGMA, get_head(image, "SATURATE"), skyval="0.0")
        seepix = get_head(image, "SEEPIX")

        # Calculate zeropoint
        refstars.wcs2pix(image)
        refstars.set_mag(filter)
        xyfile = open("%s.xy" % root, "w")
        for star in refstars:
            xyfile.write("%10.3f%10.3f\n" % (star.xval, star.yval))
        xyfile.close()
        iraf.phot(image, coords="%s.xy" % root, output="%s.mag" % root,
                  aperture=1.2*float(seepix), interac=no)
        stars = Starlist("%s.mag" % root)
        zp, zpu = stars.zeropt(refstars,method="mean",rejout=0)

        # Measure source
        ot.wcs2pix(image)
        coofile = open("%s.coo" % root, "w")
        coofile.write("%10.3f%10.3f\n" % (ot[0].xval, ot[0].yval))
        coofile.close()
        iraf.phot(image, coords="%s.coo" % root, output="%s.ot" % root,
                  aperture=1.2*float(seepix), calgorithm="none", interac=no)
        stars = Starlist("%s.ot" % root)

        # Just return 99 for non-detection
        if len(stars)==1:
            smag = stars[0].mag
            smagu = stars[0].magu
            mag = smag + zp
        else:
            mag = 99.0
            smagu = 99.0
        mjdobs = DateTimeFrom(get_head(image, OBSDATEKEY)).mjd
        exptime = get_head(image, EXPTIMEKEY)
        outf.write("%15.3f%10.1f%10.3f%10.3f%10.3f\n" % (mjdobs, exptime, mag, smagu, zpu))

    outf.close()
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
Exemple #24
0
    def transform(self, image: Image):

        if not self.requirements_check(image):
            raise ValueError(
                "Missing required transformations on image. Need:" +
                str(self.REQUIRES))
        # INIT
        from pyraf import iraf
        iraf.noao.digiphot(_doprint=0)
        iraf.noao.digiphot.daophot(_doprint=0)
        capable.OF_GRAPHICS = False

        # File handling
        temp_final_out = CONFIG["FILE_DUMP"] + 'TempPhotOut.dat'
        if os.path.exists(temp_final_out):
            os.remove(temp_final_out)

        base = image.fixed_parameters["path"].split('/')[-1]
        dao_phot_out = CONFIG["FILE_DUMP"] + base + ".mag.dat"
        phot_txt_out = CONFIG["FILE_DUMP"] + base + "PhoTxOut.dat"

        # Setting pyraf phot parameters
        photpars = iraf.photpars.getParList()
        iraf.photpars.setParam('apertures', CONFIG["APERTURE"])
        iraf.phot.setParam('image', image.fixed_parameters["path"])
        iraf.phot.setParam('coords', self._get_coordinates_file(image))
        iraf.phot.setParam('verify', 'no')
        iraf.phot.setParam('output', dao_phot_out)
        iraf.phot.setParam('interactive', 'no')

        if os.path.exists(dao_phot_out):
            os.remove(dao_phot_out)

        # Running IRAF task
        dump = iraf.phot(mode='h', Stdout=1)

        if os.path.exists(phot_txt_out):
            os.remove(phot_txt_out)

        # Getting better formatted file with magnitudes
        iraf.txdump(dao_phot_out,
                    'XCENTER,YCENTER,MAG,MERR',
                    'yes',
                    Stdout=phot_txt_out)

        # Getting results from output file
        with open(phot_txt_out) as output_file:
            results = {}
            i = 0
            for lines in output_file:
                parts = lines.split()
                results[self.objects[i].fixed_parameters["id"]] = (
                    float(parts[2]) if parts[2] != "INDEF" else None,
                    float(parts[3]) if parts[3] != "INDEF" else None)
                i += 1

        image.processing_parameters["photometry"] = results
        return image
Exemple #25
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   
Exemple #26
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
Exemple #27
0
def createMagFile(inputDir, inputFile, useCoordFile, outputFile, fullyAutomatic=False, aperturesString='5,10,15,20', bestSkyAperture='100'):	
	cr=['\n']
	iraf.cd(inputDir)
	iraf.noao()
	iraf.digiphot()
	iraf.apphot()
	iraf.phot.setParam('interactive','no')
	iraf.centerpars.setParam('cbox','5')
	iraf.photpars.setParam('apertures',aperturesString)
	iraf.fitskypars.setParam('annulus',bestSkyAperture)
	iraf.fitskypars.setParam('dannulus','10')
	iraf.phot.setParam('image',inputFile)
	iraf.phot.setParam('coords',useCoordFile)
	iraf.phot.setParam('output',outputFile)
	if fullyAutomatic: ## If you use this option, it will not prompt you for any of the aperture/sky settings
		iraf.phot(mode='h',Stdin=cr)
	else: # Otherwise, you can change them at will, and get to press enter a bunch
		iraf.phot(mode='h')
Exemple #28
0
def run_daophot(image, outfile='default', coordfile='NA', apertures='5.0,16.66', annulus=17.0, dannulus=3.0, calgorithm='centroid', salgorithm='median', fwhmpsf=2.5, backsigma=-1.0,rdnoise=5.2):

	'''THIS PROCEDURE RUNS DAOPHOT ON INPUT IMAGE'''

	# Parse input parameters
	if outfile == 'default': outfile = image + '0.mag.1'
	if coordfile == 'NA': coordfile = image + '0.coo.1'

	# Read in fits header
	f = pyfits.open(image)
	fheader = f[0].header
	f.close()

	# Extract relevant info from the header
	exptime = fheader['texptime']
	filter = fheader['FILTER2']
	ipxscl = fheader['D001ISCL']
	opxscl = fheader['D001SCAL']
	num_flts = float(fheader['NDRIZIM'])/2.0
	dp_zmag = -2.5 * np.log10(float(fheader['PHOTFLAM'])) + float(fheader['PHOTZPT'])       # zero-pt for each filter
	#df_max = 10000000.      # upper limit for "good" pixels (sometimes used to ID bad pixels). Not using this parameter right now.

	# Perform read noise correction
	rdnoise_corr = np.sqrt(num_flts * (rdnoise * opxscl/ipxscl)**2)

	# Perform background noise calculation
	if backsigma < 0.0:
		backrms=iraf.imstatistics(image+'[0]', fields='stddev', nclip=10, lsigma=3.0, usigma=3.0, cache='yes', format='no',Stdout=1)
		backsigma=float(backrms[0])


	'''Run daophot'''
	# Remove old phot output files
	file_query = os.access(outfile, os.R_OK)      
	if file_query == True: os.remove(outfile)

	# Run phot
	iraf.phot.unlearn()         # reset daophot parameters to default values
	iraf.phot(image=image+'[0]', interactive='no', verify='no', coords=coordfile, output=outfile, fwhmpsf=fwhmpsf, \
      		sigma=backsigma, readnoise=rdnoise_corr, itime=exptime, calgorithm=calgorithm, salgorithm=salgorithm, \
      		annulus=annulus, dannulus=dannulus, apertures=apertures,zmag=dp_zmag)


	return outfile 		# return name of output catalog
def calc_curve_of_growth(dataDir, image, apertures):
    aper = apertures

    # ==========
    # Make a coordinate file, PSF is in the middle
    # ==========
    # get image size
    data = pyfits.getdata(dataDir + image + '_psf.fits')
    imgSize = data.shape[0]
    
    # Write out coo file
    cooFile = image + '_psf.coo'
    _coo = open(cooFile, 'w')
    _coo.write('%.2f %.2f\n' % (imgSize/2.0, imgSize/2.0))
    _coo.close()
    
    
    # #########
    # Setup phot for running on PSF
    # #########
    ph.setup_phot(dataDir + image + '_psf', apertures=aper)
    ir.fitskypars.salgorithm = 'constant'
    ir.fitskypars.skyvalue = 0.0
    
    # Output into current directory, not data directory
    input = dataDir + image + '_psf.fits'
    output = workDir + image + '_psf.phot.mag'

    ir.phot(input, cooFile, output, verbose="no")
    
    (radius, flux, mag, merr) = ph.get_phot_output(output, silent=True)
    diff = np.zeros(len(radius), dtype=float)
    diff[1:] = mag[1:] - mag[:-1]
    
    # Save output
    pfile = open(workDir + 'cog_' + image + '.dat', 'w')
    pickle.dump(radius, pfile)
    pickle.dump(flux, pfile)
    pickle.dump(mag, pfile)
    pickle.dump(merr, pfile)
    pickle.dump(diff, pfile)
    pfile.close()

    return (radius, flux, mag, merr, diff)
def run_phot(imageRoot, silent=False,
             apertures=[25,50,75,100,125,150,175,200],
             sky_annulus=200, sky_dannulus=50, zmag=0):

    setup_phot(imageRoot, apertures=apertures, zmag=zmag, silent=silent,
               sky_annulus=sky_annulus, sky_dannulus=sky_dannulus)

    image = imageRoot + '.fits'
    coords = imageRoot + '.coo'

    # Output into current directory, not data directory
    rootSplit = imageRoot.split('/')
    output = rootSplit[-1] + '.phot.mag'

    ir.phot(image, coords, output)

    (radius, flux, mag, merr) = get_phot_output(output, silent=silent)

    return (radius, flux, mag, merr)
Exemple #31
0
def calc_curve_of_growth(dataDir, image, apertures):
    aper = apertures

    # ==========
    # Make a coordinate file, PSF is in the middle
    # ==========
    # get image size
    data = pyfits.getdata(dataDir + image + '_psf.fits')
    imgSize = data.shape[0]

    # Write out coo file
    cooFile = image + '_psf.coo'
    _coo = open(cooFile, 'w')
    _coo.write('%.2f %.2f\n' % (imgSize / 2.0, imgSize / 2.0))
    _coo.close()

    # #########
    # Setup phot for running on PSF
    # #########
    ph.setup_phot(dataDir + image + '_psf', apertures=aper)
    ir.fitskypars.salgorithm = 'constant'
    ir.fitskypars.skyvalue = 0.0

    # Output into current directory, not data directory
    input = dataDir + image + '_psf.fits'
    output = workDir + image + '_psf.phot.mag'

    ir.phot(input, cooFile, output, verbose="no")

    (radius, flux, mag, merr) = ph.get_phot_output(output, silent=True)
    diff = np.zeros(len(radius), dtype=float)
    diff[1:] = mag[1:] - mag[:-1]

    # Save output
    pfile = open(workDir + 'cog_' + image + '.dat', 'w')
    pickle.dump(radius, pfile)
    pickle.dump(flux, pfile)
    pickle.dump(mag, pfile)
    pickle.dump(merr, pfile)
    pickle.dump(diff, pfile)
    pfile.close()

    return (radius, flux, mag, merr, diff)
Exemple #32
0
	def doPhotometryACS(self, file, inputcoords, apertures, zeropoint, bgsigma, skyann, skydann):
		'''
		This function can be used to do photometry from a given image. Input parameters
		can be varied, however, the task assumes that photometry is done from an ACS
		image and the exposure time is found from the header of the file. 
		Object recentering is done with centroid algorithm and shifts up to 6 pixels are
		possible. For skyfitting algorithm mode is adopted.
		'''
		#load packages, should supress the output
		I.digiphot()
		I.apphot()

		#setting up for ACS
		I.datapar(sigma = bgsigma, exposure = 'exptime', gain = 'ccdgain')
		I.centerpars(calgorithm = 'centroid', cbox = 10., maxshift = 6.)
		I.fitskypars(salgorithm = 'mode', annulus = skyann, \
					 dannulus = skydann, skyvalue = 0.)
		I.photpars(apertures = apertures, zmag = zeropoint)

		I.phot(file, coords=inputcoords, verify='no', verbose = 'no')
Exemple #33
0
    def _get_photometry(self, seeing=None, aperture=None):
        """ Get the photometry for the target.

        If the target is a standard star, aperture photometry will be performed. For the moment nothing is done with
        the others, but in due time (TODO) photometry.py will be included here. """

        if self.objtype is not "standards":
            return None

        basename = "standards"
        fd, coords_file = tempfile.mkstemp(prefix=basename, suffix=".coords")
        os.write(fd, "{0} {1} \n".format(self.RA, self.DEC))
        os.close(fd)


        # If aperture was not given by the user, try and use the seeing as a reference for default values
        if not aperture:
            try:
                seeing = self.header.hdr[self.header.seeingk]
                aperture = 3 * seeing
            except ValueError:  # keyword was not correctly guessed
                pass

        # If aperture exists, do the photometry using it
        if aperture:
            annulus = 2 * aperture
            dannulus = max([aperture, 3])  # minimum of 3 pixels thickness for the sky annulus
            fd, photfile_name = tempfile.mkstemp(".mag.1")
            utilities.if_exists_remove(photfile_name)
            kwargs =  dict(output=photfile_name, coords=coords_file, salgorithm='median',
                      wcsin='world', fwhm=seeing, gain=self.header.gaink, exposure=self.header.exptimek,
                      airmass=self.header.airmassk, annulus=annulus, dannulus=dannulus,
                      apertures=aperture, verbose="no", verify="no", interac="no")
            iraf.phot(self.header.im_name, **kwargs)
            [counts] = iraf.txdump(photfile_name, 'FLUX', 'yes', Stdout=subprocess.PIPE)
        else:
            sys.exit("\n \n Sorry, no aperture was passed by you, and a seeing keyword was not "
                     "found in the header. \n\n ")

        utilities.if_exists_remove(coords_file)
        return float(counts)
Exemple #34
0
    def doPhotometryACS(self, file, inputcoords, apertures, zeropoint, bgsigma,
                        skyann, skydann):
        '''
		This function can be used to do photometry from a given image. Input parameters
		can be varied, however, the task assumes that photometry is done from an ACS
		image and the exposure time is found from the header of the file. 
		Object recentering is done with centroid algorithm and shifts up to 6 pixels are
		possible. For skyfitting algorithm mode is adopted.
		'''
        #load packages, should supress the output
        I.digiphot()
        I.apphot()

        #setting up for ACS
        I.datapar(sigma=bgsigma, exposure='exptime', gain='ccdgain')
        I.centerpars(calgorithm='centroid', cbox=10., maxshift=6.)
        I.fitskypars(salgorithm = 'mode', annulus = skyann, \
            dannulus = skydann, skyvalue = 0.)
        I.photpars(apertures=apertures, zmag=zeropoint)

        I.phot(file, coords=inputcoords, verify='no', verbose='no')
Exemple #35
0
def do_phot(fnlist,coofile,fwhmlist,skysiglist):
    """Runs the IRAF 'phot' routine on a series of images to determine their
    centroid positions and magnitudes. It is very likely you don't want to call
    this routine on its own, but rather have align_norm do it for you.
    
    Inputs:
    fnlist -> List of strings, each the path to a fits image
    coofile -> Path to the file containing the coordinate list of stars
    fwhmlist -> List of the PSF FWHM's for each image
    skysiglist -> List of the sky background sigmas for each image
    
    Outputs:
    photlist -> List of strings, the paths to the photometry outputs
    
    """
        
    iraf.noao(_doprint=0)
    iraf.digiphot(_doprint=0)
    iraf.apphot(_doprint=0)
    photlist = []
    for i in range(len(fnlist)):
        print "Performing photometry on the stars in image "+fnlist[i]
        photlist.append(fnlist[i]+".mag")
        iraf.phot(image=fnlist[i],
                  skyfile="",
                  coords=coofile,
                  output=photlist[i],
                  interactive="N",
                  sigma=skysiglist[i],
                  datamin='INDEF',
                  datamax='INDEF',
                  calgorithm='centroid',
                  cbox=2*fwhmlist[i],
                  salgorithm='centroid',
                  annulus=4*fwhmlist[i],
                  dannulus=6*fwhmlist[i],
                  apertures=4*fwhmlist[i],
                  verify='N')
    
    return photlist
Exemple #36
0
def do_phot(image_file_name, catalog_file_name, output_mag_file_name,
            sextractor_cfg_path, phot_params):
    """Calculates the photometry of the images.
    
    Receives the image to use, a catalog with the position of the objects
    contained in the image and the name of the output file.
    
    Args:     
        image_file_name: Name of the file with the image. 
        catalog_file_name: File with the X, Y coordinates to do phot.
        output_mag_file_name: Name of the output file with the magnitudes.
        sextractor_cfg_path: Path to the sextractor configuration files.
        phot_params: Parameters for phot.
    
    """

    # If magnitude file exists, remove it to avoid error.
    if os.path.exists(output_mag_file_name):
        os.remove(output_mag_file_name)

    logging.debug("Calculating magnitudes for: %s in %s" %
                  (image_file_name, output_mag_file_name))

    # Calculate datamin for this image.
    datamin = calculate_datamin(image_file_name, phot_params)

    # Calculate FWHM for this image.
    fwhm = astromatics.get_fwhm(sextractor_cfg_path, image_file_name)

    # Set the parameters for the photometry that depends on the image.
    set_image_specific_phot_pars(fwhm, phot_params)

    try:
        iraf.phot(image=image_file_name,
                  coords=catalog_file_name,
                  output=output_mag_file_name)
    except iraf.IrafError as exc:
        logging.error("Error executing phot on : %s" % (image_file_name))
        logging.error("Iraf error is: %s" % (exc))
Exemple #37
0
def phot(fn, outFn):
    if os.path.isfile(outFn):
        logger.debug("Skip, " + outFn + " exists!")
        return

    home = os.path.expanduser("~")

    iraf.centerpars.setParam("cbox", 3)
    iraf.centerpars.saveParList(filename= home + "/uparm/aptcentes.par")

    iraf.fitskypars.setParam("annulus", 30)
    iraf.fitskypars.setParam("dannulus", 5)
    iraf.fitskypars.saveParList(filename= home + "/uparm/aptfitsks.par")
            
    iraf.photpars.setParam("apertur", ', '.join(str(i) for i in range(3, 25, 2)))
    iraf.photpars.setParam("zmag", 25)
    iraf.photpars.saveParList(filename= home + "/uparm/aptphot.par")

    cooFile = fn + ".coo"
    iraf.phot(fn, coords =  cooFile, output = outFn, interac = "no", verify = "no", Stdout=1, cbox=3,
            exposur = "EXPTIME", airmass = "AIRMASS", filter = "DATE-OBS", #To keep it as a string
            apertur = "3, 6, 9, 12, 15, 18, 21", annulus = 40, dannulus = 5, obstime = "")
Exemple #38
0
def do_phot(image_file_name, catalog_file_name, output_mag_file_name, 
            sextractor_cfg_path, phot_params):
    """Calculates the photometry of the images.
    
    Receives the image to use, a catalog with the position of the objects
    contained in the image and the name of the output file.
    
    Args:     
        image_file_name: Name of the file with the image. 
        catalog_file_name: File with the X, Y coordinates to do phot.
        output_mag_file_name: Name of the output file with the magnitudes.
        sextractor_cfg_path: Path to the sextractor configuration files.
        phot_params: Parameters for phot.
    
    """
    
    # If magnitude file exists, remove it to avoid error.
    if os.path.exists(output_mag_file_name):
        os.remove(output_mag_file_name)

    logging.debug("Calculating magnitudes for: %s in %s" %
                  (image_file_name, output_mag_file_name))
    
    # Calculate datamin for this image.   
    datamin = calculate_datamin(image_file_name, phot_params)                         
           
    # Calculate FWHM for this image.
    fwhm = astromatics.get_fwhm(sextractor_cfg_path, image_file_name)
           
    # Set the parameters for the photometry that depends on the image.
    set_image_specific_phot_pars(fwhm, phot_params)                
                
    try:           
        iraf.phot(image = image_file_name, 
                    coords = catalog_file_name, 
                    output = output_mag_file_name)
    except iraf.IrafError as exc:
        logging.error("Error executing phot on : %s" % (image_file_name)) 
        logging.error( "Iraf error is: %s" % (exc))
Exemple #39
0
def daofind(filelist):
    iraf.datapars.setParam("exposure", "EXPTIME")
    iraf.datapars.setParam("airmass", "AIRMASS")
    iraf.datapars.setParam("filter", "INSFILTE")
    iraf.datapars.setParam("ccdread", "RDNOISE")
    iraf.datapars.setParam("gain", "GAIN")

    iraf.centerpars.setParam("calgorithm", "centroid")

    iraf.photpars.setParam("apertures", "17.56")

    iraf.fitskypars.setParam("annulus", "20.56")
    iraf.fitskypars.setParam("dannulus", "5")

    with open(filelist) as file1:
        for line in file1:
            filename = line.strip()
            print("Filename %s" % filename)
            y, z = np.loadtxt(filename + "-daoedit",
                              usecols=[3, 4],
                              unpack=True)
            sigma = np.mean(y)
            fwhm = np.mean(z)
            iraf.datapars.setParam("fwhmpsf", "%s" % str(fwhm))
            iraf.datapars.setParam("sigma", "%s" % str(sigma))
            iraf.daofind.setParam("mode", "h")
            iraf.phot.setParam("mode", "h")
            iraf.datapars.setParam("mode", "h")
            iraf.centerpars.setParam("mode", "h")
            iraf.fitskypars.setParam("mode", "h")
            iraf.photpars.setParam("mode", "h")

            iraf.daofind(filename, filename + "-daofind", mode='h')

            iraf.phot(filename,
                      coords=(filename + "-daofind"),
                      output=(filename + ".mag"),
                      mode='h')
Exemple #40
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   
Exemple #41
0
def getnoise(image, catalog):  #sky-subtracted image
    (ximage, yimage, isoarea, fluxerriso) = readcatalog(catalog)
    (xpos, ypos) = getpositions(ximage, yimage, isoarea, image)
    measurephot(xpos, ypos, image)
    iraf.digiphot()
    iraf.daophot()
    iraf.phot('check.fits',
              coords='noisecoords.dat',
              output='noise.dat',
              skyfile='sky',
              salgori='file',
              aperture='apertures',
              interactive='no',
              calgorith='none',
              verify='no')

    (aveap, aveaperr, avearea, aveareaerr) = calcavesky()
    (a, b) = solveforab(aveaperr, avearea)
    print "a,b", a, b
    s = str(image)
    (file, post) = s.split('.')
    #plotnoisepylab(aveap,aveaperr,avearea,aveareaerr,a,b,file,isoarea,fluxerriso)
    return a, b
Exemple #42
0
def measurephot(xpos, ypos, image):
    coords = open("noisecoords.dat", 'w')
    for i in range(len(xpos)):
        coords.write("%8.1f %8.1f \n" % (xpos[i], ypos[i]))
    sky = open("sky", 'w')
    for i in range(npoints):
        sky.write("0.0 \n")
    sky.close()
    aps = open("apertures", 'w')
    aps.write("1,2,3,4,5,6,7,8,9,10,11,12,13,14,15")
    aps.close()
    os.system("rm noise.dat")
    iraf.digiphot()
    iraf.daophot()
    print image
    image = "j-sky.fits"
    image = "n-sky.fits"
    iraf.digiphot.daophot.phot(image,
                               coords="noisecoords.dat",
                               output="noise.dat",
                               skyfile="sky",
                               salgori="file",
                               aperture="apertures",
                               interactive="no",
                               wcsin='logical')

    iraf.apphot()
    iraf.phot('check.fits',
              coords='noisecoords.dat',
              output='noise.dat',
              skyfile='sky',
              salgori='file',
              aperture='apertures',
              interactive='no')

    test = raw_input("enter anything")
Exemple #43
0
def p60photsub(ofile, inlis, refimage, refstars, ot, filter):

    refstars.set_mag(filter)
    coofile = open('ot.coo', 'w')
    ot.wcs2pix(refimage)
    coofile.write('%10.3f%10.3f\n' % (ot[0].xval, ot[0].yval))
    coofile.close()

    outfile = open(ofile, 'a+')

    p60photutils.p60zeropt(inlis, refstars, filter)

    subimage = iraffiles('@%s' % inlis)
    for image in subimage:

        root, ext = image.split('.')
        iraf.phot('%s.sub' % root,
                  coo='ot.coo',
                  output='%s.grb' % root,
                  interac=no)

        stars = Starlist('%s.mag' % root)
        refstars.wcs2pix(image)
        zp, zpu = stars.zeropt(refstars, method='mean', rejout=0)

        otstars = Starlist('%s.grb' % root)

        [mjd, utshut] = get_head(image, ['OBSMJD', 'UTSHUT'])

        if (len(stars) > 0):
            outfile.write('%s\t%s\t%.3f\t%.3f\t%.3f\t%.3f\n' %
                          (mjd, utshut, otstars[0].mag + zp,
                           sqrt(pow(zpu, 2) + pow(otstars[0].magu, 2)),
                           otstars[0].magu, zpu))

    outfile.close()
Exemple #44
0
import numpy as np
import astropy.io.fits as pyfits
from pyraf import iraf
from iraf import noao, digiphot, apphot, phot, txdump
import matplotlib
from matplotlib import pyplot as plt

iraf.noao.digiphot.apphot.phot.interac='no'
iraf.noao.digiphot.apphot.phot.centerp.calgori='none'
iraf.noao.digiphot.apphot.phot.fitskypars.salgori='constant'
iraf.noao.digiphot.apphot.phot.cache='yes'
iraf.noao.digiphot.apphot.phot.salgori='constant'

for band, aperture in zip(['f105w','f125w','f140w','f160w','f435w','f606w','f814w'], ['3.2','3.5','3.9','4.1','3.3','3.3','3.3']):
    iraf.noao.digiphot.apphot.phot.photpar.apertur=aperture
    iraf.phot(image='SEG_' + band + '_sex_hff_abells1063_par_CHECK.fits', interactive='no',calgorithm='none', salgori='constant',coords='box_coords_' + band + '.coo', output='PHOT_SEG_hff_abells1063_par_' + band + '.txt',verify='no')
    iraf.txdump(textfile='PHOT_SEG_hff_abells1063_par_' + band + '.txt', fields='XCEN, YCEN, SUM', expr='yes', Stdout='PHOT_SEG_hff_abells1063_par_' + band + '_OUTFILE.txt')
Exemple #45
0
def performPhotometry(task, logger):
  #iraf.prcacheOff()
  [iraf.unlearn(t) for t in ('phot','pstselect','psf','allstar')]
  iraf.set(imtype="fits,noinherit")   # set image output format
  iraf.set(clobber="yes")
  hdu=pyfits.open(task['images'])[0] 
  hdr = hdu.header
  imdata = hdu.data  
  for key,value in task['fits'].iteritems():
    task[key] = hdr.get(value,1)

  #Sextractor to find stars; add an object for the force detect
  logger.info('Running SExtractor on [%s]' % os.path.basename(task['images']))
  sex = sextractor.SExtractor()
  makeSexConfig(sex,task)
  sex.run(task['images'])
  catalog = sex.catalog()

  #Set up image parameters
  MIN_X = max(1,int(task['numpixx']*task['filtfactor']))
  MIN_Y = max(1,int(task['numpixy']*task['filtfactor']))
  MAX_X = int(task['numpixx']*(1-task['filtfactor']))
  MAX_Y = int(task['numpixy']*(1-task['filtfactor']))
  AREAXY = '[%s:%s,%s:%s]' % (MIN_X, MAX_X, MIN_Y, MAX_Y)
  AREANO = '[%s:%s,%s:%s]' % (MIN_X, MAX_X-2*MIN_X, MIN_Y, MAX_Y-2*MIN_Y)
  try:
    task['pixscale'] = abs(hdr.get('CD1_1'))*3600.
  except TypeError:
    task['pixscale'] = abs(hdr.get('CDELT1'))*3600.
  task['seeing'] = np.median( sorted([i['FWHM_IMAGE'] for i in catalog])[:int(-len(catalog)*0.5)] ) #Take the median of the "bottom" 50% of objects
  
  logger.info('--> %s SExtractor detected bright objects in the field' % (len(catalog),) )
  logger.info('--> %0.2f median FWHM of bright objects in the field, in arcsec' % (task['seeing']*task['pixscale'],))

  task['objects'] = [(i['ALPHA_J2000'],i['DELTA_J2000']) for i in catalog]
  task['objects'].append(task['objwcs'])  
  task['objectlist'] = open(os.path.join(task['output_directory'],'objectlist'),'w')  
  task['objectlist'].write('\n'.join([' %s %s' % (i[0],i[1]) for i in task['objects']]))
  task['objectlist'].close()

  logger.info('Running iraf.imstat')
  irafoutput = iraf.imstat(images=task['images']+AREANO,fields='midpt,min,max,stddev', format=0, Stdout=1)
  task['nimgs'] = hdr.get('NIMGS',1)
  task['gain'] *= task['nimgs']*2/3.
  task['ron'] *= np.sqrt(task['nimgs'])/task['nimgs']*constants.INTERPSM[task['band']]
  task['datamean'], task['datamin'], task['datamax'], task['datastdev']  = map(float, irafoutput[0].split())
  irafoutput = iraf.imstat(images=task['images'],fields='stddev,midpt',nclip=25,format=0,cache='yes',Stdout=1)
  task['skynoise'], task['datamean'] = map(float, irafoutput[0].split() )
  task['skynoise'] *= constants.INTERPSM[task['band']]
  task['airmass'] = hdr.get('AIRMASS',1)
  task['zmag'] -= (float(task['airmass'])-1.0)*constants.extinction_coefficients[task['band']]  
  task['match_proximity'] = 2.5 * task['seeing']
  logger.info('--> %5.2f counts: Sky noise, corrected for drizzle imcombine' % task['skynoise'])
  logger.info('--> %5.2f Median count value, after background subtraction' % task['datamean'])
  logger.info('--> %5.2f Airmass' % task['airmass'])

  #prepare temp files that iraf will use
  for filename in ('photfile','pstfile','psfimg','opstfile','groupfile','allstarfile','rejfile','subimage'): 
    task[filename] = open(os.path.join(task['output_directory'],filename),'w')
    task[filename].close()

  #iraf.phot to get APP magnitudes
  logger.info('Running iraf.apphot.phot')
  #apsizes = [i*task['faperture']*task['seeing'] for i in (0.4,0.5,0.6,0.8,1.0,1.2,1.5,2.0,2.5,3.0)]
  #irafapsizes = ','.join(['%.2f' % i for i in apsizes])
  irafapsizes = '%0.2f' % (task['faperture']*task['seeing'])
  kwargs = dict(image=task['images'],coords=task['objectlist'].name,
    output=task['photfile'].name,
    interac='no',scale=1,
    fwhmpsf=task['seeing'], 
    wcsin='world', wcsout='physical',
    sigma=task['skynoise'],
    datamin=task['datamin'],
    datamax=task['datamax'],
    readnoi=task['ron'],
    epadu=task['gain'],
    itime=task['exposure'],
    xairmass=task['airmass'],
    ifilter=task['band'],
    otime=task['dateobs'],
    aperture= irafapsizes,
    zmag=task['zmag'],
    annulus=task['fannulus']*task['seeing'],
    dannulus=task['fdannulus']*task['seeing'],
    calgorithm='gauss',
    cbox = 1.5*task['seeing'],
    maxshift=2.0*task['seeing'],
    mode="h",Stdout=1,verify=0)
  iraf.phot(**kwargs)

  if task['band'] not in constants.infrared:
    #iraf.pstselect to choose objects for PSF modelling
    logger.info('Running iraf.daophot.pstselect')
    kwargs = dict(image=task['images'],
                     photfile=task['photfile'].name,pstfile=task['pstfile'].name,
                     maxnpsf=task['pstnumber'],
                     wcsin='physical',
                     wcsout='physical',
                     interac="no",verify='no',scale=1,
                     fwhmpsf=task['seeing'],
                     datamin=0,
                     datamax=task['datamax'],
                     psfrad=3.0*task['seeing'],
                     fitrad=1.0*task['seeing'],
                     recente='yes',
                     nclean=task['nclean'],
                     mode="h",Stdout=1)
    iraf.pstselect(**kwargs)

    #iraf.psf to model PSF
    logger.info('Running iraf.daophot.psf')
    kwargs = dict( image=task['images'],
              photfile=task['photfile'].name,
              pstfile=task['pstfile'].name,
              psfimage=task['psfimg'].name,
              opstfile=task['opstfile'].name,
              groupfile=task['groupfile'].name,
              wcsin='physical',wcsout='physical',
              interac="no",verify="no",scale=1,
              fwhmpsf=task['seeing'],
              sigma=task['skynoise'],
              datamin=task['datamin'],
              datamax=task['datamax'],
              readnoi=task['ron'],
              epadu=task['gain'],
              itime=task['exposure'],
              xairmass=task['airmass'],
              ifilter=task['band'],
              otime=task['dateobs'],
              function=task['func'],
              varorder=task['varorder'],
              saturat='no',
              psfrad=3.0*task['seeing'],
              fitrad=1.*task['faperture']*task['seeing'],
              nclean=task['nclean'],
              mergerad=1.5*task['seeing'],
              mode='h',Stdout=1)  

    iraf.psf(**kwargs)
    logger.info('Running iraf.daophot.allstar')
    #iraf.allstars to compute PSF photometry; recenter with recenter='yes', mergerad=<value> to avoid duplicate detection
    kwargs = dict(image=task['images'],
                  photfile=task['photfile'].name,
                  wcsin='physical',
                  wcsout='physical',
                  psfimage=task['psfimg'].name,
                  allstarf=task['allstarfile'].name,
                  rejfile=task['rejfile'].name,
                  subimage=task['subimage'].name,
                  verbose=1,verify='no',scale=1,
                  fwhmpsf=task['seeing'],
                  sigma=task['skynoise'],
                  datamin=task['datamin'],
                  datamax=task['datamax'],
                  readnoi=task['ron'],
                  epadu=task['gain'],
                  itime=task['exposure'],
                  xairmass=task['airmass'],
                  ifilter=task['band'],
                  otime=task['dateobs'],
                  function=task['func'],
                  varorder=task['varorder'],
                  psfrad=3.*task['seeing'],
                  fitrad=1.*task['faperture']*task['seeing'],
                  recenter='yes',
                  mergerad=1.5*task['seeing'],
                  mode='h',Stdout=1)
    iraf.allstar(**kwargs)
  

  #Parse both photometry, convert to RA,DEC,MAG,MAGERR
  logger.info('iraf tasks complete. Parsing results and calibrating')
  photometry = {}
  photometry['APP'] = iraf.txdump(textfiles=task['photfile'].name,
                        fields='XCENTER,YCENTER,MAG,MERR',expr='yes',
                        headers='no',Stdout=1)

  if task['band'] not in constants.infrared:
    photometry['PSF'] = iraf.txdump(textfiles=task['allstarfile'].name,
                          fields='XCENTER,YCENTER,MAG,MERR',expr='yes',
                          headers='no',Stdout=1)


  for phototype in photometry:
    kwargs = dict(input='STDIN',
                  output='STDOUT',
                  insystem='%s physical' % task['images'],
                  outsystem='%s world' % task['images'],
                  ilatuni='physical',
                  ilnguni='physical',
                  olnguni='degrees',
                  olatuni='degrees',
                  ilngfor='%10.7f',
                  ilatfor='%10.7f',
                  olngfor='%10.5f',
                  olatfor='%10.5f',
                  Stdin=photometry[phototype],Stdout=1)
    photometry[phototype] = [i.split() for i in iraf.skyctran(**kwargs) if i and not i.startswith('#') and 'INDEF' not in i]
    photometry[phototype] = [map(float,(i[4],i[5],i[2],i[3])) for i in photometry[phototype] ] #Now we have [(ra,dec,'mag','mageerr'),...]
  results = calibrate((task['objwcs'][0],task['objwcs'][1]),task,photometry,logger)
#  if 'PSF' not in results:
    
  return results
Exemple #46
0
def measurephottest(inimage):
    if os.path.exists('noise.dat'):
        os.remove('noise.dat')
    iraf.phot(image=inimage,coords="noisecoords.dat",output="noise.dat",calgorith='none',skyfile="sky",salgori="file",aperture="apertures",interactive="no",verify='no',verbose='yes')
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'
    )
Exemple #48
0
def execute():

    #here we define the coords variable as a list
    coords=[]

    #indenting matters in python. Everything
    #indented under the for loop is inside the loop
    #pdb.set_trace() # break point for python debugger
    for line in open(coofnamein):
        if (line.startswith("#") or line.startswith("z1")):
            continue
        #this skips the lines that start with a #
        line=map(float,line.split())
        #this takes the line and splits it into a list of floats
        if len(line) != 4:
            continue
        #skips the lines that don't have 4 floats
        coords.append((line[0],line[1]))
        #adds the most recent coords (the 0th and 1st elements
        # to the list of coordinates

        #print coords

    coofnameout1=open(coofnameout, "w")

    for ci in coords:
        coofnameout1.write("%.4g\t%.4g\n" % ci)
        #the strange syntax is the formatting for the
        #output. It prints each coordinate with 4 decimal places
        # a tab in between, and then a new line at the end
    coofnameout1.close()
    #sys.exit(1)

    l=os.listdir(image_dir)
    l=[li for li in l if li.endswith(endstr)]
    l=[li for li in l if li.startswith(rootstr)]
    l =[image_dir + li for li in l]
    #this list takes all the files in the directory,
    #then only keeps those with the correct beginning and ending

    #l = l[:10]
    #this line can tell the code to only run on the first 10 images
    #useful for debugging

    lc_out1 = open(lc_out,"w")
    for li in l:
        print li
        #print coofnameout
        #print phot_out
        iraf.phot(li,coofnameout, phot_out, plotfile="", datapars="", centerpars="", fitskypars="", photpars="",interactive="no", radplots="no", icommands="", gcommands="", verify="no", weighting="constant", calgorithm="centroid", obstime=obstime, annulus=annulus,dannulus=dannulus,skyvalue=skyvalue,fwhmpsf=fwhmpsf,sigma=sigma)
        #this actually does the photometry
        #note all of the parameters that must be defined

        standard_output = sys.stdout
        #saves the current output -ie to the screen
        txdumpfile = open(txdumpout, 'w')
        sys.stdout = txdumpfile
        #directs the output to the txdumpfile

        iraf.txdump(phot_out, "otime,mag,merr", "yes", headers="no", parameters="no")
        #does the txdump - might you want to output other information?

        sys.stdout = standard_output # restores the stdout
        txdumpfile.close() # close the file
        os.remove(phot_out)
        #deletes the file

        phots  =[]

        for line in open(txdumpout):
            #line=map(string,line.split())
            line=str.split(line)
            phots.append((line[1],line[2]))
            #print phots

        #print line[0]

        #lc_out1.write(line[0] + "\t")
        #print stringtohours(line[0])
        lc_out1.write( "%10.7f \t" % (stringtohours(line[0])))


        for photsi in phots:
            lc_out1.write(photsi[0] + "\t")
            lc_out1.write(photsi[1] + "\t")
        lc_out1.write("\n")

        os.remove(txdumpout)
    iraf.fitskypars.skyvalu = bg
    iraf.fitskypars.annulus = 4 * fwhm
    iraf.fitskypars.dannulus = 3 * fwhm
    iraf.photpars.aperture = max(3, fwhm)
    iraf.photpars.zmag = zmag
    iraf.daopars.fitrad = fwhm  # Fitting radius in scale units. Only pixels within the fitting radius of the center of a star will contribute to
    # the fits computed by the PEAK, NSTAR and ALLSTAR tasks.

    ##### Get Coordiates and Magnitudes Stars #################
    # Run DAOFIND to get coordinates and PHOT to get magnitudes
    ###########################################################
    iraf.daofind(fn, fn[:-5] + '.coo.1', verify='no',
                 verbose='no')  # generate coo.1 file
    iraf.phot(fn,
              fn[:-5] + '.coo.1',
              fn[:-5] + '.mag.1',
              skyfile='bkg_' + fn,
              verify='no',
              verbose='no')  # generate mag.1 file

    ##### MAKE PSF STAR LIST FILE 'pst.1' #######
    # read coordinates and magnitudes of the stars selected based
    # on the catalog generated by Source Extractor and make
    # 'pst.1' file to use for running PSF task
    #######################################################
    coo1 = ascii.read(fn[:-5] + '.coo.1')
    mag1 = ascii.read(fn[:-5] + '.mag.1')
    coo1.sort('ID')
    mag1.sort('ID')
    pst1 = Table(names=('ID', 'XCENTER', 'YCENTER', 'MAG', 'MSKY'),
                 dtype=('i4', 'f8', 'f8', 'f8', 'f8'))
    for i in range(min(num_psf_stars, len(stars))):
Exemple #50
0
def run(imfile,
        coordlist,
        WCS=False,
        outroot=None,
        poisson=True,
        verbose=True,
        debug=False,
        abmags=False,
        decliner=False,
        smallskyann=False,
        calgorithm='gauss',
        upperlim=None,
        snanadat=False,
        cbox=5.0):
    """
    Use iraf.digiphot.apphot to collect aperture photometry in Vega magnitudes.

    Required Arguments:
        imfile (str):  fits image file name (cannot be fpacked)
        coord : the coordinate list for photometry
          as a str, this is the name of a text file in two columns giving x,y coordinates.
          as a python list or numpy array, this gives coordinate pairs for each target
             e.g.  coord=[ [1024,440], [502,680] ]

    Optional arguments:
        WCS : If input coordinates are in RA and DEC  (degrees), user must set WCS=True.
        outroot :  root of the output file names (outroot.mag, outroot.full, outroot.out)
            if unspecified, the root of  imfile  is used.
        poisson: Use a poisson noise model or not. If set to false, a constant noise model is
                used. Default is True
        calgorithm: Centering algorithm to pass to iraf.apphot. Choices are "gauss","centroid",
                    "none", or "ofilter". Default is "gauss"
        cbox: Size of centering box to pass to iraf.apphot. Default is 5.0
        upperlim:  None = when flux<3-sigma then report 3-sig upper limit
                   True = force computation of 3-sigma upper limit
                   False = disallow upper limits (i.e. always report measured flux)
        snanadat:  True = Report the mjd, flux and mag in a SNANA-style OBS: line
                   False = report the filename, mjd, source position, mag and errors.
        verbose: Default is True.
        debug: Start pdb. Default is False.

    Output products :  phot.out, phot.mag, phot.full
        phot.out : the raw output from apphot
        phot.full :  a detailed photometry file
        phot.mag :  header+one line summary output file :
              #image name, filter, xpos, ypos, magnitude, and errors

    Requires :  numpy, pyfits  (astLib is required if WCS=True)
    """
    if debug:
        import pdb
        pdb.set_trace()
    from math import sqrt
    from numpy import nan, log10
    from pyraf import iraf

    iraf.noao(_doprint=0)
    iraf.digiphot(_doprint=0)
    iraf.apphot(_doprint=0)

    if outroot == None: outroot = os.path.splitext(os.path.basename(imfile))[0]

    # Get info from header
    hdr = pyfits.getheader(imfile)
    exptime = hdr['EXPTIME']
    if 'EXPSTART' in hdr:
        mjdobs = "%.1f" % hdr['EXPSTART']
    else:
        mjdobs = '0.0'

    if 'DATE-OBS' in hdr:
        dateobs = hdr['DATE-OBS']
    elif 'DATE' in hdr:
        dateobs = hdr['DATE']
    else:
        dateobs = 'unknown'

    if 'D001OUUN' in hdr:
        if hdr['D001OUUN'].upper() == 'CPS':
            gain = exptime
            exptime = 1.0
        elif hdr['D001OUUN'].upper() == 'COUNTS':
            gain = 1.0
    elif hdr['BUNIT'].upper() == 'ELECTRONS/S':
        gain = exptime
        exptime = 1.0
    elif hdr['BUNIT'].upper() == 'ELECTRONS':
        gain = 1.0

    else:
        print 'Problem determining units, check image'
        return

    if 'FILTER' in hdr:
        filt = hdr['FILTER']
    elif 'FILTER1' in hdr:
        filt = hdr['FILTER1']

    if filt.startswith('CLEAR'):
        filt = hdr['FILTER2']

    instrument = hdr['INSTRUME']
    if instrument == 'WFC3':
        instrument = instrument + '_' + hdr['DETECTOR']

    if abmags:
        if instrument == 'WFC3_IR':
            ZPT = ZPT_WFC3_IR_AB[filt]
        elif instrument == 'WFC3_UVIS':
            ZPT = ZPT_WFC3_UVIS_AB[filt]
        elif instrument == 'ACS':
            ZPT = ZPT_ACS_AB[filt]
        else:
            print "Can't handle instrument: %s; check the fits header." % instrument
            #Give up and die
            return
    else:
        if instrument == 'WFC3_IR':
            ZPT = ZPT_WFC3_IR_VEGA[filt]
        elif instrument == 'WFC3_UVIS':
            ZPT = ZPT_WFC3_UVIS_VEGA[filt]
        elif instrument == 'ACS':
            ZPT = ZPT_ACS_VEGA[filt]
        else:
            print "Can't handle instrument: %s; check the fits header." % instrument
            #Give up and die
            return

    # format for coord list:
    #    coordlist= [ [x0,y0], [x1,y1], ... ]
    #    or a two-column text file
    if type(coordlist) == str:
        # user provided name of a coordinate file
        fin = open(coordlist, 'r')
        coordlines = fin.readlines()
        fin.close()
        coordvals = array([cline.split() for cline in coordlines], dtype=float)
    elif len(shape(coordlist)) == 1:
        # user provided something like coord=[x,y]
        coordvals = array([coordlist])
    else:
        # user provided something like coord= [[x1,y1],[x2,y2],[x3,y3]]
        coordvals = array(coordlist)

    # how many objects do we have ?
    numcoo = len(coordvals)

    if WCS:
        # If coords in wcs instead of x,y,
        # get wcs information from imfile for converting to xy.
        from astLib import astWCS
        wcsfits = astWCS.WCS(imfile)

    # (re)write a list of x,y positions
    coxyfile = '%s.xycoo' % outroot
    coxy = open(coxyfile, 'w')
    for coord in coordvals:
        if WCS:
            #Convert from RA and Dec to xy
            # NOTE: wcsfits returns values based on a 0,0 origin, but the iraf phot
            # packages expect a 1,1 origin. So we add 1 to each value
            xy = wcsfits.wcs2pix(coord[0], coord[1])
            xy[0] += 1
            xy[1] += 1
        else:
            xy = coord
        print >> coxy, "%10.2f  %10.2f" % (float(xy[0]), float(xy[1]))
    coxy.close()
    if verbose > 1: print("XY coords written to %s" % coxyfile)
    """ iraf.digiphot.apphot.datapars :
    2013.09.06 SR: updated to use Poisson noise model (which includes sky noise) as 
    the default, instead of 'constant' """
    iraf.unlearn(iraf.apphot.phot)
    iraf.unlearn(iraf.datapars)
    iraf.datapars.scale = 1.0
    iraf.datapars.fwhmpsf = psffwhm[instrument]
    iraf.datapars.emission = not decliner
    iraf.datapars.sigma = 'INDEF'
    iraf.datapars.datamin = 'INDEF'
    iraf.datapars.datamax = 'INDEF'

    if poisson:
        iraf.datapars.noise = 'poisson'
    else:
        iraf.datapars.noise = 'constant'

    iraf.datapars.ccdread = ''
    #iraf.datapars.gain = ''
    iraf.datapars.readnoise = 0.0
    #iraf.datapars.exposure = ' '
    #iraf.datapars.airmass = ''
    #iraf.datapars.obstime = ''
    iraf.datapars.itime = exptime
    iraf.datapars.epadu = gain
    iraf.datapars.xairmass = 'INDEF'
    iraf.datapars.ifilter = 'INDEF'
    iraf.datapars.otime = 'INDEF'

    # iraf.digiphot.apphot.centerpars :
    iraf.unlearn(iraf.centerpars)

    iraf.centerpars.calgorithm = calgorithm
    iraf.centerpars.cbox = cbox

    iraf.centerpars.cthreshold = 0.0
    iraf.centerpars.minsnratio = 1.0
    iraf.centerpars.cmaxiter = 10.0
    iraf.centerpars.maxshift = 1.0
    iraf.centerpars.clean = False
    iraf.centerpars.rclean = 1.0
    iraf.centerpars.rclip = 2.0
    iraf.centerpars.kclean = 3.0
    iraf.centerpars.mkcenter = False

    # iraf.digiphot.apphot.fitskypars :
    iraf.unlearn(iraf.fitskypars)
    iraf.fitskypars.salgorithm = 'median'
    if smallskyann:
        iraf.fitskypars.annulus = 8.0
        iraf.fitskypars.dannulus = 12.0
    else:
        iraf.fitskypars.annulus = 25.0
        iraf.fitskypars.dannulus = 40.0
    iraf.fitskypars.skyvalue = 0.0
    iraf.fitskypars.smaxiter = 10.0
    iraf.fitskypars.sloclip = 0.0
    iraf.fitskypars.shiclip = 0.0
    iraf.fitskypars.snreject = 50.0
    iraf.fitskypars.sloreject = 3.0
    iraf.fitskypars.shireject = 3.0
    iraf.fitskypars.khist = 3.0
    iraf.fitskypars.binsize = 0.1
    iraf.fitskypars.smooth = False
    iraf.fitskypars.rgrow = 0.0
    iraf.fitskypars.mksky = False

    # iraf.digiphot.apphot.photpars :
    iraf.unlearn(iraf.photpars)
    iraf.photpars.weighting = 'constant'
    iraf.photpars.apertures = APLIST
    iraf.photpars.zmag = ZPT
    iraf.photpars.mkapert = False

    photparams = {
        'interac': False,
        'radplot': False,
    }

    magfile_out = outroot + '.out'
    magfile_full = outroot + '.full'
    magfile_phot = outroot + '.mag'

    if os.path.exists(magfile_out):
        os.remove(magfile_out)

    # run photometry using the newly created coxyfile for providing input coordinates
    try:
        iraf.phot(image=imfile,
                  skyfile='',
                  coords=coxyfile,
                  output=magfile_out,
                  verify=False,
                  verbose=True,
                  Stdout=1,
                  **photparams)
    except iraf.IrafError, e:
        print("phot failed on %s with IRAF error :\n%s" % (imfile, e))
Exemple #51
0
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)
Exemple #52
0
def phot(fits_filename,
         x_in,
         y_in,
         aperture=15,
         sky=20,
         swidth=10,
         apcor=0.3,
         maxcount=30000.0,
         exptime=1.0,
         zmag=None,
         extno=0,
         centroid=True):
    """
    Compute the centroids and magnitudes of a bunch sources  on fits image.

    :rtype : astropy.table.Table
    :param fits_filename: Name of fits image to measure source photometry on.
    :type fits_filename: str
    :param x_in: x location of source to measure
    :type x_in: float, numpy.array
    :param y_in: y location of source to measure
    :type y_in: float, numpy.array
    :param aperture: radius of circular aperture to use.
    :type aperture: float
    :param sky: radius of inner sky annulus
    :type sky: float
    :param swidth: width of the sky annulus
    :type swidth: float
    :param apcor: Aperture correction to take aperture flux to full flux.
    :type apcor: float
    :param maxcount: maximum linearity in the image.
    :type maxcount: float
    :param exptime: exposure time, relative to zmag supplied
    :type exptime: float
    :param zmag: zeropoint magnitude
    :param extno: extension of fits_filename the x/y location refers to.
    """
    if not hasattr(x_in, '__iter__'):
        x_in = [
            x_in,
        ]
    if not hasattr(y_in, '__iter__'):
        y_in = [
            y_in,
        ]

    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:
        logger.debug(str(err))
        raise TaskError("Failed to open input image: %s" % err.message)

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

    # Some nominal CFHT zeropoints that might be useful
    zeropoints = {
        "I": 25.77,
        "R": 26.07,
        "V": 26.07,
        "B": 25.92,
        "DEFAULT": 26.0,
        "g.MP9401": 32.0,
        'r.MP9601': 31.9,
        'gri.MP9603': 33.520
    }

    if zmag is None:
        logger.warning(
            "No zmag supplied to daophot, looking for header or default values."
        )
        zmag = input_hdulist[extno].header.get('PHOTZP',
                                               zeropoints[filter_name])
        logger.warning("Setting zmag to: {}".format(zmag))
        # check for magic 'zeropoint.used' files
        for zpu_file in [
                "{}.zeropoint.used".format(os.path.splitext(fits_filename)[0]),
                "zeropoint.used"
        ]:
            if os.access(zpu_file, os.R_OK):
                with open(zpu_file) as zpu_fh:
                    zmag = float(zpu_fh.read())
                    logger.warning("Using file {} to set zmag to: {}".format(
                        zpu_file, zmag))
                    break

    photzp = input_hdulist[extno].header.get(
        'PHOTZP', zeropoints.get(filter_name, zeropoints["DEFAULT"]))
    if zmag != photzp:
        logger.warning(
            ("zmag sent to daophot: ({}) "
             "doesn't match PHOTZP value in image header: ({})".format(
                 zmag, photzp)))

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

    iraf.photpars.apertures = aperture
    iraf.photpars.zmag = zmag
    iraf.datapars.datamin = 0
    iraf.datapars.datamax = maxcount
    iraf.datapars.exposur = ""
    iraf.datapars.itime = exptime
    iraf.fitskypars.annulus = sky
    iraf.fitskypars.dannulus = swidth
    iraf.fitskypars.salgorithm = "mode"
    iraf.fitskypars.sloclip = 5.0
    iraf.fitskypars.shiclip = 5.0
    if centroid:
        iraf.centerpars.calgori = "centroid"
        iraf.centerpars.cbox = 5.
        iraf.centerpars.cthreshold = 0.
        iraf.centerpars.maxshift = 2.
        iraf.centerpars.clean = 'no'
    else:
        iraf.centerpars.calgori = "none"
    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)

    for i in range(len(x_in)):
        coofile.write("%f %f \n" % (x_in[i], y_in[i]))
    coofile.flush()
    # 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 target_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 + "[{}]".format(extno), coofile.name, magfile.name)
    pdump_out = ascii.read(magfile.name, format='daophot')
    logging.debug("PHOT FILE:\n" + str(pdump_out))
    if not len(pdump_out) > 0:
        mag_content = open(magfile.name).read()
        raise TaskError("photometry failed. {}".format(mag_content))

    # apply the aperture correction
    pdump_out['MAG'] -= apcor

    # if pdump_out['PIER'][0] != 0 or pdump_out['SIER'][0] != 0 or pdump_out['CIER'][0] != 0:
    #    raise ValueError("Photometry failed:\n {}".format(pdump_out))

    # Clean up temporary files generated by IRAF
    os.remove(coofile.name)
    os.remove(magfile.name)
    logger.debug("Computed aperture photometry on {} objects in {}".format(
        len(pdump_out), fits_filename))

    del input_hdulist
    return pdump_out
Exemple #53
0
def psffit(img, fwhm, psfstars, hdr, interactive, _datamax, psffun='gauss', fixaperture=False):
    ''' 
    giving an image, a psffile compute the psf using the file _psf.coo
    '''
    import lsc
    _ron = lsc.util.readkey3(hdr, 'ron')
    _gain = lsc.util.readkey3(hdr, 'gain')
    if not _ron:
        _ron = 1
        print 'warning ron not defined'
    if not _gain:
        _gain = 1
        print 'warning ron not defined'

    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)
    zmag = 0.
    varord = 0  # -1 analitic 0 - numeric

    if fixaperture:
        print 'use fix aperture 5 8 10'
        hdr = lsc.util.readhdr(img+'.fits')
        _pixelscale = lsc.util.readkey3(hdr, 'PIXSCALE')
        a1, a2, a3, a4, = float(5. / _pixelscale), float(5. / _pixelscale), float(8. / _pixelscale), float(
                    10. / _pixelscale)
    else:
        a1, a2, a3, a4, = int(fwhm + 0.5), int(fwhm * 2 + 0.5), int(fwhm * 3 + 0.5), int(fwhm * 4 + 0.5)

    iraf.fitskypars.annulus = a4
    iraf.fitskypars.salgori = 'mean'  #mode,mean,gaussian
    iraf.photpars.apertures = '%d,%d,%d' % (a2, a3, a4)
    iraf.datapars.datamin = -100
    iraf.datapars.datamax = _datamax
    iraf.datapars.readnoise = _ron
    iraf.datapars.epadu = _gain
    iraf.datapars.exposure = 'EXPTIME'
    iraf.datapars.airmass = ''
    iraf.datapars.filter = ''
    iraf.centerpars.calgori = 'centroid'
    iraf.centerpars.cbox = a2
    iraf.daopars.recenter = 'yes'
    iraf.photpars.zmag = zmag

    iraf.delete('_psf.ma*,' + img + '.psf.fit?,_psf.ps*,_psf.gr?,_psf.n*,_psf.sub.fit?', verify=False)
    iraf.phot(img+'[0]', '_psf.coo', '_psf.mag', interac=False, verify=False, verbose=False)

    # removes saturated stars from the list (IRAF just issues a warning)
    with open('_psf.mag') as f:
        text = f.read()
    text = re.sub('(.*\n){6}.*BadPixels\* \n', '', text)
    with open('_psf.mag', 'w') as f:
        f.write(text)

    iraf.daopars.psfrad = a4
    iraf.daopars.functio = psffun
    iraf.daopars.fitrad = a1
    iraf.daopars.fitsky = 'yes'
    iraf.daopars.sannulus = a4
    iraf.daopars.recenter = 'yes'
    iraf.daopars.varorder = varord

    if interactive: # not possible to run pstselect or psf interactively on 64-bit linux (Error 851)
        shutil.copyfile('_psf.mag', '_psf.pst')
        print '_' * 80
        print '>>> Mark good stars with "a" or "d"-elete. Then "f"-it,' + \
              ' "w"-write and "q"-uit (cursor on ds9)'
        print '-' * 80
    else:
        iraf.pstselect(img+'[0]', '_psf.mag', '_psf.pst', psfstars, interac=False, verify=False)

    iraf.psf(img + '[0]', '_psf.mag', '_psf.pst', img + '.psf', '_psf.psto', '_psf.psg', interac=interactive,
             verify=False, verbose=False)
    iraf.group(img + '[0]', '_psf.mag', img + '.psf', '_psf.grp', verify=False, verbose=False)
    iraf.nstar(img + '[0]', '_psf.grp', img + '.psf', '_psf.nst', '_psf.nrj', verify=False, verbose=False)

    photmag = iraf.txdump("_psf.mag", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1)
    pst = iraf.txdump("_psf.pst", 'xcenter,ycenter,id', expr='yes', Stdout=1)
    fitmag = iraf.txdump("_psf.nst", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1)
    return photmag, pst, fitmag
Exemple #54
0
	def runApperturePhotometryOnObject(self, photObject, write=False):

		# Aperture sizes
                #fap = 1.0
                #fdan = 2.0
                #fan = 3.0

		# To take photometry of a given object using DAOPHOT/APPHOT.PHOT

		# Check we have an object list
		coordfilename = self._Name.replace(".fits", "objectID_%s.coo" % photObject._id)
		coordfilename2 = self._Name.replace(".fits", "_apphot_%s.reg" % photObject._Name.replace(" ",""))

		try:
			regionOut = open(coordfilename2, "w")
			regionOut.write("image; circle(%s, %s, %s) # color = red\n"% (photObject._pixelx, photObject._pixely, photObject._fap))
			regionOut.write("image; circle(%s, %s, %s) # color = blue\n" % (photObject._pixelx, photObject._pixely, photObject._fdan))
			regionOut.write("image; circle(%s, %s, %s) # color = green\n" % (photObject._pixelx, photObject._pixely, photObject._fan))
			regionOut.close()
		except:
			print "ERROR opening region file to write"
			print sys.exc_info()[0]

		try:
			objectList = open(coordfilename,"r")
			objectList.close()
					
		except:
			objectList = open(coordfilename,"w")
			objectList.write("%s %s" % (photObject._pixelx, photObject._pixely))
			objectList.close()

                # Load as in IRAF and neglect output:
                # NOAO
                # DIGIPHOT
                # APPHOT
                # DAOFIND
                iraf.noao(_doprint=0)
                iraf.digiphot(_doprint=0)
                iraf.apphot(_doprint=0)		

		# DAOPHOT/APPHOT - PHOT Package
		# Parameters:
		#
		# image
		# skyfile???
		# coords - input file
		# output - output file
		# datapars
		# centerpars
		# fitskypars
		# photpars

		cwd = os.getcwd()
		# datapars:
		datapars = "%s/uparm/datapars.par" % cwd
		#datapars = "uparm/datapars.par"
		#iraf.datapars.saveParList(filename=datapars)
		# centerpars:
		
		centerpars = "%s/uparm/centerpars.par" % cwd
		#centerpars = "uparm/centerpars.par"
#		iraf.centerpars.saveParList(filename=centerpars)
		# fitskypars:
		fitskypars = "%s/uparm/fitskypars.par" % cwd
		#fitskypars = "uparm/fitskypars.par"
#		iraf.fitskypars.saveParList(filename=fitskypars)
		# photpars:
		photpars = "%s/uparm/photpars.par" % cwd
		#photpars = "uparm/photpars.par"
#		iraf.photpar.saveParList(filename=photpars)


		#iraf.datapar(sigma=1.5, exposure='IMGEXP', gain='GAIN', ccdread='RON')

		# standard
		#iraf.phot.setParam('image', self._Name)
		#iraf.phot.setParam("output", self._Name.replace(".fits", "_phot.mag"))
		#iraf.phot.setParam("datapar", datapars)
		#iraf.phot.setParam("centerp", centerpars)
		#iraf.phot.setParam("fitskyp", fitskypars)
		#iraf.phot.setParam("photpar", photpars)
		#iraf.phot.setParam("interac", "no")
		#iraf.phot.setParam("verify", "yes")

		# co-ordinate file
		iraf.phot.setParam("coords", coordfilename)

		# run phot
		apphotpar = "%s/uparm/phot.par" % cwd
#		iraf.phot.saveParList(filename=apphotpar)

		output = self._Name.replace(".fits", "_objectID%s_phot.mag" % photObject._id)
		try:
			self.cleanOutputFiles(output)
		except:
			print "Error cleaning failed"
			print sys.exc_info()[0]

		#apsizes= (.4*fap*self._MEDFWHM,.5*fap*self._MEDFWHM,.6*fap*self._MEDFWHM,.8*fap*self._MEDFWHM,
	        #        	1.*fap*self._MEDFWHM,1.2*fap*self._MEDFWHM, 1.5*fap*self._MEDFWHM,2.*fap*self._MEDFWHM,
	        #		2.5*fap*self._MEDFWHM,3.*fap*self._MEDFWHM
		#		)
		#irafapsizes = '%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f' % apsizes
		#irafapsizes = '%.2f' % (5*apsizes[4])

		self.loadHeader()
		if self._Band in ["J", "H", "K"]:
			ifilter = self.getHeader("FILTER")
		else:
			ifilter = self.getHeader("SUBSET")

		print "FILTER: %s" % ifilter
                print "RON: %s" % self.getHeader("RON")
                print "GAIN: %s" % self.getHeader("GAIN")
                print "EXPTIME: %s" % self.getHeader("EXPTIME")
                print "AIRMASS: %s" % self.getHeader("AIRMASS")
                print "DATE: %s" % self.getHeader("DATE-OBS")
                print "ZP: %s" % self._ZPDICT[self.getHeader("FILTER")]
		try:
			EPADU = (2/3)*self.getHeader("NIMGS")*self.getHeader("GAIN")
			print "Keyword used: NIMGS"
		except:
			EPADU = (2/3)**self.getHeader("NCOMBINE")*self.getHeader("GAIN")
			print "Keyword uparmsed: NCOMBINE"

		iraf.phot(image=self._Name,
				coords=coordfilename, 
				output=output,
				verify=0,
				verbose=1,
				interac="no",
				scale=1,
				fwhmpsf=self._MEDFWHM,
				sigma=self._skySTDEV,
				wcsin="logical",
				wcsout="logical",
				datamin=-100,
				datamax="INDEF",
				zmag=self._ZPDICT[self.getHeader("FILTER")],	# to do: make function to calculate
				annulus=photObject._fan,
				dannulus=photObject._fdan,
				calgorithm="none",
				aperture=photObject._fap,
				cbox=1.5*self._MEDFWHM,
				maxshift=15,
				mode="ql",
				Stdout=1,
		 		readnoi=self.getHeader("RON"),
				epadu=EPADU,
				itime=self.getHeader("EXPTIME"),
				xairmass=self.getHeader("AIRMASS"),
				ifilter=ifilter,
				otime=self.getHeader("DATE-OBS"),
				salgori="mode",
				#skyvalu=0,
				smaxite=1
				)

		# Parse the output
		photout = open(output, "r")
		photoObjectProperties = photout.readlines() 
		photout.close()
		
		propList = []
		for Properties in photoObjectProperties:
			if Properties[0] != "#":
				Property = [i for i in Properties.replace("\n","").replace("\\","").split(" ") if i != ""]
				propList.append(Property)

		try:
			photObject._skyFlux = float(propList[3][0])
		except:
                        photObject._skyFlux = (propList[3][0])
		try:			
			photObject._flux = float(propList[4][1])
		except:
                        photObject._flux = (propList[4][1])

		try:
			photObject._appMag = float(propList[4][4])
		except:
                        photObject._appMag = (propList[4][4])

		try:
			photObject._appMagErr = float(propList[4][5])
		except:
                        photObject._appMagErr = (propList[4][5])

		photObject._midMJD, photObject._midMJDErr = self.getMidMJD()#seconds=True,zero=55822.89371528)
Exemple #55
0
def run_daophot(image, outfile='default', coordfile='NA', backmethod='mean', backval=None, backsigma=None,rdnoise=None,\
                apertures='1,2,3,4,5,6,7,8,9,10,12,14,16,18,20,24,28,32,36,40,45,50,55,60,65,70', cbox=3.0, \
         annulus=17.0, dannulus=3.0, calgorithm='centroid', salgorithm='median', fwhmpsf=2.5, epadu=1.0):
    '''THIS PROCEDURE RUNS DAOPHOT ON INPUT IMAGE'''

    # Parse input parameters
    if outfile == 'default': outfile = image + '0.mag'
    if coordfile == 'NA': coordfile = image + '0.coo'

    # -- extract header info
    prihdr = pyfits.getheader(image)
    exptime = prihdr['exptime']
    instrum = prihdr['INSTRUME']
    detector = prihdr['DETECTOR']
    SUBARRAY = prihdr['SUBARRAY']
    ccdamp = prihdr['CCDAMP']

    if instrum == 'WFC3': filter = prihdr['FILTER']
    elif instrum == 'ACS':
        filter = prihdr['FILTER1']
        if filter[0] == 'C': filter == prihdr['FILTER2']
    else:
        raise Exception('Instrument ' + instrum +
                        ' not covered in our case list.')

    # -- record native pixel scale and no. of chips
    if instrum == 'WFC3':
        if detector == 'UVIS':
            pscale_nat = 0.03962
            if ((SUBARRAY == True) & (len(ccdamp) == 1)): nchips = 1.0
            elif SUBARRAY == False: nchips = 2.0
            else: raise Exception('Image type is not defined.')
        elif detector == 'IR':
            pscale_nat = 0.12825
            nchips = 1.0
        else:
            raise Exception('WFC3 Detector ' + detector +
                            ' not covered in our case list.')
    elif instrum == 'ACS':
        if detector == 'WFC':
            pscale_nat = 0.049
            if ((SUBARRAY == True) & (len(ccdamp) == 1)): nchips = 1.0
            elif SUBARRAY == False: nchips = 2.0
            else: raise Exception('Image type is not defined.')
        else:
            raise Exception('ACS Detector ' + detector +
                            ' not covered in our case list.')
    else:
        raise Exception('Instrument ' + instr +
                        ' not covered in our case list.')

    # -- record pixel scale of current image, image type, image axis lengths, and number of flts
    sciext = []
    pscale_img = prihdr.get('D001SCAL', default='NA')
    if pscale_img == 'NA':
        imtype = 'flt'  # we dont distinguish between flt/crclean, i.e., assume pscales are equal
        pscale_img = pscale_nat
        num_flts = 1.0
        naxis1 = pyfits.getval(image, 'NAXIS1', ext=('SCI', 1))
        naxis2 = pyfits.getval(image, 'NAXIS2', ext=('SCI', 1))
        # -- record location of science extension
        hdulist = pyfits.open(image)
        for ext in xrange(len(hdulist)):
            if hdulist[ext].name == 'SCI': sciext.append(ext)
        hdulist.close()
        if len(sciext) != 1:
            raise Exception('We do not handle images with ' +
                            str(len(sciext)) + ' SCI extensions.')
    else:
        imtype = 'drz'
        num_flts = prihdr['NDRIZIM'] / nchips
        naxis1 = pyfits.getval(image, 'NAXIS1', ext=0)
        naxis2 = pyfits.getval(image, 'NAXIS2', ext=0)
        sciext.append(0)

    # -- get zeropoints
    if instrum == 'WFC3': zeropt = get_wfc3_zeropoint(filter)
    elif instrum == 'ACS' and imtype == 'drz':
        zeropt = get_acs_zeropoint(prihdr)
    elif instrum == 'ACS' and imtype == 'flt':
        zeropt = get_acs_zeropoint(pyfits.getheader(image, ext=('SCI', 1)))

    # -- estimate read noise
    if rdnoise == None:
        rdnoise = np.zeros(len(ccdamp))
        for namp in xrange(len(ccdamp)):
            rdnoise[namp] = prihdr['READNSE' + ccdamp[namp]]
    rdnoise_corr = np.sqrt(num_flts *
                           (np.average(rdnoise) * pscale_img / pscale_nat)**2)

    # -- measure the background and noise
    if ((backval == None) | (backsigma == None)):
        # -- read in the x/y center of the source
        xc, yc = np.loadtxt(coordfile, unpack=True, usecols=(0, 1))

        # -- create temporary image for bckgrd measurement that masks sources out to 80 pixels (assign a very low number)
        tmp_image = image + '.back.fits'
        shutil.copy(image, tmp_image)
        hdulist = pyfits.open(tmp_image, mode='update')
        maskim = hdulist[sciext[0]].data
        if detector == 'IR': maskrad = 30
        else: maskrad = 80
        maskim[circular_mask(maskim.shape,
                             maskrad,
                             x_offset=(xc - naxis1 / 2.0),
                             y_offset=(yc - naxis2 / 2.0))] = -99999.0

        # -- Also mask out sources with zero effective exposure [WE ELIMINATE PIXELS WITHIN 20 OF IMAGE BORDER]
        maskim[:, 0:20] = -99999.0
        maskim[:, -20:] = -99999.0
        maskim[0:20, :] = -99999.0
        maskim[-20:, :] = -99999.0

        # -- generate initial guess for lower/upper limits (use 10 sigma)
        fmaskim = np.ndarray.flatten(maskim)
        llim = -100
        ulim = 10000.0
        init_median, init_rms = meanclip(fmaskim[(fmaskim > llim)
                                                 & (fmaskim < ulim)],
                                         maxiter=7,
                                         return_median=1)
        llim = init_median - 10.0 * init_rms
        ulim = init_median + 10.0 * init_rms

        # -- measure background and rms
        if backmethod.lower() == 'mean':
            back, backrms = meanclip(fmaskim[(fmaskim > llim)
                                             & (fmaskim < ulim)],
                                     maxiter=7)
        elif backmethod.lower() == 'median':
            back, backrms = meanclip(fmaskim[(fmaskim > llim)
                                             & (fmaskim < ulim)],
                                     maxiter=7,
                                     return_median=1)
        elif backmethod.lower() == 'mode':
            backmean, backrms = meanclip(fmaskim[(fmaskim > llim)
                                                 & (fmaskim < ulim)],
                                         maxiter=7)
            nbins = np.ceil(80.0 / (0.1 * backrms))
            cc, bb, pp = pylab.hist(fmaskim[(fmaskim > llim)
                                            & (fmaskim < ulim)],
                                    log=True,
                                    bins=nbins,
                                    range=(-40.0, 40.0))
            back = bb[cc.argmax()] + (bb.max() - bb.min()) / (2.0 *
                                                              (len(bb) - 1))
        else:
            raise Exception('Background statistical method ' + backmethod +
                            ' is not covered in our case list.')

        if backval == None: backval = back
        if backsigma == None: backsigma = backrms

        print '\n BACKGROUND =  ' + str(backval)
        print ' BACKGROUND RMS =  ' + str(backsigma) + ' \n'

        # Case of no aperture size given (we select aperture sizes of UVIS=0.2/0.4", IR=0.27/0.4", ACS=0.25/0.5")
        if apertures == '':
            if instrum == 'WFC3' and detector == 'IR':
                apertures = str(0.27 / pscale_img) + ',' + str(
                    0.4 / pscale_img)
            elif instrum == 'WFC3' and detector == 'UVIS':
                apertures = str(0.2 / pscale_img) + ',' + str(0.4 / pscale_img)
            elif instrum == 'ACS' and detector == 'WFC':
                apertures = str(0.25 / pscale_img) + ',' + str(
                    0.5 / pscale_img)
            else:
                raise exception('Instrument/Detector ' + instrum + '/' +
                                detector + ' not covered in case list.')

# Remove old phot output files
        file_query = os.access(outfile, os.R_OK)
        if file_query == True: os.remove(outfile)

        # Run phot
        iraf.phot.unlearn()  # reset daophot parameters to default values
        iraf.phot(image=image+'['+str(sciext[0])+']', interactive='no', verify='no', coords=coordfile, output=outfile, \
                  fwhmpsf=fwhmpsf, sigma=backsigma, readnoise=rdnoise_corr, itime=exptime, calgorithm=calgorithm, \
                  cbox=cbox, skyvalue=backval,apertures=apertures,zmag=zeropt,salgorithm='constant')
        #annulus=annulus, dannulus=dannulus

        return backval, backsigma  # return computed background stats for image
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
Exemple #57
0
def psffit(img, fwhm, psfstars, hdr, interactive, _datamax=45000, psffun='gauss', fixaperture=False):
    ''' 
    giving an image, a psffile compute the psf using the file _psf.coo
    '''
    import lsc
    _ron = lsc.util.readkey3(hdr, 'ron')
    _gain = lsc.util.readkey3(hdr, 'gain')
    if not _ron:
        _ron = 1
        print 'warning ron not defined'
    if not _gain:
        _gain = 1
        print 'warning ron not defined'

    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)
    zmag = 0.
    varord = 0  # -1 analitic 0 - numeric

    if fixaperture:
        print 'use fix aperture 5 8 10'
        hdr = lsc.util.readhdr(img+'.fits')
        _pixelscale = lsc.util.readkey3(hdr, 'PIXSCALE')
        a1, a2, a3, a4, = float(5. / _pixelscale), float(5. / _pixelscale), float(8. / _pixelscale), float(
                    10. / _pixelscale)
    else:
        a1, a2, a3, a4, = int(fwhm + 0.5), int(fwhm * 2 + 0.5), int(fwhm * 3 + 0.5), int(fwhm * 4 + 0.5)

    iraf.fitskypars.annulus = a4
    iraf.fitskypars.salgori = 'mean'  #mode,mean,gaussian
    iraf.photpars.apertures = '%d,%d,%d' % (a2, a3, a4)
    iraf.datapars.datamin = -100
    iraf.datapars.datamax = _datamax
    iraf.datapars.readnoise = _ron
    iraf.datapars.epadu = _gain
    iraf.datapars.exposure = 'EXPTIME'
    iraf.datapars.airmass = ''
    iraf.datapars.filter = ''
    iraf.centerpars.calgori = 'centroid'
    iraf.centerpars.cbox = a2
    iraf.daopars.recenter = 'yes'
    iraf.photpars.zmag = zmag

    iraf.delete('_psf.ma*,' + img + '.psf.fit?,_psf.ps*,_psf.gr?,_psf.n*,_psf.sub.fit?', verify=False)
    iraf.phot(img+'[0]', '_psf.coo', '_psf.mag', interac=False, verify=False, verbose=False)

    iraf.daopars.psfrad = a4
    iraf.daopars.functio = psffun
    iraf.daopars.fitrad = a1
    iraf.daopars.fitsky = 'yes'
    iraf.daopars.sannulus = a4
    iraf.daopars.recenter = 'yes'
    iraf.daopars.varorder = varord

    if interactive:
        shutil.copyfile('_psf.mag', '_psf.pst')
        print '_' * 80
        print '>>> Mark good stars with "a" or "d"-elete. Then "f"-it,' + \
              ' "w"-write and "q"-uit (cursor on ds9)'
        print '-' * 80
    else:
        iraf.pstselect(img+'[0]', '_psf.mag', '_psf.pst', psfstars, interac=False, verify=False)

    iraf.psf(img + '[0]', '_psf.mag', '_psf.pst', img + '.psf', '_psf.psto', '_psf.psg', interac=interactive,
             verify=False, verbose=False)
    iraf.group(img + '[0]', '_psf.mag', img + '.psf', '_psf.grp', verify=False, verbose=False)
    iraf.nstar(img + '[0]', '_psf.grp', img + '.psf', '_psf.nst', '_psf.nrj', verify=False, verbose=False)

    photmag = iraf.txdump("_psf.mag", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1)
    pst = iraf.txdump("_psf.pst", 'xcenter,ycenter,id', expr='yes', Stdout=1)
    fitmag = iraf.txdump("_psf.nst", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1)
    return photmag, pst, fitmag
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")
Exemple #59
0
def psffit(img,
           fwhm,
           psfstars,
           hdr,
           interactive,
           _datamin,
           _datamax,
           psffun='gauss',
           fixaperture=False):
    ''' 
    giving an image, a psffile compute the psf using the file _psf.coo
    '''
    import lsc
    _ron = lsc.util.readkey3(hdr, 'ron')
    _gain = lsc.util.readkey3(hdr, 'gain')
    if not _ron:
        _ron = 1
        print 'warning ron not defined'
    if not _gain:
        _gain = 1
        print 'warning gain not defined'

    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)
    zmag = 0.
    varord = 0  # -1 analitic 0 - numeric

    if fixaperture:
        print 'use fix aperture 5 8 10'
        hdr = lsc.util.readhdr(img + '.fits')
        _pixelscale = lsc.util.readkey3(hdr, 'PIXSCALE')
        a1, a2, a3, a4, = float(5. / _pixelscale), float(
            5. / _pixelscale), float(8. / _pixelscale), float(10. /
                                                              _pixelscale)
    else:
        a1, a2, a3, a4, = int(fwhm + 0.5), int(fwhm * 2 +
                                               0.5), int(fwhm * 3 +
                                                         0.5), int(fwhm * 4 +
                                                                   0.5)

    iraf.fitskypars.annulus = a4
    iraf.fitskypars.salgori = 'mean'  #mode,mean,gaussian
    iraf.photpars.apertures = '%d,%d,%d' % (a2, a3, a4)
    iraf.datapars.datamin = _datamin
    iraf.datapars.datamax = _datamax
    iraf.datapars.readnoise = _ron
    iraf.datapars.epadu = _gain
    iraf.datapars.exposure = 'EXPTIME'
    iraf.datapars.airmass = ''
    iraf.datapars.filter = ''
    iraf.centerpars.calgori = 'centroid'
    iraf.centerpars.cbox = a2
    iraf.daopars.recenter = 'yes'
    iraf.photpars.zmag = zmag

    psfout = img.replace('.zogypsf', '') + '.psf.fits'
    iraf.delete('_psf.ma*,_psf.ps*,_psf.gr?,_psf.n*,_psf.sub.fit?,' + psfout,
                verify=False)
    iraf.phot(img + '[0]',
              '_psf.coo',
              '_psf.mag',
              interac=False,
              verify=False,
              verbose=False)

    # removes saturated stars from the list (IRAF just issues a warning)
    with open('_psf.mag') as f:
        text = f.read()
    text = re.sub('(.*\n){6}.*BadPixels\* \n', '', text)
    with open('_psf.mag', 'w') as f:
        f.write(text)

    iraf.daopars.psfrad = a4
    iraf.daopars.functio = psffun
    iraf.daopars.fitrad = a1
    iraf.daopars.fitsky = 'yes'
    iraf.daopars.sannulus = a4
    iraf.daopars.recenter = 'yes'
    iraf.daopars.varorder = varord

    if interactive:  # not possible to run pstselect or psf interactively on 64-bit linux (Error 851)
        os.system('cp _psf.mag _psf.pst')
        print '_' * 80
        print '>>> Mark good stars with "a" or "d"-elete. Then "f"-it,' + \
              ' "w"-write and "q"-uit (cursor on ds9)'
        print '-' * 80
    else:
        iraf.pstselect(img + '[0]',
                       '_psf.mag',
                       '_psf.pst',
                       psfstars,
                       interac=False,
                       verify=False)

    iraf.psf(img + '[0]',
             '_psf.mag',
             '_psf.pst',
             psfout,
             '_psf.psto',
             '_psf.psg',
             interac=interactive,
             verify=False,
             verbose=False)
    iraf.group(img + '[0]',
               '_psf.mag',
               psfout,
               '_psf.grp',
               verify=False,
               verbose=False)
    iraf.nstar(img + '[0]',
               '_psf.grp',
               psfout,
               '_psf.nst',
               '_psf.nrj',
               verify=False,
               verbose=False)

    photmag = iraf.txdump("_psf.mag",
                          'xcenter,ycenter,id,mag,merr',
                          expr='yes',
                          Stdout=1)
    pst = iraf.txdump("_psf.pst", 'xcenter,ycenter,id', expr='yes', Stdout=1)
    fitmag = iraf.txdump("_psf.nst",
                         'xcenter,ycenter,id,mag,merr',
                         expr='yes',
                         Stdout=1)
    return photmag, pst, fitmag