      PROGRAM BIN2CNV
C     Copyright (c) 1997, W. H. Chiang 
C
C     This program is a free software. You may distribute it in its original
C     form. You may also modify it to fit your need. You must accept the 
C     following Licence Agreement before you can use this software.
C
C
C     Licence Agreement
C
C     YOU MAY NOT:
C     Rent, lease, transfer rights to the Software or remove any proprietary 
C     notices or labels on the Software. 
C
C     DISCLAIMER OF WARRANTY:
C     Since the Software is provided free of charge, the Software is provided 
C     on an AS IS basis, without warranty of any kind, including without 
C     limitation the warranties of merchantability, fitness for a particular
C     purpose and non-infringement. The entire risk as to the quality and performance 
C     of the Software is borne by you. Should the Software prove defective, 
C     you assume the entire cost of any service and repair.
C
C     LIMITATION OF LIABILITY: UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, 
C     TORT, CONTRACT, OR OTHERWISE, SHALL THE AUTHOR BE LIABLE TO YOU OR ANY OTHER 
C     PERSON FOR ANY INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 
C     OF ANY CHARACTER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF GOODWILL,
C     WORK STOPPAGE, COMPUTER FAILURE OR MALFUNCTION, OR ANY AND ALL OTHER 
C     COMMERCIAL DAMAGES OR LOSSES.
C
C
C     This program converts the unformatted result files from MODFLOW
C     to ASCII. It reads the grid information from the file BAS.DAT. 
C     
C     The following file must be available:
C       BAS.DAT    Input file for the BASIC package.
C
C     The following "unformatted" files are optional:
C       HEADS.DAT  Output file containing the calculated heads
C       DDOWN.DAT  Output file containing the calculated drawdowns
C       BUDGET.DAT Output file containing the cell-by-cell flow terms
C
C     All files and the executable of this program must be stored in the same
C     directory. The result ASCII files are: 
C       HEADS.ASC  Output file containing the calculated heads
C       DDOWN.ASC  Output file containing the calculated drawdowns
C       BUDGET.ASC Output file containing the cell-by-cell flow terms
C
C     For HEADS.ASC and DDOWN.ASC, the results are saved layer by layer.
C     Each layer has a header. The variables are described below.
C
C     KPER    STRESS PERIOD NUMBER
C     KSTP    TIME STEP NUMBER WITHIN THE PERIOD KPER
C     PERTIM  ELAPSED TIME WITHIN THE PERIOD KPER
C     TOTIM   TOTAL ELAPSED TIME SINCE THE BEGINNING OF THE SIMULATION
C     TEXT    DRAWDOWN or HEAD
C     NC      NUMBER OF COLUMNS
C     NR      NUMBER OF ROWS
C     K       THE LAYER NUMBER, FOR WHICH THE FOLLOWED MATRIX IS SAVED.
C
C     For BUDGET.ASC, results of each flow term of the entire model
C     is saved separated with a header. The variables are described below.
C
C     KPER    STRESS PERIOD NUMBER
C     KSTP    TIME STEP NUMBER WITHIN THE PERIOD KPER
C     TEXT    CELL-BY-CELL FLOW TERMS, SUCH AS 'FLOW RIGHT FACE' or 'FLOW FRONT FACE'
C     NC      NUMBER OF COLUMNS
C     NR      NUMBER OF ROWS
C     NL      NUMBER OF LAYERS
C
C
      DIMENSION A(300000)
      DIMENSION HEADNG(32)
      LENA=300000
C
C     READ DATAPATH FROM PROFILE
C
C
      OPEN(1,file='BAS.DAT')
      READ(1,20) HEADNG
   20 FORMAT(20A4)
      READ(1,30) NLAY,NROW,NCOL,NPER,ITMUNI
   30 FORMAT(8I10)
      CLOSE(1)
      NRC=NROW*NCOL
      NRCL=NROW*NCOL*NLAY
      LCNRC=1
      LCNRCL=NRC+1
      NTOTAL=NRC+NRCL
      IF (NTOTAL.GT.LENA) THEN
         WRITE(*,*) 'LENGTH OF MASTER ARRAY LENA IN THE PROGRAM'
         WRITE(*,*) 'BIN2ASC.FOR IS TOO SMALL'
	 STOP
      ENDIF

      CALL CONVERT(NLAY,NROW,NCOL,A(LCNRC),A(LCNRCL))
      stop
      end
C
C
      SUBROUTINE CONVERT(NLAY,NROW,NCOL,HEAD,BUDG)
      DIMENSION HEAD(NCOL,NROW),BUDG(NCOL,NROW,NLAY)
      CHARACTER*16 TEXT
C
C     SET ALL VALUES TO ZERO
      DO I=1,NCOL
        DO J=1,NROW
	  HEAD(I,J)=0
          DO K=1,NLAY
	    BUDG(I,J,K)=0
          END DO
        END DO
      END DO
C
      OPEN(2,file='HEADS.DAT',FORM='UNFORMATTED',ERR=51)
      OPEN(3,file='HEADS.ASC')
   33 DO NL=1,NLAY
        READ(2,ERR=51) KSTP,KPER,PERTIM,TOTIM,TEXT,NC,NR,K
        WRITE(3,50) KSTP,KPER,PERTIM,TOTIM,TEXT,NC,NR,K
        READ(2,ERR=51) HEAD
        DO J=1,NR
           write(3,*) (HEAD(I,J),I=1,NC)
        END DO
      END DO
      GO TO 33
   50 FORMAT(I6,I6,E12.4,E12.4,A16,I6,I6,I6)
   51 CONTINUE
      CLOSE(2)
      CLOSE(3)
      
      OPEN(2,file='DDOWN.DAT',FORM='UNFORMATTED',ERR=151)
      OPEN(3,file='DDOWN.ASC')
  173 DO NL=1,NLAY
         READ(2,ERR=151) KSTP,KPER,PERTIM,TOTIM,TEXT,NC,NR,K
         WRITE(3,50) KSTP,KPER,PERTIM,TOTIM,TEXT,NC,NR,K
         READ(2,ERR=151) HEAD
         DO J=1,NR
           write(3,*) (HEAD(I,J),I=1,NC)
         END DO
      END DO
      GOTO 173
  151 CONTINUE
      CLOSE(2)
      CLOSE(3)
      
      OPEN(2,file='BUDGET.DAT',FORM='UNFORMATTED',ERR=90)
      OPEN(3,file='BUDGET.ASC')
   55 READ(2,ERR=90) KSTP,KPER,TEXT,NC,NR,NL
      WRITE(3,70) KSTP,KPER,TEXT,NC,NR,NL
      READ(2,ERR=90) BUDG
      DO 60 K=1,NL
      DO 60 J=1,NR
      write(3,*) (BUDG(I,J,K),I=1,NC)
   60 CONTINUE
   70 FORMAT(I6,I6,A16,I6,I6,I6)
   80 GOTO 55
   90 CONTINUE
      CLOSE(2)
      CLOSE(3)
      
      RETURN
      END

