C     ----------------------
      SUBROUTINE WOUTPUT1(H)
C     ----------------------
C
C     SUBROUTINE AUTHOR:
C     TIM DURBIN
C     TIMOTHY J. DURBIN, INC.
C     5330 PRIMROSE DRIVE, SUITE 228
C     FAIR OAKS, CA 95628
C     (916) 536-2314
C
C     PURPOSE:
C     THIS SUBROUTINE USES THE CALCULATED HEADS FOR THE MODEL DOMAIN
C     AND ECHOS THE INPUTS OF THE .HED FILE, OUTPUTS THE
C     .PLT TO BE READ IN BY GMS, OUTPUTS THE CALCULATED HEAD AT THE
C     OBSERVATION LOCATIONS AS DEFINED BY THE .HED FILE AND OUTPUTS THE
C     GROUNDWATER BUDGET FOR THE SIMULATION. THE FIRST ENTRY POINT IS
C     CALLED ONCE BYTHE MAIN PROGRAM. THE SECOND ENTRY POINT IS CALL
C     ONCE FOR EACH TIMESTEP ITERATION.
C
      INCLUDE 'DIMEN.INC'
      INCLUDE 'WUNITS.INC'
      IMPLICIT REAL*8 (A-H,O-Z)
      IMPLICIT INTEGER*4 (I-N)
C
C     INTERNAL SCALARS AND ARRAYS.
C     INFILE      NAME OF INPUT FILE.
C     OUTFILE1    OUTPUT FOR THE HED.OUT FILE.
C     OUTFILE2    OUTPUT FOR THE HED.PLT FILE.
C     OUTFILE3    OUTPUT FOR THE HED.GPH FILE.
C     OUTFILE4    OUTPUT FOR THE HED.BUD FILE.
C     OUTEXT      EXTENSION FOR OUTPUT FILE.
C     WNAME()     NAME ASSOCIATED WITH HYDROGRAPH SITE.
C     XCNAME      COMPARTMENT NAME.
C     WNODEC()    NODE NUMBER ASSIGNED TO WEIGHT.
C     NNODE()     NUMBER OF NODES ASSOCIATED WITH HYDROGRRAPH SITE.
C     H()         HEAD VALUE AT NODE.
C     WEIGHT()    NODAL WEIGHT.
C     HGRAPH()    CALCULATED HEAD, GIVEN NODAL WEIGHT.
C     GTIME()     ELAPSED TIME FOR SIMULATION TIME STEP.
C
C     INTERNAL ARRAYS.
      CHARACTER*80 INFILE,OUTFILE1,OUTFILE2,OUTFILE3,OUTFILE4,OUTEXT
      CHARACTER*20 WNAME(MAXGWO),XCNAME
      INTEGER*4 WNODEC,WNODE(MAXGWO,MAXGWN),NNODE(MAXGWO)
      REAL*8 H(MAXNN),WEIGHT(MAXGWO,MAXGWN),HGRAPH(MAXGWO,MAXSTP)
      REAL*8 GTIME(MAXSTP)
      LOGICAL DONE
C
C     COMMON BLOCKS.
C     FNAME()     NAMES OF INPUT FILES.
C     RNAME1      SIMULATION NAME FOR GROUNDWATER-FLOW FILES.
C     RNAME2      SIMULATION NAME FOR SOLUTE-TRANSPORT FILES.
C     NCELL       NUMBER OF COMPARTMENTS.
C     CNAME()     NAMES FOR COMPARTMENTS.
C     NNC()       NUMBER OF NODES IN A COMPARTMENT.
C     NEC()       NUMBER OF WEDGE ELEMENTS WITHIN A COMPARTMENT.
C     IN()        ELEMENT INCIDENCE FOR A WEDGE ELEMENT.
C     IN2()       ELEMENT INCIDENCES FOR A TETRAHEDRAL ELEMENT,
C                     WELL LINK, FAULT ELEMENT, OR FAULT LINK.
C     IEL()       PARENT WEDGE ELEMENT FOR TETRAHEDRAL ELEMENT.
C     X()         X-COORDINATE OF A NODE.
C     Y()         Y-COORDINATE OF A NODE.
C     Z()         Z-COORDINATE OF A NODE.
C     IA()        VECTOR INDICATING POSITIONS OF ROW ENDS IN
C                     VECTOR A() OR B().
C     JA()        VECTOR INDICATION COLUMN NUMBER ASSOCIATED WITH
C                     ENTRIES IN VECTORS A() OR B().
C     A()         VECTOR OF NON-ZERO COEFFICIENTS FOR CONDUCTANCE
C                     MATRIX.
C     B()         VECTOR OF NON-ZERO COEFFICIENTS FOR CAPACITANCE
C                     MATRIX.
C     F()         RIGHTHAND VECTOR FOR SYSTEM OF EQUATIONS.
C     QX()        DARCY GROUNDWATER VELOCITY IN THE X-DIRECTION.
C     QY()        DARCY GROUNDWATER VELOCITY IN THE Y-DIRECTION.
C     QZ()        DARCY GROUNDWATER VELOCITY IN THE Z-DIRECTION.
C     NE          GLOBAL NUMBER OF WEDGE ELEMENTS.
C     NE2         GLOBAL NUMBER OF TETRAHEDRAL ELEMENTS.
C     NE3         GLOBAL NUMBER OF TETRAHEDRAL ELEMENS AND WELL LINKS.
C     NE4         GLOBAL NUMBER OF TETHEDRALAL ELEMENTS, WELL LINKS,
C                     AND FAULT ELEMENTS.
C     NE5         GLOBAL NUMBER OF TETHEDRALAL ELEMENTS, WELL LINKS,
C                     FAULT ELEMENTS, AND FAULT LINKS.
C     NN          GLOBAL NUMBER OF MESH NODES.
C     NN3         GLOBAL NUMBER OF MESH NODES AND WELL NODES.
C     NN5         GLOBAL NUMBER OF MESH NODES, WELL NODES, AND
C                     FAULT NODES.
C     ISTEP       TIME STEP NUMBER WITHIN FLOW SIMULATION.
C     ISTEP1      STARTING STEP NUMBER FOR SIMULATION.
C     ISTEP2      ENDING STEP FOR SIMULATION.
C     DELT        LENGTH OF TIME STEP.
C     STIME       INITIAL ELAPSED TIME FOR SIMULATION.
C     IPRN        FLAG TO INDICATED PRINT OUTPUT FOR TIME STEP.
C     IPLT        FLAG TO INDICATE PLOT OUTPUT FOR TIME STEP.
C     NTSTEP      NUMBER OF TRANSPORT STEPS WITHIN FLOW STEP.
C     ITSTEP      TIME STEP NUMBER WITHIN TRANSPORT SIMULATION.
C     NIA         NUMBER OF ROWS REPRESENTED IN VECTORS A() OR B().
C     NJA         NUMBER OF NON-ZERO COEFFICIENTS IN VECTORS A() OR B().
C     ITRAN       SWITCH FOR TRANSPORT SIMULATION.
C     IROUT       INTERNAL READ FOR OUT UNIT NUMBER.
C     IWOUT       INTERNAL WRITE FOR OUT UNIT NUMBER.
C
C
C     COMMON BLOCKS.
      CHARACTER*80 FNAME(30)
      CHARACTER*12 RNAME1,RNAME2
      CHARACTER*20 CNAME(MAXCEL)
      INTEGER*4 NNC(MAXCEL),NEC(MAXCEL)
      INTEGER*4 IN(MAXNE,6),IN2(MAXNE2,4),IEL(MAXNE2)
      REAL*8 X(MAXNN),Y(MAXNN),Z(MAXNN)
      INTEGER*4 IA(MAXNN+1),JA(MAXNON)
      REAL*8 A(MAXNON),B(MAXNON),F(MAXNN)
      REAL*8 QX(MAXNE2),QY(MAXNE2),QZ(MAXNE2)
      COMMON /NAMES/ FNAME,RNAME1,RNAME2
      COMMON /CELLS/ NCELL,CNAME,NNC,NEC
      COMMON /MESH/ X,Y,Z,IN,IN2,IEL,NE,NE2,NE3,NE4,NE5,NN,NN3,NN5
      COMMON /STEPS/ ISTEP,ISTEP1,ISTEP2,DELT,STIME,IPRN,IPLT,NTSTEP,
     +    ITSTEP
      COMMON /MATRIX/ A,B,IA,JA,NIA,NJA,F
      COMMON /SHARE/ QX,QY,QZ,ITRAN
      SAVE
C
C     OPEN HEAD OUTPUT AND PLOT FILES.
      WRITE(*,900)
      OUTEXT='.HED.OUT'
      INFILE=FNAME(6)
      CALL OPENFILES(RNAME1,INFILE,OUTFILE1,OUTEXT,IROUT,IWOUT)
      OUTEXT='.HED.PLT'
      CALL OPENFILES(RNAME1,INFILE,OUTFILE2,OUTEXT,0,IWPLT)
      OUTEXT='.HED.GPH'
      CALL OPENFILES(RNAME1,INFILE,OUTFILE3,OUTEXT,0,IWGPH)
      OUTEXT='.BUD.OUT'
      CALL OPENFILES(RNAME1,INFILE,OUTFILE4,OUTEXT,0,IWBUD)
      WRITE(IWOUT,902)
      WRITE(IWOUT,904) INFILE,OUTFILE1,OUTFILE2,OUTFILE3,OUTFILE4
C
C     LOOP OVER COMPARTMENTS.
      IGPH=0
      ILAST=0
      NGPH=0
      DO ICELL=1,NCELL
C
C         GROUNDWATER OBSERVATION POINTS
          READ(IROUT,906) XCNAME
          READ(IROUT,908) NGPHC
          WRITE(IWOUT,910) ICELL,NCELL,XCNAME,NGPHC
          NGPH=NGPH+NGPHC
          IF (NGPH.GT.MAXGWO) THEN
              WRITE(*,912)
              WRITE(*,914)
              STOP
          ENDIF
          IF (XCNAME.NE.CNAME(ICELL)) THEN
              WRITE(*,916)
              WRITE(*,914)
              STOP
          ENDIF
C
C         INPUT OBSERVATION WELLS
          IF (NGPHC.EQ.0) THEN
              ILAST=ILAST+NNC(ICELL)
              CYCLE
          ENDIF
          DO II=1,NGPHC
              IGPH=IGPH+1
              READ(IROUT,918) NNODE(IGPH),WNAME(IGPH)
              WRITE(IWOUT,920) IGPH,WNAME(IGPH),NNODE(IGPH)
              IF (NNODE(IGPH).GT.MAXGWN) THEN
                  WRITE(*,922)
                  WRITE(*,914)
                  STOP
              ENDIF
              DO JJ=1,NNODE(IGPH)
                  READ(IROUT,924) WNODEC,WEIGHT(IGPH,JJ)
                  WNODE(IGPH,JJ)=WNODEC+ILAST
                  WRITE(IWOUT,926) WNODEC,WNODE(IGPH,JJ),WEIGHT(IGPH,JJ)
              END DO
          END DO
          ILAST=ILAST+NNC(ICELL)
      END DO
C
C     WRITE PLOT HEADER, CLOSE INPUT FILE, AND EXIT WOUTPUT.
      WRITE(IWPLT,928)
      WRITE(IWPLT,930)
      WRITE(IWPLT,932)
      WRITE(IWPLT,934) NN
      WRITE(IWPLT,936) NE
      WRITE(IWPLT,937)
      WRITE(IWPLT,946) 0
      DO II=1,NN
          WRITE(IWPLT,948) H(II)
      END DO
      WRITE(IWOUT,938)
      WRITE(*,939)
      CLOSE (IROUT)
      RETURN
C
C     FORMATS:
900   FORMAT(1X,'*START READING IN WOUTPUT')
902   FORMAT(1X,'**START READING IN WOUTPUT**')
904   FORMAT(1X,'FILE NAMES:'/
     +    1X,'INPUT FILE..............................',A80/
     +    1X,'OUTPUT FILE.............................',A80/
     +    1X,'PLOT OUTPUT FILE........................',A80/
     +    1X,'GRAPH OUTPUT FILE.......................',A80/
     +    1X,'BUDGET OUTPUT FILE......................',A80)
906   FORMAT(A20)
908   FORMAT(I10)
910   FORMAT(/1X,'GROUNDWATER OBSERVATION POINTS:'/
     +    1X,'COMPARTMENT NUMBER......................',I10/
     +    1X,'NUMBER OF COMPARTMENTS..................',I10/
     +    1X,'COMPARTMENT NAME........................',A20/
     +    1X,'NUMBER OF HYDROGRAPHS...................',I10)
912   FORMAT(1X,'*ERROR* NUMBER OF HYDROGRAPHS EXCEEDS DIMENSION')
914   FORMAT(1X,'*ERROR* SIMULATION TERMINATED FOR ERROR IN WOUTPUT')
916   FORMAT(1X,'*ERROR* COMPARTMENT NAME DOES NOT IN WITH LIST')
918   FORMAT(I10,1X,A20)
920   FORMAT(/1X,'HYDROGRAPH PARAMETERS:'/
     +    1X,'GRAPH NUMBER............................',I10/
     +    1X,'SITE NAME...............................',A20/
     +    1X,'NUMBER OF NODES.........................',I10//
     +    1X,'SITE NODES AND WEIGHTS:'/
     +    1X,5X,'LOCAL',4X,'GLOBAL'/
     +    1X,6X,'NODE',6X,'NODE',4X,'WEIGHT')
922   FORMAT(1X,'*ERROR* NUMBER OF NODES EXCEEDS DIMENSION')
924   FORMAT(I10,F10.0)
926   FORMAT(1X,2I10,1PE10.2)
928   FORMAT('DATASET')
930   FORMAT('OBJTYPE mesh3d')
932   FORMAT('BEGSCL')
934   FORMAT('ND',I13)
936   FORMAT('NC',I13)
937   FORMAT('NAME "Scalar Heads"')
938   FORMAT(/1X,'**END READING IN WOUTPUT**')
939   FORMAT(1X,'*END   READING IN WOUTPUT')
C
C     -----------------------------------------------------------------
      ENTRY WOUTPUT2(H,SUMCH,SUMFX,SUMWL,SUMET,SUMRV,SUMLK,SUMPS,SUMFS,
     +    ITER,DIFF)
C     -----------------------------------------------------------------
C
C     PRINT OUTPUT HEADER.
      IF (ISTEP.EQ.ISTEP1) THEN
          WRITE(IWOUT,940)
          WRITE(IWBUD,940)
          WRITE(IWBUD,941)
      ENDIF
C
C     OUTPUT HEADS TO PRINT FILE.
      IF (IPRN.EQ.1) THEN
          HMAX=-1.0E+20
          HMIN=1.0E+20
          DO I=1,NN
              HMAX=MAX(HMAX,H(I))
              HMIN=MIN(HMIN,H(I))
          END DO
          WRITE(IWOUT,942) ISTEP,ISTEP1,ISTEP2,STIME,ITER,DIFF,
     +        HMIN,HMAX
          DONE=.FALSE.
          I1=-4
          DO WHILE (.NOT.DONE)
              I1=I1+5
              I2=I1+4
                  IF (I2.GE.NN) THEN
                      I2=NN
                      DONE=.TRUE.
                  ENDIF
                  WRITE(IWOUT,944) I1,I2,(H(I),I=I1,I2)
          END DO
      ENDIF
C
C     OUTPUT HEADS TO PLOT FILE.
      IF (IPLT.EQ.1) THEN
          WRITE(IWPLT,946) ISTEP
          DO II=1,NN
              WRITE(IWPLT,948) H(II)
          END DO
      ENDIF
C
C     LOAD HEADS INTO GRAPH VECTORS.
C
      GTIME(ISTEP)=STIME
      DO IGPH=1,NGPH
          HGRAPH(IGPH,ISTEP)=0.0
          DO JJ=1,NNODE(IGPH)
              II=WNODE(IGPH,JJ)
              HGRAPH(IGPH,ISTEP)=HGRAPH(IGPH,ISTEP)+
     +            H(II)*WEIGHT(IGPH,JJ)
          END DO
      END DO
C
C     OUTPUT WATER BUDGET.
      RESID1=SUMCH+SUMFX+SUMWL+SUMLK+SUMET+SUMRV-SUMPS
      RESID2=-SUMLK-SUMFS
      WRITE(IWBUD,949) ISTEP,STIME,SUMCH,SUMFX,SUMWL,SUMET,SUMRV,SUMLK,
     +    SUMPS,RESID1,SUMFS,RESID2
C
C     FLUSH PRINT BUFFERS.
      CALL FLUSH(IWOUT)
      CALL FLUSH(IWBUD)
      CALL FLUSH(IWPLT)
C     PRINT HYDROGRAPHS AND CLOSE FILES.
      IF (ISTEP.EQ.ISTEP2) THEN
          DO IGPH=1,NGPH
              WRITE(IWGPH,950) WNAME(IGPH)
              DO JSTEP=ISTEP1,ISTEP2
                  WRITE(IWGPH,952) JSTEP,GTIME(JSTEP),
     +                HGRAPH(IGPH,JSTEP)
              END DO
          END DO
          WRITE(IWOUT,954)
          CLOSE (IWOUT)
          CLOSE (IWPLT)
          CLOSE (IWGPH)
          CLOSE (IWBUD)
      ENDIF
      RETURN
C
C     FORMATS:
940   FORMAT(1X,'**START SIMULATION IN WOUTPUT**')
941   FORMAT(/1X,'WATER-BUDGET COMPONENTS:'//
     +    1X,21X,'SPECIFIED',1X,'SPECIFIED',43X,'AQUIFER',3X,'AQUIFER',
     +        5X,'FAULT',5X,'FAULT'/
     +    1X,6X,'STEP',6X,'TIME',6X,'HEAD',6X,'FLUX',6X,'WELL',8X,'ET',
     +        5X,'RIVER',5X,'FAULT',3X,'STORAGE',2X,'RESIDUAL',
     +        3X,'STORAGE',2X,'RESIDUAL')
942   FORMAT(/1X,'COMPUTED HEADS:'/
     +    1X,'STEP NUMBER.............................',I10/
     +    1X,'STARTING STEP NUMBER....................',I10/
     +    1X,'ENDING STEP NUMBER......................',I10/
     +    1X,'ELAPSED TIME............................',1PE10.2/
     +    1X,'NUMBER OF STEP ITERATIONS...............',I10/
     +    1X,'ITERATION CLOSURE.......................',1PE10.2/
     +    1X,'MINIMUM HEAD............................',0PF10.2/
     +    1X,'MAXIMUM HEAD............................',0PF10.2//
     +    1X,2(6X,'NODE'),5(11X,'HEAD'))
944   FORMAT(1X,2I10,5F15.5)
946   FORMAT('TS 0',I11)
948   FORMAT(F15.5)
949   FORMAT(1X,I10,11(1PE10.2))
950   FORMAT(1X,'SITE: ',A20/
     +    1X,6X,'STEP',6X,'TIME',11X,'HEAD')
952   FORMAT(1X,I10,1PE10.2,0PF15.5)
954   FORMAT(/1X,'**END SIMULATION IN WOUTPUT**')
      END SUBROUTINE
