      subroutine wdselect(x,z,dz,xorder,iorder,jmin,jmax,converg)
*
*  find the 'coordinates' in the P, T grid of four points that enclose 
*  a specified pressure and temperature.
*  NOTE: if you try to go off the grid, failsafe is visited.
*  do it too many times, and the program quits.
*
      implicit double precision(a-h,o-z)

      common/iii/ iii
      common /ifail/ifail
      logical converg
      real*8 xorder(1)
      integer iorder(1)

      itest=50
      j=(x-z)/dz+1.5e0
      zmax = jmax * dz + z
      if ( j .le. jmin ) then 
         j=jmin
         iii=iii+1
         if ( converg .and. iii .ge. itest ) then 
            stop
         endif
         ifail = ifail + 1
      elseif ( j .gt. jmax ) then
         j=jmax
         iii=iii+1
         if ( iii .ge. itest ) then
            stop
         endif
         ifail = ifail + 1
      endif

      ii=j
      jl=0
      jr=0
      do 16 i=1,4
         xorder(i)=z+(ii-1)*dz
         iorder(i)=ii
         if(j+jr-jmax) 12,15,12
12       if(j-jl-jmin) 13,14,13
13       if((xorder(i)-x)*dz) 14,15,15
14       jr=jr+1
         ii=j+jr
         go to 16
15       jl=jl+1
         ii=j-jl
16    continue

      return
      end 

************************************************************************
*                                                                      *
*     subroutine write1 is: entry write1 in subroutine read2           *
*                                                                      *
************************************************************************
*                                                                      *
*     subroutine write2 is: entry write2 in subroutine read2           *
*                                                                      *
************************************************************************
*                                                                      *
*     subroutine write3 is: entry write3 in subroutine read2           *
*                                                                      *
************************************************************************


