c===========================================================================

      subroutine modatm(nz,sclh2o,uw,uo3,o3trp,ztrp,pbar,z,p,wh,wo) 1
c
c purpose:  modify the atmospheric profiles of water vapor, ozone and
c           pressure.  the vertical water vapor and ozone profiles
c           will be scaled by a constant factor so that the integrated
c           amounts will match those specified in the input.  
c
c           when a water vapor scale height is specified the original
c           model atmosphere vertical profile is ignored and the water
c           vapor is distributed with the specified scale hieght and
c           total water vapor content.
c
c           the pressure profile is scaled by the ratio of the
c           specified surface pressure and default surface pressure,
c           p(1), for the model atmosphere. (if the original
c           atmosphere is in pressure equilibrium then the scaled one
c           will be also.)
c
c           Certain input values for SCLH2O, UW, UO3 and PBAR can be used
c           to select default atmospheric profiles:
c 
c           sclh2o <  0     use default water vapor scale height
c           uw     <  0     use default water vapor content
c           uo3    <  0     use default ozone content for z.ge.ztrp
c           o3trp  <  0     use default ozone content for z.lt.ztrp
c           ztrp            altitude of tropopause for ozone adjustment 
c                           (since the default value of ztrp is usually
c                           zero, uo3 usually controls the total ozone amount)
c           pbar   <= 0     use default surface pressure
c
c
c input:    units
c
c   sclh2o    km        water vapor scale height 
c
c   uw       g/cm**2    water vapor column depth        
c
c   uo3      atm-cm     ozone column depth for z.ge.ztrp. 
c                       ozone density is adjusted up or down to match uo3.  
c
c   o3trp    atm-cm     ozone column depth for z.lt.ztrp.
c
c   ztrp     km         altitude of tropopause for ozone density adjustment
c                       if o3trp.eq.0, uo3 control total ozone amount.
c
c   pbar     mb         surface pressure 
c
c   nz                  number of atmospheric levels
c
c           note: 1 atm-cm = 1 loschmidt / cm**2 = 1000 dobson units
c           
      parameter (alosch=2.6868e19)
      parameter (pmo=2.6568e-23)
c
c pmo    = mass of oxygen atom (g)
c
      dimension z(*),p(*),wh(*),wo(*)
c
c modify water vapor
c
      if(uw.ge.0.) then
c      if(uw.ne.0.) then
cdb     write(12,*) 'h2o before'
cdb     write(12,'(1p10e11.3)') wh
        if(sclh2o.gt.0.) then
          w0=uw/sclh2o
          do 10 i=1,nz
            wh(i)=w0*exp(-z(i)/sclh2o)
 10       continue
        else
          toth2o=0.
          do 20 i=nz-1,1,-1
            dz=z(i+1)-z(i)
            den1=wh(i)
            den2=wh(i+1)
            tst1=abs(den1-den2)
            tst2=min(den1,den2)
            if(tst1.le..001*den1 .or. tst2.eq.0.) then
              du=.5*dz*(den1+den2)
            else
              du=dz*(den1-den2)/log(den1/den2)
            endif
            toth2o=toth2o+du
 20       continue
c         convert h2o sum from g-km/m3 to g/cm2 
          toth2o=0.1*toth2o
          fach2o=uw/toth2o
          do 30 i=1,nz
            wh(i)=fach2o*wh(i)
 30       continue
        endif
cdb     write(12,*) 'h2o after'
cdb     write(12,'(1p10e11.3)') wh
      endif
c
c modify ozone
c
      if(uo3.ge.0..or.o3trp.ge.0.) then
            
cdb        write(12,*) 'ozone before'
cdb        write(12,'(1p10e11.3)') (wo(i),i=1,nz)
        tropo3=0.
        strto3=0.
        do 40 i=nz-1,1,-1
          if(            strto3=strto3+.5*(          else
            tropo3=tropo3+.5*(          endif
 40     continue
c         convert o3 sum from g-km/m3 to atm-cm ( = loschmidts-cm)
        ofac=30*pmo*alosch
cdb        write(12,*) 'strto3=',strto3/ofac,
cdb     &            '  tropo3=',tropo3/ofac, (tropo3+strto3)/ofac

        factrp=1.
        facstr=1.
        if(o3trp.ge.0.) factrp=ofac*o3trp/tropo3
        if(uo3.ge.0.)   facstr=ofac*uo3/(tropo3+strto3)
        
        strto3=0.
        tropo3=0.
        do 50 i=1,nz
          if(            wo(i)=facstr*wo(i)
            strto3=strto3+.5*(          else
            wo(i)=factrp*wo(i)
            tropo3=tropo3+.5*(          endif
 50     continue
cdb        write(12,*) 'ozone after'
cdb        write(12,'(1p10e11.3)') (wo(i),i=1,nz)
cdb        write(12,*) 'strto3=',strto3/ofac,
cdb     &            '  tropo3=',tropo3/ofac, (tropo3+strto3)/ofac
cdb        write(12,*) 'factrp=',factrp,'facstr=',facstr
      endif
c
c modify pressure
c
      if(pbar.gt.0.) then
cdb     write(12,*) 'pressure before'
cdb     write(12,'(1p10e11.3)') p
        facp=pbar/p(1)
        do 70 i=1,nz
          p(i)=facp*p(i)
 70     continue
cdb     write(12,*) 'pressure after'
cdb     write(12,'(1p10e11.3)') p
      endif

      return
      end