SUBROUTINE e2spec(emiss,avephi,fxosp,dtsp)
  !
  ! $Author: CPTEC $
  ! $Date: 2000/02/24 13:15:43 $
  ! $Revision: 1.0 $
  !
  !subroutine e2spec computes the exchange terms in the flux equation
  !for longwave radiation for 2 terms used for nearby layer compu-
  !tations. the method is a table lookup on a pre-
  !computed e2 function (defined in ref. (4)).
  !calculations are done in the frequency range:
  !   0-560,1200-2200 cm-1
  !motivation for these calculations is in references (1) and (4).
  IMPLICIT NONE

  INCLUDE "parmeta.f90"

  INTEGER,PARAMETER :: l=lm,imax=2*im-1,ncol=imax,nblw=163,nblx=47,nbly=15
  INTEGER,PARAMETER :: nblm=nbly-1,lp1=l+1,lp2=l+2,lp3=l+3,lm1=l-1,lm2=l-2
  INTEGER,PARAMETER :: lm3=l-3,ll=2*l,llp1=ll+1,llp2=ll+2,llp3=ll+3,llm1=ll-1
  INTEGER,PARAMETER :: llm2=ll-2,llm3=ll-3,lp1m=lp1*lp1,lp1m1=lp1m-1
  INTEGER,PARAMETER :: lp1v=lp1*(1+2*l/2),lp121=lp1*nbly,ll3p=3*l+2,nb=12
  INTEGER,PARAMETER :: inlte=3,inltep=inlte+1,nnlte=56,lp1i=imax*lp1
  INTEGER,PARAMETER :: llp1i=imax*llp1,ll3pi=imax*ll3p,nb1=nb-1,ko2=12
  INTEGER,PARAMETER :: ko21=ko2+1,ko2m=ko2-1

  REAL,INTENT(in),DIMENSION(imax,2)       :: fxosp
  REAL,INTENT(in out),DIMENSION(imax,lp1) :: avephi
  REAL,INTENT(in out),DIMENSION(imax,2)   :: dtsp
  REAL,INTENT(out),DIMENSION(imax,lp1)    :: emiss

  INCLUDE "hcon.f90"
  INCLUDE "tabcom.f90"

  INTEGER,DIMENSION(imax,lp1) :: ival
  REAL,DIMENSION(imax,lp1) :: fyo,du,tmp3
  REAL,DIMENSION(5040) :: t1,t2,t4

  EQUIVALENCE (t1(1),table1(1,1)),(t2(1),table2(1,1)), (t4(1),table3(1,1))

  INTEGER :: i

  !first we obtain the emissivities as a function of temperature
  !index fxo) and water amount (index fyo). this part of the code
  !thus generates the e2 function.
  DO  i=1,imax*2
     tmp3(i,1)=LOG10(avephi(i,1))+h16e1
     fyo(i,1)=AINT(tmp3(i,1)*ten)
     du(i,1)=tmp3(i,1)-hp1*fyo(i,1)
     ival(i,1)=h28e1*fyo(i,1)+fxosp(i,1)
     emiss(i,1)=t1(ival(i,1))+du(i,1)*t2(ival(i,1))+ dtsp(i,1)*t4(ival(i,1))
  END DO
END SUBROUTINE e2spec
