












MODULE trdmxl_oce
   !!======================================================================
   !!                   ***  MODULE trdmxl_oce  ***
   !! Ocean trends :   set tracer and momentum trend variables
   !!======================================================================
   !! History :  1.0  ! 2004-08  (C. Talandier)  New trends organization
   !!            3.5  ! 2012-02  (G. Madec) suppress the trend keys + new trdmxl formulation
   !!----------------------------------------------------------------------
   USE par_oce        ! ocean parameters

   IMPLICIT NONE
   PRIVATE

   PUBLIC   trdmxl_oce_alloc    ! Called in trdmxl.F90

   !                                                !* mixed layer trend indices
   INTEGER, PUBLIC, PARAMETER ::   jpltrd = 12      !: number of mixed-layer trends arrays
   INTEGER, PUBLIC            ::   jpktrd           !: max level for mixed-layer trends diag.
   !
   INTEGER, PUBLIC, PARAMETER ::   jpmxl_xad =  1   !: i-componant of advection   
   INTEGER, PUBLIC, PARAMETER ::   jpmxl_yad =  2   !: j-componant of advection
   INTEGER, PUBLIC, PARAMETER ::   jpmxl_zad =  3   !: k-component of advection 
   INTEGER, PUBLIC, PARAMETER ::   jpmxl_ldf =  4   !: lateral diffusion (geopot. or iso-neutral)
   INTEGER, PUBLIC, PARAMETER ::   jpmxl_zdf =  5   !: vertical diffusion  
   INTEGER, PUBLIC, PARAMETER ::   jpmxl_npc =  6   !: non penetrative convective adjustment
   INTEGER, PUBLIC, PARAMETER ::   jpmxl_bbc =  7   !: geothermal flux
   INTEGER, PUBLIC, PARAMETER ::   jpmxl_bbl =  8   !: bottom boundary layer (advective/diffusive)
   INTEGER, PUBLIC, PARAMETER ::   jpmxl_for =  9   !: forcing 
   INTEGER, PUBLIC, PARAMETER ::   jpmxl_dmp = 10   !: internal restoring trend
   INTEGER, PUBLIC, PARAMETER ::   jpmxl_zdfp = 11  !: ! iso-neutral diffusion:"pure" vertical diffusion
   INTEGER, PUBLIC, PARAMETER ::   jpmxl_atf  = 12  !: asselin trend (**MUST BE THE LAST ONE**)
   !                                                            !!* Namelist namtrd_mxl:  trend diagnostics in the mixed layer *
   INTEGER           , PUBLIC ::   nn_ctls  = 0                  !: control surface type for trends vertical integration
   REAL(dp)          , PUBLIC ::   rn_rho_c = 0.01               !: density criteria for MLD definition
   REAL(dp)          , PUBLIC ::   rn_ucf   = 1.                 !: unit conversion factor (for netCDF trends outputs)
                                                                 !  =1. (=86400.) for degC/s (degC/day) and psu/s (psu/day)
   CHARACTER(len=32), PUBLIC ::   cn_trdrst_in  = "restart_mxl"  !: suffix of ocean restart name (input)
   CHARACTER(len=32), PUBLIC ::   cn_trdrst_out = "restart_mxl"  !: suffix of ocean restart name (output)
   LOGICAL          , PUBLIC ::   ln_trdmxl_instant = .FALSE.    !: flag to diagnose inst./mean ML T/S trends
   LOGICAL          , PUBLIC ::   ln_trdmxl_restart = .FALSE.    !: flag to restart mixed-layer diagnostics


   !! Arrays used for diagnosing mixed-layer trends 
   !!---------------------------------------------------------------------
   CHARACTER(LEN=80) , PUBLIC :: clname, ctrd(jpltrd+1,2)

   INTEGER , PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   nmxl   !: mixed layer depth indexes 
   INTEGER , PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   nbol   !: mixed-layer depth indexes when read from file

   REAL(dp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::   wkx    !:

   REAL(dp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::  hmxl   , tml    , sml  , tmlb   , smlb , tmlbb  , smlbb, tmlbn  , smlbn, tmltrdm, smltrdm, tml_sum, tml_sumb, tmltrd_atf_sumb, sml_sum, sml_sumb, smltrd_atf_sumb, hmxl_sum, hmxlbn















   REAL(dp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::  tmlatfb, tmlatfn , smlatfb, smlatfn, tmlatfm, smlatfm






   REAL(dp), PUBLIC, ALLOCATABLE, DIMENSION(:,:,:) ::  tmltrd, smltrd, tmltrd_sum, tmltrd_csum_ln, tmltrd_csum_ub, smltrd_sum, smltrd_csum_ln, smltrd_csum_ub










   !!----------------------------------------------------------------------
   !! NEMO/OCE 4.0 , NEMO Consortium (2018)
   !! $Id: trdmxl_oce.F90 10425 2018-12-19 21:54:16Z smasson $ 
   !! Software governed by the CeCILL license (see ./LICENSE)
   !!----------------------------------------------------------------------
CONTAINS

  INTEGER FUNCTION trdmxl_oce_alloc()
     !!----------------------------------------------------------------------
     !!                 ***  FUNCTION trdmxl_oce_alloc   ***
     !!----------------------------------------------------------------------
     USE lib_mpp
     INTEGER :: ierr(5)
     !!----------------------------------------------------------------------

     ! Initialise jpktrd here as can no longer do it in MODULE body since
     ! jpk is now a variable.
     jpktrd = jpk   !: max level for mixed-layer trends diag.

     ierr(:) = 0

     ALLOCATE( nmxl (jpi,jpj)    , nbol (jpi,jpj),    &
        &      wkx  (jpi,jpj,jpk), hmxl (jpi,jpj),    & 
        &      tml  (jpi,jpj)    , sml  (jpi,jpj),    & 
        &      tmlb (jpi,jpj)    , smlb (jpi,jpj),    &
        &      tmlbb(jpi,jpj)    , smlbb(jpi,jpj), STAT = ierr(1) )

     ALLOCATE( tmlbn(jpi,jpj)  , smlbn(jpi,jpj),   &
        &      tmltrdm(jpi,jpj), smltrdm(jpi,jpj), &
        &      tml_sum(jpi,jpj), tml_sumb(jpi,jpj),&
        &      tmltrd_atf_sumb(jpi,jpj)           , STAT=ierr(2) )

     ALLOCATE( sml_sum(jpi,jpj), sml_sumb(jpi,jpj), &
        &      smltrd_atf_sumb(jpi,jpj),            &
        &      hmxl_sum(jpi,jpj), hmxlbn(jpi,jpj),  &
        &      tmlatfb(jpi,jpj), tmlatfn(jpi,jpj), STAT = ierr(3) )

     ALLOCATE( smlatfb(jpi,jpj), smlatfn(jpi,jpj), & 
        &      tmlatfm(jpi,jpj), smlatfm(jpi,jpj), &
        &      tmltrd(jpi,jpj,jpltrd),   smltrd(jpi,jpj,jpltrd), STAT=ierr(4))

     ALLOCATE( tmltrd_sum(jpi,jpj,jpltrd),tmltrd_csum_ln(jpi,jpj,jpltrd),      &
        &      tmltrd_csum_ub(jpi,jpj,jpltrd), smltrd_sum(jpi,jpj,jpltrd),     &
        &      smltrd_csum_ln(jpi,jpj,jpltrd), smltrd_csum_ub(jpi,jpj,jpltrd), STAT=ierr(5) )
      !
      trdmxl_oce_alloc = MAXVAL( ierr )
      CALL mpp_sum ( 'trdmxl_oce', trdmxl_oce_alloc )
      IF( trdmxl_oce_alloc /= 0 )   CALL ctl_stop( 'STOP', 'trdmxl_oce_alloc: failed to allocate arrays' )
      !
   END FUNCTION trdmxl_oce_alloc

   !!======================================================================
END MODULE trdmxl_oce
