c    @(#)rdbth.f	1.1
      subroutine rdbth(ier)
c----------------------------------------------------------------------
c  purpose: to read temperature and salinity fields at sbl levels
c
c
c_______________________________________________________________________
c
      include'mladv.inc'

c
c-------------------------------------------------------------------
c     model common blocks
      common/parm/kz,m,mm1,mm2,mp1,msq,tim,tmax,tstart,hy,rea,dt,
     +   xbasin,yy(mx0),icalc,kzp1,kzm1,iftop,ifbot,iftvv,ifrel,theta,
     +   ifsbl,ifeva
      common/unit/iin,iout,idff,idif,iorr,iowr,iplt,ifndd
      common /extpar/ rlat0,rlng0,t0,v0,dhor,ht,time0,r0,f0star
      common/cnn/n,nm1,nm2,np1
c
c_______________________________________________________________________
c
      character*80 dssbl
      dimension intgate(lmax),rml(mnmax)
      data isbl/41/
c ---- field idents compatible with sblfld.inc
      data ivtemp/20/,ivdepth/24/
      data dssbl/'sbl.th.dat'/, slon /0.25/,slat/0.25/
      data indepth/0/,intgate/lmax*0/
c
c----------------------------------------------------------------------      
c
      open(isbl,file=dssbl,status='old',form='unformatted',
     *     err=905)

      read(isbl,err=4)ijdate,mz2,nz2,dxz,dyz,rlat0z,rlong0z,thetaz
      write(iout,702)ijdate,mz2,nz2,dxz,dyz,rlat0z,rlong0z,thetaz


      if ( mz2 .ne. m .or. nz2 .ne. n .or.
     .     abs (rlat0z-rlat0).gt. slat .or.
     .     abs(thetaz - theta * 180./acos(-1.0) ).gt. slat .or.
     .     abs (abs(rlong0z) - abs(rlng0) ) .gt. slon ) then
c           ier = 1
           write(iout,703)dssbl
c           goto 5
      endif
 
c
c --  read fields
c
      msq0 = msq

      icnt = 0
 1    continue

      call wread(rml,ms0,msq0,a,ivt,level,al,al,isbl,jcnt,ier)

      if ( ier.gt.0 ) goto 2
      if ( ier.lt.0 ) goto 3
      icnt = icnt + 1
      if( icnt .eq. 1) ivtemp = ivt
c
      if (ivt .eq. ivtemp ) then
            do 101 ilevel = 1,l
               if ( al .eq. -zml(ilevel) ) goto 102
 101        continue

            goto 3
 102        continue            
            intgate(ilevel) = 1
            do 10 ip = 1,msq
               temp(ip,ilevel,1) = rml(ip)
               temp(ip,ilevel,2) = rml(ip)
               buoy(ip,ilevel,1) = ag*rml(ip)
               buoy(ip,ilevel,2) = ag*rml(ip)
 10         continue

      elseif ( ivt .eq. ivdepth ) then
         indepth = 1
c
c -- set mix layer depth in grid
c
         h1= hmin
         hb= maxh
         iabove = 0
         ibelow = 0
         do 30 ip = 1,msq
            hv = rml(ip)
            if(hv.lt.h1) then 
                iabove = iabove + 1
                hv = h1
            elseif(hv.gt.hb) then
                ibelow = ibelow + 1
                hv=hb
            endif
            out(ip)     = hv
            hdpth(ip,1) = hv
            hdpth(ip,2) = hv
 30      continue
         if (( ibelow + iabove) .gt. 0 ) then
            write(iout,1010)iabove,h1,ibelow,hb
         endif

      endif
      goto 1
2     continue
c
c -- check if all the fields are in
c
      ier = 0
      if (indepth .eq. 0)  then
          write(iout,1004)
          ier = 1
      endif

      do 50 i = 1,l
         if ( intgate(i) .eq. 0) then
            ier = 1
            write(iout,1003)i
         endif
 50   continue                       

      if ( ier .eq. 0 ) goto 6


 3    continue
     
      write(iout,750)dssbl,ier
      
      goto 5

904   continue
      write ( iout, 699)
      goto 5
905   continue
      write ( iout ,700)dssbl
      goto 5
 4    continue
      write (iout,701)dssbl
 5    continue
      close (iout)
      write(iout,*) ' Error opening Initial TempH file'
      stop
 6    continue
      close(isbl)
c---------------------------------------------
510   format(' Id 'i5,
     * ' Level [',i2,',',i2,'] D=',f8.2,'(m)',/,
     * ' T(',i6,') = ',e18.7,
     * ' - - - - - - - - - -  ')
525   format(10x,'Mixed Layer Depth ',5x,i5,/)
 699  format(' error while reading TS file name from SBL file ')
 700  format(' error while opening file ',a)
 701  format(' error while reading header of file ',a)
 702  format('julian date ',i8,' grid size m ',i4,' n ',i4,//,
     . '         dx ',f10.4,'  dy ',f10.4,/,
     . '        lat ',f10.4,' lon ',f10.4,/,
     . '        angle of rotation ',f10.4//)
 703  format(' WARNING: some of the parameters  above read from ',/,
     .a,' do not match the parameters already defined in the model')
 750  format(5x,' error while reading ',a,' with error = ',i5)
 760  format('   Number of fields read ',i3)
 900  format(a)
1001  format('    field index ',i5,' has the incorrect depth ',f10.3)
1003  format('    Temperature field at level ',i5,' is missing')
1004  format('    Mixed layer depth field is missing')
1005  format( ' value at grid point ',i6,' = ',e18.7,/,
     . ' - - - - - - - - - - - - - - - - - - - - - - - - - - ')
1010  format(' There were ',i5,' points  with depth above ',f6.2,
     .       '; and ',i5,' points with depth below ',f6.2//,
     .  '  Initial Mixed Layer values were reset within this range ')
      end
