 MODULE usersubstance
#ifdef key_substance 
   !!======================================================================
   !!                   ***  MODULE  usersubstance  ***
   !! Ocean dynamics Bio Sed:  programs which have to be written by users
   !!                           of key_substance
   !!        in order to define intermediate vairables and forcing variables
   !!======================================================================
   !! * Modules used
   use comsubstance
   USE parameters, ONLY : rsh,rlg,lchain,imin,imax,jmin,jmax,kmax,ljmin,ljmax,limin,limax, &
                          liminm1,limaxp2,ljminm1,ljmaxp2
#ifdef key_sedim_MUSTANG
   USE parameters, ONLY : ksdmin,ksdmax    
#endif
   USE comvars2d, ONLY : iscreenlog        
#ifdef key_contaminant                         
   USE parameters, ONLY : nb_var   
#endif

   IMPLICIT NONE
   PRIVATE

   !! * Accessibility
   PUBLIC user_vintermediate,                & ! routine called by main.F90
          user_init_vdriv,                   & ! routine called by main.F90
          user_ftime_vdriv                     ! routine called by step.F90
          

   !! * Shared module variables

   !! * Private variables



 CONTAINS

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

      subroutine user_vintermediate(t,cw   &
#ifdef key_sedim_MUSTANG
                                     ,cs   &
#endif
                                         )
#ifdef key_substance 
!
!********************************************************************
!
!
!     objet    : computing intermediate variables 
!                which are an arrangement of several varibales 
!
!     appelant         : step
!                      :
!     appeles          :
!                      :
!     canal d ecriture :
!                      :
!     canal de lecture :
!                      :
!     cle cpp          :
!                      :
!     variables in     : cv_wat, cv_sed
!     variables in     : selon les utilisations
!                      :
!     variables out    : cv_wat(nv_state+nv_dri+1:nv_state+nv_dri+nv_int)
!                      :
!     variables inout  :
!                      :
   !&E              2008-09 (B. Thouvenin)
!                      :
!***********************************************************
!
   !! * Arguments
   REAL(KIND=rlg),INTENT(in)                                              :: t
   REAL(KIND=rsh),DIMENSION(nv_tot,kmax,liminm1:limaxp2,ljminm1:ljmaxp2),INTENT(inout) :: cw
#ifdef key_sedim_MUSTANG
   REAL(KIND=rsh),DIMENSION(-1:nv_tot,ksdmin:ksdmax,limin:limax,ljmin:ljmax),INTENT(inout) :: cs
#endif

   INTEGER                     :: i,j,k,l
!
!************************************************************
!     Executable statements
!************************************************************
!
! HAS TO BE PROGRAMMED BY THE USER
!   
#else
  return
#endif
!
end subroutine user_vintermediate
   !!======================================================================

  SUBROUTINE user_init_vdriv(cw &
#ifdef key_sedim_MUSTANG
                            ,cs &
#endif
                                   )


   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE user_init_vdriv  ***
   !&E
   !&E ** Purpose : initialization of driving variables 
   !&E
   !&E ** Description : one must program this routine for each application
   !&E
   !&E ** Called by : main.F90
   !&E
   !&E ** External calls :
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used
   USE comsubstance, ONLY : nv_dri,l_driv_cst,t_driv,nv_state,dt_driv 
    
   !! * Arguments
   REAL(KIND=rsh),DIMENSION(nv_tot,kmax,liminm1:limaxp2,ljminm1:ljmaxp2),INTENT(inout) :: cw
#ifdef key_sedim_MUSTANG
   REAL(KIND=rsh),DIMENSION(-1:nv_tot,ksdmin:ksdmax,limin:limax,ljmin:ljmax),INTENT(inout) :: cs
#endif

   !! * Local declarations
   INTEGER                                     :: iv 

   !!----------------------------------------------------------------------
   !! * Executable part
    
   !!-+++++++++++++++++++++++++++++++++++++++++++++++++++
   !!      HAS TO BE PROGRAMMED BY THE USER 
   !!   FOR EACH DRIVING VARIABLE AND EACH APPLICATION
   !!-+++++++++++++++++++++++++++++++++++++++++++++++++++

   !! END OF USER PART !!

    t_driv=dt_driv
  
  END SUBROUTINE user_init_vdriv

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

  SUBROUTINE user_ftime_vdriv(t,cw     &
#ifdef key_sedim_MUSTANG
                                   ,cs &
#endif
                                      )

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE user_ftime_vdriv  ***
   !&E
   !&E ** Purpose : estimating of driving variables function of time
   !&E
   !&E ** Description : one must program this routine for each application
   !&E
   !&E ** Called by : main.F90
   !&E
   !&E ** External calls :
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used
    USE comsubstance, ONLY : cini_wat,nv_dri,l_driv_cst,t_driv,nv_state,dt_driv
#ifdef key_sedim_MUSTANG
    USE comMUSTANG,    ONLY : cini_sed
#endif
    
   !! * Arguments
   REAL(KIND=rlg),INTENT(in)                                              :: t
   REAL(KIND=rsh),DIMENSION(nv_tot,kmax,liminm1:limaxp2,ljminm1:ljmaxp2),INTENT(inout) :: cw
#ifdef key_sedim_MUSTANG
   REAL(KIND=rsh),DIMENSION(-1:nv_tot,ksdmin:ksdmax,limin:limax,ljmin:ljmax),INTENT(inout) :: cs
#endif

   !! * Local declarations
   INTEGER                                     :: iv

   !!----------------------------------------------------------------------
   !! * Executable part
    
   !!-+++++++++++++++++++++++++++++++++++++++++++++++++++
   !!      HAS TO BE PROGRAMMED BY THE USER 
   !!   FOR EACH DRIVING VARIABLE AND EACH APPLICATION
   !!-+++++++++++++++++++++++++++++++++++++++++++++++++++
    DO iv=1,nv_dri
     cw(nv_state+iv,:,:,:)=0.0_rsh
#ifdef key_sedim_MUSTANG
     cs(nv_state+iv,:,:,:)=0.0_rsh
#endif
    ENDDO
    t_driv=t_driv+dt_driv

  
  END SUBROUTINE user_ftime_vdriv

#endif
 END MODULE usersubstance
