def cat_trans_im(incat, dbfile, geomap_infile, refcat, clustname, septol,  **kwargs):

    '''This routine reads in a FITS catalog, writes a temporary output
    ASCII catalog, then transforms this catalog using geoxytran.  It
    then appends these new coordinates as new columns to the original catalog.

    It is usually run by hand after the geomap solution has been derived.

    INPUT

    incat: the input SExtractor catalog

    dbfile: the geomap database file

    geomap_infile: used as the database record

    refcat: the original astrometric reference catalog

    clustname: the name of the cluster

    septol: the maximum separation allowed for a match in pixels

    OPTIONAL KEYWORDS

    xmin, xmax, ymin, ymax.  These are the limits over which the
    transform was originally computed.  If these are given then it
    uses those limits to color the points in the ra and decdiff plots.
    If one is given, all must be given.

    '''

    #read in GMOS sextractor photometry catalog with (x,y) coordinates
    cat_dat = ascii.read(incat)

    #read in original astrometric catalog
    ref_dat = ascii.read(refcat)
    xref = np.array(ref_dat['X_IMAGE'])
    yref = np.array(ref_dat['Y_IMAGE'])

    #tmp coordinate files for geoxytran
    tmpin = 'tmp_geoxytran_im_in'
    tmpout = 'tmp_geoxytran_im_out'

    #make a new name for the transformed file
    newcat = incat.replace('.sexcat','.trans.cat')

    if os.path.isfile(tmpout) is True:
        cmdstr = 'rm ' + tmpout
        os.system(cmdstr)
        cmdstr = 'rm ' + newcat
        os.system(cmdstr)

    #output temporary ASCII file with coordinates
    fo = open(tmpin, "w")
    fo.write("# x y\n")
    for i,val in enumerate(cat_dat['X_IMAGE']):
        fo.write('{} {}\n'.format(cat_dat['X_IMAGE'][i],cat_dat['Y_IMAGE'][i]))
    fo.close()

    iraf.geoxytran(tmpin, tmpout, dbfile, geomap_infile, direction="backward",\
                   xcolumn=1, ycolumn = 2)

    #read in ascii output file
    trans_dat = ascii.read(tmpout)
    xtrans = np.array(trans_dat['x'])
    ytrans = np.array(trans_dat['y'])

    #replace the old RAs and DECs with new RAs and DECs in place    
    tcat_dat = cat_dat
    tcat_dat['X_IMAGE'] = xtrans
    tcat_dat['Y_IMAGE'] = ytrans

    #write the new catalog file with the transformed coordinates
    ascii.write(tcat_dat, newcat, format='commented_header')
    
    #match new catalog against original catalog
    mfile = "allcat_im_match.txt"
    (xrefm,yrefm,xtransm,ytransm, translims) = cmti.cat_im_match(xref, yref, \
                                                                 xtrans, ytrans, septol, \
                                                                 matchfile = mfile)

    #make a plot of the residuals
    allcattrans_plotfile = 'allcat_trans.' + clustname + '_coordiff_im.pdf'
    #passes ra and dec limits if they are defined to find source
    #outside of ra and dec lims.  Assumes that if one keyword is givenSPT0205_GMOS_z.v0.sexcat'
    #that all are given
    
    if 'xmin' in kwargs.keys():
        cmti.match_diff_im_plot(xrefm,yrefm,xtransm,ytransm, plotfile = allcattrans_plotfile, \
                            xmin = kwargs['xmin'], xmax = kwargs['xmax'], \
                            ymin = kwargs['ymin'], ymax = kwargs['ymax'])
    else:
        cmti.match_diff_im_plot(xrefm,yrefm,xtransm,ytransm, plotfile = allcattrans_plotfile)
def cat_match_im_SPT0205(septol, **kwargs):

    '''Written by Gregory Rudnick 10 January 2018

    PURPOSE:

    Read in two catalogs and match them within some tolerance.  Write
    the set of matched coordinates out in a format suitable for geomap input

    Plot the differences in each coordinate.

    INPUT PARAMETERS:

    septol: the maximum separation allowed for a match in pixels (0.16asec/pix for GMOS)

    OPTIONAL KEYWORD PARAMETERS

    fullcat_trans:  if set ==1 then transform full catalog

    im_trans: if set==1 then transform image using geotran

    '''

    #read in the z-band GOGREEN catalog and the reference catalog.
    clustname = 'SPT0205'
    (gg_dat, ref_dat, gg_catname, ref_catname, zcat_dat, zcatname, initcoordfile) \
        = cat_read_SPT0205()
    #print(gg_dat)

    #rename inputs to make code more readable
    xref = np.array(ref_dat['X_IMAGE'])
    yref = np.array(ref_dat['Y_IMAGE'])
    xin = np.array(zcat_dat['X_IMAGE'])
    yin = np.array(zcat_dat['Y_IMAGE'])

    #match catalogs against each other
    #return matched values
    #catpath = '/Users/grudnick/Work/GOGREEN/Catalogs/Astrometric/SPT0205'
    catpath = '.'
    geomap_infile = catpath + '/geomap_coords_im.' + clustname + '.in'
    (xrefm,yrefm,xinm,yinm, lims) \
        = cmti.cat_im_match(xref, yref, xin, yin, septol, \
                            icfile = initcoordfile, matchfile = geomap_infile)

    print("limits of coordinates are",lims)

    #run geomap to compute the transformation and geoxytran to
    #transform the input coordinates using that solution.  This also
    #plots the transformed coordinates.
    (dbfile, geomap_infile) =  georun_im(clustname, lims)
    
    #make a plot of the residuals.  Although these are for the
    #pre-transform residuals, this needs to occur in the code before
    #the first instance of geomap, otherwise for some reason the code
    #crashes because pytplot is imported before geomap is executed.
    pretrans_plotfile = catpath + '/pretrans.' + clustname + '_coordiff_im.pdf'
    cmti.match_diff_im_plot(xrefm,yrefm,xinm,yinm, plotfile = pretrans_plotfile)

    if 'fullcat_trans' in kwargs.keys():
        if kwargs['fullcat_trans'] == 1:
            cat_trans_im(zcatname, dbfile, geomap_infile, ref_catname, clustname, septol, \
                         xmin = lims['xmin'], xmax = lims['xmax'], ymin = lims['ymin'], \
                         ymax = lims['ymax'])
            
    #transform image using geotran
    if 'im_trans' in kwargs.keys():
        if kwargs['im_trans'] == 1:
            (impath, refimpath) = preimage_read(clustname)
            imtrans(impath, refimpath, dbfile,geomap_infile,lims)
def cat_match_im(septol, **kwargs):
    '''Written by Gregory Rudnick 7 June 2018

    PURPOSE:

    Read in an input image, an input catalog with x-y coordinates, a
    reference catalog with x-y coordinates, and a list of x-y
    coordinates for three matched stars.  Then match the catalogs
    within some tolerance using an initial transformation derived from
    the three matched stars.  Use the transformation to transform the
    input image and the input catalog.

    Plot the differences in each coordinate.

    This version differs from the original GOGREEN file in that I've
    taken out all of the references to GOGREEN paths so it should be generic.

    INPUT PARAMETERS:

    septol: the maximum separation allowed for a match in pixels

    NEEDED INPUT FILES (enter in cat_read() subroutine) :

    input catalog with x and y coordinates

    reference catalog

    file with coordinates of three objects in the input and reference
    image.  This contains the x-y coordinates of 1-3 reference list
    tie points in the first line, followed by the x-y coordinates of
    the 1-3 corresponding input tie point in succeeding lines.



    OPTIONAL KEYWORD PARAMETERS

    im_trans: if set==1 then transform image using geotran

    '''

    #read in the z-band GOGREEN catalog and the reference catalog.
    #@@change@@ need to put in whatever cluster name you use.
    clustname = 'CL1216-1201'
    (ref_dat, gg_catname, incat_dat, incatname, initcoordfile) \
        = cat_read()

    #rename inputs to make code more readable
    xref = np.array(ref_dat['X_IMAGE'])
    yref = np.array(ref_dat['Y_IMAGE'])
    xin = np.array(incat_dat['X_IMAGE'])
    yin = np.array(incat_dat['Y_IMAGE'])

    #match catalogs against each other
    #return matched values
    #catpath = '/Users/grudnick/Work/GOGREEN/Catalogs/Astrometric/SPT2106'
    catpath = '.'
    geomap_infile = catpath + '/geomap_coords_im.' + clustname + '.in'
    (xrefm,yrefm,xinm,yinm, lims) \
        = cmti.cat_im_match(xref, yref, xin, yin, septol, \
                            icfile = initcoordfile, matchfile = geomap_infile)

    print("limits of coordinates are", lims)

    #make a plot of the residuals
    pretrans_plotfile = catpath + '/pretrans.' + clustname + '_coordiff_im.pdf'
    cmti.match_diff_im_plot(xrefm,
                            yrefm,
                            xinm,
                            yinm,
                            plotfile=pretrans_plotfile)

    #run geomap to compute the transformation and geoxytran to
    #transform the input coordinates using that solution.  This also
    #plots the transformed coordinates.
    (dbfile, geomap_infile) = georun_im(clustname, lims)

    #transform image using geotran
    if 'im_trans' in kwargs.keys():
        if kwargs['im_trans'] == 1:
            (impath, refimpath) = preimage_read()
            imtrans(impath, refimpath, dbfile, geomap_infile, lims)