示例#1
0
def run_fitparams(weight=True):
    if weight: 
        weighting = 'photometric'
    else:
        weighting = 'uniform'

    ir.delete('standards.transParams')
    ir.digiphot()
    ir.photcal()
    ir.unlearn('fitparams')
    ir.fitparams('standards.instMag', 'photcal_ukirt_faint.dat',
                 'standards.config', 'standards.transParams',
                 weighting=weighting)
def run_fitparams(weight=True):
    if weight:
        weighting = 'photometric'
    else:
        weighting = 'uniform'

    ir.delete('standards.transParams')
    ir.digiphot()
    ir.photcal()
    ir.unlearn('fitparams')
    ir.fitparams('standards.instMag',
                 'photcal_ukirt_faint.dat',
                 'standards.config',
                 'standards.transParams',
                 weighting=weighting)
示例#3
0
def escut(image, pos_file, fwhm, peak):
    # input image file name, file name with matched source positions, **np.array of fwhm measurements for each source
    import numpy as np
    import matplotlib.pyplot as plt
    from scipy import stats
    from pyraf import iraf
    # import sewpy
    import os
    from matplotlib.path import Path

    iraf.images(_doprint=0)
    iraf.tv(_doprint=0)
    iraf.ptools(_doprint=0)
    iraf.noao(_doprint=0)
    iraf.digiphot(_doprint=0)
    iraf.photcal(_doprint=0)
    iraf.apphot(_doprint=0)
    iraf.imutil(_doprint=0)

    iraf.unlearn(iraf.phot, iraf.datapars, iraf.photpars, iraf.centerpars,
                 iraf.fitskypars)
    iraf.apphot.phot.setParam('interactive', "no")
    iraf.apphot.phot.setParam('verify', "no")
    iraf.datapars.setParam('datamax', 50000.)
    iraf.datapars.setParam('gain', "gain")
    iraf.datapars.setParam('ccdread', "rdnoise")
    iraf.datapars.setParam('exposure', "exptime")
    iraf.datapars.setParam('airmass', "airmass")
    iraf.datapars.setParam('filter', "filter")
    iraf.datapars.setParam('obstime', "time-obs")
    # iraf.datapars.setParam('obstime',"date-obs")
    iraf.datapars.setParam('sigma', "INDEF")
    iraf.photpars.setParam('zmag', 0.)
    iraf.centerpars.setParam('cbox', 9.)
    iraf.centerpars.setParam('maxshift', 3.)
    iraf.fitskypars.setParam('salgorithm', "median")
    iraf.fitskypars.setParam('dannulus', 10.)

    # clean up the indefs so we can actually do stats, but reassign them to 99999 so we don't lose track of things
    # keep a separate list without them to do the median (we need floats)
    indefs = np.where(fwhm == 'INDEF')
    good = np.where(fwhm != 'INDEF')
    fwhm[indefs] = 99.999
    fwhm = fwhm.astype(float)
    fwhm_good = fwhm[good].astype(float)

    indefs = np.where(peak == 'INDEF')
    peak[indefs] = -999.999
    peak = peak.astype(float)
    peak_good = peak[good].astype(float)

    if not os.path.isfile(image[0:-5] + '.txdump'):
        # findavgfwhm = sewpy.SEW(
        #     params = ["X_IMAGE", "Y_IMAGE", "FWHM_IMAGE", "FLAGS"],
        #     config = {"DETECT_THRESH":200.0},
        #     sexpath = "sex"
        # )
        #
        # out = findavgfwhm(image)["table"]
        #
        # fwhms = out['FWHM_IMAGE'] # This is an astropy table.
        # flags = out['FLAGS']

        # get a really rough estimate of the stellar FWHM in the image to set apertures

        # use the input fwhm measurement
        # ap1x = fwhm_est

        # xpos = datatable['X_IMAGE']
        # ypos = datatable['Y_IMAGE']
        # fwhm = datatable['FWHM_IMAGE']
        # flags = datatable['FLAGS']
        # idno = datatable['NUMBER']
        ap1x = np.median(
            fwhm_good
        )  # only use isolated detections of stars, this is the 1x aperture
        # print ap1x
        ap2x = 2.0 * ap1x

        # these = [ i for i,id in enumerate(idno) if (flags[i] == 0)]

        # with open(image[0:-5]+'.escut.pos','w+') as f:
        #     for j in range(len(xpos)):
        #         print >> f, xpos[j], ypos[j], fwhm[j], idno[j]

        iraf.datapars.setParam('fwhmpsf', ap1x)
        iraf.photpars.setParam('apertures', repr(ap1x) + ', ' + repr(ap2x))
        iraf.fitskypars.setParam('annulus', 4. * ap1x)
        iraf.apphot.phot(image=image,
                         coords=pos_file,
                         output=image[0:-5] + '.phot')
        with open(image[0:-5] + '.txdump', 'w+') as txdump_out:
            iraf.ptools.txdump(
                textfiles=image[0:-5] + '.phot',
                fields=
                "id,mag,merr,msky,stdev,rapert,xcen,ycen,ifilter,xairmass,image",
                expr=
                'MAG[1] != INDEF && MERR[1] != INDEF && MAG[2] != INDEF && MERR[2] != INDEF',
                headers='no',
                Stdout=txdump_out)

    mag1x, mag2x = np.loadtxt(image[0:-5] + '.txdump',
                              usecols=(1, 2),
                              unpack=True)
    iraf_id = np.loadtxt(image[0:-5] + '.txdump',
                         usecols=(0, ),
                         dtype=int,
                         unpack=True)
    # idno = np.loadtxt(image[0:-5]+'.escut.pos', usecols=(3,), dtype=int, unpack=True)
    xpos, ypos = np.loadtxt(pos_file, usecols=(0, 1), unpack=True)

    keepIndex = iraf_id - 1

    xpos, ypos, fwhm, peak = xpos[keepIndex], ypos[keepIndex], fwhm[
        keepIndex], peak[keepIndex]

    # print idno.size, iraf_id.size, xpos.size

    diff = mag2x - mag1x

    diffCut = diff
    magCut = mag2x
    xCut = xpos  #[good]
    yCut = ypos  #[good]
    idCut = iraf_id
    fwhmCut = fwhm  #_good
    peakCut = peak

    print(peakCut.size, magCut.size, diffCut.size)

    print(diffCut.size, 0, np.median(diffCut), diffCut.std())
    nRemoved = 1

    # plt.clf()
    # plt.scatter(peakCut, magCut, edgecolor='none')
    # plt.savefig('peaktest.pdf')

    plt.clf()
    # plt.hlines(bin_edges, -2, 1, colors='red', linestyle='dashed')
    plt.scatter(diff, mag2x, edgecolor='none', facecolor='black', s=4)
    # plt.scatter(diffCut, magCut, edgecolor='none', facecolor='blue', s=4)
    magdiff = list(
        zip(magCut.tolist(), diffCut.tolist(), peakCut.tolist(),
            idCut.tolist()))
    dtype = [('mag', float), ('diff', float), ('peak', float), ('id', int)]
    magdiff = np.array(magdiff, dtype=dtype)

    magSort = np.sort(magdiff, order='peak')

    peakRange = (magSort['peak'] > 20000.0) & (magSort['peak'] < 40000.0)
    peakVal = np.median((magSort['diff'])[np.where(peakRange)])
    # peakVal = np.median(diffCut)
    print(peakVal)

    plt.scatter((magSort['diff'])[np.where(peakRange)],
                (magSort['mag'])[np.where(peakRange)],
                edgecolor='none',
                facecolor='blue',
                s=4)

    while nRemoved != 0:
        nBefore = diffCut.size
        diffCheck = np.where(
            abs(peakVal - diffCut) < 2.0 *
            diffCut.std())  #[i for i,d in enumerate(diff) if (-0.5 < d < 0.0)]

        #
        diffCut = diffCut[diffCheck]
        nRemoved = nBefore - diffCut.size
        magCut = magCut[diffCheck]
        xCut = xCut[diffCheck]
        yCut = yCut[diffCheck]
        idCut = idCut[diffCheck]
        fwhmCut = fwhmCut[diffCheck]
        print(diffCut.size, nRemoved, np.median(diffCut), diffCut.std())
        if 0.05 < diffCut.std() < 0.06:
            nRemoved = 0
        # plt.fill_betweenx(bin_centers, bin_meds+3.0*bin_stds, bin_meds-3.0*bin_stds, facecolor='red', edgecolor='none', alpha=0.4, label='2x RMS sigma clipping region')

    # with open('escutSTD_i.pos','w+') as f:
    #     for i,blah in enumerate(xCut):
    #         print >> f, xCut[i], yCut[i], diffCut[i]

    bin_meds, bin_edges, binnumber = stats.binned_statistic(magCut,
                                                            diffCut,
                                                            statistic='median',
                                                            bins=24,
                                                            range=(-12, 0))
    bin_stds, bin_edges, binnumber = stats.binned_statistic(magCut,
                                                            diffCut,
                                                            statistic=np.std,
                                                            bins=24,
                                                            range=(-12, 0))
    bin_width = (bin_edges[1] - bin_edges[0])
    bin_centers = bin_edges[1:] - bin_width / 2
    # print bin_meds, bin_stds
    bin_hw = np.zeros_like(bin_stds)
    for i, bin_std in enumerate(bin_stds):
        if bin_std > 0.025:
            bin_hw[i] = 3.0 * bin_std
        else:
            bin_hw[i] = 0.075

    # print len(binnumber)
    # for i,bin_hwi in enumerate(bin_hw):

    left_edge = np.array(list(zip(peakVal - bin_hw, bin_centers)))
    right_edge = np.flipud(np.array(list(zip(peakVal + bin_hw, bin_centers))))
    # print left_edge, right_edge
    verts = np.vstack((left_edge, right_edge))
    # print verts
    # verts = np.delete(verts, np.array([0,1,2,22,23,24,25,45,46,47]), axis=0)

    # DON'T USE A PATH BECAUSE APPARENTLY IT CAN SELECT THE INVERSE SET!! WTF
    # print verts
    esRegion = Path(verts)
    sources = esRegion.contains_points(list(zip(diff, mag2x)))
    # print sources

    with open('escutREG_i.pos', 'w+') as f:
        for i, blah in enumerate(xpos[sources]):
            print((xpos[sources])[i], (ypos[sources])[i], (diff[sources])[i],
                  file=f)

    magCut2 = mag2x[sources]
    magCut1 = mag1x[sources]
    fwhmCut = fwhm[sources]
    xCut = xpos[sources]
    yCut = ypos[sources]
    diffCut = diff[sources]

    # find the sources that are in the std method but not the region method
    # print idCut, idno[sources]
    # extrasSTD = np.setdiff1d(idno[sources], idCut)
    # print extrasSTD.size
    # print extrasSTD
    # with open('escutUNIQUE.pos','w+') as f:
    #     for i,blah in enumerate(extrasSTD):
    #         print >> f, xpos[blah-1], ypos[blah-1]

    # fwhmcheck = np.loadtxt('testfwhmREG.log', usecols=(10,), unpack=True)
    fwhmchk2 = np.where((magCut2 < -4) & (fwhmCut < 90.0))
    print(np.median(fwhmCut[fwhmchk2]), np.std(fwhmCut[fwhmchk2]))
    fwchk = np.where(
        np.abs(fwhmCut - np.median(fwhmCut[fwhmchk2])) > 10.0 *
        np.std(fwhmCut[fwhmchk2]))
    drop = np.abs(fwhmCut - np.median(fwhmCut[fwhmchk2])) > 10.0 * np.std(
        fwhmCut[fwhmchk2])
    keep = np.abs(fwhmCut - np.median(fwhmCut[fwhmchk2])) <= 10.0 * np.std(
        fwhmCut[fwhmchk2])

    with open('escutVBAD_i.pos', 'w+') as f:
        for i, blah in enumerate(xCut[fwchk]):
            print((xCut[fwchk])[i], (yCut[fwchk])[i], file=f)

    with open('escut_i.pos', 'w+') as f:
        for i, blah in enumerate(xCut):
            if not drop[i]:
                print(xCut[i],
                      yCut[i],
                      magCut2[i],
                      fwhmCut[i],
                      magCut1[i],
                      file=f)

    with open('escut_g.pos', 'w+') as f:
        for i, blah in enumerate(xCut):
            if not drop[i]:
                print(xCut[i],
                      yCut[i],
                      magCut2[i],
                      fwhmCut[i],
                      magCut1[i],
                      file=f)

    plt.fill_betweenx(bin_centers,
                      peakVal + bin_hw,
                      peakVal - bin_hw,
                      facecolor='red',
                      edgecolor='none',
                      alpha=0.4,
                      label='2x RMS sigma clipping region')

    plt.scatter(diffCut[fwchk],
                magCut2[fwchk],
                edgecolor='none',
                facecolor='red',
                s=4)
    plt.ylim(0, -12)
    plt.xlabel('$m_{2x} - m_{1x}$')
    plt.ylabel('$m_{2x}$')
    plt.xlim(-2, 1)
    plt.savefig('testmagiraf.pdf')

    plt.clf()
    plt.scatter(magCut2, fwhmCut, edgecolor='none', facecolor='black')
    plt.scatter(magCut2[fwchk],
                fwhmCut[fwchk],
                edgecolor='none',
                facecolor='red')
    plt.hlines([np.median(fwhmCut)], -12, 0, colors='red', linestyle='dashed')
    plt.hlines([
        np.median(fwhmCut) + fwhmCut.std(),
        np.median(fwhmCut) - fwhmCut.std()
    ],
               -12,
               0,
               colors='red',
               linestyle='dotted')
    plt.ylim(0, 20)
    plt.xlim(-12, 0)
    plt.ylabel('fwhm')
    plt.xlabel('$m_{2x}$')
    plt.savefig('fwhmcheck.pdf')

    return fwhmCut[keep]
示例#4
0
def organizeFiles():
    # Create files that are usually generated by mkimset.
    # We have more information about how to properly group our
    # files together.
    imsetsFilename = 'standards.imageSets'
    obsparFilename = 'standards.obsParams'
    shiftsFilename = 'standards.shifts'
    iMagniFilename = 'standards.instMag' 

    imsets = open(imsetsFilename, 'w')
    obspar = open(obsparFilename, 'w')
    shifts = open(shiftsFilename, 'w')

    for star in stars:
        roots = []

        for filter in filters:
            dir = star.lower() + '_' + filter.lower()
            if filter == 'Lp':
                dir += '2'
                
            images = glob.glob(dataDir + dir + '/c????.fits')
            imageRoots = []
            for ii in images:
                fileRoot = ii.split('/')[-1]
                imageRoots.append(fileRoot.replace('.fits', ''))

            roots.append(imageRoots)
            print star, filter, len(imageRoots)
            
        longCount = np.array([len(r) for r in roots]).max()
        print 'Long Count for %s = %d' % (star, longCount)

        for ii in range(longCount):
            imsets.write('%5s :  ' % star)

            for ff in range(len(roots)):
                if ii < len(roots[ff]):
                    # Write obsParams 
                    ir.txdump(roots[ff][ii] + '.phot.mag', 
                              'IMAGE,IFILTER,ITIME,XAIRMASS,OTIME', 
                              'yes', Stdout=obspar)

                    # Write shifts
                    posInfo = ir.txdump(roots[ff][ii] + '.phot.mag', 
                                        'IMAGE,XCENTER,YCENTER', 
                                        'yes', Stdout=1)
                    posFields = posInfo[0].split()
                    shiftX = -1.0 * float(posFields[1])
                    shiftY = -1.0 * float(posFields[2])
                    shifts.write('%-10s  %7.2f  %7.2f\n' % 
                                 (posFields[0], shiftX, shiftY))
                                                          
                    
                    # Write imageSet
                    imsets.write('%-10s  ' % (roots[ff][ii] + '.fits'))
                else:
                    imsets.write('%-10s  ' % ('INDEF'))

            imsets.write('\n')

    imsets.close()
    obspar.close()
    shifts.close()

    # Now run mknobsfile
    ir.delete(iMagniFilename)
    ir.digiphot()
    ir.photcal()
    ir.unlearn('mknobsfile')
    ir.mknobsfile.obsparams = obsparFilename
    ir.mknobsfile.shifts = shiftsFilename
    ir.mknobsfile('*.phot.mag', ','.join(filters), imsetsFilename, 
                  iMagniFilename, verbose='no', wrap='no')
    
    
    # Run mkconfig
    ir.delete('standards.config')
    ir.unlearn('mkconfig')
    ir.mkconfig('standards.config', 'fphotcal_ukirt_faint.dat', 
                'fstandards.instMag.dat', 'tphotcal_ukirt_faint.dat', 
                check='no', edit='no')
示例#5
0
def main(argv):
	home_root = os.environ['HOME']
	iraf.images(_doprint=0)
	iraf.tv(_doprint=0)
	iraf.ptools(_doprint=0)
	iraf.noao(_doprint=0)
	iraf.digiphot(_doprint=0)
	iraf.photcal(_doprint=0)
	iraf.apphot(_doprint=0)  
	iraf.imutil(_doprint=0)

	for file_ in os.listdir("./"):
		if file_.endswith("_i.fits"):
			fits_file_i = file_
			title_string = file_[0:9]
		if file_.endswith("_g.fits"):
			fits_file_g = file_
				
	
	fits_h_i = fits.open(fits_file_i)
	fits_h_g = fits.open(fits_file_g)
	
	fwhm_i = fits_h_i[0].header['FWHMPSF']
	fwhm_g = fits_h_g[0].header['FWHMPSF']
	print 'Image FWHM :: g = {0:5.3f} : i = {1:5.3f}'.format(fwhm_g,fwhm_i)
	
	# if not os.path.isdir('psf'): # make a psf subfolder if it doesn't exist
	# 	os.mkdir('psf')
	
	# # and then copy in the data files if they don't already exist as files
	# if not os.path.isfile('psf/'+fits_file_i):
	# 	iraf.images.imcopy(fits_file_i,'psf/'+fits_file_i,verbose="yes")
	# 
	# if not os.path.isfile('psf/'+fits_file_g) :
	# 	iraf.images.imcopy(fits_file_g,'psf/'+fits_file_g,verbose="yes")
	# 
	# # also copy in the apcor star lists
	# if not (os.path.isfile('psf/apcor_stars_i.txt') or os.path.islink('psf/apcor_stars_i.txt')) :
	# 	shutil.copyfile('apcor_stars_i.txt','psf/apcor_stars_i.txt')	
	# 
	# if not (os.path.isfile('psf/apcor_stars_g.txt') or os.path.islink('psf/apcor_stars_g.txt')) :
	# 	shutil.copyfile('apcor_stars_g.txt','psf/apcor_stars_g.txt')	
	# 	
	# # and change to the psf folder
	# os.chdir('psf')

	iraf.unlearn(iraf.phot,iraf.datapars,iraf.photpars,iraf.centerpars,iraf.fitskypars)
	
	iraf.apphot.phot.setParam('interactive',"no")
	iraf.apphot.phot.setParam('verify',"no")
	iraf.datapars.setParam('datamax',"INDEF")
	iraf.datapars.setParam('gain',"gain")
	iraf.datapars.setParam('ccdread',"rdnoise")
	iraf.datapars.setParam('exposure',"exptime")
	iraf.datapars.setParam('airmass',"airmass")
	iraf.datapars.setParam('filter',"filter")
	iraf.datapars.setParam('obstime',"time-obs")
	iraf.datapars.setParam('sigma',"INDEF")
	iraf.photpars.setParam('zmag',0.)
	iraf.centerpars.setParam('cbox',9.)
	iraf.centerpars.setParam('maxshift',3.)
	iraf.fitskypars.setParam('salgorithm',"median")
	iraf.fitskypars.setParam('dannulus',10.)
	
	iraf.datapars.setParam('fwhmpsf',fwhm_g)
	iraf.photpars.setParam('apertures',iraf.nint(4.*fwhm_g))
	iraf.fitskypars.setParam('annulus',(6.*fwhm_g))
	
	iraf.apphot.phot(image=fits_file_g, coords='apcor_stars_g.txt', output="g_psfstars.mag.1")
	
	iraf.datapars.setParam('fwhmpsf',fwhm_i)
	iraf.photpars.setParam('apertures',iraf.nint(4.*fwhm_i))
	iraf.fitskypars.setParam('annulus',(6.*fwhm_i))
	
	iraf.apphot.phot(image=fits_file_i, coords='apcor_stars_i.txt', output="i_psfstars.mag.1")
	
	with open("apcor_stars_i.txt") as foo:
		lines = len(foo.readlines())
	
	iraf.daophot(_doprint=0)
	iraf.pstselect.setParam('image',fits_file_i)
	iraf.pstselect.setParam('photfile',"i_psfstars.mag.1")
	iraf.pstselect.setParam('pstfile',"default")
	iraf.pstselect.setParam('maxnpsf',lines)
	iraf.pstselect.setParam('plottype',"mesh")
	
	iraf.daophot.pstselect(verify='no', mode='h')
	
	with open("apcor_stars_g.txt") as foo:
		lines = len(foo.readlines())
	
	iraf.pstselect.setParam('image',fits_file_g)
	iraf.pstselect.setParam('photfile',"g_psfstars.mag.1")
	iraf.pstselect.setParam('pstfile',"default")
	iraf.pstselect.setParam('maxnpsf',lines)
	iraf.pstselect.setParam('plottype',"mesh")
	
	iraf.daophot.pstselect()
	
	iraf.datapars.setParam('datamax',"INDEF")
	iraf.datapars.setParam('gain',"gain")
	iraf.datapars.setParam('ccdread',"rdnoise")
	iraf.datapars.setParam('exposure',"exptime")
	iraf.datapars.setParam('airmass',"airmass")
	iraf.datapars.setParam('filter',"filter")
	iraf.datapars.setParam('obstime',"time-obs")
	iraf.datapars.setParam('sigma',"INDEF")
	iraf.daopars.setParam('psfrad',iraf.nint(3.*fwhm_i))
	iraf.daopars.setParam('fitrad',fwhm_i)
	
	iraf.psf.setParam('image',fits_file_i)
	iraf.psf.setParam('photfile',"i_psfstars.mag.1")
	iraf.psf.setParam('pstfile',fits_file_i+".pst.1")
	iraf.psf.setParam('interactive', 'no')
	iraf.daophot.psf()
	
	iraf.psf.setParam('image',fits_file_g)
	iraf.psf.setParam('photfile',"g_psfstars.mag.1")
	iraf.psf.setParam('pstfile',fits_file_g+".pst.1")
	iraf.psf.setParam('interactive', 'no')
	iraf.daophot.psf()
def organizeFiles():
    # Create files that are usually generated by mkimset.
    # We have more information about how to properly group our
    # files together.
    imsetsFilename = 'standards.imageSets'
    obsparFilename = 'standards.obsParams'
    shiftsFilename = 'standards.shifts'
    iMagniFilename = 'standards.instMag'

    imsets = open(imsetsFilename, 'w')
    obspar = open(obsparFilename, 'w')
    shifts = open(shiftsFilename, 'w')

    for star in stars:
        roots = []

        for filter in filters:
            dir = star.lower() + '_' + filter.lower()
            if filter == 'Lp':
                dir += '2'

            images = glob.glob(dataDir + dir + '/c????.fits')
            imageRoots = []
            for ii in images:
                fileRoot = ii.split('/')[-1]
                imageRoots.append(fileRoot.replace('.fits', ''))

            roots.append(imageRoots)
            print star, filter, len(imageRoots)

        longCount = np.array([len(r) for r in roots]).max()
        print 'Long Count for %s = %d' % (star, longCount)

        for ii in range(longCount):
            imsets.write('%5s :  ' % star)

            for ff in range(len(roots)):
                if ii < len(roots[ff]):
                    # Write obsParams
                    ir.txdump(roots[ff][ii] + '.phot.mag',
                              'IMAGE,IFILTER,ITIME,XAIRMASS,OTIME',
                              'yes',
                              Stdout=obspar)

                    # Write shifts
                    posInfo = ir.txdump(roots[ff][ii] + '.phot.mag',
                                        'IMAGE,XCENTER,YCENTER',
                                        'yes',
                                        Stdout=1)
                    posFields = posInfo[0].split()
                    shiftX = -1.0 * float(posFields[1])
                    shiftY = -1.0 * float(posFields[2])
                    shifts.write('%-10s  %7.2f  %7.2f\n' %
                                 (posFields[0], shiftX, shiftY))

                    # Write imageSet
                    imsets.write('%-10s  ' % (roots[ff][ii] + '.fits'))
                else:
                    imsets.write('%-10s  ' % ('INDEF'))

            imsets.write('\n')

    imsets.close()
    obspar.close()
    shifts.close()

    # Now run mknobsfile
    ir.delete(iMagniFilename)
    ir.digiphot()
    ir.photcal()
    ir.unlearn('mknobsfile')
    ir.mknobsfile.obsparams = obsparFilename
    ir.mknobsfile.shifts = shiftsFilename
    ir.mknobsfile('*.phot.mag',
                  ','.join(filters),
                  imsetsFilename,
                  iMagniFilename,
                  verbose='no',
                  wrap='no')

    # Run mkconfig
    ir.delete('standards.config')
    ir.unlearn('mkconfig')
    ir.mkconfig('standards.config',
                'fphotcal_ukirt_faint.dat',
                'fstandards.instMag.dat',
                'tphotcal_ukirt_faint.dat',
                check='no',
                edit='no')
示例#7
0
#! /usr/local/bin/python
import os, sys
import numpy as np
from pyraf import iraf
from odi_calibrate import download_sdss, js_calibrate
from sdss_fit import getVabs
from collections import OrderedDict

iraf.images(_doprint=0)
iraf.tv(_doprint=0)
iraf.ptools(_doprint=0)
iraf.noao(_doprint=0)
iraf.digiphot(_doprint=0)
iraf.photcal(_doprint=0)
iraf.apphot(_doprint=0)
iraf.imutil(_doprint=0)

def getHImass(object, dm):
    # print object, mpc
    uchvcdb = os.path.dirname(os.path.abspath(__file__))+'/predblist.sort.csv'
    name, mass = np.loadtxt(uchvcdb, usecols=(1,6), dtype=str, delimiter=',', unpack=True)
    # find the right row
    coord = [i for i,this in enumerate(mass) if object.upper() in name[i]][0]
    # print 'the HI mass of', name[coord], 'is', mass[coord], 'at 1 Mpc'
    
    # mpc = mpc/1000.
    mpc = pow(10,((dm + 5.)/5.))/1000000.
    logm = float(mass[coord])
    mass = mpc*mpc*10**logm  # make sure to scale by the distance in Mpc^2
    
    print '{:3.1f}'.format(np.log10(mass))
示例#8
0
    coordLines = coord.splitlines()

    fwhm = float(coordLines[9].split()[3])
    sigma = float(coordLines[19].split()[3])
    threshold = float(coordLines[27].split()[3])
    coordFile.close()

    print(objname, filter_, fwhm, sigma, threshold)
    print('creating psf...')

    iraf.images(_doprint=0)
    iraf.tv(_doprint=0)
    iraf.ptools(_doprint=0)
    iraf.noao(_doprint=0)
    iraf.digiphot(_doprint=0)
    iraf.photcal(_doprint=0)
    iraf.apphot(_doprint=0)
    iraf.imutil(_doprint=0)
    iraf.daophot(_doprint=0)
    iraf.tables(_doprint=0)

    iraf.unlearn(iraf.ptools.pselect, iraf.tables.tcreate, iraf.tables.tmatch,
                 iraf.tables.tinfo, iraf.tables.tdump)

    # first measure the psf for the image
    iraf.unlearn(iraf.phot, iraf.datapars, iraf.photpars, iraf.centerpars,
                 iraf.fitskypars)

    iraf.apphot.phot.setParam('interactive', "no")
    iraf.apphot.phot.setParam('verify', "no")
    iraf.datapars.setParam('datamax', "INDEF")
示例#9
0
文件: calibphot.py 项目: cenko/python
import pyfits
from types import *

# Access to the iqutils
from iqutils import *
import iqpkg

# Necessary packages
iraf.images()
iraf.immatch()
iraf.noao()
iraf.imred()
iraf.ccdred()
iraf.digiphot()
iraf.daophot()
iraf.photcal()
iraf.images()
iraf.imcoords()

yes=iraf.yes
no=iraf.no
INDEF=iraf.INDEF
hedit=iraf.hedit
imgets=iraf.imgets
imcombine=iraf.imcombine

pyrafdir="python/pyraf/"
pyrafdir_key='PYRAFPARS'

if os.environ.has_key(pyrafdir_key):
    pardir=os.environ[pyrafdir_key]
示例#10
0
def escut(image, pos_file, fwhm, peak):
    # input image file name, file name with matched source positions, **np.array of fwhm measurements for each source
    import numpy as np
    import matplotlib.pyplot as plt
    from scipy import stats
    from pyraf import iraf
    # import sewpy
    import os
    from matplotlib.path import Path
    
    iraf.images(_doprint=0)
    iraf.tv(_doprint=0)
    iraf.ptools(_doprint=0)
    iraf.noao(_doprint=0)
    iraf.digiphot(_doprint=0)
    iraf.photcal(_doprint=0)
    iraf.apphot(_doprint=0)  
    iraf.imutil(_doprint=0)
    
    iraf.unlearn(iraf.phot,iraf.datapars,iraf.photpars,iraf.centerpars,iraf.fitskypars)
    iraf.apphot.phot.setParam('interactive',"no")
    iraf.apphot.phot.setParam('verify',"no")
    iraf.datapars.setParam('datamax',50000.)
    iraf.datapars.setParam('gain',"gain")
    iraf.datapars.setParam('ccdread',"rdnoise")
    iraf.datapars.setParam('exposure',"exptime")
    iraf.datapars.setParam('airmass',"airmass")
    iraf.datapars.setParam('filter',"filter")
    iraf.datapars.setParam('obstime',"time-obs")
    # iraf.datapars.setParam('obstime',"date-obs")
    iraf.datapars.setParam('sigma',"INDEF")
    iraf.photpars.setParam('zmag',0.)
    iraf.centerpars.setParam('cbox',9.)
    iraf.centerpars.setParam('maxshift',3.)
    iraf.fitskypars.setParam('salgorithm',"median")
    iraf.fitskypars.setParam('dannulus',10.)
    
    # clean up the indefs so we can actually do stats, but reassign them to 99999 so we don't lose track of things
    # keep a separate list without them to do the median (we need floats)
    indefs = np.where(fwhm=='INDEF')
    good = np.where(fwhm!='INDEF')
    fwhm[indefs] = 99.999
    fwhm = fwhm.astype(float)
    fwhm_good = fwhm[good].astype(float)

    indefs = np.where(peak=='INDEF')
    peak[indefs] = -999.999
    peak = peak.astype(float)
    peak_good = peak[good].astype(float)
    
    if not os.path.isfile(image[0:-5]+'.txdump'):
        # findavgfwhm = sewpy.SEW(
        #     params = ["X_IMAGE", "Y_IMAGE", "FWHM_IMAGE", "FLAGS"],
        #     config = {"DETECT_THRESH":200.0},
        #     sexpath = "sex"
        # )
        # 
        # out = findavgfwhm(image)["table"]
        # 
        # fwhms = out['FWHM_IMAGE'] # This is an astropy table.
        # flags = out['FLAGS']
        
        # get a really rough estimate of the stellar FWHM in the image to set apertures
        
        # use the input fwhm measurement
        # ap1x = fwhm_est

        
        # xpos = datatable['X_IMAGE']
        # ypos = datatable['Y_IMAGE']
        # fwhm = datatable['FWHM_IMAGE']
        # flags = datatable['FLAGS']
        # idno = datatable['NUMBER']
        ap1x = np.median(fwhm_good) # only use isolated detections of stars, this is the 1x aperture
        # print ap1x
        ap2x = 2.0*ap1x
        
        # these = [ i for i,id in enumerate(idno) if (flags[i] == 0)]
        
        # with open(image[0:-5]+'.escut.pos','w+') as f:
        #     for j in range(len(xpos)):
        #         print >> f, xpos[j], ypos[j], fwhm[j], idno[j]
        
        iraf.datapars.setParam('fwhmpsf',ap1x)
        iraf.photpars.setParam('apertures',repr(ap1x)+', '+repr(ap2x))
        iraf.fitskypars.setParam('annulus',4.*ap1x)
        iraf.apphot.phot(image=image, coords=pos_file, output=image[0:-5]+'.phot')
        with open(image[0:-5]+'.txdump','w+') as txdump_out :
            iraf.ptools.txdump(textfiles=image[0:-5]+'.phot', fields="id,mag,merr,msky,stdev,rapert,xcen,ycen,ifilter,xairmass,image", expr='MAG[1] != INDEF && MERR[1] != INDEF && MAG[2] != INDEF && MERR[2] != INDEF', headers='no', Stdout=txdump_out)
            
    mag1x, mag2x = np.loadtxt(image[0:-5]+'.txdump', usecols=(1,2), unpack=True)
    iraf_id = np.loadtxt(image[0:-5]+'.txdump', usecols=(0,), dtype=int, unpack=True)
    # idno = np.loadtxt(image[0:-5]+'.escut.pos', usecols=(3,), dtype=int, unpack=True)
    xpos, ypos = np.loadtxt(pos_file, usecols=(0,1), unpack=True)
    
    keepIndex = iraf_id - 1
    
    xpos, ypos, fwhm, peak = xpos[keepIndex], ypos[keepIndex], fwhm[keepIndex], peak[keepIndex]
    
    # print idno.size, iraf_id.size, xpos.size
    
    diff = mag2x - mag1x
    
    diffCut = diff
    magCut = mag2x
    xCut = xpos#[good]
    yCut = ypos#[good]
    idCut = iraf_id
    fwhmCut = fwhm#_good
    peakCut = peak
    
    print peakCut.size, magCut.size, diffCut.size
    
    print diffCut.size, 0, np.median(diffCut), diffCut.std()
    nRemoved = 1   
    
    # plt.clf()
    # plt.scatter(peakCut, magCut, edgecolor='none')
    # plt.savefig('peaktest.pdf')
    
    plt.clf()
    # plt.hlines(bin_edges, -2, 1, colors='red', linestyle='dashed')
    plt.scatter(diff, mag2x, edgecolor='none', facecolor='black', s=4)
    # plt.scatter(diffCut, magCut, edgecolor='none', facecolor='blue', s=4)
    magdiff = zip(magCut.tolist(), diffCut.tolist(), peakCut.tolist(), idCut.tolist())
    dtype = [('mag',float), ('diff', float), ('peak', float), ('id', int)]
    magdiff = np.array(magdiff, dtype=dtype)
    
    magSort = np.sort(magdiff, order='peak')

    peakRange = (magSort['peak'] > 20000.0) & (magSort['peak'] < 40000.0)
    peakVal = np.median((magSort['diff'])[np.where(peakRange)])
    # peakVal = np.median(diffCut)
    print peakVal
    
    plt.scatter((magSort['diff'])[np.where(peakRange)], (magSort['mag'])[np.where(peakRange)], edgecolor='none', facecolor='blue', s=4)
    
    while nRemoved != 0:
        nBefore = diffCut.size
        diffCheck = np.where(abs(peakVal-diffCut) < 2.5*diffCut.std())#[i for i,d in enumerate(diff) if (-0.5 < d < 0.0)]
        # 
        diffCut = diffCut[diffCheck]
        nRemoved = nBefore - diffCut.size
        magCut = magCut[diffCheck]
        xCut = xCut[diffCheck]
        yCut = yCut[diffCheck]
        idCut = idCut[diffCheck]
        fwhmCut = fwhmCut[diffCheck]
        print diffCut.size, nRemoved, np.median(diffCut), diffCut.std()
        if 0.05 < diffCut.std() <0.06:
           nRemoved=0 
        # plt.fill_betweenx(bin_centers, bin_meds+3.0*bin_stds, bin_meds-3.0*bin_stds, facecolor='red', edgecolor='none', alpha=0.4, label='2x RMS sigma clipping region')
        
    # with open('escutSTD_i.pos','w+') as f:
    #     for i,blah in enumerate(xCut):
    #         print >> f, xCut[i], yCut[i], diffCut[i]
            
    bin_meds, bin_edges, binnumber = stats.binned_statistic(magCut, diffCut, statistic='median', bins=24, range=(magCut.min(),magCut.max()))
    bin_stds, bin_edges, binnumber = stats.binned_statistic(magCut, diffCut, statistic=np.std, bins=24, range=(magCut.min(),magCut.max()))
    bin_width = (bin_edges[1] - bin_edges[0])
    bin_centers = bin_edges[1:] - bin_width/2
    # print bin_meds, bin_stds
    
    left_edge = np.array(zip(peakVal-2.5*bin_stds, bin_centers))
    right_edge = np.flipud(np.array(zip(peakVal+2.5*bin_stds, bin_centers)))
    # print left_edge, right_edge
    verts = np.vstack((left_edge, right_edge))
    # print verts
    # verts = np.delete(verts, np.array([0,1,2,22,23,24,25,45,46,47]), axis=0)
    
    # print verts
    esRegion = Path(verts)
    sources = esRegion.contains_points(zip(diff,mag2x))
    # print sources
    
    with open('escutREG_i.pos','w+') as f:
        for i,blah in enumerate(xpos[sources]):
            print >> f, (xpos[sources])[i], (ypos[sources])[i], (diff[sources])[i]
    
    magCut = mag2x[sources]
    fwhmCut = fwhm[sources]   
    xCut = xpos[sources]
    yCut = ypos[sources] 
    diffCut = diff[sources]    
    
    # find the sources that are in the std method but not the region method
    # print idCut, idno[sources]
    # extrasSTD = np.setdiff1d(idno[sources], idCut)
    # print extrasSTD.size
    # print extrasSTD
    # with open('escutUNIQUE.pos','w+') as f:
    #     for i,blah in enumerate(extrasSTD):
    #         print >> f, xpos[blah-1], ypos[blah-1]
            
    # fwhmcheck = np.loadtxt('testfwhmREG.log', usecols=(10,), unpack=True)
    fwhmchk2 = np.where((magCut<-4) & (fwhmCut<90.0))
    print np.median(fwhmCut[fwhmchk2]), np.std(fwhmCut[fwhmchk2])
    fwchk = np.where(np.abs(fwhmCut-np.median(fwhmCut[fwhmchk2])) > 10.0*np.std(fwhmCut[fwhmchk2]))
    drop = np.abs(fwhmCut-np.median(fwhmCut[fwhmchk2])) > 10.0*np.std(fwhmCut[fwhmchk2])
    # fwmag = mag2x[sources]
    
    with open('escutVBAD_i.pos','w+') as f:
        for i,blah in enumerate(xCut[fwchk]):
            print >> f, (xCut[fwchk])[i], (yCut[fwchk])[i]
            
    with open('escut_r.pos','w+') as f:
        for i,blah in enumerate(xCut):
            if not drop[i]:
                print >> f, xCut[i], yCut[i]
    
    with open('escut_g.pos','w+') as f:
        for i,blah in enumerate(xCut):
            if not drop[i]:
                print >> f, xCut[i], yCut[i]
    
    plt.fill_betweenx(bin_centers, peakVal+2.5*bin_stds, peakVal-2.5*bin_stds, facecolor='red', edgecolor='none', alpha=0.4, label='2x RMS sigma clipping region')
    plt.scatter(diffCut[fwchk], magCut[fwchk], edgecolor='none', facecolor='red', s=4)
    plt.ylim(0,-12)
    plt.xlabel('$m_{2x} - m_{1x}$')
    plt.ylabel('$m_{2x}$')
    plt.xlim(-2,1)
    plt.savefig('testmagiraf.pdf')
    
    plt.clf()
    plt.scatter(magCut, fwhmCut, edgecolor='none', facecolor='black')
    plt.scatter(magCut[fwchk], fwhmCut[fwchk], edgecolor='none', facecolor='red')
    plt.hlines([np.median(fwhmCut)], -12, 0, colors='red', linestyle='dashed')
    plt.hlines([np.median(fwhmCut)+fwhmCut.std(), np.median(fwhmCut)-fwhmCut.std()], -12, 0, colors='red', linestyle='dotted')
    plt.ylim(0,20)
    plt.xlim(-12,0)
    plt.ylabel('fwhm')
    plt.xlabel('$m_{2x}$')
    plt.savefig('fwhmcheck.pdf')
    
    
    return True
示例#11
0
import pyfits
from types import *

# Access to the iqutils
from iqutils import *
import iqpkg

# Necessary packages
iraf.images()
iraf.immatch()
iraf.noao()
iraf.imred()
iraf.ccdred()
iraf.digiphot()
iraf.daophot()
iraf.photcal()
iraf.images()
iraf.imcoords()

yes = iraf.yes
no = iraf.no
INDEF = iraf.INDEF
hedit = iraf.hedit
imgets = iraf.imgets
imcombine = iraf.imcombine

pyrafdir = "python/pyraf/"
pyrafdir_key = 'PYRAFPARS'

if os.environ.has_key(pyrafdir_key):
    pardir = os.environ[pyrafdir_key]
示例#12
0
文件: ctio_mkpsf.py 项目: OSSOS/MOP
def build(f):

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

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

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

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

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

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

    import jjkmode

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

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

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

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

    while current_ext <= NEXTEND:

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

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

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

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

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

        iraf.centerpars.calgorithm = "centroid"

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            print "Rebuilding the PSF"

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

            print "re-subtracting with rebuilt psf"

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        current_ext = current_ext + 1

    outfits.close()
    return mef_psf
示例#13
0
def build(f):

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

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

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

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

    import jjkmode

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

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

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

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

    while (current_ext <= NEXTEND):

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

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

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

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

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

        iraf.centerpars.calgorithm = 'centroid'

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            print "Rebuilding the PSF"

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

            print "re-subtracting with rebuilt psf"

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        current_ext = current_ext + 1

    outfits.close()
    return mef_psf