Index: NEMO/branches/2020/dev_r13327_KERNEL-06_2_techene_e3/src/OCE/DIA/diaptr.F90
===================================================================
--- NEMO/branches/2020/dev_r13327_KERNEL-06_2_techene_e3/src/OCE/DIA/diaptr.F90	(revision 13426)
+++ NEMO/branches/2020/dev_r13327_KERNEL-06_2_techene_e3/src/OCE/DIA/diaptr.F90	(revision 13998)
@@ -36,16 +36,11 @@
    END INTERFACE
 
-   PUBLIC   ptr_sj         ! call by tra_ldf & tra_adv routines
-   PUBLIC   ptr_sjk        ! 
-   PUBLIC   dia_ptr_init   ! call in memogcm
    PUBLIC   dia_ptr        ! call in step module
    PUBLIC   dia_ptr_hst    ! called from tra_ldf/tra_adv routines
 
-   !                                  !!** namelist  namptr  **
    REAL(wp), ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::   hstr_adv, hstr_ldf, hstr_eiv   !: Heat/Salt TRansports(adv, diff, Bolus.)
    REAL(wp), ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::   hstr_ove, hstr_btr, hstr_vtr   !: heat Salt TRansports(overturn, baro, merional)
 
-   LOGICAL , PUBLIC ::   l_diaptr        !: tracers  trend flag (set from namelist in trdini)
-   INTEGER, PARAMETER, PUBLIC ::   nptr = 5  ! (glo, atl, pac, ind, ipc)
+   LOGICAL, PUBLIC ::   l_diaptr       !: tracers  trend flag
 
    REAL(wp) ::   rc_sv    = 1.e-6_wp   ! conversion from m3/s to Sverdrup
@@ -59,5 +54,5 @@
    REAL(wp), TARGET, ALLOCATABLE, SAVE, DIMENSION(:,:) :: p_fval2d
 
-   LOGICAL ::   ll_init = .TRUE.        !: tracers  trend flag (set from namelist in trdini)
+   LOGICAL ::   ll_init = .TRUE.        !: tracers  trend flag
    
    !! * Substitutions
@@ -88,20 +83,26 @@
       !
       !overturning calculation
-      REAL(wp), DIMENSION(jpj,jpk,nptr) :: sjk, r1_sjk, v_msf  ! i-mean i-k-surface and its inverse
-      REAL(wp), DIMENSION(jpj,jpk,nptr) :: zt_jk, zs_jk ! i-mean T and S, j-Stream-Function
-
-      REAL(wp), DIMENSION(jpi,jpj,jpk,nptr)  :: z4d1, z4d2
-      REAL(wp), DIMENSION(jpi,jpj,nptr)      :: z3dtr ! i-mean T and S, j-Stream-Function
+      REAL(wp), DIMENSION(:,:,:  ), ALLOCATABLE ::   sjk, r1_sjk, v_msf  ! i-mean i-k-surface and its inverse
+      REAL(wp), DIMENSION(:,:,:  ), ALLOCATABLE ::   zt_jk, zs_jk        ! i-mean T and S, j-Stream-Function
+
+      REAL(wp), DIMENSION(:,:,:,:), ALLOCATABLE ::   z4d1, z4d2
+      REAL(wp), DIMENSION(:,:,:  ), ALLOCATABLE ::   z3dtr
       !!----------------------------------------------------------------------
       !
       IF( ln_timing )   CALL timing_start('dia_ptr')
 
-      IF( kt == nit000 .AND. ll_init )   CALL dia_ptr_init
-      !
-      IF( .NOT. l_diaptr )   RETURN
-
+      IF( kt == nit000 .AND. ll_init )   CALL dia_ptr_init   ! -> will define l_diaptr and nbasin
+      !
+      IF( .NOT. l_diaptr ) THEN
+         IF( ln_timing ) CALL timing_stop('dia_ptr')
+         RETURN
+      ENDIF
+      !
+      ALLOCATE( z3dtr(jpi,jpj,nbasin) )
+      !
       IF( PRESENT( pvtr ) ) THEN
          IF( iom_use( 'zomsf' ) ) THEN    ! effective MSF
-            DO jn = 1, nptr                                    ! by sub-basins
+            ALLOCATE( z4d1(jpi,jpj,jpk,nbasin) )
+            DO jn = 1, nbasin                                    ! by sub-basins
                z4d1(1,:,:,jn) =  ptr_sjk( pvtr(:,:,:), btmsk34(:,:,jn) )  ! zonal cumulative effective transport excluding closed seas
                DO jk = jpkm1, 1, -1 
@@ -113,4 +114,5 @@
             END DO
             CALL iom_put( 'zomsf', z4d1 * rc_sv )
+            DEALLOCATE( z4d1 )
          ENDIF
          IF(  iom_use( 'sopstove' ) .OR. iom_use( 'sophtove' ) .OR.   &
@@ -127,5 +129,7 @@
          ENDIF
          IF( iom_use( 'sopstove' ) .OR. iom_use( 'sophtove' ) ) THEN
-            DO jn = 1, nptr
+            DO jn = 1, nbasin
+               ALLOCATE( sjk(jpj,jpk,nbasin), r1_sjk(jpj,jpk,nbasin), v_msf(jpj,jpk,nbasin),   &
+                  &                          zt_jk(jpj,jpk,nbasin), zs_jk(jpj,jpk,nbasin) )
                sjk(:,:,jn) = ptr_sjk( zmask(:,:,:), btmsk(:,:,jn) )
                r1_sjk(:,:,jn) = 0._wp
@@ -137,7 +141,8 @@
                hstr_ove(:,jp_tem,jn) = SUM( v_msf(:,:,jn)*zt_jk(:,:,jn), 2 )
                hstr_ove(:,jp_sal,jn) = SUM( v_msf(:,:,jn)*zs_jk(:,:,jn), 2 )
+               DEALLOCATE( sjk, r1_sjk, v_msf, zt_jk, zs_jk )
                !
             ENDDO
-            DO jn = 1, nptr
+            DO jn = 1, nbasin
                z3dtr(1,:,jn) = hstr_ove(:,jp_tem,jn) * rc_pwatt  !  (conversion in PW)
                DO ji = 1, jpi
@@ -146,5 +151,5 @@
             ENDDO
             CALL iom_put( 'sophtove', z3dtr )
-            DO jn = 1, nptr
+            DO jn = 1, nbasin
                z3dtr(1,:,jn) = hstr_ove(:,jp_sal,jn) * rc_ggram !  (conversion in Gg)
                DO ji = 1, jpi
@@ -157,5 +162,6 @@
          IF( iom_use( 'sopstbtr' ) .OR. iom_use( 'sophtbtr' ) ) THEN
             ! Calculate barotropic heat and salt transport here 
-            DO jn = 1, nptr
+            DO jn = 1, nbasin
+               ALLOCATE( sjk(jpj,1,nbasin), r1_sjk(jpj,1,nbasin) )
                sjk(:,1,jn) = ptr_sj( zmask(:,:,:), btmsk(:,:,jn) )
                r1_sjk(:,1,jn) = 0._wp
@@ -167,7 +173,8 @@
                hstr_btr(:,jp_tem,jn) = zvsum(:) * ztsum(:) * r1_sjk(:,1,jn)
                hstr_btr(:,jp_sal,jn) = zvsum(:) * zssum(:) * r1_sjk(:,1,jn)
+               DEALLOCATE( sjk, r1_sjk )
                !
             ENDDO
-            DO jn = 1, nptr
+            DO jn = 1, nbasin
                z3dtr(1,:,jn) = hstr_btr(:,jp_tem,jn) * rc_pwatt  !  (conversion in PW)
                DO ji = 1, jpi
@@ -176,5 +183,5 @@
             ENDDO
             CALL iom_put( 'sophtbtr', z3dtr )
-            DO jn = 1, nptr
+            DO jn = 1, nbasin
                z3dtr(1,:,jn) = hstr_btr(:,jp_sal,jn) * rc_ggram !  (conversion in Gg)
                DO ji = 1, jpi
@@ -190,4 +197,5 @@
          zts(:,:,:,:) = 0._wp
          IF( iom_use( 'zotem' ) .OR. iom_use( 'zosal' ) .OR. iom_use( 'zosrf' )  ) THEN    ! i-mean i-k-surface 
+            ALLOCATE( z4d1(jpi,jpj,jpk,nbasin), z4d2(jpi,jpj,jpk,nbasin) )
             DO_3D( 1, 1, 1, 1, 1, jpkm1 )
                zsfc = e1t(ji,jj) * e3t(ji,jj,jk,Kmm)
@@ -197,11 +205,14 @@
             END_3D
             !
-            DO jn = 1, nptr
+            DO jn = 1, nbasin
                zmask(1,:,:) = ptr_sjk( zmask(:,:,:), btmsk(:,:,jn) )
+               DO ji = 1, jpi
+                  zmask(ji,:,:) = zmask(1,:,:)
+               ENDDO
                z4d1(:,:,:,jn) = zmask(:,:,:)
             ENDDO
             CALL iom_put( 'zosrf', z4d1 )
             !
-            DO jn = 1, nptr
+            DO jn = 1, nbasin
                z4d2(1,:,:,jn) = ptr_sjk( zts(:,:,:,jp_tem), btmsk(:,:,jn) ) &
                   &            / MAX( z4d1(1,:,:,jn), 10.e-15 )
@@ -212,5 +223,5 @@
             CALL iom_put( 'zotem', z4d2 )
             !
-            DO jn = 1, nptr
+            DO jn = 1, nbasin
                z4d2(1,:,:,jn) = ptr_sjk( zts(:,:,:,jp_sal), btmsk(:,:,jn) ) &
                   &            / MAX( z4d1(1,:,:,jn), 10.e-15 )
@@ -220,4 +231,5 @@
             ENDDO
             CALL iom_put( 'zosal', z4d2 )
+            DEALLOCATE( z4d1, z4d2 )
             !
          ENDIF
@@ -226,5 +238,5 @@
          IF( iom_use( 'sophtadv' ) .OR. iom_use( 'sopstadv' ) ) THEN  
             ! 
-            DO jn = 1, nptr
+            DO jn = 1, nbasin
                z3dtr(1,:,jn) = hstr_adv(:,jp_tem,jn) * rc_pwatt  !  (conversion in PW)
                DO ji = 1, jpi
@@ -233,5 +245,5 @@
             ENDDO
             CALL iom_put( 'sophtadv', z3dtr )
-            DO jn = 1, nptr
+            DO jn = 1, nbasin
                z3dtr(1,:,jn) = hstr_adv(:,jp_sal,jn) * rc_ggram !  (conversion in Gg)
                DO ji = 1, jpi
@@ -244,5 +256,5 @@
          IF( iom_use( 'sophtldf' ) .OR. iom_use( 'sopstldf' ) ) THEN  
             ! 
-            DO jn = 1, nptr
+            DO jn = 1, nbasin
                z3dtr(1,:,jn) = hstr_ldf(:,jp_tem,jn) * rc_pwatt  !  (conversion in PW)
                DO ji = 1, jpi
@@ -251,5 +263,5 @@
             ENDDO
             CALL iom_put( 'sophtldf', z3dtr )
-            DO jn = 1, nptr
+            DO jn = 1, nbasin
                z3dtr(1,:,jn) = hstr_ldf(:,jp_sal,jn) * rc_ggram !  (conversion in Gg)
                DO ji = 1, jpi
@@ -262,5 +274,5 @@
          IF( iom_use( 'sophteiv' ) .OR. iom_use( 'sopsteiv' ) ) THEN  
             ! 
-            DO jn = 1, nptr
+            DO jn = 1, nbasin
                z3dtr(1,:,jn) = hstr_eiv(:,jp_tem,jn) * rc_pwatt  !  (conversion in PW)
                DO ji = 1, jpi
@@ -269,5 +281,5 @@
             ENDDO
             CALL iom_put( 'sophteiv', z3dtr )
-            DO jn = 1, nptr
+            DO jn = 1, nbasin
                z3dtr(1,:,jn) = hstr_eiv(:,jp_sal,jn) * rc_ggram !  (conversion in Gg)
                DO ji = 1, jpi
@@ -287,5 +299,5 @@
              CALL dia_ptr_hst( jp_tem, 'vtr', zts(:,:,:,jp_tem) )
              CALL dia_ptr_hst( jp_sal, 'vtr', zts(:,:,:,jp_sal) )
-             DO jn = 1, nptr
+             DO jn = 1, nbasin
                 z3dtr(1,:,jn) = hstr_vtr(:,jp_tem,jn) * rc_pwatt  !  (conversion in PW)
                 DO ji = 1, jpi
@@ -294,5 +306,5 @@
              ENDDO
              CALL iom_put( 'sophtvtr', z3dtr )
-             DO jn = 1, nptr
+             DO jn = 1, nbasin
                z3dtr(1,:,jn) = hstr_vtr(:,jp_sal,jn) * rc_ggram !  (conversion in Gg)
                DO ji = 1, jpi
@@ -311,4 +323,6 @@
       ENDIF
       !
+      DEALLOCATE( z3dtr )
+      !
       IF( ln_timing )   CALL timing_stop('dia_ptr')
       !
@@ -320,5 +334,5 @@
       !!                  ***  ROUTINE dia_ptr_init  ***
       !!                   
-      !! ** Purpose :   Initialization, namelist read
+      !! ** Purpose :   Initialization
       !!----------------------------------------------------------------------
       INTEGER ::  inum, jn           ! local integers
@@ -326,13 +340,14 @@
       REAL(wp), DIMENSION(jpi,jpj) :: zmsk
       !!----------------------------------------------------------------------
-
-      l_diaptr = .FALSE.
-      IF(   iom_use( 'zomsf'    ) .OR. iom_use( 'zotem'    ) .OR. iom_use( 'zosal'    ) .OR.  &
-         &  iom_use( 'zosrf'    ) .OR. iom_use( 'sopstove' ) .OR. iom_use( 'sophtove' ) .OR.  &
-         &  iom_use( 'sopstbtr' ) .OR. iom_use( 'sophtbtr' ) .OR. iom_use( 'sophtadv' ) .OR.  &
-         &  iom_use( 'sopstadv' ) .OR. iom_use( 'sophtldf' ) .OR. iom_use( 'sopstldf' ) .OR.  & 
-         &  iom_use( 'sophteiv' ) .OR. iom_use( 'sopsteiv' ) .OR. iom_use( 'sopstvtr' ) .OR.  &
-         &  iom_use( 'sophtvtr' ) .OR. iom_use( 'uocetr_vsum_cumul' ) )  l_diaptr  = .TRUE.
-
+      
+      ! l_diaptr is defined with iom_use
+      !   --> dia_ptr_init must be done after the call to iom_init
+      !   --> cannot be .TRUE. without cpp key: key_iom -->  nbasin define by iom_init is initialized
+      l_diaptr = iom_use( 'zomsf'    ) .OR. iom_use( 'zotem'    ) .OR. iom_use( 'zosal'    ) .OR.  &
+         &       iom_use( 'zosrf'    ) .OR. iom_use( 'sopstove' ) .OR. iom_use( 'sophtove' ) .OR.  &
+         &       iom_use( 'sopstbtr' ) .OR. iom_use( 'sophtbtr' ) .OR. iom_use( 'sophtadv' ) .OR.  &
+         &       iom_use( 'sopstadv' ) .OR. iom_use( 'sophtldf' ) .OR. iom_use( 'sopstldf' ) .OR.  & 
+         &       iom_use( 'sophteiv' ) .OR. iom_use( 'sopsteiv' ) .OR. iom_use( 'sopstvtr' ) .OR.  &
+         &       iom_use( 'sophtvtr' ) .OR. iom_use( 'uocetr_vsum_cumul' ) 
  
       IF(lwp) THEN                     ! Control print
@@ -340,5 +355,4 @@
          WRITE(numout,*) 'dia_ptr_init : poleward transport and msf initialization'
          WRITE(numout,*) '~~~~~~~~~~~~'
-         WRITE(numout,*) '   Namelist namptr : set ptr parameters'
          WRITE(numout,*) '      Poleward heat & salt transport (T) or not (F)      l_diaptr  = ', l_diaptr
       ENDIF
@@ -347,5 +361,5 @@
          !
          IF( dia_ptr_alloc() /= 0 )   CALL ctl_stop( 'STOP', 'dia_ptr_init : unable to allocate arrays' )
-
+         !
          rc_pwatt = rc_pwatt * rho0_rcp          ! conversion from K.s-1 to PetaWatt
          rc_ggram = rc_ggram * rho0              ! conversion from m3/s to Gg/s
@@ -354,12 +368,14 @@
 
          btmsk(:,:,1) = tmask_i(:,:)                 
-         CALL iom_open( 'subbasins', inum,  ldstop = .FALSE.  )
-         CALL iom_get( inum, jpdom_global, 'atlmsk', btmsk(:,:,2) )   ! Atlantic basin
-         CALL iom_get( inum, jpdom_global, 'pacmsk', btmsk(:,:,3) )   ! Pacific  basin
-         CALL iom_get( inum, jpdom_global, 'indmsk', btmsk(:,:,4) )   ! Indian   basin
-         CALL iom_close( inum )
-         btmsk(:,:,5) = MAX ( btmsk(:,:,3), btmsk(:,:,4) )          ! Indo-Pacific basin
-         DO jn = 2, nptr
-            btmsk(:,:,jn) = btmsk(:,:,jn) * tmask_i(:,:)               ! interior domain only
+         IF( nbasin == 5 ) THEN   ! nbasin has been initialized in iom_init to define the axis "basin"
+            CALL iom_open( 'subbasins', inum )
+            CALL iom_get( inum, jpdom_global, 'atlmsk', btmsk(:,:,2) )   ! Atlantic basin
+            CALL iom_get( inum, jpdom_global, 'pacmsk', btmsk(:,:,3) )   ! Pacific  basin
+            CALL iom_get( inum, jpdom_global, 'indmsk', btmsk(:,:,4) )   ! Indian   basin
+            CALL iom_close( inum )
+            btmsk(:,:,5) = MAX ( btmsk(:,:,3), btmsk(:,:,4) )            ! Indo-Pacific basin
+         ENDIF
+         DO jn = 2, nbasin
+            btmsk(:,:,jn) = btmsk(:,:,jn) * tmask_i(:,:)                 ! interior domain only
          END DO
          ! JD : modification so that overturning streamfunction is available in Atlantic at 34S to compare with observations
@@ -370,6 +386,6 @@
          END WHERE
          btmsk34(:,:,1) = btmsk(:,:,1)                 
-         DO jn = 2, nptr
-            btmsk34(:,:,jn) = btmsk(:,:,jn) * zmsk(:,:)               ! interior domain only
+         DO jn = 2, nbasin
+            btmsk34(:,:,jn) = btmsk(:,:,jn) * zmsk(:,:)                  ! interior domain only
          ENDDO
 
@@ -405,10 +421,10 @@
       IF( cptr == 'adv' ) THEN
          IF( ktra == jp_tem )  THEN
-             DO jn = 1, nptr
+             DO jn = 1, nbasin
                 hstr_adv(:,jp_tem,jn) = ptr_sj( pvflx(:,:,:), btmsk(:,:,jn) )
              ENDDO
          ENDIF
          IF( ktra == jp_sal )  THEN
-             DO jn = 1, nptr
+             DO jn = 1, nbasin
                 hstr_adv(:,jp_sal,jn) = ptr_sj( pvflx(:,:,:), btmsk(:,:,jn) )
              ENDDO
@@ -418,10 +434,10 @@
       IF( cptr == 'ldf' ) THEN
          IF( ktra == jp_tem )  THEN
-             DO jn = 1, nptr
+             DO jn = 1, nbasin
                 hstr_ldf(:,jp_tem,jn) = ptr_sj( pvflx(:,:,:), btmsk(:,:,jn) )
              ENDDO
          ENDIF
          IF( ktra == jp_sal )  THEN
-             DO jn = 1, nptr
+             DO jn = 1, nbasin
                 hstr_ldf(:,jp_sal,jn) = ptr_sj( pvflx(:,:,:), btmsk(:,:,jn) )
              ENDDO
@@ -431,10 +447,10 @@
       IF( cptr == 'eiv' ) THEN
          IF( ktra == jp_tem )  THEN
-             DO jn = 1, nptr
+             DO jn = 1, nbasin
                 hstr_eiv(:,jp_tem,jn) = ptr_sj( pvflx(:,:,:), btmsk(:,:,jn) )
              ENDDO
          ENDIF
          IF( ktra == jp_sal )  THEN
-             DO jn = 1, nptr
+             DO jn = 1, nbasin
                 hstr_eiv(:,jp_sal,jn) = ptr_sj( pvflx(:,:,:), btmsk(:,:,jn) )
              ENDDO
@@ -444,10 +460,10 @@
       IF( cptr == 'vtr' ) THEN
          IF( ktra == jp_tem )  THEN
-             DO jn = 1, nptr
+             DO jn = 1, nbasin
                 hstr_vtr(:,jp_tem,jn) = ptr_sj( pvflx(:,:,:), btmsk(:,:,jn) )
              ENDDO
          ENDIF
          IF( ktra == jp_sal )  THEN
-             DO jn = 1, nptr
+             DO jn = 1, nbasin
                 hstr_vtr(:,jp_sal,jn) = ptr_sj( pvflx(:,:,:), btmsk(:,:,jn) )
              ENDDO
@@ -467,9 +483,11 @@
       ierr(:) = 0
       !
+      ! nbasin has been initialized in iom_init to define the axis "basin"
+      !
       IF( .NOT. ALLOCATED( btmsk ) ) THEN
-         ALLOCATE( btmsk(jpi,jpj,nptr)    , btmsk34(jpi,jpj,nptr),   &
-            &      hstr_adv(jpj,jpts,nptr), hstr_eiv(jpj,jpts,nptr), &
-            &      hstr_ove(jpj,jpts,nptr), hstr_btr(jpj,jpts,nptr), &
-            &      hstr_ldf(jpj,jpts,nptr), hstr_vtr(jpj,jpts,nptr), STAT=ierr(1)  )
+         ALLOCATE( btmsk(jpi,jpj,nbasin)    , btmsk34(jpi,jpj,nbasin),   &
+            &      hstr_adv(jpj,jpts,nbasin), hstr_eiv(jpj,jpts,nbasin), &
+            &      hstr_ove(jpj,jpts,nbasin), hstr_btr(jpj,jpts,nbasin), &
+            &      hstr_ldf(jpj,jpts,nbasin), hstr_vtr(jpj,jpts,nbasin), STAT=ierr(1)  )
             !
          ALLOCATE( p_fval1d(jpj), p_fval2d(jpj,jpk), Stat=ierr(2))
