C
C Program GRD2D3D to extend 2-D grid into 3-D grid
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 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 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 Names of the input and output files:
C     GRD='string'... Names of the input ASCII file with the 2-D grid
C             values.
C             Default: GRD='grd.out'
C     GRDNEW='string'... Name of the output ASCII file containing the
C             3-D grid values.  The 3-D grid value G(X1,X2,X3) is the
C             2-D grid value G(X1,X2) minus X3 coordinate,
C               G(X1,X2,X3)=G(X1,X2)-X3  .
C             Default: GRDNEW='grdnew.out'
C     For general description of the files with gridded data refer
C     to file forms.htm.
C Data specifying dimensions of the input and output grids:
C     N1=positive integer... Number of gridpoints along the X1 axis
C             (inner loop).
C             Default: N1=1
C     N2=positive integer... Number of gridpoints along the X2 axis
C             (intermediate loop).
C             Default: N2=1
C     N3=positive integer... Number of gridpoints along the X3 axis
C             (outer loop).  Applies just to the output grid.
C             Default: N3=1
C     O3=real... Third coordinate of the grid origin (first point of the
C             grid).  Applies just to the output grid.
C             Default: O3=0.
C     D3=real... Grid interval in the direction of the X3 axis.
C             Applies just to the output grid.
C             Default: D3=1.
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
C.......................................................................
C
      EXTERNAL UARRAY
      REAL UARRAY
C
C     Filenames and parameters:
      CHARACTER*80 FSEP,FGRD1,FGRD2
      INTEGER LU
      REAL UNDEF
      PARAMETER (LU=1)
C
C     Input data:
      INTEGER N1,N2,N3
      REAL O3,D3
C
C     Other variables:
      INTEGER I1,I2,I3,I,J
      REAL X3
C
      UNDEF=UARRAY()
C
C-----------------------------------------------------------------------
C
C     Reading input SEP parameter file:
      WRITE(*,'(A)') '+GRD2D3D: Enter input filename: '
      FSEP=' '
      READ(*,*) FSEP
      IF (FSEP.EQ.' ') THEN
C       GRD2D3D-01
        CALL ERROR('GRD2D3D-01: 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
      CALL RSEP1(LU,FSEP)
      WRITE(*,'(A)') '+GRD2D3D: Working ...           '
C
C     Reading input parameters from the SEP file:
      CALL RSEP3T('GRD'   ,FGRD1,'grd.out'   )
      CALL RSEP3T('GRDNEW',FGRD2,'grdnew.out')
      CALL RSEP3I('N1',N1,1)
      CALL RSEP3I('N2',N2,1)
      CALL RSEP3I('N3',N3,1)
      CALL RSEP3R('O3',O3,0.)
      CALL RSEP3R('D3',D3,1.)
      IF(N1*N2*(N3+1).GT.MRAM) THEN
C       GRD2D3D-02
        CALL ERROR('GRD2D3D-02: Too small array RAM(MRAM)')
C       Array RAM(MRAM) allocated in include file 'ram.inc' is too small
C       to contain both input (N1*N2 values) and output (N1*N2*N3
C       values) grids.  You may wish to increase the dimension MRAM in
C       file ram.inc.
      END IF
C
C     Reading input grid:
      CALL RARRAY(LU,FGRD1,'FORMATTED',.TRUE.,UNDEF,N1*N2,RAM)
C
C     Calculating 3-D grid values:
      J=N1*N2
      DO 13 I3=0,N3-1
        X3=O3+D3*FLOAT(I3)
        I=0
        DO 12 I2=0,N2-1
          DO 11 I1=0,N1-1
            I=I+1
            J=J+1
            RAM(J)=RAM(I)-X3
   11     CONTINUE
   12   CONTINUE
   13 CONTINUE
C
C     Writing output grid:
      CALL WARRAY(LU,FGRD2,'FORMATTED',.TRUE.,UNDEF,.FALSE.,0.,N1*N2*N3,
     *                                                     RAM(N1*N2+1))
      WRITE(*,'(A)') '+GRD2D3D: 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