C EFGWTIL.f
C
C VIS-5D analysis function to compute Tilting term of Escalar Frontogenesis 
C with wet-bulb
C potential temperature instead of potential temperature (in 1.E-9 K / s 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,imap
      integer iu, iv, iw, it, ihum
      real Pi,Rt,Per,thetax,thetay,thetap,thetagrad,ux,uy,vx,vy,
     $     omex,omey,exnf,tem,ds
      real tpi,ews,td,tcp,VAPFW,DPTOF,TCONOF,WOBF
      real p(22),rocp,rg,g,xincx,xincy
      parameter (Pi=3.141596,Rt=6371227.,Per=86400.,rocp=2./7.,
     $           rg=287.04,g=9.807)
      real ome(200,200,50), theta(200,200,50)
      real EFGTIL
      

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


C     Find the U, V, W, T, RH and MAPFACCR variables
      iu = -1
      iv = -1
      iw = -1
      it = -1
      ihum = -1
      imap = -1
      do var=1,nvars
         if (names(var) .eq. 'U') iu = var
         if (names(var) .eq. 'V') iv = var
         if (names(var) .eq. 'W') iw = var         
         if (names(var) .eq. 'T') it = var
         if (names(var) .eq. 'RH') ihum = var         
         if (names(var) .eq. 'MAPFACCR') imap = var              
      end do

C     If U, V, W, T, RH or MAPFACCR not found, return error 1
      if (iu .eq. -1 .or. iv .eq. -1.or. iw .eq. -1 .or. it .eq. -1
     $    .or. ihum .eq. -1 .or. imap .eq. -1) then
         print *, "Couldn't find U, V, W, T, RH or 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 Calculo de P (Pa) y Conversion de W a ome (barica, en Pa/s), y calculo de
C wet bulb potential temperature theta (K) 
      do il=1,maxnl
       p(il)=1012.5*exp(vert_args(il)/-7.2)*100.
       exnf=(100000./p(il))**rocp   
        do ic=1,nc
         do ir=1,nr
          tem=ingrid(ir,ic,il,it)+273.16
          ome(ir,ic,il)=-p(il)*g*ingrid(ir,ic,il,iw)/
     $                  (100.*rg*tem)
          tpi=tem*exnf
          EWS=VAPFW(ingrid(ir,ic,il,it))
	  TD=DPTOF(EWS*ingrid(ir,ic,il,ihum)/100.)
	  TCP=TCONOF(ingrid(ir,ic,il,it),TD)
	  theta(ir,ic,il)=tpi-WOBF(tpi)+WOBF(TCP)	                    
         enddo
        enddo
      enddo  

      xincx=2.*ds
      xincy=2.*ds

c Calculo del EFG en 1.E-9 K / m s 
        do 105 il=1,maxnl
        
         do ic=2,nc-1
           do ir=2,nr-1
	     ux=(ingrid(ir,ic+1,il,iu)-ingrid(ir,ic-1,il,iu))/xincx
             ux=ux*ingrid(ir,ic,1,imap)	               
	     uy=(ingrid(ir-1,ic,il,iu)-ingrid(ir+1,ic,il,iu))/xincy
             uy=uy*ingrid(ir,ic,1,imap)	               
	     vx=(ingrid(ir,ic+1,il,iv)-ingrid(ir,ic-1,il,iv))/xincx
             vx=vx*ingrid(ir,ic,1,imap)	               
	     vy=(ingrid(ir-1,ic,il,iv)-ingrid(ir+1,ic,il,iv))/xincy
             vy=vy*ingrid(ir,ic,1,imap)	            
	     omex=(ome(ir,ic+1,il)-ome(ir,ic-1,il))/xincx
             omex=omex*ingrid(ir,ic,1,imap)	               
	     omey=(ome(ir-1,ic,il)-ome(ir+1,ic,il))/xincy
             omey=omey*ingrid(ir,ic,1,imap)	                        
	     thetax=(theta(ir,ic+1,il)-theta(ir,ic-1,il))/xincx
             thetax=thetax*ingrid(ir,ic,1,imap)	               
	     thetay=(theta(ir-1,ic,il)-theta(ir+1,ic,il))/xincy
             thetay=thetay*ingrid(ir,ic,1,imap)	               
             thetagrad=Sqrt(thetax*thetax+thetay*thetay) 
	if (il.eq.1) then
	     thetap=(theta(ir,ic,1)-theta(ir,ic,2))/(p(1)-p(2))
	else if (il.eq.maxnl) then
	     thetap=(theta(ir,ic,maxnl-1)-theta(ir,ic,maxnl))/
     &              (p(maxnl-1)-p(maxnl))
	else
	     thetap=((p(il)-p(il+1))*(theta(ir,ic,il-1)-
     &           theta(ir,ic,il))/(p(il-1)-p(il))+(p(il-1)-p(il))*
     &           (theta(ir,ic,il)-theta(ir,ic,il+1))/(p(il)-p(il+1)))/
     &           (p(il-1)-p(il+1))
	endif
	  EFGTIL=-(1./thetagrad)*thetap*(omex*thetax+omey*thetay)
	  outgrid(ir,ic,il) = EFGTIL*1.E9
          enddo
        enddo 
     

        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(nr,nc,il)=2.*outgrid(nr-1,nc-1,il)-outgrid(nr-2,nc-2,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)

105      end do

      USERFUNC = 0
      RETURN
      END



c ===========================================================================
C	----------------------------------------------------------
C	ALGORITMO PARA CALCULAR LA FUNCION DE WOBUS
C	ENTRADA TEMPERATURA EN C

	FUNCTION WOBF(T)
	
	X=T-20.
	IF(X) 10,10,20
10	CONTINUE

	POL=1.+X*(-8.8416605E-3+X*(1.4714143E-4+X*(-9.671989E-7
     #  +X*(-3.2607217E-8+X*(-3.8598073E-10)))))
	POL=POL*POL
	WOBF=15.130/(POL*POL)
	RETURN

20	POL=1.+X*(3.6182989E-3+X*(-1.3603273E-5+X*(4.9618922E-7
     #  +X*(-6.1059365E-9+X*(3.9401551E-11+X*(-1.2588129E-13
     #  +X*(1.668828E-16)))))))
	POL=POL*POL
	WOBF=29.930/(POL*POL)+.96*X-14.8
	RETURN
	END
c ===========================================================================


c ===========================================================================
C	ALGORITMO PARA CALCULAR LA PRESION DE VAPOR SATURANTE
C	RESPECTO AL AGUA EN hPa.
c	ENTRADA TEMPERATURA EN C.

	FUNCTION VAPFW(T)

	X=T
	IF(100.-X)3,4,4

3	X=X-273.16

C	AJUSTE DE LA CURVA ENTRE -50 < T < 100
4	POL=.99999683E-0+X*(-.90826951E-2+X*(.78736169E-4+
     1	X*(-.61117958E-6+X*(.43884187E-8+X*(-.29883885E-10+
     2	X*(.21874425E-12+X*(-.17892321E-14+X*(.11112018E-16+
     3	X*(-.30994571E-19)))))))))
	POL=POL*POL
	POL=POL*POL
	VAPFW=6.1078/(POL*POL)
	RETURN
	END
c ===========================================================================


c ===========================================================================
C	ALGORITMO PARA INVERTIR LA FORMULA DE TETENS
C	CALCULA LA TEMPERATURA EN GRADOS C DADA LA PRESION DE VAPOR
C	SATURANTE EN hPa

	FUNCTION DPTOF(EW)

	TOL=.0001
	IF (EW-.21382876E-9)20,20,30

20	DPTOF=-10000.
	RETURN

30	IF (1013.-EW)20,100,100

100	X=ALOG(EW/6.1078)
	BOT=17.269388-X
	DPTOF=(273.3*X)/BOT
	BOT=BOT*EW
200	EDP=VAPFW(DPTOF)
	DTDE=(DPTOF+237.7)/BOT
	DELT=DTDE*(EW-EDP)
	DPTOF=DPTOF+DELT

	IF(ABS(DELT)-TOL)300,300,200

300	DPTOF=DPTOF-TOL
	RETURN
	END
c ===========================================================================

c ===========================================================================
C	-------------------------------------------------------------
C	ALGORITMO PARA CALCULAR LA TEMPERATURA DE CONDENSACION
C	PO ELEVACION. ENTRADA TEMPERATURA Y PUNTO DE ROCIO AMBOS
C	EN C o K. LA TEMPERATURA DE CONDENSACION SE DEVUELVE EN C.


	FUNCTION TCONOF(TEMP,DEWPT)
	S=TEMP-DEWPT
	T=TEMP
	IF(100.0-TEMP)4,5,5
4	T=TEMP-273.16
C	AJUSTE DE UNA CURVA
5	DLT=S*(1.2185+.001278*T+S*(-.00219+11.73E-6*S-5.2E-6*T))
	TCONOF=T-DLT
	RETURN
	END
c ===========================================================================


