      SUBROUTINE ABQMAIN
C
C     PROGRAM GRIDFILE_2D
c
c This program reads an ASCII formatted gridded wave data file and
c writes the corresponding unformatted binary gridded wave file required
c by ABAQUS.
c
c This program is valid for 2D and 3D gridded wave data.
c
c Description of variables:
c
c First Record:
c
c  NCOMP  = number of wave components
c  DTG    = time increment at which the data are given
c  NWGX   = number of grid points in the grid's x-direction
c  NWGY   = number of grid points in the grid's y-direction (if NWGY=1,
c           ABAQUS assumes data are constant in they local y-direction)
c  NWGZ   = number of grid points in the grid's z-direction (if NWGZ=0 or 1,
c           the analysis is two-dimensional and the y-direction is vertical)
c  IPDYN  = flag indicated whether or not dynamic pressure information is 
c           stored (IPDYN=1) or not stored (IPDYN=0) on the gridded wave file.
c
c Second Record:  This record is not used by ABAQUS.  It is provided for 
c                 information in user subroutine UEL via the interface 
c                 GETWAVE. The meaning of arrays AMP and WXL is left to 
c                 the user, but PHI is converted to radians.
c
c  AMP    = array containing "wave amplitudes" of each wave component
c  WXL    = array containing "wavelengths" of these components
c  PHI    = array containing "phase angles" (in degrees) of these components
c
c Third record:
c
c  WGX    = array containing local x-coordinates of the grid points
c  WGY    = array containing local y-coordinates of the grid points
c  WGZ    = array containing local z-coordinates of the grid points
c
c Remaining records (one for each time at which the wave data is given):
c
c  WGVX   = array containing local x-components of the wave particle velocity 
c  WGVY   = array containing local x-components of the wave particle velocity 
c  WGVZ   = array containing local x-components of the wave particle velocity 
c  WGAX   = array containing local x-components of the wave particle
c           acceleration
c  WGAY   = array containing local x-components of the wave particle
c           acceleration
c  WGAZ   = array containing local x-components of the wave particle
c           acceleration
c  WZCRST = array containing wave surface elevation
c  NCRST  = array containing the index for the vertical grid level just above
c           the instantaneous water surface
c
C
      PARAMETER(MCOMP=10,MDIMX=10,MDIMY=100,MDIMZ=100,MX=10,MY=10)
C
      DIMENSION AMP(MCOMP),WXL(MCOMP),PHI(MCOMP),
     $     WGX(MDIMX),WGY(MDIMY),WGZ(MDIMZ),
     $     WGVX(MDIMX,MDIMY,MDIMZ),WGVY(MDIMX,MDIMY,MDIMZ),
     $     WGVZ(MDIMX,MDIMY,MDIMZ),WGAX(MDIMX,MDIMY,MDIMZ),
     $     WGAY(MDIMX,MDIMY,MDIMZ),WGAZ(MDIMX,MDIMY,MDIMZ),
     $     P(MDIMX,MDIMY,MDIMZ),DPDZ(MDIMX,MDIMY,MDIMZ),
     $     WZCRST(MX,MY),NCRST(MX,MY)

      OPEN(UNIT=9, FILE='gridwave_2d.inp',FORM='FORMATTED',STATUS='OLD')
      OPEN(UNIT=10,FILE='gridwave_2d.binary',FORM='UNFORMATTED',
     $     STATUS='NEW')

      READ(9,*) NCOMP,DTG,NWGX,NWGY,NWGZ,IPDYN
      IF (NWGZ.EQ.0) NWGZ=1
      WRITE(10) NCOMP,DTG,NWGX,NWGY,NWGZ,IPDYN

      READ(9,*) (AMP(K1),WXL(K1),PHI(K1),K1=1,NCOMP)
      WRITE(10) (AMP(K1),WXL(K1),PHI(K1),K1=1,NCOMP)
      
      IF (NWGZ.EQ.1) THEN
         READ(9,*) (WGX(K1),K1=1,NWGX), (WGY(K1),K1=1,NWGY)
         WRITE(10) (WGX(K1),K1=1,NWGX), (WGY(K1),K1=1,NWGY)
      ELSE 
         READ(9,*) (WGX(K1),K1=1,NWGX), (WGY(K1),K1=1,NWGY),
     $        (WGZ(K1),K1=1,NWGZ)
         WRITE(10) (WGX(K1),K1=1,NWGX), (WGY(K1),K1=1,NWGY),
     $        (WGZ(K1),K1=1,NWGZ)
      END IF
C
C NWHILE is a dummy flag for DO WHILE loop. 
C
      NWHILE = 1
      NCOUNT = 0
      IF(IPDYN.EQ.0) THEN
         IF (NWGZ.EQ.1) THEN
            DO WHILE (NWHILE.NE.0)
               READ(9,*,END=100) ((WGVX(K1,K2,1), WGVY(K1,K2,1),
     $           WGAX(K1,K2,1),WGAY(K1,K2,1), K2=1,NWGY),
     $           WZCRST(K1,1),NCRST(K1,1), K1=1,NWGX)
               NCOUNT=NCOUNT+1
               WRITE(10) ((WGVX(K1,K2,1), WGVY(K1,K2,1),
     $           WGAX(K1,K2,1),WGAY(K1,K2,1), K2=1,NWGY),
     $           WZCRST(K1,1),NCRST(K1,1), K1=1,NWGX)
            END DO
         ELSE 
            DO WHILE (NWHILE.NE.0)
               READ(9,*,END=100) (((WGVX(K1,K2,K3), WGVY(K1,K2,K3),
     $           WGVZ(K1,K2,K3),WGAX(K1,K2,K3),WGAY(K1,K2,K3),
     $           WGAZ(K1,K2,K3),K3=1,NWGZ),WZCRST(K1,K2),NCRST(K1,K2),
     $           K1=1,NWGX),K2=1,NWGY)
               NCOUNT=NCOUNT+1
               WRITE(10) (((WGVX(K1,K2,K3), WGVY(K1,K2,K3),
     $           WGVZ(K1,K2,K3),WGAX(K1,K2,K3),WGAY(K1,K2,K3),
     $           WGAZ(K1,K2,K3),K3=1,NWGZ),WZCRST(K1,K2),NCRST(K1,K2),
     $           K1=1,NWGX),K2=1,NWGY)
            ENDDO
         END IF
      ELSE
         IF (NWGZ.EQ.1) THEN
            DO WHILE (NWHILE.NE.0)
               READ(9,*,END=100) ((WGVX(K1,K2,1), WGVY(K1,K2,1),
     $           WGAX(K1,K2,1),WGAY(K1,K2,1),
     $           P(K1,K2,1),DPDZ(K1,K2,1),K2=1,NWGY),
     $           WZCRST(K1,1),NCRST(K1,1),K1=1,NWGX)
               NCOUNT=NCOUNT+1
               WRITE(10) ((WGVX(K1,K2,1), WGVY(K1,K2,1),
     $           WGAX(K1,K2,1),WGAY(K1,K2,1),
     $           P(K1,K2,1),DPDZ(K1,K2,1),K2=1,NWGY),
     $           WZCRST(K1,1),NCRST(K1,1),K1=1,NWGX)
            ENDDO
         ELSE 
            DO WHILE (NWHILE.NE.0)
               READ(9,*,END=100) (((WGVX(K1,K2,K3), WGVY(K1,K2,K3),
     $           WGVZ(K1,K2,K3),WGAX(K1,K2,K3),WGAY(K1,K2,K3),
     $           WGAZ(K1,K2,K3),P(K1,K2,K3),DPDZ(K1,K2,K3),K3=1,NWGZ),
     $           WZCRST(K1,K2),NCRST(K1,K2),K1=1,NWGX),K2=1,NWGY)
               NCOUNT=NCOUNT+1
               WRITE(10) (((WGVX(K1,K2,K3), WGVY(K1,K2,K3),
     $           WGVZ(K1,K2,K3),WGAX(K1,K2,K3),WGAY(K1,K2,K3),
     $           WGAZ(K1,K2,K3),P(K1,K2,K3),DPDZ(K1,K2,K3),K3=1,NWGZ),
     $           WZCRST(K1,K2),NCRST(K1,K2),K1=1,NWGX),K2=1,NWGY)
            ENDDO
         END IF
      ENDIF
 100  CONTINUE
      WRITE(*,'(I6," COMPLETE TIME POINTS READ")') NCOUNT

      END

