#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 *extrfp;
   char         c;
   int          i,tape,skip=0, extr=0;
   double	zsave;
   DWELL   *dwell;   
   RADAR   *radar;
   DISPLAY *disp;
   float   *prods, *zptr, *nptr, *vptr;
   int     type=PPI,dispnum='1',dchange=0,pmode=0;   
   long	t_start=0, t_stop=0, l_time = 0;
   
   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 < 3 || argc == 4 || argc > 5)
      {
      printf("Use: %s data_filename output_filename [start_time stop_time]\n",argv[0]);
      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 */
	}
    if(argc == 5){t_start = atol(argv[3]); t_stop = atol(argv[4]);}
   if((extrfp = fopen(argv[2],"ab")) == NULL)
   	{
	printf("Can't open output file\n");
	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; /* Always jam trcorrtog to 0.0 for extract.  */

   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; /* Always jam trcorrtog to 0.0 for extract.  */
      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 */
      
      zptr = nptr = (float *)prods;
      zptr += ZOFF;
      nptr += NOFF;

      z = gmtime((const time_t *)&dwell->header.time);
      l_time = ((z->tm_hour*100 + z->tm_min)*100 + z->tm_sec);
      
      if(argc == 5)
      	extr = (t_start <= l_time && l_time < t_stop)? 1: 0;
      
      if(argc != 5 || extr)
      		display(dwell,disp,prods);  /* write one dwell to display */
      if(extr)
      	{
	fprintf(extrfp,"%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(extrfp, "%4.1f ",*nptr > .15? *zptr: -99.); 
		zptr += 16;
		nptr += 16;
		}	
	fprintf(extrfp, "\n");
	}	
      }
   while(c != 'Q' && c != 27);
   closeall();
   }

void closeall(void)
   {
   close_display();
   close_disk();
   close_tape();
   }

   




