 MODULE comsubstance

!!======================================================================
!!                   ***  MODULE comsubstance   ***
!!
!!  ** History :
!!        ! 2008-04 (C. Struski) ajout l_out_subs
!!        ! 2008-09 (G. Galibert / ALTRAN) attributes of output files that
!!                                         respect NetCDF OCO v1.1 file format
!!        ! 2009-02 (P. Le Hir) ajout igrav1,igrav2,isand1,isand2,imud1,imud2
!!        ! 2009-09 (B. Thouvenin) management of driving variables
!!        ! 2010-06 (V.M. Faure, N. Bousserez, M. Baklouti) coupling with eco3m
!!
!!======================================================================





























































!! * Modules used



   USE parameters, ONLY : lchain,kmax,imin,imax,jmin,jmax,obc_width,rsh,rlg,ljminp2, &
                          ljmaxm1,ljminp1,ljmin,ljmax,riosh
   USE parameters, ONLY : nb_var


!! * Shared or public module variables

! l_out_log    : flag positionne a .true. pour sauver les concentrations sous format mogarithmique
! l_bilan     : flag positionne a .true. pour estimer les bilans de concentrations
! name_bilfil : nom du fichier pour definir les zones de bilans et frontieres
! nb_border : number of border threw which fluxes are calculting for budgets (bilan)
   CHARACTER(LEN=lchain)                      :: file_outflow,name_filesubs,filevardiag,file_flxatm_subs
   CHARACTER(LEN=lchain),DIMENSION(-1:nb_var) :: name_var,long_name_var,standard_name_var,unit_var

!MD 08/06/18
   CHARACTER(LEN=lchain)                      :: name_var_sf,long_name_var_sf,standard_name_var_sf
   CHARACTER(LEN=lchain)                      :: name_var_sf1,long_name_var_sf1,standard_name_var_sf1
!END MD 08/06/18
   REAL(KIND=rsh)       ,DIMENSION(-1:nb_var) :: valid_min_var, valid_max_var
   LOGICAL                                    :: l_bilan,l_out_log,l_subflxatm,l_subflxatm_xyt
   LOGICAL                                    :: l_cvrain_readfile,l_subflxatm_readfile
   CHARACTER(LEN=lchain)                      :: name_bilfil
   INTEGER                                    :: nb_border,iout_budget
   CHARACTER(LEN=19)                          :: date_startsub,date_start_budget
   REAL(KIND=rlg)                             :: tdebsubs,dtout_budget,tdeb_budget
   REAL(KIND=rsh)                             :: sflx_sub_atm_depth

! 4D array containing all the tracers
   REAL(kind=rsh),DIMENSION(:,:,:,:),ALLOCATABLE :: cv_wat
! intermediate variable (used in UQ macho scheme)
   REAL(kind=rsh),DIMENSION(:,:,:,:),ALLOCATABLE :: c2
!#ifdef key_tssub_macho3d
   REAL(kind=rsh),DIMENSION(:,:,:,:),ALLOCATABLE :: c3
!#endif


! ----------------------------------------------------------------------------
! GESTION DU TYPE DE VARIABLE
! ----------------------------------------------------------------------------

   INTEGER                          :: nv_grav,nv_sand,nv_mud,                 &
                                       igrav1,igrav2,isand1,isand2,imud1,imud2
   INTEGER                          :: nv_cp,nv_ncp,nv_dis,nv_sorb
   INTEGER                          :: nv_fix,nv_dri,nv_int
   INTEGER                          :: nvp,nvpc
   INTEGER                          :: nvpi ! intermediate nvp to distinguish the methodology
! (subdt for the complete equation : fx,fy reestimated
! or for vertical equation only)
   INTEGER                          :: nv_adv,nv_state,nv_tot
! number of different maximum settling velocities
   INTEGER                          :: nb_ws_max  ! a group per different value of the maximum velocities
   INTEGER,DIMENSION(nb_var)        :: itypv_r
   INTEGER,DIMENSION(:),ALLOCATABLE :: itypv

   LOGICAL,DIMENSION(:),ALLOCATABLE :: l_out_subs,l_subs2D

! indices des particulaires constitutives associees aux particulaires adsorbees, alloues dans traceur.F90
   INTEGER,DIMENSION(:),ALLOCATABLE :: irkm_var_assoc
   CHARACTER(LEN=lchain),DIMENSION(nb_var) :: name_varpc_assoc
   REAL(KIND=rsh)                   :: dhmin
! ----------------------------------------------------------------------------
! GESTION CORRESPONDANCE ORDRE DES VARIABLES LUES ET VARIABLES DANS MODELE
! ----------------------------------------------------------------------------

! irk_mod : rang (position) dans le modele de la variable dans tableaux gerant substances
! irk_fil : rang (position) dans le fichier de donnees de la variable

   INTEGER       ,DIMENSION(:),ALLOCATABLE :: irk_mod,irk_fil
   REAL(KIND=rsh),DIMENSION(nb_var)        :: diam_r,cini_sed_r,ros_r,unit_modif_mudbio_N2dw


!  gestion des variables forcantes variables with time
   LOGICAL                                  :: l_driv_cst
   REAL(KIND=rlg)                           :: dt_driv,t_driv

! ----------------------------------------------------------------------------
! MATRIX
! ----------------------------------------------------------------------------
   REAL(KIND=rsh),ALLOCATABLE,DIMENSION(:,:,:,:) :: phicon
   REAL(KIND=rsh),ALLOCATABLE,DIMENSION(:,:,:,:) :: phicon_drycell
   REAL(KIND=rsh),DIMENSION(:)       ,ALLOCATABLE            :: f90,t90,cini_wat,cini_air
! P. Le Hir: real(KIND=rsh),dimension(:),allocatable                   :: cini_sed
! P. Le Hir: etait gardee ici car lue dans traceur si key_sedim ou non, mais c est cini_sed_r
! P. Le Hir: qui est lu par traceur
   REAL(KIND=rsh),DIMENSION(:)      ,ALLOCATABLE :: typart,typdiss,sub_flx_atm,cv_rain
   REAL(KIND=rsh),DIMENSION(:,:)    ,ALLOCATABLE :: sub_flx_atm_t,cv_rain_t
   REAL(KIND=rsh),DIMENSION(:,:,:,:),ALLOCATABLE :: obc_cv_wat_e,obc_cv_wat_w
   REAL(KIND=rsh),DIMENSION(:,:,:,:),ALLOCATABLE :: obc_cv_wat_n,obc_cv_wat_s
   INTEGER       ,DIMENSION(imin:imax)           :: obc_cv_ktherm_s,obc_cv_ktherm_n
   INTEGER       ,DIMENSION(jmin:jmax)           :: obc_cv_ktherm_w,obc_cv_ktherm_e
   REAL(KIND=rsh),DIMENSION(:,:,:),ALLOCATABLE   :: phicon_atm


! Matrice de sedimento conservees pour calculer flx_w2s et permettre aux variables
! de quitter la colonne d eau meme si on ne fait pas de sedimentologie
   REAL(KIND=rsh),DIMENSION(:,:,:,:),ALLOCATABLE :: ws3
   REAL(KIND=rsh),DIMENSION(:)      ,ALLOCATABLE :: ws_free_min, ws_free_max
   REAL(KIND=rsh),DIMENSION(:,:)    ,ALLOCATABLE :: ws_free_para, ws_hind_para
   INTEGER       ,DIMENSION(:)      ,ALLOCATABLE :: ws_free_opt,ws_hind_opt
   REAL(KIND=rsh),DIMENSION(:)      ,ALLOCATABLE :: tocd


! Management of group of substances with settling velocity
! a group corresponds to a value of the maximum settling velocities
   INTEGER       ,DIMENSION(:,:),ALLOCATABLE     :: iv_ws_max ! index of variable with settling velocity
   INTEGER       ,DIMENSION(:),ALLOCATABLE       :: ndt_part  ! sub-time step relative to a max settling velocity
   INTEGER       ,DIMENSION(:),ALLOCATABLE       :: nv_ws_max ! number of variables with the same max settling velocity
   INTEGER       ,DIMENSION(:),ALLOCATABLE       :: ap_ws_gr  !
   REAL(KIND=rsh),DIMENSION(:),ALLOCATABLE       :: sv_part

! ----------------------------------------------------------------------------
! PARAMETRIZATION of SUBSTANCE OUTFLOWS
! ----------------------------------------------------------------------------


   LOGICAL              ,DIMENSION(:)      ,ALLOCATABLE :: l_rejflux
   INTEGER                                              :: nbrej,nbobc_cv
   INTEGER              ,DIMENSION(:)      ,ALLOCATABLE :: krej,numriv
   INTEGER              ,DIMENSION(:)      ,ALLOCATABLE :: nucon
   REAL(KIND=rsh)       ,DIMENSION(:)      ,ALLOCATABLE :: xrej,yrej
   REAL(KIND=rsh)       ,DIMENSION(:,:)    ,ALLOCATABLE :: valrej
   REAL(KIND=rsh)       ,DIMENSION(:,:,:,:),ALLOCATABLE :: obc_cv_data
   INTEGER              ,DIMENSION(:,:)    ,ALLOCATABLE :: obc_cv_nbcol
   CHARACTER(LEN=lchain),DIMENSION(:)      ,ALLOCATABLE :: obc_cv_name
   CHARACTER(LEN=lchain),DIMENSION(:)      ,ALLOCATABLE :: init_cv_name
   INTEGER              ,DIMENSION(:)      ,ALLOCATABLE :: obc_cv_ibeg,obc_cv_iend


! ----------------------------------------------------------------------------
! PARAMETRES RELATIFS A LA BIOLOGIE
! ----------------------------------------------------------------------------



! ----------------------------------------------------------------------------
! PARAMETRES RELATIFS A PENETRATION DE LA LUMIERE
! ----------------------------------------------------------------------------


   REAL(KIND=rsh),DIMENSION(:,:,:),ALLOCATABLE :: attenuation,alumplafond,extinction

!  REAL(KIND=rsh),DIMENSION(:,:)  ,ALLOCATABLE :: phytomoy,phytocarre





! ---------------------------------------------------------------------------
! VARIABLES FLUXES AT THE INTERFACE WATER-SEDIMENT
! ---------------------------------------------------------------------------

! Matrice de sedimento conservees au cas ou on veut calculer les flux et permettre aux variables
! de quitter la colonne d eau meme si on ne fait pas de sedimentologie (attention Pb USE)
! (seulement si pas key_MUSTANG, car avec key_MUSTANG, les flx sont rangés dans comMUSTANG)
!#if ! defined 1
!#if defined key_contaminant || defined key_biolo
!   REAL(KIND=rsh),DIMENSION(:,:,:),ALLOCATABLE :: flx_s2w,flx_w2s,flx_w2s_sum
!#endif
!#endif


   REAL(KIND=rsh),DIMENSION(:,:,:,:),ALLOCATABLE :: fy,fx
   REAL(kind=rsh),ALLOCATABLE,DIMENSION(:,:,:,:) :: fz

   CONTAINS

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

  SUBROUTINE alloc_fxfyfz
   
!&E--------------------------------------------------------------------------
!&E                 ***  ROUTINE alloc_fxfyfz  ***
!&E
!&E ** History :
!&E       !  2009-04 (A. Mano) MPI2D
!&E
!&E--------------------------------------------------------------------------
!! * Modules used
   USE parameters, ONLY : limin,limax,liminm1,ljminm1

   IMPLICIT NONE

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

   IF (ALLOCATED(fx)) PRINT*,"error: fx already allocated"
   ALLOCATE(fx(nv_adv,kmax,liminm1:limax,ljmin:ljmax) )
   ALLOCATE(fy(nv_adv,kmax,limin:limax,ljminm1:ljmax) )
   fx(:,:,:,:)=0.0_rsh
   fy(:,:,:,:)=0.0_rsh
   ALLOCATE(fz(nv_adv,0:kmax,limin:limax,ljmin:ljmax) )
   fz(:,:,:,:)=0.0_rsh

  END SUBROUTINE alloc_fxfyfz

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

  SUBROUTINE dealloc_fxfyfz
   
!&E--------------------------------------------------------------------------
!&E                 ***  ROUTINE alloc_fxfyfz  ***
!&E--------------------------------------------------------------------------
!! * Modules used
   IMPLICIT NONE

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

   DEALLOCATE(fx,fy)
   DEALLOCATE(fz)

  END SUBROUTINE dealloc_fxfyfz





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

 END MODULE comsubstance
