Compute A-B-(B-A).

    Note that 'list_of_fileinfo' must contain 4 file names.

    Parameters
    ----------
    list_of_fileinfo : list of strings
        List of the for files corresponding to the ABBA observation.
    outfile : string
        Base name for the output FITS file name.
    extnum : int
        Extension number to be read (default value 0 = primary extension).
    noheader : bool
        If True, the header of the first image in the ABBA sequence is
        not copied in the resulting image(s).
    save_partial : bool
        If True, the partial (A-B) and -(B-A) images are also saved.
    debugplot : integer or None
        Determines whether intermediate computations and/or plots
        are displayed. The valid codes are defined in
        numina.array.display.pause_debugplot.

def compute_abba_result(list_of_fileinfo, outfile, extnum=0, noheader=False,
                        save_partial=False, debugplot=0):
    """Compute A-B-(B-A).

    Note that 'list_of_fileinfo' must contain 4 file names.

    Parameters
    ----------
    list_of_fileinfo : list of strings
        List of the for files corresponding to the ABBA observation.
    outfile : string
        Base name for the output FITS file name.
    extnum : int
        Extension number to be read (default value 0 = primary extension).
    noheader : bool
        If True, the header of the first image in the ABBA sequence is
        not copied in the resulting image(s).
    save_partial : bool
        If True, the partial (A-B) and -(B-A) images are also saved.
    debugplot : integer or None
        Determines whether intermediate computations and/or plots
        are displayed. The valid codes are defined in
        numina.array.display.pause_debugplot.

    """

    # check number of images
    if len(list_of_fileinfo) != 4:
        raise ValueError("Unexpected number of ABBA files: " +
                         str(len(list_of_fileinfo)))

    # avoid PyCharm warnings
    # (local variable might be referenced before assignment)
    naxis1 = 0
    naxis2 = 0
    image_header = None

    # check image dimensions
    for i in range(4):
        hdulist = fits.open(list_of_fileinfo[i].filename)
        if i == 0:
            image_header = hdulist[extnum].header
            naxis1 = image_header['naxis1']
            naxis2 = image_header['naxis2']
            hdulist.close()
        else:
            image_header_ = hdulist[extnum].header
            naxis1_ = image_header_['naxis1']
            naxis2_ = image_header_['naxis2']
            hdulist.close()
            if naxis1 != naxis1_ or naxis2 != naxis2_:
                print('>>> naxis1, naxis2..:', naxis1, naxis2)
                print('>>> naxis1_, naxis2_:', naxis1_, naxis2_)
                raise ValueError("Image dimensions do not agree!")

    # initialize outpuf array(s)
    if save_partial:
        result_ini = np.zeros((naxis2, naxis1), dtype=np.float)
        result_end = np.zeros((naxis2, naxis1), dtype=np.float)
    else:
        result_ini = None
        result_end = None
    result = np.zeros((naxis2, naxis1), dtype=np.float)

    # read the four images and compute arithmetic combination
    for i in range(4):
        if abs(debugplot) >= 10:
            print('Reading ' + list_of_fileinfo[i].filename + '...')
        hdulist = fits.open(list_of_fileinfo[i].filename)
        image2d = hdulist[extnum].data.astype(np.float)
        hdulist.close()
        if i == 0:
            if save_partial:
                result_ini += image2d
            result += image2d
        elif i == 1:
            if save_partial:
                result_ini -= image2d
            result -= image2d
        elif i == 2:
            if save_partial:
                result_end -= image2d
            result -= image2d
        elif i == 3:
            if save_partial:
                result_end += image2d
            result += image2d
        else:
            raise ValueError('Unexpected image number: ' + str(i))

    # save results
    if noheader:
        image_header = None
    if save_partial:
        if abs(debugplot) >= 10:
            print("==> Generating output file: " + outfile + "_sub1.fits...")
        hdu = fits.PrimaryHDU(result_ini.astype(np.float), image_header)
        hdu.writeto(outfile + '_sub1.fits', overwrite=True)
        if abs(debugplot) >= 10:
            print("==> Generating output file: " + outfile + "_sub2.fits...")
        hdu = fits.PrimaryHDU(result_end.astype(np.float), image_header)
        hdu.writeto(outfile + '_sub2.fits', overwrite=True)
    if abs(debugplot) >= 10:
        print("==> Generating output file: " + outfile + ".fits...")
    hdu = fits.PrimaryHDU(result.astype(np.float), image_header)
    hdu.writeto(outfile + '.fits', overwrite=True)