

#include "hdr_extract.h"

#include "idd_header.h"

#include <stdio.h>

static float rad2deg(float x)
{
	return x; //57.29577951308*x;
}

hdr_extract_info_t* hdr_extract_antenna(const void* rawdata, unsigned int offset)
{
	static FILE* hfd=0;

	static iddheader::ge_header_t phdr;

	const unsigned int* p=reinterpret_cast<const unsigned int*>(rawdata);

	const iddheader::ge_header_t& ge=*reinterpret_cast<const iddheader::ge_header_t*>(&p[11+offset]);


	static hdr_extract_info_t xinfo;

	xinfo.tt=ge.signal.ttag;

	xinfo.ant_tt=ge.general_settings.antenna.position.antttag;

	xinfo.ant.az=rad2deg(ge.general_settings.antenna.position.antennaaz_rad);
	xinfo.ant.el=rad2deg(ge.general_settings.antenna.position.antennael_rad);
	xinfo.nav.az=rad2deg(ge.general_settings.antenna.position.navaz_rad);
	xinfo.nav.el=rad2deg(ge.general_settings.antenna.position.navel_rad);
	xinfo.body.az=rad2deg(ge.general_settings.antenna.position.bodyaz_rad);
	xinfo.body.el=rad2deg(ge.general_settings.antenna.position.bodyel_rad);

	xinfo.pe_tt=ge.function_settings.pe_settings.time_tag;
	xinfo.pe.az=rad2deg(ge.function_settings.pe_settings.ant_az);
	xinfo.pe.el=rad2deg(ge.function_settings.pe_settings.ant_el);


	if (hfd==0)
	{
		hfd=fopen("header_in.txt", "w");
	}
	else
	{
		const iddheader::ge_header_t& hdr=ge;

        fprintf(hfd, "%u;%s;%d;%d;%f;%.3f;%d;%d;%d;ANT;%d;%f;%f;%f;%f;%f;PE;%d;%f;%f;%f\n", //;TGT;%d;%f;%f;%d;%f;%f;%f;%f\n",
        	 hdr.signal.bacth_counter,
             hdr.mode.operation_mode==iddtypes::stt_master_mode ? "STT" :"SRC",
             hdr.mode.master_mode,
             hdr.signal.ttag,
             (hdr.signal.ttag)*64E-3,
             (hdr.signal.ttag-phdr.signal.ttag)*64E-3,

             0, 0, 0, //hdr.mode.result_id, hdr.mode.tgt_unique_id, hpt.results_id,

             hdr.general_settings.antenna.position.antttag,
             (hdr.general_settings.antenna.position.antttag*64E-3),
             rad2deg(hdr.general_settings.antenna.position.navaz_rad),
             rad2deg(hdr.general_settings.antenna.position.navel_rad),

             hdr.general_settings.antenna.position.urx_rad,
             hdr.general_settings.antenna.position.vrx_rad,

             hdr.function_settings.pe_settings.time_tag,
             (hdr.function_settings.pe_settings.time_tag)*64E-3,

             rad2deg(hdr.function_settings.pe_settings.ant_az),
             rad2deg(hdr.function_settings.pe_settings.ant_el)
             );
#if 0
             hpt.results_id,

             rad2deg(tgt_nav_az),
             rad2deg(tgt_nav_el),

             hpt.measures_list.num_meas,
             hpt.measures_list.main_measure[0].mr_daz,
             hpt.measures_list.main_measure[0].mr_del,
             hpt.measures_list.main_measure[0].rng_tgt,
             hpt.measures_list.main_measure[0].vel_tgt
                 );
#endif

	}

	phdr=ge;
	return &xinfo;
}
