Index: NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/DIA/diaar5.F90
===================================================================
--- NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/DIA/diaar5.F90	(revision 11396)
+++ NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/DIA/diaar5.F90	(revision 13463)
@@ -32,5 +32,4 @@
    REAL(wp)                         ::   vol0         ! ocean volume (interior domain)
    REAL(wp)                         ::   area_tot     ! total ocean surface (interior domain)
-   REAL(wp), ALLOCATABLE, SAVE, DIMENSION(:,:  ) ::   area         ! cell surface (interior domain)
    REAL(wp), ALLOCATABLE, SAVE, DIMENSION(:,:  ) ::   thick0       ! ocean thickness (interior domain)
    REAL(wp), ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::   sn0          ! initial salinity
@@ -39,5 +38,6 @@
       
    !! * Substitutions
-#  include "vectopt_loop_substitute.h90"
+#  include "do_loop_substitute.h90"
+#  include "domzgr_substitute.h90"
    !!----------------------------------------------------------------------
    !! NEMO/OCE 4.0 , NEMO Consortium (2018)
@@ -54,5 +54,5 @@
       !!----------------------------------------------------------------------
       !
-      ALLOCATE( area(jpi,jpj), thick0(jpi,jpj) , sn0(jpi,jpj,jpk) , STAT=dia_ar5_alloc )
+      ALLOCATE( thick0(jpi,jpj) , sn0(jpi,jpj,jpk) , STAT=dia_ar5_alloc )
       !
       CALL mpp_sum ( 'diaar5', dia_ar5_alloc )
@@ -62,5 +62,5 @@
 
 
-   SUBROUTINE dia_ar5( kt )
+   SUBROUTINE dia_ar5( kt, Kmm )
       !!----------------------------------------------------------------------
       !!                    ***  ROUTINE dia_ar5  ***
@@ -70,12 +70,13 @@
       !
       INTEGER, INTENT( in ) ::   kt   ! ocean time-step index
-      !
-      INTEGER  ::   ji, jj, jk                      ! dummy loop arguments
-      REAL(wp) ::   zvolssh, zvol, zssh_steric, zztmp, zarho, ztemp, zsal, zmass
+      INTEGER, INTENT( in ) ::   Kmm  ! ocean time level index
+      !
+      INTEGER  ::   ji, jj, jk, iks, ikb                      ! dummy loop arguments
+      REAL(wp) ::   zvolssh, zvol, zssh_steric, zztmp, zarho, ztemp, zsal, zmass, zsst
       REAL(wp) ::   zaw, zbw, zrw
       !
       REAL(wp), ALLOCATABLE, DIMENSION(:,:)     :: zarea_ssh , zbotpres       ! 2D workspace 
-      REAL(wp), ALLOCATABLE, DIMENSION(:,:)     :: zpe                         ! 2D workspace 
-      REAL(wp), ALLOCATABLE, DIMENSION(:,:,:)   :: zrhd , zrhop               ! 3D workspace
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:)     :: z2d, zpe                   ! 2D workspace 
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:,:)   :: z3d, zrhd, ztpot, zgdept   ! 3D workspace (zgdept: needed to use the substitute)
       REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:) :: ztsn                       ! 4D workspace
 
@@ -86,19 +87,42 @@
 
       IF( l_ar5 ) THEN 
-         ALLOCATE( zarea_ssh(jpi,jpj) , zbotpres(jpi,jpj) )
-         ALLOCATE( zrhd(jpi,jpj,jpk) , zrhop(jpi,jpj,jpk) )
+         ALLOCATE( zarea_ssh(jpi,jpj), zbotpres(jpi,jpj), z2d(jpi,jpj) )
+         ALLOCATE( zrhd(jpi,jpj,jpk) )
          ALLOCATE( ztsn(jpi,jpj,jpk,jpts) )
-         zarea_ssh(:,:) = area(:,:) * sshn(:,:)
-      ENDIF
+         zarea_ssh(:,:) = e1e2t(:,:) * ssh(:,:,Kmm)
+      ENDIF
+      !
+      CALL iom_put( 'e2u'      , e2u  (:,:) )
+      CALL iom_put( 'e1v'      , e1v  (:,:) )
+      CALL iom_put( 'areacello', e1e2t(:,:) )
+      !
+      IF( iom_use( 'volcello' ) .OR. iom_use( 'masscello' )  ) THEN  
+         zrhd(:,:,jpk) = 0._wp        ! ocean volume ; rhd is used as workspace
+         DO jk = 1, jpkm1
+            zrhd(:,:,jk) = e1e2t(:,:) * e3t(:,:,jk,Kmm) * tmask(:,:,jk)
+         END DO
+         DO jk = 1, jpk
+            z3d(:,:,jk) =  rho0 * e3t(:,:,jk,Kmm) * tmask(:,:,jk)
+         END DO 
+         CALL iom_put( 'volcello'  , zrhd(:,:,:)  )  ! WARNING not consistent with CMIP DR where volcello is at ca. 2000
+         CALL iom_put( 'masscello' , z3d (:,:,:) )   ! ocean mass
+      ENDIF 
+      !
+      IF( iom_use( 'e3tb' ) )  THEN    ! bottom layer thickness
+         DO_2D( 1, 1, 1, 1 )
+            ikb = mbkt(ji,jj)
+            z2d(ji,jj) = e3t(ji,jj,ikb,Kmm)
+         END_2D
+         CALL iom_put( 'e3tb', z2d )
+      ENDIF 
       !
       IF( iom_use( 'voltot' ) .OR. iom_use( 'sshtot' )  .OR. iom_use( 'sshdyn' )  ) THEN    
          !                                         ! total volume of liquid seawater
-         zvolssh = SUM( zarea_ssh(:,:) ) 
-         CALL mpp_sum( 'diaar5', zvolssh )
-         zvol = vol0 + zvolssh
+         zvolssh = glob_sum( 'diaar5', zarea_ssh(:,:) ) 
+         zvol    = vol0 + zvolssh
       
          CALL iom_put( 'voltot', zvol               )
          CALL iom_put( 'sshtot', zvolssh / area_tot )
-         CALL iom_put( 'sshdyn', sshn(:,:) - (zvolssh / area_tot) )
+         CALL iom_put( 'sshdyn', ssh(:,:,Kmm) - (zvolssh / area_tot) )
          !
       ENDIF
@@ -106,11 +130,15 @@
       IF( iom_use( 'botpres' ) .OR. iom_use( 'sshthster' )  .OR. iom_use( 'sshsteric' )  ) THEN    
          !                     
-         ztsn(:,:,:,jp_tem) = tsn(:,:,:,jp_tem)                    ! thermosteric ssh
+         ztsn(:,:,:,jp_tem) = ts(:,:,:,jp_tem,Kmm)                    ! thermosteric ssh
          ztsn(:,:,:,jp_sal) = sn0(:,:,:)
-         CALL eos( ztsn, zrhd, gdept_n(:,:,:) )                       ! now in situ density using initial salinity
+         ALLOCATE( zgdept(jpi,jpj,jpk) )
+         DO jk = 1, jpk
+            zgdept(:,:,jk) = gdept(:,:,jk,Kmm)
+         END DO
+         CALL eos( ztsn, zrhd, zgdept)                       ! now in situ density using initial salinity
          !
          zbotpres(:,:) = 0._wp                        ! no atmospheric surface pressure, levitating sea-ice
          DO jk = 1, jpkm1
-            zbotpres(:,:) = zbotpres(:,:) + e3t_n(:,:,jk) * zrhd(:,:,jk)
+            zbotpres(:,:) = zbotpres(:,:) + e3t(:,:,jk,Kmm) * zrhd(:,:,jk)
          END DO
          IF( ln_linssh ) THEN
@@ -118,9 +146,10 @@
                DO ji = 1, jpi
                   DO jj = 1, jpj
-                     zbotpres(ji,jj) = zbotpres(ji,jj) + sshn(ji,jj) * zrhd(ji,jj,mikt(ji,jj)) + riceload(ji,jj)
+                     iks = mikt(ji,jj)
+                     zbotpres(ji,jj) = zbotpres(ji,jj) + ssh(ji,jj,Kmm) * zrhd(ji,jj,iks) + riceload(ji,jj)
                   END DO
                END DO
             ELSE
-               zbotpres(:,:) = zbotpres(:,:) + sshn(:,:) * zrhd(:,:,1)
+               zbotpres(:,:) = zbotpres(:,:) + ssh(:,:,Kmm) * zrhd(:,:,1)
             END IF
 !!gm
@@ -129,17 +158,12 @@
          END IF
          !                                         
-         zarho = SUM( area(:,:) * zbotpres(:,:) ) 
-         CALL mpp_sum( 'diaar5', zarho )
+         zarho = glob_sum( 'diaar5', e1e2t(:,:) * zbotpres(:,:) ) 
          zssh_steric = - zarho / area_tot
          CALL iom_put( 'sshthster', zssh_steric )
       
          !                                         ! steric sea surface height
-         CALL eos( tsn, zrhd, zrhop, gdept_n(:,:,:) )                 ! now in situ and potential density
-         zrhop(:,:,jpk) = 0._wp
-         CALL iom_put( 'rhop', zrhop )
-         !
          zbotpres(:,:) = 0._wp                        ! no atmospheric surface pressure, levitating sea-ice
          DO jk = 1, jpkm1
-            zbotpres(:,:) = zbotpres(:,:) + e3t_n(:,:,jk) * zrhd(:,:,jk)
+            zbotpres(:,:) = zbotpres(:,:) + e3t(:,:,jk,Kmm) * rhd(:,:,jk)
          END DO
          IF( ln_linssh ) THEN
@@ -147,103 +171,126 @@
                DO ji = 1,jpi
                   DO jj = 1,jpj
-                     zbotpres(ji,jj) = zbotpres(ji,jj) + sshn(ji,jj) * zrhd(ji,jj,mikt(ji,jj)) + riceload(ji,jj)
+                     iks = mikt(ji,jj)
+                     zbotpres(ji,jj) = zbotpres(ji,jj) + ssh(ji,jj,Kmm) * rhd(ji,jj,iks) + riceload(ji,jj)
                   END DO
                END DO
             ELSE
-               zbotpres(:,:) = zbotpres(:,:) + sshn(:,:) * zrhd(:,:,1)
+               zbotpres(:,:) = zbotpres(:,:) + ssh(:,:,Kmm) * rhd(:,:,1)
             END IF
          END IF
          !    
-         zarho = SUM( area(:,:) * zbotpres(:,:) ) 
-         CALL mpp_sum( 'diaar5', zarho )
+         zarho = glob_sum( 'diaar5', e1e2t(:,:) * zbotpres(:,:) ) 
          zssh_steric = - zarho / area_tot
          CALL iom_put( 'sshsteric', zssh_steric )
-      
          !                                         ! ocean bottom pressure
-         zztmp = rau0 * grav * 1.e-4_wp               ! recover pressure from pressure anomaly and cover to dbar = 1.e4 Pa
-         zbotpres(:,:) = zztmp * ( zbotpres(:,:) + sshn(:,:) + thick0(:,:) )
+         zztmp = rho0 * grav * 1.e-4_wp               ! recover pressure from pressure anomaly and cover to dbar = 1.e4 Pa
+         zbotpres(:,:) = zztmp * ( zbotpres(:,:) + ssh(:,:,Kmm) + thick0(:,:) )
          CALL iom_put( 'botpres', zbotpres )
          !
+         DEALLOCATE( zgdept )
+         !
       ENDIF
 
       IF( iom_use( 'masstot' ) .OR. iom_use( 'temptot' )  .OR. iom_use( 'saltot' )  ) THEN    
-         !                                         ! Mean density anomalie, temperature and salinity
-         ztemp = 0._wp
-         zsal  = 0._wp
-         DO jk = 1, jpkm1
-            DO jj = 1, jpj
-               DO ji = 1, jpi
-                  zztmp = area(ji,jj) * e3t_n(ji,jj,jk)
-                  ztemp = ztemp + zztmp * tsn(ji,jj,jk,jp_tem)
-                  zsal  = zsal  + zztmp * tsn(ji,jj,jk,jp_sal)
-               END DO
-            END DO
-         END DO
-         IF( ln_linssh ) THEN
+          !                                         ! Mean density anomalie, temperature and salinity
+          ztsn(:,:,:,:) = 0._wp                    ! ztsn(:,:,1,jp_tem/sal) is used here as 2D Workspace for temperature & salinity
+          DO_3D( 1, 1, 1, 1, 1, jpkm1 )
+             zztmp = e1e2t(ji,jj) * e3t(ji,jj,jk,Kmm)
+             ztsn(ji,jj,1,jp_tem) = ztsn(ji,jj,1,jp_tem) + zztmp * ts(ji,jj,jk,jp_tem,Kmm)
+             ztsn(ji,jj,1,jp_sal) = ztsn(ji,jj,1,jp_sal) + zztmp * ts(ji,jj,jk,jp_sal,Kmm)
+          END_3D
+
+          IF( ln_linssh ) THEN
             IF( ln_isfcav ) THEN
                DO ji = 1, jpi
                   DO jj = 1, jpj
-                     ztemp = ztemp + zarea_ssh(ji,jj) * tsn(ji,jj,mikt(ji,jj),jp_tem) 
-                     zsal  = zsal  + zarea_ssh(ji,jj) * tsn(ji,jj,mikt(ji,jj),jp_sal) 
+                     iks = mikt(ji,jj)
+                     ztsn(ji,jj,1,jp_tem) = ztsn(ji,jj,1,jp_tem) + zarea_ssh(ji,jj) * ts(ji,jj,iks,jp_tem,Kmm) 
+                     ztsn(ji,jj,1,jp_sal) = ztsn(ji,jj,1,jp_sal) + zarea_ssh(ji,jj) * ts(ji,jj,iks,jp_sal,Kmm) 
                   END DO
                END DO
             ELSE
-               ztemp = ztemp + SUM( zarea_ssh(:,:) * tsn(:,:,1,jp_tem) )
-               zsal  = zsal  + SUM( zarea_ssh(:,:) * tsn(:,:,1,jp_sal) )
+               ztsn(:,:,1,jp_tem) = ztsn(:,:,1,jp_tem) + zarea_ssh(:,:) * ts(:,:,1,jp_tem,Kmm) 
+               ztsn(:,:,1,jp_sal) = ztsn(:,:,1,jp_sal) + zarea_ssh(:,:) * ts(:,:,1,jp_sal,Kmm) 
             END IF
          ENDIF
-         IF( lk_mpp ) THEN  
-            CALL mpp_sum( 'diaar5', ztemp )
-            CALL mpp_sum( 'diaar5', zsal  )
-         END IF
-         !
-         zmass = rau0 * ( zarho + zvol )                 ! total mass of liquid seawater
-         ztemp = ztemp / zvol                            ! potential temperature in liquid seawater
-         zsal  = zsal  / zvol                            ! Salinity of liquid seawater
+         !
+         ztemp = glob_sum( 'diaar5', ztsn(:,:,1,jp_tem) )
+         zsal  = glob_sum( 'diaar5', ztsn(:,:,1,jp_sal) )
+         zmass = rho0 * ( zarho + zvol )      
          !
          CALL iom_put( 'masstot', zmass )
-         CALL iom_put( 'temptot', ztemp )
-         CALL iom_put( 'saltot' , zsal  )
-         !
+         CALL iom_put( 'temptot', ztemp / zvol )
+         CALL iom_put( 'saltot' , zsal  / zvol )
+         !
+      ENDIF     
+
+      IF( ln_teos10 ) THEN        ! ! potential temperature (TEOS-10 case)
+         IF( iom_use( 'toce_pot') .OR. iom_use( 'temptot_pot' ) .OR. iom_use( 'sst_pot' )  &
+                                  .OR. iom_use( 'ssttot' ) .OR.  iom_use( 'tosmint_pot' ) ) THEN
+            !
+            ALLOCATE( ztpot(jpi,jpj,jpk) )
+            ztpot(:,:,jpk) = 0._wp
+            DO jk = 1, jpkm1
+               ztpot(:,:,jk) = eos_pt_from_ct( ts(:,:,jk,jp_tem,Kmm), ts(:,:,jk,jp_sal,Kmm) )
+            END DO
+            !
+            CALL iom_put( 'toce_pot', ztpot(:,:,:) )  ! potential temperature (TEOS-10 case)
+            CALL iom_put( 'sst_pot' , ztpot(:,:,1) )  ! surface temperature
+            !
+            IF( iom_use( 'temptot_pot' ) ) THEN   ! Output potential temperature in case we use TEOS-10
+               z2d(:,:) = 0._wp
+               DO jk = 1, jpkm1
+                 z2d(:,:) = z2d(:,:) + e1e2t(:,:) * e3t(:,:,jk,Kmm) * ztpot(:,:,jk)
+               END DO
+               ztemp = glob_sum( 'diaar5', z2d(:,:)  ) 
+               CALL iom_put( 'temptot_pot', ztemp / zvol )
+             ENDIF
+             !
+             IF( iom_use( 'ssttot' ) ) THEN   ! Output potential temperature in case we use TEOS-10
+               zsst = glob_sum( 'diaar5',  e1e2t(:,:) * ztpot(:,:,1)  ) 
+               CALL iom_put( 'ssttot', zsst / area_tot )
+             ENDIF
+             ! Vertical integral of temperature
+             IF( iom_use( 'tosmint_pot') ) THEN
+               z2d(:,:) = 0._wp
+               DO_3D( 1, 1, 1, 1, 1, jpkm1 )
+                  z2d(ji,jj) = z2d(ji,jj) + rho0 * e3t(ji,jj,jk,Kmm) *  ztpot(ji,jj,jk)
+               END_3D
+               CALL iom_put( 'tosmint_pot', z2d ) 
+            ENDIF
+            DEALLOCATE( ztpot )
+        ENDIF
+      ELSE       
+         IF( iom_use('ssttot') ) THEN   ! Output sst in case we use EOS-80
+            zsst  = glob_sum( 'diaar5', e1e2t(:,:) * ts(:,:,1,jp_tem,Kmm) )
+            CALL iom_put('ssttot', zsst / area_tot )
+         ENDIF
       ENDIF
 
       IF( iom_use( 'tnpeo' )) THEN    
-      ! Work done against stratification by vertical mixing
-      ! Exclude points where rn2 is negative as convection kicks in here and
-      ! work is not being done against stratification
+        ! Work done against stratification by vertical mixing
+        ! Exclude points where rn2 is negative as convection kicks in here and
+        ! work is not being done against stratification
          ALLOCATE( zpe(jpi,jpj) )
          zpe(:,:) = 0._wp
          IF( ln_zdfddm ) THEN
-            DO jk = 2, jpk
-               DO jj = 1, jpj
-                  DO ji = 1, jpi
-                     IF( rn2(ji,jj,jk) > 0._wp ) THEN
-                        zrw =   ( gdepw_n(ji,jj,jk  ) - gdept_n(ji,jj,jk) )   &
-                           &  / ( gdept_n(ji,jj,jk-1) - gdept_n(ji,jj,jk) )
-!!gm  this can be reduced to :  (depw-dept) / e3w   (NB idem dans bn2 !)
-!                        zrw =   ( gdept_n(ji,jj,jk) - gdepw_n(ji,jj,jk) ) / e3w_n(ji,jj,jk)
-!!gm end
-                        !
-                        zaw = rab_n(ji,jj,jk,jp_tem) * (1. - zrw) + rab_n(ji,jj,jk-1,jp_tem)* zrw
-                        zbw = rab_n(ji,jj,jk,jp_sal) * (1. - zrw) + rab_n(ji,jj,jk-1,jp_sal)* zrw
-                        !
-                        zpe(ji, jj) = zpe(ji, jj)            &
-                           &        -  grav * (  avt(ji,jj,jk) * zaw * (tsn(ji,jj,jk-1,jp_tem) - tsn(ji,jj,jk,jp_tem) )  &
-                           &                   - avs(ji,jj,jk) * zbw * (tsn(ji,jj,jk-1,jp_sal) - tsn(ji,jj,jk,jp_sal) ) )
-                     ENDIF
-                  END DO
-               END DO
-             END DO
+            DO_3D( 1, 1, 1, 1, 2, jpk )
+               IF( rn2(ji,jj,jk) > 0._wp ) THEN
+                  zrw = ( gdept(ji,jj,jk,Kmm) - gdepw(ji,jj,jk,Kmm) ) / e3w(ji,jj,jk,Kmm)
+                  !
+                  zaw = rab_n(ji,jj,jk,jp_tem) * (1. - zrw) + rab_n(ji,jj,jk-1,jp_tem)* zrw
+                  zbw = rab_n(ji,jj,jk,jp_sal) * (1. - zrw) + rab_n(ji,jj,jk-1,jp_sal)* zrw
+                  !
+                  zpe(ji, jj) = zpe(ji,jj)   &
+                     &        -  grav * (  avt(ji,jj,jk) * zaw * (ts(ji,jj,jk-1,jp_tem,Kmm) - ts(ji,jj,jk,jp_tem,Kmm) )  &
+                     &                   - avs(ji,jj,jk) * zbw * (ts(ji,jj,jk-1,jp_sal,Kmm) - ts(ji,jj,jk,jp_sal,Kmm) ) )
+               ENDIF
+            END_3D
           ELSE
-            DO jk = 1, jpk
-               DO ji = 1, jpi
-                  DO jj = 1, jpj
-                     zpe(ji,jj) = zpe(ji,jj) + avt(ji, jj, jk) * MIN(0._wp,rn2(ji, jj, jk)) * rau0 * e3w_n(ji, jj, jk)
-                  END DO
-               END DO
-            END DO
+            DO_3D( 1, 1, 1, 1, 1, jpk )
+               zpe(ji,jj) = zpe(ji,jj) + avt(ji,jj,jk) * MIN(0._wp,rn2(ji,jj,jk)) * rho0 * e3w(ji,jj,jk,Kmm)
+            END_3D
          ENDIF
-!!gm useless lbc_lnk since the computation above is performed over 1:jpi & 1:jpj
-!!gm           CALL lbc_lnk( 'diaar5', zpe, 'T', 1._wp)         
           CALL iom_put( 'tnpeo', zpe )
           DEALLOCATE( zpe )
@@ -251,6 +298,5 @@
 
       IF( l_ar5 ) THEN
-        DEALLOCATE( zarea_ssh , zbotpres )
-        DEALLOCATE( zrhd      , zrhop    )
+        DEALLOCATE( zarea_ssh , zbotpres, z2d )
         DEALLOCATE( ztsn                 )
       ENDIF
@@ -261,5 +307,5 @@
 
 
-   SUBROUTINE dia_ar5_hst( ktra, cptr, pua, pva ) 
+   SUBROUTINE dia_ar5_hst( ktra, cptr, puflx, pvflx ) 
       !!----------------------------------------------------------------------
       !!                    ***  ROUTINE dia_ar5_htr ***
@@ -270,6 +316,6 @@
       INTEGER                         , INTENT(in )  :: ktra  ! tracer index
       CHARACTER(len=3)                , INTENT(in)   :: cptr  ! transport type  'adv'/'ldf'
-      REAL(wp), DIMENSION(jpi,jpj,jpk), INTENT(in)   :: pua   ! 3D input array of advection/diffusion
-      REAL(wp), DIMENSION(jpi,jpj,jpk), INTENT(in)   :: pva   ! 3D input array of advection/diffusion
+      REAL(wp), DIMENSION(jpi,jpj,jpk), INTENT(in)   :: puflx  ! u-flux of advection/diffusion
+      REAL(wp), DIMENSION(jpi,jpj,jpk), INTENT(in)   :: pvflx  ! v-flux of advection/diffusion
       !
       INTEGER    ::  ji, jj, jk
@@ -277,38 +323,30 @@
 
     
-      z2d(:,:) = pua(:,:,1) 
-      DO jk = 1, jpkm1
-         DO jj = 2, jpjm1
-            DO ji = fs_2, fs_jpim1   ! vector opt.
-               z2d(ji,jj) = z2d(ji,jj) + pua(ji,jj,jk) 
-            END DO
-         END DO
-       END DO
-       CALL lbc_lnk( 'diaar5', z2d, 'U', -1. )
+      z2d(:,:) = puflx(:,:,1) 
+      DO_3D( 0, 0, 0, 0, 1, jpkm1 )
+         z2d(ji,jj) = z2d(ji,jj) + puflx(ji,jj,jk) 
+      END_3D
+       CALL lbc_lnk( 'diaar5', z2d, 'U', -1.0_wp )
        IF( cptr == 'adv' ) THEN
-          IF( ktra == jp_tem ) CALL iom_put( "uadv_heattr" , rau0_rcp * z2d )  ! advective heat transport in i-direction
-          IF( ktra == jp_sal ) CALL iom_put( "uadv_salttr" , rau0     * z2d )  ! advective salt transport in i-direction
+          IF( ktra == jp_tem ) CALL iom_put( 'uadv_heattr' , rho0_rcp * z2d )  ! advective heat transport in i-direction
+          IF( ktra == jp_sal ) CALL iom_put( 'uadv_salttr' , rho0     * z2d )  ! advective salt transport in i-direction
        ENDIF
        IF( cptr == 'ldf' ) THEN
-          IF( ktra == jp_tem ) CALL iom_put( "udiff_heattr" , rau0_rcp * z2d ) ! diffusive heat transport in i-direction
-          IF( ktra == jp_sal ) CALL iom_put( "udiff_salttr" , rau0     * z2d ) ! diffusive salt transport in i-direction
+          IF( ktra == jp_tem ) CALL iom_put( 'udiff_heattr' , rho0_rcp * z2d ) ! diffusive heat transport in i-direction
+          IF( ktra == jp_sal ) CALL iom_put( 'udiff_salttr' , rho0     * z2d ) ! diffusive salt transport in i-direction
        ENDIF
        !
-       z2d(:,:) = pva(:,:,1) 
-       DO jk = 1, jpkm1
-          DO jj = 2, jpjm1
-             DO ji = fs_2, fs_jpim1   ! vector opt.
-                z2d(ji,jj) = z2d(ji,jj) + pva(ji,jj,jk) 
-             END DO
-          END DO
-       END DO
-       CALL lbc_lnk( 'diaar5', z2d, 'V', -1. )
+       z2d(:,:) = pvflx(:,:,1) 
+       DO_3D( 0, 0, 0, 0, 1, jpkm1 )
+          z2d(ji,jj) = z2d(ji,jj) + pvflx(ji,jj,jk) 
+       END_3D
+       CALL lbc_lnk( 'diaar5', z2d, 'V', -1.0_wp )
        IF( cptr == 'adv' ) THEN
-          IF( ktra == jp_tem ) CALL iom_put( "vadv_heattr" , rau0_rcp * z2d )  ! advective heat transport in j-direction
-          IF( ktra == jp_sal ) CALL iom_put( "vadv_salttr" , rau0     * z2d )  ! advective salt transport in j-direction
+          IF( ktra == jp_tem ) CALL iom_put( 'vadv_heattr' , rho0_rcp * z2d )  ! advective heat transport in j-direction
+          IF( ktra == jp_sal ) CALL iom_put( 'vadv_salttr' , rho0     * z2d )  ! advective salt transport in j-direction
        ENDIF
        IF( cptr == 'ldf' ) THEN
-          IF( ktra == jp_tem ) CALL iom_put( "vdiff_heattr" , rau0_rcp * z2d ) ! diffusive heat transport in j-direction
-          IF( ktra == jp_sal ) CALL iom_put( "vdiff_salttr" , rau0     * z2d ) ! diffusive salt transport in j-direction
+          IF( ktra == jp_tem ) CALL iom_put( 'vdiff_heattr' , rho0_rcp * z2d ) ! diffusive heat transport in j-direction
+          IF( ktra == jp_sal ) CALL iom_put( 'vdiff_salttr' , rho0     * z2d ) ! diffusive salt transport in j-direction
        ENDIF
           
@@ -323,8 +361,9 @@
       !!----------------------------------------------------------------------
       INTEGER  ::   inum
-      INTEGER  ::   ik
+      INTEGER  ::   ik, idep
       INTEGER  ::   ji, jj, jk  ! dummy loop indices
       REAL(wp) ::   zztmp  
       REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:) ::   zsaldta   ! Jan/Dec levitus salinity
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:)     ::   zvol0     
       !
       !!----------------------------------------------------------------------
@@ -333,5 +372,6 @@
       IF(   iom_use( 'voltot'  ) .OR. iom_use( 'sshtot'    )  .OR. iom_use( 'sshdyn' )  .OR.  & 
          &  iom_use( 'masstot' ) .OR. iom_use( 'temptot'   )  .OR. iom_use( 'saltot' ) .OR.  &    
-         &  iom_use( 'botpres' ) .OR. iom_use( 'sshthster' )  .OR. iom_use( 'sshsteric' )  ) L_ar5 = .TRUE.
+         &  iom_use( 'botpres' ) .OR. iom_use( 'sshthster' )  .OR. iom_use( 'sshsteric' ) .OR. &
+         &  iom_use( 'rhop' )  ) L_ar5 = .TRUE.
   
       IF( l_ar5 ) THEN
@@ -340,21 +380,22 @@
          IF( dia_ar5_alloc() /= 0 )   CALL ctl_stop( 'STOP', 'dia_ar5_init : unable to allocate arrays' )
 
-         area(:,:) = e1e2t(:,:) * tmask_i(:,:)
-
-         area_tot = SUM( area(:,:) )   ;   CALL mpp_sum( 'diaar5', area_tot )
-
-         vol0        = 0._wp
+         area_tot  = glob_sum( 'diaar5', e1e2t(:,:) )
+
+         ALLOCATE( zvol0(jpi,jpj) )
+         zvol0 (:,:) = 0._wp
          thick0(:,:) = 0._wp
-         DO jk = 1, jpkm1
-            vol0        = vol0        + SUM( area (:,:) * tmask(:,:,jk) * e3t_0(:,:,jk) )
-            thick0(:,:) = thick0(:,:) +    tmask_i(:,:) * tmask(:,:,jk) * e3t_0(:,:,jk)
-         END DO
-         CALL mpp_sum( 'diaar5', vol0 )
+         DO_3D( 1, 1, 1, 1, 1, jpkm1 )
+            idep = tmask(ji,jj,jk) * e3t_0(ji,jj,jk)
+            zvol0 (ji,jj) = zvol0 (ji,jj) +  idep * e1e2t(ji,jj)
+            thick0(ji,jj) = thick0(ji,jj) +  idep    
+         END_3D
+         vol0 = glob_sum( 'diaar5', zvol0 )
+         DEALLOCATE( zvol0 )
 
          IF( iom_use( 'sshthster' ) ) THEN
-            ALLOCATE( zsaldta(jpi,jpj,jpj,jpts) )
+            ALLOCATE( zsaldta(jpi,jpj,jpk,jpts) )
             CALL iom_open ( 'sali_ref_clim_monthly', inum )
-            CALL iom_get  ( inum, jpdom_data, 'vosaline' , zsaldta(:,:,:,1), 1  )
-            CALL iom_get  ( inum, jpdom_data, 'vosaline' , zsaldta(:,:,:,2), 12 )
+            CALL iom_get  ( inum, jpdom_global, 'vosaline' , zsaldta(:,:,:,1), 1  )
+            CALL iom_get  ( inum, jpdom_global, 'vosaline' , zsaldta(:,:,:,2), 12 )
             CALL iom_close( inum )
 
@@ -362,13 +403,11 @@
             sn0(:,:,:) = sn0(:,:,:) * tmask(:,:,:)
             IF( ln_zps ) THEN               ! z-coord. partial steps
-               DO jj = 1, jpj               ! interpolation of salinity at the last ocean level (i.e. the partial step)
-                  DO ji = 1, jpi
-                     ik = mbkt(ji,jj)
-                     IF( ik > 1 ) THEN
-                        zztmp = ( gdept_1d(ik) - gdept_0(ji,jj,ik) ) / ( gdept_1d(ik) - gdept_1d(ik-1) )
-                        sn0(ji,jj,ik) = ( 1._wp - zztmp ) * sn0(ji,jj,ik) + zztmp * sn0(ji,jj,ik-1)
-                     ENDIF
-                  END DO
-               END DO
+               DO_2D( 1, 1, 1, 1 )
+                  ik = mbkt(ji,jj)
+                  IF( ik > 1 ) THEN
+                     zztmp = ( gdept_1d(ik) - gdept_0(ji,jj,ik) ) / ( gdept_1d(ik) - gdept_1d(ik-1) )
+                     sn0(ji,jj,ik) = ( 1._wp - zztmp ) * sn0(ji,jj,ik) + zztmp * sn0(ji,jj,ik-1)
+                  ENDIF
+               END_2D
             ENDIF
             !
