C ESP58.f
C
C VIS-5D analysis function to compute thickness 500-850 hPa (in m)

      INTEGER FUNCTION USERFUNC( OUTGRID, OUTNL, OUTLOWLEV,
     *                           INGRID, NR, NC, NL, LOWLEV, MAXNL,	
     *                           NVARS, NAMES,
     *                           DATE, TIME,
     *                           PROJECTION, PROJ_ARGS,
     *                           VERTICAL, VERT_ARGS )
C     ARGUMENTS:
	implicit none
      INTEGER NVARS
      INTEGER NR, NC, NL(NVARS), LOWLEV(NVARS), MAXNL
      REAL OUTGRID(NR,NC,MAXNL)
      INTEGER OUTNL, OUTLOWLEV
      REAL INGRID(NR,NC,MAXNL,NVARS)
      CHARACTER*8 NAMES(NVARS)
      INTEGER DATE, TIME
      INTEGER PROJECTION
      REAL PROJ_ARGS(*)
      INTEGER VERTICAL
      REAL VERT_ARGS(*)

C     LOCAL VARS:
      integer var, ir, ic, il
      integer ig,il850,il500
	real p

C     Specify number of levels in OUTGRID
      OUTNL = 1
      OUTLOWLEV = 0

C   Find the H variable
      ig = -1
      do var=1,nvars
         if (names(var) .eq. 'H') ig = var
      end do
      
C   If H not found, return error 1
      if (ig .eq. -1) then
         print *, "Couldn't find H vars!"
         userfunc = 1
         return
      endif
            
      do il=1,maxnl
       p=1012.5*exp(vert_args(il)/-7.2)
       If ((p.Gt.849.).And.(p.Lt.851.)) Then
        il850=il
       Endif
       If ((p.Gt.499.).And.(p.Lt.501.)) Then
        il500=il
       Endif
      enddo


C     Compute espessor
         do ic=1,nc
            do ir=1,nr
		do il=1,maxnl		
               outgrid(ir,ic,il) =ingrid(ir,ic,il500,ig)-
     & ingrid(ir,ic,il850,ig)
		enddo
            end do
         end do
      USERFUNC = 0
      RETURN
      END
