Exemple #1
0
def doiraf(ximage, yimage, xmax, ymax):
    (xpos, ypos) = getpositions(ximage, yimage, xmax, ymax)
    coords = open("noisecoords.dat", 'w')
    for i in range(len(xpos)):
        coords.write("%8.1f %8.1f \n" % (xpos[i], ypos[i]))
    coords.close()
    sky = open("sky", 'w')
    for i in range(npoints):
        sky.write("0.0 \n")
    sky.close()
    aps = open("apertures", 'w')
    aps.write("1,1.5,2,2.6,3,3.5,4,4.5,5,5.5")
    aps.close()

    #runiraf()
    iraf.digiphot()
    iraf.daophot()
    ##iraf.apphot()
    image = 'mosaic_minus_median_extract.fits'
    #print image
    os.system("rm noise.dat")
    iraf.digiphot.daophot.phot(image,
                               coords="noisecoords.dat",
                               output="noise.dat",
                               calgorithm='none',
                               skyfile="sky",
                               salgori="file",
                               aperture="apertures",
                               interactive="no",
                               verify='no',
                               verbose='no')
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 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 #4
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 #5
0
def set_default(base):
    # Loading necessary IRAF packages
    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)
    iraf.apphot(_doprint=0)
    iraf.reset(min_lenuserarea='200000')

    iraf.datapars.setParam('datamin','INDEF')
    iraf.datapars.setParam('datamax','60000')
    iraf.datapars.setParam('exposure','EXPTIME')
    iraf.datapars.setParam('airmass', 'AIRMASS')
    iraf.datapars.setParam('filter', 'FILTER')

    iraf.findpars.setParam('threshold', 3.0)
    iraf.findpars.setParam('sharphi',  1)
    iraf.findpars.setParam('roundhi', 1.3)
    iraf.findpars.setParam('roundlo', -1.3)

    iraf.daofind.setParam('verify','no')
    iraf.daofind.setParam('interactive','no')

    iraf.photpars.setParam('zmag',25.0)
    iraf.photpars.setParam('weighti','constant')
    iraf.photpars.setParam('apertur',3.0)

    iraf.phot.setParam('output',base+'default')
    iraf.phot.setParam('coords',base+'default')
    iraf.phot.setParam('verify','no')
    iraf.phot.setParam('interactive','no')

    iraf.fitpsf.setParam('box',10.0)
    iraf.fitpsf.setParam('verify','no')
    iraf.fitpsf.setParam('interactive','no')

    iraf.centerpars.setParam('calgori','none')
    iraf.fitskypars.setParam('salgorithm','mode')

    iraf.daopars.setParam('functio','moffat15')
    iraf.daopars.setParam('varorde','0')
    iraf.daopars.setParam('nclean','0')
    iraf.daopars.setParam('saturat','no')
    iraf.daopars.setParam('fitsky','yes')
    iraf.daopars.setParam('recenter','yes')
    iraf.daopars.setParam('groupsk','yes')
    iraf.daopars.setParam('maxnsta','40000')

    iraf.psf.setParam('photfile',base+'default')
    iraf.psf.setParam('pstfile',base+'default')
    iraf.psf.setParam('psfimage',base+'default')
    iraf.psf.setParam('opstfile',base+'default')
    iraf.psf.setParam('groupfil',base+'default')
    iraf.psf.setParam('interac','no')
    iraf.psf.setParam('matchby','yes')
    iraf.psf.setParam('verify','no')
    iraf.psf.setParam('showplo','no')

    iraf.allstar.setParam('verify','no')
Exemple #6
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 #7
0
def findStars(frame,fwhm,sigma,thresh,extra="",overwrite=True,interactive=True):
	if overwrite:
		if os.path.isfile(frame+".fullcoo"):
			os.system("rm "+frame+".fullcoo")
	iraf.noao()
	iraf.digiphot()
	iraf.daophot()
	iraf.datapars.setParam("fwhmpsf",fwhm)
	iraf.datapars.setParam("sigma",sigma)
	iraf.findpars.setParam("thresh",thresh)
	foo = iraf.daofind(image=frame+extra,output=frame+".fullcoo",Stdin=cr,Stdout=1)
Exemple #8
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 #9
0
def aperture_photometry(image,photfilesuffix,threshold=4.,wcs='logical',mode='small', telescope='Pairitel'):
  '''perform aperture photometry on a given image
  
  This procedure perfrom IRAF/DAOPHOT aperture phtometry on (all extensions of) a fits image.
  It sets some global daophot parameters, then perfroms daophot.daofind and daophot.phot
  The output .mag file is filtered for valid magnitudes and a mag.sex file computed, where
  all coordinates are transformed to sexagesimal coordinates.
  
  input: image: filename
  keywords:  threshold=4 :daofind detection threshold in sigma_sky
             wcs='logical' :working wcs system, can be  'logical','physical' or 'tv'
                        in any case an additional sexagesimal output file will be computed
         mode='small' : 'standard' = 'large' or 'psf' = 'small' choses a pre-defined aperture size for 
                        aperture photometry of standards or just as starting point for psf photometry
  '''
  print 'Performing aperture photometry on '+image
  if telescope == 'Pairitel':
      set_Pairitel_params()
  elif telescope == 'FLWO':
      set_FLWO_params()
  else:
      print 'No parameters found for this telescope!'
  
  iraf.mscred(_doprint=0)
  iraf.digiphot(_doprint=0)
  iraf.daophot(_doprint=0)
  iraf.daophot.verify = False
  iraf.daophot.wcsin = wcs
  iraf.daophot.wcsout = wcs
  iraf.daophot.wcspsf = wcs
  iraf.centerpars.cbox=2.*iraf.datapars.fwhmpsf
  iraf.fitskypars.annulus=20 # usually one would use 5.*iraf.datapars.fwhmpsf
  #annulus must be wide enough to have good sky statistics
  iraf.fitskypars.dannulus=10. # this is standard
  if 'standard'.find(mode.lower()) != -1 or 'large'.find(mode.lower()) != -1:
    iraf.photpars.aperture = 4.*iraf.datapars.fwhmpsf
    iraf.centerpars.calgorithm = 'centroid'
  elif 'small'.find(mode.lower()) != -1 or 'psf'.find(mode.lower()) != -1:
    iraf.photpars.aperture = 4. # close in for crowded regions
    iraf.centerpars.calgorithm = 'none'
  else:
    print '''use: aperture_photometry(image,threshold=4.,wcs="logical",mode="small")
    
    The following mode keywords are implemented: standard, large, psf, small'''
    raise NotImplementedError
  #for imagebi in fits_ext(image):
  sky,skydev=get_sky(image)
  iraf.datapars.datamin=sky-5*skydev
  iraf.datapars.sigma=skydev      
  #this could be changed to use Stdin and Stdout so that fewer files are written (saves space and time), but for debug purposes better keep all that
  iraf.daofind(image, output='default', verbose=False, verify=False, threshold=threshold) 
  iraf.daophot.phot(image, coords='default', output=image+photfilesuffix, verbose=False, verify=False, interactive=False, wcsout=wcs)
Exemple #10
0
def set_FLWO_params():
  '''Set some daophot parameters, which are typical for my FLWO images
  '''
  iraf.digiphot(_doprint=0)
  iraf.daophot(_doprint=0)
  iraf.datapars.fwhmpsf = 4.5
  iraf.datapars.datamax=40000.
  iraf.datapars.ccdread = "rdnoise"
  iraf.datapars.gain = "gain"
  iraf.datapars.exposure = "exptime"
  iraf.datapars.airmass = "air"
  iraf.datapars.filter = "filter"
  iraf.datapars.obstime="date-obs"
Exemple #11
0
def marksn2(img,fitstab,frame=1,fitstab2='',verbose=False):
   from pyraf import iraf
   from numpy import array   #,log10
   import lsc
   iraf.noao(_doprint=0)
   iraf.digiphot(_doprint=0)
   iraf.daophot(_doprint=0)
   iraf.images(_doprint=0)
   iraf.imcoords(_doprint=0)
   iraf.proto(_doprint=0)
   iraf.set(stdimage='imt1024')
   hdr=lsc.util.readhdr(fitstab)
   _filter=lsc.util.readkey3(hdr,'filter')
   column=lsc.lscabsphotdef.makecatalogue([fitstab])[_filter][fitstab]

   rasex=array(column['ra0'],float)
   decsex=array(column['dec0'],float)


   if fitstab2:
      hdr=lsc.util.readhdr(fitstab2)
      _filter=lsc.util.readkey3(hdr,'filter')
      _exptime=lsc.util.readkey3(hdr,'exptime')
      column=lsc.lscabsphotdef.makecatalogue([fitstab2])[_filter][fitstab2]
      rasex2=array(column['ra0'],float)
      decsex2=array(column['dec0'],float)

   iraf.set(stdimage='imt1024')
   iraf.display(img + '[0]',frame,fill=True,Stdout=1)
   vector=[]
   for i in range(0,len(rasex)):
      vector.append(str(rasex[i])+' '+str(decsex[i]))

   xy = iraf.wcsctran('STDIN',output="STDOUT",Stdin=vector,Stdout=1,image=img+'[0]',inwcs='world',units='degrees degrees',outwcs='logical',\
                         formats='%10.1f %10.1f',verbose='yes')[3:]
   iraf.tvmark(frame,'STDIN',Stdin=list(xy),mark="circle",number='yes',label='no',radii=10,nxoffse=5,nyoffse=5,color=207,txsize=2)

   if verbose:
 #     print 2.5*log10(_exptime)
      for i in range(0,len(column['ra0'])):
         print xy[i],column['ra0'][i],column['dec0'][i],column['magp3'][i],column['magp4'][i],column['smagf'][i],column['magp2'][i]

   if fitstab2:
      vector2=[]
      for i in range(0,len(rasex2)):
         vector2.append(str(rasex2[i])+' '+str(decsex2[i]))
      xy1 = iraf.wcsctran('STDIN',output="STDOUT",Stdin=vector2,Stdout=1,image=img+'[0]',inwcs='world',units='degrees degrees',outwcs='logical',\
                            formats='%10.1f %10.1f',verbose='yes')[3:]
      iraf.tvmark(frame,'STDIN',Stdin=list(xy1),mark="cross",number='yes',label='no',radii=10,nxoffse=5,nyoffse=5,color=205,txsize=2)
Exemple #12
0
def set_Pairitel_params():
  '''Set some daophot parameters, which are typical for my Pairitel images
  '''
  iraf.digiphot(_doprint=0)
  iraf.daophot(_doprint=0)
  iraf.apphot(_doprint=0)
  iraf.datapars.fwhmpsf = 4.5
  iraf.datapars.datamax=15000.
  iraf.centerpars.cbox = 9.0
  iraf.centerpars.calgorithm = "none"
  iraf.datapars.datamin = "INDEF"
  iraf.datapars.ccdread="RDNOISE" # 30. in 2MASS?
  iraf.datapars.gain = "GAIN"
  iraf.datapars.exposure = "EXPTIME"
  iraf.datapars.airmass = "AIRMASS"
  iraf.datapars.filter = "FILTER"
  iraf.datapars.obstime="DATE-OBS"
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
def make_iraf_psf(fname):

    with fits.open(fname) as stp:  # Read in image info placed in header previously
        header = stp[0].header
        ra = header["RA"]
        dec = header["DEC"]
        for ext in range(len(stp)):  # Look in each header section
            try:
                header = stp[ext].header
                expnum = header["EXPNUM"]
            except:
                continue

    stamp = fits.open(fname)
    for ext in range(
        len(stamp)
    ):  # Establish size of image and location of source in image (source should be centered in cutout)
        try:
            world_coords = wcs.WCS(stamp[ext].header)
            image_coords = world_coords.sky2xy(float(ra), float(dec))
            image_size = stamp[ext].data.shape
        except:
            continue

    # Some cutouts are split over multiple CCDs, meaning the source may not actually be in some of these small image 'pieces'
    if 0 < image_coords[0] < image_size[0] and 0 < image_coords[1] < image_size[1]:

        ccdnum = stamp[ext].header.get("EXTNO", None)

        psf_file = storage.get_file(expnum, ccd=ccdnum, ext="psf.fits")  # Get psf file for image from storage

        iraf.noao()
        iraf.digiphot()
        iraf.daophot(_doprint=0)
        psf_image = psf_file.replace("psf", "seepsf")

        # Seepsf creates psf_image
        iraf.seepsf(psf_file, psf_image, xpsf=image_coords[0], ypsf=image_coords[1])
        os.unlink(psf_file)

        return psf_image

    else:
        return None
def doPhot():
	print 'Running doPhot'
	try:
		mapFile = open('map.dat')
	except:
		print 'map.dat not found. Exiting.'
		sys.exit()
	try:
		changeFile = open('change.dat')
	except:
		print 'change.dat not found. Exiting.'
		sys.exit()

	coordList = list()
	for line in mapFile:
		coords = map(float,line.split())
		coordList.append(coords)
	coordList = np.array(coordList)

	iraf.noao()
	iraf.digiphot()
	iraf.daophot()
	iraf.ptools()
	iraf.set(clobber='yes')
	
	photFile = open('phot.dat', 'w')
	for line in changeFile.readlines():
		elms = line.split()
		imageName = elms[0]
		changeCoords = np.array([float(elms[1]),float(elms[2])])
		newCoords = coordList + changeCoords
		print 'Image: '+imageName
		coordFile = makeCoordFile(newCoords)
		
		iraf.noao.digiphot.daophot.phot(image=imagesDir+imageName, coords=coordFile, output='.temp-phot', skyfile='', verify='no', fwhmpsf=fwhmpsf, sigma=sigma, readnoise=readnoise, epadu=epadu, exposure=exposureHeader, obstime=obstimeHeader, calgorithm=calgorithm, cbox=cbox, apertures=apertures, annulus=annulus, dannulus=dannulus)

		result = iraf.noao.digiphot.ptools.txdump(Stdout=1, textfiles='.temp-phot', fields='mag, merr, otime', expr='yes')
		writeString = result[0].split()[-1] +' '+ ' '.join([' '.join(x.split()[:2]) for x in result])
		photFile.write(writeString+"\n")
		
	photFile.close()
	raw_input('doPhot Done. Hit return key to continue.')
Exemple #16
0
def main():
    print '=' * 50
    print '<<<<<run the aperphot.py >>>>>'

    namelst = glob.glob("*.obs")
    delete_file(namelst)
    print '<<<<<clear all the previous .obs file>>>>>'

    #读取参数 pos文件需提前写入
    fname = glob.glob('*.fits')
    posname = glob.glob("../*.pos")
    posname = posname[0]

    # --- readling list ---
    imgs = [i.strip() for i in fname]
    aper1, aper2, aper3 = read_aper()
    iraf.daophot()
    aperphot(imgs, posname, aper1, aper2, aper3)

    print '<<<<< run aperphot.py successfully >>>>>'
    print '=' * 50
Exemple #17
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
def get_star_data(asteroid_id, mag, expnum, header):
    """
    From ossos psf fitted image, calculate mean of the flux of each row of the rotated PSF
    """

    # calculate mean psf
    uri = storage.get_uri(expnum.strip('p'), header[_CCD].split('d')[1])
    ossos_psf = '{}.psf.fits'.format(uri.strip('.fits'))
    local_psf = '{}{}.psf.fits'.format(expnum, header[_CCD].split('d')[1])
    local_file_path = '{}/{}'.format(_STAMPS_DIR, local_psf)
    storage.copy(ossos_psf, local_file_path)

    # pvwcs = wcs.WCS(header)
    # x, y = pvwcs.sky2xy(asteroid_id['ra'].values, asteroid_id['dec'].values)
    x = asteroid_id[_XMID_HEADER].values[0]
    y = asteroid_id[_YMID_HEADER].values[0]

    # run seepsf on the mean psf image
    iraf.set(uparm="./")
    iraf.digiphot(_doprint=0)
    iraf.apphot(_doprint=0)
    iraf.daophot(_doprint=0)
    iraf.seepsf(local_file_path, local_psf, xpsf=x, ypsf=y, magnitude=mag)

    with fits.open(local_psf) as hdulist:
        data = hdulist[0].data

    th = math.degrees(asteroid_id[_THETA_HEADER].values[0])
    data_rot = rotate(data, th)
    data_rot = np.ma.masked_where(data_rot == 0, data_rot)

    data_mean = np.ma.mean(data_rot, axis=1)

    os.unlink(local_psf)
    os.unlink(local_file_path)

    return data_mean[np.nonzero(np.ma.fix_invalid(data_mean, fill_value=0))[0]]
Exemple #19
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 #20
0
def init(iraf_dir=DEFAULT_IRAF_DIR):
    '''Initialize IRAF for use in photometry
    @param iraf_dir The home directory for iraf, i.e. where login.cl
    and uparm are located, slightly annoying that we need this, but not
    much we can do about it'''
    global _initialized
    if _initialized:
        #already initialized no need to run again
        return
    global iraf
    global yes
    global no
    with workingDirectory(iraf_dir):
        from pyraf import iraf
        yes = iraf.yes
        no = iraf.no
        #now load the packages we need
        iraf.noao()
        iraf.digiphot()
        iraf.daophot()
        iraf.obsutil()

        _initialized = True
        return
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
def doaphot_psf_photometry(path, imageFile, extent, extension):

    data, image, hdulist, size, mid_point = load_image_data(
        imageFile, extent, extension=extension)
    # garbage collect data and image
    data = None
    image = None

    # import IRAF packages
    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)

    # dao_params.txt must be created manually using daoedit in iraf/pyraf for 5 stars
    dao_params = extract_dao_params(
        imageFile.strip(".fits") + "_dao_params.txt")

    sky = dao_params[0]
    sky_sigma = dao_params[1]
    fwhm = dao_params[2]
    datamin = dao_params[3]
    aperature_radius = dao_params[4]
    annulus_inner_radius = dao_params[5]
    annulus_outer_radius = dao_params[6]

    # get datapars
    datapars = extract_header_info(hdulist)

    datamax = datapars[0]
    ccdread = datapars[1]
    gain = datapars[2]
    readnoise = datapars[3]
    epadu = datapars[4]
    exposure = datapars[5]
    airmass = datapars[6]
    filter = datapars[7]
    obstime = datapars[8]
    itime = datapars[9]
    xairmass = datapars[10]
    ifilter = datapars[11]
    otime = datapars[12]

    # set datapars
    iraf.datapars.unlearn()
    iraf.datapars.setParam('fwhmpsf', fwhm)
    iraf.datapars.setParam('sigma', sky_sigma)
    iraf.datapars.setParam('datamin', datamin)
    iraf.datapars.setParam('datamax', datamax)
    iraf.datapars.setParam('ccdread', ccdread)
    iraf.datapars.setParam('gain', gain)
    iraf.datapars.setParam('readnoise', readnoise)
    iraf.datapars.setParam('epadu', epadu)
    iraf.datapars.setParam('exposure', exposure)
    iraf.datapars.setParam('airmass', airmass)
    iraf.datapars.setParam('filter', filter)
    iraf.datapars.setParam('obstime', obstime)
    iraf.datapars.setParam('itime', itime)
    iraf.datapars.setParam('xairmass', xairmass)
    iraf.datapars.setParam('ifilter', ifilter)
    iraf.datapars.setParam('otime', otime)

    # set photpars
    iraf.photpars.unlearn()
    iraf.photpars.setParam('apertures', aperature_radius)
    zp_estimate = iraf.photpars.getParam('zmag')
    # set centerpars
    iraf.centerpars.unlearn()
    iraf.centerpars.setParam('calgorithm', 'centroid')
    iraf.centerpars.setParam('cbox', 5.)

    # set fitskypars
    iraf.fitskypars.unlearn()
    iraf.fitskypars.setParam('annulus', annulus_inner_radius)
    iraf.fitskypars.setParam('dannulus', annulus_outer_radius)

    # run phot
    run_phot(imageFile, imageFile + ".quality.coo")

    # set daopars
    iraf.daopars.unlearn()
    iraf.daopars.setParam('function', 'auto')
    iraf.daopars.setParam('psfrad', 2 * int(fwhm) + 1)
    iraf.daopars.setParam('fitrad', fwhm)

    # select a psf/prf star
    # taking whatever the default selection is, can't see a way to pass coords of desired
    # stars, if could would use those in dao_params.txt
    # An alternative is to reorder the objects so those in dao_params.txt are at top of
    # sources table, assuming those are the defaults selected here.
    iraf.pstselect.unlearn()
    iraf.pstselect.setParam('maxnpsf', 5)
    iraf.pstselect(image=imageFile,
                   photfile=imageFile + ".mags.1",
                   pstfile=imageFile + ".pst.1",
                   interactive='no')

    # fit the psf
    iraf.psf.unlearn()
    iraf.psf(image=imageFile, \
             photfile=imageFile+".mags.1",\
             pstfile=imageFile+".pst.1",\
             psfimage=imageFile+".psf.1.fits",\
             opstfile=imageFile+".pst.2",\
             groupfile=imageFile+".psg.1",\
             interactive='no')
    # check the psf
    # perhaps pass it through ML and visualise
    # save visualisation for later manual checks
    iraf.seepsf.unlearn()
    iraf.seepsf(psfimage=imageFile + ".psf.1.fits",
                image=imageFile + ".psf.1s.fits")

    hdulist_psf = fits.open(imageFile + ".psf.1s.fits")
    #print "[*] plotting PSF for visual check."
    #plt.imshow(hdulist_psf[0].data, interpolation="nearest",cmap="hot")
    #plt.axis("off")
    #plt.show()

    # perform photometry
    run_allstar(imageFile, imageFile + ".psf.1.fits")
    return zp_estimate, imageFile + ".psf.1.fits"
Exemple #23
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
Exemple #24
0
def do_phot(inputImage,
            coord_list,
            fwhm,
            sky_annulus=20,
            width_sky=20,
            zeropoint=25.):
    """
    perform aperture photmoetry on the input image at the specified locations
    readnoise and epadu depends on telescope. The values written bellow are for 1.3, 512 * 512CCD
    """
    aper = str(1) + ":" + str(4 * float(fwhm)) + ":" + "0.5"
    print "\t Phot aperture: %s pixels" % (aper)
    print "\t Sky Annulus: %i -> %i pixels" % (sky_annulus,
                                               sky_annulus + width_sky)

    output = inputImage + ".phot"  #can be anything you like
    if os.access(output, os.F_OK):
        print "Removing previous photometry file"
        os.remove(output)
    print "\t Saving output files as %s" % (output)

    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)

    iraf.datapars.fwhmpsf = 4.
    iraf.datapars.datamin = 0.0
    iraf.datapars.datamax = 200000  #
    iraf.datapars.sigma = 4.  #  Standard deviation of background in counts
    iraf.datapars.readnoise = 4.3  #  Depending on CCD
    iraf.datapars.epadu = 1.4  #  Gain e/ADU; 1 frame

    iraf.centerpars.calgorithm = 'none'
    iraf.centerpars.cbox = 5
    iraf.fitskypars.annulus = sky_annulus  #  Inner radius of sky annulus in scale units
    iraf.fitskypars.dannulus = width_sky  #  Width of sky annulus in scale units

    iraf.photpars.apertures = aper
    iraf.photpars.zmag = zeropoint
    iraf.photpars.weighting = "constant"
    iraf.photpars.mkapert = "no"

    iraf.daophot.phot.interactive = "no"
    iraf.daophot.phot.verify = "no"
    iraf.daophot.phot.verbose = "no"

    iraf.fitskypars.salgo = "mode"
    '''
    iraf.fitskypars.skyval=0
    iraf.fitskypars.smaxi=10
    iraf.fitskypars.sloc=0
    iraf.fitskypars.shic=0
    iraf.fitskypars.snrej=50
    iraf.fitskypars.slorej=3.
    iraf.fitskypars.shirej=3.
    iraf.fitskypars.khist=3
    iraf.fitskypars.binsi=0.1
    '''

    #inputImage =inputImage + "[SCI,1]" #assumed, might be risky
    iraf.daophot.phot.coords = coord_list
    iraf.daophot.phot.output = output
    #iraf.phot.rdnoise=6.1, iraf.phot.gain=1.4  # on paper

    iraf.daophot.phot(inputImage,
                      output=output,
                      coords=coord_list,
                      verbose="no",
                      verify="no",
                      interactive="no")
    return output
Exemple #25
0
def marksn2(img, fitstab, frame=1, fitstab2='', verbose=False):
    from pyraf import iraf
    from numpy import array  #,log10
    import lsc
    iraf.noao(_doprint=0)
    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)
    iraf.images(_doprint=0)
    iraf.imcoords(_doprint=0)
    iraf.proto(_doprint=0)
    iraf.set(stdimage='imt1024')
    hdr = lsc.util.readhdr(fitstab)
    _filter = lsc.util.readkey3(hdr, 'filter')
    column = lsc.lscabsphotdef.makecatalogue([fitstab])[_filter][fitstab]

    rasex = array(column['ra0'], float)
    decsex = array(column['dec0'], float)

    if fitstab2:
        hdr = lsc.util.readhdr(fitstab2)
        _filter = lsc.util.readkey3(hdr, 'filter')
        _exptime = lsc.util.readkey3(hdr, 'exptime')
        column = lsc.lscabsphotdef.makecatalogue([fitstab2])[_filter][fitstab2]
        rasex2 = array(column['ra0'], float)
        decsex2 = array(column['dec0'], float)

    iraf.set(stdimage='imt1024')
    iraf.display(img + '[0]', frame, fill=True, Stdout=1)
    vector = []
    for i in range(0, len(rasex)):
        vector.append(str(rasex[i]) + ' ' + str(decsex[i]))

    xy = iraf.wcsctran('STDIN',output="STDOUT",Stdin=vector,Stdout=1,image=img+'[0]',inwcs='world',units='degrees degrees',outwcs='logical',\
                          formats='%10.1f %10.1f',verbose='yes')[3:]
    iraf.tvmark(frame,
                'STDIN',
                Stdin=list(xy),
                mark="circle",
                number='yes',
                label='no',
                radii=10,
                nxoffse=5,
                nyoffse=5,
                color=207,
                txsize=2)

    if verbose:
        #     print 2.5*log10(_exptime)
        for i in range(0, len(column['ra0'])):
            print xy[i], column['ra0'][i], column['dec0'][i], column['magp3'][
                i], column['magp4'][i], column['smagf'][i], column['magp2'][i]

    if fitstab2:
        vector2 = []
        for i in range(0, len(rasex2)):
            vector2.append(str(rasex2[i]) + ' ' + str(decsex2[i]))
        xy1 = iraf.wcsctran('STDIN',output="STDOUT",Stdin=vector2,Stdout=1,image=img+'[0]',inwcs='world',units='degrees degrees',outwcs='logical',\
                              formats='%10.1f %10.1f',verbose='yes')[3:]
        iraf.tvmark(frame,
                    'STDIN',
                    Stdin=list(xy1),
                    mark="cross",
                    number='yes',
                    label='no',
                    radii=10,
                    nxoffse=5,
                    nyoffse=5,
                    color=205,
                    txsize=2)
Exemple #26
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 #27
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 #28
0
def psffit(img, fwhm, psfstars, hdr, interactive, _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)
#        a1, a2, a3, a4, = int(5), int(8), int(10), int(12)
    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('_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 = int(a4)
    iraf.daopars.wsannul = int(a4)
    iraf.daopars.recenter = _center
    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+'.fits[0]', '_psf.mag', '_psf.pst', psfstars, interac=False, verify=False)

    iraf.psf(img+'.fits[0]', '_psf.mag', '_psf.pst', img + '.psf', '_psf.psto', '_psf.psg', interac=interactive,
             verify=False, verbose=False)

#    if os.path.isfile(img + '.psf.fits'):
#        print 'file there'

    iraf.group(img+'.fits[0]', '_psf.mag', img + '.psf.fits', '_psf.grp', verify=False, verbose=False)
    iraf.nstar(img+'.fits[0]', '_psf.grp', img + '.psf.fits', '_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 #29
0
def phot(im,coords,radii,
         useOtherData=None,
         aperCorrs=[],
         refImDir='refImages',
         cent='centroid',
         xxx='',
         linearInterp=False,
         psfInterp=False):

    """Aperture photometry of WFC3 ir/uv1,2 images.

    This code uses iraf's phot package to perform
    simple aperture photometry.

    It then returns a list of arrays, where each entry 
    is the magnitudes and errors for every coordinate pair 
    given.

    It can do three different versions. The most
    basic (and default) is photometry where
    all pixels with imquality flags>0 are 
    ignored. This is the fastest, but typically 
    produces faintward biased answers.

    Another technique is the use of linear interpolation
    using the nearest four good pixels in a cross pattern
    that bound the bad pixel. This produces less biased
    answers the the simple verion. WARNING: not very well
    tested! This is set with linearInterp=True

    The last and most reliable technique (also the slowest)
    uses tinytim psfs to perform the interpolation. All
    good pixels within the aperture are used to match the
    psf flux to that observed. The bad pixels are sampled
    off the psf and scaled to the correct flux to produce
    the corrected image from which the photometry is 
    acquired. This is set with psfInterp=True

    Inputs:
    im - the image filename without the '.fits'.
    coords - an array of coordinates in form [x,y]. These
             are in iraf coordinates ie. (+1,+1) from pyfits
             coordinates!
    radii - an array of radii [r1,r2,r3,..] float acceptable.
    aperCorrs - an array of aperture corrections to apply.
    refImDir='refImages' - the directory with reference images.
                           This will actually be ../refImages
    cent='centroid' - perform an iraf centroid as default.
                      set to 'none' if no centroid desired.
                      
                      if centroiding, returns IRAF type 
                      coordinates.

    xxx='' - a string to add to the corrected image filename.
    """

    if psfInterp:
        import WFC3_PSFFits as P
        import sys

    from pyraf import iraf

    ###for pyraf
    yes=1
    no=0

    #import the necessary packages
    iraf.digiphot()
    iraf.daophot()
    
    #set local parameters
    iraf.set(uparm="./")
    
    iraf.daophot.verbose=no
    iraf.daophot.verify=no
    iraf.daophot.update=no
    
    iraf.centerpars.calgori=cent
    iraf.centerpars.maxshift=2.
    iraf.centerpars.minsnra=1.0
    #iraf.daopars.fitrad=1.0      #enough to leave set
    
    iraf.photpars.apertur=2
    iraf.photpars.zmag=0.0
    
    iraf.datapars.fwhmpsf=2.
    iraf.datapars.sigma=0.0
    iraf.datapars.datamin=-1000
    iraf.datapars.datamax=45000
    iraf.datapars.ccdread=""
    iraf.datapars.gain=""
    iraf.datapars.readnoi=0.0
    iraf.datapars.epadu=1.0
    iraf.datapars.exposure=""
    iraf.datapars.itime=1.0
    iraf.datapars.airmass=""
    iraf.datapars.filter=""
    iraf.datapars.obstime=""
    
    iraf.fitskypars.salgori='mode'
    iraf.fitskypars.annulus=max(radii)*1.2
    iraf.fitskypars.dannulu=8
    
    #save the new parameters
    iraf.datapars.saveParList()
    iraf.photpars.saveParList()
    #iraf.daopars.saveParList()
    iraf.centerpars.saveParList()
    iraf.daophot.saveParList()
    iraf.fitskypars.saveParList()



    if '.fits' in im: fn=im
    else: fn=im+'.fits'
    
    #get the image data
    ha=pyfits.open(fn)
    header0=ha[0].header
    header1=ha[1].header
    data=ha[1].data.astype('f')
    error=ha[2].data
    qual=ha[3].data

    if useOtherData<>None: data=useOtherData
    (a,b)=data.shape
        
    DETECTOR=header0['DETECTOR']
    FILTER=string.strip(header0['FILTER'])
    GAIN=float(header0['CCDGAIN'])
    EXPTIME=float(header0['EXPTIME'])
    if DETECTOR=="UVIS":
        PHOTZPT=float(header1['PHOTZPT'])
        PHOTFLAM=float(header1['PHOTFLAM'])
        data/=EXPTIME
        error/=EXPTIME
    else:
        PHOTZPT=float(header0['PHOTZPT'])
        PHOTFLAM=float(header0['PHOTFLAM'])
    XCEN=int(float(header1['CENTERA1']))
    YCEN=int(float(header1['CENTERA2']))
    XSIZE=int(float(header1['SIZAXIS1']))
    YSIZE=int(float(header1['SIZAXIS2']))

    #headers are f****d in some of the ir images. This is a 
    #cluge to fix that
    if XSIZE==522: XSIZE=512
    if YSIZE==522: YSIZE=512


    ###get the flat
    #flathan=pyfits.open('flat_'+FILTER+'.fits')
    #flatdat=flathan[1].data
    #flathan.close()

    #get the pixel area map
    if DETECTOR=='UVIS':
        if YCEN<2051:
            #chip 1
            name='UVIS1wfc3_map.fits'
            detector='uv1'
        else:
            #chip 2
            name='UVIS1wfc3_map.fits'
            detector='uv2'
    else:
        name='ir_wfc3_map.fits'
        detector='ir'
    PAMhan=pyfits.open(refImDir+'/'+name)
    (A,B)=PAMhan[1].data.shape
    if (a==YSIZE or a==A):
        my=0
        My=a
    else:
        if detector=='uv1' or detector=='uv2':
            my=YCEN-YSIZE/2-2051-1
            My=my+YSIZE+1
        else:
            my=YCEN-YSIZE/2-1
            My=my+YSIZE+1

    if (b==XSIZE or b==B):
        mx=0
        Mx=b
    else:
        mx=XCEN-XSIZE/2-1
        Mx=mx+XSIZE+1
    PAMdata=PAMhan[1].data.astype('f')[my:My,mx:Mx]
    data*=PAMdata
    error*=PAMdata
    #data/=flatdat

    #get the hand-selected bad-pixel map
    badHan=open(im+'.fits.badPix')
    dunk=badHan.readlines()
    badHan.close()
    badPixManual=[]
    for ii in range(len(dunk)):
        s=dunk[ii].split()
        xbp=int(float(s[0]))
        ybp=int(float(s[1]))
        for yy in range(-1,2):
            for xx in range(-1,2):
                badPixManual.append([xbp-1+xx,ybp-1+yy])

    #fill in all the bad pixels using the median bg value around the images 
    #edges (4 pixels wide) and with a linear interpolation within the 
    #central area near each individual input coordinate
    #ldata=data.reshape(a*b)*1.0
    #ldata.sort()
    #median=ldata[a*b/2]
    
    median=num.median(data)
    noise=num.std(data)
    
    #at least mark each manually identified bac pixel with the median of the image
    for ii in range(len(badPixManual)):
            data[badPixManual[ii][1],badPixManual[ii][0]]=median
    
    """
    for i in range(a):
        for j in range(4):
            if qual[i][j]>0:
                data[i][j]=median
            if qual[i][b-j-1]>0:
                data[i][b-j-1]=median
    for i in range(b):
        for j in range(4):
            if qual[j][i]>0:
                data[j][i]=median
            if qual[a-j-1][i]>0:
                data[a-j-1][i]=median
    """          
    

    if psfInterp:
        for kk in range(len(coords)):
            xp=int(coords[kk][0]-1+0.5)
            yp=int(coords[kk][1]-1+0.5)
            R=int(max(radii))+2

            x=[]
            y=[]
            gx=[]
            gy=[]
            for i in range(yp-R,yp+R):
                for j in range(xp-R,xp+R):
                    if (j<0 or j>b or i<0 or i>a): continue
                    
                    if qual[i][j]>0:# or ([j,i] in badPixManual):

                        x.append(j)
                        y.append(i)
                    else:
                        gx.append(j)
                        gy.append(i)
            
            bad_x=num.array(x)
            bad_y=num.array(y)
            good_x=num.array(gx)
            good_y=num.array(gy)
            
            if len(x)==0: continue

            [d,f]=im.split('/')
            os.chdir(d)
            
            FILTER=FILTER.replace('W','w').replace('M','m')
            obj=P.PSFFit(f+'.fits',1,useMemory='n')
            obj.initObject(0,coords[kk][0]-1,coords[kk][1]-1,1,3.5)
            try: os.remove('genImage.fits')
            except: pass
            try: os.remove('genImagePSF.fits')
            except: pass
            obj.produceImage(write='y')
            
            psfHan=pyfits.open('genImage.fits')
            psfData=(psfHan[0].data/EXPTIME)*PAMdata
            psfHan.close()
            
            
            try: os.remove('genImage.fits')
            except: pass
            try: os.remove('genImagePSF.fits')
            except: pass
            
            os.chdir('../')
            
            
            xmin=max(0,xp-R)
            xmax=min(b,xp+R)
            ymin=max(0,yp-R)
            ymax=min(a,yp+R)


            dcut=data[good_y,good_x]
            psfCut=psfData[good_y,good_x]
            w=num.where(dcut>median+median**0.5)[0]
            multi=num.sum(dcut[w]/psfCut[w])/len(w)
            
            data[bad_y,bad_x]=psfData[bad_y,bad_x]*multi+median

    if linearInterp: #probably will fail if the peak of the psf is on a bad pixel!
        for kk in range(len(coords)):                    
            xp=int(coords[kk][0]-1+0.5)
            yp=int(coords[kk][1]-1+0.5)
            R=int(max(radii))+2
            for i in range(yp-R,yp+R):
                for j in range(xp-R,xp+R):
                    if (j<0 or j>b or i<0 or i>a): continue
                    
                    if qual[i][j]>0 or ([j,i] in badPix):
                        x=[]
                        y=[]
                        z=[]
                        val=-1
                        c=-1
                        while val==-1:
                            if qual[i+c][j]==0:
                                val=data[i+c][j]
                            else:
                                c-=1
                        x.append(j)
                        y.append(i+c)
                        z.append(val)
                        
                        val=-1
                        c=1
                        while val==-1:
                            if qual[i+c][j]==0:
                                val=data[i+c][j]
                            else:
                                c+=1
                        x.append(j)
                        y.append(i+c)
                        z.append(val)
                        
                        val=-1
                        c=-1
                        while val==-1:
                            if qual[i][j+c]==0:
                                val=data[i][j+c]
                            else:
                                c-=1
                        x.append(j+c)
                        y.append(i)
                        z.append(val)
                        
                        val=-1
                        c=1
                        while val==-1:
                            if qual[i][j+c]==0:
                                val=data[i][j+c]
                            else:
                                c+=1
                                
                        x.append(j+c)
                        y.append(i)
                        z.append(val)
                        
                        inte=interp.bisplrep(x,y,z,kx=1,ky=1)
                        value=interp.bisplev([i],[j],inte)
                        data[i][j]=value
                        #print "BORKED! Something about linear interpolation produces negative pixel values. This has not been checked to see if it's OK. It could just be noise. Though it might not be!!!"


    #write the coordinates file
    han=open('spot','w+')
    for ii in range(len(coords)):
        print >>han,coords[ii][0],coords[ii][1]
    han.close()

    #setup the necessary keywords
    iraf.datapars.gain='CCDGAIN'
    iraf.datapars.itime=1.0
    iraf.datapars.saveParList()
    iraf.photpars.zmag=PHOTZPT

    
    #write out the interpolated image for photometry

    #get centroid
    #only works on the non-phot-flam-ed image
    if cent=='centroid':

                            
        try:
            os.remove('junk'+xxx+'_cent.fits')
            os.remove('junk'+xxx+'_cent.fits1.ctr.1')
        except:
            pass
        for ii in range(a):
            ha[1].data[ii,:]=data[ii,:]
        ha.writeto('junk'+xxx+'_cent.fits')


        iraf.photpars.apertur=radii[0]
        iraf.photpars.saveParList()

        iraf.center(image='junk'+xxx+'_cent.fits[1]',coords='spot',verify=no,interactive=no)
        Zing= iraf.txdump(textfiles = 'junk'+xxx+'_cent.fits1.ctr.1', fields = 'XCENTER,YCENTER', expr=iraf.yes, Stdout=1)
        
        try:
            os.remove('junk'+xxx+'_cent.fits1.ctr.1')
        except:
            pass

        XC=[]
        YC=[]
        ZZ=Zing[0].split()
        for ii in range(len(ZZ)/2):
            if 'INDEF' not in ZZ[0]:
                XC.append(float(ZZ[0]))
                YC.append(float(ZZ[1]))
            else:
                XC.append(-32768.)
                YC.append(-32768.)

        #write the new centroided coordinates file
        han=open('spot','w+')
        for ii in range(len(coords)):
            print >>han,XC[ii],YC[ii]
        han.close()

        iraf.centerpars.calgori=cent
        iraf.centerpars.saveParList()
    else:
        XC=[]
        YC=[]
        for ii in range(len(coords)):
            XC.append(coords[ii][0])    
            YC.append(coords[ii][1])


    data*=PHOTFLAM
    error*=PHOTFLAM
    
    for ii in range(a):
        ha[1].data[ii,:]=data[ii,:]

    try:
        os.remove('junk'+xxx+'.fits')
    except:
        pass

    ha.writeto('junk'+xxx+'.fits')
    ha.close()


    #get the quadrature sum photometric error approximately
    repFac=1
    E=num.repeat(num.repeat(error,repFac,axis=0),repFac,axis=1)/float(repFac*repFac)
    xcoord=num.repeat(num.array([num.arange(b*repFac)+1.]),(a)*repFac,axis=0)/float(repFac)
    ycoord=num.repeat(num.transpose(num.array([num.arange(a*repFac)+1.])),b*repFac,axis=1)/float(repFac)

    

    ES=[]
    for i in range(len(coords)):
        ES.append([])
        for kk in range(len(radii)):
            EEr=E*1.0
            dist=((xcoord-XC[i])**2+(ycoord-YC[i])**2)**0.5
            w=num.where(dist>radii[kk])
            EEr[w]*=0.0
            
            ES[i].append(num.sqrt(num.sum(EEr**2)))
            



    MS=[]
    MES=[]
    for i in range(len(coords)):
        MS.append([])
        MES.append([])
        
    for kk in range(len(radii)):
        iraf.photpars.apertur=radii[kk]
        iraf.photpars.saveParList()


        try:
            os.remove('spottedm')
        except:
            pass
        iraf.daophot.phot(image='junk'+xxx+'.fits[1]', coords='spot', output= 'spottedm', interactive=no, verbose=no)


        ####magnitude equation is
        ####  m = Z-2.5*LOG(f_DN*PHOTFLAM)
        ####magnitude error is then
        ####  dm=1.09/sqrt(f_DN*EXPTIME*GAIN)
        #### Remember that to get f back in ADU we need to divide by PHOTFLAM
        #### 0.95 included to consider approximate qe effect
        Zing= iraf.txdump(textfiles = 'spottedm', fields = 'MAG,FLUX,SUM', expr=iraf.yes, Stdout=1)

        for i in range(len(MS)):
            ZZ=Zing[i].split()
            #print ZZ
            if 'INDEF' not in ZZ[0]:
                magVal=float(ZZ[0])
                if aperCorrs<>[]: magVal-=aperCorrs[i]
                MS[i].append(magVal)
                
                ES[i][kk]*=2.5/num.log(10)
                ES[i][kk]/=float(ZZ[1])
                
                flux=float(ZZ[1])/PHOTFLAM
                dflux=num.sqrt(float(ZZ[2])/PHOTFLAM)

                #print flux,dflux
                MES[i].append(1.09*dflux/(flux*num.sqrt(GAIN*EXPTIME/0.95)))

            else:
                MS[i].append(-32768)
                MES[i].append(-32768)

    try:
        os.remove('junk'+xxx+'_cent.fits')
    except:
        pass
    try:
        os.remove('junk'+xxx+'.fits')
    except:
        pass


    if cent<>'none':
        return (MS,ES,median,XC,YC)
    else:
        return (MS,ES,median)
Exemple #30
0
def execute():

    ################NGC1023####################################################
    ## use GALFIT to fit and to create a model disk vs bulge##
    ##galfit.feedme##

    iraf.images()

    # path to fits files:
    total = config.iraf_input_dir+config.totalfits
    bulge = config.iraf_input_dir+config.bulgefits
    fraction = config.iraf_tmp_dir+config.fractionfits

    input_dir = config.iraf_input_dir
    tmp_dir = config.iraf_tmp_dir
    if not os.path.exists(tmp_dir):
        os.mkdir(tmp_dir)

    if os.path.exists(fraction):
         iraf.imdelete(fraction)

    print fraction
    print bulge

    # Makes the fraction image by dividing the bulge by the total:
    iraf.imarith(bulge, "/", total, fraction)

    """ LLT: I don't think we need this anymore:

    if os.path.exists(tmp_dir+"snb.fits"):
        iraf.module.imdelete(tmp_dir+"snb.fits")
    if os.path.exists(tmp_dir+"tnb.fits"):
        iraf.module.imdelete(tmp_dir+"tnb.fits")

    iraf.module.imcopy(bulge, tmp_dir+"snb.fits")
    iraf.module.imcopy(total, tmp_dir+"tnb.fits")
    """

    ##########################################################################
    ##Note: the coordinate in pixel as obtained from RA and Dec differ from the
    ##pixels one because of distorsion corrections as done with astrometry==>

    ##RA and Dec coordinate in arcsec are the correct one and don't need do be
    ##transfer in pixels

    ############################################################################
    ##Consistency check

    #displ(image="../fits_inputs/galaxy.fits",frame=1) 
    #tvmark(frame=1,coords="Kall.dat",color=205,lab-,num+)

    #displ(image="f.fits",zrange=no,zscale=no,ztrans="linear",z1=0,z2=1,frame=3) 
    #tvmark(frame=3,coords="Kall.dat",color=201,mark="rectangle",lengths="6",lab-,num-)

    ###########################################################################
    ##Use program overplot.sm to create both Kall.dat & compagna.dat
    ##in the RA & Dec of PNS there is a rotation of 90deg respect to the pixel
    ## x-->-x 
    ##after long reflexion I decide the right PA is 90-47.11=42.89 (just turning the PNe as observed on the R image)

    ##########################################################################
    ############################################################################

    ## to measure the relative flux use phot (daophot) ##with centerpars=none!!!###

    ##I used sigma=stdv(skys)=0  ===> no bg 
    ##fwhmpsf=2.27 as in galfit psf2.fits (imexam A enclosed)
    ##skyvalue=0 ===> no bg

    ##no centerpars
    ##apertures=3.0 pixels (=1.8 arcsec)

    ##zmag=25

    ##snb.fits & tnb.fits without bg (when I made the model)

    # filenames

    silentremove(config.bulgemag)
    silentremove(config.bulgetxt)
    silentremove(config.totalmag)
    silentremove(config.totaltxt)
    silentremove(config.fractionmag)
    silentremove(config.fractiontxt)

    iraf.noao(_doprint=False)
    iraf.digiphot(_doprint=False)
    iraf.daophot(_doprint=False)

    iraf.phot(bulge,config.position_file, config.bulgemag, skyfile="", datapars="",scale=1.,fwhmpsf=2.27,emissio="yes",sigma=0.0,datamin=-100, datamax=1e5,noise="poisson", centerpars="",calgorithm="none",cbox=0,cthreshold=0.,minsnratio=1., cmaxiter=0,maxshift=0,clean="no",rclean=0.,rclip=0.,kclean=0., mkcenter="no", fitskypars="",salgorithm="constant",annulus=10,dannulus=10,skyvalue=0., smaxiter=10,sloclip=0.,shiclip=0.,snreject=50,sloreject=3., shireject=3.,khist=3.,binsize=0.1,smooth="no",rgrow=0., mksky="no", photpars="",weighting="constant",apertures=3,zmag=0.,mkapert="no", interactive="no",radplots="no",verify="no",update="no",verbose="no", graphics="stdgraph",display="stdimage",icommands="",gcommands="")

    # LLT: Corrected to pyraf, use stdout = instead of >>
    iraf.txdump(config.bulgemag, "XCENTER,YCENTER,STDEV,FLUX,SUM,AREA,ID", "yes", headers="no",paramet="no", Stdout=config.bulgetxt)

    iraf.phot(total, config.position_file, config.totalmag, skyfile="", datapars="",scale=1.,fwhmpsf=2.27,emissio="yes",sigma=0.0,datamin=-100, datamax=1e5,noise="poisson", centerpars="",calgorithm="none",cbox=0,cthreshold=0.,minsnratio=1., cmaxiter=0,maxshift=0.,clean="no",rclean=0.,rclip=0.,kclean=0., mkcenter="no", fitskypars="",salgorithm="constant",annulus=10,dannulus=10,skyvalue=0., smaxiter=10,sloclip=0.,shiclip=0.,snreject=50,sloreject=3., shireject=3.,khist=3.,binsize=0.1,smooth="no",rgrow=0., mksky="no", photpars="",weighting="constant",apertures=3,zmag=25.,mkapert="no", interactive="no",radplots="no",verify="no",update="no",verbose="no", graphics="stdgraph",display="stdimage",icommands="",gcommands="")

    iraf.txdump(config.totalmag, "XCENTER,YCENTER,STDEV,FLUX,SUM,AREA,ID", "yes", headers="no",paramet="no", Stdout=config.totaltxt)

    iraf.phot(fraction,config.position_file, config.fractionmag, skyfile="", datapars="", scale=1., fwhmpsf=2.27, emissio="no", sigma=0.0, datamin=-100, datamax=1e5, noise="poisson", centerpars="", calgorithm="none", cbox=0, cthreshold=0., minsnratio=1., cmaxiter=0, maxshift=0., clean="no", rclean=0., rclip=0., kclean=0., mkcenter="no", fitskypars="", salgorithm="constant", annulus=10, dannulus=10, skyvalue=0., smaxiter=10, sloclip=0., shiclip=0., snreject=50, sloreject=3., shireject=3., khist=3., binsize=0.1, smooth="no", rgrow=0., mksky="no", photpars="", weighting="constant", apertures=3, zmag=25., mkapert="no", interactive="no", radplots="no", verify="no", update="no", verbose="no", graphics="stdgraph", display="stdimage", icommands="", gcommands="")

    iraf.txdump(config.fractionmag, "XCENTER,YCENTER,STDEV,FLUX,SUM,AREA,ID", "yes", headers="no",paramet="no", Stdout=config.fractiontxt)
Exemple #31
0
def psfphot(image,
            coofile,
            ot,
            wtimage="",
            varorder=1,
            clobber=globclob,
            verbose=globver,
            pixtol=3.0,
            maxnpsf=25):
    """ perform PSF-based photometry on a single target star (SN?) at RA, Dec and  
        also on a set of comparison stars, using daophot.  simultaneously 
        perform aperture photometry on all the comparison stars (after 
        subtracting off contributions from neighbors) to enable absolute 
        photometry by comparison to aperture photometry of standard stars 
        observed in other fields """

    # Defaults / constants
    psfmult = 5.0  #standard factor (multiplied by fwhm to get psfradius)
    psfmultsmall = 3.0  #similar to psfmult, adjusted for nstar and substar

    # Necessary package
    iraf.imutil()
    iraf.digiphot()
    iraf.daophot()

    # Detect stars
    iqobjs("%s.sub.fits" % image[:-5],
           1.5,
           12000.0,
           wtimage=wtimage,
           skyval="0.0")

    root = image[:-5]
    [gain, rnoise, fwhm] = get_head(image, ["GAIN", "READN", "SEEPIX"])
    fwhm = float(fwhm)
    rnoise = float(rnoise)

    iraf.iterstat(image)

    # Saturation level
    if not check_head(image, "SATURATE"):
        saturate = 60000.0
    else:
        saturate = get_head(image, "SATURATE")

    # Update datapars and daopars
    iraf.datapars.fwhmpsf = fwhm
    iraf.datapars.sigma = iraf.iterstat.sigma
    iraf.datapars.datamin = iraf.iterstat.median - 10 * iraf.iterstat.sigma
    iraf.datapars.datamax = 0.50 * saturate
    iraf.datapars.readnoise = rnoise
    iraf.datapars.epadu = gain
    iraf.daopars.psfrad = psfmult * fwhm
    iraf.daopars.fitrad = fwhm
    iraf.daopars.function = "gauss,moffat15,moffat25,lorentz,penny1"
    iraf.daopars.varorder = varorder

    # Reference stars file
    stars = Starlist(coofile)
    stars.wcs2pix(image)
    outf = open("%s.coo.1" % image[:-5], "w")
    for star in stars:
        outf.write("%10.3f%10.3f\n" % (star.xval, star.yval))
    outf.close()

    #Aperture photometry
    iraf.daophot.phot(root,
                      'default',
                      'default',
                      apertures=fwhm,
                      verify=no,
                      interac=no,
                      verbose=verbose)

    iraf.datapars.datamax = 0.50 * saturate
    iraf.pstselect(root,
                   'default',
                   'default',
                   maxnpsf,
                   interactive=no,
                   verify=no,
                   verbose=verbose)

    iraf.psf(root,
             'default',
             'default',
             'default',
             'default',
             'default',
             interactive=no,
             showplots=no,
             verify=no,
             verbose=verbose)

    iraf.allstar(root,
                 'default',
                 'default',
                 'default',
                 'default',
                 'default',
                 verify=no,
                 verbose=verbose)

    # Prep for subtracted image
    iraf.iterstat("%s.sub.fits" % root)

    iraf.datapars.sigma = iraf.iterstat.sigma
    iraf.datapars.datamin = iraf.iterstat.median - 10 * iraf.iterstat.sigma
    iraf.datapars.datamax = saturate

    # Look for source at OT location
    substars = Starlist("%s.sub.fits.stars" % image[:-5])
    otstars = Starlist(ot)
    otstars.wcs2pix("%s.sub.fits" % image[:-5])
    smatch, omatch = substars.match(otstars, tol=pixtol, useflags=no)

    # Generate coo file
    otcoo = open("%s.sub.coo.1" % image[:-5], "w")

    if len(smatch) == 0:
        otcoo.write("%10.3f%10.3f\n" % (otstars[0].xval, otstars[0].yval))
    else:
        otcoo.write("%10.3f%10.3f\n" % (smatch[0].xval, smatch[0].yval))

    otcoo.close()

    iraf.daophot.phot("%s.sub.fits" % root,
                      "%s.sub.coo.1" % image[:-5],
                      'default',
                      'default',
                      apertures=fwhm,
                      calgorithm="none",
                      interac=no,
                      verify=no,
                      verbose=verbose)

    if len(smatch) == 0:
        print "No match in subtracted image: %s.sub.fits" % root
    else:
        iraf.allstar("%s.sub.fits" % root,
                     'default',
                     "%s.psf.1.fits" % root,
                     'default',
                     'default',
                     'default',
                     verify=no,
                     verbose=no)

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

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

  print("A. Find stars using daofind")

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


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

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

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

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

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

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


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

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

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

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

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

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

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

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

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



  #Delete intermediate images 
  for f in ("rmask.fits","redit.fits","maskimage.fits","maskfile.mag","maskfile.satmag","temp.file","withsky.fits","mask.fits"):
    silentDelete(f)
Exemple #33
0
def makeillumination(lista, flatfield):  #,outputfile,illum_frame):
    import os, glob, string, re
    from astropy.io import fits as pyfits
    import ntt
    from ntt.util import readhdr, readkey3, delete, display_image, defsex, name_duplicate, correctcard
    from numpy import compress, array, argmax, argmin, min, argsort, float32
    import datetime
    MJDtoday = 55927 + (datetime.date.today() -
                        datetime.date(2012, 01, 01)).days
    _date = readkey3(readhdr(lista[0]), 'date-night')
    _filter = readkey3(readhdr(lista[0]), 'filter')
    illum_frame = name_duplicate(
        lista[0], 'illum_' + _date + '_' + _filter + '_' + str(MJDtoday), '')
    from pyraf import iraf
    iraf.images(_doprint=0, Stdout=0)
    iraf.imutil(_doprint=0, Stdout=0)
    iraf.utilities(_doprint=0, Stdout=0)
    iraf.noao(_doprint=0, Stdout=0)
    iraf.imred(_doprint=0, Stdout=0)
    iraf.ccdred(_doprint=0, Stdout=0)
    iraf.digiphot(_doprint=0, Stdout=0)
    iraf.daophot(_doprint=0, Stdout=0)
    iraf.generic(_doprint=0, Stdout=0)
    toforget = [
        'digiphot.daophot', 'imutil.imarith', 'image', 'utilities.surfit'
    ]
    for t in toforget:
        iraf.unlearn(t)
    n = len(lista)
    #   start loop to read image names from the input file
    lista1 = []
    iraf.ccdred.verbose = 'no'
    ff = open('templist.lst', 'w')
    for i in range(0, len(lista)):
        ff.write('C' + lista[i] + '\n')
        delete('C' + lista[i])
        delete('C' + re.sub('.fits', '_sub.fits', lista[i]))
        ntt.sofiphotredudef.crosstalk(lista[i], 'C' + lista[i])
        iraf.noao.imred.ccdred.ccdproc('C' + lista[i],
                                       output='',
                                       overscan="no",
                                       trim="yes",
                                       ccdtype='',
                                       darkcor='no',
                                       fixpix='no',
                                       zerocor="no",
                                       flatcor='yes',
                                       illumco='no',
                                       trimsec='[1:1024,1:1007]',
                                       biassec='',
                                       flat=flatfield,
                                       illum='')
        correctcard('C' + lista[i])
        lista1.append('C' + lista[i])
    ff.close()
    print '\n### prereducing STD frames to compute illumination correction ........'
    lista2, skyfile = ntt.sofiphotredudef.skysub(
        lista1, readkey3(readhdr(lista1[0]), 'ron'),
        readkey3(readhdr(lista1[0]), 'gain'), True)
    lista2 = ntt.sofiphotredudef.sortbyJD(lista2)
    print '\n### use x on the star and q  to continue....'
    display_image(lista2[0], 2, '', '', False)
    delete('tmpone.coo')
    iraf.image.tv.imexamine(lista2[0],
                            2,
                            logfile='tmpone.coo',
                            keeplog='yes',
                            xformat='',
                            yformat='',
                            wcs='logical')
    iraf.tvmark(2,
                'tmpone.coo',
                mark="circle",
                number='yes',
                label='no',
                radii=8,
                nxoffse=5,
                nyoffse=5,
                color=204,
                txsize=2)
    xycoo = iraf.proto.fields('tmpone.coo', '1,2', Stdout=1)
    x0, y0 = string.split(xycoo[0])
    x0 = float(x0)
    y0 = float(y0)
    xcum0 = readkey3(readhdr(lista2[0]), 'xcum')
    ycum0 = readkey3(readhdr(lista2[0]), 'ycum')
    iraf.digiphot(_doprint=0, Stdout=0)
    iraf.daophot(_doprint=0, Stdout=0)
    iraf.noao.digiphot.daophot.datapars.datamin = -1000
    iraf.noao.digiphot.daophot.datapars.datamax = 60000
    iraf.noao.digiphot.daophot.daopars.function = 'gauss'
    iraf.noao.digiphot.daophot.photpars.zmag = 0
    namesex = defsex('default.sex')
    for i in range(0, len(lista2)):
        j = i + 1
        xcum = readkey3(readhdr(lista2[i]), 'xcum')
        ycum = readkey3(readhdr(lista2[i]), 'ycum')
        xx = x0 - xcum0 + xcum
        yy = y0 - ycum0 + ycum
        # sex objects
        os.system('sex ' + lista2[i] + ' -c ' + namesex + '>  _logsex')
        delete('_logsex')
        xpix = iraf.proto.fields('detections.cat', fields='2', Stdout=1)
        ypix = iraf.proto.fields('detections.cat', fields='3', Stdout=1)
        cm = iraf.proto.fields('detections.cat', fields='4', Stdout=1)
        cm = compress((array(xpix) != ''), array(cm, float))
        ypix = compress((array(xpix) != ''), array(ypix, float))
        xpix = compress((array(xpix) != ''), array(xpix, float))
        if len(xpix) > 300:
            num = 300
        else:
            num = len(xpix) - 1
        xpix = xpix[argsort(cm)][0:num]
        ypix = ypix[argsort(cm)][0:num]
        distance = (ypix - yy)**2 + (xpix - xx)**2
        xx1, yy1 = xpix[argmin(distance)], ypix[argmin(distance)]
        f = open('tmpone.coo', 'w')
        f.write(str(xx1) + ' ' + str(yy1) + '\n')
        f.close()
        display_image(lista2[i], 1, '', '', False)
        iraf.tvmark(1,
                    'tmpone.coo',
                    mark="circle",
                    number='yes',
                    label='no',
                    radii=8,
                    nxoffse=5,
                    nyoffse=5,
                    color=204,
                    txsize=2)
        answ = 'n'
        while answ != 'y':
            answ = raw_input('selected the right one [[y]/n] ?')
            if not answ:
                answ = 'y'
            if answ in ['y', 'YES', 'yes', 'Y']:
                print lista2[i]
                delete('pippo.' + str(j) + '.mag')
                gggg = iraf.digiphot.daophot.phot(lista2[i],
                                                  "tmpone.coo",
                                                  output="pippo." + str(j) +
                                                  ".mag",
                                                  verify='no',
                                                  interac='no',
                                                  Stdout=1)
                try:
                    float(string.split(gggg[0])[3])
                    answ = 'y'
                except:
                    print '\n### warning'
                    answ = 'n'
            else:
                print '\n### select the std star'
                display_image(lista2[i], 1, '', '', False)
                iraf.image.tv.imexamine(lista2[i],
                                        1,
                                        logfile='tmpone.coo',
                                        keeplog='yes',
                                        xformat='',
                                        yformat='',
                                        wcs='logical')
                xycoo = iraf.proto.fields('tmpone.coo', '1,2', Stdout=1)
                x2, y2 = string.split(xycoo[0])
                f = open('tmpone.coo', 'w')
                f.write(str(x2) + ' ' + str(y2) + '\n')
                f.close()
                delete('pippo.' + str(j) + '.mag')
                print '###### new selection ' + str(x2), str(y2)
                gggg = iraf.digiphot.daophot.phot(lista2[i],
                                                  "tmpone.coo",
                                                  output='pippo.' + str(j) +
                                                  '.mag',
                                                  verify='no',
                                                  interac='no',
                                                  Stdout=1)
                try:
                    float(string.split(gggg[0])[3])
                    answ = 'y'
                except:
                    print '\n### warning'
                    answ = 'n'

    os.system('ls pippo.*.mag > tempmag.lst')
    tmptbl0 = iraf.txdump(textfile="@tempmag.lst",
                          fields="XCENTER,YCENTER,FLUX",
                          expr='yes',
                          Stdout=1)
    ff = open('magnitudini', 'w')
    for i in tmptbl0:
        ff.write(i + '\n')
    ff.close()
    #   delete the temporary images and files
    delete("temp*.fits")
    delete('temp*.lst')
    delete(illum_frame)
    print '\n### fitting the illumination surface...'
    aaa = iraf.utilities.surfit('magnitudini',
                                image=illum_frame,
                                function="polynomial",
                                xorder=2,
                                yorder=2,
                                xterms="full",
                                ncols=1024,
                                nlines=1024,
                                Stdout=1)
    iraf.noao.imred.generic.normalize(illum_frame)
    correctcard(lista[0])
    data, hdr = pyfits.getdata(illum_frame, 0, header=True)
    data0, hdr0 = pyfits.getdata(lista[0], 0, header=True)
    delete(illum_frame)
    pyfits.writeto(illum_frame, float32(data), hdr0)
    flatfield0 = string.split(flatfield, '/')[-1]
    ntt.util.updateheader(illum_frame, 0,
                          {'MKILLUM': [flatfield0, 'flat field']})
    display_image(illum_frame, 1, '', '', False)
    for i in range(0, len(lista)):  # in lista:
        img = lista[i]
        delete('pippo.' + str(i) + '.mag')
        delete('C' + img)
        delete('C' + re.sub('.fits', '_sky.fits', img))
#    delete('C*.fits.mag.1')
#    iraf.hedit(illum_frame,'MKILLUM','Illum. corr. created '+flatfield,add='yes',update='yes',verify='no')
    return illum_frame
Exemple #34
0
        _show = True
    if option.redo == False:
        redo = False
    else:
        redo = True
    arterr = ''
    if _interactive:
        _show = True
    print _show, _interactive
    # ####################### SET   IRAF   PARAMETERS  #######################
    from pyraf import iraf

    iraf.astcat(_doprint=0)
    iraf.imcoords(_doprint=0)
    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)
    zmag = 0
    iraf.digiphot.daophot.photpars.zmag = zmag
    #################################    CHECK HEADER    ######################################
    warn = '##################################\n'
    for imglong in imglist:
        if imglong:
            if imglong[-5:] == '.fits':
                img = imglong[:-5]
            else:
                img = imglong
            ######
            try:
                print string.split(img, '/')[-1] + '.fits'
                agnkey.agnsqldef.updatevalue(
                    'dataredulco', 'psfmag', 9999,
Exemple #35
0
def fitsn(_recenter, img, imgpsf, fwhm0, apco0, z22, z11, midpt, size, nor,
          _values, DM):
    from pyraf import iraf
    import string, os, sys
    from numpy import log10

    a1 = int(fwhm0 + .5)
    a2 = int(2. * fwhm0 + .5)
    a3 = int(3. * fwhm0 + .5)
    a4 = int(4. * fwhm0 + .5)
    a5 = int(5. * fwhm0 + .5)
    ap = str(a1) + "," + str(a2) + "," + str(a3)

    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)

    from iraf import digiphot
    from iraf import daophot
    from iraf import ptools

    zmag = 0
    iraf.noao.digiphot.daophot.photpars.zmag = zmag

    iraf.delete("apori", verify='no')
    iraf.delete(img + ".sn.mag", verify='no')
    iraf.noao.digiphot.daophot.phot("original",
                                    img + ".sn.coo",
                                    "apori",
                                    veri='no')
    iraf.noao.digiphot.daophot.phot("sn",
                                    img + ".sn.coo",
                                    img + ".sn.mag",
                                    veri='no')
    iraf.noao.digiphot.daophot.daopars.psfrad = a4
    iraf.noao.digiphot.daophot.daopars.fitrad = fwhm0
    iraf.noao.digiphot.daophot.daopars.fitsky = 'no'
    iraf.noao.digiphot.daophot.daopars.sannulus = int(a4)

    if _recenter:
        answ = raw_input(">>> recentering for targets [yes/no] ? [yes] ")
        if not answ: answ = 'yes'
    else:
        answ = 'yes'
    iraf.noao.digiphot.daophot.daopars.recenter = answ
    iraf.noao.digiphot.daophot.daopars.fitsky = 'yes'
    iraf.delete(img + ".sn.als", verify='no')
    iraf.allstar("sn",
                 img + ".sn.mag",
                 imgpsf,
                 img + ".sn.als",
                 "",
                 "residual",
                 veri='no',
                 verb='no')
    iraf.delete("snfit.fits", verify='no')
    iraf.imarith("sn", "-", "residual", "snfit")
    iraf.delete("skyfit.fits", verify='no')
    iraf.imarith("original", "-", "snfit", "skyfit")
    iraf.txsort(img + ".sn.als", "ID")
    tmptbl = iraf.txdump(img + ".sn.als",
                         "mag,merr,xcenter,ycenter",
                         expr='yes',
                         Stdout=1)
    magerr, fitmag, centx, centy = [], [], [], []
    for i in tmptbl:
        try:
            fitmag.append(float(string.split(i)[0]))  #+2.5*log10(nor))
        except:
            fitmag.append('INDEF')
        try:
            magerr.append(float(string.split(i)[1]))
        except:
            magerr.append('INDEF')

        centx.append(float(string.split(i)[2]))
        centy.append(float(string.split(i)[3]))
    tmptbl = iraf.txdump("apori", "mag", expr='yes', Stdout=1)
    apori1, apori2, apori3 = [], [], []
    for i in tmptbl:
        try:
            apori1.append(
                float(string.split(i)[0]) - float(_values) -
                float(DM))  #+2.5*log10(nor)
        except:
            apori1.append('INDEF')
        try:
            apori2.append(
                float(string.split(i)[1]) - float(_values) -
                float(DM))  #+2.5*log10(nor)
        except:
            apori2.append('INDEF')
        try:
            apori3.append(
                float(string.split(i)[2]) - float(_values) -
                float(DM))  #+2.5*log10(nor)
        except:
            apori3.append('INDEF')

    iraf.txsort(img + ".sn.mag", "YCENTER")
    tmptbl = iraf.txdump(img + ".sn.mag", "mag,magerr", expr='yes', Stdout=1)

    print "********************************************************************"
    print "ID <apmag on original>  <apmag on bgsubt> fitmag truemag err_fit"
    print "     ", a1, "       ", a2, "      ", a3, "        ", a1, "     ", a2, "     ", a3

    apmag1, apmag2, apmag3, truemag = [], [], [], []
    for i in range(len(tmptbl)):
        try:
            apmag1.append(
                float(string.split(tmptbl[i])[0]) - float(_values) -
                float(DM))  #+2.5*log10(nor)
        except:
            apmag1.append('INDEF')
        try:
            apmag2.append(
                float(string.split(tmptbl[i])[1]) - float(_values) -
                float(DM))  #+2.5*log10(nor)
        except:
            apmag2.append('INDEF')
        try:
            apmag3.append(
                float(string.split(tmptbl[i])[2]) - float(_values) -
                float(DM))  #+2.5*log10(no)
        except:
            apmag3.append('INDEF')
        try:
            truemag.append(
                float(fitmag[i]) + float(apco0) + 2.5 * log10(nor) -
                float(_values) - float(DM))
        except:
            truemag.append('INDEF')
        print i, apori1[i], apori2[i], apori3[i], apmag1[i], apmag2[i], apmag3[
            i], fitmag[i], truemag[i], magerr[i]

    print "********************************************************************"

    iraf.display("original",
                 1,
                 fill='yes',
                 xcen=.25,
                 ycen=.25,
                 xsize=.3,
                 ysize=.3,
                 zscal='no',
                 zrang='no',
                 z2=z22,
                 z1=z11)

    z01 = z11 - midpt
    z02 = z22 - midpt
    s1 = 1
    s2 = -int(fwhm0)
    iraf.delete("tmptbl", ve='no')
    ff = open('tmptbl', 'w')
    ff.write(str(s1) + ' ' + str(s2) + " ORIGINAL")
    ff.close()
    iraf.tvmark(1,
                "tmptbl",
                autol='no',
                mark="none",
                inter='no',
                label='yes',
                txsize=2)

    iraf.display("snfit",
                 1,
                 erase='no',
                 fill='yes',
                 xcen=.25,
                 ycen=.75,
                 xsize=.3,
                 ysize=.3,
                 zscal='no',
                 zrang='no',
                 z2=z02,
                 z1=z01)
    iraf.delete("tmptbl", ve='no')
    tmptbl0 = iraf.txdump(img + ".sn.als", "xcen,ycen", expr='yes', Stdout=1)
    ff = open('tmptbl', 'w')
    for i in tmptbl0:
        ff.write(i + '\n')
    ff.close()
    lra = int((2 * float(size) * float(fwhm0)) * 2)
    iraf.tvmark(1,
                "tmptbl",
                autol='no',
                mark="circle",
                number='yes',
                nyoffset=lra,
                radi=a2,
                txsize=2,
                inter='no')
    s1 = 1
    s2 = -1 * int(fwhm0)
    iraf.delete("tmptbl", ve='no')
    ff = open('tmptbl', 'w')
    ff.write(str(s1) + ' ' + str(s2) + " FITTED")
    ff.close()
    iraf.tvmark(1,
                "tmptbl",
                autol='no',
                mark="none",
                inter='no',
                label='yes',
                txsize=2)

    iraf.display("skyfit",
                 1,
                 erase='no',
                 fill='yes',
                 xcen=.75,
                 ycen=.25,
                 xsize=.3,
                 ysize=.3,
                 zscal='no',
                 zrang='no',
                 z2=z22,
                 z1=z11)
    s1 = 1
    s2 = -1 * int(fwhm0)
    iraf.delete("tmptbl", ve='no')
    ff = open('tmptbl', 'w')
    ff.write(str(s1) + ' ' + str(s2) + " RESIDUAL")
    ff.close()
    iraf.tvmark(1,
                "tmptbl",
                autol='no',
                mark="none",
                inter='no',
                label='yes',
                txsize=2)
    print '###'
    print '### magnitude scaled to target exposure time using: ' + str(
        2.5 * log10(nor))
    print '### fit magnitude corrected with aperture correction: ' + str(
        float(apco0))
    print '### magnitude scaled to target using DM: ' + str(DM)
    print '###'
    return apori1, apori2, apori3, apmag1, apmag2, apmag3, fitmag, truemag, magerr, centx, centy
Exemple #36
0
def iraf_phot(f,x,y,zmag=26.5,apin=10,skyin=15,skywidth=10):
    """Compute the magnitude of the star at location x/y"""
    import pyfits
    import re
    infits=pyfits.open(f,'update')
    f=re.sub(r'.fits$','',f)
    
    ### Get my python routines
    from pyraf import iraf
    from pyraf.irafpar import IrafParList

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


    ### Load the required IRAF packages
    iraf.digiphot()
    iraf.apphot()
    iraf.daophot()
    
    ### temp file name hash.
    tfile={}
    
    iraf.datapars.datamax=60000
    iraf.datapars.datamin=-1000
    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=5.0
    
    iraf.centerpars.calgorithm='centroid'
    iraf.photpars.zmag=zmag
    iraf.photpars.apertures=apin
    iraf.fitskypars.annulus=skyin
    iraf.fitskypars.dannulus=skywidth
    iraf.daophot.verbose=iraf.no
    iraf.daophot.verify=iraf.no
    iraf.daophot.update=iraf.no
    
    iraf.psf.interactive=iraf.no
    iraf.pstselect.interactive=iraf.no
    
    iraf.datapars.saveParList()
    iraf.fitskypars.saveParList()
    iraf.centerpars.saveParList()
    iraf.findpars.saveParList()
    iraf.photpars.saveParList()
    
    tfiles = ['coo','mag']
    

    
    for file in tfiles:
        extname=f
        tfile[file]=extname+"."+file
        if ( os.access(tfile[file],os.F_OK) ):
            os.unlink(tfile[file])
            
    this_image=f
        
    fd = open(tfile['coo'],'w')
    fd.write('%f %f\n' % ( x, y) )
    fd.close()
    
    print "Measuring photometry psf star in "+tfile['coo']
    iraf.daophot.phot(image=this_image,
                      coords=tfile['coo'],
                      output=tfile['mag'])

    import string
    a=iraf.txdump(tfile['mag'],"MAG,XCEN,YCEN",iraf.yes,Stdout=1)
    (mag,x,y)=string.split(a[0])

    inhdu=infits[0].header
    
    inhdu.update("PSFMAG",float(mag),comment="PSF Magnitude")
    inhdu.update("PSF_X",float(x),comment="PSF Magnitude")
    inhdu.update("PSF_Y",float(y),comment="PSF Magnitude")
    inhdu.update("ZMAG",zmag,comment="ZMAG of PSF ")

    ## now measure using a smaller aperture to get aper correction
    iraf.photpars.apertures=apin*3.0
    os.unlink(tfile['mag'])
    iraf.daophot.phot(image=this_image,
                      coords=tfile['coo'],
                      output=tfile['mag'])
    a=iraf.txdump(tfile['mag'],"MAG,XCEN,YCEN",iraf.yes,Stdout=1)
    (magout,x,y)=string.split(a[0])
    inhdu.update("APCOR",float(magout)-float(mag),comment="AP_OUT - AP_IN")
    inhdu.update("AP_IN",apin*3.0,comment="Small aperature")
    inhdu.update("AP_OUT",apin,comment="Large aperture")

    
    #    ### append this psf to the output images....
    
    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])
    return 0
Exemple #37
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()
Exemple #38
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
Exemple #39
0
def makeillumination(lista,flatfield):#,outputfile,illum_frame):
    import os,glob,string,re
    from astropy.io import fits as pyfits
    import ntt
    from ntt.util import readhdr, readkey3, delete, display_image, defsex,  name_duplicate, correctcard
    from numpy import compress, array, argmax, argmin, min, argsort, float32
    import datetime
    MJDtoday = 55927 + (datetime.date.today() - datetime.date(2012, 01, 01)).days
    _date = readkey3(readhdr(lista[0]), 'date-night')
    _filter = readkey3(readhdr(lista[0]), 'filter')
    illum_frame = name_duplicate(
        lista[0], 'illum_' + _date + '_' + _filter + '_' + str(MJDtoday), '')
    from pyraf import iraf
    iraf.images(_doprint=0)
    iraf.imutil(_doprint=0)
    iraf.utilities(_doprint=0)
    iraf.noao(_doprint=0)
    iraf.imred(_doprint=0)
    iraf.ccdred(_doprint=0)
    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)
    iraf.generic(_doprint=0)
    toforget = ['digiphot.daophot', 'imutil.imarith',
                'image', 'utilities.surfit']
    for t in toforget:
        iraf.unlearn(t)
    n = len(lista)
#   start loop to read image names from the input file
    lista1 = []
    iraf.ccdred.verbose = 'no'
    ff = open('templist.lst', 'w')
    for i in range(0, len(lista)):
        ff.write('C' + lista[i] + '\n')
        delete('C' + lista[i])
        delete('C' + re.sub('.fits', '_sub.fits', lista[i]))
        ntt.sofiphotredudef.crosstalk(lista[i], 'C' + lista[i])
        iraf.noao.imred.ccdred.ccdproc('C' + lista[i], output='', overscan="no", trim="yes", ccdtype='', darkcor='no', fixpix='no', zerocor="no", flatcor='yes',
                                       illumco='no', trimsec='[1:1024,1:1007]', biassec='', flat=flatfield, illum='')
        correctcard('C' + lista[i])
        lista1.append('C' + lista[i])
    ff.close()
    print '\n### prereducing STD frames to compute illumination correction ........'
    lista2, skyfile = ntt.sofiphotredudef.skysub(lista1, readkey3(
        readhdr(lista1[0]), 'ron'), readkey3(readhdr(lista1[0]), 'gain'), True)
    lista2 = ntt.sofiphotredudef.sortbyJD(lista2)
    print '\n### use x on the star and q  to continue....'
    display_image(lista2[0], 2, '', '', False)
    delete('tmpone.coo')
    iraf.image.tv.imexamine(lista2[0], 2, logfile='tmpone.coo',
                            keeplog='yes', xformat='', yformat='', wcs='logical')
    iraf.tvmark(2, 'tmpone.coo', mark="circle", number='yes',
                label='no', radii=8, nxoffse=5, nyoffse=5, color=204, txsize=2)
    xycoo = iraf.proto.fields('tmpone.coo', '1,2', Stdout=1)
    x0, y0 = string.split(xycoo[0])
    x0 = float(x0)
    y0 = float(y0)
    xcum0 = readkey3(readhdr(lista2[0]), 'xcum')
    ycum0 = readkey3(readhdr(lista2[0]), 'ycum')
    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)
    iraf.noao.digiphot.daophot.datapars.datamin = -1000
    iraf.noao.digiphot.daophot.datapars.datamax = 60000
    iraf.noao.digiphot.daophot.daopars.function = 'gauss'
    iraf.noao.digiphot.daophot.photpars.zmag = 0
    namesex = defsex('default.sex')
    for i in range(0, len(lista2)):
        j = i + 1
        xcum = readkey3(readhdr(lista2[i]), 'xcum')
        ycum = readkey3(readhdr(lista2[i]), 'ycum')
        xx = x0 - xcum0 + xcum
        yy = y0 - ycum0 + ycum
        # sex objects
        os.system('sex ' + lista2[i] + ' -c ' + namesex + '>  _logsex')
        delete('_logsex')
        xpix = iraf.proto.fields('detections.cat', fields='2', Stdout=1)
        ypix = iraf.proto.fields('detections.cat', fields='3', Stdout=1)
        cm = iraf.proto.fields('detections.cat', fields='4', Stdout=1)
        cm = compress((array(xpix) != ''), array(cm, float))
        ypix = compress((array(xpix) != ''), array(ypix, float))
        xpix = compress((array(xpix) != ''), array(xpix, float))
        if len(xpix) > 300:
            num = 300
        else:
            num = len(xpix) - 1
        xpix = xpix[argsort(cm)][0:num]
        ypix = ypix[argsort(cm)][0:num]
        distance = (ypix - yy)**2 + (xpix - xx)**2
        xx1, yy1 = xpix[argmin(distance)], ypix[argmin(distance)]
        f = open('tmpone.coo', 'w')
        f.write(str(xx1) + ' ' + str(yy1) + '\n')
        f.close()
        display_image(lista2[i], 1, '', '', False)
        iraf.tvmark(1, 'tmpone.coo', mark="circle", number='yes',
                    label='no', radii=8, nxoffse=5, nyoffse=5, color=204, txsize=2)
        answ = 'n'
        while answ != 'y':
            answ = raw_input('selected the right one [[y]/n] ?')
            if not answ:
                answ = 'y'
            if answ in ['y', 'YES', 'yes', 'Y']:
                print lista2[i]
                delete('pippo.' + str(j) + '.mag')
                gggg = iraf.digiphot.daophot.phot(
                    lista2[i], "tmpone.coo", output="pippo." + str(j) + ".mag", verify='no', interac='no', Stdout=1)
                try:
                    float(string.split(gggg[0])[3])
                    answ = 'y'
                except:
                    print '\n### warning'
                    answ = 'n'
            else:
                print '\n### select the std star'
                display_image(lista2[i], 1, '', '', False)
                iraf.image.tv.imexamine(lista2[
                                        i], 1, logfile='tmpone.coo', keeplog='yes', xformat='', yformat='', wcs='logical')
                xycoo = iraf.proto.fields('tmpone.coo', '1,2', Stdout=1)
                x2, y2 = string.split(xycoo[0])
                f = open('tmpone.coo', 'w')
                f.write(str(x2) + ' ' + str(y2) + '\n')
                f.close()
                delete('pippo.' + str(j) + '.mag')
                print '###### new selection ' + str(x2), str(y2)
                gggg = iraf.digiphot.daophot.phot(
                    lista2[i], "tmpone.coo", output='pippo.' + str(j) + '.mag', verify='no', interac='no', Stdout=1)
                try:
                    float(string.split(gggg[0])[3])
                    answ = 'y'
                except:
                    print '\n### warning'
                    answ = 'n'

    os.system('ls pippo.*.mag > tempmag.lst')
    tmptbl0 = iraf.txdump(textfile="@tempmag.lst",
                          fields="XCENTER,YCENTER,FLUX", expr='yes', Stdout=1)
    ff = open('magnitudini', 'w')
    for i in tmptbl0:
        ff.write(i + '\n')
    ff.close()
#   delete the temporary images and files
    delete("temp*.fits")
    delete('temp*.lst')
    delete(illum_frame)
    print '\n### fitting the illumination surface...'
    aaa = iraf.utilities.surfit('magnitudini', image=illum_frame, function="polynomial",
                                xorder=2, yorder=2, xterms="full", ncols=1024, nlines=1024, Stdout=1)
    iraf.noao.imred.generic.normalize(illum_frame)
    correctcard(lista[0])
    data, hdr = pyfits.getdata(illum_frame, 0, header=True)
    data0, hdr0 = pyfits.getdata(lista[0], 0, header=True)
    delete(illum_frame)
    pyfits.writeto(illum_frame, float32(data), hdr0)
    flatfield0 = string.split(flatfield, '/')[-1]
    ntt.util.updateheader(
        illum_frame, 0, {'MKILLUM': [flatfield0, 'flat field']})
    display_image(illum_frame, 1, '', '', False)
    for i in range(0, len(lista)):  # in lista:
        img = lista[i]
        delete('pippo.' + str(i) + '.mag')
        delete('C' + img)
        delete('C' + re.sub('.fits', '_sky.fits', img))
#    delete('C*.fits.mag.1')
#    iraf.hedit(illum_frame,'MKILLUM','Illum. corr. created '+flatfield,add='yes',update='yes',verify='no')
    return illum_frame
Exemple #40
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 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 #41
0
def setup_phot(imageRoot, silent=False,
               apertures=[25,50,75,100,125,150,175,200],
               sky_annulus=200, sky_dannulus=50, zmag=0):

    # Load image header
    hdr = pyfits.getheader(imageRoot + '.fits')

    ir.digiphot()
    ir.daophot()
    ir.unlearn('phot')
    ir.unlearn('datapars')
    ir.unlearn('centerpars')
    ir.unlearn('fitskypars')
    ir.unlearn('photpars')

    ##########
    # Set up datapars
    ##########
    ir.datapars.fwhmpsf = 5.0 # shouldn't really matter
    ir.datapars.sigma = 'INDEF'
    ir.datapars.datamin = 'INDEF'

    if os.path.exists(imageRoot + '.max'):
        max_file = open(imageRoot + '.max', 'r')
        max_line = max_file.readline()
        max = float(max_line)
        ir.datapars.datamax = max

        if not silent:
            print 'Set ir.datapars.datamax = %d' % max

    # Pull gain from the header
    ir.datapars.gain = 'GAIN'
    ir.datapars.epadu = 'INDEF'

    # Assumes 43.1 electrons per read of noise
    nreads = 1.0
    if int(hdr['SAMPMODE']) == 3:
        nreads = int(hdr['MULTISAM'])
    
    ir.datapars.ccdread = ''
    ir.datapars.readnoise = 43.1 * math.sqrt(2.0) / math.sqrt(nreads)

    # Get exposure times from header
    ir.datapars.exposure = ''
    ir.datapars.itime = float(hdr['ITIME']) * int(hdr['COADDS'])

    # Other Header keywords
    ir.datapars.airmass = 'AIRMASS'
    ir.datapars.filter = 'FWINAME'
    ir.datapars.obstime = 'EXPSTART'

    
    ##########
    # Setup centerpars. We will use *.coo file for initial guess.
    ##########
    ir.centerpars.calgorithm = 'centroid'

    ##########
    # Setup fitskypars
    ##########
    ir.fitskypars.salgorithm = 'centroid'
    ir.fitskypars.annulus = sky_annulus
    ir.fitskypars.dannulus = sky_dannulus

    ##########
    # Setup photpars
    ##########
    # Setup a zeropoint... this assumes Strehl = 1, but good enough for now.
    ir.photpars.zmag = zmag
    ir.photpars.apertures = ','.join([str(aa) for aa in apertures])
    
    ##########
    # Setup phot
    ##########
    ir.phot.interactive = 'no'
    ir.phot.radplots = 'no'
    ir.phot.verify = 'No'

    if silent:
        ir.phot.verbose = 'no'
    else:
        ir.phot.verbose = 'yes'
def setup_phot(imageRoot, silent=False,
               apertures=[25,50,75,100,125,150,175,200],
               sky_annulus=200, sky_dannulus=50, zmag=0):

    # Load image header
    hdr = pyfits.getheader(imageRoot + '.fits')

    ir.digiphot()
    ir.daophot()
    ir.unlearn('phot')
    ir.unlearn('datapars')
    ir.unlearn('centerpars')
    ir.unlearn('fitskypars')
    ir.unlearn('photpars')

    ##########
    # Set up datapars
    ##########
    ir.datapars.fwhmpsf = 5.0 # shouldn't really matter
    ir.datapars.sigma = 'INDEF'
    ir.datapars.datamin = 'INDEF'

    if os.path.exists(imageRoot + '.max'):
        max_file = open(imageRoot + '.max', 'r')
        max_line = max_file.readline()
        max = float(max_line)
        ir.datapars.datamax = max

        if not silent:
            print 'Set ir.datapars.datamax = %d' % max

    # Pull gain from the header
    ir.datapars.gain = 'GAIN'
    ir.datapars.epadu = 'INDEF'

    # Assumes 43.1 electrons per read of noise
    nreads = 1.0
    if int(hdr['SAMPMODE']) == 3:
        nreads = int(hdr['MULTISAM'])
    
    ir.datapars.ccdread = ''
    ir.datapars.readnoise = 43.1 * math.sqrt(2.0) / math.sqrt(nreads)

    # Get exposure times from header
    ir.datapars.exposure = ''
    ir.datapars.itime = float(hdr['ITIME']) * int(hdr['COADDS'])

    # Other Header keywords
    ir.datapars.airmass = 'AIRMASS'
    ir.datapars.filter = 'FWINAME'
    ir.datapars.obstime = 'EXPSTART'

    
    ##########
    # Setup centerpars. We will use *.coo file for initial guess.
    ##########
    ir.centerpars.calgorithm = 'centroid'

    ##########
    # Setup fitskypars
    ##########
    ir.fitskypars.salgorithm = 'centroid'
    ir.fitskypars.annulus = sky_annulus
    ir.fitskypars.dannulus = sky_dannulus

    ##########
    # Setup photpars
    ##########
    # Setup a zeropoint... this assumes Strehl = 1, but good enough for now.
    ir.photpars.zmag = zmag
    ir.photpars.apertures = ','.join([str(aa) for aa in apertures])
    
    ##########
    # Setup phot
    ##########
    ir.phot.interactive = 'no'
    ir.phot.radplots = 'no'
    ir.phot.verify = 'No'

    if silent:
        ir.phot.verbose = 'no'
    else:
        ir.phot.verbose = 'yes'
Exemple #43
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
Exemple #44
0
def alignImages(imageName,imageName_Ha):
  #Read the fwhm and seeing from the image file
  imageHeader = fits.open(imageName+".fits")[0]
  fwhm = imageHeader.header['SEEING']
  annul = 5.0 * fwhm
  aper=3.0 * fwhm
  sigma = imageHeader.header['SKYSIGMA']
  iraf.daophot(_doprint=0)
  #Do 'daofind' on the image to locate the stars
  print("1. Find stars using 'daofind'")
  
  #Configure 'datapars', 'findpars', and 'daofind'
  
  iraf.datapars.fwhmpsf=fwhm
  iraf.datapars.sigma=sigma
  iraf.datapars.datamin=-10
  iraf.datapars.ccdread="RDNOISE"
  iraf.datapars.gain="GAIN"
  iraf.datapars.exposure="EXPTIME"
  iraf.datapars.airmass="AIRMASS"
  iraf.datapars.filter="FILTER"
  iraf.datapars.obstime="TIME-OBS"

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

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

  iraf.daofind(imageName,'coord')

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


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

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

  iraf.photpars.apertures=aper

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

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

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

  print (" ")

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

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

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


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

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

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

  #Pick out only the required data of stars from the .25 file
  sys.stdout=open("pdump.file","w")
  iraf.pdump ("stars25","xcenter,ycenter,flux","yes")
  sys.stdout = sys.__stdout__
  
  print("The coordinates and flux are stored in pdump.file")
  
  #Align images
  iraf.imcopy(imageName,'rIn')
  iraf.imcopy(imageName_Ha,'haIn')
  print("Aligning images")
  #iraf.imalign.verbose='no'
  iraf.imalign("rIn,haIn", "rIn","pdump.file","R_final,Ha_final")
            f3 = open(os.path.join(self.pars.wdir, self.irafphotdir, 'coordlist'), 'w')
            for n in self.dataset.frames:
                basename = os.path.splitext(os.path.basename(self.dataset.frames[n]['irafpath']))[0]
                shutil.copy(os.path.join(self.pars.wdir, self.dataset.masterdir, 'master.final'),
                            os.path.join(self.pars.wdir, self.irafphotdir, basename + '.coord'))
                f2.write(os.path.join(self.pars.wdir, self.irafphotdir, basename + '.phot') + '\n')
                f3.write(os.path.join(self.pars.wdir, self.irafphotdir, basename + '.coord') + '\n')
            f2.close()
            f3.close()

            logging.info('Photometry will be performed using the following apertures: %s' % self. apsizestr)

            # initialize IRAF
            iraf.noao(Stdout=1)
            iraf.digiphot(Stdout=1)
            iraf.daophot(Stdout=1, verify='no')
            iraf.tables(Stdout=1)
            iraf.ttools(Stdout=1)
            iraf.astutil(Stdout=1)

            # set some parameters for iraf photometry
            iraf.fitskypars.setParam('salgori', self.pars.iraf_phot['salgori'])
            iraf.fitskypars.setParam('annulus', self.pars.iraf_phot['annulus'])
            iraf.fitskypars.setParam('dannulu', self.pars.iraf_phot['dannulu'])
            iraf.centerpars.setParam('calgori', self.pars.iraf_phot['calgori'])
            iraf.centerpars.setParam('cbox',    self.pars.iraf_phot['cbox'])
            iraf.centerpars.setParam('maxshif', self.pars.iraf_phot['maxshif'])
            iraf.photpars.setParam('apertur', self.apsizestr)

            o = iraf.phot(image='@'+os.path.join(self.pars.wdir, self.dataset.framesdir, 'imagelist'), wcsin='world',
                          coords='@'+os.path.join(self.pars.wdir, self.irafphotdir, 'coordlist'),
Exemple #46
0
#just like in IRAF, packages must be loaded

import sys, os, shutil, signal, math, types
from numpy import *
import pdb # python debugger

#numpy is the NUMber PYthon package that can do array math

from pyraf import iraf #,irafglobals
iraf.images()
iraf.imutil()
iraf.noao()
iraf.digiphot()
iraf.ptools()
iraf.daophot()

from easy_phot_params import *
#this loads all of the variables defined in easy_phot_params

def stringtohours(str):
    hrs,mins,secs = map(float,str.split(":"))
    return hrs+mins/60. + secs/3600.

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
Exemple #47
0
def fitsn(img,imgpsf,coordlist,_recenter,fwhm0,original,sn,residual,_show,_interactive,dmax,dmin,z11='',z22='',midpt='',size=7,apco0=0):
    import lsc
    lsc.util.delete("apori")
    lsc.util.delete(img+".sn.mag")
#################################
    from pyraf import iraf
    import string
    iraf.imcoords(_doprint=0)
    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)
    from iraf import digiphot
    from iraf import daophot
    from iraf import ptools
    a1 = int(fwhm0)
    a2 = int(2.*fwhm0+.5)
    a3 = int(3.*fwhm0+.5)
    a4 = int(4.*fwhm0+.5)
    ap = str(a1)+","+str(a2)+","+str(a3)
########################################
    if _recenter:        answ='yes'
    else:                answ='no'
#########################################
    hdr=lsc.util.readhdr(img+'.fits')
    _gain=lsc.util.readkey3(hdr,'gain')
    _ron=lsc.util.readkey3(hdr,'ron')
    _exptime=lsc.util.readkey3(hdr,'exptime')
    iraf.noao.digiphot.daophot.photpars.zmag = 0
    iraf.noao.digiphot.daophot.datapars.readnoi = _gain
    iraf.noao.digiphot.daophot.datapars.epadu = _ron
    iraf.noao.digiphot.daophot.datapars.datamin = dmin
    iraf.noao.digiphot.daophot.datapars.datamax = dmax
    iraf.noao.daophot.fitskypars.annulus=a3
    iraf.noao.daophot.photpars.apertures = ap
    iraf.noao.digiphot.daophot.datapars.exposure = 'exptime'
    iraf.noao.digiphot.daophot.datapars.airmass = 'airmass'
    iraf.noao.digiphot.daophot.datapars.filter = 'filter2'
    iraf.noao.digiphot.daophot.daopars.psfrad = a4
    #  modify fitrad to 3 fwhm to see if works better     
    iraf.noao.digiphot.daophot.daopars.fitrad = fwhm0 #* 3
    iraf.noao.digiphot.daophot.daopars.sannulus = int(a4)
    iraf.noao.digiphot.daophot.daopars.recenter = answ
    iraf.noao.digiphot.daophot.daopars.fitsky = 'yes'
#    iraf.noao.digiphot.daophot.centerpars.cbox = 0
    iraf.noao.digiphot.daophot.centerpars.cbox = 4
    iraf.noao.digiphot.daophot.centerpars.calgori = 'gauss'

    #  fitskypars.salgorithm = "constant"
    #  fitskypars.skyvalue = 0
    print '\n### recentering: '+str(answ)
    if _show:
        iraf.noao.digiphot.daophot.phot(original,coordlist,"apori",veri='no')   
        iraf.noao.digiphot.daophot.phot(sn,coordlist,img+".sn.mag",veri='no')   
    else:
        iraf.noao.digiphot.daophot.phot(original,coordlist,"apori",veri='no',verb='no')   
        iraf.noao.digiphot.daophot.phot(sn,coordlist,img+".sn.mag",veri='no',verb='no')   

    lsc.util.delete(img+".sn.als")
    print sn,imgpsf,img
    iraf.allstar(sn,img+".sn.mag",imgpsf,img+".sn.als","",residual,veri='no',verb='no')
    lsc.util.delete("snfit.fits")
    iraf.imarith(sn+'.fits',"-",residual+'.fits',"snfit.fits")
    lsc.util.delete("skyfit.fits")
    iraf.imarith(original+'.fits',"-","snfit.fits","skyfit.fits")
    iraf.txsort(img+".sn.als","ID")
    tmptbl = iraf.txdump(img+".sn.als","mag,merr,xcenter,ycenter",expr='yes', Stdout=1)
    magerr,fitmag,centx,centy=[],[],[],[]
    for i in tmptbl:
        try:
            fitmag.append(float(string.split(i)[0]))#-2.5*log10(_exptime))
        except:
            fitmag.append(string.split(i)[0])
        try:
            magerr.append(float(string.split(i)[1]))
        except:
            magerr.append(string.split(i)[1])
        centx.append(float(string.split(i)[2]))
        centy.append(float(string.split(i)[3]))
    tmptbl=iraf.txdump("apori","mag",expr='yes', Stdout=1)
    apori1,apori2,apori3=[],[],[]
    for i in tmptbl:
        try:
            apori1.append(float(string.split(i)[0]))#-2.5*log10(_exptime))
        except:
            apori1.append(string.split(i)[0])
        try:            
            apori2.append(float(string.split(i)[1]))#-2.5*log10(_exptime))
        except:
            apori2.append(string.split(i)[1])
        try:
            apori3.append(float(string.split(i)[2]))#-2.5*log10(_exptime))
        except:
            apori3.append(string.split(i)[2])
            
    iraf.txsort(img+".sn.mag","YCENTER")
    tmptbl=iraf.txdump(img+".sn.mag","mag,magerr",expr='yes', Stdout=1) 

    if _show:
        print "********************************************************************"
        print "ID <apmag on original>  <apmag on bgsubt> fitmag truemag err_fit"         
        print "     ",a1,"       ",a2,"      ",a3,"        ",a1,"     ",a2,"     ",a3 


    apmag1,apmag2,apmag3,truemag=[],[],[],[]
    for i in range(len(tmptbl)):
        try:
            apmag1.append(float(string.split(tmptbl[i])[0]))#-2.5*log10(_exptime))
        except:
            apmag1.append(9999)
        try:
            apmag2.append(float(string.split(tmptbl[i])[1]))#-2.5*log10(_exptime))
        except:
            apmag2.append(9999)
        try:
            apmag3.append(float(string.split(tmptbl[i])[2]))#-2.5*log10(_exptime))
        except:
            apmag3.append(9999)
        try:
            truemag.append(fitmag[i]+float(apco0))
        except:
            truemag.append('INDEF')
        if _show:
            print i,apori1[i],apori2[i],apori3[i],apmag1[i],apmag2[i],apmag3[i],fitmag[i],truemag[i],magerr[i]
    if _show:
        print "********************************************************************"

    if _show:
        print midpt,z11,z22
        _tmp1,_tmp2,goon=lsc.util.display_image(original+'.fits',1, z11, z22, False, _xcen=.25, _ycen=.25, _xsize=.3, _ysize=.3)
        z01 = float(z11)-float(midpt)
        z02 = float(z22)-float(midpt) 
        s1 = 1
        s2 = -int(fwhm0)
        lsc.util.delete("tmptbl")
        ff=open('tmptbl','w')
        ff.write(str(s1)+' '+str(s2)+" ORIGINAL")
        ff.close()    
        iraf.tvmark(1,"tmptbl",autol='no',mark="none",inter='no',label='yes',txsize=2)
        _tmp1,_tmp2,goon=lsc.util.display_image('snfit.fits',1, z01, z02, False, _xcen=.25, _ycen=.75, _xsize=.3, _ysize=.3, _erase='no')
        lsc.util.delete("tmptbl")
        tmptbl0=iraf.txdump(img+".sn.als","xcen,ycen",expr='yes',Stdout=1)
        ff=open('tmptbl','w')
        for i in tmptbl0:
            ff.write(i+'\n')
        ff.close()    
        lra = int((2*float(size)*float(fwhm0))*2)
        iraf.tvmark(1,"tmptbl",autol='no',mark="circle", number='yes',nyoffset=lra,radi=a2,txsize=2,inter='no')
        s1 = 1
        s2 = -1*int(fwhm0)
        lsc.util.delete("tmptbl")
        ff=open('tmptbl','w')
        ff.write(str(s1)+' '+str(s2)+" FITTED")
        ff.close()    
        iraf.tvmark(1,"tmptbl",autol='no',mark="none",inter='no',label='yes',txsize=2)
        _tmp1,_tmp2,goon=lsc.util.display_image('skyfit.fits',1, z11, z22, False, _xcen=.75, _ycen=.25, _xsize=.3, _ysize=.3, _erase='no')
        s1 = 1
        s2 = -1*int(fwhm0)
        lsc.util.delete("tmptbl")
        ff=open('tmptbl','w')
        ff.write(str(s1)+' '+str(s2)+" RESIDUAL")
        ff.close()    
        iraf.tvmark(1,"tmptbl",autol='no',mark="none",inter='no',label='yes',txsize=2)
    return apori1,apori2,apori3,apmag1,apmag2,apmag3,fitmag,truemag,magerr,centx,centy
Exemple #48
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
Exemple #49
0
def phot(fits_filename, x_in, y_in, aperture=15, sky=20, swidth=10, apcor=0.3,
         maxcount=30000.0, exptime=1.0):
    """
    Compute the centroids and magnitudes of a bunch sources detected on
    CFHT-MEGAPRIME images.

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

    Returns a MOPfiles data structure.
    """

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    return hdu
Exemple #50
0
import sys
import astropy.io.ascii as ascii
import numpy as np
import string
import pickle
from copy import deepcopy

import input_info
reload(input_info)
import photometry
reload(photometry)
import photometry_both
reload(photometry_both)

iraf.noao()
iraf.digiphot()
iraf.daophot()


# do psf photometry for master image.
masterpsffile = input_info.masterimage + '.pstbyhand'

threshold_secondrun = 10.

photometry.do_psf_photometry([input_info.masterimage], satmag=input_info.satmag, photfilesuffix=input_info.photfilesuffix,psfstarfile=masterpsffile, thresh = threshold_secondrun, psfcleaningradius=input_info.pfscleaningradius)





Exemple #51
0
def fitsn(img,
          imgpsf,
          coordlist,
          _recenter,
          fwhm0,
          original,
          sn,
          residual,
          _show,
          _interactive,
          z11='',
          z22='',
          midpt='',
          size=7,
          apco0=0,
          dmax=51000,
          dmin=-500):
    import lsc
    lsc.util.delete("apori")
    lsc.util.delete(img + ".sn.mag")
    #################################
    from pyraf import iraf
    import string
    iraf.imcoords(_doprint=0)
    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)
    from iraf import digiphot
    from iraf import daophot
    from iraf import ptools
    a1 = int(fwhm0)
    a2 = int(2. * fwhm0 + .5)
    a3 = int(3. * fwhm0 + .5)
    a4 = int(4. * fwhm0 + .5)
    ap = str(a1) + "," + str(a2) + "," + str(a3)
    ########################################
    if _recenter: answ = 'yes'
    else: answ = 'no'
    #########################################
    hdr = lsc.util.readhdr(img + '.fits')
    _gain = lsc.util.readkey3(hdr, 'gain')
    _ron = lsc.util.readkey3(hdr, 'ron')
    _exptime = lsc.util.readkey3(hdr, 'exptime')
    _datamin = dmin
    _datamax = dmax
    iraf.noao.digiphot.daophot.photpars.zmag = 0
    iraf.noao.digiphot.daophot.datapars.readnoi = _gain  #1.4   #_ron
    iraf.noao.digiphot.daophot.datapars.epadu = _ron  #  13      #_gain
    iraf.noao.digiphot.daophot.datapars.datamin = _datamin  # -100  #_datamin
    iraf.noao.digiphot.daophot.datapars.datamax = _datamax
    iraf.noao.daophot.fitskypars.annulus = a3
    iraf.noao.daophot.photpars.apertures = ap
    iraf.noao.digiphot.daophot.datapars.exposure = 'exptime'
    iraf.noao.digiphot.daophot.datapars.airmass = 'airmass'
    iraf.noao.digiphot.daophot.datapars.filter = 'filter2'
    iraf.noao.digiphot.daophot.daopars.psfrad = a4
    #  modify fitrad to 3 fwhm to see if works better
    iraf.noao.digiphot.daophot.daopars.fitrad = fwhm0  #* 3
    iraf.noao.digiphot.daophot.daopars.sannulus = int(a4)
    iraf.noao.digiphot.daophot.daopars.recenter = answ
    iraf.noao.digiphot.daophot.daopars.fitsky = 'yes'
    #    iraf.noao.digiphot.daophot.centerpars.cbox = 0
    iraf.noao.digiphot.daophot.centerpars.cbox = 4
    iraf.noao.digiphot.daophot.centerpars.calgori = 'gauss'

    #  fitskypars.salgorithm = "constant"
    #  fitskypars.skyvalue = 0
    print '\n### recentering: ' + str(answ)
    if _show:
        iraf.noao.digiphot.daophot.phot(original,
                                        coordlist,
                                        "apori",
                                        veri='no')
        iraf.noao.digiphot.daophot.phot(sn,
                                        coordlist,
                                        img + ".sn.mag",
                                        veri='no')
    else:
        iraf.noao.digiphot.daophot.phot(original,
                                        coordlist,
                                        "apori",
                                        veri='no',
                                        verb='no')
        iraf.noao.digiphot.daophot.phot(sn,
                                        coordlist,
                                        img + ".sn.mag",
                                        veri='no',
                                        verb='no')

    lsc.util.delete(img + ".sn.als")
    print sn, imgpsf, img
    iraf.allstar(sn,
                 img + ".sn.mag",
                 imgpsf,
                 img + ".sn.als",
                 "",
                 residual,
                 veri='no',
                 verb='no')
    lsc.util.delete("snfit.fits")
    iraf.imarith(sn + '.fits', "-", residual + '.fits', "snfit.fits")
    lsc.util.delete("skyfit.fits")
    iraf.imarith(original + '.fits', "-", "snfit.fits", "skyfit.fits")
    iraf.txsort(img + ".sn.als", "ID")
    tmptbl = iraf.txdump(img + ".sn.als",
                         "mag,merr,xcenter,ycenter",
                         expr='yes',
                         Stdout=1)
    magerr, fitmag, centx, centy = [], [], [], []
    for i in tmptbl:
        try:
            fitmag.append(float(string.split(i)[0]))  #-2.5*log10(_exptime))
        except:
            fitmag.append(string.split(i)[0])
        try:
            magerr.append(float(string.split(i)[1]))
        except:
            magerr.append(string.split(i)[1])
        centx.append(float(string.split(i)[2]))
        centy.append(float(string.split(i)[3]))
    tmptbl = iraf.txdump("apori", "mag", expr='yes', Stdout=1)
    apori1, apori2, apori3 = [], [], []
    for i in tmptbl:
        try:
            apori1.append(float(string.split(i)[0]))  #-2.5*log10(_exptime))
        except:
            apori1.append(string.split(i)[0])
        try:
            apori2.append(float(string.split(i)[1]))  #-2.5*log10(_exptime))
        except:
            apori2.append(string.split(i)[1])
        try:
            apori3.append(float(string.split(i)[2]))  #-2.5*log10(_exptime))
        except:
            apori3.append(string.split(i)[2])

    iraf.txsort(img + ".sn.mag", "YCENTER")
    tmptbl = iraf.txdump(img + ".sn.mag", "mag,magerr", expr='yes', Stdout=1)

    if _show:
        print "********************************************************************"
        print "ID <apmag on original>  <apmag on bgsubt> fitmag truemag err_fit"
        print "     ", a1, "       ", a2, "      ", a3, "        ", a1, "     ", a2, "     ", a3

    apmag1, apmag2, apmag3, truemag = [], [], [], []
    for i in range(len(tmptbl)):
        try:
            apmag1.append(float(string.split(
                tmptbl[i])[0]))  #-2.5*log10(_exptime))
        except:
            apmag1.append(9999)
        try:
            apmag2.append(float(string.split(
                tmptbl[i])[1]))  #-2.5*log10(_exptime))
        except:
            apmag2.append(9999)
        try:
            apmag3.append(float(string.split(
                tmptbl[i])[2]))  #-2.5*log10(_exptime))
        except:
            apmag3.append(9999)
        try:
            truemag.append(fitmag[i] + float(apco0))
        except:
            truemag.append('INDEF')
        if _show:
            print i, apori1[i], apori2[i], apori3[i], apmag1[i], apmag2[
                i], apmag3[i], fitmag[i], truemag[i], magerr[i]
    if _show:
        print "********************************************************************"

    if _show:
        print midpt, z11, z22
        _tmp1, _tmp2, goon = lsc.util.display_image(original + '.fits',
                                                    1,
                                                    z11,
                                                    z22,
                                                    False,
                                                    _xcen=.25,
                                                    _ycen=.25,
                                                    _xsize=.3,
                                                    _ysize=.3)
        z01 = float(z11) - float(midpt)
        z02 = float(z22) - float(midpt)
        s1 = 1
        s2 = -int(fwhm0)
        lsc.util.delete("tmptbl")
        ff = open('tmptbl', 'w')
        ff.write(str(s1) + ' ' + str(s2) + " ORIGINAL")
        ff.close()
        iraf.tvmark(1,
                    "tmptbl",
                    autol='no',
                    mark="none",
                    inter='no',
                    label='yes',
                    txsize=2)
        _tmp1, _tmp2, goon = lsc.util.display_image('snfit.fits',
                                                    1,
                                                    z01,
                                                    z02,
                                                    False,
                                                    _xcen=.25,
                                                    _ycen=.75,
                                                    _xsize=.3,
                                                    _ysize=.3,
                                                    _erase='no')
        lsc.util.delete("tmptbl")
        tmptbl0 = iraf.txdump(img + ".sn.als",
                              "xcen,ycen",
                              expr='yes',
                              Stdout=1)
        ff = open('tmptbl', 'w')
        for i in tmptbl0:
            ff.write(i + '\n')
        ff.close()
        lra = int((2 * float(size) * float(fwhm0)) * 2)
        iraf.tvmark(1,
                    "tmptbl",
                    autol='no',
                    mark="circle",
                    number='yes',
                    nyoffset=lra,
                    radi=a2,
                    txsize=2,
                    inter='no')
        s1 = 1
        s2 = -1 * int(fwhm0)
        lsc.util.delete("tmptbl")
        ff = open('tmptbl', 'w')
        ff.write(str(s1) + ' ' + str(s2) + " FITTED")
        ff.close()
        iraf.tvmark(1,
                    "tmptbl",
                    autol='no',
                    mark="none",
                    inter='no',
                    label='yes',
                    txsize=2)
        _tmp1, _tmp2, goon = lsc.util.display_image('skyfit.fits',
                                                    1,
                                                    z11,
                                                    z22,
                                                    False,
                                                    _xcen=.75,
                                                    _ycen=.25,
                                                    _xsize=.3,
                                                    _ysize=.3,
                                                    _erase='no')
        s1 = 1
        s2 = -1 * int(fwhm0)
        lsc.util.delete("tmptbl")
        ff = open('tmptbl', 'w')
        ff.write(str(s1) + ' ' + str(s2) + " RESIDUAL")
        ff.close()
        iraf.tvmark(1,
                    "tmptbl",
                    autol='no',
                    mark="none",
                    inter='no',
                    label='yes',
                    txsize=2)
    return apori1, apori2, apori3, apmag1, apmag2, apmag3, fitmag, truemag, magerr, centx, centy
def maskStars(imageName):
    #read in the fits data and header
    imageHeader = fits.open(imageName + ".fits")[0]

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

    skyv = imageHeader.header['SKY']

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

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

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

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

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

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

    print("A. Find stars using daofind")

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    #Delete intermediate images
    for f in ("rmask.fits", "redit.fits", "maskimage.fits", "maskfile.mag",
              "maskfile.satmag", "temp.file", "withsky.fits", "mask.fits"):
        silentDelete(f)
Exemple #53
0
                        if args.convolve in ['t','i']:
                            _convolve=' -c ' + args.convolve + ' '
                        else:
                            _convolve=''

                        if args.afssc:
                            substamplist, xpix1, ypix1, xpix2, ypix2 = crossmatchtwofiles(imgtarg, imgtemp)
                            _afssc = ' -cmp ' + str(substamplist) + ' -afssc 1 '
                        else:
                            _afssc = ''

                        if args.difftype == '1':
                            #do subtraction
                            iraf.noao()
                            iraf.digiphot()
                            iraf.daophot(_doprint=0)
                            psftarg = '_targpsf.fits'
                            psftemp = '_temppsf.fits'
                            os.system('rm {0} {1}'.format(psftarg, psftemp))
                            iraf.seepsf(imgtarg_path.replace('.fits','.psf.fits'), psftarg)
                            iraf.seepsf(imgtemp_path.replace('.fits','.psf.fits'), psftemp)
                            try:
                                print 'Passing images to PyZOGY'
                                run_subtraction(imgtarg, imgtemp, psftarg, psftemp,
                                                           science_mask='_targmask.fits',
                                                           reference_mask='_tempmask.fits',
                                                           science_saturation=sat_targ,
                                                           reference_saturation=sat_temp,
                                                           n_stamps=1,
                                                           output=imgout,
                                                           normalization=args.normalize,
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 #55
0
def plant_kbos(filename, psf, kbos, shifts, prefix):
    """
    Add KBOs to an image
    :param filename: name of the image to add KBOs to
    :param psf: Point Spread Function in IRAF/DAOPHOT format, used by ADDSTAR
    :param kbos: list of KBOs to add, has format as returned by KBOGenerator
    :param shifts: dictionary to transform coordinates to reference frame.
    :param prefix: an estimate FWHM of the image, used to determine trailing.
    :return: None
    """

    iraf.set(uparm="./")
    iraf.digiphot()
    iraf.apphot()
    iraf.daophot(_doprint=0)
    iraf.images()

    if shifts['nmag'] < 4:
        logging.warning("Mag shift based on fewer than 4 common stars.")
        fd = open("plant.WARNING", 'a')
        fd.write("Mag shift based on fewer than 4 common stars.")
        fd.close()

    if shifts['emag'] > 0.05:
        logging.warning("Mag shift has large uncertainty.")
        fd = open("plant.WARNING", 'a')
        fd.write("Mag shift hsa large uncertainty.")
        fd.close()

    addstar = tempfile.NamedTemporaryFile(suffix=".add", mode='w')

    # transform KBO locations to this frame using the shifts provided.
    w = get_wcs(shifts)

    header = fits.open(filename)[0].header

    # set the rate of motion in units of pixels/hour instead of ''/hour
    scale = header['PIXSCAL1']
    rate = kbos['sky_rate'] / scale

    # compute the location of the KBOs in the current frame.

    # offset magnitudes from the reference frame to the current one.
    mag = kbos['mag'] - shifts['dmag']
    angle = radians(kbos['angle'])

    # Move the x/y locations to account for the sky motion of the source.
    x = kbos['x'] - rate * 24.0 * shifts['dmjd'] * cos(angle)
    y = kbos['y'] - rate * 24.0 * shifts['dmjd'] * sin(angle)
    x, y = w.wcs_world2pix(x, y, 1)

    # Each source will be added as a series of PSFs so that a new PSF is
    # added for each pixel the source moves.
    itime = float(header['EXPTIME']) / 3600.0
    npsf = fabs(rint(rate * itime)) + 1
    mag += 2.5 * log10(npsf)
    dt_per_psf = itime / npsf

    # Build an addstar file to be used in the planting of source.
    idx = 0
    for record in transpose([x, y, mag, npsf, dt_per_psf, rate, angle]):
        x = record[0]
        y = record[1]
        mag = record[2]
        npsf = record[3]
        dt = record[4]
        rate = record[5]
        angle = record[6]
        for i in range(int(npsf)):
            idx += 1
            x += dt * rate * math.cos(angle)
            y += dt * rate * math.sin(angle)
            addstar.write("{} {} {} {}\n".format(x, y, mag, idx))

    addstar.flush()
    fk_image = prefix + filename
    try:
        os.unlink(fk_image)
    except OSError as err:
        if err.errno == errno.ENOENT:
            pass
        else:
            raise

    # add the sources to the image.
    if os.access(f'{fk_image}.art', os.R_OK):
        os.unlink(f'{fk_image}.art')
    iraf.daophot.addstar(filename,
                         addstar.name,
                         psf,
                         fk_image,
                         simple=True,
                         verify=False,
                         verbose=False)
    # convert the image to short integers.
    iraf.images.chpix(fk_image, fk_image, 'ushort')
def absphot(img,_field,_catalogue,_fix,_color,rejection,_interactive,_type='fit',redo=False,show=False,cutmag=-1,database='dataredulco',_calib='sloan'):
    from astropy.io import fits
    import lsc
    import math
    import sys,re,string,os
    from lsc.util import readkey3, readhdr
    from numpy import array, compress, zeros, median, std, asarray, isfinite,mean
    from pyraf import iraf
    if show:
          from pylab import ion,plot,draw,clf
          import time
    iraf.noao(_doprint=0)
    iraf.digiphot(_doprint=0)
    iraf.daophot(_doprint=0)
    iraf.images(_doprint=0)
    iraf.imcoords(_doprint=0)
    iraf.proto(_doprint=0)
    t = fits.open(img)
    tbdata = t[1].data
    hdr2=t[1].header
    hdr=lsc.util.readhdr(img)
    _cat=readkey3(hdr,'catalog')
    _telescope=lsc.util.readkey3(hdr,'telescop')
    _instrume=lsc.util.readkey3(hdr,'instrume')
    _filter=lsc.util.readkey3(hdr,'filter')
    _airmass=lsc.util.readkey3(hdr,'airmass')
    _exptime=lsc.util.readkey3(hdr,'exptime')
    _date=lsc.util.readkey3(hdr,'date-obs')
    _object=lsc.util.readkey3(hdr,'object')
    _ra=lsc.util.readkey3(hdr,'RA')
    _dec=lsc.util.readkey3(hdr,'DEC')
    print _filter
    if _telescope in ['lsc','1m0-04','1m0-05','1m0-09']:     kk=lsc.sites.extintion('ctio')
    elif _telescope in ['elp','1m0-08']:                     kk=lsc.sites.extintion('mcdonald')
    elif _telescope in ['cpt','1m0-12','1m0-10','1m0-13']:   kk=lsc.sites.extintion('southafrica')
    elif _telescope in ['ftn','Faulkes Telescope North']:    kk=lsc.sites.extintion('mauna')
    elif _telescope in ['1m0-03','1m0-11','coj','fts','Faulkes Telescope South']:    kk=lsc.sites.extintion('siding')

    if _calib not in ['sloan','sloanprime','natural','apass','']:   colorefisso=lsc.sites.colfix(_instrume)
    else:                                                           colorefisso=lsc.sites.colfix(_instrume,_calib)

    print redo
    print _cat
    if _cat and not redo:
        print 'already calibrated'
    else:
     try:
           lsc.mysqldef.updatevalue(database,'zcat','X',string.split(re.sub('.sn2.fits','.fits',img),'/')[-1])
           if os.path.isfile(string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]):
                 lsc.mysqldef.updatevalue(database,'zcat','X',string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1])
     except: print 'module mysqldef not found'

     column=makecatalogue([img])[_filter][img]

     rasex=array(column['ra0'],float)
     decsex=array(column['dec0'],float)
     if _type=='fit':
        magsex=array(column['smagf'],float)
        magerrsex=array(column['smagerrf'],float)
     elif _type=='ph':
        magsex=array(column['magp3'],float)
        magerrsex=array(column['merrp3'],float)
     else: sys.exit(_type+' not valid (ph or fit)')
     

     print len(rasex)
     if not cutmag: cutmag=99
     #else: cutmag= cutmag-2.5*math.log10(float(_exptime))
     if len(compress( array(magsex) < float(cutmag) , magsex)) < 5 : cutmag=99  # not cut if only few object
     rasex     = compress(array(magsex,float)<=cutmag,rasex)
     decsex    = compress(array(magsex,float)<=cutmag,decsex)
     magerrsex = compress(array(magsex,float)<=cutmag,magerrsex)
     magsex    = compress(array(magsex,float)<=cutmag,array(magsex))

     print len(rasex)
     if _interactive:
        iraf.set(stdimage='imt1024')
        iraf.display(re.sub('.sn2','',img),1,fill=True,Stdout=1)
        vector=[]
        for i in range(0,len(rasex)):
            vector.append(str(rasex[i])+' '+str(decsex[i]))
        xy = iraf.wcsctran('STDIN',output="STDOUT",Stdin=vector,Stdout=1,image=img,inwcs='world',units='degrees degrees',outwcs='logical',\
                               formats='%10.1f %10.1f',verbose='yes')[3:]
        iraf.tvmark(1,'STDIN',Stdin=list(xy),mark="circle",number='yes',label='no',radii=10,nxoffse=5,nyoffse=5,color=207,txsize=2)
#        raw_input('here')
     if _catalogue:
        ######## use external catalogue
        if _catalogue[0]=='/':   stdcooC=lsc.lscastrodef.readtxt(_catalogue)
        else:                   stdcooC=lsc.lscastrodef.readtxt(lsc.__path__[0]+'/standard/cat/'+_catalogue)
        rastdC,decstdL=array(stdcooC['ra'],float),array(stdcooC['dec'],float)
        lsc.util.delete('tmp.stdL.pix')
        colonne=str(stdcooC['rapos'])+'   '+str(stdcooC['decpos'])
        if _catalogue[0]=='/': 
            iraf.wcsctran(_catalogue,'tmp.stdL.pix',img,inwcs='world',units='degrees degrees',outwcs='logical',\
                          columns=colonne,formats='%10.1f %10.1f',verbose='no')
        else:
            iraf.wcsctran(lsc.__path__[0]+'/standard/cat/'+_catalogue,'tmp.stdL.pix',img,inwcs='world',units='degrees degrees',outwcs='logical',\
                          columns=colonne,formats='%10.1f %10.1f',verbose='no')
        standardpixC=lsc.lscastrodef.readtxt('tmp.stdL.pix')
        if _interactive:
            iraf.tvmark(1,'tmp.stdL.pix',mark="circle",number='yes',label='no',radii=8,nxoffse=5,nyoffse=5,color=204,txsize=2)
        xstdC=standardpixC['ra']
        ystdC=standardpixC['dec']
        idstdC=standardpixC['id']
        xstdC=compress((array(xstdC,float)<readkey3(hdr,'XDIM'))&(array(xstdC,float)>0)&(array(ystdC,float)>0)&(array(ystdC,float)<readkey3(hdr,'YDIM')),xstdC)
        xstdL=xstdLL=xstdS=xstdC
        standardpixL=standardpixLL=standardpixS=standardpixC
        stdcooL=stdcooLL=stdcooS=stdcooC
     else:
        ######## check if it is landolt field
        stdcooL=lsc.lscastrodef.readtxt(lsc.__path__[0]+'/standard/cat/landolt.cat')
        rastdL,decstdL=array(stdcooL['ra'],float),array(stdcooL['dec'],float)
        lsc.util.delete('tmp.stdL.pix')
        iraf.wcsctran(lsc.__path__[0]+'/standard/cat/landolt.cat','tmp.stdL.pix',img,inwcs='world',units='degrees degrees',outwcs='logical',\
                          columns='1 2',formats='%10.1f %10.1f',verbose='no')
        standardpixL=lsc.lscastrodef.readtxt('tmp.stdL.pix')
        if _interactive:
            iraf.tvmark(1,'tmp.stdL.pix',mark="circle",number='yes',label='no',radii=8,nxoffse=5,nyoffse=5,color=204,txsize=2)
            print 'yelow circles sextractor'

        xstdL=standardpixL['ra']
        ystdL=standardpixL['dec']
        idstdL=standardpixL['id']
        xstdL=compress((array(xstdL,float)<readkey3(hdr,'XDIM'))&(array(xstdL,float)>0)&(array(ystdL,float)>0)&(array(ystdL,float)<readkey3(hdr,'YDIM')),xstdL)

        ######## check if it is Stetson field
        stdcooLL=lsc.lscastrodef.readtxt(lsc.__path__[0]+'/standard/cat/StetsonCat.dat')
        ww=asarray([i for i in range(len(stdcooLL['ra'])) if ( abs(float(stdcooLL['ra'][i])-float(_ra))<.2 and  abs(float(stdcooLL['dec'][i])-_dec)<.2    )])
        if len(ww)>0:
            for hh in stdcooLL.keys(): 
                if type(stdcooLL[hh])!=int:
                    if hh not in ['id','ra','dec']:
                        stdcooLL[hh]=array(array(stdcooLL[hh])[ww],float)
                    else:
                        stdcooLL[hh]=array(stdcooLL[hh])[ww]
        lll=[]
        for i in range(0,len(stdcooLL['ra'])): lll.append(stdcooLL['ra'][i]+' '+stdcooLL['dec'][i])

        rastdLL,decstdLL=array(stdcooLL['ra'],float),array(stdcooLL['dec'],float)
        lsc.util.delete('tmp.stdLL.pix')
        iraf.wcsctran('STDIN','tmp.stdLL.pix',img,inwcs='world',Stdin=lll,units='degrees degrees',outwcs='logical',\
                          columns='1 2',formats='%10.1f %10.1f',verbose='no')
        if _interactive:
            iraf.tvmark(1,'tmp.stdLL.pix',mark="cross",number='yes',label='no',radii=8,nxoffse=5,nyoffse=5,color=204,txsize=2)
            print 'red crosses Stetson'

        standardpixLL={}
        for ii in stdcooLL.keys(): standardpixLL[ii]=stdcooLL[ii]
        standardpixLL['ra']=array(iraf.proto.fields('tmp.stdLL.pix',fields='1',Stdout=1),float) #standardpixLL['ra']
        standardpixLL['dec']=array(iraf.proto.fields('tmp.stdLL.pix',fields='2',Stdout=1),float) #standardpixLL['dec']
        xstdLL=array(iraf.proto.fields('tmp.stdLL.pix',fields='1',Stdout=1),float) #standardpixLL['ra']
        ystdLL=array(iraf.proto.fields('tmp.stdLL.pix',fields='2',Stdout=1),float) #standardpixLL['dec']
        idstdLL=standardpixLL['id']

        xstdLL=compress((array(xstdLL,float)<readkey3(hdr,'XDIM'))&(array(xstdLL,float)>0)&(array(ystdLL,float)>0)&(array(ystdLL,float)<readkey3(hdr,'YDIM')),xstdLL)
        ######## check if it is sloan field
        magsel0,magsel1=12,18
        _ids=lsc.lscastrodef.sloan2file(_ra,_dec,20,float(magsel0),float(magsel1),'_tmpsloan.cat')
        ascifile='_tmpsloan.cat'
        stdcooS=lsc.lscastrodef.readtxt(ascifile)
        rastdS,decstdS=array(stdcooS['ra'],float),array(stdcooS['dec'],float)
        lsc.util.delete('tmp.stdS.pix')
        iraf.wcsctran(ascifile,'tmp.stdS.pix',img,inwcs='world',units='degrees degrees',outwcs='logical',columns='1 2',formats='%10.1f %10.1f',verbose='no')
        standardpixS=lsc.lscastrodef.readtxt('tmp.stdS.pix')
        if _interactive:
            iraf.tvmark(1,'tmp.stdS.pix',mark="cross",number='yes',label='no',radii=8,nxoffse=5,nyoffse=5,color=205,txsize=2)
            print 'green cross sloan'
        xstdS=standardpixS['ra']
        ystdS=standardpixS['dec']
        idstdS=standardpixS['id']
        xstdS=compress((array(xstdS,float)<readkey3(hdr,'XDIM'))&(array(xstdS,float)>0)&(array(ystdS,float)>0)&(array(ystdS,float)<readkey3(hdr,'YDIM')),xstdS)
        ##############################################3
     if not _catalogue and len(xstdLL)>0:
             xstdL=xstdLL
             standardpixL=standardpixLL
             stdcooL=stdcooLL

     if _filter in ['U', 'B', 'V', 'R','I','Bessell-B','Bessell-V','Bessell-R','Bessell-I']:
        filters={'U':'U', 'B':'B', 'V':'V', 'R':'R', 'I':'I','Bessell-B':'B','Bessell-V':'V','Bessell-R':'R','Bessell-I':'I'}
        if _color:
            colors=lsc.myloopdef.chosecolor(_color,False)
            if not colors:             colors={'U':['UB'],'B':['UB','BV'],'V':['BV','VR'],'R':['VR','RI'],'I':['RI']}
        else:
            colors={'U':['UB'],'B':['UB','BV'],'V':['BV','VR'],'R':['VR','RI'],'I':['RI']}
        if _field=='sloan':
                standardpix,stdcoo={'ra':[9999],'dec':[9999],'id':[1]},{'ra':[9999],'dec':[9999]}
                print 'filters and field selected do not match'
        else:
            _field='landolt'
            if len(xstdL)>=1:
                    standardpix=standardpixL
                    stdcoo=stdcooL
                    if not _catalogue:
                          if len(xstdLL)>0: _catalogue='StetsonCat.dat'
                          else:             _catalogue='landolt.dat'
            elif len(xstdS)>=1:
                if not _catalogue:  _catalogue='sdss8'  
                standardpix=standardpixS
                stdcoo=stdcooS
                stdcoo=lsc.lscastrodef.transformsloanlandolt(stdcoo)
                if not _catalogue:  _catalogue='sdss8' 
                print '\n### transform sloan in landolt'
            else:
                print 'landolt, but catalogue not found'
                standardpix,stdcoo={'ra':[9999],'dec':[9999],'id':[1]},{'ra':[9999],'dec':[9999]}
     elif _filter in  ['up','gp','rp','ip','zs','SDSS-G','SDSS-R','SDSS-I','Pan-Starrs-Z']: 
        filters={'up':'u','ip':'i','gp':'g','rp':'r','zs':'z','SDSS-G':'g','SDSS-R':'r','SDSS-I':'i','Pan-Starrs-Z':'z'}
        if _color:
            colors=lsc.myloopdef.chosecolor(_color,False)
            if not colors:             colors={'i':['ri','iz'],'r':['gr','ri'],'g':['ug','gr'],'z':['iz'],'u':['ug']}
        else:
            colors={'i':['ri','iz'],'r':['gr','ri'],'g':['ug','gr'],'z':['iz'],'u':['ug']}
        if _field=='landolt':   
                standardpix,stdcoo={'ra':[9999],'dec':[9999],'id':[1]},{'ra':[9999],'dec':[9999]}
                print 'filters and field selected do not match'
        else:
            _field='sloan'
            if len(xstdS)>=1:
                if not _catalogue:  _catalogue='sdss8' 
                standardpix=standardpixS
                stdcoo=stdcooS
            elif len(xstdL)>=1:
                standardpix=standardpixL
                stdcoo=stdcooL
                stdcoo=lsc.lscastrodef.transformlandoltsloan(stdcoo)
                if not _catalogue:  _catalogue='landolt.dat' 
                print '\n### transform landolt to sloan'
            else:
                print 'sloan, but not in the sdss footprint'
                standardpix,stdcoo={'ra':[9999],'dec':[9999],'id':[1]},{'ra':[9999],'dec':[9999]}        

     xstd=standardpix['ra']
     ystd=standardpix['dec']
     idstd=standardpix['id']
     rastd,decstd=array(stdcoo['ra'],float),array(stdcoo['dec'],float)
     xstd0=compress((array(xstd,float)<readkey3(hdr,'XDIM'))&(array(xstd,float)>0)&(array(ystd,float)>0)&(array(ystd,float)<readkey3(hdr,'YDIM')),xstd)

     if len(xstd0)>1:  ########   go only if standard stars are in the field  ##########
        magstd0={}
        airmass0={}
        result={}
        fileph={}
        print '\n###  standard field: '+str(_field)
        ystd0=compress((array(xstd,float)<readkey3(hdr,'XDIM'))&(array(xstd,float)>0)&(array(ystd,float)>0)\
                               &(array(ystd,float)<readkey3(hdr,'YDIM')),ystd)
        rastd0=compress((array(xstd,float)<readkey3(hdr,'XDIM'))&(array(xstd,float)>0)&(array(ystd,float)>0)\
                                &(array(ystd,float)<readkey3(hdr,'YDIM')),rastd)
        decstd0=compress((array(xstd,float)<readkey3(hdr,'XDIM'))&(array(xstd,float)>0)&(array(ystd,float)>0)\
                                 &(array(ystd,float)<readkey3(hdr,'YDIM')),decstd)
        idstd0=compress((array(xstd,float)<readkey3(hdr,'XDIM'))&(array(xstd,float)>0)&(array(ystd,float)>0)\
                                 &(array(ystd,float)<readkey3(hdr,'YDIM')),idstd)
        stdcoo0={}
        for key in stdcoo.keys():
              if key in 'ugrizUBVRI':
                    stdcoo0[key]=compress((array(xstd,float)<readkey3(hdr,'XDIM'))&(array(xstd,float)>0)&(array(ystd,float)>0)\
                                                &(array(ystd,float)<readkey3(hdr,'YDIM')),stdcoo[key])
        ###############################################################
        #               pos0 = standard                          pos1 = sextractor
        distvec,pos0,pos1=lsc.lscastrodef.crossmatch(array(rastd0),array(decstd0),array(rasex),array(decsex),10)
        for key in stdcoo0.keys():            stdcoo0[key]=stdcoo0[key][pos0]
        rastd0=rastd0[pos0]
        decstd0=decstd0[pos0]
        idstd0=idstd0[pos0]
        rasex=rasex[pos1]
        decsex=decsex[pos1]
        # after change in may 2013 mag in sn2.fits file are already at 1s
        magsex=magsex[pos1]-kk[filters[_filter]]*float(_airmass)  #   - K x airmass
#        magsex=magsex[pos1]+2.5*math.log10(float(_exptime))-kk[filters[_filter]]*float(_airmass)  #  mag    exptime      - K x airmass
#################################################################################
        if _field=='landolt':
            print '\n###  landolt system'
            for _filtlandolt in 'UBVRI':
                if _filtlandolt==filters[_filter]:  airmass0[_filtlandolt]=  0 #_airmass
                else: airmass0[_filtlandolt]= 0
                magstd0[_filtlandolt]=stdcoo0[_filtlandolt]
            fileph['mU']=zeros(len(rastd0))+999
            fileph['mB']=zeros(len(rastd0))+999
            fileph['mV']=zeros(len(rastd0))+999
            fileph['mR']=zeros(len(rastd0))+999
            fileph['mI']=zeros(len(rastd0))+999
            fileph['V']=magstd0['V']
            fileph['BV']=array(array(magstd0['B'],float)-array(magstd0['V'],float),str)
            fileph['UB']=array(array(magstd0['U'],float)-array(magstd0['B'],float),str)
            fileph['VR']=array(array(magstd0['V'],float)-array(magstd0['R'],float),str)
            fileph['RI']=array(array(magstd0['R'],float)-array(magstd0['I'],float),str)
        elif _field=='sloan':
            for _filtsloan in 'ugriz':
                if _filtsloan==filters[_filter]:  airmass0[_filtsloan]= 0   # _airmass
                else: airmass0[_filtsloan]=0
                magstd0[_filtsloan]=stdcoo0[_filtsloan]
            fileph['mu']=zeros(len(rastd0))+999
            fileph['mg']=zeros(len(rastd0))+999
            fileph['mr']=zeros(len(rastd0))+999
            fileph['mi']=zeros(len(rastd0))+999
            fileph['mz']=zeros(len(rastd0))+999
            fileph['r']=magstd0['r']
            fileph['gr']=array(array(magstd0['g'],float)-array(magstd0['r'],float),str)
            fileph['ri']=array(array(magstd0['r'],float)-array(magstd0['i'],float),str)
            fileph['ug']=array(array(magstd0['u'],float)-array(magstd0['g'],float),str)
            fileph['iz']=array(array(magstd0['i'],float)-array(magstd0['z'],float),str)
########################################################################################
        zero=[]
        magcor=[]
        fil = open(re.sub('.fits','.ph',img),'w')
        fil.write(str(_instrume)+' '+str(_date)+'\n')
        fil.write('*** '+_object+' '+str(len(magsex))+'\n')
        if _field=='landolt':
            fil.write('%6.6s\t%6.6s\t%6.6s\t%6.6s\t%6.6s\n' % (str(1),str(1),str(1),str(1),str(1)))  # exptime
            fil.write('%6.6s\t%6.6s\t%6.6s\t%6.6s\t%6.6s\n' % (str(airmass0['U']),str(airmass0['B']),str(airmass0['V']),str(airmass0['R']),str(airmass0['I'])))
        elif _field=='sloan':
            fil.write('%6.6s\t%6.6s\t%6.6s\t%6.6s\t%6.6s\n' % (str(1),str(1),str(1),str(1),str(1)))  # exptime
            fil.write('%6.6s\t%6.6s\t%6.6s\t%6.6s\t%6.6s\n' % (str(airmass0['u']),str(airmass0['g']),str(airmass0['r']),str(airmass0['i']),str(airmass0['z'])))
        for i in range(0,len(magsex)): 
            fileph['m'+filters[_filter]][i]=magsex[i]    #  instrumental mangitude of std in pos0[i]
            if _field=='landolt':
                stringastandard='%12.12s\t%7.7s\t%7.7s\t%7.7s\t%7.7s\t%7.7s' % (idstd0[i],fileph['V'][i],fileph['BV'][i],fileph['UB'][i],\
                                                                                fileph['VR'][i],fileph['RI'][i])
                fil.write('%7.7s\t%7.7s\t%7.7s\t%7.7s\t%7.7s\t%60.60s\n' \
                              % (str(fileph['mU'][i]),str(fileph['mB'][i]),str(fileph['mV'][i]),str(fileph['mR'][i]),str(fileph['mI'][i]),str(stringastandard)))
            elif _field=='sloan':
                stringastandard='%12.12s\t%7.7s\t%7.7s\t%7.7s\t%7.7s\t%7.7s' % (idstd0[i],fileph['r'][i],fileph['gr'][i],fileph['ug'][i],\
                                                                                fileph['ri'][i],fileph['iz'][i])
                fil.write('%7.7s\t%7.7s\t%7.7s\t%7.7s\t%7.7s\t%60.60s\n' \
                              % (str(fileph['mu'][i]),str(fileph['mg'][i]),str(fileph['mr'][i]),str(fileph['mi'][i]),str(fileph['mz'][i]),str(stringastandard)))
            zero.append(float(float(magstd0[filters[_filter]][i]))-float(magsex[i]))
            magcor.append(magsex[i])
        fil.close()

        if show:
              import time
              from pylab import ion,plot,draw
              ion()
              aa=mean(compress(abs(array(zero))<99,zero))
              xxx=compress((abs(array(magcor))<99)&(abs(array(zero))<99),magcor)
              yyy=compress((abs(array(zero))<99)&(abs(array(magcor))<99),zero)
              plot(xxx,yyy,'or')
              plot([min(compress(abs(array(magcor))<99,magcor)),max(compress(abs(array(magcor))<99,magcor))],[aa,aa],'-b')
              draw()
              print std(compress(abs(array(zero))<99,zero))
              time.sleep(5)

        colorvec=colors[filters[_filter]]
        for col in colorvec:
            col0=magstd0[col[0]] 
            col1=magstd0[col[1]]
            colstd0=array(col0,float)-array(col1,float)
            ################## sex  ######################
            colore=[]
            for i in range(0,len(pos1)):   colore.append(colstd0[i])

            colore1=compress(abs(array(zero))<50,array(colore))
            zero1=compress(abs(array(zero))<50,array(zero))
            zero2=compress(abs(array(colore1))<2,array(zero1))
            colore2=compress(abs(array(colore1))<2,array(colore1))
            if _fix: fisso=colorefisso[filters[_filter]+col]
            else: fisso=''

            if len(colore2)==0:
                print 'no calibration, '+_filter+' '+_field
                b,a,sa,sb=9999,9999,0,0
            else:
                if _interactive:    a,sa,b,sb=fitcol(colore2,zero2,_filter,col,fisso)
                else:               a,sa,b,sb=fitcol2(colore2,zero2,_filter,col,fisso,show,rejection)

            print a,sa,b,sb
            result[filters[_filter]+col]=[a,sa,b,sb]
        if result:
            print '\n### zeropoint ..... done at airmass 0'
            if _catalogue:
                lsc.util.updateheader(img,0,{'CATALOG':[str(string.split(_catalogue,'/')[-1]),'catalogue source']})
            stringa=''
            for ll in result:
                for kk in range(0,len(result[ll])):
                                    if not isfinite(result[ll][kk]): result[ll][kk]=0.0 
                valore='%3.3s %6.6s %6.6s  %6.6s  %6.6s' %  (str(ll),str(result[ll][0]),str(result[ll][2]),str(result[ll][1]),str(result[ll][3]))
                print '### ',valore
                lsc.util.updateheader(img,0,{'zp'+ll:[str(valore),'a b sa sb in y=a+bx']})
                if ll[0]==ll[2]: num=2
                elif ll[0]==ll[1]: num=1
                else: sys.exit('somthing wrong with color '+ll)
                print ll,num
                try:
                    print 'zcol'+str(num),ll[1:],string.split(re.sub('.sn2.fits','.fits',img),'/')[-1]
                    lsc.mysqldef.updatevalue(database,'zcol'+str(num),ll[1:],string.split(re.sub('.sn2.fits','.fits',img),'/')[-1])
                    lsc.mysqldef.updatevalue(database,'z'+str(num),result[ll][0],string.split(re.sub('.sn2.fits','.fits',img),'/')[-1])
                    lsc.mysqldef.updatevalue(database,'c'+str(num),result[ll][2],string.split(re.sub('.sn2.fits','.fits',img),'/')[-1])
                    lsc.mysqldef.updatevalue(database,'dz'+str(num),result[ll][1],string.split(re.sub('.sn2.fits','.fits',img),'/')[-1])
                    lsc.mysqldef.updatevalue(database,'dc'+str(num),result[ll][3],string.split(re.sub('.sn2.fits','.fits',img),'/')[-1])
                    if os.path.isfile(string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]):
                          lsc.mysqldef.updatevalue(database,'zcol'+str(num),ll[1:],string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1])
                          lsc.mysqldef.updatevalue(database,'z'+str(num),result[ll][0],string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1])
                          lsc.mysqldef.updatevalue(database,'c'+str(num),result[ll][2],string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1])
                          lsc.mysqldef.updatevalue(database,'dz'+str(num),result[ll][1],string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1])
                          lsc.mysqldef.updatevalue(database,'dc'+str(num),result[ll][3],string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1])
                    if result[ll][0]!=9999:
                          print _catalogue
                          lsc.mysqldef.updatevalue(database,'zcat',string.split(_catalogue,'/')[-1],string.split(re.sub('.sn2.fits','.fits',img),'/')[-1])
                          if os.path.isfile(string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]):
                                lsc.mysqldef.updatevalue(database,'zcat',string.split(_catalogue,'/')[-1],string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1])
                    else:
                        lsc.mysqldef.updatevalue(database,'zcat','X',string.split(re.sub('.sn2.fits','.fits',img),'/')[-1])
                        if os.path.isfile(string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]):
                              lsc.mysqldef.updatevalue(database,'zcat','X',string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1])
                except: print 'module mysqldef not found'
Exemple #57
0
def plant_kbos(filename, psf, kbos, shifts, prefix):
    """
    Add KBOs to an image
    :param filename: name of the image to add KBOs to
    :param psf: the Point Spread Function in IRAF/DAOPHOT format to be used by ADDSTAR
    :param kbos: list of KBOs to add, has format as returned by KBOGenerator
    :param shifts: dictionary with shifts to transfer to coordinates to reference frame.
    :param prefix: an estimate FWHM of the image, used to determine trailing.
    :return: None
    """

    iraf.set(uparm="./")
    iraf.digiphot()
    iraf.apphot()
    iraf.daophot(_doprint=0)
    iraf.images()

    if shifts['nmag'] < 4:
        logging.warning("Mag shift based on fewer than 4 common stars.")
        fd = open("plant.WARNING", 'a')
        fd.write("Mag shift based on fewer than 4 common stars.")
        fd.close()

    if shifts['emag'] > 0.05:
        logging.warning("Mag shift has large uncertainty.")
        fd = open("plant.WARNING", 'a')
        fd.write("Mag shift hsa large uncertainty.")
        fd.close()

    addstar = tempfile.NamedTemporaryFile(suffix=".add")

    # transform KBO locations to this frame using the shifts provided.
    w = get_wcs(shifts)

    header = fits.open(filename)[0].header

    # set the rate of motion in units of pixels/hour instead of ''/hour
    scale = header['PIXSCAL1']
    rate = kbos['sky_rate']/scale

    # compute the location of the KBOs in the current frame.

    # offset magnitudes from the reference frame to the current one.
    mag = kbos['mag'] - shifts['dmag']
    angle = radians(kbos['angle'])

    # Move the x/y locations to account for the sky motion of the source.
    x = kbos['x'] - rate*24.0*shifts['dmjd']*cos(angle)
    y = kbos['y'] - rate*24.0*shifts['dmjd']*sin(angle)
    x, y = w.wcs_world2pix(x, y, 1)

    # Each source will be added as a series of PSFs so that a new PSF is added for each pixel the source moves.
    itime = float(header['EXPTIME'])/3600.0
    npsf = fabs(rint(rate * itime)) + 1
    mag += 2.5*log10(npsf)
    dt_per_psf = itime/npsf

    # Build an addstar file to be used in the planting of source.
    idx = 0
    for record in transpose([x, y, mag, npsf, dt_per_psf, rate, angle]):
        x = record[0]
        y = record[1]
        mag = record[2]
        npsf = record[3]
        dt = record[4]
        rate = record[5]
        angle = record[6]
        for i in range(int(npsf)):
            idx += 1
            x += dt*rate*math.cos(angle)
            y += dt*rate*math.sin(angle)
            addstar.write("{} {} {} {}\n".format(x, y, mag, idx))

    addstar.flush()
    fk_image = prefix+filename
    try:
        os.unlink(fk_image)
    except OSError as err:
        if err.errno == errno.ENOENT:
            pass
        else:
            raise

    # add the sources to the image.
    iraf.daophot.addstar(filename, addstar.name, psf, fk_image,
                         simple=True, verify=False, verbose=False)
    # convert the image to short integers.
    iraf.images.chpix(fk_image, fk_image, 'ushort')