Index: branches/2017/dev_merge_2017/NEMOGCM/NEMO/SAS_SRC/nemogcm.F90
===================================================================
--- branches/2017/dev_merge_2017/NEMOGCM/NEMO/SAS_SRC/nemogcm.F90	(revision 9200)
+++ branches/2017/dev_merge_2017/NEMOGCM/NEMO/SAS_SRC/nemogcm.F90	(revision 9213)
@@ -18,4 +18,5 @@
    !!   nemo_partition: calculate MPP domain decomposition
    !!   factorise     : calculate the factors of the no. of MPI processes
+   !!   nemo_nfdcom   : Setup for north fold exchanges with explicit point-to-point messaging
    !!----------------------------------------------------------------------
    USE step_oce       ! module used in the ocean time stepping module
@@ -37,5 +38,5 @@
    USE lib_mpp        ! distributed memory computing
    USE mppini         ! shared/distributed memory setting (mpp_init routine)
-   USE lbcnfd   , ONLY: isendto, nsndto, nfsloop, nfeloop ! Setup of north fold exchanges
+   USE lbcnfd  , ONLY : isendto, nsndto, nfsloop, nfeloop   ! Setup of north fold exchanges
    USE lib_fortran    ! Fortran utilities (allows no signed zero when 'key_nosignedzero' defined)
 #if defined key_iomput
@@ -52,5 +53,5 @@
 
    !!----------------------------------------------------------------------
-   !! NEMO/OPA 4.0 , NEMO Consortium (2016)
+   !! NEMO/OPA 4.0 , NEMO Consortium (2018)
    !! $Id$
    !! Software governed by the CeCILL licence     (NEMOGCM/NEMO_CeCILL.txt)
@@ -76,7 +77,6 @@
       !
 #if defined key_agrif
-     CALL Agrif_Init_Grids()      ! AGRIF: set the meshes
-#endif
-      !
+      CALL Agrif_Init_Grids()      ! AGRIF: set the meshes
+#endif
       !                            !-----------------------!
       CALL nemo_init               !==  Initialisations  ==!
@@ -102,21 +102,39 @@
       !                            !-----------------------!
       istp = nit000
+      !
 #if defined key_agrif
+      !                                               !==  AGRIF time-stepping  ==!
       CALL Agrif_Regrid()
-#endif
-
-      DO WHILE ( istp <= nitend .AND. nstop == 0 )
-#if defined key_agrif
-         CALL stp                         ! AGRIF: time stepping
+      !
+      DO WHILE( istp <= nitend .AND. nstop == 0 )
+         CALL stp
+         istp = istp + 1
+      END DO
+      !
+      IF( .NOT. Agrif_Root() ) THEN
+         CALL Agrif_ParentGrid_To_ChildGrid()
+         IF( ln_timing )   CALL timing_finalize
+         CALL Agrif_ChildGrid_To_ParentGrid()
+      ENDIF
+      !
 #else
-         IF ( .NOT. ln_diurnal_only ) THEN
-            CALL stp( istp )                 ! standard time stepping
-         ELSE
-            CALL stp_diurnal( istp )        ! time step only the diurnal SST
-         ENDIF 
-#endif
-         istp = istp + 1
-         IF( lk_mpp )   CALL mpp_max( nstop )
+      !
+      IF( .NOT.ln_diurnal_only ) THEN                 !==  Standard time-stepping  ==!
+         !
+         DO WHILE( istp <= nitend .AND. nstop == 0 )
+            CALL stp        ( istp ) 
+            istp = istp + 1
          END DO
+         !
+      ELSE                                            !==  diurnal SST time-steeping only  ==!
+         !
+         DO WHILE( istp <= nitend .AND. nstop == 0 )
+            CALL stp_diurnal( istp )   ! time step only the diurnal SST 
+            istp = istp + 1
+         END DO
+         !
+      ENDIF
+      !
+#endif
       !
       IF( ln_icebergs )   CALL icb_end( nitend )
@@ -129,12 +147,8 @@
       IF( nstop /= 0 .AND. lwp ) THEN        ! error print
          WRITE(numout,cform_err)
-         WRITE(numout,*) nstop, ' error have been found'
-      ENDIF
-      !
-#if defined key_agrif
-         CALL Agrif_ParentGrid_To_ChildGrid()
-         IF( ln_timing )   CALL timing_finalize
-         CALL Agrif_ChildGrid_To_ParentGrid()
-#endif
+         WRITE(numout,*) '   ==>>>   nemo_gcm: a total of ', nstop, ' errors have been found'
+         WRITE(numout,*)
+      ENDIF
+      !
       IF( ln_timing )   CALL timing_finalize
       !
@@ -142,11 +156,9 @@
       !
 #if defined key_iomput
-      CALL xios_finalize                     ! end mpp communications with xios
-      IF( lk_oasis )   CALL cpl_finalize     ! end coupling and mpp communications with OASIS
+                                    CALL xios_finalize  ! end mpp communications with xios
+      IF( lk_oasis     )            CALL cpl_finalize   ! end coupling and mpp communications with OASIS
 #else
-      IF( lk_oasis ) THEN 
-         CALL cpl_finalize                   ! end coupling and mpp communications with OASIS
-      ELSE
-         IF( lk_mpp )   CALL mppstop         ! end mpp communications
+      IF    ( lk_oasis ) THEN   ;   CALL cpl_finalize   ! end coupling and mpp communications with OASIS
+      ELSEIF( lk_mpp   ) THEN   ;   CALL mppstop        ! end mpp communications
       ENDIF
 #endif
@@ -161,16 +173,15 @@
       !! ** Purpose :   initialization of the NEMO GCM
       !!----------------------------------------------------------------------
-      INTEGER  ::   ji            ! dummy loop indices
-      INTEGER  ::   ilocal_comm   ! local integer
-      INTEGER  ::   ios, inum     !   -      -
-      INTEGER  ::   iiarea, ijarea     ! local integers
-      INTEGER  ::   iirest, ijrest     ! local integers
+      INTEGER  ::   ji                 ! dummy loop indices
+      INTEGER  ::   ios, ilocal_comm   ! local integers
+      INTEGER  ::   iiarea, ijarea     !   -       -
+      INTEGER  ::   iirest, ijrest     !   -       -
       CHARACTER(len=120), DIMENSION(30) ::   cltxt, cltxt2, clnam
       CHARACTER(len=80)                 ::   clname
-      !
+      !!
       NAMELIST/namctl/ ln_ctl   , nn_print, nn_ictls, nn_ictle,   &
          &             nn_isplt , nn_jsplt, nn_jctls, nn_jctle,   &
          &             ln_timing, ln_diacfl
-      NAMELIST/namcfg/ ln_read_cfg, cn_domcfg, ln_write_cfg, cn_domcfg_out, ln_use_jattr, ln_closea
+      NAMELIST/namcfg/ ln_read_cfg, cn_domcfg, ln_closea, ln_write_cfg, cn_domcfg_out, ln_use_jattr
       !!----------------------------------------------------------------------
       !
@@ -193,5 +204,5 @@
    ENDIF
       !
-      REWIND( numnam_ref )              ! Namelist namctl in reference namelist : Control prints
+      REWIND( numnam_ref )              ! Namelist namctl in reference namelist
       READ  ( numnam_ref, namctl, IOSTAT = ios, ERR = 901 )
 901   IF( ios /= 0 )   CALL ctl_nam ( ios , 'namctl in reference namelist', .TRUE. )
@@ -200,8 +211,8 @@
 902   IF( ios >  0 )   CALL ctl_nam ( ios , 'namctl in configuration namelist', .TRUE. )
       !
-      REWIND( numnam_ref )              ! Namelist namcfg in reference namelist : Control prints
+      REWIND( numnam_ref )              ! Namelist namcfg in reference namelist
       READ  ( numnam_ref, namcfg, IOSTAT = ios, ERR = 903 )
 903   IF( ios /= 0 )   CALL ctl_nam ( ios , 'namcfg in reference namelist', .TRUE. )
-      REWIND( numnam_cfg )              ! Namelist namcfg in confguration namelist : Control prints & Benchmark
+      REWIND( numnam_cfg )              ! Namelist namcfg in confguration namelist
       READ  ( numnam_cfg, namcfg, IOSTAT = ios, ERR = 904 )
 904   IF( ios >  0 )   CALL ctl_nam ( ios , 'namcfg in configuration namelist', .TRUE. )   
@@ -325,8 +336,6 @@
       IF(lwp) THEN                            ! open listing units
          !
-         IF( lk_oasis ) THEN
-            CALL ctl_opn( numout,   'sas.output', 'REPLACE', 'FORMATTED', 'SEQUENTIAL', -1, 6, .FALSE., narea )
-         ELSE
-            CALL ctl_opn( numout, 'ocean.output', 'REPLACE', 'FORMATTED', 'SEQUENTIAL', -1, 6, .FALSE., narea )
+         IF( lk_oasis ) THEN   ;   CALL ctl_opn( numout,   'sas.output', 'REPLACE','FORMATTED','SEQUENTIAL', -1, 6, .FALSE., narea )
+         ELSE                  ;   CALL ctl_opn( numout, 'ocean.output', 'REPLACE','FORMATTED','SEQUENTIAL', -1, 6, .FALSE., narea )
          ENDIF
          !
@@ -335,5 +344,5 @@
          WRITE(numout,*) '                       NEMO team'
          WRITE(numout,*) '            Ocean General Circulation Model'
-         WRITE(numout,*) '                  version 3.7  (2016) '
+         WRITE(numout,*) '                NEMO version 4.0  (2017) '
          WRITE(numout,*) '             StandAlone Surface version (SAS) '
          WRITE(numout,*)
@@ -354,4 +363,5 @@
       ! Now we know the dimensions of the grid and numout has been set: we can allocate arrays
       CALL nemo_alloc()
+
       !                             !-------------------------------!
       !                             !  NEMO general initialization  !
@@ -361,20 +371,22 @@
 
       !                                      ! Domain decomposition
-      CALL mpp_init
-      !
-      IF( ln_timing    )   CALL timing_init
-      !
-      !                                     ! General initialization
-                           CALL phy_cst    ! Physical constants
-                           CALL eos_init   ! Equation of state
-                           CALL dom_init   ! Domain
-
-     IF( ln_nnogather )    CALL nemo_northcomms   ! Initialise the northfold neighbour lists (must be done after the masks are defined)
-
-      IF( ln_ctl      )    CALL prt_ctl_init   ! Print control
-                           CALL day_init   ! model calendar (using both namelist and restart infos)
-      IF( ln_rstart )       CALL rst_read_open
-
-                           CALL sbc_init   ! Forcings : surface module 
+      CALL mpp_init                             ! MPP
+      IF( ln_nnogather )   CALL nemo_nfdcom     ! northfold neighbour lists
+      !
+      !
+      !                                      ! General initialization
+      IF( ln_timing    )   CALL timing_init     ! timing
+      IF( ln_timing    )   CALL timing_start( 'nemo_init')
+
+                           CALL phy_cst         ! Physical constants
+                           CALL eos_init        ! Equation of seawater
+                           CALL dom_init        ! Domain
+      IF( ln_ctl      )    CALL prt_ctl_init    ! Print control
+      
+                           CALL day_init        ! model calendar (using both namelist and restart infos)
+      IF( ln_rstart )      CALL rst_read_open
+
+      !                                      ! external forcing 
+                           CALL sbc_init        ! Forcings : surface module 
 
       ! ==> clem: open boundaries init. is mandatory for sea-ice because ice BDY is not decoupled from  
@@ -384,6 +396,8 @@
       ! ==>
                            CALL icb_init( rdt, nit000)   ! initialise icebergs instance
-      
-      IF(lwp) WRITE(numout,*) 'Euler time step switch is ', neuler
+      !
+      IF(lwp) WRITE(numout,cform_aaa)           ! Flag AAAAAAA
+      !
+      IF( ln_timing    )   CALL timing_stop( 'nemo_init')
       !
    END SUBROUTINE nemo_init
@@ -394,5 +408,5 @@
       !!                     ***  ROUTINE nemo_ctl  ***
       !!
-      !! ** Purpose :   control print setting 
+      !! ** Purpose :   control print setting
       !!
       !! ** Method  : - print namctl information and check some consistencies
@@ -402,5 +416,5 @@
          WRITE(numout,*)
          WRITE(numout,*) 'nemo_ctl: Control prints'
-         WRITE(numout,*) '~~~~~~~ '
+         WRITE(numout,*) '~~~~~~~~'
          WRITE(numout,*) '   Namelist namctl'
          WRITE(numout,*) '      run control (for debugging)     ln_ctl     = ', ln_ctl
@@ -426,13 +440,14 @@
       IF(lwp) THEN                  ! control print
          WRITE(numout,*)
-         WRITE(numout,*) 'namcfg  : configuration initialization through namelist read'
-         WRITE(numout,*) '~~~~~~~ '
          WRITE(numout,*) '   Namelist namcfg'
-         WRITE(numout,*) '      read domain configuration files               ln_read_cfg      = ', ln_read_cfg
+         WRITE(numout,*) '      read domain configuration file                ln_read_cfg      = ', ln_read_cfg
          WRITE(numout,*) '         filename to be read                           cn_domcfg     = ', TRIM(cn_domcfg)
-         WRITE(numout,*) '      write  configuration definition files         ln_write_cfg     = ', ln_write_cfg
+         WRITE(numout,*) '         keep closed seas in the domain (if exist)     ln_closea     = ', ln_closea
+         WRITE(numout,*) '      create a configuration definition file        ln_write_cfg     = ', ln_write_cfg
          WRITE(numout,*) '         filename to be written                        cn_domcfg_out = ', TRIM(cn_domcfg_out)
          WRITE(numout,*) '      use file attribute if exists as i/p j-start   ln_use_jattr     = ', ln_use_jattr
       ENDIF
+      IF( .NOT.ln_read_cfg )   ln_closea = .false.   ! dealing possible only with a domcfg file
+      !
       !                             ! Parameter control
       !
@@ -474,7 +489,10 @@
       ENDIF
       !
-      IF( 1_wp /= SIGN(1._wp,-0._wp)  )   CALL ctl_stop( 'nemo_ctl: The intrinsec SIGN function follows ',  &
-         &                                               'f2003 standard. '                              ,  &
-         &                                               'Compile with key_nosignedzero enabled' )
+      IF( 1._wp /= SIGN(1._wp,-0._wp)  )   CALL ctl_stop( 'nemo_ctl: The intrinsec SIGN function follows f2003 standard.',  &
+         &                                                'Compile with key_nosignedzero enabled' )
+      !
+#if defined key_agrif
+      IF( ln_timing )   CALL ctl_stop( 'AGRIF not implemented with ln_timing = true')
+#endif
       !
    END SUBROUTINE nemo_ctl
@@ -492,5 +510,5 @@
       CALL iom_close                                 ! close all input/output files managed by iom_*
       !
-      IF( numstp          /= -1 )   CLOSE( numstp      )   ! time-step file      
+      IF( numstp          /= -1 )   CLOSE( numstp          )   ! time-step file      
       IF( numnam_ref      /= -1 )   CLOSE( numnam_ref      )   ! oce reference namelist
       IF( numnam_cfg      /= -1 )   CLOSE( numnam_cfg      )   ! oce configuration namelist
@@ -499,6 +517,6 @@
       IF( numnam_ice_cfg  /= -1 )   CLOSE( numnam_ice_cfg  )   ! ice configuration namelist
       IF( lwm.AND.numoni  /= -1 )   CLOSE( numoni          )   ! ice output namelist
-      IF( numevo_ice      /= -1 )   CLOSE( numevo_ice  )   ! ice variables (temp. evolution)
-      IF( numout          /=  6 )   CLOSE( numout      )   ! standard model output file
+      IF( numevo_ice      /= -1 )   CLOSE( numevo_ice      )   ! ice variables (temp. evolution)
+      IF( numout          /=  6 )   CLOSE( numout          )   ! standard model output file
       !
       numout = 6                                     ! redefine numout in case it is used after this point...
@@ -515,7 +533,7 @@
       !! ** Method  :
       !!----------------------------------------------------------------------
-      USE diawri    , ONLY: dia_wri_alloc
-      USE dom_oce   , ONLY: dom_oce_alloc
-      USE bdy_oce   , ONLY: ln_bdy, bdy_oce_alloc
+      USE diawri    , ONLY : dia_wri_alloc
+      USE dom_oce   , ONLY : dom_oce_alloc
+      USE bdy_oce   , ONLY : ln_bdy, bdy_oce_alloc
       USE oce       ! mandatory for sea-ice because needed for bdy arrays
       !
@@ -523,11 +541,11 @@
       !!----------------------------------------------------------------------
       !
-      ierr =        dia_wri_alloc   ()
-      ierr = ierr + dom_oce_alloc   ()          ! ocean domain
-      ierr = ierr + oce_alloc       ()          ! (tsn...) needed for agrif and/or ESIM and bdy
-      ierr = ierr + bdy_oce_alloc   ()          ! bdy masks (incl. initialization)
+      ierr =        dia_wri_alloc()
+      ierr = ierr + dom_oce_alloc()          ! ocean domain
+      ierr = ierr + oce_alloc    ()          ! (tsn...) needed for agrif and/or ESIM and bdy
+      ierr = ierr + bdy_oce_alloc()          ! bdy masks (incl. initialization)
       !
       IF( lk_mpp    )   CALL mpp_sum( ierr )
-      IF( ierr /= 0 )   CALL ctl_stop( 'STOP', 'nemo_alloc : unable to allocate standard ocean arrays' )
+      IF( ierr /= 0 )   CALL ctl_stop( 'STOP', 'nemo_alloc: unable to allocate standard ocean arrays' )
       !
    END SUBROUTINE nemo_alloc
@@ -538,5 +556,5 @@
       !!                 ***  ROUTINE nemo_partition  ***
       !!
-      !! ** Purpose :   
+      !! ** Purpose :
       !!
       !! ** Method  :
@@ -607,7 +625,6 @@
       knfax = 0
       !
-      ! Find the factors of n.
-      IF( kn .NE. 1 ) THEN
-
+      IF( kn /= 1 ) THEN      ! Find the factors of n
+         !
          ! nu holds the unfactorised part of the number.
          ! knfax holds the number of factors found.
@@ -622,7 +639,7 @@
             ifac = ilfax(jl)
             IF( ifac > inu )   CYCLE
-   
+            !
             ! Test whether the factor will divide.
-   
+            !
             IF( MOD(inu,ifac) == 0 ) THEN
                !
@@ -648,7 +665,7 @@
 #if defined key_mpp_mpi
 
-   SUBROUTINE nemo_northcomms
-      !!----------------------------------------------------------------------
-      !!                     ***  ROUTINE  nemo_northcomms  ***
+   SUBROUTINE nemo_nfdcom
+      !!----------------------------------------------------------------------
+      !!                     ***  ROUTINE  nemo_nfdcom  ***
       !! ** Purpose :   Setup for north fold exchanges with explicit 
       !!                point-to-point messaging
@@ -669,57 +686,52 @@
       nsndto     = 0
       !
-      !if I am a process in the north
-      IF ( njmpp == njmppmax ) THEN
-          !sxM is the first point (in the global domain) needed to compute the
-          !north-fold for the current process
-          sxM = jpiglo - nimppt(narea) - nlcit(narea) + 1
-          !dxM is the last point (in the global domain) needed to compute the
-          !north-fold for the current process
-          dxM = jpiglo - nimppt(narea) + 2
-
-          !loop over the other north-fold processes to find the processes
-          !managing the points belonging to the sxT-dxT range
-  
-          DO jn = 1, jpni
-                !sxT is the first point (in the global domain) of the jn
-                !process
-                sxT = nfiimpp(jn, jpnj)
-                !dxT is the last point (in the global domain) of the jn
-                !process
-                dxT = nfiimpp(jn, jpnj) + nfilcit(jn, jpnj) - 1
-                IF ((sxM .gt. sxT) .AND. (sxM .lt. dxT)) THEN
-                   nsndto = nsndto + 1
-                     isendto(nsndto) = jn
-                ELSEIF ((sxM .le. sxT) .AND. (dxM .ge. dxT)) THEN
-                   nsndto = nsndto + 1
-                   isendto(nsndto) = jn
-                ELSEIF ((dxM .lt. dxT) .AND. (sxT .lt. dxM)) THEN
-                   nsndto = nsndto + 1
-                   isendto(nsndto) = jn
-                ENDIF
-          END DO
-          nfsloop = 1
-          nfeloop = nlci
-          DO jn = 2,jpni-1
-           IF(nfipproc(jn,jpnj) .eq. (narea - 1)) THEN
-              IF (nfipproc(jn - 1 ,jpnj) .eq. -1) THEN
-                 nfsloop = nldi
-              ENDIF
-              IF (nfipproc(jn + 1,jpnj) .eq. -1) THEN
-                 nfeloop = nlei
-              ENDIF
-           ENDIF
-        END DO
-
+      IF ( njmpp == njmppmax ) THEN      ! if I am a process in the north
+         !
+         !sxM is the first point (in the global domain) needed to compute the north-fold for the current process
+         sxM = jpiglo - nimppt(narea) - nlcit(narea) + 1
+         !dxM is the last point (in the global domain) needed to compute the north-fold for the current process
+         dxM = jpiglo - nimppt(narea) + 2
+         !
+         ! loop over the other north-fold processes to find the processes
+         ! managing the points belonging to the sxT-dxT range
+         !
+         DO jn = 1, jpni
+            !
+            sxT = nfiimpp(jn, jpnj)                            ! sxT = 1st  point (in the global domain) of the jn process
+            dxT = nfiimpp(jn, jpnj) + nfilcit(jn, jpnj) - 1    ! dxT = last point (in the global domain) of the jn process
+            !
+            IF    ( sxT < sxM  .AND.  sxM < dxT ) THEN
+               nsndto          = nsndto + 1
+               isendto(nsndto) = jn
+            ELSEIF( sxM <= sxT  .AND.  dxM >= dxT ) THEN
+               nsndto          = nsndto + 1
+               isendto(nsndto) = jn
+            ELSEIF( dxM <  dxT  .AND.  sxT <  dxM ) THEN
+               nsndto          = nsndto + 1
+               isendto(nsndto) = jn
+            ENDIF
+            !
+         END DO
+         nfsloop = 1
+         nfeloop = nlci
+         DO jn = 2,jpni-1
+            IF( nfipproc(jn,jpnj) == (narea - 1) ) THEN
+               IF( nfipproc(jn-1,jpnj) == -1 )   nfsloop = nldi
+               IF( nfipproc(jn+1,jpnj) == -1 )   nfeloop = nlei
+            ENDIF
+         END DO
+         !
       ENDIF
       l_north_nogather = .TRUE.
-   END SUBROUTINE nemo_northcomms
+      !
+   END SUBROUTINE nemo_nfdcom
 
 #else
-   SUBROUTINE nemo_northcomms      ! Dummy routine
-      WRITE(*,*) 'nemo_northcomms: You should not have seen this print! error?'
-   END SUBROUTINE nemo_northcomms
+   SUBROUTINE nemo_nfdcom      ! Dummy routine
+      WRITE(*,*) 'nemo_nfdcom: You should not have seen this print! error?'
+   END SUBROUTINE nemo_nfdcom
 #endif
 
    !!======================================================================
 END MODULE nemogcm
+
