*=*=*=*= sortvarc.html =*=*=*=*
SUBROUTINE sortvarc

SUBROUTINE sortvarc


      SUBROUTINE sortvarc
     $(itau,ucov,h,pext,pks,phis,pbarx,pbarxy,vorpot,phi,bern,dp
     . ,time)
      IMPLICIT NONE

c=======================================================================
c
c   Auteur:    P. Le Van
c   -------
c
c   Objet:
c   ------
c
c   sortie des variables de controle
c
c=======================================================================
c-----------------------------------------------------------------------
c   Declarations:
c   -------------

#include "dimensions.h"
#include "paramet.h"
#include "comconst.h"
#include "comvert.h"
#include "comgeom.h"
#include "ener.h"
#include "logic.h"
#include "temps.h"

c   Arguments:
c   ----------

      INTEGER itau
      REAL ucov(ip1jmp1,llm),h(ip1jmp1,llm)
      REAL pext(ip1jmp1),pks(ip1jmp1),phis(ip1jmp1)
      REAL pbarx(ip1jmp1),pbarxy(ip1jm),vorpot(ip1jm,llm)
      REAL phi(ip1jmp1,llm),bern(ip1jmp1,llm)
      REAL dp(ip1jmp1)
      REAL time

c   Local:
c   ------

      REAL vor(ip1jm),bernf(ip1jmp1,llm),ztotl(llm)
      REAL etotl(llm),stotl(llm),rmsvl(llm),angl(llm),ge(ip1jmp1)
      REAL pbarcosp(ip1jm),omegcosp(ip1jm)
      REAL dtvrs1j,rjour,heure,radsg,radomeg,cosphi
      REAL rday
      INTEGER  l, ij, imjmp1

      EXTERNAL  filtreg
c     EXTERNAL FLUSH
      EXTERNAL   SSUM, SCOPY
      REAL       SSUM

c-----------------------------------------------------------------------

       dtvrs1j   = dtvr/daysec
       rjour     = float( int( itau * dtvrs1j ))
       heure=(itau*dtvrs1j-rjour)*24
       imjmp1    = iim * jjp1

c   .....  Calcul  de  rmsdpdt  .....

       CALL multipl(ip1jmp1,dp,unsaire,ge)
       CALL multipl(ip1jmp1,ge,ge,ge)
       rmsdpdt=SSUM(ip1jmp1,ge,1)-SSUM(jjp1,ge,iip1)
c
       rmsdpdt = daysec* 1.e-2 * SQRT(rmsdpdt/imjmp1)
c    ...  Modif  P.Le Van  ( 8/09/95 )  ...

       CALL SCOPY( ijp1llm,bern,1,bernf,1 )
       CALL filtreg(bernf,jjp1,llm,-2,2,.true.,1)

c   .....  Calcul du moment  angulaire   .....

       radsg=rad /g
       radomeg=rad*omeg
       DO ij=iip2,ip1jm
          cosphi= COS(rlatu((ij-1)/iip1+1))
          pbarcosp(ij) = pbarx(ij) * cosphi
          omegcosp(ij) = radomeg   * cosphi
       ENDDO

c  ...  Calcul  de l'energie,de l'enstrophie,de l'entropie et de rmsv  .

       DO l=1,llm
          DO ij = 1,ip1jm
             vor(ij)=vorpot(ij,l)*vorpot(ij,l)*pbarxy(ij)*dsig(l)
          ENDDO
          ztotl(l)=dsig(l)*(SSUM(ip1jm,vor,1)-SSUM(jjm,vor,iip1))

          DO ij = 1,ip1jmp1
             ge(ij)=pext(ij)*(phis(ij)+h(ij,l)*pks(ij)*
     s       s(l)+bernf(ij,l)-phi(ij,l))
          ENDDO
          etotl(l)=(SSUM(ip1jmp1,ge,1)-SSUM(jjp1,ge,iip1))*dsig(l)

          DO ij = 1,ip1jmp1
             ge(ij)=pext(ij)*h(ij,l)
          ENDDO
          stotl(l)=dsig(l)*(SSUM(ip1jmp1,ge,1)-SSUM(jjp1,ge,iip1))

          DO ij=1,ip1jmp1
             ge(ij)=pext(ij)*AMAX1(bernf(ij,l)-phi(ij,l),0.)
          ENDDO
          rmsvl(l)=2.*dsig(l)*(SSUM(ip1jmp1,ge,1)-SSUM(jjp1,ge,iip1))

          DO ij =iip2,ip1jm
             ge(ij)=(ucov(ij,l)/cu(ij)+omegcosp(ij))*pbarcosp(ij)
          ENDDO
          angl(l)=radsg*dsig(l)*
     s    (SSUM(ip1jm-iip1,ge(iip2),1)-SSUM(jjm-1,ge(iip2),iip1))
      ENDDO

      CALL SCOPY(ip1jmp1,pext,1,ge,1)
      ptot  = SSUM(ip1jmp1,ge,1)-SSUM(jjp1,ge,iip1)
      etot  = SSUM(     llm, etotl, 1 )
      ztot  = SSUM(     llm, ztotl, 1 )
      stot  = SSUM(     llm, stotl, 1 )
      rmsv  = SSUM(     llm, rmsvl, 1 )
      ang   = SSUM(     llm,  angl, 1 )

      rday = FLOAT(INT ( day_ini + time ))
c
      IF(ptot0.eq.0.)  THEN
         PRINT 3500, itau, rday, heure,time
         PRINT*,'WARNING!!! On recalcule les valeurs initiales de :'
         PRINT*,'ptot,rmsdpdt,etot,ztot,stot,rmsv,ang'
         PRINT *, ptot,rmsdpdt,etot,ztot,stot,rmsv,ang
         etot0 = etot
         ptot0 = ptot
         ztot0 = ztot
         stot0 = stot
         ang0 = ang
      END IF

      etot= etot/etot0
      rmsv= SQRT(rmsv/ptot)
      ptot= ptot/ptot0
      ztot= ztot/ztot0
      stot= stot/stot0
      ang=ang/ ang0


ccc   PRINT 3500, itau, itau*dtvr/daysec, heure, time
      PRINT 3500, itau, rday, heure, time
      PRINT 4000, ptot,rmsdpdt,etot,ztot,stot,rmsv,ang

c   !!! WARNING adhoc pour Mtests martiens
      WRITE(78,'(2e15.5)')  day_ini+time,ptot*7.

      RETURN

3500   FORMAT('0'10(1h*),4x'pas'i7,5x'jour'f5.0,'heure'f5.1,4x
     *   ,'date',f10.5,4x,10(1h*))
4000   FORMAT(10x'masse'4x'rmsdpdt'7x'energie'2x'enstrophie'
     * 2x'entropie'3x'rmsv'4x'mt.ang'/' dudu ',f10.6,e13.6,5f10.3/
     * )
      END