#include        <conio.h>       /* for getch, kbhit */
#include        <stdio.h>       /* for malloc */
#include        <dos.h>         /* for delay() */

#define EXTERN
#include        "..\include\globals.h"

#define         PPI     0
#define         RHI     1

double DACVOLTAGE = -4.2;
int slow = 0;
extern double trcorrtog;

main(argc,argv)
char    *argv[];   
int     argc;
   {
   char         c;
   int          tape,skip=0;
   double	zsave;
   DWELL   *dwell;   
   RADAR   *radar;
   DISPLAY *disp;
   float   *prods;
   int     type=PPI,dispnum='1',dchange=0,pmode=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 < 2)
      {
      printf("Use: %s data_filename\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 */
	}

   /* 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 */

   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 *= 2;
      if(c == '2')   disp->rgpp /= 2;

      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;
      
      if(c == 'X') trcorrtog = trcorrtog == 1? 0: 1; /* Toggle TR correction */
      
      if(c == 'G') savescr(dwell->header.time); /* make a gif file */
      
      if(c == 'P' && disp->type != PPI)              {disp->type = PPI; dchange = 1; c = dispnum;}
      if(c == 'R' && disp->type != RHI)              {disp->type = RHI; dchange = 1; c = dispnum;}
      if(c == 'S')slow++;
      if(c == 'D')slow=0;
      if(c >= '0' && c <= '9' && c != dispnum) dchange = 1;
      
if(slow)usleep(slow*12000);

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 */

     display(dwell,disp,prods);  /* write one dwell to display */
      }
   while(c != 'Q' && c != 27);
   closeall();
   }

void closeall(void)
   {
   close_display();
   close_disk();
   close_tape();
   }

   




