C
C Program RPPLOT to plot ray parameters
C
C Version: 7.20
C Date: 2015, April 7
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 Program to create simple PostScript plot of the distribution of the
C rays and homogeneous triangles either on the normalized ray domain or
C on the reference surface. The program is able to plot any of following
C objects: basic rays, two-point rays, auxiliary rays, homogeneous
C triangles, receivers. Objects are colour-coded according to the
C ray history, colours may be chosen. Rays are symbol-coded according
C to the ray history, symbols and their heights may be chosen. The
C limits of displayed part of the ray domain (or reference surface)
C may also be given by input data.
C
C The program is intended for results of two-parametric ray tracing,
C but may be also used to display results of one-parametric ray tracing.
C Note that then the dimension of normalized ray domain is ANUM x BNUM
C and there are no triangles and no auxiliary rays.
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
C     SEP
C     parameter file.  The parameters, which do not differ from their
C     defaults, need not be specified in file 'SEP'.
C Integers describing, what is to be plotted:
C     IRBAS=integer... 1 if basic rays are to be plotted. Other value
C             means that the basic rays are not to be plotted.
C             Default: IRBAS=1
C     IRTWO=integer... 1 if two-point rays are to be plotted.
C             Other value disables plotting of the two-point rays.
C             Default: IRTWO=1
C     IRAUX=integer... 1 if auxiliary rays are to be plotted.
C             Other value disables plotting of the auxiliary rays.
C             Default: IRAUX=1
C     ITHOM=integer... 1 if homogeneous triangles are to be plotted.
C             Other value disables plotting of the triangles.
C             Default: ITHOM=1
C     ISANG=integer... 1 if the distribution of rays and (or) triangles
C             on the normalized ray domain is to be plotted. Otherwise
C             the distribution of (end)points of the successful rays
C             on the receiver surface will be plotted.
C             Default: ISANG=1
C     ISHP=integer... 0 if objects of all histories are to be plotted,
C             otherwise only the objects of the history equal to ISHP
C             will be plotted.
C             Default: ISHP=0
C     ISUC=integer... 1 if objects of positive histories are to be
C             plotted in colors according to COLORS, and objects of
C             negative histories are to be ploted in black. Note that
C             positive ray histories correspond to the successful rays,
C             which pass the reference surface.
C             Otherwise objects of all histories are to be plotted
C             in colors according to file COLORS.
C             Default: ISUC=0
C     IHIST=INTEGER... 1 if symbols corresponding to the plotted ray
C             histories and their numbers are to be plotted under the
C             horizontal axis of the figure. The numbers and symbols
C             are always plotted in corresponding colors, even if
C             nonzero ISUC or ISHP are given. Maximum of 99 symbols
C             may be plotted. The symbols and numbers are HTEXT high.
C             Default: IHIST=0
C     ICTWO=nonnegative integer... Index of colour to be used for
C             plotting the two-point rays. Description of file COLORS.
C             For ICTWO=0 the two-point rays are plotted in the same
C             colour as other rays.
C             Default: ICTWO=0
C     ISRCS=nonnegative integer... Index of the source for which ray
C             parameters are to be plotted. File 'CRT-I' with initial
C             points may contain several waves of several sources, but
C             only one elementary wave may be plotted by a single
C             invocation of RPPLOT.
C             For ISRCS=0 the first elementary wave from file 'CRT-I'
C             is plotted, regardless of its source index.
C             Default: ISRCS=0
C     IWAVES=nonnegative integer... Index of the wave to be plotted.
C             File 'CRT-I' with initial points may contain several
C             waves, but only one wave may be plotted by a single
C             invocation of RPPLOT.
C             For IWAVES=0 the first elementary wave from file 'CRT-I'
C             is plotted.
C             Default: IWAVES=0
C
C Limits of the plot, either in normalized ray parameters (if ISANG=1),
C or in reference surface coordinates.
C     PLIM1=real... Minimum of first parameter (coordinate).
C             Default: PLIM1=0.
C     PLIM2=real... Maximum of first parameter (coordinate).
C             Default: PLIM2=1.
C     PLIM3=real... Minimum of second parameter (coordinate).
C             Default: PLIM3=0.
C     PLIM4=real... Maximum of second parameter (coordinate).
C             Default: PLIM4=1.
C
C Heights of symbols on the resulting plot in centimeters:
C     HRBAS=real... Height of symbols for basic rays.
C             Default: HRBAS=0.2
C     HRTWO=real... Height of symbols for two-point rays.
C             Default: HRTWO=0.2
C     HRAUX=real... Height of symbols for auxiliary rays.
C             Default: HRAUX=0.2
C     HTEXT=real... Height of the text along axes of the figure.
C             Default: HTEXT=0.5
C
C Dimensiones of the plot in centimeters:
C     HSIZE=real... Horizontal dimension of the plot (without legend).
C             Default: HSIZE=15.
C     VSIZE=real... Vertical dimension of the plot (without legend).
C             Default: VSIZE=15.
C
C PostScript instructions:
C     CALCOPS='string'... String with the PostScript instructions, see
C             file calcops.for.
C     SHOWPAGE=integer... Switch to remove the 'showpage' command, see
C             file calcops.for.
C
C Names of output and input files:
C     RPPLOT='string'... Name of the output PostScript file with the
C             figure. If RPPLOT=' ' (default), files 'plot00.ps',
C             'plot01.ps', ... are generated.
C             Default: RPPLOT=' '
C     SYMBOLS='string'... Name of the file with numbers of symbols.
C             This file has on each line a single integer number,
C             which is the number of symbol to be used for rays
C             and triangles of the history equal to number of the
C             line. I.e., on the first line is a number of symbol
C             to be used for plotting of rays and triangles with
C             history 1 or -1.
C             Description of file SYMBOLS.
C             Default: SYMBOLS=' '
C     COLORS='string'... Name of the file with numbers of colors.
C             Description of file COLORS.
C             Default: COLORS=' '
C     CRTOUT='string'... File with the names of the output files of
C             program CRT. If blank, default names are considered.
C             For general description of file CRTOUT refer to file
C             writ.for.
C             Description specific to this program:
C               Just the first set of names of CRT output files is read
C               from file CRTOUT. Only files
C               'CRT-I' and
C               'CRT-T'
C               are read by RPPLOT.
C             Default: CRTOUT=' ' which means 'CRT-I'='s01i.out' and
C               'CRT-T'='t01.out'
C
C Filename and parameters used for plotting receivers. Used only
C when ISANG=0.
C     REC='string'... If non-blank, the name of the file with the names
C             and coordinates of the receiver points.
C             Description of file
C             REC.
C             Default: REC=' '
C     RPAR='string'... String containing the name of the file with the
C             data specifying the take-off parameters of the calculated
C             rays.  Only the values ISRFX1 and ISRFX2 are read from
C             RPAR.  Data set RPAR must be in separate file, it must
C             not be appended to file DCRT.   See the
C             description of data set
C             RPAR
C             in subroutine file 'rpar.for'.
C             Default: RPAR=' ' which means ISRFX1=-1 and ISRFX2=-2
C     ICREC=positive integer... Color to be used for plotting the
C             receivers. The color is chosen according to COLORS.
C             Default: ICREC=1
C     ISREC=positive integer... Index of symbol to be used for plotting
C             the receivers according to the file SYMBOLS.
C             Default: ISREC=3
C     HREC=real...  Height (in centimeters) of the symbols used for
C             plotting the receivers.
C             Default: HREC=0.3
C
C Example of the input parameters in history file
C len-crt.h.
C
C
C                                                 
C Input formatted file SYMBOLS:
C     The file contains in I-th line a single integer telling the index
C     of symbol to be used to plot the rays or triangles of the history
C     I or -I. Only MSYMB different symbols are available, all the rays
C     with value of ray history greater or equal (in absolut value)
C     MSYMB will be ploted with the same symbols.
C For MSYMB refer to the file 'rpplot.inc'.
C Example of data SYMBOLS.
C Another example of data SYMBOLS.
C
C                                                  
C Input formatted file COLORS:
C     The file contains in I-th line a single integer telling the index
C     of colour to be used to plot the rays or triangles of the history
C     I or -I according to the file
C     'calcops.rgb',
C     or according to the colors defined in the source code
C     'calcops.for'
C     if 'calcops.rgb' is not available.
c     Only MCOL different colors are available, all the rays
C     and triangles with value of ray history greater or equal (in
C     absolut value) MCOL will be ploted in the same colors.
C For MCOL refer to the file 'rpplot.inc'.
C Example of data COLORS.
C
C ......................................................................
C Subroutines and external functions required:
      EXTERNAL RPPLER,CIREAD,CIERAS,RPTPL,RPRPL,RPSYMB,SYMBOL,
     *ERROR,RSEP1,RSEP3T,RSEP3R,RSEP3I,PLOTS,PLOT,NEWPEN,PLOTN,AP00
C     RPPLER,CIREAD,CIERAS,RPTPL,RPRPL ... This file.
C     RPSYMB... File rpsymb.for.
C     ERROR... File error.for.
C     RSEP1,RSEP3I,RSEP3T,RSEP3R... File sep.for.
C     PLOTS,PLOT,NEWPEN,PLOTN,SYMBOL... File calcops.for.
C     AP00 ... File ap.for.
C
C Common block /RAM/ to store the information about triangles and rays:
      INCLUDE 'rpplot.inc'
C
C.......................................................................
C
C     Auxiliary storage locations:
      INTEGER IRAY1,IRAY2,IRAY3,IR0
      INTEGER I1,I2,IT1,NCHAR
      INTEGER I,J,K,L,M,N,ISRFX1,ISRFX2
      INTEGER IRBAS,IRTWO,IRAUX,ITHOM,ISANG,ISREC,ICREC,IWAVES,ISRCS
      REAL HREC,XREC(3),P1,P2,DP
      INTEGER LU0,LU1,LU2,LU3,LU4,LU5,LU6,LU7
      LOGICAL LEND
      PARAMETER (LU0=10,LU1=1,LU2=2,LU3=3,LU4=4,LU5=5,LU6=6,LU7=7)
      CHARACTER*2  TEXT
      CHARACTER*80 FILSEP,FILINI,FILTRI,FILSYM,FILCOL,FILCRT,FILREC,
     *             FILRPA,CH,CH1,FILEPS
C-----------------------------------------------------------------------
C
      LEND=.FALSE.
C
C     Reading name of SEP file with input data:
      WRITE(*,'(A)') '+RPPLOT: Enter input filename: '
      FILSEP=' '
      READ(*,*) FILSEP
C
C     Reading all data from the SEP file into the memory:
      IF (FILSEP.NE.' ') THEN
        CALL RSEP1(LU0,FILSEP)
      ELSE
C       RPPLOT-04
        CALL ERROR('RPPLOT-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
      WRITE(*,'(A)') '+RPPLOT: Working...            '
C
      ISHMAX=0
C
C     Reading quantities telling what is to be plotted:
      CALL RSEP3I('IRBAS',IRBAS,1)
      CALL RSEP3I('IRTWO',IRTWO,1)
      CALL RSEP3I('IRAUX',IRAUX,1)
      CALL RSEP3I('ITHOM',ITHOM,1)
      CALL RSEP3I('ISANG',ISANG,1)
      CALL RSEP3I('ISHP ',ISHP ,0)
      CALL RSEP3I('ISUC ',ISUC ,0)
      CALL RSEP3I('IHIST',IHIST,0)
      CALL RSEP3I('ICTWO',ICTWO,0)
      LRBAS=.FALSE.
      LRTWO=.FALSE.
      LRAUX=.FALSE.
      LTHOM=.FALSE.
      LSANG=.FALSE.
      IF (IRBAS.EQ.1) LRBAS=.TRUE.
      IF (IRTWO.EQ.1) LRTWO=.TRUE.
      IF (IRAUX.EQ.1) LRAUX=.TRUE.
      IF (ITHOM.EQ.1) LTHOM=.TRUE.
      IF (ISANG.EQ.1) LSANG=.TRUE.
C
C     Reading limits of the plot:
      CALL RSEP3R('PLIM1',PLIMIT(1),0.)
      CALL RSEP3R('PLIM2',PLIMIT(2),1.)
      CALL RSEP3R('PLIM3',PLIMIT(3),0.)
      CALL RSEP3R('PLIM4',PLIMIT(4),1.)
C
C     Reading heights of symbols and names of files:
      CALL RSEP3R('HRBAS',HRBAS,0.2)
      CALL RSEP3R('HRTWO',HRTWO,0.2)
      CALL RSEP3R('HRAUX',HRAUX,0.2)
      CALL RSEP3R('HSIZE',HOR,15.)
      CALL RSEP3R('VSIZE',VER,15.)
      CALL RSEP3R('HTEXT',HTEXT,0.5)
      CALL RSEP3T('SYMBOLS',FILSYM,' ')
      CALL RSEP3T('COLORS',FILCOL,' ')
      CALL RSEP3T('RPPLOT',FILEPS,' ')
C
C     Reading filenames of the files with computed triangles and rays
C     and index of the wave:
      CALL RSEP3T('CRTOUT',FILCRT,' ')
      FILINI='s01i.out'
      FILTRI='t01.out'
      IF (FILCRT.NE.' ') THEN
        OPEN(LU1,FILE=FILCRT,FORM='FORMATTED',STATUS='OLD')
        READ(LU1,*) CH,CH1,FILINI,FILTRI
        CLOSE(LU1)
      ENDIF
      CALL RSEP3I('ISRCS',ISRCS,0)
      CALL RSEP3I('IWAVES',IWAVES,0)
C
C     Reading quantities and filename used for plotting receivers:
      CALL RSEP3T('REC',FILREC,' ')
      CALL RSEP3T('RPAR',FILRPA,' ')
      CALL RSEP3I('ISREC',ISREC,3)
      CALL RSEP3I('ICREC',ICREC,1)
      CALL RSEP3R('HREC',HREC,0.3)
C
C
      IF (LTHOM) THEN
C       Reading file with computed triangles,
C       sorting the rays in triangles:
        NT=0
        NRAMP=0
        IRAYMI=1
        IRAYMA=0
        OPEN(LU3,FILE=FILTRI,FORM='FORMATTED',STATUS='OLD')
  10    CONTINUE
          READ(LU3,*,END=20) IRAY1,IRAY2,IRAY3
          IF (IRAY1.EQ.0) THEN
C           New elementary wave:
  12        CONTINUE
            IF (((ISRCS.NE.0).AND.(IRAY2.NE.ISRCS)).OR.
     *          ((IWAVES.NE.0).AND.(IRAY3.NE.IWAVES))) THEN
C             Skipping all the rays of this elementary wave:
  14          CONTINUE
                READ(LU3,*,END=20) IRAY1,IRAY2,IRAY3
              IF (IRAY1.NE.0) GOTO 14
              GOTO 12
            ELSE
              GOTO 10
            ENDIF
          ENDIF
C
          IF (IRAY3.EQ.0) THEN
C           RPPLOT-05
            CALL WARN('RPPLOT-05: Triangles can not be plotted')
C           The third index of the ray in file 'CRT-T' is zero, which
C           indicates one-parametric shooting, homogeneous triangles
C           cannot be plotted.
            GOTO 20
          ENDIF
C
          IF (IRAY1.GT.IRAYMA) IRAYMA=IRAY1
          IF (IRAY2.GT.IRAYMA) IRAYMA=IRAY2
          IF (IRAY3.GT.IRAYMA) IRAYMA=IRAY3
          IF (IRAY1.LT.IRAY2) THEN
            I=IRAY1
            IRAY1=IRAY2
            IRAY2=I
          ENDIF
          IF (IRAY2.LT.IRAY3) THEN
            I=IRAY2
            IRAY2=IRAY3
            IRAY3=I
          ENDIF
          IF (IRAY1.LT.IRAY2) THEN
            I=IRAY1
            IRAY1=IRAY2
            IRAY2=I
          ENDIF
          NRAMP=NRAMP+1
          IF (NRAMP.GT.MRAM) CALL RPPLER
          IRAM(NRAMP)=IRAY1
          NRAMP=NRAMP+1
          IF (NRAMP.GT.MRAM) CALL RPPLER
          IRAM(NRAMP)=IRAY2
          NRAMP=NRAMP+1
          IF (NRAMP.GT.MRAM) CALL RPPLER
          IRAM(NRAMP)=IRAY3
          NT=NT+1
        GOTO 10
  20    CONTINUE
        CLOSE(LU3)
        NR=IRAYMA
C
C       Sorting the triangles:
        DO 40, I1=NRAMP-5,1,-3
          DO 30, I2=1,I1,3
            L=I2+3
            IF (IRAM(I2).GT.IRAM(L)) THEN
              J=I2+1
              K=I2+2
              M=I2+4
              N=I2+5
              I       =IRAM(I2)
              IRAM(I2)=IRAM(L)
              IRAM(L) =I
              I       =IRAM(J)
              IRAM(J) =IRAM(M)
              IRAM(M) =I
              I       =IRAM(K)
              IRAM(K) =IRAM(N)
              IRAM(N) =I
            ENDIF
  30      CONTINUE
  40    CONTINUE
C
C
C       Forming an auxiliary array with information about when can be
C       rays erased from the memory ("deleting array"):
        IF (NRAMP+NR.GT.MRAM) CALL RPPLER
        DO 50, I1=NRAMP+1,NRAMP+NR
          IRAM(I1)=I1-NRAMP
  50    CONTINUE
        NRAMP=NRAMP+NR
        ORAYE=-3*NT
        DO 60, I1=1,3*NT,3
          IRAM(IRAM(I1  )-ORAYE)=IRAM(I1)
          IRAM(IRAM(I1+1)-ORAYE)=IRAM(I1)
          IRAM(IRAM(I1+2)-ORAYE)=IRAM(I1)
  60    CONTINUE
      ELSE
        NR=0
        NT=0
        ORAYE=0
        NRAMP=0
        IRAYMI=0
      ENDIF
C
C
C     Forming an auxiliary array with information about addresses
C     of the ends of records for rays in array RAM ("addressing array"):
C     "Ray" IRAYMI-1:
      NRAMP=NRAMP+1
      IF (NRAMP.GT.MRAM) CALL RPPLER
      IRAM(NRAMP)=NRAMP+NR
C     All other rays:
      IF (NRAMP+NR.GT.MRAM) CALL RPPLER
      DO 70, I1=NRAMP+1,NRAMP+NR
        IRAM(I1)=0
  70  CONTINUE
      NRAMP=NRAMP+NR
      ORAYA=-3*NT-NR-1
C
C
C     Choosing symbols:
      DO 71, I1=1,MSYMB
        ISYMB(I1)=I1
  71  CONTINUE
C
      OPEN(LU4,FILE=FILSYM,STATUS='OLD',ERR=74)
      DO 72, I1=1,MSYMB
        READ (LU4,*,END=74) ISYMB(I1)
 72   CONTINUE
 74   CONTINUE
      CLOSE (LU4)
C
C     Choosing colours:
      DO 75, I1=1,MCOL
        ICOL(I1)=I1
  75  CONTINUE
C
      OPEN(LU5,FILE=FILCOL,STATUS='OLD',ERR=78)
      DO 76, I1=1,MCOL
        READ (LU5,*,END=78) ICOL(I1)
 76   CONTINUE
 78   CONTINUE
      CLOSE (LU5)
C
      P1DIF=PLIMIT(2)-PLIMIT(1)
      P2DIF=PLIMIT(4)-PLIMIT(3)
      IF (IHIST.EQ.1) THEN
        DO=1.+4.5*HTEXT
      ELSE
        DO=1.+1.5*HTEXT
      ENDIF
C
      IF(FILEPS.NE.' ') THEN
        CALL PLOTN(FILEPS,0)
      END IF
      CALL PLOTS(0,0,0)
      CALL RPSYMB(0,0.,0.,0.)
C
C     Contures:
      CALL NEWPEN(1)
      CALL PLOT(HOR*((PLIMIT(1)-PLIMIT(1))/P1DIF)+DO,
     *          VER*((PLIMIT(3)-PLIMIT(3))/P2DIF)+DO,3)
      CALL PLOT(HOR*((PLIMIT(2)-PLIMIT(1))/P1DIF)+DO,
     *          VER*((PLIMIT(3)-PLIMIT(3))/P2DIF)+DO,2)
      CALL PLOT(HOR*((PLIMIT(2)-PLIMIT(1))/P1DIF)+DO,
     *          VER*((PLIMIT(4)-PLIMIT(3))/P2DIF)+DO,2)
      CALL PLOT(HOR*((PLIMIT(1)-PLIMIT(1))/P1DIF)+DO,
     *          VER*((PLIMIT(4)-PLIMIT(3))/P2DIF)+DO,2)
      CALL PLOT(HOR*((PLIMIT(1)-PLIMIT(1))/P1DIF)+DO,
     *          VER*((PLIMIT(3)-PLIMIT(3))/P2DIF)+DO,2)
      CALL PLOT(HOR*((PLIMIT(1)-PLIMIT(1))/P1DIF)+DO,
     *          VER*((PLIMIT(3)-PLIMIT(3))/P2DIF)+DO,3)
C
C     Labels along the figure:
      IF (LSANG) THEN
        CALL SYMBOL(HOR*( 0.1                     )+DO,
     *              VER*( 0.                      )+DO-1.5*HTEXT,
     *              HTEXT,'FIRST SHOOTING PARAMETER',0.,24)
        CALL SYMBOL(HOR*(0.                       )+DO-0.5*HTEXT,
     *              VER*(0.1                      )+DO       ,
     *              HTEXT,'SECOND SHOOTING PARAMETER',90.,25)
      ELSE
        CALL SYMBOL(HOR*( 0.1                     )+DO,
     *              VER*( 0.                      )+DO-1.5*HTEXT,
     *              HTEXT,'FIRST SURFACE COORDINATE',0.,24)
        CALL SYMBOL(HOR*(0.                       )+DO-0.5*HTEXT,
     *              VER*(0.1                      )+DO       ,
     *              HTEXT,'SECOND SURFACE COORDINATE',90.,25)
      ENDIF
C
C     Plotting receivers:
      IF (.NOT.LSANG) THEN
        OPEN(LU6,FILE=FILREC,FORM='FORMATTED',STATUS='OLD',ERR=80)
        READ(LU6,*,END=80) CH
        CALL NEWPEN(ICREC)
        ISRFX1=1
        ISRFX2=2
        IF (FILRPA.NE.' ') THEN
          OPEN(LU7,FILE=FILRPA,FORM='FORMATTED',STATUS='OLD')
          READ(LU7,*,END=79,ERR=79) CH
          READ(LU7,*,END=79,ERR=79) I1,ISRFX1,ISRFX2
          ISRFX1=IABS(ISRFX1)
          ISRFX2=IABS(ISRFX2)
          IF (((ISRFX1.NE.1).AND.(ISRFX1.NE.2).AND.(ISRFX1.NE.3)).OR.
     *        ((ISRFX2.NE.1).AND.(ISRFX2.NE.2).AND.(ISRFX2.NE.3))) THEN
C           RPPLOT-06
            CALL ERROR('RPPLOT-06: Cannot handle this value of ISRFXi')
C           Program RPPLOT can handle only the values -1, -2 and -3 for
C           ISRFX1 and ISRFX2 - the X1 and X2 functions must coincide
C           with the model coordinates.
          ENDIF
          CLOSE(LU7)
          GOTO 81
  79      CONTINUE
C         RPPLOT-07
          CALL ERROR('RPPLOT-07: Error when reading ISRFXi')
C         Program RPPLOT was not able to read the values of ISRFX1
C         and ISRFX2 from the file RPAR.
C         See the description of parameter RPAR.
        ENDIF
  81    CONTINUE
          CH='$'
          XREC(1)=0.
          XREC(2)=0.
          XREC(3)=0.
          READ(LU6,*,END=80) CH,XREC
          IF (CH.EQ.'$') GOTO 81
          P1=XREC(ISRFX1)
          P2=XREC(ISRFX2)
          IF ((P1.GE.PLIMIT(1)).AND.(P1.LE.PLIMIT(2)).AND.
     *        (P2.GE.PLIMIT(3)).AND.(P2.LE.PLIMIT(4)))
     *         CALL RPSYMB(ISREC,
     *              HOR*((P1-PLIMIT(1))/P1DIF)+DO,
     *              VER*((P2-PLIMIT(3))/P2DIF)+DO,HREC)
        GOTO 81
      ENDIF
C
C
  80  CLOSE(LU6)
      OPEN(LU2,FILE=FILINI,FORM='UNFORMATTED',STATUS='OLD')
      IR0=0
      IF (LTHOM) THEN
C       Loop for all the triangles:
        DO 100, IT1=1,3*NT-2,3
C
C         If necessary, reading new rays:
          IF ((IRAM(IRAM(IT1)-ORAYA+1).EQ.0).AND.(.NOT.LEND)) THEN
            CALL CIREAD(LU2,IRAM(IT1),ISRCS,IWAVES,LEND)
          ENDIF
C
C         Empting the array RAM:
          IF ((MRAM-NRAMP).LT.(MRAM/10.)) CALL CIERAS
C
C         Plotting the triangle:
          CALL RPTPL(IT1)
C
C         Plotting the rays:
          DO 102, I1=IR0+1,IRAM(IT1)
            CALL RPRPL(I1)
 102      CONTINUE
          IR0=IRAM(IT1)
C
  100   CONTINUE
C       End of the loop for all the triangles.
      ENDIF
C     Plotting remaining rays:
  110 CONTINUE
      IF (.NOT.LEND) THEN
        IR0=IR0+1
        CALL CIREAD(LU2,IR0,ISRCS,IWAVES,LEND)
        CALL RPRPL(IR0)
        GOTO 110
      ENDIF
C
C     Plotting symbols for ray histories:
      IF ((IHIST.EQ.1).AND.ISHMAX.GE.2) THEN
        DP=P1DIF/(ISHMAX-1)
C       Plotting lower left corner to make correct bounding box:
        CALL PLOT(HOR*( 0.                      )+DO          ,
     *            VER*( 0.                      )+DO-4.5*HTEXT,3)
        CALL PLOT(HOR*( 0.                      )+DO          ,
     *            VER*( 0.                      )+DO-4.5*HTEXT,2)
        TEXT=' '
C       Loop over all the ray histories:
        DO 120, I1=1,ISHMAX
C         Horizontal position of the symbol:
          P1=PLIMIT(1)+(I1-1)*DP
C         Converting I1 into text:
          IF (I1.LT.10) THEN
            TEXT(1:1)=CHAR(ICHAR('0')+I1)
            X1ADD=-0.5*HTEXT
            NCHAR=1
          ELSEIF (I1.LT.100) THEN
            TEXT(1:1)=CHAR(ICHAR('0')+I1/10)
            TEXT(2:2)=CHAR(ICHAR('0')+(I1-(I1/10)*10))
            X1ADD=-1.0*HTEXT
            NCHAR=2
          ELSE
C           RPPLOT-08
            CALL WARN('RPPLOT-08: Ray history higher than 99')
C           Recent version is coded for maximum of 99 ray histories.
          ENDIF
          CALL NEWPEN(ICOL(MIN0(MCOL,I1)))
C         Plotting the value of the ray history:
          CALL SYMBOL(HOR*((P1-PLIMIT(1))/P1DIF)+DO+X1ADD,
     *                VER*( 0.                 )+DO-3.0*HTEXT,
     *                HTEXT,TEXT,0.,NCHAR)
C         Plotting the symbol:
          CALL RPSYMB(ISYMB(MIN0(MSYMB,I1)),
     *                HOR*((P1-PLIMIT(1))/P1DIF)+DO,
     *                VER*( 0.                 )+DO-4.0*HTEXT,HTEXT)
  120   CONTINUE
      ENDIF
C
      CALL PLOT(0.,0.,999)
      WRITE(*,'(A)') '+RPPLOT: Done.                 '
      STOP
      END
C
C
C=======================================================================
C
      SUBROUTINE CIREAD(LU2,IR1,ISRCS,IWAVES,LEND)
C
C----------------------------------------------------------------------
C Subroutine to read the unformatted output of program CRT and
C to write it into array (I)RAM.
C Reading the output files is completed by a simple invocation of
C subroutine AP00 of file 'ap.for'.
C
      INTEGER LU2,IR1,ISRCS,IWAVES
      LOGICAL LEND
C Input:
C     LU2...  Number of logical unit corresponding to the file with
C             the quantities at the initial points of rays.
C     IR1...  Index of the first ray of the actually processed
C             triangle.
C     ISRCS... Index of the source of the wave to be plotted.
C     IWAVES... Index of the elementary wave to be plotted.
C Output:
C     LEND... .TRUE. when the end of file with rays is reached,
C             othervise .FALSE..
C
C Coded by Petr Bulant
C
C ...........................
C Common block /POINTC/ to store the results of complete ray tracing:
      INCLUDE 'pointc.inc'
C None of the storage locations of the common block are altered.
C ...........................
C Common block /RAM/ to store the information about triangles and rays:
      INCLUDE 'rpplot.inc'
C
C-----------------------------------------------------------------------
C     Loop for the points of rays:
   10 CONTINUE
        IF ((NRAMP+2*NQ).GT.MRAM) THEN
C         Freeing the memory:
          CALL CIERAS
          IF ((NRAMP+2*NQ).GT.MRAM) CALL RPPLER
        ENDIF
C       Reading the results of the complete ray tracing:
        CALL AP00(0,0,LU2)
        IF (IWAVE.LT.1) THEN
C         End of rays:
          CLOSE(LU2)
          LEND=.TRUE.
          RETURN
        ENDIF
        IF (((ISRCS.NE.0).AND.(ISRCS.NE.ISRC)).OR.
     *      ((IWAVES.NE.0).AND.(IWAVES.NE.IWAVE)))
C         Skipping this elementary wave:
     *    GOTO 10
        IF (IRAY.LT.IRAYMI) GOTO 10
        IF (IRAM(IRAY-ORAYE).NE.0) THEN
C         Writing the results of the complete ray tracing - recording
C         new initial point on a ray:
          IF (LSANG) THEN
C           Normalized ray parameters:
            RAM(NRAMP+1)=YI(26)
            RAM(NRAMP+2)=YI(27)
          ELSE
C           Reference surface coordinates:
            RAM(NRAMP+1)=YI(28)
            RAM(NRAMP+2)=YI(29)
          ENDIF
C         Index of the receiver:
          IRAM(NRAMP+3)=IREC
C         History:
          IF (ISHEET.EQ.0) ISHEET=1
          IRAM(NRAMP+4)=ISHEET
          NRAMP=NRAMP+NQ
        ENDIF
        IRAM(IRAY-ORAYA)=NRAMP
        IF (IRAY.GT.IR1) RETURN
      GOTO 10
C
      END
C
C
C=======================================================================
C
      SUBROUTINE CIERAS
C
C----------------------------------------------------------------------
C Subroutine for empting the array (I)RAM. All the parameters
C of all the rays, which will no more be used, are erased.
C
C No input.
C No output.
C
C Subroutines and external functions required:
C
C Coded by Petr Bulant
C
C ...........................
C Common block /POINTC/ to store the results of complete ray tracing:
      INCLUDE 'pointc.inc'
C     IRAY... Index of the ray being actually read in by CIREAD.
C             This procedure supposes, that any ray with higher
C             index than IRAY was not read in.
C None of the storage locations of the common block are altered.
C ...........................
C Common block /RAM/ to store the information about triangles and rays:
      INCLUDE 'rpplot.inc'
C.......................................................................
C     Auxiliary storage locations:
      INTEGER I1,I2,J1
      INTEGER IADDRP
C     I1...   Controls main loop over rays.
C     I2...   Controls the loop over parameters of ray I1.
C     J1...   Address of the last used record of array RAM.
C
C-----------------------------------------------------------------------
      J1=IRAM(IRAYMI-1-ORAYA)
      IADDRP=J1
C     Loop for the rays:
      DO 20, I1=IRAYMI,IRAY
        IF (IRAM(I1-ORAYE).GE.(IRAY-1)) THEN
C         This ray is not to be erased:
          DO 10, I2=IADDRP+1,IRAM(I1-ORAYA)
            J1=J1+1
            RAM(J1)=RAM(I2)
   10     CONTINUE
          IADDRP=IRAM(I1-ORAYA)
          IRAM(I1-ORAYA)=J1
        ELSE
C         This ray is to be erased:
          IRAM(I1-ORAYE)=0
          IADDRP=IRAM(I1-ORAYA)
          IRAM(I1-ORAYA)=J1
        ENDIF
   20 CONTINUE
      NRAMP=J1
      RETURN
      END
C=======================================================================
C
      SUBROUTINE RPTPL(IT1)
C
C----------------------------------------------------------------------
C Subroutine for plotting the triangle formed by the rays
C IRAM(IT1), IRAM(IT1+1), IRAM(IT1+2).
C
      INTEGER IT1
C Input:
C     IT1...  The address of the index of the first ray of the triangle
C             to be plotted.
C No output.
C
C Coded by Petr Bulant
C
C ...........................
C Common block /RAM/ to store the information about triangles and rays:
      INCLUDE 'rpplot.inc'
C.......................................................................
C Auxiliary storage locations:
      INTEGER I1,ILIN,ISH
      REAL P1A,P2A,P1B,P2B,P1C,P2C,P1,P2,P1O,P2O
      REAL P1MI,P1MA,P2MI,P2MA,DP1,DP2
      LOGICAL LPL
C-----------------------------------------------------------------------
      IF ((IRAM(IRAM(IT1  )-ORAYA).EQ.0).OR.
     *    (IRAM(IRAM(IT1+1)-ORAYA).EQ.0).OR.
     *    (IRAM(IRAM(IT1+2)-ORAYA).EQ.0)) THEN
C       RPPLOT-01
        CALL ERROR('RPPLOT-01: Parameters of a ray not found in memory')
C       This error may be caused by
C       K2P 
C       not equal to zero, then only two-point rays are stored in
C       output files of CRT.
C       It may occur also in case, when a file with points along rays is
C       specified instead of input file with initial points of rays.
C       This also happens when there is less than ISRCS sources or less
C       than IWAVES elementary waves in the file with initial points.
      ENDIF
C
      P1A=RAM(IRAM(IRAM(IT1  )-ORAYA-1)+1)
      P2A=RAM(IRAM(IRAM(IT1  )-ORAYA-1)+2)
      P1B=RAM(IRAM(IRAM(IT1+1)-ORAYA-1)+1)
      P2B=RAM(IRAM(IRAM(IT1+1)-ORAYA-1)+2)
      P1C=RAM(IRAM(IRAM(IT1+2)-ORAYA-1)+1)
      P2C=RAM(IRAM(IRAM(IT1+2)-ORAYA-1)+2)
      ISH=IRAM(IRAM(IRAM(IT1+2)-ORAYA-1)+4)
C
      IF ((ISHP.NE.0).AND.(ISHP.NE.ISH)) RETURN
C
      IF ((.NOT.LSANG).AND.(ISH.LE.0)) RETURN
C
      P1MI=AMIN1(P1A,P1B,P1C)
      P1MA=AMAX1(P1A,P1B,P1C)
      P2MI=AMIN1(P2A,P2B,P2C)
      P2MA=AMAX1(P2A,P2B,P2C)
C
      ILIN=50*INT(AMAX1(((P1MA-P1MI)/P1DIF),((P2MA-P2MI)/P2DIF),1.))
      IF ((P1MI.GE.PLIMIT(1)).AND.(P1MA.LE.PLIMIT(2)).AND.
     *    (P2MI.GE.PLIMIT(3)).AND.(P2MA.LE.PLIMIT(4))) ILIN=1
C
      P1=P1A
      P2=P2A
      IF ((P1.GE.PLIMIT(1)).AND.(P1.LE.PLIMIT(2)).AND.
     *    (P2.GE.PLIMIT(3)).AND.(P2.LE.PLIMIT(4))) THEN
        IF ((ISUC.EQ.1).AND.(ISH.LT.0)) THEN
          CALL NEWPEN(1)
        ELSE
          CALL NEWPEN(ICOL(MIN0(MCOL,IABS(ISH))))
        ENDIF
        CALL PLOT(HOR*((P1-PLIMIT(1))/P1DIF)     +DO,
     *            VER*((P2-PLIMIT(3))/P2DIF)     +DO,3)
        P1O=P1
        P2O=P2
        LPL=.TRUE.
      ELSE
        LPL=.FALSE.
      ENDIF
      DP1=(P1B-P1A)/ILIN
      DP2=(P2B-P2A)/ILIN
      DO 30, I1=1,ILIN
        P1=P1+DP1
        P2=P2+DP2
        IF ((P1.GE.PLIMIT(1)).AND.(P1.LE.PLIMIT(2)).AND.
     *      (P2.GE.PLIMIT(3)).AND.(P2.LE.PLIMIT(4))) THEN
          IF (LPL) THEN
            CALL PLOT(HOR*((P1-PLIMIT(1))/P1DIF)     +DO,
     *                VER*((P2-PLIMIT(3))/P2DIF)     +DO,2)
            P1O=P1
            P2O=P2
          ELSE
            IF ((ISUC.EQ.1).AND.(ISH.LT.0)) THEN
              CALL NEWPEN(1)
            ELSE
              CALL NEWPEN(ICOL(MIN0(MCOL,IABS(ISH))))
            ENDIF
            CALL PLOT(HOR*((P1-PLIMIT(1))/P1DIF)     +DO,
     *                VER*((P2-PLIMIT(3))/P2DIF)     +DO,3)
            P1O=P1
            P2O=P2
            LPL=.TRUE.
          ENDIF
        ELSE
          IF (LPL) THEN
            CALL PLOT(HOR*((P1O-PLIMIT(1))/P1DIF)     +DO,
     *                VER*((P2O-PLIMIT(3))/P2DIF)     +DO,3)
            LPL=.FALSE.
          ENDIF
        ENDIF
  30  CONTINUE
      DP1=(P1C-P1B)/ILIN
      DP2=(P2C-P2B)/ILIN
      DO 32, I1=1,ILIN
        P1=P1+DP1
        P2=P2+DP2
        IF ((P1.GE.PLIMIT(1)).AND.(P1.LE.PLIMIT(2)).AND.
     *      (P2.GE.PLIMIT(3)).AND.(P2.LE.PLIMIT(4))) THEN
          IF (LPL) THEN
            CALL PLOT(HOR*((P1-PLIMIT(1))/P1DIF)     +DO,
     *                VER*((P2-PLIMIT(3))/P2DIF)     +DO,2)
            P1O=P1
            P2O=P2
          ELSE
            IF ((ISUC.EQ.1).AND.(ISH.LT.0)) THEN
              CALL NEWPEN(1)
            ELSE
              CALL NEWPEN(ICOL(MIN0(MCOL,IABS(ISH))))
            ENDIF
            CALL PLOT(HOR*((P1-PLIMIT(1))/P1DIF)     +DO,
     *                VER*((P2-PLIMIT(3))/P2DIF)     +DO,3)
            P1O=P1
            P2O=P2
            LPL=.TRUE.
          ENDIF
        ELSE
          IF (LPL) THEN
            CALL PLOT(HOR*((P1O-PLIMIT(1))/P1DIF)     +DO,
     *                VER*((P2O-PLIMIT(3))/P2DIF)     +DO,3)
            LPL=.FALSE.
          ENDIF
        ENDIF
  32  CONTINUE
      DP1=(P1A-P1C)/ILIN
      DP2=(P2A-P2C)/ILIN
      DO 34, I1=1,ILIN
        P1=P1+DP1
        P2=P2+DP2
        IF ((P1.GE.PLIMIT(1)).AND.(P1.LE.PLIMIT(2)).AND.
     *      (P2.GE.PLIMIT(3)).AND.(P2.LE.PLIMIT(4))) THEN
          IF (LPL) THEN
            CALL PLOT(HOR*((P1-PLIMIT(1))/P1DIF)     +DO,
     *                VER*((P2-PLIMIT(3))/P2DIF)     +DO,2)
            P1O=P1
            P2O=P2
          ELSE
            IF ((ISUC.EQ.1).AND.(ISH.LT.0)) THEN
              CALL NEWPEN(1)
            ELSE
              CALL NEWPEN(ICOL(MIN0(MCOL,IABS(ISH))))
            ENDIF
            CALL PLOT(HOR*((P1-PLIMIT(1))/P1DIF)     +DO,
     *                VER*((P2-PLIMIT(3))/P2DIF)     +DO,3)
            P1O=P1
            P2O=P2
            LPL=.TRUE.
          ENDIF
        ELSE
          IF (LPL) THEN
            CALL PLOT(HOR*((P1O-PLIMIT(1))/P1DIF)     +DO,
     *                VER*((P2O-PLIMIT(3))/P2DIF)     +DO,3)
            LPL=.FALSE.
          ENDIF
        ENDIF
  34  CONTINUE
      RETURN
      END
C=======================================================================
C
      SUBROUTINE RPRPL(IR1)
C
C----------------------------------------------------------------------
C Subroutine for plotting the ray IR1.
C
      INTEGER IR1
C Input:
C     IR1...  Index of the ray to be plotted.
C No output.
C
C Coded by Petr Bulant
C
C ...........................
C Common block /RAM/ to store the information about triangles and rays:
      INCLUDE 'rpplot.inc'
C.......................................................................
C Auxiliary storage locations:
      INTEGER ISH,IREC
      REAL P1,P2
      CHARACTER*80 TXTERR
C-----------------------------------------------------------------------
      IF (IRAM(IR1-ORAYA).EQ.0) THEN
C       RPPLOT-02
        WRITE(TXTERR,'(A,I6,A)')
     *  'RPPLOT-02: Parameters of a ray ',IR1,' not found in memory'
        CALL ERROR(TXTERR)
C       This error may be caused by
C       K2P 
C       not equal to zero, then only two-point rays are stored in
C       output files of CRT.
C       It may occur also in case, when a file with points along rays is
C       specified instead of input file with initial points of rays.
C       This also happens when there is less than ISRCS sources or less
C       than IWAVES elementary waves in the file with initial points.
      ENDIF
C
      P1=RAM(IRAM(IR1-ORAYA-1)+1)
      P2=RAM(IRAM(IR1-ORAYA-1)+2)
      IREC=IRAM(IRAM(IR1-ORAYA-1)+3)
      ISH =IRAM(IRAM(IR1-ORAYA-1)+4)
      ISHMAX=MAX0(ISHMAX,IABS(ISH))
C
      IF ((ISHP.NE.0).AND.(ISHP.NE.ISH)) RETURN
C
      IF ((.NOT.LSANG).AND.(ISH.LE.0)) RETURN
C
      IF ((P1.GE.PLIMIT(1)).AND.(P1.LE.PLIMIT(2)).AND.
     *    (P2.GE.PLIMIT(3)).AND.(P2.LE.PLIMIT(4))) THEN
        IF ((ISUC.EQ.1).AND.(ISH.LT.0)) THEN
          CALL NEWPEN(1)
        ELSE
          CALL NEWPEN(ICOL(MIN0(MCOL,IABS(ISH))))
        ENDIF
        IF (LRBAS.AND.(IREC.EQ.0)) THEN
C         Basic ray:
          CALL RPSYMB(ISYMB(MIN0(MSYMB,IABS(ISH))),
     *              HOR*((P1-PLIMIT(1))/P1DIF)+DO,
     *              VER*((P2-PLIMIT(3))/P2DIF)+DO,HRBAS)
          RETURN
        ENDIF
        IF (LRTWO.AND.(IREC.GT.0)) THEN
C         Two-point ray:
          IF (ICTWO.NE.0) THEN
            CALL NEWPEN(ICOL(MIN0(MCOL,ICTWO)))
          ENDIF
          CALL RPSYMB(ISYMB(MIN0(MSYMB,IABS(ISH))),
     *              HOR*((P1-PLIMIT(1))/P1DIF)+DO,
     *              VER*((P2-PLIMIT(3))/P2DIF)+DO,HRTWO)
          RETURN
        ENDIF
        IF (LRAUX.AND.(IREC.EQ.-1)) THEN
C         Auxiliary ray:
          CALL RPSYMB(ISYMB(MIN0(MSYMB,IABS(ISH))),
     *              HOR*((P1-PLIMIT(1))/P1DIF)+DO,
     *              VER*((P2-PLIMIT(3))/P2DIF)+DO,HRAUX)
          RETURN
        ENDIF
      ENDIF
      END
C=======================================================================
C
      SUBROUTINE RPPLER
C
C----------------------------------------------------------------------
C     RPPLOT-03
      CALL ERROR('RPPLOT-03: Array (I)RAM too small')
C     This error may be caused by too small dimension of array
C     RAM. Try to enlarge the parameter MRAM in common block
C     RAM.
      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 'calcops.for'
C     calcops.for
      INCLUDE 'ap.for'
C     ap.for
      INCLUDE 'rpsymb.for'
C     rpsymb.for
C
C=======================================================================
C