#include <csv.c>
#include <array_2d.c>

void reoval(REAL *x,REAL *y)
{ // Make it oval again
	const REAL f=0.633062082,xr2rms=4.724722231/10,yr2rms=3.40975131/10; // CBETA-V ellipse parameters (need it for corrector response)
	// There was a completely arbitrary multiply-by-10 in my spreadsheet to make the graphs look right, hence dividing by 10 here
	*x*=xr2rms; *y*=yr2rms;
	*y-=f**x;
}

int main(void)
{
	REAL **r=array_2d_loadCSV("..\\reponsematx_H0.1A.csv"),
		**p=array_2d_loadCSV("..\\realmachinephases.csv");
	int x,y,m=array_2d_ys(r);
	for (x=m-1;x>=0;x--) for (y=m-1;y>=0;y--) r[x][y]=r[x*2][y];	
	printf("m=%d\n",m);
	REAL **er=array_2d(m,m);
	for (x=m-1;x>=0;x--) for (y=m-1;y>=0;y--)
	{
		if (y>x) er[x][y]=0;
		else
		{
			REAL th=M_TWOPI*(p[x][0]-p[y][0])+0.1696,a=sin(th),b=-cos(th);
			reoval(&a,&b);
			er[x][y]=a;
		}
	}
	array_2d_saveCSV(er,"er.csv");
	array_2d_free(er);
	array_2d_free(r); array_2d_free(p);
}
