def toMultiExtensionFits(waiveredObject,
                         multiExtensionFileName=None,
                         forceFileOutput=False,
                         verbose=False):
    """
        Convert the input waivered FITS object to a multi-extension FITS
        HDUList object.  Generate an output multi-extension FITS file if
        requested.

        Parameters:

          waiveredObject  input object representing a waivered FITS file;
                          either a astroyp.io.fits.HDUList object, a file object, or a
                          file specification

          outputFileName  file specification for the output file
                          Default: None - do not generate an output file

          forceFileOutput force the generation of an output file when the
                          outputFileName parameter is None; the output file
                          specification will be the same as the input file
                          specification with the last character of the base
                          name replaced with the character 'h'.
                          Default: False

          verbose         provide verbose output
                          Default: False

        Returns:

          mhdul           an HDUList object in multi-extension FITS format.

        Exceptions:

          TypeError       Input object is not a HDUList, a file object or a
                          file name
    """

    if isinstance(waiveredObject, fits.HDUList):
        whdul = waiveredObject
        inputObjectDescription = "HDUList object"
    else:
        try:
            whdul = fits.open(waiveredObject)
            if isinstance(waiveredObject, string_types):
                inputObjectDescription = "file " + waiveredObject
            else:
                inputObjectDescription = "file " + waiveredObject.name
        except TypeError:
            raise TypeError("Input object must be HDUList, file object, " + \
                            "or file name")

    _verify(whdul)

    undesiredPrimaryHeaderKeywords = ['ORIGIN','FITSDATE','FILENAME',
                                      'ALLG-MAX','ALLG-MIN','ODATTYPE',
                                      'SDASMGNU','OPSIZE','CTYPE2',
                                      'CD2_2','CD2_1','CD1_2','CTYPE3',
                                      'CD3_3','CD3_1','CD1_3','CD2_3',
                                      'CD3_2']
    #
    # Create the multi-extension primary header as a copy of the
    # wavered file primary header
    #
    mPHeader = whdul[0].header
    originalDataType =  whdul[0].header.get('ODATTYPE','')
    #
    # Remove primary header cards with keywords matching the
    # list of undesired primary header keywords
    #
    for keyword in undesiredPrimaryHeaderKeywords:
        #
        # Be careful only to delete the first card that matches
        # the keyword, not all of the cards
        #
        if keyword in mPHeader:
            del mPHeader[mPHeader.index(keyword)]
    #
    # Get the columns from the secondary HDU table
    #
    wcols = whdul[1].columns
    #
    # Remove primary header cards with keywords matching the
    # column names in the secondary HDU table
    #
    for keyword in wcols.names:
        if keyword in mPHeader:
            del mPHeader[keyword]
    #
    # Create the PrimaryHDU
    #
    mPHdu = fits.PrimaryHDU(header=mPHeader)
    #
    # Add the EXTEND card
    #
    mPHdu.header.set('EXTEND', value=True, after='NAXIS')
    #
    # Add the NEXTEND card.  There will be one extension
    # for each row in the wavered Fits file table HDU.
    #
    mPHdu.header['NEXTEND'] = (whdul[1].data.shape[0],
                               'Number of standard extensions')
    #
    # Create the multi-extension file HDUList from the primary header
    #
    mhdul = fits.HDUList([mPHdu])
    #
    # Create the extension HDUs for the multi-extension file.  There
    # will be one extension for each row in the wavered file's table.
    #
    instrument = mPHeader.get('INSTRUME', '')
    nrows = whdul[1].data.shape[0]

    for i in range(0,nrows):
        #
        # Create the basic HDU from the data
        #
        if nrows == 1:
            #
            # Handle case where there is only one row in the table
            #
            data = whdul[0].data
        else:
            data = whdul[0].data[i]

        mhdul.append(fits.ImageHDU(data))
        #
        # Add cards to the header for each keyword in the column
        # names of the secondary HDU table from the wavered file
        #
        for keyword,format,unit in zip(wcols.names,wcols.formats,wcols.units):
            if unit == 'LOGICAL-':
                #
                # Handle logical values
                #
                if whdul[1].data.field(keyword)[i].strip() == 'T':
                    d = True
                else:
                    d = False
            elif format[0] == 'E':
                #
                # Handle floating point values
                #
                fmt = '%'+format[1:]+'G'
                d = eval(fmt % float(whdul[1].data.field(keyword)[i]))
            else:
                d = whdul[1].data.field(keyword)[i]

            kw_descr = ""
            if keyword in whdul[1].header:
                kw_descr = whdul[1].header[keyword]
            mhdul[i+1].header[keyword] = (d, kw_descr)
        #
        # If original data is unsigned short then scale the data.
        #
        if originalDataType == 'USHORT':
            mhdul[i+1].scale('int16','',bscale=1,bzero=32768)
            mhdul[i+1].header.set('BSCALE', value=1, before='BZERO')
        #
        # For WFPC2 and FOS instruments require additional header cards
        #
        if instrument in ('WFPC2','FOC'):
            #
            # Add EXTNAME card to header
            #
            mhdul[i+1].header['EXTNAME'] = (mPHeader.get('FILETYPE',''),
                                            'extension name')
            #
            # Add EXTVER card to the header
            #
            mhdul[i+1]._extver = i+1
            mhdul[i+1].header.set('EXTVER', value=i+1,
                                  comment='extension version number',
                                  after='EXTNAME')
            #
            # Add the EXPNAME card to the header
            #
            mhdul[i+1].header.set('EXPNAME',
                                  mPHeader.get('ROOTNAME', ''),
                                  '9 character exposure identifier',
                                  before='EXTVER')
            #
            # Add the INHERIT card to the header.
            #
            mhdul[i+1].header.set('INHERIT', True,
                                  'inherit the primary header',
                                  after='EXTVER')
            #
            # Add the ROOTNAME card to the header
            #
            mhdul[i+1].header.set('ROOTNAME',
                                  mPHeader.get('ROOTNAME', ''),
                                  'rootname of the observationset',
                                  after='INHERIT')

    if not multiExtensionFileName and forceFileOutput:
        base,ext = os.path.splitext(whdul[0]._file.name)
        multiExtensionFileName = base[:-1]+'h'+ext

    verboseString = "Input " + inputObjectDescription + \
                    " converted to multi-extension FITS format."

    if multiExtensionFileName:
        if instrument in ('WFPC2','FOC'):
            #
            # write the FILENAME card to the header for the WFPC2 and FOC
            # instruments
            #
            head,tail = os.path.split(multiExtensionFileName)
            mhdul[0].header.set('FILENAME', value=tail, after='NEXTEND')

        if ASTROPY_VER_GE13:
            mhdul.writeto(multiExtensionFileName, overwrite=True)
        else:
            mhdul.writeto(multiExtensionFileName, clobber=True)

        verboseString = verboseString[:-1] + " and written to " + \
                        multiExtensionFileName + "."

    if verbose:
        print(verboseString)

    return mhdul
Convert the input waivered FITS object to a multi-extension FITS
        HDUList object.  Generate an output multi-extension FITS file if
        requested.

        Parameters:

          waiveredObject  input object representing a waivered FITS file;
                          either a astroyp.io.fits.HDUList object, a file object, or a
                          file specification

          outputFileName  file specification for the output file
                          Default: None - do not generate an output file

          forceFileOutput force the generation of an output file when the
                          outputFileName parameter is None; the output file
                          specification will be the same as the input file
                          specification with the last character of the base
                          name replaced with the character 'h'.
                          Default: False

          verbose         provide verbose output
                          Default: False

        Returns:

          mhdul           an HDUList object in multi-extension FITS format.

        Exceptions:

          TypeError       Input object is not a HDUList, a file object or a
                          file name