      PROGRAM TOBIN

C     Converts ASCII format to binary format

      INTEGER SHDFIL, ASCFIL
      PARAMETER ( SHDFIL = 25, ASCFIL = 5, 
     &   MAXNRD = 500, MAXNR = 4094 )

      REAL R( MAXNR ), RD( MAXNRD ), SD( MAXNRD )
      COMPLEX P( MAXNR )
      CHARACTER TITLE*80, PLTTYP*10

C     *** Read the header data ***

C     OPEN ( FILE = 'ASCFIL', UNIT = ASCFIL, STATUS = 'OLD',
C    &       FORM = 'FORMATTED' )

      READ( ASCFIL, 3000 ) TITLE
 3000 FORMAT( A80 )

      READ( ASCFIL, * ) PLTTYP, XS, YS, THETA
      READ( ASCFIL, * ) FREQ, NSD, NRD, NR

      READ( ASCFIL, 1000 ) ( SD( I ), I = 1, NSD )
      READ( ASCFIL, 1000 ) ( RD( I ), I = 1, NRD )
      READ( ASCFIL, 1000 ) ( R(  I ), I = 1, NR )
 1000 FORMAT( 6E13.5 )

C     *** Write the header to the SHDFIL ***

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

C     *** Main loop: convert pressure surfaces for each S/R combo ***

      DO ISD = 1, NSD
         IREC = 6 + ( ISD - 1 ) * NRD

         DO IRD = 1, NRD
            IREC = IREC + 1
            READ( ASCFIL, 7000 ) ( P( J ), J = 1, NR )
 7000       FORMAT( 6E13.5 )
            WRITE( *, * ) ISD, IRD, SD( ISD ), RD( IRD )
            WRITE( SHDFIL, REC = IREC ) ( P( J ), J = 1, NR )
         END DO

      END DO

      STOP
      END
