      PROGRAM RADIUS

C     Converts complex pressure to a detection radius

      INTEGER INPFIL, PRTFIL, SHDFIL, FOMFIL
      PARAMETER ( INPFIL = 5, PRTFIL = 6, SHDFIL = 26, FOMFIL = 25,
     &   MAXNSD = 4094, MAXNRD = 4094, MAXNR = 4094, MXNFOM = 501 )

      REAL R( MAXNR ), SD( MAXNSD ), RD( MAXNRD ), TL( MAXNR ),
     &     FOM( MXNFOM ), RAD( MXNFOM )
      COMPLEX P( MAXNR )
      CHARACTER TITLE*80, PLTTYP*10

      WRITE( PRTFIL, * ) 'SIGMA?'
      READ(  INPFIL, * ) SIGMA
      SIGMA  = ABS( SIGMA )

      WRITE( PRTFIL, * ) 'NFOM, FOMMIN, FOMMAX'
      READ(  INPFIL, * )  NFOM, FOMMIN, FOMMAX

      DELFOM = ( FOMMAX - FOMMIN ) / ( NFOM - 1 )
      DO 1000 IFOM = 1, NFOM
         FOM( IFOM ) = FOMMIN + ( IFOM - 1 ) * DELFOM
 1000 CONTINUE

C     *** Read/write header ***

      CALL RDHEAD( SHDFIL, SHDFILIN, MAXNR, MAXNSD, MAXNRD,
     &   FREQ, R, NR, DELTAR, PLTTYP, XS, YS, THETA,
     &   SD, NSD, RD, NRD, TITLE )

      CALL HEADER( TITLE, SD, NSD, RD, NRD,
     &   FOM, NFOM, FREQ, ATTEN, PLTTYP, XS, YS, THETA )

C     Read in pressure field and convert to transmission loss

      DO 9000 ISR = 1, NSD * NRD
         IREC =  ISR + 6
         READ ( SHDFIL, REC = IREC )
     &      SRCD, RCVRD, ( P( IR ), IR = 1, NR )

C        --- convert pressure to TL

         DO IR = 1, NR
            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        --- For each FOM, compute detection radius

         DO IFOM = 1, NFOM
            RAD( IFOM ) = 0.0
            DO IR = 2, NR
               RAD( IFOM ) = RAD( IFOM ) + ( R( IR ) - R( IR - 1 ) )
     &             * TRANSIT( FOM( IFOM ) - TL( IR ), SIGMA )

C              --- Glimpse range:
C               IF ( TRANSIT( FOM( IFOM ) - TL( IR ), SIGMA ) .GE. 0.5 )
C     &            RAD( IFOM ) = R( IR )
            END DO
         END DO

C        --- Write out the radius
         WRITE( FOMFIL, REC = IREC ) SRCD, RCVRD,
     &      ( RAD( IFOM ) / 1000.0, 0.0, IFOM = 1, NFOM )
 9000 CONTINUE

      STOP
      END
