Index: trunk/NEMOGCM/NEMO/OPA_SRC/ICB/icbrst.F90
===================================================================
--- trunk/NEMOGCM/NEMO/OPA_SRC/ICB/icbrst.F90	(revision 5341)
+++ trunk/NEMOGCM/NEMO/OPA_SRC/ICB/icbrst.F90	(revision 6140)
@@ -12,4 +12,6 @@
    !!            -    !                            Currently needs a fixed processor
    !!            -    !                            layout between restarts
+   !!            -    !  2015-11  Dave Storkey     Convert icb_rst_read to use IOM so can
+   !!                                              read single restart files
    !!----------------------------------------------------------------------
    !!----------------------------------------------------------------------
@@ -22,4 +24,5 @@
    USE lib_mpp        ! NEMO MPI library, lk_mpp in particular
    USE netcdf         ! netcdf routines for IO
+   USE iom
    USE icb_oce        ! define iceberg arrays
    USE icbutl         ! iceberg utility routines
@@ -57,10 +60,6 @@
       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
+      INTEGER                      ::   ii,ij,iclass
+      REAL(wp), DIMENSION(nkounts) ::   zdata      
       LOGICAL                      ::   ll_found_restart
       CHARACTER(len=256)           ::   cl_path
@@ -71,138 +70,62 @@
       !!----------------------------------------------------------------------
 
-      ! Find a restart file. Assume iceberg restarts in same directory as ocean restarts. 
+      ! 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)
       IF( cl_path(LEN_TRIM(cl_path):) /= '/' ) cl_path = TRIM(cl_path) // '/'
-      cl_filename = ' '
-      IF ( lk_mpp ) THEN
-         cl_filename = ' '
-         WRITE( cl_filename, '("restart_icebergs_",I4.4,".nc")' ) narea-1
-         INQUIRE( file=TRIM(cl_path)//TRIM(cl_filename), exist=ll_found_restart )
-      ELSE
-         cl_filename = 'restart_icebergs.nc'
-         INQUIRE( file=TRIM(cl_path)//TRIM(cl_filename), exist=ll_found_restart )
-      ENDIF
-
-      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(cl_path)//TRIM(cl_filename)
-
-      nret = NF90_OPEN(TRIM(cl_path)//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(:)
-
-            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 ',jn,' is at ', &
-                                              localpt%lon,localpt%lat,' on PE ',narea-1
+      cl_filename = TRIM(cn_ocerst_in)//'_icebergs'
+      CALL iom_open( TRIM(cl_path)//cl_filename, ncid )
+
+      IF( iom_file(ncid)%iduld .GE. 0) THEN
+
+         ibergs_in_file = iom_file(ncid)%lenuld
+         DO jn = 1,ibergs_in_file
+
+            ! iom_get treats the unlimited dimension as time. Here the unlimited dimension 
+            ! is the iceberg index, but we can still use the ktime keyword to get the iceberg we want. 
+
+            CALL iom_get( ncid, 'xi'     ,localpt%xi  , ktime=jn )
+            CALL iom_get( ncid, 'yj'     ,localpt%yj  , ktime=jn )
+
+            ii = INT( localpt%xi + 0.5 )
+            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/) )
+               localberg%number(:) = INT(zdata(:))
+               CALL iom_get( ncid, 'mass_scaling' , localberg%mass_scaling, ktime=jn )
+               CALL iom_get( ncid, 'lon'          , localpt%lon           , ktime=jn )
+               CALL iom_get( ncid, 'lat'          , localpt%lat           , ktime=jn )
+               CALL iom_get( ncid, 'uvel'         , localpt%uvel          , ktime=jn )
+               CALL iom_get( ncid, 'vvel'         , localpt%vvel          , ktime=jn )
+               CALL iom_get( ncid, 'mass'         , localpt%mass          , ktime=jn )
+               CALL iom_get( ncid, 'thickness'    , localpt%thickness     , ktime=jn )
+               CALL iom_get( ncid, 'width'        , localpt%width         , ktime=jn )
+               CALL iom_get( ncid, 'length'       , localpt%length        , ktime=jn )
+               CALL iom_get( ncid, 'year'         , zdata(1)              , ktime=jn )
+               localpt%year = INT(zdata(1))
+               CALL iom_get( ncid, 'day'          , localpt%day           , ktime=jn )
+               CALL iom_get( ncid, 'mass_of_bits' , localpt%mass_of_bits  , ktime=jn )
+               CALL iom_get( ncid, 'heat_density' , localpt%heat_density  , ktime=jn )
+
+               !
+               CALL icb_utl_add( localberg, localpt )
+
             ENDIF
-            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)
-            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 icb_utl_add( localberg, localpt )
+
          END DO
-         !
-      ENDIF
-
-      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)
-      END DO
-
-      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
-      nret = NF90_CLOSE(ncid)
-      IF (nret .ne. NF90_NOERR) CALL ctl_stop('icebergs, read_restart: nf_close failed')
+
+      ENDIF 
+
+      ! 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_unknown, 'kount' , zdata(:) )
+      num_bergs(:) = INT(zdata(:))
 
       ! Sanity check
@@ -211,9 +134,13 @@
          WRITE(numout,'(2(a,i5))') 'icebergs, read_restart_bergs: # bergs =',jn,' on PE',narea-1
       IF( lk_mpp ) THEN
-         CALL mpp_sum(ibergs_in_file)
+         ! Only mpp_sum ibergs_in_file if we are reading from multiple restart files. 
+         IF( INDEX(iom_file(ncid)%name,'icebergs.nc' ) .EQ. 0 ) CALL mpp_sum(ibergs_in_file)
          CALL mpp_sum(jn)
       ENDIF
       IF(lwp)   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'
+      !
+      ! Finish up
+      CALL iom_close( ncid )
       !
       IF( lwp .and. nn_verbose_level >= 0)  WRITE(numout,'(a)') 'icebergs, read_restart_bergs: completed'
