C TTp3.f
C
C VIS-5D function to calculate COMPONENT 3 OF PERTURBATION top temperature 
C (TTp3). 

C See below ROMU: For personal choices on variables etc...
C ==============================================================================

      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
      integer it,itm  

      real TTp(200,200),TTp3(200,200)

C ==============================================================================
      
C Specify number of levels in OUTGRID
      OUTNL = 1
      OUTLOWLEV = maxnl-1
      
C Find the T, Tm variables (TOTAL and MEAN fields)
      it = -1
      itm = -1
      do var=1,nvars
        if (names(var) .eq. 'T') it = var 
        if (names(var) .eq. 'Tm') itm = var                    
      end do

C If T, Tm not found, return error 1
      if ( it.eq.-1 .or. itm.eq.-1 ) then
        print *, "Couldn't find T or Tm vars!"
        userfunc = 1
        return
      endif
      
C ==============================================================================

C Calculate perturbation TTp (C), from which to define component TTp3
   
      Do 33 ir=1,nr
      Do 33 ic=1,nc
        TTp(ir,ic)=ingrid(ir,ic,maxnl,it)-ingrid(ir,ic,maxnl,itm)
33    Continue
      print*,'Calculated TTp'
      
      
C CALCULATION OF COMPONENT 3 OF PERTURBATION TOP TEMPERATURE TTp3 (C) 
C =============================> ROMU: Define condition/function of TTp3 below 

      DO 20 ir=1,nr
      DO 20 ic=1,nc

C TTp3 SUPERSTORM
c        TTp3(ir,ic)=TTp(ir,ic)*0.

C TTp3 VALENCIA
        TTp3(ir,ic)=TTp(ir,ic)*0.

20    CONTINUE

      print*,'Calculated TTp3'
 
C ==============================================================================

C OUTPUT OF COMPONENT 3 OF PERTURBATION TOP TEMPERATURE TTp3 (in C)

      print*,
     $'Output of COMPONENT 3 OF PERTURBATION TOP TEMPERATURE (C)'
      DO 9999 ir=1,nr
      DO 9999 ic=1,nc
        outgrid(ir,ic,OUTNL)=TTp3(ir,ic)
9999  CONTINUE    

C ==============================================================================
               
      USERFUNC = 0
      RETURN
      END

C ==============================================================================
