      PROGRAM SWITCHSR

C     Switches the sources and receivers in a file

      INTEGER INFIL, OUTFIL 
      PARAMETER ( INFIL = 26,  OUTFIL = 25,
     &   MAXNSD = 4094, MAXNRD = 4094, MAXNR = 4094 )

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

C     *** Read replica file header ***

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

C     *** Write new header with SD, RD interchanged ***

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

C     *** Loop to read successive slices ***

      DO ISD = 1, NSD

         DO IRD = 1, NRD
C           --- read in pressure for a particular source/rcvr
            IRECIN  = 6 + NRD * ( ISD - 1 ) + IRD
            READ (  INFIL, REC = IRECIN ) ( P( IR ), IR = 1, NR )
            WRITE( *, * ) 'SD = ', SD( ISD ), 'RD = ', RD( IRD )

C           --- Write it out in a new record position
            IRECOUT = 6 + NSD * ( IRD - 1 ) + ISD
            WRITE( OUTFIL, REC = IRECOUT ) ( P( IR ), IR = 1, NR )
         END DO

      END DO

      CLOSE( OUTFIL )

      STOP
      END
