      real function relhum (t,w,p)  ! relative humidity [%]
      implicit none

c NCL function: rh = relhum (t,w,p)

                                   ! input
      real  t                      ! temperature  [K]
      real  w                      ! mixing ratio [kg/kg]
      real  p                      ! pressure     [Pa]  

c This is built from CCM Processor routines:
c     subroutine crlhmp1(w,t,mlon,mlon,klvl,p,rh)
c     subroutine estbl0b(ES,T)
C
C** NAME=ESBL0B
C
C     TABLE OF ES FROM -100 TO +102 C IN ONE-DEGREE INCREMENTS.
C
      real  tabl1(82),tabl2(122)
      real  table(204)
      equivalence (tabl1(1),table(1)),(tabl2(1),table(83))
      data tabl1            /.01403,.01719,.02101,.02561,.03117,.03784,
     1.04584,.05542,.06685,.08049,.09672,.1160,.1388,.1658,.1977,.2353,
     2.2796,.3316,.3925,.4638,.5472,.6444,.7577,.8894,1.042,1.220,1.425,
     31.662,1.936,2.252,2.615,3.032,3.511,4.060,4.688,5.406,6.225,7.159,
     48.223,9.432,10.80,12.36,14.13,16.12,18.38,20.92,23.80,27.03,30.67,
     534.76,39.35,44.49,50.26,56.71,63.93,71.98,80.97,90.98,102.1,114.5,
     6128.3,143.6,160.6,179.4,200.2,223.3,248.8,276.9,307.9,342.1,379.8,
     7421.3,466.9,517.0,572.0,632.3,698.5,770.9,850.2,937.0,1032.0,
     8 1146.6/
      data tabl2                     /1272.0,1408.1,1556.7,1716.9,1890.
     13,2077.6,2279.6,2496.7,2729.8,2980.0,3247.8,3534.1,3839.8,4164.8,
     24510.5,4876.9,5265.1,5675.2,6107.8,6566.2,7054.7,7575.3,8129.4,87
     319.2,9346.5,10013.,10722.,11474.,12272.,13119.,14017.,14969.,1597
     47.,17044.,18173.,19367.,20630.,21964.,23373.,24861.,26430.,28086.
     5,29831.,31671.,33608.,35649.,37796.,40055.,42430.,44927.,47551.,
     650307.,53200.,56236.,59422.,62762.,66264.,69934.,73777.,77802.,82
     7015.,86423.,91034.,95855.,100890.,106160.,111660.,117400.,123400.
     8,129650.,136170.,142980.,150070.,157460.,165160.,173180.,181530.,
     9190220.,199260.,
     X                          208670.,218450.,228610.,239180.,250160.,
     1261560.,273400.,285700.,298450.,311690.,325420.,339650.,354410.,
     2369710.,385560.,401980.,418980.,436590.,454810.,473670.,493170.,
     3513350.,534220.,555800.,578090.,601130.,624940.,649530.,674920.,
     4701130.,728190.,756110.,784920.,814630.,845280.,876880.,909450.,
     5943020.,977610.,1013250.,1049940.,1087740.,1087740./

      real    tp, t2, es, pp 
      integer it

      tp = t
      if(tp .gt. 375.16) tp = 375.16
      if(tp .lt. 173.16) tp = 173.16
      it = tp-173.16
      t2 = 173.16+it
      es =(t2+1.-tp)*table(it+1)+(tp-t2)*table(it+2)
      es = es*1.0e-01

      relhum = (w*(p-0.378*es)/ (0.622*es))*100.
      if (relhum.lt.0.) relhum = 0.0001

      return
      end

