      SUBROUTINE TSTATD(ICODE,IAC,IBURN1,IBURN,IEFAN,IDBG)
C
C  DRIVER FOR MULTIZONE THERMOSTATS.
C   ICODE  =1, TSTAT CALC; =2, TSTAT1 CALC; =3,TSTAT2 CALC;
C          =4, TSTAT3 CALC; =5, TSTAT4 CALC; =6, TSTAT5 CALC;
C          =7, TSTAT6 CALC(EXECUTED CONDITIONALLY FROM TSTAT5)
C   ON-OFF INDICATORS - - - - -
C     IAC   AIR CONDITIONER
C     IBURN1  1ST STAGE  HEATING
C     IBURN   1ST AND 2ND STAGE  HEATING
C     IEFAN  BLOWER STATE (WILL BE CYCLED ON WITH AIR CONDITIONER)
C
CMDK NWL
CMDK NZN
CMDK NZW
CMDK BIDRUN
CMDK BLKTS1
CMDK BLKTST
CMDK ENCBK1
CMDK HILIMB
CMDK ITM
CMDK TEMP1
CMDK TIMEB
CMDK TSTATC
CMDK TSTB2
CMDK TSTB3
CMDK TSTB4
      INTEGER  IACZP(NZN),IACZQ(NZN),IBRN1Z(NZN), 
     +         IBRNP(NZN),IBRNQ(NZN),IACZPQ(NZN),IBRNPQ(NZN)
      REAL TIMEOF(NZN),TIMEON(NZN),TIME0(NZN)
     +,TSSH1L(NZN)
      LOGICAL FIRST
      DATA FIRST/.TRUE./,IDMP/0/,NPR/0/,NPRMAX/0/,IDMPMX/50/
      DATA TIME0/NZN*0./,TIMEOF/NZN*0./TIMEON/NZN*0./
      DATA TSSH1L/NZN*0./
      DATA IDUMPQ/0/,IRESETBK/0/
C
      IDBGSV=IDBG
C  FOR RUNNING 121486 BELOW!!!!!!!!!!!!!!
      IF(IDRUN.EQ.'1214'.AND.IRESETBK.EQ.0)THEN
        IF(TIME.GT.12.00)THEN
C  SETBACK IN ZONES 1 AND 3 AFTER NOON OF 1ST DAY OF 121486 RUN!
          IRESETBK=1
          IDUMPZ(1)=0
          IDUMPZ(3)=0
          ICTRLZ(1)=0
          ICTRLZ(3)=0
          IBRN(1)=0
          IBRN(3)=0
          IBRNQ(1)=0
          IBRNQ(3)=0
          IBRNP(1)=0
          IBRNP(3)=0
          IBRNPQ(1)=0
          IBRNPQ(3)=0
          IACZ(1)=0
          IACZ(3)=0
          IACZQ(1)=0
          IACZQ(3)=0
          ITIM=0
          ENDIF
        ENDIF
C     IF(TIME.GT.7.00)IDBG=1
C     IF(TIME.GT.7.31)STOP 'TSTATD AT 7.31 HR'
C  SEE IDBG SET AT LABEL 22 ALSO!!!!!!!!!!
C
      IF(ICODE.EQ.1)THEN
        CALL TSTAT(IACZP,IBRN1Z,IBRNP,IEFAN,TI,TWALL,NRM)
C  INITIAL VALUES OF ANTICIPATOR TEMPS ARE READ IN TSTAT ABOVE
        GO TO 34
        ENDIF
      IF(ICODE.EQ.2)THEN
        CALL TSTAT1(IACZP,IBRN1Z,IBRNP,IEFAN,TI,TWALL,NRM)
        GO TO 34
        ENDIF
C  ICODE GT 2 BELOW
      IF(FIRST)THEN
        IF(IMODE.EQ.1)THEN
C  COOLING OR HTG/COOLING
          IAC=1
          IACLS=IAC
          IBURN1=0
          IBURN=0
          IBRNLS=IBURN
          IEFAN=1
          DO 2 I=1,NZN
          IF(ICTRLZ(I).EQ.1)THEN
            IACZ(I)=1
            IACZP(I)=1
            IACZPQ(I)=1
          ELSE
            IACZ(I)=0
            IACZP(I)=0
            IACZPQ(I)=0
            ENDIF
          IBRN(I)=0
          IBRNP(I)=0
   2      IBRNPQ(I)=0
        ELSE IF(IMODE.EQ.2)THEN
C  HEATING ONLY
          IAC=0
          IACLS=IAC
          IBURN1=1
          IBURN=1
          IBRNLS=IBURN
          IEFAN=0
          DO 3 I=1,NZN
          IACZ(I)=0
          IACZP(I)=0
          IACZPQ(I)=0
          IF(ICTRLZ(I).EQ.1)THEN
            IBRN(I)=1
            IBRNP(I)=1
            IBRNPQ(I)=1
          ELSE
            IBRN(I)=0
            IBRNP(I)=0
            IBRNPQ(I)=0
            ENDIF
   3      CONTINUE
          ENDIF
        ENDIF
C
C  - - - - - - - - - - - TOP OF ZONE LOOP HERE - - - - - - - - - - - - -
      IF(ICODE.EQ.5)THEN
        IACN=0
        IBURNN=0
        ENDIF
C
      DO 10 J=1,NROOMS
      NRM=NRMA(J)
      IF(ICTRLZ(NRM).EQ.0)GO TO 10
      TIZ=TI(NRM)
      TWZ=TWSTAT(NRM)
      IF(ICODE.EQ.3)THEN
C  SET INITIAL STATE OF ALL THERMOSTAT VARIABLES
C    NOTE!!!IBRN IS USED TO SET DUCT DAMPERS TO ZONES
C           IBRNP IS SAME AS IBRN EXCEPT IBRN MAY GET CHANGED BY DUMPING.
C           SIMILARY WITH IACZ AND IACZP
        CALL TSTAT2(IACZP,IBRN1Z,IBRNP,IEFAN,TIZ,TWZ,NRM)
        IF(IDBG.NE.0)WRITE(60,501)NRM,IACZP(NRM),IBRN1Z(NRM),
     +    IBRNP(NRM),IEFAN,TIZ,TWZ
        IBRN(NRM)=IBRNP(NRM)
        IACZ(NRM)=IACZP(NRM)
        GO TO 10
        ENDIF
C
      IF(ICODE.EQ.4)THEN
C                 SET VALUES FOR TIME CONSTANTS
        CALL TSTAT3(IACZP,IBRN1Z,IBRNP,IEFAN,TI,TWALL,NRM)
        GO TO 10
        ENDIF
C        
      IF(ICODE.EQ.5)THEN
C                 SWITCH TSTAT IF NEEDED!      
        CALL TSTAT4(IACZP,IBRN1Z,IBRNP,IEFAN,TIZ,TWZ,NRM)
        IF(IDBG.NE.0)THEN
          IF(NRM.EQ.1)THEN
            WRITE(60,503)NRM,IBRNP(NRM),IBURNNZ(NRM),TIZ,TWZ
          ELSE IF(NRM.EQ.2)THEN
            WRITE(60,508)NRM,IBRNP(NRM),IBURNNZ(NRM),TIZ,TWZ
          ELSE IF(NRM.EQ.3)THEN
            WRITE(60,509)NRM,IBRNP(NRM),IBURNNZ(NRM),TIZ,TWZ
            ENDIF
          ENDIF
C  TURN ON FURNACE OR A/C IF ANY ZONE TSTAT IS ON!          
C  IBRNHI IS HILIMIT SWITCH CODE
        IF(IBURNNZ(NRM).EQ.1.AND.IBRNHI.EQ.1)THEN
          IBURNN=1
        ENDIF
        IF(IACNZ(NRM).EQ.1)IACN=1
C  PRINT TIME AT SETPOINT CHANGES DURING HEATING
        IF(TSSH1.NE.TSSH1L(NRM))THEN
          TSSH1L(NRM)=TSSH1
          TODP=(TIME/24.-INT(TIME/24.))*24.
          IF(IDBG.NE.0)WRITE(60,513)TSSH1,NRM,TIME,TODP
          ENDIF
        GO TO 10
        ENDIF
C
      IF(ICODE.EQ.6)THEN
C  RESET VALUES
        CALL TSTAT5(IACZP,IBRN1Z,IBRNP,IEFAN,TIZ,TWZ,NRM)
        IF(IDBG.NE.0)WRITE(60,504)NRM,IACZP(NRM),IBRN1Z(NRM),
     +    IBRNP(NRM),IEFAN
        IACZ(NRM)=IACZP(NRM)
        IBRN(NRM)=IBRNP(NRM)
        GO TO 10
        ENDIF
C
      IF(ICODE.EQ.7)THEN
C  WRITE VALUES
        CALL TSTAT6(IACZP,IBRN1Z,IBRNP,IEFAN,TIZ,TWZ,NRM)
        ENDIF
   10 CONTINUE
C - - - - - - - - - BOTTOM OF ZONE LOOP HERE - - - - - - - - - -
      IF(ICODE.LT.6.OR.ICODE.EQ.7)GO TO 30
C  ICODE = 6 HERE !!!
C  NOW SET IAC, IBRN1,IBURN,IEFAN IF NOT DUMPING RESIDUAL HEAT/COOL
      IEB=0
      IEQ=0
      IAC=0
      IBURN1=0
      IBURN=0
      DO 12 J=1,NROOMS
      NRM=NRMA(J)
      IF(ICTRLZ(NRM).EQ.0)GO TO 12
      IF(IACZP(NRM).EQ.1)IAC=1
C  NOTE!!!!IBURN1 IS PROBABLY NEVER USED UNLESS TWO-STAGE FURNACE
C     FURNACE OPERATION IS MODELED (NOT CHECKED OUT BY BATTELLE)
      IF(IBRN1Z(NRM).EQ.1)IBURN1=1
      IF(IBRNP(NRM).EQ.1)IBURN=1
      IF(IDBG.NE.0)WRITE(60,512)NRM,IAC,IBURN,TIME
   12 CONTINUE
      IF(IBRNLS.NE.IBURN.OR.IACLS.NE.IAC)THEN
         IEB=1
         IEQ=1
         ENDIF
C
      DO 16 J=1,NROOMS
      NRM=NRMA(J)
      IF(ICTRLZ(NRM).EQ.0)GO TO 16
C
      NPR=NPR+1
      IF(NPR.LE.NPRMAX)THEN
        IF(IBRNPQ(NRM).NE.IBRNP(NRM)
     +                         .OR.IACZPQ(NRM).NE.IACZP(NRM))THEN
          DTM=TIME-TIME0(NRM)
          IF(IBRNPQ(NRM).EQ.0)THEN
C   BURNER TURNED ON
              TIMEOF(NRM)=DTM
              CYTIME=TIMEON(NRM)+TIMEOF(NRM)
              CPH=1/CYTIME
              PCTON=100.*TIMEON(NRM)/CYTIME
              TIMP=TIME*60.
              CYT=CYTIME*60.
              TIMOF=TIMEOF(NRM)*60.
              IF(IDBG.NE.0)WRITE(60,510)NRM,TIME,TIMP,PCTON,CPH,
     +                     CYT,TIMOF
              TIME0(NRM)=TIME
          ELSE
C   BURNER TURNED OFF
              TIMEON(NRM)=DTM
              TIME0(NRM)=TIME
              TIMP=TIME*60.
              TIMON=TIMEON(NRM)*60.
              IF(IDBG.NE.0)WRITE(60,511)NRM,TIMP,TIMON
              ENDIF
           ENDIF
          ENDIF
   16 CONTINUE
C
C  SET DAMPERS TO DUMP ZONES IF NECESSARY !!!!!!!!!!!!!
C    NOTE:::DUMP ZONES ARE SET BY VALUES IN IDUMPZ INPUT IN STRRUN
C           IN NAMELIST "CONTROL".
C  CHECK TO SEE IF ALL TSTATS ARE OFF AND BLOWER IS ON.
C  IF SO, OPEN DAMPERS TO DUMP ZONES.
      IDUMP=0
      IF(IEFAN.EQ.1)THEN
          IBZ=0
          DO 18 J=1,NROOMS
          NRM=NRMA(J)
C                                      IS THIS ZONE CONDITIONED?
          IF(ICTRLZ(NRM).EQ.0)GO TO 18
C                                      IS ZONE HEATING TSTAT ON?
          IF(IBRNP(NRM).EQ.1)IBZ=IBZ+1
C                                      IS ZONE COOLING TSTAT ON?
          IF(IACZP(NRM).EQ.1)IBZ=IBZ+1
   18     CONTINUE
          IF(IBZ.EQ.0)THEN
C                                      OPEN DAMPERS TO DUMP ZONES.
              IDUMP=1
              DO 20 J=1,NROOMS
              NRM=NRMA(J)
              IF(ICTRLZ(NRM).EQ.0)GO TO 20
              IF(IDUMPZ(NRM).EQ.0)GO TO 20
              IBRN(NRM)=1
              IACZ(NRM)=1
   20         CONTINUE
              IF(IDBG.NE.0.AND.IDMP.LE.IDMPMX)THEN
                IDMP=IDMP+1
                WRITE(60,507) TIME
              ENDIF
          ENDIF
      ENDIF
      IF(.NOT.FIRST)THEN
C  CHECK TO SEE IF TSTAT IN ANY ZONE CHANGED STATE OR BLWR CHANGED STATE.
C    IF SO, CALL DCTFLW(IFLG=1)SO THAT DUCT AND INTERZONE FLOWS GET SET
          IFLG=0
          DO 22 J=1,NROOMS
          NRM=NRMA(J)
          IF(ICTRLZ(NRM).EQ.0)GO TO 22

C  SET INDICE IF DUMPING NOW OR IN PREV TIMESTEP!
          IDMPQ=0
          IF(IDUMP.EQ.1.OR.IDUMPQ.EQ.1)IDMPQ=1

C  HAS ZONE DAMPER CHANGED STATE?
          IF((IBRN(NRM).NE.IBRNQ(NRM)).OR.(IACZ(NRM).NE.IACZQ(NRM)))THEN
            IFLG=1
C  SET INDICATOR (IEQ) FOR CHANGE IN STATE OF ZONE DAMPERS
C  IF DUMPING NOW OR IN PREV TIMESTEP, SET IEQ = 2 SO EVENT PROCESSOR
C  WILL KNOW THAT THIS CHANGE IN STATE WASNT CHANGED BY A THERMOSTAT!
            IEQ=1
            IF(IDMPQ.EQ.1)IEQ=2
          ENDIF

C   DONT CHANGE DUCT FLOWS UNTIL BLOWER COMES ON
          IU1=1
          IF(IEFAN.EQ.IBLWPQ.AND.IEFAN.EQ.0)IU1=0

C   CHANGE DUCT FLOWS WHEN BLOWER GOES OFF
          IF(IEFAN.NE.IBLWPQ)THEN
             IF(IU1.EQ.1)IFLG=1
             IEQ=1
             ENDIF
C   CHANGE DUCT FLOWS WHEN ANY ZONE CALLS FOR HEAT OR SHUTS OFF HEAT
          IF((IBRN(NRM).NE.IBRNQ(NRM)).OR.(IACZ(NRM).NE.IACZQ(NRM)))THEN
             IF(IU1.EQ.1)IFLG=1
             IEQ=1
             ENDIF
   22     CONTINUE
C
C          IDBGSS=IDBG
C         IDBG=IDBGSV
          IF(IFLG.EQ.1)CALL DCTFLW(IDBG)
C          IDBG=IDBGSS
C
          ENDIF
C  PRINT EVENTS AND TI VALUES ?
          IF(IEQ.EQ.1.OR.IEB.EQ.1)CALL EVENTP(IEB,IEQ,IBRNP,IACZP,
     +      IBRN,IACZ)
          IF(IDBG.NE.0)THEN
              WRITE(60,505)(IBRN(K),K=1,NROOMS)
              WRITE(60,506)(IBRNQ(K),K=1,NROOMS)
              ENDIF
   30 CONTINUE

C  SAVE OLD VALUES OF STATE CODES
      DO 32 J=1,NROOMS
      NRM=NRMA(J)
      IF(ICTRLZ(NRM).EQ.0)GO TO 32
C  SUPPLY DAMPER STATE CODES
      IBRNQ(NRM)=IBRN(NRM)
      IACZQ(NRM)=IACZ(NRM)
C  TSTAT STATE CODES
      IBRNPQ(NRM)=IBRNP(NRM)
      IACZPQ(NRM)=IACZP(NRM)
   32 CONTINUE
C  BLOWER STATE CODE
      IBLWPQ=IEFAN
C  BURNER (GAS VALVE) STATE CODE
      IBRNLS=IBURN
C  A/C STATE CODE
      IACLS=IAC
      IDUMPQ=IDUMP
      FIRST=.FALSE.
   34 IDBG=IDBGSV
      RETURN
  501 FORMAT(10X,'NRM,IACZP,IBRN1Z,IBRNP,IEFAN= ',5I5/
     +10X,'TIZ,TWZ= ',2G13.5)
  503 FORMAT(1X,'NRM,IBRNP(OLD,NEW),TA,TW= ',3I3,2G13.5)
  504 FORMAT(10X,'NRM,IACZ,IBRN1Z,IBRNP,IEFAN= ',5I5)
  505 FORMAT(10X,'NEW IBRN S= ',5I5)
  506 FORMAT(10X,'OLD IBRN S= ',5I5)
  507 FORMAT(1X,20(1H$),'TSTATD:DUMPING RESIDUAL HEAT/COOL AT TIME ',
     +G13.5,' HR')
  508 FORMAT(3X,'NRM,IBRNP(OLD,NEW),TA,TW= ',3I3,2G13.5)
  509 FORMAT(5X,'NRM,IBRNP(OLD,NEW),TA,TW= ',3I3,2G13.5)
  510 FORMAT(1H0,20(1H*),' THERMOSTAT ON EVENT IN ZONE ',I4/
     +21X,'TIME(HR & MIN),PCTON,CPH= ',4G13.5/
     +21X,'CYCLE TIME,TIME OFF(MIN)= ',2G13.5/)
  511 FORMAT(1H0,25(1H*),' THERMOSTAT OFF EVENT IN ZONE ',I4/
     +26X,'TIME(MIN),TIME ON(MIN)= ',2G13.5/)
  512 FORMAT(1H0,'TSTATD:*****CONTROL TO SPACE CONDITIONER*****'/
     +7X,'NRM,IAC,IBURN,TIME= ',3I5,2X,F12.5)
  513 FORMAT(1X,'TSTATD:TSSH1= ',G13.5,' IN ZONE ',I4,' AT TIMYR,'
     +,'TIMDAY= ',2G13.5)
      END
