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 cat_match_sky_SpARCS0035(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 arcseconds

    OPTIONAL KEYWORD PARAMETERS

    fullcat_trans:  if set ==1 then transform full catalog

    speczcat_trans:  if set ==1 then transform the spectroscopic catalog

    '''

    #read in the z-band GOGREEN catalog and the reference catalog.
    clustname = 'SpARCS0035'
    (gg_dat, ref_dat, gg_catname, ref_catname, zcat_dat, zcat,
     speczcatname) = cat_read_SpARCS0035()
    #print(gg_dat)

    #rename inputs to make code more readable
    raref = np.array(ref_dat['ALPHA_SKY'])
    decref = np.array(ref_dat['DELTA_SKY'])
    rain = gg_dat['RA']
    decin = gg_dat['DEC']

    #match catalogs against each other
    #return matched values
    #catpath = '/Users/grudnick/Work/GOGREEN/Catalogs/Astrometric/SpARCS0035'
    catpath = '.'
    geomap_infile = catpath + '/geomap_coords_sky.' + clustname + '.in'
    (rarefm, decrefm, rainm, decinm,
     lims) = cmts.cat_sky_match(raref,
                                decref,
                                rain,
                                decin,
                                septol,
                                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_sky(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_sky.pdf'
    cmts.match_diff_sky_plot(rarefm,
                             decrefm,
                             rainm,
                             decinm,
                             plotfile=pretrans_plotfile)

    if 'fullcat_trans' in kwargs.keys():
        if kwargs['fullcat_trans'] == 1:
            cat_trans_sky(gg_catname, dbfile, geomap_infile, ref_catname, clustname, septol, \
                      ramin = lims['ramin'], ramax = lims['ramax'], decmin = lims['decmin'], \
                      decmax = lims['decmax'])

    if 'speczcat_trans' in kwargs.keys():
        if kwargs['speczcat_trans'] == 1:
            speczcat_trans_sky(speczcatname, dbfile, geomap_infile, ref_catname, clustname, septol, \
                      ramin = lims['ramin'], ramax = lims['ramax'], decmin = lims['decmin'], \
                      decmax = lims['decmax'])
def speczcat_trans_sky(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 writes a new catalog with  old coordinates replaced by the new ones.

    INPUT

    incat: the input fits 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 arcseconds

    OPTIONAL KEYWORDS

    ramin, ramax, decmin, decmax.  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 the spectroscopic redshift catalog
    cat_hdul = fits.open(incat)
    cat_dat = cat_hdul["MDF"].data

    #read in original astrometric catalog
    ref_dat = ascii.read(refcat)
    raref = np.array(ref_dat['ALPHA_SKY'])
    decref = np.array(ref_dat['DELTA_SKY'])

    #tmp coordinate files for geoxytran
    tmpin = 'tmp_geoxytran_sky_in'
    tmpout = 'tmp_geoxytran_sky_out'

    #remove the existing transformed file
    newcat = incat.replace('.fits', '.trans.fits')

    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("# ra dec\n")
    for i, val in enumerate(cat_dat['RA']):
        #convert speczcat RA to degrees when writing out
        fo.write('{} {}\n'.format(cat_dat['RA'][i] * 15.0, cat_dat['DEC'][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)
    ratrans = np.array(trans_dat['ra'])
    dectrans = np.array(trans_dat['dec'])

    #replace the old RAs and DECs with new RAs and DECs in place
    tcat_hdul = cat_hdul
    #convert RA back to hours for output
    tcat_hdul["MDF"].data['RA'] = ratrans / 15.0
    tcat_hdul["MDF"].data['DEC'] = dectrans

    #write the new fits file
    tcat_hdul.writeto(newcat)

    #match new catalog against original catalog
    mfile = "speczcat_sky_match.txt"
    (rarefm,decrefm,ratransm,dectransm, translims) = cmts.cat_sky_match(raref, decref, \
                                                                       ratrans, dectrans, septol, \
                                                                       matchfile = mfile)

    #make a plot of the residuals
    allcattrans_plotfile = 'speczcat_trans.' + clustname + '_coordiff_sky.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 given
    #that all are given
    if 'ramin' in kwargs.keys():
        cmts.match_diff_sky_plot(rarefm,decrefm,ratransm,dectransm, plotfile = allcattrans_plotfile, \
                            ramin = kwargs['ramin'], ramax = kwargs['ramax'], \
                            decmin = kwargs['decmin'], decmax = kwargs['decmax'])
    else:
        cmts.match_diff_sky_plot(rarefm,
                                 decrefm,
                                 ratransm,
                                 dectransm,
                                 plotfile=allcattrans_plotfile)