MODULE p4zlys
   !!======================================================================
   !!                         ***  MODULE p4zlys  ***
   !! TOP :   PISCES 
   !!======================================================================
   !! History :    -   !  1988-07  (E. MAIER-REIMER) Original code
   !!              -   !  1998     (O. Aumont) additions
   !!              -   !  1999     (C. Le Quere) modifications
   !!             1.0  !  2004     (O. Aumont) modifications
   !!             2.0  !  2007-12  (C. Ethe, G. Madec)  F90
   !!                  !  2011-02  (J. Simeon, J. Orr)  Calcon salinity dependence
   !!             3.4  !  2011-06  (O. Aumont, C. Ethe) Improvment of calcite dissolution
   !!             3.6  !  2015-05  (O. Aumont) PISCES quota
   !!             4.2  !  2020     (J. ORR )  rhop is replaced by "in situ density" rhd
   !!----------------------------------------------------------------------
   !!   p4z_lys        :   Compute the CaCO3 dissolution 
   !!   p4z_lys_init   :   Read the namelist parameters
   !!----------------------------------------------------------------------
   USE oce_trc         !  shared variables between ocean and passive tracers
   USE trc             !  passive tracers common variables 
   USE sms_pisces      !  PISCES Source Minus Sink variables
   USE p4zche          !  Chemical model
   USE prtctl          !  print control for debugging
   USE iom             !  I/O manager

   IMPLICIT NONE
   PRIVATE

   PUBLIC   p4z_lys         ! called in trcsms_pisces.F90
   PUBLIC   p4z_lys_init    ! called in trcsms_pisces.F90

   REAL(wp), PUBLIC ::   kdca   !: diss. rate constant calcite
   REAL(wp), PUBLIC ::   nca    !: order of reaction for calcite dissolution

   INTEGER  ::   rmtss              ! number of seconds per month 

   !! * Module variables
   REAL(wp) :: calcon = 1.03E-2           !: mean calcite concentration [Ca2+]  in sea water [mole/kg solution]
   ! J. ORR: Made consistent with mocsy's choice based on literature review from Munhoven
!   REAL(wp) :: calcon = 1.0287E-2           !: mean calcite concentration [Ca2+] in sea water [mole/kg solution]

   !! * Substitutions
#  include "do_loop_substitute.h90"
   !!----------------------------------------------------------------------
   !! NEMO/TOP 4.0 , NEMO Consortium (2018)
   !! $Id: p4zlys.F90 15532 2021-11-24 11:47:32Z techene $ 
   !! Software governed by the CeCILL license (see ./LICENSE)
   !!----------------------------------------------------------------------

CONTAINS

   SUBROUTINE p4z_lys( kt, knt, Kbb, Krhs )
      !!---------------------------------------------------------------------
      !!                     ***  ROUTINE p4z_lys  ***
      !!
      !! ** Purpose :   CALCULATES DEGREE OF CACO3 SATURATION IN THE WATER
      !!                COLUMN, DISSOLUTION/PRECIPITATION OF CACO3 AND LOSS
      !!                OF CACO3 TO THE CACO3 SEDIMENT POOL.
      !!
      !! ** Method  : - ???
      !!---------------------------------------------------------------------
      INTEGER, INTENT(in) ::   kt, knt   ! ocean time step and ???
      INTEGER, INTENT(in)  ::  Kbb, Krhs ! time level indices
      !
      INTEGER  ::   ji, jj, jk, jn
      REAL(wp) ::   zdispot, zrhd, zcalcon
      REAL(wp) ::   zomegaca, zexcess, zexcess0, zkd, zco3, ztra
      CHARACTER (len=25) ::   charout
      REAL(wp), DIMENSION(A2D(0),jpk) :: zhinit, zhi
      REAL(wp), ALLOCATABLE, DIMENSION(:,:,:) ::  zcaldiss, zbicarb, zco3sat
      !!---------------------------------------------------------------------
      !
      IF( ln_timing )  CALL timing_start('p4z_lys')
      !
      IF( iom_use( "CO3"    ) ) ALLOCATE( zbicarb (A2D(0),jpk) )  
      IF( iom_use( "CO3sat" ) ) ALLOCATE( zco3sat (A2D(0),jpk) ) 
      IF( iom_use( "DCAL"   ) ) ALLOCATE( zcaldiss(A2D(0),jpk) ) 

      DO_3D( 0, 0, 0, 0, 1, jpkm1)
        zhinit(ji,jj,jk) = hi(ji,jj,jk) / ( rhd(ji,jj,jk) + 1._wp )
      END_3D
      !
      !     -------------------------------------------
      !     COMPUTE [CO3--] and [H+] CONCENTRATIONS
      !     -------------------------------------------

      CALL solve_at_general( zhinit, zhi, Kbb )

      DO_3D( 0, 0, 0, 0, 1, jpkm1)
         !
         zco3           = tr(ji,jj,jk,jpdic,Kbb) * ak13(ji,jj,jk) * ak23(ji,jj,jk) / (zhi(ji,jj,jk)**2   &
            &             + ak13(ji,jj,jk) * zhi(ji,jj,jk) + ak13(ji,jj,jk) * ak23(ji,jj,jk) + rtrn )
         hi  (ji,jj,jk) = zhi(ji,jj,jk) * ( rhd(ji,jj,jk) + 1._wp )

         ! CALCULATE DEGREE OF CACO3 SATURATION AND CORRESPONDING
         ! DISSOLOUTION AND PRECIPITATION OF CACO3 (BE AWARE OF MGCO3)


         ! DEVIATION OF [CO3--] FROM SATURATION VALUE
         ! Salinity dependance in zomegaca and divide by rhd to have good units
         zcalcon  = calcon * ( salinprac(ji,jj,jk) / 35._wp )
         zrhd    = rhd(ji,jj,jk) + 1._wp
         zomegaca = ( zcalcon * zco3 ) / ( aksp(ji,jj,jk) * zrhd + rtrn )

         ! SET DEGREE OF UNDER-/SUPERSATURATION
         excess(ji,jj,jk) = 1._wp - zomegaca
         zexcess0 = MAX( 0., excess(ji,jj,jk) )

         IF( zomegaca < 0.8 ) THEN
            zexcess = zexcess0**nca
            ! AMOUNT CACO3 THAT RE-ENTERS SOLUTION
            zdispot = kdca * zexcess * tr(ji,jj,jk,jpcal,Kbb)
         ELSE
            zkd = kdca * 0.2**(nca - 0.11)
            zexcess = zexcess0**0.11
            zdispot = zkd * zexcess * tr(ji,jj,jk,jpcal,Kbb)
        ENDIF

        !  CHANGE OF [CO3--] , [ALK], PARTICULATE [CACO3],
        !       AND [SUM(CO2)] DUE TO CACO3 DISSOLUTION/PRECIPITATION
        ztra =  zdispot * rfact2 / rmtss ! calcite dissolution
        !
        tr(ji,jj,jk,jptal,Krhs) = tr(ji,jj,jk,jptal,Krhs) + 2. * ztra
        tr(ji,jj,jk,jpcal,Krhs) = tr(ji,jj,jk,jpcal,Krhs) -      ztra
        tr(ji,jj,jk,jpdic,Krhs) = tr(ji,jj,jk,jpdic,Krhs) +      ztra
        ! 
        IF( iom_use( "CO3"    ) ) zbicarb (ji,jj,jk) = zco3                       !  bicarbonate
        IF( iom_use( "CO3sat" ) ) zco3sat (ji,jj,jk) = zco3 / ( zomegaca + rtrn )  ! calcite saturation
        IF( iom_use( "DCAL"   ) ) zcaldiss(ji,jj,jk) = ztra                        ! calcite dissolution
        !
      END_3D
      !

      IF( lk_iomput .AND. knt == nrdttrc ) THEN
         IF( iom_use( "PH" ) )  CALL iom_put( "PH" , -1. * LOG10( MAX( hi(A2D(0),1:jpk), rtrn ) ) * tmask(A2D(0),1:jpk) )
         IF( iom_use( "CO3"    ) ) THEN  ! bicarbonate 
            zbicarb(A2D(0),jpk)  = 0. 
            CALL iom_put( "CO3"   , zbicarb(A2D(0),1:jpk) * 1.e+3 * tmask(A2D(0),1:jpk) ) 
            DEALLOCATE( zbicarb )
         ENDIF
         IF( iom_use( "CO3sat" ) ) THEN   ! calcite saturation
            zco3sat(A2D(0),jpk)  = 0. 
            CALL iom_put( "CO3sat", zco3sat(A2D(0),1:jpk) * 1.e+3 * tmask(A2D(0),1:jpk) ) 
            DEALLOCATE( zco3sat )
         ENDIF
         IF( iom_use( "DCAL" ) ) THEN  ! calcite dissolution
            zcaldiss(A2D(0),jpk)  = 0. 
            CALL iom_put( "DCAL", zcaldiss(A2D(0),1:jpk) * 1.e+3 * rfact2r * tmask(A2D(0),1:jpk) ) 
            DEALLOCATE( zcaldiss )
         ENDIF              
      ENDIF
      !
      IF(sn_cfctl%l_prttrc)   THEN  ! print mean trends (used for debugging)
        WRITE(charout, FMT="('lys ')")
        CALL prt_ctl_info( charout, cdcomp = 'top' )
        CALL prt_ctl(tab4d_1=tr(:,:,:,:,Krhs), mask1=tmask, clinfo=ctrcnm)
      ENDIF
      !
      IF( ln_timing )   CALL timing_stop('p4z_lys')
      !
   END SUBROUTINE p4z_lys


   SUBROUTINE p4z_lys_init
      !!----------------------------------------------------------------------
      !!                  ***  ROUTINE p4z_lys_init  ***
      !!
      !! ** Purpose :   Initialization of CaCO3 dissolution parameters
      !!
      !! ** Method  :   Read the nampiscal namelist and check the parameters
      !!      called at the first timestep (nittrc000)
      !!
      !! ** input   :   Namelist nampiscal
      !!
      !!----------------------------------------------------------------------
      INTEGER ::   ios   ! Local integer
      !
      NAMELIST/nampiscal/ kdca, nca
      !!----------------------------------------------------------------------
      IF(lwp) THEN
         WRITE(numout,*)
         WRITE(numout,*) 'p4z_lys_init : initialization of CaCO3 dissolution'
         WRITE(numout,*) '~~~~~~~~~~~~'
      ENDIF
      !
      READ  ( numnatp_ref, nampiscal, IOSTAT = ios, ERR = 901)
901   IF( ios /= 0 )   CALL ctl_nam ( ios , 'nampiscal in reference namelist' )
      READ  ( numnatp_cfg, nampiscal, IOSTAT = ios, ERR = 902 )
902   IF( ios >  0 )   CALL ctl_nam ( ios , 'nampiscal in configuration namelist' )
      IF(lwm) WRITE( numonp, nampiscal )
      !
      IF(lwp) THEN                         ! control print
         WRITE(numout,*) '   Namelist : nampiscal'
         WRITE(numout,*) '      diss. rate constant calcite (per month)        kdca =', kdca
         WRITE(numout,*) '      order of reaction for calcite dissolution      nca  =', nca
      ENDIF
      !
      ! Number of seconds per month 
      rmtss =  nyear_len(1) * rday / raamo
      !
   END SUBROUTINE p4z_lys_init

   !!======================================================================
END MODULE p4zlys
