 MODULE cometemp

   !!======================================================================
   !!                   ***  MODULE cometemp  ***
   !! 
   !!======================================================================

#include "toolcpp.h"

   !! * Modules used
   USE parameters, ONLY : rsh,rlg,imin,imax,jmin,jmax,kmax,ljminp2,ljmaxm1, &
                          ljminp1,ljmin,ljmax,limin,limax,liminm1,ljminm1

   !! * Public variables
   LOGICAL                                       :: l_sflx_radlossbot,l_sflx_solarcst,l_rapflux,l_meteoverif
   REAL(kind=rsh)                                :: sflx_solarcst,coext,chp,sflx_rad_maxdepth
   REAL(kind=rsh),ALLOCATABLE,DIMENSION(:,:)     :: tclim
   REAL(kind=rsh),ALLOCATABLE,DIMENSION(:,:)     :: rh1,rh2,vv1,vv2,fcc1,fcc2,sat1,sat2
   REAL(kind=rsh),ALLOCATABLE,DIMENSION(:,:)     :: rh,sat,fcc,ventvent
   REAL(kind=rsh),ALLOCATABLE,DIMENSION(:,:)     :: hfrelax
   REAL(kind=rlg),ALLOCATABLE,DIMENSION(:,:,:)   :: ftempx,ftempy,phitemp,phisal
   REAL(kind=rsh),ALLOCATABLE,DIMENSION(:,:,:)   :: fdiftemp ! flux d advection/diffusion de temperature

 CONTAINS

   !!======================================================================

  SUBROUTINE alloc_cometemp

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE alloc_cometemp  ***
   !&E
   !&E ** Purpose : 
   !&E
   !&E ** Description :
   !&E
   !&E ** Called by :
   !&E
   !&E ** External calls : 
   !&E
   !&E ** Used ij-arrays : 
   !&E
   !&E ** Modified variables :
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !  
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used
   IMPLICIT NONE

   !! * Arguments

   !! * Local declarations
   INTEGER :: j

   !!----------------------------------------------------------------------
   !! * Executable part

   IF_NOAGRIF (ALLOCATED(phitemp)) PRINT*,"error: cometemp allocated"
   ALLOCATE(phitemp(kmax,limin:limax,ljmin:ljmax) )
   ALLOCATE(phisal(kmax,limin:limax,ljmin:ljmax) )
   ALLOCATE(hfrelax(limin:limax,ljmin:ljmax) )
   ALLOCATE(rh(limin:limax,ljmin:ljmax) )
   ALLOCATE(sat(limin:limax,ljmin:ljmax) )
   ALLOCATE(fcc(limin:limax,ljmin:ljmax) )
   ALLOCATE(ventvent(limin:limax,ljmin:ljmax) )
   ALLOCATE(tclim(limin:limax,ljmin:ljmax) )

    ! Initialize flux
!$OMP PARALLEL DO PRIVATE(j)
   DO j=ljmin,ljmax
     hfrelax(:,j)=0.0_rsh 
     rh(:,j)=0.0_rsh 
     sat(:,j)=0.0_rsh 
     fcc(:,j)=0.0_rsh 
     ventvent(:,j)=0.0_rsh 
     tclim(:,j)=0.0_rsh 
     phitemp(:,:,j)=0.0_rlg
     phisal(:,:,j)=0.0_rlg
   END DO
!$OMP END PARALLEL DO

  END SUBROUTINE alloc_cometemp

   !!======================================================================

  SUBROUTINE dealloc_cometemp

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE dealloc_cometemp  ***
   !&E
   !&E ** Purpose : 
   !&E
   !&E ** Description :
   !&E
   !&E ** Called by :
   !&E
   !&E ** External calls : 
   !&E
   !&E ** Used ij-arrays : 
   !&E
   !&E ** Modified variables :
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !  
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used
   IMPLICIT NONE

   !! * Arguments

   !! * Local declarations

   !!----------------------------------------------------------------------
   !! * Executable part

   IF_NOAGRIF (.NOT.ALLOCATED(phitemp)) PRINT*,"error: cometemp not allocated"
   DEALLOCATE(phitemp,phisal)
   DEALLOCATE(hfrelax,rh,sat,fcc,ventvent,tclim)

  END SUBROUTINE dealloc_cometemp

   !!======================================================================

  SUBROUTINE alloc_cometemp_beforestep

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE alloc_cometemp_beforestep  ***
   !&E
   !&E ** Purpose : 
   !&E
   !&E ** Description :
   !&E
   !&E ** Called by :
   !&E
   !&E ** External calls : 
   !&E
   !&E ** Used ij-arrays : 
   !&E
   !&E ** Modified variables :
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !  
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used
   IMPLICIT NONE

   !! * Arguments

   !! * Local declarations

   !!----------------------------------------------------------------------
   !! * Executable part

   IF_NOAGRIF (ALLOCATED(rh1)) PRINT*,"error: cometemp_flxsurf allocated"
   ALLOCATE(rh1(limin:limax,ljmin:ljmax) )
   ALLOCATE(sat1(limin:limax,ljmin:ljmax) )
   ALLOCATE(fcc1(limin:limax,ljmin:ljmax) )
   ALLOCATE(vv1(limin:limax,ljmin:ljmax) )
   ALLOCATE(rh2(limin:limax,ljmin:ljmax) )
   ALLOCATE(sat2(limin:limax,ljmin:ljmax) )
   ALLOCATE(fcc2(limin:limax,ljmin:ljmax) )
   ALLOCATE(vv2(limin:limax,ljmin:ljmax) )

   !initialisation for sflxread & tool_interp2d_sflx
   !tool_interp2d_sflx puts value only between ig(j) id(j)
   !but sflxread uses value from imin:imax, thus
   !these values must be initialised as zero.
   vv1(:,:) =0.0_rsh ; vv2(:,:) =0.0_rsh
   sat1(:,:)=0.0_rsh ; sat2(:,:)=0.0_rsh
   fcc1(:,:)=0.0_rsh ; fcc2(:,:)=0.0_rsh
   rh1(:,:) =0.0_rsh ; rh2(:,:) =0.0_rsh

  END SUBROUTINE alloc_cometemp_beforestep

   !!======================================================================

  SUBROUTINE dealloc_cometemp_afterstep

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE dealloc_cometemp_afterstep  ***
   !&E
   !&E ** Purpose : 
   !&E
   !&E ** Description :
   !&E
   !&E ** Called by :
   !&E
   !&E ** External calls : 
   !&E
   !&E ** Used ij-arrays : 
   !&E
   !&E ** Modified variables :
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !  
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used
   IMPLICIT NONE

   !! * Arguments

   !! * Local declarations

   !!----------------------------------------------------------------------
   !! * Executable part

   IF_NOAGRIF (.NOT.ALLOCATED(rh1)) PRINT*,"error: cometemp_flxsurf not allocated"
   DEALLOCATE(rh1,rh2,sat1,sat2,fcc1,fcc2,vv1,vv2)

  END SUBROUTINE dealloc_cometemp_afterstep

   !!======================================================================

  SUBROUTINE alloc_ftempxy

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE alloc_ftempxy  ***
   !&E
   !&E ** Purpose : 
   !&E
   !&E ** Description :
   !&E
   !&E ** Called by :
   !&E
   !&E ** External calls : 
   !&E
   !&E ** Used ij-arrays : 
   !&E
   !&E ** Modified variables :
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !    2009-04 (A. Mano) MPI2D
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used
   IMPLICIT NONE

   !! * Arguments

   !! * Local declarations

   !!----------------------------------------------------------------------
   !! * Executable part

   IF_NOAGRIF (ALLOCATED(ftempx)) PRINT*,"error: ftempx already allocated"
   ALLOCATE(ftempx(kmax,liminm1:limax,ljmin:ljmax) )
   ALLOCATE(ftempy(kmax,limin:limax,ljminm1:ljmax) )
   ftempx(:,:,:)=0.0_rlg
   ftempy(:,:,:)=0.0_rlg

  END SUBROUTINE alloc_ftempxy

   !!======================================================================

  SUBROUTINE dealloc_ftempxy

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE dealloc_ftempxy  ***
   !&E
   !&E ** Purpose : 
   !&E
   !&E ** Description :
   !&E
   !&E ** Called by :
   !&E
   !&E ** External calls : 
   !&E
   !&E ** Used ij-arrays : 
   !&E
   !&E ** Modified variables :
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !    2009-04 (A. Mano) MPI2D
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used
   IMPLICIT NONE

   !! * Arguments

   !! * Local declarations

   !!----------------------------------------------------------------------
   !! * Executable part
   DEALLOCATE(ftempx,ftempy)


  END SUBROUTINE dealloc_ftempxy

   !!======================================================================

#if defined key_ts_diffh_geo || defined key_ts_diffh_rho
  SUBROUTINE alloc_fdifadtemp

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE alloc_fdifadtemp  ***
   !&E
   !&E ** Purpose : 
   !&E
   !&E ** Description :
   !&E
   !&E ** Called by :
   !&E
   !&E ** External calls : 
   !&E
   !&E ** Used ij-arrays : 
   !&E
   !&E ** Modified variables :
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !    
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used
   IMPLICIT NONE

   !! * Arguments

   !! * Local declarations

   !!----------------------------------------------------------------------
   !! * Executable part

   IF_NOAGRIF (ALLOCATED(fdiftemp)) PRINT*,"error: fdiftemp already allocated"
   ALLOCATE(fdiftemp(kmax,limin:limax,ljmin:ljmax))
   fdiftemp(:,:,:)=0.0_rlg

  END SUBROUTINE alloc_fdifadtemp

   !!======================================================================

  SUBROUTINE dealloc_fdifadtemp

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE dealloc_fdifadtemp  ***
   !&E
   !&E ** Purpose : 
   !&E
   !&E ** Description :
   !&E
   !&E ** Called by :
   !&E
   !&E ** External calls : 
   !&E
   !&E ** Used ij-arrays : 
   !&E
   !&E ** Modified variables :
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !    
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used
   IMPLICIT NONE

   !! * Arguments

   !! * Local declarations

   !!----------------------------------------------------------------------
   !! * Executable part

   DEALLOCATE(fdiftemp)

  END SUBROUTINE dealloc_fdifadtemp

#endif

   !!======================================================================

 END MODULE cometemp
