      PROGRAM PLOTSSP

C     PLOT THE SSP FOR A GIVEN ENVIRONMENT

      INTEGER ENVFIL, PRTFIL, PRTFIL2
      PARAMETER ( ENVFIL = 5, PRTFIL = 6, PRTFIL2 = 99,
     &   MAXMED = 51 )

      INTEGER NG( MAXMED )
      REAL    ZV( 5000 ), CV( 5000 )
      REAL*8  DEPTH( MAXMED ), RHO( 2000 ), FREQ, RHOT, RHOB,
     &   SIGMA( MAXMED ), BUMDEN, ETA, XI
      COMPLEX*16 CP( 2000 ), CS( 2000 ), CPT, CST, CPB, CSB
      CHARACTER TOPOPT*8, BOTOPT*8, PLTITL*80

C     *** Read in environmental info ***

      PLTITL = 'PLOTSSP- '

      CALL READIN( PLTITL, FREQ, MAXMED, NMEDIA,
     &   TOPOPT, CPT, CST, RHOT, BUMDEN, ETA, XI, NG, SIGMA, DEPTH, 
     &   BOTOPT, CPB, CSB, RHOB,
     &   ENVFIL, PRTFIL2 )

      CLOSE( ENVFIL )

C      DEPTH( NMEDIA + 2 ) = DEPTH( NMEDIA + 1 ) + 100.0

C     Generate vector of Z, CP, CS, RHO points

      CMIN = +1.0E9
      CMAX = -1.0E9
      IZ = 0

      DO MED = 1, NMEDIA
         N  = 200
         N1 = N + 1
         CALL PROFIL( DEPTH( MED ), CP, CS, RHO, MED, N1, FREQ,
     &      TOPOPT(1:1), TOPOPT(3:3), 'TAB', ENVFIL, PRTFIL )

         H = ( DEPTH( MED + 1 ) - DEPTH( MED ) ) / N

         DO J = 1, N1
            IZ = IZ + 1
            ZV( IZ ) = DEPTH( MED ) + ( J - 1 ) * H
            CV( IZ ) = REAL( CP( J ) )
            CMIN = MIN( CMIN, CV( IZ ) )
            CMAX = MAX( CMAX, CV( IZ ) )
         END DO

      END DO   ! next medium

      NZ = IZ
      ZMIN = DEPTH( 1 )
      ZMAX = DEPTH( NMEDIA + 1 )

C     Begin plotting

      LAST = INDEX( PLTITL, '    ' ) - 1
      WRITE( PRTFIL, 1000 ) PLTITL(1:LAST)
 1000 FORMAT( '@ title "', A, '"' )

      WRITE( PRTFIL, 2000 ) INT( ZMIN ), INT( ZMAX ),
     &    10 * INT( CMIN / 10.0 ), 10 * INT( CMAX / 10.0 + 1 )
 2000 FORMAT( '@ world xmin ', I5, /,
     &        '@ world xmax ', I5, /,
     &        '@ world ymin ', I5, /,
     &        '@ world ymax ', I5 )

      DO IZ = 1, NZ
         WRITE( PRTFIL, * ) ZV( IZ ), CV( IZ )
      END DO

      STOP
      END
