      PROGRAM PLTTLD

C     Plot transmission loss versus depth

C     This module should be modified to use less storage
C     Specifically, there is no need for PHIR(MAXM, MAXNZ)

      INTEGER PLPFIL, PRTFIL, STAFIL
      PARAMETER ( PLPFIL = 1, PRTFIL = 6, STAFIL = 10,
     &   MAXNZ = 251, MAXM = 1500 )

      REAL RD( MAXNZ ), RR( MAXNZ), TL( MAXNZ )
      COMPLEX CK( MAXM ), PHIS( MAXM ), PHIR( MAXM, MAXNZ ), P( MAXNZ )
      CHARACTER OPT*50, TITLE*80, COMP*1

C     Read in user parameters

      OPEN( FILE = 'PLPFIL', UNIT = PLPFIL, STATUS = 'OLD' )
      READ( PLPFIL, * ) OPT
      READ( PLPFIL, * ) M
      READ( PLPFIL, * ) SD
      READ( PLPFIL, * ) R

C     Read receiver depths

      RD( 3 ) = -999.9
      READ( PLPFIL, * ) NRD, ( RD( IR ), IR = 1, NRD )

      IF ( NRD .GT. MAXNZ ) THEN
         WRITE( PRTFIL, * ) 'NRD, MAXNZ = ', NRD, MAXNZ
         CALL ERROUT( PRTFIL, 'F', 'PLOTTLR', 'Too many receivers'  )
      ENDIF

      CALL SUBTAB( RD, NRD )

C     Set receiver displacements to zero

      RR( 1 ) = 0.0
      RR( 2 ) = 0.0
      RR( 3 ) = -999.9
      CALL SUBTAB( RR, NRD )

C     *** Read in modes ***

      COMP  = OPT(3:3)
      IPROF = 1
      NSD   = 1

      CALL GETMOD( IPROF, ' ', MAXM, SD, NSD,  'N',
     &   CK, PHIS, M1, FREQ, TITLE )

      CALL GETMOD( IPROF, ' ', MAXM, RD, NRD, COMP,
     &   CK, PHIR, M1, FREQ, TITLE )

      M = MIN( M, M1 )

C     *** Compute the field ***

      RM = 1000.0 * R
      NR = 1
      CALL EVAL( PHIS, PHIR, NRD, RM, NR, RR, CK, M, OPT, P )

C     Write pressure to STAFIL

      WRITE( STAFIL, * ) ( P( IR ), IR = 1, NRD )

      DO IR = 1, NRD
         PSQ = P( IR ) * CONJG( P( IR ) )
         TL( IR ) = 200.0
         IF ( PSQ .GT. 1.0E-20 ) TL( IR ) = -10.0 * ALOG10( PSQ )
      END DO

C     *** Write data ***

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

      WRITE( PRTFIL, 2000 ) NINT( FREQ ), NINT( SD ), NINT( R )
 2000 FORMAT(  '@ subtitle "',
     &         'F  =', I4, ' Hz, ',
     &         'Sd =', I4, ' m,  ',
     &         'Rd =', I4, ' m"' )
 
      DO IRD = 1, NRD
         WRITE( PRTFIL, * ) RD( IRD ), TL( IRD ) 
      END DO

      STOP
      END
