#include "gwin.user.h"

float sdata[10][1000];
float mindata =  1e15;
float maxdata = -1e15;
char s[8][20];
float wind[4],xdx,height,width;
int n,npoints;
int reset(),cancel_redraw;
int quit();

main()
{
char string[256];
char string1[100];
int i,j;
float x,y,xanchor,yanchor,xold,yold;


   while(1 == 1){
      if(!gets(string))exit(1); /* read one line */
      if(!strncmp("     TIME",string,(int)9)){  /* look for data */
         printf("FOUND DATA\n");
         break;
      }
   }
   n = sscanf(string,"%s %s %s %s %s %s %s %s %s %s",
                      s[0],s[1],s[2],s[3],s[4],s[5],s[6]
                      ,s[7],s[8],s[9]);  /* store field names */


   /* print field names */
   printf("%s %s %s %s %s\n",s[0],s[1],s[2],s[3],s[4],s[5]);

   if(!gets(string))exit(1);  /* skip blank line */

   j = 0;
   while(1 == 1){
      for(i=0;i<n;i++){
         if(scanf("%e",&sdata[i][j]) < 1)goto plot;
         if(sdata[i][j]>maxdata) maxdata = sdata[i][j];
         if(sdata[i][j]<mindata) mindata = sdata[i][j];
      }
      j++;
   }

plot:
   npoints = j;

   USTART("high2",(float)0.,(float)640.0,(float)0.,(float)400.0);

   uamenu(G,1,0,0,"COMMANDS",' ',0,MIDRAWN|MENUENABLED,NULL);
   uamenu(G,1,1,0,"RESET",'R',0,MIDRAWN|ITEMTEXT|HIGHCOMP
          |COMMSEQ|ITEMENABLED,reset);
   uamenu(G,1,2,0,"QUIT",'Q',0,MIDRAWN|ITEMTEXT|HIGHCOMP
          |COMMSEQ|ITEMENABLED,quit);

   reset();
   uuev.key = '\0';

   while(1 == 1){
      while(uuev.key != 'a') {
         if(ugrinl(G,&x,&y,&uuev)){UEND();exit(0);};

         sprintf(string1,"x = %15.8e",x);
         uprint(G, wind[0] + .1*width, wind[3] -.05*height, string1);
         sprintf(string1,"y = %15.8e",y);
         uprint(G, wind[0] + .5*width, wind[3] -.05*height, string1);
      }
      uset(G,"comp");
      xanchor = x;   /* anchor point */
      yanchor = y;
      xold = x;
      yold = y;

      while (uuev.key != 'A'){
         if(ugrinl(G,&x,&y,&uuev)){UEND();exit(0);};
         urect(G,xanchor,yanchor,xold,yold);
         urect(G,xanchor,yanchor,x,y);
         xold = x;
         yold = y;
      }

      if((xanchor != x) && (yanchor != y)){
         wind[0] = (xanchor < x) ? xanchor : x;
         wind[1] = (xanchor > x) ? xanchor : x;
         wind[2] = (yanchor < y) ? yanchor : y;
         wind[3] = (yanchor > y) ? yanchor : y;
      }

      width  = wind[1] - wind[0];
      height = wind[3] - wind[2];

      uwindo(G,wind[0],wind[1],wind[2],wind[3]);

      uset(G,"ncom");
      uerase(G);
      uoutln(G);
      uset(G,"clip");

      redraw();
   }
}


reset()
{
   wind[0] = sdata[0][0];
   wind[1] = sdata[0][npoints-1];
   wind[2] = mindata - .1*(maxdata-mindata);
   wind[3] = maxdata + .1*(maxdata-mindata);

   width  = wind[1] - wind[0];
   height = wind[3] - wind[2];

   uwindo(G,wind[0],wind[1],wind[2],wind[3]);

   uset(G,"ncli");

   upset(G,"ccol",(float)0.0);
   uerase(G);
   uoutln(G);

   redraw();
   cancel_redraw = 1;
}

redraw()
{
int i,j;
float x,y;

   cancel_redraw = 0;
   for(i=1;i<n;i++){
      upset(G,"colo",(float)(i+2));
      uprint(G, wind[0]+.8*width, wind[3]-(.04 + .03*(i-1))*height,s[i]);
      umove(G,sdata[0][0],sdata[i][0]);
      for(j=1;j<npoints;j++){
         if(ugrina(G,&x,&y,&uuev)){UEND();exit(0);};
         if(cancel_redraw){
            upset(G,"colo",7.0);
            return(0);
         }
         udraw(G,sdata[0][j],sdata[i][j]);
      }
   }
   upset(G,"colo",7.0);
}

quit()
{
   UEND();
   exit(0);
}

