Index: /trunk/NEMO/OFF_SRC/IOM/in_out_manager.F90
===================================================================
--- /trunk/NEMO/OFF_SRC/IOM/in_out_manager.F90	(revision 569)
+++ /trunk/NEMO/OFF_SRC/IOM/in_out_manager.F90	(revision 569)
@@ -0,0 +1,174 @@
+MODULE in_out_manager   
+   !!======================================================================
+   !!                       ***  MODULE  in_out_manager  ***
+   !! Ocean physics:  vertical mixing coefficient compute from the tke 
+   !!                 turbulent closure parameterization
+   !!=====================================================================
+   !! History :   8.5  !  02-06  (G. Madec)  original code
+   !!             9.0  !  06-07  (S. Masson)  iom, add ctl_stop, ctl_warn
+   !!----------------------------------------------------------------------
+
+   !!----------------------------------------------------------------------
+   !!   ctl_stop   : update momentum and tracer Kz from a tke scheme
+   !!   ctl_warn   : initialization, namelist read, and parameters control
+   !!----------------------------------------------------------------------
+   USE par_kind
+   USE par_oce
+   USE lib_print         ! formated print library
+
+   IMPLICIT NONE
+   PUBLIC
+
+   !!----------------------------------------------------------------------
+   !!                   namrun namelist parameters
+   !!----------------------------------------------------------------------
+   CHARACTER (len=16) ::   cexper     = "exp0"    !: experiment name used for output filename
+   LOGICAL            ::   ln_rstart  = .FALSE.   !: start from (F) rest or (T) a restart file
+   INTEGER            ::   no         = 0         !: job number
+   INTEGER            ::   nrstdt     = 0         !: control of the time step (0, 1 or 2)
+   INTEGER            ::   nn_rstssh  = 0         !: hand made initilization of ssh or not (1/0)
+   INTEGER            ::   nit000     = 1         !: index of the first time step
+   INTEGER            ::   nitend     = 10        !: index of the last time step
+   INTEGER            ::   ndate0     = 961115    !: initial calendar date aammjj
+   INTEGER            ::   nleapy     = 0         !: Leap year calendar flag (0/1 or 30)
+   INTEGER            ::   ninist     = 0         !: initial state output flag (0/1)
+   !!----------------------------------------------------------------------
+   !! was in restart but moved here because of the OFF line... better solution should be found...
+   !!----------------------------------------------------------------------
+   INTEGER            ::   nitrst                 !: time step at which restart file should be written
+   !!----------------------------------------------------------------------
+   !!                    output monitoring
+   !!----------------------------------------------------------------------
+   LOGICAL            ::   ln_ctl     = .FALSE.   !: run control for debugging
+   INTEGER            ::   nstock     =   10      !: restart file frequency
+   INTEGER            ::   nprint     =    0      !: level of print (0 no print)
+   INTEGER            ::   nwrite     =   10      !: restart file frequency
+   INTEGER            ::   nictls     =    0      !: Start i indice for the SUM control
+   INTEGER            ::   nictle     =    0      !: End   i indice for the SUM control
+   INTEGER            ::   njctls     =    0      !: Start j indice for the SUM control
+   INTEGER            ::   njctle     =    0      !: End   j indice for the SUM control
+   INTEGER            ::   isplt      =    1      !: number of processors following i
+   INTEGER            ::   jsplt      =    1      !: number of processors following j
+   INTEGER            ::   ijsplt     =    1      !: nb of local domain = nb of processors
+   INTEGER            ::   nbench     =    0      !: benchmark parameter (0/1)
+   INTEGER            ::   nbit_cmp   =    0      !: bit reproducibility  (0/1)
+   !!----------------------------------------------------------------------
+   !!                        logical units
+   !!----------------------------------------------------------------------
+   INTEGER            ::   numstp     =  1        !: logical unit for time step
+   INTEGER            ::   numout     =  2        !: logical unit for output print
+   INTEGER            ::   numnam     =  3        !: logical unit for namelist
+   INTEGER            ::   numnam_ice =  4        !: logical unit for ice namelist
+   INTEGER            ::   numevo_ice = 17        !: logical unit for ice variables (temp. evolution)
+   INTEGER            ::   numsol     = 25        !: logical unit for solver statistics
+   INTEGER            ::   numwri     = 40        !: logical unit for output write
+   INTEGER            ::   numisp     = 41        !: logical unit for island statistics
+   INTEGER            ::   numgap     = 45        !: logical unit for differences diagnostic
+   INTEGER            ::   numbol     = 67        !: logical unit for "bol" diagnostics
+   INTEGER            ::   numptr     = 68        !: logical unit for Poleward TRansports
+   INTEGER            ::   numflo     = 69        !: logical unit for drifting floats
+
+   !!----------------------------------------------------------------------
+   !!                          Run control  
+   !!----------------------------------------------------------------------
+   
+   INTEGER            ::   nstop = 0                !: error flag (=number of reason for a premature stop run)
+   INTEGER            ::   nwarn = 0                !: warning flag (=number of warning found during the run)
+   CHARACTER(LEN=100) ::   ctmp1, ctmp2, ctmp3      !: temporary character
+   CHARACTER (len=64) ::   cform_err = "(/,' ===>>> : E R R O R',     /,'         ===========',/)"       !:
+   CHARACTER (len=64) ::   cform_war = "(/,' ===>>> : W A R N I N G', /,'         ===============',/)"   !:
+   LOGICAL            ::   lwp                      !: boolean : true on the 1st processor only
+   LOGICAL            ::   lsp_area = .TRUE.        !: to make a control print over a specific area
+   !!----------------------------------------------------------------------
+   !!  OPA 9.0 , LOCEAN-IPSL (2005) 
+   !! $Header$ 
+   !! Software governed by the CeCILL licence (modipsl/doc/NEMO_CeCILL.txt)
+   !!----------------------------------------------------------------------
+
+CONTAINS
+
+   SUBROUTINE ctl_stop( cd1, cd2, cd3, cd4, cd5,   &
+      &                 cd6, cd7, cd8, cd9, cd10 )
+      !!-----------------------------------------------------------------------
+      !!                  ***  ROUTINE  stop_opa  ***
+      !!
+      !! ** Purpose : ??? blah blah....
+      !!-----------------------------------------------------------------------
+      CHARACTER(len=*), INTENT(in), OPTIONAL ::  cd1, cd2, cd3, cd4, cd5
+      CHARACTER(len=*), INTENT(in), OPTIONAL ::  cd6, cd7, cd8, cd9, cd10
+      !!-----------------------------------------------------------------------
+      !
+      nstop = nstop + 1 
+      IF(lwp) THEN
+         WRITE(numout,"(/,' ===>>> : E R R O R',     /,'         ===========',/)") 
+         IF( PRESENT(cd1 ) ) WRITE(numout,*) cd1
+         IF( PRESENT(cd2 ) ) WRITE(numout,*) cd2
+         IF( PRESENT(cd3 ) ) WRITE(numout,*) cd3
+         IF( PRESENT(cd4 ) ) WRITE(numout,*) cd4
+         IF( PRESENT(cd5 ) ) WRITE(numout,*) cd5
+         IF( PRESENT(cd6 ) ) WRITE(numout,*) cd6
+         IF( PRESENT(cd7 ) ) WRITE(numout,*) cd7
+         IF( PRESENT(cd8 ) ) WRITE(numout,*) cd8
+         IF( PRESENT(cd9 ) ) WRITE(numout,*) cd9
+         IF( PRESENT(cd10) ) WRITE(numout,*) cd10
+      ENDIF
+      CALL FLUSH(numout)
+      !
+   END SUBROUTINE ctl_stop
+
+
+   SUBROUTINE ctl_warn( cd1, cd2, cd3, cd4, cd5,   &
+      &                 cd6, cd7, cd8, cd9, cd10 )
+      !!-----------------------------------------------------------------------
+      !!                  ***  ROUTINE  stop_warn  ***
+      !!
+      !! ** Purpose : ???  blah blah....
+      !!-----------------------------------------------------------------------
+      CHARACTER(len=*), INTENT(in), OPTIONAL ::  cd1, cd2, cd3, cd4, cd5
+      CHARACTER(len=*), INTENT(in), OPTIONAL ::  cd6, cd7, cd8, cd9, cd10
+      !!-----------------------------------------------------------------------
+      ! 
+      nwarn = nwarn + 1 
+      IF(lwp) THEN
+         WRITE(numout,"(/,' ===>>> : W A R N I N G', /,'         ===============',/)") 
+         IF( PRESENT(cd1 ) ) WRITE(numout,*) cd1
+         IF( PRESENT(cd2 ) ) WRITE(numout,*) cd2
+         IF( PRESENT(cd3 ) ) WRITE(numout,*) cd3
+         IF( PRESENT(cd4 ) ) WRITE(numout,*) cd4
+         IF( PRESENT(cd5 ) ) WRITE(numout,*) cd5
+         IF( PRESENT(cd6 ) ) WRITE(numout,*) cd6
+         IF( PRESENT(cd7 ) ) WRITE(numout,*) cd7
+         IF( PRESENT(cd8 ) ) WRITE(numout,*) cd8
+         IF( PRESENT(cd9 ) ) WRITE(numout,*) cd9
+         IF( PRESENT(cd10) ) WRITE(numout,*) cd10
+      ENDIF
+      CALL FLUSH(numout)
+      !
+   END SUBROUTINE ctl_warn
+
+
+   FUNCTION getunit()
+     !!-----------------------------------------------------------------------
+     !!                  ***  FUNCTION  getunit  ***
+     !!
+     !! ** Purpose : ???  blah blah....
+     !!-----------------------------------------------------------------------
+     INTEGER :: getunit
+     LOGICAL :: llopn 
+     !---------------------------------------------------------------------
+     getunit = 50   ! choose a unit that is big enough then it is
+                    !  not already used in OPA
+     llopn = .TRUE.
+     DO WHILE( (getunit < 998) .AND. llopn )
+        getunit = getunit + 1
+        INQUIRE( unit = getunit, opened = llopn )
+     END DO
+     IF( (getunit == 999) .AND. llopn ) THEN
+        CALL ctl_stop( 'getunit: All logical units until 999 are used...' )
+        getunit = -1
+     ENDIF
+     
+   END FUNCTION getunit
+
+   !!=====================================================================
+END MODULE in_out_manager
Index: /trunk/NEMO/OFF_SRC/IOM/iom.F90
===================================================================
--- /trunk/NEMO/OFF_SRC/IOM/iom.F90	(revision 569)
+++ /trunk/NEMO/OFF_SRC/IOM/iom.F90	(revision 569)
@@ -0,0 +1,754 @@
+MODULE iom
+   !!=====================================================================
+   !!                    ***  MODULE  iom ***
+   !! Input/Output manager :  Library to read input files
+   !!====================================================================
+   !! History :  9.0  ! 05 12  (J. Belier) Original code
+   !!            9.0  ! 06 02  (S. Masson) Adaptation to NEMO
+   !!--------------------------------------------------------------------
+   !!gm  caution add !DIR nec: improved performance to be checked as well as no result changes
+
+   !!--------------------------------------------------------------------
+   !!   iom_open       : open a file read only
+   !!   iom_close      : close a file or all files opened by iom
+   !!   iom_get        : read a field (interfaced to several routines)
+   !!   iom_gettime    : read the time axis cdvar in the file
+   !!   iom_varid      : get the id of a variable in a file
+   !!   iom_rstput     : write a field in a restart file (interfaced to several routines)
+   !!--------------------------------------------------------------------
+   USE in_out_manager  ! I/O manager
+   USE dom_oce         ! ocean space and time domain
+   USE iom_def         ! iom variables definitions
+   USE iom_ioipsl      ! NetCDF format with IOIPSL library
+   USE iom_nf90        ! NetCDF format with native NetCDF library
+   USE iom_rstdimg     ! restarts access direct format "dimg" style...
+
+   IMPLICIT NONE
+   PUBLIC   !   must be public to be able to access iom_def through iom
+   
+   PUBLIC iom_open, iom_close, iom_varid, iom_get, iom_gettime, iom_rstput
+
+   INTERFACE iom_get
+      MODULE PROCEDURE iom_g0d, iom_g1d, iom_g2d, iom_g3d
+   END INTERFACE
+   INTERFACE iom_rstput
+      MODULE PROCEDURE iom_rp0d, iom_rp1d, iom_rp2d, iom_rp3d
+   END INTERFACE
+
+   !!----------------------------------------------------------------------
+   !!  OPA 9.0 , LOCEAN-IPSL (2006)
+   !! $Header$
+   !! Software governed by the CeCILL licence (modipsl/doc/NEMO_CeCILL.txt)
+   !!----------------------------------------------------------------------
+
+CONTAINS
+
+   SUBROUTINE iom_open( cdname, kiomid, ldwrt, kdom, kiolib )
+      !!---------------------------------------------------------------------
+      !!                   ***  SUBROUTINE  iom_open  ***
+      !!
+      !! ** Purpose :  open an input file (return 0 if not found)
+      !!---------------------------------------------------------------------
+      CHARACTER(len=*), INTENT(in   )           ::   cdname   ! File name
+      INTEGER         , INTENT(  out)           ::   kiomid   ! iom identifier of the opened file
+      LOGICAL         , INTENT(in   ), OPTIONAL ::   ldwrt    ! open in write modeb          (default = .FALSE.)
+      INTEGER         , INTENT(in   ), OPTIONAL ::   kdom     ! Type of domain to be written (default = jpdom_local_noovlap)
+      INTEGER         , INTENT(in   ), OPTIONAL ::   kiolib   ! library used to open the file (default = jpnf90) 
+
+      CHARACTER(LEN=100)    ::   clname    ! the name of the file based on cdname [[+clcpu]+clcpu]
+      CHARACTER(LEN=100)    ::   cltmpn    ! tempory name to store clname (in writting mode)
+      CHARACTER(LEN=10)     ::   clsuffix  ! ".nc" or ".dimg"
+      CHARACTER(LEN=15)     ::   clcpu     ! the cpu number (max jpmax_digits digits)
+      CHARACTER(LEN=100)    ::   clinfo    ! info character
+      LOGICAL               ::   llok      ! check the existence 
+      LOGICAL               ::   llwrt     ! 
+      INTEGER               ::   iolib     ! library do we use to open the file
+      INTEGER               ::   icnt      ! counter for digits in clcpu (max = jpmax_digits)
+      INTEGER               ::   iln, ils  ! lengths of character
+      INTEGER               ::   idom      ! type of domain
+      INTEGER               ::   istop     ! 
+      INTEGER, DIMENSION(2,5) ::   idompar ! domain parameters: 
+      ! local number of points for x,y dimensions
+      ! position of first local point for x,y dimensions
+      ! position of last local point for x,y dimensions
+      ! start halo size for x,y dimensions
+      ! end halo size for x,y dimensions
+      !---------------------------------------------------------------------
+      ! Initializations and control
+      ! =============
+      clinfo = '                    iom_open ~~~  '
+      istop = nstop
+      ! if iom_open is called for the first time: initialize iom_file(:)%nfid to 0
+      ! (could be done when defining iom_file in f95 but not in f90)
+      IF( iom_init == 0 ) THEN
+         iom_file(:)%nfid = 0
+         iom_init = 1
+      ENDIF
+      ! do we read or write the file?
+      IF( PRESENT(ldwrt) ) THEN   ;   llwrt = ldwrt
+      ELSE                        ;   llwrt = .FALSE.
+      ENDIF
+      ! what library do we use to open the file?
+      IF( PRESENT(kiolib) ) THEN   ;   iolib = kiolib
+      ELSE                         ;   iolib = jpnf90
+      ENDIF
+      ! create the file name by added, if needed, TRIM(Agrif_CFixed()) and TRIM(clsuffix)
+      ! =============
+      clname   = trim(cdname)
+#if defined key_agrif
+      if ( .NOT. Agrif_Root() ) clname = TRIM(Agrif_CFixed())//'_'//TRIM(clname)
+#endif    
+      ! which suffix should we use?
+      SELECT CASE (iolib)
+      CASE (jpioipsl ) ;   clsuffix = '.nc'
+      CASE (jpnf90   ) ;   clsuffix = '.nc'
+      CASE (jprstdimg) ;   clsuffix = '.dimg'
+      CASE DEFAULT     ;   clsuffix = ''
+         CALL ctl_stop( TRIM(clinfo), 'accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
+      END SELECT
+      ! Add the suffix if needed
+      iln = LEN_TRIM(clname)
+      ils = LEN_TRIM(clsuffix)
+      IF( iln <= ils) clname = clname(1:iln)//TRIM(clsuffix)
+      IF( clname(iln-ils+1:iln) /= TRIM(clsuffix) )   clname = clname(1:iln)//TRIM(clsuffix)
+      cltmpn = clname   ! store this name
+      ! try to find if the file to be opened already exist
+      ! =============
+      INQUIRE( FILE = clname, EXIST = llok )
+      IF( .NOT.llok ) THEN
+         ! we try to add the cpu number to the name
+         WRITE(clcpu,*) narea-1
+         clcpu  = TRIM(ADJUSTL(clcpu))
+         iln = INDEX(clname,TRIM(clsuffix))
+         clname = clname(1:iln-1)//'_'//TRIM(clcpu)//TRIM(clsuffix)
+         icnt = 0
+         INQUIRE( FILE = clname, EXIST = llok ) 
+         ! we try different formats for the cpu number by adding 0
+         DO WHILE( .NOT.llok .AND. icnt < jpmax_digits )
+            clcpu  = "0"//trim(clcpu)
+            clname = clname(1:iln-1)//'_'//TRIM(clcpu)//TRIM(clsuffix)
+            INQUIRE( FILE = clname, EXIST = llok )
+            icnt = icnt + 1
+         END DO
+      ENDIF
+      ! check the domain definition
+      idom = jpdom_local_noovlap   ! default definition
+      IF( PRESENT(kdom) )   idom = kdom
+      ! create the domain informations
+      ! =============
+      SELECT CASE (idom)
+      CASE (jpdom_local_full)
+         idompar(:,1) = (/ jpi             , jpj              /)
+         idompar(:,2) = (/ nimpp           , njmpp            /)
+         idompar(:,3) = (/ nimpp + jpi - 1 , njmpp + jpj - 1  /)
+         idompar(:,4) = (/ nldi - 1        , nldj - 1         /)
+         idompar(:,5) = (/ jpi - nlei      , jpj - nlej       /)
+      CASE (jpdom_local_noextra)
+         idompar(:,1) = (/ nlci            , nlcj             /)
+         idompar(:,2) = (/ nimpp           , njmpp            /)
+         idompar(:,3) = (/ nimpp + nlci - 1, njmpp + nlcj - 1 /)
+         idompar(:,4) = (/ nldi - 1        , nldj - 1         /)
+         idompar(:,5) = (/ nlci - nlei     , nlcj - nlej      /)
+      CASE (jpdom_local_noovlap)
+         idompar(:,1) = (/ nlei - nldi + 1 , nlej - nldj + 1  /)
+         idompar(:,2) = (/ nimpp + nldi - 1, njmpp + nldj - 1 /)
+         idompar(:,3) = (/ nimpp + nlei - 1, njmpp + nlej - 1 /)
+         idompar(:,4) = (/ 0               , 0                /)
+         idompar(:,5) = (/ 0               , 0                /)
+      CASE DEFAULT
+         CALL ctl_stop( TRIM(clinfo), 'wrong value of kdom, only jpdom_local* cases are accepted' )
+      END SELECT
+      ! Open the NetCDF or RSTDIMG file
+      ! =============
+      ! do we have some free file identifier?
+      IF( MINVAL(iom_file(:)%nfid) /= 0 )   &
+            &   CALL ctl_stop( TRIM(clinfo), 'No more free file identifier', 'increase jpmax_files in iom_def' )
+      IF( istop == nstop ) THEN   ! no error within this routine
+         IF( .NOT. llok )   clname = cltmpn   ! get back the file name without the cpu number
+         SELECT CASE (iolib)
+         CASE (jpioipsl )   ;   CALL iom_ioipsl_open(  clname, kiomid, llwrt, llok, idompar )
+         CASE (jpnf90   )   ;   CALL iom_nf90_open(    clname, kiomid, llwrt, llok, idompar )
+         CASE (jprstdimg)   ;   CALL iom_rstdimg_open( clname, kiomid, llwrt, llok, idompar )
+         CASE DEFAULT
+            CALL ctl_stop( TRIM(clinfo)//' accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
+         END SELECT
+      ENDIF
+      !
+   END SUBROUTINE iom_open
+
+
+   SUBROUTINE iom_close( kiomid )
+      !!--------------------------------------------------------------------
+      !!                   ***  SUBROUTINE  iom_close  ***
+      !!
+      !! ** Purpose : close an input file, or all files opened by iom
+      !!--------------------------------------------------------------------
+      INTEGER, INTENT(in), OPTIONAL ::   kiomid   ! iom identifier of the file to be closed
+      !                                           ! No argument : all the files opened by iom are closed
+
+      INTEGER ::   jf         ! dummy loop indices
+      INTEGER ::   i_s, i_e   ! temporary integer
+      CHARACTER(LEN=100)    ::   clinfo    ! info character
+      !---------------------------------------------------------------------
+      !
+      clinfo = '                    iom_close ~~~  '
+      IF( PRESENT(kiomid) ) THEN
+         i_s = kiomid
+         i_e = kiomid
+      ELSE
+         i_s = 1
+         i_e = jpmax_files
+      ENDIF
+
+      IF( i_s > 0 ) THEN
+         DO jf = i_s, i_e
+            IF( iom_file(jf)%nfid > 0 ) THEN
+               SELECT CASE (iom_file(jf)%iolib)
+               CASE (jpioipsl )   ;   CALL iom_ioipsl_close(  jf )
+               CASE (jpnf90   )   ;   CALL iom_nf90_close(    jf )
+               CASE (jprstdimg)   ;   CALL iom_rstdimg_close( jf )
+               CASE DEFAULT
+                  CALL ctl_stop( TRIM(clinfo)//' accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
+               END SELECT
+               iom_file(jf)%nfid       = 0   ! free the id 
+               IF(lwp) WRITE(numout,*) TRIM(clinfo)//' close file: '//TRIM(iom_file(kiomid)%name)//' ok'
+            ELSEIF( PRESENT(kiomid) ) THEN
+               WRITE(ctmp1,*) '--->',  kiomid
+               CALL ctl_stop( TRIM(clinfo)//' Invalid file identifier', ctmp1 )
+            ENDIF
+         END DO
+      ENDIF
+      !    
+   END SUBROUTINE iom_close
+
+
+   FUNCTION iom_varid ( kiomid, cdvar, kdimsz )  
+      !!-----------------------------------------------------------------------
+      !!                  ***  FUNCTION  iom_varid  ***
+      !!
+      !! ** Purpose : get the id of a variable in a file (return 0 if not found)
+      !!-----------------------------------------------------------------------
+      INTEGER              , INTENT(in   )           ::   kiomid   ! file Identifier
+      CHARACTER(len=*)     , INTENT(in   )           ::   cdvar    ! name of the variable
+      INTEGER, DIMENSION(:), INTENT(  out), OPTIONAL ::   kdimsz   ! size of the dimensions
+      !
+      INTEGER                        ::   iom_varid, iiv, i_nvd
+      LOGICAL                        ::   ll_fnd
+      CHARACTER(LEN=100)             ::   clinfo                   ! info character
+      !!-----------------------------------------------------------------------
+      clinfo = 'iom_varid, file: '//trim(iom_file(kiomid)%name)//', var: '//trim(cdvar)
+      iom_varid = 0                         ! default definition
+      !
+      IF( kiomid > 0 ) THEN
+         IF( iom_file(kiomid)%nfid == 0 ) THEN 
+            CALL ctl_stop( trim(clinfo), 'the file is not open' )
+         ELSE
+            ll_fnd  = .FALSE.
+            iiv = 0
+            !
+            DO WHILE ( .NOT.ll_fnd .AND. iiv < iom_file(kiomid)%nvars )
+               iiv = iiv + 1
+               ll_fnd  = ( TRIM(cdvar) == TRIM(iom_file(kiomid)%cn_var(iiv)) )
+            END DO
+            !
+            IF( .NOT.ll_fnd ) THEN
+               iiv = iiv + 1
+               IF( iiv <= jpmax_vars ) THEN
+                  SELECT CASE (iom_file(kiomid)%iolib)
+                  CASE (jpioipsl )   ;   iom_varid = iom_ioipsl_varid( kiomid, cdvar, iiv, kdimsz )
+                  CASE (jpnf90   )   ;   iom_varid = iom_nf90_varid(   kiomid, cdvar, iiv, kdimsz )
+                  CASE (jprstdimg)   ;   iom_varid = 0   ! all variables are listed in iom_file
+                  CASE DEFAULT   
+                     CALL ctl_stop( TRIM(clinfo)//' accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
+                  END SELECT
+               ELSE
+                  CALL ctl_stop( trim(clinfo), 'Too many variables in the file '//iom_file(kiomid)%name,   &
+                        &                         'increase the parameter jpmax_vars')
+               ENDIF
+            ELSE
+               iom_varid = iiv
+               IF( PRESENT(kdimsz) ) THEN 
+                  i_nvd = iom_file(kiomid)%ndims(iiv)
+                  IF( i_nvd == size(kdimsz) ) THEN
+                     kdimsz(:) = iom_file(kiomid)%dimsz(1:i_nvd,iiv)
+                  ELSE
+                     WRITE(ctmp1,*) i_nvd, size(kdimsz)
+                     CALL ctl_stop( trim(clinfo), 'error in kdimsz size'//trim(ctmp1) )
+                  ENDIF
+               ENDIF
+            ENDIF
+         ENDIF
+      ENDIF
+      !
+   END FUNCTION iom_varid
+
+
+   !!----------------------------------------------------------------------
+   !!                   INTERFACE iom_get
+   !!----------------------------------------------------------------------
+   SUBROUTINE iom_g0d( kiomid, cdvar, pvar )
+      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
+      CHARACTER(len=*), INTENT(in   )                 ::   cdvar     ! Name of the variable
+      REAL(wp)        , INTENT(  out)                 ::   pvar      ! read field
+      !
+      INTEGER               :: idvar   ! variable id
+      !
+      idvar = iom_varid( kiomid, cdvar )
+      IF( iom_file(kiomid)%nfid > 0 .AND. idvar > 0 ) THEN
+         SELECT CASE (iom_file(kiomid)%iolib)
+         CASE (jpioipsl )   ;   CALL iom_ioipsl_get(  kiomid, idvar, pvar )
+         CASE (jpnf90   )   ;   CALL iom_nf90_get(    kiomid, idvar, pvar )
+         CASE (jprstdimg)   ;   CALL iom_rstdimg_get( kiomid, idvar, pvar )
+         CASE DEFAULT    
+            CALL ctl_stop( 'iom_g0d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
+         END SELECT
+      END IF
+   END SUBROUTINE iom_g0d
+
+   SUBROUTINE iom_g1d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount )
+      INTEGER         , INTENT(in   )                         ::   kiomid    ! Identifier of the file
+      INTEGER         , INTENT(in   )                         ::   kdom      ! Type of domain to be read
+      CHARACTER(len=*), INTENT(in   )                         ::   cdvar     ! Name of the variable
+      REAL(wp)        , INTENT(  out), DIMENSION(:)           ::   pvar      ! read field
+      INTEGER         , INTENT(in   )              , OPTIONAL ::   ktime     ! record number
+      INTEGER         , INTENT(in   ), DIMENSION(1), OPTIONAL ::   kstart    ! start axis position of the reading 
+      INTEGER         , INTENT(in   ), DIMENSION(1), OPTIONAL ::   kcount    ! number of points in each axis
+      !
+      IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r1d=pvar,   &
+            &                                        ktime=ktime, kstart=kstart, kcount=kcount )
+   END SUBROUTINE iom_g1d
+
+   SUBROUTINE iom_g2d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount )
+      INTEGER         , INTENT(in   )                           ::   kiomid    ! Identifier of the file
+      INTEGER         , INTENT(in   )                           ::   kdom      ! Type of domain to be read
+      CHARACTER(len=*), INTENT(in   )                           ::   cdvar     ! Name of the variable
+      REAL(wp)        , INTENT(  out), DIMENSION(:,:)           ::   pvar      ! read field
+      INTEGER         , INTENT(in   )                , OPTIONAL ::   ktime     ! record number
+      INTEGER         , INTENT(in   ), DIMENSION(2)  , OPTIONAL ::   kstart    ! start axis position of the reading 
+      INTEGER         , INTENT(in   ), DIMENSION(2)  , OPTIONAL ::   kcount    ! number of points in each axis
+      !
+      IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r2d=pvar,   &
+            &                                        ktime=ktime, kstart=kstart, kcount=kcount )
+   END SUBROUTINE iom_g2d
+
+   SUBROUTINE iom_g3d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount )
+      INTEGER         , INTENT(in   )                             ::   kiomid    ! Identifier of the file
+      INTEGER         , INTENT(in   )                             ::   kdom      ! Type of domain to be read
+      CHARACTER(len=*), INTENT(in   )                             ::   cdvar     ! Name of the variable
+      REAL(wp)        , INTENT(  out), DIMENSION(:,:,:)           ::   pvar      ! read field
+      INTEGER         , INTENT(in   )                  , OPTIONAL ::   ktime     ! record number
+      INTEGER         , INTENT(in   ), DIMENSION(3)    , OPTIONAL ::   kstart    ! start axis position of the reading 
+      INTEGER         , INTENT(in   ), DIMENSION(3)    , OPTIONAL ::   kcount    ! number of points in each axis
+      !
+      IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r3d=pvar,   &
+            &                                        ktime=ktime, kstart=kstart, kcount=kcount )
+   END SUBROUTINE iom_g3d
+   !!----------------------------------------------------------------------
+
+   SUBROUTINE iom_get_123d( kiomid, kdom  , cdvar ,   &
+         &                   pv_r1d, pv_r2d, pv_r3d,   &
+         &                   ktime , kstart, kcount  )
+      !!-----------------------------------------------------------------------
+      !!                  ***  ROUTINE  iom_get_123d  ***
+      !!
+      !! ** Purpose : read a 1D/2D/3D variable
+      !!
+      !! ** Method : read ONE record at each CALL
+      !!-----------------------------------------------------------------------
+      INTEGER                    , INTENT(in   )           ::   kiomid     ! Identifier of the file
+      INTEGER                    , INTENT(in   )           ::   kdom       ! Type of domain to be read
+      CHARACTER(len=*)           , INTENT(in   )           ::   cdvar      ! Name of the variable
+      REAL(wp), DIMENSION(:)     , INTENT(  out), OPTIONAL ::   pv_r1d     ! read field (1D case)
+      REAL(wp), DIMENSION(:,:)   , INTENT(  out), OPTIONAL ::   pv_r2d     ! read field (2D case)
+      REAL(wp), DIMENSION(:,:,:) , INTENT(  out), OPTIONAL ::   pv_r3d     ! read field (3D case)
+      INTEGER                    , INTENT(in   ), OPTIONAL ::   ktime      ! record number
+      INTEGER , DIMENSION(:)     , INTENT(in   ), OPTIONAL ::   kstart     ! start position of the reading in each axis 
+      INTEGER , DIMENSION(:)     , INTENT(in   ), OPTIONAL ::   kcount     ! number of points to be read in each axis
+      !
+      INTEGER                        ::   jl          ! loop on number of dimension 
+      INTEGER                        ::   idom        ! type of domain
+      INTEGER                        ::   idvar       ! id of the variable
+      INTEGER                        ::   inbdim      ! number of dimensions of the variable
+      INTEGER                        ::   idmspc      ! number of spatial dimensions 
+      INTEGER                        ::   itime       ! record number
+      INTEGER                        ::   istop       ! temporary value of nstop
+      INTEGER, DIMENSION(jpmax_dims) ::   istart      ! starting point to read for each axis
+      INTEGER, DIMENSION(jpmax_dims) ::   icnt        ! number of value to read along each axis 
+      INTEGER, DIMENSION(jpmax_dims) ::   idimsz      ! size of the dimensions of the variable
+      REAL(wp)                       ::   zscf, zofs  ! sacle_factor and add_offset
+      INTEGER                        ::   itmp        ! temporary integer
+      CHARACTER(LEN=100)             ::   clinfo      ! info character
+      !---------------------------------------------------------------------
+      !
+      clinfo = '          iom_get_123d, file: '//trim(iom_file(kiomid)%name)//', var: '//trim(cdvar)
+      ! local definition of the domain ?
+      idom = kdom
+      ! check kcount and kstart optionals parameters...
+      IF( PRESENT(kcount) .AND. (.NOT. PRESENT(kstart)) )   &
+            CALL ctl_stop( trim(clinfo), 'kcount present needs kstart present' )
+      IF( PRESENT(kstart) .AND. (.NOT. PRESENT(kcount)) )   &
+            CALL ctl_stop( trim(clinfo), 'kstart present needs kcount present' )
+      IF( PRESENT(kstart) .AND. idom /= jpdom_unknown   )   &
+            CALL ctl_stop( trim(clinfo), 'kstart present needs kdom = jpdom_unknown' )
+
+      ! Search for the variable in the data base (eventually actualize data)
+      istop = nstop
+      idvar = iom_varid( kiomid, cdvar )
+      !
+      IF( idvar > 0 ) THEN
+         ! to write iom_file(kiomid)%dimsz in a shorter way !
+         idimsz(:) = iom_file(kiomid)%dimsz(:, idvar) 
+         inbdim = iom_file(kiomid)%ndims(idvar)            ! number of dimensions in the file
+         idmspc = inbdim                                   ! number of spatial dimensions in the file
+         IF( iom_file(kiomid)%luld(idvar) )   idmspc = inbdim - 1
+         IF( idmspc > 3 )   CALL ctl_stop(trim(clinfo),   &
+               &                    'the file has more than 3 spatial dimensions this case is not coded...' ) 
+         IF( idom == jpdom_local ) THEN        ! Identify the domain in case of jpdom_local definition
+            IF( idimsz(1) == jpi .AND. idimsz(2) == jpj ) THEN 
+               idom = jpdom_local_full
+            ELSEIF( idimsz(1) == nlci .AND. idimsz(2) == nlcj ) THEN 
+               idom = jpdom_local_noextra
+            ELSEIF( idimsz(1) == (nlei - nldi + 1) .AND. idimsz(2) == (nlej - nldj + 1) ) THEN 
+               idom = jpdom_local_noovlap
+            ELSE 
+               CALL ctl_stop( trim(clinfo), 'impossible to identify the local domain' )
+            ENDIF
+         ENDIF
+         !
+         ! definition of istart and icnt
+         !
+         ! initializations
+         istart(:) = 1
+         icnt  (:) = 1
+         itime = 1
+         IF( PRESENT(ktime) ) itime = ktime
+         !
+         IF( PRESENT(pv_r1d) ) THEN
+            IF( idmspc == 1 ) THEN 
+               ! data is 1d array (+ maybe a temporal dimension)
+               IF( PRESENT(kstart) ) THEN
+                  istart(1:2) = (/ kstart(1), itime /)
+                  icnt(1) = kcount(1)
+               ELSE
+                  IF( kdom == jpdom_unknown ) THEN
+                     istart(2) = itime
+                     icnt(1) = idimsz(1)
+                  ELSE 
+                     CALL ctl_stop( trim(clinfo), 'case not coded...You must use jpdom_unknown' )
+                  ENDIF
+               ENDIF
+            ELSE
+               CALL ctl_stop( trim(clinfo), 'to keep iom lisibility, when reading a 1D array,', &
+                     &         'we do not accept data with more than 1 spatial dimension',     &
+                     &         'Use ncwa -a to suppress the unnecessary dimensions')
+            ENDIF
+         ELSEIF( PRESENT(pv_r2d) ) THEN
+            SELECT CASE (idmspc)
+            CASE (1)
+               CALL ctl_stop( trim(clinfo), 'the file has only 1 spatial dimension',  &
+                     &         'it is impossible to read a 2d array from this file...')
+            CASE (2)
+               ! data is 2d array (+ maybe a temporal dimension)
+               IF( PRESENT(kstart) ) THEN
+                  istart(1:3) = (/ kstart(1:2), itime /)
+                  icnt(1:2) = kcount(1:2)
+               ELSE
+                  IF( kdom == jpdom_unknown ) THEN
+                     istart(3) = itime
+                     icnt(1:2) = idimsz(1:2)
+                  ELSE 
+                     IF( idom == jpdom_data ) THEN
+                        istart(1:3) = (/ mig(1), mjg(1), itime /)
+                     ELSEIF( idom == jpdom_global ) THEN
+                        istart(1:3) = (/ nimpp, njmpp, itime /)
+                     ENDIF
+                     ! we do not read the overlap -> we start to read at nldi, nldj
+                     IF( idom /= jpdom_local_noovlap )   istart(1:2) = istart(1:2) + (/ nldi - 1, nldj - 1 /)
+                     ! we do not read the overlap and the extra-halos  
+                     ! -> from nldi to nlei and from nldj to nlej 
+                     icnt(1:2) = (/ nlei - nldi + 1, nlej - nldj + 1 /)
+                  ENDIF
+               ENDIF
+            CASE DEFAULT
+               IF( itime == 1 .AND. idimsz(3) == 1 .AND. idmspc == 3 ) THEN
+                  CALL ctl_warn( trim(clinfo), '2D array but 3 spatial dimensions for the data...',                &
+                        &         'As the size of the z dimension is 1 and as we try to read the first record, ',   &
+                        &         'we accept this case even if there is a possible mix-up between z and time dimension' )           
+                  IF( PRESENT(kstart) ) THEN
+                     istart(1:2) = kstart(1:2)
+                     icnt(1:2) = kcount(1:2)
+                  ELSE
+                     IF( kdom == jpdom_unknown ) THEN
+                        icnt(1:2) = idimsz(1:2)
+                     ELSE 
+                        IF( idom == jpdom_data ) THEN
+                           istart(1:2) = (/ mig(1), mjg(1) /)
+                        ELSEIF( idom == jpdom_global ) THEN
+                           istart(1:2) = (/ nimpp, njmpp /)
+                        ENDIF
+                        ! we do not read the overlap -> we start to read at nldi, nldj
+                        IF( idom /= jpdom_local_noovlap )   istart(1:2) = istart(1:2) + (/ nldi - 1, nldj - 1 /)
+                        ! we do not read the overlap and the extra-halos  
+                        ! -> from nldi to nlei and from nldj to nlej 
+                        icnt(1:2) = (/ nlei - nldi + 1, nlej - nldj + 1 /)
+                     ENDIF
+                  ENDIF
+               ELSE
+                  CALL ctl_stop( trim(clinfo), 'to keep iom lisibility, when reading a 2D array,',           &
+                        &                       'we do not accept data with more than 2 spatial dimension',   &
+                        &                       'Use ncwa -a to suppress the unnecessary dimensions' )
+               ENDIF
+            END SELECT
+         ELSEIF( PRESENT(pv_r3d) ) THEN
+            SELECT CASE (idmspc)
+            CASE( 1 )
+               CALL ctl_stop( trim(clinfo), 'the file has only 1 spatial dimension',            &
+                     &                       'it is impossible to read a 3d array from this file...' )
+            CASE( 2 )
+               CALL ctl_stop( trim(clinfo), 'the file has only 2 spatial dimension',            &
+                     &                       'it is impossible to read a 3d array from this file...' )
+            CASE( 3 )
+               ! data is 3d array (+ maybe a temporal dimension)
+               IF( PRESENT(kstart) ) THEN
+                  istart(1:4) = (/ kstart(1:3), itime /)
+                  icnt(1:3) = kcount(1:3)
+               ELSE
+                  IF( kdom == jpdom_unknown ) THEN
+                     istart(4) = itime
+                     icnt(1:3) = idimsz(1:3)
+                  ELSE 
+                     IF( idom == jpdom_data ) THEN
+                        istart(1:4) = (/ mig(1), mjg(1), 1, itime /)
+                     ELSEIF( idom == jpdom_global ) THEN
+                        istart(1:4) = (/ nimpp, njmpp, 1, itime /)
+                     ENDIF
+                     ! we do not read the overlap -> we start to read at nldi, nldj
+                     IF( idom /= jpdom_local_noovlap )   istart(1:2) = istart(1:2) + (/ nldi - 1, nldj - 1 /)
+                     ! we do not read the overlap and the extra-halos
+                     ! -> from nldi to nlei and from nldj to nlej 
+                     icnt(1:2) = (/ nlei - nldi + 1, nlej - nldj + 1 /)
+                     IF( idom == jpdom_data ) THEN 
+                        icnt(3) = jpkdta
+                     ELSE
+                        icnt(3) = jpk
+                     ENDIF
+                  ENDIF
+               ENDIF
+            CASE DEFAULT
+               CALL ctl_stop( trim(clinfo), 'to keep iom lisibility, when reading a 3D array,',   &
+                     &         'we do not accept data with more than 3 spatial dimension',         &
+                     &         'Use ncwa -a to suppress the unnecessary dimensions' )           
+            END SELECT
+         ENDIF
+
+         ! check that istart and icnt can be used with this file
+         !-
+         DO jl = 1, jpmax_dims
+            itmp = istart(jl)+icnt(jl)-1
+            IF( (itmp) > idimsz(jl) .AND. idimsz(jl) /= 0 ) THEN
+               WRITE(ctmp1,*) '(istart(',jl,') + icnt(',jl,') - 1) = ', itmp
+               WRITE(ctmp2,*) ' is larger than idimsz(',jl,'): ', idimsz(jl)
+               CALL ctl_stop( trim(clinfo), 'start and count too big regarding to the size of the data, ', ctmp1, ctmp2 )     
+            ENDIF
+         END DO
+
+         ! check that icnt matches the input array
+         !-     
+         IF( PRESENT(pv_r1d) ) THEN
+            itmp = size(pv_r1d)
+            WRITE(ctmp1,*) 'size(pv_r1d): ', itmp, ' /= icnt(1): ', icnt(1)
+            IF( itmp /= icnt(1) )   CALL ctl_stop( trim(clinfo), ctmp1 )
+         ELSEIF( PRESENT(pv_r2d) ) THEN
+            DO jl = 1, 2
+               IF( idom == jpdom_unknown ) THEN
+                  itmp = size(pv_r2d, jl)
+                  WRITE(ctmp1,*) 'size(pv_r2d, ',jl,'): ',itmp,' /= icnt(',jl,'): ',icnt(jl)
+               ELSE 
+                  itmp = size(pv_r2d(nldi:nlei,nldj:nlej), jl)
+                  WRITE(ctmp1,*) 'size(pv_r2d(nldi:nlei,nldj:nlej), ',jl,'): ',itmp,' /= icnt(',jl,'): ',icnt(jl)
+               ENDIF
+               IF( itmp /= icnt(jl) )   CALL ctl_stop( trim(clinfo), ctmp1 )
+            END DO
+         ELSEIF( PRESENT(pv_r3d) ) THEN
+            DO jl = 1, 3
+               IF( idom == jpdom_unknown ) THEN
+                  itmp = size(pv_r3d, jl)
+                  WRITE(ctmp1,*) 'size(pv_r3d, ',jl,'): ',itmp,' /= icnt(',jl,'):', icnt(jl)
+               ELSE 
+                  itmp = size(pv_r3d(nldi:nlei,nldj:nlej,:), jl)
+                  WRITE(ctmp1,*) 'size(pv_r3d(nldi:nlei,nldj:nlej), ',jl,'): ',itmp,' /= icnt(',jl,'): ',icnt(jl)
+               ENDIF
+               IF( itmp /= icnt(jl) )   CALL ctl_stop( trim(clinfo), ctmp1 )
+            END DO
+         ENDIF
+      ENDIF
+
+      ! read the data
+      !-     
+      IF( istop == nstop) THEN ! no additional errors until this point...
+         !
+         SELECT CASE (iom_file(kiomid)%iolib)
+         CASE (jpioipsl )   ;   CALL iom_ioipsl_get(  kiomid, idom, idvar, inbdim, istart, icnt, pv_r1d, pv_r2d, pv_r3d )
+         CASE (jpnf90   )   ;   CALL iom_nf90_get(    kiomid, idom, idvar, inbdim, istart, icnt, pv_r1d, pv_r2d, pv_r3d )
+         CASE (jprstdimg)   ;   CALL iom_rstdimg_get( kiomid, idom, idvar,         istart, icnt, pv_r1d, pv_r2d, pv_r3d )
+         CASE DEFAULT    
+            CALL ctl_stop( TRIM(clinfo)//' accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
+         END SELECT
+         IF( istop == nstop .AND. lwp )   &
+               WRITE(numout,*) '           read '//trim(cdvar)//' in '//trim(iom_file(kiomid)%name)//' ok'
+         !--- Apply scale_factor and offset
+         zscf = iom_file(kiomid)%scf(idvar)      ! scale factor
+         zofs = iom_file(kiomid)%ofs(idvar)      ! offset
+         IF( PRESENT(pv_r1d) ) THEN
+            IF( zscf /= 1. )   pv_r1d(:) = pv_r1d(:) * zscf 
+            IF( zofs /= 0. )   pv_r1d(:) = pv_r1d(:) + zofs
+         ELSEIF( PRESENT(pv_r2d) ) THEN
+!CDIR COLLAPSE
+            IF( zscf /= 1.)   pv_r2d(:,:) = pv_r2d(:,:) * zscf
+!CDIR COLLAPSE
+            IF( zofs /= 0.)   pv_r2d(:,:) = pv_r2d(:,:) + zofs
+         ELSEIF( PRESENT(pv_r3d) ) THEN
+!CDIR COLLAPSE
+            IF( zscf /= 1.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) * zscf
+!CDIR COLLAPSE
+            IF( zofs /= 0.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) + zofs
+         ENDIF
+         !
+      ENDIF
+      !
+   END SUBROUTINE iom_get_123d
+
+
+   SUBROUTINE iom_gettime( kiomid, cdvar, ptime )
+      !!--------------------------------------------------------------------
+      !!                   ***  SUBROUTINE iom_gettime  ***
+      !!
+      !! ** Purpose : read the time axis cdvar in the file 
+      !!--------------------------------------------------------------------
+      INTEGER               , INTENT(in   ) ::   kiomid   ! file Identifier
+      CHARACTER(len=*)      , INTENT(in   ) ::   cdvar    ! time axis name
+      REAL(wp), DIMENSION(:), INTENT(  out) ::   ptime    ! the time axis
+      !
+      INTEGER            ::   idvar    ! id of the variable
+      CHARACTER(LEN=100) ::   clinfo   ! info character
+      !---------------------------------------------------------------------
+      !
+      clinfo = 'iom_gettime, file: '//trim(iom_file(kiomid)%name)//', var: '//trim(cdvar)
+      idvar = iom_varid( kiomid, cdvar )
+      !
+      ptime(:) = 0. ! default definition
+      IF( idvar > 0 ) THEN
+         IF( iom_file(kiomid)%ndims(idvar) == 1 ) THEN
+            IF( iom_file(kiomid)%luld(idvar) ) THEN
+               IF( iom_file(kiomid)%dimsz(1,idvar) == size(ptime) ) THEN
+                  SELECT CASE (iom_file(kiomid)%iolib)
+                  CASE (jpioipsl )   ;   CALL iom_ioipsl_gettime( kiomid, idvar, ptime )
+                  CASE (jpnf90   )   ;   CALL iom_nf90_gettime(   kiomid, idvar, ptime )
+                  CASE (jprstdimg)   ;   CALL ctl_stop( TRIM(clinfo)//' case IO library == jprstdimg not coded...' )
+                  CASE DEFAULT    
+                     CALL ctl_stop( TRIM(clinfo)//' accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
+                  END SELECT
+               ELSE
+                  WRITE(ctmp1,*) 'error with the size of ptime ',size(ptime),iom_file(kiomid)%dimsz(1,idvar)
+                  CALL ctl_stop( trim(clinfo), trim(ctmp1) )
+               ENDIF
+            ELSE
+               CALL ctl_stop( trim(clinfo), 'variable dimension is not unlimited... use iom_get' )
+            ENDIF
+         ELSE
+            CALL ctl_stop( trim(clinfo), 'the variable has more than 1 dimension' )
+         ENDIF
+      ELSE
+         CALL ctl_stop( trim(clinfo), 'variable not found in '//iom_file(kiomid)%name )
+      ENDIF
+      !
+   END SUBROUTINE iom_gettime
+
+
+   !!----------------------------------------------------------------------
+   !!                   INTERFACE iom_rstput
+   !!----------------------------------------------------------------------
+   SUBROUTINE iom_rp0d( kt, kwrite, kiomid, cdvar, pvar, ktype )
+      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
+      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
+      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file 
+      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
+      REAL(wp)        , INTENT(in)                         ::   pvar     ! written field
+      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
+      INTEGER :: ivid   ! variable id
+      IF( iom_file(kiomid)%nfid > 0 ) THEN
+         ivid = iom_varid(kiomid, cdvar)
+         SELECT CASE (iom_file(kiomid)%iolib)
+         CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r0d = pvar )
+         CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r0d = pvar )
+         CASE (jprstdimg)   ;   IF( kt == kwrite )    CALL iom_rstdimg_rstput( kiomid, cdvar, pvar )
+         CASE DEFAULT     
+            CALL ctl_stop( 'iom_rp0d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
+         END SELECT
+      END IF
+   END SUBROUTINE iom_rp0d
+
+   SUBROUTINE iom_rp1d( kt, kwrite, kiomid, cdvar, pvar, ktype )
+      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
+      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
+      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file 
+      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
+      REAL(wp)        , INTENT(in), DIMENSION(        jpk) ::   pvar     ! written field
+      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
+      INTEGER :: ivid   ! variable id
+      IF( iom_file(kiomid)%nfid > 0 ) THEN
+         ivid = iom_varid(kiomid, cdvar)
+         SELECT CASE (iom_file(kiomid)%iolib)
+         CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r1d = pvar )
+         CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r1d = pvar )
+         CASE (jprstdimg)   ;   IF( kt == kwrite )    CALL iom_rstdimg_rstput( kiomid, cdvar, pv_r1d = pvar )
+         CASE DEFAULT     
+            CALL ctl_stop( 'iom_rp1d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
+         END SELECT
+      END IF
+   END SUBROUTINE iom_rp1d
+
+   SUBROUTINE iom_rp2d( kt, kwrite, kiomid, cdvar, pvar, ktype )
+      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
+      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
+      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file 
+      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
+      REAL(wp)        , INTENT(in), DIMENSION(jpi,jpj    ) ::   pvar     ! written field
+      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
+      INTEGER :: ivid   ! variable id
+      IF( iom_file(kiomid)%nfid > 0 ) THEN
+         ivid = iom_varid(kiomid, cdvar)
+         SELECT CASE (iom_file(kiomid)%iolib)
+         CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r2d = pvar )
+         CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r2d = pvar )
+         CASE (jprstdimg)   ;   IF( kt == kwrite )   CALL iom_rstdimg_rstput( kiomid, cdvar, pv_r2d = pvar ) 
+         CASE DEFAULT     
+            CALL ctl_stop( 'iom_rp2d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
+         END SELECT
+      END IF
+   END SUBROUTINE iom_rp2d
+
+   SUBROUTINE iom_rp3d( kt, kwrite, kiomid, cdvar, pvar, ktype )
+      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
+      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
+      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file 
+      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
+      REAL(wp)        , INTENT(in), DIMENSION(jpi,jpj,jpk) ::   pvar     ! written field
+      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
+      INTEGER :: ivid   ! variable id
+      IF( iom_file(kiomid)%nfid > 0 ) THEN
+         ivid = iom_varid(kiomid, cdvar)
+         SELECT CASE (iom_file(kiomid)%iolib)
+         CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r3d = pvar )
+         CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r3d = pvar )
+         CASE (jprstdimg)   ;   IF( kt == kwrite )   CALL iom_rstdimg_rstput( kiomid, cdvar, pv_r3d = pvar )
+         CASE DEFAULT     
+            CALL ctl_stop( 'iom_rp3d: accepted IO library are only jpioipsl and jprstdimg' )
+         END SELECT
+      END IF
+   END SUBROUTINE iom_rp3d
+   !!----------------------------------------------------------------------
+
+
+   !!======================================================================
+END MODULE iom
Index: /trunk/NEMO/OFF_SRC/IOM/iom_def.F90
===================================================================
--- /trunk/NEMO/OFF_SRC/IOM/iom_def.F90	(revision 569)
+++ /trunk/NEMO/OFF_SRC/IOM/iom_def.F90	(revision 569)
@@ -0,0 +1,68 @@
+MODULE iom_def
+   !!=====================================================================
+   !!                    ***  MODULE  iom_def ***
+   !! IOM variables definitions
+   !!====================================================================
+   !! History :  9.0  ! 06 09  (S. Masson) Original code
+   !!--------------------------------------------------------------------
+   !!---------------------------------------------------------------------------------
+   !! OPA 9.0 , LOCEAN-IPSL (2006) 
+   !! $Header$ 
+   !! Software governed by the CeCILL licence (modipsl/doc/NEMO_CeCILL.txt)
+   !!---------------------------------------------------------------------------------
+
+   USE par_kind
+
+   IMPLICIT NONE
+   PRIVATE
+
+   INTEGER, PARAMETER, PUBLIC ::   jpdom_data          = 1   !: ( 1  :jpidta, 1  :jpjdta)
+   INTEGER, PARAMETER, PUBLIC ::   jpdom_global        = 2   !: ( 1  :jpiglo, 1  :jpjglo)
+   INTEGER, PARAMETER, PUBLIC ::   jpdom_local         = 3   !: One of the 3 following cases
+   INTEGER, PARAMETER, PUBLIC ::   jpdom_local_full    = 4   !: ( 1  :jpi   , 1  :jpi   )
+   INTEGER, PARAMETER, PUBLIC ::   jpdom_local_noextra = 5   !: ( 1  :nlci  , 1  :nlcj  )
+   INTEGER, PARAMETER, PUBLIC ::   jpdom_local_noovlap = 6   !: (nldi:nlei  ,nldj:nlej  )
+   INTEGER, PARAMETER, PUBLIC ::   jpdom_unknown       = 7   !: No dimension checking
+
+   INTEGER, PARAMETER, PUBLIC ::   jpioipsl    = 100      !: Use ioipsl (fliocom only) library
+   INTEGER, PARAMETER, PUBLIC ::   jpnf90      = 101      !: Use nf90 library
+   INTEGER, PARAMETER, PUBLIC ::   jprstdimg   = 102      !: Use restart dimgs (fortran direct acces) library
+#if defined key_dimgout
+   INTEGER, PARAMETER, PUBLIC ::   jprstlib  = jprstdimg  !: restarts io library
+#else
+   INTEGER, PARAMETER, PUBLIC ::   jprstlib  = jpnf90     !: restarts io library
+#endif
+
+   INTEGER, PARAMETER, PUBLIC ::   jp_r8    = 200      !: write REAL(8)
+   INTEGER, PARAMETER, PUBLIC ::   jp_r4    = 201      !: write REAL(4)
+   INTEGER, PARAMETER, PUBLIC ::   jp_i4    = 202      !: write INTEGER(4)
+   INTEGER, PARAMETER, PUBLIC ::   jp_i2    = 203      !: write INTEGER(2)
+   INTEGER, PARAMETER, PUBLIC ::   jp_i1    = 204      !: write INTEGER(1)
+
+   INTEGER, PARAMETER, PUBLIC ::   jpmax_files  = 20   !: maximum number of simultaneously opened file
+   INTEGER, PARAMETER, PUBLIC ::   jpmax_vars   = 60   !: maximum number of variables in one file
+   INTEGER, PARAMETER, PUBLIC ::   jpmax_dims   =  4   !: maximum number of dimensions for one variable
+   INTEGER, PARAMETER, PUBLIC ::   jpmax_digits =  5   !: maximum number of digits for the cpu number in the file name
+
+   INTEGER, PUBLIC            ::   iom_init = 0        !: used to initialize iom_file(:)%nfid to 0
+!$AGRIF_DO_NOT_TREAT
+   TYPE, PUBLIC ::   file_descriptor
+      CHARACTER(LEN=240)                        ::   name     !: name of the file
+      INTEGER                                   ::   nfid     !: identifier of the file (0 if closed)
+      INTEGER                                   ::   iolib    !: library used to read the file (jpioipsl, jpnf90 or jprstdimg)
+      INTEGER                                   ::   nvars    !: number of identified varibles in the file
+      INTEGER                                   ::   iduld    !: id of the unlimited dimension
+      INTEGER                                   ::   irec     !: writing record position  
+      CHARACTER(LEN=16), DIMENSION(jpmax_vars)  ::   cn_var   !: names of the variables
+      INTEGER, DIMENSION(jpmax_vars)            ::   nvid     !: id of the variables
+      INTEGER, DIMENSION(jpmax_vars)            ::   ndims    !: number of dimensions of the variables
+      LOGICAL, DIMENSION(jpmax_vars)            ::   luld     !: variable using the unlimited dimension
+      INTEGER, DIMENSION(jpmax_dims,jpmax_vars) ::   dimsz    !: size of variables dimensions 
+      REAL(kind=wp), DIMENSION(jpmax_vars)      ::   scf      !: scale_factor of the variables
+      REAL(kind=wp), DIMENSION(jpmax_vars)      ::   ofs      !: add_offset of the variables
+   END TYPE file_descriptor
+   TYPE(file_descriptor), DIMENSION(jpmax_files), PUBLIC ::   iom_file !: array containing the info for all opened files
+!$AGRIF_END_DO_NOT_TREAT
+
+   !!=====================================================================
+END MODULE iom_def
Index: /trunk/NEMO/OFF_SRC/IOM/iom_ioipsl.F90
===================================================================
--- /trunk/NEMO/OFF_SRC/IOM/iom_ioipsl.F90	(revision 569)
+++ /trunk/NEMO/OFF_SRC/IOM/iom_ioipsl.F90	(revision 569)
@@ -0,0 +1,432 @@
+MODULE iom_ioipsl
+   !!=====================================================================
+   !!                    ***  MODULE  iom_ioipsl ***
+   !! Input/Output manager :  Library to read input files with IOIPSL (only fliocom module)
+   !!====================================================================
+   !! History :  9.0  ! 05 12  (J. Belier) Original code
+   !!            9.0  ! 06 02  (S. Masson) Adaptation to NEMO
+   !!--------------------------------------------------------------------
+   !!gm  caution add !DIR nec: improved performance to be checked as well as no result changes
+
+   !!--------------------------------------------------------------------
+   !!   iom_open       : open a file read only
+   !!   iom_close      : close a file or all files opened by iom
+   !!   iom_get        : read a field (interfaced to several routines)
+   !!   iom_gettime    : read the time axis kvid in the file
+   !!   iom_varid      : get the id of a variable in a file
+   !!   iom_rstput     : write a field in a restart file (interfaced to several routines)
+   !!--------------------------------------------------------------------
+   USE in_out_manager  ! I/O manager
+   USE dom_oce         ! ocean space and time domain
+   USE lbclnk          ! lateal boundary condition / mpp exchanges
+   USE iom_def         ! iom variables definitions
+   USE ioipsl          ! IOIPSL library
+
+   IMPLICIT NONE
+   PRIVATE
+
+   PUBLIC iom_ioipsl_open, iom_ioipsl_close, iom_ioipsl_varid, iom_ioipsl_get, iom_ioipsl_gettime, iom_ioipsl_rstput
+
+   INTERFACE iom_ioipsl_get
+      MODULE PROCEDURE iom_ioipsl_g0d, iom_ioipsl_g123d
+   END INTERFACE
+   INTERFACE iom_ioipsl_rstput
+      MODULE PROCEDURE iom_ioipsl_rp0123d
+   END INTERFACE
+   !!----------------------------------------------------------------------
+   !!  OPA 9.0 , LOCEAN-IPSL (2006)
+   !! $Header$
+   !! Software governed by the CeCILL licence (modipsl/doc/NEMO_CeCILL.txt)
+   !!----------------------------------------------------------------------
+
+CONTAINS
+
+   SUBROUTINE iom_ioipsl_open( cdname, kiomid, ldwrt, ldok, kdompar )
+      !!---------------------------------------------------------------------
+      !!                   ***  SUBROUTINE  iom_open  ***
+      !!
+      !! ** Purpose :  open an input file with IOIPSL (only fliocom module)
+      !!---------------------------------------------------------------------
+      CHARACTER(len=*)       , INTENT(inout) ::   cdname    ! File name
+      INTEGER                , INTENT(  out) ::   kiomid    ! ioipsl identifier of the opened file
+      LOGICAL                , INTENT(in   ) ::   ldwrt     ! read or write the file?
+      LOGICAL                , INTENT(in   ) ::   ldok      ! check the existence 
+      INTEGER, DIMENSION(2,5), INTENT(in   ) ::   kdompar   ! domain parameters: 
+
+      CHARACTER(LEN=100) ::   clinfo     ! info character
+      INTEGER            ::   iln        ! lengths of character
+      INTEGER            ::   istop      ! temporary storage of nstop
+      INTEGER            ::   ifliodom   ! model domain identifier (see flio_dom_set)
+      INTEGER            ::   ioipslid   ! ioipsl identifier of the opened file
+      !---------------------------------------------------------------------
+
+      clinfo = '                    iom_ioipsl_open ~~~  '
+      istop = nstop
+      !
+      IF( ldok ) THEN      ! Open existing file...
+         !                 ! =============
+         IF( ldwrt ) THEN  ! ... in write mode
+            IF(lwp) WRITE(numout,*) TRIM(clinfo)//' open existing file: '//TRIM(cdname)//' in WRITE mode'
+            CALL flioopfd( TRIM(cdname), ioipslid, "WRITE" )
+         ELSE              ! ... in read mode
+            IF(lwp) WRITE(numout,*) TRIM(clinfo)//' open existing file: '//TRIM(cdname)//' in READ mode'
+            CALL flioopfd( TRIM(cdname), ioipslid )
+         ENDIF
+      ELSE                 ! the file does not exist
+         !                 ! =============
+         iln = INDEX( cdname, '.nc' )
+         IF( ldwrt ) THEN  ! the file should be open in write mode so we create it...
+            IF( jpnij > 1 ) THEN
+               ! define the domain position regarding to the global domain (mainly useful in mpp)
+               CALL flio_dom_set( jpnij, narea-1, (/1, 2/), (/jpiglo, jpjglo/)   &
+                  &             , kdompar(:,1), kdompar(:,2), kdompar(:,3), kdompar(:,4), kdompar(:,5)   &
+                  &             , 'BOX', ifliodom )        
+               ! Note that fliocrfd may change the value of cdname (add the cpu number...)
+               IF(lwp) WRITE(numout,*) TRIM(clinfo)//' create new file: '//cdname(1:iln-1)//'... in WRITE mode'
+               CALL fliocrfd( cdname, (/'x'         , 'y'         , 'z', 't'/)   &
+                  &         , (/kdompar(1,1), kdompar(2,1), jpk, -1 /), ioipslid, ifliodom )
+            ELSE              ! the file should be open for read mode so it must exist...
+               IF(lwp) WRITE(numout,*) TRIM(clinfo)//' create new file: '//cdname//' in WRITE mode'
+               CALL fliocrfd( cdname, (/'x'         , 'y'         , 'z', 't'/)   &
+                  &         , (/kdompar(1,1), kdompar(2,1), jpk, -1 /), ioipslid )
+            ENDIF
+         ELSE              ! the file should be open for read mode so it must exist...
+            CALL ctl_stop( TRIM(clinfo), 'File '//cdname(1:iln-1)//'* not found' )
+         ENDIF
+      ENDIF
+      ! start to fill file informations
+      ! =============
+      IF( istop == nstop ) THEN   ! no error within this routine
+         kiomid = MINLOC(iom_file(:)%nfid, dim = 1)
+         iom_file(kiomid)%name   = TRIM(cdname)
+         iom_file(kiomid)%nfid   = ioipslid
+         iom_file(kiomid)%iolib  = jpioipsl
+         iom_file(kiomid)%nvars  = 0
+         iom_file(kiomid)%irec   = -1   ! useless for NetCDF files
+         CALL flioinqf( ioipslid, id_uld = iom_file(kiomid)%iduld )
+         IF(lwp) WRITE(numout,*) '                   ---> '//TRIM(cdname)//' OK'
+      ELSE
+         kiomid = 0               ! return error flag
+      ENDIF
+      !
+   END SUBROUTINE iom_ioipsl_open
+
+
+   SUBROUTINE iom_ioipsl_close( kiomid )
+      !!--------------------------------------------------------------------
+      !!                   ***  SUBROUTINE  iom_ioipsl_close  ***
+      !!
+      !! ** Purpose : close an input file with IOIPSL (only fliocom module) 
+      !!--------------------------------------------------------------------
+      INTEGER, INTENT(in) ::   kiomid   ! iom identifier of the file to be closed
+      !---------------------------------------------------------------------
+      !
+      CALL flioclo( iom_file(kiomid)%nfid )
+      !    
+   END SUBROUTINE iom_ioipsl_close
+
+
+   FUNCTION iom_ioipsl_varid ( kiomid, cdvar, kiv, kdimsz )  
+      !!-----------------------------------------------------------------------
+      !!                  ***  FUNCTION  iom_varid  ***
+      !!
+      !! ** Purpose : get the id of a variable in a file with IOIPSL (only fliocom module)
+      !!-----------------------------------------------------------------------
+      INTEGER              , INTENT(in   )           ::   kiomid   ! file Identifier
+      CHARACTER(len=*)     , INTENT(in   )           ::   cdvar    ! name of the variable
+      INTEGER              , INTENT(in   )           ::   kiv   ! 
+      INTEGER, DIMENSION(:), INTENT(  out), OPTIONAL ::   kdimsz   ! size of the dimensions
+      !
+      INTEGER                        ::   iom_ioipsl_varid  ! iom variable Id
+      INTEGER                        ::   ioipslid          ! ioipsl file identifier
+      INTEGER                        ::   ji                ! dummy loop index
+      INTEGER                        ::   i_nvd             ! number of dimension of the variable
+      INTEGER, DIMENSION(jpmax_dims) ::   idimid            ! dimension ids of the variable
+      LOGICAL                        ::   ll_fnd            ! found test
+      CHARACTER(LEN=100)             ::   clinfo            ! info character
+      !!-----------------------------------------------------------------------
+      clinfo = 'iom_ioipsl_varid, file: '//trim(iom_file(kiomid)%name)//', var: '//trim(cdvar)
+      iom_ioipsl_varid = 0                  ! default definition
+      IF( PRESENT(kdimsz) ) kdimsz(:) = 0   ! default definition
+      ioipslid = iom_file(kiomid)%nfid      ! get back ioipsl file identifier 
+      CALL flioinqv( ioipslid, cdvar, ll_fnd, nb_dims = i_nvd )   ! does the variable exist in the file
+      IF( ll_fnd ) THEN
+         IF( i_nvd <= jpmax_dims ) THEN
+            iom_ioipsl_varid = kiv
+            iom_file(kiomid)%nvars       = kiv
+            iom_file(kiomid)%nvid(kiv)   = -1   ! variable id is not available in ioipsl
+            iom_file(kiomid)%cn_var(kiv) = TRIM(cdvar)
+            iom_file(kiomid)%ndims(kiv)  = i_nvd
+            CALL flioinqv( ioipslid, cdvar, ll_fnd,   &
+                  &           len_dims = iom_file(kiomid)%dimsz(1:i_nvd,kiv), &   ! dimensions size
+                  &           id_dims  = idimid(1:i_nvd) )                        ! dimensions ids
+            DO ji = 1, i_nvd   ! find the unlimited dimension
+               IF( idimid(ji) == iom_file(kiomid)%iduld ) iom_file(kiomid)%luld(kiv) = .TRUE.
+            END DO
+            !---------- Deal with scale_factor and offset
+            CALL flioinqa( ioipslid, cdvar, 'scale_factor', ll_fnd )
+            IF( ll_fnd) THEN
+               CALL fliogeta( ioipslid, cdvar, 'scale_factor', iom_file(kiomid)%scf(kiv) )
+            ELSE
+               iom_file(kiomid)%scf(kiv) = 1.
+            END IF
+            CALL flioinqa( ioipslid, cdvar, 'offset', ll_fnd )
+            IF( ll_fnd ) THEN
+               CALL fliogeta( ioipslid, cdvar, 'offset', iom_file(kiomid)%ofs(kiv) )
+            ELSE
+               iom_file(kiomid)%ofs(kiv) = 0.
+            END IF
+            ! return the simension size
+            IF( PRESENT(kdimsz) ) THEN 
+               IF( i_nvd == SIZE(kdimsz) ) THEN
+                  kdimsz(:) = iom_file(kiomid)%dimsz(1:i_nvd,kiv)
+               ELSE
+                  WRITE(ctmp1,*) i_nvd, SIZE(kdimsz)
+                  CALL ctl_stop( TRIM(clinfo), 'error in kdimsz size'//TRIM(ctmp1) )
+               ENDIF
+            ENDIF
+         ELSE
+            CALL ctl_stop( TRIM(clinfo), 'Too many dimensions in the file '//iom_file(kiomid)%name,   &
+                  &                       'increase the parameter jpmax_vars')
+         ENDIF
+!!$                  ELSE  
+!!$                     CALL ctl_warn( trim(clinfo), 'Variable '//trim(cdvar)// &
+!!$                        &                         ' is not found in the file '//trim(iom_file(kiomid)%name) )
+      ENDIF
+      !
+   END FUNCTION iom_ioipsl_varid
+
+
+   SUBROUTINE iom_ioipsl_g0d( kiomid, kvid, pvar )
+      !!-----------------------------------------------------------------------
+      !!                  ***  ROUTINE  iom_ioipsl_g0d  ***
+      !!
+      !! ** Purpose : read a scalar with IOIPSL (only fliocom module)
+      !!-----------------------------------------------------------------------
+      INTEGER , INTENT(in   ) ::   kiomid    ! Identifier of the file
+      INTEGER , INTENT(in   ) ::   kvid      ! variable id
+      REAL(wp), INTENT(  out) ::   pvar      ! read field
+      !
+      CALL fliogetv( iom_file(kiomid)%nfid, TRIM(iom_file(kiomid)%cn_var(kvid)), pvar )
+      ! 
+   END SUBROUTINE iom_ioipsl_g0d
+
+
+   SUBROUTINE iom_ioipsl_g123d( kiomid, kdom, kvid, knbdim, kstart, kcount,    &
+         &                          pv_r1d, pv_r2d, pv_r3d)
+      !!-----------------------------------------------------------------------
+      !!                  ***  ROUTINE  iom_ioipsl_g123d  ***
+      !!
+      !! ** Purpose : read a 1D/2D/3D variable with IOIPSL (only fliocom module)
+      !!
+      !! ** Method : read ONE record at each CALL
+      !!-----------------------------------------------------------------------
+      INTEGER                    , INTENT(in   )           ::   kiomid     ! iom identifier of the file
+      INTEGER                    , INTENT(in   )           ::   kdom       ! Type of domain to be read
+      INTEGER                    , INTENT(in   )           ::   kvid       ! Name of the variable
+      INTEGER                    , INTENT(in   )           ::   knbdim     ! number of dimensions of the variable
+      INTEGER , DIMENSION(:)     , INTENT(in   )           ::   kstart     ! start position of the reading in each axis 
+      INTEGER , DIMENSION(:)     , INTENT(in   )           ::   kcount     ! number of points to be read in each axis
+      REAL(wp), DIMENSION(:)     , INTENT(  out), OPTIONAL ::   pv_r1d     ! read field (1D case)
+      REAL(wp), DIMENSION(:,:)   , INTENT(  out), OPTIONAL ::   pv_r2d     ! read field (2D case)
+      REAL(wp), DIMENSION(:,:,:) , INTENT(  out), OPTIONAL ::   pv_r3d     ! read field (3D case)
+      !
+      INTEGER               ::   ioipslid   ! ioipsl file identifier
+      CHARACTER(LEN=100)    ::   clvn       ! variable name
+      !---------------------------------------------------------------------
+      clvn = TRIM(iom_file(kiomid)%cn_var(kvid))
+      ioipslid = iom_file(kiomid)%nfid
+      !
+      IF( PRESENT(pv_r1d) ) THEN
+         CALL fliogetv( ioipslid, clvn, pv_r1d(:), start=kstart(1:knbdim), count=kcount(1:knbdim) )
+      ELSEIF( PRESENT(pv_r2d) ) THEN
+         IF( kdom /= jpdom_unknown ) THEN
+            CALL fliogetv( ioipslid, clvn, pv_r2d(nldi:nlei,nldj:nlej), start=kstart(1:knbdim), count=kcount(1:knbdim) )
+            !--- Fill the overlap areas and extra hallows (mpp)
+            CALL lbc_lnk (pv_r2d,'Z',-999.,'no0')
+         ELSE
+            CALL fliogetv( ioipslid, clvn, pv_r2d(:,:), start=kstart(1:knbdim), count=kcount(1:knbdim) )
+         ENDIF
+      ELSEIF( PRESENT(pv_r3d) ) THEN
+         IF( kdom /= jpdom_unknown ) THEN
+            CALL fliogetv( ioipslid, clvn, pv_r3d(nldi:nlei,nldj:nlej,:), start=kstart(1:knbdim),   &
+                  &                                                         count=kcount  (1:knbdim) )
+            !--- Fill the overlap areas and extra hallows (mpp)
+            ! this if could be simplified with the new lbc_lnk that works with any size of the 3rd dimension
+            IF( kcount(3) == jpk ) THEN
+               CALL lbc_lnk( pv_r3d,'Z',-999.,'no0' )
+            ELSE   ! put some arbitrary value (a call to lbc_lnk will be done later...)
+               pv_r3d(nlei+1:jpi,      :   ,:) = 1.            
+               pv_r3d(      :   ,nlej+1:jpj,:) = 1.            
+            ENDIF
+         ELSE
+            CALL fliogetv( ioipslid, clvn, pv_r3d(:,:,:), start=kstart(1:knbdim), count=kcount(1:knbdim) )
+         ENDIF
+      ENDIF
+      !
+      !
+   END SUBROUTINE iom_ioipsl_g123d
+
+
+   SUBROUTINE iom_ioipsl_gettime( kiomid, kvid, ptime )
+      !!--------------------------------------------------------------------
+      !!                   ***  SUBROUTINE iom_gettime  ***
+      !!
+      !! ** Purpose : read the time axis kvid in the file with IOIPSL (only fliocom module)
+      !!--------------------------------------------------------------------
+      INTEGER               , INTENT(in   ) ::   kiomid   ! file Identifier
+      INTEGER               , INTENT(in   ) ::   kvid     ! variable id
+      REAL(wp), DIMENSION(:), INTENT(  out) ::   ptime    ! the time axis
+      !---------------------------------------------------------------------
+      !
+      CALL fliogetv( iom_file(kiomid)%nfid, TRIM(iom_file(kiomid)%cn_var(kvid)), ptime(:),   &
+            &         start=(/ 1 /), count=(/ iom_file(kiomid)%dimsz(1, kvid) /) )
+      !
+   END SUBROUTINE iom_ioipsl_gettime
+
+
+   SUBROUTINE iom_ioipsl_rp0123d( kt, kwrite, kiomid, cdvar , kvid  , ktype,   &
+         &                                 pv_r0d, pv_r1d, pv_r2d, pv_r3d )
+      !!--------------------------------------------------------------------
+      !!                   ***  SUBROUTINE  iom_ioipsl_rstput  ***
+      !!
+      !! ** Purpose : read the time axis cdvar in the file 
+      !!--------------------------------------------------------------------
+      INTEGER                         , INTENT(in)           ::   kt       ! ocean time-step
+      INTEGER                         , INTENT(in)           ::   kwrite   ! writing time-step
+      INTEGER                         , INTENT(in)           ::   kiomid   ! Identifier of the file 
+      CHARACTER(len=*)                , INTENT(in)           ::   cdvar    ! variable name
+      INTEGER                         , INTENT(in)           ::   kvid     ! variable id
+      INTEGER                         , INTENT(in), OPTIONAL ::   ktype    ! variable type (default R8)
+      REAL(wp)                        , INTENT(in), OPTIONAL ::   pv_r0d   ! written Od field
+      REAL(wp), DIMENSION(        jpk), INTENT(in), OPTIONAL ::   pv_r1d   ! written 1d field
+      REAL(wp), DIMENSION(jpi,jpj    ), INTENT(in), OPTIONAL ::   pv_r2d   ! written 2d field
+      REAL(wp), DIMENSION(jpi,jpj,jpk), INTENT(in), OPTIONAL ::   pv_r3d   ! written 3d field
+      !
+      INTEGER               :: idims                ! number of dimension
+      INTEGER               :: idvar                ! variable id
+      INTEGER               :: itype                ! variable type
+      INTEGER               :: ix1, ix2, iy1, iy2   ! subdomain indexes
+      INTEGER, DIMENSION(4) :: idimsz               ! dimensions size  
+      INTEGER, DIMENSION(4) :: idimid               ! dimensions id
+      CHARACTER(LEN=100)    :: clinfo               ! info character
+      INTEGER               :: ioipslid             ! ioipsl file identifier
+      !---------------------------------------------------------------------
+      !
+      clinfo = '          iom_ioipsl_rp0123d, file: '//TRIM(iom_file(kiomid)%name)//', var: '//TRIM(cdvar)
+      ioipslid = iom_file(kiomid)%nfid
+      !
+      ! define dimension variables if it is not already done
+      ! ==========================
+      IF( iom_file(kiomid)%nvars == 0 ) THEN
+         ! define the dimension variables if it is not already done
+         CALL fliodefv( ioipslid,'nav_lon', (/1,2/), v_t=flio_r4   , axis='X',   &
+               &                  long_name="Longitude", units="degrees_east" )
+         CALL fliodefv( ioipslid,'nav_lat', (/1,2/), v_t=flio_r4   , axis='Y',   &
+               &                  long_name="Latitude", units="degrees_north" )
+         CALL fliodefv( ioipslid,'nav_lev', (/3/)  , v_t=flio_i4   , axis='Z',   &
+               &                  long_name="Model levels",units="model_levels")
+         CALL fliodefv( ioipslid,'time_counter', (/4/), v_t=flio_r4, axis='T',   &
+               &                  long_name="Time axis", units='seconds since 0001-01-01 00:00:00' )
+         ! update informations structure related the dimension variable we just added...
+         iom_file(kiomid)%nvars         = 4
+         iom_file(kiomid)%luld(1:4)     = (/ .FALSE., .FALSE., .FALSE., .TRUE. /)
+         iom_file(kiomid)%cn_var(1:3)   = (/ 'nav_lon', 'nav_lat', 'nav_lev' /)
+         iom_file(kiomid)%cn_var(4)     = 'time_counter'
+         iom_file(kiomid)%ndims(1:4)    = (/ 2, 2, 1, 1 /)  
+         CALL flioinqf( ioipslid, ln_dim = idimsz )
+         iom_file(kiomid)%dimsz(1:2, 1) = idimsz(1:2)
+         iom_file(kiomid)%dimsz(1:2, 2) = idimsz(1:2)
+         iom_file(kiomid)%dimsz(1, 3:4) = (/idimsz(3), 1/)
+         IF(lwp) WRITE(numout,*) TRIM(clinfo)//' define dimension variables done'
+      ENDIF
+
+      ! define the data if it is not already done
+      ! ===============
+      IF( kvid <= 0 ) THEN
+         ! variable definition
+         IF(     PRESENT(pv_r0d) ) THEN   ;   idims = 0
+         ELSEIF( PRESENT(pv_r1d) ) THEN   ;   idims = 2   ;   idimid(1:idims) = (/    3,4/)
+         ELSEIF( PRESENT(pv_r2d) ) THEN   ;   idims = 3   ;   idimid(1:idims) = (/1,2  ,4/)
+         ELSEIF( PRESENT(pv_r3d) ) THEN   ;   idims = 4   ;   idimid(1:idims) = (/1,2,3,4/)
+         ENDIF
+         IF( PRESENT(ktype) ) THEN   ! variable external type
+            SELECT CASE (ktype)
+            CASE (jp_r8)  ;   itype = flio_r8
+            CASE (jp_r4)  ;   itype = flio_r4
+            CASE (jp_i4)  ;   itype = flio_i4
+            CASE (jp_i2)  ;   itype = flio_i2
+            CASE (jp_i1)  ;   itype = flio_i1   !   fliocom does not handle i1 type of variable
+            CASE DEFAULT   ;   CALL ctl_stop( TRIM(clinfo)//' unknown variable type' )
+            END SELECT
+         ELSE
+            itype = flio_r8
+         ENDIF
+         IF( PRESENT(pv_r0d) ) THEN   ;   CALL fliodefv (ioipslid, TRIM(cdvar)                 , v_t = itype)
+         ELSE                         ;   CALL fliodefv (ioipslid, TRIM(cdvar), idimid(1:idims), v_t = itype)
+         ENDIF
+         ! update informations structure related the new variable we want to add...
+         idvar                          = iom_file(kiomid)%nvars + 1
+         iom_file(kiomid)%nvars         = idvar
+         iom_file(kiomid)%cn_var(idvar) = TRIM(cdvar)
+         iom_file(kiomid)%nvid(idvar)   = -1   ! netcdf variable id is not available in ioipsl
+         iom_file(kiomid)%scf(idvar)    = 1.
+         iom_file(kiomid)%ofs(idvar)    = 0.
+         iom_file(kiomid)%ndims(idvar)  = idims
+         IF( .NOT. PRESENT(pv_r0d) ) THEN
+            iom_file(kiomid)%luld(idvar) = .TRUE.
+            CALL flioinqf( ioipslid, ln_dim = idimsz )
+            iom_file(kiomid)%dimsz(1:idims-1,idvar) = idimsz(idimid(1:idims-1))
+         ELSE                               
+            iom_file(kiomid)%luld(idvar) = .FALSE.
+         ENDIF
+         IF(lwp) WRITE(numout,*) TRIM(clinfo)//' defined ok'
+      ELSE
+         idvar = kvid
+      ENDIF
+
+      ! time step kwrite : write the variable
+      IF( kt == kwrite ) THEN
+         ! on what kind of domain must the data be written?
+         IF( PRESENT(pv_r2d) .OR. PRESENT(pv_r3d) ) THEN
+            idimsz(1:2) = iom_file(kiomid)%dimsz(1:2,idvar)
+            IF(     idimsz(1) == (nlei - nldi + 1) .AND. idimsz(2) == (nlej - nldj + 1) ) THEN
+               ix1 = nldi   ;   ix2 = nlei   ;   iy1 = nldj   ;   iy2 = nlej
+            ELSEIF( idimsz(1) == nlci              .AND. idimsz(2) == nlcj              ) THEN
+               ix1 = 1      ;   ix2 = nlci   ;   iy1 = 1      ;   iy2 = nlcj
+            ELSEIF( idimsz(1) == jpi               .AND. idimsz(2) == jpj               ) THEN
+               ix1 = 1      ;   ix2 = jpi    ;   iy1 = 1      ;   iy2 = jpj
+            ELSE 
+               CALL ctl_stop( 'iom_ioipsl_rp0123d: should have been an impossible case...' )
+            ENDIF
+
+            ! write dimension variables if it is not already done
+            ! =============
+            IF( iom_file(kiomid)%dimsz(1, 1) == 0 ) THEN
+               CALL flioputv( ioipslid, 'nav_lon'     , glamt(ix1:ix2, iy1:iy2) )
+               CALL flioputv( ioipslid, 'nav_lat'     , gphit(ix1:ix2, iy1:iy2) )
+               CALL flioputv( ioipslid, 'nav_lev'     , gdept_0 )
+               ! +++ WRONG VALUE: to be improved but not really useful...
+               CALL flioputv( ioipslid, 'time_counter', kt )   
+               IF(lwp) WRITE(numout,*) TRIM(clinfo)//' write dimension variables done'
+            ENDIF
+         ENDIF
+
+         ! write the data
+         ! =============
+         IF(     PRESENT(pv_r0d) ) THEN   ;   CALL flioputv( ioipslid, cdvar, pv_r0d                      )
+         ELSEIF( PRESENT(pv_r1d) ) THEN   ;   CALL flioputv( ioipslid, cdvar, pv_r1d(                  :) )
+         ELSEIF( PRESENT(pv_r2d) ) THEN   ;   CALL flioputv( ioipslid, cdvar, pv_r2d(ix1:ix2, iy1:iy2   ) )
+         ELSEIF( PRESENT(pv_r3d) ) THEN   ;   CALL flioputv( ioipslid, cdvar, pv_r3d(ix1:ix2, iy1:iy2, :) )
+         ENDIF
+         ! add 1 to the size of the temporal dimension (not really useful...)
+         IF( iom_file(kiomid)%luld(idvar) )   iom_file(kiomid)%dimsz(iom_file(kiomid)%ndims(idvar), idvar)    &
+               &                            = iom_file(kiomid)%dimsz(iom_file(kiomid)%ndims(idvar), idvar) + 1
+         IF(lwp) WRITE(numout,*) TRIM(clinfo)//' written ok'
+      ENDIF
+      !     
+   END SUBROUTINE iom_ioipsl_rp0123d
+
+
+   !!======================================================================
+END MODULE iom_ioipsl
Index: /trunk/NEMO/OFF_SRC/IOM/iom_nf90.F90
===================================================================
--- /trunk/NEMO/OFF_SRC/IOM/iom_nf90.F90	(revision 569)
+++ /trunk/NEMO/OFF_SRC/IOM/iom_nf90.F90	(revision 569)
@@ -0,0 +1,483 @@
+MODULE iom_nf90
+   !!=====================================================================
+   !!                    ***  MODULE  iom_nf90 ***
+   !! Input/Output manager :  Library to read input files with NF90 (only fliocom module)
+   !!====================================================================
+   !! History :  9.0  ! 05 12  (J. Belier) Original code
+   !!            9.0  ! 06 02  (S. Masson) Adaptation to NEMO
+   !!--------------------------------------------------------------------
+   !!gm  caution add !DIR nec: improved performance to be checked as well as no result changes
+
+   !!--------------------------------------------------------------------
+   !!   iom_open       : open a file read only
+   !!   iom_close      : close a file or all files opened by iom
+   !!   iom_get        : read a field (interfaced to several routines)
+   !!   iom_gettime    : read the time axis kvid in the file
+   !!   iom_varid      : get the id of a variable in a file
+   !!   iom_rstput     : write a field in a restart file (interfaced to several routines)
+   !!--------------------------------------------------------------------
+   USE in_out_manager  ! I/O manager
+   USE dom_oce         ! ocean space and time domain
+   USE lbclnk          ! lateal boundary condition / mpp exchanges
+   USE iom_def         ! iom variables definitions
+   USE netcdf          ! NetCDF library
+
+   IMPLICIT NONE
+   PRIVATE
+
+   PUBLIC iom_nf90_open, iom_nf90_close, iom_nf90_varid, iom_nf90_get, iom_nf90_gettime, iom_nf90_rstput
+
+   INTERFACE iom_nf90_get
+      MODULE PROCEDURE iom_nf90_g0d, iom_nf90_g123d
+   END INTERFACE
+   INTERFACE iom_nf90_rstput
+      MODULE PROCEDURE iom_nf90_rp0123d
+   END INTERFACE
+   !!----------------------------------------------------------------------
+   !!  OPA 9.0 , LOCEAN-IPSL (2006)
+   !! $Header$
+   !! Software governed by the CeCILL licence (modipsl/doc/NEMO_CeCILL.txt)
+   !!----------------------------------------------------------------------
+
+CONTAINS
+
+   SUBROUTINE iom_nf90_open( cdname, kiomid, ldwrt, ldok, kdompar )
+      !!---------------------------------------------------------------------
+      !!                   ***  SUBROUTINE  iom_open  ***
+      !!
+      !! ** Purpose : open an input file with NF90
+      !!---------------------------------------------------------------------
+      CHARACTER(len=*)       , INTENT(inout) ::   cdname    ! File name
+      INTEGER                , INTENT(  out) ::   kiomid    ! nf90 identifier of the opened file
+      LOGICAL                , INTENT(in   ) ::   ldwrt     ! read or write the file?
+      LOGICAL                , INTENT(in   ) ::   ldok      ! check the existence 
+      INTEGER, DIMENSION(2,5), INTENT(in   ) ::   kdompar   ! domain parameters: 
+
+      CHARACTER(LEN=100) ::   clinfo   ! info character
+      CHARACTER(LEN=100) ::   cltmp    ! temporary character
+      INTEGER            ::   iln      ! lengths of character
+      INTEGER            ::   istop    ! temporary storage of nstop
+      INTEGER            ::   if90id   ! nf90 identifier of the opened file
+      INTEGER            ::   idmy     ! dummy variable
+      !---------------------------------------------------------------------
+
+      clinfo = '                    iom_nf90_open ~~~  '
+      istop = nstop   ! store the actual value of nstop
+      !
+      IF( ldok ) THEN      ! Open existing file...
+         !                 ! =============
+         IF( ldwrt ) THEN  ! ... in write mode
+            IF(lwp) WRITE(numout,*) TRIM(clinfo)//' open existing file: '//TRIM(cdname)//' in WRITE mode'
+            CALL iom_nf90_check(NF90_OPEN(TRIM(cdname), NF90_WRITE  , if90id), clinfo)
+            CALL iom_nf90_check(NF90_SET_FILL( if90id, NF90_NOFILL, idmy    ), clinfo)
+         ELSE              ! ... in read mode
+            IF(lwp) WRITE(numout,*) TRIM(clinfo)//' open existing file: '//TRIM(cdname)//' in READ mode'
+            CALL iom_nf90_check(NF90_OPEN(TRIM(cdname), NF90_NOWRITE, if90id), clinfo)
+         ENDIF
+      ELSE                 ! the file does not exist
+         !                 ! =============
+         iln = INDEX( cdname, '.nc' )
+         IF( ldwrt ) THEN  ! the file should be open in write mode so we create it...
+            IF( jpnij > 1 ) THEN
+               WRITE(cltmp,'(a,a,i4.4,a)') cdname(1:iln-1), '_', narea-1, '.nc'
+               cdname = TRIM(cltmp)
+            ENDIF
+            IF(lwp) WRITE(numout,*) TRIM(clinfo)//' create new file: '//TRIM(cdname)//' in WRITE mode'
+            CALL iom_nf90_check(NF90_CREATE( TRIM(cdname), NF90_NOCLOBBER, if90id ), clinfo)
+            CALL iom_nf90_check(NF90_SET_FILL( if90id, NF90_NOFILL, idmy          ), clinfo)
+            ! define dimensions
+            CALL iom_nf90_check(NF90_DEF_DIM( if90id, 'x', kdompar(1,1)  , idmy ), clinfo)
+            CALL iom_nf90_check(NF90_DEF_DIM( if90id, 'y', kdompar(2,1)  , idmy ), clinfo)
+            CALL iom_nf90_check(NF90_DEF_DIM( if90id, 'z', jpk           , idmy ), clinfo)
+            CALL iom_nf90_check(NF90_DEF_DIM( if90id, 't', NF90_UNLIMITED, idmy ), clinfo)
+            ! global attributes
+            CALL iom_nf90_check(NF90_PUT_ATT( if90id, NF90_GLOBAL, 'DOMAIN_number_total'   , jpnij              ), clinfo)
+            CALL iom_nf90_check(NF90_PUT_ATT( if90id, NF90_GLOBAL, 'DOMAIN_number'         , narea-1            ), clinfo)
+            CALL iom_nf90_check(NF90_PUT_ATT( if90id, NF90_GLOBAL, 'DOMAIN_dimensions_ids' , (/1     , 2     /) ), clinfo)
+            CALL iom_nf90_check(NF90_PUT_ATT( if90id, NF90_GLOBAL, 'DOMAIN_size_global'    , (/jpiglo, jpjglo/) ), clinfo)
+            CALL iom_nf90_check(NF90_PUT_ATT( if90id, NF90_GLOBAL, 'DOMAIN_size_local'     , kdompar(:,1)       ), clinfo)
+            CALL iom_nf90_check(NF90_PUT_ATT( if90id, NF90_GLOBAL, 'DOMAIN_position_first' , kdompar(:,2)       ), clinfo)
+            CALL iom_nf90_check(NF90_PUT_ATT( if90id, NF90_GLOBAL, 'DOMAIN_position_last'  , kdompar(:,3)       ), clinfo)
+            CALL iom_nf90_check(NF90_PUT_ATT( if90id, NF90_GLOBAL, 'DOMAIN_halo_size_start', kdompar(:,4)       ), clinfo)
+            CALL iom_nf90_check(NF90_PUT_ATT( if90id, NF90_GLOBAL, 'DOMAIN_halo_size_end'  , kdompar(:,5)       ), clinfo)
+            CALL iom_nf90_check(NF90_PUT_ATT( if90id, NF90_GLOBAL, 'DOMAIN_type'           , 'BOX'              ), clinfo)
+         ELSE              ! the file should be open for read mode so it must exist...
+            CALL ctl_stop( TRIM(clinfo), 'File '//cdname(1:iln-1)//'* not found' )
+         ENDIF
+      ENDIF
+      ! start to fill file informations
+      ! =============
+      IF( istop == nstop ) THEN   ! no error within this routine
+         kiomid = MINLOC(iom_file(:)%nfid, dim = 1)
+         iom_file(kiomid)%name   = TRIM(cdname)
+         iom_file(kiomid)%nfid   = if90id
+         iom_file(kiomid)%iolib  = jpnf90
+         iom_file(kiomid)%nvars  = 0
+         iom_file(kiomid)%irec   = -1   ! useless for NetCDF files, used to know if the file is in define mode 
+         CALL iom_nf90_check(NF90_Inquire(if90id, unlimitedDimId = iom_file(kiomid)%iduld), clinfo)
+         IF(lwp) WRITE(numout,*) '                   ---> '//TRIM(cdname)//' OK'
+      ELSE
+         kiomid = 0               ! return error flag
+      ENDIF
+      !
+   END SUBROUTINE iom_nf90_open
+
+
+   SUBROUTINE iom_nf90_close( kiomid )
+      !!--------------------------------------------------------------------
+      !!                   ***  SUBROUTINE  iom_nf90_close  ***
+      !!
+      !! ** Purpose : close an input file with NF90
+      !!--------------------------------------------------------------------
+      INTEGER, INTENT(in) ::   kiomid   ! iom identifier of the file to be closed
+      CHARACTER(LEN=100)  ::   clinfo   ! info character
+      !---------------------------------------------------------------------
+      !
+      clinfo = '      iom_nf90_close    , file: '//TRIM(iom_file(kiomid)%name)
+      CALL iom_nf90_check(NF90_CLOSE(iom_file(kiomid)%nfid), clinfo)
+      !    
+   END SUBROUTINE iom_nf90_close
+
+
+   FUNCTION iom_nf90_varid ( kiomid, cdvar, kiv, kdimsz )  
+      !!-----------------------------------------------------------------------
+      !!                  ***  FUNCTION  iom_varid  ***
+      !!
+      !! ** Purpose : get the id of a variable in a file with NF90
+      !!-----------------------------------------------------------------------
+      INTEGER              , INTENT(in   )           ::   kiomid   ! file Identifier
+      CHARACTER(len=*)     , INTENT(in   )           ::   cdvar    ! name of the variable
+      INTEGER              , INTENT(in   )           ::   kiv   ! 
+      INTEGER, DIMENSION(:), INTENT(  out), OPTIONAL ::   kdimsz   ! size of the dimensions
+      !
+      INTEGER                        ::   iom_nf90_varid   ! iom variable Id
+      INTEGER                        ::   if90id           ! nf90 file identifier
+      INTEGER                        ::   ji               ! dummy loop index
+      INTEGER                        ::   ivarid           ! NetCDF  variable Id
+      INTEGER                        ::   i_nvd            ! number of dimension of the variable
+      INTEGER, DIMENSION(jpmax_dims) ::   idimid           ! dimension ids of the variable
+      LOGICAL                        ::   llok             ! ok  test
+      CHARACTER(LEN=100)             ::   clinfo           ! info character
+      !!-----------------------------------------------------------------------
+      clinfo = '          iom_nf90_varid, file: '//TRIM(iom_file(kiomid)%name)//', var: '//TRIM(cdvar)
+      iom_nf90_varid = 0                    ! default definition
+      IF( PRESENT(kdimsz) ) kdimsz(:) = 0   ! default definition
+      if90id = iom_file(kiomid)%nfid        ! get back NetCDF file id
+      !
+      llok = NF90_INQ_VARID( if90id, TRIM(cdvar), ivarid ) == nf90_noerr   ! does the variable exist in the file
+      IF( llok ) THEN
+         iom_nf90_varid = kiv
+         iom_file(kiomid)%nvars       = kiv
+         iom_file(kiomid)%nvid(kiv)   = ivarid
+         iom_file(kiomid)%cn_var(kiv) = TRIM(cdvar)
+         CALL iom_nf90_check(NF90_Inquire_Variable(if90id, ivarid, ndims = i_nvd), clinfo)   ! number of dimensions
+         iom_file(kiomid)%ndims(kiv)  = i_nvd
+         CALL iom_nf90_check(NF90_Inquire_Variable(if90id, ivarid, dimids = idimid(1:i_nvd)), clinfo)   ! dimensions ids
+         DO ji = 1, i_nvd   ! dimensions size
+            CALL iom_nf90_check(NF90_Inquire_Dimension(if90id, idimid(ji), len = iom_file(kiomid)%dimsz(ji,kiv)), clinfo)   
+            IF( idimid(ji) == iom_file(kiomid)%iduld ) iom_file(kiomid)%luld(kiv) = .TRUE.   ! unlimited dimension? 
+         END DO
+         !---------- Deal with scale_factor and offset
+         llok = NF90_Inquire_attribute(if90id, ivarid, 'scale_factor') == nf90_noerr
+         IF( llok) THEN
+            CALL iom_nf90_check(NF90_GET_ATT(if90id, ivarid, 'scale_factor', iom_file(kiomid)%scf(kiv)), clinfo)
+         ELSE
+            iom_file(kiomid)%scf(kiv) = 1.
+         END IF
+         llok = NF90_Inquire_attribute(if90id, ivarid, 'offset') == nf90_noerr
+         IF( llok ) THEN
+            CALL iom_nf90_check(NF90_GET_ATT(if90id, ivarid, 'offset', iom_file(kiomid)%ofs(kiv)), clinfo)
+         ELSE
+            iom_file(kiomid)%ofs(kiv) = 0.
+         END IF
+         ! return the simension size
+         IF( PRESENT(kdimsz) ) THEN 
+            IF( i_nvd == SIZE(kdimsz) ) THEN
+               kdimsz(:) = iom_file(kiomid)%dimsz(1:i_nvd,kiv)
+            ELSE
+               WRITE(ctmp1,*) i_nvd, SIZE(kdimsz)
+               CALL ctl_stop( TRIM(clinfo), 'error in kdimsz size'//TRIM(ctmp1) )
+            ENDIF
+         ENDIF
+!!$                  ELSE  
+!!$                     CALL ctl_warn( trim(clinfo), 'Variable '//trim(cdvar)// &
+!!$                        &                         ' is not found in the file '//trim(iom_file(kiomid)%name) )
+      ENDIF
+      !
+   END FUNCTION iom_nf90_varid
+
+
+   SUBROUTINE iom_nf90_g0d( kiomid, kvid, pvar )
+      !!-----------------------------------------------------------------------
+      !!                  ***  ROUTINE  iom_nf90_g0d  ***
+      !!
+      !! ** Purpose : read a scalar with NF90
+      !!-----------------------------------------------------------------------
+      INTEGER , INTENT(in   ) ::   kiomid   ! Identifier of the file
+      INTEGER , INTENT(in   ) ::   kvid     ! variable id
+      REAL(wp), INTENT(  out) ::   pvar     ! read field
+      !
+      CHARACTER(LEN=100)      ::   clinfo   ! info character
+      !---------------------------------------------------------------------
+      clinfo = 'iom_nf90_g0d , file: '//TRIM(iom_file(kiomid)%name)//', var: '//TRIM(iom_file(kiomid)%cn_var(kvid))
+      CALL iom_nf90_check(NF90_GET_VAR(iom_file(kiomid)%nfid, iom_file(kiomid)%nvid(kvid), pvar), clinfo )
+      ! 
+   END SUBROUTINE iom_nf90_g0d
+
+
+   SUBROUTINE iom_nf90_g123d( kiomid, kdom, kvid, knbdim, kstart, kcount,    &
+         &                          pv_r1d, pv_r2d, pv_r3d)
+      !!-----------------------------------------------------------------------
+      !!                  ***  ROUTINE  iom_nf90_g123d  ***
+      !!
+      !! ** Purpose : read a 1D/2D/3D variable with NF90
+      !!
+      !! ** Method : read ONE record at each CALL
+      !!-----------------------------------------------------------------------
+      INTEGER                    , INTENT(in   )           ::   kiomid    ! iom identifier of the file
+      INTEGER                    , INTENT(in   )           ::   kdom      ! Type of domain to be read
+      INTEGER                    , INTENT(in   )           ::   kvid      ! Name of the variable
+      INTEGER                    , INTENT(in   )           ::   knbdim    ! number of dimensions of the variable
+      INTEGER , DIMENSION(:)     , INTENT(in   )           ::   kstart    ! start position of the reading in each axis 
+      INTEGER , DIMENSION(:)     , INTENT(in   )           ::   kcount    ! number of points to be read in each axis
+      REAL(wp), DIMENSION(:)     , INTENT(  out), OPTIONAL ::   pv_r1d    ! read field (1D case)
+      REAL(wp), DIMENSION(:,:)   , INTENT(  out), OPTIONAL ::   pv_r2d    ! read field (2D case)
+      REAL(wp), DIMENSION(:,:,:) , INTENT(  out), OPTIONAL ::   pv_r3d    ! read field (3D case)
+      !
+      CHARACTER(LEN=100) ::   clinfo   ! info character
+      INTEGER            ::   if90id   ! nf90 identifier of the opened file
+      INTEGER            ::   ivid     ! nf90 variable id
+      !---------------------------------------------------------------------
+      clinfo = 'iom_nf90_g123d , file: '//TRIM(iom_file(kiomid)%name)//', var: '//TRIM(iom_file(kiomid)%cn_var(kvid))
+      if90id = iom_file(kiomid)%nfid         ! get back NetCDF file id
+      ivid   = iom_file(kiomid)%nvid(kvid)   ! get back NetCDF var id
+      IF( PRESENT(pv_r1d) ) THEN
+         CALL iom_nf90_check(NF90_GET_VAR(if90id, ivid, pv_r1d(:), start=kstart(1:knbdim), count=kcount(1:knbdim)), clinfo )
+      ELSEIF( PRESENT(pv_r2d) ) THEN
+         IF( kdom /= jpdom_unknown ) THEN
+            CALL iom_nf90_check(NF90_GET_VAR(if90id, ivid, pv_r2d(nldi:nlei,nldj:nlej),   &
+                  &                           start=kstart(1:knbdim), count=kcount(1:knbdim)), clinfo)
+            !--- Fill the overlap areas and extra hallows (mpp)
+            CALL lbc_lnk (pv_r2d,'Z',-999.,'no0')
+         ELSE
+            CALL iom_nf90_check(NF90_GET_VAR(if90id, ivid, pv_r2d(:,:), start=kstart(1:knbdim), count=kcount(1:knbdim)), clinfo)
+         ENDIF
+      ELSEIF( PRESENT(pv_r3d) ) THEN
+         IF( kdom /= jpdom_unknown ) THEN
+            CALL iom_nf90_check(NF90_GET_VAR(if90id, ivid, pv_r3d(nldi:nlei,nldj:nlej,:),   &
+                  &                           start=kstart(1:knbdim), count=kcount  (1:knbdim)), clinfo)
+            !--- Fill the overlap areas and extra hallows (mpp)
+            ! this if could be simplified with the new lbc_lnk that works with any size of the 3rd dimension
+            IF( kcount(3) == jpk ) THEN
+               CALL lbc_lnk( pv_r3d,'Z',-999.,'no0' )
+            ELSE   ! put some arbitrary value (a call to lbc_lnk will be done later...)
+               pv_r3d(nlei+1:jpi,      :   ,:) = 1.            
+               pv_r3d(      :   ,nlej+1:jpj,:) = 1.            
+            ENDIF
+         ELSE
+            CALL iom_nf90_check(NF90_GET_VAR(if90id, ivid, pv_r3d(:,:,:),   &
+                  &                           start=kstart(1:knbdim), count=kcount  (1:knbdim)), clinfo)
+         ENDIF
+      ENDIF
+      !
+   END SUBROUTINE iom_nf90_g123d
+
+
+   SUBROUTINE iom_nf90_gettime( kiomid, kvid, ptime )
+      !!--------------------------------------------------------------------
+      !!                   ***  SUBROUTINE iom_gettime  ***
+      !!
+      !! ** Purpose : read the time axis kvid in the file with NF90
+      !!--------------------------------------------------------------------
+      INTEGER               , INTENT(in   ) ::   kiomid   ! file Identifier
+      INTEGER               , INTENT(in   ) ::   kvid     ! variable id
+      REAL(wp), DIMENSION(:), INTENT(  out) ::   ptime    ! the time axis
+      !
+      CHARACTER(LEN=100) ::   clinfo     ! info character
+      !---------------------------------------------------------------------
+      clinfo = 'iom_nf90_gettime, file: '//TRIM(iom_file(kiomid)%name)//', var: '//TRIM(iom_file(kiomid)%cn_var(kvid))
+      CALL iom_nf90_check(NF90_GET_VAR(iom_file(kiomid)%nfid, iom_file(kiomid)%nvid(kvid), ptime(:),   &
+            &                           start=(/ 1 /), count=(/ iom_file(kiomid)%dimsz(1, kvid) /)), clinfo)
+      !
+   END SUBROUTINE iom_nf90_gettime
+
+
+   SUBROUTINE iom_nf90_rp0123d( kt, kwrite, kiomid, cdvar , kvid  , ktype,   &
+         &                               pv_r0d, pv_r1d, pv_r2d, pv_r3d )
+      !!--------------------------------------------------------------------
+      !!                   ***  SUBROUTINE  iom_nf90_rstput  ***
+      !!
+      !! ** Purpose : read the time axis cdvar in the file 
+      !!--------------------------------------------------------------------
+      INTEGER                         , INTENT(in)           ::   kt       ! ocean time-step
+      INTEGER                         , INTENT(in)           ::   kwrite   ! writing time-step
+      INTEGER                         , INTENT(in)           ::   kiomid   ! Identifier of the file 
+      CHARACTER(len=*)                , INTENT(in)           ::   cdvar    ! variable name
+      INTEGER                         , INTENT(in)           ::   kvid     ! variable id
+      INTEGER                         , INTENT(in), OPTIONAL ::   ktype    ! variable type (default R8)
+      REAL(wp)                        , INTENT(in), OPTIONAL ::   pv_r0d   ! written Od field
+      REAL(wp), DIMENSION(        jpk), INTENT(in), OPTIONAL ::   pv_r1d   ! written 1d field
+      REAL(wp), DIMENSION(jpi,jpj    ), INTENT(in), OPTIONAL ::   pv_r2d   ! written 2d field
+      REAL(wp), DIMENSION(jpi,jpj,jpk), INTENT(in), OPTIONAL ::   pv_r3d   ! written 3d field
+      !
+      INTEGER               :: idims                ! number of dimension
+      INTEGER               :: idvar                ! variable id
+      INTEGER               :: jd                   ! dimension loop counter   
+      INTEGER               :: ix1, ix2, iy1, iy2   ! subdomain indexes   
+      INTEGER, DIMENSION(4) :: idimsz               ! dimensions size  
+      INTEGER, DIMENSION(4) :: idimid               ! dimensions id
+      CHARACTER(LEN=100)    :: clinfo               ! info character
+      CHARACTER(LEN= 12), DIMENSION(4) :: cltmp     ! temporary character
+      INTEGER               :: if90id               ! nf90 file identifier
+      INTEGER               :: idmy                 ! dummy variable
+      INTEGER               :: itype                ! variable type
+      !---------------------------------------------------------------------
+      !
+      clinfo = '          iom_nf90_rp0123d, file: '//TRIM(iom_file(kiomid)%name)//', var: '//TRIM(cdvar)
+      if90id = iom_file(kiomid)%nfid
+      !
+      ! define dimension variables if it is not already done
+      ! ==========================
+      IF( iom_file(kiomid)%nvars == 0 ) THEN
+         ! are we in define mode?
+         IF( iom_file(kiomid)%irec /= -1 ) THEN   ! trick: irec used to know if the file is in define mode or not
+            CALL iom_nf90_check(NF90_REDEF( if90id ), clinfo)   ;   iom_file(kiomid)%irec = -1
+         ENDIF
+         ! define the dimension variables if it is not already done
+         cltmp = (/ 'nav_lon     ', 'nav_lat     ', 'nav_lev     ', 'time_counter' /)
+         CALL iom_nf90_check(NF90_DEF_VAR( if90id, TRIM(cltmp(1)), NF90_FLOAT , (/ 1, 2 /), iom_file(kiomid)%nvid(1) ), clinfo)
+         CALL iom_nf90_check(NF90_DEF_VAR( if90id, TRIM(cltmp(2)), NF90_FLOAT , (/ 1, 2 /), iom_file(kiomid)%nvid(2) ), clinfo)
+         CALL iom_nf90_check(NF90_DEF_VAR( if90id, TRIM(cltmp(3)), NF90_FLOAT , (/ 3    /), iom_file(kiomid)%nvid(3) ), clinfo)
+         CALL iom_nf90_check(NF90_DEF_VAR( if90id, TRIM(cltmp(4)), NF90_DOUBLE, (/ 4    /), iom_file(kiomid)%nvid(4) ), clinfo)
+         ! update informations structure related the dimension variable we just added...
+         iom_file(kiomid)%nvars       = 4
+         iom_file(kiomid)%luld(1:4)   = (/ .FALSE., .FALSE., .FALSE., .TRUE. /)
+         iom_file(kiomid)%cn_var(1:4) = cltmp
+         iom_file(kiomid)%ndims(1:4)  = (/ 2, 2, 1, 1 /)  
+         CALL iom_nf90_check(NF90_INQUIRE_DIMENSION( if90id, 1, len = iom_file(kiomid)%dimsz(1,1) ), clinfo)
+         CALL iom_nf90_check(NF90_INQUIRE_DIMENSION( if90id, 2, len = iom_file(kiomid)%dimsz(2,1) ), clinfo)
+         iom_file(kiomid)%dimsz(1:2, 2) = iom_file(kiomid)%dimsz(1:2, 1)
+         CALL iom_nf90_check(NF90_INQUIRE_DIMENSION( if90id, 3, len = iom_file(kiomid)%dimsz(1,3) ), clinfo)
+         iom_file(kiomid)%dimsz(1  , 4) = 1   ! unlimited dimension
+         IF(lwp) WRITE(numout,*) TRIM(clinfo)//' define dimension variables done'
+      ENDIF
+      ! define the data if it is not already done
+      ! ===============
+      IF( kvid <= 0 ) THEN
+         idvar = iom_file(kiomid)%nvars + 1
+         ! are we in define mode?
+         IF( iom_file(kiomid)%irec /= -1 ) THEN   ! trick: irec used to know if the file is in define mode or not
+            CALL iom_nf90_check(NF90_REDEF( if90id ), clinfo)   ;   iom_file(kiomid)%irec = -1
+         ENDIF
+         ! variable definition
+         IF(     PRESENT(pv_r0d) ) THEN   ;   idims = 0
+         ELSEIF( PRESENT(pv_r1d) ) THEN   ;   idims = 2   ;   idimid(1:idims) = (/    3,4/)
+         ELSEIF( PRESENT(pv_r2d) ) THEN   ;   idims = 3   ;   idimid(1:idims) = (/1,2  ,4/)
+         ELSEIF( PRESENT(pv_r3d) ) THEN   ;   idims = 4   ;   idimid(1:idims) = (/1,2,3,4/)
+         ENDIF
+         IF( PRESENT(ktype) ) THEN   ! variable external type
+            SELECT CASE (ktype)
+            CASE (jp_r8)  ;   itype = NF90_DOUBLE
+            CASE (jp_r4)  ;   itype = NF90_FLOAT
+            CASE (jp_i4)  ;   itype = NF90_INT
+            CASE (jp_i2)  ;   itype = NF90_SHORT
+            CASE (jp_i1)  ;   itype = NF90_BYTE
+            CASE DEFAULT   ;   CALL ctl_stop( TRIM(clinfo)//' unknown variable type' )
+            END SELECT
+         ELSE
+            itype = NF90_DOUBLE
+         ENDIF
+         IF( PRESENT(pv_r0d) ) THEN
+            CALL iom_nf90_check(NF90_DEF_VAR( if90id, TRIM(cdvar), itype,                    &
+                 &                            iom_file(kiomid)%nvid(idvar) ), clinfo)
+         ELSE
+            CALL iom_nf90_check(NF90_DEF_VAR( if90id, TRIM(cdvar), itype, idimid(1:idims),   &
+                 &                            iom_file(kiomid)%nvid(idvar) ), clinfo)
+         ENDIF
+         ! update informations structure related the new variable we want to add...
+         iom_file(kiomid)%nvars         = idvar
+         iom_file(kiomid)%cn_var(idvar) = TRIM(cdvar)
+         iom_file(kiomid)%scf(idvar)    = 1.
+         iom_file(kiomid)%ofs(idvar)    = 0.
+         iom_file(kiomid)%ndims(idvar)  = idims
+         IF( .NOT. PRESENT(pv_r0d) ) THEN   ;   iom_file(kiomid)%luld(idvar) = .TRUE.
+         ELSE                               ;   iom_file(kiomid)%luld(idvar) = .FALSE.
+         ENDIF
+         DO jd = 1, idims
+            CALL iom_nf90_check(NF90_INQUIRE_DIMENSION( if90id, idimid(jd), len = iom_file(kiomid)%dimsz(jd,idvar) ), clinfo)
+         END DO
+         IF(lwp) WRITE(numout,*) TRIM(clinfo)//' defined ok'
+      ELSE
+         idvar = kvid
+      ENDIF
+
+      ! time step kwrite : write the variable
+      IF( kt == kwrite ) THEN
+         ! are we in write mode?
+         IF( iom_file(kiomid)%irec == -1 ) THEN   ! trick: irec used to know if the file is in define mode or not
+            CALL iom_nf90_check(NF90_ENDDEF( if90id ), clinfo)   ;   iom_file(kiomid)%irec = 0
+         ENDIF
+         ! on what kind of domain must the data be written?
+         IF( PRESENT(pv_r2d) .OR. PRESENT(pv_r3d) ) THEN
+            idimsz(1:2) = iom_file(kiomid)%dimsz(1:2,idvar)
+            IF(     idimsz(1) == (nlei - nldi + 1) .AND. idimsz(2) == (nlej - nldj + 1) ) THEN
+               ix1 = nldi   ;   ix2 = nlei   ;   iy1 = nldj   ;   iy2 = nlej
+            ELSEIF( idimsz(1) == nlci              .AND. idimsz(2) == nlcj              ) THEN
+               ix1 = 1      ;   ix2 = nlci   ;   iy1 = 1      ;   iy2 = nlcj
+            ELSEIF( idimsz(1) == jpi               .AND. idimsz(2) == jpj               ) THEN
+               ix1 = 1      ;   ix2 = jpi    ;   iy1 = 1      ;   iy2 = jpj
+            ELSE 
+               CALL ctl_stop( 'iom_nf90_rp0123d: should have been an impossible case...' )
+            ENDIF
+
+            ! write dimension variables if it is not already done
+            ! =============
+            IF( iom_file(kiomid)%dimsz(1, 1) == 0 ) THEN
+               CALL iom_nf90_check(NF90_INQ_VARID( if90id, 'nav_lon'     , idmy ), clinfo)
+               CALL iom_nf90_check(NF90_PUT_VAR( if90id, idmy, glamt(ix1:ix2, iy1:iy2) ), clinfo)
+               CALL iom_nf90_check(NF90_INQ_VARID( if90id, 'nav_lat'     , idmy ), clinfo)
+               CALL iom_nf90_check(NF90_PUT_VAR( if90id, idmy, gphit(ix1:ix2, iy1:iy2) ), clinfo)
+               CALL iom_nf90_check(NF90_INQ_VARID( if90id, 'nav_lev'     , idmy ), clinfo)
+               CALL iom_nf90_check(NF90_PUT_VAR( if90id, idmy, gdept_0                 ), clinfo)
+               ! +++ WRONG VALUE: to be improved but not really useful...
+               CALL iom_nf90_check(NF90_INQ_VARID( if90id, 'time_counter', idmy ), clinfo)
+               CALL iom_nf90_check(NF90_PUT_VAR( if90id, idmy, kt                      ), clinfo)   
+               IF(lwp) WRITE(numout,*) TRIM(clinfo)//' write dimension variables done'
+            ENDIF
+         ENDIF
+
+         ! write the data
+         ! =============
+         IF(     PRESENT(pv_r0d) ) THEN
+            CALL iom_nf90_check(NF90_PUT_VAR( if90id, idvar, pv_r0d                      ), clinfo)
+         ELSEIF( PRESENT(pv_r1d) ) THEN
+            CALL iom_nf90_check(NF90_PUT_VAR( if90id, idvar, pv_r1d(                  :) ), clinfo)
+         ELSEIF( PRESENT(pv_r2d) ) THEN
+            CALL iom_nf90_check(NF90_PUT_VAR( if90id, idvar, pv_r2d(ix1:ix2, iy1:iy2   ) ), clinfo)
+         ELSEIF( PRESENT(pv_r3d) ) THEN
+            CALL iom_nf90_check(NF90_PUT_VAR( if90id, idvar, pv_r3d(ix1:ix2, iy1:iy2, :) ), clinfo)
+         ENDIF
+         ! add 1 to the size of the temporal dimension (not really useful...)
+         IF( iom_file(kiomid)%luld(idvar) )   iom_file(kiomid)%dimsz(iom_file(kiomid)%ndims(idvar), idvar)    &
+               &                            = iom_file(kiomid)%dimsz(iom_file(kiomid)%ndims(idvar), idvar) + 1
+         IF(lwp) WRITE(numout,*) TRIM(clinfo)//' written ok'
+      ENDIF
+      !     
+   END SUBROUTINE iom_nf90_rp0123d
+
+
+   SUBROUTINE iom_nf90_check( kstatus, cdinfo )
+      !!--------------------------------------------------------------------
+      !!                   ***  SUBROUTINE iom_nf90_check  ***
+      !!
+      !! ** Purpose :   check nf90 errors
+      !!--------------------------------------------------------------------
+      INTEGER,          INTENT(in) :: kstatus
+      CHARACTER(LEN=*), INTENT(in) :: cdinfo
+      !---------------------------------------------------------------------
+      IF(kstatus /= nf90_noerr)   CALL ctl_stop( 'iom_nf90_check : '//TRIM(nf90_strerror(kstatus)), TRIM(cdinfo) )
+   END SUBROUTINE iom_nf90_check
+
+
+   !!======================================================================
+END MODULE iom_nf90
Index: /trunk/NEMO/OFF_SRC/IOM/iom_rstdimg.F90
===================================================================
--- /trunk/NEMO/OFF_SRC/IOM/iom_rstdimg.F90	(revision 569)
+++ /trunk/NEMO/OFF_SRC/IOM/iom_rstdimg.F90	(revision 569)
@@ -0,0 +1,492 @@
+MODULE iom_rstdimg
+   !!=====================================================================
+   !!                    ***  MODULE  iom_rstdimg ***
+   !! Input/Output manager :  Library to read input rstdimg files
+   !!====================================================================
+   !! History :  9.0  ! 06 09  (S. Masson) Original code
+   !!--------------------------------------------------------------------
+
+   !!--------------------------------------------------------------------
+   !!   iom_open       : open a file read only
+   !!   iom_close      : close a file or all files opened by iom
+   !!   iom_get        : read a field (interfaced to several routines)
+   !!   iom_gettime    : read the time axis kvid in the file
+   !!   iom_varid      : get the id of a variable in a file
+   !!   iom_rstput     : write a field in a restart file (interfaced to several routines)
+   !!--------------------------------------------------------------------
+   USE in_out_manager  ! I/O manager
+   USE dom_oce         ! ocean space and time domain
+   USE lbclnk          ! lateal boundary condition / mpp exchanges
+   USE iom_def         ! iom variables definitions
+
+   IMPLICIT NONE
+   PRIVATE
+
+   PUBLIC iom_rstdimg_open, iom_rstdimg_close, iom_rstdimg_get, iom_rstdimg_rstput
+
+   INTERFACE iom_rstdimg_get
+      MODULE PROCEDURE iom_rstdimg_g0d, iom_rstdimg_g123d
+   END INTERFACE
+   INTERFACE iom_rstdimg_rstput
+      MODULE PROCEDURE iom_rstdimg_rp0d, iom_rstdimg_rp123d
+   END INTERFACE
+   !!----------------------------------------------------------------------
+   !!  OPA 9.0 , LOCEAN-IPSL (2006)
+   !! $Header$
+   !! Software governed by the CeCILL licence (modipsl/doc/NEMO_CeCILL.txt)
+   !!----------------------------------------------------------------------
+
+CONTAINS
+
+   SUBROUTINE iom_rstdimg_open( cdname, kiomid, ldwrt, ldok, kdompar )
+      !!---------------------------------------------------------------------
+      !!                   ***  SUBROUTINE  iom_open  ***
+      !!
+      !! ** Purpose :  open an input file read only (return 0 if not found)
+      !!---------------------------------------------------------------------
+      CHARACTER(len=*)       , INTENT(inout) ::   cdname   ! File name
+      INTEGER                , INTENT(  out) ::   kiomid   ! iom identifier of the opened file
+      LOGICAL                , INTENT(in   ) ::   ldwrt    ! read or write the file?
+      LOGICAL                , INTENT(in   ) ::   ldok     ! check the existence 
+      INTEGER, DIMENSION(2,5), INTENT(in   ) ::   kdompar  ! domain parameters: 
+
+      CHARACTER(LEN=100)                      ::   clinfo                     ! info character
+      CHARACTER(LEN=100)                      ::   cltmp                      ! temporary character
+      INTEGER                                 ::   jv                         ! loop counter
+      INTEGER                                 ::   istop                      ! temporary storage of nstop
+      INTEGER                                 ::   idrst                      ! logical unit of the restart file
+      INTEGER                                 ::   iln                        ! lengths of character
+      INTEGER                                 ::   irecl8                     ! record length
+      INTEGER                                 ::   ios                        ! IO status
+      INTEGER                                 ::   ivnum                      ! number of variables
+      INTEGER                                 ::   ishft                      ! counter shift
+      INTEGER                                 ::   inx, iny, inz              ! x,y,z dimension of the variable
+      INTEGER                                 ::   in0d, in1d, in2d, in3d     ! number of 0/1/2/3D variables
+      INTEGER                                 ::   ipni, ipnj, ipnij, iarea   ! domain decomposition 
+      CHARACTER(LEN=8), DIMENSION(jpmax_vars) ::   clna0d, clna1d, clna2d, clna3d     ! name of 0/1/2/3D variables
+      REAL(wp),         DIMENSION(jpmax_vars) ::   zval0d, zval1d, zval2d, zval3d     ! value of 0d variables or record
+      !                                                                               ! position for 1/2/3D variables
+      !---------------------------------------------------------------------
+
+      clinfo = '                    iom_rstdimg_open ~~~  '
+      istop = nstop      ! store the actual value of nstop
+      ios = 0            ! default definition
+      kiomid = 0         ! default definition
+      ! get a free unit
+      idrst = getunit()  ! get a free logical unit for the restart file
+!!$#if defined key_agrif 
+!!$      idrst = Agrif_Get_Unit()      
+!!$#endif 
+      ! Open the file...
+      ! =============
+      IF( ldok ) THEN      ! Open existing file...
+         ! find the record length
+         OPEN( idrst, FILE = TRIM(cdname), FORM = 'unformatted', ACCESS = 'direct'   &
+            &       , RECL = 8, STATUS = 'old', ACTION = 'read', IOSTAT = ios, ERR = 987 )
+         READ( idrst, REC = 1, IOSTAT = ios, ERR = 987 ) irecl8
+         CLOSE( idrst )
+         ! Open the file with the appropriate record length and parameters
+         IF( ldwrt ) THEN  ! ... in readwrite mode
+            IF(lwp) WRITE(numout,*) TRIM(clinfo)//' open existing file: '//TRIM(cdname)//' in READWRITE mode'
+            OPEN( idrst, FILE = TRIM(cdname), FORM = 'unformatted', ACCESS = 'direct'   &
+               &       , RECL = irecl8, STATUS = 'old', ACTION = 'readwrite', IOSTAT = ios, ERR = 987 )
+         ELSE              ! ... in read mode
+            IF(lwp) WRITE(numout,*) TRIM(clinfo)//' open existing file: '//TRIM(cdname)//' in READ mode'
+            OPEN( idrst, FILE = TRIM(cdname), FORM = 'unformatted', ACCESS = 'direct'   &
+               &       , RECL = irecl8, STATUS = 'old', ACTION = 'read'     , IOSTAT = ios, ERR = 987 )
+         ENDIF
+      ELSE                 ! the file does not exist
+         iln = INDEX( cdname, '.dimg' )
+         IF( ldwrt ) THEN  ! the file should be open in readwrite mode so we create it...
+            irecl8= kdompar(1,1) * kdompar(2,1) * wp
+            IF( jpnij > 1 ) THEN
+               WRITE(cltmp,'(a,a,i4.4,a)') cdname(1:iln-1), '_', narea-1, '.dimg'
+               cdname = TRIM(cltmp)
+            ENDIF
+            IF(lwp) WRITE(numout,*) TRIM(clinfo)//' create new file: '//TRIM(cdname)//' in READWRITE mode'
+            OPEN( idrst, FILE = TRIM(cdname), FORM = 'UNFORMATTED', ACCESS = 'DIRECT'   &
+               &       , RECL = irecl8, STATUS = 'new', ACTION = 'readwrite', IOSTAT = ios, ERR = 987 )
+         ELSE              ! the file should be open for read mode so it must exist...
+            CALL ctl_stop( TRIM(clinfo), 'File '//cdname(1:iln-1)//'* not found' )
+         ENDIF
+      ENDIF
+      ! Performs checks on the file
+      ! =============
+      IF( ldok ) THEN      ! old file
+         READ( idrst, REC = 1, IOSTAT = ios, ERR = 987 ) irecl8, inx, iny, inz, in0d, in1d, in2d, in3d
+         READ( idrst, REC = 1, IOSTAT = ios, ERR = 987 ) irecl8, inx, iny, inz, in0d, in1d, in2d, in3d,   &
+            &   clna0d(1:in0d), zval0d(1:in0d), clna1d(1:in1d), zval1d(1:in1d),   &
+            &   clna2d(1:in2d), zval2d(1:in2d), clna3d(1:in3d), zval3d(1:in3d),   &
+            &   ipni, ipnj, ipnij, iarea 
+         clinfo = TRIM(clinfo)//' file '//TRIM(cdname)
+         IF( inx   /= kdompar(1,1) )   CALL ctl_stop( TRIM(clinfo), 'Mismatch in domain size in i direction' )
+         IF( iny   /= kdompar(2,1) )   CALL ctl_stop( TRIM(clinfo), 'Mismatch in domain size in j direction' )
+         IF( inz   /= jpk          )   CALL ctl_stop( TRIM(clinfo), 'Mismatch in domain size in k direction' )
+         IF( ipni  /= jpni         )   CALL ctl_stop( TRIM(clinfo), 'Processor splitting changed along I' )
+         IF( ipnj  /= jpnj         )   CALL ctl_stop( TRIM(clinfo), 'Processor splitting changed along J' )
+         IF( ipnij /= jpnij        )   CALL ctl_stop( TRIM(clinfo), 'Total number of processors changed' )
+         IF( iarea /= narea        )   CALL ctl_stop( TRIM(clinfo), 'Mismatch in area numbering ...' )
+      ENDIF
+      ! fill file informations
+      ! =============
+      IF( istop == nstop ) THEN   ! no error within this routine
+         kiomid = MINLOC(iom_file(:)%nfid, dim = 1)
+         iom_file(kiomid)%name    = TRIM(cdname)
+         iom_file(kiomid)%nfid    = idrst
+         iom_file(kiomid)%iolib   = jprstdimg
+         iom_file(kiomid)%iduld   = -1
+         IF( ldok ) THEN      ! old file
+            ! read variables informations from the file header
+            IF(  TRIM(clna0d(1)) == 'no0d' )   in0d = 0
+            IF(  TRIM(clna1d(1)) == 'no1d' )   in1d = 0
+            IF(  TRIM(clna2d(1)) == 'no2d' )   in2d = 0
+            IF(  TRIM(clna3d(1)) == 'no3d' )   in3d = 0
+            ivnum = in0d + in1d + in2d + in3d
+            iom_file(kiomid)%nvars            = ivnum
+            iom_file(kiomid)%irec             = 2 + in1d + in2d + inz * in3d
+            iom_file(kiomid)%luld(   1:ivnum) = .FALSE.
+            iom_file(kiomid)%scf(    1:ivnum) = 1.
+            ! scalar variable
+            DO jv = 1, in0d
+               iom_file(kiomid)%cn_var(jv) = TRIM(clna0d(jv))
+               iom_file(kiomid)%nvid(  jv) = 1
+               iom_file(kiomid)%ndims( jv) = 0
+               iom_file(kiomid)%ofs(   jv) = zval0d(jv)   ! warning: trick... we use ofs to store the value
+            END DO
+            ! 1d variable
+            ishft = in0d
+            DO jv = 1, in1d
+               iom_file(kiomid)%cn_var(    ishft + jv) = TRIM(clna1d(jv))
+               iom_file(kiomid)%nvid(      ishft + jv) = zval1d(jv)
+               iom_file(kiomid)%ndims(     ishft + jv) = 1
+               iom_file(kiomid)%dimsz(1  , ishft + jv) = jpk
+               iom_file(kiomid)%ofs(       ishft + jv) = 0.
+            END DO
+            ! 2d variable
+            ishft = in0d + in1d
+            DO jv = 1, in2d
+               iom_file(kiomid)%cn_var(    ishft + jv) = TRIM(clna2d(jv))
+               iom_file(kiomid)%nvid(      ishft + jv) = zval2d(jv)
+               iom_file(kiomid)%ndims(     ishft + jv) = 2
+               iom_file(kiomid)%dimsz(1:2, ishft + jv) = (/ inx, iny /)
+               iom_file(kiomid)%ofs(       ishft + jv) = 0.
+            END DO
+            ! 3d variable
+            ishft = in0d + in1d + in2d 
+            DO jv = 1, in3d
+               iom_file(kiomid)%cn_var(    ishft + jv) = TRIM(clna3d(jv))
+               iom_file(kiomid)%nvid(      ishft + jv) = zval3d(jv)
+               iom_file(kiomid)%ndims(     ishft + jv) = 3
+               iom_file(kiomid)%dimsz(1:3, ishft + jv) = (/ inx, iny, jpk /)
+               iom_file(kiomid)%ofs(       ishft + jv) = 0.
+            END DO
+         ELSE                 ! new file
+            iom_file(kiomid)%nvars = 0
+            iom_file(kiomid)%irec  = 2
+            ! store file informations
+            WRITE( idrst, REC = 1, IOSTAT = ios, ERR = 987 ) irecl8, kdompar(:,1), jpk   ! store domain size
+         ENDIF
+      ENDIF
+987   CONTINUE
+      IF( ios /= 0 ) THEN
+         WRITE(ctmp1,*) '           iostat = ', ios
+         CALL ctl_stop( TRIM(clinfo), '   error in opening file '//TRIM(cdname), ctmp1 )
+      ENDIF
+      !
+   END SUBROUTINE iom_rstdimg_open
+
+
+   SUBROUTINE iom_rstdimg_close( kiomid )
+      !!--------------------------------------------------------------------
+      !!                   ***  SUBROUTINE  iom_rstdimg_close  ***
+      !!
+      !! ** Purpose : close an input file
+      !!--------------------------------------------------------------------
+      INTEGER, INTENT(in) ::   kiomid   ! iom identifier of the file to be closed
+      !
+      CHARACTER(LEN=100)                      ::   clinfo                     ! info character
+      INTEGER                                 ::   jv                         ! loop counter
+      INTEGER                                 ::   irecl8                     ! record length
+      INTEGER                                 ::   ios                        ! IO status
+      INTEGER                                 ::   ivnum                      ! number of variables
+      INTEGER                                 ::   idrst                      ! file logical unit
+      INTEGER                                 ::   inx, iny, inz              ! x,y,z dimension of the variable
+      INTEGER                                 ::   in0d, in1d, in2d, in3d     ! number of 0/1/2/3D variables
+      CHARACTER(LEN=8), DIMENSION(jpmax_vars) ::   clna0d, clna1d, clna2d, clna3d     ! name of 0/1/2/3D variables
+      REAL(wp),         DIMENSION(jpmax_vars) ::   zval0d, zval1d, zval2d, zval3d     ! value of 0d variables or record
+      !                                                                               ! position for 1/2/3D variables
+      !---------------------------------------------------------------------
+      !
+      clinfo = '                    iom_rstdimg_close ~~~  '
+      idrst = iom_file(kiomid)%nfid   ! get back the logical unit of the restart file
+      ! test if we can write in the file (test with INQUIRE gives alsways YES even with read only files...)
+      READ(  idrst, REC = 1, IOSTAT = ios, ERR = 987 ) irecl8, inx, iny, inz   
+      WRITE( idrst, REC = 1, IOSTAT = ios            ) irecl8, inx, iny, inz   
+      ! We can write in the file => we update its header before closing
+      IF( ios == 0 ) THEN
+         READ( idrst, REC = 1, IOSTAT = ios, ERR = 987 ) irecl8, inx, iny, inz   ! get back domain size
+         ivnum = iom_file(kiomid)%nvars
+         in0d = 0   ;   in1d = 0   ;   in2d = 0   ;   in3d = 0
+         DO jv = 1, ivnum   ! loop on each variable to get its name and value/record position
+            SELECT CASE (iom_file(kiomid)%ndims(jv))
+            CASE (0)   ! scalar variable
+               in0d = in0d + 1
+               clna0d(in0d) = TRIM(iom_file(kiomid)%cn_var(jv))
+               zval0d(in0d) = iom_file(kiomid)%ofs(jv)   ! warning: trick... we use ofs to store the value
+            CASE (1)   ! 1d variable
+               in1d = in1d + 1
+               clna1d(in1d) = TRIM(iom_file(kiomid)%cn_var(jv))
+               zval1d(in1d) = iom_file(kiomid)%nvid(jv)
+            CASE (2)   ! 2d variable
+               in2d = in2d + 1
+               clna2d(in2d) = TRIM(iom_file(kiomid)%cn_var(jv))
+               zval2d(in2d) = iom_file(kiomid)%nvid(jv)
+            CASE (3)   ! 3d variable
+               in3d = in3d + 1
+               clna3d(in3d) = TRIM(iom_file(kiomid)%cn_var(jv))
+               zval3d(in3d) = iom_file(kiomid)%nvid(jv)
+            CASE DEFAULT   ;   CALL ctl_stop( TRIM(clinfo), 'Should not ne there...' )
+            END SELECT
+         END DO
+         ! force to have at least 1 valriable in each list (not necessary (?), but safer)
+         IF( in0d == 0 ) THEN   ;   in0d = 1   ;   clna0d(1) = 'no0d'   ;   zval0d(1) = -1.   ;   ENDIF
+         IF( in1d == 0 ) THEN   ;   in1d = 1   ;   clna1d(1) = 'no1d'   ;   zval1d(1) = -1.   ;   ENDIF
+         IF( in2d == 0 ) THEN   ;   in2d = 1   ;   clna2d(1) = 'no2d'   ;   zval2d(1) = -1.   ;   ENDIF
+         IF( in3d == 0 ) THEN   ;   in3d = 1   ;   clna3d(1) = 'no3d'   ;   zval3d(1) = -1.   ;   ENDIF
+         ! update the file header before closing it
+         WRITE( idrst, REC = 1, IOSTAT = ios, ERR = 987 ) irecl8, inx, iny, inz, in0d, in1d, in2d, in3d,   &
+            &   clna0d(1:in0d), zval0d(1:in0d), clna1d(1:in1d), zval1d(1:in1d),   &
+            &   clna2d(1:in2d), zval2d(1:in2d), clna3d(1:in3d), zval3d(1:in3d),   &
+            &   jpni, jpnj, jpnij, narea, jpiglo, jpjglo, nlcit, nlcjt, nldit, nldjt, nleit, nlejt, nimppt, njmppt
+      ELSE
+         ios = 0   ! we cannot write in the file
+      ENDIF
+      !
+      CLOSE( idrst, IOSTAT = ios, ERR = 987 )
+987   CONTINUE
+      IF( ios /= 0 ) THEN
+         WRITE(ctmp1,*) '           iostat = ', ios
+         CALL ctl_stop( TRIM(clinfo),   &
+            &   '   error when updating the header of '//TRIM(iom_file(kiomid)%name), ctmp1 )
+      ENDIF
+      !    
+   END SUBROUTINE iom_rstdimg_close
+
+
+   SUBROUTINE iom_rstdimg_g0d( kiomid, kvid, pvar )
+      !!-----------------------------------------------------------------------
+      !!                  ***  ROUTINE  iom_rstdimg_g0d  ***
+      !!
+      !! ** Purpose : read a scalar with RSTDIMG
+      !!-----------------------------------------------------------------------
+      INTEGER,  INTENT(in   ) ::   kiomid    ! Identifier of the file
+      INTEGER,  INTENT(in   ) ::   kvid      ! variable id
+      REAL(wp), INTENT(  out) ::   pvar      ! read field
+      !---------------------------------------------------------------------
+      !
+      pvar = iom_file(kiomid)%ofs(kvid)   ! warning: trick... we use ofs to store the value
+      !
+   END SUBROUTINE iom_rstdimg_g0d
+
+
+   SUBROUTINE iom_rstdimg_rp0d( kiomid, cdvar, pv_r0d )
+      !!--------------------------------------------------------------------
+      !!                   ***  SUBROUTINE  iom_rstdimg_rstput  ***
+      !!
+      !! ** Purpose : write a scalar with RSTDIMG
+      !!--------------------------------------------------------------------
+      INTEGER                   , INTENT(in) ::   kiomid   ! Identifier of the file 
+      CHARACTER(len=*)          , INTENT(in) ::   cdvar    ! time axis name
+      REAL(wp)                  , INTENT(in) ::   pv_r0d   ! written 0d field
+      !
+      CHARACTER(LEN=100) ::   clinfo     ! info character
+      INTEGER            ::   idvar      ! variable id
+      !---------------------------------------------------------------------
+      !  
+      clinfo = '                    iom_rstdimg_rp0d ~~~  '
+      idvar = iom_file(kiomid)%nvars + 1
+      IF( idvar <= jpmax_vars ) THEN
+         iom_file(kiomid)%nvars = idvar
+         iom_file(kiomid)%cn_var(idvar) = TRIM(cdvar)
+         iom_file(kiomid)%nvid(   idvar) = 1   ! useless, Od variables a strored in record 1
+         iom_file(kiomid)%ndims(  idvar) = 0
+         iom_file(kiomid)%luld(   idvar) = .FALSE.
+         iom_file(kiomid)%scf(    idvar) = 1.
+         iom_file(kiomid)%ofs(    idvar) = pv_r0d   ! warning: trick... we use ofs to store the value
+      ELSE
+         CALL ctl_stop( TRIM(clinfo), 'increase the value of jpmax_vars' )
+      ENDIF
+   END SUBROUTINE iom_rstdimg_rp0d
+
+
+   SUBROUTINE iom_rstdimg_g123d( kiomid, kdom  , kvid  , kstart, kcount,    &
+         &                          pv_r1d, pv_r2d, pv_r3d )
+      !!-----------------------------------------------------------------------
+      !!                  ***  ROUTINE  iom_rstdimg_g123d  ***
+      !!
+      !! ** Purpose : read a 1D/2D/3D variable with RSTDIMG
+      !!
+      !! ** Method : read ONE record at each CALL
+      !!-----------------------------------------------------------------------
+      INTEGER                    , INTENT(in   )           ::   kiomid     ! iom identifier of the file
+      INTEGER                    , INTENT(in   )           ::   kdom       ! Type of domain to be read
+      INTEGER                    , INTENT(in   )           ::   kvid       ! variable id
+      INTEGER , DIMENSION(:)     , INTENT(in   )           ::   kstart     ! start position of the reading in each axis 
+      INTEGER , DIMENSION(:)     , INTENT(in   )           ::   kcount     ! number of points to be read in each axis
+      REAL(wp), DIMENSION(:)     , INTENT(  out), OPTIONAL ::   pv_r1d     ! read field (1D case)
+      REAL(wp), DIMENSION(:,:)   , INTENT(  out), OPTIONAL ::   pv_r2d     ! read field (2D case)
+      REAL(wp), DIMENSION(:,:,:) , INTENT(  out), OPTIONAL ::   pv_r3d     ! read field (3D case)
+
+      CHARACTER(LEN=100) ::   clinfo               ! info character
+      INTEGER            ::   ios                  ! IO status
+      INTEGER            ::   jk                   ! loop counter
+      INTEGER            ::   idrst                ! logical unit of the restart file
+      INTEGER            ::   istop                ! temporary storage of nstop
+      INTEGER            ::   ix1, ix2, iy1, iy2   ! subdomain indexes
+      !---------------------------------------------------------------------
+      clinfo = '                    iom_rstdimg_g123d ~~~  '
+      !
+      istop = nstop                   ! store the actual value of nstop
+      idrst = iom_file(kiomid)%nfid   ! get back the logical unit of the restart file
+      IF( kdom == jpdom_data .OR. kdom == jpdom_global )   &
+            &   CALL ctl_stop( TRIM(clinfo), TRIM(iom_file(kiomid)%cn_var(kvid))//': case not coded for rstdimg files' )
+      !
+      IF( istop == nstop .AND. kvid > 0 ) THEN
+         IF( .NOT. PRESENT(pv_r1d) ) THEN
+            SELECT CASE (kdom)   ! find the right index of the array to be read
+            CASE (jpdom_local_full)      ;   ix1 = nldi   ;   ix2 = nlei   ;   iy1 = nldj   ;   iy2 = nlej
+            CASE (jpdom_local_noextra)   ;   ix1 = 1      ;   ix2 = nlci   ;   iy1 = 1      ;   iy2 = nlcj
+            CASE (jpdom_local_noovlap)   ;   ix1 = nldi   ;   ix2 = nlei   ;   iy1 = nldj   ;   iy2 = nlej
+            CASE DEFAULT                 ;   CALL ctl_stop( clinfo, 'we should not be there...' )
+            END SELECT
+         ENDIF
+         IF(     PRESENT(pv_r1d) ) THEN   ! read 1D variables
+            READ( idrst, REC = iom_file(kiomid)%nvid(kvid), IOSTAT = ios, ERR = 987 )   pv_r1d(:)
+         ELSEIF( PRESENT(pv_r2d) ) THEN   ! read 2D variables
+            READ( idrst, REC = iom_file(kiomid)%nvid(kvid), IOSTAT = ios, ERR = 987 )   pv_r2d(ix1:ix2, iy1:iy2 )
+            SELECT CASE (kdom)
+            CASE (jpdom_local_noextra)   !--- Fill extra hallows (mpp)       
+               pv_r2d(nlci+1:jpi,      :   ) = 1.    
+               pv_r2d(      :   ,nlcj+1:jpj) = 1.            
+            CASE (jpdom_local_noovlap)   !--- Fill the overlap areas and extra hallows (mpp)
+               CALL lbc_lnk (pv_r2d,'Z',-999.,'no0')
+            CASE DEFAULT
+            END SELECT
+         ELSEIF( PRESENT(pv_r3d) ) THEN   ! read 3D variables
+            DO jk = 1, iom_file(kiomid)%dimsz(3,kvid)   ! do loop on each level
+               READ( idrst, REC = iom_file(kiomid)%nvid(kvid) + jk - 1, IOSTAT = ios, ERR = 987 )   &
+                     &      pv_r3d( ix1:ix2, iy1:iy2, jk )
+            END DO
+            SELECT CASE (kdom)
+            CASE (jpdom_local_noextra)   !--- Fill extra hallows (mpp)       
+               pv_r3d(nlci+1:jpi,      :   ,:) = 1.    
+               pv_r3d(      :   ,nlcj+1:jpj,:) = 1.            
+            CASE (jpdom_local_noovlap)   !--- Fill the overlap areas and extra hallows (mpp)
+               ! this if could be simplified with the new lbc_lnk that works with any size of the 3rd dimension
+               IF( kcount(3) == jpk ) THEN
+                  CALL lbc_lnk( pv_r3d,'Z',-999.,'no0' )
+               ELSE
+                  pv_r3d(nlei+1:jpi,      :   ,:) = 1.            
+                  pv_r3d(      :   ,nlej+1:jpj,:) = 1.            
+               ENDIF
+            CASE DEFAULT
+            END SELECT
+         ENDIF
+      ENDIF
+987   CONTINUE
+      IF( ios /= 0 ) THEN
+         WRITE(ctmp1,*) '           iostat = ', ios
+         CALL ctl_stop( TRIM(clinfo), '   IO error with file '//TRIM(iom_file(kiomid)%name), ctmp1 )
+      ENDIF
+      !
+   END SUBROUTINE iom_rstdimg_g123d
+
+
+   SUBROUTINE iom_rstdimg_rp123d( kiomid, cdvar, pv_r1d, pv_r2d, pv_r3d )
+      !!--------------------------------------------------------------------
+      !!                   ***  SUBROUTINE  iom_rstdimg_rstput  ***
+      !!
+      !! ** Purpose : write a 2D/3D variable with RSTDIMG
+      !!--------------------------------------------------------------------
+      INTEGER                         , INTENT(in)           ::   kiomid   ! Identifier of the file 
+      CHARACTER(len=*)                , INTENT(in)           ::   cdvar    ! time axis name
+      REAL(wp), DIMENSION(        jpk), INTENT(in), OPTIONAL ::   pv_r1d   ! written 1d field
+      REAL(wp), DIMENSION(jpi,jpj    ), INTENT(in), OPTIONAL ::   pv_r2d   ! written 2d field
+      REAL(wp), DIMENSION(jpi,jpj,jpk), INTENT(in), OPTIONAL ::   pv_r3d   ! written 3d field
+      !
+      CHARACTER(LEN=100)    ::   clinfo               ! info character
+      INTEGER               ::   irecl8               ! reacord length
+      INTEGER               ::   ios                  ! IO status
+      INTEGER               ::   idrst                ! reacord length
+      INTEGER               ::   inx, iny, inz        ! x,y,z dimension of the variable
+      INTEGER               ::   idvar                ! variable id
+      INTEGER               ::   istop                ! temporary storage of nstop
+      INTEGER               ::   irec                 ! record number
+      INTEGER               ::   ix1, ix2, iy1, iy2   ! subdomain indexes
+      INTEGER               ::   jk                   ! loop counter
+      !---------------------------------------------------------------------
+      !
+      clinfo = '          iom_rstdimg_rp123d, file: '//TRIM(iom_file(kiomid)%name)//', var: '//TRIM(cdvar)
+      istop = nstop                   ! store the actual value of nstop
+      irec = iom_file(kiomid)%irec    ! get back the record number of the variable
+      idrst = iom_file(kiomid)%nfid   ! get back the logical unit of the restart file
+      idvar = iom_file(kiomid)%nvars + 1
+      IF( idvar > jpmax_vars )   CALL ctl_stop( TRIM(clinfo), 'increase the value of jpmax_vars' )
+      IF( .NOT. PRESENT(pv_r1d) ) THEN
+         ! find which part of data must be written
+         READ( idrst, REC = 1, IOSTAT = ios, ERR = 987 ) irecl8, inx, iny, inz
+         IF(     inx == (nlei - nldi + 1) .AND. iny == (nlej - nldj + 1) ) THEN
+            ix1 = nldi   ;   ix2 = nlei   ;   iy1 = nldj   ;   iy2 = nlej
+         ELSEIF( inx == nlci              .AND. iny == nlcj              ) THEN
+            ix1 = 1      ;   ix2 = nlci   ;   iy1 = 1      ;   iy2 = nlcj
+         ELSEIF( inx == jpi               .AND. iny == jpj               ) THEN
+            ix1 = 1      ;   ix2 = jpi    ;   iy1 = 1      ;   iy2 = jpj
+         ELSE 
+            CALL ctl_stop( clinfo, 'should have been an impossible case...' )
+         ENDIF
+      ENDIF
+      IF( istop == nstop ) THEN
+         ! write the data
+         IF(     PRESENT(pv_r1d) ) THEN   ! 1D variable
+            WRITE( idrst, REC = irec            , IOSTAT = ios, ERR = 987 ) pv_r1d(:)
+         ELSEIF( PRESENT(pv_r2d) ) THEN   ! 2D variable
+            WRITE( idrst, REC = irec            , IOSTAT = ios, ERR = 987 ) pv_r2d(ix1:ix2, iy1:iy2    )
+         ELSEIF( PRESENT(pv_r3d) ) THEN   ! 3D variable
+            DO jk = 1, jpk   ! do loop on each level
+               WRITE( idrst, REC = irec + jk - 1, IOSTAT = ios, ERR = 987 ) pv_r3d(ix1:ix2, iy1:iy2, jk)
+            END DO
+         ENDIF
+         ! fill the file informations
+         iom_file(kiomid)%nvars = idvar
+         IF(     PRESENT(pv_r1d) ) THEN
+            iom_file(kiomid)%irec              = irec + 1
+            iom_file(kiomid)%ndims(     idvar) = 1
+            iom_file(kiomid)%dimsz(1  , idvar) = inz
+         ELSEIF( PRESENT(pv_r2d) ) THEN
+            iom_file(kiomid)%irec              = irec + 1
+            iom_file(kiomid)%ndims(     idvar) = 2
+            iom_file(kiomid)%dimsz(1:2, idvar) = (/ inx, iny /)
+         ELSEIF( PRESENT(pv_r3d) ) THEN
+            iom_file(kiomid)%irec              = irec + inz
+            iom_file(kiomid)%ndims(     idvar) = 3
+            iom_file(kiomid)%dimsz(1:3, idvar) = (/ inx, iny, inz /)
+         ENDIF
+         iom_file(kiomid)%cn_var(idvar) = TRIM(cdvar)
+         iom_file(kiomid)%nvid(  idvar) = irec
+         iom_file(kiomid)%luld(  idvar) = .FALSE.
+         iom_file(kiomid)%scf(   idvar) = 1.
+         iom_file(kiomid)%ofs(   idvar) = 0.
+      ENDIF
+987   CONTINUE
+      IF( ios /= 0 ) THEN
+         WRITE(ctmp1,*) '           iostat = ', ios
+         CALL ctl_stop( TRIM(clinfo), '   IO error with file '//TRIM(iom_file(kiomid)%name), ctmp1 )
+      ELSE
+         IF(lwp) WRITE(numout,*) TRIM(clinfo)//' written ok'
+      ENDIF
+      !     
+   END SUBROUTINE iom_rstdimg_rp123d
+
+
+   !!======================================================================
+END MODULE iom_rstdimg
