      SUBROUTINE TSTAT(IACZP,IBURN1Z,IBRNP,IEFAN,TI,TWALL,I)
C
C  THERMOSTAT MODEL
C    IACZP - A/C ON-OFF SIGNAL IN EACH ZONE
C    IBURN
C
C  I - ZONE NO.
C  IHONEY  = 0  CARRIER THERMOSTAT
C           1  HONEYWELL ROUND THERMOSTAT
C           2  T8200 HONEYWELL THERMOSTAT(GAS FURNACE)
C           3  T8300 HONEYWELL THERMOSTAT(HEAT PUMP W/ SETBACK)
C
C   MODE = 1  AUTO HTG AND CLG
C          2  CLG ONLY
C          3  HTG ONLY
C          4  NON AUTOMATIC HTG AND CLG
C
C   TIME  MUST BE TIME OF DAY(HRS) AND IS USED FOR SETBACK CALC
C  REVISION:  ADDED T8300 MODEL ON 8/14/85
C           (INTEGRAL ACTION ON HTG SETBACK RECOVERY NOT YET MODELED)
C  1/11/91 RDF REDUCED MEMORY REQMT BY REVISING SETBACK CODE AND
C              ELIMINATING RUNNING AVG OPTION WHICH WAS NEVER USED
C  3/31/92 RDF REVISED THE MULTIZONE VERSION FROM TRNSYS-LIKE W/
C              CONSTANTS IN CON ARRAY, REAL AND INTEG VAR IN VAR AND 
C              IVAR ARRAY TO SIMPLY DIMENSIONING ALL TERMS IN SINGLE-
C              ZONE TSTAT.  NOW TSTATS IN EACH ZONE CAN BE UNIQUE 
C              AND ANY LATENT ERROR SHOULD BE ELIMINATED.
C             
      PARAMETER (SMALL=1.E-37)
CMDK NWL
CMDK NZN
CMDK NZW
CMDK BIDRUN
CMDK BLKTS1
CMDK BLSTBK
CMDK BLKTST
CMDK COLLEC
CMDK DEBG
CMDK ENCBK1
CMDK TIMEB
CMDK TSTATC
      DIMENSION ANTC1(NZN),ANTH1(NZN),ANTH2(NZN),CNINTG(NZN),
     + DIFFC1(NZN),DIFFH1(NZN),DIFFH2(NZN),IACZP(NZN),
     + IACN1(NZN),IBRNP(NZN),IBUR1N(NZN),IBUR2N(NZN),IBURN1Z(NZN),
     + IBURN2(NZN),IHONEY(NZN),SBKCOF(NZN),SBKCON(NZN),
     + SBKHOF(NZN),SBKHOF2(NZN),SBKHON(NZN),SBKHON2(NZN),SETBC(NZN),
     + SETBH(NZN),SETBH2(NZN),SETC1(NZN),SETH1(NZN),SETH2(NZN),
     + SETHON2(NZN),TAC1(NZN),TAC1N(NZN),TAH1(NZN),TAH2(NZN),
     + TATC1(NZN),TATH1(NZN),TATH2(NZN),TCTAC1(NZN),
     + TCTAH1(NZN),TCTAH2(NZN),TCTC1(NZN),TCTH1(NZN),TCTH2(NZN),
     + TCTWC1(NZN),TCTWH1(NZN),TCTWH2(NZN),TSC1(NZN),TSC1N(NZN),
     + TSCT1(NZN),TSH1(NZN),TSH1N(NZN),TSH2(NZN),TSH2N(NZN),
     + TSHT1(NZN),TSHT2(NZN),TSWC1(NZN),TSWC1N(NZN),TSWH1(NZN),
     + TSWH1N(NZN),TSWH2(NZN),TSWH2N(NZN),TWCT1(NZN),TWHT1(NZN),
     + TWHT2(NZN),WALCPG(NZN)
      DIMENSION ICNT(NZN),AIRCPG(NZN),XINTG(NZN),IAC1(NZN),IACQ(NZN)
C
      LOGICAL NDEBUG
C   ---   ____  ----   PARAMETERS IN NAMELIST
C  IHONEY  - THERMOSTAT TYPE(SEE ABOVE)
C  WALCPG  - WALL COUPLING FRACTION
C  IZNCOLL - ZONE IN WHICH TO COLLECT TSTAT SENSOR TEMP IN CDATA(64)
C  CNINTG   INTEGRATOR CONSTANT USED TO MINIMIZE DROOP IN T8300 TSTAT
C  TWCT1,TWHT1,TWHT2  WALL TIME CONST,HR
C  TATC1,TATH1,TATH2   ANTICIPATOR TIME CONST,HR
C  TAH1,TAH2   INITIAL VALUES OF ANTICIPATOR TEMP,F
C ***   TSXTN  - SENSOR TIME CONSTANT (FOR AIR SENSOR IN T8200 AND 
C                T8300 TSTAT,HR
C ***   ANTXN  - ANTICIPATION
C ***   SETXN  - SET POINT
C ***   DIFFXN - DIFFERENTIAL
C ***   SETBX  - AMOUNT OF SET-BACK(POSITIVE FOR SETBACK,NEG FOR SETUP)
C ***   SBKXON - SET-BACK START TIME (0. TO 24. HR)
C ***   SBKXOF - SET-BACK STOP TIME (0. TO 24. HR)
      NAMELIST/TSTATN/
     + IHONEY,WALCPG,IZNCOLL,CNINTG,
     + TSCT1,TWCT1,TATC1,ANTC1,TAC1,
     + DIFFC1,SETC1,SETBC,SBKCON,SBKCOF,
     + TSHT1,TWHT1,TATH1,ANTH1,TAH1,
     + DIFFH1,SETH1,SETBH,SBKHON,SBKHOF,
     + TSHT2,TWHT2,TATH2,ANTH2,TAH2,
     + DIFFH2,SETH2,SETBH2,SBKHON2,SBKHOF2
C     
      DATA WALCPG,TWCT1,TATC1,TWHT1,TATH1,TWHT2,TATH2/7*NZN*0./
      DATA TAC1N/NZN*0./
      DATA IBRNI/1/,IBLW/0/
      DATA NPRMAX/500/
      DATA TAH1,TAH2/2*NZN*0./
      DATA CNINTG/NZN*0./
      DATA IRESETBK/0/
      NDEBUG=.FALSE.
      NPR=0
      OPEN(15,FILE='TAPE15',STATUS='OLD',IOSTAT=IO15)
      IF(IO15.NE.0)THEN
        WRITE(60,*) ' TSTAT: CANT OPEN TAPE15 WITH HONEYW NAMELIST'
        STOP ' TSTAT: CANT OPEN TAPE15'
      ENDIF
      READ(15,TSTATN)
      WRITE(60,TSTATN)     
      CLOSE(15)
      DO 2 J=1,NZN
      ICNT(J)=0
      IF(CNINTG(J).LT.0.)THEN
        CNINTG(J)=ABS(CNINTG(J))
        ICNT(J)=1
      ENDIF
C  - - -  INPUT CNINTG(J) AS NEG IF WANT TO BYPASS INTEG ON DIF1
C          (USED ON RAMP UP TEST TO CHECK OPERATION OF TSTAT ON CLG)
      IF (SETC1(J) .LT. SETH1(J)+DIFFH1(J)) THEN
        WRITE(60,3020)
        STOP ' ***OVERLAP BETWEEN A/C & H/P THERMOSTATS - RUN STOPPED'
      ENDIF
      WRITE(60,*)' THERMOSTAT INPUT DATA FOR ZONE ',J
      WRITE(60,2170) TSHT1(J),ANTH1(J),SETH1(J),DIFFH1(J),SETBH(J),
     +               SBKHON(J),SBKHOF(J)
      WRITE(60,2180) TSHT2(J),ANTH2(J),SETH2(J),DIFFH2(J),SETBH2(J),
     +               SBKHON2(J),SBKHOF2(J)
      WRITE(60,2190) IHONEY,WALCPG,TWCT1(J),TWHT1(J),TWHT2(J),
     +               TATC1(J),TATH1(J),TATH2(J)
      AIRCPG(J)=1.-WALCPG(J)
      IF(ITHERM.EQ.0)AIRCPG(J)=1.
    2 CONTINUE      
      IMODE=1
      IF(MODE.EQ.3)IMODE=2
      RETURN
C 
      ENTRY TSTAT1(IACZP,IBURN1Z,IBRNP,IEFAN,TI,TWALL,I)
C *** RESET IMODE FOR SPECIFIED HTG AND CLG MONTHS, IF ANY
      NPR=0
      IF ((KM.GE.MONCS).AND.(KM.LE.MONCE)) IMODE=1
      IF ((KM.GE.MONHS).OR.(KM.LE.MONHE)) IMODE=2
      RETURN
C
      ENTRY TSTAT2(IACZP,IBURN1Z,IBRNP,IEFAN,TI,TWALL,I)
C *** DETERMINE INITIAL STATES OF ALL CONTROLS
C
      TT=TI
      IF(ITHERM.GT.0)THEN
        IF(WALCPG(I).EQ.0.)THEN
          TT=.5*(TI+TWALL)
        ELSE
          TT=TI*AIRCPG(I)+TWALL*WALCPG(I)
        ENDIF
      ENDIF
      TSC1(I)=TT
      TSH1(I)=TT
      TSH2(I)=TT
      TAC1(I)=0.
      TDIF1=0.
      IF(IHONEY(I).LT.2)GO TO 146
      TIA=TI*AIRCPG(I)
      TTPW=TWALL*WALCPG(I)
      TSC1(I)=TIA
      TSH1(I)=TIA
      TSH2(I)=TIA
      TSWC1(I)=TTPW
      TSWH1(I)=TTPW
      TSWH2(I)=TTPW
      TSC1N(I)=TSC1(I)
      TSH1N(I)=TSH1(I)
      TSH2N(I)=TSH2(I)
      TSWC1N(I)=TSWC1(I)
      TSWH1N(I)=TSWH1(I)
      TSWH2N(I)=TSWH2(I)
  146 IAC1(I)=1-IACZP(I)
      IACNZ(I)=IACZP(I)
      IACN1(I)=1-IACNZ(I)
      IF(MODE.EQ.2) THEN
        IBRNP(I)=0
        IBURNNZ(I)=0
        IBUR1N(I)=0
        IBUR2N(I)=0
        IBURN1Z(I)=0
        IBURN2(I)=0
      ELSE
        IBRNP(I)=IBRNI
        IBURNNZ(I)=IBRNI
        IBUR1N(I)=IBRNI
        IBUR2N(I)=IBRNI
        IBURN1Z(I)=IBRNI
        IBURN2(I)=IBRNI
      ENDIF
      IEFAN=IBLW
      CALL SETBAK(TIME,IMODE,SETH1(I),SETH2(I),SETBH(I),SBKHON(I),
     +            SBKHOF(I),SETBH2(I),SBKHON2(I),SBKHOF2(I),TSSH1,
     +            TSSH2,SETC1(I),SETBC(I),SBKCON(I),SBKCOF(I),TSSC1)
      GO TO (390,390,400,410), MODE
  390 TSUM=TSC1(I)
      IF(IHONEY(I).EQ.1)TSUM=TSC1(I)+TAC1(I)
      IF(IHONEY(I).GE.2)THEN
        TSENS=TSC1N(I)+TSWC1N(I)
        TDIF=TSENS-TSSC1
        TSM=TDIF+TAC1N(I)
        GO TO 392
      ENDIF
      TSM=TSUM-TSSC1
  392 IF(TSM.GE.DIFFC1(I))IACZP(I)=1
      IF(TSM.LE.0.)IACZP(I)=0
      IAC1(I)=1-IACZP(I)
      IF(MODE.GT.1) GO TO 420
 400  CONTINUE
C ***   FURNACE CANNOT BE ON IF A/C IS ON . . .
      TSUM1=TSH1(I)
      IF(IHONEY(I).EQ.1)TSUM1=TSH1(I)+TAH1(I)
      IF(IHONEY(I).GE.2)THEN
        TSENS=TSH1N(I)+TSWH1N(I)
        TDIF=TSENS-TSSH1
        TSM=TDIF+TAH1N(I)
        GO TO 402
      ENDIF
      TSM=TSUM1-TSSH1
  402 IF(TSM.GE.DIFFH1(I))IBURN1Z(I)=0
      IF(TSM.LE.0.)IBURN1Z(I)=IAC1(I)
      TSUM2=TSH2(I)
      IF(IHONEY(I).EQ.1)TSUM2=TSH2(I)+TAH2(I)
      IF(IHONEY(I).GE.2)THEN
        TSENS=TSH2N(I)+TSWH2N(I)
        TDIF=TSENS-TSSH2
        TSM=TDIF+TAH2N(I)
        GO TO 404
      ENDIF
      TSM=TSUM2-TSSH2
  404 IF(TSM.GE.DIFFH2(I))IBURN2(I)=0
      IF(TSM.LE.0.)IBURN2(I)=IAC1(I)
      IACP= (1-IBURN1Z(I))*IBURN2(I)
      IBRNP(I)= IBURN1Z(I)+IACP
      GO TO 420
410   IF((KM.GE.MONCS) .AND. (KM.LE.MONCE)) GO TO 390
      IF((KM.GE.MONHS) .OR. (KM.LE.MONHE)) GO TO 400
  420 CONTINUE
      RETURN
C
      ENTRY TSTAT3(IACZP,IBURN1Z,IBRNP,IEFAN,TI,TWALL,I)
C  ***  COMPUTE TIME CONSTANT TERMS
      TCTC1(I) = DTIME/TSCT1(I)
      TCTH1(I) = DTIME/TSHT1(I)
      TCTH2(I)= DTIME/TSHT2(I)
      IF(IHONEY(I)-1)430,422,422
  422 TCTAC1(I)=DTIME/TATC1(I)
      TCTAH1(I)=DTIME/TATH1(I)
      TCTAH2(I)=DTIME/TATH2(I)
      IF(IHONEY(I).EQ.1)GO TO 430
      TCTWC1(I)=DTIME/TWCT1(I)
      TCTWH1(I)=DTIME/TWHT1(I)
      TCTWH2(I)=DTIME/TWHT2(I)
      XINTG(I)=CNINTG(I)*DTIME
  430 CONTINUE
      RETURN
C
      ENTRY TSTAT4(IACZP,IBURN1Z,IBRNP,IEFAN,TI,TWALL,I)
C
C FOR RUNNING 121486 BELOW!!!!!(SEE ALSO TSTATD)
      IF(IDRUN.EQ.'1214'.AND.IRESETBK.EQ.0)THEN
        IF(TIME.GT.12.00)THEN
C  SET BACK IN ZONES 1 AND 3 AFTER NOON OF 1ST DAY OF 121486 RUN!
          IRESETBK=1        
          SBKHON(1)=0.
          SBKHON(3)=0.
          SETBH(1)=40.
          SETBH(3)=40.
          SBKHOF(1)=24.
          SBKHOF(3)=24.
          SETBH2(1)=40.
          SETBH2(3)=40.
          SBKHOF2(1)=24.
          SBKHOF2(3)=24.  
        ENDIF
      ENDIF
C *** FIND NEW SET POINTS FOR ALL THERMOSTATS
      CALL SETBAK(TIME,IMODE,SETH1(I),SETH2(I),SETBH(I),SBKHON(I),
     +            SBKHOF(I),SETBH2(I),SETHON2(I),SBKHOF2(I),TSSH1,
     +            TSSH2,SETC1(I),SETBC(I),SBKCON(I),SBKCOF(I),TSSC1)
C      
      IAC1(I)=1-IACZP(I)
      TT = TI
      IF(ITHERM.GT.0.)THEN
        IF(WALCPG(I).EQ.0.)THEN
          TT=.5*(TI+TWALL)
        ELSE
          TT=TI*AIRCPG(I)+TWALL*WALCPG(I)
        ENDIF
      ENDIF
      IF(IMODE.GT.1)GOTO 690
C  ****  COOLING
      IF(IHONEY(I).EQ.0)THEN
C             CARRIER THERMOSTAT      
        TSC1N(I) = TCTC1(I)*(TT-TSC1(I)+ANTC1(I)*IAC1(I))+TSC1(I)
        TSUM=TSC1N(I)
        TSM=TSUM-TSSC1
      ELSEIF(IHONEY(I).EQ.1)THEN
C             HONEYWELL ROUND THERMOSTAT      
        TSC1N(I)=TCTC1(I)*(TT-TSC1(I))+TSC1(I)
        TAC1N(I)=TCTAC1(I)*(ANTC1(I)*IACZP(I)-TAC1(I))+TAC1(I)
        TSUM=TSC1N(I)+TAC1N(I)
        TSM=TSUM-TSSC1
      ELSE
C              HONEYWELL T8200 AND T8300 THERMOSTATS
        TSC1N(I)=TCTC1(I)*(TI*AIRCPG(I)-TSC1(I))+TSC1(I)
        TSWC1N(I)=TCTWC1(I)*(TWALL*WALCPG(I)-TSWC1(I))+TSWC1(I)
        TAC1N(I)=TCTAC1(I)*(ANTC1(I)*IACZP(I)-TAC1(I))+TAC1(I)
        TSENS=TSC1N(I)+TSWC1N(I)
        TDIF=TSENS-TSSC1
C  INTEGRATE TEMP DIF TO MINIMIZE DROOP BY INCREASING CYCLE RATE
        IF(IHONEY(I).EQ.2)THEN
          TDIF1=0.
        ELSE
          IF(ICNT(I).EQ.0)THEN
            TDIF1=TDIF*XINTG(I)+TDIF1
            IF(TDIF1.GT.3.) TDIF1=3.
            IF(TDIF1.LT.0.) TDIF1=0.
          ELSE
            TDIF1=0.
          ENDIF
        ENDIF
        TSM=TDIF1+TAC1N(I)+TDIF
      ENDIF
      IF(ICOLL.NE.0.AND.I.EQ.IZNCOLL)CDATA(64) = TSM
      IF(TSM.GE.DIFFC1(I)) THEN
        IACNZ(I)=1
        IBURNNZ(I)=0
      ENDIF
      IF(TSM.LE.0.)IACNZ(I)=0
      IEFAN=IACNZ(I)
      IF(NDEBUG) WRITE(60,510) TDIF,TAC1N(I),TDIF1
      IF(MODE.GT.1)GO TO 710
      IACN1(I)=1-IACNZ(I)
C  ***  HEATING
  690 IF(IHONEY(I).EQ.0)THEN
C              CARRIER THERMOSTAT EQUATIONS 
        TSH1N(I) = TCTH1(I)*(TT-TSH1(I)+ANTH1(I)*IBURN1Z(I))+TSH1(I)
        TSH2N(I)=TCTH2(I)*(TT-TSH2(I)+ANTH2(I)*IBURN2(I))+TSH2(I)
        TSUM1=TSH1N(I)
        TSUM2=TSH2N(I)
        TSM1=TSUM1-TSSH1
      ELSE
        TAH1N(I)=TCTAH1(I)*(ANTH1(I)*IBURN1Z(I)-TAH1(I))+TAH1(I)
        IF(TAH1N(I).LT.0.)TAH1N(I)=0.
        TAH2N(I)=TCTAH2(I)*(ANTH2(I)*IBURN2(I)-TAH2(I))+TAH2(I)
        IF(TAH2N(I).LT.0.)TAH2N(I)=0.
        IF(IHONEY(I).EQ.1)THEN
C              HONEYWELL ROUND THERMOSTAT
          TSH1N(I)=TCTH1(I)*(TT-TSH1(I))+TSH1(I)
          TSH2N(I)=TCTH2(I)*(TT-TSH2(I))+TSH2(I)
          TSUM1=TSH1N(I)+TAH1N(I)
          TSUM2=TSH2N(I)+TAH2N(I)
          TSM1=TSUM1-TSSH1
          TSM2=TSUM2-TSSH2
        ELSE
C             HONEYWELL T8200 AND T8300 THERMOSTATS
          TSH1N(I)=TCTH1(I)*(TI*AIRCPG(I)-TSH1(I))+TSH1(I)
          IF(TSH1N(I).LT.SMALL)TSH1N(I)=0.
          TSWH1N(I)=TCTWH1(I)*(TWALL*WALCPG(I)-TSWH1(I))+TSWH1(I)
          IF(TSWH1N(I).LT.SMALL)TSWH1N(I)=0.
          TSH2N(I)=TCTH2(I)*(TI*AIRCPG(I)-TSH2(I))+TSH2(I)
          IF(TSH2N(I).LT.SMALL)TSH2N(I)=0.
          TSWH2N(I)=TCTWH2(I)*(TWALL*WALCPG(I)-TSWH2(I))+TSWH2(I)
          IF(TSWH2N(I).LT.SMALL)TSWH2N(I)=0.
          TSENS=TSH1N(I)+TSWH1N(I)
          TDIF=TSENS-TSSH1
          IF(IHONEY(I).EQ.2)THEN
            TDIF1=0.
          ELSE
            IF(ICNT(I).EQ.0)THEN
              TDIF1=TDIF*XINTG(I)+TDIF1
              IF(TDIF1.GT.3.) TDIF1=3.
              IF(TDIF1.LT.0.) TDIF1=0.
            ELSE
              TDIF1=0.
            ENDIF
          ENDIF
          TSM1=TDIF1+TAH1N(I) + TDIF
          TSENS=TSH2N(I)+TSWH2N(I)
          TDIF=TSENS-TSSH2
          IF(IHONEY(I).EQ.2)THEN
            TDIF1=0.
          ELSE
            IF(ICNT(I).EQ.0)THEN
              TDIF1=TDIF*XINTG(I)+TDIF1
              IF(TDIF1.GT.3.) TDIF1=3.
              IF(TDIF1.LT.0.) TDIF1=0.
            ELSE
              TDIF1=0.
            ENDIF
          ENDIF
          TSM2=TDIF1+TAH2N(I) + TDIF
        ENDIF
      ENDIF
C - - - SET STATE OF THERMOSTAT SWITCH
      IF(TSM1.GE.DIFFH1(I))IBUR1N(I)=0
      IF(TSM1.LE.0.)IBUR1N(I)=IACN1(I)
      IF(ICOLL.NE.0.AND.IZNCOLL.EQ.I) CDATA(64)=TSM1
C      
      IF(IHONEY(I).LT.2)TSM2=TSUM2-TSSH2
      IF(TSM2.GE.DIFFH2(I))IBUR2N(I)=0
      IF(TSM2.LE.0.)IBUR2N(I)=IACN1(I)
C      
710   IACP=(1-IBUR1N(I))*IBUR2N(I)
      IBURNNZ(I)=IACP+IBUR1N(I)
      IF(NDEBUG)THEN
        WRITE(60,*)' TIME,IBURNN,TSSH1= ',TIME,IBURNNZ(I),TSSH1
        WRITE(60,*)'      TAH1N,TSM1,DIFFH1= ',TAH1N(I),TSM1,DIFFH1(I)
        WRITE(60,*)'      TSENS,TI,TWALL= ',TSENS,TI,TWALL
        WRITE(60,*)' '
      ENDIF
      RETURN
C
      ENTRY TSTAT5(IACZP,IBURN1Z,IBRNP,IEFAN,TI,TWALL,I)
C  ***  RESET VALUES
      TSC1(I) = TSC1N(I)
      TSH1(I)=TSH1N(I)
      TSH2(I)=TSH2N(I)
      TAC1(I)=TAC1N(I)
      TSWC1(I)=TSWC1N(I)
      TAH1(I)=TAH1N(I)
      TAH2(I)=TAH2N(I)
      TSWH1(I)=TSWH1N(I)
      TSWH2(I)=TSWH2N(I)
      IBURN1Z(I)=IBUR1N(I)
      IBURN2(I)=IBUR2N(I)
      IBRNP(I)=IBURNNZ(I)
      IACQ(I)=IACZP(I)
      IACZP(I)=IACNZ(I)
      IF(.NOT.NDEBUG)RETURN
      GO TO 720
C      
      ENTRY TSTAT6(IACZP,IBURN1Z,IBRNP,IEFAN,TI,TWALL,I)
C *** WRITE VALUES OF THERMOSTAT PARAMETERS      
  720 NPR=NPR+1
      IF(NPR.GT.NPRMAX)RETURN
      IF(NPR.LE.1.AND.MODE.NE.2)WRITE(60,3001)
      IF(NPR.LE.1.AND.MODE.EQ.2)WRITE(60,3002)
      IF(MODE.NE.2)THEN
      WRITE(60,3000)TIME,TI,TWALL,TSH1(I),TSH2(I),TAH1(I),TAH2(I),
     +               TSWH1(I),TSWH2(I),IBURN1Z(I),IBURN2(I),IBRNP(I)
      ELSE
        WRITE(60,3003)TIME,TI,TWALL,TSC1(I),TAC1(I),TSWC1(I),
     +                 IACQ(I),IACNZ(I)
      ENDIF
      RETURN
  500 FORMAT(1X,'TAC1N= ',G13.5)
  510 FORMAT(1X,'TSTAT - TDIF,TAC1N,TDIF1 ',3E11.3)
 2160 FORMAT(1H0,17X,'THERMOSTAT SETTING',11X,'SET BACK'/14X,
     +       'T/C    ANTC   SET   DIFF   AMT    ON    OFF'/
     +       ' COOLING   ',F9.5,F5.2,F7.1,F6.3,F7.1,2F7.2)
 2170 FORMAT(' HEAT #1   ',F9.5,F5.2,F7.1,F6.3,F7.1,2F7.2)
 2180 FORMAT (' HEAT #2   ',F9.5,F5.2,F7.1,F6.3,F7.1,2F7.2)
 2190 FORMAT(1X,' IHONEY= ',I5,' WALL COUPLING= ',G13.5/
     +1X,'WALL SENSOR TIME CONST(CLG,HTG1,HTG2)= ',3G13.5/
     +1X,'ANTICIPATOR TIME CONST(CLG,HTG1,HTG2)= ',3G13.5)
 3000 FORMAT(1X,F10.3,8(1X,F6.2),3(3X,I4))
 3001 FORMAT(1H1,3X,4HTIME,6X,2HTI,4X,5HTWALL,2X,4HTSH1,3X,4HTSH2,
     +3X,4HTAH1,3X,4HTAH2,3X,5HTSWH1,2X,5HTSWH2,2X,6HIBURN1,1X,
     +6HIBURN2,1X,5HIBURN)
 3002 FORMAT(1H1,3X,4HTIME,6X,2HTI,4X,5HTWALL,2X,4HTSC1,3X,4HTAC1,
     +3X,5HTSWC1,7X,3HIAC,9X,
     +4HIACN)
 3003 FORMAT(1X,F10.3,5(1X,F6.2),2(3X,I4))
 3020 FORMAT(' ***OVERLAP BETWEEN A/C & H/P THERMOSTATS - RUN STOPPED')
      END
      SUBROUTINE SETBAK(TIME,IMODE,SETH1,SETH2,SETBH,SBKHON,SBKHOF,
     +                  SETBH2,SBKHON2,SBKHOF2,
     +                  TSSH1,TSSH2,SETC1,SETBC,SBKCON,SBKCOF,TSSC1)
C  SETBACK/SETUP CONTROL MODEL     
C           NOTE:: THE CODE BELOW FROM MULTIZONE SP43 TSTAT
C                  ELIMINATES THE NEED FOR LARGE SETBHN,SETBCN ARRAYS      
      IF(IMODE.EQ.1)THEN
C   **********  COOLING  ***********      
        IF(SBKCON.EQ.SBKCOF)THEN
C                                   ***NO SETBACK 
          TSSC1=SETC1 
        ELSE
C                                   ***SETBACK SOMETIME DURING THE DAY
          SETBCN=0. 
          IF(SBKCON.GT.SBKCOF)THEN
C                                   ***SETBACK STARTS BEFORE MIDNIGHT 
            IF(TIME.GT.SBKCON)SETBCN=SETBC
            IF(TIME.LE.SBKCOF)SETBCN=SETBC
          ELSE
C                                   ***SETBACK STARTS AFTER MIDNIGHT
            IF(SBKCON.LE.TIME.AND.TIME.LE.SBKCOF)SETBCN=SETBC 
          ENDIF 
          TSSC1=SETC1-SETBCN
        ENDIF 
      ELSE
C        ********* HEATING **********      
        ICODE=0
        IF(SBKHON.NE.SBKHOF.AND.SBKHON2.EQ.SBKHOF2)THEN
          ICODE=1
        ELSEIF(SBKHON.EQ.SBKHOF.AND.SBKHON2.NE.SBKHOF2)THEN
          ICODE=2
        ELSE
C              BOTH SETBACKS AVAILABLE!        
          ICODE=3
        ENDIF
        IF(ICODE.EQ.0)THEN
          TSSH1=SETH1
          TSSH2=SETH2
          GO TO 30
        ENDIF
        IF(ICODE.EQ.2)GO TO 10
C              1ST SETBACK!!!!!!!!!!!        
C                                   ***SETBACK SOMETIME DURING THE DAY
        SETBHN=0. 
        IF(SBKHON.GT.SBKHOF)THEN
C                                   ***SETBACK STARTS BEFORE MIDNIGHT 
          IF(TIME.GT.SBKHON)SETBHN=SETBH
          IF(TIME.LE.SBKHOF)SETBHN=SETBH
        ELSE
C                                   ***SETBACK STARTS AFTER MIDNIGHT
          IF(SBKHON.LE.TIME.AND.TIME.LE.SBKHOF)SETBHN=SETBH 
        ENDIF 
        IF(ICODE.EQ.1)GO TO 20
C              2ND SETBACK!!!!!!!!!!!          
   10   CONTINUE          
C                                   ***SETBACK SOMETIME DURING THE DAY
        IF(SBKHON2.GT.SBKHOF2)THEN
C                                   ***SETBACK STARTS BEFORE MIDNIGHT 
          IF(TIME.GT.SBKHON2)SETBHN=SETBH2
          IF(TIME.LE.SBKHOF2)SETBHN=SETBH2
        ELSE
C                                   ***SETBACK STARTS AFTER MIDNIGHT
          IF(SBKHON2.LE.TIME.AND.TIME.LE.SBKHOF2)SETBHN=SETBH2 
        ENDIF 
   20   CONTINUE          
        TSSH1=SETH1-SETBHN
        TSSH2=SETH2-SETBHN
      ENDIF
   30 CONTINUE      
      RETURN
      END
