C                                                            <HTML><PRE>
C Program GRDMERGE to merge two nonoverlapping sets of values given on
C the same grid into a single set.
C
C Version: 5.20
C Date: 1997, October 27
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                                                    <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','GRD1','GRD2','GRD','GRDAUX',/
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     'GRD1','GRD2'... Strings in apostrophes containing the names of
C             the input ASCII files with the grid values.  No gridpoint
C             may have the value defined in both the files if 'GRDAUX'
C             is blank (default).
C     'GRD'... String in apostrophes containing the name of the output
C             ASCII files containing the grid values collected from both
C             the input files.
C     'GRDAUX'... String in apostrophes containing the name of the
C             auxiliary output file.  If specified and nonblank, the
C             input grid values may overlap.  Of each pair of overlaping
C             values, the value of file GRD2 will be collected in this
C             file.  This file is not created if there is no overlap.
C     /...    Input data line must be terminated by a slash.
C     Default: 'SEP'='grd.h', 'GRD1'='grd1.out', 'GRD2'='grd2.out',
C             'GRD'='grd.out', 'GRDAUX'=' '.
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="ram.inc">ram.inc</A>
C
C.......................................................................
C
      INTEGER LU,N1,N2,N3,N1N2N3,I
      REAL UNDEF
      PARAMETER (LU=1,UNDEF=-999999.)
      CHARACTER*80 FSEP,FGRD1,FGRD2,FGRD,FGRDA
      LOGICAL LGRDA
C
C.......................................................................
C
C     Main input data:
C     Default:
      FSEP ='grd.h'
      FGRD1='grd1.out'
      FGRD2='grd2.out'
      FGRD ='grd.out'
      FGRDA=' '
C     Reading main input data:
      WRITE (*,'(2A)')
     *  ' Enter filenames of Grid header + 2 input and 1 output grids: '
      READ (*,*) FSEP,FGRD1,FGRD2,FGRD,FGRDA
C
C     Reading all the data from file FGRD to the memory
C     (SEP parameter file form):
      CALL RSEP1(LU,FSEP)

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
      IF(2*N1N2N3.GT.MRAM) THEN
C       <A NAME="error01"></A><FONT COLOR="RED">GRDMERGE-01</FONT>
        CALL ERROR('GRDMERGE-01: Too small array RAM(MRAM)')
C       Array RAM(MRAM) allocated in include file 'ram.inc' is too small
C       to contain two input grids (2*N1*N2*N3 values).  You may wish to
C       increse the dimension MRAM in file 'ram.inc'.
C       <A HREF="ram.inc">ram.inc</A>
      END IF
C
C     Reading input grids:
      CALL RARRAY(LU,FGRD1,'FORMATTED',.TRUE.,UNDEF,N1N2N3,RAM)
      CALL RARRAY(LU,FGRD2,'FORMATTED',.TRUE.,UNDEF,N1N2N3,
     *                                                    RAM(N1N2N3+1))
C
C     Merging the grids:
      LGRDA=.FALSE.
      DO 10 I=1,N1N2N3
        IF(RAM(I).EQ.UNDEF) THEN
          RAM(I)=RAM(N1N2N3+I)
          RAM(N1N2N3+I)=UNDEF
        ELSE
          IF(RAM(N1N2N3+I).NE.UNDEF) THEN
            IF(FGRDA.EQ.' ') THEN
C             <A NAME="error02"></A><FONT COLOR="RED">GRDMERGE-02</FONT>
              CALL ERROR('GRDMERGE-02: Overlapping grid values')
C             Value at the same gridpoint is defined in both input files
C             and the auxiliary file is not specified.
            ELSE
              LGRDA=.TRUE.
            END IF
          END IF
        END IF
   10 CONTINUE
C
C     Writing output grid:
      CALL WARRAY(LU,FGRD,'FORMATTED',.TRUE.,UNDEF,.FALSE.,0.,N1N2N3,
     *                                                              RAM)
      IF(LGRDA) THEN
        CALL WARRAY(LU,FGRDA,'FORMATTED',.TRUE.,UNDEF,.FALSE.,0.,N1N2N3,
     *                                                    RAM(N1N2N3+1))
      END IF
      STOP
      END
C
C=======================================================================
C
      INCLUDE 'error.for'
C     <A HREF="error.for">error.for</A>
      INCLUDE 'sep.for'
C     <A HREF="sep.for">sep.for</A>
      INCLUDE 'forms.for'
C     <A HREF="forms.for">forms.for</A>
      INCLUDE 'length.for'
C     <A HREF="length.for">length.for</A>
C
C=======================================================================
C                                                                 </PRE>
