# include "ppStatsInternal.h"

// calculate the stats for the non-constant entries (already calculated)
bool ppStatsFromMetadataStats (psArray *entries) {

    for (int i = 0; i < entries->n; i++) {
	ppStatsEntry *entry = entries->data[i];

	if (!strcasecmp (entry->statistic, "constant")) continue;
	
	// XXX skip or warn on missing stats?
	if (!entry->vector) continue;

	psStatsOptions option;
	if (!strcasecmp (entry->statistic, "rms")) {
	    option = psStatsOptionFromString ("ROBUST_MEDIAN");
	} else {
	    option = psStatsOptionFromString (entry->statistic);
	}
	
	psStats *stats = psStatsAlloc (option);

	psVectorStats (stats, entry->vector, NULL, NULL, 0);

	double value = psStatsGetValue (stats, option);

	if (!strcasecmp (entry->statistic, "rms")) {
	    value = sqrt(value);
	}

	entry->value = psMetadataItemAllocF32 (entry->keyword, entry->statistic, value);
    }
    return true;
}
