      SUBROUTINE XBTbias_gour_rese_new(itsprobe,depth,temp,maxlevel,
     * maxcalc,numsec,numlevels,iyearx,month,ixprobe,bmiss,ifix,iplat)

C     XBTBIASgour CORRECTS XBT BIAS BASED ON INFORMATION FROM
C     GOURETSKI AND RESEGHETTI 2010  THE CORRECTIONS ARE MADE BEFORE
C     INTERPOLATION TO STANDARD LEVELS
      parameter (maxyear=50,maxset=40,n46=8,n7db=8,n5=3,n10=4)
      parameter (maxdepth=3000,monthx=12,ntno=4)
      parameter (ntmax=4)
      character*21 firsttime
      dimension depth(maxlevel),temp(maxlevel,maxcalc)
      dimension itype46(n46),itype7db(n7db)
      dimension itype5(n5),itype10(n10)
      dimension itypeno(ntno)
      dimension thermal_bias(maxyear,monthx,ntmax)
      dimension dstretch(maxyear,monthx,maxdepth,ntmax)
      dimension t_bias(maxyear,ntmax)
c     dimension d_bias(maxyear,ntyp)
      dimension coeff1(maxyear,ntmax),coeff2(maxyear,ntmax)

      data itype46 /202,203,208,209,215,216,223,226/
      data itype7db /201,207,214,217,227,228,232,235/
      data itype5 /210,219,224/
      data itype10 /205,211,220,229/
      data itypeno /234,233,204,225/


      save firsttime
      save thermal_bias,dstretch,coeff1,
     * coeff2,t_bias,nyearmin,nyearmax,myearmin,myearmax,
     * oyearmax,oyearmin
      if ( firsttime .ne. 'xbtbias_gour_rese_new' ) then
       firsttime = 'xbtbias_gour_rese_new'

      call readgournew1(maxyear,monthx,ntmax,thermal_bias,nyearmin,
     * nyearmax)
      call readgournew2(maxyear,monthx,ntmax,thermal_bias,nyearmin,
     * nyearmax)
      call readgournew3(maxyear,monthx,ntmax,thermal_bias,myearmin,
     * myearmax)
      call readgournew4(maxyear,monthx,maxdepth,ntmax,dstretch,
     * nyearmin,nyearmax)
      call readgournew5(maxyear,monthx,maxdepth,ntmax,dstretch,
     * oyearmin,oyearmax)
      call readgournew6(maxyear,monthx,maxdepth,ntmax,dstretch,
     * myearmin,myearmax)
      call readgournew7(maxyear,ntmax,coeff1,coeff2,t_bias,
     * nyearmin,nyearmax)
      endif
       ntyp=0
       ifix=0
      if ( numlevels .le. maxlevel ) then
       dpt=depth(numlevels)
      else
       call overmax(maxlevel,numsec,numlevels,0,ic,np)
       dpt=temp(ic,np)
      endif
       shalmax=550.
c      write(6,*) 'entershallowdeep'
       call xbtshallowdeep(ishallow,ixprobe,dpt,shalmax,iplat)
c      write(6,*) 'ishallow',ishallow
c       if (ishallow .eq. -1) return
c      if ( dpt .gt. 960. ) return
      idpt=dpt
      iyear=iyearx
      if ( iyearx .lt. 1967 ) iyear=1967
      if ( iyearx .gt. nyearmax ) iyear=nyearmax
c     write(6,*) 'iyear,nyearmin',iyear,nyearmin
       if (itsprobe .eq. 2) then
c      write(6,*) 'itsprobe',itsprobe,ixprobe
        if (ixprobe .eq. 2) then       
         ntyp=0
        elseif ( ixprobe .ne. 2 .and. ixprobe .ne. 0) then
        do 150 nn=1,n46
150      if ( ixprobe .eq. itype46(nn) ) ntyp=1
        do 155 nn=1,n7db
155      if ( ixprobe .eq. itype7db(nn) ) ntyp=2
        do 160 nn=1,n5
160      if ( ixprobe .eq. itype5(nn) ) ntyp=3
c       write(6,*) 'ixp',ixprobe,ntyp
        do 165 nn=1,n10
165      if ( ixprobe .eq. itype10(nn) ) ntyp=4
        do 170,nn=1,ntno
170      if ( ixprobe .eq. itypeno(nn) ) ntyp=0
        endif
 
        if (ntyp .eq. 0) then
        if (ishallow .eq. 1) ntyp=1
        if (ishallow .eq. 0) ntyp=2
        endif
        if (ntyp .eq. 2) then
        if (dpt .gt. 960) return
c        if (iyearx .lt. 1970) then
c        dstretch(iyear,month,idepth,ntyp)=dstretch(inyear,month,
c     * idepth,ntyp)
c        endif
        endif
        if (ntyp .eq. 3) then
        if (iyearx .lt. 1984 ) iyear=1984
        endif
        endif 
c      endif
c      write(6,*) 'ixprobe,iyearx,iyear', ixprobe,iyear,iyear
c     write(6,*) 'ntyp',ntyp
c     write(6,*) 'iyearmin,iyearmax',iyearmin,iyearmax
       ifix=1
       if (ntyp .ne. 4 ) then
       do 200 k=1,numlevels
       idepth=depth(k)
       depth(k)=depth(k)*(dstretch(iyear,month,idepth,ntyp))
       temp(k,1)=temp(k,1)-thermal_bias(iyear,month,ntyp)
c       write(6,*) 'iyearx,month',iyear,month,
c     * ixprobe,idpt,idepth
c     * month,thermal_bias(iyear,month,ntyp),dstretch(iyear,
c     *month,idepth,ntyp),idepth,ixprobe
c      write(6,*) 'iyearx,month,thermal_bias,dstretch,depth',iyear,
c    * month,thermal_bias(iyear,month,ntyp),dstretch(iyear,
c    * month,idepth,ntyp),idepth,iyear,month,idepth,ntyp
200    continue
c       write(6,*) 'iyear,month,ntyp', iyear,month,ntyp,
c     * thermal_bias(iyear,month,ntyp),dstretch(iyear,month,depth(100),
c     * ntyp),depth(100)
       else
       do 400 k=1,numlevels
       d_bias= -1.*(coeff1(iyear,ntyp)+
     * coeff2(iyear,ntyp)*depth(k))
       depth(k)=depth(k)-d_bias
       temp(k,1)=temp(k,1)-t_bias(iyear,ntyp)
c       write(6,*) 'iyearx,month,iuniq',iyear,month,
c      * ixprobe,idpt,idepth,iuniq
400    continue
      endif
c     write(6,*) dstretch(iyear,month,depth(k),ntyp)
      return
      end

