      FUNCTION ACHB( TODDB, TBAS, WSPED, TSTACK )
C
C SUBROUTINE FOR GASASHR INFILTRATION MODEL IN BASEMENT
C      THE OPTION FOR CONSTANT INFILTRATION HAS BEEN ADDED AS
C       ACMB=0, ACCNB=INFILTRATION
C     VARIABLE   DEFINITION
C     --------   ----------------------------
C     XIN(I)     1=OUTDOOR TEMPERATURE, TODDB
C                2=INDOOR  TEMPERATUE,  TBAS
C                3=WIND WPEED,          WSPED
C                4=STACK TEMPERATURE,   TSTACK
C     RUNAVE     RUNNING AVERAGE OF THE PAST NRNAVB XIN'S
C     RUNINF     THE PAST NRNAVB VALUES OF XIN
C     NRNAVB       THE NUMBER OF PAST VALUES TO KEEP RUNNING
C                AVERAGES OF (ONE FOR EACH TIME STEP, EG. FOR AN OUTER
C                OF 2 MIN, NRNAVB=3 IS 4 MINUTES OF DATA
C     IOLD       POINTS TO THE OLDEST VALUE IN THE RUNINF ARRAY
C
C
CMDK ACHBQ
      DIMENSION RUNINF(180,4), RUNAVE(4), XIN(4)
      LOGICAL DEBUG,FIRST
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(ACMB.LE.1.0E-37) THEN
          ACHB=ACCNB
          RETURN
      ENDIF
      IF(FIRST) THEN
          FIRST=.FALSE.
          ACDTOI=ACBB
          ACDTOS=ACSB
          IF(NRNAVB.GT.MAXNR) THEN
            WRITE(60,*) 'NRNAVB=',NRNAVB,'. RESET TO ',MAXNR
            NRNAVB=MAXNR
          ENDIF
      ENDIF
C
      XIN(1)=TODDB
      XIN(2)=TBAS
      XIN(3)=WSPED
      XIN(4)=TSTACK
C
C
C *** CREATE AND MAINTAIN RUNNING AVERAGES
C
      IF(IOLD.GE.NRNAVB) IOLD=0
      IOLD=IOLD+1
C
      DO 210 I=1,4
      RUNAVE(I)=RUNAVE(I)+(XIN(I)-RUNINF(IOLD,I))/FLOAT(NRNAVB)
      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
      ACHB=ACMB*SQRT(ACDTOI*TDOI+ACWSB*WS+ACDTOS*TDOS) + ACCNB
      IF(ACHB.LT.0.) ACHB=0.
      RETURN
C
C---------------------------------------------------------
C     ENTRY FOR INSTANTANEOUS INFILTRATION
C
      ENTRY ACHIB( TODDB, TBAS, WSPED, TSTACK )
      RTO= 1.0/( TODDB+460.)
      TDOI=ABS( RTO - 1.0/(TBAS+460.) )
      WS  = WSPED*WSPED*RTO
      TDOS=ABS( RTO - 1.0/(TSTACK+460.) )
C
      ACHIB=ACMB*SQRT(ACDTOI*TDOI+ACWSB*WS+ACDTOS*TDOS) + ACCNB
      IF(ACHIB.LT.0.) ACHIB=0.
      RETURN
C
C---------------------------------------------------------
C     ENTRY FOR INSTANTANEOUS INFILTRATION - COOLING MODE
C
      ENTRY ACHB2( TODDB, TA, WSPED, TSTACK )
      IF(ACMB.LE.1.E-37) THEN
         ACHB2=ACCNB
         RETURN
         ENDIF
      RTO= 1.0/( TODDB+480.)
      TDOI=ABS( RTO - 1.0/(TA+460.) )
      WS  = WSPED*WSPED*RTO
      TDOS=ABS( RTO - 1.0/(TSTACK+460.) )
C
      ACHB2=ACMB*SQRT(ACDTOI*TDOI+ACWSB*WS+ACDTOS*TDOS) - ACCNB
      IF(ACHB2.LT.0.) ACHB2=0.
      RETURN
      END
