Index: branches/2012/dev_r3337_NOCS10_ICB/NEMOGCM/NEMO/OPA_SRC/ICB/icbrst.F90
===================================================================
--- branches/2012/dev_r3337_NOCS10_ICB/NEMOGCM/NEMO/OPA_SRC/ICB/icbrst.F90	(revision 3339)
+++ branches/2012/dev_r3337_NOCS10_ICB/NEMOGCM/NEMO/OPA_SRC/ICB/icbrst.F90	(revision 3359)
@@ -29,13 +29,14 @@
 
    REAL(wp), DIMENSION(:,:,:), PRIVATE, POINTER :: griddata => NULL()    ! need 2d array to read in with
-   INTEGER,  DIMENSION(3),     PRIVATE          :: strt3, lngth3
-   INTEGER,                    PRIVATE          :: lonid, latid, xid, yid, uvelid, vvelid
-   INTEGER,                    PRIVATE          :: massid, thicknessid, widthid, lengthid
-   INTEGER,                    PRIVATE          :: yearid, dayid
-   INTEGER,                    PRIVATE          :: scaling_id, mass_of_bits_id, heat_density_id, numberid
-   INTEGER,                    PRIVATE          :: siceid, sheatid, calvid, calvhid, kountid
-
-   PUBLIC   icebergs_read_restart       ! routine called in xxx.F90 module
-   PUBLIC   icebergs_write_restart      ! routine called in xxx.F90 module
+   INTEGER,  DIMENSION(3),     PRIVATE          :: nstrt3, nlngth3
+   INTEGER,                    PRIVATE          :: nlonid, nlatid, nxid, nyid, nuvelid, nvvelid
+   INTEGER,                    PRIVATE          :: nmassid, nthicknessid, nwidthid, nlengthid
+   INTEGER,                    PRIVATE          :: nyearid, ndayid
+   INTEGER,                    PRIVATE          :: nscaling_id, nmass_of_bits_id, nheat_density_id, numberid
+   INTEGER,                    PRIVATE          :: nsiceid, nsheatid, ncalvid, ncalvhid, nkountid
+   INTEGER,                    PRIVATE          :: nret, ncid, nc_dim
+
+   PUBLIC   icebergs_read_restart       ! routine called in icbini.F90 module
+   PUBLIC   icebergs_write_restart      ! routine called in icbrun.F90 module
 
 CONTAINS
@@ -47,122 +48,122 @@
 
       ! Local variables
-      INTEGER                               :: ndim, nvar, natt
-      INTEGER                               :: k, iret, ncid, unlim_dim, x_dim, y_dim, c_dim, nbergs_in_file
-      INTEGER                               :: mclass
-      INTEGER, DIMENSION(1)                 :: strt, lngth, idata
-      INTEGER, DIMENSION(2)                 :: strt2, lngth2
-      INTEGER, DIMENSION(nkounts)           :: idata2
-      REAL(wp), DIMENSION(1)                :: data                                         ! need 1d array to read in with
+      INTEGER                               ::   idim, ivar, iatt
+      INTEGER                               ::   jn, iunlim_dim, ibergs_in_file
+      INTEGER                               ::   iclass
+      INTEGER, DIMENSION(1)                 ::   istrt, ilngth, idata
+      INTEGER, DIMENSION(2)                 ::   istrt2, ilngth2
+      INTEGER, DIMENSION(nkounts)           ::   idata2
+      REAL(wp), DIMENSION(1)                ::   zdata                                         ! need 1d array to read in with
                                                                                             ! start and count arrays
-      LOGICAL                               :: lres, found_restart, multiPErestart=.FALSE.
-      CHARACTER(len=80)                     :: filename
-      CHARACTER(len=NF90_MAX_NAME)          :: dname
-      TYPE(iceberg)                         :: localberg ! NOT a pointer but an actual local variable
-      TYPE(point)                           :: localpt   ! NOT a pointer but an actual local variable
+      LOGICAL                               ::   ll_found_restart, ll_multiPErestart=.FALSE.
+      CHARACTER(len=80)                     ::   cl_filename
+      CHARACTER(len=NF90_MAX_NAME)          ::   cl_dname
+      TYPE(iceberg)                         ::   localberg ! NOT a pointer but an actual local variable
+      TYPE(point)                           ::   localpt   ! NOT a pointer but an actual local variable
 
       IF( .NOT. ASSOCIATED(griddata) ) ALLOCATE( griddata(jpi,jpj,1) )
 
       ! Find a restart file
-      multiPErestart=.FALSE.
+      ll_multiPErestart=.FALSE.
       DO
-         filename = ' '
-         filename = 'restart_icebergs.nc'
-         INQUIRE(file=TRIM(filename),exist=found_restart)
-         IF (found_restart) EXIT
-         filename = ' '
-         WRITE(filename,'("restart_icebergs_",I4.4,".nc")') narea-1
-         INQUIRE(file=TRIM(filename),exist=found_restart)
-         IF (found_restart) THEN
-            multiPErestart=.TRUE.
+         cl_filename = ' '
+         cl_filename = 'restart_icebergs.nc'
+         INQUIRE( file=TRIM(cl_filename), exist=ll_found_restart )
+         IF ( ll_found_restart ) EXIT
+         cl_filename = ' '
+         WRITE( cl_filename, '("restart_icebergs_",I4.4,".nc")' ) narea-1
+         INQUIRE( file=TRIM(cl_filename), exist=ll_found_restart )
+         IF ( ll_found_restart ) THEN
+            ll_multiPErestart = .TRUE.
             EXIT
          ENDIF
          IF (nn_verbose_level >= 0 .AND. lwp) &
-            WRITE(numout,'(a)') 'read_restart_bergs: no restart file found'
-         multiPErestart=.TRUE. ! force sanity checking in a multi-PE mode if no file found on this PE
+            WRITE( numout, '(a)' ) 'read_restart_bergs: no restart file found'
+         ll_multiPErestart = .TRUE.                         ! force checking in a MPP if no file found on this PE
          EXIT
       ENDDO
 
-      IF ( .NOT. found_restart) THEN ! only do the following if a file was found
+      IF ( .NOT. ll_found_restart) THEN                     ! only do the following if a file was found
          CALL ctl_stop('icebergs: no restart file found')
       ENDIF
 
       IF (nn_verbose_level >= 0 .AND. lwp)  &
-         WRITE(numout,'(2a)') 'icebergs, read_restart_bergs: found restart file = ',TRIM(filename)
-
-      iret = NF90_OPEN(TRIM(filename), NF90_NOWRITE, ncid)
-      IF (iret .ne. NF90_NOERR) CALL ctl_stop('icebergs, read_restart_bergs: nf_open failed')
-
-      iret = nf90_inquire(ncid, ndim, nvar, natt, unlim_dim)
-      IF (iret .ne. NF90_NOERR) CALL ctl_stop('icebergs, read_restart_bergs: nf_inquire failed')
-
-      IF( unlim_dim .NE. -1) THEN
-
-         iret = nf90_inquire_dimension(ncid, unlim_dim, dname, nbergs_in_file)
-         IF (iret .ne. NF90_NOERR) CALL ctl_stop('icebergs, read_restart_bergs: nf_inq_dimlen failed')
-
-         iret = NF90_INQ_VARID(ncid, 'number', numberid)
-         iret = NF90_INQ_VARID(ncid, 'mass_scaling', scaling_id)
-         iret = NF90_INQ_VARID(ncid, 'xi', xid)
-         iret = NF90_INQ_VARID(ncid, 'yj', yid)
-         iret = NF90_INQ_VARID(ncid, 'lon', lonid)
-         iret = NF90_INQ_VARID(ncid, 'lat', latid)
-         iret = NF90_INQ_VARID(ncid, 'uvel', uvelid)
-         iret = NF90_INQ_VARID(ncid, 'vvel', vvelid)
-         iret = NF90_INQ_VARID(ncid, 'mass', massid)
-         iret = NF90_INQ_VARID(ncid, 'thickness', thicknessid)
-         iret = NF90_INQ_VARID(ncid, 'width', widthid)
-         iret = NF90_INQ_VARID(ncid, 'length', lengthid)
-         iret = NF90_INQ_VARID(ncid, 'year', yearid)
-         iret = NF90_INQ_VARID(ncid, 'day', dayid)
-         iret = NF90_INQ_VARID(ncid, 'mass_of_bits', mass_of_bits_id)
-         iret = NF90_INQ_VARID(ncid, 'heat_density', heat_density_id)
-
-         lngth(1) = 1
-         strt2(1) = 1
-         lngth2(1) = nkounts
-         lngth2(2) = 1
-         DO k=1, nbergs_in_file
-
-            strt(1) = k
-            strt2(2) = k
-
-            iret = NF90_GET_VAR(ncid, numberid, idata2, strt2, lngth2 )
+         WRITE(numout,'(2a)') 'icebergs, read_restart_bergs: found restart file = ',TRIM(cl_filename)
+
+      nret = NF90_OPEN(TRIM(cl_filename), NF90_NOWRITE, ncid)
+      IF (nret .ne. NF90_NOERR) CALL ctl_stop('icebergs, read_restart_bergs: nf_open failed')
+
+      nret = nf90_inquire(ncid, idim, ivar, iatt, iunlim_dim)
+      IF (nret .ne. NF90_NOERR) CALL ctl_stop('icebergs, read_restart_bergs: nf_inquire failed')
+
+      IF( iunlim_dim .NE. -1) THEN
+
+         nret = nf90_inquire_dimension(ncid, iunlim_dim, cl_dname, ibergs_in_file)
+         IF (nret .ne. NF90_NOERR) CALL ctl_stop('icebergs, read_restart_bergs: nf_inq_dimlen failed')
+
+         nret = NF90_INQ_VARID(ncid, 'number', numberid)
+         nret = NF90_INQ_VARID(ncid, 'mass_scaling', nscaling_id)
+         nret = NF90_INQ_VARID(ncid, 'xi', nxid)
+         nret = NF90_INQ_VARID(ncid, 'yj', nyid)
+         nret = NF90_INQ_VARID(ncid, 'lon', nlonid)
+         nret = NF90_INQ_VARID(ncid, 'lat', nlatid)
+         nret = NF90_INQ_VARID(ncid, 'uvel', nuvelid)
+         nret = NF90_INQ_VARID(ncid, 'vvel', nvvelid)
+         nret = NF90_INQ_VARID(ncid, 'mass', nmassid)
+         nret = NF90_INQ_VARID(ncid, 'thickness', nthicknessid)
+         nret = NF90_INQ_VARID(ncid, 'width', nwidthid)
+         nret = NF90_INQ_VARID(ncid, 'length', nlengthid)
+         nret = NF90_INQ_VARID(ncid, 'year', nyearid)
+         nret = NF90_INQ_VARID(ncid, 'day', ndayid)
+         nret = NF90_INQ_VARID(ncid, 'mass_of_bits', nmass_of_bits_id)
+         nret = NF90_INQ_VARID(ncid, 'heat_density', nheat_density_id)
+
+         ilngth(1) = 1
+         istrt2(1) = 1
+         ilngth2(1) = nkounts
+         ilngth2(2) = 1
+         DO jn=1, ibergs_in_file
+
+            istrt(1) = jn
+            istrt2(2) = jn
+
+            nret = NF90_GET_VAR(ncid, numberid, idata2, istrt2, ilngth2 )
             localberg%number(:) = idata2(:)
 
-            iret = NF90_GET_VAR(ncid, scaling_id, data, strt, lngth )
-            localberg%mass_scaling = data(1)
-
-            iret = NF90_GET_VAR(ncid, lonid, data, strt, lngth)
-            localpt%lon = data(1)
-            iret = NF90_GET_VAR(ncid, latid, data, strt, lngth)
-            localpt%lat = data(1)
+            nret = NF90_GET_VAR(ncid, nscaling_id, zdata, istrt, ilngth )
+            localberg%mass_scaling = zdata(1)
+
+            nret = NF90_GET_VAR(ncid, nlonid, zdata, istrt, ilngth)
+            localpt%lon = zdata(1)
+            nret = NF90_GET_VAR(ncid, nlatid, zdata, istrt, ilngth)
+            localpt%lat = zdata(1)
             IF (nn_verbose_level >= 2 .AND. lwp) THEN
-               WRITE(numout,'(a,i5,a,2f10.4,a,i5)') 'icebergs, read_restart_bergs: berg ',k,' is at ', &
+               WRITE(numout,'(a,i5,a,2f10.4,a,i5)') 'icebergs, read_restart_bergs: berg ',jn,' is at ', &
                                               localpt%lon,localpt%lat,' on PE ',narea-1
             ENDIF
-            iret = NF90_GET_VAR(ncid, xid, data, strt, lngth)
-            localpt%xi = data(1)
-            iret = NF90_GET_VAR(ncid, yid, data, strt, lngth)
-            localpt%yj = data(1)
-            iret = NF90_GET_VAR(ncid, uvelid, data, strt, lngth )
-            localpt%uvel = data(1)
-            iret = NF90_GET_VAR(ncid, vvelid, data, strt, lngth )
-            localpt%vvel = data(1)
-            iret = NF90_GET_VAR(ncid, massid, data, strt, lngth )
-            localpt%mass = data(1)
-            iret = NF90_GET_VAR(ncid, thicknessid, data, strt, lngth )
-            localpt%thickness = data(1)
-            iret = NF90_GET_VAR(ncid, widthid, data, strt, lngth )
-            localpt%width = data(1)
-            iret = NF90_GET_VAR(ncid, lengthid, data, strt, lngth )
-            localpt%length = data(1)
-            iret = NF90_GET_VAR(ncid, yearid, idata, strt, lngth )
+            nret = NF90_GET_VAR(ncid, nxid, zdata, istrt, ilngth)
+            localpt%xi = zdata(1)
+            nret = NF90_GET_VAR(ncid, nyid, zdata, istrt, ilngth)
+            localpt%yj = zdata(1)
+            nret = NF90_GET_VAR(ncid, nuvelid, zdata, istrt, ilngth )
+            localpt%uvel = zdata(1)
+            nret = NF90_GET_VAR(ncid, nvvelid, zdata, istrt, ilngth )
+            localpt%vvel = zdata(1)
+            nret = NF90_GET_VAR(ncid, nmassid, zdata, istrt, ilngth )
+            localpt%mass = zdata(1)
+            nret = NF90_GET_VAR(ncid, nthicknessid, zdata, istrt, ilngth )
+            localpt%thickness = zdata(1)
+            nret = NF90_GET_VAR(ncid, nwidthid, zdata, istrt, ilngth )
+            localpt%width = zdata(1)
+            nret = NF90_GET_VAR(ncid, nlengthid, zdata, istrt, ilngth )
+            localpt%length = zdata(1)
+            nret = NF90_GET_VAR(ncid, nyearid, idata, istrt, ilngth )
             localpt%year = idata(1)
-            iret = NF90_GET_VAR(ncid, dayid, data, strt, lngth )
-            localpt%day = data(1)
-            iret = NF90_GET_VAR(ncid, mass_of_bits_id, data, strt, lngth )
-            localpt%mass_of_bits = data(1)
-            iret = NF90_GET_VAR(ncid, heat_density_id, data, strt, lngth )
-            localpt%heat_density = data(1)
+            nret = NF90_GET_VAR(ncid, ndayid, zdata, istrt, ilngth )
+            localpt%day = zdata(1)
+            nret = NF90_GET_VAR(ncid, nmass_of_bits_id, zdata, istrt, ilngth )
+            localpt%mass_of_bits = zdata(1)
+            nret = NF90_GET_VAR(ncid, nheat_density_id, zdata, istrt, ilngth )
+            localpt%heat_density = zdata(1)
             !
             CALL add_new_berg_to_list( localberg, localpt )
@@ -171,52 +172,52 @@
       ENDIF
 
-      iret = NF90_INQ_DIMID( ncid, 'c', c_dim )
-      IF (iret .ne. NF90_NOERR) CALL ctl_stop('icebergs, read_restart: nf_inq_dimid c failed')
-
-      iret = NF90_INQUIRE_DIMENSION( ncid, c_dim, dname, mclass )
-      IF (iret .ne. NF90_NOERR) CALL ctl_stop('icebergs, read_restart: nf_inquire_dimension failed')
-
-      iret = NF90_INQ_VARID(ncid, 'kount'       , kountid)
-      iret = NF90_INQ_VARID(ncid, 'calving'     , calvid)
-      iret = NF90_INQ_VARID(ncid, 'calving_hflx', calvhid)
-      iret = NF90_INQ_VARID(ncid, 'stored_ice'  , siceid)
-      iret = NF90_INQ_VARID(ncid, 'stored_heat' , sheatid)
-
-      strt3(1) = 1
-      strt3(2) = 1
-      lngth3(1) = jpi
-      lngth3(2) = jpj
-      lngth3(3) = 1
-
-      DO k=1,mclass
-
-         strt3(3) = k
-
-         iret = NF90_GET_VAR( ncid, siceid , griddata, strt3, lngth3 )
-         berg_grid%stored_ice(:,:,k) = griddata(:,:,1)
+      nret = NF90_INQ_DIMID( ncid, 'c', nc_dim )
+      IF (nret .ne. NF90_NOERR) CALL ctl_stop('icebergs, read_restart: nf_inq_dimid c failed')
+
+      nret = NF90_INQUIRE_DIMENSION( ncid, nc_dim, cl_dname, iclass )
+      IF (nret .ne. NF90_NOERR) CALL ctl_stop('icebergs, read_restart: nf_inquire_dimension failed')
+
+      nret = NF90_INQ_VARID(ncid, 'kount'       , nkountid)
+      nret = NF90_INQ_VARID(ncid, 'calving'     , ncalvid)
+      nret = NF90_INQ_VARID(ncid, 'calving_hflx', ncalvhid)
+      nret = NF90_INQ_VARID(ncid, 'stored_ice'  , nsiceid)
+      nret = NF90_INQ_VARID(ncid, 'stored_heat' , nsheatid)
+
+      nstrt3(1) = 1
+      nstrt3(2) = 1
+      nlngth3(1) = jpi
+      nlngth3(2) = jpj
+      nlngth3(3) = 1
+
+      DO jn=1,iclass
+
+         nstrt3(3) = jn
+
+         nret = NF90_GET_VAR( ncid, nsiceid , griddata, nstrt3, nlngth3 )
+         berg_grid%stored_ice(:,:,jn) = griddata(:,:,1)
 
       ENDDO
 
-      iret = NF90_GET_VAR( ncid, calvid , p_calving(:,:) )
-      iret = NF90_GET_VAR( ncid, calvhid, p_calving_hflx(:,:) )
-      iret = NF90_GET_VAR( ncid, sheatid, berg_grid%stored_heat(:,:) )
-      iret = NF90_GET_VAR( ncid, kountid, idata2(:) )
-      kount_bergs(:) = idata2(:)
+      nret = NF90_GET_VAR( ncid, ncalvid , src_calving(:,:) )
+      nret = NF90_GET_VAR( ncid, ncalvhid, src_calving_hflx(:,:) )
+      nret = NF90_GET_VAR( ncid, nsheatid, berg_grid%stored_heat(:,:) )
+      nret = NF90_GET_VAR( ncid, nkountid, idata2(:) )
+      num_bergs(:) = idata2(:)
 
       ! Finish up
-      iret = NF90_CLOSE(ncid)
-      IF (iret .ne. NF90_NOERR) CALL ctl_stop('icebergs, read_restart: nf_close failed')
+      nret = NF90_CLOSE(ncid)
+      IF (nret .ne. NF90_NOERR) CALL ctl_stop('icebergs, read_restart: nf_close failed')
 
       ! Sanity check
-      k = count_bergs()
+      jn = count_bergs()
       IF (nn_verbose_level >= 0)   &
-         WRITE(numout,'(2(a,i5))') 'icebergs, read_restart_bergs: # bergs =',k,' on PE',narea-1
+         WRITE(numout,'(2(a,i5))') 'icebergs, read_restart_bergs: # bergs =',jn,' on PE',narea-1
       IF( lk_mpp ) THEN
-         IF (multiPErestart) CALL mpp_sum(nbergs_in_file) ! In case PE 0 didn't open a file
-         CALL mpp_sum(k)
+         IF (ll_multiPErestart) CALL mpp_sum(ibergs_in_file) ! In case PE 0 didn't open a file
+         CALL mpp_sum(jn)
       ENDIF
       IF ( lwp ) THEN
-         WRITE(numout,'(a,i5,a,i5,a)') 'icebergs, read_restart_bergs: there were',nbergs_in_file, &
-                                       ' bergs in the restart file and', k,' bergs have been read'
+         WRITE(numout,'(a,i5,a,i5,a)') 'icebergs, read_restart_bergs: there were',ibergs_in_file, &
+                                       ' bergs in the restart file and', jn,' bergs have been read'
       ENDIF
 
@@ -232,6 +233,7 @@
       INTEGER, INTENT( in )                 :: kt
       ! Local variables
-      INTEGER                               :: iret, ncid, k_dim, n_dim, x_dim, y_dim, c_dim, k
-      CHARACTER(len=80)                     :: filename
+      INTEGER,                              :: ix_dim, iy_dim, ik_dim, in_dim
+      INTEGER                               :: jn
+      CHARACTER(len=80)                     :: cl_filename
       TYPE(iceberg), POINTER                :: this
       TYPE(point)  , POINTER                :: pt
@@ -239,44 +241,44 @@
       IF( .NOT. ASSOCIATED(griddata) ) ALLOCATE( griddata(jpi,jpj,1) )
 
-      WRITE(filename,'("icebergs_",I8.8,"_restart_",I4.4,".nc")') kt, narea-1
-      IF (nn_verbose_level >= 0) WRITE(numout,'(2a)') 'icebergs, write_restart: creating ',TRIM(filename)
-
-      iret = NF90_CREATE(TRIM(filename), NF90_CLOBBER, ncid)
-      IF (iret .ne. NF90_NOERR) CALL ctl_stop('icebergs, write_restart: nf_create failed')
+      WRITE(cl_filename,'("icebergs_",I8.8,"_restart_",I4.4,".nc")') kt, narea-1
+      IF (nn_verbose_level >= 0) WRITE(numout,'(2a)') 'icebergs, write_restart: creating ',TRIM(cl_filename)
+
+      nret = NF90_CREATE(TRIM(cl_filename), NF90_CLOBBER, ncid)
+      IF (nret .ne. NF90_NOERR) CALL ctl_stop('icebergs, write_restart: nf_create failed')
 
       ! Dimensions
-      iret = NF90_DEF_DIM(ncid, 'x', jpi, x_dim)
-      IF (iret .ne. NF90_NOERR) CALL ctl_stop('icebergs, write_restart: nf_def_dim x failed')
-
-      iret = NF90_DEF_DIM(ncid, 'y', jpj, y_dim)
-      IF (iret .ne. NF90_NOERR) CALL ctl_stop('icebergs, write_restart: nf_def_dim y failed')
-
-      iret = NF90_DEF_DIM(ncid, 'c', nclasses, c_dim)
-      IF (iret .ne. NF90_NOERR) CALL ctl_stop('icebergs, write_restart: nf_def_dim c failed')
-
-      iret = NF90_DEF_DIM(ncid, 'k', nkounts, k_dim)
-      IF (iret .ne. NF90_NOERR) CALL ctl_stop('icebergs, write_restart: nf_def_dim k failed')
+      nret = NF90_DEF_DIM(ncid, 'x', jpi, 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)
+      IF (nret .ne. NF90_NOERR) CALL ctl_stop('icebergs, write_restart: nf_def_dim y failed')
+
+      nret = NF90_DEF_DIM(ncid, 'c', nclasses, nc_dim)
+      IF (nret .ne. NF90_NOERR) CALL ctl_stop('icebergs, write_restart: nf_def_dim c failed')
+
+      nret = NF90_DEF_DIM(ncid, 'k', nkounts, ik_dim)
+      IF (nret .ne. NF90_NOERR) CALL ctl_stop('icebergs, write_restart: nf_def_dim k failed')
 
       IF (associated(first_berg)) then
-         iret = NF90_DEF_DIM(ncid, 'n', NF90_UNLIMITED, n_dim)
-         IF (iret .ne. NF90_NOERR) CALL ctl_stop('icebergs, write_restart: nf_def_dim n failed')
+         nret = NF90_DEF_DIM(ncid, 'n', NF90_UNLIMITED, in_dim)
+         IF (nret .ne. NF90_NOERR) CALL ctl_stop('icebergs, write_restart: nf_def_dim n failed')
       ENDIF
 
       ! Variables
-      iret = NF90_DEF_VAR(ncid, 'kount'       , NF90_INT   , (/ k_dim /), kountid)
-      iret = NF90_DEF_VAR(ncid, 'calving'     , NF90_DOUBLE, (/ x_dim, y_dim /), calvid)
-      iret = NF90_DEF_VAR(ncid, 'calving_hflx', NF90_DOUBLE, (/ x_dim, y_dim /), calvhid)
-      iret = NF90_DEF_VAR(ncid, 'stored_ice'  , NF90_DOUBLE, (/ x_dim, y_dim, c_dim /), siceid)
-      iret = NF90_DEF_VAR(ncid, 'stored_heat' , NF90_DOUBLE, (/ x_dim, y_dim /), sheatid)
+      nret = NF90_DEF_VAR(ncid, 'kount'       , NF90_INT   , (/ ik_dim /), nkountid)
+      nret = NF90_DEF_VAR(ncid, 'calving'     , NF90_DOUBLE, (/ ix_dim, iy_dim /), ncalvid)
+      nret = NF90_DEF_VAR(ncid, 'calving_hflx', NF90_DOUBLE, (/ ix_dim, iy_dim /), ncalvhid)
+      nret = NF90_DEF_VAR(ncid, 'stored_ice'  , NF90_DOUBLE, (/ ix_dim, iy_dim, nc_dim /), nsiceid)
+      nret = NF90_DEF_VAR(ncid, 'stored_heat' , NF90_DOUBLE, (/ ix_dim, iy_dim /), nsheatid)
 
       ! Attributes
-      iret = NF90_PUT_ATT(ncid, calvid , 'long_name', 'iceberg calving')
-      iret = NF90_PUT_ATT(ncid, calvid , 'units', 'some')
-      iret = NF90_PUT_ATT(ncid, calvhid, 'long_name', 'heat flux associated with iceberg calving')
-      iret = NF90_PUT_ATT(ncid, calvhid, 'units', 'some')
-      iret = NF90_PUT_ATT(ncid, siceid , 'long_name', 'stored ice used to calve icebergs')
-      iret = NF90_PUT_ATT(ncid, siceid , 'units', 'kg/s')
-      iret = NF90_PUT_ATT(ncid, sheatid, 'long_name', 'heat in stored ice used to calve icebergs')
-      iret = NF90_PUT_ATT(ncid, sheatid, 'units', 'J/kg/s')
+      nret = NF90_PUT_ATT(ncid, ncalvid , 'long_name', 'iceberg calving')
+      nret = NF90_PUT_ATT(ncid, ncalvid , 'units', 'some')
+      nret = NF90_PUT_ATT(ncid, ncalvhid, 'long_name', 'heat flux associated with iceberg calving')
+      nret = NF90_PUT_ATT(ncid, ncalvhid, 'units', 'some')
+      nret = NF90_PUT_ATT(ncid, nsiceid , 'long_name', 'stored ice used to calve icebergs')
+      nret = NF90_PUT_ATT(ncid, nsiceid , 'units', 'kg/s')
+      nret = NF90_PUT_ATT(ncid, nsheatid, 'long_name', 'heat in stored ice used to calve icebergs')
+      nret = NF90_PUT_ATT(ncid, nsheatid, 'units', 'J/kg/s')
 
       IF ( ASSOCIATED(first_berg) ) THEN
@@ -285,121 +287,121 @@
 
          ! Variables
-         iret = NF90_DEF_VAR(ncid, 'lon', NF90_DOUBLE, n_dim, lonid)
-         iret = NF90_DEF_VAR(ncid, 'lat', NF90_DOUBLE, n_dim, latid)
-         iret = NF90_DEF_VAR(ncid, 'xi', NF90_DOUBLE, n_dim, xid)
-         iret = NF90_DEF_VAR(ncid, 'yj', NF90_DOUBLE, n_dim, yid)
-         iret = NF90_DEF_VAR(ncid, 'uvel', NF90_DOUBLE, n_dim, uvelid)
-         iret = NF90_DEF_VAR(ncid, 'vvel', NF90_DOUBLE, n_dim, vvelid)
-         iret = NF90_DEF_VAR(ncid, 'mass', NF90_DOUBLE, n_dim, massid)
-         iret = NF90_DEF_VAR(ncid, 'thickness', NF90_DOUBLE, n_dim, thicknessid)
-         iret = NF90_DEF_VAR(ncid, 'width', NF90_DOUBLE, n_dim, widthid)
-         iret = NF90_DEF_VAR(ncid, 'length', NF90_DOUBLE, n_dim, lengthid)
-         iret = NF90_DEF_VAR(ncid, 'number', NF90_INT, (/k_dim,n_dim/), numberid)
-         iret = NF90_DEF_VAR(ncid, 'year', NF90_INT, n_dim, yearid)
-         iret = NF90_DEF_VAR(ncid, 'day', NF90_DOUBLE, n_dim, dayid)
-         iret = NF90_DEF_VAR(ncid, 'mass_scaling', NF90_DOUBLE, n_dim, scaling_id)
-         iret = NF90_DEF_VAR(ncid, 'mass_of_bits', NF90_DOUBLE, n_dim, mass_of_bits_id)
-         iret = NF90_DEF_VAR(ncid, 'heat_density', NF90_DOUBLE, n_dim, heat_density_id)
+         nret = NF90_DEF_VAR(ncid, 'lon', NF90_DOUBLE, in_dim, nlonid)
+         nret = NF90_DEF_VAR(ncid, 'lat', NF90_DOUBLE, in_dim, nlatid)
+         nret = NF90_DEF_VAR(ncid, 'xi', NF90_DOUBLE, in_dim, nxid)
+         nret = NF90_DEF_VAR(ncid, 'yj', NF90_DOUBLE, in_dim, nyid)
+         nret = NF90_DEF_VAR(ncid, 'uvel', NF90_DOUBLE, in_dim, nuvelid)
+         nret = NF90_DEF_VAR(ncid, 'vvel', NF90_DOUBLE, in_dim, nvvelid)
+         nret = NF90_DEF_VAR(ncid, 'mass', NF90_DOUBLE, in_dim, nmassid)
+         nret = NF90_DEF_VAR(ncid, 'thickness', NF90_DOUBLE, in_dim, nthicknessid)
+         nret = NF90_DEF_VAR(ncid, 'width', NF90_DOUBLE, in_dim, nwidthid)
+         nret = NF90_DEF_VAR(ncid, 'length', NF90_DOUBLE, in_dim, nlengthid)
+         nret = NF90_DEF_VAR(ncid, 'number', NF90_INT, (/ik_dim,in_dim/), numberid)
+         nret = NF90_DEF_VAR(ncid, 'year', NF90_INT, in_dim, nyearid)
+         nret = NF90_DEF_VAR(ncid, 'day', NF90_DOUBLE, in_dim, ndayid)
+         nret = NF90_DEF_VAR(ncid, 'mass_scaling', NF90_DOUBLE, in_dim, nscaling_id)
+         nret = NF90_DEF_VAR(ncid, 'mass_of_bits', NF90_DOUBLE, in_dim, nmass_of_bits_id)
+         nret = NF90_DEF_VAR(ncid, 'heat_density', NF90_DOUBLE, in_dim, nheat_density_id)
 
          ! Attributes
-         iret = NF90_PUT_ATT(ncid, lonid, 'long_name', 'longitude')
-         iret = NF90_PUT_ATT(ncid, lonid, 'units', 'degrees_E')
-         iret = NF90_PUT_ATT(ncid, latid, 'long_name', 'latitude')
-         iret = NF90_PUT_ATT(ncid, latid, 'units', 'degrees_N')
-         iret = NF90_PUT_ATT(ncid, xid, 'long_name', 'x grid box position')
-         iret = NF90_PUT_ATT(ncid, xid, 'units', 'fractional')
-         iret = NF90_PUT_ATT(ncid, yid, 'long_name', 'y grid box position')
-         iret = NF90_PUT_ATT(ncid, yid, 'units', 'fractional')
-         iret = NF90_PUT_ATT(ncid, uvelid, 'long_name', 'zonal velocity')
-         iret = NF90_PUT_ATT(ncid, uvelid, 'units', 'm/s')
-         iret = NF90_PUT_ATT(ncid, vvelid, 'long_name', 'meridional velocity')
-         iret = NF90_PUT_ATT(ncid, vvelid, 'units', 'm/s')
-         iret = NF90_PUT_ATT(ncid, massid, 'long_name', 'mass')
-         iret = NF90_PUT_ATT(ncid, massid, 'units', 'kg')
-         iret = NF90_PUT_ATT(ncid, thicknessid, 'long_name', 'thickness')
-         iret = NF90_PUT_ATT(ncid, thicknessid, 'units', 'm')
-         iret = NF90_PUT_ATT(ncid, widthid, 'long_name', 'width')
-         iret = NF90_PUT_ATT(ncid, widthid, 'units', 'm')
-         iret = NF90_PUT_ATT(ncid, lengthid, 'long_name', 'length')
-         iret = NF90_PUT_ATT(ncid, lengthid, 'units', 'm')
-         iret = NF90_PUT_ATT(ncid, numberid, 'long_name', 'iceberg number on this processor')
-         iret = NF90_PUT_ATT(ncid, numberid, 'units', 'count')
-         iret = NF90_PUT_ATT(ncid, yearid, 'long_name', 'calendar year of calving event')
-         iret = NF90_PUT_ATT(ncid, yearid, 'units', 'years')
-         iret = NF90_PUT_ATT(ncid, dayid, 'long_name', 'year day of calving event')
-         iret = NF90_PUT_ATT(ncid, dayid, 'units', 'days')
-         iret = NF90_PUT_ATT(ncid, scaling_id, 'long_name', 'scaling factor for mass of calving berg')
-         iret = NF90_PUT_ATT(ncid, scaling_id, 'units', 'none')
-         iret = NF90_PUT_ATT(ncid, mass_of_bits_id, 'long_name', 'mass of bergy bits')
-         iret = NF90_PUT_ATT(ncid, mass_of_bits_id, 'units', 'kg')
-         iret = NF90_PUT_ATT(ncid, heat_density_id, 'long_name', 'heat density')
-         iret = NF90_PUT_ATT(ncid, heat_density_id, 'units', 'J/kg')
+         nret = NF90_PUT_ATT(ncid, nlonid, 'long_name', 'longitude')
+         nret = NF90_PUT_ATT(ncid, nlonid, 'units', 'degrees_E')
+         nret = NF90_PUT_ATT(ncid, nlatid, 'long_name', 'latitude')
+         nret = NF90_PUT_ATT(ncid, nlatid, 'units', 'degrees_N')
+         nret = NF90_PUT_ATT(ncid, nxid, 'long_name', 'x grid box position')
+         nret = NF90_PUT_ATT(ncid, nxid, 'units', 'fractional')
+         nret = NF90_PUT_ATT(ncid, nyid, 'long_name', 'y grid box position')
+         nret = NF90_PUT_ATT(ncid, nyid, 'units', 'fractional')
+         nret = NF90_PUT_ATT(ncid, nuvelid, 'long_name', 'zonal velocity')
+         nret = NF90_PUT_ATT(ncid, nuvelid, 'units', 'm/s')
+         nret = NF90_PUT_ATT(ncid, nvvelid, 'long_name', 'meridional velocity')
+         nret = NF90_PUT_ATT(ncid, nvvelid, 'units', 'm/s')
+         nret = NF90_PUT_ATT(ncid, nmassid, 'long_name', 'mass')
+         nret = NF90_PUT_ATT(ncid, nmassid, 'units', 'kg')
+         nret = NF90_PUT_ATT(ncid, nthicknessid, 'long_name', 'thickness')
+         nret = NF90_PUT_ATT(ncid, nthicknessid, 'units', 'm')
+         nret = NF90_PUT_ATT(ncid, nwidthid, 'long_name', 'width')
+         nret = NF90_PUT_ATT(ncid, nwidthid, 'units', 'm')
+         nret = NF90_PUT_ATT(ncid, nlengthid, 'long_name', 'length')
+         nret = NF90_PUT_ATT(ncid, nlengthid, 'units', 'm')
+         nret = NF90_PUT_ATT(ncid, numberid, 'long_name', 'iceberg number on this processor')
+         nret = NF90_PUT_ATT(ncid, numberid, 'units', 'count')
+         nret = NF90_PUT_ATT(ncid, nyearid, 'long_name', 'calendar year of calving event')
+         nret = NF90_PUT_ATT(ncid, nyearid, 'units', 'years')
+         nret = NF90_PUT_ATT(ncid, ndayid, 'long_name', 'year day of calving event')
+         nret = NF90_PUT_ATT(ncid, ndayid, 'units', 'days')
+         nret = NF90_PUT_ATT(ncid, nscaling_id, 'long_name', 'scaling factor for mass of calving berg')
+         nret = NF90_PUT_ATT(ncid, nscaling_id, 'units', 'none')
+         nret = NF90_PUT_ATT(ncid, nmass_of_bits_id, 'long_name', 'mass of bergy bits')
+         nret = NF90_PUT_ATT(ncid, nmass_of_bits_id, 'units', 'kg')
+         nret = NF90_PUT_ATT(ncid, nheat_density_id, 'long_name', 'heat density')
+         nret = NF90_PUT_ATT(ncid, nheat_density_id, 'units', 'J/kg')
 
       ENDIF ! associated(first_berg)
 
       ! End define mode
-      iret = NF90_ENDDEF(ncid)
+      nret = NF90_ENDDEF(ncid)
 
       ! --------------------------------
       ! now write some data
 
-      strt3(1) = 1
-      strt3(2) = 1
-      lngth3(1) = jpi
-      lngth3(2) = jpj
-      lngth3(3) = 1
-
-      DO k=1,nclasses
-         griddata(:,:,1) = berg_grid%stored_ice(:,:,k)
-         strt3(3) = k
-         iret = NF90_PUT_VAR( ncid, siceid, griddata, strt3, lngth3 )
-         IF (iret .ne. NF90_NOERR) THEN
-            IF( lwp ) WRITE(numout,*) TRIM(NF90_STRERROR( iret ))
+      nstrt3(1) = 1
+      nstrt3(2) = 1
+      nlngth3(1) = jpi
+      nlngth3(2) = jpj
+      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 )
+         IF (nret .ne. NF90_NOERR) THEN
+            IF( lwp ) WRITE(numout,*) TRIM(NF90_STRERROR( nret ))
             CALL ctl_stop('icebergs, write_restart: nf_put_var stored_ice failed')
          ENDIF
       ENDDO
-      IF( lwp ) WRITE(numout,*) 'file: ',TRIM(filename),' var: stored_ice  written'
-
-      iret = NF90_PUT_VAR( ncid, kountid, kount_bergs(:) )
-      IF (iret .ne. NF90_NOERR) CALL ctl_stop('icebergs, write_restart: nf_put_var kount failed')
-
-      iret = NF90_PUT_VAR( ncid, sheatid, berg_grid%stored_heat(:,:) )
-      IF (iret .ne. NF90_NOERR) CALL ctl_stop('icebergs, write_restart: nf_put_var stored_heat failed')
-      IF( lwp ) WRITE(numout,*) 'file: ',TRIM(filename),' var: stored_heat written'
-
-      iret = NF90_PUT_VAR( ncid, calvid , p_calving(:,:) )
-      IF (iret .ne. NF90_NOERR) CALL ctl_stop('icebergs, write_restart: nf_put_var calving failed')
-      iret = NF90_PUT_VAR( ncid, calvhid, p_calving_hflx(:,:) )
-      IF (iret .ne. NF90_NOERR) CALL ctl_stop('icebergs, write_restart: nf_put_var calving_hflx failed')
-      IF( lwp ) WRITE(numout,*) 'file: ',TRIM(filename),' var: calving written'
+      IF( lwp ) WRITE(numout,*) 'file: ',TRIM(cl_filename),' var: stored_ice  written'
+
+      nret = NF90_PUT_VAR( ncid, nkountid, num_bergs(:) )
+      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(:,:) )
+      IF (nret .ne. NF90_NOERR) CALL ctl_stop('icebergs, write_restart: nf_put_var stored_heat failed')
+      IF( lwp ) WRITE(numout,*) 'file: ',TRIM(cl_filename),' var: stored_heat written'
+
+      nret = NF90_PUT_VAR( ncid, ncalvid , src_calving(:,:) )
+      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(:,:) )
+      IF (nret .ne. NF90_NOERR) CALL ctl_stop('icebergs, write_restart: nf_put_var calving_hflx failed')
+      IF( lwp ) WRITE(numout,*) 'file: ',TRIM(cl_filename),' var: calving written'
 
       IF ( ASSOCIATED(first_berg) ) THEN
 
          ! Write variables
-         ! sga - just write out the current point of the trajectory
+         ! just write out the current point of the trajectory
 
          this => first_berg
-         k = 0
+         jn = 0
          DO WHILE (ASSOCIATED(this))
             pt => this%current_point
-            k=k+1
-
-            iret = NF90_PUT_VAR(ncid, numberid, this%number, (/1,k/), (/nkounts,1/) )
-            iret = NF90_PUT_VAR(ncid, scaling_id, this%mass_scaling, (/ k /) )
-
-            iret = NF90_PUT_VAR(ncid, lonid, pt%lon, (/ k /) )
-            iret = NF90_PUT_VAR(ncid, latid, pt%lat, (/ k /) )
-            iret = NF90_PUT_VAR(ncid, xid, pt%xi, (/ k /) )
-            iret = NF90_PUT_VAR(ncid, yid, pt%yj, (/ k /) )
-            iret = NF90_PUT_VAR(ncid, uvelid, pt%uvel, (/ k /) )
-            iret = NF90_PUT_VAR(ncid, vvelid, pt%vvel, (/ k /) )
-            iret = NF90_PUT_VAR(ncid, massid, pt%mass, (/ k /) )
-            iret = NF90_PUT_VAR(ncid, thicknessid, pt%thickness, (/ k /) )
-            iret = NF90_PUT_VAR(ncid, widthid, pt%width, (/ k /) )
-            iret = NF90_PUT_VAR(ncid, lengthid, pt%length, (/ k /) )
-            iret = NF90_PUT_VAR(ncid, yearid, pt%year, (/ k /) )
-            iret = NF90_PUT_VAR(ncid, dayid, pt%day, (/ k /) )
-            iret = NF90_PUT_VAR(ncid, mass_of_bits_id, pt%mass_of_bits, (/ k /) )
-            iret = NF90_PUT_VAR(ncid, heat_density_id, pt%heat_density, (/ k /) )
+            jn=jn+1
+
+            nret = NF90_PUT_VAR(ncid, numberid, this%number, (/1,jn/), (/nkounts,1/) )
+            nret = NF90_PUT_VAR(ncid, nscaling_id, this%mass_scaling, (/ jn /) )
+
+            nret = NF90_PUT_VAR(ncid, nlonid, pt%lon, (/ jn /) )
+            nret = NF90_PUT_VAR(ncid, nlatid, pt%lat, (/ jn /) )
+            nret = NF90_PUT_VAR(ncid, nxid, pt%xi, (/ jn /) )
+            nret = NF90_PUT_VAR(ncid, nyid, pt%yj, (/ jn /) )
+            nret = NF90_PUT_VAR(ncid, nuvelid, pt%uvel, (/ jn /) )
+            nret = NF90_PUT_VAR(ncid, nvvelid, pt%vvel, (/ jn /) )
+            nret = NF90_PUT_VAR(ncid, nmassid, pt%mass, (/ jn /) )
+            nret = NF90_PUT_VAR(ncid, nthicknessid, pt%thickness, (/ jn /) )
+            nret = NF90_PUT_VAR(ncid, nwidthid, pt%width, (/ jn /) )
+            nret = NF90_PUT_VAR(ncid, nlengthid, pt%length, (/ jn /) )
+            nret = NF90_PUT_VAR(ncid, nyearid, pt%year, (/ jn /) )
+            nret = NF90_PUT_VAR(ncid, ndayid, pt%day, (/ jn /) )
+            nret = NF90_PUT_VAR(ncid, nmass_of_bits_id, pt%mass_of_bits, (/ jn /) )
+            nret = NF90_PUT_VAR(ncid, nheat_density_id, pt%heat_density, (/ jn /) )
 
             this=>this%next
@@ -409,6 +411,6 @@
 
       ! Finish up
-      iret = NF90_CLOSE(ncid)
-      IF (iret .ne. NF90_NOERR) CALL ctl_stop('icebergs, write_restart: nf_close failed')
+      nret = NF90_CLOSE(ncid)
+      IF (nret .ne. NF90_NOERR) CALL ctl_stop('icebergs, write_restart: nf_close failed')
 
    END SUBROUTINE icebergs_write_restart
