core.log("Measure HFR and eccentricity of stars");

var chart = {
    "title": "Measure stars",
    "legend": {"visible": true, "align": "left"},
    "series": [
        {
            "title": "HFR",
            "type": "bar",
            "y":[]
        },
        {
            "title": "Ecc",
            "type": "bar",
            "y":[]
        },
        {
            "title": "Star count",
            "type": "linePoints",
            "y":[],
            "y2": true,
            "bestFit": true
        }
    ]
};

core.setSolverProfile(5);
for(file of files)
{
    if(file.suffix() == "fits" || file.suffix() == "fit" || file.suffix() == "xisf")
	{
		var stars = file.extractStars(true);
		var sumHFR = 0;
		var ecc = 0; 
		for(star of stars)
		{
		    sumHFR += star.HFR;
		    ecc += Math.sqrt(1 - (star.b * star.b) / (star.a * star.a));
		}
		chart.series[0].y.push(sumHFR / stars.length);
		chart.series[1].y.push(ecc / stars.length);
		chart.series[2].y.push(stars.length);
		
		core.log(file.fileName() + " Stars:" + stars.length + " HFR: " + sumHFR / stars.length + " Ecc: " + ecc / stars.length);
	}
}

core.plot(chart);

