#include        <conio.h>       /* for getch, kbhit */
#include        <stdio.h>       /* for malloc */
#include        <dos.h>         /* for delay() */
#include	<time.h>
#include	<math.h>

#define EXTERN
#include        "..\include\globals.h"

extern double trcorrtog;

#define         PPI     0
#define         RHI     1

#define		VOFF	0
#define		POFF	1
#define		NOFF	2
#define		WOFF	3
#define		ZOFF	4
#define		CZOFF	5

double DACVOLTAGE = -4.2;

main(argc,argv)
char    *argv[];   
int     argc;
{
	struct tm *z;
	FILE *fz, *fn, *fv, *fp, *fs, *fc;
	char         c;
	int          i,tape,skip=0, extr=0;
	double	zsave;
	DWELL   *dwell;   
	RADAR   *radar;
	DISPLAY *disp;
	float   *prods, *zptr, *nptr, *vptr, *pptr, *sptr, *cptr;
	int     type=PPI,dispnum='1',dchange=0,pmode=0;   
	long	t_start=0, t_stop=0, l_time = 0;
	char    *szzname, *sznname, *szvname, *szpname, *szsname, *szcname;


	if((prods = (float *)malloc(sizeof(float) * 1200 * 8)) == NULL)
	{puts("not enough memory"); exit(0);
	}

	if((dwell = (DWELL *)malloc(sizeof(DWELL))) == NULL)
	{puts("not enough memory"); exit(0);
	}

	if((disp = (DISPLAY *)malloc(sizeof(DISPLAY))) == NULL)
	{puts("not enough memory"); exit(0);
	}

	if((radar = (RADAR *)malloc(sizeof(RADAR))) == NULL)
	{puts("not enough memory"); exit(0);
	}

	/* read in the display configuration file */
	readdisplay("",disp);

	if(argc < 2)
	{
		printf("Use: %s data_filename\n",argv[0]);
		printf("   Produces 6 files as follows:\n");
		printf("     data_filenameV - Velocity (m/s)\n");
		printf("     data_filenameP - Power (dBm)\n");
		printf("     data_filenameN - NCP\n");
		printf("     data_filenameS - Spectral Width (m/s)\n");
		printf("     data_filenameZ - Reflectivity (dBZ)\n");
		printf("     data_filenameC - Coherent Reflectivity (dBZ)\n");
		exit(0);
	}

	tape = !strcmp(argv[1],"tape");   
	if(tape)
	{if(!init_intape(dwell,radar))             exit(0);
	}
	else
	{if(!init_indisk(dwell,radar,argv[1]))     exit(0);
	}

	if(tape)
	{
		puts("Looking for radar structure");
		while(read_tape(dwell,radar) == 0); /* read until a radar structure or error */
		puts("Radar structure read");
		while(read_tape(dwell,radar) == 1); /* read until a dwell structure or error */
	}
	else
	{
		puts("Looking for radar structure");
		while(read_disk(dwell,radar) == 0); /* read until a radar structure or error */
		puts("Radar structure read");
		while(read_disk(dwell,radar) == 1); /* read until a dwell structure or error */
	}

	/* Make the filenames for the output files */
	i = strlen(argv[1]) + 10;
	if((szzname = (char *)malloc(i)) == NULL)
	{puts("not enough memory"); exit(0);
	}
	if((sznname = (char *)malloc(i)) == NULL)
	{puts("not enough memory"); exit(0);
	}
	if((szvname = (char *)malloc(i)) == NULL)
	{puts("not enough memory"); exit(0);
	}
	if((szpname = (char *)malloc(i)) == NULL)
	{puts("not enough memory"); exit(0);
	}
	if((szsname = (char *)malloc(i)) == NULL)
	{puts("not enough memory"); exit(0);
	}
	if((szcname = (char *)malloc(i)) == NULL)
	{puts("not enough memory"); exit(0);
	}
	
	sprintf(szzname,"%s%s", argv[1], "Z");
	sprintf(sznname,"%s%s", argv[1], "N");
	sprintf(szvname,"%s%s", argv[1], "V");
	sprintf(szpname,"%s%s", argv[1], "P");
	sprintf(szsname,"%s%s", argv[1], "S");
	sprintf(szcname,"%s%s", argv[1], "C");

	/* Open the output files */
	if((fz = fopen(szzname,"ab")) == NULL)
	{
		printf("Can't open output file %s\n", szzname);
		exit(0);
	}
	if((fn = fopen(sznname,"ab")) == NULL)
	{
		printf("Can't open output file %s\n", sznname);
		exit(0);
	}
	if((fv = fopen(szvname,"ab")) == NULL)
	{
		printf("Can't open output file %s\n", szvname);
		exit(0);
	}
	if((fp = fopen(szpname,"ab")) == NULL)
	{
		printf("Can't open output file %s\n", szpname);
		exit(0);
	}
	if((fs = fopen(szsname,"ab")) == NULL)
	{
		printf("Can't open output file %s\n", szsname);
		exit(0);
	}
	if((fc = fopen(szcname,"ab")) == NULL)
	{
		printf("Can't open output file %s\n", szcname);
		exit(0);
	}
	/* do this AFTER init disk */
	if(!init_display(radar,dwell,disp)) {closeall(); exit(-1);
	}

	disp->recording = 1;  /* the word "play" will appear on display */
	zsave = disp->rgpp; 	/* /(3*1.414); */
	disp->rgpp =zsave;
	disp->type |=2; /* Put on the grid bit */
	disp->type |=4; /* Put on the VP bit */
      	trcorrtog = 0.0;

	do
	{

		c = kbhit() ? toupper(getch()) : 1; /* get keyboard input (1 is benign) */

		/* if c = 0 and there is already is another character, then */ 
		/* a function key was hit */
		if(c == 0) 
		{
			c = getch() - 0x3B;    /* parse the function key */
			if(c < 6  || (c < 8 && dwell->header.dataformat == DATA_POL1))
			{
				disp->displayparm = c;  
				selections(disp);
			}
			c = 1;        /* reset c to benign value */
		}

		if(dwell->header.dataformat != DATA_POL1 && disp->displayparm > 5)
		{
			disp->displayparm = 0;  
			selections(disp);
			disp->displayparm = 5;  
			selections(disp);
		}

		if(c == ' ')   skip = 1 - skip;   

		if(c == '1')   disp->rgpp *= 1.414;
		if(c == '2')   disp->rgpp /= 1.414;

		if(c == '3')   disp->type ^= 2;   /* flip the grid bit */
		if(c == '4')   disp->type ^= 4;   /* vertical profile bit */

		if(c == 'F')   disp->fakeangles = 1 - disp->fakeangles;
		if(c == 'T')   {disp->threshold += 0.05; if(disp->threshold > .99) disp->threshold=0.0;
		}
		if(c == 'U')   disp->threshold = 0.0;

      		trcorrtog = 0.0;
		if(c == 'G') savescr(dwell->header.time); /* make a gif file */
		if(c == 'R') extr = extr == 1? 0: 1; /* Toggle the write flag */
		if(c >= '0' && c <= '9' && c != dispnum) dchange = 1;

		if(!skip)
			if(tape)
			{
			if(read_tape(dwell,radar) == 1)  /* if a radar structure */
			{
				while(read_tape(dwell,radar) == 1); /* read until a dwell or error */
				init_display(radar,dwell,disp); /* change scales to match new parameters */
				//            init_disp_scales(radar,dwell,disp); /* change scales to match new parameters */
				continue;
			}
			}
			else
			{
			if(read_disk(dwell,radar) == 1)       /* if a radar structure */
			{
				while(read_disk(dwell,radar) == 1); /* read until a dwell or error */
				//            init_disp_scales(radar,dwell,disp); /* change scales to match new parameters */
				init_display(radar,dwell,disp); /* change scales to match new parameters */
				continue;
			}
			}

			products(dwell,radar,prods);  /* compute scientific radar parameters */

		vptr = pptr = nptr = sptr = zptr = cptr = prods;

		vptr += VOFF;	
		pptr += POFF;
		nptr += NOFF;
		sptr += WOFF;
		zptr += ZOFF;
		cptr += CZOFF;

		z = gmtime((const time_t *)&dwell->header.time);
		l_time = ((z->tm_hour*100 + z->tm_min)*100 + z->tm_sec);

		display(dwell,disp,prods);  /* write one dwell to display */

		/* Write to the output files. */
		fprintf(fz,"%02d%02d%02d %02d%02d%02d ",
			z->tm_year,z->tm_mon+1,z->tm_mday,
			z->tm_hour,z->tm_min,z->tm_sec);
		fprintf(fn,"%02d%02d%02d %02d%02d%02d ",
			z->tm_year,z->tm_mon+1,z->tm_mday,
			z->tm_hour,z->tm_min,z->tm_sec);
		fprintf(fv,"%02d%02d%02d %02d%02d%02d ",
			z->tm_year,z->tm_mon+1,z->tm_mday,
			z->tm_hour,z->tm_min,z->tm_sec);
		fprintf(fp,"%02d%02d%02d %02d%02d%02d ",
			z->tm_year,z->tm_mon+1,z->tm_mday,
			z->tm_hour,z->tm_min,z->tm_sec);
		fprintf(fs,"%02d%02d%02d %02d%02d%02d ",
			z->tm_year,z->tm_mon+1,z->tm_mday,
			z->tm_hour,z->tm_min,z->tm_sec);
		fprintf(fc,"%02d%02d%02d %02d%02d%02d ",
			z->tm_year,z->tm_mon+1,z->tm_mday,
			z->tm_hour,z->tm_min,z->tm_sec);
		for(i=0; i<dwell->header.gates; i++)
		{
			fprintf(fz, "%4.1f ",*zptr); 
			fprintf(fn, "%4.1f ",*nptr); 
			fprintf(fv, "%4.1f ",*vptr); 
			fprintf(fp, "%4.1f ",*pptr); 
			fprintf(fs, "%4.1f ",*sptr); 
			fprintf(fc, "%4.1f ",*cptr); 
			zptr += 16;
			nptr += 16;
			vptr += 16;
			pptr += 16;
			sptr += 16;
			cptr += 16;
		}	
		fprintf(fz, "\n"); 
		fprintf(fn, "\n"); 
		fprintf(fv, "\n"); 
		fprintf(fp, "\n"); 
		fprintf(fs, "\n"); 
		fprintf(fc, "\n"); 

	}
	while(c != 'Q' && c != 27);
	closeall();
}

void closeall(void)
{
   	close_display();
	close_disk();
	close_tape();
}

   




