Index: NEMO/branches/2018/dev_r10164_HPC09_ESIWACE_PREP_MERGE/src/OCE/LBC/mpp_loc_generic.h90
===================================================================
--- NEMO/branches/2018/dev_r10164_HPC09_ESIWACE_PREP_MERGE/src/OCE/LBC/mpp_loc_generic.h90	(revision 10314)
+++ NEMO/branches/2018/dev_r10164_HPC09_ESIWACE_PREP_MERGE/src/OCE/LBC/mpp_loc_generic.h90	(revision 10357)
@@ -1,9 +1,9 @@
                           !==  IN: ptab is an array  ==!
-#      define ARRAY_TYPE(i,j,k)    REAL(wp)          , INTENT(in   ) ::   ARRAY_IN(i,j,k)
-#      define MASK_TYPE(i,j,k)     REAL(wp)          , INTENT(in   ) ::   MASK_IN(i,j,k)
+#      define ARRAY_TYPE(i,j,k)    REAL(wp)        , INTENT(in   ) ::   ARRAY_IN(i,j,k)
+#      define MASK_TYPE(i,j,k)     REAL(wp)        , INTENT(in   ) ::   MASK_IN(i,j,k)
 #   if defined DIM_2d
 #      define ARRAY_IN(i,j,k)   ptab(i,j)
 #      define MASK_IN(i,j,k)    pmask(i,j)
-#      define INDEX_TYPE(k)        INTEGER           , INTENT(  out) ::   kindex(2)
+#      define INDEX_TYPE(k)        INTEGER         , INTENT(  out) ::   kindex(2)
 #      define K_SIZE(ptab)      1
 #   endif
@@ -11,12 +11,14 @@
 #      define ARRAY_IN(i,j,k)   ptab(i,j,k)
 #      define MASK_IN(i,j,k)    pmask(i,j,k)
-#      define INDEX_TYPE(k)        INTEGER           , INTENT(  out) ::   kindex(3)
+#      define INDEX_TYPE(k)        INTEGER         , INTENT(  out) ::   kindex(3)
 #      define K_SIZE(ptab)      SIZE(ptab,3)
 #   endif
 #   if defined OPERATION_MAXLOC
 #      define MPI_OPERATION mpi_maxloc
+#      define LOC_OPERATION MAXLOC
 #   endif
 #   if defined OPERATION_MINLOC
 #      define MPI_OPERATION mpi_minloc
+#      define LOC_OPERATION MINLOC
 #   endif
 
@@ -24,6 +26,6 @@
       !!----------------------------------------------------------------------
       CHARACTER(len=*), INTENT(in   ) ::   cdname  ! name of the calling subroutine
-      ARRAY_TYPE(:,:,:)                                ! array on which loctrans operation is applied
-      MASK_TYPE(:,:,:)                                 ! local mask
+      ARRAY_TYPE(:,:,:)                            ! array on which loctrans operation is applied
+      MASK_TYPE(:,:,:)                             ! local mask
       REAL(wp)        , INTENT(  out) ::   pmin    ! Global minimum of ptab
       INDEX_TYPE(:)                                ! index of minimum in global frame
@@ -31,4 +33,5 @@
       !
       INTEGER  ::   ierror, ii, idim
+      INTEGER  ::   index0
       REAL(wp) ::   zmin     ! local minimum
       INTEGER , DIMENSION(:), ALLOCATABLE  ::   ilocs
@@ -41,18 +44,26 @@
       ALLOCATE ( ilocs(idim) )
       !
-      zmin  = MINVAL( ARRAY_IN(:,:,:) , mask= MASK_IN(:,:,:) == 1._wp )
-      ilocs = MINLOC( ARRAY_IN(:,:,:) , mask= MASK_IN(:,:,:) == 1._wp )
+      ilocs = LOC_OPERATION( ARRAY_IN(:,:,:) , mask= MASK_IN(:,:,:) == 1._wp )
+      zmin  = ARRAY_IN(ilocs(1),ilocs(2),ilocs(3))
       !
-      DO ii = 1, idim
-        IF (ii == 1) kindex(1) = ilocs(1) + nimpp - 1
-        IF (ii == 2) kindex(2) = ilocs(2) + njmpp - 1
-        IF (ii == 3) kindex(3) = ilocs(3)
-      ENDDO
+      kindex(1) = ilocs(1) + nimpp - 1
+#  if defined DIM_2d || defined DIM_3d    /* avoid warning when kindex has 1 element */
+      kindex(2) = ilocs(2) + njmpp - 1
+#  endif
+#  if defined DIM_3d                      /* avoid warning when kindex has 2 elements */
+      kindex(3) = ilocs(3)
+#  endif
       ! 
       DEALLOCATE (ilocs)
       !
+      index0 = kindex(1)-1   ! 1d index starting at 0
+#  if defined DIM_2d || defined DIM_3d   /* avoid warning when kindex has 1 element */
+      index0 = index0 + jpiglo * (kindex(2)-1)
+#  endif
+#  if defined DIM_3d                     /* avoid warning when kindex has 2 elements */
+      index0 = index0 + jpiglo * jpjglo * (kindex(3)-1)
+#  endif
       zain(1,:) = zmin
-      zain(2,:) = kindex(1) + 10000.* kindex(2)
-      IF ( idim == 3) zain(2,:) = zain(2,:) + 100000000.*kindex(3)
+      zain(2,:) = REAL(index0, wp)
       !
       IF( ln_timing ) CALL tic_tac(.TRUE., ld_global = .TRUE.)
@@ -60,13 +71,16 @@
       IF( ln_timing ) CALL tic_tac(.FALSE., ld_global = .TRUE.)
       !
-      pmin = zaout(1,1)
-      IF ( idim == 3) THEN
-        kindex(3)   = INT( zaout(2,1) / 100000000. )
-        kindex(2)   = INT( zaout(2,1) - kindex(3) * 100000000. ) / 10000
-        kindex(1)   = INT( zaout(2,1) - kindex(3) * 100000000. -kindex(2) * 10000. )
-      ELSE
-        kindex(2)   = INT( zaout(2,1) / 10000 )
-        kindex(1)   = INT( zaout(2,1) -kindex(2) * 10000. )
-      ENDIF
+      pmin      = zaout(1,1)
+      index0    = NINT( zaout(2,1) )
+#  if defined DIM_3d                     /* avoid warning when kindex has 2 elements */
+      kindex(3) = index0 / (jpiglo*jpjglo)
+      index0    = index0 - kindex(3) * (jpiglo*jpjglo)
+#  endif
+#  if defined DIM_2d || defined DIM_3d   /* avoid warning when kindex has 1 element */
+      kindex(2) = index0 / jpiglo
+      index0 = index0 - kindex(2) * jpiglo
+#  endif
+      kindex(1) = index0
+      kindex(:) = kindex(:) + 1   ! start indices at 1
 #else
       kindex = 0 ; pmin = 0.
@@ -82,3 +96,4 @@
 #undef K_SIZE
 #undef MPI_OPERATION
+#undef LOC_OPERATION
 #undef INDEX_TYPE
