Index: NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/ICB/icbrst.F90
===================================================================
--- NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/ICB/icbrst.F90	(revision 11396)
+++ NEMO/branches/2019/dev_r11351_fldread_with_XIOS/src/OCE/ICB/icbrst.F90	(revision 13463)
@@ -69,10 +69,9 @@
       TYPE(point)                  ::   localpt   ! NOT a pointer but an actual local variable
       !!----------------------------------------------------------------------
-
       ! Find a restart file. Assume iceberg restarts in same directory as ocean restarts
       ! and are called TRIM(cn_ocerst)//'_icebergs'
-      cl_path = TRIM(cn_ocerst_indir)
+      cl_path = TRIM(cn_icbrst_indir)
       IF( cl_path(LEN_TRIM(cl_path):) /= '/' ) cl_path = TRIM(cl_path) // '/'
-      cl_filename = TRIM(cn_ocerst_in)//'_icebergs'
+      cl_filename = TRIM(cn_icbrst_in)
       CALL iom_open( TRIM(cl_path)//cl_filename, ncid )
 
@@ -92,8 +91,8 @@
             ij = INT( localpt%yj + 0.5 )
             ! Only proceed if this iceberg is on the local processor (excluding halos).
-            IF ( ii .GE. nldi+nimpp-1 .AND. ii .LE. nlei+nimpp-1 .AND. &
-           &     ij .GE. nldj+njmpp-1 .AND. ij .LE. nlej+njmpp-1 ) THEN           
-
-               CALL iom_get( ncid, jpdom_unknown, 'number'       , zdata(:) , ktime=jn, kstart=(/1/), kcount=(/nkounts/) )
+            IF ( ii >= mig(Nis0) .AND. ii <= mig(Nie0) .AND.   &
+           &     ij >= mjg(Njs0) .AND. ij <= mjg(Nje0) ) THEN           
+
+               CALL iom_get( ncid, jpdom_unknown, 'number', zdata(:) , ktime=jn, kstart=(/1/), kcount=(/nkounts/) )
                localberg%number(:) = INT(zdata(:))
                imax_icb = MAX( imax_icb, INT(zdata(1)) )
@@ -124,13 +123,12 @@
 
       ! Gridded variables
-      CALL iom_get( ncid, jpdom_autoglo,    'calving'     , src_calving  )
-      CALL iom_get( ncid, jpdom_autoglo,    'calving_hflx', src_calving_hflx  )
-      CALL iom_get( ncid, jpdom_autoglo,    'stored_heat' , berg_grid%stored_heat  )
-      CALL iom_get( ncid, jpdom_autoglo_xy, 'stored_ice'  , berg_grid%stored_ice, kstart=(/1,1,1/), kcount=(/1,1,nclasses/) )
+      CALL iom_get( ncid, jpdom_auto,    'calving'     , src_calving  )
+      CALL iom_get( ncid, jpdom_auto,    'calving_hflx', src_calving_hflx  )
+      CALL iom_get( ncid, jpdom_auto,    'stored_heat' , berg_grid%stored_heat  )
+      ! with jpdom_auto_xy, ue use only the third element of kstart and kcount.
+      CALL iom_get( ncid, jpdom_auto_xy, 'stored_ice'  , berg_grid%stored_ice, kstart=(/-99,-99,1/), kcount=(/-99,-99,nclasses/) )
       
       CALL iom_get( ncid, jpdom_unknown, 'kount' , zdata(:) )
       num_bergs(:) = INT(zdata(:))
-      ! Close file
-      CALL iom_close( ncid )
       !
 
@@ -146,4 +144,6 @@
       IF( lwp )   WRITE(numout,'(a,i5,a,i5,a)') 'icebergs, icb_rst_read: there were',ibergs_in_file,   &
          &                                    ' bergs in the restart file and', jn,' bergs have been read'
+      ! Close file
+      CALL iom_close( ncid )
       !
       ! Confirm that all areas have a suitable base for assigning new iceberg
@@ -189,7 +189,10 @@
       !
       INTEGER ::   jn   ! dummy loop index
+      INTEGER ::   idg  ! number of digits
       INTEGER ::   ix_dim, iy_dim, ik_dim, in_dim
       CHARACTER(len=256)     :: cl_path
       CHARACTER(len=256)     :: cl_filename
+      CHARACTER(len=8  )     :: cl_kt
+      CHARACTER(LEN=12 )     :: clfmt            ! writing format
       TYPE(iceberg), POINTER :: this
       TYPE(point)  , POINTER :: pt
@@ -204,11 +207,20 @@
          ! Only operate on the restart timestep itself.
          ! Assume we write iceberg restarts to same directory as ocean restarts.
-         cl_path = TRIM(cn_ocerst_outdir)
+         !
+         ! directory name
+         cl_path = TRIM(cn_icbrst_outdir)
          IF( cl_path(LEN_TRIM(cl_path):) /= '/' ) cl_path = TRIM(cl_path) // '/'
+         !
+         ! file name
+         WRITE(cl_kt, '(i8.8)') kt
+         cl_filename = TRIM(cexper)//"_"//cl_kt//"_"//TRIM(cn_icbrst_out)
          IF( lk_mpp ) THEN
-            WRITE(cl_filename,'(A,"_icebergs_",I8.8,"_restart_",I4.4,".nc")') TRIM(cexper), kt, narea-1
+            idg = MAX( INT(LOG10(REAL(MAX(1,jpnij-1),wp))) + 1, 4 )          ! how many digits to we need to write? min=4, max=9
+            WRITE(clfmt, "('(a,a,i', i1, '.', i1, ',a)')") idg, idg          ! '(a,a,ix.x,a)'
+            WRITE(cl_filename,  clfmt) TRIM(cl_filename), '_', narea-1, '.nc'
          ELSE
-            WRITE(cl_filename,'(A,"_icebergs_",I8.8,"_restart.nc")') TRIM(cexper), kt
+            WRITE(cl_filename,'(a,a)') TRIM(cl_filename),               '.nc'
          ENDIF
+
          IF ( lwp .AND. nn_verbose_level >= 0) WRITE(numout,'(2a)') 'icebergs, write_restart: creating ',  &
            &                                                         TRIM(cl_path)//TRIM(cl_filename)
@@ -218,8 +230,8 @@
    
          ! Dimensions
-         nret = NF90_DEF_DIM(ncid, 'x', jpi, ix_dim)
+         nret = NF90_DEF_DIM(ncid, 'x', Ni_0, ix_dim)
          IF (nret .ne. NF90_NOERR) CALL ctl_stop('icebergs, write_restart: nf_def_dim x failed')
    
-         nret = NF90_DEF_DIM(ncid, 'y', jpj, iy_dim)
+         nret = NF90_DEF_DIM(ncid, 'y', Nj_0, iy_dim)
          IF (nret .ne. NF90_NOERR) CALL ctl_stop('icebergs, write_restart: nf_def_dim y failed')
    
@@ -233,14 +245,14 @@
          IF( lk_mpp ) THEN
             ! Set domain parameters (assume jpdom_local_full)
-            nret = NF90_PUT_ATT( ncid, NF90_GLOBAL, 'DOMAIN_number_total'   , jpnij              )
-            nret = NF90_PUT_ATT( ncid, NF90_GLOBAL, 'DOMAIN_number'         , narea-1            )
-            nret = NF90_PUT_ATT( ncid, NF90_GLOBAL, 'DOMAIN_dimensions_ids' , (/1     , 2     /) )
-            nret = NF90_PUT_ATT( ncid, NF90_GLOBAL, 'DOMAIN_size_global'    , (/jpiglo, jpjglo/) )
-            nret = NF90_PUT_ATT( ncid, NF90_GLOBAL, 'DOMAIN_size_local'     , (/jpi   , jpj   /) )
-            nret = NF90_PUT_ATT( ncid, NF90_GLOBAL, 'DOMAIN_position_first' , (/nimpp , njmpp /) )
-            nret = NF90_PUT_ATT( ncid, NF90_GLOBAL, 'DOMAIN_position_last'  , (/nimpp + jpi - 1 , njmpp + jpj - 1  /) )
-            nret = NF90_PUT_ATT( ncid, NF90_GLOBAL, 'DOMAIN_halo_size_start', (/nldi - 1        , nldj - 1         /) )
-            nret = NF90_PUT_ATT( ncid, NF90_GLOBAL, 'DOMAIN_halo_size_end'  , (/jpi - nlei      , jpj - nlej       /) )
-            nret = NF90_PUT_ATT( ncid, NF90_GLOBAL, 'DOMAIN_type'           , 'BOX'              )
+            nret = NF90_PUT_ATT( ncid, NF90_GLOBAL, 'DOMAIN_number_total'   , jpnij                        )
+            nret = NF90_PUT_ATT( ncid, NF90_GLOBAL, 'DOMAIN_number'         , narea-1                      )
+            nret = NF90_PUT_ATT( ncid, NF90_GLOBAL, 'DOMAIN_dimensions_ids' , (/ 1         , 2          /) )
+            nret = NF90_PUT_ATT( ncid, NF90_GLOBAL, 'DOMAIN_size_global'    , (/ Ni0glo    , Nj0glo     /) )
+            nret = NF90_PUT_ATT( ncid, NF90_GLOBAL, 'DOMAIN_size_local'     , (/ Ni_0      , Nj_0       /) )
+            nret = NF90_PUT_ATT( ncid, NF90_GLOBAL, 'DOMAIN_position_first' , (/ mig0(Nis0), mjg0(Njs0) /) )
+            nret = NF90_PUT_ATT( ncid, NF90_GLOBAL, 'DOMAIN_position_last'  , (/ mig0(Nie0), mjg0(Nje0) /) )
+            nret = NF90_PUT_ATT( ncid, NF90_GLOBAL, 'DOMAIN_halo_size_start', (/ 0         , 0          /) )
+            nret = NF90_PUT_ATT( ncid, NF90_GLOBAL, 'DOMAIN_halo_size_end'  , (/ 0         , 0          /) )
+            nret = NF90_PUT_ATT( ncid, NF90_GLOBAL, 'DOMAIN_type'           , 'BOX'                        )
          ENDIF
          
@@ -333,12 +345,11 @@
          nstrt3(1) = 1
          nstrt3(2) = 1
-         nlngth3(1) = jpi
-         nlngth3(2) = jpj
+         nlngth3(1) = Ni_0
+         nlngth3(2) = Nj_0
          nlngth3(3) = 1
    
          DO jn=1,nclasses
-            griddata(:,:,1) = berg_grid%stored_ice(:,:,jn)
             nstrt3(3) = jn
-            nret = NF90_PUT_VAR( ncid, nsiceid, griddata, nstrt3, nlngth3 )
+            nret = NF90_PUT_VAR( ncid, nsiceid, berg_grid%stored_ice(Nis0:Nie0,Njs0:Nje0,jn), nstrt3, nlngth3 )
             IF (nret .ne. NF90_NOERR) THEN
                IF( lwp ) WRITE(numout,*) TRIM(NF90_STRERROR( nret ))
@@ -351,11 +362,11 @@
          IF (nret .ne. NF90_NOERR) CALL ctl_stop('icebergs, write_restart: nf_put_var kount failed')
    
-         nret = NF90_PUT_VAR( ncid, nsheatid, berg_grid%stored_heat(:,:) )
+         nret = NF90_PUT_VAR( ncid, nsheatid, berg_grid%stored_heat(Nis0:Nie0,Njs0:Nje0) )
          IF (nret .ne. NF90_NOERR) CALL ctl_stop('icebergs, write_restart: nf_put_var stored_heat failed')
          IF( lwp ) WRITE(numout,*) 'file: ',TRIM(cl_path)//TRIM(cl_filename),' var: stored_heat written'
    
-         nret = NF90_PUT_VAR( ncid, ncalvid , src_calving(:,:) )
+         nret = NF90_PUT_VAR( ncid, ncalvid , src_calving(Nis0:Nie0,Njs0:Nje0) )
          IF (nret .ne. NF90_NOERR) CALL ctl_stop('icebergs, write_restart: nf_put_var calving failed')
-         nret = NF90_PUT_VAR( ncid, ncalvhid, src_calving_hflx(:,:) )
+         nret = NF90_PUT_VAR( ncid, ncalvhid, src_calving_hflx(Nis0:Nie0,Njs0:Nje0) )
          IF (nret .ne. NF90_NOERR) CALL ctl_stop('icebergs, write_restart: nf_put_var calving_hflx failed')
          IF( lwp ) WRITE(numout,*) 'file: ',TRIM(cl_path)//TRIM(cl_filename),' var: calving written'
