      PROGRAM TOASC

C     Converts shade file to ASCII format

      INTEGER SHDFIL, ASCFIL
      PARAMETER ( SHDFIL = 25, ASCFIL = 26, 
     &   MAXNSD = 500, MAXNRD = 5000, MAXNR = 4094 )

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

C     *** Read header ***

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

C     *** Write header ***

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

      WRITE( ASCFIL, 500 ) TITLE
  500 FORMAT( A80 )

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

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

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( SHDFIL, REC = IREC ) ( P( J ), J = 1, NR)
            WRITE( *, * ) ISD, IRD, SD( ISD ), RD( IRD )
            WRITE( ASCFIL, 600 ) ( P( J ), J = 1, NR)
  600       FORMAT( 6E13.5 )
         END DO

      END DO

      CLOSE( ASCFIL )

      STOP
      END
