#include <stdio.h>
#include "pmFPA.h"
#include "pmHDU.h"
#include "pmHDUUtils.h"


pmHDU *pmHDUFromFPA(pmFPA *fpa          // FPA for which to find HDU
                   )
{
    return fpa->hdu;
}

pmHDU *pmHDUFromChip(pmChip *chip       // Chip for which to find HDU
                    )
{
    pmHDU *hdu = chip->hdu;             // The HDU information
    if (!hdu) {
        hdu = pmHDUFromFPA(chip->parent); // Grab HDU info from the FPA
    }

    return hdu;
}

pmHDU *pmHDUFromCell(pmCell *cell       // Cell for which to find HDU
                    )
{
    pmHDU *hdu = cell->hdu;             // The HDU information
    if (!hdu) {
        hdu = pmHDUFromChip(cell->parent); // Grab HDU info from the chip
    }

    return hdu;
}

pmHDU *pmHDUFromReadout (pmReadout *readout)
{

    pmCell *cell = readout->parent; // cell containing this readout;
    pmHDU *hdu = pmHDUFromCell (cell);
    return hdu;
}

// Get the lowest HDU
pmHDU *pmHDUGetLowest(pmFPA *fpa, // The FPA
                      pmChip *chip, // The chip, or NULL
                      pmCell *cell // The cell, or NULL
                     )
{
    pmHDU *hdu = NULL;          // The HDU that's at the lowest level
    if (cell) {
        hdu = pmHDUFromCell(cell);
    } else if (chip) {
        hdu = pmHDUFromChip(chip);
    } else if (fpa) {
        hdu = pmHDUFromFPA(fpa);
    }

    return hdu;
}

