      subroutine resadj(iozon,irefl,resn,sens,rsens,dndx,delnt,
     1 fgprf,fgtmp,s2prf,aptmp,ozbst,aptoz,estozn,delr,lprint,
     2 resbst)
c
c***********************************************************************
c
c     resadj
c
c        march, 2001 by charlie wellemeyer of SSAI
c
c     purpose
c
c        adjust residues after calculation of best ozone 
c
c     variables
c       name      type   i/o   description
c       ----      ----   ---   -----------
c      arguments
c       iozon      i*4    i    total ozone wavelength index
c       irefl      i*4    i    reflectivity wavelength index
c       res(8)     r*4    i    residues calculated using first set of profiles
c                             (low for xlat < 45, mid for xlat > 45 degrees)
c       sens(8)    r*4    i    sensitivities calculated using first set of 
c                             profiles (low for xlat < 45, mid for 
c                             xlat > 45 degrees)
c       rsens(8)   r*4    i    reflectivity sensitivities calculated using first
c                             set of profiles (low for xlat < 45, mid for
c       dndx(8,11) r*4    i    layer ozone sensitivities of nvalue
c       delnt(8,11)r*4    i    layer temperature change of nvalue
c       fgprf(11)  r*4    i    first guess ozone profile
c       fgtmp(11)  r*4    i    first guess temperature profile
c       s2prf(11)  r*4    i    step 2 solution ozone profile
c       aptmp(11)  r*4    i    apriori temperature profile
c       estozn     r*4    i    ozone estimated from 317.5 nm
c       aptoz      r*4    i    apriori total ozone
c       ozbst      r*4    i    best ozone
c       lprint     l*4    i    if = .true., print
c       resbst(8)  r*4    o    best residues
c
c***********************************************************************
c
      implicit none
      integer*4 iozon, irefl, iw, lyr
      real delr, sumfg, sums2
c
c     -- input parameters
c
      real resn(8),sens(8),rsens(8),dndx(8,11),delnt(8,11),fgprf(11),
     1    fgtmp(11),s2prf(11),aptmp(11),ozbst,aptoz,estozn
      logical lprint(30)
c
c     -- internal parameters
c
      real drsozn(8), drstmp(8), zero
c
c     -- output parameters
c
      real resbst(8)
c
      data zero/0.0/
c
c      if (lprint(19)) write (6,1000) iozon, resn, sens, rsens,
c     1  dndx, delnt, fgprf, s2prf, fgtmp, aptmp, ozbst, estozn
c
c  calculate residue adjustment for reflectivity channel to get delr
c
        irefl=irefl-4
        iw = irefl
c
        drsozn(iw)=0.0
        drstmp(iw)=0.0
        sumfg = 0.0
        sums2 = 0.0
c
c  calculate adjustments associated with ozone dependence
c
         if (lprint(19)) write(6,1100)
         do 100 lyr = 1,11
           if(iw.lt.8) then
             drsozn(iw)=drsozn(iw)-(s2prf(lyr)-fgprf(lyr))*dndx(iw,lyr)
             drstmp(iw)=drstmp(iw)-delnt(iw,lyr)
           endif
c
           if (lprint(19)) then
             if(iw.lt.8) then
               write(6,1200) iw,lyr-1,fgprf(lyr),
     1         s2prf(lyr),dndx(iw,lyr),(fgprf(lyr)-s2prf(lyr))*
     2         dndx(iw,lyr),fgtmp(lyr)-aptmp(lyr),delnt(iw,lyr)
             else
               write(6,1200) iw,lyr-1,fgprf(lyr),
     1         s2prf(lyr),zero,zero,fgtmp(lyr)-aptmp(lyr),zero
             endif
             sumfg = sumfg + fgprf(lyr)
             sums2 = sums2 + s2prf(lyr)
           endif
  100    continue
c
c  calculate reflectivity adjustment and residue adjustment
c
        delr = (resn(iw) + drsozn(iw) + drstmp(iw))/rsens(irefl)
c       delr = 0.0
        resbst(iw) = resn(iw) + drsozn(iw) + drstmp(iw) -
     1                          delr*rsens(iw)
c
      if (lprint(19)) write(6,1300) sumfg, sums2, drsozn(iw),
     1     drstmp(iw),delr,rsens(iw),delr*rsens(iw)
c
c  adjust residues for remaining wavelengths
c
      do 300 iw = 1,8
        if(iw.ne.irefl) then
          drsozn(iw)=0.0
          drstmp(iw)=0.0
          sumfg = 0.0
          sums2 = 0.0
c
         if (lprint(19)) write(6,1100)
         do 200 lyr = 1,11
           if(iw.lt.8) then
             drsozn(iw)=drsozn(iw)-(s2prf(lyr)-fgprf(lyr))*dndx(iw,lyr)
             drstmp(iw)=drstmp(iw)-delnt(iw,lyr)
           endif
c
           if (lprint(19)) then
             if(iw.lt.8) then
               write(6,1200) iw,lyr-1,fgprf(lyr),
     1         s2prf(lyr),dndx(iw,lyr),(fgprf(lyr)-s2prf(lyr))*
     2         dndx(iw,lyr),fgtmp(lyr)-aptmp(lyr),delnt(iw,lyr)
             else
               write(6,1200) iw,lyr-1,fgprf(lyr),
     1         s2prf(lyr),zero,zero,fgtmp(lyr)-aptmp(lyr),zero
             endif
             sumfg = sumfg + fgprf(lyr)
             sums2 = sums2 + s2prf(lyr)
           endif
  200    continue
c
c  calculate adjusted residue
c
        resbst(iw) = resn(iw) + drsozn(iw) + drstmp(iw) -
     1                          delr*rsens(iw)
        if(iw.eq.1) then
          resbst(iw) = resn(iw) + drsozn(iw) + drstmp(iw)
          if (lprint(19)) write(6,1300) sumfg, sums2, drsozn(iw),
     1     drstmp(iw),delr,rsens(iw),delr*rsens(iw)
        else
          if (lprint(19)) write(6,1300) sumfg, sums2, drsozn(iw),
     1     drstmp(iw),delr,rsens(iw),delr*rsens(iw)
        endif
      endif
c
  300 continue
c
      if (lprint(19)) write (6,1400) resn,
     1     drsozn, drstmp, resbst
c
        irefl=irefl+4
      return
c
c  format statements
c
 1000 format (/'Subroutine resadj'/'Input:  iozon = ',i2/
     1 ' resn  = ',8f8.3/'sens   = ',8f8.3/'rsens  = ',8f8.3/
     2 ' dndx  = ',11(/5f12.6),/,' delnt = ',11(/5f12.6)/
     3 ' fgprf = ',/,11f7.2/' s2prf = ',/,11f7.2/
     3 ' fgtmp = ',/,11f7.2/' aptmp = ',/,11f7.2/
     4 ' ozbst = ',f8.2,' estozn = ',f8.2)
 1100 format(1x,'Internal:',/,
     6 '  iwave layer  F.G.   Step2    dN/dX   LyrAdj  ',
     7 'Tfg-Tap  TmpAdj')
 1200 format(1x,i4,i6,2f8.2,2f9.4,f8.2,2f9.4)
 1300 format(1x,'sums:',5x,2f8.2,9x,f9.4,8x,f9.4,/
     1   ' delr = ',f8.4,' rsens(iw) = ',f10.4,
     2   ' resadj = ',f8.4)
 1400 format ('resn   = ',8f8.3,/,'drsozn = ',8f8.3,/,
     1 'drstmp = ',8f8.3,/,'resbst = ',8f8.3)
c
      end
