      SUBROUTINE ENDLP1(IDAYM,IREP, IRH, ISWING,
     +NROOMS, POWA, RHA, RHDIR, RHIO, RLOADL, RLOADS, TDIR, TIMAX,
     +TIMAXN, TIMIN, TIO, TS, IDBG)
C      SUBROUTINE ENDLP1(COMFTT, DTIM1N, IREP, IRH, ISWING,
C     +NROOMS, POWA, RHA, RHDIR, RHIO, RLOADL, RLOADS, TDIR, TIMAX,
C     +TIMAXN, TIMIN, TIO, TS, IDBG)
C
C-----PERFORMS CALCULATIONS FOR THE END OF INNER TIMELOOP
C
CMDK NLVGRM
CMDK NZN
CMDK NZW
C-----COMMON BLOCKS
CMDK BLKBSF
CMDK BLKQS
CMDK DUCTS2
CMDK HUMIDC
CMDK IZZQ
CMDK QUAYLE
CMDK SIMA
CMDK TEMP1
CMDK TEMPB
CMDK TIMEB
CMDK BLKTM2
CMDK TSTATC
CMDK WIBLK
C
C      REAL    COMFTT (11, 21)
C
C-----LOCAL VARIABLES
      LOGICAL I21F
      LOGICAL FIRST
C      INTEGER ID (40), IH (40)
      REAL TDAR(NZN),TAL(NZN),TDARN(NZN),TAMN(NZN),TAMX(NZN)
C
C-----INITIALIZATIONS
C      DATA ID     /1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30*11/
C      DATA IH     /1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
C     1   16, 17, 18, 19, 20, 20*21/
C      DATA FIRST/.TRUE./
      DATA I21F/.TRUE./
C
      RNSBDV=1./NSUBDV
      IF (IMODE.EQ.1) THEN
         IF(I21F)THEN
          I21F=.FALSE.
          OPEN(21,FILE='TAPE21',STATUS='NEW',IOSTAT=IO21)
          IF(IO21.NE.0)THEN
           WRITE(60,*)' ENDLP1: CANT OPEN TAPE21 WITH COOLING DATA'
           STOP ' ENDLP1: CANT OPEN TAPE21 WITH COOLING DATA'
           ENDIF
          ENDIF
         WRITE (21, 500) TINDEX, POWA*RNSBDV, RLOADS*RNSBDV,
     1      RLOADL*RNSBDV
         END IF
      RHA=RHA*RNSBDV
      DO  2 J=1,NROOMS
      QDCTBA(J)=QDCTBA(J)*RNSBDV
    2 TA(J)=TA(J)*RNSBDV
      TATTA(1)=TATTA(1)*RNSBDV
      IF(NATC.EQ.2)TATTA(2)=TATTA(2)*RNSBDV
      TBASA=TBASA*RNSBDV
      TCRWA=TCRWA*RNSBDV
      TS=TS*RNSBDV
      DO 8 I=1,NZW
      QCSURF(I)=QCSURF(I)*RNSBDV
      IF(IDAYM.EQ.NDAYM)QCSURFS(I)=QCSURFS(I)+QCSURF(I)
      IF(IDBG.EQ.1)WRITE(60,505)I,QCSURF(I)
    8 CONTINUE
      IF(IDAYM.EQ.NDAYM)THEN
        TBASM=TBASM+TBASA
        NTBASM=NTBASM+1
      ENDIF
      DO 10 I=1,2
      QCEIL(I)=QCEIL(I)*RNSBDV
      QROOF(I)=QROOF(I)*RNSBDV
      IF(IDBG.EQ.1)WRITE(60,506)I,QCEIL(I),QROOF(I)
   10 CONTINUE
      IF(FIRST)THEN
         DO 15 I=1,NROOMS
         TAL(I)=TA(I)
         TDAR(I)=0.
         TAMN(I)=0.
         TAMX(I)=0.
   15    CONTINUE
         ENDIF
      TDIRN=TA(NLVGRM)-TIO
      RHDIRN=RHA-RHIO
      DO 20 I=1, IZT+NATC
      TMP(1,I)=TMPN(1,I)
      TMP(2,I)=TMPN(2,I)
      TMP(3,I)=TMPN(3,I)
      TMP(4,I)=TMPN(4,I)
   20 CONTINUE
      IF (IREP.GE.NDPRE) THEN
C--------COMFORT INDEX
C         IF (ICOMF.NE.1 .AND. TI(NLVGRM).GE.65.) THEN
C            IDRYB=(TI(NLVGRM)-65.)*.5+1.
C  NOTE
C            ISHUM=WI(NLVGRM)*1000.+1.
C            COMFTT (ID (IDRYB), IH (ISHUM))=
C     1         COMFTT (ID (IDRYB), IH (ISHUM))+DTIM1N
C         END IF
         IF (TDIRN*TDIR.LT.0.) THEN
C-----------TEMP HAS CHANGED DIRECTION: CALC SWING AND TRACK MAX/MIN'S
            ISWING=ISWING+1
            IF (TIO.GT.TIMAX) TIMAX=TIO
            IF (TIO.GT.TIMAXN .AND. SOLARH(IT).LE.0.0)
     1         TIMAXN=TIO
            IF (TIO.LT.TIMIN) TIMIN=TIO
         END IF
         IF (RHDIRN*RHDIR.LT.0.) THEN
            IRH=IRH+1
            WRITE(1,501)RHIO
         END IF
      END IF
      TIO=TA(NLVGRM)
      RHIO=RHA
      TDIR=TDIRN
      RHDIR=RHDIRN
      IF(.NOT.FIRST)THEN
        DO 40 I=1,NROOMS
        TDARN(I)=TA(I)-TAL(I)
        IF(TDARN(I)*TDAR(I).LT.0.)THEN
          IF(TA(I).GT.TAL(I))THEN
C                         TAL IS A MIN. TEMP
            TAMN(I)=TAL(I)
            IF(TAMX(I).NE.0.)THEN
              SWNG=TAMX(I)-TAMN(I)
              IF(I.EQ.1)WRITE(11,502)I,TAMN(I),TAMX(I),SWNG
              IF(I.EQ.2)WRITE(11,503)I,TAMN(I),TAMX(I),SWNG
              IF(I.EQ.3)WRITE(11,504)I,TAMN(I),TAMX(I),SWNG
              ENDIF
          ELSE
C                        TAL IS A MAX. TEMP
            TAMX(I)=TAL(I)
            ENDIF
          ENDIF
        TDAR(I)=TDARN(I)
        TAL(I)=TA(I)
  40    CONTINUE
        ENDIF
       FIRST=.FALSE.
      RETURN
C
  500 FORMAT (1X, F12.5, 3 (1X, E10.4))
  501 FORMAT(1X,G13.5)
  502 FORMAT(1X,I5,3G13.5)
  503 FORMAT(10X,I5,3G13.5)
  504 FORMAT(19X,I5,3G13.5)
  505 FORMAT(10X,'IZW,QCSURF= ',I5,G13.5)
  506 FORMAT(12X,'IA,QCEIL,QROOF= ',I4,2G13.5)
      END
