/** @file  pmPSF_IO.c
 *
 * This file contains functions to read and write PSF models using the psMetadata Config file
 * format.
 *
 *  @author EAM, IfA
 *
 *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
 *  @date $Date: 2006-06-03 01:02:08 $
 *
 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
 *
 */

/*****************************************************************************/
/* INCLUDE FILES                                                             */
/*****************************************************************************/

#include <pslib.h>
#include "pmHDU.h"
#include "pmFPA.h"
#include "pmFPAfile.h"
#include "pmFPAfile.h"

#include "pmPeaks.h"
#include "pmMoments.h"
#include "pmGrowthCurve.h"
#include "pmModel.h"
#include "pmPSF.h"
#include "pmPSF_IO.h"
#include "pmSource.h"
#include "pmModelGroup.h"
#include "pmSourceIO.h"

psMetadata *pmPSFtoMD (psMetadata *metadata, pmPSF *psf)
{

    if (metadata == NULL) {
        metadata = psMetadataAlloc ();
    }

    char *modelName = pmModelGetType (psf->type);
    psMetadataAdd (metadata, PS_LIST_TAIL, "PSF_MODEL_NAME", PS_DATA_STRING, "PSF model name", modelName);

    psMetadataAdd (metadata, PS_LIST_TAIL, "PSF_POISSON_ERRORS", PS_DATA_BOOL, "Poisson errors for fits", psf->poissonErrors);

    int nPar = pmModelParameterCount (psf->type)    ;
    psMetadataAdd (metadata, PS_LIST_TAIL, "PSF_MODEL_NPAR", PS_DATA_S32, "PSF model parameter count", nPar);

    for (int i = 0; i < nPar - 4; i++) {
        psPolynomial2D *poly = psf->params->data[i];
        psPolynomial2DtoMD (metadata, poly, "PSF_PAR%02d", i);
    }
    psPolynomial4DtoMD (metadata, psf->ApTrend, "APTREND");

    psMetadataAdd (metadata, PS_LIST_TAIL, "PSF_AP_RESID", PS_DATA_F32, "aperture residual", psf->ApResid);
    psMetadataAdd (metadata, PS_LIST_TAIL, "PSF_dAP_RESID", PS_DATA_F32, "aperture residual scatter", psf->dApResid);
    psMetadataAdd (metadata, PS_LIST_TAIL, "PSF_SKY_BIAS", PS_DATA_F32, "sky bias level", psf->skyBias);

    psMetadataAdd (metadata, PS_LIST_TAIL, "PSF_CHISQ", PS_DATA_F32, "chi-square for fit", psf->chisq);
    psMetadataAdd (metadata, PS_LIST_TAIL, "PSF_NSTARS", PS_DATA_S32, "number of stars used to measure PSF", psf->nPSFstars);

    return metadata;
}

pmPSF *pmPSFfromMD (psMetadata *metadata)
{

    bool status;
    char keyword[80];

    char *modelName = psMetadataLookupPtr (&status, metadata, "PSF_MODEL_NAME");
    pmModelType type = pmModelSetType (modelName);

    bool poissonErrors = psMetadataLookupPtr (&status, metadata, "PSF_POISSON_ERRORS");
    if (!status)
        poissonErrors = true;

    pmPSF *psf = pmPSFAlloc (type, poissonErrors);

    int nPar = psMetadataLookupS32 (&status, metadata, "PSF_MODEL_NPAR");
    if (nPar != pmModelParameterCount (psf->type))
        psAbort ("read PSF" , "mismatch model par count");

    for (int i = 0; i < nPar - 4; i++) {
        sprintf (keyword, "PSF_PAR%02d", i);
        psMetadata *folder = psMetadataLookupPtr (&status, metadata, keyword);
        psPolynomial2D *poly = psPolynomial2DfromMD (folder);
        psFree (psf->params->data[i]);
        psf->params->data[i] = poly;
    }
    sprintf (keyword, "APTREND");
    psMetadata *folder = psMetadataLookupPtr (&status, metadata, keyword);
    psPolynomial4D *poly = psPolynomial4DfromMD (folder);
    psFree (psf->ApTrend);
    psf->ApTrend = poly;

    psf->ApResid = psMetadataLookupF32 (&status, metadata, "PSF_AP_RESID");
    psf->dApResid = psMetadataLookupF32 (&status, metadata, "PSF_dAP_RESID");
    psf->skyBias = psMetadataLookupF32 (&status, metadata, "PSF_SKY_BIAS");

    psf->chisq = psMetadataLookupF32 (&status, metadata, "PSF_CHISQ");
    psf->nPSFstars = psMetadataLookupS32 (&status, metadata, "PSF_NSTARS");

    psFree (metadata);
    return (psf);
}

bool pmFPAviewWritePSFmodel (const pmFPAview *view, pmFPAfile *file)
{

    pmFPA *fpa = file->fpa;

    if (view->chip == -1) {
        pmFPAWritePSFmodel (fpa, view, file);
        return true;
    }

    if (view->chip >= fpa->chips->n) {
        return false;
    }
    pmChip *chip = fpa->chips->data[view->chip];

    if (view->cell == -1) {
        pmChipWritePSFmodel (chip, view, file);
        return true;
    }

    if (view->cell >= chip->cells->n) {
        return false;
    }
    pmCell *cell = chip->cells->data[view->cell];

    if (view->readout == -1) {
        pmCellWritePSFmodel (cell, view, file);
        return true;
    }

    if (view->readout >= cell->readouts->n) {
        return false;
    }
    pmReadout *readout = cell->readouts->data[view->readout];

    pmReadoutWritePSFmodel (readout, view, file);
    return true;
}

// read in all chip-level PSFmodel files for this FPA
bool pmFPAWritePSFmodel (pmFPA *fpa, const pmFPAview *view, pmFPAfile *file)
{

    for (int i = 0; i < fpa->chips->n; i++) {

        pmChip *chip = fpa->chips->data[i];
        pmChipWritePSFmodel (chip, view, file);
    }
    return true;
}

// read in all cell-level PSFmodel files for this chip
bool pmChipWritePSFmodel (pmChip *chip, const pmFPAview *view, pmFPAfile *file)
{

    for (int i = 0; i < chip->cells->n; i++) {

        pmCell *cell = chip->cells->data[i];
        pmCellWritePSFmodel (cell, view, file);
    }
    return true;
}

// read in all readout-level PSFmodel files for this cell
bool pmCellWritePSFmodel (pmCell *cell, const pmFPAview *view, pmFPAfile *file)
{

    for (int i = 0; i < cell->readouts->n; i++) {

        pmReadout *readout = cell->readouts->data[i];
        pmReadoutWritePSFmodel (readout, view, file);
    }
    return true;
}

// read in all readout-level Objects files for this cell
bool pmReadoutWritePSFmodel (pmReadout *readout, const pmFPAview *view, pmFPAfile *file)
{
    bool status;
    char *filename;

    pmPSF *psf = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.PSF");

    switch (file->type) {
    case PM_FPA_FILE_PSF:
        filename = pmFPAfileNameFromRule (file->filerule, file, view);
        psMetadata *psfData = pmPSFtoMD (NULL, psf);
        psMetadataConfigWrite (psfData, filename);
        psFree (psfData);
        psFree (filename);
        return true;

    default:
        fprintf (stderr, "warning: type mismatch\n");
        break;
    }
    return false;
}



bool pmFPAviewReadPSFmodel (const pmFPAview *view, pmFPAfile *file)
{

    pmFPA *fpa = file->fpa;

    if (view->chip == -1) {
        pmFPAReadPSFmodel (fpa, view, file);
        return true;
    }

    if (view->chip >= fpa->chips->n) {
        return false;
    }
    pmChip *chip = fpa->chips->data[view->chip];

    if (view->cell == -1) {
        pmChipReadPSFmodel (chip, view, file);
        return true;
    }

    if (view->cell >= chip->cells->n) {
        return false;
    }
    pmCell *cell = chip->cells->data[view->cell];

    if (view->readout == -1) {
        pmCellReadPSFmodel (cell, view, file);
        return true;
    }

    if (view->readout >= cell->readouts->n) {
        return false;
    }
    pmReadout *readout = cell->readouts->data[view->readout];

    pmReadoutReadPSFmodel (readout, view, file);
    return true;
}

// read in all chip-level PSFmodel files for this FPA
bool pmFPAReadPSFmodel (pmFPA *fpa, const pmFPAview *view, pmFPAfile *file)
{

    for (int i = 0; i < fpa->chips->n; i++) {

        pmChip *chip = fpa->chips->data[i];
        pmChipReadPSFmodel (chip, view, file);
    }
    return true;
}

// read in all cell-level PSFmodel files for this chip
bool pmChipReadPSFmodel (pmChip *chip, const pmFPAview *view, pmFPAfile *file)
{

    for (int i = 0; i < chip->cells->n; i++) {

        pmCell *cell = chip->cells->data[i];
        pmCellReadPSFmodel (cell, view, file);
    }
    return true;
}

// read in all readout-level PSFmodel files for this cell
bool pmCellReadPSFmodel (pmCell *cell, const pmFPAview *view, pmFPAfile *file)
{

    for (int i = 0; i < cell->readouts->n; i++) {

        pmReadout *readout = cell->readouts->data[i];
        pmReadoutReadPSFmodel (readout, view, file);
    }
    return true;
}

// read in all readout-level Objects files for this cell
bool pmReadoutReadPSFmodel (pmReadout *readout, const pmFPAview *view, pmFPAfile *file)
{

    unsigned int Nfail;
    char *filename;

    switch (file->type) {
    case PM_FPA_FILE_PSF:
        filename = pmFPAfileNameFromRule (file->filerule, file, view);

        psMetadata *psfData = psMetadataConfigParse (NULL, &Nfail, filename, FALSE);
        pmPSF *psf = pmPSFfromMD (psfData);
        psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.PSF", PS_DATA_UNKNOWN, "psphot psf", psf);

        psFree (psf);
        psFree (psfData);
        psFree (filename);

        return true;

    default:
        fprintf (stderr, "warning: type mismatch\n");
        break;
    }
    return false;
}

