SUBROUTINE o3int(phalf)
  !
  ! $Author: CPTEC $
  ! $Date: 2000/02/24 13:15:43 $
  ! $Revision: 1.0 $
  !
  ! subprogram:    o3int       compute zonal mean ozone for eta lyrs
  !   prgmmr: kenneth campana  org: w/nmc23    date: 89-07-07
  !           michael baldwin  org: w/nmc22    date: 92-06-08
  ! abstract: this code written at gfdl...
  !   calculates seasonal zonal mean ozone,every 5 deg of latitude,
  !   for current model vertical coordinate. output data in g/g * 1.e4
  !   code is called only once.
  ! program history log:
  !   84-01-01  fels and schwarzkopf,gfdl.
  !   89-07-07  k. campana - adapted stand-alone code for in-line use.
  !   92-06-08  m. baldwin - update to run in eta model

  IMPLICIT NONE

  INCLUDE "parmeta.f90"
  INTEGER, PARAMETER :: n=lm
  INTEGER, PARAMETER :: np=n+1
  INTEGER, PARAMETER :: np2=n+2
  INTEGER, PARAMETER :: nm1=n-1

  REAL,INTENT(out),DIMENSION(np):: phalf

  !seasonal climatologies of o3 (obtained from a previously run
  !code which interpolates o3 to user vertical coordinate).
  !defined as 5 deg lat means n.p.->s.p.
  REAL,DIMENSION(37,lm) :: dduo3n,ddo3n2,ddo3n3,ddo3n4
  COMMON/savmem/dduo3n,ddo3n2,ddo3n3,ddo3n4 !wint.,spr.,sum.,fall

  REAL,DIMENSION(n) :: rbar
  REAL,DIMENSION(45) :: ph1
  REAL,DIMENSION(37) :: ph2
  REAL,DIMENSION(48) :: p1
  REAL,DIMENSION(33) :: p2
  REAL,DIMENSION(81) :: rstd,p,rdata
  REAL,DIMENSION(82) :: qi,ph
  REAL,DIMENSION(19) :: tempn
  REAL,DIMENSION(19,n) ::dduo3
  REAL,DIMENSION(10,9) :: o3hi2
  REAL,DIMENSION(10,16) :: o3lo1,o3lo2,o3lo3,o3lo4,o3hi1
  REAL,DIMENSION(10,25) :: o3hi
  REAL,DIMENSION(10,40) :: ro3m
  REAL,DIMENSION(10,41) :: ro31,ro32,ro3
  REAL,DIMENSION(19,41) :: duo3n
  REAL,DIMENSION(37,n) :: o35deg

  INTEGER :: nkk,nk,nkp,k,l,ncase,itape,iplace,kk,nkm,nkmm,ki,kq,jj,ken
  REAL :: o3rd,o3tot,o3du

  EQUIVALENCE (o3hi1(1,1),o3hi(1,1)),(o3hi2(1,1),o3hi(1,17))
  EQUIVALENCE (ph1(1),ph(1)),(ph2(1),ph(46))
  EQUIVALENCE (p1(1),p(1)),(p2(1),p(49))
  DATA ph1/      0.,  &
       0.1027246e-04, 0.1239831e-04, 0.1491845e-04, 0.1788053e-04,  &
       0.2135032e-04, 0.2540162e-04, 0.3011718e-04, 0.3558949e-04,  &
       0.4192172e-04, 0.4922875e-04, 0.5763817e-04, 0.6729146e-04,  &
       0.7834518e-04, 0.9097232e-04, 0.1053635e-03, 0.1217288e-03,  &
       0.1402989e-03, 0.1613270e-03, 0.1850904e-03, 0.2119495e-03,  &
       0.2423836e-03, 0.2768980e-03, 0.3160017e-03, 0.3602623e-03,  &
       0.4103126e-03, 0.4668569e-03, 0.5306792e-03, 0.6026516e-03,  &
       0.6839018e-03, 0.7759249e-03, 0.8803303e-03, 0.9987843e-03,  &
       0.1133178e-02, 0.1285955e-02, 0.1460360e-02, 0.1660001e-02,  &
       0.1888764e-02, 0.2151165e-02, 0.2452466e-02, 0.2798806e-02,  &
       0.3197345e-02, 0.3656456e-02, 0.4185934e-02, 0.4797257e-02/
  DATA ph2/ 0.5503893e-02, 0.6321654e-02, 0.7269144e-02, 0.8368272e-02,  &
       0.9644873e-02, 0.1112946e-01, 0.1285810e-01, 0.1487354e-01,  &
       0.1722643e-01, 0.1997696e-01, 0.2319670e-01, 0.2697093e-01,  &
       0.3140135e-01, 0.3660952e-01, 0.4274090e-01, 0.4996992e-01,  &
       0.5848471e-01, 0.6847525e-01, 0.8017242e-01, 0.9386772e-01,  &
       0.1099026e+00, 0.1286765e+00, 0.1506574e+00, 0.1763932e+00,  &
       0.2065253e+00, 0.2415209e+00, 0.2814823e+00, 0.3266369e+00,  &
       0.3774861e+00, 0.4345638e+00, 0.4984375e+00, 0.5697097e+00,  &
       0.6490189e+00, 0.7370409e+00, 0.8344896e+00, 0.9421190e+00, 0.1000000e+01/
  DATA p1/ 0.9300000e-05, 0.1129521e-04, 0.1360915e-04, 0.1635370e-04,  &
       0.1954990e-04, 0.2331653e-04, 0.2767314e-04, 0.3277707e-04,  &
       0.3864321e-04, 0.4547839e-04, 0.5328839e-04, 0.6234301e-04,  &
       0.7263268e-04, 0.8450696e-04, 0.9793231e-04, 0.1133587e-03,  &
       0.1307170e-03, 0.1505832e-03, 0.1728373e-03, 0.1982122e-03,  &
       0.2266389e-03, 0.2592220e-03, 0.2957792e-03, 0.3376068e-03,  &
       0.3844381e-03, 0.4379281e-03, 0.4976965e-03, 0.5658476e-03,  &
       0.6418494e-03, 0.7287094e-03, 0.8261995e-03, 0.9380076e-03,  &
       0.1063498e-02, 0.1207423e-02, 0.1369594e-02, 0.1557141e-02,  &
       0.1769657e-02, 0.2015887e-02, 0.2295520e-02, 0.2620143e-02,  &
       0.2989651e-02, 0.3419469e-02, 0.3909867e-02, 0.4481491e-02,  &
       0.5135272e-02, 0.5898971e-02, 0.6774619e-02, 0.7799763e-02/
  DATA p2/ 0.8978218e-02, 0.1036103e-01, 0.1195488e-01, 0.1382957e-01,  &
       0.1599631e-01, 0.1855114e-01, 0.2151235e-01, 0.2501293e-01,  &
       0.2908220e-01, 0.3390544e-01, 0.3952926e-01, 0.4621349e-01,  &
       0.5403168e-01, 0.6330472e-01, 0.7406807e-01, 0.8677983e-01,  &
       0.1015345e+00, 0.1189603e+00, 0.1391863e+00, 0.1630739e+00,  &
       0.1908004e+00, 0.2235461e+00, 0.2609410e+00, 0.3036404e+00,  &
       0.3513750e+00, 0.4055375e+00, 0.4656677e+00, 0.5335132e+00,  &
       0.6083618e+00, 0.6923932e+00, 0.7845676e+00, 0.8875882e+00, 0.1000000e+01/
  DATA o3hi1/ .55,.50,.45,.45,.40,.35,.35,.30,.30,.30,  &
       .55,.51,.46,.47,.42,.38,.37,.36,.35,.35,  &
       .55,.53,.48,.49,.44,.42,.41,.40,.38,.38,  &
       .60,.55,.52,.52,.50,.47,.46,.44,.42,.41,  &
       .65,.60,.55,.56,.53,.52,.50,.48,.45,.45,  &
       .75,.65,.60,.60,.55,.55,.55,.50,.48,.47,  &
       .80,.75,.75,.75,.70,.70,.65,.63,.60,.60,  &
       .90,.85,.85,.80,.80,.75,.75,.74,.72,.71,  &
       1.10,1.05,1.00,.90,.90,.90,.85,.83,.80,.80,  &
       1.40,1.30,1.25,1.25,1.25,1.20,1.15,1.10,1.05,1.00,  &
       1.7,1.7,1.6,1.6,1.6,1.6,1.6,1.6,1.5,1.5,  &
       2.1,2.0,1.9,1.9,1.9,1.8,1.8,1.8,1.7,1.7,  &
       2.4,2.3,2.2,2.2,2.2,2.1,2.1,2.1,2.0,2.0,  &
       2.7,2.5,2.5,2.5,2.5,2.5,2.4,2.4,2.3,2.3,  &
       2.9,2.8,2.7,2.7,2.7,2.7,2.7,2.7,2.6,2.6,  &
       3.1,3.1,3.0,3.0,3.0,3.0,3.0,3.0,2.9,2.8/
  DATA o3hi2/ 3.3,3.4,3.4,3.6,3.7,3.9,4.0,4.1,4.0,3.8,  &
       3.6,3.8,3.9,4.2,4.7,5.3,5.6,5.7,5.5,5.2,  &
       4.1,4.3,4.7,5.2,6.0,6.7,7.0,6.8,6.4,6.2,  &
       5.4,5.7,6.0,6.6,7.3,8.0,8.4,7.7,7.1,6.7,  &
       6.7,6.8,7.0,7.6,8.3,10.0,9.6,8.2,7.5,7.2,  &
       9.2,9.3,9.4,9.6,10.3,10.6,10.0,8.5,7.7,7.3,  &
       12.6,12.1,12.0,12.1,11.7,11.0,10.0,8.6,7.8,7.4,  &
       14.2,13.5,13.1,12.8,11.9,10.9,9.8,8.5,7.8,7.5,  &
       14.3,14.0,13.4,12.7,11.6,10.6,9.3,8.4,7.6,7.3/
  DATA o3lo1/ 14.9,14.2,13.3,12.5,11.2,10.3,9.5,8.6,7.5,7.4,  &
       14.5,14.1,13.0,11.8,10.5,9.8,9.2,7.9,7.4,7.4,  &
       11.8,11.5,10.9,10.5,9.9,9.6,8.9,7.5,7.2,7.2,  &
       7.3,7.7,7.8,8.4,8.4,8.5,7.9,7.4,7.1,7.1,  &
       4.1,4.4,5.3,6.6,6.9,7.5,7.4,7.2,7.0,6.9,  &
       1.8,1.9,2.5,3.3,4.5,5.8,6.3,6.3,6.4,6.1,  &
       0.4,0.5,0.8,1.2,2.7,3.6,4.6,4.7,5.0,5.2,  &
       .10,.15,.20,.50,1.4,2.1,3.0,3.2,3.5,3.9,  &
       .07,.10,.12,.30,1.0,1.4,1.8,1.9,2.3,2.5,  &
       .06,.08,.10,.15,.60,.80,1.4,1.5,1.5,1.6,  &
       .05,.05,.06,.09,.20,.40,.70,.80,.90,.90,  &
       .05,.05,.06,.08,.10,.13,.20,.25,.30,.40,  &
       .05,.05,.05,.06,.07,.07,.08,.09,.10,.13,  &
       .05,.05,.05,.05,.06,.06,.06,.06,.07,.07,  &
       .05,.05,.05,.05,.05,.05,.05,.06,.06,.06,  &
       .04,.04,.04,.04,.04,.04,.04,.05,.05,.05/
  DATA o3lo2/ 14.8,14.2,13.8,12.2,11.0,9.8,8.5,7.8,7.4,6.9,  &
       13.2,13.0,12.5,11.3,10.4,9.0,7.8,7.5,7.0,6.6,  &
       10.6,10.6,10.7,10.1,9.4,8.6,7.5,7.0,6.5,6.1,  &
       7.0,7.3,7.5,7.5,7.5,7.3,6.7,6.4,6.0,5.8,  &
       3.8,4.0,4.7,5.0,5.2,5.9,5.8,5.6,5.5,5.5,  &
       1.4,1.6,2.4,3.0,3.7,4.1,4.6,4.8,5.1,5.0,  &
       .40,.50,.90,1.2,2.0,2.7,3.2,3.6,4.3,4.1,  &
       .07,.10,.20,.30,.80,1.4,2.1,2.4,2.7,3.0,  &
       .06,.07,.09,.15,.30,.70,1.2,1.4,1.6,2.0,  &
       .05,.05,.06,.12,.15,.30,.60,.70,.80,.80,  &
       .04,.05,.06,.08,.09,.15,.30,.40,.40,.40,  &
       .04,.04,.05,.055,.06,.09,.12,.13,.15,.15,  &
       .03,.03,.045,.052,.055,.06,.07,.07,.06,.07,  &
       .03,.03,.04,.051,.052,.052,.06,.06,.05,.05,  &
       .02,.02,.03,.05,.05,.05,.04,.04,.04,.04,  &
       .02,.02,.02,.04,.04,.04,.03,.03,.03,.03/
  DATA o3lo3/ 14.5,14.0,13.5,11.3,11.0,10.0,9.0,8.3,7.5,7.3,  &
       13.5,13.2,12.5,11.1,10.4,9.7,8.2,7.8,7.4,6.8,  &
       10.8,10.9,11.0,10.4,10.0,9.6,7.9,7.5,7.0,6.7,  &
       7.3,7.5,7.8,8.5,9.0,8.5,7.7,7.4,6.9,6.5,  &
       4.1,4.5,5.3,6.2,7.3,7.7,7.3,7.0,6.6,6.4,  &
       1.8,2.0,2.2,3.8,4.3,5.6,6.2,6.2,6.4,6.2,  &
       .30,.50,.60,1.5,2.8,3.7,4.5,4.7,5.5,5.6,  &
       .09,.10,.15,.60,1.2,2.1,3.0,3.5,4.0,4.3,  &
       .06,.08,.10,.30,.60,1.1,1.9,2.2,2.9,3.0,  &
       .04,.05,.06,.15,.45,.60,1.1,1.3,1.6,1.8,  &
       .04,.04,.04,.08,.20,.30,.55,.60,.75,.90,  &
       .04,.04,.04,.05,.06,.10,.12,.15,.20,.25,  &
       .04,.04,.03,.04,.05,.06,.07,.07,.07,.08,  &
       .03,.03,.04,.05,.05,.05,.05,.05,.05,.05,  &
       .03,.03,.03,.04,.04,.04,.05,.05,.04,.04,  &
       .02,.02,.02,.04,.04,.04,.04,.04,.03,.03/
  DATA o3lo4/ 14.2,13.8,13.2,12.5,11.7,10.5,8.6,7.8,7.5,6.6,  &
       12.5,12.4,12.2,11.7,10.8,9.8,7.8,7.2,6.5,6.1,  &
       10.6,10.5,10.4,10.1,9.6,9.0,7.1,6.8,6.1,5.9,  &
       7.0,7.4,7.9,7.8,7.6,7.3,6.2,6.1,5.8,5.6,  &
       4.2,4.6,5.1,5.6,5.9,5.9,5.9,5.8,5.6,5.3,  &
       2.1,2.3,2.6,2.9,3.5,4.3,4.8,4.9,5.1,5.1,  &
       0.7,0.8,1.0,1.5,2.0,2.8,3.5,3.6,3.7,4.0,  &
       .15,.20,.40,.50,.60,1.4,2.1,2.2,2.3,2.5,  &
       .08,.10,.15,.25,.30,.90,1.2,1.3,1.4,1.6,  &
       .07,.08,.10,.14,.20,.50,.70,.90,.90,.80,  &
       .05,.06,.08,.12,.14,.20,.35,.40,.60,.50,  &
       .05,.05,.08,.09,.09,.09,.11,.12,.15,.18,  &
       .04,.05,.06,.07,.07,.08,.08,.08,.08,.08,  &
       .04,.04,.05,.07,.07,.07,.07,.07,.06,.05,  &
       .02,.02,.04,.05,.05,.05,.05,.05,.04,.04,  &
       .02,.02,.03,.04,.04,.04,.04,.04,.03,.03/

  nkk=41
  nk=81
  nkp=nk+1
  DO  k=1,np
     phalf(k)=phalf(k)*1.0e+03
  END DO
  !24 pstd(k)=pstd(k+1)*1.0e 03
  DO  k=1,nk
     ph(k)=ph(k)*1013250.
     p(k)=p(k)*1013250.
  END DO
  ph(nkp)=ph(nkp)*1013250.

  !load arrays ro31,ro32,as in dicks pgm.
  DO  k=1,25
     DO  l=1,10
        ro31(l,k)=o3hi(l,k)
        ro32(l,k)=o3hi(l,k)
     END DO
  END DO

  DO  ncase=1,4
     itape=ncase+50
     iplace=2
     IF (ncase == 2) iplace=4
     IF (ncase == 3) iplace=1
     IF (ncase == 4) iplace=3
     !ncase=1: spring (in n.h.)
     !ncase=2: fall   (in n.h.)
     !ncase=3: winter (in n.h.)
     !ncase=4: summer (in n.h.)
     IF (ncase == 1.OR.ncase == 2) THEN
        DO  k=26,41
           DO  l=1,10
              ro31(l,k)=o3lo1(l,k-25)
              ro32(l,k)=o3lo2(l,k-25)
           END DO
        END DO
     END IF
     IF (ncase == 3.OR.ncase == 4) THEN
        DO  k=26,41
           DO  l=1,10
              ro31(l,k)=o3lo3(l,k-25)
              ro32(l,k)=o3lo4(l,k-25)
           END DO
        END DO
     END IF
     DO  kk=1,nkk
        DO  l=1,10
           duo3n(l,kk)=ro31(11-l,kk)
           duo3n(l+9,kk)=ro32(l,kk)
        END DO
        duo3n(10,kk)=.5*(ro31(1,kk)+ro32(1,kk))
     END DO
     !for ncase=2 or ncase=4,reverse latitude arrangement of corr. season
     IF (ncase == 2.OR.ncase == 4) THEN
        DO  kk=1,nkk
           DO  l=1,19
              tempn(l)=duo3n(20-l,kk)
           END DO
           DO  l=1,19
              duo3n(l,kk)=tempn(l)
           END DO
        END DO
     END IF
     !duo3n now is o3 profile for appropriate season,at std. pressure
     !levels
     !begin latitude (10 deg) loop
     DO  l=1,19
        DO  kk=1,nkk
           rstd(kk)=duo3n(l,kk)
        END DO
        nkm=nk-1
        nkmm=nk-3
        !bessels half-point interpolation formula
        DO  k=4,nkmm,2
           ki=k/2
           rdata(k)=.5*(rstd(ki)+rstd(ki+1))-(rstd(ki+2)-rstd(ki+1)-rstd(ki)+  &
                rstd(ki-1))/16.
        END DO
        rdata(2)=.5*(rstd(2)+rstd(1))
        rdata(nkm)=.5*(rstd(nkk)+rstd(nkk-1))
        !put unchanged data into new array
        DO  k=1,nk,2
           kq=(k+1)/2
           rdata(k)=rstd(kq)
        END DO
        !calculate layer-mean ozone mixing ratio for each model level
        DO  kk=1,n
           rbar(kk)=0.
           !loop to calculate sums to get layer ozone mean
           DO  k=1,nk
              IF(ph(k+1) < phalf(kk)) CYCLE
              IF(ph(k) > phalf(kk+1)) CYCLE
              IF(ph(k+1) < phalf(kk+1).AND.ph(k) < phalf(kk)) rbar(kk)=rbar(kk  &
                   )+rdata(k)*(ph(k+1)-phalf(kk))
              IF(ph(k+1) < phalf(kk+1).AND.ph(k) >= phalf(kk)) rbar(kk)=rbar(kk)+ &
                   rdata(k)*(ph(k+1)-ph(k))
              IF(ph(k+1) > phalf(kk+1).AND.ph(k) > phalf(kk)) rbar(kk)=rbar(kk)+ &
                   rdata(k)*(phalf(kk+1)-ph(k))
           END DO
           rbar(kk)=rbar(kk)/(phalf(kk+1)-phalf(kk))
           IF(rbar(kk) > .0000) CYCLE
           !code to cover case when model resolution is so fine that no value
           !of p(k) in the ozone data array falls between phalf(kk+1) and
           !phalf(kk).   procedure is to simply grab the nearest value from
           !rdata
           DO  k=1,nk
              IF(ph(k) < phalf(kk).AND.ph(k+1) >= phalf(kk+1)) rbar(kk)=rdata(k )
           END DO
        END DO
        !calculate total ozone
        o3rd=0.
        DO  kk=1,80
           o3rd=o3rd+rdata(kk)*(ph(kk+1)-ph(kk))
        END DO
        o3rd=o3rd+rdata(81)*(p(81)-ph(81))
        o3rd=o3rd/980.
        o3tot=0.
        DO  kk=1,n
           o3tot=o3tot+rbar(kk)*(phalf(kk+1)-phalf(kk))
        END DO
        o3tot=o3tot/980.
        !units are micrograms/cm**2
        o3du=o3tot/2.144
        !o3du units are dobson units (10**-3 atm-cm)
        DO  kk=1,n
           dduo3(l,kk)=rbar(kk)*.01
        END DO
     END DO
     !end of latitude loop

     !create 5 deg ozone quantities by linear interpolation of
     !10 deg values
     DO  kk=1,n
        DO  l=1,19
           o35deg(2*l-1,kk)=dduo3(l,kk)
        END DO
        DO  l=1,18
           o35deg(2*l,kk)=0.5*(dduo3(l,kk)+dduo3(l+1,kk))
        END DO
     END DO
     !output to unit (itape) the ozone values for later use o222 
     IF (iplace == 1) THEN
        DO  jj=1,37
           DO  ken=1,n
              dduo3n(jj,ken) = o35deg(jj,ken)
           END DO
        END DO
     ELSE IF (iplace == 2) THEN
        DO  jj=1,37
           DO  ken=1,n
              ddo3n2(jj,ken) = o35deg(jj,ken)
           END DO
        END DO
     ELSE IF (iplace == 3) THEN
        DO  jj=1,37
           DO  ken=1,n
              ddo3n3(jj,ken) = o35deg(jj,ken)
           END DO
        END DO
     ELSE IF (iplace == 4) THEN
        DO  jj=1,37
           DO  ken=1,n
              ddo3n4(jj,ken) = o35deg(jj,ken)
           END DO
        END DO
     END IF
  END DO
  !end of loop over cases
END SUBROUTINE o3int
