C
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: 6.00
C Date: 2005, November 12
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 Description of data files:
C
C Input data read from the standard input device (*):
C     The data are read by the list directed input (free format) and
C     consist of a single string 'SEP':
C     'SEP'...String in apostrophes containing the name of the input
C             SEP parameter or history file with the input data.
C     No default, 'SEP' must be specified and cannot be blank.
C
C                                                     
C Input/output data file 'SEP':
C     File 'SEP' has the form of the SEP
C     parameter file.  The parameters, which do not differ from their
C     defaults, need not be specified in file 'SEP'.
C Data describing the grid:
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 Names of the input and output files:
C     NUM='string'...Name of the input ASCII file containing, for each
C             gridpoint, the integer number of given values (travel
C             times) in the gridpoint.
C             For general description of files with gridded data refer
C             to file forms.htm.
C             Default: NUM='num.out'
C     HIST='string'...Name of the input file containing, for each
C             gridpoint, the ray histories corresponding to all given
C             values (travel times).
C             For general description of the files with multivalued
C             gridded data refer to file
C             forms.htm.
C             Default: HIST='hist.out'
C     MGRD='string'...Name of the input file containing, for each
C             gridpoint, all given values (travel times).
C             For general description of the files with multivalued
C             gridded data refer to file
C             forms.htm.
C             Default: MGRD='mtt.out'
C     GRD='string'...String in apostrophes controling the name(s) of the
C             output ASCII files with data cubes:
C             For IMGRD=0:
C               The name of the output file with the generated
C               single-valued grids stored as several "snapshots".
C               The number of "snapshots" equals the maximum number of
C               values given at a point.
C             Otherwise:
C               The template name of the output files with the generated
C               single-valued grids.  The number of output files equals
C               the maximum number of values given at a point.
C               Generation of names of output files:
C                 All digits contained within the filename are assumed
C                 to form an integer number.  This number is increased
C                 by 0 for the first output file, by 1 for the second
C                 one, etc. The other characters of the filename remain
C                 unchanged.
C             For general description of files with gridded data refer
C             to file forms.htm.
C             Default: GRD='grd00.out'
C     IMGRD=integer:
C             IMGRD=0: Output single-valued grids are stored as several
C               "snapshots" in a single output file.  The number of
C               "snapshots" is appended to the end of the input SEP
C               file in the form of: N4=integer
C             Otherwise: Output single-valued grids are stored in
C               separate output files.
C             Default: IMGRD=0
C Output data appended at the end of file (written just if IMGRD=0):
C     N4=positive integer... Number of single-valued grids ("snapshots")
C             in the output file GRD.
C Optional parameters specifying the form of the real quantities
C written in the output formatted files:
C     MINDIG,MAXDIG=positive integers ... See the description in file
C             forms.for.
C
C=======================================================================
C
C Common block /RAMC/:
      INCLUDE 'ram.inc'
C     ram.inc
C
      INTEGER IRAM(MRAM)
      EQUIVALENCE (IRAM,RAM)
C
C.......................................................................
C
      EXTERNAL UARRAY
      REAL UARRAY
C
      CHARACTER*80 FGRD,FMUL,FHIS,FVAL,FOUT
      INTEGER INDEF,LU1,LU2,MGRD,I,I4,K,M,N,N1,N2,N3,N1N2N3,KK,MM,IHIST
      REAL UNDEF
      PARAMETER (LU1=1,LU2=2,INDEF=-999999)
C
      UNDEF=UARRAY()
C
C-----------------------------------------------------------------------
C
C     Reading name of SEP file with input data:
      WRITE(*,'(A)') '+MTTGRD: Enter input filename: '
      FGRD=' '
      READ(*,*) FGRD
      WRITE(*,'(A)') '+MTTGRD: Working ...           '
C
C     Reading all data from the SEP file into the memory:
      IF (FGRD.NE.' ') THEN
        CALL WSEP1(LU1,FGRD)
C       File remains open for writing.
      ELSE
C       MTTGRD-04
        CALL ERROR('MTTGRD-04: SEP file not given')
C       Input file in the form of the SEP (Stanford Exploration Project)
C       parameter or history file must be specified.
C       There is no default filename.
      ENDIF
C
C     Reading input parameters from the SEP file:
      CALL RSEP3T('NUM',FMUL,'num.out')
      CALL RSEP3T('HIST',FHIS,'hist.out')
      CALL RSEP3T('MGRD',FVAL,'mtt.out')
      CALL RSEP3T('GRD',FOUT,'grd00.out')
C
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
      CALL RSEP3I('IMGRD',MGRD,0)
C
      IF(N1N2N3.GT.MRAM) THEN
C       MTTGRD-01
        CALL ERROR('MTTGRD-01: Too small array RAM')
C       Dimension MRAM of array RAM in include file
C       ram.inc
C       should probably be increased to accommodate the input integer
C       grid values.
      END IF
      CALL RARRAI(LU2,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       MTTGRD-02
        CALL ERROR('MTTGRD-02: Too small array RAM')
C       Dimension MRAM of array RAM in include file
C       ram.inc
C       should probably be increased to accommodate the input integer
C       grid values, all input multivalued grid values, and one output
C       singlevalued grid.
      END IF
      CALL RARRAY(LU2,FVAL,'FORMATTED',.TRUE.,UNDEF,N,RAM(2*N1N2N3+1))
      CALL RARRAI(LU2,FHIS,'FORMATTED',.TRUE.,INDEF,N,
     *                                             IRAM(2*N1N2N3+N+1))
      WRITE(*,'(A)') '+History:'
      WRITE(*,'(A)')
C
C     Loop over singlevalued grids:
      DO 50 I4=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       M is the maximum number of still unwritten gridpoint values:
C
        IF(MGRD.EQ.0) THEN
C         Writing output file:
          IF(I4.EQ.1) THEN
            OPEN(LU2,FILE=FOUT,FORM='FORMATTED')
          END IF
          CALL WARRAY(LU2,' ','FORMATTED',
     *                     .TRUE.,UNDEF,.FALSE.,0.,N1N2N3,RAM(N1N2N3+1))
          WRITE(*,'(A,I7)') '+',IHIST
          WRITE(*,'(A,I7)')
          IF(M.LE.0) THEN
            CLOSE(LU2)
            CALL WSEP3I(LU1,'N4',I4)
            GO TO 90
          END IF
        ELSE
C         Generating new output filename:
          IF(I4.GT.1) THEN
            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             MTTGRD-03
              CALL 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.
   32       CONTINUE
          END IF
C
C         Writing output file:
          CALL WARRAY(LU2,FOUT,'FORMATTED',
     *                     .TRUE.,UNDEF,.FALSE.,0.,N1N2N3,RAM(N1N2N3+1))
          WRITE(*,'(A,I7)') '+',IHIST
          WRITE(*,'(A,I7)')
          IF(M.LE.0) THEN
            GO TO 90
          END IF
        END IF
   50 CONTINUE
C
   90 CONTINUE
      CLOSE(LU1)
      WRITE(*,'(A)') '+MTTGRD: Done.                 '
      STOP
      END
C
C=======================================================================
C
      INCLUDE 'error.for'
C     error.for
      INCLUDE 'sep.for'
C     sep.for
      INCLUDE 'forms.for'
C     forms.for
      INCLUDE 'length.for'
C     length.for
C
C=======================================================================
C