#include <stdio.h>
#include <assert.h>
#include <pslib.h>
#include "pmFPA.h"
#include "pmHDU.h"
#include "pmConcepts.h"
#include "pmFPAHeader.h"

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Public functions
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool pmCellReadHeader(pmCell *cell,     // Cell for which to read header
                      psFits *fits      // FITS file handle
                     )
{
    PS_ASSERT_PTR_NON_NULL(cell, false);
    PS_ASSERT_PTR_NON_NULL(fits, false);

    if (!cell->hdu) {
        return pmChipReadHeader(cell->parent, fits);
    }
    if (!pmHDUReadHeader(cell->hdu, fits)) {
        psError(PS_ERR_IO, false, "Unable to read header for cell.\n");
        return false;
    }

    return pmConceptsReadCell(cell, PM_CONCEPT_SOURCE_HEADER, false, NULL);
}


bool pmChipReadHeader(pmChip *chip,     // Chip for which to read header
                      psFits *fits      // FITS file handle
                     )
{
    PS_ASSERT_PTR_NON_NULL(chip, false);
    PS_ASSERT_PTR_NON_NULL(fits, false);

    if (!chip->hdu) {
        return pmFPAReadHeader(chip->parent, fits);
    }
    if (!pmHDUReadHeader(chip->hdu, fits)) {
        psError(PS_ERR_IO, false, "Unable to read header for cell.\n");
        return false;
    }

    if (!pmConceptsReadChip(chip, PM_CONCEPT_SOURCE_HEADER, true, true, NULL)) {
        psError(PS_ERR_UNKNOWN, false, "Unable to read concepts for chip.\n");
        return false;
    }

    return true;
}


bool pmFPAReadHeader(pmFPA *fpa,        // FPA for which to read header
                     psFits *fits       // FITS file handle
                    )
{
    PS_ASSERT_PTR_NON_NULL(fpa, false);
    PS_ASSERT_PTR_NON_NULL(fits, false);

    if (!fpa->hdu) {
        return false;
    }
    if (!pmHDUReadHeader(fpa->hdu, fits)) {
        psError(PS_ERR_IO, false, "Unable to read header for cell.\n");
        return false;
    }

    if (!pmConceptsReadFPA(fpa, PM_CONCEPT_SOURCE_HEADER, true, NULL)) {
        psError(PS_ERR_UNKNOWN, false, "Unable to read concepts for FPA.\n");
        return false;
    }

    return true;
}
