def changeSegment(filename, detector, segment):
    """Replace '_a' with '_b' or vice versa, if appropriate.

    This was written for auto/GO wavecal file names for FUV data.  Wavecals
    are processed from the x1d file, and the name of the raw file is for
    the first segment in the input list (which will be FUVA if both segments
    are present).  When calibrating segment B data, the name or names of
    the wavecal files need to be changed to end in "_b.fits" instead of
    "_a.fits".

    Parameters
    ----------
    filename: str
        One or more file names, separated by spaces

    detector: str
        FUV or NUV

    segment: str
        FUVA or FUVB, if detector is FUV

    Returns
    -------
    str
        A copy of the input filename, but with '_a' replaced with '_b'
        or vice versa, or no change if the input name does not end in
        '_a.fits' or '_b.fits'
    """
    if detector != 'FUV':
        return filename
    if segment == 'FUVB':
        names = filename.split()
        new_names = []
        for name in names:
            if name.endswith('_a.fits'):
                n = len(name) - 7
                name = name[:n] + '_b.fits'
            new_names.append(name)
        filename = ' '.join(new_names)
    elif segment == 'FUVA':
        names = filename.split()
        new_names = []
        for name in names:
            if name.endswith('_b.fits'):
                n = len(name) - 7
                name = name[:n] + '_a.fits'
            new_names.append(name)
        filename = ' '.join(new_names)
    return filename
