C                                                            <HTML><PRE>
C Program MTTGRD converting multivalued travel times produced by program
C MTT into several singlevalued grids corresponding to individual ray
C histories.
C
C Version: 5.10
C Date: 1997, October 22
C
C Coded by: Ludek Klimes
C     Department of Geophysics, Charles University Prague,
C     Ke Karlovu 3, 121 16 Praha 2, Czech Republic,
C     E-mail: klimes@seis.karlov.mff.cuni.cz
C
C.......................................................................
C
C Program MTTGRD is similar to program MGRD of package FORMS.  The only
C difference is that program MTTGRD separates the grid values according
C to the ray histories, and generates the output single-valued grids
C separately for each ray history.  The number of output single-valued
C grids is thus roughly twice the number of single-valued grids produced
C for the same data by program MGRD.  The user may thus choose between
C programs MGRD and MTTGRD to convert a multivalued travel-time grid
C into single-valued grids, depending on which kind of output he
C prefers.
C
C.......................................................................
C
C                                                    <A NAME="data"></A>
C Description of the data files:
C
C The data are read in by the list directed input (free format).
C In the description of data files, each numbered paragraph indicates
C the beginning of a new input operation (new READ statement).
C If the symbolic name of the input variable is enclosed in apostrophes,
C the corresponding value in input data is of the type CHARACTER, i.e.
C it should be a character string enclosed in apostrophes.  If the first
C letter of the symbolic name is I-N, the corresponding value is of the
C type INTEGER.  Otherwise, the input parameter is of the type REAL and
C may or may not contain a decimal point.
C
C Input data read from the * external unit:
C     The interactive * external unit may also be redirected to the file
C     containing the relevant data.
C (1) 'SEP','NUM','HIST','MGRD','GRD',/
C     'SEP'...String in apostrophes containing the name of the input
C             file with the data specifying grid dimensions.
C             <A HREF="#SEP">Description of file SEP</A>
C     'NUM'...String in apostrophes containing the name of the input
C             ASCII file containing, for each gridpoint, the integer
C             number of given values (travel times).
C     'HIST'..String in apostrophes containing the name of the input
C             file containing, for each gridpoint, the ray histories
C             corresponding to all given values (travel times).
C     'MGRD'..String in apostrophes containing the name of the input
C             file containing, for each gridpoint, all given values
C             (travel times).
C     'GRD'...String in apostrophes containing the template name of the
C             output ASCII files with the generated single-valued grid
C             values.  The number of output files equals the maximum
C             number of values given at a point.
C             Generation of names of output files:
C               All digits contained within the filename are assumed to
C               form an integer number.  This number is increased by 1
C               for the first output file, by 2 for the second one, etc.
C               The other characters of the filename remain unchanged.
C     /...    Input data line must be terminated by a slash.
C     Default: 'SEP'='grd.h', 'NUM'='num.out', 'HIST'='hist.out',
C             'MGRD'='mtt.out', 'GRD'='grd00.out'
C
C                                                     <A NAME="SEP"></A>
C Data file SEP has the form of the SEP (Stanford Exploration Project)
C parameter file:
C     All the data are specified in the form of PARAMETER=VALUE, e.g.
C     N1=50, with PARAMETER directly preceding = without intervening
C     spaces and with VALUE directly following = without intervening
C     spaces.  The PARAMETER=VALUE couple must be delimited by a space
C     or comma from both sides.
C     The PARAMETER string is not case-sensitive.
C     PARAMETER= followed by a space resets the default parameter value.
C     All other text in the input files is ignored.  The file thus may
C     contain unused data or comments without leading comment character.
C     Everything between comment character # and the end of the
C     respective line is ignored, too.
C     The PARAMETER=VALUE couples may be specified in any order.
C     The last appearance takes precedence.
C Data specifying grid dimensions:
C     N1=positive integer... Number of gridpoints along the X1 axis.
C             Default: N1=1
C     N2=positive integer... Number of gridpoints along the X2 axis.
C             Default: N2=1
C     N3=positive integer... Number of gridpoints along the X3 axis.
C             Default: N3=1
C
C=======================================================================
C
C Common block /RAMC/:
      INCLUDE 'ram.inc'
C     <A HREF="../forms/ram.inc">ram.inc</A>
C
      INTEGER IRAM(MRAM)
      EQUIVALENCE (IRAM,RAM)
C
C.......................................................................
C
      CHARACTER*80 FGRD,FMUL,FHIS,FVAL,FOUT
      INTEGER INDEF,LU,I,J,K,M,N,N1,N2,N3,N1N2N3,KK,MM,IHIST
      REAL UNDEF
      PARAMETER (LU=1,INDEF=-999999,UNDEF=-999999.)
C
C.......................................................................
C
C     Main input data:
C     Default:
      FGRD='grd.h'
      FMUL='num.out'
      FHIS='hist.out'
      FVAL='mtt.out'
      FOUT='grd00.out'
C     Reading main input data:
      WRITE (*,'(2A)')
     *  ' Enter 5 filenames (SEP,NUM,HIST,MGRD,GRD): '
      READ (*,*) FGRD,FMUL,FHIS,FVAL,FOUT
C
C     Reading all the data from file FGRD to the memory
C     (SEP parameter file form):
      CALL RSEP1(LU,FGRD)

C     Recalling the data specifying grid dimensions
C     (arguments: Name of value in input data, Variable, Default):
      CALL RSEP3I('N1',N1,1)
      CALL RSEP3I('N2',N2,1)
      CALL RSEP3I('N3',N3,1)
      N1N2N3=N1*N2*N3
C
      IF(N1N2N3.GT.MRAM) THEN
C       <A NAME="error01"></A><FONT COLOR="RED">MTTGRD-01</FONT>
        PAUSE 'Error MTTGRD-01: Too small array RAM'
C       Dimension MRAM of array RAM in include file
C       <A HREF="../forms/ram.inc">ram.inc</A>
C       should probably be increased to accommodate the input integer
C       grid values.
        STOP
      END IF
      CALL RARRAI(LU,FMUL,'FORMATTED',.TRUE.,1,N1N2N3,IRAM)
      M=0
      N=0
      DO 10 I=1,N1N2N3
        M=MAX0(IRAM(I),M)
        N=N+IRAM(I)
   10 CONTINUE
      IF(2*N1N2N3+2*N.GT.MRAM) THEN
C       <A NAME="error02"></A><FONT COLOR="RED">MTTGRD-02</FONT>
        PAUSE 'Error MTTGRD-02: Too small array RAM'
C       Dimension MRAM of array RAM in include file
C       <A HREF="../forms/ram.inc">ram.inc</A>
C       should probably be increased to accommodate the input integer
C       grid values, all input multivalued grid values, and one output
C       singlevalued grid.
        STOP
      END IF
      CALL RARRAY(LU,FVAL,'FORMATTED',.TRUE.,UNDEF,N,RAM(2*N1N2N3+1))
      CALL RARRAI(LU,FHIS,'FORMATTED',.TRUE.,INDEF,N,IRAM(2*N1N2N3+N+1))
      WRITE(*,'(A)') '+         Output grid:'
      WRITE(*,'(A)') '+History:'
      WRITE(*,'(A)')
C
C     Loop over singlevalued grids:
      DO 50 J=1,999999
        IHIST=INDEF
        M=0
        K=2*N1N2N3
        DO 20 I=1,N1N2N3
          MM=0
          RAM(N1N2N3+I)=UNDEF
          DO 15 KK=K+N+1,K+N+IRAM(I)
            IF(IRAM(KK).NE.INDEF) THEN
              IF(IHIST.EQ.INDEF) THEN
                IHIST=IRAM(KK)
              END IF
              IF(IRAM(KK).EQ.IHIST.AND.RAM(N1N2N3+I).EQ.UNDEF) THEN
                IRAM(KK)=INDEF
                RAM(N1N2N3+I)=RAM(KK-N)
              ELSE
                MM=MM+1
              END IF
            END IF
   15     CONTINUE
          M=MAX0(MM,M)
          K=K+IRAM(I)
   20   CONTINUE
C
C       Generating output file name:
        K=LEN(FOUT)
   30   CONTINUE
        DO 31 I=K,1,-1
          IF(LLE('0',FOUT(I:I)).AND.LLE(FOUT(I:I),'8')) THEN
            FOUT(I:I)=CHAR(ICHAR(FOUT(I:I))+1)
            GO TO 32
          ELSE IF(FOUT(I:I).EQ.'9') THEN
            FOUT(I:I)='0'
            K=I-1
            GO TO 30
          END IF
   31   CONTINUE
C         <A NAME="error03"></A><FONT COLOR="RED">MTTGRD-03</FONT>
          PAUSE 'Error MTTGRD-03: Too many output grids'
C         The digits in the template name of the output files do not
C         allow for the generation of all singlevalued output grids.
C         The number of digits should be increased.
          STOP
   32   CONTINUE
C
C       Writing output file:
        CALL WARRAY(LU,FOUT,'FORMATTED',
     *                     .TRUE.,UNDEF,.FALSE.,0.,N1N2N3,RAM(N1N2N3+1))
        WRITE(*,'(A,I7)') '+',IHIST
        WRITE(*,'(A,I7)')
C
C       M is the maximum number of still unwritten gridpoint values:
        IF(M.LE.0) THEN
          GO TO 90
        END IF
   50 CONTINUE
C
   90 CONTINUE
      STOP
      END
C
C=======================================================================
C
      INCLUDE 'sep.for'
C     <A HREF="../forms/sep.for">sep.for</A>
      INCLUDE 'length.for'
C     <A HREF="../forms/length.for">length.for</A>
      INCLUDE 'forms.for'
C     <A HREF="../forms/forms.for">forms.for</A>
C
C=======================================================================
C                                                                 </PRE>
