      PROGRAM GRDCON

***********************************************************************
*                                                                     *
* THIS PROGRAM CONVERTS NADCON GRID FILES THAT HAVE BE TRANSLATED TO  *
* ASCII BACK TO BINARY.
*                                                                     *
* VERSION CODE:  1.00                                                 *
*                                                                     *
* VERSION DATE:  30-JAN-90                                            *
*                                                                     *
*        AUTHOR:   WARREN T. DEWHURST, PH.D.                          *
*                    LIEUTENANT COMMANDER, NOAA                       *
*                  ALICE R. DREW                                      *
*                  NATIONAL GEODETIC SURVEY, NOS, NOAA                *
*                  ROCKVILLE, MD   20852                              *
***********************************************************************

***********************************************************************
*                                                                     *
*                  DISCLAIMER                                         *
*                                                                     *
*   THIS PROGRAM AND SUPPORTING INFORMATION IS FURNISHED BY THE       *
* GOVERNMENT OF THE UNITED STATES OF AMERICA, AND IS ACCEPTED AND     *
* USED BY THE RECIPIENT WITH THE UNDERSTANDING THAT THE UNITED STATES *
* GOVERNMENT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, CONCERNING THE  *
* ACCURACY, COMPLETENESS, RELIABILITY, OR SUITABILITY OF THIS         *
* PROGRAM, OF ITS CONSTITUENT PARTS, OR OF ANY SUPPORTING DATA.       *
*                                                                     *
*   THE GOVERNMENT OF THE UNITED STATES OF AMERICA SHALL BE UNDER NO  *
* LIABILITY WHATSOEVER RESULTING FROM ANY USE OF THIS PROGRAM.  THIS  *
* PROGRAM SHOULD NOT BE RELIED UPON AS THE SOLE BASIS FOR SOLVING A   *
* PROBLEM WHOSE INCORRECT SOLUTION COULD RESULT IN INJURY TO PERSON   *
* OR PROPERTY.                                                        *
*                                                                     *
*   THIS PROGRAM IS PROPERTY OF THE GOVERNMENT OF THE UNITED STATES   *
* OF AMERICA.  THEREFORE, THE RECIPIENT FURTHER AGREES NOT TO ASSERT  *
* PROPRIETARY RIGHTS THEREIN AND NOT TO REPRESENT THIS PROGRAM TO     *
* ANYONE AS BEING OTHER THAN A GOVERNMENT PROGRAM.                    *
*                                                                     *
***********************************************************************

      IMPLICIT REAL (A-H, O-Z)
      IMPLICIT INTEGER (I-N)
      INTEGER NMAX
      PARAMETER (NMAX=1024)

      CHARACTER*32 FILEIN,FILEOUT
      CHARACTER*1 JUNK
      CHARACTER*56 RIDENT*56
      CHARACTER*8 PGM*8
      INTEGER NC, NR, LRECL, NZ, I, IREC, J
      REAL XO, XMAX, YO, YMAX, ZMIN, ZMAX, DX, DY, ANGLE, BUFFER(NMAX)

  100 WRITE (*,*) ' Enter input (ASCII) file name:'
      READ (*,'(A)') FILEIN
      OPEN (10,FILE=FILEIN,STATUS='OLD',ERR=100)

* Read ASCII file header information, write to screen

      READ (10,'(A)') JUNK
      READ (10,*) NC, NR
      IF (NC .GT. NMAX) THEN
        WRITE (*,*) ' *** No. of columns (', NC,
     &              ') too large for GRDCON buffer of (', NMAX, ') ***'
        STOP
      END IF
      READ (10,*) XO, XMAX
      READ (10,*) YO, YMAX
      READ (10,*) ZMIN, ZMAX
      WRITE (*,*) ' *** Input grid information ***'
      WRITE (*,*) ' NC = ', NC, ' NR = ', NR
      WRITE (*,*) ' XMIN = ', XO, ' XMAX = ', XMAX
      WRITE (*,*) ' YMIN = ', YO, ' YMAX = ', YMAX
      WRITE (*,*) ' ZMIN = ', ZMIN, ' ZMAX = ', ZMAX

* Calculate (CPFS) binary file header information and open file

      LRECL = 4*(NC + 1)
      DX = (XMAX - XO)/(NC - 1)
      DY = (YMAX - YO)/(NR - 1)
      ANGLE = 0.0
      RIDENT = ' '
      PGM = 'GRDCON'
      NZ = 1

* Get name of output (CPFS) binary grid file

  200 WRITE (*,*) ' Enter name of output binary grid file:'
      READ (*,'(A)') FILEOUT
      OPEN (11,FILE=FILEOUT,FORM='UNFORMATTED',ACCESS='DIRECT',
     &      RECL=LRECL,STATUS='NEW',ERR=200)

* Write header record in binary file

      WRITE (11,REC=1) RIDENT, PGM, NC, NR, NZ, XO, DX, YO, DY, ANGLE

* Copy file data

      DO 300 I = 1, NR
        IREC = I + 1
        READ (10,*,ERR=700) (BUFFER(J), J = 1, NC)
        WRITE (11,REC=IREC) I, (BUFFER(J), J = 1, NC)
        WRITE (*,*) ' ROW ', I
  300 CONTINUE

  600 CLOSE(10)
      CLOSE(11)
      STOP

* Error Message

  700 WRITE (*,*) ' ***** Error in reading input ascii file ', FILEIN
      GOTO 600
      END
