C
C Program SWAP to swap the bytes, i.e., to convert binary gridded data
C (data cubes) between little-endian (PC's) and big-endian (VAX, Alpha,
C RISC workstations) hardware
C
C Version: 5.50
C Date: 2000, November 25
C
C Coded by: Ludek Klimes
C     Department of Geophysics, Charles University Prague,
C     Ke Karlovu 3, 121 16 Praha 2, Czech Republic,
C     E-mail: klimes@seis.karlov.mff.cuni.cz
C
C.......................................................................
C
C Attention:  Functionality of program SWAP is strongly affected by
C the Fortran compiler and by the options of the compiler.
C Program SWAP can work only if the compiler supports unformatted
C direct-access files "without headers".
C Program SWAP uses INTEGER*1 statement, which violates Fotran 77
C standard.
C
C.......................................................................
C                                                    
C Description of data files:
C
C Input data read from the standard input device (*):
C     The data are read by the list directed input (free format) and
C     consist of a single string 'SEP':
C     'SEP'...String in apostrophes containing the name of the input
C             SEP parameter or history file with the input data.
C     No default, 'SEP' must be specified and cannot be blank.
C
C                                                     
C Input data file 'SEP':
C     File 'SEP' has the form of the SEP
C     parameter file.  The parameters, which do not differ from their
C     defaults, need not be specified in file 'SEP'.
C Data specifying dimensions of the input grid:
C     N1=positive integer... Number of gridpoints along the X1 axis.
C             Default: N1=1
C     N2=positive integer... Number of gridpoints along the X2 axis.
C             Default: N2=1
C     N3=positive integer... Number of gridpoints along the X3 axis.
C             Default: N3=1
C Names of the grid file being swapped:
C     IN='string'... String with the name of the input unformatted file
C             containing the gridded values.  The file should contain
C             just the 4 byte IEEE reals.  The length of the file is
C             thus exactly 4*N1*N2*N3 bytes.
C             No default, IN must be specified and cannot be blank.
C Data specifying input/output format:
C     ESIZE=integer... Number of bytes per a real in the input binary
C             file.  Must be ESIZE=4.
C             Default: ESIZE=4
C
C=======================================================================
C
      CHARACTER*80 FILE1
      INTEGER LU1
      PARAMETER (LU1=1)
C
      INTEGER N1,N2,N3,I
      INTEGER*1 I1(4),I2(4)
      REAL AUX1,AUX2
      EQUIVALENCE (I1,AUX1),(I2,AUX2)
C
C.......................................................................
C
C     Reading main input data:
      WRITE(*,'(A)') '+SWAP: Enter input filename: '
      FILE1=' '
      READ(*,*) FILE1
      IF (FILE1.EQ.' ') THEN
C       SWAP-01
        CALL ERROR('SWAP-01: SEP file not given')
C       Input file in the form of the SEP (Stanford Exploration Project)
C       parameter or history file must be specified.
C       There is no default filename.
      END IF
      CALL RSEP1(LU1,FILE1)
C
C     Input/output file with gridded data:
      CALL RSEP3T('IN',FILE1,' ')
      IF (FILE1.EQ.' ') THEN
C       SWAP-02
        CALL ERROR('SWAP-02: Input file not specified')
      END IF
      CALL RSEP3I('ESIZE',I,4)
      IF (I.NE.4) THEN
C       SWAP-04
        CALL ERROR('SWAP-04: Binary reals not 4-byte long')
      END IF
C
C     Reading grid dimensions:
      CALL RSEP3I('N1',N1,1)
      CALL RSEP3I('N2',N2,1)
      CALL RSEP3I('N3',N3,1)
C
C     Swapping:
      WRITE(*,'(A)') '+SWAP: Swapping...           '
      OPEN(LU1,FILE=FILE1,FORM='UNFORMATTED',ACCESS='DIRECT',RECL=4,
     *                                                     STATUS='OLD')
C
C     Any Fortran 77 compiler (option "direct files without headers"):
      DO 10 I=1,N1*N2*N3
        READ(LU1,REC=I) AUX1
        I2(1)=I1(4)
        I2(2)=I1(3)
        I2(3)=I1(2)
        I2(4)=I1(1)
        WRITE(LU1,REC=I) AUX2
   10 CONTINUE
C
      CLOSE(LU1)
      WRITE(*,'(A)') '+SWAP: Done.                 '
      STOP
      END
C
C=======================================================================
C
      INCLUDE 'error.for'
C     error.for
      INCLUDE 'sep.for'
C     sep.for
      INCLUDE 'length.for'
C     length.for
C
C=======================================================================
C