Index: branches/UKMO/dev_r10171_test_crs_AMM7/NEMOGCM/CONFIG/GYRE_PISCES/EXP00/namelist_cfg
===================================================================
--- branches/UKMO/dev_r10171_test_crs_AMM7/NEMOGCM/CONFIG/GYRE_PISCES/EXP00/namelist_cfg	(revision 10207)
+++ branches/UKMO/dev_r10171_test_crs_AMM7/NEMOGCM/CONFIG/GYRE_PISCES/EXP00/namelist_cfg	(revision 10216)
@@ -66,4 +66,14 @@
                !   passive tracer coarsened online simulations
 !-----------------------------------------------------------------------
+   nn_factx    = 3         !  Reduction factor of x-direction
+   nn_facty    = 3         !  Reduction factor of y-direction
+   nn_msh_crs  = 1         !  create (=1) a mesh file or not (=0)
+   nn_crs_kz   = 0         ! 0, MEAN of volume boxes
+                           ! 1, MAX of boxes
+                           ! 2, MIN of boxes
+                           ! 3, 10**(MEAN(LOG()) of volume boxes
+                           ! 4, MEDIANE of boxes
+   ln_crs_wn   = .true.    ! wn coarsened (T) or computed using horizontal divergence ( F )
+   ln_crs_top  = .true.    ! coarsening online for the BCG model ( T) or only for outpout of physics ( F )
 /
 !-----------------------------------------------------------------------
Index: branches/UKMO/dev_r10171_test_crs_AMM7/NEMOGCM/CONFIG/GYRE_PISCES/cpp_GYRE_PISCES.fcm
===================================================================
--- branches/UKMO/dev_r10171_test_crs_AMM7/NEMOGCM/CONFIG/GYRE_PISCES/cpp_GYRE_PISCES.fcm	(revision 10207)
+++ branches/UKMO/dev_r10171_test_crs_AMM7/NEMOGCM/CONFIG/GYRE_PISCES/cpp_GYRE_PISCES.fcm	(revision 10216)
@@ -1,1 +1,1 @@
-bld::tool::fppkeys  key_dynspg_flt key_ldfslp key_zdftke key_top key_pisces_reduced key_iomput key_mpp_mpi
+bld::tool::fppkeys  key_dynspg_flt key_ldfslp key_zdftke key_top key_pisces_reduced key_iomput key_mpp_mpi key_crs
Index: branches/UKMO/dev_r10171_test_crs_AMM7/NEMOGCM/NEMO/OPA_SRC/CRS/crsdom.F90
===================================================================
--- branches/UKMO/dev_r10171_test_crs_AMM7/NEMOGCM/NEMO/OPA_SRC/CRS/crsdom.F90	(revision 10207)
+++ branches/UKMO/dev_r10171_test_crs_AMM7/NEMOGCM/NEMO/OPA_SRC/CRS/crsdom.F90	(revision 10216)
@@ -73,4 +73,17 @@
             ijje = mje_crs(jj)
 
+            IF( ijis .le. 0   )WRITE(narea+1000-1,*)"BUG ijis ";CALL FLUSH(narea+1000-1)
+            IF( ijie .gt. jpi )WRITE(narea+1000-1,*)"BUG ijie ";CALL FLUSH(narea+1000-1)
+            IF( ijjs .le. 0   )WRITE(narea+1000-1,*)"BUG ijjs ";CALL FLUSH(narea+1000-1)
+            IF( ijje .gt. jpj )WRITE(narea+1000-1,*)"BUG ijje ";CALL FLUSH(narea+1000-1)
+
+            !IF( jj==nlej_crs .OR. ji==nlei_crs )THEN
+            !WRITE(narea+1000-1,*)"MASK A",ji,jj
+            !WRITE(narea+1000-1,*)"MASK A",ji+nimpp_crs-1,jj+njmpp_crs-1
+            !WRITE(narea+1000-1,*)"MASK B",ijis,ijie,ijjs,ijje
+            !WRITE(narea+1000-1,*)"MASK B",ijis+nimpp-1,ijie+nimpp-1,ijjs+njmpp-1,ijje+njmpp-1
+            !WRITE(narea+1000-1,*)"MASK C",SUM( tmask(ijis:ijie,ijjs:ijje,jk) )
+            !ENDIF
+
             zmask = 0.0
             zmask = SUM( tmask(ijis:ijie,ijjs:ijje,jk) )
@@ -95,4 +108,5 @@
    CALL crs_lbc_lnk( umask_crs, 'U', 1.0 )
    CALL crs_lbc_lnk( fmask_crs, 'F', 1.0 )
+
    !
    END SUBROUTINE crs_dom_msk
@@ -216,4 +230,7 @@
   
       !!----------------------------------------------------------------  
+      p_e1_crs(:,:)=0._wp
+      p_e2_crs(:,:)=0._wp
+
       ! Initialize      
 
@@ -1118,9 +1135,19 @@
       SELECT CASE ( jperio )
 
-      CASE ( 0, 1 )
-      jpiglo_crs   = INT( (jpiglo - 2) / nn_factx ) + 2
-      jpjglo_crs   = INT( (jpjglo - 2) / nn_facty ) + 2
-
-      CASE ( 3, 4 )    !  
+      CASE ( 0, 1 )   ! limited area
+
+              !--!--!--!--!--!--!--!--!--!
+              !        !        !        !
+              !        !        !        !
+              !        !        !        !
+              !--!--!--!--!--!--!--!--!--!
+      !IBI36               1  2  3  4  5  
+      !IBI12  !    1   !   2    !   3    ! 
+      
+
+      jpiglo_crs   = INT( (jpiglo - 4) / nn_factx ) + 4
+      jpjglo_crs   = INT( (jpjglo - 4) / nn_facty ) + 4
+
+      CASE ( 3, 4 )    ! global
       jpiglo_crs   = INT( (jpiglo - 2) / nn_factx ) + 2
       jpjglo_crs   = INT( (jpjglo - MOD(jpjglo, nn_facty)) / nn_facty ) + 2
@@ -1183,5 +1210,4 @@
          ! check
          !==========================================================================
-         !CALL FLUSH(narea+1000-1)
          !WRITE(narea+1000-1,*)"nfipproc(ji,jj),narea :",nfipproc(iproci,iprocj),narea
          !WRITE(narea+1000-1,*)"proc i,j ",iproci,iprocj
@@ -1233,8 +1259,9 @@
 
             DO ji=1,jpiglo_crs
-               ijis=nn_factx*(ji-1)+1
-               ijie=nn_factx*(ji-1)+3
+               ijis=nn_factx*ji-6
+               ijie=nn_factx*ji-4
                mis2_crs(ji)=ijis
                mie2_crs(ji)=ijie
+               !WRITE(narea+1000-1,*)"glo crs",ji,ijis,ijie,ijis-nimpp+1,ijie-nimpp+1
             ENDDO
 
@@ -1271,8 +1298,10 @@
 
          !----------------------------------------------------------------------------------------------
-         ! I-3 compute nldi_crs and compute mis2_crs and mie2_crs for the first cell of the local domain 
+         ! I-3 compute nldi_crs 
+         !     special case  when the domain on the left is land: 
+         !         compute mis2_crs and mie2_crs for the first cell of the local domain 
          !---------------------------------------------------------------------------------------------
          nldi_crs = 2
-         IF( nowe == -1 .AND. ( (jperio==3 .OR. jperio==4 ) .OR. ( (jperio==0 .OR. jperio==1 ) .AND. iproci .NE. 1 )) )THEN
+         IF( nowe == -1 .AND. ( jperio==3 .OR. jperio==4 .OR. jperio==0 .OR. jperio==1 ) )THEN
 
             mie2_crs(ijis-1) = mis2_crs(ijis)-1
@@ -1281,6 +1310,6 @@
                CASE(1)
                   nldi_crs=2
-                  mie2_crs(ijis-1) = -1
-                  mis2_crs(ijis-1) = -1
+                  mie2_crs(ijis-1) = 1
+                  mis2_crs(ijis-1) = 1
                CASE(2)
                   nldi_crs=2
@@ -1289,6 +1318,4 @@
                   nldi_crs=2
                   mis2_crs(ijis-1) = mie2_crs(ijis-1) -1
-               CASE DEFAULT
-                  WRITE(narea+8000-1,*)"WRONG VALUE FOR iistart ",ii_start
             END SELECT
 
@@ -1300,4 +1327,6 @@
          nimpp_crs = ijis-1
          IF( nimpp==1 )nimpp_crs=1
+
+         !WRITE(narea+1000-1,*)" nimpp_crs ",nimpp_crs
 
          !-------------------------------------------------------------------------------
@@ -1317,10 +1346,6 @@
          ! I-6 compute nlei_crs and nlci_crs 
          !-------------------------------------------------------------------------------
-         nlei_crs=ijie-nimpp_crs+1
-         !nlci_crs=nlei_crs ! cbr ???? +jpreci
-         !IF( iproci == jpni ) THEN ; nlci_crs=nlei_crs ! cbr ???? +jpreci
-         !ELSE                      ; nlci_crs=nlei_crs+1
-         !ENDIF
-         !cbr???? IF( iproci == jpni )nlei_crs=nlci_crs
+         nlei_crs = ijie-nimpp_crs+1
+         nlci_crs = nlei_crs+1
 
          !-------------------------------------------------------------------------------
@@ -1340,35 +1365,95 @@
          !---------------------------------------------------------------------------------------
          DO ji = 1, nlei_crs
-            IF( mig_crs(ji) .GT. jpiglo_crs )WRITE(narea+1000-1,*)"BUG1 " ; CALL FLUSH(narea+1000-1)
             mis_crs(ji) = mis2_crs(mig_crs(ji)) - nimpp + 1
             mie_crs(ji) = mie2_crs(mig_crs(ji)) - nimpp + 1
-            !IF( iproci == jpni  .AND. ji == nlei_crs )THEN
-            !   mie_crs(ji) = nlei
-            !   mie2_crs(mig_crs(ji)) = mig(nlei)
-            !ENDIF
             nfactx(ji)  = mie_crs(ji)-mis_crs(ji)+1
-         ENDDO
-
-         !---------
-         !cbr 
+         ENDDO         
+
+         !-----------------------------------------------------------------------------
+         !adapt western indices for E-W BC ( jperio 3/4 ) or ghost cells ( jperio 0/1) 
+         !-----------------------------------------------------------------------------
          IF( iproci == 1 ) THEN
-            nldi_crs=1
-            mis_crs(1) = 1
-            mie_crs(1) = 1
-            mis2_crs(1) = 1
-            mie2_crs(1) = 1
+
+            SELECT CASE ( jperio )
+
+            CASE ( 3, 4 )
+
+               nldi_crs=1
+               mis_crs(1)  = 1
+               mie_crs(1)  = 1
+               mis2_crs(1) = 1
+               mie2_crs(1) = 1
+
+            CASE ( 0, 1 )
+               nldi_crs=1
+               !the first cell is a ghsot cell; so set indices to 1 just to have an positive indice
+               mis_crs(1)  = 1
+               mie_crs(1)  = 1
+               mis2_crs(1) = 1
+               mie2_crs(1) = 1
+               nfactx(1)   = 1
+
+               !the second coarsened cell correspond to the first (ghost) and second cell of HR grid
+               mis_crs(2)  = 1
+               mie_crs(2)  = 2
+               mis2_crs(2) = 1
+               mie2_crs(2) = 2
+               nfactx(2)   = 2
+
+            CASE DEFAULT
+               WRITE(numout,*) 'crs_init. Only jperio = 0, 1, 3, 4 supported; narea: ',narea
+            END SELECT
+        
          ENDIF
 
-         IF( iproci == jpni ) THEN
-            nlei_crs=jpiglo_crs-nimpp_crs+1
-            nlci_crs=nlei_crs
-            mis_crs(nlei_crs) = 1
-            mie_crs(nlei_crs) = 1
-            mis2_crs(nlei_crs) = 1
-            mie2_crs(nlei_crs) = 1
-            nfactx(nlei_crs)=0
-         ELSE
-            nlci_crs=nlei_crs+1
-         ENDIF
+         !----------------------------------------------------------------------------
+         !adapt eastern indices for E-W BC ( jperio 3/4 ) or ghost cells ( jperio 0/1) 
+         !----------------------------------------------------------------------------
+         SELECT CASE ( jperio )
+
+         CASE ( 3, 4 )
+
+            IF( iproci == jpni ) THEN
+               nlei_crs           = jpiglo_crs-nimpp_crs+1
+               nlci_crs           = nlei_crs
+               mis_crs(nlei_crs)  = 1
+               mie_crs(nlei_crs)  = 1
+               mis2_crs(nlei_crs) = 1
+               mie2_crs(nlei_crs) = 1
+               nfactx(nlei_crs)   = 0
+            ENDIF
+
+         CASE ( 0, 1 )
+            IF( iproci == jpni ) THEN
+
+             !              |                 |                 |                 |
+             !            __|_____!_____!_____|_____!_____!_____|_____!_____!_____|
+             !HR    grid loc                         nlei
+             !HR    grid glo                        jpiglo
+             !HRCRS grid loc                      nlei_crs-1           nlei
+             !HRCRS grid glo                      jpiglo-1            jpiglo
+
+
+               !last cell ( = ghost cell)
+               nlei_crs           = jpiglo_crs-nimpp_crs+1
+               nlci_crs           = nlei_crs
+     
+               mis2_crs(nlei_crs) = jpiglo
+               mie2_crs(nlei_crs) = jpiglo
+               mis_crs (nlei_crs) = jpiglo     - nimpp + 1
+               mie_crs (nlei_crs) = jpiglo     - nimpp + 1
+               nfactx  (nlei_crs) = 1
+
+               mis2_crs(nlei_crs-1) = jpiglo - 1
+               mie2_crs(nlei_crs-1) = jpiglo
+               mis_crs (nlei_crs-1) = jpiglo - 1 - nimpp + 1 
+               mie_crs (nlei_crs-1) = jpiglo     - nimpp + 1
+               nfactx  (nlei_crs-1) = 2
+
+            ENDIF
+         CASE DEFAULT
+            WRITE(numout,*) 'crs_init. Only jperio = 0, 1, 3, 4 supported; narea: ',narea
+         END SELECT
+
 
          !WRITE(narea+1000-1,*)"loc crs jpi nldi,nlei,nlci ",jpi_crs, nldi_crs            ,nlei_crs             ,nlci_crs
@@ -1408,8 +1493,9 @@
 
             DO jj=1,jpjglo_crs
-               ijjs=nn_facty*(jj-1)+1
-               ijje=nn_facty*(jj-1)+3
+               ijjs=nn_facty*jj-6
+               ijje=nn_facty*jj-4
                mjs2_crs(jj)=ijjs
                mje2_crs(jj)=ijje
+               !WRITE(narea+1000-1,*)"glo crs",jj,ijjs,ijje,ijjs-njmpp+1,ijje-njmpp+1
             ENDDO
 
@@ -1421,4 +1507,5 @@
                mjs2_crs(jj)=ijjs
                mje2_crs(jj)=ijje
+               !WRITE(narea+1000-1,*)"glo crs",jj,ijjs,ijje,ijjs-njmpp+1,ijje-njmpp+1
             ENDDO
 
@@ -1444,5 +1531,8 @@
 
          !----------------------------------------------------------------------------------------------
-         ! J-3 compute nldj_crs and compute mjs2_crs and mje2_crs for the first cell of the local domain 
+         ! J-3 compute nldj_crs 
+         !     global: special case when the domain on the north is land: 
+         !             compute mjs2_crs and mje2_crs for the first cell of the local domain 
+         !     local :  special case for southern domain  
          !---------------------------------------------------------------------------------------------
          nldj_crs = 2
@@ -1480,4 +1570,5 @@
          njmpp_crs = ijjs-1
          IF( njmpp==1 )njmpp_crs=1
+         !WRITE(narea+1000-1,*)" njmpp_crs ",njmpp_crs
 
          !-------------------------------------------------------------------------------
@@ -1499,18 +1590,12 @@
          nlcj_crs=nlej_crs+jprecj
 
+         !north-fold BC for global
          IF( iprocj == jpnj )THEN
             IF( jperio==3 .OR. jperio==4 )THEN
                nlej_crs=nlej_crs+1
                nlcj_crs=nlej_crs
-            ELSE
-               nlej_crs= nlej_crs+1
-               nlcj_crs= nlcj_crs+1
             ENDIF
          ENDIF
 
-         !WRITE(narea+1000-1,*)"loc crs jpj nldj,nlej,nlcj ",jpj_crs, nldj_crs            ,nlej_crs             ,nlcj_crs
-         !CALL FLUSH(narea+1000-1)
-         !WRITE(narea+1000-1,*)"glo crs jpj nldj,nlej      ",jpj_crs, nldj_crs+njmpp_crs-1,nlej_crs+njmpp_crs-1
-         !CALL FLUSH(narea+1000-1)
          !-------------------------------------------------------------------------------
          ! J-7 local to global and global to local indices for CRS grid
@@ -1530,4 +1615,45 @@
             mjs_crs(jj) = mjs2_crs(mjg_crs(jj)) - njmpp + 1
             mje_crs(jj) = mje2_crs(mjg_crs(jj)) - njmpp + 1
+            nfacty(jj)  = mje_crs(jj)-mjs_crs(jj)+1
+            !WRITE(narea+1000-1,*)"test J",jj,mjg_crs(jj),mjs_crs(jj),mje_crs(jj),mjs_crs(jj)+njmpp-1,mje_crs(jj)+njmpp-1,nfacty(jj)
+         ENDDO
+
+         !-----------------------------------------------------------------------------
+         !adpat indices for southern side of the domain (ghost cells for jperio 0/1 ) 
+         !-----------------------------------------------------------------------------
+         IF( iprocj == 1 ) THEN
+
+            SELECT CASE ( jperio )
+
+            CASE ( 3, 4 )
+               ! do nothing 
+            CASE ( 0, 1 )
+                nldj_crs = 1
+               !the first cell is a ghsot cell; so set indices to 1 just to have an positive indice
+               mjs_crs(1)  = 1
+               mje_crs(1)  = 1
+               mjs2_crs(1) = 1
+               mje2_crs(1) = 1
+               nfacty(1)   = 1
+
+               !the second coarsened cell correspond to the first (ghost) and second cell of HR grid
+               mjs_crs(2)  = 1
+               mje_crs(2)  = 2
+               mjs2_crs(2) = 1
+               mje2_crs(2) = 2
+               nfacty(2)   = 2
+
+            CASE DEFAULT
+               WRITE(numout,*) 'crs_init. Only jperio = 0, 1, 3, 4 supported; narea: ',narea
+            END SELECT
+
+         ENDIF
+
+         !-----------------------------------------------
+         !adapt northen indices for ghost cells ( jperio 0/1) 
+         !-----------------------------------------------
+         SELECT CASE ( jperio )
+
+         CASE ( 3, 4 )
             IF( iprocj == jpnj  .AND. jj == nlej_crs )THEN
                mjs_crs(jj) = nlej
@@ -1536,7 +1662,29 @@
                mje2_crs(mjg_crs(jj)) = mjg(nlej)
             ENDIF
-            nfacty(jj)  = mje_crs(jj)-mjs_crs(jj)+1
-            !WRITE(narea+1000-1,*)"test J",jj,mjg_crs(jj),mjs_crs(jj),mje_crs(jj),mjs_crs(jj)+njmpp-1,mje_crs(jj)+njmpp-1,nfacty(jj)
-         ENDDO
+
+         CASE ( 0, 1 )
+            IF( iprocj == jpnj ) THEN
+               !north side of the domain
+
+               !last cell ( = ghost cell)
+               nlej_crs           = jpjglo_crs-njmpp_crs+1
+               nlcj_crs           = nlej_crs
+
+               mjs2_crs(nlej_crs) = jpjglo
+               mje2_crs(nlej_crs) = jpjglo
+               mjs_crs (nlej_crs) = jpjglo     - njmpp + 1
+               mje_crs (nlej_crs) = jpjglo     - njmpp + 1
+               nfacty  (nlej_crs) = 1
+
+               mjs2_crs(nlej_crs-1) = jpjglo - 1
+               mje2_crs(nlej_crs-1) = jpjglo
+               mjs_crs (nlej_crs-1) = jpjglo - 1 - njmpp + 1
+               mje_crs (nlej_crs-1) = jpjglo     - njmpp + 1
+               nfacty  (nlej_crs-1) = 2
+
+            ENDIF
+         CASE DEFAULT
+           WRITE(numout,*) 'crs_init. Only jperio = 0, 1, 3, 4 supported; narea: ',narea
+         END SELECT
 
          !WRITE(narea+1000-1,*)"loc crs jpj nldj,nlej,nlcj ",jpj_crs, nldj_crs ,nlej_crs,nlcj_crs ; CALL FLUSH(narea+1000-1)
@@ -1747,4 +1895,5 @@
 
       !!----------------------------------------------------------------
+
       mikt_crs(:,:)=1
 
