Index: NEMO/branches/2018/dev_r10164_HPC09_ESIWACE_PREP_MERGE/src/OCE/stpctl.F90
===================================================================
--- NEMO/branches/2018/dev_r10164_HPC09_ESIWACE_PREP_MERGE/src/OCE/stpctl.F90	(revision 10314)
+++ NEMO/branches/2018/dev_r10164_HPC09_ESIWACE_PREP_MERGE/src/OCE/stpctl.F90	(revision 10358)
@@ -33,4 +33,5 @@
 
    INTEGER  ::   idrun, idtime, idssh, idu, ids1, ids2, istatus
+   LOGICAL  ::   lsomeoce
    !!----------------------------------------------------------------------
    !! NEMO/OCE 4.0 , NEMO Consortium (2018)
@@ -60,12 +61,8 @@
       INTEGER, INTENT(inout) ::   kindic   ! error indicator
       !!
-      INTEGER  ::   ji, jj, jk             ! dummy loop indices
-      INTEGER  ::   ih(2)                  ! local integers
-      INTEGER  ::   iu(3)                  !   -       -
-      INTEGER  ::   is1(3)                 !   -       -
-      INTEGER  ::   is2(3)                 !   -       -
-      REAL(wp) ::   zzz                    ! local real 
-      INTEGER , DIMENSION(3) ::   ilocu, ilocs1, ilocs2
-      INTEGER , DIMENSION(2) ::   iloch
+      INTEGER                ::   ji, jj, jk          ! dummy loop indices
+      INTEGER, DIMENSION(2)  ::   ih                  ! min/max loc indices
+      INTEGER, DIMENSION(3)  ::   iu, is1, is2        ! min/max loc indices
+      REAL(wp)               ::   zzz                 ! local real 
       REAL(wp), DIMENSION(5) ::   zmax
       CHARACTER(len=20) :: clname
@@ -77,9 +74,8 @@
          WRITE(numout,*) '~~~~~~~'
          !                                ! open time.step file
-         CALL ctl_opn( numstp, 'time.step', 'REPLACE', 'FORMATTED', 'SEQUENTIAL', -1, numout, lwp, narea )
+         IF( lwm ) CALL ctl_opn( numstp, 'time.step', 'REPLACE', 'FORMATTED', 'SEQUENTIAL', -1, numout, lwp, narea )
          !                                ! open run.stat file
-         CALL ctl_opn( numrun, 'run.stat', 'REPLACE', 'FORMATTED', 'SEQUENTIAL', -1, numout, lwp, narea )
-
-         IF( lwm ) THEN
+         IF( ln_ctl .AND. lwm ) THEN
+            CALL ctl_opn( numrun, 'run.stat', 'REPLACE', 'FORMATTED', 'SEQUENTIAL', -1, numout, lwp, narea )
             clname = 'run.stat.nc'
             IF( .NOT. Agrif_Root() )   clname = TRIM(Agrif_CFixed())//"_"//TRIM(clname)
@@ -92,8 +88,8 @@
             istatus = NF90_ENDDEF(idrun)
          ENDIF
-         
       ENDIF
+      IF( kt == nit000 )   lsomeoce = COUNT( ssmask(:,:) == 1._wp ) > 0
       !
-      IF(lwp) THEN                        !==  current time step  ==!   ("time.step" file)
+      IF(lwm) THEN                        !==  current time step  ==!   ("time.step" file)
          WRITE ( numstp, '(1x, i8)' )   kt
          REWIND( numstp )
@@ -111,61 +107,11 @@
       zmax(5) = REAL( nstop , wp )                                            ! stop indicator
       !
-      IF( lk_mpp ) THEN
-         CALL mpp_max( "stpctl", zmax )    ! max over the global domain
-         !
+      IF( lk_mpp .AND. ln_ctl ) THEN
+         CALL mpp_max( "stpctl", zmax )          ! max over the global domain
          nstop = NINT( zmax(5) )                 ! nstop indicator sheared among all local domains
       ENDIF
-      !
-      IF( MOD( kt, nwrite ) == 1 .AND. lwp ) THEN
-         WRITE(numout,*) ' ==>> time-step= ', kt, ' |ssh| max: ',   zmax(1), ' |U| max: ', zmax(2),   &
-            &                                     ' S min: '    , - zmax(3), ' S max: ', zmax(4)
-      ENDIF
-      !
-      IF (  zmax(1) >   15._wp .OR.   &                    ! too large sea surface height ( > 15 m )
-         &  zmax(2) >   10._wp .OR.   &                    ! too large velocity ( > 10 m/s)
-         &  zmax(3) >=   0._wp .OR.   &                    ! negative or zero sea surface salinity
-         &  zmax(4) >= 100._wp .OR.   &                    ! too large sea surface salinity ( > 100 )
-         &  zmax(4) <    0._wp .OR.   &                    ! too large sea surface salinity (keep this line for sea-ice)
-         &  ISNAN( zmax(1) + zmax(2) + zmax(3) )  ) THEN   ! NaN encounter in the tests
-         IF( lk_mpp ) THEN
-            CALL mpp_maxloc( 'stpctl', ABS(sshn)        , ssmask(:,:)  , zzz, ih  )
-            CALL mpp_maxloc( 'stpctl', ABS(un)          , umask (:,:,:), zzz, iu  )
-            CALL mpp_minloc( 'stpctl', tsn(:,:,:,jp_sal), tmask (:,:,:), zzz, is1 )
-            CALL mpp_maxloc( 'stpctl', tsn(:,:,:,jp_sal), tmask (:,:,:), zzz, is2 )
-         ELSE
-            iloch  = MINLOC( ABS( sshn(:,:)   )                               )
-            ilocu  = MAXLOC( ABS( un  (:,:,:) )                               )
-            ilocs1 = MINLOC( tsn(:,:,:,jp_sal) , mask = tmask(:,:,:) == 1._wp )
-            ilocs2 = MAXLOC( tsn(:,:,:,jp_sal) , mask = tmask(:,:,:) == 1._wp )
-            ih(1)  = iloch (1) + nimpp - 1   ;   ih(2)  = iloch (2) + njmpp - 1
-            iu(1)  = ilocu (1) + nimpp - 1   ;   iu(2)  = ilocu (2) + njmpp - 1   ;   iu(3)  = ilocu (3)
-            is1(1) = ilocs1(1) + nimpp - 1   ;   is1(2) = ilocs1(2) + njmpp - 1   ;   is1(3) = ilocs1(3)
-            is2(1) = ilocs2(1) + nimpp - 1   ;   is2(2) = ilocs2(2) + njmpp - 1   ;   is2(3) = ilocs2(3)
-         ENDIF
-         IF(lwp) THEN
-            WRITE(numout,cform_err)
-            WRITE(numout,*) ' stp_ctl: |ssh| > 10 m  or  |U| > 10 m/s  or  S <= 0  or  S >= 100  or  NaN encounter in the tests'
-            WRITE(numout,*) ' ======= '
-            WRITE(numout,9100) kt,   zmax(1), ih(1) , ih(2)
-            WRITE(numout,9200) kt,   zmax(2), iu(1) , iu(2) , iu(3)
-            WRITE(numout,9300) kt, - zmax(3), is1(1), is1(2), is1(3)
-            WRITE(numout,9400) kt,   zmax(4), is2(1), is2(2), is2(3)
-            WRITE(numout,*)
-            WRITE(numout,*) '          output of last computed fields in output.abort.nc file'
-         ENDIF
-         kindic = -3
-         !
-         nstop = nstop + 1                            ! increase nstop by 1 (on all local domains)
-         CALL dia_wri_state( 'output.abort', kt )     ! create an output.abort file
-         !
-      ENDIF
-9100  FORMAT (' kt=',i8,'   |ssh| max: ',1pg11.4,', at  i j  : ',2i5)
-9200  FORMAT (' kt=',i8,'   |U|   max: ',1pg11.4,', at  i j k: ',3i5)
-9300  FORMAT (' kt=',i8,'   S     min: ',1pg11.4,', at  i j k: ',3i5)
-9400  FORMAT (' kt=',i8,'   S     max: ',1pg11.4,', at  i j k: ',3i5)
-      !
-      !                                            !==  run statistics  ==!   ("run.stat" file)
-      IF(lwp) WRITE(numrun,9500) kt, zmax(1), zmax(2), -zmax(3), zmax(4)
-      IF( lwm ) THEN
+      !                                   !==  run statistics  ==!   ("run.stat" files)
+      IF( ln_ctl .AND. lwm ) THEN
+         WRITE(numrun,9500) kt, zmax(1), zmax(2), -zmax(3), zmax(4)
          istatus = NF90_PUT_VAR( idrun, idssh, (/ zmax(1)/), (/kt/), (/1/) )
          istatus = NF90_PUT_VAR( idrun,   idu, (/ zmax(2)/), (/kt/), (/1/) )
@@ -175,5 +121,53 @@
          IF( kt == nitend         ) istatus = NF90_CLOSE(idrun)
       END IF
+      !                                   !==  error handling  ==!
+      IF( ( ln_ctl .OR. lsomeoce ) .AND. (   &             ! have use mpp_max (because ln_ctl=.T.) or contains some ocean points
+         &  zmax(1) >   15._wp .OR.   &                    ! too large sea surface height ( > 15 m )
+         &  zmax(2) >   10._wp .OR.   &                    ! too large velocity ( > 10 m/s)
+         &  zmax(3) >=   0._wp .OR.   &                    ! negative or zero sea surface salinity
+         &  zmax(4) >= 100._wp .OR.   &                    ! too large sea surface salinity ( > 100 )
+         &  zmax(4) <    0._wp .OR.   &                    ! too large sea surface salinity (keep this line for sea-ice)
+         &  ISNAN( zmax(1) + zmax(2) + zmax(3) ) ) ) THEN   ! NaN encounter in the tests
+         IF( lk_mpp .AND. ln_ctl ) THEN
+            CALL mpp_maxloc( 'stpctl', ABS(sshn)        , ssmask(:,:)  , zzz, ih  )
+            CALL mpp_maxloc( 'stpctl', ABS(un)          , umask (:,:,:), zzz, iu  )
+            CALL mpp_minloc( 'stpctl', tsn(:,:,:,jp_sal), tmask (:,:,:), zzz, is1 )
+            CALL mpp_maxloc( 'stpctl', tsn(:,:,:,jp_sal), tmask (:,:,:), zzz, is2 )
+         ELSE
+            ih(:)  = MAXLOC( ABS( sshn(:,:)   )                              ) + (/ nimpp - 1, njmpp - 1    /)
+            iu(:)  = MAXLOC( ABS( un  (:,:,:) )                              ) + (/ nimpp - 1, njmpp - 1, 0 /)
+            is1(:) = MINLOC( tsn(:,:,:,jp_sal), mask = tmask(:,:,:) == 1._wp ) + (/ nimpp - 1, njmpp - 1, 0 /)
+            is2(:) = MAXLOC( tsn(:,:,:,jp_sal), mask = tmask(:,:,:) == 1._wp ) + (/ nimpp - 1, njmpp - 1, 0 /)
+         ENDIF
+         IF( numout == 6 )   &   ! force to open ocean.output file
+            CALL ctl_opn( numout, 'ocean.output', 'APPEND', 'FORMATTED', 'SEQUENTIAL', -1, 6, .FALSE. )
+
+         WRITE(numout,cform_err)
+         WRITE(numout,*) ' stp_ctl: |ssh| > 10 m  or  |U| > 10 m/s  or  S <= 0  or  S >= 100  or  NaN encounter in the tests'
+         WRITE(numout,*) ' ======= '
+         IF( lk_mpp .AND. .NOT. ln_ctl ) WRITE(numout,*) 'E R R O R message from sub-domain: ', narea
+         WRITE(numout,9100) kt,   zmax(1), ih(1) , ih(2)
+         WRITE(numout,9200) kt,   zmax(2), iu(1) , iu(2) , iu(3)
+         WRITE(numout,9300) kt, - zmax(3), is1(1), is1(2), is1(3)
+         WRITE(numout,9400) kt,   zmax(4), is2(1), is2(2), is2(3)
+         WRITE(numout,*)
+         WRITE(numout,*) '          output of last computed fields in output.abort.nc file'
+         
+         CALL dia_wri_state( 'output.abort' )     ! create an output.abort file
+         
+         IF( ln_ctl ) THEN
+            kindic = -3
+            nstop = nstop + 1                            ! increase nstop by 1 (on all local domains)
+         ELSE
+            CALL ctl_stop()
+            CALL mppstop(ld_force_abort = .true.)
+         ENDIF
+         !
+      ENDIF
       !
+9100  FORMAT (' kt=',i8,'   |ssh| max: ',1pg11.4,', at  i j  : ',2i5)
+9200  FORMAT (' kt=',i8,'   |U|   max: ',1pg11.4,', at  i j k: ',3i5)
+9300  FORMAT (' kt=',i8,'   S     min: ',1pg11.4,', at  i j k: ',3i5)
+9400  FORMAT (' kt=',i8,'   S     max: ',1pg11.4,', at  i j k: ',3i5)
 9500  FORMAT(' it :', i8, '    |ssh|_max: ', D23.16, ' |U|_max: ', D23.16,' S_min: ', D23.16,' S_max: ', D23.16)
       !
