      SUBROUTINE HAMON_mbtxbtmean(itsprobe,temp,maxlevel,maxcalc,
     * iwarm,nlevels,bmiss,depth,nbinmax,xmeanall)

C     CALCULATES MEAN TEMPERATURE DOWN TO GIVEN LEVEL FOR EACH
C     GIVEN DEPTH

c     read in delta fields

      parameter (maxbin=200)
      dimension xcount(maxbin)
      dimension xmeant(maxbin)
      dimension temp(maxlevel,maxcalc)
      dimension depth(maxlevel)


c     get means in five-meter bins

      depthmax=0.
      if (itsprobe .eq. 1) then
      nbinmax=20
      elseif (itsprobe .eq. 2) then
      nbinmax=40
      endif
      nbinout=0
      ip2=1

      do 25 k=1,maxbin
       xcount(k)=0.
       xmeant(k)=0.
25    continue

      do 35 k=1,nlevels

       if ( temp(k,ip2) .gt. bmiss .and.
     *      temp(k,ip2) .lt. 50. ) then
        nbin=(depth(k)/5)+1
c       write(6,*) 'xx',depth(k),nbin,maxbin,nbinmax
        if ( nbin .gt. maxbin ) goto 35
        if ( nbin .gt. nbinmax ) nbinmax=nbin
        xcount(nbin)=xcount(nbin)+1.
        xmeant(nbin)=xmeant(nbin)+temp(k,ip2)
       endif
c      write(6,*) 'bin',k,nbin,depth(k),xcount(nbin),
c    *  temp(k,ip2)

35    continue

      nbinout=nbinmax
      nfirst=0
      do 40 k=1,nbinout

        if ( xcount(k) .gt. 0. .and.
     *   nfirst .eq. 0 ) nfirst=k       
       if ( xcount(k) .gt. 1. ) then

        xmeant(k)=xmeant(k)/xcount(k)

       endif

40    continue

c    fill in up to surface

      do 45 k=1,nfirst-1
45     xmeant(k)=xmeant(nfirst) 

c     fill in missing bins

      do 50 k=nfirst+1,nbinout

       if ( xcount(k) .lt. 1. ) then

        k1=k-1

        k2=0
        do 75 kk2=k+1,nbinout
75       if ( k2 .eq. 0 .and. xcount(kk2)  .gt. 0. )
     *    k2=kk2

        d1=(k1-1)*5.
        d2=(k2-1)*5.
        d=(k-1)*5.
        xmeant(k)=xmeant(k1)+((d-d1)/(d2-d1))*(xmeant(k2)-xmeant(k1)) 
       endif
50    continue

c     write(6,*) 'here1'
      dtot=0
      xdep=0
      do 60 k=1,nbinout
      dtot=dtot+xmeant(k)
      xdep=xdep+1.
      xmeanall=dtot/xdep
60    continue
      if (itsprobe .eq. 1) then
      if (xmeanall .ge. 12.) then
       iwarm=1
      elseif (xmeanall .lt. 12.) then
       iwarm=0
      endif
      elseif (itsprobe .eq. 2) then
      if (xmeanall .ge. 10.) then
       iwarm=1
      elseif (xmeanall .lt. 10.) then
       iwarm=0
      endif
      endif
c     write(6,*)'xmeanall',xmeanall

      return
      end
