def xym2bar(run, year, filt, suffix='', Nepochs=1, camera='c9',
            zeropoint='z0', clobber=False, ref_xym2bar=None):
    """
    Run xym2bar on the contents of the directory given by:
    <year>_<filt><suffix>/
        00.DATA/
        01.XYM/
    """
    os.chdir('{0}_{1}{2}/01.XYM'.format(year, filt, suffix))

    program = 'xym2bar'

    _log = open('flystar_xym2bar.log', 'w')
    
    # Clean up some old files if necessaary
    if clobber:
        logger(_log, '*** Deleting old IN.* TRANS.* MAT.* and MATCHUP.XYMEEE files')
        old_files = ['IN.xym2bar',
                     'TRANS.xym2bar',
                     'MATCHUP.XYMEEE']
        fileUtil.rmall(old_files)

    # Fetch all the *.xym files we will be working with
    xym_files = glob.glob('*.xym')
    Nxym = len(xym_files)

    # Make the camera and magnitude limits into arrays if they aren't already.
    if (hasattr(camera, '__iter__') == False) or (len(camera) < Nxym):
        camera = np.repeat(camera, Nxym)

    if (hasattr(zeropoint, '__iter__') == False) or (len(zeropoint) < Nxym):
        zeropoint = np.repeat(zeropoint, Nxym)

    try: 
        ##########
        # xym2bar -- Make IN.xym2bar
        ##########
        logger(_log, '*** Writing IN.xym2bar')

        in_fmt = '{idx:03d} "{ff}" {cam} {zp}\n'
        f_in_bar = open('IN.xym2bar', 'w')
        if ref_xym2bar != None:
            f_in_bar.write( in_fmt.format(idx=0, ff=ref_xym2bar, cam='', zp='z0') )
        for ii in range(Nxym):
            f_in_bar.write( in_fmt.format(idx=ii+1, ff=xym_files[ii], cam=camera[ii],
                                          zp=zeropoint[ii]) )
        f_in_bar.close()

        ##########
        # xym2bar -- Run
        ##########
        cmd = ['xym2bar', str(Nepochs)]
        if ref_xym2bar != None:
            cmd += ['I']
            
        logger(_log, '*** Running:  \n' + ' '.join(cmd))

        subprocess.call(cmd, stdout=_log, stderr=_log)

        ##########
        # Copy files over.
        ##########
        logger('*** Copying files to <file>.{0}'.format(run))
        shutil.copy("IN.xym2bar", "IN.xym2bar." + run)
        shutil.copy("TRANS.xym2bar", "TRANS.xym2bar." + run)
        shutil.copy("MATCHUP.XYMEEE", "MATCHUP.XYMEEE." + run)
        

    finally:
        os.chdir('../../')
        _log.close()


    return
def xym2mat(run, year, filt, suffix='', ref=None,
            ref_camera='c9', ref_mag='',
            camera='c9', mag='',
            clobber=False):
    """
    Run xym2mat data in the specified directory, <year>_<filter><suffix>. 
    There is a specific directory structure that is expected within <dir>:

    00.DATA/
    01.XYM/
    """
    os.chdir('{0}_{1}{2}/01.XYM'.format(year, filter, suffix))

    program = 'xym2mat'

    # Clean up some old files if necessaary
    if clobber:
        print('*** Deleting old IN.* TRANS.* MAT.* and MATCHUP.XYMEEE files\n')
        old_files = ['IN.xym2mat', 'TRANS.xym2mat']
        fileUtil.rmall(old_files)
        fileUtil.rmall( glob.glob('MAT.*') )

    # Fetch all the *.xym files we will be working with
    xym_files = glob.glob('*.xym')
    Nxym = len(xym_files)

    # Make the camera and magnitude limits into arrays if they aren't already.
    if (hasattr(camera, '__iter__') == False) or (len(camera) < Nxym):
        camera = np.repeat(camera, Nxym)

    if (hasattr(mag, '__iter__') == False) or (len(mag) < Nxym):
        mag = np.repeat(mag, Nxym)

    # Keep a log file
    _log = open('flystar_xym2mat.log', 'w')

    try: 
        ########## 
        # xym2mat -- IN.xym2mat
        ##########
        logger(_log, '*** Writing IN.xym2mat')
        
        f_in_mat = open('IN.xym2mat', 'w')

        in_fmt = '{idx:03d} "{ff}" {cam}'
        if mag[0] != None:
            in_fmt += ' "{mag}"'
        in_fmt += '\n'
    
        # Build the reference epoch line for xym2mat
        if ref == None:
            ref = xym_files[0]
            ref_camera = camera[0]
            ref_mag = mag[0]

        f_in_mat.write( in_fmt.format(idx=0, ff=ref, cam=ref_camera, mag=ref_mag) )
        for ii in range(Nxym):
            f_in_mat.write( in_fmt.format(idx=ii+1, ff=xym_files[ii], cam=camera[ii], mag=mag[ii]) )
        f_in_mat.close()

        ##########
        # xym2mat -- Run
        ##########
        logger(_log, '*** Calling xym2mat')

        # First call a little more open
        cmd = ['xym2mat', '20']
        logger(_log, '*** Running:')
        logger(_log, ' '.join(cmd))
        subprocess.call(cmd, stdout=_log, stderr=_log)

        # Second call a little more constrained
        cmd[1] = '22'
        logger(_log, 'Running:')
        logger(_log, ' '.join(cmd))
        subprocess.call(cmd, stdout=_log, stderr=_log)

        ##########
        # Copy files over.
        ##########
        logger('*** Copying files to <file>.{0}'.format(run))
        shutil.copy("IN.xym2mat", "IN.xym2mat." + run)
        shutil.copy("TRANS.xym2mat", "TRANS.xym2mat." + run)
        

    finally:
        os.chdir('../../')
        _log.close()