      subroutine sbdndx(ichan,index1,index2,lprint,dndxi,ddiss,
     1     dndxtr,dndxsbx)
c
c***********************************************************************
c
c     sbdndx
c
c     september, 2001 charlie wellemeyer of ssai
c
c     purpose
c        perform table lookups for computation of dndx
c        for a given table pressure, profile, and zenith angle
c
c     method
c        performs a table lookup on quantities i0, iss, tr,
c        using precomputed lagrangian polynomial coefficients 
c        interpolates table values to evaluate quantities for
c        current thet0 and theta. Retrieves nodal sb from table.
c
c     variables
c       name       type   i/o   description
c       ----       ----   ---   -----------
c
c      arguments
c       ichan      i*4     i    channel index for calculation
c       index1     i*4     i    table address for lamda1 interpolation
c       index2     i*4     i    table address for lamda1 fitted coeffs
c                               and sb factor
c       lprint     l*4     i    debug print control array
c       dndxi      r*8     o    i0 values from sensitivity tables
c       ddiss      r*8     o    single scattered i0 values from tables
c       dndxtr     r*8     o    transmittance values from sensitivity tables
c       dndxsbx    r*8     o    atmos backrefl from sensitivity tables
c
c      internal
c       ind         i*4          computed table index for lacda1
c
c***********************************************************************
c
      implicit none
      integer ichan, index1, index2, ilyr, new_index1, new_index2,
     1      i, ind
      logical lprint(30)
      real dndxi(12), ddiss(12), dndxtr(12), dndxsbx(12), ssi0
c
      include 'lpoly.com'
      include 'dndxtbl.com'
c     real logi0(10,21,9,4,12), lgssi0(10,21,9,4,12)
c     real tti0(10,21,9,4,12), sb(21,9,4,12)
c     equivalence (dndxlogi0,logi0),(dndxssi0,lgssi0),
c    1     (dndxti0,tti0),(dndxsb,sb)
c
      integer layer1(12), layer2(12)
      real inot, ione, itwo, tran
      real zone, ztwo
      real pi
c
c     -- data statements of nodal indecese for perturbed
c     -- tables in 1) angle dependent and 2) angle
c     -- independent portions of the tables
c
      data layer1/0,7560,15120,22680,30240,37800,45360,52920,
     1     60480,68040,75600,83160/
      data layer2/0,756,1512,2268,3024,3780,4536,5292,6048,6804,
     1     7560,8316/
      data pi/3.1415926/
c
      if(lprint(24)) write(6,1000) index1,index2
c
c     -- perform table interpolation for lamda
c
      do ilyr = 1,12
         new_index1 = index1+layer1(ilyr)
         new_index2 = index2+layer2(ilyr)
         inot = 0.
         ssi0 = 0.
         tran = 0.
         do 100 i=1,4
            ind=new_index1+i-1
            inot = inot + dndxlogi0(ind)*cof(i,ichan)
            ssi0 = ssi0 + dndxssi0(ind)*cof(i,ichan)
c     if(ichan.eq.7.and.ilyr.eq.1.and.index1.lt.1891) then
c       print*,'ilyr,index1,layer1(ilyr),new_index1'
c       print*,ilyr,index1,layer1(ilyr),new_index1
c       print*,'i,ind,10.**dndxssi0(ind),cof(i,ichan)'
c       print*,i,ind,10.**dndxssi0(ind),cof(i,ichan)
c     endif
            tran = tran + dndxti0(ind)*cof(i,ichan)
100      continue
c
c        -- convert table values into i1, i2, t, and i0
c
         inot = 10.**inot
         ssi0 = 10.**ssi0
         tran = tran * inot
c     if(ichan.eq.7.and.ilyr.eq.1.and.index1.lt.1891) then
c       print*,'sbdndx: ssi0 =',ssi0
c     endif
         if(lprint(24)) write(6,1100) ilyr,inot,ssi0,tran
c
c        -- compute izero, t, and sbar
c
         dndxi(ilyr)  = inot 
         ddiss(ilyr)   = ssi0
         dndxtr(ilyr)  = tran 
         dndxsbx(ilyr) = dndxsb(new_index2) 
         if(lprint(24)) write (6,1200) dndxi(ilyr),ddiss(ilyr),
     1     dndxtr(ilyr),dndxsbx(ilyr)
      enddo
c
      return
c
c  format statements
c
 1000 format('Subroutine sbdndx',/'Input:   index1 = ',i10,
     1           ' index2 = ',i10)
 1100 format('Intern:  layer = ',i8,'  inot   = ',1pe12.4,
     1 ' ione   = ',1pe12.4,' itwo   = ',1pe12.4)
 1200 format('Output:  dndxi  = '1pe12.4,' dndxtr = ',1pe12.4,
     1 ' dndxsbx= ',1pe12.4)
c
      end
