Index: branches/2016/dev_CNRS_2016/NEMOGCM/NEMO/OPA_SRC/nemogcm.F90
===================================================================
--- branches/2016/dev_CNRS_2016/NEMOGCM/NEMO/OPA_SRC/nemogcm.F90	(revision 7276)
+++ branches/2016/dev_CNRS_2016/NEMOGCM/NEMO/OPA_SRC/nemogcm.F90	(revision 7277)
@@ -33,4 +33,5 @@
    !!             -   ! 2013-06  (I. Epicoco, S. Mocavero, CMCC) nemo_northcomms: setup avoiding MPI communication 
    !!             -   ! 2014-12  (G. Madec) remove KPP scheme and cross-land advection (cla)
+   !!            4.0  ! 2016-10  (G. Madec, S. Flavoni)  domain configuration / user defined interface
    !!----------------------------------------------------------------------
 
@@ -45,10 +46,7 @@
    !!----------------------------------------------------------------------
    USE step_oce       ! module used in the ocean time stepping module (step.F90)
-   USE domcfg         ! domain configuration               (dom_cfg routine)
-   USE mppini         ! shared/distributed memory setting (mpp_init routine)
-   USE domain         ! domain initialization             (dom_init routine)
-#if defined key_nemocice_decomp
-   USE ice_domain_size, only: nx_global, ny_global
-#endif
+   USE phycst         ! physical constant                  (par_cst routine)
+   USE domain         ! domain initialization   (dom_init & dom_cfg routines)
+   USE usrdef_nam     ! user defined configuration
    USE tideini        ! tidal components initialization   (tide_ini routine)
    USE bdyini         ! open boundary cond. setting       (bdy_init routine)
@@ -60,5 +58,4 @@
    USE ldftra         ! lateral diffusivity setting    (ldftra_init routine)
    USE zdfini         ! vertical physics setting          (zdf_init routine)
-   USE phycst         ! physical constant                  (par_cst routine)
    USE trdini         ! dyn/tra trends initialization     (trd_init routine)
    USE asminc         ! assimilation increments     
@@ -68,5 +65,4 @@
    USE diaobs         ! Observation diagnostics       (dia_obs_init routine)
    USE diacfl         ! CFL diagnostics               (dia_cfl_init routine)
-   USE lib_fortran    ! Fortran utilities (allows no signed zero when 'key_nosignedzero' defined)
    USE step           ! NEMO time-stepping                 (stp     routine)
    USE icbini         ! handle bergs, initialisation
@@ -78,19 +74,25 @@
    USE stopar         ! Stochastic param.: ???
    USE stopts         ! Stochastic param.: ???
+   USE diurnal_bulk   ! diurnal bulk SST 
+   USE step_diu       ! diurnal bulk SST timestepping (called from here if run offline)
+   USE crsini         ! initialise grid coarsening utility
+   USE diatmb         ! Top,middle,bottom output
+   USE dia25h         ! 25h mean output
+   USE sbc_oce , ONLY : lk_oasis
+   USE wet_dry        ! Wetting and drying setting   (wad_init routine)
 #if defined key_top
    USE trcini         ! passive tracer initialisation
 #endif
+#if defined key_nemocice_decomp
+   USE ice_domain_size, only: nx_global, ny_global
+#endif
+   !
    USE lib_mpp        ! distributed memory computing
-   USE diurnal_bulk    ! diurnal bulk SST 
-   USE step_diu        ! diurnal bulk SST timestepping (called from here if run offline)
+   USE mppini         ! shared/distributed memory setting (mpp_init routine)
+   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
    USE xios           ! xIOserver
 #endif
-   USE crsini         ! initialise grid coarsening utility
-   USE lbcnfd , ONLY  : isendto, nsndto, nfsloop, nfeloop   ! Setup of north fold exchanges 
-   USE sbc_oce, ONLY  : lk_oasis
-   USE diatmb          ! Top,middle,bottom output
-   USE dia25h          ! 25h mean output
-   USE wet_dry         ! Wetting and drying setting   (wad_init routine)
 
    IMPLICIT NONE
@@ -104,5 +106,5 @@
 
    !!----------------------------------------------------------------------
-   !! NEMO/OPA 3.7 , NEMO Consortium (2015)
+   !! NEMO/OPA 4.0 , NEMO Consortium (2016)
    !! $Id$
    !! Software governed by the CeCILL licence     (NEMOGCM/NEMO_CeCILL.txt)
@@ -124,5 +126,5 @@
       !!              Madec, 2008, internal report, IPSL.
       !!----------------------------------------------------------------------
-      INTEGER ::   istp       ! time step index
+      INTEGER ::   istp   ! time step index
       !!----------------------------------------------------------------------
       !
@@ -130,5 +132,5 @@
       CALL Agrif_Init_Grids()      ! AGRIF: set the meshes
 #endif
-
+      !
       !                            !-----------------------!
       CALL nemo_init               !==  Initialisations  ==!
@@ -195,7 +197,7 @@
       !                            !==  finalize the run  ==!
       !                            !------------------------!
-      IF(lwp) WRITE(numout,cform_aaa)   ! Flag AAAAAAA
-      !
-      IF( nstop /= 0 .AND. lwp ) THEN   ! error print
+      IF(lwp) WRITE(numout,cform_aaa)        ! Flag AAAAAAA
+      !
+      IF( nstop /= 0 .AND. lwp ) THEN        ! error print
          WRITE(numout,cform_err)
          WRITE(numout,*) nstop, ' error have been found'
@@ -215,11 +217,11 @@
       !
 #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
+         CALL cpl_finalize                   ! end coupling and mpp communications with OASIS
       ELSE
-         IF( lk_mpp )   CALL mppstop    ! end mpp communications
+         IF( lk_mpp )   CALL mppstop         ! end mpp communications
       ENDIF
 #endif
@@ -234,17 +236,17 @@
       !! ** Purpose :   initialization of the NEMO GCM
       !!----------------------------------------------------------------------
-      INTEGER ::   ji            ! dummy loop indices
-      INTEGER ::   ilocal_comm   ! local integer
-      INTEGER ::   ios
-      CHARACTER(len=80), DIMENSION(16) ::   cltxt
-      !
-      NAMELIST/namctl/ ln_ctl  , nn_print, nn_ictls, nn_ictle,   &
-         &             nn_isplt, nn_jsplt, nn_jctls, nn_jctle,   &
-         &             nn_bench, nn_timing, nn_diacfl
-      NAMELIST/namcfg/ cp_cfg, cp_cfz, jp_cfg, jpidta, jpjdta, jpkdta, jpiglo, jpjglo, &
-         &             jpizoom, jpjzoom, jperio, ln_use_jattr
-      !!----------------------------------------------------------------------
-      !
-      cltxt = ''
+      INTEGER  ::   ji                 ! dummy loop indices
+      INTEGER  ::   ios, ilocal_comm   ! local integer
+      CHARACTER(len=120), DIMENSION(30) ::   cltxt, cltxt2, clnam
+      !
+      NAMELIST/namctl/ ln_ctl   , nn_print, nn_ictls, nn_ictle,   &
+         &             nn_isplt , nn_jsplt, nn_jctls, nn_jctle,   &
+         &             nn_timing, nn_diacfl
+      NAMELIST/namcfg/ ln_read_cfg, cn_domcfg, ln_write_cfg, cn_domcfg_out, ln_use_jattr
+      !!----------------------------------------------------------------------
+      !
+      cltxt  = ''
+      cltxt2 = ''
+      clnam  = ''  
       cxios_context = 'nemo'
       !
@@ -253,14 +255,13 @@
       CALL ctl_opn( numnam_cfg, 'namelist_cfg', 'OLD', 'FORMATTED', 'SEQUENTIAL', -1, 6, .FALSE. )
       !
-      REWIND( numnam_ref )              ! Namelist namctl in reference namelist : Control prints & Benchmark
+      REWIND( numnam_ref )              ! Namelist namctl in reference namelist : Control prints
       READ  ( numnam_ref, namctl, IOSTAT = ios, ERR = 901 )
 901   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namctl in reference namelist', .TRUE. )
-
-      REWIND( numnam_cfg )              ! Namelist namctl in confguration namelist : Control prints & Benchmark
+      !
+      REWIND( numnam_cfg )              ! Namelist namctl in confguration namelist
       READ  ( numnam_cfg, namctl, IOSTAT = ios, ERR = 902 )
 902   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namctl in configuration namelist', .TRUE. )
-
-      !
-      REWIND( numnam_ref )              ! Namelist namcfg in reference namelist : Control prints & Benchmark
+      !
+      REWIND( numnam_ref )              ! Namelist namcfg in reference namelist : Control prints
       READ  ( numnam_ref, namcfg, IOSTAT = ios, ERR = 903 )
 903   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namcfg in reference namelist', .TRUE. )
@@ -270,19 +271,26 @@
 904   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namcfg in configuration namelist', .TRUE. )   
 
-! Force values for AGRIF zoom (cf. agrif_user.F90)
+      !                             !--------------------------!
+      !                             !  Set global domain size  !   (control print return in cltxt2)
+      !                             !--------------------------!
+      IF( ln_read_cfg ) THEN              ! Read sizes in domain configuration file
+         CALL domain_cfg ( cltxt2,        cn_cfg, nn_cfg, jpiglo, jpjglo, jpkglo, jperio )
+         !
+      ELSE                                ! user-defined namelist
+         CALL usr_def_nam( cltxt2, clnam, cn_cfg, nn_cfg, jpiglo, jpjglo, jpkglo, jperio )
+      ENDIF
+      !
+      jpk = jpkglo
+      !
 #if defined key_agrif
-   IF( .NOT. Agrif_Root() ) THEN
-      jpiglo  = nbcellsx + 2 + 2*nbghostcells
-      jpjglo  = nbcellsy + 2 + 2*nbghostcells
-      jpi     = ( jpiglo-2*jpreci + (jpni-1+0) ) / jpni + 2*jpreci
-      jpj     = ( jpjglo-2*jprecj + (jpnj-1+0) ) / jpnj + 2*jprecj
-      jpidta  = jpiglo
-      jpjdta  = jpjglo
-      jpizoom = 1
-      jpjzoom = 1
-      nperio  = 0
-      jperio  = 0
-      ln_use_jattr = .false.
-   ENDIF
+      IF( .NOT. Agrif_Root() ) THEN       ! AGRIF children: specific setting (cf. agrif_user.F90)
+         jpiglo  = nbcellsx + 2 + 2*nbghostcells
+         jpjglo  = nbcellsy + 2 + 2*nbghostcells
+         jpi     = ( jpiglo-2*jpreci + (jpni-1+0) ) / jpni + 2*jpreci
+         jpj     = ( jpjglo-2*jprecj + (jpnj-1+0) ) / jpnj + 2*jprecj
+         nperio  = 0
+         jperio  = 0
+         ln_use_jattr = .false.
+      ENDIF
 #endif
       !
@@ -295,8 +303,8 @@
       IF( Agrif_Root() ) THEN
          IF( lk_oasis ) THEN
-            CALL cpl_init( "oceanx", ilocal_comm )                     ! nemo local communicator given by oasis
-            CALL xios_initialize( "not used",local_comm=ilocal_comm )    ! send nemo communicator to xios
+            CALL cpl_init( "oceanx", ilocal_comm )                               ! nemo local communicator given by oasis
+            CALL xios_initialize( "not used"       ,local_comm= ilocal_comm )    ! send nemo communicator to xios
          ELSE
-            CALL  xios_initialize( "for_xios_mpi_id",return_comm=ilocal_comm )    ! nemo local communicator given by xios
+            CALL xios_initialize( "for_xios_mpi_id",return_comm=ilocal_comm )    ! nemo local communicator given by xios
          ENDIF
       ENDIF
@@ -306,14 +314,14 @@
       IF( lk_oasis ) THEN
          IF( Agrif_Root() ) THEN
-            CALL cpl_init( "oceanx", ilocal_comm )                      ! nemo local communicator given by oasis
+            CALL cpl_init( "oceanx", ilocal_comm )          ! nemo local communicator given by oasis
          ENDIF
          ! Nodes selection (control print return in cltxt)
          narea = mynode( cltxt, 'output.namelist.dyn', numnam_ref, numnam_cfg, numond , nstop, ilocal_comm )
       ELSE
-         ilocal_comm = 0
-         ! Nodes selection (control print return in cltxt)
+         ilocal_comm = 0                                    ! Nodes selection (control print return in cltxt)
          narea = mynode( cltxt, 'output.namelist.dyn', numnam_ref, numnam_cfg, numond , nstop )
       ENDIF
 #endif
+
       narea = narea + 1                                     ! mynode return the rank of proc (0 --> jpnij -1 )
 
@@ -321,10 +329,14 @@
       lwp = (narea == 1) .OR. ln_ctl                        ! control of all listing output print
 
-      IF(lwm) THEN
-         ! write merged namelists from earlier to output namelist now that the
-         ! file has been opened in call to mynode. nammpp has already been
-         ! written in mynode (if lk_mpp_mpi)
+      IF(lwm) THEN               ! write merged namelists from earlier to output namelist 
+         !                       ! now that the file has been opened in call to mynode. 
+         !                       ! NB: nammpp has already been written in mynode (if lk_mpp_mpi)
          WRITE( numond, namctl )
          WRITE( numond, namcfg )
+         IF( .NOT.ln_read_cfg ) THEN
+            DO ji = 1, SIZE(clnam)
+               IF( TRIM(clnam(ji)) /= '' )   WRITE(numond, * ) clnam(ji)     ! namusr_def print
+            END DO
+         ENDIF
       ENDIF
 
@@ -341,7 +353,5 @@
       ENDIF
 
-      ! Calculate domain dimensions given calculated jpni and jpnj
-      ! This used to be done in par_oce.F90 when they were parameters rather than variables
-      IF( Agrif_Root() ) THEN
+      IF( Agrif_Root() ) THEN       ! AGRIF mother: specific setting from jpni and jpnj
 #if defined key_nemocice_decomp
          jpi = ( nx_global+2-2*jpreci + (jpni-1) ) / jpni + 2*jpreci    ! first  dim.
@@ -351,16 +361,20 @@
          jpj = ( jpjglo     -2*jprecj + (jpnj-1) ) / jpnj + 2*jprecj    ! second dim.
 #endif
-      ENDIF         
-         jpk = jpkdta                                             ! third dim
+      ENDIF
+
+!!gm ???    why here  it has already been done in line 301 !
+      jpk = jpkglo                                             ! third dim
+!!gm end
+
 #if defined key_agrif
-         ! simple trick to use same vertical grid as parent but different number of levels: 
-         ! Save maximum number of levels in jpkdta, then define all vertical grids with this number.
-         ! Suppress once vertical online interpolation is ok
-         IF(.NOT.Agrif_Root())   jpkdta = Agrif_Parent( jpkdta )
-#endif
-         jpim1 = jpi-1                                            ! inner domain indices
-         jpjm1 = jpj-1                                            !   "           "
-         jpkm1 = jpk-1                                            !   "           "
-         jpij  = jpi*jpj                                          !  jpi x j
+      ! simple trick to use same vertical grid as parent but different number of levels: 
+      ! Save maximum number of levels in jpkglo, then define all vertical grids with this number.
+      ! Suppress once vertical online interpolation is ok
+      IF(.NOT.Agrif_Root())   jpkglo = Agrif_Parent( jpkglo )
+#endif
+      jpim1 = jpi-1                                            ! inner domain indices
+      jpjm1 = jpj-1                                            !   "           "
+      jpkm1 = jpk-1                                            !   "           "
+      jpij  = jpi*jpj                                          !  jpi x j
 
       IF(lwp) THEN                            ! open listing units
@@ -372,16 +386,21 @@
          WRITE(numout,*) '                       NEMO team'
          WRITE(numout,*) '            Ocean General Circulation Model'
-         WRITE(numout,*) '                  version 3.7  (2015) '
+         WRITE(numout,*) '                NEMO version 3.7  (2016) '
          WRITE(numout,*)
          WRITE(numout,*)
          DO ji = 1, SIZE(cltxt)
-            IF( TRIM(cltxt(ji)) /= '' )   WRITE(numout,*) cltxt(ji)      ! control print of mynode
+            IF( TRIM(cltxt (ji)) /= '' )   WRITE(numout,*) cltxt(ji)    ! control print of mynode
          END DO
-         WRITE(numout,cform_aaa)                                         ! Flag AAAAAAA
+         WRITE(numout,*)
+         WRITE(numout,*)
+         DO ji = 1, SIZE(cltxt2)
+            IF( TRIM(cltxt2(ji)) /= '' )   WRITE(numout,*) cltxt2(ji)   ! control print of domain size
+         END DO
          !
-      ENDIF
-
-      ! Now we know the dimensions of the grid and numout has been set we can
-      ! allocate arrays
+         WRITE(numout,cform_aaa)                                        ! Flag AAAAAAA
+         !
+      ENDIF
+
+      ! Now we know the dimensions of the grid and numout has been set: we can allocate arrays
       CALL nemo_alloc()
 
@@ -390,5 +409,5 @@
       !                             !-------------------------------!
 
-      CALL nemo_ctl                          ! Control prints & Benchmark
+      CALL nemo_ctl                          ! Control prints
 
       !                                      ! Domain decomposition
@@ -404,5 +423,4 @@
       IF( lk_c1d        )   CALL     c1d_init   ! 1D column configuration
                             CALL     wad_init   ! Wetting and drying options
-                            CALL     dom_cfg    ! Domain configuration
                             CALL     dom_init   ! Domain
       IF( ln_crs        )   CALL     crs_init   ! coarsened grid: domain initialization 
@@ -503,5 +521,4 @@
                             CALL dia_tmb_init  ! TMB outputs
                             CALL dia_25h_init  ! 25h mean  outputs
-
       !
    END SUBROUTINE nemo_init
@@ -519,5 +536,5 @@
       IF(lwp) THEN                  ! control print
          WRITE(numout,*)
-         WRITE(numout,*) 'nemo_ctl: Control prints & Benchmark'
+         WRITE(numout,*) 'nemo_ctl: Control prints'
          WRITE(numout,*) '~~~~~~~ '
          WRITE(numout,*) '   Namelist namctl'
@@ -530,5 +547,4 @@
          WRITE(numout,*) '      number of proc. following i     nn_isplt   = ', nn_isplt
          WRITE(numout,*) '      number of proc. following j     nn_jsplt   = ', nn_jsplt
-         WRITE(numout,*) '      benchmark parameter (0/1)       nn_bench   = ', nn_bench
          WRITE(numout,*) '      timing activated    (0/1)       nn_timing  = ', nn_timing
       ENDIF
@@ -541,5 +557,4 @@
       isplt     = nn_isplt
       jsplt     = nn_jsplt
-      nbench    = nn_bench
 
       IF(lwp) THEN                  ! control print
@@ -548,16 +563,9 @@
          WRITE(numout,*) '~~~~~~~ '
          WRITE(numout,*) '   Namelist namcfg'
-         WRITE(numout,*) '      configuration name                               cp_cfg  = ', TRIM(cp_cfg)
-         WRITE(numout,*) '      configuration zoom name                          cp_cfz  = ', TRIM(cp_cfz)
-         WRITE(numout,*) '      configuration resolution                         jp_cfg  = ', jp_cfg
-         WRITE(numout,*) '      1st lateral dimension ( >= jpiglo )              jpidta  = ', jpidta
-         WRITE(numout,*) '      2nd    "         "    ( >= jpjglo )              jpjdta  = ', jpjdta
-         WRITE(numout,*) '      3nd    "         "                               jpkdta  = ', jpkdta
-         WRITE(numout,*) '      1st dimension of global domain in i              jpiglo  = ', jpiglo
-         WRITE(numout,*) '      2nd    -                  -    in j              jpjglo  = ', jpjglo
-         WRITE(numout,*) '      left bottom i index of the zoom (in data domain) jpizoom = ', jpizoom
-         WRITE(numout,*) '      left bottom j index of the zoom (in data domain) jpizoom = ', jpjzoom
-         WRITE(numout,*) '      lateral cond. type (between 0 and 6)             jperio  = ', jperio   
-         WRITE(numout,*) '      use file attribute if exists as i/p j-start ln_use_jattr = ', ln_use_jattr
+         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 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
       !                             ! Parameter control
@@ -600,12 +608,4 @@
       ENDIF
       !
-      IF( nbench == 1 ) THEN              ! Benchmark
-         SELECT CASE ( cp_cfg )
-         CASE ( 'gyre' )   ;   CALL ctl_warn( ' The Benchmark is activated ' )
-         CASE DEFAULT      ;   CALL ctl_stop( ' The Benchmark is based on the GYRE configuration:',   &
-            &                                 ' cp_cfg = "gyre" in namelist &namcfg or set nbench = 0' )
-         END SELECT
-      ENDIF
-      !
       IF( 1_wp /= SIGN(1._wp,-0._wp)  )   CALL ctl_stop( 'nemo_ctl: The intrinsec SIGN function follows ',  &
          &                                               'f2003 standard. '                              ,  &
@@ -666,5 +666,5 @@
       !!----------------------------------------------------------------------
       !
-      ierr =        oce_alloc       ()          ! ocean
+      ierr =        oce_alloc       ()          ! ocean 
       ierr = ierr + dia_wri_alloc   ()
       ierr = ierr + dom_oce_alloc   ()          ! ocean domain
@@ -842,12 +842,12 @@
                 IF ((sxM .gt. sxT) .AND. (sxM .lt. dxT)) THEN
                    nsndto = nsndto + 1
-                     isendto(nsndto) = jn
+                   isendto(nsndto) = jn
                 ELSEIF ((sxM .le. sxT) .AND. (dxM .ge. dxT)) THEN
                    nsndto = nsndto + 1
-                     isendto(nsndto) = jn
+                   isendto(nsndto) = jn
                 ELSEIF ((dxM .lt. dxT) .AND. (sxT .lt. dxM)) THEN
                    nsndto = nsndto + 1
-                     isendto(nsndto) = jn
-                END IF
+                   isendto(nsndto) = jn
+                ENDIF
           END DO
           nfsloop = 1
