C
C Program ASCBIN to convert gridded data (data cubes) from formatted
C ascii files to direct-access binary files
C
C Version: 5.40
C Date: 2000, May 29
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 ASCBIN is strongly affected by
C the Fortran compiler and by the options of the compiler.
C Program ASCBIN can work only if the compiler supports unformatted
C direct-access files "without headers".
C Binary data on little-endian hardware (PC's) and big-endian hardware
C (VAX, Alpha, RISC workstations) should strictly be distinguished.
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 files:
C     ASC='string'... String with the name of the input formatted file
C             containing the gridded values.  The file contains N1*N2*N3
C             reals designed to be read by a single list directed (free
C             format) read statement.
C             No default, ASC must be specified and cannot be blank.
C     IN='string'... String with the name of the output unformatted file
C             to contain the gridded values.  The file will contain just
C             the 4 byte IEEE reals.  The length of the file will thus
C             be exactly 4*N1*N2*N3 bytes.
C             No default, IN must be specified and cannot be blank.
C Data specifying output format:
C     ESIZE=integer... Number of bytes per a real in the output binary
C             file.  Must be ESIZE=4.
C             Default: ESIZE=4
C     UNDEF=real... If the input ascii file contains undefined values,
C             the value of UNDEF will be written in place of the
C             undefined values into the output binary file.
C             Default: UNDEF=0.
C
C=======================================================================
C
C Common block /RAMC/:
      INCLUDE 'ram.inc'
C     ram.inc
C
C.......................................................................
C
      CHARACTER*80 FILE1,FILE2
      INTEGER LU1,LU2
      PARAMETER (LU1=1,LU2=2)
C
      INTEGER N1,N2,N3,I
      REAL UNDEF
C
C.......................................................................
C
C     Reading main input data:
      WRITE(*,'(A)') '+ASCBIN: Enter input filename: '
      FILE1=' '
      READ(*,*) FILE1
      IF (FILE1.NE.' ') THEN
        CALL RSEP1(LU1,FILE1)
      ELSE
C       ASCBIN-01
        CALL ERROR('ASCBIN-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.
      ENDIF
C
C     Input and output files with gridded data:
      CALL RSEP3T('ASC',FILE1,' ')
      IF (FILE1.EQ.' ') THEN
C       ASCBIN-02
        CALL ERROR('ASCBIN-02: Input file not specified')
      END IF
      CALL RSEP3T('IN',FILE2,' ')
      IF (FILE2.EQ.' ') THEN
C       ASCBIN-03
        CALL ERROR('ASCBIN-03: Output file not specified')
      END IF
      CALL RSEP3I('ESIZE',I,4)
      IF (I.NE.4) THEN
C       BINASC-04
        CALL ERROR('BINASC-04: Binary reals not 4-byte long')
      END IF
      CALL RSEP3R('UNDEF',UNDEF,0.)
C
C     Reading grid dimensions:
      CALL RSEP3I('N1',N1,1)
      CALL RSEP3I('N2',N2,1)
      CALL RSEP3I('N3',N3,1)
      IF (N1*N2*N3.GT.MRAM) THEN
C       ASCBIN-05
        CALL ERROR('ASCBIN-05: Small dimension MRAM of array RAM')
      END IF
C
C     Reading input grid values:
      WRITE(*,'(A)') '+ASCBIN: Reading...            '
      CALL RARRAY(LU1,FILE1,'FORMATTED',.TRUE.,UNDEF,N1*N2*N3,RAM)
C
C     Writing output grid values:
      WRITE(*,'(A)') '+ASCBIN: Writing...            '
      CALL WBIN(LU2,FILE2,RAM,N1*N2*N3)
C
      WRITE(*,'(A)') '+ASCBIN: Done.                 '
      STOP
      END
C
C=======================================================================
C
      SUBROUTINE WBIN(LU,FILE,GRID,N)
      INTEGER LU,N
      CHARACTER*(*) FILE
      REAL GRID(N)
C
C-----------------------------------------------------------------------
C
      INTEGER I
C
C     Any Fortran 77 compiler (option "direct files without headers"):
      OPEN(LU,FILE=FILE,FORM='UNFORMATTED',ACCESS='DIRECT',RECL=4)
      DO 10 I=1,N
        WRITE(LU,REC=I) GRID(I)
   10 CONTINUE
C
C     Lahey F77L3 (compiler-dependent Fortran extension):
*     OPEN(LU,FILE=FILE,FORM='UNFORMATTED',ACCESS='TRANSPARENT')
*     WRITE(LU) GRID
C
      CLOSE(LU)
      RETURN
      END
C
C=======================================================================
C
      INCLUDE 'error.for'
C     error.for
      INCLUDE 'sep.for'
C     sep.for
      INCLUDE 'forms.for'
C     forms.for
      INCLUDE 'length.for'
C     length.for
C
C=======================================================================
C