C UISAL.f
C
C VIS-5D analysis function to compute U-Component of Isallobaric wind 
C (in m/s)

      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,ilat,imap
      integer iuag,iug,ivg
      real lat,Pi,Rt,Per,cor,uisal,ds
      parameter (Pi=3.141596,Rt=6371227.,Per=86400.)

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

C     Find the Geupot variable
      iuag = -1
      iug = -1
      ivg = -1
      ilat = -1
      imap = -1         
      do var=1,nvars
         if (names(var) .eq. 'UAG') iuag = var
         if (names(var) .eq. 'UG') iug = var
         if (names(var) .eq. 'VG') ivg = var
         if (names(var) .eq. 'LATITCRS') ilat = var
         if (names(var) .eq. 'MAPFACCR') imap = var           
      end do

C     If U, or V not found, return error 1
      if (iuag.eq.-1.or.iug.eq.-1.or.ivg.eq.-1
     $    .or. ilat.eq.-1 .or. imap.eq.-1) then
         print *, "Couldn't find UAG,UG,VG or LATITCRS, MAPFACCR vars!"
         userfunc = 1
         return
      endif
      
      if (projection.Eq.2) then
        ds=proj_args(6)*1000.        
      else if (projection.Eq.3) then  
        ds=proj_args(5)*1000. 
      else
        print *, "Uncomprensible projection data!"
        userfunc = 1
        return
      endif

C     Compute Geostrophic absolute vorticity
      do il=1,maxnl
         do ic=2,nc-1
            do ir=2,nr-1
	    lat=ingrid(ir,ic,1,ilat)*Pi/180.
	    cor=4.*Pi/Per*Sin(lat)
	    uisal=ingrid(ir,ic,il,iuag)+(ingrid(ir,ic,il,iug)*
     & (ingrid(ir,ic+1,il,ivg)-ingrid(ir,ic-1,il,ivg))/
     &(2.*ds)+
     &ingrid(ir,ic,il,ivg)*(ingrid(ir-1,ic,il,ivg)-
     &ingrid(ir+1,ic,il,ivg))/(2.*ds))/cor         		
               outgrid(ir,ic,il) = uisal*ingrid(ir,ic,1,imap)
            end do
         end do
        do ir=2,nr-1
	   outgrid(ir,1,il)=2.*outgrid(ir,2,il)-outgrid(ir,3,il)
	   outgrid(ir,nc,il)=2.*outgrid(ir,nc-1,il)-outgrid(ir,nc-2,il)
	enddo
	do ic=2,nc-1
	  outgrid(1,ic,il)=2.*outgrid(2,ic,il)-outgrid(3,ic,il)
	  outgrid(nr,ic,il)=2.*outgrid(nr-1,ic,il)-outgrid(nr-2,ic,il)
	enddo
	outgrid(1,1,il)=2.*outgrid(2,2,il)-outgrid(3,3,il)
	outgrid(1,nc,il)=2.*outgrid(2,nc-1,il)-outgrid(3,nc-2,il)
       outgrid(nr,1,il)=2.*outgrid(nr-1,2,il)-outgrid(nr-2,3,il)
       outgrid(nr,nc,il)=2.*outgrid(nr-1,nc-1,il)-outgrid(nr-2,nc-2,il)
      end do
C	print*, "Escalat en *1.e+5"
      USERFUNC = 0
      RETURN
      END
