C
C Program GRDRAN to compute the pseudorandom numbers distributed
C uniformly between -0.5 and 0.5 on the input grid.
C
C Version: 7.40
C Date: 2017, May 19
C
C Coded by Petr Bulant
C     Department of Geophysics, Charles University Prague,
C     Ke Karlovu 3, 121 16 Praha 2, Czech Republic,
C     http://sw3d.cz/staff/bulant.htm
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 Data specifying the parameters of 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 Data specifying the distribution:
C     RANDIS='string'... Character specifying the distribution:
C             RANDIS='U' or RANDIS='u':  Uniform distribution between
C                                        -0.5 and 0.5.
C             RANDIS='G' or RANDIS='g':  Gaussian distribution.
C             Default: RANDIS='U'
C Selection of a particular pseudorandom representation:
C     ISEED=integer ... Nonzero integer seed for the generation of
C             pseudorandom numbers.  Its sign is ignored.
C     Default: ISEED=-1.
C Name of output formatted file with the computed values:
C     RANOUT='string'
C             Default: RANOUT='grdran.out'
C     For general description of the files with gridded data refer to
C     file forms.htm.
C Optional parameters specifying the form of the quantities
C written in the output formatted files:
C     MINDIG,MAXDIG=positive integers ... See the description in file
C             forms.for.
C     NUMLIN=positive integer ... Number of reals or integers in one
C             line of the output file. See the description in file
C             forms.for.
C Value of undefined quantities:
C     UNDEF=real... The value to be used for undefined real quantities.
C             Default: UNDEF=undefined value used in forms.for
C
C-----------------------------------------------------------------------
C
C Subroutines and external functions required:
      EXTERNAL ERROR,RSEP1,RSEP3T,RSEP3I,WARRAY,RAN3
      REAL RAN3
C     ERROR...File error.for.
C     RSEP1,RSEP3T,RSEP3I...
C             File sep.for.
C     WARRAY..File forms.for.
C     RAN3... File
C     ran3.for of Numerical Recipes.
C
C Common block /RAMC/:
      INCLUDE 'ram.inc'
C     ram.inc
C
      CHARACTER*80 FILSEP,FILOUT
      CHARACTER*1 RANDIS
      INTEGER LU1
      PARAMETER (LU1=1)
      INTEGER N1,N2,N3,ISEED,I1,N1N2N3
C-----------------------------------------------------------------------
C
C     Reading a name of the file with the input data:
      WRITE(*,'(A)') '+GRDRAN: Enter input filename: '
      FILSEP=' '
      READ(*,*) FILSEP
C
C     Reading all the data from the SEP file into the memory:
      IF (FILSEP.NE.' ') THEN
        CALL RSEP1(LU1,FILSEP)
      ELSE
C       GRDRAN-01
        CALL ERROR('GRDRAN-01: SEP file not given')
      ENDIF
C
      WRITE(*,'(A)') '+GRDRAN: Working ...           '
C
C     Filename of the output file:
      CALL RSEP3T('RANOUT',FILOUT,'grdran.out')
C     The values describing the grid:
      CALL RSEP3I('N1',N1,1)
      CALL RSEP3I('N2',N2,1)
      CALL RSEP3I('N3',N3,1)
      N1N2N3=N1*N2*N3
      IF (N1N2N3.GT.MRAM) THEN
C       GRDRAN-02
        CALL ERROR('GRDRAN-02: Small array RAM.')
      ENDIF
C     Reading distribution:
      CALL RSEP3T('RANDIS',RANDIS,'U')
C     Reading numerical constant ISEED:
      CALL RSEP3I('ISEED',ISEED,-1)
C
C     Random numbers:
C     Uniform distribution
      IF (RANDIS.EQ.'U'.OR.RANDIS.EQ.'u') THEN
        DO 10, I1=1,N1N2N3
          RAM(I1)=RAN3(ISEED)-0.5
   10   CONTINUE
      ELSE IF (RANDIS.EQ.'G'.OR.RANDIS.EQ.'g') THEN
C       Gaussian distribution
        DO 21, I1=1,N1N2N3,2
   20     CONTINUE
            V1=2.*RAN3(ISEED)-1.
            V2=2.*RAN3(ISEED)-1.
            VV=V1*V1+V2*V2
          IF(VV.GE.1.) GO TO 20
          VV=SQRT(-2.*ALOG(VV)/VV)
          RAM(I1)=V1*VV
          IF (I.LT.N1N2N3) THEN
            RAM(I1+1)=V2*VV
          END IF
   21   CONTINUE
      ELSE
C       GRDRAN-03
        CALL ERROR('GRDRAN-03: Unknown distribution.')
C       Check the value of parameter RANDIS.
      ENDIF
C
      IF (FILOUT.NE.' ') THEN
        CALL WARRAY(LU1,FILOUT,'FORMATTED',.FALSE.,0.,.FALSE.,0.,
     *              N1N2N3,RAM)
      ENDIF
      WRITE(*,'(A)') '+GRDRAN: 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
      INCLUDE 'ran3.for'
C     ran3.for of Numerical Recipes
C
C=======================================================================
C