Esempio n. 1
0
def runall(prefixes,cat):
    for prefix in prefixes:
	images=glob.glob(prefix)
	iraf.imgets(images[0],'FILTER')
	filter=str(iraf.imgets.value)

	for im in images:
	    runsextractor(im)

	    t=im.split('.')
	    if cat < 0.1:
		out='sdssmatch-'+str(t[0])
		refcat='sdsscoords.dat'
		s='cat '+out+' >> sdssmatch'+filter
		plotsdsspos(im)
	    if cat > .1:
		out='2massmatch-'+str(t[0])
		refcat='2masscoords.dat'
		s='cat '+out+' >> 2massmatch'+filter
		plot2masspos(im)
	    #iraf.xyxymatch(input='testxy.cat',reference='sdsscoords.dat',output=out,tolerance=20.,refpoints='refpoints',interactive='yes')
	    iraf.xyxymatch(input='testxy.cat',reference=refcat,output=out,tolerance=10.,refpoints='refpoints',interactive='no')
	    os.system(s)

	infile='sdssmatch'+filter
	infile='2massmatch'+filter
	iraf.geomap(input=infile,database='PiscesBok',transform=filter,xmin=1.,xmax=1024,ymin=1.,ymax=1024.)
Esempio n. 2
0
 def geomap(self, xmax=-1, ymax=-1, fitgeometry='rscale'):
     if not self.coofile:
         raise ValueError, "Specify self.coofile first!"
     if os.path.exists(self.database):
         os.remove(self.database)
     if xmax < 0: xmax = self.hdr_ref['naxis1']
     if ymax < 0: ymax = self.hdr_ref['naxis2']
     iraf.geomap(self.coofile, self.database, 1, xmax, 1, ymax,
                 interactive='no', fitgeometry=fitgeometry)
Esempio n. 3
0
 def geomap(self, database, xmax=-1, ymax=-1):
     if not self.coofile:
         raise ValueError, "Specify self.coofile first!"
     if os.path.exists(database):
         os.remove(database)
     if xmax < 0: xmax = self.hdr_in['naxis1']
     if ymax < 0: ymax = self.hdr_in['naxis2']
     iraf.geomap(self.coofile, database, 1, xmax, 1, ymax, interactive='no')
     self.database = database
def georun_sky(clustname, lims):

    #run geomap and geoxytran.  I have to run this within this module
    #as geomap crashes when run from an external module.

    #in this version

    catpath = '.'
    geomap_infile = catpath + '/geomap_coords_sky.' + clustname + '.in'
    dbfile = catpath + '/' + clustname + '_geomap_sky.db'
    iraf.geomap.xmin = lims['ramin']
    iraf.geomap.xmax = lims['ramax']
    iraf.geomap.ymin = lims['decmin']
    iraf.geomap.ymax = lims['decmax']
    iraf.geomap.maxiter = 3
    iraf.geomap.reject = 3.0
    iraf.geomap.fitgeometry = 'rxyscale'

    iraf.geomap(geomap_infile, dbfile)

    geotran_outfile = catpath + '/geomap_coords_sky.' + clustname + '.out'

    #remove geotran output file if it already exists
    if os.path.isfile(geotran_outfile) is True:
        cmdstr = 'rm ' + geotran_outfile
        os.system(cmdstr)

    #transform the input coordinates in the geomap input file.
    iraf.geoxytran(geomap_infile,
                   geotran_outfile,
                   dbfile,
                   geomap_infile,
                   direction="backward",
                   xcolumn=3,
                   ycolumn=4)

    #read in the geomap input catalog that was transformed using geoxytran.
    trans_ggdat = ascii.read(geotran_outfile)
    ratrans = trans_ggdat['rain']
    dectrans = trans_ggdat['decin']
    raref = trans_ggdat['raref']
    decref = trans_ggdat['decref']

    #make a plot of the new residuals
    posttrans_plotfile = catpath + '/posttrans.' + clustname + '_coordiff_sky.pdf'
    cmts.match_diff_sky_plot(raref,
                             decref,
                             ratrans,
                             dectrans,
                             plotfile=posttrans_plotfile)

    return dbfile, geomap_infile
def AlignImage(TargetImg,Template,AlignedImageName,fitgeometry="general"):
    """Align Template Image to TargetImage to create output image AlignedImageName.
    fitgeometry can be set to 'rotate' when the Template is also TIRSPEC data
    Otherwise if Template is 2MASS or other instrument set it as 'general'  """
    MatchingXYcoordsFile = os.path.splitext(AlignedImageName)[0]+'.XYXY'
    if not os.path.isfile(MatchingXYcoordsFile) :
        CreateMatchingXYCoords(TargetImg,Template,MatchingXYcoordsFile)
    else:
        print('Using Old {0} file'.format(MatchingXYcoordsFile))

    XYtranformdb = os.path.splitext(AlignedImageName)[0]+'rtran.db'
    iraf.geomap(input=MatchingXYcoordsFile, database=XYtranformdb, xmin=1, xmax=1024, ymin=1, ymax=1024, interactive=0,fitgeometry=fitgeometry)
    #Now geotran the Template image to match the TargetImg
    iraf.geotran(input=Template,output=AlignedImageName,database=XYtranformdb,transforms=MatchingXYcoordsFile)
    #return the transfgorm .db filename just incase it is needed for the user
    return XYtranformdb
Esempio n. 6
0
            pox_ini=j
            l=i
            m=j
            print 500,i+1,j+1-4,i+1
        #se ve que se este en el otro
        if (imagen[i+1,j+1] != 0) and (imagen[i+1,j+2] == 0):
            pox_fin=j
            print 526,i+1,j+4+1,i+1 

        else:
            pos_ini=pos_ini
            pos_fin=pos_fin

sys.stdout.close() 
sys.stdout=temp
 


####
#
# Ejecuta geomap, mapea el x e y del orden curvo a uno nuevo recto 
# running geomap 
#
####

import pyraf
from pyraf import iraf
from iraf import immatch, geomap

iraf.geomap(input=file_out,database=file_out+"_database.txt",fitgeometry="general",function="polynomial",xxorder=3,xyorder=3,yxorder=3,yyorder=3,maxiter=10)
Esempio n. 7
0
        workdir, "geodatabase"
    )  # better, it prevents pbs when doing simultaneous alignments with same pipe.
    if os.path.isfile(databasename):
        os.remove(databasename)

    iraf.unlearn(iraf.geomap)
    iraf.geomap.fitgeom = "rscale"  # shift, xyscale, rotate, rscale
    iraf.geomap.function = "polynomial"
    iraf.geomap.transfo = "broccoli"
    iraf.geomap.interac = "no"
    #iraf.geomap.verbose="no"

    mapblabla = iraf.geomap(input=geomapin,
                            database=databasename,
                            xmin=1,
                            xmax=dimx,
                            ymin=1,
                            ymax=dimy,
                            Stdout=1)

    for line in mapblabla:
        if "X and Y scale:" in line:
            mapscale = line.split()[4:6]
        if "Xin and Yin fit rms:" in line:
            maprmss = line.split()[-2:]
        if "X and Y axis rotation:" in line:
            mapangles = line.split()[-4:-2]
        if "X and Y shift:" in line:
            mapshifts = line.split()[-4:-2]

    geomaprms = math.sqrt(
Esempio n. 8
0
def rectify_image(image,
                  reference,
                  output,
                  refout=None,
                  fitgeometry='general',
                  function='polynomial',
                  xxorder=3,
                  yyorder=3,
                  xyorder=3,
                  yxorder=3,
                  clobber=1,
                  objects=None,
                  interactive=1,
                  maxiter=5,
                  reject=3,
                  xrange=None,
                  yrange=None,
                  thresh=2.0,
                  scale=0.125,
                  fwhm=4,
                  image_sigma=None,
                  reference_sigma=None,
                  debug=0,
                  aoffset=0):
    '''This task will take an image and a reference image and apply geomap/
   geotran to aligned them.  image is registered and output to output.
   You can specify the fit geometry (shift, xyscale, rotate, rscale,
   rxyscale, general).  You can also choose the geenral function
   (legendre, chebyshev, polynomial).  You can specfy the order of all
   the terms (xxorder, yyorder, xyorder, yxorder).  If you choose
   clobber=1 (the default), new images overwrite older ones.  Lastly, if
   you want to use a pre-existing list of objects, specify them as the
   objects keyword.'''

    # First, if we already have objects to use, save time by not finding them
    #  again!

    if objects is None:
        sex(image,
            output='image.cat',
            thresh=thresh,
            scale=scale,
            fwhm=fwhm,
            sigma_map=image_sigma)
        sex(reference,
            output='reference.cat',
            thresh=thresh,
            scale=scale,
            fwhm=fwhm,
            sigma_map=reference_sigma)

        im_masks1 = get_mask(image)
        im_masks2 = get_mask(reference)
        if aoffset == 0:
            # Check the ROTANG header field to see if we need to give a rotation
            # offset to the object matcher.
            rotang1 = get_header(reference, "ROTANG", float)
            rotang2 = get_header(image, "ROTANG", float)
            if rotang1 is not None and rotang2 is not None:
                if abs(rotang1 - rotang2) > 0.1:
                    aoffset = rotang1 - rotang2

        # Run the matchum routine to get corresponding objects:
        (x2, y2, x1, y1, o1, o2) = matchum('image.cat',
                                           'reference.cat',
                                           im_masks1=im_masks1,
                                           im_masks2=im_masks2,
                                           xrange=xrange,
                                           yrange=yrange,
                                           debug=debug,
                                           aoffset=aoffset)

        f = open('geomap.objects', 'w')
        for i in range(len(x1)):
            f.write('%.2f %.2f %.2f %.2f\n' % (x1[i], y1[i], x2[i], y2[i]))
        f.close()
        objects = "geomap.objects"
    else:
        data = pygplot.columns(objects)
        x1 = data[0]
        y1 = data[1]
        x2 = data[2]
        y2 = data[3]

    xshift = int(stats.bwt(x2 - x1)[0])
    yshift = int(stats.bwt(y2 - y1)[0])

    # We now have some selected objects to use with geomap.  Let's figure out
    #  the area of validity for the data using ths shifts computed above.
    (r_xmax, r_ymax) = get_image_size(reference)
    if xshift < 0:
        xmin = -xshift
        xmax = r_xmax
    else:
        xmin = 1
        xmax = r_xmax - xshift
    if yshift < 0:
        ymin = -yshift
        ymax = r_ymax
    else:
        ymin = 1
        ymax = r_ymax - yshift
    #xmin = min(x1);  xmax = max(x1)
    #ymin = min(y1);  ymax = max(y1)
    print xshift, yshift, xmin, xmax, ymin, ymax

    iraf.images()
    if os.path.isfile('geomap.db'): os.unlink('geomap.db')

    # We may not have enough points to do a general fit, so we do them in order of
    #   complexity.  Hopefully, we have at least ONE point (for the shift)
    fitgeometries = [fitgeometry, 'rxyscale', 'rotate', 'shift']

    success = 0
    for fitgeo in fitgeometries:
        print "using fitgeometry = ", fitgeo
        res = iraf.geomap(input=objects,
                          database='geomap.db',
                          xmin=1,
                          ymin=1,
                          xmax=r_xmax,
                          ymax=r_ymax,
                          fitgeometry=fitgeo,
                          function=function,
                          xxorder=xxorder,
                          yyorder=yyorder,
                          xyorder=xyorder,
                          yxorder=yxorder,
                          results='',
                          transforms=objects,
                          interactive=interactive,
                          maxiter=maxiter,
                          reject=reject,
                          Stdout=1)
        # assume all went well
        success = 1
        for line in res:
            print line
            if re.search('Too few points', line):
                success = 0
        if success:
            break

    if clobber and os.path.isfile(output): iraf.imdel(output)
    iraf.geotran(input=image,
                 output='temp.rectify',
                 database='geomap.db',
                 transforms=objects,
                 geometry='geometric',
                 interpolant="linear")
    section = "[%d:%d,%d:%d]" % (xmin, xmax, ymin, ymax)
    print 'Valid section of data:', section
    iraf.imcopy(input='temp.rectify', output=output)
    iraf.imdel('temp.rectify')
    return (xmin, xmax, ymin, ymax)
Esempio n. 9
0
def mscimage(input=None, output=None, format='image', pixmask=no,verbose=')_.verbose',wcssource='image',reference='',ra=INDEF,dec=INDEF,scale=INDEF,rotation=INDEF,blank=0.0,interpolant='poly5',minterpolant='linear',boundary='reflect',constant=0.0,fluxconserve=no,ntrim=8,nxblock=INDEF,nyblock=INDEF,interactive=no,nx=10,ny=20,fitgeometry='general',xxorder=4,xyorder=4,xxterms='half',yxorder=4,yyorder=4,yxterms='half',fd_in='',fd_ext='',fd_coord='',mode='ql',DOLLARnargs=0,taskObj=None):

	Vars = IrafParList('mscimage')
	Vars.addParam(makeIrafPar(input, datatype='string', name='input', mode='a',prompt='List of input mosaic exposures'))
	Vars.addParam(makeIrafPar(output, datatype='string', name='output',mode='a',prompt='List of output images'))
	Vars.addParam(makeIrafPar(format, datatype='string', name='format',enum=['image', 'mef'],mode='h',prompt='Output format (image|mef)'))
	Vars.addParam(makeIrafPar(pixmask, datatype='bool', name='pixmask',mode='h',prompt='Create pixel mask?'))
	Vars.addParam(makeIrafPar(verbose, datatype='bool', name='verbose',mode='h',prompt='Verbose output?\n\n# Output WCS parameters'))
	Vars.addParam(makeIrafPar(wcssource, datatype='string', name='wcssource',enum=['image', 'parameters', 'match'],mode='h',prompt='Output WCS source (image|parameters|match)'))
	Vars.addParam(makeIrafPar(reference, datatype='file', name='reference',mode='h',prompt='Reference image'))
	Vars.addParam(makeIrafPar(ra, datatype='real', name='ra', max=24.0,min=0.0,mode='h',prompt='RA of tangent point (hours)'))
	Vars.addParam(makeIrafPar(dec, datatype='real', name='dec', max=90.0,min=-90.0,mode='h',prompt='DEC of tangent point (degrees)'))
	Vars.addParam(makeIrafPar(scale, datatype='real', name='scale', mode='h',prompt='Scale (arcsec/pixel)'))
	Vars.addParam(makeIrafPar(rotation, datatype='real', name='rotation',max=360.0,min=-360.0,mode='h',prompt='Rotation of DEC from N to E (degrees)\n\n# Resampling parmeters'))
	Vars.addParam(makeIrafPar(blank, datatype='real', name='blank', mode='h',prompt='Blank value'))
	Vars.addParam(makeIrafPar(interpolant, datatype='string',name='interpolant',mode='h',prompt='Interpolant for data'))
	Vars.addParam(makeIrafPar(minterpolant, datatype='string',name='minterpolant',mode='h',prompt='Interpolant for mask'))
	Vars.addParam(makeIrafPar(boundary, datatype='string', name='boundary',enum=['nearest', 'constant', 'reflect', 'wrap'],mode='h',prompt='Boundary extension'))
	Vars.addParam(makeIrafPar(constant, datatype='real', name='constant',mode='h',prompt='Constant boundary extension value'))
	Vars.addParam(makeIrafPar(fluxconserve, datatype='bool',name='fluxconserve',mode='h',prompt='Preserve flux per unit area?'))
	Vars.addParam(makeIrafPar(ntrim, datatype='int', name='ntrim', min=0,mode='h',prompt='Edge trim in each extension'))
	Vars.addParam(makeIrafPar(nxblock, datatype='int', name='nxblock',mode='h',prompt='X dimension of working block size in pixels'))
	Vars.addParam(makeIrafPar(nyblock, datatype='int', name='nyblock',mode='h',prompt='Y dimension of working block size in pixels\n\n# Geometric mapping parameters'))
	Vars.addParam(makeIrafPar(interactive, datatype='bool', name='interactive',mode='h',prompt='Fit mapping interactively?'))
	Vars.addParam(makeIrafPar(nx, datatype='int', name='nx', mode='h',prompt='Number of x grid points'))
	Vars.addParam(makeIrafPar(ny, datatype='int', name='ny', mode='h',prompt='Number of y grid points'))
	Vars.addParam(makeIrafPar(fitgeometry, datatype='string',name='fitgeometry',enum=['shift', 'xyscale', 'rotate', 'rscale', 'rxyscale', 'general'],mode='h',prompt='Fitting geometry'))
	Vars.addParam(makeIrafPar(xxorder, datatype='int', name='xxorder', min=2,mode='h',prompt='Order of x fit in x'))
	Vars.addParam(makeIrafPar(xyorder, datatype='int', name='xyorder', min=2,mode='h',prompt='Order of x fit in y'))
	Vars.addParam(makeIrafPar(xxterms, datatype='string', name='xxterms',mode='h',prompt='X fit cross terms type'))
	Vars.addParam(makeIrafPar(yxorder, datatype='int', name='yxorder', min=2,mode='h',prompt='Order of y fit in x'))
	Vars.addParam(makeIrafPar(yyorder, datatype='int', name='yyorder', min=2,mode='h',prompt='Order of y fit in y'))
	Vars.addParam(makeIrafPar(yxterms, datatype='string', name='yxterms',mode='h',prompt='Y fit cross terms type\n\n'))
	Vars.addParam(makeIrafPar(fd_in, datatype='struct', name='fd_in',list_flag=1,mode='h',prompt=''))
	Vars.addParam(makeIrafPar(fd_ext, datatype='struct', name='fd_ext',list_flag=1,mode='h',prompt=''))
	Vars.addParam(makeIrafPar(fd_coord, datatype='struct', name='fd_coord',list_flag=1,mode='h',prompt=''))
	Vars.addParam(makeIrafPar(mode, datatype='string', name='mode', mode='h',prompt=''))
	Vars.addParam(makeIrafPar(DOLLARnargs, datatype='int', name='$nargs',mode='h'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='in', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='out', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='ref', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='pl', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='image', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='trimsec', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='outsec', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='plsec', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='inlists', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='extlist', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='pllist', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='coord', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='db', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='wcsref', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='outtemp', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='file', name='pltemp', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='nc', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='nl', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='ncref', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='nlref', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='cmin', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='cmax', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='lmin', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='lmax', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='nimage', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='nimages', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='nxblk', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='int', name='nyblk', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='real', name='x', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='real', name='y', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='real', name='rval', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='real', name='xmin', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='real', name='xmax', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='real', name='ymin', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='real', name='ymax', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='real', name='crpix1', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='real', name='crpix2', mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='string', name='extname',mode='u'))
	Vars.addParam(makeIrafPar(None, datatype='string', name='str', mode='u'))

	iraf.cache('mscextensions', 'mscgmask')
	Vars.inlists = iraf.mktemp('tmp$iraf')
	Vars.extlist = iraf.mktemp('tmp$iraf')
	Vars.pllist = iraf.mktemp('tmp$iraf')
	Vars.coord = iraf.mktemp('tmp$iraf')
	Vars.db = iraf.mktemp('tmp$iraf')
	Vars.outtemp = iraf.mktemp('tmp')
	Vars.wcsref = iraf.mktemp('tmp')
	Vars.pltemp = iraf.mktemp('tmp')
	iraf.joinlists(Vars.input, Vars.output, output = Vars.inlists, delim = ' ',short=yes,type = 'image')
	Vars.fd_in = Vars.inlists
	while (iraf.fscan(locals(), 'Vars.fd_in', 'Vars.PYin', 'Vars.out') != EOF):
		if (iraf.imaccess(Vars.out)):
			iraf.printf('Warning: Image already exists (%s)\n', Vars.out)
			continue
		if (Vars.pixmask):
			Vars.pl = Vars.out
			Vars.nc = iraf.strlen(Vars.pl)
			if (Vars.nc > 5 and iraf.substr(Vars.pl, Vars.nc - 4, Vars.nc) == '.fits'):
				Vars.pl = iraf.substr(Vars.pl, 1, Vars.nc - 5)
			elif (Vars.nc > 4 and iraf.substr(Vars.out, Vars.nc - 3, Vars.nc) == '.imh'):
				Vars.pl = iraf.substr(Vars.pl, 1, Vars.nc - 4)
			Vars.pl = Vars.pl + '_bpm'
			if (Vars.format == 'image' and iraf.imaccess(Vars.pl)):
				iraf.printf('Warning: Mask already exists (%s)\n', Vars.pl)
				continue
		else:
			Vars.pl = ''
		iraf.mscextensions(Vars.PYin, output = 'file', index = '0-',extname = '',extver = '',lindex = no,lname = yes,lver = no,ikparams = '',Stdout=Vars.extlist)
		Vars.nimages = int(iraf.mscextensions.nimages)
		Vars.nimage = 0
		if (Vars.nimages < 1):
			iraf.printf("WARNING: No input image data found in `%s'.\
",Vars.PYin)
			iraf.delete(Vars.extlist, verify = no)
			continue
		if (not iraf.imaccess(Vars.wcsref)):
			Vars.ref = Vars.reference
			if (Vars.wcssource == 'match'):
				Vars.wcsref = Vars.ref
			else:
				iraf.mscwtemplate('@' + Vars.extlist, Vars.wcsref,wcssource = Vars.wcssource,reference = Vars.ref,ra = Vars.ra,dec = Vars.dec,scale = Vars.scale,rotation = Vars.rotation,projection = '',verbose = Vars.verbose)
		Vars.fd_ext = Vars.extlist
		while (iraf.fscan(locals(), 'Vars.fd_ext', 'Vars.image') != EOF):
			Vars.nimage = Vars.nimage + 1
			if (Vars.nimages > 1):
				Pipe1 = iraf.hselect(Vars.image, 'extname', yes, Stdout=1)
				iraf.scan(locals(), 'Vars.extname', Stdin=Pipe1)
				del Pipe1
				if (iraf.nscan() == 0):
					Vars.extname = 'im' + str(Vars.nimage)
				Pipe1 = iraf.printf('%s[%s,append]\n', Vars.outtemp,Vars.extname,Stdout=1)
				iraf.scan(locals(), 'Vars.outsec', Stdin=Pipe1)
				del Pipe1
				Pipe1 = iraf.printf('%s%s\n', Vars.pl, Vars.extname, Stdout=1)
				iraf.scan(locals(), 'Vars.plsec', Stdin=Pipe1)
				del Pipe1
			else:
				Vars.extname = ''
				Vars.outsec = Vars.outtemp
				Vars.plsec = Vars.pl
			if (Vars.pixmask and iraf.imaccess(Vars.plsec)):
				iraf.delete(Vars.coord, verify = no)
				iraf.delete(Vars.db, verify = no)
				iraf.printf('Warning: Mask already exists (%s)\n', Vars.plsec)
				continue
			if (Vars.verbose):
				iraf.printf('Resampling %s ...\n', Vars.image)
			Pipe1 = iraf.hselect(Vars.image, 'naxis1,naxis2', yes, Stdout=1)
			iraf.scan(locals(), 'Vars.nc', 'Vars.nl', Stdin=Pipe1)
			del Pipe1
			Vars.cmin = 1 + Vars.ntrim
			Vars.cmax = Vars.nc - Vars.ntrim
			Vars.lmin = 1 + Vars.ntrim
			Vars.lmax = Vars.nl - Vars.ntrim
			Pipe1 = iraf.printf('[%d:%d,%d:%d]\n', Vars.cmin, Vars.cmax,Vars.lmin,Vars.lmax,Stdout=1)
			iraf.scan(locals(), 'Vars.trimsec', Stdin=Pipe1)
			del Pipe1
			if (Vars.wcssource == 'match'):
				Pipe1 = iraf.hselect(Vars.ref, 'naxis1,naxis2', yes, Stdout=1)
				iraf.scan(locals(), 'Vars.ncref', 'Vars.nlref', Stdin=Pipe1)
				del Pipe1
				Vars.xmin = (Vars.ncref - 1.) / (Vars.nx - 1.)
				Vars.ymin = (Vars.nlref - 1.) / (Vars.ny - 1.)
				Vars.ymax = 1
				while (Vars.ymax <= Vars.nlref + 1):
					Vars.xmax = 1
					while (Vars.xmax <= Vars.ncref + 1):
						iraf.clPrint(Vars.xmax, Vars.ymax, Vars.xmax,Vars.ymax,StdoutAppend=Vars.coord)
						Vars.xmax = Vars.xmax + Vars.xmin
					Vars.ymax = Vars.ymax + Vars.ymin
				iraf.mscctran(Vars.coord, Vars.db, Vars.ref, 'logical','world',columns = '3 4',units = '',formats = '%.4H %.3h',min_sigdigit = 10,verbose = no)
				iraf.delete(Vars.coord, verify=no)
				iraf.wcsctran(Vars.db, Vars.coord, Vars.image + Vars.trimsec,inwcs = 'world',outwcs = 'logical',columns = '3 4',units = 'hours native',formats = '',min_sigdigit = 10,verbose = no)
				iraf.delete(Vars.db, verify=no)
			else:
				Vars.nc = Vars.cmax - Vars.cmin + 1
				Vars.nl = Vars.lmax - Vars.lmin + 1
				Vars.xmin = (Vars.nc - 1.) / (Vars.nx - 1.)
				Vars.ymin = (Vars.nl - 1.) / (Vars.ny - 1.)
				Vars.ymax = 1
				while (Vars.ymax <= Vars.nl + 1):
					Vars.xmax = 1
					while (Vars.xmax <= Vars.nc + 1):
						iraf.clPrint(Vars.xmax, Vars.ymax, Vars.xmax,Vars.ymax,StdoutAppend=Vars.coord)
						Vars.xmax = Vars.xmax + Vars.xmin
					Vars.ymax = Vars.ymax + Vars.ymin
				iraf.mscctran(Vars.coord, Vars.db, Vars.image + Vars.trimsec,'logical','world',columns = '1 2',units = '',formats = '%.4H %.3h',min_sigdigit = 10,verbose = no)
				iraf.delete(Vars.coord, verify=no)
				iraf.wcsctran(Vars.db, Vars.coord, Vars.wcsref,inwcs = 'world',outwcs = 'logical',columns = '1 2',units = 'hours native',formats = '',min_sigdigit = 10,verbose = no)
				iraf.delete(Vars.db, verify=no)
			Vars.xmax = 0.
			Vars.xmin = 1.
			Vars.ymax = 0.
			Vars.ymin = 1.
			Vars.fd_coord = Vars.coord
			while (iraf.fscan(locals(), 'Vars.fd_coord', 'Vars.x', 'Vars.y') != EOF):
				if (iraf.nscan() < 2):
					continue
				if (Vars.xmax < Vars.xmin):
					Vars.xmin = Vars.x
					Vars.xmax = Vars.x
					Vars.ymin = Vars.y
					Vars.ymax = Vars.y
				else:
					Vars.xmin = float(iraf.minimum(Vars.x, Vars.xmin))
					Vars.xmax = float(iraf.maximum(Vars.x, Vars.xmax))
					Vars.ymin = float(iraf.minimum(Vars.y, Vars.ymin))
					Vars.ymax = float(iraf.maximum(Vars.y, Vars.ymax))
			Vars.fd_coord = ''
			if (Vars.xmax <= Vars.xmin or Vars.ymax <= Vars.ymin):
				iraf.error(1, 'No overlap for matching reference')
			Vars.cmin = int(iraf.nint(Vars.xmin - 1.5))
			Vars.cmax = int(iraf.nint(Vars.xmax + 1.5))
			Vars.lmin = int(iraf.nint(Vars.ymin - 1.5))
			Vars.lmax = int(iraf.nint(Vars.ymax + 1.5))
			iraf.geomap(Vars.coord, Vars.db, Vars.cmin, Vars.cmax, Vars.lmin,Vars.lmax,transforms = '',results = '',fitgeometry = Vars.fitgeometry,function = 'chebyshev',xxorder = Vars.xxorder,xyorder = Vars.xyorder,xxterms = Vars.xxterms,yxorder = Vars.yxorder,yyorder = Vars.yyorder,yxterms = Vars.yxterms,reject = INDEF,calctype = 'double',verbose = no,interactive = Vars.interactive,graphics = 'stdgraph',cursor = '')
			if (Vars.wcssource == 'match'):
				Vars.cmin = 1
				Vars.lmin = 1
				Vars.cmax = Vars.ncref
				Vars.lmax = Vars.nlref
			if (Vars.nxblock == INDEF):
				Vars.nxblk = Vars.cmax - Vars.cmin + 3
			else:
				Vars.nxblk = Vars.nxblock
			if (Vars.nyblock == INDEF):
				Vars.nyblk = Vars.lmax - Vars.lmin + 3
			else:
				Vars.nyblk = Vars.nyblock
			iraf.geotran(Vars.image + Vars.trimsec, Vars.outsec, Vars.db,Vars.coord,geometry = 'geometric',xin = INDEF,yin = INDEF,xshift = INDEF,yshift = INDEF,xout = INDEF,yout = INDEF,xmag = INDEF,ymag = INDEF,xrotation = INDEF,yrotation = INDEF,xmin = Vars.cmin,xmax = Vars.cmax,ymin = Vars.lmin,ymax = Vars.lmax,xsample = 10.,ysample = 10.,xscale = 1.,yscale = 1.,ncols = INDEF,nlines = INDEF,interpolant = Vars.interpolant,boundary = 'constant',constant = Vars.constant,fluxconserve = Vars.fluxconserve,nxblock = Vars.nxblk,nyblock = Vars.nyblk,verbose = no)
			iraf.wcscopy(Vars.outsec, Vars.wcsref, verbose=no)
			Vars.xmin = 0.
			Vars.ymin = 0.
			Pipe1 = iraf.hselect(Vars.outsec, 'crpix1,crpix2', yes, Stdout=1)
			iraf.scan(locals(), 'Vars.xmin', 'Vars.ymin', Stdin=Pipe1)
			del Pipe1
			Vars.xmin = Vars.xmin - Vars.cmin + 1
			Vars.ymin = Vars.ymin - Vars.lmin + 1
			if (Vars.nimage == 1):
				Vars.crpix1 = Vars.xmin
				Vars.crpix2 = Vars.ymin
			else:
				Vars.crpix1 = float(iraf.maximum(Vars.crpix1, Vars.xmin))
				Vars.crpix2 = float(iraf.maximum(Vars.crpix2, Vars.ymin))
			iraf.hedit(Vars.outsec, 'crpix1', Vars.xmin, add=yes, verify=no,show=no,update=yes)
			iraf.hedit(Vars.outsec, 'crpix2', Vars.ymin, add=yes, verify=no,show=no,update=yes)
			if (Vars.pixmask):
				Pipe1 = iraf.printf('%s%s\n', Vars.pl, Vars.extname, Stdout=1)
				iraf.scan(locals(), 'Vars.plsec', Stdin=Pipe1)
				del Pipe1
				iraf.mscgmask(Vars.image + Vars.trimsec, Vars.pltemp + '.pl','BPM',mval = 10000)
				iraf.geotran(Vars.pltemp, Vars.plsec + '.fits', Vars.db,Vars.coord,geometry = 'geometric',xin = INDEF,yin = INDEF,xshift = INDEF,yshift = INDEF,xout = INDEF,yout = INDEF,xmag = INDEF,ymag = INDEF,xrotation = INDEF,yrotation = INDEF,xmin = Vars.cmin,xmax = Vars.cmax,ymin = Vars.lmin,ymax = Vars.lmax,xsample = 10.,ysample = 10.,interpolant = Vars.minterpolant,boundary = 'constant',constant = 20000.,fluxconserve = no,nxblock = Vars.nxblk,nyblock = Vars.nyblk,verbose = no)
				iraf.imdelete(Vars.pltemp, verify=no)
				iraf.mscpmask(Vars.plsec + '.fits', Vars.plsec + '.pl')
				iraf.imdelete(Vars.plsec + '.fits', verify=no)
				iraf.hedit(Vars.outsec, 'BPM', Vars.plsec + '.pl', add=yes,show=no,verify=no,update=yes)
				iraf.wcscopy(Vars.plsec, Vars.outsec, verbose=no)
				iraf.clPrint(Vars.plsec, StdoutAppend=Vars.pllist)
			else:
				iraf.hedit(Vars.outsec, 'BPM', PYdel=yes, add=no, addonly=no,show=no,verify=no,update=yes)
			iraf.delete(Vars.coord, verify = no)
			iraf.delete(Vars.db, verify = no)
		Vars.fd_ext = ''
		iraf.delete(Vars.extlist, verify = no)
		if (Vars.nimages > 1 and Vars.format == 'image'):
			if (Vars.verbose):
				iraf.printf('Creating image %s ...\n', Vars.out)
			iraf.mscextensions(Vars.outtemp, output = 'file', index = '',extname = '',extver = '',lindex = no,lname = yes,lver = no,ikparams = '',Stdout=Vars.extlist)
			if (Vars.pixmask):
				iraf.combine('@' + Vars.pllist, Vars.pltemp + '.pl',headers = '',bpmasks = Vars.pl,rejmasks = '',nrejmasks = '',expmasks = '',sigmas = '',imcmb = '',ccdtype = '',amps = no,subsets = no,delete = no,combine = 'average',reject = 'none',project = no,outtype = 'real',outlimits = '',offsets = 'wcs',masktype = 'none',maskvalue = '0',blank = 0.,scale = 'none',zero = 'none',weight = 'none',statsec = '',lthreshold = INDEF,hthreshold = 0.99,nlow = 1,nhigh = 1,nkeep = 1,mclip = yes,lsigma = 3.,hsigma = 3.,rdnoise = '0.',gain = '1.',snoise = '0.',sigscale = 0.1,pclip =  - 0.5,grow = 0.,Stdout='dev$null')
				iraf.imdelete(Vars.pltemp, verify=no)
				iraf.combine('@' + Vars.extlist, Vars.out, headers = '',bpmasks = '',rejmasks = '',nrejmasks = '',expmasks = '',sigmas = '',imcmb = '',ccdtype = '',amps = no,subsets = no,delete = no,combine = 'average',reject = 'none',project = no,outtype = 'real',outlimits = '',offsets = 'wcs',masktype = 'badvalue',maskvalue = '2',blank = 0.,scale = 'none',zero = 'none',weight = 'none',statsec = '',lthreshold = INDEF,hthreshold = INDEF,nlow = 1,nhigh = 1,nkeep = 1,mclip = yes,lsigma = 3.,hsigma = 3.,rdnoise = '0.',gain = '1.',snoise = '0.',sigscale = 0.1,pclip =  - 0.5,grow = 0.,Stdout='dev$null')
				iraf.hedit(Vars.out, 'BPM', Vars.pl, add=yes, verify=no,show=no,update=yes)
				iraf.hedit(Vars.pl, 'IMCMB???,PROCID??', add=no, addonly=no,PYdel=yes,update=yes,verify=no,show=no)
			else:
				iraf.combine('@' + Vars.extlist, Vars.out, headers = '',bpmasks = '',rejmasks = '',nrejmasks = '',expmasks = '',sigmas = '',imcmb = '',ccdtype = '',amps = no,subsets = no,delete = no,combine = 'average',reject = 'none',project = no,outtype = 'real',outlimits = '',offsets = 'wcs',masktype = 'none',maskvalue = '2',blank = 0.,scale = 'none',zero = 'none',weight = 'none',statsec = '',lthreshold = INDEF,hthreshold = INDEF,nlow = 1,nhigh = 1,nkeep = 1,mclip = yes,lsigma = 3.,hsigma = 3.,rdnoise = '0.',gain = '1.',snoise = '0.',sigscale = 0.1,pclip =  - 0.5,grow = 0.,Stdout='dev$null')
			Pipe2 = iraf.hselect('@' + Vars.extlist, 'gain', yes, Stdout=1)
			Pipe1 = iraf.average(data_value = 0., Stdin=Pipe2, Stdout=1)
			del Pipe2
			iraf.scan(locals(), 'Vars.rval', Stdin=Pipe1)
			del Pipe1
			iraf.hedit(Vars.out, 'gain', Vars.rval, add=yes, PYdel=no,update=yes,verify=no,show=no)
			Pipe2 = iraf.hselect('@' + Vars.extlist, 'rdnoise', yes, Stdout=1)
			Pipe1 = iraf.average(data_value = 0., Stdin=Pipe2, Stdout=1)
			del Pipe2
			iraf.scan(locals(), 'Vars.rval', Stdin=Pipe1)
			del Pipe1
			iraf.hedit(Vars.out, 'rdnoise', Vars.rval, add=yes, PYdel=no,update=yes,verify=no,show=no)
			iraf.hedit(Vars.out, 'IMCMB???,PROCID??', add=no, addonly=no,PYdel=yes,update=yes,verify=no,show=no)
			iraf.hedit(Vars.out,'NEXTEND,DETSEC,CCDSEC,AMPSEC,IMAGEID,DATASEC,TRIMSEC,BIASSEC',add=no,addonly=no,PYdel=yes,update=yes,verify=no,show=no)
			iraf.imdelete(Vars.outtemp, verify=no)
			if (iraf.access(Vars.pllist)):
				iraf.imdelete('@' + Vars.pllist, verify=no)
				iraf.delete(Vars.pllist, verify=no)
			iraf.delete(Vars.extlist, verify = no)
		elif (Vars.nimages > 1):
			iraf.imrename(Vars.outtemp, Vars.out, verbose=no)
			iraf.mscextensions(Vars.out, output = 'file', index = '',extname = '',extver = '',lindex = no,lname = yes,lver = no,ikparams = '',Stdout=Vars.extlist)
			Vars.fd_ext = Vars.extlist
			while (iraf.fscan(locals(), 'Vars.fd_ext', 'Vars.image') != EOF):
				Pipe1 = iraf.hselect(Vars.image, 'naxis1,naxis2,crpix1,crpix2',yes,Stdout=1)
				iraf.scan(locals(), 'Vars.nc', 'Vars.nl', 'Vars.xmin','Vars.ymin',Stdin=Pipe1)
				del Pipe1
				Vars.cmin = int(iraf.nint(Vars.crpix1 - Vars.xmin + 1))
				Vars.lmin = int(iraf.nint(Vars.crpix2 - Vars.ymin + 1))
				Vars.cmax = Vars.nc + Vars.cmin - 1
				Vars.lmax = Vars.nl + Vars.lmin - 1
				Pipe1 = iraf.printf('[%d:%d,%d:%d]\n', Vars.cmin, Vars.cmax,Vars.lmin,Vars.lmax,Stdout=1)
				iraf.scan(locals(), 'Vars.str', Stdin=Pipe1)
				del Pipe1
				iraf.hedit(Vars.image, 'DETSEC', Vars.str, add=yes, verify=no,show=no,update=yes)
				iraf.hedit(Vars.image, 'DTM1_1', 1., add=yes, verify=no,show=no,update=yes)
				iraf.hedit(Vars.image, 'DTM2_2', 1., add=yes, verify=no,show=no,update=yes)
				Vars.cmin = Vars.cmin - 1
				Vars.lmin = Vars.lmin - 1
				iraf.hedit(Vars.image, 'DTV1', Vars.cmin, add=yes, verify=no,show=no,update=yes)
				iraf.hedit(Vars.image, 'DTV2', Vars.lmin, add=yes, verify=no,show=no,update=yes)
				iraf.hedit(Vars.image,'CCDSUM,CCDSEC,AMPSEC,ATM1_1,ATM2_2,ATV1,ATV2',PYdel=yes,add=no,addonly=no,verify=no,show=no,update=yes)
			Vars.fd_ext = ''
			iraf.delete(Vars.extlist, verify=no)
		else:
			iraf.imrename(Vars.outsec, Vars.out, verbose=no)
		if (iraf.access(Vars.pllist)):
			iraf.delete(Vars.pllist, verify=no)
	Vars.fd_in = ''
	iraf.delete(Vars.inlists, verify = no)
	if (Vars.wcssource != 'match' and iraf.imaccess(Vars.wcsref)):
		iraf.imdelete(Vars.wcsref, verify=no)
Esempio n. 10
0
def get_align_to_subaru(sci='M0416_Ks_c1_mp_avg.fits',
                        wht='M0416_Ks_c1_mp_exp.fits',
                        field='',
                        clean=True,
                        toler=3,
                        verbose=False,
                        fitgeometry='shift',
                        shift_max=20,
                        rms_max=1.1,
                        rot_max=2,
                        rot_only=True,
                        THRESH=2,
                        align_data=None):
    """
    Align HAWK-I images to the FF Subaru astrometric reference catalogs
    """

    #sci='M0416_Ks_c1_mp_avg.fits'; wht='M0416_Ks_c1_mp_exp.fits'

    ### Make object catalog
    se = threedhst.sex.SExtractor()
    se.aXeParams()
    se.copyConvFile()
    se.overwrite = True
    se.options['CHECKIMAGE_TYPE'] = 'NONE'
    if wht is None:
        se.options['WEIGHT_TYPE'] = 'NONE'
    else:
        se.options['WEIGHT_TYPE'] = 'MAP_WEIGHT'
        se.options['WEIGHT_IMAGE'] = wht

    se.options['FILTER'] = 'Y'

    se.options['DETECT_THRESH'] = '%d' % (THRESH)
    se.options['ANALYSIS_THRESH'] = '%d' % (THRESH)
    se.options['MAG_ZEROPOINT'] = '26.0'

    #### Run SExtractor on direct and alignment images
    ## direct image
    se.options['CATALOG_NAME'] = 'direct.cat'
    status = se.sextractImage(sci)
    threedhst.sex.sexcatRegions('direct.cat', 'direct.reg', format=2)

    directCat = threedhst.sex.mySexCat('direct.cat')

    #### Get the X/Y coords of the reference catalog
    #head = pyfits.getheader(sci, 0)
    #wcs = pywcs.WCS(head)
    if 'M0416' in sci:
        ra_list, dec_list, mag = np.loadtxt(
            os.getenv('HAWKI') +
            '/FrontierFields/HST/hlsp_frontier_subaru_suprimecam_macs0416-astrom_R_v1_cat.txt',
            unpack=True)
        if ('c4' in sci):
            ra_list, dec_list, mag = np.loadtxt(
                os.getenv('HAWKI') +
                '/FrontierFields/HST/M0416/macs0416_f814w_radec.cat',
                unpack=True)
    #
    if 'M0717' in sci:
        ra_list, dec_list, mag = np.loadtxt('subaru.radec', unpack=True)

    if ('M1149' in sci) | (field == 'M1149'):
        ra_list, dec_list, mag = np.loadtxt(
            '/Users/brammer/Research/VLT/HAWKI/MACS1149/hlsp_frontier_subaru_suprimecam_macs1149-astrom_R_v1_cat.txt',
            unpack=True)

    if 'A2744' in sci:
        ra_list, dec_list, mag = np.loadtxt(
            os.getenv('HAWKI') +
            '/FrontierFields/HST/hlsp_frontier_subaru_suprimecam_abell2744-astrom_i_v1_cat.txt',
            unpack=True)
        if ('c1' in sci) | ('c4' in sci):
            ra_list, dec_list, mag = np.loadtxt(
                os.getenv('HAWKI') +
                '/FrontierFields/HST/abell2744_f814w_radec.cat',
                unpack=True)

    if align_data is not None:
        ra_list, dec_list, mag = align_data

    im = pyfits.open(sci)
    print sci

    sh = im[0].shape
    head = im[0].header
    head['CUNIT1'] = 'deg'
    head['CUNIT2'] = 'deg'
    wcs = pywcs.WCS(head)

    x_image, y_image = wcs.wcs_sky2pix(ra_list, dec_list, 1)

    try:
        x_image, y_image = wcs.wcs_sky2pix(ra_list, dec_list, 1)
    except:
        x_image, y_image = wcs.wcs_world2pix(ra_list, dec_list, 1)

    ok = (x_image > 0) & (y_image > 0) & (x_image < sh[1]) & (y_image < sh[1])

    x_image, y_image = x_image[ok], y_image[ok]

    fpr = open('align.reg', 'w')
    fpr.write('image\n')
    for i in range(ok.sum()):
        fpr.write('circle(%.6f, %.6f,0.3") # color=magenta\n' %
                  (x_image[i], y_image[i]))
    fpr.close()

    # x_image, y_image = [], []
    #
    # for ra, dec in zip(ra_list, dec_list):
    #     x, y = wcs.wcs_sky2pix([[ra, dec]], 1)[0]
    #     if (x > 0) & (y > 0) & (x < sh[1]) & (y < sh[1]):
    #         x_image.append(x)
    #         y_image.append(y)

    alignCat = catIO.EmptyCat()
    alignCat['X_IMAGE'] = np.array(x_image)
    alignCat['Y_IMAGE'] = np.array(y_image)

    xshift = 0
    yshift = 0
    rot = 0
    scale = 1.

    xrms = 2
    yrms = 2

    NITER = 5
    IT = 0
    while (IT < NITER):
        IT = IT + 1

        #### Get x,y coordinates of detected objects
        ## direct image
        fp = open('direct.xy', 'w')
        for i in range(len(directCat.X_IMAGE)):
            fp.write('%s  %s\n' % (directCat.X_IMAGE[i], directCat.Y_IMAGE[i]))
        fp.close()

        ## alignment image
        fp = open('align.xy', 'w')
        for i in range(len(alignCat.X_IMAGE)):
            fp.write('%s  %s\n' % (np.float(alignCat.X_IMAGE[i]) + xshift,
                                   np.float(alignCat.Y_IMAGE[i]) + yshift))
        fp.close()

        iraf.flpr()
        iraf.flpr()
        iraf.flpr()
        #### iraf.xyxymatch to find matches between the two catalogs
        pow = toler * 1.
        try:
            os.remove('align.match')
        except:
            pass
        status1 = iraf.xyxymatch(input="direct.xy",
                                 reference="align.xy",
                                 output="align.match",
                                 tolerance=2**pow,
                                 separation=0,
                                 verbose=iraf.yes,
                                 Stdout=1)

        nmatch = 0
        while status1[-1].startswith('0') | (nmatch < 10) | (float(
                status1[-3].split()[1]) > 40):
            pow += 1
            os.remove('align.match')
            status1 = iraf.xyxymatch(input="direct.xy",
                                     reference="align.xy",
                                     output="align.match",
                                     tolerance=2**pow,
                                     separation=0,
                                     verbose=iraf.yes,
                                     Stdout=1)
            #
            nmatch = 0
            for line in open('align.match').xreadlines():
                nmatch += 1

        if verbose:
            for line in status1:
                print line

        #### Compute shifts with iraf.geomap
        iraf.flpr()
        iraf.flpr()
        iraf.flpr()
        try:
            os.remove("align.map")
        except:
            pass

        status2 = iraf.geomap(input="align.match",
                              database="align.map",
                              fitgeometry=fitgeometry,
                              interactive=iraf.no,
                              xmin=iraf.INDEF,
                              xmax=iraf.INDEF,
                              ymin=iraf.INDEF,
                              ymax=iraf.INDEF,
                              maxiter=10,
                              reject=2.0,
                              Stdout=1)
        if verbose:
            for line in status2:
                print line

        #fp = open(root+'.iraf.log','a')
        #fp.writelines(status1)
        #fp.writelines(status2)
        #fp.close()

        #### Parse geomap.output
        fp = open("align.map", "r")
        for line in fp.readlines():
            spl = line.split()
            if spl[0].startswith('xshift'):
                xshift += float(spl[1])
            if spl[0].startswith('yshift'):
                yshift += float(spl[1])
            if spl[0].startswith('xrotation'):
                rot = float(spl[1])
            if spl[0].startswith('xmag'):
                scale = float(spl[1])
            if spl[0].startswith('xrms'):
                xrms = float(spl[1])
            if spl[0].startswith('yrms'):
                yrms = float(spl[1])

        fp.close()

        #os.system('wc align.match')
        print 'Shift iteration #%d, xshift=%f, yshift=%f, rot=%f, scl=%f (rms: %5.2f,%5.2f)' % (
            IT, xshift, yshift, rot, scale, xrms, yrms)

    os.system(
        'cat align.match | grep -v "\#" | grep [0-9] | awk \'{print "circle(", $1, ",", $2, ",4) # color=green"}\' > d.reg'
    )
    os.system(
        'cat align.match | grep -v "\#" | grep [0-9] | awk \'{print "circle(", $3, ",", $4, ",4) # color=magenta"}\' > a.reg'
    )

    shutil.copy('align.map', sci.replace('.fits', '.align.map'))
    shutil.copy('align.match', sci.replace('.fits', '.align.match'))

    #### Cleanup
    if clean:
        rmfiles = [
            'align.cat', 'align.map', 'align.match', 'align.reg', 'align.xy',
            'direct.cat', 'direct.reg', 'direct.xy'
        ]

        for file in rmfiles:
            try:
                os.remove(file)
            except:
                pass

    fp = open(sci.replace('.fits', '.align.info'), 'w')
    fp.write('# image xshift yshift rot scale xrms yrms\n')
    fp.write('%s %.3f %.3f %.4f %.4f %.3f %.3f\n' %
             (sci, xshift, yshift, rot, scale, xrms, yrms))

    if (np.abs(xshift) > shift_max) | (np.abs(yshift) > shift_max) | (
            xrms > rms_max) | (yrms > rms_max):
        print 'Shifts out of allowed range.  Run again with increased shift_max to accept.'
        #return xshift, yshift, rot, scale, xrms, yrms
        ## Add a small shift that should come out easily with another
        ## shift iteration
        xshift, yshift, rot, scale, xrms, yrms = 2, 2, 0, 1.0, -99, -99

    for file in [sci, wht]:
        if ('r' in fitgeometry) & rot_only:
            xshift, yshift = 0, 0

        #apply_offsets(file, [[xshift, yshift, rot, scale]])
        from drizzlepac import updatehdr
        updatehdr.updatewcs_with_shift(file,
                                       sci,
                                       wcsname='DRZWCS',
                                       rot=rot,
                                       scale=scale,
                                       xsh=xshift,
                                       ysh=yshift,
                                       fit=None,
                                       xrms=xrms,
                                       yrms=yrms,
                                       verbose=False,
                                       force=True,
                                       sciext=0)

    if '_dr' in sci:
        im = pyfits.open(sci)
        h = im[0].header
        for i in range(h['NDRIZIM']):
            flt_str = h['D%03dDATA' % (i + 1)]
            if 'sci,2' in flt_str:
                continue
            #
            flt_im = flt_str.split('[')[0]
            ext = int(flt_str.split('[')[1][:-1].split(',')[1])
            updatehdr.updatewcs_with_shift(flt_im,
                                           sci,
                                           wcsname='GTWEAK',
                                           rot=rot,
                                           scale=scale,
                                           xsh=xshift,
                                           ysh=yshift,
                                           fit=None,
                                           xrms=xrms,
                                           yrms=yrms,
                                           verbose=False,
                                           force=True,
                                           sciext='SCI')

        # im = pyfits.open(file, mode='update')
        # wcs = pywcs.WCS(im[0].header)
        # wcs.rotateCD(-rot)
        # wcs.wcs.cd /= scale
        # #
        # im[0].header['CRPIX1'] += xshift
        # im[0].header['CRPIX2'] += yshift
        # #
        # for i in [0,1]:
        #     for j in [0,1]:
        #         im[0].header['CD%d_%d' %(i+1, j+1)] = wcs.wcs.cd[i,j]
        # #
        # im.flush()

    return xshift, yshift, rot, scale, xrms, yrms
Esempio n. 11
0
def spitzer_sub(new,
                newunc,
                newcov,
                ref,
                refunc,
                refcov,
                out,
                stamps=None,
                tmass=None):

    # Remove nan from mosaics
    x = pyfits.open(new)
    y = np.nan_to_num(x[0].data)
    x[0].data = y
    new2 = "n%s" % new
    x.writeto(new2)

    # Find stars in new image
    os.system("$REDUCTION/runsex.pl %s 5.0 -weight %s" % (new2, newcov))

    # Find stars in reference
    os.system("$REDUCTION/runsex.pl %s 5.0 -weight %s" % (ref, refcov))

    # Create file for geotran
    stars = Starlist("%s.stars" % new2)
    refstars = Starlist("%s.stars" % ref)
    refstars.pix2wcs(ref)
    refstars.wcs2pix(new2)
    a, b = stars.match(refstars, maxnum=1000)

    # Create file for geotran
    b.pix2wcs(new2)
    b.wcs2pix(ref)
    refroot = ref.split(".")[0]
    outf = open("%s.match" % refroot, "w")
    for i in range(len(a)):
        outf.write("%10.3f%10.3f%10.3f%10.3f\n" %
                   (a[i].xval, a[i].yval, b[i].xval, b[i].yval))
    outf.close()

    # Geomap and geotran
    [naxis1, naxis2] = get_head(new, ["NAXIS1", "NAXIS2"])
    iraf.geomap("%s.match" % refroot,
                "%s.db" % refroot,
                1,
                naxis1,
                1,
                naxis2,
                fitgeometry="rotate",
                interactive=no)
    iraf.geotran(ref, "t%s" % ref, "%s.db" % refroot, "%s.match" % refroot)
    iraf.geotran(refunc, "t%s" % refunc, "%s.db" % refroot,
                 "%s.match" % refroot)

    # Get stars for PSF matching
    if stamps != None:

        psfstars = Starlist(stamps)
        psfstars.pix2wcs(ref)
        psfstars.wcs2pix(new2)
        outf = open("stamps.lis", "w")
        for star in psfstars:
            outf.write("%10.3f%10.3f\n" % (star.xval, star.yval))
        outf.close()

    # Appropriate parameters
    iraf.iterstat(ref)
    update_head(ref, ["MEDSKY", "SKYSIG"],
                [iraf.iterstat.median, iraf.iterstat.sigma])
    [refskybkg, refskysig] = get_head(ref, ["MEDSKY", "SKYSIG"])
    tl = refskybkg - 10 * refskysig
    tu = 30000.0
    iraf.iterstat(new)
    update_head(new, ["MEDSKY", "SKYSIG"],
                [iraf.iterstat.median, iraf.iterstat.sigma])
    [newskybkg, newskysig] = get_head(new, ["MEDSKY", "SKYSIG"])
    il = newskybkg - 10 * newskysig
    iu = 30000.0

    # Run hotpants
    hpcmd = "hotpants -inim %s -tmplim t%s -outim %s -tni t%s -ini %s -nsx 3 -nsy 3 -savexy %s.xy -ko 0 -bgo 0 -oni u%s -n t -tl %.2f -tu %.2f -il %.2f -iu %.2f -r 7.5 -rss 18.0" % (
        new2, ref, out, refunc, newunc, new2, out, tl, tu, il, iu)
    if stamps != None:
        hpcmd += " -ssf stamps.lis -afssc 0"
    os.system(hpcmd)
    #iraf.imarith(new2, "-", "t%s" % ref, out)

    # Create appropriate weight image
    #iraf.imexpr("sqrt(a**2 + b**2)", "u%s" % out, a=newunc, b=refunc)
    iraf.imarith(1, "/", "u%s" % out, "temp1.fits")
    iraf.imarith("temp1.fits", "/", "u%s" % out, "w%s" % out)
    os.remove("temp1.fits")

    # Pick up candidates
    os.system("$REDUCTION/runsex.pl %s 2.0 -weight w%s" % (out, out))
    stars = Starlist("%s.stars" % out)
    cands = []
    [nax1, nax2] = get_head(ref, ["NAXIS1", "NAXIS2"])
    stars.pix2wcs(new)
    stars.wcs2pix(ref)
    refu = pyfits.open(refunc)
    for star in stars:
        if star.xval > EDGETOL and star.xval < (
                nax1 - EDGETOL) and star.yval > EDGETOL and star.yval < (
                    nax2 - EDGETOL) and refu[0].data[
                        star.yval, star.
                        xval] != 0 and star.fwhmw > 0.5 and star.fwhmw < 10.0:
            cands.append(star)
    scands = Starlist(stars=cands)

    # Filter out bright stars
    if tmass == None:
        scands.write("cands.reg")
    else:
        stmass = Starlist(tmass)
        stmass.wcs2pix(ref)
        s2cands = scands.nomatch(stmass)
        s2cands.write("cands.reg")

    # More comprehensive list
    os.system("$REDUCTION/runsex.pl %s 1.5" % out)
    stars = Starlist("%s.stars" % out)
    cands = []
    [nax1, nax2] = get_head(ref, ["NAXIS1", "NAXIS2"])
    stars.pix2wcs(new)
    stars.wcs2pix(ref)
    refu = pyfits.open(refunc)
    for star in stars:
        if star.xval > EDGETOL and star.xval < (
                nax1 - EDGETOL) and star.yval > EDGETOL and star.yval < (
                    nax2 - EDGETOL) and refu[0].data[
                        star.yval, star.
                        xval] != 0 and star.fwhmw > 0.5 and star.fwhmw < 10.0:
            cands.append(star)
    scands = Starlist(stars=cands)
    scands.write("cands_all.reg")

    return
Esempio n. 12
0
def irafalign(filepath,
              uknstarlist,
              refstarlist,
              shape,
              alifilepath=None,
              outdir="alipy_out",
              makepng=False,
              hdu=0,
              verbose=True):
    """
    Uses iraf geomap and gregister to align the image. Three steps :

     * Write the matched source lists into an input file for geomap
     * Compute a geomap transform from these stars.
     * Run gregister

    :param filepath: FITS file to be aligned
    :type filepath: string

    :param uknstarlist: A list of stars from the "unknown" image to be aligned,
                        that matches to ...
    :type uknstarlist: list of Star objects
    :param refstarlist: ... the list of corresponding stars in the reference
                        image.
    :type refstarlist: list of Star objects

    :param shape: Output shape (width, height)
    :type shape: tuple

    :param alifilepath: where to save the aligned image. If None, I put it in
                        the default directory.
    :type alifilepath: string

    :param makepng: If True I make a png of the aligned image as well.
    :type makepng: boolean

    :param hdu: The hdu of the fits file that you want me to use.
                0 is primary. If multihdu, 1 is usually science.
    """

    try:
        from pyraf import iraf
    except ImportError:
        print("Couldn't import pyraf !")
        return

    assert len(uknstarlist) == len(refstarlist)
    if len(uknstarlist) < 2:
        if verbose:
            print("Not enough stars for using geomap !")
        return

    basename = os.path.splitext(os.path.basename(filepath))[0]
    geomapinpath = basename + ".geomapin"
    geodatabasepath = basename + ".geodatabase"
    if os.path.isfile(geomapinpath):
        os.remove(geomapinpath)
    if os.path.isfile(geodatabasepath):
        os.remove(geodatabasepath)

    # Step 1, we write the geomap input.
    table = []
    for (uknstar, refstar) in zip(uknstarlist, refstarlist):
        table.append([refstar.x, refstar.y, uknstar.x, uknstar.y])
    geomap = open(geomapinpath, "w")
    writer = csv.writer(geomap, delimiter="\t")
    writer.writerows(table)
    geomap.close()

    # Step 2, geomap

    iraf.unlearn(iraf.geomap)
    iraf.geomap.fitgeom = "rscale"
    # You can change this to:
    # shift, xyscale, rotate, rscale
    iraf.geomap.function = "polynomial"  # Surface type
    iraf.geomap.maxiter = 3  # Maximum number of rejection iterations
    iraf.geomap.reject = 3.0  # Rejection limit in sigma units

    # other options you could specify :
    # (xxorder=    2) Order of x fit in x
    # (xyorder=    2) Order of x fit in y
    # (xxterms= half) X fit cross terms type
    # (yxorder=    2) Order of y fit in x
    # (yyorder=    2) Order of y fit in y
    # (yxterms= half) Y fit cross terms type
    # (calctyp= real) Computation type

    iraf.geomap.transfo = "broccoli"  # keep it
    iraf.geomap.interac = "no"  # keep it
    iraf.geomap.verbose = "yes"  # keep it
    # iraf.geomap.results = "bla.summary" # The optional results summary files

    geomapblabla = iraf.geomap(input=geomapinpath,
                               database=geodatabasepath,
                               xmin=1,
                               xmax=shape[0],
                               ymin=1,
                               ymax=shape[1],
                               Stdout=1)

    # We read this output ...
    for line in geomapblabla:
        if "X and Y scale:" in line:
            mapscale = line.split()[4:6]
        if "Xin and Yin fit rms:" in line:
            maprmss = line.split()[-2:]
        if "X and Y axis rotation:" in line:
            mapangles = line.split()[-4:-2]
        if "X and Y shift:" in line:
            mapshifts = line.split()[-4:-2]
            # not used?

    geomaprms = math.sqrt(
        float(maprmss[0]) * float(maprmss[0]) +
        float(maprmss[1]) * float(maprmss[1]))
    geomapangle = float(mapangles[0])  # % 360.0
    geomapscale = 1.0 / float(mapscale[0])

    if mapscale[0] != mapscale[1]:
        raise RuntimeError("Error reading geomap scale")
    if verbose:
        print(("IRAF geomap : Rotation %+11.6f [deg], "
               "scale %8.6f, RMS %.3f [pixel]") %
              (geomapangle, geomapscale, geomaprms))
    # Step 3

    if alifilepath is None:
        alifilepath = os.path.join(outdir, basename + "_gregister.fits")
    else:
        outdir = os.path.split(alifilepath)[0]
    if not os.path.isdir(outdir):
        os.makedirs(outdir)
    if os.path.isfile(alifilepath):
        os.remove(alifilepath)

    iraf.unlearn(iraf.gregister)
    iraf.gregister.geometry = "geometric"  # linear, distortion, geometric
    iraf.gregister.interpo = "spline3"  # linear, spline3
    iraf.gregister.boundary = "constant"  # padding with zero
    iraf.gregister.constant = 0.0
    iraf.gregister.fluxconserve = "yes"

    if verbose:
        print("IRAF gregister ...")

    regblabla = iraf.gregister(input='%s[%s]' % (filepath, hdu),
                               output=alifilepath,
                               database=geodatabasepath,
                               transform="broccoli",
                               Stdout=1)
    # not used?

    if verbose:
        print("IRAF gregister done !")

    if os.path.isfile(geomapinpath):
        os.remove(geomapinpath)
    if os.path.isfile(geodatabasepath):
        os.remove(geodatabasepath)

    if makepng:
        try:
            import f2n
        except ImportError:
            print("Couldn't import f2n -- install it !")
            return
        myimage = f2n.fromfits(alifilepath, verbose=False)
        myimage.setzscale("auto", "auto")
        myimage.makepilimage("log", negative=False)
        myimage.writetitle(os.path.basename(alifilepath))
        if not os.path.isdir(outdir):
            os.makedirs(outdir)
        myimage.tonet(
            os.path.join(outdir,
                         os.path.basename(alifilepath) + ".png"))
Esempio n. 13
0
def get_align_to_subaru(sci='M0416_Ks_c1_mp_avg.fits', wht='M0416_Ks_c1_mp_exp.fits', field='', clean=True, toler=3, verbose=False, fitgeometry='shift', shift_max=20, rms_max=1.1, rot_max=2, rot_only=True, THRESH=2, align_data=None):
    """
    Align HAWK-I images to the FF Subaru astrometric reference catalogs
    """
    
    #sci='M0416_Ks_c1_mp_avg.fits'; wht='M0416_Ks_c1_mp_exp.fits'
    
    ### Make object catalog
    se = threedhst.sex.SExtractor()
    se.aXeParams()
    se.copyConvFile()
    se.overwrite = True
    se.options['CHECKIMAGE_TYPE'] = 'NONE'
    if wht is None:
        se.options['WEIGHT_TYPE']     = 'NONE'
    else:
        se.options['WEIGHT_TYPE']     = 'MAP_WEIGHT'
        se.options['WEIGHT_IMAGE']    = wht
    
    se.options['FILTER']    = 'Y'
               
    se.options['DETECT_THRESH']    = '%d' %(THRESH)
    se.options['ANALYSIS_THRESH']  = '%d' %(THRESH)
    se.options['MAG_ZEROPOINT'] = '26.0'

    #### Run SExtractor on direct and alignment images
    ## direct image
    se.options['CATALOG_NAME']    = 'direct.cat'
    status = se.sextractImage(sci)
    threedhst.sex.sexcatRegions('direct.cat', 'direct.reg', format=2)
    
    directCat = threedhst.sex.mySexCat('direct.cat')
    
    #### Get the X/Y coords of the reference catalog    
    #head = pyfits.getheader(sci, 0)
    #wcs = pywcs.WCS(head)
    if 'M0416' in sci:
        ra_list, dec_list, mag = np.loadtxt(os.getenv('HAWKI')+'/FrontierFields/HST/hlsp_frontier_subaru_suprimecam_macs0416-astrom_R_v1_cat.txt', unpack=True)
        if ('c4' in sci):
            ra_list, dec_list, mag = np.loadtxt(os.getenv('HAWKI')+'/FrontierFields/HST/M0416/macs0416_f814w_radec.cat', unpack=True)
    #
    if 'M0717' in sci:
        ra_list, dec_list, mag = np.loadtxt('subaru.radec', unpack=True)

    if ('M1149' in sci) | (field == 'M1149'):
        ra_list, dec_list, mag = np.loadtxt('/Users/brammer/Research/VLT/HAWKI/MACS1149/hlsp_frontier_subaru_suprimecam_macs1149-astrom_R_v1_cat.txt', unpack=True)
            
    if 'A2744' in sci:
        ra_list, dec_list, mag = np.loadtxt(os.getenv('HAWKI')+'/FrontierFields/HST/hlsp_frontier_subaru_suprimecam_abell2744-astrom_i_v1_cat.txt', unpack=True)
        if ('c1' in sci) | ('c4' in sci):
            ra_list, dec_list, mag = np.loadtxt(os.getenv('HAWKI')+'/FrontierFields/HST/abell2744_f814w_radec.cat', unpack=True)
    
    if align_data is not None:
        ra_list, dec_list, mag = align_data
            
    im = pyfits.open(sci)
    print sci
    
    sh = im[0].shape
    head = im[0].header
    head['CUNIT1'] = 'deg'; head['CUNIT2'] = 'deg'
    wcs = pywcs.WCS(head)

    x_image, y_image = wcs.wcs_sky2pix(ra_list, dec_list, 1)
    
    try:
        x_image, y_image = wcs.wcs_sky2pix(ra_list, dec_list, 1)
    except:
        x_image, y_image = wcs.wcs_world2pix(ra_list, dec_list, 1)
    
    ok = (x_image > 0) & (y_image > 0) & (x_image < sh[1]) & (y_image < sh[1])

    x_image, y_image = x_image[ok], y_image[ok]
    
    fpr = open('align.reg','w')
    fpr.write('image\n')
    for i in range(ok.sum()): fpr.write('circle(%.6f, %.6f,0.3") # color=magenta\n' %(x_image[i], y_image[i]))
    fpr.close()
    
    # x_image, y_image = [], []
    # 
    # for ra, dec in zip(ra_list, dec_list):
    #     x, y = wcs.wcs_sky2pix([[ra, dec]], 1)[0]
    #     if (x > 0) & (y > 0) & (x < sh[1]) & (y < sh[1]):
    #         x_image.append(x)
    #         y_image.append(y)
    
    alignCat = catIO.EmptyCat()
    alignCat['X_IMAGE'] = np.array(x_image)
    alignCat['Y_IMAGE'] = np.array(y_image)
    
    xshift = 0
    yshift = 0
    rot = 0
    scale = 1.
    
    xrms = 2
    yrms = 2
    
    NITER = 5
    IT = 0
    while (IT < NITER):
        IT = IT+1
        
        #### Get x,y coordinates of detected objects
        ## direct image
        fp = open('direct.xy','w')
        for i in range(len(directCat.X_IMAGE)):
            fp.write('%s  %s\n' %(directCat.X_IMAGE[i],directCat.Y_IMAGE[i]))
        fp.close()

        ## alignment image
        fp = open('align.xy','w')
        for i in range(len(alignCat.X_IMAGE)):
            fp.write('%s  %s\n' %(np.float(alignCat.X_IMAGE[i])+xshift,
                       np.float(alignCat.Y_IMAGE[i])+yshift))
        fp.close()

        iraf.flpr()
        iraf.flpr()
        iraf.flpr()
        #### iraf.xyxymatch to find matches between the two catalogs
        pow = toler*1.
        try:
            os.remove('align.match')
        except:
            pass
        status1 = iraf.xyxymatch(input="direct.xy", reference="align.xy",
                       output="align.match",
                       tolerance=2**pow, separation=0, verbose=iraf.yes, Stdout=1)
        
        nmatch = 0
        while status1[-1].startswith('0') | (nmatch < 10) | (float(status1[-3].split()[1]) > 40):
            pow+=1
            os.remove('align.match')
            status1 = iraf.xyxymatch(input="direct.xy", reference="align.xy",
                           output="align.match",
                           tolerance=2**pow, separation=0, verbose=iraf.yes, Stdout=1)
            #
            nmatch = 0
            for line in open('align.match').xreadlines(  ): nmatch += 1
            
        if verbose:
            for line in status1:
                print line
        
                
        #### Compute shifts with iraf.geomap
        iraf.flpr()
        iraf.flpr()
        iraf.flpr()
        try:
            os.remove("align.map")
        except:
            pass
            
        status2 = iraf.geomap(input="align.match", database="align.map",
                    fitgeometry=fitgeometry, interactive=iraf.no, 
                    xmin=iraf.INDEF, xmax=iraf.INDEF, ymin=iraf.INDEF, ymax=iraf.INDEF,
                    maxiter = 10, reject = 2.0, Stdout=1)
        if verbose:
            for line in status2:
                print line
        
        #fp = open(root+'.iraf.log','a')
        #fp.writelines(status1)
        #fp.writelines(status2)
        #fp.close()
                
        #### Parse geomap.output 
        fp = open("align.map","r")
        for line in fp.readlines():
            spl = line.split()
            if spl[0].startswith('xshift'):
                xshift += float(spl[1])    
            if spl[0].startswith('yshift'):
                yshift += float(spl[1])    
            if spl[0].startswith('xrotation'):
                rot = float(spl[1])    
            if spl[0].startswith('xmag'):
                scale = float(spl[1])    
            if spl[0].startswith('xrms'):
                xrms = float(spl[1])    
            if spl[0].startswith('yrms'):
                yrms = float(spl[1])    
            
        fp.close()
        
        #os.system('wc align.match')
        print 'Shift iteration #%d, xshift=%f, yshift=%f, rot=%f, scl=%f (rms: %5.2f,%5.2f)' %(IT, xshift, yshift, rot, scale, xrms, yrms)
    
    os.system('cat align.match | grep -v "\#" | grep [0-9] | awk \'{print "circle(", $1, ",", $2, ",4) # color=green"}\' > d.reg')
    os.system('cat align.match | grep -v "\#" | grep [0-9] | awk \'{print "circle(", $3, ",", $4, ",4) # color=magenta"}\' > a.reg')
    
    shutil.copy('align.map', sci.replace('.fits', '.align.map'))
    shutil.copy('align.match', sci.replace('.fits', '.align.match'))
    
    #### Cleanup
    if clean:
        rmfiles = ['align.cat', 'align.map','align.match','align.reg','align.xy', 'direct.cat','direct.reg','direct.xy']
        
        for file in rmfiles:
            try:
                os.remove(file)
            except:
                pass
    
    fp = open(sci.replace('.fits', '.align.info'), 'w')
    fp.write('# image xshift yshift rot scale xrms yrms\n')
    fp.write('%s %.3f %.3f %.4f %.4f %.3f %.3f\n' %(sci, xshift, yshift, rot, scale, xrms, yrms))
    
    if (np.abs(xshift) > shift_max) | (np.abs(yshift) > shift_max) | (xrms > rms_max) | (yrms > rms_max):
        print 'Shifts out of allowed range.  Run again with increased shift_max to accept.'
        #return xshift, yshift, rot, scale, xrms, yrms
        ## Add a small shift that should come out easily with another 
        ## shift iteration
        xshift, yshift, rot, scale, xrms, yrms = 2,2,0,1.0,-99,-99
        
    for file in [sci, wht]:
        if ('r' in fitgeometry) & rot_only:
            xshift, yshift = 0, 0
            
        #apply_offsets(file, [[xshift, yshift, rot, scale]])
        from drizzlepac import updatehdr
        updatehdr.updatewcs_with_shift(file, sci, wcsname='DRZWCS',
                        rot=rot,scale=scale,
                        xsh=xshift, ysh=yshift,
                        fit=None,
                        xrms=xrms, yrms = yrms,
                        verbose=False, force=True, sciext=0)
        
    if '_dr' in sci:
        im = pyfits.open(sci)
        h = im[0].header
        for i in range(h['NDRIZIM']):
            flt_str = h['D%03dDATA' %(i+1)]
            if 'sci,2' in flt_str:
                continue
            #
            flt_im = flt_str.split('[')[0]
            ext = int(flt_str.split('[')[1][:-1].split(',')[1])
            updatehdr.updatewcs_with_shift(flt_im, sci, wcsname='GTWEAK', rot=rot, scale=scale, xsh=xshift, ysh=yshift,
                            fit=None, xrms=xrms, yrms = yrms, verbose=False, force=True, sciext='SCI')
                
        # im = pyfits.open(file, mode='update')
        # wcs = pywcs.WCS(im[0].header)
        # wcs.rotateCD(-rot)
        # wcs.wcs.cd /= scale
        # #
        # im[0].header['CRPIX1'] += xshift
        # im[0].header['CRPIX2'] += yshift
        # #
        # for i in [0,1]:
        #     for j in [0,1]:
        #         im[0].header['CD%d_%d' %(i+1, j+1)] = wcs.wcs.cd[i,j]
        # #        
        # im.flush()
    
    return xshift, yshift, rot, scale, xrms, yrms
Esempio n. 14
0
def mscimage(input=None,
             output=None,
             format='image',
             pixmask=no,
             verbose=')_.verbose',
             wcssource='image',
             reference='',
             ra=INDEF,
             dec=INDEF,
             scale=INDEF,
             rotation=INDEF,
             blank=0.0,
             interpolant='poly5',
             minterpolant='linear',
             boundary='reflect',
             constant=0.0,
             fluxconserve=no,
             ntrim=8,
             nxblock=INDEF,
             nyblock=INDEF,
             interactive=no,
             nx=10,
             ny=20,
             fitgeometry='general',
             xxorder=4,
             xyorder=4,
             xxterms='half',
             yxorder=4,
             yyorder=4,
             yxterms='half',
             fd_in='',
             fd_ext='',
             fd_coord='',
             mode='ql',
             DOLLARnargs=0,
             taskObj=None):

    Vars = IrafParList('mscimage')
    Vars.addParam(
        makeIrafPar(input,
                    datatype='string',
                    name='input',
                    mode='a',
                    prompt='List of input mosaic exposures'))
    Vars.addParam(
        makeIrafPar(output,
                    datatype='string',
                    name='output',
                    mode='a',
                    prompt='List of output images'))
    Vars.addParam(
        makeIrafPar(format,
                    datatype='string',
                    name='format',
                    enum=['image', 'mef'],
                    mode='h',
                    prompt='Output format (image|mef)'))
    Vars.addParam(
        makeIrafPar(pixmask,
                    datatype='bool',
                    name='pixmask',
                    mode='h',
                    prompt='Create pixel mask?'))
    Vars.addParam(
        makeIrafPar(verbose,
                    datatype='bool',
                    name='verbose',
                    mode='h',
                    prompt='Verbose output?\n\n# Output WCS parameters'))
    Vars.addParam(
        makeIrafPar(wcssource,
                    datatype='string',
                    name='wcssource',
                    enum=['image', 'parameters', 'match'],
                    mode='h',
                    prompt='Output WCS source (image|parameters|match)'))
    Vars.addParam(
        makeIrafPar(reference,
                    datatype='file',
                    name='reference',
                    mode='h',
                    prompt='Reference image'))
    Vars.addParam(
        makeIrafPar(ra,
                    datatype='real',
                    name='ra',
                    max=24.0,
                    min=0.0,
                    mode='h',
                    prompt='RA of tangent point (hours)'))
    Vars.addParam(
        makeIrafPar(dec,
                    datatype='real',
                    name='dec',
                    max=90.0,
                    min=-90.0,
                    mode='h',
                    prompt='DEC of tangent point (degrees)'))
    Vars.addParam(
        makeIrafPar(scale,
                    datatype='real',
                    name='scale',
                    mode='h',
                    prompt='Scale (arcsec/pixel)'))
    Vars.addParam(
        makeIrafPar(
            rotation,
            datatype='real',
            name='rotation',
            max=360.0,
            min=-360.0,
            mode='h',
            prompt=
            'Rotation of DEC from N to E (degrees)\n\n# Resampling parmeters'))
    Vars.addParam(
        makeIrafPar(blank,
                    datatype='real',
                    name='blank',
                    mode='h',
                    prompt='Blank value'))
    Vars.addParam(
        makeIrafPar(interpolant,
                    datatype='string',
                    name='interpolant',
                    mode='h',
                    prompt='Interpolant for data'))
    Vars.addParam(
        makeIrafPar(minterpolant,
                    datatype='string',
                    name='minterpolant',
                    mode='h',
                    prompt='Interpolant for mask'))
    Vars.addParam(
        makeIrafPar(boundary,
                    datatype='string',
                    name='boundary',
                    enum=['nearest', 'constant', 'reflect', 'wrap'],
                    mode='h',
                    prompt='Boundary extension'))
    Vars.addParam(
        makeIrafPar(constant,
                    datatype='real',
                    name='constant',
                    mode='h',
                    prompt='Constant boundary extension value'))
    Vars.addParam(
        makeIrafPar(fluxconserve,
                    datatype='bool',
                    name='fluxconserve',
                    mode='h',
                    prompt='Preserve flux per unit area?'))
    Vars.addParam(
        makeIrafPar(ntrim,
                    datatype='int',
                    name='ntrim',
                    min=0,
                    mode='h',
                    prompt='Edge trim in each extension'))
    Vars.addParam(
        makeIrafPar(nxblock,
                    datatype='int',
                    name='nxblock',
                    mode='h',
                    prompt='X dimension of working block size in pixels'))
    Vars.addParam(
        makeIrafPar(
            nyblock,
            datatype='int',
            name='nyblock',
            mode='h',
            prompt=
            'Y dimension of working block size in pixels\n\n# Geometric mapping parameters'
        ))
    Vars.addParam(
        makeIrafPar(interactive,
                    datatype='bool',
                    name='interactive',
                    mode='h',
                    prompt='Fit mapping interactively?'))
    Vars.addParam(
        makeIrafPar(nx,
                    datatype='int',
                    name='nx',
                    mode='h',
                    prompt='Number of x grid points'))
    Vars.addParam(
        makeIrafPar(ny,
                    datatype='int',
                    name='ny',
                    mode='h',
                    prompt='Number of y grid points'))
    Vars.addParam(
        makeIrafPar(fitgeometry,
                    datatype='string',
                    name='fitgeometry',
                    enum=[
                        'shift', 'xyscale', 'rotate', 'rscale', 'rxyscale',
                        'general'
                    ],
                    mode='h',
                    prompt='Fitting geometry'))
    Vars.addParam(
        makeIrafPar(xxorder,
                    datatype='int',
                    name='xxorder',
                    min=2,
                    mode='h',
                    prompt='Order of x fit in x'))
    Vars.addParam(
        makeIrafPar(xyorder,
                    datatype='int',
                    name='xyorder',
                    min=2,
                    mode='h',
                    prompt='Order of x fit in y'))
    Vars.addParam(
        makeIrafPar(xxterms,
                    datatype='string',
                    name='xxterms',
                    mode='h',
                    prompt='X fit cross terms type'))
    Vars.addParam(
        makeIrafPar(yxorder,
                    datatype='int',
                    name='yxorder',
                    min=2,
                    mode='h',
                    prompt='Order of y fit in x'))
    Vars.addParam(
        makeIrafPar(yyorder,
                    datatype='int',
                    name='yyorder',
                    min=2,
                    mode='h',
                    prompt='Order of y fit in y'))
    Vars.addParam(
        makeIrafPar(yxterms,
                    datatype='string',
                    name='yxterms',
                    mode='h',
                    prompt='Y fit cross terms type\n\n'))
    Vars.addParam(
        makeIrafPar(fd_in,
                    datatype='struct',
                    name='fd_in',
                    list_flag=1,
                    mode='h',
                    prompt=''))
    Vars.addParam(
        makeIrafPar(fd_ext,
                    datatype='struct',
                    name='fd_ext',
                    list_flag=1,
                    mode='h',
                    prompt=''))
    Vars.addParam(
        makeIrafPar(fd_coord,
                    datatype='struct',
                    name='fd_coord',
                    list_flag=1,
                    mode='h',
                    prompt=''))
    Vars.addParam(
        makeIrafPar(mode, datatype='string', name='mode', mode='h', prompt=''))
    Vars.addParam(
        makeIrafPar(DOLLARnargs, datatype='int', name='$nargs', mode='h'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='in', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='out', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='ref', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='pl', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='image', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='trimsec', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='outsec', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='plsec', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='inlists', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='extlist', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='pllist', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='coord', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='db', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='wcsref', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='outtemp', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='file', name='pltemp', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='int', name='nc', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='int', name='nl', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='int', name='ncref', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='int', name='nlref', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='int', name='cmin', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='int', name='cmax', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='int', name='lmin', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='int', name='lmax', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='int', name='nimage', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='int', name='nimages', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='int', name='nxblk', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='int', name='nyblk', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='real', name='x', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='real', name='y', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='real', name='rval', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='real', name='xmin', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='real', name='xmax', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='real', name='ymin', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='real', name='ymax', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='real', name='crpix1', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='real', name='crpix2', mode='u'))
    Vars.addParam(
        makeIrafPar(None, datatype='string', name='extname', mode='u'))
    Vars.addParam(makeIrafPar(None, datatype='string', name='str', mode='u'))

    iraf.cache('mscextensions', 'mscgmask')
    Vars.inlists = iraf.mktemp('tmp$iraf')
    Vars.extlist = iraf.mktemp('tmp$iraf')
    Vars.pllist = iraf.mktemp('tmp$iraf')
    Vars.coord = iraf.mktemp('tmp$iraf')
    Vars.db = iraf.mktemp('tmp$iraf')
    Vars.outtemp = iraf.mktemp('tmp')
    Vars.wcsref = iraf.mktemp('tmp')
    Vars.pltemp = iraf.mktemp('tmp')
    iraf.joinlists(Vars.input,
                   Vars.output,
                   output=Vars.inlists,
                   delim=' ',
                   short=yes,
                   type='image')
    Vars.fd_in = Vars.inlists
    while (iraf.fscan(locals(), 'Vars.fd_in', 'Vars.PYin', 'Vars.out') != EOF):
        if (iraf.imaccess(Vars.out)):
            iraf.printf('Warning: Image already exists (%s)\n', Vars.out)
            continue
        if (Vars.pixmask):
            Vars.pl = Vars.out
            Vars.nc = iraf.strlen(Vars.pl)
            if (Vars.nc > 5
                    and iraf.substr(Vars.pl, Vars.nc - 4, Vars.nc) == '.fits'):
                Vars.pl = iraf.substr(Vars.pl, 1, Vars.nc - 5)
            elif (Vars.nc > 4
                  and iraf.substr(Vars.out, Vars.nc - 3, Vars.nc) == '.imh'):
                Vars.pl = iraf.substr(Vars.pl, 1, Vars.nc - 4)
            Vars.pl = Vars.pl + '_bpm'
            if (Vars.format == 'image' and iraf.imaccess(Vars.pl)):
                iraf.printf('Warning: Mask already exists (%s)\n', Vars.pl)
                continue
        else:
            Vars.pl = ''
        iraf.mscextensions(Vars.PYin,
                           output='file',
                           index='0-',
                           extname='',
                           extver='',
                           lindex=no,
                           lname=yes,
                           lver=no,
                           ikparams='',
                           Stdout=Vars.extlist)
        Vars.nimages = int(iraf.mscextensions.nimages)
        Vars.nimage = 0
        if (Vars.nimages < 1):
            iraf.printf("WARNING: No input image data found in `%s'.\
", Vars.PYin)
            iraf.delete(Vars.extlist, verify=no)
            continue
        if (not iraf.imaccess(Vars.wcsref)):
            Vars.ref = Vars.reference
            if (Vars.wcssource == 'match'):
                Vars.wcsref = Vars.ref
            else:
                iraf.mscwtemplate('@' + Vars.extlist,
                                  Vars.wcsref,
                                  wcssource=Vars.wcssource,
                                  reference=Vars.ref,
                                  ra=Vars.ra,
                                  dec=Vars.dec,
                                  scale=Vars.scale,
                                  rotation=Vars.rotation,
                                  projection='',
                                  verbose=Vars.verbose)
        Vars.fd_ext = Vars.extlist
        while (iraf.fscan(locals(), 'Vars.fd_ext', 'Vars.image') != EOF):
            Vars.nimage = Vars.nimage + 1
            if (Vars.nimages > 1):
                Pipe1 = iraf.hselect(Vars.image, 'extname', yes, Stdout=1)
                iraf.scan(locals(), 'Vars.extname', Stdin=Pipe1)
                del Pipe1
                if (iraf.nscan() == 0):
                    Vars.extname = 'im' + str(Vars.nimage)
                Pipe1 = iraf.printf('%s[%s,append]\n',
                                    Vars.outtemp,
                                    Vars.extname,
                                    Stdout=1)
                iraf.scan(locals(), 'Vars.outsec', Stdin=Pipe1)
                del Pipe1
                Pipe1 = iraf.printf('%s%s\n', Vars.pl, Vars.extname, Stdout=1)
                iraf.scan(locals(), 'Vars.plsec', Stdin=Pipe1)
                del Pipe1
            else:
                Vars.extname = ''
                Vars.outsec = Vars.outtemp
                Vars.plsec = Vars.pl
            if (Vars.pixmask and iraf.imaccess(Vars.plsec)):
                iraf.delete(Vars.coord, verify=no)
                iraf.delete(Vars.db, verify=no)
                iraf.printf('Warning: Mask already exists (%s)\n', Vars.plsec)
                continue
            if (Vars.verbose):
                iraf.printf('Resampling %s ...\n', Vars.image)
            Pipe1 = iraf.hselect(Vars.image, 'naxis1,naxis2', yes, Stdout=1)
            iraf.scan(locals(), 'Vars.nc', 'Vars.nl', Stdin=Pipe1)
            del Pipe1
            Vars.cmin = 1 + Vars.ntrim
            Vars.cmax = Vars.nc - Vars.ntrim
            Vars.lmin = 1 + Vars.ntrim
            Vars.lmax = Vars.nl - Vars.ntrim
            Pipe1 = iraf.printf('[%d:%d,%d:%d]\n',
                                Vars.cmin,
                                Vars.cmax,
                                Vars.lmin,
                                Vars.lmax,
                                Stdout=1)
            iraf.scan(locals(), 'Vars.trimsec', Stdin=Pipe1)
            del Pipe1
            if (Vars.wcssource == 'match'):
                Pipe1 = iraf.hselect(Vars.ref, 'naxis1,naxis2', yes, Stdout=1)
                iraf.scan(locals(), 'Vars.ncref', 'Vars.nlref', Stdin=Pipe1)
                del Pipe1
                Vars.xmin = (Vars.ncref - 1.) / (Vars.nx - 1.)
                Vars.ymin = (Vars.nlref - 1.) / (Vars.ny - 1.)
                Vars.ymax = 1
                while (Vars.ymax <= Vars.nlref + 1):
                    Vars.xmax = 1
                    while (Vars.xmax <= Vars.ncref + 1):
                        iraf.clPrint(Vars.xmax,
                                     Vars.ymax,
                                     Vars.xmax,
                                     Vars.ymax,
                                     StdoutAppend=Vars.coord)
                        Vars.xmax = Vars.xmax + Vars.xmin
                    Vars.ymax = Vars.ymax + Vars.ymin
                iraf.mscctran(Vars.coord,
                              Vars.db,
                              Vars.ref,
                              'logical',
                              'world',
                              columns='3 4',
                              units='',
                              formats='%.4H %.3h',
                              min_sigdigit=10,
                              verbose=no)
                iraf.delete(Vars.coord, verify=no)
                iraf.wcsctran(Vars.db,
                              Vars.coord,
                              Vars.image + Vars.trimsec,
                              inwcs='world',
                              outwcs='logical',
                              columns='3 4',
                              units='hours native',
                              formats='',
                              min_sigdigit=10,
                              verbose=no)
                iraf.delete(Vars.db, verify=no)
            else:
                Vars.nc = Vars.cmax - Vars.cmin + 1
                Vars.nl = Vars.lmax - Vars.lmin + 1
                Vars.xmin = (Vars.nc - 1.) / (Vars.nx - 1.)
                Vars.ymin = (Vars.nl - 1.) / (Vars.ny - 1.)
                Vars.ymax = 1
                while (Vars.ymax <= Vars.nl + 1):
                    Vars.xmax = 1
                    while (Vars.xmax <= Vars.nc + 1):
                        iraf.clPrint(Vars.xmax,
                                     Vars.ymax,
                                     Vars.xmax,
                                     Vars.ymax,
                                     StdoutAppend=Vars.coord)
                        Vars.xmax = Vars.xmax + Vars.xmin
                    Vars.ymax = Vars.ymax + Vars.ymin
                iraf.mscctran(Vars.coord,
                              Vars.db,
                              Vars.image + Vars.trimsec,
                              'logical',
                              'world',
                              columns='1 2',
                              units='',
                              formats='%.4H %.3h',
                              min_sigdigit=10,
                              verbose=no)
                iraf.delete(Vars.coord, verify=no)
                iraf.wcsctran(Vars.db,
                              Vars.coord,
                              Vars.wcsref,
                              inwcs='world',
                              outwcs='logical',
                              columns='1 2',
                              units='hours native',
                              formats='',
                              min_sigdigit=10,
                              verbose=no)
                iraf.delete(Vars.db, verify=no)
            Vars.xmax = 0.
            Vars.xmin = 1.
            Vars.ymax = 0.
            Vars.ymin = 1.
            Vars.fd_coord = Vars.coord
            while (iraf.fscan(locals(), 'Vars.fd_coord', 'Vars.x', 'Vars.y') !=
                   EOF):
                if (iraf.nscan() < 2):
                    continue
                if (Vars.xmax < Vars.xmin):
                    Vars.xmin = Vars.x
                    Vars.xmax = Vars.x
                    Vars.ymin = Vars.y
                    Vars.ymax = Vars.y
                else:
                    Vars.xmin = float(iraf.minimum(Vars.x, Vars.xmin))
                    Vars.xmax = float(iraf.maximum(Vars.x, Vars.xmax))
                    Vars.ymin = float(iraf.minimum(Vars.y, Vars.ymin))
                    Vars.ymax = float(iraf.maximum(Vars.y, Vars.ymax))
            Vars.fd_coord = ''
            if (Vars.xmax <= Vars.xmin or Vars.ymax <= Vars.ymin):
                iraf.error(1, 'No overlap for matching reference')
            Vars.cmin = int(iraf.nint(Vars.xmin - 1.5))
            Vars.cmax = int(iraf.nint(Vars.xmax + 1.5))
            Vars.lmin = int(iraf.nint(Vars.ymin - 1.5))
            Vars.lmax = int(iraf.nint(Vars.ymax + 1.5))
            iraf.geomap(Vars.coord,
                        Vars.db,
                        Vars.cmin,
                        Vars.cmax,
                        Vars.lmin,
                        Vars.lmax,
                        transforms='',
                        results='',
                        fitgeometry=Vars.fitgeometry,
                        function='chebyshev',
                        xxorder=Vars.xxorder,
                        xyorder=Vars.xyorder,
                        xxterms=Vars.xxterms,
                        yxorder=Vars.yxorder,
                        yyorder=Vars.yyorder,
                        yxterms=Vars.yxterms,
                        reject=INDEF,
                        calctype='double',
                        verbose=no,
                        interactive=Vars.interactive,
                        graphics='stdgraph',
                        cursor='')
            if (Vars.wcssource == 'match'):
                Vars.cmin = 1
                Vars.lmin = 1
                Vars.cmax = Vars.ncref
                Vars.lmax = Vars.nlref
            if (Vars.nxblock == INDEF):
                Vars.nxblk = Vars.cmax - Vars.cmin + 3
            else:
                Vars.nxblk = Vars.nxblock
            if (Vars.nyblock == INDEF):
                Vars.nyblk = Vars.lmax - Vars.lmin + 3
            else:
                Vars.nyblk = Vars.nyblock
            iraf.geotran(Vars.image + Vars.trimsec,
                         Vars.outsec,
                         Vars.db,
                         Vars.coord,
                         geometry='geometric',
                         xin=INDEF,
                         yin=INDEF,
                         xshift=INDEF,
                         yshift=INDEF,
                         xout=INDEF,
                         yout=INDEF,
                         xmag=INDEF,
                         ymag=INDEF,
                         xrotation=INDEF,
                         yrotation=INDEF,
                         xmin=Vars.cmin,
                         xmax=Vars.cmax,
                         ymin=Vars.lmin,
                         ymax=Vars.lmax,
                         xsample=10.,
                         ysample=10.,
                         xscale=1.,
                         yscale=1.,
                         ncols=INDEF,
                         nlines=INDEF,
                         interpolant=Vars.interpolant,
                         boundary='constant',
                         constant=Vars.constant,
                         fluxconserve=Vars.fluxconserve,
                         nxblock=Vars.nxblk,
                         nyblock=Vars.nyblk,
                         verbose=no)
            iraf.wcscopy(Vars.outsec, Vars.wcsref, verbose=no)
            Vars.xmin = 0.
            Vars.ymin = 0.
            Pipe1 = iraf.hselect(Vars.outsec, 'crpix1,crpix2', yes, Stdout=1)
            iraf.scan(locals(), 'Vars.xmin', 'Vars.ymin', Stdin=Pipe1)
            del Pipe1
            Vars.xmin = Vars.xmin - Vars.cmin + 1
            Vars.ymin = Vars.ymin - Vars.lmin + 1
            if (Vars.nimage == 1):
                Vars.crpix1 = Vars.xmin
                Vars.crpix2 = Vars.ymin
            else:
                Vars.crpix1 = float(iraf.maximum(Vars.crpix1, Vars.xmin))
                Vars.crpix2 = float(iraf.maximum(Vars.crpix2, Vars.ymin))
            iraf.hedit(Vars.outsec,
                       'crpix1',
                       Vars.xmin,
                       add=yes,
                       verify=no,
                       show=no,
                       update=yes)
            iraf.hedit(Vars.outsec,
                       'crpix2',
                       Vars.ymin,
                       add=yes,
                       verify=no,
                       show=no,
                       update=yes)
            if (Vars.pixmask):
                Pipe1 = iraf.printf('%s%s\n', Vars.pl, Vars.extname, Stdout=1)
                iraf.scan(locals(), 'Vars.plsec', Stdin=Pipe1)
                del Pipe1
                iraf.mscgmask(Vars.image + Vars.trimsec,
                              Vars.pltemp + '.pl',
                              'BPM',
                              mval=10000)
                iraf.geotran(Vars.pltemp,
                             Vars.plsec + '.fits',
                             Vars.db,
                             Vars.coord,
                             geometry='geometric',
                             xin=INDEF,
                             yin=INDEF,
                             xshift=INDEF,
                             yshift=INDEF,
                             xout=INDEF,
                             yout=INDEF,
                             xmag=INDEF,
                             ymag=INDEF,
                             xrotation=INDEF,
                             yrotation=INDEF,
                             xmin=Vars.cmin,
                             xmax=Vars.cmax,
                             ymin=Vars.lmin,
                             ymax=Vars.lmax,
                             xsample=10.,
                             ysample=10.,
                             interpolant=Vars.minterpolant,
                             boundary='constant',
                             constant=20000.,
                             fluxconserve=no,
                             nxblock=Vars.nxblk,
                             nyblock=Vars.nyblk,
                             verbose=no)
                iraf.imdelete(Vars.pltemp, verify=no)
                iraf.mscpmask(Vars.plsec + '.fits', Vars.plsec + '.pl')
                iraf.imdelete(Vars.plsec + '.fits', verify=no)
                iraf.hedit(Vars.outsec,
                           'BPM',
                           Vars.plsec + '.pl',
                           add=yes,
                           show=no,
                           verify=no,
                           update=yes)
                iraf.wcscopy(Vars.plsec, Vars.outsec, verbose=no)
                iraf.clPrint(Vars.plsec, StdoutAppend=Vars.pllist)
            else:
                iraf.hedit(Vars.outsec,
                           'BPM',
                           PYdel=yes,
                           add=no,
                           addonly=no,
                           show=no,
                           verify=no,
                           update=yes)
            iraf.delete(Vars.coord, verify=no)
            iraf.delete(Vars.db, verify=no)
        Vars.fd_ext = ''
        iraf.delete(Vars.extlist, verify=no)
        if (Vars.nimages > 1 and Vars.format == 'image'):
            if (Vars.verbose):
                iraf.printf('Creating image %s ...\n', Vars.out)
            iraf.mscextensions(Vars.outtemp,
                               output='file',
                               index='',
                               extname='',
                               extver='',
                               lindex=no,
                               lname=yes,
                               lver=no,
                               ikparams='',
                               Stdout=Vars.extlist)
            if (Vars.pixmask):
                iraf.combine('@' + Vars.pllist,
                             Vars.pltemp + '.pl',
                             headers='',
                             bpmasks=Vars.pl,
                             rejmasks='',
                             nrejmasks='',
                             expmasks='',
                             sigmas='',
                             imcmb='',
                             ccdtype='',
                             amps=no,
                             subsets=no,
                             delete=no,
                             combine='average',
                             reject='none',
                             project=no,
                             outtype='real',
                             outlimits='',
                             offsets='wcs',
                             masktype='none',
                             maskvalue='0',
                             blank=0.,
                             scale='none',
                             zero='none',
                             weight='none',
                             statsec='',
                             lthreshold=INDEF,
                             hthreshold=0.99,
                             nlow=1,
                             nhigh=1,
                             nkeep=1,
                             mclip=yes,
                             lsigma=3.,
                             hsigma=3.,
                             rdnoise='0.',
                             gain='1.',
                             snoise='0.',
                             sigscale=0.1,
                             pclip=-0.5,
                             grow=0.,
                             Stdout='dev$null')
                iraf.imdelete(Vars.pltemp, verify=no)
                iraf.combine('@' + Vars.extlist,
                             Vars.out,
                             headers='',
                             bpmasks='',
                             rejmasks='',
                             nrejmasks='',
                             expmasks='',
                             sigmas='',
                             imcmb='',
                             ccdtype='',
                             amps=no,
                             subsets=no,
                             delete=no,
                             combine='average',
                             reject='none',
                             project=no,
                             outtype='real',
                             outlimits='',
                             offsets='wcs',
                             masktype='badvalue',
                             maskvalue='2',
                             blank=0.,
                             scale='none',
                             zero='none',
                             weight='none',
                             statsec='',
                             lthreshold=INDEF,
                             hthreshold=INDEF,
                             nlow=1,
                             nhigh=1,
                             nkeep=1,
                             mclip=yes,
                             lsigma=3.,
                             hsigma=3.,
                             rdnoise='0.',
                             gain='1.',
                             snoise='0.',
                             sigscale=0.1,
                             pclip=-0.5,
                             grow=0.,
                             Stdout='dev$null')
                iraf.hedit(Vars.out,
                           'BPM',
                           Vars.pl,
                           add=yes,
                           verify=no,
                           show=no,
                           update=yes)
                iraf.hedit(Vars.pl,
                           'IMCMB???,PROCID??',
                           add=no,
                           addonly=no,
                           PYdel=yes,
                           update=yes,
                           verify=no,
                           show=no)
            else:
                iraf.combine('@' + Vars.extlist,
                             Vars.out,
                             headers='',
                             bpmasks='',
                             rejmasks='',
                             nrejmasks='',
                             expmasks='',
                             sigmas='',
                             imcmb='',
                             ccdtype='',
                             amps=no,
                             subsets=no,
                             delete=no,
                             combine='average',
                             reject='none',
                             project=no,
                             outtype='real',
                             outlimits='',
                             offsets='wcs',
                             masktype='none',
                             maskvalue='2',
                             blank=0.,
                             scale='none',
                             zero='none',
                             weight='none',
                             statsec='',
                             lthreshold=INDEF,
                             hthreshold=INDEF,
                             nlow=1,
                             nhigh=1,
                             nkeep=1,
                             mclip=yes,
                             lsigma=3.,
                             hsigma=3.,
                             rdnoise='0.',
                             gain='1.',
                             snoise='0.',
                             sigscale=0.1,
                             pclip=-0.5,
                             grow=0.,
                             Stdout='dev$null')
            Pipe2 = iraf.hselect('@' + Vars.extlist, 'gain', yes, Stdout=1)
            Pipe1 = iraf.average(data_value=0., Stdin=Pipe2, Stdout=1)
            del Pipe2
            iraf.scan(locals(), 'Vars.rval', Stdin=Pipe1)
            del Pipe1
            iraf.hedit(Vars.out,
                       'gain',
                       Vars.rval,
                       add=yes,
                       PYdel=no,
                       update=yes,
                       verify=no,
                       show=no)
            Pipe2 = iraf.hselect('@' + Vars.extlist, 'rdnoise', yes, Stdout=1)
            Pipe1 = iraf.average(data_value=0., Stdin=Pipe2, Stdout=1)
            del Pipe2
            iraf.scan(locals(), 'Vars.rval', Stdin=Pipe1)
            del Pipe1
            iraf.hedit(Vars.out,
                       'rdnoise',
                       Vars.rval,
                       add=yes,
                       PYdel=no,
                       update=yes,
                       verify=no,
                       show=no)
            iraf.hedit(Vars.out,
                       'IMCMB???,PROCID??',
                       add=no,
                       addonly=no,
                       PYdel=yes,
                       update=yes,
                       verify=no,
                       show=no)
            iraf.hedit(
                Vars.out,
                'NEXTEND,DETSEC,CCDSEC,AMPSEC,IMAGEID,DATASEC,TRIMSEC,BIASSEC',
                add=no,
                addonly=no,
                PYdel=yes,
                update=yes,
                verify=no,
                show=no)
            iraf.imdelete(Vars.outtemp, verify=no)
            if (iraf.access(Vars.pllist)):
                iraf.imdelete('@' + Vars.pllist, verify=no)
                iraf.delete(Vars.pllist, verify=no)
            iraf.delete(Vars.extlist, verify=no)
        elif (Vars.nimages > 1):
            iraf.imrename(Vars.outtemp, Vars.out, verbose=no)
            iraf.mscextensions(Vars.out,
                               output='file',
                               index='',
                               extname='',
                               extver='',
                               lindex=no,
                               lname=yes,
                               lver=no,
                               ikparams='',
                               Stdout=Vars.extlist)
            Vars.fd_ext = Vars.extlist
            while (iraf.fscan(locals(), 'Vars.fd_ext', 'Vars.image') != EOF):
                Pipe1 = iraf.hselect(Vars.image,
                                     'naxis1,naxis2,crpix1,crpix2',
                                     yes,
                                     Stdout=1)
                iraf.scan(locals(),
                          'Vars.nc',
                          'Vars.nl',
                          'Vars.xmin',
                          'Vars.ymin',
                          Stdin=Pipe1)
                del Pipe1
                Vars.cmin = int(iraf.nint(Vars.crpix1 - Vars.xmin + 1))
                Vars.lmin = int(iraf.nint(Vars.crpix2 - Vars.ymin + 1))
                Vars.cmax = Vars.nc + Vars.cmin - 1
                Vars.lmax = Vars.nl + Vars.lmin - 1
                Pipe1 = iraf.printf('[%d:%d,%d:%d]\n',
                                    Vars.cmin,
                                    Vars.cmax,
                                    Vars.lmin,
                                    Vars.lmax,
                                    Stdout=1)
                iraf.scan(locals(), 'Vars.str', Stdin=Pipe1)
                del Pipe1
                iraf.hedit(Vars.image,
                           'DETSEC',
                           Vars.str,
                           add=yes,
                           verify=no,
                           show=no,
                           update=yes)
                iraf.hedit(Vars.image,
                           'DTM1_1',
                           1.,
                           add=yes,
                           verify=no,
                           show=no,
                           update=yes)
                iraf.hedit(Vars.image,
                           'DTM2_2',
                           1.,
                           add=yes,
                           verify=no,
                           show=no,
                           update=yes)
                Vars.cmin = Vars.cmin - 1
                Vars.lmin = Vars.lmin - 1
                iraf.hedit(Vars.image,
                           'DTV1',
                           Vars.cmin,
                           add=yes,
                           verify=no,
                           show=no,
                           update=yes)
                iraf.hedit(Vars.image,
                           'DTV2',
                           Vars.lmin,
                           add=yes,
                           verify=no,
                           show=no,
                           update=yes)
                iraf.hedit(Vars.image,
                           'CCDSUM,CCDSEC,AMPSEC,ATM1_1,ATM2_2,ATV1,ATV2',
                           PYdel=yes,
                           add=no,
                           addonly=no,
                           verify=no,
                           show=no,
                           update=yes)
            Vars.fd_ext = ''
            iraf.delete(Vars.extlist, verify=no)
        else:
            iraf.imrename(Vars.outsec, Vars.out, verbose=no)
        if (iraf.access(Vars.pllist)):
            iraf.delete(Vars.pllist, verify=no)
    Vars.fd_in = ''
    iraf.delete(Vars.inlists, verify=no)
    if (Vars.wcssource != 'match' and iraf.imaccess(Vars.wcsref)):
        iraf.imdelete(Vars.wcsref, verify=no)
Esempio n. 15
0
def psfphot(inlist, ra, dec, reffilt, interact, fwhm, readnoise, gain, 
            threshold,refimage=None,starfile=None,maxnpsf=5, 
            clobber=globclob,verbose=globver,skykey='SKYBKG',
            filtkey='FILTER',pixtol=3.0):


    """ 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()

    # Parse inputs
    infiles=iraffiles(inlist)

    # Which file is reffilt?  call it refimage
    if refimage==None:
        for image in infiles:
            if check_head(image, filtkey):
                try:
                    imgfilt = get_head(image, filtkey)
                    if imgfilt == reffilt:
                        refimage = image
                        break
                except:
                    pass
            
    if not refimage:
        print "BAD USER!  No image corresponds to the filter: %s" % reffilt
        return
    else:
        refroot='s'+refimage.split('.')[0]

    #first make sure to add back in background of sky
    iraf.iqsubsky(inlist, sub=no, skykey=skykey)

    #put reference image first on list
    infiles.remove(refimage)
    infiles.insert(0,refimage)

    #setup for keywords
    if gain == "!GAIN":
        try: gainval = float(get_head(image, gain))
        except:
            print "Bad header keyword for gain."
    else:
        gainval = float(gain)

    if readnoise == "!READNOISE":
        try: readval = float(get_head(image, readnoise))
        except:
            print "Bad header keyword for readnoise."
    else:
        readval = float(readnoise)

    # Process each file in turn
    for image in infiles:

        # Check that the image is there
        check_exist(image,"r")

        # Grab image root name
        root=image.split('.')[0]

        # Map image to reference image
        if not (image==refimage):
            [nx,ny]=get_head(image,['NAXIS1','NAXIS2'])
            stars=Starlist(get_head(image,'STARFILE'))
            refstars=Starlist(get_head(refimage,'STARFILE'))
            refstars.pix2wcs(refimage)
            refstars.wcs2pix(image)
            match,refmatch=stars.match(refstars,useflags=yes,tol=10.0)
            nstars=len(match)
            if not (nstars>2):
                print 'Could not find star matches between reference and %s' % image
                infiles.remove(image)
                continue
            refmatch.pix2wcs(image)
            refmatch.wcs2pix(refimage)
            matchfile=open('%s.match' % root, 'w')
            for i in range(len(match)):
                matchfile.write('%10.3f%10.3f%10.3f%10.3f\n' % 
                               (refmatch[i].xval,refmatch[i].yval,
                                match[i].xval,match[i].yval))
            matchfile.close()
            check_exist('%s.geodb' % root, 'w', clobber=clobber)
            iraf.geomap('%s.match' % root,'%s.geodb' % root,1.0,nx,1.0,ny,
                        verbose=no,interactive=no)
            check_exist('s%s.fits' % root, 'w', clobber=clobber)
            iraf.geotran(image,'s%s' % root,'%s.geodb' % root,
                         '%s.match' % root,geometry="geometric",
                         boundary="constant",verbose=no)
        else:
            iraf.imcopy(image,'s%s' % root)
        root='s%s' % root
 
        #get sky level and calculate sigma
        #if check_head(image, skykey):
        #    try:
        #        sky=float(get_head(image, skykey))
        #    except:
        #        print "No sky levels in header."

        #sigma= (((sky * gainval) + readval**2)**.5) / gainval        
        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.90*saturate
        iraf.datapars.readnoise=readval
        iraf.datapars.epadu=gainval
        iraf.datapars.filter=filtkey
        iraf.daopars.psfrad=psfmult*fwhm
        iraf.daopars.fitrad=fwhm
        iraf.daopars.function="gauss,moffat15,moffat25,lorentz,penny1"

        #find stars in image unless a starlist is given
        if image==refimage and starfile==None:
            iraf.daophot.daofind(root,'refimage.coo.1',threshold=threshold,verify=no,
                         verbose=verbose)
        elif image==refimage:
            shutil.copy(starfile,'refimage.coo.1')

        #initial photometry
        iraf.daophot.phot(root,'refimage.coo.1','default',aperture=fwhm,verify=no,
                  verbose=verbose)

        #select stars for psf the first time
        refstarsfile = "refimage.pst.1"
        if image == refimage:
            iraf.pstselect(root,'default',refstarsfile,maxnpsf,
                           interactive=yes,verify=no,verbose=verbose)

        #fit the psf
        iraf.psf(root,'default',refstarsfile,'default','default','default',
                 interactive=interact,verify=no,verbose=verbose)

        #identify neighboring/interfering stars to selected stars
        groupingfile = root+".psg.1"
        iraf.nstar(root,groupingfile,'default','default','default',
                   psfrad= psfmultsmall * fwhm,verify=no,verbose=verbose)

        #subtract out neighboring stars from image
        iraf.substar(root,'default',refstarsfile,'default','default',
                     psfrad=psfmultsmall*fwhm,verify=no,verbose=verbose)

        #repeat psf to get better psf model
        #IRAF's interactive version usually crashes
        subtractedimage = root+".sub.1"
        iraf.psf(subtractedimage,root+".nst.1",refstarsfile,'%s.psf.2' % root,
                 '%s.pst.2' % root,'%s.psg.2' % root,interactive=interact,
                 verify=no,verbose=verbose)

        #Need to make sure SN was detected by daofind
        stars=Starlist('%s.mag.1' % root)
        SN=Star(name='SN',radeg=ra,dcdeg=dec,fwhm=2.0,fwhmw=2.0)
        SNlis=Starlist(stars=[SN])
        SNlis.wcs2pix(image)
        if (len(stars.match(SNlis)[0])==0):
            #No match - need to add to daofind file
            print "No match!"
            coofile=open('refimage.coo.1', 'a+')
            coofile.write('%10.3f%10.3f%9.3f%8.3f%13.3f%12.3f%8i\n' % (SNlis[0].xval, SNlis[0].yval,99.999,0.500,0.000,0.000,999))
            coofile.close()    

        #repeat aperture photometry to get good comparisons to standard fields
        iraf.daophot.phot(root,'refimage.coo.1','default',aperture=psfmult*fwhm,
                  verify=no,verbose=verbose)

        # allstar run
        iraf.allstar(root,'default','default','default','default','default',
                     verify=no,verbose=verbose)
Esempio n. 16
0
def align_to_reference(
    ROOT_DIRECT,
    ALIGN_IMAGE,
    fitgeometry="shift",
    clean=True,
    verbose=False,
    ALIGN_EXTENSION=0,
    toler=3,
    skip_swarp=False,
    align_sdss_ds9=False,
    catalog=None,
):
    """
xshift, yshift, rot, scale, xrms, yrms = align_to_reference()
    """
    import os
    import glob
    import shutil

    from pyraf import iraf
    from iraf import stsdas, dither

    import threedhst
    from threedhst import catIO

    no = iraf.no
    yes = iraf.yes
    INDEF = iraf.INDEF

    #### Clean slate
    rmfiles = [
        "SCI.fits",
        "WHT.fits",
        "align.cat",
        "direct.cat" "align.map",
        "align.match",
        "align.reg",
        "align.xy",
        "direct.reg",
        "direct.xy",
        "ds9_align.tsv",
    ]

    for file in rmfiles:
        try:
            os.remove(file)
        except:
            pass

    if catalog is not None:
        align_sdss_ds9 = True

    #### Get only images that overlap from the ALIGN_IMAGE list
    if not align_sdss_ds9:
        align_img_list = find_align_images_that_overlap(
            ROOT_DIRECT + "_drz.fits", ALIGN_IMAGE, ALIGN_EXTENSION=ALIGN_EXTENSION
        )
        if not align_img_list:
            print "threedhst.shifts.align_to_reference: no alignment images overlap."
            return 0, 0

    #### Use swarp to combine the alignment images to the same image
    #### dimensions as the direct mosaic
    if (not skip_swarp) & (not align_sdss_ds9):
        try:
            os.remove(ROOT_DIRECT + "_align.fits")
        except:
            pass
        matchImagePixels(
            input=align_img_list,
            matchImage=ROOT_DIRECT + "_drz.fits",
            output=ROOT_DIRECT + "_align.fits",
            match_extension=1,
            input_extension=ALIGN_EXTENSION,
        )

    #### Run SExtractor on the direct image, with the WHT
    #### extension as a weight image
    se = threedhst.sex.SExtractor()
    se.aXeParams()
    se.copyConvFile()
    se.overwrite = True
    se.options["CHECKIMAGE_TYPE"] = "NONE"
    se.options["WEIGHT_TYPE"] = "MAP_WEIGHT"
    se.options["WEIGHT_IMAGE"] = "WHT.fits"
    se.options["FILTER"] = "Y"
    ## Detect thresholds (default = 1.5)
    THRESH = 10
    if align_sdss_ds9:
        if "Vizier" not in REFERENCE_CATALOG:
            THRESH = 20

    se.options["DETECT_THRESH"] = "%d" % (THRESH)
    se.options["ANALYSIS_THRESH"] = "%d" % (THRESH)
    se.options["MAG_ZEROPOINT"] = str(threedhst.options["MAG_ZEROPOINT"])

    #### Run SExtractor on direct and alignment images
    ## direct image
    se.options["CATALOG_NAME"] = "direct.cat"
    iraf.imcopy(ROOT_DIRECT + "_drz.fits[1]", "SCI.fits")
    iraf.imcopy(ROOT_DIRECT + "_drz.fits[2]", "WHT.fits")
    status = se.sextractImage("SCI.fits")

    ## Read the catalog
    directCat = threedhst.sex.mySexCat("direct.cat")

    if align_sdss_ds9:
        ### Use ds9 SDSS catalog to refine alignment
        import threedhst.dq
        import pywcs
        import threedhst.catIO as catIO

        wcs = pywcs.WCS(pyfits.getheader("SCI.fits", 0))
        # wcs = pywcs.WCS(pyfits.getheader('Q0821+3107-F140W_drz.fits', 1))

        if "Vizier" in REFERENCE_CATALOG:
            #### Use (unstable) astroquery Vizier search
            #### CFHTLS-Deep: 'Vizier.II/317'
            VIZIER_CAT = REFERENCE_CATALOG.split("Vizier.")[1]
            print "Align to Vizier catalog: http://vizier.u-strasbg.fr/viz-bin/VizieR?-source=%s" % (VIZIER_CAT)

            import astroquery

            if astroquery.__version__ < "0.0.dev1078":
                from astroquery import vizier

                query = {}
                query["-source"] = VIZIER_CAT
                # query["-out"] = ["_r", "CFHTLS", "rmag"]
                query["-out"] = ["_RAJ2000", "_DEJ2000"]  ### Just RA/Dec.

                #### Center position and query radius
                r0, d0 = wcs.wcs_pix2sky([[wcs.naxis1 / 2.0, wcs.naxis2 / 2.0]], 1)[0]
                rll, dll = wcs.wcs_pix2sky([[0, 0]], 1)[0]
                corner_radius = (
                    np.sqrt((r0 - rll) ** 2 * np.cos(d0 / 360.0 * 2 * np.pi) ** 2 + (d0 - dll) ** 2) * 60.0 * 1.5
                )
                h = query["-c"] = "%.6f %.6f" % (r0, d0)
                query["-c.rm"] = "%.3f" % (corner_radius)  ### xxx check image size

                #### Run the query
                vt = vizier.vizquery(query)
            else:
                #### Newer astroquery
                from astroquery.vizier import Vizier
                import astropy.coordinates as coord
                import astropy.units as u

                Vizier.ROW_LIMIT = -1

                r0, d0 = wcs.wcs_pix2sky([[wcs.naxis1 / 2.0, wcs.naxis2 / 2.0]], 1)[0]
                rll, dll = wcs.wcs_pix2sky([[0, 0]], 1)[0]
                corner_radius = (
                    np.sqrt((r0 - rll) ** 2 * np.cos(d0 / 360.0 * 2 * np.pi) ** 2 + (d0 - dll) ** 2) * 60.0 * 1.5
                )
                #
                c = coord.ICRSCoordinates(ra=r0, dec=d0, unit=(u.deg, u.deg))
                #### something with astropy.coordinates
                # c.icrs.ra.degree = c.icrs.ra.degrees
                # c.icrs.dec.degree = c.icrs.dec.degrees
                #
                vt = Vizier.query_region(c, width=u.Quantity(corner_radius, u.arcminute), catalog=[VIZIER_CAT])[0]

            #### Make a region file
            ra_list, dec_list = vt["RAJ2000"], vt["DEJ2000"]
            print "Vizier, found %d objects." % (len(ra_list))
            fp = open("%s.vizier.reg" % (ROOT_DIRECT), "w")
            fp.write("# %s, r=%.1f'\nfk5\n" % (VIZIER_CAT, corner_radius))
            for ra, dec in zip(ra_list, dec_list):
                fp.write('circle(%.6f, %.6f, 0.5")\n' % (ra, dec))
            #
            fp.close()
        else:
            #### Use DS9 catalog
            ds9 = threedhst.dq.myDS9()
            ds9.set("file SCI.fits")
            # ds9.set('file Q0821+3107-F140W_drz.fits')
            ds9.set("catalog %s" % (REFERENCE_CATALOG))
            ### Can't find XPA access point for "copy to regions"
            ds9.set("catalog export tsv ds9_align.tsv")
            lines = open("ds9_align.tsv").readlines()
            ra_list, dec_list = [], []
            for line in lines[1:]:
                spl = line.split()
                ra, dec = float(spl[0]), float(spl[1])
                ra_list.append(ra)
                dec_list.append(dec)
            #
            del (ds9)

        x_image, y_image = [], []
        for ra, dec in zip(ra_list, dec_list):
            x, y = wcs.wcs_sky2pix([[ra, dec]], 1)[0]
            x_image.append(x)
            y_image.append(y)

        alignCat = catIO.EmptyCat()
        alignCat["X_IMAGE"] = np.array(x_image)
        alignCat["Y_IMAGE"] = np.array(y_image)

    else:
        ## alignment image
        se.options["CATALOG_NAME"] = "align.cat"
        status = se.sextractImage(ROOT_DIRECT + "_align.fits")
        alignCat = threedhst.sex.mySexCat("align.cat")

    xshift = 0
    yshift = 0
    rot = 0
    scale = 1.0

    xrms = 2
    yrms = 2

    NITER = 5
    IT = 0
    while IT < NITER:
        IT = IT + 1

        #### Get x,y coordinates of detected objects
        ## direct image
        fp = open("direct.xy", "w")
        for i in range(len(directCat.X_IMAGE)):
            fp.write("%s  %s\n" % (directCat.X_IMAGE[i], directCat.Y_IMAGE[i]))
        fp.close()

        ## alignment image
        fp = open("align.xy", "w")
        for i in range(len(alignCat.X_IMAGE)):
            fp.write("%s  %s\n" % (np.float(alignCat.X_IMAGE[i]) + xshift, np.float(alignCat.Y_IMAGE[i]) + yshift))
        fp.close()

        iraf.flpr()
        iraf.flpr()
        iraf.flpr()
        #### iraf.xyxymatch to find matches between the two catalogs
        pow = toler * 1.0
        try:
            os.remove("align.match")
        except:
            pass

        status1 = iraf.xyxymatch(
            input="direct.xy",
            reference="align.xy",
            output="align.match",
            tolerance=2 ** pow,
            separation=0,
            verbose=yes,
            Stdout=1,
        )

        nmatch = 0
        while status1[-1].startswith("0") | (nmatch < 10):
            pow += 1
            os.remove("align.match")
            status1 = iraf.xyxymatch(
                input="direct.xy",
                reference="align.xy",
                output="align.match",
                tolerance=2 ** pow,
                separation=0,
                verbose=yes,
                Stdout=1,
            )
            #
            nmatch = 0
            for line in open("align.match").xreadlines():
                nmatch += 1

        if verbose:
            for line in status1:
                print line

        #### Compute shifts with iraf.geomap
        iraf.flpr()
        iraf.flpr()
        iraf.flpr()
        try:
            os.remove("align.map")
        except:
            pass

        status2 = iraf.geomap(
            input="align.match",
            database="align.map",
            fitgeometry=fitgeometry,
            interactive=no,
            xmin=INDEF,
            xmax=INDEF,
            ymin=INDEF,
            ymax=INDEF,
            maxiter=10,
            reject=2.0,
            Stdout=1,
        )
        if verbose:
            for line in status2:
                print line

        # fp = open(root+'.iraf.log','a')
        # fp.writelines(status1)
        # fp.writelines(status2)
        # fp.close()

        #### Parse geomap.output
        fp = open("align.map", "r")
        for line in fp.readlines():
            spl = line.split()
            if spl[0].startswith("xshift"):
                xshift += float(spl[1])
            if spl[0].startswith("yshift"):
                yshift += float(spl[1])
            if spl[0].startswith("xrotation"):
                rot = float(spl[1])
            if spl[0].startswith("xmag"):
                scale = float(spl[1])
            if spl[0].startswith("xrms"):
                xrms = float(spl[1])
            if spl[0].startswith("yrms"):
                yrms = float(spl[1])

        fp.close()

        # os.system('wc align.match')
        print "Shift iteration #%d, xshift=%f, yshift=%f, rot=%f, scl=%f (rms: %5.2f,%5.2f)" % (
            IT,
            xshift,
            yshift,
            rot,
            scale,
            xrms,
            yrms,
        )

    im = pyfits.open("SCI.fits")

    shutil.copy("align.map", ROOT_DIRECT + "_align.map")
    shutil.copy("align.match", ROOT_DIRECT + "_align.match")

    #### Cleanup
    if clean:
        rmfiles = [
            "SCI.fits",
            "WHT.fits",
            "align.cat",
            "align.map",
            "align.match",
            "align.reg",
            "align.xy",
            "direct.cat",
            "direct.reg",
            "direct.xy",
            "drz_sci.fits",
            "drz_wht.fits",
            "bg.fits",
        ]

        for file in rmfiles:
            try:
                os.remove(file)
            except:
                pass

    return xshift, yshift, rot, scale, xrms, yrms
Esempio n. 17
0
def spitzer_sub(new, newunc, newcov, ref, refunc, refcov, out,
                stamps=None, tmass=None):

    # Remove nan from mosaics
    x = pyfits.open(new)
    y = np.nan_to_num(x[0].data)
    x[0].data = y
    new2 = "n%s" % new
    x.writeto(new2)

    # Find stars in new image
    os.system("$REDUCTION/runsex.pl %s 5.0 -weight %s" % (new2, newcov))

    # Find stars in reference
    os.system("$REDUCTION/runsex.pl %s 5.0 -weight %s" % (ref, refcov))

    # Create file for geotran
    stars = Starlist("%s.stars" % new2)
    refstars = Starlist("%s.stars" % ref)
    refstars.pix2wcs(ref)
    refstars.wcs2pix(new2)
    a,b = stars.match(refstars, maxnum=1000)

    # Create file for geotran
    b.pix2wcs(new2)
    b.wcs2pix(ref)
    refroot = ref.split(".")[0]
    outf = open("%s.match" % refroot, "w")
    for i in range(len(a)):
        outf.write("%10.3f%10.3f%10.3f%10.3f\n" % (a[i].xval, a[i].yval,
                   b[i].xval, b[i].yval))
    outf.close()

    # Geomap and geotran
    [naxis1, naxis2] = get_head(new, ["NAXIS1", "NAXIS2"])
    iraf.geomap("%s.match" % refroot, "%s.db" % refroot, 1, naxis1, 1, naxis2,
                fitgeometry="rotate", interactive=no)
    iraf.geotran(ref, "t%s" % ref, "%s.db" % refroot, "%s.match" % refroot)
    iraf.geotran(refunc, "t%s" % refunc, "%s.db" % refroot, 
                 "%s.match" % refroot)

    # Get stars for PSF matching
    if stamps != None:

        psfstars = Starlist(stamps)
        psfstars.pix2wcs(ref)
        psfstars.wcs2pix(new2)
        outf = open("stamps.lis", "w")
        for star in psfstars:
            outf.write("%10.3f%10.3f\n" % (star.xval, star.yval))
        outf.close()

    # Appropriate parameters
    iraf.iterstat(ref)
    update_head(ref, ["MEDSKY", "SKYSIG"], [iraf.iterstat.median,
                                            iraf.iterstat.sigma])
    [refskybkg, refskysig] = get_head(ref, ["MEDSKY", "SKYSIG"])
    tl = refskybkg - 10 * refskysig; tu = 30000.0
    iraf.iterstat(new)
    update_head(new, ["MEDSKY", "SKYSIG"], [iraf.iterstat.median, 
                                            iraf.iterstat.sigma])
    [newskybkg, newskysig] = get_head(new, ["MEDSKY", "SKYSIG"])
    il = newskybkg - 10 * newskysig; iu = 30000.0
    
    # Run hotpants
    hpcmd = "hotpants -inim %s -tmplim t%s -outim %s -tni t%s -ini %s -nsx 3 -nsy 3 -savexy %s.xy -ko 0 -bgo 0 -oni u%s -n t -tl %.2f -tu %.2f -il %.2f -iu %.2f -r 7.5 -rss 18.0" % (new2, ref, out, refunc, newunc, new2, out, tl, tu, il, iu)
    if stamps != None:
        hpcmd += " -ssf stamps.lis -afssc 0"
    os.system(hpcmd)
    #iraf.imarith(new2, "-", "t%s" % ref, out)

    # Create appropriate weight image
    #iraf.imexpr("sqrt(a**2 + b**2)", "u%s" % out, a=newunc, b=refunc)
    iraf.imarith(1, "/", "u%s" % out, "temp1.fits")
    iraf.imarith("temp1.fits", "/", "u%s" % out, "w%s" % out)
    os.remove("temp1.fits")

    # Pick up candidates
    os.system("$REDUCTION/runsex.pl %s 2.0 -weight w%s" % (out, out))
    stars = Starlist("%s.stars" % out)
    cands = []
    [nax1, nax2] = get_head(ref, ["NAXIS1", "NAXIS2"])
    stars.pix2wcs(new)
    stars.wcs2pix(ref)
    refu = pyfits.open(refunc)
    for star in stars:
        if star.xval > EDGETOL and star.xval < (nax1 - EDGETOL) and star.yval > EDGETOL and star.yval < (nax2 - EDGETOL) and refu[0].data[star.yval,star.xval] != 0 and star.fwhmw > 0.5 and star.fwhmw < 10.0:
            cands.append(star)
    scands = Starlist(stars=cands)

    # Filter out bright stars
    if tmass==None:
        scands.write("cands.reg")
    else:
        stmass = Starlist(tmass)
        stmass.wcs2pix(ref)
        s2cands = scands.nomatch(stmass)
        s2cands.write("cands.reg")
    
    # More comprehensive list
    os.system("$REDUCTION/runsex.pl %s 1.5" % out)
    stars = Starlist("%s.stars" % out)
    cands = []
    [nax1, nax2] = get_head(ref, ["NAXIS1", "NAXIS2"])
    stars.pix2wcs(new)
    stars.wcs2pix(ref)
    refu = pyfits.open(refunc)
    for star in stars:
        if star.xval > EDGETOL and star.xval < (nax1 - EDGETOL) and star.yval > EDGETOL and star.yval < (nax2 - EDGETOL) and refu[0].data[star.yval,star.xval] != 0 and star.fwhmw > 0.5 and star.fwhmw < 10.0:
            cands.append(star)
    scands = Starlist(stars=cands)
    scands.write("cands_all.reg")

    return
Esempio n. 18
0
def alignimages(im1, im2):
    runsex = 1  #run sextractor to get object catalogs
    auto = 0.
    t = im1.split('.')
    prefixim1 = t[0]
    t = prefixim1.split('pre')
    fieldid = t[0]  #saves cl1018 or whatever as fieldid
    t = im2.split('.')
    prefixim2 = t[0]

    filexy1 = prefixim1 + '-xy.cat'
    filexy2 = prefixim2 + '-xy.cat'
    print "aligning images for ", fieldid
    if runsex > 0:

        runsextractor(im1, auto)
        infile = open('test.cat', 'r')  #keep only sources w/in useable field

        outfile = open(filexy1, 'w')
        for line in infile:  #keep only targets w/in 500 pixels of center
            if line.find('#') > -1:
                continue
            t = line.split()
            out = '%8.4f %8.4f \n' % (float(t[10]), float(t[11]))
            outfile.write(out)
        infile.close()
        outfile.close()

        print "running sextractor on ", im2
        runsextractor(im2, auto)

        infile = open('test.cat', 'r')  #keep only sources w/in useable field

        outfile = open(filexy2, 'w')
        for line in infile:  #keep only targets w/in 500 pixels of center
            if line.find('#') > -1:
                continue
            t = line.split()
            out = '%8.4f %8.4f \n' % (float(t[10]), float(t[11]))
            outfile.write(out)
        infile.close()
        outfile.close()

    match = fieldid + '-match'
    dbase = fieldid + '-match-geomap'
    iraf.display(im1, 1, fill='yes')
    iraf.display(im2, 2, fill='yes')
    reffile = fieldid + 'refpoints'  #make these individually.  First line w/3points in gmos image (x1 y1 x2 y2 x3 y3). 2nd line w/same points in ediscs I-band
    flag = raw_input("Should we create a refpoints file (0=no, 1=yes)?\n")
    flag = int(flag)
    if flag > 0.1:
        s = 'cp cl1018refpoints ' + reffile
        os.system(s)
        print "open ", reffile, " in emacs, delete 2 lines"
        print "Enter x1 y1 x2 y2  x3 y3 for ref image (gmos)"
        print "Enter x1 y1 x2 y2  x3 y3 for 2nd image (vlt)"
        iraf.imexam()

    iraf.xyxymatch(filexy2,
                   filexy1,
                   match,
                   tolerance=15,
                   refpoints=reffile,
                   matching='tolerance',
                   interactive='no')
    iraf.imgets(image=im1, param='i_naxis1')
    t = iraf.imgets.value
    im1xmax = (float(t))
    iraf.imgets(image=im1, param='i_naxis2')
    t = iraf.imgets.value
    im1ymax = (float(t))

    iraf.geomap(input=match,
                database=dbase,
                function='polynomial',
                xmin=1,
                xmax=im1xmax,
                ymin=1,
                ymax=im1ymax,
                xxorder=4,
                xyorder=4,
                yyorder=4,
                yxorder=4,
                transform='gmos',
                interactive='yes')
    transformim = 1
    if transformim > 0:
        outim2 = 'g' + im2
        iraf.geotran(im2, output=outim2, database=dbase, transform='gmos')
        iraf.display(im2, 1, fill='yes')
        iraf.display(outim2, 2, fill='yes')
        iraf.display(im1, 3, fill='yes')
Esempio n. 19
0
def imshiftcomb(inlist,
                outimg,
                fitgeom='shift',
                inpref='',
                objmask='none',
                combine='average',
                reject='none',
                fscale=False,
                fbase=100,
                fhead='F1',
                second=False,
                first_pref='sdfr',
                second_pref='sdf2r',
                indep=False,
                sigmap='none',
                expmap='none',
                whtmap='none',
                gain=5.6,
                ffpref=''):

    # check output image
    if os.access(outimg, os.R_OK):
        print >> sys.stderr, 'operation would overwrite existing image (%s)' % outimg
        return 1

    # check input image list
    inimg_arr = check_input(inlist, inpref)
    if isinstance(inimg_arr, int):
        return 1

    # check input image list
    if sigmap != 'none':
        ffimg_arr = check_input2(inlist, ffpref)
        if isinstance(ffimg_arr, int):
            return 1

    # check optional output image
    if sigmap != 'none':
        if os.access(sigmap, os.R_OK):
            print >> sys.stderr, 'operation would overwrite existing image (%s)' % sigmap
            return 1
    if expmap != 'none':
        if os.access(expmap, os.R_OK):
            print >> sys.stderr, 'operation would overwrite existing image (%s)' % expmap
            return 1

    if whtmap != 'none':
        if os.access(whtmap, os.R_OK):
            print >> sys.stderr, 'operation would overwrite existing image (%s)' % whtmap
            return 1

    # get array size
    im = pyfits.open(inimg_arr[0])
    nx = im[0].header['NAXIS1']
    ny = im[0].header['NAXIS2']
    im.close()

    # check geomap data
    dx = []
    dy = []
    gmp_arr = []
    gmp2_arr = []
    dbs_arr = []
    for i in range(len(inimg_arr)):
        fname, ext = os.path.splitext(inimg_arr[i])
        gmp = fname + '.gmp'
        gmp2 = fname + '.gmp2'
        dbs = fname + '.dbs'

        gmp_arr.append(gmp)
        gmp2_arr.append(gmp2)
        dbs_arr.append(dbs)

        if not os.access(gmp, os.R_OK):
            print >> sys.stderr, 'geomap file (%s) does not exist' % (gmp)
            return 1

        if os.access(dbs, os.R_OK):
            print >> sys.stderr, 'database file (%s) is already exist' % (dbs)
            return 1

        if os.access(gmp2, os.R_OK):
            print >> sys.stderr, 'modified geomap file (%s) is already exist' % (
                gmp2)
            return 1

        fgmp = open(gmp)
        nl = 1
        dx_ave = 0.0
        dy_ave = 0.0
        for line in fgmp:
            if not line.startswith('#'):
                param = line[:-1].split()
                if len(param) != 4:
                    print >> sys.stderr, 'Invalid format in line %d of %s: %s' % (
                        nl, gmp, line[:-1])
                    fgmp.close()
                    return 1
                else:
                    if isfloat(param[0]) == False or isfloat(
                            param[1]) == False or isfloat(
                                param[2]) == False or isfloat(
                                    param[3]) == False:
                        print >> sys.stderr, 'failed to decode line %d of %s: %s' % (
                            nl, gmp, line[:-1])
                        fgmp.close()
                        return 1
                    else:
                        dx_ave += float(param[0]) - float(param[2])
                        dy_ave += float(param[1]) - float(param[3])

                nl += 1
        #print inimg_arr[i],nl
        dx.append(dx_ave / (nl - 1))
        dy.append(dy_ave / (nl - 1))

    if len(inimg_arr) != len(dx):
        print >> sys.stderr, 'number of input images does not match with that of offsets'
        return 1

    #print 'debug'
    #print dx, max(dx), min(dx)
    #print dy, max(dy), min(dy)

    # check object mask
    if objmask.lower() == 'none':
        objmask = ''
    else:
        objmask_arr = check_inpref(objmask, inimg_arr)
        if isinstance(objmask_arr, int):
            return 1

    # independent run flag
    if indep:
        second = True

    # prepare for temporary file
    tmp = tempfile.NamedTemporaryFile(suffix='', prefix='', dir='/tmp')
    tmp_prefix = tmp.name
    tmp.close()

    # calculate image median for zero shift
    iraf.unlearn('imstat')
    iraf.unlearn('mimstat')
    bgmed = []
    for i in range(len(inimg_arr)):
        if objmask == '':
            ret = iraf.imstat(inimg_arr[i],
                              format='no',
                              fields='midpt',
                              nclip=50,
                              lsigma=3.,
                              usigma=3.,
                              Stdout=1)
        else:
            if not second:
                ret = iraf.mimstat(inimg_arr[i],
                                   imasks=objmask_arr[i] + '[pl]',
                                   format='no',
                                   fields='midpt',
                                   nclip=50,
                                   lsigma=3.,
                                   usigma=3.,
                                   Stdout=1)
            else:
                ret = iraf.mimstat(inimg_arr[i],
                                   imasks=objmask_arr[i],
                                   format='no',
                                   fields='midpt',
                                   nclip=50,
                                   lsigma=3.,
                                   usigma=3.,
                                   Stdout=1)
        if len(ret) == 1:
            bgmed.append(-1.0 * float(ret[0]))
        else:
            fout.close()
            remove_temp_all(tmp_prefix)
            print >> sys.stderr, 'failed to calculate median of the background in %s' % inimg_arr[
                i]
            return 1

    # get large array size and combined image size
    ret = get_large_region(nx, ny, dx, dy)
    if len(ret) != 6:
        print >> sys.stderr, 'failed to get large array size'
        return 1

    x_size = ret[0]
    y_size = ret[1]
    xcmin = ret[2]
    xcmax = ret[3]
    ycmin = ret[4]
    ycmax = ret[5]

    # calculate image region in the large format
    xmin = int((x_size - nx) / 2) + 1
    xmax = nx + int((x_size - nx) / 2)
    ymin = int((y_size - ny) / 2) + 1
    ymax = ny + int((y_size - ny) / 2)

    #print 'debug'
    #print x_size, y_size, xcmin, xcmax, ycmin, ycmax
    #print xmin, xmax, ymin, ymax

    # copy image to larger format and shift image #
    iraf.unlearn('geomap')
    iraf.unlearn('geotran')

    obj_list = tmp_prefix + '_obj.lst'
    if os.access(obj_list, os.R_OK):
        os.remove(obj_list)
    fobj = open(obj_list, 'w')

    # for exposure time weight
    expweight = tmp_prefix + '_exp.lst'
    if os.access(expweight, os.R_OK):
        os.remove(expweight)
    fexp = open(expweight, 'w')

    # for zero offset
    zeroshift = tmp_prefix + '_zeroshift.dat'
    if os.access(zeroshift, os.R_OK):
        os.remove(zeroshift)
    fzero = open(zeroshift, 'w')

    # save the original fit geometry
    fitgeom_org = fitgeom

    # preparing for the sigma list and mask
    if sigmap == 'none':
        tmp_rejmask = ''
    else:
        tmp_rejmask = tmp_prefix + 'rejmask.fits'
        if os.access(tmp_rejmask, os.R_OK):
            os.remove(tmp_rejmask)
        inverse_var_list = tmp_prefix + '_var.lst'
        if os.access(inverse_var_list, os.R_OK):
            os.remove(inverse_var_list)
        finverse_var = open(inverse_var_list, 'w')

    for i in range(len(inimg_arr)):

        # restore the original fit geometry
        fitgeom = fitgeom_org

        # geometry transformation
        fgmp = open(gmp_arr[i])
        fgmp2 = open(gmp2_arr[i], 'w')
        nobj = 0
        for line in fgmp:
            if not line.startswith('#'):
                param = line[:-1].split()
                xref = float(param[0]) + xmin - 1
                yref = float(param[1]) + ymin - 1
                xin = float(param[2]) + xmin - 1
                yin = float(param[3]) + ymin - 1
                fgmp2.write('%.3f %.3f %.3f %.3f\n' % (xref, yref, xin, yin))
                nobj += 1
        fgmp.close()
        fgmp2.close()

        # check number of objects
        if i == 0 and nobj == 1 and fitgeom == 'rotate':
            print 'Warning: Number of reference objects is not enought to measure the rotation'
            print 'Warning: Only shift applied for all images'
            fitgeom = 'shift'
            fitgeom_org = 'shift'

        if nobj == 1 and fitgeom == 'rotate':
            print 'Warning: Number of objects in %s is not enought to measure the rotation' % (
                inimg_arr[i])
            print 'Warning: Only shift applied for %s' % (inimg_arr[i])
            fitgeom = 'shift'

        # mapping geometry
        iraf.geomap(gmp2_arr[i],
                    dbs_arr[i],
                    1,
                    x_size,
                    1,
                    y_size,
                    fitgeom=fitgeom,
                    interac='no')

        # mask frame
        msk = np.ones((y_size, x_size))
        msk[ymin - 1:ymax, xmin - 1:xmax] = 0
        hdu = pyfits.PrimaryHDU(msk)
        msk_img = pyfits.HDUList([hdu])
        msk_fits = tmp_prefix + 'mask' + os.path.basename(inimg_arr[i])
        msktr_fits = tmp_prefix + 'masktr' + os.path.basename(inimg_arr[i])
        if os.access(msk_fits, os.R_OK):
            os.remove(msk_fits)
        if os.access(msktr_fits, os.R_OK):
            os.remove(msktr_fits)
        msk_img.writeto(msk_fits)
        msk_img.close()

        # transform mask geometry
        iraf.geotran(msk_fits,
                     msktr_fits,
                     dbs_arr[i],
                     gmp2_arr[i],
                     geometr='linear',
                     boundar='constant',
                     constant=1)
        os.remove(msk_fits)
        convert_maskfits_int(msktr_fits, msktr_fits)

        # load original frame
        img = pyfits.open(inimg_arr[i])
        if sigmap != 'none':
            ffimg = pyfits.open(ffimg_arr[i])

        # for exposure time weight
        try:
            t = float(img[0].header['EXP1TIME'])
            coadd = float(img[0].header['COADDS'])
            expt = t * coadd
        except KeyError:
            print >> sys.stderr, 'can not read exposure time from the header of %s' % inimg_arr[
                i]
            img.close()
            return 1

        # for flux scaling and weight
        if fscale:
            try:
                flux = float(img[0].header[fhead])
            except KeyError:
                print >> sys.stderr, 'can not read flux keyword (%s) from the header of %s' % (
                    fhead, inimg_arr[i])
                img.close()
                return 1

            flux_scale = fbase / flux
            weight = expt * (1.0 / flux_scale)**2

        else:
            flux_scale = 1.0
            weight = expt

        fzero.write('%f\n' % (bgmed[i] * flux_scale))
        fexp.write('%f\n' % weight)

        # object frame
        obj = np.zeros((y_size, x_size))
        obj[ymin - 1:ymax, xmin - 1:xmax] = img[0].data * flux_scale
        hdu = pyfits.PrimaryHDU(obj)
        obj_img = pyfits.HDUList([hdu])
        obj_img[0].header = img[0].header
        obj_img[0].header['bpm'] = msktr_fits
        obj_img[0].header['expmap'] = expt
        obj_fits = tmp_prefix + 'obj' + os.path.basename(inimg_arr[i])
        objtr_fits = tmp_prefix + 'objtr' + os.path.basename(inimg_arr[i])
        if os.access(obj_fits, os.R_OK):
            os.remove(obj_fits)
        obj_img.writeto(obj_fits)
        obj_img.close()
        iraf.geotran(obj_fits,
                     objtr_fits,
                     dbs_arr[i],
                     gmp2_arr[i],
                     geometr='linear',
                     boundar='constant',
                     constant=0)
        fobj.write('%s\n' % objtr_fits)
        img.close()

        if sigmap != 'none':
            inverse_var = np.zeros((y_size, x_size))
            inverse_var[ymin - 1:ymax, xmin -
                        1:xmax] = (np.sqrt(ffimg[0].data / (gain * expt)) *
                                   flux_scale)**-2
            hdu_var = pyfits.PrimaryHDU(inverse_var)
            inverse_var_img = pyfits.HDUList([hdu_var])
            inverse_var_img[0].header = img[0].header
            inverse_var_img[0].header['bpm2'] = '%s[*,*,%d]' % (tmp_rejmask,
                                                                i + 1)
            inverse_var_fits = tmp_prefix + 'var' + os.path.basename(
                inimg_arr[i])
            inverse_vartr_fits = tmp_prefix + 'vartr' + os.path.basename(
                inimg_arr[i])
            if os.access(inverse_var_fits, os.R_OK):
                os.remove(inverse_var_fits)
            if os.access(inverse_vartr_fits, os.R_OK):
                os.remove(inverse_vartr_fits)
            inverse_var_img.writeto(inverse_var_fits)
            inverse_var_img.close()
            iraf.geotran(inverse_var_fits,
                         inverse_vartr_fits,
                         dbs_arr[i],
                         gmp2_arr[i],
                         geometr='linear',
                         boundar='constant',
                         constant=0)
            finverse_var.write('%s\n' % inverse_vartr_fits)
            ffimg.close()

    # close file handlers
    fobj.close()
    fexp.close()
    fzero.close()
    if sigmap != 'none':
        finverse_var.close()

    # combine image
    comb_img = tmp_prefix + '_comb.fits'
    if os.access(comb_img, os.R_OK):
        os.remove(comb_img)
    if expmap == 'none':
        tmp_expmap = ''
    else:
        tmp_expmap = tmp_prefix + 'expmap.fits'
        if os.access(tmp_expmap, os.R_OK):
            os.remove(tmp_expmap)

    iraf.unlearn('imcombine')
    try:
        iraf.imcombine('@' + obj_list,
                       comb_img,
                       sigma='',
                       rejmask=tmp_rejmask,
                       expmasks=tmp_expmap,
                       combine=combine,
                       reject=reject,
                       masktype='!BPM',
                       maskvalue=0.0,
                       zero='@' + zeroshift,
                       weight='@' + expweight,
                       expname='EXPMAP')
    except:
        if os.access(comb_img, os.R_OK):
            os.remove(comb_img)
        if expmap != 'none':
            if os.access(tmp_expmap, os.R_OK):
                os.remove(tmp_expmap)
        iraf.imcombine('@' + obj_list,
                       comb_img,
                       sigma='',
                       rejmask='',
                       expmasks=tmp_expmap,
                       combine=combine,
                       reject=reject,
                       masktype='!BPM',
                       maskvalue=0.0,
                       zero='@' + zeroshift,
                       weight='@' + expweight,
                       expname='EXPMAP')

    if sigmap != 'none':
        tmp_inverse_var_sum = tmp_prefix + 'inverse_var.fits'
        if os.access(tmp_inverse_var_sum, os.R_OK):
            os.remove(tmp_inverse_var_sum)
        iraf.imcombine('@' + inverse_var_list,
                       tmp_inverse_var_sum,
                       combine='sum',
                       reject='none',
                       masktype='!BPM',
                       maskvalue=0.0)
        iraf.stsdas()
        tmp_sigma = tmp_prefix + 'sigma.fits'
        if os.access(tmp_sigma, os.R_OK):
            os.remove(tmp_sigma)
        iraf.imcalc(tmp_inverse_var_sum,
                    tmp_sigma,
                    'sqrt(1.0/im1)',
                    pixtype='double')

    # cut image
    iraf.unlearn('imcopy')
    cut_img = '%s[%d:%d,%d:%d]' % (comb_img, xcmin, xcmax, ycmin, ycmax)
    iraf.imcopy(cut_img, outimg)
    if expmap != 'none':
        cut_exp = '%s[%d:%d,%d:%d]' % (tmp_expmap, xcmin, xcmax, ycmin, ycmax)
        iraf.imcopy(cut_exp, expmap)
    if sigmap != 'none':
        cut_sigma = '%s[%d:%d,%d:%d]' % (tmp_sigma, xcmin, xcmax, ycmin, ycmax)
        iraf.imcopy(cut_sigma, sigmap)
        if whtmap != 'none':
            cut_wht = '%s[%d:%d,%d:%d]' % (tmp_inverse_var_sum, xcmin, xcmax,
                                           ycmin, ycmax)
            iraf.imcopy(cut_wht, whtmap)

    # delete temporary object files
    remove_temp_all(tmp_prefix + 'obj')
    os.remove(obj_list)
    os.remove(comb_img)

    # record relative offset between input images and combined image and rotation
    for i in range(len(inimg_arr)):
        im = pyfits.open(inimg_arr[i], mode='update')

        if second:

            if indep:

                # calculate offset
                dxc = xcmin - xmin - dx[i]
                dyc = ycmin - ymin - dy[i]

                # retrieve rotation
                rot = 0.0
                fdbs = open(dbs_arr[i])
                for line in fdbs:
                    param = line[:-1].split()
                    if param[0] == 'xrotation':
                        rot = float(param[1])

                if rot > 180.0:
                    rot = rot - 360.0

                im[0].header['dx'] = dx[i]
                im[0].header['dy'] = dy[i]
                im[0].header['dxc'] = dxc
                im[0].header['dyc'] = dyc
                im[0].header['rotation'] = rot

            else:
                # check number of objects in the geomap file
                nobj = 0
                fgmp = open(gmp_arr[0])
                for line in fgmp:
                    nobj += 1

                im1 = pyfits.open(inimg_arr[i].replace(second_pref,
                                                       first_pref),
                                  mode='update')
                for j in range(nobj):
                    key = 'XC%d' % (j + 1)
                    im[0].header[key] = float(im1[0].header[key])
                    key = 'YC%d' % (j + 1)
                    im[0].header[key] = float(im1[0].header[key])
                    key = 'PEAK%d' % (j + 1)
                    im[0].header[key] = float(im1[0].header[key])
                    key = 'FWHM%d' % (j + 1)
                    im[0].header[key] = float(im1[0].header[key])
                key = 'DX'
                im[0].header[key] = float(im1[0].header[key])
                key = 'DY'
                im[0].header[key] = float(im1[0].header[key])
                key = 'DXC'
                im[0].header[key] = float(im1[0].header[key])
                key = 'DYC'
                im[0].header[key] = float(im1[0].header[key])
                key = 'ROTATION'
                im[0].header[key] = float(im1[0].header[key])
                im1.close()

        else:

            # calculate offset
            dxc = xcmin - xmin - dx[i]
            dyc = ycmin - ymin - dy[i]

            # retrieve rotation
            rot = 0.0
            fdbs = open(dbs_arr[i])
            for line in fdbs:
                param = line[:-1].split()
                if param[0] == 'xrotation':
                    rot = float(param[1])

            if rot > 180.0:
                rot = rot - 360.0

            im[0].header['dx'] = dx[i]
            im[0].header['dy'] = dy[i]
            im[0].header['dxc'] = dxc
            im[0].header['dyc'] = dyc
            im[0].header['rotation'] = rot

        im.close()

    # remove all temporary files
    remove_temp_all(tmp_prefix)

    return 0
Esempio n. 20
0
def align_to_reference(ROOT_DIRECT,
                       ALIGN_IMAGE,
                       fitgeometry="shift",
                       clean=True,
                       verbose=False,
                       ALIGN_EXTENSION=0,
                       toler=3,
                       skip_swarp=False,
                       align_sdss_ds9=False,
                       catalog=None):
    """
xshift, yshift, rot, scale, xrms, yrms = align_to_reference()
    """
    import os
    import glob
    import shutil

    from pyraf import iraf
    from iraf import stsdas, dither

    import threedhst
    from threedhst import catIO

    no = iraf.no
    yes = iraf.yes
    INDEF = iraf.INDEF

    #### Clean slate
    rmfiles = [
        'SCI.fits', 'WHT.fits', 'align.cat', 'direct.cat'
        'align.map', 'align.match', 'align.reg', 'align.xy', 'direct.reg',
        'direct.xy', 'ds9_align.tsv'
    ]

    for file in rmfiles:
        try:
            os.remove(file)
        except:
            pass

    if catalog is not None:
        align_sdss_ds9 = True

    #### Get only images that overlap from the ALIGN_IMAGE list
    if not align_sdss_ds9:
        align_img_list = find_align_images_that_overlap(
            ROOT_DIRECT + '_drz.fits',
            ALIGN_IMAGE,
            ALIGN_EXTENSION=ALIGN_EXTENSION)
        if not align_img_list:
            print 'threedhst.shifts.align_to_reference: no alignment images overlap.'
            return 0, 0

    #### Use swarp to combine the alignment images to the same image
    #### dimensions as the direct mosaic
    if (not skip_swarp) & (not align_sdss_ds9):
        try:
            os.remove(ROOT_DIRECT + '_align.fits')
        except:
            pass
        matchImagePixels(input=align_img_list,
                         matchImage=ROOT_DIRECT + '_drz.fits',
                         output=ROOT_DIRECT + '_align.fits',
                         match_extension=1,
                         input_extension=ALIGN_EXTENSION)

    #### Run SExtractor on the direct image, with the WHT
    #### extension as a weight image
    se = threedhst.sex.SExtractor()
    se.aXeParams()
    se.copyConvFile()
    se.overwrite = True
    se.options['CHECKIMAGE_TYPE'] = 'NONE'
    se.options['WEIGHT_TYPE'] = 'MAP_WEIGHT'
    se.options['WEIGHT_IMAGE'] = 'WHT.fits'
    se.options['FILTER'] = 'Y'
    ## Detect thresholds (default = 1.5)
    THRESH = 10
    if align_sdss_ds9:
        if 'Vizier' not in REFERENCE_CATALOG:
            THRESH = 20

    se.options['DETECT_THRESH'] = '%d' % (THRESH)
    se.options['ANALYSIS_THRESH'] = '%d' % (THRESH)
    se.options['MAG_ZEROPOINT'] = str(threedhst.options['MAG_ZEROPOINT'])

    #### Run SExtractor on direct and alignment images
    ## direct image
    se.options['CATALOG_NAME'] = 'direct.cat'
    iraf.imcopy(ROOT_DIRECT + '_drz.fits[1]', "SCI.fits")
    iraf.imcopy(ROOT_DIRECT + '_drz.fits[2]', "WHT.fits")
    status = se.sextractImage('SCI.fits')

    ## Read the catalog
    directCat = threedhst.sex.mySexCat('direct.cat')

    if align_sdss_ds9:
        ### Use ds9 SDSS catalog to refine alignment
        import threedhst.dq
        import pywcs
        import threedhst.catIO as catIO

        wcs = pywcs.WCS(pyfits.getheader('SCI.fits', 0))
        #wcs = pywcs.WCS(pyfits.getheader('Q0821+3107-F140W_drz.fits', 1))

        if 'Vizier' in REFERENCE_CATALOG:
            #### Use (unstable) astroquery Vizier search
            #### CFHTLS-Deep: 'Vizier.II/317'
            VIZIER_CAT = REFERENCE_CATALOG.split('Vizier.')[1]
            print 'Align to Vizier catalog: http://vizier.u-strasbg.fr/viz-bin/VizieR?-source=%s' % (
                VIZIER_CAT)

            import astroquery
            if astroquery.__version__ < '0.0.dev1078':
                from astroquery import vizier

                query = {}
                query["-source"] = VIZIER_CAT
                #query["-out"] = ["_r", "CFHTLS", "rmag"]
                query["-out"] = ["_RAJ2000", "_DEJ2000"]  ### Just RA/Dec.

                #### Center position and query radius
                r0, d0 = wcs.wcs_pix2sky([[wcs.naxis1 / 2., wcs.naxis2 / 2.]],
                                         1)[0]
                rll, dll = wcs.wcs_pix2sky([[0, 0]], 1)[0]
                corner_radius = np.sqrt(
                    (r0 - rll)**2 * np.cos(d0 / 360. * 2 * np.pi)**2 +
                    (d0 - dll)**2) * 60. * 1.5
                h = query["-c"] = "%.6f %.6f" % (r0, d0)
                query["-c.rm"] = "%.3f" % (corner_radius
                                           )  ### xxx check image size

                #### Run the query
                vt = vizier.vizquery(query)
            else:
                #### Newer astroquery
                from astroquery.vizier import Vizier
                import astropy.coordinates as coord
                import astropy.units as u

                Vizier.ROW_LIMIT = -1

                r0, d0 = wcs.wcs_pix2sky([[wcs.naxis1 / 2., wcs.naxis2 / 2.]],
                                         1)[0]
                rll, dll = wcs.wcs_pix2sky([[0, 0]], 1)[0]
                corner_radius = np.sqrt(
                    (r0 - rll)**2 * np.cos(d0 / 360. * 2 * np.pi)**2 +
                    (d0 - dll)**2) * 60. * 1.5
                #
                c = coord.ICRSCoordinates(ra=r0, dec=d0, unit=(u.deg, u.deg))
                #### something with astropy.coordinates
                # c.icrs.ra.degree = c.icrs.ra.degrees
                # c.icrs.dec.degree = c.icrs.dec.degrees
                #
                vt = Vizier.query_region(c,
                                         width=u.Quantity(
                                             corner_radius, u.arcminute),
                                         catalog=[VIZIER_CAT])[0]

            #### Make a region file
            ra_list, dec_list = vt['RAJ2000'], vt['DEJ2000']
            print 'Vizier, found %d objects.' % (len(ra_list))
            fp = open('%s.vizier.reg' % (ROOT_DIRECT), 'w')
            fp.write('# %s, r=%.1f\'\nfk5\n' % (VIZIER_CAT, corner_radius))
            for ra, dec in zip(ra_list, dec_list):
                fp.write('circle(%.6f, %.6f, 0.5")\n' % (ra, dec))
            #
            fp.close()
        else:
            #### Use DS9 catalog
            ds9 = threedhst.dq.myDS9()
            ds9.set('file SCI.fits')
            #ds9.set('file Q0821+3107-F140W_drz.fits')
            ds9.set('catalog %s' % (REFERENCE_CATALOG))
            ### Can't find XPA access point for "copy to regions"
            ds9.set('catalog export tsv ds9_align.tsv')
            lines = open('ds9_align.tsv').readlines()
            ra_list, dec_list = [], []
            for line in lines[1:]:
                spl = line.split()
                ra, dec = float(spl[0]), float(spl[1])
                ra_list.append(ra)
                dec_list.append(dec)
            #
            del (ds9)

        x_image, y_image = [], []
        for ra, dec in zip(ra_list, dec_list):
            x, y = wcs.wcs_sky2pix([[ra, dec]], 1)[0]
            x_image.append(x)
            y_image.append(y)

        alignCat = catIO.EmptyCat()
        alignCat['X_IMAGE'] = np.array(x_image)
        alignCat['Y_IMAGE'] = np.array(y_image)

    else:
        ## alignment image
        se.options['CATALOG_NAME'] = 'align.cat'
        status = se.sextractImage(ROOT_DIRECT + '_align.fits')
        alignCat = threedhst.sex.mySexCat('align.cat')

    xshift = 0
    yshift = 0
    rot = 0
    scale = 1.

    xrms = 2
    yrms = 2

    NITER = 5
    IT = 0
    while (IT < NITER):
        IT = IT + 1

        #### Get x,y coordinates of detected objects
        ## direct image
        fp = open('direct.xy', 'w')
        for i in range(len(directCat.X_IMAGE)):
            fp.write('%s  %s\n' % (directCat.X_IMAGE[i], directCat.Y_IMAGE[i]))
        fp.close()

        ## alignment image
        fp = open('align.xy', 'w')
        for i in range(len(alignCat.X_IMAGE)):
            fp.write('%s  %s\n' % (np.float(alignCat.X_IMAGE[i]) + xshift,
                                   np.float(alignCat.Y_IMAGE[i]) + yshift))
        fp.close()

        iraf.flpr()
        iraf.flpr()
        iraf.flpr()
        #### iraf.xyxymatch to find matches between the two catalogs
        pow = toler * 1.
        try:
            os.remove('align.match')
        except:
            pass

        status1 = iraf.xyxymatch(input="direct.xy",
                                 reference="align.xy",
                                 output="align.match",
                                 tolerance=2**pow,
                                 separation=0,
                                 verbose=yes,
                                 Stdout=1)

        nmatch = 0
        while status1[-1].startswith('0') | (nmatch < 10):
            pow += 1
            os.remove('align.match')
            status1 = iraf.xyxymatch(input="direct.xy",
                                     reference="align.xy",
                                     output="align.match",
                                     tolerance=2**pow,
                                     separation=0,
                                     verbose=yes,
                                     Stdout=1)
            #
            nmatch = 0
            for line in open('align.match').xreadlines():
                nmatch += 1

        if verbose:
            for line in status1:
                print line

        #### Compute shifts with iraf.geomap
        iraf.flpr()
        iraf.flpr()
        iraf.flpr()
        try:
            os.remove("align.map")
        except:
            pass

        status2 = iraf.geomap(input="align.match",
                              database="align.map",
                              fitgeometry=fitgeometry,
                              interactive=no,
                              xmin=INDEF,
                              xmax=INDEF,
                              ymin=INDEF,
                              ymax=INDEF,
                              maxiter=10,
                              reject=2.0,
                              Stdout=1)
        if verbose:
            for line in status2:
                print line

        #fp = open(root+'.iraf.log','a')
        #fp.writelines(status1)
        #fp.writelines(status2)
        #fp.close()

        #### Parse geomap.output
        fp = open("align.map", "r")
        for line in fp.readlines():
            spl = line.split()
            if spl[0].startswith('xshift'):
                xshift += float(spl[1])
            if spl[0].startswith('yshift'):
                yshift += float(spl[1])
            if spl[0].startswith('xrotation'):
                rot = float(spl[1])
            if spl[0].startswith('xmag'):
                scale = float(spl[1])
            if spl[0].startswith('xrms'):
                xrms = float(spl[1])
            if spl[0].startswith('yrms'):
                yrms = float(spl[1])

        fp.close()

        #os.system('wc align.match')
        print 'Shift iteration #%d, xshift=%f, yshift=%f, rot=%f, scl=%f (rms: %5.2f,%5.2f)' % (
            IT, xshift, yshift, rot, scale, xrms, yrms)

    im = pyfits.open('SCI.fits')

    shutil.copy('align.map', ROOT_DIRECT + '_align.map')
    shutil.copy('align.match', ROOT_DIRECT + '_align.match')

    #### Cleanup
    if clean:
        rmfiles = [
            'SCI.fits', 'WHT.fits', 'align.cat', 'align.map', 'align.match',
            'align.reg', 'align.xy', 'direct.cat', 'direct.reg', 'direct.xy',
            'drz_sci.fits', 'drz_wht.fits', 'bg.fits'
        ]

        for file in rmfiles:
            try:
                os.remove(file)
            except:
                pass

    return xshift, yshift, rot, scale, xrms, yrms
Esempio n. 21
0
def kaitsub(refimage,inlist,kernel=9,satval=50000.0,dspace=0,dbkg=0,
            nstamps=2,clobber=globclob):

    infiles = iraffiles(inlist)

    # RA and Dec of center pixel in reference image
    [nx, ny] = get_head(refimage, ['NAXIS1', 'NAXIS2'])
    xcenpix = (nx / 2.0)
    ycenpix = (ny / 2.0)
    [[ra, dec]] = impix2wcs(refimage, xcenpix, ycenpix)

    # Big loop
    for image in infiles:

        # Separate image rootname from extension
        root,ext = image.split('.')
 
        # Make sure WCS fit was successful
        if not check_head(image, 'IQWCS'):
            print 'Skipping image %s: IQWCS not successful' % image
            infiles.remove(image)
            continue
        
        # Remap image using objects detected on both frames and
        # create stamp list
        stars=Starlist(get_head(image,'STARFILE'))
        refstars=Starlist(get_head(refimage,'STARFILE'))
        refstars.pix2wcs(refimage)
        refstars.wcs2pix(image)
        match,refmatch=stars.match(refstars,useflags=yes,tol=10.0)
        nstars=len(match)
        if not (nstars>2):
            print 'Could not find star matches between reference and %s' % image
            infiles.remove(image)
            continue
        refmatch.pix2wcs(image)
        refmatch.wcs2pix(refimage)
        matchfile=open('%s.match' % root, 'w')
        stampfile=open('%s.stamps' % root, 'w')
        for i in range(len(match)):
            matchfile.write('%10.3f%10.3f%10.3f%10.3f\n' % (refmatch[i].xval,
                            refmatch[i].yval,match[i].xval,match[i].yval))
            stampfile.write('%10.3f%10.3f\n' % (refmatch[i].xval,
                            refmatch[i].yval))
        matchfile.close()
        stampfile.close()
        check_exist('%s.geodb' % root, 'w', clobber=clobber)
        iraf.geomap('%s.match' % root,'%s.geodb' % root,1.0,nx,1.0,ny,
                    fitgeom="general",verbose=no,interactive=no)
        check_exist('%s.shift.fits' % root, 'w', clobber=clobber)
        iraf.geotran(image,'%s.shift' % root,'%s.geodb' % root,
                     '%s.match' % root,geometry="geometric",
                     boundary="constant",verbose=no)
 
        # Run the subtraction
        check_exist('%s.sub.fits' % root, 'w', clobber=clobber)
        if os.path.exists("stamps.lis"):
            cmd = '$REDUCTION/hotpants -inim %s.shift.fits -tmplim %s -outim %s.sub.fits -tu %.2f -tg %.2f -tr %.2f -iu %.2f -ig %.2f -ir %.2f -r %.2f -ssf stamps.lis -afssc 0 -n t -ko %i -bgo %i -savexy %s.st -nsx %i -nsy %i' % (root, refimage, root, satval, AP_GAIN, AP_READN, satval, FL_GAIN, FL_READN, kernel, dspace, dbkg, root, nstamps, nstamps)
        else:
            cmd = '$REDUCTION/hotpants -inim %s.shift.fits -tmplim %s -outim %s.sub.fits -tu %.2f -tg %.2f -tr %.2f -iu %.2f -ig %.2f -ir %.2f -r %.2f -ssf %s.stamps -afssc 0 -n t -ko %i -bgo %i -savexy %s.st -nsx %i -nsy %i' % (root, refimage, root, satval, AP_GAIN, AP_READN, satval, FL_GAIN, FL_READN, kernel, root, dspace, dbkg, root, nstamps, nstamps)
            #cmd = '$REDUCTION/pois -k %i %i -s %.2f %.2f -S %s.stamps %s %s.shift.fits %s.sub.fits' % (kernel,kernel,satval,satval,root,refimage,root,root)
        fcmd = os.popen(cmd, 'r')
        sublines = fcmd.readlines()
        fcmd.close()

    # Return
    print 'Exiting successfully'
    return
Esempio n. 22
0
def psfphot(inlist,
            ra,
            dec,
            reffilt,
            interact,
            fwhm,
            readnoise,
            gain,
            threshold,
            refimage=None,
            starfile=None,
            maxnpsf=5,
            clobber=globclob,
            verbose=globver,
            skykey='SKYBKG',
            filtkey='FILTER',
            pixtol=3.0):
    """ 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()

    # Parse inputs
    infiles = iraffiles(inlist)

    # Which file is reffilt?  call it refimage
    if refimage == None:
        for image in infiles:
            if check_head(image, filtkey):
                try:
                    imgfilt = get_head(image, filtkey)
                    if imgfilt == reffilt:
                        refimage = image
                        break
                except:
                    pass

    if not refimage:
        print "BAD USER!  No image corresponds to the filter: %s" % reffilt
        return
    else:
        refroot = 's' + refimage.split('.')[0]

    #first make sure to add back in background of sky
    iraf.iqsubsky(inlist, sub=no, skykey=skykey)

    #put reference image first on list
    infiles.remove(refimage)
    infiles.insert(0, refimage)

    #setup for keywords
    if gain == "!GAIN":
        try:
            gainval = float(get_head(image, gain))
        except:
            print "Bad header keyword for gain."
    else:
        gainval = float(gain)

    if readnoise == "!READNOISE":
        try:
            readval = float(get_head(image, readnoise))
        except:
            print "Bad header keyword for readnoise."
    else:
        readval = float(readnoise)

    # Process each file in turn
    for image in infiles:

        # Check that the image is there
        check_exist(image, "r")

        # Grab image root name
        root = image.split('.')[0]

        # Map image to reference image
        if not (image == refimage):
            [nx, ny] = get_head(image, ['NAXIS1', 'NAXIS2'])
            stars = Starlist(get_head(image, 'STARFILE'))
            refstars = Starlist(get_head(refimage, 'STARFILE'))
            refstars.pix2wcs(refimage)
            refstars.wcs2pix(image)
            match, refmatch = stars.match(refstars, useflags=yes, tol=10.0)
            nstars = len(match)
            if not (nstars > 2):
                print 'Could not find star matches between reference and %s' % image
                infiles.remove(image)
                continue
            refmatch.pix2wcs(image)
            refmatch.wcs2pix(refimage)
            matchfile = open('%s.match' % root, 'w')
            for i in range(len(match)):
                matchfile.write('%10.3f%10.3f%10.3f%10.3f\n' %
                                (refmatch[i].xval, refmatch[i].yval,
                                 match[i].xval, match[i].yval))
            matchfile.close()
            check_exist('%s.geodb' % root, 'w', clobber=clobber)
            iraf.geomap('%s.match' % root,
                        '%s.geodb' % root,
                        1.0,
                        nx,
                        1.0,
                        ny,
                        verbose=no,
                        interactive=no)
            check_exist('s%s.fits' % root, 'w', clobber=clobber)
            iraf.geotran(image,
                         's%s' % root,
                         '%s.geodb' % root,
                         '%s.match' % root,
                         geometry="geometric",
                         boundary="constant",
                         verbose=no)
        else:
            iraf.imcopy(image, 's%s' % root)
        root = 's%s' % root

        #get sky level and calculate sigma
        #if check_head(image, skykey):
        #    try:
        #        sky=float(get_head(image, skykey))
        #    except:
        #        print "No sky levels in header."

        #sigma= (((sky * gainval) + readval**2)**.5) / gainval
        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.90 * saturate
        iraf.datapars.readnoise = readval
        iraf.datapars.epadu = gainval
        iraf.datapars.filter = filtkey
        iraf.daopars.psfrad = psfmult * fwhm
        iraf.daopars.fitrad = fwhm
        iraf.daopars.function = "gauss,moffat15,moffat25,lorentz,penny1"

        #find stars in image unless a starlist is given
        if image == refimage and starfile == None:
            iraf.daophot.daofind(root,
                                 'refimage.coo.1',
                                 threshold=threshold,
                                 verify=no,
                                 verbose=verbose)
        elif image == refimage:
            shutil.copy(starfile, 'refimage.coo.1')

        #initial photometry
        iraf.daophot.phot(root,
                          'refimage.coo.1',
                          'default',
                          aperture=fwhm,
                          verify=no,
                          verbose=verbose)

        #select stars for psf the first time
        refstarsfile = "refimage.pst.1"
        if image == refimage:
            iraf.pstselect(root,
                           'default',
                           refstarsfile,
                           maxnpsf,
                           interactive=yes,
                           verify=no,
                           verbose=verbose)

        #fit the psf
        iraf.psf(root,
                 'default',
                 refstarsfile,
                 'default',
                 'default',
                 'default',
                 interactive=interact,
                 verify=no,
                 verbose=verbose)

        #identify neighboring/interfering stars to selected stars
        groupingfile = root + ".psg.1"
        iraf.nstar(root,
                   groupingfile,
                   'default',
                   'default',
                   'default',
                   psfrad=psfmultsmall * fwhm,
                   verify=no,
                   verbose=verbose)

        #subtract out neighboring stars from image
        iraf.substar(root,
                     'default',
                     refstarsfile,
                     'default',
                     'default',
                     psfrad=psfmultsmall * fwhm,
                     verify=no,
                     verbose=verbose)

        #repeat psf to get better psf model
        #IRAF's interactive version usually crashes
        subtractedimage = root + ".sub.1"
        iraf.psf(subtractedimage,
                 root + ".nst.1",
                 refstarsfile,
                 '%s.psf.2' % root,
                 '%s.pst.2' % root,
                 '%s.psg.2' % root,
                 interactive=interact,
                 verify=no,
                 verbose=verbose)

        #Need to make sure SN was detected by daofind
        stars = Starlist('%s.mag.1' % root)
        SN = Star(name='SN', radeg=ra, dcdeg=dec, fwhm=2.0, fwhmw=2.0)
        SNlis = Starlist(stars=[SN])
        SNlis.wcs2pix(image)
        if (len(stars.match(SNlis)[0]) == 0):
            #No match - need to add to daofind file
            print "No match!"
            coofile = open('refimage.coo.1', 'a+')
            coofile.write('%10.3f%10.3f%9.3f%8.3f%13.3f%12.3f%8i\n' %
                          (SNlis[0].xval, SNlis[0].yval, 99.999, 0.500, 0.000,
                           0.000, 999))
            coofile.close()

        #repeat aperture photometry to get good comparisons to standard fields
        iraf.daophot.phot(root,
                          'refimage.coo.1',
                          'default',
                          aperture=psfmult * fwhm,
                          verify=no,
                          verbose=verbose)

        # allstar run
        iraf.allstar(root,
                     'default',
                     'default',
                     'default',
                     'default',
                     'default',
                     verify=no,
                     verbose=verbose)
Esempio n. 23
0
def irafalign(filepath, uknstarlist, refstarlist, shape,
              alifilepath=None, outdir="alipy_out",
              makepng=False, hdu=0, verbose=True):
    """
    Uses iraf geomap and gregister to align the image. Three steps :

     * Write the matched source lists into an input file for geomap
     * Compute a geomap transform from these stars.
     * Run gregister

    :param filepath: FITS file to be aligned
    :type filepath: string

    :param uknstarlist: A list of stars from the "unknown" image to be aligned,
                        that matches to ...
    :type uknstarlist: list of Star objects
    :param refstarlist: ... the list of corresponding stars in the reference
                        image.
    :type refstarlist: list of Star objects

    :param shape: Output shape (width, height)
    :type shape: tuple

    :param alifilepath: where to save the aligned image. If None, I put it in
                        the default directory.
    :type alifilepath: string

    :param makepng: If True I make a png of the aligned image as well.
    :type makepng: boolean

    :param hdu: The hdu of the fits file that you want me to use.
                0 is primary. If multihdu, 1 is usually science.
    """

    try:
        from pyraf import iraf
    except ImportError:
        print("Couldn't import pyraf !")
        return

    assert len(uknstarlist) == len(refstarlist)
    if len(uknstarlist) < 2:
        if verbose:
            print("Not enough stars for using geomap !")
        return

    basename = os.path.splitext(os.path.basename(filepath))[0]
    geomapinpath = basename + ".geomapin"
    geodatabasepath = basename + ".geodatabase"
    if os.path.isfile(geomapinpath):
        os.remove(geomapinpath)
    if os.path.isfile(geodatabasepath):
        os.remove(geodatabasepath)

    # Step 1, we write the geomap input.
    table = []
    for (uknstar, refstar) in zip(uknstarlist, refstarlist):
        table.append([refstar.x, refstar.y, uknstar.x, uknstar.y])
    geomap = open(geomapinpath, "wb")  # b needed for csv
    writer = csv.writer(geomap, delimiter="\t")
    writer.writerows(table)
    geomap.close()

    # Step 2, geomap

    iraf.unlearn(iraf.geomap)
    iraf.geomap.fitgeom = "rscale"
    # You can change this to:
    # shift, xyscale, rotate, rscale
    iraf.geomap.function = "polynomial"  # Surface type
    iraf.geomap.maxiter = 3         # Maximum number of rejection iterations
    iraf.geomap.reject = 3.0        # Rejection limit in sigma units

    # other options you could specify :
    # (xxorder=    2) Order of x fit in x
    # (xyorder=    2) Order of x fit in y
    # (xxterms= half) X fit cross terms type
    # (yxorder=    2) Order of y fit in x
    # (yyorder=    2) Order of y fit in y
    # (yxterms= half) Y fit cross terms type
    # (calctyp= real) Computation type

    iraf.geomap.transfo = "broccoli"    # keep it
    iraf.geomap.interac = "no"		# keep it
    iraf.geomap.verbose = "yes"		# keep it
    # iraf.geomap.results = "bla.summary" # The optional results summary files

    geomapblabla = iraf.geomap(input=geomapinpath,
                               database=geodatabasepath,
                               xmin=1, xmax=shape[0],
                               ymin=1, ymax=shape[1],
                               Stdout=1)

    # We read this output ...
    for line in geomapblabla:
        if "X and Y scale:" in line:
            mapscale = line.split()[4:6]
        if "Xin and Yin fit rms:" in line:
            maprmss = line.split()[-2:]
        if "X and Y axis rotation:" in line:
            mapangles = line.split()[-4:-2]
        if "X and Y shift:" in line:
            mapshifts = line.split()[-4:-2]
            # not used?

    geomaprms = math.sqrt(float(maprmss[0]) * float(maprmss[0]) +
                          float(maprmss[1]) * float(maprmss[1]))
    geomapangle = float(mapangles[0])  # % 360.0
    geomapscale = 1.0 / float(mapscale[0])

    if mapscale[0] != mapscale[1]:
        raise RuntimeError("Error reading geomap scale")
    if verbose:
        print(("IRAF geomap : Rotation %+11.6f [deg], "
               "scale %8.6f, RMS %.3f [pixel]") % (geomapangle,
                                                   geomapscale,
                                                   geomaprms))
    # Step 3

    if alifilepath is None:
        alifilepath = os.path.join(outdir, basename + "_gregister.fits")
    else:
        outdir = os.path.split(alifilepath)[0]
    if not os.path.isdir(outdir):
        os.makedirs(outdir)
    if os.path.isfile(alifilepath):
        os.remove(alifilepath)

    iraf.unlearn(iraf.gregister)
    iraf.gregister.geometry = "geometric"  # linear, distortion, geometric
    iraf.gregister.interpo = "spline3"     # linear, spline3
    iraf.gregister.boundary = "constant"   # padding with zero
    iraf.gregister.constant = 0.0
    iraf.gregister.fluxconserve = "yes"

    if verbose:
        print("IRAF gregister ...")

    regblabla = iraf.gregister(input='%s[%s]' % (filepath, hdu),
                               output=alifilepath,
                               database=geodatabasepath,
                               transform="broccoli",
                               Stdout=1)
    # not used?

    if verbose:
        print("IRAF gregister done !")

    if os.path.isfile(geomapinpath):
        os.remove(geomapinpath)
    if os.path.isfile(geodatabasepath):
        os.remove(geodatabasepath)

    if makepng:
        try:
            import f2n
        except ImportError:
            print("Couldn't import f2n -- install it !")
            return
        myimage = f2n.fromfits(alifilepath, verbose=False)
        myimage.setzscale("auto", "auto")
        myimage.makepilimage("log", negative=False)
        myimage.writetitle(os.path.basename(alifilepath))
        if not os.path.isdir(outdir):
            os.makedirs(outdir)
        myimage.tonet(
            os.path.join(outdir, os.path.basename(alifilepath) + ".png"))
Esempio n. 24
0
def align_direct_to_reference(verbose=True, n_iter=20, drizzled_image=True):
	"""
	Use iraf software geomap to get shift solutions between images.

	n_iter = number of times to iterate over the alignment routines
	drizzled_image = boolean to distinguish between a drizzled image used for alignment,
	                 in which case the shifts must be transferred back to oiginal flt.
	"""

	### get the root name:
	root = wfc3_grism.options['ROOT_DIRECT']

	### get the alignment image which has been produced
	### with run_sregister_to_cutout_CANDELS_region()
	align_image = '%s_align_reference.fits' %(root)

	### now run SExtractor on the direct and refereance
	### images to build up 2 catalogs:
	se = wfc3_grism.sex.SExtractor()
	se.aXeParams()
	se.copyConvFile()
	se.overwrite = True
	se.options['CHECKIMAGE_TYPE'] = 'NONE'
	se.options['FILTER']    = 'Y'
	se.options['DETECT_THRESH']    = '%.1f' % wfc3_grism.options['ALIGN_DETECT_THRESH'] 
	se.options['ANALYSIS_THRESH']  = '3' 
	se.options['MAG_ZEROPOINT'] = '%.2f' % wfc3_grism.options['MAG_ZEROPOINT']
	se.options['DETECT_MINAREA'] = '%.1f' % wfc3_grism.options['ALIGN_DETECT_MINAREA'] 

	### generate the direct image catalog:
	se.options['CATALOG_NAME']    = 'direct.cat'
	iraf.imcopy('%s_drz.fits[SCI]' %(root), "SCI.fits", verbose=False)

	## if not using drizzled image for alignment done't include
	### a weight image:
	if drizzled_image:
		se.options['WEIGHT_TYPE']     = 'MAP_WEIGHT'
		se.options['WEIGHT_IMAGE']    = 'WHT.fits'
		iraf.imcopy('%s_drz.fits[WHT]' %(root), "WHT.fits", verbose=False)
	else:
		se.options['WEIGHT_TYPE']     = 'NONE'
		se.options['WEIGHT_IMAGE']    = 'WHT.fits'

	status = se.sextractImage('SCI.fits')

	### generate the alignment image catalog:
	se.options['CATALOG_NAME']    = 'align.cat'
	se.options['WEIGHT_TYPE']     = 'NONE'
	se.options['WEIGHT_IMAGE']    = 'WHT.fits'
	status = se.sextractImage(align_image)

	### Read the catalogs
	direct_cat = wfc3_grism.sex.mySexCat('direct.cat')
	align_cat = wfc3_grism.sex.mySexCat('align.cat')

	### initialize x,y shift parameters so can be 
	### updated with each iteration, the x,y shifts 
	### will converge twoard an optimal value over the
	### iterations:
	xshift = 0
	yshift = 0
	rot = 0
	scale = 1.

	### empty line in the output to make things clearer:
	print ""

	### now loop through the process until xrms and yrms both < 0.5
	### or run out of number of iterations:
	xrms, yrms = 100, 100
	toler = wfc3_grism.options['ALIGN_TOLERANCE']
	iteration = 0
	max_iter = wfc3_grism.options['ALIGN_ITERATIONS']
	while ((xrms > 0.1) | (yrms > 0.1)) & (iteration <= max_iter):

		print "Running matching algorithm on iteration #%d" %(iteration)

		### Get x,y coordinates of detected objects direct image
		fp = open('direct.xy','w')
		for i in range(len(direct_cat.X_IMAGE)):
			fp.write('%-15s%-15s\n' %(direct_cat.X_IMAGE[i],direct_cat.Y_IMAGE[i]))
		fp.close()

		### Get x,y coordinates of detected objects alignment image
		fp = open('align.xy','w')
		for i in range(len(align_cat.X_IMAGE)):
			fp.write('%-15s%-15s\n' %(np.float(align_cat.X_IMAGE[i])+xshift, np.float(align_cat.Y_IMAGE[i])+yshift))
		fp.close()

		### iraf flpr()
		wfc3_grism.utils.iraf_flpr()

		### remove previous solution:
		if iteration > 0:
			os.remove('align.match')

		### get the alignment catalog:
		status = iraf.xyxymatch(input="direct.xy", 
								reference="align.xy",
								output="align.match",
								tolerance=2**toler, 
								separation=0, 
								verbose=True, 
								Stdout=1)

		### iraf flpr()
		wfc3_grism.utils.iraf_flpr()

		### now get shifts with geomap:
		if iteration > 0:
			os.remove('align.map')

		iraf.geomap(input="align.match", 
					database="align.map",
					fitgeometry="shift", 
					interactive=False, 
					xmin=iraf.INDEF, 
					xmax=iraf.INDEF, 
					ymin=iraf.INDEF, 
					ymax=iraf.INDEF,
					maxiter = 10, 
					reject = 2.0, 
					Stdout=1)

		### get the output from geomap:
		fp = open("align.map", "r")
		for line in fp.readlines():
			spl = line.split()
			if spl[0].startswith('xshift'):
				xshift += float(spl[1])    
			if spl[0].startswith('yshift'):
				yshift += float(spl[1])    
			if spl[0].startswith('xrotation'):
				rot = float(spl[1])    
			if spl[0].startswith('xmag'):
				scale = float(spl[1])    
			if spl[0].startswith('xrms'):
				xrms = float(spl[1])    
			if spl[0].startswith('yrms'):
				yrms = float(spl[1])    
		fp.close()

		### update iteration counter:
		iteration += 1
		toler += 1

		print 'Shift iteration #%d, xshift=%f, yshift=%f, rot=%f, scl=%f (rms: %5.2f,%5.2f)' %(iteration, xshift, yshift, rot, scale, xrms, yrms)

	### copy the final align.map for posterity:
	shutil.copy('align.map', '%s_align.map' %(root))

	## cleanup from the alignment process:
	remvfiles = ['SCI.fits','WHT.fits','align.cat',
				'align.map','align.match','align.reg','align.xy',
				'direct.cat','direct.reg','direct.xy',
				'drz_sci.fits','drz_wht.fits','bg.fits', 'imxymatch.1', 'sex_stderr',
				'wfc3_grism_auto.sex', 'wfc3_grism_auto.param', 'default.nnw', 'default.conv']

	for file in remvfiles:
		try:
			os.remove(file)
		except:
			pass

	if drizzled_image:
		#### shifts measured in drz frame.  Translate to the flt frame:
		drz = fits.open('%s_drz.fits' %(root))

		#### Get reference angle from first image in the ASN file:
		asn = wfc3_grism.utils.ASNFile('%s_asn.fits' %(root))
		alpha = (180. - fits.getheader(asn.exposures[0]+'_flt.fits', 1)['PA_APER']) / 360. * 2 * np.pi

		### Get the drizzle scale from the MultiDrizzle '.run' file:
		for line in open('%s.run' %(root),'r'):
			if line.startswith('drizzle.scale'):
				drizzle_scale = line.split()[2]

		print drizzle_scale

		### get the equivalent shifts in the FLT frames:
		xsh = (xshift*np.cos(alpha) - yshift*np.sin(alpha))*np.float(drizzle_scale)
		ysh = (xshift*np.sin(alpha) + yshift*np.cos(alpha))*np.float(drizzle_scale)

		print 'Final shift:', xsh, ysh, drz[1].header['PA_APER']
	else:
		xsh = xshift
		ysh = yshift

	fp = open('%s_align.info' %(root),'w')
	fp.write('%s %8.3f %8.3f %8.3f\n' %(align_image, xsh, ysh, rot)) 
	fp.close()

	#### Read the shiftfile
	if drizzled_image:

		shiftF = ShiftFile('%s_initial_shifts.txt' %(root))

		shiftF.xshift = list(np.array(shiftF.xshift)-xsh)
		shiftF.yshift = list(np.array(shiftF.yshift)-ysh)
		shiftF.rotate = list((np.array(shiftF.rotate)+rot) % 360)
		shiftF.scale = list(np.array(shiftF.scale)*scale)

		shiftF.write('%s_final_shifts.txt' %(root))

	else:
		### use the default shift file in wfc3_grism data folder:
		shiftF = ShiftFile('/disk1/fc/FIGS/wfc3_grism/data/default_shift_file.txt')

		### add the reference image as needed by multidrizzle:
		shiftF.headerlines[1] = '# refimage: %s \n' %(align_image)

		#### Apply the alignment shifts to the shiftfile
		shiftF.xshift = [np.array(xsh)]
		shiftF.yshift = [np.array(ysh)]
		shiftF.rotate = [np.array(rot) % 360]
		shiftF.scale = [np.array(scale)]

		shiftF.write('%s_final_shifts.txt' %(root))
Esempio n. 25
0
def stacking(cllist,zpofflist,ref,zprefoff=0.0,stackname='stack',shiftsize=400):

    """
    """

    #Reset the IRAF tasks used in this routine.
    iraf.unlearn('imcalc')
    iraf.unlearn('imcombine')
    iraf.unlearn('imreplace')
    iraf.unlearn('xyxymatch')
    iraf.unlearn('geomap')
    iraf.unlearn('geotran')
    iraf.unlearn('imcopy')

    #Find reference image in reference directory. Check to make
    #sure that it is actually the image and not the mask file!
    #Grab the mask for adding to the mask list now.
    (refimg,refmask,expmap)=classify(ref+'/tu*.fits')
    zpref=pf.open(refimg)[0].header['MAGZERO']
#    zprefoff=NewfirmZPoffset[ref.split('/')[-1]]
    zprefoff=float(zprefoff)

    #Get 2MASS PSC positions for reference cluster image.
    catalog=get2masspsc(refimg)
    foo=file_check(ref+'/2mass_ref_stars.cdt',delete=True)
    foo=open(ref+'/2mass_ref_stars.cdt','w')
    for y in catalog:
        data=y.split()
        foo.write(data[6]+'\t'+data[7]+'\n')
    foo.close()

    #Create lists for files to be input into the stacking routine.
    foo=file_check('matchlist',delete=True)
    foo=file_check('scalelist',delete=True)
    foo=file_check('shiftlist',delete=True)
    foo=file_check('masklist',delete=True)
    foo=file_check('shiftmask',delete=True)
    foo=file_check('expmaplist',delete=True)
    (matchlist,scalelist,shiftlist,masklist,
     shiftmask,finalmasks,stacklist,stackmask,
     finalmasks2,expmaplist,shiftexp,expmaplist2)=(open('matchlist','w'),open('scalelist','w'),
                                        open('shiftlist','w'),open('masklist','w'),
                                        open('shiftmask','w'),open('finalmasks','w'),
                                        open('stacklist','w'),open('stackmask','w'),
                                        open('finalmasks2','w'),open('expmaplist','w'),
                                        open('shiftexp','w'),open('expmaplist2','w'))
    (xsize,ysize)=(np.array([]),np.array([]))
    
    
    #Step through all of the input cluster directories.
    i=0
    for x in cllist:
        #Find the image, mask, and exposure map files. Get zeropoints and
        #scale image to the reference image.
        scaleimg=x+'/scaled_to_'+ref.split('/')[-1]+'.fits'
        foo=file_check(scaleimg,delete=True)
        (img,mask,expmap)=classify(x+'/tu*.fits')
        imgzp=pf.open(img)[0].header['MAGZERO']
        (xs,ys)=(pf.open(img)[0].header['NAXIS1'],pf.open(img)[0].header['NAXIS2'])
        (xsize,ysize)=(np.append(xsize,xs),np.append(ysize,ys))

        imgzpoff=float(zpofflist[i])
#        imgzpoff=NewfirmZPoffset[x.split('/')[-1]]
        scale=scalecounts(imgzp+imgzpoff,zpref+zprefoff)
        iraf.imcalc(img,scaleimg,'im1*'+str(scale))

        #Get X,Y pixel positions of 2MASS sources from the 2MASS PSC
        #in the image. Use these to compute shifts relative to the
        #reference image using IRAF task geomap.
        foo=file_check(x+'/2mass_ref_stars.cdt',delete=True)
        foo=open(x+'/2mass_ref_stars.cdt','w')
        catalog=get2masspsc(scaleimg)
        for y in catalog:
            data=y.split()
            foo.write(data[6]+'\t'+data[7]+'\n')
        foo.close()
    
        #Match the 2MASS PSC positions with stars in the reference
        #image using xyxymatch. The matched source list is then fed
        #into geomap to get the X and Y shifts.
        foo=file_check(x+'/2mass_matched.cdt',delete=True)
        iraf.xyxymatch(x+'/2mass_ref_stars.cdt',ref+'/2mass_ref_stars.cdt',
                       x+'/2mass_matched.cdt','200.0',verbose='no')

        #Append all of the names of the files for the input and output filename
        #lists to be passed to IRAF tasks further down the line.
        matchlist.write(x+'/2mass_matched.cdt\n')
        scalelist.write(scaleimg+'\n')
        foo=file_check(x+'/scaled_and_shifted.fits',delete=True)
        shiftlist.write(x+'/scaled_and_shifted.fits['+str(shiftsize)+':'+\
                 str(int(np.max(xsize))+shiftsize)+','+str(shiftsize)+':'+\
                 str(int(np.max(ysize))+shiftsize)+']\n')
        stacklist.write(x+'/scaled_and_shifted.fits\n')
        file_check(x+'/mask_tmp.fits',delete=True)
        file_check(x+'/expmap_tmp.fits',delete=True)
        iraf.imarith(mask+'[1]','*',1000.0,x+'/mask_tmp.fits',pixtype='real')
        iraf.imarith(expmap+'[1]','*',1.0,x+'/expmap_tmp.fits',pixtype='real')
        offset=2.558435
        file_check(x+'/mask_tmp2.fits',delete=True)
        iraf.imcalc(x+'/mask_tmp.fits',x+'/mask_tmp2.fits','im1+'+str(offset))
        os.remove(x+'/mask_tmp.fits')
        masklist.write(x+'/mask_tmp2.fits\n')
        file_check(x+'/mask_shift.fits',delete=True)
        shiftmask.write(x+'/mask_shift.fits['+str(shiftsize)+':'+\
                    str(int(np.max(xsize))+shiftsize)+','+str(shiftsize)+':'+\
                    str(int(np.max(ysize))+shiftsize)+']\n')
        stackmask.write(x+'/mask_shift.fits\n')
        finalmasks.write(x+'/mask_final.fits\n')
        finalmasks2.write(x+'/mask_final.fits[0]\n')
        expmaplist.write(x+'/expmap_tmp.fits[0]\n')
        shiftexp.write(x+'/expmap_shift.fits['+str(shiftsize)+':'+\
                    str(int(np.max(xsize))+shiftsize)+','+str(shiftsize)+':'+\
                    str(int(np.max(ysize))+shiftsize)+']\n')
        expmaplist2.write(x+'/expmap_shift.fits\n')
        i += 1

    #Close all of the input and output filename lists to be passed to IRAF tasks.
    matchlist.close()
    scalelist.close()
    stacklist.close()
    masklist.close()
    shiftmask.close()
    finalmasks.close()
    shiftlist.close()
    stackmask.close()
    finalmasks2.close()
    expmaplist.close()
    expmaplist2.close()
    shiftexp.close()

    #Get the shifts between all input files (including the reference) and the
    #reference image itself.
    foo=file_check('shift.db',delete=True)
    iraf.geomap('@matchlist','shift.db',1.0,np.max(xsize),
                1.0,np.max(ysize),fitgeometry='shift',interactive='no',
                maxiter=2,function='legendre',verbose='no')

    #Shift the input images (including the reference) and associated mask files
    #to a common pixel grid. Add some padding around the individual frames (-99
    #in the images, 1 in the bad pixel masks) to ensure that the images will
    #combine properly.
    (maxx,maxy)=(np.max(xsize)+shiftsize+100.0,np.max(ysize)+shiftsize+100.0)
    iraf.geotran('@scalelist','@shiftlist','shift.db','@matchlist',geometry='linear',
                 boundary='constant',nlines=maxy,ncols=maxx,constant=-99.0)

    iraf.geotran('@masklist','@shiftmask','shift.db','@matchlist',geometry='linear',
                 boundary='constant',nlines=maxy,ncols=maxx,constant=1000.0,
                 nxblock=10000,nyblock=10000)
    
    iraf.geotran('@expmaplist','@shiftexp','shift.db','@matchlist',geometry='linear',
                 boundary='constant',nlines=maxy,ncols=maxx,constant=0.)

    for x in cllist:
        file_check(x+'/mask_final.fits',delete=True)
        shutil.copy(x+'/mask_shift.fits',x+'/mask_final.fits')
        iraf.hedit(x+'/scaled_and_shifted.fits[0]','BPM',x+'/mask_final.fits[0]',
                   add='yes',update='yes',verify='no')
    iraf.imreplace('@finalmasks2',0,upper=offset)
    iraf.imreplace('@finalmasks2',1,lower=offset)

    file_check(stackname,delete=True)
    file_check(stackname[:-5]+'_mask.pl',delete=True)
    file_check(stackname[:-5]+'_expmap.fits',delete=True)
    iraf.imcombine('@stacklist',stackname,bpmasks=stackname[:-5]+'_bpm',
                   masktype='goodval',reject='none',mclip='yes',lthresh='INDEF',hthresh='INDEF',
                   hsigma=10.0,lsigma='INDEF',nrejmasks=stackname[:-5]+'_nrej',
                   sigmas=stackname[:-5]+'_sigma',grow=2.5,nkeep=1,blank=-99.0,gain=8.0,rdnoise=35.0)
    iraf.imcombine('@expmaplist2',stackname[:-5]+'_expmap.fits',combine='sum')
    hdu=pf.open(stackname,mode='update')
    hdu[0].header['BPM']=stackname.split('/')[-1][:-5]+'_mask.pl'
    hdu[0].header['MAGZERO']=zpref+zprefoff
    hdu.close()

	#Fix the WCS information in the stacked image.
    copyhead(stackname,refimg,offset=shiftsize)
    applywcs(stackname,stackname[:-5]+'_wcs.fits')

    trash=['matchlist','scalelist','shiftlist','masklist','shiftmask','finalmasks',
            'shift.db','stacklist','finalmasks2','stackmask','tmp_wcs.fits','expmaplist',
            'expmaplist2','shiftexp']
    for x in trash:
        os.remove(x)
Esempio n. 26
0
def kaitsub(refimage,
            inlist,
            kernel=9,
            satval=50000.0,
            dspace=0,
            dbkg=0,
            nstamps=2,
            clobber=globclob):

    infiles = iraffiles(inlist)

    # RA and Dec of center pixel in reference image
    [nx, ny] = get_head(refimage, ['NAXIS1', 'NAXIS2'])
    xcenpix = (nx / 2.0)
    ycenpix = (ny / 2.0)
    [[ra, dec]] = impix2wcs(refimage, xcenpix, ycenpix)

    # Big loop
    for image in infiles:

        # Separate image rootname from extension
        root, ext = image.split('.')

        # Make sure WCS fit was successful
        if not check_head(image, 'IQWCS'):
            print 'Skipping image %s: IQWCS not successful' % image
            infiles.remove(image)
            continue

        # Remap image using objects detected on both frames and
        # create stamp list
        stars = Starlist(get_head(image, 'STARFILE'))
        refstars = Starlist(get_head(refimage, 'STARFILE'))
        refstars.pix2wcs(refimage)
        refstars.wcs2pix(image)
        match, refmatch = stars.match(refstars, useflags=yes, tol=10.0)
        nstars = len(match)
        if not (nstars > 2):
            print 'Could not find star matches between reference and %s' % image
            infiles.remove(image)
            continue
        refmatch.pix2wcs(image)
        refmatch.wcs2pix(refimage)
        matchfile = open('%s.match' % root, 'w')
        stampfile = open('%s.stamps' % root, 'w')
        for i in range(len(match)):
            matchfile.write('%10.3f%10.3f%10.3f%10.3f\n' %
                            (refmatch[i].xval, refmatch[i].yval, match[i].xval,
                             match[i].yval))
            stampfile.write('%10.3f%10.3f\n' %
                            (refmatch[i].xval, refmatch[i].yval))
        matchfile.close()
        stampfile.close()
        check_exist('%s.geodb' % root, 'w', clobber=clobber)
        iraf.geomap('%s.match' % root,
                    '%s.geodb' % root,
                    1.0,
                    nx,
                    1.0,
                    ny,
                    fitgeom="general",
                    verbose=no,
                    interactive=no)
        check_exist('%s.shift.fits' % root, 'w', clobber=clobber)
        iraf.geotran(image,
                     '%s.shift' % root,
                     '%s.geodb' % root,
                     '%s.match' % root,
                     geometry="geometric",
                     boundary="constant",
                     verbose=no)

        # Run the subtraction
        check_exist('%s.sub.fits' % root, 'w', clobber=clobber)
        if os.path.exists("stamps.lis"):
            cmd = '$REDUCTION/hotpants -inim %s.shift.fits -tmplim %s -outim %s.sub.fits -tu %.2f -tg %.2f -tr %.2f -iu %.2f -ig %.2f -ir %.2f -r %.2f -ssf stamps.lis -afssc 0 -n t -ko %i -bgo %i -savexy %s.st -nsx %i -nsy %i' % (
                root, refimage, root, satval, AP_GAIN, AP_READN, satval,
                FL_GAIN, FL_READN, kernel, dspace, dbkg, root, nstamps,
                nstamps)
        else:
            cmd = '$REDUCTION/hotpants -inim %s.shift.fits -tmplim %s -outim %s.sub.fits -tu %.2f -tg %.2f -tr %.2f -iu %.2f -ig %.2f -ir %.2f -r %.2f -ssf %s.stamps -afssc 0 -n t -ko %i -bgo %i -savexy %s.st -nsx %i -nsy %i' % (
                root, refimage, root, satval, AP_GAIN, AP_READN, satval,
                FL_GAIN, FL_READN, kernel, root, dspace, dbkg, root, nstamps,
                nstamps)
            #cmd = '$REDUCTION/pois -k %i %i -s %.2f %.2f -S %s.stamps %s %s.shift.fits %s.sub.fits' % (kernel,kernel,satval,satval,root,refimage,root,root)
        fcmd = os.popen(cmd, 'r')
        sublines = fcmd.readlines()
        fcmd.close()

    # Return
    print 'Exiting successfully'
    return