/** @file psastroMetaDataStats.c
 *
 *  @brief
 *
 *  @ingroup psastro
 *
 *  @author IfA
 *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
 *  @date $Date: 2009-02-07 02:03:34 $
 *  Copyright 2009 Institute for Astronomy, University of Hawaii
 */

# include "psastroInternal.h"

bool psastroMetadataStats (pmConfig *config, psMetadata *stats) {

    bool status;

    char *filename = psMetadataLookupStr (&status, config->arguments, "STATS");
    if (!filename) return true; // stats not requested

    // Extract statistics from the last output fpa
    pmFPAfile *output = psMetadataLookupPtr(&status, config->files, "PSASTRO.OUTPUT");

    if (!output) {
        psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to find output file (PSASTRO.OUTPUT).");
        return false;
    }

    // extract stats for the complete fpa
    pmFPAview *view = pmFPAviewAlloc(0);

    if (!ppStatsMetadata(stats, output->fpa, view, 0, config)) {
        psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to generate stats for image.");
        psFree(view);
        return false;
    }

    // if we did not request any specific stats, the structure is empty
    if (stats && stats->list->n == 0) {
        psWarning ("stats output specified, but no requested stats entries in headers");
        psFree(view);
        return true;
    }

    // convert the stats MDC to a string
    char *statsMDC = psMetadataConfigFormat(stats);
    if (!statsMDC || strlen(statsMDC) == 0) {
        psError(PS_ERR_IO, false, "Unable to serialize stats metadata.\n");
        return false;
    }

    // convert to a real UNIX filename
    psString resolved = pmConfigConvertFilename(filename, config, true, false); // Resolved filename
    FILE *statsFile = fopen (resolved, "w");
    if (!statsFile) {
        psError(PS_ERR_IO, true, "Unable to open statistics file %s for writing.\n", resolved);
        psFree(view);
        psFree(statsMDC);
        psFree(resolved);
        return false;
    }

    // write the stats MDC to a file
    fprintf(statsFile, "%s", statsMDC);
    fclose(statsFile);

    pmConfigRunFilenameAddWrite(config, "STATS", filename);

    psFree(resolved);
    psFree(statsMDC);
    psFree(view);
    return true;
}
