 MODULE comfluxdo

!!======================================================================
!!                   ***  MODULE comfluxdo  ***
!! ** History :
!!       !                        Original code
!!       !  2008-06  (V. Garnier) introduction of phiriver to allow the prescription of
!!       !                        the heat flux relative to river discharges and
!!       !                        the prescription of the heat flux relative to rain-evap
!!       !  2010-03  (V. Garnier) correction of phiriver to allow several river
!!       !                        discharges at the same location
!!       !  2011-02  (B. Thouvenin) type of fzeau_ws
!!======================================================================





























































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

!! * Shared or public module variables
   LOGICAL,DIMENSION(:),ALLOCATABLE               :: l_flestat,l_tempriv
   CHARACTER(len=lchain)                          :: file_river
   CHARACTER(len=lchain),DIMENSION(:),ALLOCATABLE :: nomriviere
   INTEGER                                        :: nbfl
   INTEGER,DIMENSION(:),ALLOCATABLE               :: iriv,jriv,kriv
   REAL(kind=rsh),DIMENSION(:),ALLOCATABLE        :: debmoy,tmfle,amtfle,river_thickness
   REAL(kind=rsh),DIMENSION(:,:),ALLOCATABLE      :: debit,temp_riv
   REAL(kind=rsh),DIMENSION(:,:),ALLOCATABLE      :: buoy2d


   REAL(kind=rlg),ALLOCATABLE,DIMENSION(:,:,:) :: fsalx,fsaly
   REAL(kind=rsh),ALLOCATABLE,DIMENSION(:,:,:) :: fdifsal ! diffusion flux of salinity (fadsal+fdifsal=fsalx)
   REAL(kind=rlg),ALLOCATABLE,DIMENSION(:,:,:) :: fzeau

   REAL(kind=rsh),ALLOCATABLE,DIMENSION(:,:,:) :: fzeau_ws

   REAL(kind=rlg),ALLOCATABLE,DIMENSION(:,:,:) :: fxeau,fyeau,phieau
   REAL(kind=rsh),ALLOCATABLE,DIMENSION(:,:)   :: xeadv,rain,evap,phiriver
   REAL(kind=rsh) :: phitot_glob
 CONTAINS

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

  SUBROUTINE alloc_comfluxdo

!&E---------------------------------------------------------------------
!&E                 ***  ROUTINE alloc_comfluxdo  ***
!&E
!&E---------------------------------------------------------------------
!! * Modules used
   IMPLICIT NONE

!! * Local declarations
   INTEGER :: j

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

   IF (ALLOCATED(xeadv)) PRINT*,"error: comfluxdo allocated"
   ALLOCATE(xeadv(limin:limax,ljmin:ljmax) )
   ALLOCATE(buoy2d(limin:limaxp1,ljmin:ljmaxp1) )
   ALLOCATE(rain(limin:limax,ljmin:ljmax) )
   ALLOCATE(evap(limin:limax,ljmin:ljmax) )
   ALLOCATE(phieau(kmax,limin:limax,ljmin:ljmax) )
   ALLOCATE(fxeau(kmax,liminm1:limax,ljmin:ljmax) )
   ALLOCATE(fyeau(kmax,limin:limax,ljminm1:ljmax) )

   buoy2d(:,:)=0.0_rsh

! initialize fluxes
   phieau(:,:,:) = 0.0_rlg
   fxeau(:,liminm1,:)=0.0_rlg
   fyeau(:,:,ljminm1)=0.0_rlg
!$OMP PARALLEL DO PRIVATE(j)
   DO j=ljmin,ljmax
     fxeau(:,:,j)=0.0_rlg
     fyeau(:,:,j)=0.0_rlg
     rain(:,j)=0.0_rsh
     evap(:,j)=0.0_rsh
   END DO
!$OMP END PARALLEL DO

  END SUBROUTINE alloc_comfluxdo

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

  SUBROUTINE dealloc_comfluxdo

!&E---------------------------------------------------------------------
!&E                 ***  ROUTINE dealloc_comfluxdo  ***
!&E
!&E---------------------------------------------------------------------
!! * Modules used
   IMPLICIT NONE

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

   DEALLOCATE(xeadv,buoy2d,rain,evap,fxeau,fyeau,phieau)
   IF (nbfl/=0) DEALLOCATE(phiriver)

  END SUBROUTINE dealloc_comfluxdo

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

  SUBROUTINE alloc_comfluxdo3d

!&E---------------------------------------------------------------------
!&E                 ***  ROUTINE alloc_comfluxdo3d  ***
!&E
!&E---------------------------------------------------------------------
!! * Modules used
   IMPLICIT NONE

!! * Local declarations
   INTEGER :: j

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

   ALLOCATE(fzeau(kmax,limin:limax,ljmin:ljmax) )

   ALLOCATE(fzeau_ws(1:kmax-1,limin:limax,ljmin:ljmax) )

!$OMP PARALLEL DO PRIVATE(j)
   DO j=ljmin,ljmax
     fzeau(:,:,j)=0.0_rlg

     fzeau_ws(:,:,j)=0.0_rsh

   END DO

!$OMP END PARALLEL DO
  END SUBROUTINE alloc_comfluxdo3d

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

  SUBROUTINE dealloc_comfluxdo3d

!&E---------------------------------------------------------------------
!&E                 ***  ROUTINE dealloc_comfluxdo3d  ***
!&E
!&E---------------------------------------------------------------------
!! * Modules used
   IMPLICIT NONE

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

   DEALLOCATE(fzeau)

   DEALLOCATE(fzeau_ws)


  END SUBROUTINE dealloc_comfluxdo3d

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

  SUBROUTINE alloc_fsalxy

!&E---------------------------------------------------------------------
!&E                 ***  ROUTINE alloc_fsalxy  ***
!&E
!&E ** History :
!&E       !  2009-04  (A. Mano) MPI2D
!&E
!&E---------------------------------------------------------------------
!! * Modules used
   IMPLICIT NONE

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

   IF (ALLOCATED(fsalx)) PRINT*,"error: fsalx already allocated"
   ALLOCATE(fsalx(kmax,liminm1:limax,ljmin:ljmax) )
   ALLOCATE(fsaly(kmax,limin:limax,ljminm1:ljmax) )
   fsalx(:,:,:)=0.0_rlg
   fsaly(:,:,:)=0.0_rlg

  END SUBROUTINE alloc_fsalxy

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

  SUBROUTINE dealloc_fsalxy

!&E---------------------------------------------------------------------
!&E                 ***  ROUTINE dealloc_fsalxy  ***
!&E
!&E---------------------------------------------------------------------
!! * Modules used
   IMPLICIT NONE

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

   DEALLOCATE(fsalx,fsaly)

  END SUBROUTINE dealloc_fsalxy

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



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

 END MODULE comfluxdo
