      SUBROUTINE XBTbiaslijing2(depth,temp,maxlevel,maxcalc,
     * itsprobe,ixbttype,iyear0,numsec,numlevels,bmiss,iwrite,
     * ifix, xswitch,iplat)

      parameter (maxyear=3000,ntmax=9)
      parameter (maxlevelloc=6000)
      parameter (n46=8,n7db=8,nfd=2,n10=3,n5=3,n11=2)
      parameter (a_h95=6.691, b_h95=0.00225)
      parameter (a_t5=6.828, b_t5=0.00182)
      parameter (a_t10=6.301,b_t10=0.00216)

      character*14 firsttime

      dimension tbias1(maxyear,ntmax)
      dimension acorr1(maxyear,ntmax)
      dimension depth(maxlevel),temp(maxlevel,maxcalc)
      dimension t0(maxlevelloc),d1(maxlevelloc)

      dimension a1(ntmax),a2(ntmax)
      dimension b1(ntmax),b2(ntmax)
      dimension at1(ntmax),tta(ntmax)
      dimension ttb(ntmax)

      dimension itype46(n46),itype7db(n7db),itypefd(nfd)
      dimension itype10(n10),itype5(n5),itype11(n11)

      data itype46 /202,203,208,209,215,216,223,226/
      data itype7db /201,207,214,217,227,228,232,235/
      data itypefd /213,236/
      data itype11 /206,212/
      data itype10 /205,211,220/
      data itype5 /204,210,219/

      common /eachparm/ numeach(6),numtot2,numtot3,ntax,numsecset

      save firsttime
      save tbias1,acorr1,nyearmin,
     * nyearmax,a1,a2,b1,b2,at1,tta,ttb

      if ( firsttime .ne. 'XBTbiaslijing2' ) then
       firsttime='XBTbiaslijing2'

c     read in corrections
       call readlijingtable_coeff(ntmax,a1,a2,b1,b2,at1,tta,ttb)
       call readlijingtable1(maxyear,ntmax,acorr1,nyearmin,
     *  nyearmax)
       call readlijingtable2(maxyear,ntmax,tbias1)
      endif

c     Determine probe type

      ifix=0
      if ( itsprobe .ne. 2 ) return

c      ntyp is class of XBT for correction
      ntyp=0

c   ntyp definitions
c    1: T7/DB
c    2: unk_deep
c    3: T4/T6
c    4: unk_shallow
c    5: T10
c    6: T5
c    7: TSK T4/T6
c    8: TSK T5
c    9: TSK T7

c      Determine shallow or deep

      if ( numlevels .le. maxlevel ) then
       dpt=depth(numlevels)
      else
       call overmax(maxlevel,numsec,numlevels,0,ic,np)
       dpt=temp(ic,np)
      endif

      call xbtshallowdeep(ishallow,ixbttype,dpt,550.,iplat)
c     write(6,*) 'xbtshall',ixbttype,iplat,ishallow

c     Do not adjust if ishallow=-1 (different probe types)


      ihanawa=-1

      do 704 nn=1,n5
       if ( itype5(nn) .eq. ixbttype) then
        ntyp=6
        ihanawa=6
       endif
704   continue

      if ( ishallow .eq. -1 .and. ntyp .ne. 6) return
      ifix=1

c      Check if this is a TSK

      itsk=istsk(ixbttype)

c      identify XBT class for correction

      do 700 nn=1,n7db
700    if ( itype7db(nn) .eq. ixbttype) ntyp=1

      do 701 nn=1,n46
701    if ( itype46(nn) .eq. ixbttype) ntyp=3

c    fast deep now part of unknown
      do 702 nn=1,nfd
       if ( itypefd(nn) .eq. ixbttype) then
        ntyp=0
c       ihanawa=3
c     if depth > 930 m treat as T5, otherwise as unknown
        if ( dpt .ge. 930. ) ihanawa=6
       endif
702   continue

      do 703 nn=1,n10
       if ( itype10(nn) .eq. ixbttype) then
        ntyp=5
        ihanawa=5
       endif
703   continue

      do 705 nn=1,n11
       if ( itype11(nn) .eq. ixbttype) then
        ntyp=0
c       ihanawa=4
       endif
705   continue
 
      if ( ntyp .eq. 0 ) then
       if ( ishallow .eq. 1 ) ntyp=4
       if ( ishallow .eq. 0 ) ntyp=2
      elseif ( itsk .ne. 0 ) then

       if ( ntyp .eq. 1 ) ntyp=9
       if ( ntyp .eq. 3 ) ntyp=7
       if ( ntyp .eq. 6 ) ntyp=8

      endif

c     get mean temperature 0-100m, full profile

      call lijing2_meant(ishallow,temp,maxlevel,maxcalc,
     * numlevels,bmiss,depth,nbinmax,xmeanall,xmeans)

      iyear=iyear0
      if ( iyear .lt. nyearmin ) iyear=nyearmin
      if ( iyear .gt. nyearmax ) iyear=nyearmax

      afull=a_h95
      bfull=b_h95

c    ihanawa=1 needs
      if ( ihanawa .eq. -1 ) then
       if ( xswitch .eq. 1. ) then
        ihanawa=1
       else
        ihanawa=0
       endif
      endif

      if ( ntyp .eq. 6 .or. ntyp .eq. 8 ) then  
       afull=a_t5
       bfull=b_t5
      elseif ( ntyp .eq. 5 ) then
       afull=a_t10
       bfull=b_t10
      endif

      xa=(afull+acorr1(iyear,ntyp))+(xmeans*at1(ntyp))
      xb=(xa*a1(ntyp)) - a2(ntyp)
      xc=(xa*b1(ntyp)) - b2(ntyp)
c     Change: Jul 3, 2017: temp_original instead of
c     xmeanall is used.  Calculation done below
c     tb=tbias1(iyear,ntyp)+((xmeanall*tta(ntyp))+ttb(ntyp))
c     if ( iwrite .eq. 1 ) 
c     write(6,*) 'bias',iyear,ixbttype,ntyp,xa,xb,xc,tb
c     write(6,*) 'xm',xmeanall,xmeans,acorr1(iyear,ntyp),
c    * tta(ntyp),a1(ntyp),a2(ntyp),b1(ntyp),b2(ntyp),
c    * tbias1(iyear,ntyp),ttb(ntyp),at1(ntyp)
c     write(6,*) 'xsw',xsw

c     make depth/temperature corrections

      call timefromdepth(numlevels,t0,maxlevel,ihanawa,depth)
      call droprate_apply(xa,xb,numlevels,d1,maxlevel,t0)
c     write(6,*) 'H95',t0(14),xa,xb,xc,d1(14),d1(14)-xc

      do 80 n=1,numlevels

       if ( n .le. maxlevel ) then
        depth(n)=d1(n)-xc
        tb=tbias1(iyear,ntyp)+((temp(n,1)*tta(ntyp))+ttb(ntyp))
        temp(n,1)=temp(n,1)-tb
       else
        call overmax(maxlevel,numsec,n,0,ic,np)
        temp(ic,np)=temp(ic,np)+d1(n)-xc
        tb=tbias1(iyear,ntyp)+((temp(ic,np+1)*tta(ntyp))+ttb(ntyp))
        temp(ic,np+1)=temp(ic,np+1)-tb
       endif

80    continue

      return
      end
