      SUBROUTINE ACHL(NRM, TODDB, TA, WSPED, TSTACK, ACINFL)
C
C SUBROUTINE FOR GASASHR INFILTRATION MODEL IN LIVING SPACE
C      THE OPTION FOR CONSTANT INFILTRATION IS OBTAINED IF:
C       ACML=0, ACCNL=INFILTRATION
C      10/85 OPTION FOR INFILTRATION=F(TIME) ADDED. FEJ
C
C     VARIABLE   DEFINITION
C     --------   ----------------------------
C     XIN(I)     1=OUTDOOR TEMPERATURE, TODDB
C                2=INDOOR  TEMPERATUE,  TA
C                3=WIND WPEED,          WSPED
C                4=STACK TEMPERATURE,   TSTACK
C     RUNAVE     RUNNING AVERAGE OF THE PAST NRNAVL XIN'S
C     RUNINF     THE PAST NRNAVL VALUES OF XIN
C     NRNAVL       THE NUMBER OF PAST VALUES TO KEEP RUNNING
C                AVERAGES OF (ONE FOR EACH TIME STEP, EG. FOR AN OUTER
C                OF 2 MIN, NRNAVL=3 IS 4 MINUTES OF DATA
C     IOLD       POINTS TO THE OLDEST VALUE IN THE RUNINF ARRAY
C
CMDK NWL
CMDK NZN
CMDK NZW
CMDK ACHLQ
CMDK BLKTM2
CMDK ENCBK1
      DOUBLE PRECISION TINF
      REAL ACDTOI(NZN), ACDTOS(NZN), ACINFL(NZN), RUNAVE(4),
     +     RUNINF(180,4), TA(NZN), XIN(4), XINF(NZN)
      LOGICAL DEBUG,FIRST,I22F
C
      DATA IOLD,MAXNR,  DEBUG,FIRST/9999,180,  .FALSE.,.TRUE./
      DATA RUNINF/180*40., 180*70., 180*5., 180*80./
      DATA RUNAVE/   40.,    70.,    5.,    80./
C
      IF(ACML(NRM).LE.1.0E-37) THEN
          ACINFL(NRM)=ACCNL(NRM)
          RETURN
      ENDIF
      IF(FIRST) THEN
          FIRST=.FALSE.
          ACDTOI(NRM)=ACBL(NRM)*STORY
          ACDTOS(NRM)=ACSL(NRM)*STORY
          IF(NRNAVL.GT.MAXNR) THEN
            WRITE(60,*) 'NRNAVL=',NRNAVL,'. RESET TO ',MAXNR
            NRNAVL=MAXNR
          ENDIF
      ENDIF
C
      XIN(1)=TODDB
      XIN(2)=TA(NRM)
      XIN(3)=WSPED
      XIN(4)=TSTACK
C
C
C *** CREATE AND MAINTAIN RUNNING AVERAGES
C
      IF(IOLD.GE.NRNAVL) IOLD=0
      IOLD=IOLD+1
C
      DO 210 I=1,4
      RUNAVE(I)=RUNAVE(I)+(XIN(I)-RUNINF(IOLD,I))/FLOAT(NRNAVL)
      RUNINF(IOLD,I)=XIN(I)
  210 CONTINUE
      IF(DEBUG) WRITE(60,*) IOLD,RUNAVE
C
      RTO= 1.0/( RUNAVE(1)+460.)
      TDOI=ABS( RTO - 1.0/(RUNAVE(2)+460.) )
      WS  = RUNAVE(3)*RUNAVE(3)*RTO
      TDOS=ABS( RTO - 1.0/(RUNAVE(4)+460.) )
C
C *** CALCULATE AIR CHANGES BASED ON AVE VALUES
C
      ACINFL(NRM)=ACML(NRM)*SQRT(ACDTOI(NRM)*TDOI+ACWSL(NRM)*WS+
     +     ACDTOS(NRM)*TDOS)+ACCNL(NRM)
      IF(ACINFL(NRM).LT.0.) ACINFL(NRM)=0.
      RETURN
C
C---------------------------------------------------------
C     ENTRY FOR INSTANTANEOUS INFILTRATION
C
      ENTRY ACHIL(NRM, TODDB, TA, WSPED, TSTACK, ACINFL)
      RTO= 1.0/( TODDB+460.)
      TDOI=ABS( RTO - 1.0/(TA(NRM)+460.) )
      WS  = WSPED*WSPED*RTO
      TDOS=ABS( RTO - 1.0/(TSTACK+460.) )
C
      ACINFL(NRM)=ACML(NRM)*SQRT(ACDTOI(NRM)*TDOI+ACWSL(NRM)*WS+
     +     ACDTOS(NRM)*TDOS)+ACCNL(NRM)
      IF(ACINFL(NRM).LT.0.) ACINFL(NRM)=0.
      RETURN
C
C----------------------------------------------------------------
C     ENTRY FOR INFILTRATION = F(TIME). TAPE22 CONTAINS THE TIME
C     AND INFILTRATION DATA .
C
      ENTRY ACHLTIM(NRM,TODDB,TA,WSPED,TSTACK,ACINFL)
      DATA IO22/0/, TINF,XINF/0.,NZN*0.0/
      DATA I22F/.TRUE./
      IF(I22F)THEN
         OPEN(22,FILE='TAPE22',STATUS='OLD')
      I22F=.FALSE.
      ENDIF
  300 IF(IO22.NE.0 .OR. TINDEX.LE.TINF)THEN
         DO 310 JT=1,NROOMS
         NRM=NRMA(JT)
  310    ACINFL(NRM)=XINF(NRM)
      ELSE
         READ(22,*,IOSTAT=IO22)TINF,(XINF(JT),JT=1,NROOMS)
         IF(TINF.EQ.0.)STOP 'SUB.ACHL ERR300'
         GOTO 300
      ENDIF
      RETURN
C
C---------------------------------------------------------
C     ENTRY FOR INSTANTANEOUS INFILTRATION - COOLING MODE
C
      ENTRY ACHL2(NRM, TODDB, TA, WSPED, TSTACK, ACINFL)
      IF(ACML(NRM).LE.1.E-37) THEN
         ACINFL(NRM)=ACCNL(NRM)
         RETURN
         ENDIF
      RTO= 1.0/( TODDB+480.)
      TDOI=ABS( RTO - 1.0/(TA(NRM)+460.) )
      WS  = WSPED*WSPED*RTO
      TDOS=ABS( RTO - 1.0/(TSTACK+460.) )
C
      ACINFL(NRM)=ACML(NRM)*SQRT(ACDTOI(NRM)*TDOI+ACWSL(NRM)*WS+
     +          ACDTOS(NRM)*TDOS)-ACCNL(NRM)
      IF(ACINFL(NRM).LT.0.) ACINFL(NRM)=0.
      RETURN
      END
