// cpx2tex.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <malloc.h>
#include <math.h>
#include <stdlib.h>

typedef struct _comp {
	double re;
    double im;
} complex, *pcomplex; 

#define Pi 3.141592654


int _tmain(int argc, _TCHAR* argv[])
{
	double intenzitaZdroje,ampv,amp,amax,amin,faze;
	int positFcx,positFcy,positAcx,positAcy,piezo;
	char timebuf1[20],timebuf2[20],timebuf3[20];
	FILE *fr,*fw,*frd;
	pcomplex data;
	complex T;
	int x = 400;  // 320
	int y = 320;  // 240
	int i,j,c,fp;
	double avA,avAv;
	double avF[5];
	double	nav = 0,// konstanta navazani faze
			nav2 = 0, // zpetne navazovani
			prev1 = 0, // predchozi hodnota
			nm; // faze v nm
/*	int vyr[5][2] = {10,10,          // souradnice na kterych se bude delat vyrez
					310,10,
					10,230,
					310,230,
					160,120}; */
	int vyr[5][2] = {10,10,          // souradnice na kterych se bude delat vyrez
					390,10,
					10,310,
					390,310,
					200,160}; 

	int velvyr = 10;  // vyrez (10+10)^2
	char fn[255];
	int chng;
	
	//if(_wtoi(argv[0]) < 1) return 1;

	data = (pcomplex)malloc(x*y*sizeof(complex));
	
	fw = fopen(".\\avrt.txt","w");
	fprintf(fw,"cas;poradi;amplituda;amplitudavyrez;faze0;faze1;faze2;faze3;faze4;faze+nav;faze-nav;faze4[nm];intenzita_Zdroje;prumerna_amplituda_ve_vyrezu;prumerna_amplituda;max_amplituda;min_amplituda;prumerna_faze_ve_vyrezu;pozice_vyrezu_faze_x;y;pozice_vyrezu_amplitudy_x;y;relativniintenzita;piezo\n");
	
	for(c = 0; c < _wtoi(argv[1]) + 1; c++){
		sprintf(fn,".\\%04d.cpx",c);
		fr = fopen(fn,"rb");
		sprintf(fn,".\\%04d.cpd",c);
		frd = fopen(fn,"r");
		fscanf(frd,"cas;intenzita_Zdroje;prumerna_amplituda_ve_vyrezu;prumerna_amplituda;max_amplituda;min_amplituda;prumerna_faze_ve_vyrezu;pozice_vyrezu_faze_x;y;pozice_vyrezu_amplitudy_x;y,piezo\n");
		fscanf(frd,"%*s %s %s %s %*d;%lf;%lf;%lf;%lf;%lf;%lf;%d;%d;%d;%d;%d",timebuf1,timebuf2,timebuf3,&intenzitaZdroje,&ampv,&amp,&amax,&amin,&faze,&positFcx,&positFcy,&positAcx,&positAcy,&piezo);
		fclose(frd);
		vyr[0][0] = positFcx;
		vyr[0][1] = positFcy;
		fread((void *)data,sizeof(complex),x*y,fr);
		avA = 0;
		for(i = 0; i < y; i++)
			for(j = 0; j < x; j++){
				T = *(data + i * x + j);
 				avA += pow(pow(T.re,2) + pow(T.im,2),0.5);
			}
		avA /= (x*y);

		avAv = 0;
		for(i = 42 - velvyr; i < 42 + velvyr; i++)
			for(j = 358 - velvyr; j < 358 + velvyr; j++){
				T = *(data + i * x + j);
 				avAv += pow(pow(T.re,2) + pow(T.im,2),0.5);
			}
		avAv /= (pow((velvyr+velvyr),2.));


		// vypocet prumeru faze ve vice mistech
		for(fp = 0; fp < 5; fp++){
			avF[fp] = 0;
			for(i = vyr[fp][1] - velvyr; i < vyr[fp][1] + velvyr; i++)
				for(j = vyr[fp][0] - velvyr; j < vyr[fp][0] + velvyr; j++){
					T = *(data + i * x + j);
					avF[fp] += atan2(T.im,T.re);
				}
			avF[fp] /= (pow((velvyr+velvyr),2.));
		}

		if((abs(prev1 - avF[4]) > 2) || ((abs(prev1) > 2) && ((abs(avF[4])/avF[4]) * (abs(prev1)/prev1)) == -1)){
			if(c > (chng + 2)){
				nav += (2 * Pi);
				nav2 -= (2 * Pi);
			}
			chng = c;
		}

		prev1 = avF[4];
		nm = (avF[4])/(2*Pi/547*(1+cos(asin(0.25))));

		printf("%d avA %lf avF %lf\r",c,avA,avF[4] + nav);
		//printf("%d avA %lf avF %lf %lf %lf %lf %lf %lf %lf\r",c,avA,avF[0],avF[1],avF[2],avF[3],avF[4],avF[4] + nav,nm);
		//fprintf(fw,"%d;%lf;%lf;%lf;%lf;%lf;%lf;%lf;%lf;%lf;%lf\n",c,avA,avAv,avF[0],avF[1],avF[2],avF[3],avF[4],avF[4] + nav,avF[4] + nav2,nm);
		fprintf(fw,"%s-%s %s;%d;%lf;%lf;%lf;%lf;%lf;%lf;%lf;%lf;%lf;%lf;%lf;%lf;%lf;%lf;%lf;%lf;%d;%d;%d;%d;%lf;%d\n",timebuf2,timebuf1,timebuf3,c,avA,avAv,avF[0],avF[1],avF[2],avF[3],avF[4],avF[4] + nav,avF[4] + nav2,nm,intenzitaZdroje,ampv,amp,amax,amin,faze,positFcx,positFcy,positAcx,positAcy,avA/intenzitaZdroje,piezo);
		fclose(fr);
	}
	fclose(fw);
	return 0;

}

