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

#include "stdafx.h"           //vyžadováno programem Microsoft Visual Studio
#include "iostream"
#include <fstream>
#include "iomanip"
#define _USE_MATH_DEFINES
#include "math.h"
#include <string>
#include "matice.h"

using namespace std;

class Rinex
{
public:
  // deklarace členských proměnných:
	ifstream protokol, rinex, parametry;
	string verze, typ1, typ2, odpad, *fazove_mereni, *datum, *observacni_typ, **nazev_druzice, *datum_zarizeni, **druzice_SD, **druzice_TDSD;
	int *cas_zarizeni, pocet_observacnich_typu, pocet_bloku_skut, *pocet_druzic, max_pocet_druzic, pocet_radku_protokolu, doba_observace; 
	int *cas, ***sila_signalu, ***LLI, leap_seconds, pocitadlo_bloku, *pocitadlo_druzic, **omezeni_skut, *cas_razeni, *cas_SD, *cas_TDSD;
	int omezeni, *pocet_druzic_SD, *pocet_druzic_TDSD;
	double *azimut_zarizeni, *elevace_zarizeni, *azimut_zarizeni_orig, *elevace_zarizeni_orig, interval, ***observace, ***dif_observace_ref, ***dif_observace_test, ***dif_observace_ref_kor, ***dif_observace_test_kor;
	double ***jednoducha_diference, ***casova_diference, **razeni_azimut, **razeni_elevace, **SD_azimut, **SD_elevace, **TDSD_azimut, **TDSD_elevace;
	double *azimut_zarizeni_SD, *elevace_zarizeni_SD, **PCO, **ant_azimut, **ant_elevace, **koef_PCV;
 
  // deklarace členských funkcí:
	void nacti_protokol(string file);
	void nacti_rinex(string file, int pocet_bloku, int *cas_zarizeni);
	void zobraz(double x);
	void serad(int *cas_r, int *cas_t, string **druzice_r, string **druzice_t, string *datum_r, string *datum_t, int doba_r, int doba_t, int interval_r, int interval_t, int *parametr2_r, int *parametr2_t, string *obs_typ_r, string *obs_typ_t, int pocet_obs_r, int pocet_obs_t, int max_pocet_druz, double ***observace_r, double ***observace_t, double *azimut_zarizeni, double *elevace_zarizeni);
	void LOS(int bloky, int *druzice, int **obs_typy, int *cas_razeni, double ***dif_obs_r, double ***dif_obs_t, string **druzice_SD, double *azimut_zarizeni, double *elevace_zarizeni);
	void SD(int bloky, int *druzice, int **obs_typy, double ***dif_obs_r, double ***dif_obs_t, int *cas_razeni, double **azimut_razeni, double **elevace_razeni);
	void TDSD(int bloky, int *druzice, int **obs_typy, double ***j_diference, int *cas_SD, string **druzice_SD, double **azimut_SD, double **elevace_SD);
	void vyrovnej_1(int bloky, int *druzice, int **obs_typy, double ***casova_diference, double *azimut_zarizeni, double *elevace_zarizeni, double **azimut_TDSD, double **elevace_TDSD, int omezeni, string *fazove_mereni);
	void vyrovnej_2(int bloky, int *druzice, double ***casova_diference, double **ant_azimut, double **ant_elevace, int omezeni, string *fazove_mereni, double **PCO);
};

//funkce načítající protokol o pohybech polohovacího zařízení během měření:
void Rinex::nacti_protokol(string file)
{
	string radek_protokolu, rok, mesic, den, hod, min, sec, azimut, zenit;
	int hod_i, min_i, sec_i;

	protokol.open(file);				//1. otevření - jen pro zjištění rozměru

	cout << "Nacitam casy a uhly naklonu z protokolu o pohybu zarizeni " << file << " ..." << endl;

	for (int i = 0; !protokol.eof(); i++)						
	{
		getline(protokol,radek_protokolu);
		pocet_radku_protokolu = i;
	}

	protokol.close();

	datum_zarizeni = new string[pocet_radku_protokolu];
	cas_zarizeni = new int[pocet_radku_protokolu];
	azimut_zarizeni = new double[pocet_radku_protokolu];
	elevace_zarizeni = new double[pocet_radku_protokolu];
	azimut_zarizeni_orig = new double[pocet_radku_protokolu];
	elevace_zarizeni_orig = new double[pocet_radku_protokolu];

	protokol.open(file);					//2. otevření - pro načtení dat

	cout << "Zadejte aktualni pocet prestupnych vterin ke dni mereni: " << endl;
	cin >> leap_seconds;

	for (int i = 0; i < pocet_radku_protokolu; i++)
	{
		while (odpad != "time:")
			protokol >> odpad;

		protokol >> rok >> mesic >> den >> hod >> min >> sec;
		
		while (odpad != "Z):")
			protokol >> odpad;

		protokol >> azimut >> zenit >> odpad >> odpad >> odpad;

		rok = rok.substr(2,2);

		if(mesic.length() == 1)
			mesic = "0" + mesic;

		if(den.length() == 1)
			den = "0" + den;
		
		datum_zarizeni[i] = rok + mesic + den;

		azimut_zarizeni[i] = stod(azimut);
		azimut_zarizeni_orig[i] = stod(azimut);

		if (azimut_zarizeni[i] < 0)
			azimut_zarizeni[i] = 360 + azimut_zarizeni[i];

		elevace_zarizeni[i] = 90 - stod(zenit);						// elevace konektoru od směru na sever
		elevace_zarizeni_orig[i] = stod(zenit);	
		
		//převod času na čas GPST
		sec_i = stoi(sec);
		min_i = stoi(min);
		hod_i = stoi(hod);

		sec_i = sec_i - leap_seconds;

		if (sec_i < 0)
		{
			min_i--;
			sec_i = sec_i + 60;
		}

		if (min_i < 0)
		{
			hod_i--;
			min_i = min_i + 60;
		}
		
		hod = to_string(hod_i);
		min = to_string(min_i);
		sec = to_string(sec_i);

		if(min.length() == 1)
			min = "0" + min;
		
		if(sec.length() == 1)
			sec = "0" + sec;

		cas_zarizeni[i] = stoi(hod + min + sec);		
	}
	protokol.close();
}

//funkce načítající observační data:
void Rinex::nacti_rinex(string file, int pocet_bloku, int *cas_zarizeni)            
{
	int pocet_radku_observacnich_typu, index, pocet_radku_druzic, pocet_radku_jedne_druzice;
	string observ, epocha, index_str, druzice, druzice2, pismeno, pocet_observ_str; 
	string obs_typy, obs_typy2;
	
	rinex.open(file);
	
	cout << "Nacitam observacni data ze souboru " << file << " ..." << endl;
	
  //načtení hlavičky
	rinex >> verze >> typ1 >> typ2;					
	
	while (observ.find("# / TYPES OF OBSERV") == string::npos)
		getline(rinex,observ);
	
	pocet_observ_str = observ.substr(0,6);
	obs_typy = observ.substr(6,54);
	pocet_observacnich_typu = stoi(pocet_observ_str);

	if (pocet_observacnich_typu%9 == 0)
		pocet_radku_observacnich_typu = (pocet_observacnich_typu/9);
	else
		pocet_radku_observacnich_typu = (pocet_observacnich_typu/9)+1;							
	
	observacni_typ = new string [pocet_observacnich_typu];	
	
	for (int i = 0; i < pocet_radku_observacnich_typu; i++)
	{
		getline(rinex,obs_typy2);
		obs_typy2 = obs_typy2.substr(6,54);
		obs_typy += obs_typy2;
	}

	for (int i = 0; i < pocet_observacnich_typu; i++)
		observacni_typ[i] = obs_typy.substr(4+i*6,2);

	while (odpad != "HEADER")
		rinex >> odpad;

	//načtení vlastních dat									
	max_pocet_druzic = 32/2 + 24/2 + 30/2;										

	string ***observace_komplet_str = new string **[pocet_bloku];			          
		for (int i = 0; i < pocet_bloku; i++)
		{
			observace_komplet_str[i] = new string *[max_pocet_druzic];					
			
			for (int j = 0; j < max_pocet_druzic; j++)							
				observace_komplet_str[i][j] = new string [pocet_observacnich_typu];			
		}

	string ***observace_str = new string **[pocet_bloku];							              
		for (int i = 0; i < pocet_bloku; i++)
		{
			observace_str[i] = new string *[max_pocet_druzic];					
			
			for (int j = 0; j < max_pocet_druzic; j++)								        
				observace_str[i][j] = new string [pocet_observacnich_typu];			
		}

	observace = new double **[pocet_bloku];
		for (int i = 0; i < pocet_bloku; i++)
		{
			observace[i] = new double *[max_pocet_druzic];
			
			for (int j = 0; j < max_pocet_druzic; j++)					
				observace[i][j] = new double [pocet_observacnich_typu];		
		}
	
	string ***sila_signalu_str = new string **[pocet_bloku];			
		for (int i = 0; i < pocet_bloku; i++)
		{
			sila_signalu_str[i] = new string *[max_pocet_druzic];					
			
			for (int j = 0; j < max_pocet_druzic; j++)							
				sila_signalu_str[i][j] = new string [pocet_observacnich_typu];			
		}

	sila_signalu = new int **[pocet_bloku];
		for (int i = 0; i < pocet_bloku; i++)
		{
			sila_signalu[i] = new int *[max_pocet_druzic];
			
			for (int j = 0; j < max_pocet_druzic; j++)					
				sila_signalu[i][j] = new int [pocet_observacnich_typu];		
		}

	string ***LLI_str = new string **[pocet_bloku];			
		for (int i = 0; i < pocet_bloku; i++)
		{
			LLI_str[i] = new string *[max_pocet_druzic];					
			
			for (int j = 0; j < max_pocet_druzic; j++)							
				LLI_str[i][j] = new string [pocet_observacnich_typu];			
		}

	LLI = new int **[pocet_bloku];
		for (int i = 0; i < pocet_bloku; i++)
		{
			LLI[i] = new int *[max_pocet_druzic];
			
			for (int j = 0; j < max_pocet_druzic; j++)					
				LLI[i][j] = new int [pocet_observacnich_typu];		
		}

	pocet_druzic = new int [pocet_bloku];

	string *rok = new string [pocet_bloku];
	string *mesic = new string [pocet_bloku];
	string *den = new string [pocet_bloku];
	string *hod = new string [pocet_bloku];
	string *min = new string [pocet_bloku];
	string *sec = new string [pocet_bloku];
	string *datum_cele = new string [pocet_bloku];
	string *datum_edit = new string [pocet_bloku];
	string *cas_str = new string [pocet_bloku];
	string *cas_str_edit = new string [pocet_bloku];
	
	datum = new string [pocet_bloku];
	cas = new int [pocet_bloku];

	string **nazev_druzice_orig = new string *[pocet_bloku];
		for (int i = 0; i < pocet_bloku; i++)
			nazev_druzice_orig[i] = new string [max_pocet_druzic];

	string **nazev_druzice_edit = new string *[pocet_bloku];
		for (int i = 0; i < pocet_bloku; i++)
			nazev_druzice_edit[i] = new string [max_pocet_druzic];

	nazev_druzice = new string *[pocet_bloku];
		for (int i = 0; i < pocet_bloku; i++)
			nazev_druzice[i] = new string [max_pocet_druzic];

	getline(rinex,odpad);																

	int pom = 0;
	pocet_bloku_skut = 0;

	for (int i = 0; !rinex.eof(); i++)			// první rozměr = časové epochy (datové bloky)		       																	
	{
		if (pom == pocet_bloku)
			break;

		getline(rinex,epocha);
		
		if (epocha.length() == 0)				// případné prázdné řádky na konci souboru
			break;

		rok[i] = epocha.substr(1,2);
		mesic[i] = epocha.substr(4,2);
		den[i] = epocha.substr(7,2);

		hod[i] = epocha.substr(10,2);
		min[i] = epocha.substr(13,2);
		sec[i] = epocha.substr(16,10);
		
		index_str = epocha.substr(28,1);
		index = stoi(index_str);
		
		druzice = epocha.substr(30,12*3+2);
		
		datum_cele[i] = rok[i] + mesic[i] + den[i];
		
		cas_str[i] = hod[i] + min[i] + sec[i];

		for (int n = 0; n < datum_cele[i].length(); n++)                
		{
			pismeno = datum_cele[i].substr(n,1);
			
			if (pismeno == " ")
				pismeno = "0";
		
			datum_edit[i] += pismeno;
		}

		datum[i] = datum_edit[i];
		datum_edit[i].clear();

		for (int n = 0; n < cas_str[i].length(); n++)                
		{
			pismeno = cas_str[i].substr(n,1);
			
			if (pismeno == " ")
				pismeno = "0";
		
			cas_str_edit[i] += pismeno;
		}
		
		cas[i] = stoi(cas_str_edit[i]);
		
		cas_str_edit[i].clear();

		if (cas[i] > cas_zarizeni[pom])					//pro případ, kdy je interval GNSS měření větší než interval pozic zařízení
		{
			while(cas[i] > cas_zarizeni[pom])
			{
				pom++;	

				if (pom == pocet_bloku)
					break;
			}
		}

		pocet_druzic[i] = atoi(druzice.c_str());
		
		if (pocet_druzic[i]%12 == 0)
			pocet_radku_druzic = pocet_druzic[i]/12;
		else
			pocet_radku_druzic = (pocet_druzic[i]/12) + 1;
	
		if(index > 1 && index < 6)
		{
			for(int k = 0; k < pocet_druzic[i]; k++)
				getline(rinex,odpad);
			
			i--;
			continue;
		}
		
		if (pocet_druzic[i] > 12)
		{
			for (int j = 0; j < pocet_radku_druzic - 1; j++)
			{
				getline(rinex,druzice2);
				druzice2 = druzice2.substr(32,12*3);
				druzice += druzice2;
			}
		}

		for (int j = 0; j < pocet_druzic[i]; j++)
		{
			nazev_druzice_orig[i][j] = druzice.substr(2+j*3,3);

			for (int n = 0; n < nazev_druzice_orig[i][j].length(); n++)                
			{
				pismeno = nazev_druzice_orig[i][j].substr(n,1);
			
				if (pismeno == " ")
					pismeno = "0";
		
				nazev_druzice_edit[i][j] += pismeno;
			}
			nazev_druzice[i][j] = nazev_druzice_edit[i][j];
			nazev_druzice_edit[i][j].clear();
		}
	
		if (pocet_observacnich_typu%5 == 0)
			pocet_radku_jedne_druzice = pocet_observacnich_typu/5;
		else
			pocet_radku_jedne_druzice = (pocet_observacnich_typu/5)+1;

		if (cas[i] == cas_zarizeni[pom])				// vyhodnocuje se, zda se jedná o zajmový řádek, nebo ne									
		{
			for (int j = 0; j < pocet_druzic[i]; j++)				               
			{	
				string *radek = new string [pocet_radku_jedne_druzice];

				for (int n = 0; n < pocet_radku_jedne_druzice; n++)    
				{
					getline(rinex,radek[n]);
				
					int pocet_hodnot = radek[n].length()/15;	           
			
					if (pocet_hodnot == 0)
						pocet_hodnot = 1;

					int kriterium1;

					int kriterium2 = pocet_hodnot + n*5;

					if (n == pocet_radku_jedne_druzice - 1)
						kriterium1 = pocet_observacnich_typu;
					else
						kriterium1 = (n+1)*5;

					if (radek[n].length() == pocet_hodnot*(15+1))											
					{
						for (int k = n*5; k < kriterium2; k++)							
						{
							observace_komplet_str[i][j][k] = radek[n].substr(((k-n*5)*15)+(1+k-n*5),15);					
					
							LLI_str[i][j][k] = observace_komplet_str[i][j][k].substr(13,1);
							sila_signalu_str[i][j][k] = observace_komplet_str[i][j][k].substr(14,1); 
							observace_str[i][j][k] = observace_komplet_str[i][j][k].substr(0,13);
						}
				
						if (kriterium2 != kriterium1)
						{
							for (int k = kriterium2; k < kriterium1; k++)
							{
								observace_str[i][j][k] = " --- ";
								LLI_str[i][j][k] = " --- ";
								sila_signalu_str[i][j][k] = " --- ";			
							}
						}
					}
				
					else if (radek[n].length() == 0)
					{
						for (int k = n*5; k < kriterium1; k++)
						{
							observace_str[i][j][k] = " --- ";
							LLI_str[i][j][k] = " --- ";
							sila_signalu_str[i][j][k] = " --- ";
						}
					}

					else
					{
						for (int k = n*5; k < kriterium2-1; k++)							
						{
							observace_komplet_str[i][j][k] = radek[n].substr(((k-n*5)*15)+(1+k-n*5),15);

							observace_str[i][j][k] = observace_komplet_str[i][j][k].substr(0,13);
							LLI_str[i][j][k] = observace_komplet_str[i][j][k].substr(13,1);
							sila_signalu_str[i][j][k] = observace_komplet_str[i][j][k].substr(14,1); 
						}
					
						observace_komplet_str[i][j][kriterium2-1] = radek[n].substr(((kriterium2-1-n*5)*15)+(1+kriterium2-1-n*5),15);
					
						if (observace_komplet_str[i][j][kriterium2-1].substr(13,1).length() == 1)					
							LLI_str[i][j][kriterium2-1] = observace_komplet_str[i][j][kriterium2-1].substr(13,1);
						else
							LLI_str[i][j][kriterium2-1] = " --- ";
						
							sila_signalu_str[i][j][kriterium2-1] = " --- ";
						
							observace_str[i][j][kriterium2-1] = observace_komplet_str[i][j][kriterium2-1].substr(0,13);
					
						if (kriterium2 != kriterium1)
						{
							for (int k = kriterium2; k < kriterium1; k++)
							{
								observace_str[i][j][k] = " --- ";
								LLI_str[i][j][k] = " --- ";
								sila_signalu_str[i][j][k] = " --- ";			
							}
						}
					}
				}

				for (int k = 0; k < pocet_observacnich_typu; k++)											
				{
					if (observace_str[i][j][k] == "             " || observace_str[i][j][k] == " --- ")
						continue;
				
					observace[i][j][k] = stod(observace_str[i][j][k]);	
				
					if (sila_signalu_str[i][j][k] == " " || sila_signalu_str[i][j][k] == " --- ")
						continue;
	
					sila_signalu[i][j][k] = stoi(sila_signalu_str[i][j][k]);	
	
					if (LLI_str[i][j][k] == " " || LLI_str[i][j][k] == " --- ")
						continue;
				
					LLI[i][j][k] = stoi(LLI_str[i][j][k]);	
				}
			
				for (int k = 0; k < pocet_observacnich_typu; k++)
				{
					if (observace[i][j][k] < -842150450)
						continue;
				}
			}
			
			pom++;
			pocet_bloku_skut++;

			if (pom == pocet_bloku)
				break;
		}
		
		else
		{
			for (int x = 0; x < pocet_druzic[i]*pocet_radku_jedne_druzice; x++)
				getline(rinex,odpad);
			
			i--;
			continue;
		}
	}

	interval = cas[1] - cas[0];
	doba_observace = pocet_bloku_skut*interval;                

	rinex.close();

	// dealokace paměti pro již nepotřebné proměnné a pole:							
	delete[] rok;
	delete[] mesic;
	delete[] den;
	delete[] hod;
	delete[] min;
	delete[] sec;
	delete[] cas_str;
	delete[] cas_str_edit;

	for (int i = 0; i < pocet_bloku; i++)
	{
		for (int j = 0; j < max_pocet_druzic; j++)		
		{
			delete[] observace_komplet_str[i][j];
		}
		delete[] observace_komplet_str[i];
	}
	delete[] observace_komplet_str;

	for (int i = 0; i < pocet_bloku; i++)
	{
		for (int j = 0; j < max_pocet_druzic; j++)
		{
			delete[] observace_str[i][j];
		}
		delete[] observace_str[i];
	}
	delete[] observace_str;

	for (int i = 0; i < pocet_bloku; i++)
	{
		for (int j = 0; j < max_pocet_druzic; j++)
		{
			delete[] sila_signalu_str[i][j];
		}
		delete[] sila_signalu_str[i];
	}
	delete[] sila_signalu_str;

	for (int i = 0; i < pocet_bloku; i++)
	{
		for (int j = 0; j < max_pocet_druzic; j++)
		{
			delete[] LLI_str[i][j];
		}
		delete[] LLI_str[i];
	}
	delete[] LLI_str;
}

// funkce, která zobrazí libovolnou hodnotu v observačních datech:
void Rinex::zobraz(double x)            
{
	if (x > -842150451)												// -842150451 je double hodnota prázdného znaku...										
		cout << endl << setprecision(3) << fixed << x << endl;	
	else
		cout << endl << "NENI HODNOTA!" << endl;
}

// funkce, která k sobě přiřazuje hodnoty ze stejné časové epochy, stejné družice a stejného observačního typu:
void Rinex::serad(int *cas_r, int *cas_t, string **druzice_r, string **druzice_t, string *datum_r, string *datum_t, int doba_r, int doba_t, int interval_r, int interval_t, int *parametr2_r, int *parametr2_t, string *obs_typ_r, string *obs_typ_t, int pocet_obs_r, int pocet_obs_t, int max_pocet_druz, double ***observace_r, double ***observace_t, double *azimut_zarizeni, double *elevace_zarizeni)			
{
	if (datum_r[0] == datum_t[0])
	{
		cout << "Provadim razeni dat ..." << endl;
		int i, j, k;
		int pom1_1 = 0; 
		int pom1_2 = 0;	
		pocitadlo_bloku = 0;			
		int parametr1;
	
		int pom2_1, pom2_2; 
		int	pom3_1[3];
		int pom3_2[3];

		if (doba_r < doba_t)
			doba_t = doba_r;				

		if (interval_r > interval_t)
			interval_t = interval_r;

		parametr1 = doba_t/interval_t;		

		pocitadlo_druzic = new int [parametr1];

		azimut_zarizeni_SD = new double [parametr1];
		elevace_zarizeni_SD = new double [parametr1];

		omezeni = 3;

		fazove_mereni = new string[omezeni];

		fazove_mereni[0] = "L1";
		fazove_mereni[1] = "L2";
		fazove_mereni[2] = "L5";

		omezeni_skut = new int *[parametr1];
		for (int i = 0; i < parametr1; i++)
			omezeni_skut[i] = new int [max_pocet_druz];
		
		druzice_SD = new string *[parametr1];					
		for (int i = 0; i < parametr1; i++)
			druzice_SD[i] = new string [max_pocet_druz];

		dif_observace_ref = new double **[parametr1];
		for (int i = 0; i < parametr1; i++)
		{
			dif_observace_ref[i] = new double *[max_pocet_druz];
			
			for (int j = 0; j < max_pocet_druz; j++)					
				dif_observace_ref[i][j] = new double [omezeni];		
		}
		
		dif_observace_test = new double **[parametr1];
		for (int i = 0; i < parametr1; i++)
		{
			dif_observace_test[i] = new double *[max_pocet_druz];
			
			for (int j = 0; j < max_pocet_druz; j++)					
				dif_observace_test[i][j] = new double [omezeni];		
		}
	
		//ŘAZENÍ OBSERVAČNÍCH TYPŮ => řešíme zvlášť a nejdřív, protože je jednotné a už se nemění...
		for (k = 0; k < omezeni; k++)						
		{
			pom3_1[k] = 0;	
			pom3_2[k] = 0;
			
			while (obs_typ_r[pom3_1[k]] != fazove_mereni[k])   
			{
				pom3_1[k]++;
					
				if (pom3_1[k] == pocet_obs_r)
				{
					obs_typ_r[pom3_1[k]-1] = "-";
					pom3_1[k]--;
					break;
				}
			}
			
			while (obs_typ_t[pom3_2[k]] != fazove_mereni[k])
			{
				pom3_2[k]++;
	
				if (pom3_2[k] == pocet_obs_t)
				{
					obs_typ_t[pom3_2[k]-1] = "--";				//aby se nepřiřazovalo jako stejné, když ani jedna aparatura nebude mít jednu z frekvencí 						
					pom3_2[k]--;
					break;
				}
			}
			
			if (obs_typ_r[pom3_1[k]] != obs_typ_t[pom3_2[k]])
			{
				k--;
				omezeni--;
			}			
		}

		for (i = 0; ; i++)
		{	
		// ČÁST 1: ŘAZENÍ DATOVÝCH BLOKŮ
			if (pocitadlo_bloku == parametr1)						
				break;

			if (cas_r[i] != cas_t[i])
			{
				if (cas_r[i] < cas_t[i])
				{	
					while (cas_r[pom1_1] != cas_t[i])
						pom1_1++;			
				}
				
				else
				{
					while (cas_r[i] != cas_t[pom1_2])
						pom1_2++;
				}
			}

			int z;

			if (pom1_1 > pom1_2)
				z = pom1_1;
			else
				z = pom1_2;

			azimut_zarizeni_SD[i] = azimut_zarizeni[z];		
			elevace_zarizeni_SD[i] = elevace_zarizeni[z];
		
		// ČÁST 2: ŘAZENÍ DRUŽIC
			pocitadlo_druzic[i] = 0;
			j = 0;

			for (pom2_1 = 0; pom2_1 < parametr2_r[pom1_1]; pom2_1++) 			
			{
				pom2_2 = 0;								             	
				
				while (druzice_r[pom1_1][pom2_1] != druzice_t[pom1_2][pom2_2])
				{
					pom2_2++;
					
					if (pom2_2 == parametr2_t[pom1_2])								
						break;											
				}
				
				if (druzice_r[pom1_1][pom2_1] == druzice_t[pom1_2][pom2_2])		
				{
		// ČÁST 3: PŘIŘAZENÍ OBSERVACÍ
					omezeni_skut[i][j] = 0;
					druzice_SD[i][j] = druzice_r[pom1_1][pom2_1];						// je jedno jestli _r nebo _t ....

					for (k = 0; k < omezeni; k++)						
					{	
						if (observace_r[pom1_1][pom2_1][pom3_1[k]] > 0 && observace_t[pom1_2][pom2_2][pom3_2[k]] > 0)
						{
							dif_observace_ref[i][j][k] = observace_r[pom1_1][pom2_1][pom3_1[k]];
							dif_observace_test[i][j][k] = observace_t[pom1_2][pom2_2][pom3_2[k]];	
							omezeni_skut[i][j]++;
						}
						else
							continue;
					}
					pocitadlo_druzic[i]++;					
					j++;
				}
			}
			pom1_1++;										
		    pom1_2++;
			pocitadlo_bloku++;
		}

		cas_razeni = new int [pocitadlo_bloku];						
	
		if (cas_r[0] < cas_t[0])
			cas_razeni = cas_t;
		else
			cas_razeni = cas_r;
	}
	
	else
		cout << "NELZE VYHODNOTIT - NEKORESPONDUJICI DATA!!" << endl;

}

//funkce pro přiřazení hodnot azimutů a elevací k jednotlivým TDSD:
void Rinex::LOS(int bloky, int *druzice, int **obs_typy,  int *cas_razeni, double ***dif_obs_r, double ***dif_obs_t, string **druzice_SD, double *azimut_zarizeni, double *elevace_zarizeni)
{
	cout << "Importuji azimuty a elevace k jednotlivym druzicim ..." << endl;
	
	string satpos, nazev_sat, cas_par_str_cely, radek_parametru, azimut_str, zenit_str, citac, cas_par_str;
	string druz_x_str, druz_y_str, druz_z_str;
	string *cas_par_cast = new string [3];	
	int pocet_radku_parametru, pom1, pom2, cas_par;
	double azimut, zenit, druz_x, druz_y, druz_z, ref_x, ref_y, ref_z, st_test_x, st_test_y, st_test_z;
	double st_test_B, st_test_L, st_test_Hel;
	double rychlost_svetla = 299.792458;

	double vlnova_delka[3] = {rychlost_svetla/1575.42, rychlost_svetla/1227.60, rychlost_svetla/1176.45};

	double *test_x = new double [bloky];
	double *test_y = new double [bloky];
	double *test_z = new double [bloky];

	double a = 6378137;
	double b = 6356752.31425;
	double e_2 = (pow(a,2)-pow(b,2))/pow(a,2);

	cout << "Zadejte pravouhle prostorove souradnice bodu s referencni aparaturou:" << endl << "X: ";
	cin >> ref_x;
	cout << "Y: ";
	cin >> ref_y;
	cout << "Z: ";
	cin >> ref_z;

	cout << "Zadejte pravouhle prostorove souradnice bodu s testovanou aparaturou:" << endl << "X: ";
	cin >> st_test_x;
	cout << "Y: ";
	cin >> st_test_y;
	cout << "Z: ";
	cin >> st_test_z;

	// výpočet zeměpisných souřadnic stanoviska s testovanou aparaturou:
	st_test_L = (atan(st_test_y/st_test_x))*(180/M_PI);
	
	double p = sqrt(pow(st_test_y,2)+pow(st_test_x,2));
	double *B_pom = new double [20];
	double *Hel_pom = new double [20];
	double *N_pom = new double [20];
	
	B_pom[0] = (atan((1/(1-e_2))*(st_test_z/p)))*(180/M_PI);
	N_pom[0] = a/sqrt(1-e_2*pow(sin(B_pom[0]/(180/M_PI)),2));
	Hel_pom[0] = 0;

	for (int n = 0; ;n++)
	{
		B_pom[n+1] = (atan(st_test_z/(p*(1-e_2*(N_pom[n]/(N_pom[n]+Hel_pom[n]))))))*(180/M_PI);
		N_pom[n+1] = a/sqrt(1-e_2*pow(sin(B_pom[n+1]/(180/M_PI)),2));
		Hel_pom[n+1] = p/(cos(B_pom[n+1]/(180/M_PI)))-N_pom[n+1];
		
		if(abs(B_pom[n+1]-B_pom[n])<=0.000000003 && abs(Hel_pom[n+1]-Hel_pom[n])<=0.0001)
		{
			st_test_B = B_pom[n+1];
			st_test_Hel = Hel_pom[n+1];
			break;
		}
		else
			continue;
	}

	// výpočet aktuálních souřadnic ARP v závislosti na natočení polohovacího ramene
	TMatice translace(3,1), Rw1(3,3), matice1(3,1), matice2(3,1);
	TMatice X0(3,1), Rz(3,3), Ry(3,3), S2(3,3), R(3,3);

	translace.set(0,0,0.00034);	//e1N
	translace.set(1,0,0.00027);	//e1E
	translace.set(2,0,0.27594+0.05230);	//k+h

	Rw1.set(0,0,1);	Rw1.set(0,1,0); Rw1.set(0,2,0);
	Rw1.set(1,0,0); Rw1.set(1,1,cos((100.069-100)/(200/M_PI))); Rw1.set(1,2,sin((100.069-100)/(200/M_PI)));
	Rw1.set(2,0,0); Rw1.set(2,1,-sin((100.069-100)/(200/M_PI))); Rw1.set(2,2,cos((100.069-100)/(200/M_PI)));

	matice1.set(0,0,-0.00004); matice1.set(1,0,0);	matice1.set(2,0,0);
	
	matice2.set(0,0,0); 
	matice2.set(1,0,0.00020-(0.21240-0.05230-0.02113)*((100.069-100)/(200/M_PI)));
	matice2.set(2,0,0.21240-0.05230-0.02113);

	//matice pro transformaci:
	X0.set(0,0,st_test_x); X0.set(1,0,st_test_y); X0.set(2,0,st_test_z);

	Rz.set(0,0,cos((180-st_test_L)/(180/M_PI))); Rz.set(0,1,sin((180-st_test_L)/(180/M_PI))); Rz.set(0,2,0);
	Rz.set(1,0,-sin((180-st_test_L)/(180/M_PI))); Rz.set(1,1,cos((180-st_test_L)/(180/M_PI))); Rz.set(1,2,0);
	Rz.set(2,0,0); Rz.set(2,1,0); Rz.set(2,2,1);

	Ry.set(0,0,cos((90-st_test_B)/(180/M_PI))); Ry.set(0,1,0); Ry.set(0,2,-sin((90-st_test_B)/(180/M_PI))); 
	Ry.set(1,0,0); Ry.set(1,1,1); Ry.set(1,2,0);
	Ry.set(2,0,sin((90-st_test_B)/(180/M_PI))); Ry.set(2,1,0); Ry.set(2,2,cos((90-st_test_B)/(180/M_PI))); 

	S2.set(0,0,1); S2.set(0,1,0); S2.set(0,2,0); 
	S2.set(1,0,0); S2.set(1,1,-1); S2.set(1,2,0); 
	S2.set(2,0,0); S2.set(2,1,0); S2.set(2,2,1); 

	R = Rz.soucin(Ry);
	R = R.soucin(S2);

	for (int i = 0; i < bloky; i++)
	{
		TMatice ARP_NEU(3,1), ARP(3,1), RM1(3,3), RM2(3,3), pom_matice1(3,3), pom_matice2(3,1);

		RM1.set(0,0,cos(azimut_zarizeni[i]/(180/M_PI)));	RM1.set(0,1,sin(azimut_zarizeni[i]/(180/M_PI)));	RM1.set(0,2,0);
		RM1.set(1,0,-sin(azimut_zarizeni[i]/(180/M_PI)));	RM1.set(1,1,cos(azimut_zarizeni[i]/(180/M_PI)));	RM1.set(1,2,0);
		RM1.set(2,0,0);	RM1.set(2,1,0);	RM1.set(2,2,1);

		RM2.set(0,0,cos(elevace_zarizeni[i]/(180/M_PI)));	RM2.set(0,1,0);		RM2.set(0,2,-sin(elevace_zarizeni[i]/(180/M_PI)));
		RM2.set(1,0,0);	RM2.set(1,1,-1); RM2.set(1,2,0);
		RM2.set(2,0,sin(elevace_zarizeni[i]/(180/M_PI)));	RM2.set(2,1,0);		RM2.set(2,2,cos(elevace_zarizeni[i]/(180/M_PI)));

		pom_matice1 = RM2.soucin(Rw1);
		pom_matice2 = pom_matice1.soucin(matice2);
		pom_matice2 = matice1 + pom_matice2;
		pom_matice2 = RM1.soucin(pom_matice2);

		ARP_NEU = translace + pom_matice2;
		ARP_NEU.set(2,0,ARP_NEU.get(2,0)+0.02420);			// navýšení o převýšení výšky přístroje nad pilířem
		
		// transformace lokální topocentrické soustavy NEU do pravouhlého prostorového souřadnicového systému:
		ARP = R.soucin(ARP_NEU);
		ARP = X0 + ARP;

		// převod do proměnných, se kterými se dá v rámci třídy Rinex dále pracovat:
		test_x[i] = ARP.get(0,0);
		test_y[i] = ARP.get(1,0);
		test_z[i] = ARP.get(2,0);
	}
	
	pocet_druzic_SD = new int[bloky];
	
	double **korekce_ref = new double *[bloky];
	for (int i = 0; i < bloky; i++)
		korekce_ref[i] = new double[druzice[i]];

	double **korekce_test = new double *[bloky];
	for (int i = 0; i < bloky; i++)
		korekce_test[i] = new double[druzice[i]];

	razeni_azimut = new double *[bloky];			
	for (int i = 0; i < bloky; i++)
		razeni_azimut[i] = new double [druzice[i]];
			
	razeni_elevace = new double *[bloky];			
	for (int i = 0; i < bloky; i++)
		razeni_elevace[i] = new double [druzice[i]];

	dif_observace_ref_kor = new double **[bloky];
	for (int i = 0; i < bloky; i++)
	{
		dif_observace_ref_kor[i] = new double *[druzice[i]];
			
		for (int j = 0; j < druzice[i]; j++)					
			dif_observace_ref_kor[i][j] = new double [obs_typy[i][j]];		
	}
		
	dif_observace_test_kor = new double **[bloky];
	for (int i = 0; i < bloky; i++)
	{
		dif_observace_test_kor[i] = new double *[druzice[i]];
			
		for (int j = 0; j < druzice[i]; j++)					
			dif_observace_test_kor[i][j] = new double [obs_typy[i][j]];		
	}
		
	for (int sat = 1; sat <= 32; sat++)										// ... sat = družice
	{
		if (sat < 10)
		{
			satpos = "SatPos_G0" + to_string(sat) + ".txt";
			nazev_sat = "G0" + to_string(sat);
		}

		else
		{
			satpos = "SatPos_G" + to_string(sat) + ".txt";
			nazev_sat = "G" + to_string(sat);
		}

		parametry.open(satpos);

		//tato smyčka přiřazuje jednotlivým TDSD příslušné azimuty a elevace
		for (int i = 0; i < bloky; i++)										// ... i = časové bloky	
			for (int j = 0; j < druzice[i]; j++)							// ... j = jednotlivé družice
			{
				if (druzice_SD[i][j] != nazev_sat)
					continue;

				else
				{
					for (int m = 0; !parametry.eof(); m++)
					{
						while (odpad != "time:")
							parametry >> odpad;
		
						parametry >> odpad >> cas_par_str_cely >> odpad >> odpad >> azimut_str >> odpad >> odpad >> zenit_str;
						parametry >> odpad >> odpad >> druz_x_str >> odpad >> odpad >> druz_y_str >> odpad >> odpad >> druz_z_str;

						azimut = stod(azimut_str);
						zenit = stod(zenit_str);
						druz_x = stod(druz_x_str);
						druz_y = stod(druz_y_str);
						druz_z = stod(druz_z_str);

						//konverze formátu času použitého ve výstupu programu Ing. Kuruce, Ph.D. na tvar času, který používám já
						pom1 = 0;
	
						for (int n = 0; n < 3; n++)
						{
							pom2 = 0;
					
							while (citac != ":" && citac != ",")
							{
								citac = cas_par_str_cely.substr(pom1,1);
								cas_par_cast[n] += citac; 
								pom1++;
								pom2++;
							}

							if (pom2 == 2)
								cas_par_cast[n] = "0" + cas_par_cast[n];
		
							cas_par_cast[n].resize(cas_par_cast[n].length()-1);
	
							cas_par_str += cas_par_cast[n];

							cas_par_cast[n].clear();
							citac.clear();
						}
						cas_par = stoi(cas_par_str);
						cas_par_str.clear();

						if (cas_par == cas_razeni[i])
						{
							razeni_azimut[i][j] = azimut;
							razeni_elevace[i][j] = 90 - zenit;					//přepočet na elevaci
							
							korekce_ref[i][j] = sqrt(pow((druz_x - ref_x),2) + pow((druz_y - ref_y),2) + pow((druz_z - ref_z),2));
							korekce_test[i][j] = sqrt(pow((druz_x - test_x[i]),2) + pow((druz_y - test_y[i]),2) + pow((druz_z - test_z[i]),2));
							
							pocet_druzic_SD[i] = j + 1;
							break;
						}

						else
							continue;
					}
				}
			}
	parametry.close();
	}

	for (int i = 0; i < bloky; i++)													
		for (int j = 0; j < pocet_druzic_SD[i]; j++)
			for (int k = 0; k < obs_typy[i][j]; k++)
			{
				dif_observace_ref_kor[i][j][k] = dif_observace_ref[i][j][k]*vlnova_delka[k] - korekce_ref[i][j];
				dif_observace_test_kor[i][j][k] = dif_observace_test[i][j][k]*vlnova_delka[k] - korekce_test[i][j];
			}
}

// funkce provádějící jednoduché diference:
void Rinex::SD(int bloky, int *druzice, int **obs_typy, double ***dif_obs_r, double ***dif_obs_t, int *cas_razeni, double **azimut_razeni, double **elevace_razeni)
{
	cout << "Provadim jednoduche diference ..." << endl;
	jednoducha_diference = new double **[bloky];
		for (int i = 0; i < bloky; i++)
		{
			jednoducha_diference[i] = new double *[druzice[i]];
			
			for (int j = 0; j < druzice[i]; j++)					
				jednoducha_diference[i][j] = new double [obs_typy[i][j]];		
		}
	
	cas_SD = new int [bloky];

	SD_azimut = new double *[bloky];			
	for (int i = 0; i < bloky; i++)
		SD_azimut[i] = new double [druzice[i]];
			
	SD_elevace = new double *[bloky];			
	for (int i = 0; i < bloky; i++)
		SD_elevace[i] = new double [druzice[i]];

	for (int i = 0; i < bloky; i++)														
		for (int j = 0; j < druzice[i]; j++)
			for (int k = 0; k < obs_typy[i][j]; k++)
			{
				jednoducha_diference[i][j][k] = dif_obs_t[i][j][k] - dif_obs_r[i][j][k];
				cas_SD[i] = cas_razeni[i];
				SD_azimut[i][j] = azimut_razeni[i][j];
				SD_elevace[i][j] = elevace_razeni[i][j];				
			}
}

//funkce provádějící časové diference jednoduchých diferencí:
void Rinex::TDSD(int bloky, int *druzice, int **obs_typy, double ***j_diference, int *cas_SD, string **druzice_SD, double **azimut_SD, double **elevace_SD)
{
	cout << "Provadim casove diference jednoduchych diferenci ..." << endl;

	casova_diference = new double **[bloky-1];			
	for (int i = 0; i < bloky-1; i++)
	{
		casova_diference[i] = new double *[druzice[i]];
			
		for (int j = 0; j < druzice[i]; j++)					
			casova_diference[i][j] = new double [obs_typy[i][j]];		
	}
	
	cas_TDSD = new int [bloky-1];
	pocet_druzic_TDSD = new int [bloky-1];

	TDSD_azimut = new double *[bloky-1];			
	for (int i = 0; i < bloky-1; i++)
		TDSD_azimut[i] = new double [druzice[i]];
			
	TDSD_elevace = new double *[bloky-1];			
	for (int i = 0; i < bloky-1; i++)
		TDSD_elevace[i] = new double [druzice[i]];

	druzice_TDSD = new string *[bloky-1];
		for (int i = 0; i < bloky-1; i++)
			druzice_TDSD[i] = new string [druzice[i]];

	int pom1, pom2;

	for (int i = 0; i < bloky-1; i++)									
	{
		pom2 = 0;
		pocet_druzic_TDSD[i] = 0;

		for (int j = 0;; j++)						
		{
			if (pom2 == druzice[i+1])
				break;

			pom1 = 0;
				
			while (druzice_SD[i][pom1] !=  druzice_SD[i+1][pom2])
			{
				pom1++;

				if (pom1 == druzice[i])
					break;
			}

			if (druzice_SD[i][pom1] == druzice_SD[i+1][pom2])
			{
				druzice_SD[i][j] = druzice_SD[i][pom1];
				
				if (obs_typy[i][pom1] > obs_typy[i+1][pom2])		
					obs_typy[i][pom1] = obs_typy[i+1][pom2];

				for (int k = 0; k < obs_typy[i][pom1]; k++)						
				{
					if (abs(j_diference[i][pom1][k] - j_diference[i+1][pom2][k]) < abs(j_diference[i][pom1][k]) && abs(j_diference[i][pom1][k] - j_diference[i+1][pom2][k]) < abs(j_diference[i+1][pom2][k]))					
					{
						casova_diference[i][j][k] = j_diference[i][pom1][k] - j_diference[i+1][pom2][k];
						cas_TDSD[i] = cas_SD[i];
						obs_typy[i][j] = obs_typy[i][pom1];
						druzice_TDSD[i][j] = druzice_SD[i][j];
						TDSD_azimut[i][j] = azimut_SD[i][pom1];						
						TDSD_elevace[i][j] = elevace_SD[i][pom1];
					}
					
					else
						continue;
				}
				pocet_druzic_TDSD[i]++;
				pom2++;
			}

			else
			{
				j--;
				pom2++;
				continue;
			}
		}
	}
}

// funkce provádějící první stupeň vyrovnání MNČ:
void Rinex::vyrovnej_1(int bloky, int *druzice, int **obs_typy, double ***casova_diference, double *azimut_zarizeni, double *elevace_zarizeni, double **azimut_TDSD, double **elevace_TDSD, int omezeni, string *fazove_mereni)
{
	double stan_maska;
	
	cout << "Zadejte hodnotu stanicni elevacni masky [deg]:" <<endl;
	cin >> stan_maska;
	
	cout << "Provadim prvni stupen vyrovnani MNC ... " << endl;

	double ***casova_diference_15deg = new double **[bloky-1];			
	for (int i = 0; i < bloky-1; i++)
	{
		casova_diference_15deg[i] = new double *[druzice[i]];
			
		for (int j = 0; j < druzice[i]; j++)					
			casova_diference_15deg[i][j] = new double [obs_typy[i][j]];		
	}

	PCO = new double *[omezeni];			
	for (int i = 0; i < omezeni; i++)
		PCO[i] = new double [3];

	ant_azimut = new double *[bloky-1];			
	for (int i = 0; i < bloky-1; i++)
		ant_azimut[i] = new double [druzice[i]];
	
	double **ant_azimut_15deg = new double *[bloky-1];			
	for (int i = 0; i < bloky-1; i++)
		ant_azimut_15deg[i] = new double [druzice[i]];

	ant_elevace = new double *[bloky-1];			
	for (int i = 0; i < bloky-1; i++)
		ant_elevace[i] = new double [druzice[i]];

	double **ant_elevace_15deg = new double *[bloky-1];			
	for (int i = 0; i < bloky-1; i++)
		ant_elevace_15deg[i] = new double [druzice[i]];

	double **elevace_TDSD_pom = new double *[bloky-1];			
	for (int i = 0; i < bloky-1; i++)
		elevace_TDSD_pom[i] = new double [druzice[i]];

	int *pocet_druzic_TDSD_15deg = new int[bloky];

	int *pocet_mereni = new int[omezeni];

	for (int i = 0; i < bloky-1; i++)				
		for (int j = 0; j < druzice[i]; j++)
		{			
			if (azimut_TDSD[i][j] > 90 && azimut_TDSD[i][j] < 270)
				elevace_TDSD_pom[i][j] = 180 - elevace_TDSD[i][j];
			else
				elevace_TDSD_pom[i][j] = elevace_TDSD[i][j];

			ant_elevace[i][j] = 90 - elevace_zarizeni[i] + elevace_TDSD_pom[i][j];
			ant_azimut[i][j] = azimut_TDSD[i][j] - azimut_zarizeni[i];

			if(ant_elevace[i][j] > 90)
				ant_elevace[i][j] = 180 - ant_elevace[i][j];

			if(ant_azimut[i][j] < 0)
				ant_azimut[i][j] = ant_azimut[i][j] + 360;
		}

	for (int i = 0; i < bloky-1; i++)	
	{
		pocet_druzic_TDSD_15deg[i] = 0;
		int j = 0;

		for (int pom = 0; pom < druzice[i]; pom++)
		{
			if (ant_elevace[i][pom] > 15 && elevace_TDSD[i][pom] > stan_maska)
			{
				for (int k = 0; k < obs_typy[i][j]; k++)
				{
					casova_diference_15deg[i][j][k] = casova_diference[i][pom][k];
					ant_elevace_15deg[i][j] = ant_elevace[i][pom];
					ant_azimut_15deg[i][j] = ant_azimut[i][pom];
				}
				j++;
				pocet_druzic_TDSD_15deg[i]++;
			}
			else
				continue;
		}
	}

	// Zjištění rozměru:
	for (int k = 0; k < omezeni; k++)								
	{
		pocet_mereni[k] = 0;

		for (int i = 0; i < bloky-1; i++)
		{
			for (int j = 0; j < pocet_druzic_TDSD_15deg[i]; j++)			
			{
				if (casova_diference_15deg[i][j][k]	> -50000000000)		
					pocet_mereni[k]++;	
					
				else
					continue;
			}
		}
	}

	double **vyr_hodnota_TDSD = new double*[omezeni];
	for (int i = 0; i < omezeni; i++)
		vyr_hodnota_TDSD[i] = new double[pocet_mereni[i]];

	double **vyr_hodnota_elevace = new double*[omezeni];
	for (int i = 0; i < omezeni; i++)
		vyr_hodnota_elevace[i] = new double[pocet_mereni[i]];

	double **vyr_hodnota_azimut = new double*[omezeni];
	for (int i = 0; i < omezeni; i++)
		vyr_hodnota_azimut[i] = new double[pocet_mereni[i]];

	double *x0 = new double [omezeni];
	double *y0 = new double [omezeni];
	double *z0 = new double [omezeni];

	// přeorganizování proměnných:
	for (int k = 0; k < omezeni; k++)
	{
		int n = 0;

		for (int i = 0; i < bloky-1; i++)
			for (int j = 0; j < pocet_druzic_TDSD_15deg[i]; j++)			
			{
				if (casova_diference_15deg[i][j][k]	> -50000000000)		
				{
					vyr_hodnota_TDSD[k][n] = casova_diference_15deg[i][j][k];
					vyr_hodnota_azimut[k][n] = ant_azimut_15deg[i][j]/(180/M_PI);
					vyr_hodnota_elevace[k][n] = ant_elevace_15deg[i][j]/(180/M_PI);
					n++;
				}

				else
					continue;
			}
	}

	// vyloučení odlehlých hodnot TDSD:
	double *prumer = new double[omezeni];
	double *odchylka = new double[omezeni];
		
	for (int k = 0; k < omezeni; k++)
	{
		prumer[k] = 0;
		odchylka[k] = 0;

		for(int n = 0; n < pocet_mereni[k]; n++)
			prumer[k] += vyr_hodnota_TDSD[k][n];

		prumer[k] = prumer[k]/pocet_mereni[k];

		for(int n = 0; n < pocet_mereni[k]; n++)
			odchylka[k] += pow((prumer[k] - vyr_hodnota_TDSD[k][n]),2);

		odchylka[k] = sqrt(odchylka[k]/(pocet_mereni[k]-1));

		int pom = 0;
		for (int n = 0; n < pocet_mereni[k]; n++)
		{
			if (abs(vyr_hodnota_TDSD[k][n]) < odchylka[k])
			{
				vyr_hodnota_TDSD[k][pom] = vyr_hodnota_TDSD[k][n];
				vyr_hodnota_azimut[k][pom] = vyr_hodnota_azimut[k][n];
				vyr_hodnota_elevace[k][pom] = vyr_hodnota_elevace[k][n];
				pom++;
			}
			else
				continue;
		}
		pocet_mereni[k] = pom;
	}

	string sour[] = {"N","E","U"};

	ofstream vypis_PCO;
	vypis_PCO.open("Hodnoty_PCO.txt");

	// PRVNÍ STUPEŃ VYROVNÁNÍ
	for (int k = 0; k < omezeni; k++)						 // pro každou frekvenci zvlášť celé znova:
	{
		cout << "Zadejte slozky PCO pro frekvenci " << fazove_mereni[k] << " dle NGS [mm]:" << endl << "N: ";
		cin >> x0[k];
		cout << "E: ";
		cin >> y0[k];
		cout << "U: ";
		cin >> z0[k];

		vypis_PCO << "Vysledne PCO pro frekvenci " << fazove_mereni[k] << " [m]:" << endl;
		
		// 1) alokace paměti pro všechny zúčastněné matice:
		TMatice A(pocet_mereni[k],3), AT(3,pocet_mereni[k]), l´(pocet_mereni[k],1), dX(3,1);
		TMatice N(3,3), N_inv(3,3), N_inv_zap(3,3), y(3,1), X(3,1), X0(3,1);
		
		// 2) nastavení matice plánu A:
		for (int r = 0; r < pocet_mereni[k]; r++)
		{
			A.set(r,0,cos(vyr_hodnota_elevace[k][r])*cos(vyr_hodnota_azimut[k][r]) - cos(vyr_hodnota_elevace[k][r+1])*cos(vyr_hodnota_azimut[k][r+1]));
			A.set(r,1,cos(vyr_hodnota_elevace[k][r])*sin(vyr_hodnota_azimut[k][r]) - cos(vyr_hodnota_elevace[k][r+1])*sin(vyr_hodnota_azimut[k][r+1]));
			A.set(r,2,sin(vyr_hodnota_elevace[k][r]) - sin(vyr_hodnota_elevace[k][r+1]));
		}

		// 3) nastavení transponované matice plánu AT:
		AT = A.transpozice();
		
		// 4) nastavení matice redukovaných měření l´:
		X0.set(0,0,x0[k]/1000); X0.set(1,0,y0[k]/1000); X0.set(2,0,z0[k]/1000);
		
		for (int r = 0; r < pocet_mereni[k]; r++)
			l´.set(r,0,(cos(vyr_hodnota_elevace[k][r])*cos(vyr_hodnota_azimut[k][r]) - cos(vyr_hodnota_elevace[k][r+1])*cos(vyr_hodnota_azimut[k][r+1]))
				*X0.get(0,0) + (cos(vyr_hodnota_elevace[k][r])*sin(vyr_hodnota_azimut[k][r]) - cos(vyr_hodnota_elevace[k][r+1])*sin(vyr_hodnota_azimut[k][r+1]))
				*X0.get(1,0) + (sin(vyr_hodnota_elevace[k][r]) - sin(vyr_hodnota_elevace[k][r+1]))*X0.get(2,0)-vyr_hodnota_TDSD[k][r]);

		// 5) výpočet matice koeficientů normálních rovnic:
		N = AT.soucin(A);
		
		// 6) výpočet matice y:
		y = AT.soucin(l´);
     
		// 7) výpočet inverzní matice k matici N:
		N_inv = N.inverze();
		
		// 8) Výpočet přírůstků neznámých:
		N_inv_zap = -N_inv;

		dX = N_inv_zap.soucin(y);
		
		// 9) Výpočet neznámých
		X = X0 + dX;
		
		// 10) Přepočet do proměnné, se kterou se dá v rámci třídy Rinex dále pracovat:
		int j = 0;
		for (int i = 0; i < 3; i++)
		{
			PCO[k][i] = X.get(i,j);
			vypis_PCO << sour[i] << ": " << fixed << setprecision(4) << PCO[k][i] << endl;
		}
		vypis_PCO << endl;
	}
	vypis_PCO.close();
	cout << "Hodnoty slozek PCO byly vypsany do souboru Hodnoty_PCO.txt" << endl;
}

// funkce provádějící druhý stupeň vyrovnání MNČ:
void Rinex::vyrovnej_2(int bloky, int *druzice, double ***casova_diference, double **ant_azimut, double **ant_elevace, int omezeni, string *fazove_mereni, double **PCO)
{
	cout << "Provadim druhy stupen vyrovnani MNC ... " << endl;

	int *pocet_mereni = new int[omezeni];

	// Zjištění rozměru:
	for (int k = 0; k < omezeni; k++)								
	{
		pocet_mereni[k] = 0;

		for (int i = 0; i < bloky-1; i++)
		{
			for (int j = 0; j < druzice[i]; j++)			
			{
				if (casova_diference[i][j][k] > -50000000000 && ant_azimut[i][j] > -360 && ant_elevace[i][j] > -90)	
					pocet_mereni[k]++;	

				else
					continue;
			}
		}
	}

	double **vyr_hodnota_TDSD = new double*[omezeni];
	for (int i = 0; i < omezeni; i++)
		vyr_hodnota_TDSD[i] = new double[pocet_mereni[i]];

	double **vyr_dTDSD = new double*[omezeni];
	for (int i = 0; i < omezeni; i++)
		vyr_dTDSD[i] = new double[pocet_mereni[i]];

	double **vyr_el = new double*[omezeni];
	for (int i = 0; i < omezeni; i++)
		vyr_el[i] = new double[pocet_mereni[i]];

	double **vyr_az = new double*[omezeni];
	for (int i = 0; i < omezeni; i++)
		vyr_az[i] = new double[pocet_mereni[i]];

	// přeorganizování proměnných
	for (int k = 0; k < omezeni; k++)
	{
		int n = 0;

		for (int i = 0; i < bloky-1; i++)
			for (int j = 0; j < druzice[i]; j++)			
			{
				if (casova_diference[i][j][k] > -50000000000 && ant_azimut[i][j] > -360 && ant_elevace[i][j] > -90)		
				{
					vyr_hodnota_TDSD[k][n] = casova_diference[i][j][k];
					vyr_az[k][n] = ant_azimut[i][j]/(180/M_PI);
					vyr_el[k][n] = ant_elevace[i][j]/(180/M_PI);
					n++;
				}

				else
					continue;
			}
	}

	// vyloučení odlehlých hodnot TDSD
	double *prumer = new double[omezeni];
	double *odchylka = new double[omezeni];
		
	for (int k = 0; k < omezeni; k++)
	{
		prumer[k] = 0;
		odchylka[k] = 0;

		for(int n = 0; n < pocet_mereni[k]; n++)
			prumer[k] += vyr_hodnota_TDSD[k][n];

		prumer[k] = prumer[k]/pocet_mereni[k];

		for(int n = 0; n < pocet_mereni[k]; n++)
			odchylka[k] += pow((prumer[k] - vyr_hodnota_TDSD[k][n]),2);

		odchylka[k] = sqrt(odchylka[k]/(pocet_mereni[k]-1));

		int pom = 0;
		for (int n = 0; n < pocet_mereni[k]; n++)
		{
			if (abs(vyr_hodnota_TDSD[k][n]) < odchylka[k])
			{
				vyr_hodnota_TDSD[k][pom] = vyr_hodnota_TDSD[k][n];
				vyr_az[k][pom] = vyr_az[k][n];
				vyr_el[k][pom] = vyr_el[k][n];
				pom++;
			}
			else
				continue;
		}
		pocet_mereni[k] = pom;
	}

	koef_PCV = new double *[omezeni];			
	for (int i = 0; i < omezeni; i++)
		koef_PCV[i] = new double [68];

	ofstream vypis_PCV;
	vypis_PCV.open("Hodnoty_PCV.txt");

	// DRUHÝ STUPEŃ VYROVNÁNÍ
	for (int k = 0; k < omezeni; k++)						 // pro každou frekvenci zvlášť celé znova:
	{
		vypis_PCV << "Hodnoty PCV pro frekvenci " << fazove_mereni[k] << " : [m]" << endl << endl;

		// 1) alokace paměti pro všechny zúčastněné matice:
		TMatice A(pocet_mereni[k],68), AT(68,pocet_mereni[k]), l´(pocet_mereni[k],1), X(68,1), dX(68,1);
		TMatice N(68,68), N_inv(68,68), N_inv_zap(68,68), y(68,1);

		// 2) nastavení matice plánu A:
		for (int r = 0; r < pocet_mereni[k]; r++)
		{
			A.set(r,0,sin(vyr_el[k][r]) - sin(vyr_el[k][r+1])); //d a1,0
			A.set(r,1,-cos(vyr_el[k][r])*cos(vyr_az[k][r])+cos(vyr_el[k][r+1])*cos(vyr_az[k][r+1])); // d a1,1
			A.set(r,2,-cos(vyr_el[k][r])*sin(vyr_az[k][r])+cos(vyr_el[k][r+1])*sin(vyr_az[k][r+1])); // d b1,1
			
			A.set(r,3,(3*pow(sin(vyr_el[k][r]),2)-1)-(3*pow(sin(vyr_el[k][r+1]),2)-1)); // d a2,0
			A.set(r,4,-6*sin(vyr_el[k][r])*cos(vyr_el[k][r])*cos(vyr_az[k][r])+6*sin(vyr_el[k][r+1])*cos(vyr_el[k][r+1])*cos(vyr_az[k][r+1])); // d a2,1
			A.set(r,5,-6*sin(vyr_el[k][r])*cos(vyr_el[k][r])*sin(vyr_az[k][r])+6*sin(vyr_el[k][r+1])*cos(vyr_el[k][r+1])*sin(vyr_az[k][r+1])); // d b2,1
			A.set(r,6,6*pow(cos(vyr_el[k][r]),2)*cos(2*vyr_az[k][r])-6*pow(cos(vyr_el[k][r+1]),2)*cos(2*vyr_az[k][r+1])); // d a2,2
			A.set(r,7,6*pow(cos(vyr_el[k][r]),2)*sin(2*vyr_az[k][r])-6*pow(cos(vyr_el[k][r+1]),2)*sin(2*vyr_az[k][r+1])); // d b2,2

			A.set(r,8,(3/2*sin(vyr_el[k][r])*(pow(sin(vyr_el[k][r]),2)-1)+48*pow(sin(vyr_el[k][r]),3))-(3/2*sin(vyr_el[k][r+1])*
				(pow(sin(vyr_el[k][r+1]),2)-1)+48*pow(sin(vyr_el[k][r+1]),3))); // d a3,0
			A.set(r,9,(-cos(vyr_el[k][r])*(4*pow(sin(vyr_el[k][r]),2)+3/2*(pow(sin(vyr_el[k][r]),2)-1))*cos(vyr_az[k][r]))-(-cos(vyr_el[k][r+1])
				*(4*pow(sin(vyr_el[k][r+1]),2)+3/2*(pow(sin(vyr_el[k][r+1]),2)-1))*cos(vyr_az[k][r+1]))); // d a3,1
			A.set(r,10,(-cos(vyr_el[k][r])*(4*pow(sin(vyr_el[k][r]),2)+3/2*(pow(sin(vyr_el[k][r]),2)-1))*sin(vyr_az[k][r]))-(-cos(vyr_el[k][r+1])
				*(4*pow(sin(vyr_el[k][r+1]),2)+3/2*(pow(sin(vyr_el[k][r+1]),2)-1))*sin(vyr_az[k][r+1]))); // d b3,1
			A.set(r,11,15*pow(cos(vyr_el[k][r]),2)*sin(vyr_el[k][r])*cos(2*vyr_az[k][r])-15*pow(cos(vyr_el[k][r+1]),2)*sin(vyr_el[k][r+1])
				*cos(2*vyr_az[k][r+1])); // d a3,2
			A.set(r,12,15*pow(cos(vyr_el[k][r]),2)*sin(vyr_el[k][r])*sin(2*vyr_az[k][r])-15*pow(cos(vyr_el[k][r+1]),2)*sin(vyr_el[k][r+1])
				*sin(2*vyr_az[k][r+1])); // d b3,2
			A.set(r,13,-15*pow(cos(vyr_el[k][r]),3)*cos(3*vyr_az[k][r])+15*pow(cos(vyr_el[k][r+1]),3)*cos(3*vyr_az[k][r+1])); // d a3,3
			A.set(r,14,-15*pow(cos(vyr_el[k][r]),3)*sin(3*vyr_az[k][r])+15*pow(cos(vyr_el[k][r+1]),3)*sin(3*vyr_az[k][r+1])); // d b3,3

			A.set(r,15,(3/8*pow((pow(sin(vyr_el[k][r]),2)-1),2)+3*pow(sin(vyr_el[k][r]),2)*(pow(sin(vyr_el[k][r]),2)-1)+pow(sin(vyr_el[k][r]),4))
				-(3/8*pow((pow(sin(vyr_el[k][r+1]),2)-1),2)+3*pow(sin(vyr_el[k][r+1]),2)*(pow(sin(vyr_el[k][r+1]),2)-1)
				+pow(sin(vyr_el[k][r+1]),4))); // d a4,0
			A.set(r,16,(-5/2*cos(vyr_el[k][r])*(4*pow(sin(vyr_el[k][r]),3)+3*sin(vyr_el[k][r])*(pow(sin(vyr_el[k][r]),2)-1))*cos(vyr_az[k][r]))
				-(-5/2*cos(vyr_el[k][r+1])*(4*pow(sin(vyr_el[k][r+1]),3)+3*sin(vyr_el[k][r+1])*(pow(sin(vyr_el[k][r+1]),2)-1))*cos(vyr_az[k][r+1]))); // d a4,1
			A.set(r,17,(-5/2*cos(vyr_el[k][r])*(4*pow(sin(vyr_el[k][r]),3)+3*sin(vyr_el[k][r])*(pow(sin(vyr_el[k][r]),2)-1))*sin(vyr_az[k][r]))
				-(-5/2*cos(vyr_el[k][r+1])*(4*pow(sin(vyr_el[k][r+1]),3)+3*sin(vyr_el[k][r+1])*(pow(sin(vyr_el[k][r+1]),2)-1))*sin(vyr_az[k][r+1]))); // d b4,1
			A.set(r,18,(pow(cos(vyr_el[k][r]),2)*(15/2*(pow(sin(vyr_el[k][r]),2)-1)+35*pow(sin(vyr_el[k][r]),2))*cos(vyr_az[k][r]))-
				(pow(cos(vyr_el[k][r+1]),2)*(15/2*(pow(sin(vyr_el[k][r+1]),2)-1)+35*pow(sin(vyr_el[k][r+1]),2))*cos(vyr_az[k][r+1]))); // d a4,2
			A.set(r,19,(pow(cos(vyr_el[k][r]),2)*(15/2*(pow(sin(vyr_el[k][r]),2)-1)+35*pow(sin(vyr_el[k][r]),2))*sin(vyr_az[k][r]))-
				(pow(cos(vyr_el[k][r+1]),2)*(15/2*(pow(sin(vyr_el[k][r+1]),2)-1)+35*pow(sin(vyr_el[k][r+1]),2))*sin(vyr_az[k][r+1]))); // d b4,2
			A.set(r,20,-85*pow(cos(vyr_el[k][r]),3)*sin(vyr_el[k][r])*cos(3*vyr_az[k][r])+85*pow(cos(vyr_el[k][r+1]),3)
				*sin(vyr_el[k][r+1])*cos(3*vyr_az[k][r+1])); // d a4,3
			A.set(r,21,-85*pow(cos(vyr_el[k][r]),3)*sin(vyr_el[k][r])*sin(3*vyr_az[k][r])+85*pow(cos(vyr_el[k][r+1]),3)
				*sin(vyr_el[k][r+1])*sin(3*vyr_az[k][r+1])); // d b4,3
			A.set(r,22,85*pow(cos(vyr_el[k][r]),4)*cos(4*vyr_az[k][r])-85*pow(cos(vyr_el[k][r+1]),4)*cos(4*vyr_az[k][r+1])); // d a4,4
			A.set(r,23,85*pow(cos(vyr_el[k][r]),4)*sin(4*vyr_az[k][r])-85*pow(cos(vyr_el[k][r+1]),4)*sin(4*vyr_az[k][r+1])); // d b4,4

			A.set(r,24,(15/8*sin(vyr_el[k][r])*pow((pow(sin(vyr_el[k][r]),2)-1),2)+5*pow(sin(vyr_el[k][r]),3)*
				(pow(sin(vyr_el[k][r]),2)-1)+pow(sin(vyr_el[k][r]),5))-(15/8*sin(vyr_el[k][r+1])*pow((pow(sin(vyr_el[k][r+1]),2)-1),2)
				+5*pow(sin(vyr_el[k][r+1]),3)*(pow(sin(vyr_el[k][r+1]),2)-1)+pow(sin(vyr_el[k][r+1]),5))); // d a5,0
			A.set(r,25,(-cos(vyr_el[k][r])*(15/8*pow((pow(sin(vyr_el[k][r]),2)-1),2)+45/2*pow(sin(vyr_el[k][r]),2)*(pow(sin(vyr_el[k][r]),2)-1)
				+15*pow(sin(vyr_el[k][r]),4))*cos(vyr_az[k][r]))-(-cos(vyr_el[k][r+1])*(15/8*pow((pow(sin(vyr_el[k][r+1]),2)-1),2)+
				45/2*pow(sin(vyr_el[k][r+1]),2)*(pow(sin(vyr_el[k][r+1]),2)-1)+15*pow(sin(vyr_el[k][r+1]),4))*cos(vyr_az[k][r+1]))); // d a5,1
			A.set(r,26,(-cos(vyr_el[k][r])*(15/8*pow((pow(sin(vyr_el[k][r]),2)-1),2)+45/2*pow(sin(vyr_el[k][r]),2)*(pow(sin(vyr_el[k][r]),2)-1)
				+15*pow(sin(vyr_el[k][r]),4))*sin(vyr_az[k][r]))-(-cos(vyr_el[k][r+1])*(15/8*pow((pow(sin(vyr_el[k][r+1]),2)-1),2)+
				45/2*pow(sin(vyr_el[k][r+1]),2)*(pow(sin(vyr_el[k][r+1]),2)-1)+15*pow(sin(vyr_el[k][r+1]),4))*sin(vyr_az[k][r+1]))); // d b5,1
			A.set(r,27,105*pow(cos(vyr_el[k][r]),2)*(pow(sin(vyr_el[k][r]),3)+1/2*sin(vyr_el[k][r])*(pow(sin(vyr_el[k][r]),2)-1))*cos(2*vyr_az[k][r])
				-105*pow(cos(vyr_el[k][r+1]),2)*(pow(sin(vyr_el[k][r+1]),3)+1/2*sin(vyr_el[k][r+1])*(pow(sin(vyr_el[k][r+1]),2)-1))*cos(2*vyr_az[k][r+1])); // d a5,2
			A.set(r,28,105*pow(cos(vyr_el[k][r]),2)*(pow(sin(vyr_el[k][r]),3)+1/2*sin(vyr_el[k][r])*(pow(sin(vyr_el[k][r]),2)-1))*sin(2*vyr_az[k][r])
				-105*pow(cos(vyr_el[k][r+1]),2)*(pow(sin(vyr_el[k][r+1]),3)+1/2*sin(vyr_el[k][r+1])*(pow(sin(vyr_el[k][r+1]),2)-1))*sin(2*vyr_az[k][r+1])); // d b5,2
			A.set(r,29,pow(cos(vyr_el[k][r]),3)*(420*pow(sin(vyr_el[k][r]),2)-105/2*(pow(sin(vyr_el[k][r]),2)-1))*cos(3*vyr_az[k][r])+pow(cos(vyr_el[k][r+1]),3)*
				(420*pow(sin(vyr_el[k][r+1]),2)-105/2*(pow(sin(vyr_el[k][r+1]),2)-1))*cos(3*vyr_az[k][r+1])); // d a5,3
			A.set(r,30,pow(cos(vyr_el[k][r]),3)*(420*pow(sin(vyr_el[k][r]),2)-105/2*(pow(sin(vyr_el[k][r]),2)-1))*sin(3*vyr_az[k][r])+pow(cos(vyr_el[k][r+1]),3)*
				(420*pow(sin(vyr_el[k][r+1]),2)-105/2*(pow(sin(vyr_el[k][r+1]),2)-1))*sin(3*vyr_az[k][r+1])); // d b5,3
			A.set(r,31,pow(cos(vyr_el[k][r]),4)*945*sin(vyr_el[k][r])*cos(4*vyr_az[k][r])-pow(cos(vyr_el[k][r+1]),4)*945*sin(vyr_el[k][r+1])*
				cos(4*vyr_az[k][r+1])); // d a5,4
			A.set(r,32,pow(cos(vyr_el[k][r]),4)*945*sin(vyr_el[k][r])*sin(4*vyr_az[k][r])-pow(cos(vyr_el[k][r+1]),4)*945*sin(vyr_el[k][r+1])*
				sin(4*vyr_az[k][r+1])); // d b5,4
			A.set(r,33,-945*pow(cos(vyr_el[k][r]),5)*cos(5*vyr_az[k][r])+945*pow(cos(vyr_el[k][r+1]),5)*cos(5*vyr_az[k][r+1])); // d a5,5
			A.set(r,34,-945*pow(cos(vyr_el[k][r]),5)*sin(5*vyr_az[k][r])+945*pow(cos(vyr_el[k][r+1]),5)*sin(5*vyr_az[k][r+1])); // d b5,5

			A.set(r,35,(5/16*pow((pow(sin(vyr_el[k][r]),2)-1),3)+45/8*pow(sin(vyr_el[k][r]),2)*pow((pow(sin(vyr_el[k][r]),2)-1),2)+
				15/2*pow(sin(vyr_el[k][r]),4)*(pow(sin(vyr_el[k][r]),2)-1)+pow(sin(vyr_el[k][r]),6))-(5/16*pow((pow(sin(vyr_el[k][r+1]),2)-1),3)
				+45/8*pow(sin(vyr_el[k][r+1]),2)*pow((pow(sin(vyr_el[k][r+1]),2)-1),2)+15/2*pow(sin(vyr_el[k][r+1]),4)*(pow(sin(vyr_el[k][r+1]),2)-1)+
				pow(sin(vyr_el[k][r+1]),6))); // d a6,0
			A.set(r,36,(-cos(vyr_el[k][r])*(105/8*sin(vyr_el[k][r])*pow((pow(sin(vyr_el[k][r]),2)-1),2)+105/2*pow(sin(vyr_el[k][r]),3)*
				(pow(sin(vyr_el[k][r]),2)-1)+21*pow(sin(vyr_el[k][r]),5))*cos(vyr_az[k][r]))-(-cos(vyr_el[k][r+1])*(105/8*sin(vyr_el[k][r+1])*
				pow((pow(sin(vyr_el[k][r+1]),2)-1),2)+105/2*pow(sin(vyr_el[k][r+1]),3)*(pow(sin(vyr_el[k][r+1]),2)-1)+21*pow(sin(vyr_el[k][r+1]),5))
				*cos(vyr_az[k][r+1]))); // d a6,1
			A.set(r,37,(-cos(vyr_el[k][r])*(105/8*sin(vyr_el[k][r])*pow((pow(sin(vyr_el[k][r]),2)-1),2)+105/2*pow(sin(vyr_el[k][r]),3)*
				(pow(sin(vyr_el[k][r]),2)-1)+21*pow(sin(vyr_el[k][r]),5))*sin(vyr_az[k][r]))-(-cos(vyr_el[k][r+1])*(105/8*sin(vyr_el[k][r+1])*
				pow((pow(sin(vyr_el[k][r+1]),2)-1),2)+105/2*pow(sin(vyr_el[k][r+1]),3)*(pow(sin(vyr_el[k][r+1]),2)-1)+21*pow(sin(vyr_el[k][r+1]),5))
				*sin(vyr_az[k][r+1]))); // d b6,1
			A.set(r,38,(pow(cos(vyr_el[k][r]),2)*(105/8*pow((pow(sin(vyr_el[k][r]),2)-1),2)+210*pow(sin(vyr_el[k][r]),2)*
				(pow(sin(vyr_el[k][r]),2)-1)+210*pow(sin(vyr_el[k][r]),4))*cos(2*vyr_az[k][r]))-(pow(cos(vyr_el[k][r+1]),2)*
				(105/8*pow((pow(sin(vyr_el[k][r+1]),2)-1),2)+210*pow(sin(vyr_el[k][r+1]),2)*(pow(sin(vyr_el[k][r+1]),2)-1)
				+210*pow(sin(vyr_el[k][r+1]),4))*cos(2*vyr_az[k][r+1]))); // d a6,2
			A.set(r,39,(pow(cos(vyr_el[k][r]),2)*(105/8*pow((pow(sin(vyr_el[k][r]),2)-1),2)+210*pow(sin(vyr_el[k][r]),2)*
				(pow(sin(vyr_el[k][r]),2)-1)+210*pow(sin(vyr_el[k][r]),4))*sin(2*vyr_az[k][r]))-(pow(cos(vyr_el[k][r+1]),2)*
				(105/8*pow((pow(sin(vyr_el[k][r+1]),2)-1),2)+210*pow(sin(vyr_el[k][r+1]),2)*(pow(sin(vyr_el[k][r+1]),2)-1)
				+210*pow(sin(vyr_el[k][r+1]),4))*sin(2*vyr_az[k][r+1]))); // d b6,2
			A.set(r,40,(-pow(cos(vyr_el[k][r]),3)*(945/2*sin(vyr_el[k][r])*(pow(sin(vyr_el[k][r]),2)-1)+1260*pow(sin(vyr_el[k][r]),3))*
				cos(3*vyr_az[k][r]))-(-pow(cos(vyr_el[k][r+1]),3)*(945/2*sin(vyr_el[k][r+1])*(pow(sin(vyr_el[k][r+1]),2)-1)+
				1260*pow(sin(vyr_el[k][r+1]),3))*cos(3*vyr_az[k][r+1]))); //d a6,3
			A.set(r,41,(-pow(cos(vyr_el[k][r]),3)*(945/2*sin(vyr_el[k][r])*(pow(sin(vyr_el[k][r]),2)-1)+1260*pow(sin(vyr_el[k][r]),3))*
				sin(3*vyr_az[k][r]))-(-pow(cos(vyr_el[k][r+1]),3)*(945/2*sin(vyr_el[k][r+1])*(pow(sin(vyr_el[k][r+1]),2)-1)+
				1260*pow(sin(vyr_el[k][r+1]),3))*sin(3*vyr_az[k][r+1]))); //d b6,3
			A.set(r,42,(pow(cos(vyr_el[k][r]),4)*(945/2*(pow(sin(vyr_el[k][r]),2)-1)+4725*pow(sin(vyr_el[k][r]),2))*cos(4*vyr_az[k][r]))
				-(pow(cos(vyr_el[k][r+1]),4)*(945/2*(pow(sin(vyr_el[k][r+1]),2)-1)+4725*pow(sin(vyr_el[k][r+1]),2))*cos(4*vyr_az[k][r+1]))); // d a6,4
			A.set(r,43,(pow(cos(vyr_el[k][r]),4)*(945/2*(pow(sin(vyr_el[k][r]),2)-1)+4725*pow(sin(vyr_el[k][r]),2))*sin(4*vyr_az[k][r]))
				-(pow(cos(vyr_el[k][r+1]),4)*(945/2*(pow(sin(vyr_el[k][r+1]),2)-1)+4725*pow(sin(vyr_el[k][r+1]),2))*sin(4*vyr_az[k][r+1]))); // d b6,4
			A.set(r,44,(-pow(cos(vyr_el[k][r]),5)*10395*sin(vyr_el[k][r])*cos(5*vyr_az[k][r]))-(-pow(cos(vyr_el[k][r+1]),5)*10395*sin(vyr_el[k][r+1])
				*cos(5*vyr_az[k][r+1]))); // d a6,5
			A.set(r,45,(-pow(cos(vyr_el[k][r]),5)*10395*sin(vyr_el[k][r])*sin(5*vyr_az[k][r]))-(-pow(cos(vyr_el[k][r+1]),5)*10395*sin(vyr_el[k][r+1])
				*sin(5*vyr_az[k][r+1]))); // d b6,5

			A.set(r,46,(15/8*sin(vyr_el[k][r])*pow((pow(sin(vyr_el[k][r]),2)-1),3)+127/16*pow(sin(vyr_el[k][r]),3)*pow((pow(sin(vyr_el[k][r]),2)-1),2)
				+53/6*pow(sin(vyr_el[k][r]),5)*(pow(sin(vyr_el[k][r]),2)-1)+5/6*pow(sin(vyr_el[k][r]),7))-(15/8*sin(vyr_el[k][r+1])
				*pow((pow(sin(vyr_el[k][r+1]),2)-1),3)+127/16*pow(sin(vyr_el[k][r+1]),3)*pow((pow(sin(vyr_el[k][r+1]),2)-1),2)
				+53/6*pow(sin(vyr_el[k][r+1]),5)*(pow(sin(vyr_el[k][r+1]),2)-1)+5/6*pow(sin(vyr_el[k][r+1]),7))); // d a7,0
			A.set(r,47,(-cos(vyr_el[k][r])*(15/8*pow((pow(sin(vyr_el[k][r]),2)-1),3)*561/16*pow(sin(vyr_el[k][r]),2)*pow((pow(sin(vyr_el[k][r]),2)-1),2)
				+911/12*pow(sin(vyr_el[k][r]),4)*(pow(sin(vyr_el[k][r]),2)-1)+47/2*pow(sin(vyr_el[k][r]),6))*cos(vyr_az[k][r]))-(-cos(vyr_el[k][r+1])
				*(15/8*pow((pow(sin(vyr_el[k][r+1]),2)-1),3)*561/16*pow(sin(vyr_el[k][r+1]),2)*pow((pow(sin(vyr_el[k][r+1]),2)-1),2)
				+911/12*pow(sin(vyr_el[k][r+1]),4)*(pow(sin(vyr_el[k][r+1]),2)-1)+47/2*pow(sin(vyr_el[k][r+1]),6))*cos(vyr_az[k][r+1]))); // d a7,1
			A.set(r,48,(-cos(vyr_el[k][r])*(15/8*pow((pow(sin(vyr_el[k][r]),2)-1),3)*561/16*pow(sin(vyr_el[k][r]),2)*pow((pow(sin(vyr_el[k][r]),2)-1),2)
				+911/12*pow(sin(vyr_el[k][r]),4)*(pow(sin(vyr_el[k][r]),2)-1)+47/2*pow(sin(vyr_el[k][r]),6))*sin(vyr_az[k][r]))-(-cos(vyr_el[k][r+1])
				*(15/8*pow((pow(sin(vyr_el[k][r+1]),2)-1),3)*561/16*pow(sin(vyr_el[k][r+1]),2)*pow((pow(sin(vyr_el[k][r+1]),2)-1),2)
				+911/12*pow(sin(vyr_el[k][r+1]),4)*(pow(sin(vyr_el[k][r+1]),2)-1)+47/2*pow(sin(vyr_el[k][r+1]),6))*sin(vyr_az[k][r+1]))); // d b7,1
			A.set(r,49,(pow(cos(vyr_el[k][r]),2)*(651/8*sin(vyr_el[k][r])*pow((pow(sin(vyr_el[k][r]),2)-1),2)+5327/12*pow(sin(vyr_el[k][r]),3)
				*(pow(sin(vyr_el[k][r]),2)-1)+1757/6*pow(sin(vyr_el[k][r]),5))*cos(2*vyr_az[k][r]))-(pow(cos(vyr_el[k][r+1]),2)*
				(651/8*sin(vyr_el[k][r+1])*pow((pow(sin(vyr_el[k][r+1]),2)-1),2)+5327/12*pow(sin(vyr_el[k][r+1]),3)
				*(pow(sin(vyr_el[k][r+1]),2)-1)+1757/6*pow(sin(vyr_el[k][r+1]),5))*cos(2*vyr_az[k][r+1]))); // d a7,2
			A.set(r,50,(pow(cos(vyr_el[k][r]),2)*(651/8*sin(vyr_el[k][r])*pow((pow(sin(vyr_el[k][r]),2)-1),2)+5327/12*pow(sin(vyr_el[k][r]),3)
				*(pow(sin(vyr_el[k][r]),2)-1)+1757/6*pow(sin(vyr_el[k][r]),5))*sin(2*vyr_az[k][r]))-(pow(cos(vyr_el[k][r+1]),2)*
				(651/8*sin(vyr_el[k][r+1])*pow((pow(sin(vyr_el[k][r+1]),2)-1),2)+5327/12*pow(sin(vyr_el[k][r+1]),3)
				*(pow(sin(vyr_el[k][r+1]),2)-1)+1757/6*pow(sin(vyr_el[k][r+1]),5))*sin(2*vyr_az[k][r+1]))); // d b7,2
			A.set(r,51,(-pow(cos(vyr_el[k][r]),3)*(651/8*pow((pow(sin(vyr_el[k][r]),2)-1),2)+6629/4*pow(sin(vyr_el[k][r]),2)
				*(pow(sin(vyr_el[k][r]),2)-1)+2352*pow(sin(vyr_el[k][r]),4))*cos(3*vyr_az[k][r]))-(-pow(cos(vyr_el[k][r+1]),3)
				*(651/8*pow((pow(sin(vyr_el[k][r+1]),2)-1),2)+6629/4*pow(sin(vyr_el[k][r+1]),2)*(pow(sin(vyr_el[k][r+1]),2)-1)
				+2352*pow(sin(vyr_el[k][r+1]),4))*cos(3*vyr_az[k][r+1]))); // d a7,3
			A.set(r,52,(-pow(cos(vyr_el[k][r]),3)*(651/8*pow((pow(sin(vyr_el[k][r]),2)-1),2)+6629/4*pow(sin(vyr_el[k][r]),2)
				*(pow(sin(vyr_el[k][r]),2)-1)+2352*pow(sin(vyr_el[k][r]),4))*sin(3*vyr_az[k][r]))-(-pow(cos(vyr_el[k][r+1]),3)
				*(651/8*pow((pow(sin(vyr_el[k][r+1]),2)-1),2)+6629/4*pow(sin(vyr_el[k][r+1]),2)*(pow(sin(vyr_el[k][r+1]),2)-1)
				+2352*pow(sin(vyr_el[k][r+1]),4))*sin(3*vyr_az[k][r+1]))); // d b7,3
			A.set(r,53,(pow(cos(vyr_el[k][r]),4)*(3640*sin(vyr_el[k][r])*(pow(sin(vyr_el[k][r]),2)-1)+25445/2*pow(sin(vyr_el[k][r]),3))
				*cos(4*vyr_az[k][r]))-(pow(cos(vyr_el[k][r+1]),4)*(3640*sin(vyr_el[k][r+1])*(pow(sin(vyr_el[k][r+1]),2)-1)
				+25445/2*pow(sin(vyr_el[k][r+1]),3))*cos(4*vyr_az[k][r+1]))); // d a7,4
			A.set(r,54,(pow(cos(vyr_el[k][r]),4)*(3640*sin(vyr_el[k][r])*(pow(sin(vyr_el[k][r]),2)-1)+25445/2*pow(sin(vyr_el[k][r]),3))
				*sin(4*vyr_az[k][r]))-(pow(cos(vyr_el[k][r+1]),4)*(3640*sin(vyr_el[k][r+1])*(pow(sin(vyr_el[k][r+1]),2)-1)
				+25445/2*pow(sin(vyr_el[k][r+1]),3))*sin(4*vyr_az[k][r+1]))); // d b7,4
			A.set(r,55,(-pow(cos(vyr_el[k][r]),5)*(3640*(pow(sin(vyr_el[k][r]),2)-1)+32725*pow(sin(vyr_el[k][r]),2))*cos(5*vyr_az[k][r]))
				-(-pow(cos(vyr_el[k][r+1]),5)*(3640*(pow(sin(vyr_el[k][r+1]),2)-1)+32725*pow(sin(vyr_el[k][r+1]),2))*cos(5*vyr_az[k][r+1]))); // d a7,5
			A.set(r,56,(-pow(cos(vyr_el[k][r]),5)*(3640*(pow(sin(vyr_el[k][r]),2)-1)+32725*pow(sin(vyr_el[k][r]),2))*sin(5*vyr_az[k][r]))
				-(-pow(cos(vyr_el[k][r+1]),5)*(3640*(pow(sin(vyr_el[k][r+1]),2)-1)+32725*pow(sin(vyr_el[k][r+1]),2))*sin(5*vyr_az[k][r+1]))); // d b7,5

			A.set(r,57,(35/128*pow((pow(sin(vyr_el[k][r]),2)-1),4)+35/4*pow(sin(vyr_el[k][r]),2)*pow((pow(sin(vyr_el[k][r]),2)-1),3)+
				185/8*pow(sin(vyr_el[k][r]),4)*pow((pow(sin(vyr_el[k][r]),2)-1),2)+23/2*pow(sin(vyr_el[k][r]),6)*(pow(sin(vyr_el[k][r]),2)-1)
				+pow(sin(vyr_el[k][r]),8))-(35/128*pow((pow(sin(vyr_el[k][r+1]),2)-1),4)+35/4*pow(sin(vyr_el[k][r+1]),2)*
				pow((pow(sin(vyr_el[k][r+1]),2)-1),3)+185/8*pow(sin(vyr_el[k][r+1]),4)*pow((pow(sin(vyr_el[k][r+1]),2)-1),2)+
				23/2*pow(sin(vyr_el[k][r+1]),6)*(pow(sin(vyr_el[k][r+1]),2)-1)+pow(sin(vyr_el[k][r+1]),8))); // d a8,0
			A.set(r,58,(-cos(vyr_el[k][r])*(315/16*sin(vyr_el[k][r])*pow((pow(sin(vyr_el[k][r]),2)-1),3)+145*pow(sin(vyr_el[k][r]),3)
				*pow((pow(sin(vyr_el[k][r]),2)-1),2)+323/2*pow(sin(vyr_el[k][r]),5)*(pow(sin(vyr_el[k][r]),2)-1)+31*pow(sin(vyr_el[k][r]),7))
				*cos(vyr_az[k][r]))-(-cos(vyr_el[k][r+1])*(315/16*sin(vyr_el[k][r+1])*pow((pow(sin(vyr_el[k][r+1]),2)-1),3)+145*pow(sin(vyr_el[k][r+1]),3)
				*pow((pow(sin(vyr_el[k][r+1]),2)-1),2)+323/2*pow(sin(vyr_el[k][r+1]),5)*(pow(sin(vyr_el[k][r+1]),2)-1)+31*pow(sin(vyr_el[k][r+1]),7))
				*cos(vyr_az[k][r+1]))); // d a8,1
			A.set(r,59,(-cos(vyr_el[k][r])*(315/16*sin(vyr_el[k][r])*pow((pow(sin(vyr_el[k][r]),2)-1),3)+145*pow(sin(vyr_el[k][r]),3)
				*pow((pow(sin(vyr_el[k][r]),2)-1),2)+323/2*pow(sin(vyr_el[k][r]),5)*(pow(sin(vyr_el[k][r]),2)-1)+31*pow(sin(vyr_el[k][r]),7))
				*sin(vyr_az[k][r]))-(-cos(vyr_el[k][r+1])*(315/16*sin(vyr_el[k][r+1])*pow((pow(sin(vyr_el[k][r+1]),2)-1),3)+145*pow(sin(vyr_el[k][r+1]),3)
				*pow((pow(sin(vyr_el[k][r+1]),2)-1),2)+323/2*pow(sin(vyr_el[k][r+1]),5)*(pow(sin(vyr_el[k][r+1]),2)-1)+31*pow(sin(vyr_el[k][r+1]),7))
				*sin(vyr_az[k][r+1]))); // d b8,1
			A.set(r,60,(pow(cos(vyr_el[k][r]),2)*(315/16*pow((pow(sin(vyr_el[k][r]),2)-1),3)+4425/8*pow(sin(vyr_el[k][r]),2)
				*pow((pow(sin(vyr_el[k][r]),2)-1),2)+2775/2*pow(sin(vyr_el[k][r]),4)*(pow(sin(vyr_el[k][r]),2)-1)+540*pow(sin(vyr_el[k][r]),6))*
				cos(2*vyr_az[k][r]))-(pow(cos(vyr_el[k][r+1]),2)*(315/16*pow((pow(sin(vyr_el[k][r+1]),2)-1),3)+4425/8*pow(sin(vyr_el[k][r+1]),2)
				*pow((pow(sin(vyr_el[k][r+1]),2)-1),2)+2775/2*pow(sin(vyr_el[k][r+1]),4)*(pow(sin(vyr_el[k][r+1]),2)-1)+540*pow(sin(vyr_el[k][r+1]),6))*
				cos(2*vyr_az[k][r+1]))); // d a8,2
			A.set(r,61,(pow(cos(vyr_el[k][r]),2)*(315/16*pow((pow(sin(vyr_el[k][r]),2)-1),3)+4425/8*pow(sin(vyr_el[k][r]),2)
				*pow((pow(sin(vyr_el[k][r]),2)-1),2)+2775/2*pow(sin(vyr_el[k][r]),4)*(pow(sin(vyr_el[k][r]),2)-1)+540*pow(sin(vyr_el[k][r]),6))*
				sin(2*vyr_az[k][r]))-(pow(cos(vyr_el[k][r+1]),2)*(315/16*pow((pow(sin(vyr_el[k][r+1]),2)-1),3)+4425/8*pow(sin(vyr_el[k][r+1]),2)
				*pow((pow(sin(vyr_el[k][r+1]),2)-1),2)+2775/2*pow(sin(vyr_el[k][r+1]),4)*(pow(sin(vyr_el[k][r+1]),2)-1)+540*pow(sin(vyr_el[k][r+1]),6))*
				sin(2*vyr_az[k][r+1]))); // d b8,2
			A.set(r,62,(-pow(cos(vyr_el[k][r]),3)*(9795/8*sin(vyr_el[k][r])*pow((pow(sin(vyr_el[k][r]),2)-1),2)+15525/2*pow(sin(vyr_el[k][r]),3)
				*(pow(sin(vyr_el[k][r]),2)-1)+6015*pow(sin(vyr_el[k][r]),5))*cos(3*vyr_az[k][r]))-(-pow(cos(vyr_el[k][r+1]),3)*
				(9795/8*sin(vyr_el[k][r+1])*pow((pow(sin(vyr_el[k][r+1]),2)-1),2)+15525/2*pow(sin(vyr_el[k][r+1]),3)
				*(pow(sin(vyr_el[k][r+1]),2)-1)+6015*pow(sin(vyr_el[k][r+1]),5))*cos(3*vyr_az[k][r+1])));  // d a8,3
			A.set(r,63,(-pow(cos(vyr_el[k][r]),3)*(9795/8*sin(vyr_el[k][r])*pow((pow(sin(vyr_el[k][r]),2)-1),2)+15525/2*pow(sin(vyr_el[k][r]),3)
				*(pow(sin(vyr_el[k][r]),2)-1)+6015*pow(sin(vyr_el[k][r]),5))*sin(3*vyr_az[k][r]))-(-pow(cos(vyr_el[k][r+1]),3)*
				(9795/8*sin(vyr_el[k][r+1])*pow((pow(sin(vyr_el[k][r+1]),2)-1),2)+15525/2*pow(sin(vyr_el[k][r+1]),3)
				*(pow(sin(vyr_el[k][r+1]),2)-1)+6015*pow(sin(vyr_el[k][r+1]),5))*sin(3*vyr_az[k][r+1])));  // d b8,3
			A.set(r,64,(pow(cos(vyr_el[k][r]),4)*(9795/8*pow((pow(sin(vyr_el[k][r]),2)-1),2)+28185*pow(sin(vyr_el[k][r]),2)
				*(pow(sin(vyr_el[k][r]),2)-1)+45600*pow(sin(vyr_el[k][r]),4))*cos(4*vyr_az[k][r]))-(pow(cos(vyr_el[k][r+1]),4)
				*(9795/8*pow((pow(sin(vyr_el[k][r+1]),2)-1),2)+28185*pow(sin(vyr_el[k][r+1]),2)*(pow(sin(vyr_el[k][r+1]),2)-1)
				+45600*pow(sin(vyr_el[k][r+1]),4))*cos(4*vyr_az[k][r+1]))); // d a8,4
			A.set(r,65,(pow(cos(vyr_el[k][r]),4)*(9795/8*pow((pow(sin(vyr_el[k][r]),2)-1),2)+28185*pow(sin(vyr_el[k][r]),2)
				*(pow(sin(vyr_el[k][r]),2)-1)+45600*pow(sin(vyr_el[k][r]),4))*sin(4*vyr_az[k][r]))-(pow(cos(vyr_el[k][r+1]),4)
				*(9795/8*pow((pow(sin(vyr_el[k][r+1]),2)-1),2)+28185*pow(sin(vyr_el[k][r+1]),2)*(pow(sin(vyr_el[k][r+1]),2)-1)
				+45600*pow(sin(vyr_el[k][r+1]),4))*sin(4*vyr_az[k][r+1]))); // d b8,4
			A.set(r,66,(-pow(cos(vyr_el[k][r]),5)*(122535/2*sin(vyr_el[k][r])*(pow(sin(vyr_el[k][r]),2)-1)+238770*pow(sin(vyr_el[k][r]),3))
				*cos(5*vyr_el[k][r]))-(-pow(cos(vyr_el[k][r+1]),5)*(122535/2*sin(vyr_el[k][r+1])*(pow(sin(vyr_el[k][r+1]),2)-1)
				+238770*pow(sin(vyr_el[k][r+1]),3))*cos(5*vyr_el[k][r+1]))); // d a8,5
			A.set(r,67,(-pow(cos(vyr_el[k][r]),5)*(122535/2*sin(vyr_el[k][r])*(pow(sin(vyr_el[k][r]),2)-1)+238770*pow(sin(vyr_el[k][r]),3))
				*sin(5*vyr_el[k][r]))-(-pow(cos(vyr_el[k][r+1]),5)*(122535/2*sin(vyr_el[k][r+1])*(pow(sin(vyr_el[k][r+1]),2)-1)
				+238770*pow(sin(vyr_el[k][r+1]),3))*sin(5*vyr_el[k][r+1]))); // d b8,5
		}

		// 3) výpočet transponované matice plánu AT:
		AT = A.transpozice();
		
		// 4) nastavení matice redukovaných měření l´ (pro anm0 = 0, bnm0 = 0):
		for (int r = 0; r < pocet_mereni[k]; r++)
		{
			vyr_dTDSD[k][r] = vyr_hodnota_TDSD[k][r] - (cos(vyr_el[k][r])*cos(vyr_az[k][r])-cos(vyr_el[k][r+1])*cos(vyr_az[k][r+1]))*PCO[k][0]
				-(cos(vyr_el[k][r])*sin(vyr_az[k][r])-cos(vyr_el[k][r+1])*sin(vyr_az[k][r+1]))*PCO[k][1] - (sin(vyr_el[k][r])-sin(vyr_el[k][r+1]))*PCO[k][2];
			l´.set(r,0,-vyr_dTDSD[k][r]);
		}

		// 5) výpočet matice koeficientů normálních rovnic:
		N = AT.soucin(A);

		// 6) výpočet matice y:
		y = AT.soucin(l´);
     
		// 7) výpočet inverzní matice k matici N:
		N_inv = N.inverze();

		// 8) Výpočet přírůstků neznámých:
		N_inv_zap = -N_inv;

		dX = N_inv_zap.soucin(y);
		X = dX;
		
		// 9) Přepočet do proměnné, se kterou se dá v rámci třídy Rinex dále pracovat:
		int j = 0;
		for (int i = 0; i < 68; i++)
			koef_PCV[k][i] = X.get(i,j);
			
		// Výpočet a výpis PCV
		
		double PCV;
		double ro = 180/M_PI;
		int az, el;


		for (int r = 0; r < 72; r++)			
		{
			if (r == 0)
			{
				int el_vyp;
				vypis_PCV << "         ";
				for (int i = 0; i < 19; i++)
				{
					el_vyp = i*5;
					vypis_PCV << setw(10) << " El = " << el_vyp << ": ";
				}
				vypis_PCV <<endl;
			}
			
			az = r*5;
			vypis_PCV << "Az = " << az << ":   ";

			for (int s = 0; s < 19; s++)		
			{	
				el = s*5;	
				
				PCV = sin(el/ro)*koef_PCV[k][0] - cos(el/ro)*(koef_PCV[k][1]*cos(az/ro)+koef_PCV[k][2]*sin(az/ro))
					+(3*pow(sin(el/ro),2)-1)*koef_PCV[k][3]-6*sin(el/ro)*cos(el/ro)*(koef_PCV[k][4]*cos(az/ro)+koef_PCV[k][5]*sin(az/ro))
					+6*pow(cos(el/ro),2)*(koef_PCV[k][6]*cos(2*az/ro)+koef_PCV[k][7]*sin(2*az/ro))+(3/2*sin(el/ro)*(pow(sin(el/ro),2)-1)
					+48*pow(sin(el/ro),3))*koef_PCV[k][8] - cos(el/ro)*(4*pow(sin(el/ro),2)+3/2*(pow(sin(el/ro),2)-1))*(koef_PCV[k][9]*cos(az/ro)
					+koef_PCV[k][10]*sin(az/ro))+15*pow(cos(el/ro),2)*sin(el/ro)*(koef_PCV[k][11]*cos(2*az/ro)+koef_PCV[k][12]*sin(2*az/ro))
					-15*pow(cos(el/ro),3)*(koef_PCV[k][13]*cos(3*az/ro)+koef_PCV[k][14]*sin(3*az/ro))+(3/8*pow((pow(sin(el/ro),2)-1),2)+
					3*pow(sin(el/ro),2)*(pow(sin(el/ro),2)-1)+pow(sin(el/ro),4))*koef_PCV[k][15] - 5/2*cos(el/ro)*(4*pow(sin(el/ro),3)+3*sin(el/ro)*
					(pow(sin(el/ro),2)-1))*(koef_PCV[k][16]*cos(az/ro)+koef_PCV[k][17]*sin(az/ro))+pow(cos(el/ro),2)*(35*pow(sin(el/ro),2)+15/2*
					(pow(sin(el/ro),2)-1))*(koef_PCV[k][18]*cos(2*az/ro)+koef_PCV[k][19]*sin(2*az/ro))-85*pow(cos(el/ro),3)*sin(el/ro)*(koef_PCV[k][20]
					*cos(3*az/ro)+koef_PCV[k][21]*sin(3*az/ro))+85*pow(cos(el/ro),4)*(koef_PCV[k][22]*cos(4*az/ro)+koef_PCV[k][23]*sin(4*az/ro))
					+(15/8*sin(el/ro)*pow(pow(sin(el/ro),2)-1,2)+5*pow(sin(el/ro),3)*(pow(sin(el/ro),2)-1)+pow(sin(el/ro),5))*koef_PCV[k][24]
					-cos(el/ro)*(15/8*pow(pow(sin(el/ro),2)-1,2)+45/2*pow(sin(el/ro),2)*(pow(sin(el/ro),2)-1)+15*pow(sin(el/ro),4))*(koef_PCV[k][25]
					*cos(az/ro)+koef_PCV[k][26]*sin(az/ro))+105*pow(cos(el/ro),2)*(pow(sin(el/ro),3)+1/2*sin(el/ro)*(pow(sin(el/ro),2)-1))*(koef_PCV[k][27]
					*cos(2*az/ro)+koef_PCV[k][28]*sin(2*az/ro))-pow(cos(el/ro),3)*(420*pow(sin(el/ro),2)-105/2*(pow(sin(el/ro),2)-1))*(koef_PCV[k][29]
					*cos(3*az/ro)+koef_PCV[k][30]*sin(3*az/ro))+pow(cos(el/ro),4)*945*sin(el/ro)*(koef_PCV[k][31]*cos(4*az/ro)+koef_PCV[k][32]*sin(4*az/ro))
					-945*pow(cos(el/ro),5)*(koef_PCV[k][33]*cos(5*az/ro)+koef_PCV[k][34]*sin(5*az/ro))+(5/16*pow(pow(sin(el/ro),2)-1,3)+45/8*pow(sin(el/ro),2)
					*pow(pow(sin(el/ro),2)-1,2)+15/2*pow(sin(el/ro),4)*(pow(sin(el/ro),2)-1)+pow(sin(el/ro),6))*koef_PCV[k][35] - cos(el/ro)*(105/8*
					sin(el/ro)*pow(pow(sin(el/ro),2)-1,2)+105/2*pow(sin(el/ro),3)*(pow(sin(el/ro),2)-1)+21*pow(sin(el/ro),5))*(koef_PCV[k][36]*cos(az/ro)
					+koef_PCV[k][37]*sin(az/ro))+pow(cos(el/ro),2)*(105/8*pow(pow(sin(el/ro),2)-1,2)+210*pow(sin(el/ro),2)*(pow(sin(el/ro),2)-1)+210*
					pow(sin(el/ro),4))*(koef_PCV[k][38]*cos(2*az/ro)+koef_PCV[k][39]*sin(2*az/ro))-pow(cos(el/ro),3)*(945/2*sin(el/ro)*(pow(sin(el/ro),2)-1)
					+1260*pow(sin(el/ro),3))*(koef_PCV[k][40]*cos(3*az/ro)+koef_PCV[k][41]*sin(3*az/ro))+pow(cos(el/ro),4)*(945/2*(pow(sin(el/ro),2)-1)
					+4725*pow(sin(el/ro),2))*(koef_PCV[k][42]*cos(4*az/ro)+koef_PCV[k][43]*sin(4*az/ro))-pow(cos(el/ro),5)*10395*sin(el/ro)*(koef_PCV[k][44]
					*cos(5*az/ro)+koef_PCV[k][45]*sin(5*az/ro))+(15/8*sin(el/ro)*pow(pow(sin(el/ro),2)-1,3)+127/16*pow(sin(el/ro),3)*pow(pow(sin(el/ro),2)-1,2)
					+53/6*pow(sin(el/ro),5)*(pow(sin(el/ro),2)-1)+5/6*pow(sin(el/ro),7))*koef_PCV[k][46]-cos(el/ro)*(15/8*pow(pow(sin(el/ro),2)-1,3)+561/16*
					pow(sin(el/ro),2)*pow(pow(sin(el/ro),2)-1,2)+911/12*pow(sin(el/ro),4)*(pow(sin(el/ro),2)-1)+47/2*pow(sin(el/ro),6))*(koef_PCV[k][47]*cos(az/ro)
					+koef_PCV[k][48]*sin(az/ro))+pow(cos(el/ro),2)*(651/8*sin(el/ro)*pow(pow(sin(el/ro),2)-1,2)+5327/12*pow(sin(el/ro),3)*(pow(sin(el/ro),2)-1)+
					2352*pow(sin(el/ro),4))*(koef_PCV[k][49]*cos(2*az/ro)+koef_PCV[k][50]*sin(2*az/ro))-pow(cos(el/ro),3)*(651/8*pow(pow(sin(el/ro),2)-1,2)+
					6629/4*pow(sin(el/ro),2)*(pow(sin(el/ro),2)-1)+2352*pow(sin(el/ro),4))*(koef_PCV[k][51]*cos(3*az/ro)+koef_PCV[k][52]*sin(3*az/ro))+
					pow(cos(el/ro),4)*(3640*sin(el/ro)*(pow(sin(el/ro),2)-1)+25445/2*pow(sin(el/ro),3))*(koef_PCV[k][53]*cos(4*az/ro)+
					koef_PCV[k][54]*sin(4*az/ro))-pow(cos(el/ro),5)*(3640*(pow(sin(el/ro),2)-1)+32725*pow(sin(el/ro),2))*(koef_PCV[k][55]*cos(5*az/ro)+
					koef_PCV[k][56]*sin(5*az/ro))+(35/128*pow(pow(sin(el/ro),2)-1,4)+35/4*pow(sin(el/ro),2)*pow(pow(sin(el/ro),2)-1,3)+185/8*pow(sin(el/ro),4)*
					pow(pow(sin(el/ro),2)-1,2)+23/2*pow(sin(el/ro),6)*(pow(sin(el/ro),2)-1)+pow(sin(el/ro),8))*koef_PCV[k][57]-cos(el/ro)*(315/16*sin(el/ro)*
					pow(pow(sin(el/ro),2)-1,3)+145*pow(sin(el/ro),3)*pow(pow(sin(el/ro),2)-1,2)+323/2*pow(sin(el/ro),5)*(pow(sin(el/ro),2)-1)+31*pow(sin(el/ro),7))
					*(koef_PCV[k][58]*cos(az/ro)+koef_PCV[k][59]*sin(az/ro))+pow(cos(el/ro),2)*(315/16*pow(pow(sin(el/ro),2)-1,3)+4425/8*pow(sin(el/ro),2)*
					pow(pow(sin(el/ro),2)-1,2)+2775/2*pow(sin(el/ro),4)*(pow(sin(el/ro),2)-1)+540*pow(sin(el/ro),6))*(koef_PCV[k][60]*cos(2*az/ro)+koef_PCV[k][61]*sin(2*az/ro))
					-pow(cos(el/ro),3)*(9795/8*sin(el/ro)*pow(pow(sin(el/ro),2)-1,2)+15525/2*pow(sin(el/ro),3)*(pow(sin(el/ro),2)-1)+6015*pow(sin(el/ro),5))*
					(koef_PCV[k][62]*cos(3*az/ro)+koef_PCV[k][63]*sin(3*az/ro))+pow(cos(el/ro),4)*(9795/8*pow(pow(sin(el/ro),2)-1,2)+28185*pow(sin(el/ro),2)*(pow(sin(el/ro),2)-1)
					+45600*pow(sin(el/ro),4))*(koef_PCV[k][64]*cos(4*az/ro)+koef_PCV[k][65]*sin(4*az/ro))-pow(cos(el/ro),5)*(122535/2*sin(el/ro)*(pow(sin(el/ro),2)-1)
					+238770*pow(sin(el/ro),3))*(koef_PCV[k][66]*cos(5*az/ro)+koef_PCV[k][67]*sin(5*az/ro));

					vypis_PCV << fixed << setprecision(4) << setw(10) << PCV << "    ";
			}
			vypis_PCV << endl;
		}
		vypis_PCV << endl << endl;
	}
	vypis_PCV.close();
	cout << "Hodnoty PCV byly vypsany do souboru Hodnoty_PCV.txt" << endl;
}

// hlavní funkce
int main(int argc, char* argv[])
{
	Rinex prot, ref, test, sd, tdsd, mnc;
	string file_prot, file_ref, file_test;

	// 1. krok: načtení observačních dat ve formátu RINEX:
	cout << "Zadejte nazev souboru s protokolem o mereni kalibracniho zarizeni: " << endl;
	cin >> file_prot;
	prot.nacti_protokol(file_prot);								

	cout << "Zadejte nazev souboru s observacnimi daty porizenymi referencnim prijimacem: " << endl;
	cin >> file_ref;
	ref.nacti_rinex(file_ref, prot.pocet_radku_protokolu, prot.cas_zarizeni);						
	
	cout << "Zadejte nazev souboru s observacnimi daty porizenymi testovanym prijimacem: " << endl;
	cin >> file_test;
	test.nacti_rinex(file_test, prot.pocet_radku_protokolu, prot.cas_zarizeni);						

	// 2. krok: diferenciace načtených dat:
	sd.serad(ref.cas, test.cas, ref.nazev_druzice, test.nazev_druzice, ref.datum, test.datum, ref.doba_observace, test.doba_observace, ref.interval, test.interval, ref.pocet_druzic, test.pocet_druzic, ref.observacni_typ, test.observacni_typ, ref.pocet_observacnich_typu, test.pocet_observacnich_typu, ref.max_pocet_druzic, ref.observace, test.observace, prot.azimut_zarizeni, prot.elevace_zarizeni);																
	sd.LOS(sd.pocitadlo_bloku, sd.pocitadlo_druzic, sd.omezeni_skut, sd.cas_razeni, sd.dif_observace_ref, sd.dif_observace_test, sd.druzice_SD, prot.azimut_zarizeni_orig, prot.elevace_zarizeni_orig);
	sd.SD(sd.pocitadlo_bloku, sd.pocet_druzic_SD, sd.omezeni_skut, sd.dif_observace_ref_kor, sd.dif_observace_test_kor, sd.cas_razeni, sd.razeni_azimut, sd.razeni_elevace);
	tdsd.TDSD(sd.pocitadlo_bloku, sd.pocet_druzic_SD, sd.omezeni_skut, sd.jednoducha_diference, sd.cas_SD, sd.druzice_SD, sd.SD_azimut, sd.SD_elevace);

	// 3. krok: dvoustupňové vyrovnání metodou nejmenších čtverců
	mnc.vyrovnej_1(sd.pocitadlo_bloku, tdsd.pocet_druzic_TDSD, sd.omezeni_skut, tdsd.casova_diference, sd.azimut_zarizeni_SD, sd.elevace_zarizeni_SD, tdsd.TDSD_azimut, tdsd.TDSD_elevace, sd.omezeni, sd.fazove_mereni);
	mnc.vyrovnej_2(sd.pocitadlo_bloku, tdsd.pocet_druzic_TDSD, tdsd.casova_diference, mnc.ant_azimut, mnc.ant_elevace, sd.omezeni, sd.fazove_mereni, mnc.PCO);

	system("pause");
	return 0;
}

