PROGRAM awaplot( INPUT, OUTPUT, EIN, AUS );

use iostd;

VAR
    N,MAXANZ          : INTEGER;
    continue          : BOOLEAN;
    ein_name,aus_name : STRING;
    c                 : CHAR;
    EIN,AUS           : TEXT;

PROCEDURE MAIN;
VAR 
     P             : DYNAMIC ARRAY [0..N] OF REAL;
     Q,R           : REAL;
     I,J,K,ix,iy   : INTEGER;
     C             : CHAR;
     S             : STRING;
BEGIN
   WRITELN( '  plot components i vs. j  ( 0 <= i,j <= ', N, ')  ' );
   WRITELN( '  if i=j=0 then plot step size h vs. time t ' );
   WRITE  ( '                    enter i and j    : ' );
   READ(ix,iy);
 
   { --- read AWA output file and generate plot data --- }
   I:= 1;
   IF ( ix = 0 ) AND ( iy = 0 ) THEN  { plot step size h vs. time t }
   BEGIN
      REPEAT
         IF NOT EOF(EIN) THEN READLN(EIN,S);
         IF SUBSTRING(S,1,3) = 't =' THEN
         BEGIN
            Q:= R;
            R:= RVAL( SUBSTRING(S,4,LENGTH(S)-3) );
            FOR J:= 1 TO N DO READLN(EIN,S);
            if i>1 then  writeln(AUS, Q, ' ', R-Q );
            I:= SUCC(I);
         END;
      UNTIL EOF(EIN);
      WRITELN('  plot data for step size h vs. time t generated.' );
   END
   ELSE  { plot component i vs. component j }
   BEGIN
      REPEAT
         IF NOT EOF(EIN) THEN READLN(EIN,S);
         IF SUBSTRING(S,1,3) = 't =' THEN
         BEGIN
            P[0]:= RVAL( SUBSTRING(S,4,LENGTH(S)-3) );
            FOR J:= 1 TO N DO
            BEGIN
               READLN(EIN,S);
               K:= 0;
               REPEAT K:= SUCC(K) UNTIL S[K] = '.';
               P[J]:= RVAL( SUBSTRING(S,K-2,LENGTH(S)-K+3) );
            END;
            writeln(AUS, P[ix], ' ', P[iy] );
            I:= SUCC(I);
         END;
      UNTIL EOF(EIN);
      WRITELN('  plot data for component ',ix,' vs. component ',iy,' generated.' );
   END;
END {  M A I N  };

BEGIN
   WRITELN;
   WRITELN(' AWAPLOT' );
   WRITELN('    simple tool to transform AWA output files into ASCII tables' );
   WRITELN('    for further processing with plot software (e.g. GNUPLOT)' );
   WRITELN;
   REPEAT
      REPEAT
         WRITE( 'enter input file name (CR to abort)  : '); READLN; READ(ein_name);
         continue:= ( length(ein_name) = 0 )  OR  filexists(ein_name);
         IF NOT continue THEN  WRITELN('File ''',ein_name,''' not found !' );
      UNTIL continue;
      IF length(ein_name) > 0 THEN
      BEGIN
         REPEAT
            WRITE( 'enter dimension n of ode system      : ' ); READ(N);
            IF N <= 0 THEN WRITELN('Warning: n <= 0, please reenter.');
         UNTIL n > 0;
         REPEAT
            RESET( EIN, ein_name );
            REPEAT
               continue:= TRUE;
               WRITE( 'enter output file name (CR to abort) : '); READLN; READ(aus_name);
               IF ( length(aus_name) > 0 )  AND  filexists(aus_name) THEN
               BEGIN
                  WRITE('WARNING: File ''',aus_name,''' exists already ! overwrite (y/n) ? ');
                  READLN; READ(c);
                  continue:= c = 'y';
               END;
            UNTIL continue;
            IF length(aus_name) > 0 THEN
            BEGIN
               REWRITE( AUS, aus_name );
               MAIN;
            END;
         UNTIL length(aus_name) = 0;
      END;
   UNTIL length(ein_name) = 0;
END.
