Index: branches/2014/dev_r4650_UKMO14.12_STAND_ALONE_OBSOPER/NEMOGCM/TOOLS/SIREN/src/create_restart.f90
===================================================================
--- branches/2014/dev_r4650_UKMO14.12_STAND_ALONE_OBSOPER/NEMOGCM/TOOLS/SIREN/src/create_restart.f90	(revision 4810)
+++ branches/2014/dev_r4650_UKMO14.12_STAND_ALONE_OBSOPER/NEMOGCM/TOOLS/SIREN/src/create_restart.f90	(revision 5600)
@@ -7,27 +7,150 @@
 !
 ! DESCRIPTION:
+!> @file
 !> @brief 
 !> This program create restart file.
 !>
 !> @details
-!> Variables are read from restart file, or standard output.
-!> Then theses variables are interpolated on fine grid. 
-!> Finally table are split over new decomposition. 
-!>
-!> @author
-!> J.Paul
+!> @section sec1 method
+!> Variables could be extracted from fine grid file, interpolated from coarse
+!> grid file or restart file, or manually written.<br/> 
+!> Then they are split over new decomposition. 
+!> @note 
+!>    method could be different for each variable.
+!>
+!> @section sec2 how to
+!>    to create restart file:<br/>
+!> @code{.sh}
+!>    ./SIREN/bin/create_restart create_restart.nam
+!> @endcode
+!>    
+!>    create_restart.nam comprise 9 namelists:<br/>
+!>       - logger namelist (namlog)
+!>       - config namelist (namcfg)
+!>       - coarse grid namelist (namcrs)
+!>       - fine grid namelist (namfin)
+!>       - vertical grid namelist (namzgr)
+!>       - partial step namelist (namzps)
+!>       - variable namelist (namvar)
+!>       - nesting namelist (namnst)
+!>       - output namelist (namout)
+!>    
+!>    @note 
+!>       All namelists have to be in file create_restart.nam 
+!>       however variables of those namelists are all optional.
+!>
+!>    * _logger namelist (namlog)_:<br/>
+!>       - cn_logfile   : log filename
+!>       - cn_verbosity : verbosity ('trace','debug','info',
+!> 'warning','error','fatal')
+!>       - in_maxerror  : maximum number of error allowed
+!>
+!>    * _config namelist (namcfg)_:<br/>
+!>       - cn_varcfg : variable configuration file
+!> (see ./SIREN/cfg/variable.cfg)
+!>
+!>    * _coarse grid namelist (namcrs):<br/>
+!>       - cn_coord0 : coordinate file
+!>       - in_perio0 : NEMO periodicity index (see Model Boundary Condition in
+!> [NEMO documentation](http://www.nemo-ocean.eu/About-NEMO/Reference-manuals))
+!>
+!>    * _fine grid namelist (namfin)_:<br/>
+!>       - cn_coord1 : coordinate file
+!>       - cn_bathy1 : bathymetry file
+!>       - in_perio1 : NEMO periodicity index
+!>       - in_extrap : number of land point to be extrapolated 
+!>       before writing file
+!>
+!>    * _vertical grid namelist (namzgr)_:<br/>
+!>       - dn_pp_to_be_computed  :
+!>       - dn_ppsur              :
+!>       - dn_ppa0               :
+!>       - dn_ppa1               :
+!>       - dn_ppa2               : 
+!>       - dn_ppkth              :
+!>       - dn_ppkth2             :
+!>       - dn_ppacr              :
+!>       - dn_ppacr2             :
+!>       - dn_ppdzmin            :
+!>       - dn_pphmax             :
+!>       - in_nlevel             : number of vertical level
+!>
+!>    * _partial step namelist (namzps)_:<br/>
+!>       - dn_e3zps_min          :
+!>       - dn_e3zps_rat          : 
+!>
+!>    * _variable namelist (namvar)_:<br/>
+!>       - cn_varinfo : list of variable and extra information about request(s) 
+!>       to be used.<br/>
+!>          each elements of *cn_varinfo* is a string character.<br/>
+!>          it is composed of the variable name follow by ':', 
+!>          then request(s) to be used on this variable.<br/> 
+!>          request could be:
+!>             - interpolation method
+!>             - extrapolation method
+!>             - filter method
+!>             - > minimum value
+!>             - < maximum value
+!>
+!>             requests must be separated by ';'.<br/>
+!>             order of requests does not matter.<br/>
+!>
+!>          informations about available method could be find in @ref interp,
+!>          @ref extrap and @ref filter.<br/>
+!>          Example: 'votemper: linear; hann; dist_weight','vosaline: cubic'
+!>          @note 
+!>             If you do not specify a method which is required, 
+!>             default one is apply.
+!>       - cn_varfile : list of variable, and corresponding file<br/> 
+!>          *cn_varfile* is the path and filename of the file where find
+!>          variable.<br/>
+!>          @note 
+!>             *cn_varfile* could be a matrix of value, if you want to filled
+!>             manually variable value.<br/>
+!>             the variable array of value is split into equal subdomain.<br/>
+!>             Each subdomain is filled with the corresponding value 
+!>             of the matrix.<br/>          
+!>             separators used to defined matrix are:
+!>                - ',' for line
+!>                - '/' for row
+!>                - '\' for level<br/>
+!>                Example:<br/>
+!>                   3,2,3/1,4,5  =>  @f$ \left( \begin{array}{ccc}
+!>                                         3 & 2 & 3 \\
+!>                                         1 & 4 & 5 \end{array} \right) @f$
+!>
+!>          Examples: 
+!>             - 'votemper:gridT.nc', 'vozocrtx:gridU.nc'
+!>             - 'votemper:10\25', 'vozocrtx:gridU.nc'
+!>
+!>             to get all variable from one file:
+!>             - 'all:restart.dimg'
+!>
+!>    * _nesting namelist (namnst)_:<br/>
+!>       - in_rhoi  : refinement factor in i-direction
+!>       - in_rhoj  : refinement factor in j-direction
+!>       @note 
+!>          coarse grid indices will be deduced from fine grid
+!>          coordinate file.
+!>
+!>    * _output namelist (namout)_:<br/>
+!>       - cn_fileout : output file
+!>       - in_nproc  : total number of processor to be used
+!>       - in_niproc : i-direction number of processor
+!>       - in_njproc : j-direction numebr of processor
+!>       - cn_type   : output format ('dimg', 'cdf')
+!>
+!> @author J.Paul
 ! REVISION HISTORY:
-!> @date Nov, 2013 - Initial Version
-!
+!> @date November, 2013 - Initial Version
+!> @date September, 2014
+!> - add header for user
+!> - offset computed considering grid point
+!> - add attributes in output variable
+!>
 !> @note Software governed by the CeCILL licence     (NEMOGCM/NEMO_CeCILL.txt)
-!>
-!> @todo
-!> - add attributes indices and refinement in output file
-!> - check fileout exist at the beginning
 !----------------------------------------------------------------------
-!> @code
 PROGRAM create_restart
 
-!   USE netcdf                         ! nf90 library
    USE global                          ! global variable
    USE kind                            ! F90 kind parameter
@@ -41,5 +164,4 @@
    USE multi                           ! multi file manager
    USE iom                             ! I/O manager
-   USE dom                             ! domain manager
    USE grid                            ! grid manager
    USE vgrid                           ! vertical grid manager
@@ -48,5 +170,7 @@
    USE filter                          ! filter manager
    USE mpp                             ! MPP manager
+   USE dom                             ! domain manager
    USE iom_mpp                         ! MPP I/O manager
+   USE iom_dom                         ! DOM I/O manager
 
    IMPLICIT NONE
@@ -58,24 +182,26 @@
    CHARACTER(LEN=lc)                                  :: cl_name
    CHARACTER(LEN=lc)                                  :: cl_data
+   CHARACTER(LEN=lc)                                  :: cl_fileout 
 
    INTEGER(i4)                                        :: il_narg
    INTEGER(i4)                                        :: il_status
    INTEGER(i4)                                        :: il_fileid
+   INTEGER(i4)                                        :: il_nvar
    INTEGER(i4)                                        :: il_attid
+   INTEGER(i4)                                        :: il_imin1
+   INTEGER(i4)                                        :: il_imax1
+   INTEGER(i4)                                        :: il_jmin1
+   INTEGER(i4)                                        :: il_jmax1
    INTEGER(i4)                                        :: il_imin0
    INTEGER(i4)                                        :: il_imax0
    INTEGER(i4)                                        :: il_jmin0
    INTEGER(i4)                                        :: il_jmax0
+   INTEGER(i4)                                        :: il_index
    INTEGER(i4)      , DIMENSION(ip_maxdim)            :: il_rho
-   INTEGER(i4)      , DIMENSION(2)                    :: il_xghost
+   INTEGER(i4)      , DIMENSION(2,2)                  :: il_xghost
    INTEGER(i4)      , DIMENSION(2,2)                  :: il_offset
-   INTEGER(i4)      , DIMENSION(2,2,2)                :: il_ind
+   INTEGER(i4)      , DIMENSION(2,2)                  :: il_ind
 
    LOGICAL                                            :: ll_exist
-
-   TYPE(TFILE)                                        :: tl_coord0
-   TYPE(TFILE)                                        :: tl_coord1
-   TYPE(TFILE)                                        :: tl_bathy1
-   TYPE(TFILE)                                        :: tl_file
 
    TYPE(TDOM)                                         :: tl_dom1
@@ -88,5 +214,4 @@
    TYPE(TVAR)                                         :: tl_lon
    TYPE(TVAR)                                         :: tl_lat
-   TYPE(TVAR)                                         :: tl_tmp
    TYPE(TVAR)       , DIMENSION(:)      , ALLOCATABLE :: tl_var
    TYPE(TVAR)       , DIMENSION(:)      , ALLOCATABLE :: tl_level
@@ -94,6 +219,10 @@
    TYPE(TDIM)       , DIMENSION(ip_maxdim)            :: tl_dim
 
+   TYPE(TMPP)                                         :: tl_coord0
+   TYPE(TMPP)                                         :: tl_coord1
+   TYPE(TMPP)                                         :: tl_bathy1
    TYPE(TMPP)                                         :: tl_mpp
    TYPE(TMPP)                                         :: tl_mppout
+
    TYPE(TMULTI)                                       :: tl_multi
 
@@ -104,29 +233,50 @@
 
    ! namelist variable
+   ! namlog
    CHARACTER(LEN=lc) :: cn_logfile = 'create_restart.log' 
    CHARACTER(LEN=lc) :: cn_verbosity = 'warning' 
-
+   INTEGER(i4)       :: in_maxerror = 5
+
+   ! namcfg
    CHARACTER(LEN=lc) :: cn_varcfg = 'variable.cfg' 
 
+   ! namcrs
    CHARACTER(LEN=lc) :: cn_coord0 = '' 
    INTEGER(i4)       :: in_perio0 = -1
 
+   ! namfin
    CHARACTER(LEN=lc) :: cn_coord1 = ''
    CHARACTER(LEN=lc) :: cn_bathy1 = ''
    INTEGER(i4)       :: in_perio1 = -1
    INTEGER(i4)       :: in_extrap = 0
-   LOGICAL           :: ln_fillclosed = .TRUE.
-
-   CHARACTER(LEN=lc), DIMENSION(ig_maxvar) :: cn_varinfo = ''
-   CHARACTER(LEN=lc), DIMENSION(ig_maxvar) :: cn_varfile = ''
-
-   INTEGER(i4)       :: in_imin0 = 0
-   INTEGER(i4)       :: in_imax0 = 0
-   INTEGER(i4)       :: in_jmin0 = 0
-   INTEGER(i4)       :: in_jmax0 = 0
+
+   !namzgr
+   REAL(dp)          :: dn_pp_to_be_computed = 0._dp
+   REAL(dp)          :: dn_ppsur     = -3958.951371276829_dp
+   REAL(dp)          :: dn_ppa0      =   103.9530096000000_dp
+   REAL(dp)          :: dn_ppa1      =     2.4159512690000_dp
+   REAL(dp)          :: dn_ppa2      =   100.7609285000000_dp
+   REAL(dp)          :: dn_ppkth     =    15.3510137000000_dp
+   REAL(dp)          :: dn_ppkth2    =    48.0298937200000_dp
+   REAL(dp)          :: dn_ppacr     =     7.0000000000000_dp
+   REAL(dp)          :: dn_ppacr2    =    13.000000000000_dp
+   REAL(dp)          :: dn_ppdzmin   = 6._dp
+   REAL(dp)          :: dn_pphmax    = 5750._dp
+   INTEGER(i4)       :: in_nlevel    = 75
+
+   !namzps
+   REAL(dp)          :: dn_e3zps_min = 25._dp
+   REAL(dp)          :: dn_e3zps_rat = 0.2_dp
+
+   ! namvar
+   CHARACTER(LEN=lc), DIMENSION(ip_maxvar) :: cn_varinfo = ''
+   CHARACTER(LEN=lc), DIMENSION(ip_maxvar) :: cn_varfile = ''
+
+   ! namnst
    INTEGER(i4)       :: in_rhoi = 0
    INTEGER(i4)       :: in_rhoj = 0
 
-   CHARACTER(LEN=lc) :: cn_fileout = 'restart' 
+   ! namout
+   CHARACTER(LEN=lc) :: cn_fileout = 'restart.nc' 
    INTEGER(i4)       :: in_nproc   = 0
    INTEGER(i4)       :: in_niproc  = 0
@@ -138,29 +288,43 @@
    NAMELIST /namlog/ &  !< logger namelist
    &  cn_logfile,    &  !< log file
-   &  cn_verbosity      !< log verbosity
+   &  cn_verbosity,  &  !< log verbosity
+   &  in_maxerror       !< logger maximum error
 
    NAMELIST /namcfg/ &  !< configuration namelist
    &  cn_varcfg         !< variable configuration file
 
-   NAMELIST /namcrs/ &   !< coarse grid namelist
-   &  cn_coord0,  &      !< coordinate file
-   &  in_perio0          !< periodicity index
+   NAMELIST /namcrs/ &  !< coarse grid namelist
+   &  cn_coord0,  &     !< coordinate file
+   &  in_perio0         !< periodicity index
    
-   NAMELIST /namfin/ &   !< fine grid namelist
-   &  cn_coord1,   &     !< coordinate file
-   &  cn_bathy1,   &     !< bathymetry file
-   &  in_perio1,   &     !< periodicity index
-   &  in_extrap,   &     !<
-   &  ln_fillclosed      !< fill closed sea
+   NAMELIST /namfin/ &  !< fine grid namelist
+   &  cn_coord1,   &    !< coordinate file
+   &  cn_bathy1,   &    !< bathymetry file
+   &  in_perio1,   &    !< periodicity index
+   &  in_extrap
  
+   NAMELIST /namzgr/ &
+   &  dn_pp_to_be_computed, &
+   &  dn_ppsur,     &
+   &  dn_ppa0,      &
+   &  dn_ppa1,      &
+   &  dn_ppa2,      &
+   &  dn_ppkth,     &
+   &  dn_ppkth2,    &
+   &  dn_ppacr,     &
+   &  dn_ppacr2,    &
+   &  dn_ppdzmin,   &
+   &  dn_pphmax,    &
+   &  in_nlevel         !< number of vertical level
+
+   NAMELIST /namzps/ &
+   &  dn_e3zps_min, &
+   &  dn_e3zps_rat
+
    NAMELIST /namvar/ &  !< variable namelist
-   &  cn_varinfo, &     !< list of variable and interpolation method to be used. (ex: 'votemper:linear','vosaline:cubic' )
+   &  cn_varinfo, &     !< list of variable and interpolation method to be used.
    &  cn_varfile        !< list of variable file
    
    NAMELIST /namnst/ &  !< nesting namelist
-   &  in_imin0,    &     !< i-direction lower left  point indice 
-   &  in_imax0,    &     !< i-direction upper right point indice
-   &  in_jmin0,    &     !< j-direction lower left  point indice
-   &  in_jmax0,    &     !< j-direction upper right point indice
    &  in_rhoi,    &     !< refinement factor in i-direction
    &  in_rhoj           !< refinement factor in j-direction
@@ -174,6 +338,6 @@
    !-------------------------------------------------------------------
 
-   !1- namelist
-   !1-1 get namelist
+   ! namelist
+   ! get namelist
    il_narg=COMMAND_ARGUMENT_COUNT() !f03 intrinsec
    IF( il_narg/=1 )THEN
@@ -184,5 +348,5 @@
    ENDIF
    
-   !1-2 read namelist
+   ! read namelist
    INQUIRE(FILE=TRIM(cl_namelist), EXIST=ll_exist)
    IF( ll_exist )THEN
@@ -203,18 +367,19 @@
 
       READ( il_fileid, NML = namlog )
-      !1-2-1 define log file
-      CALL logger_open(TRIM(cn_logfile),TRIM(cn_verbosity))
+      ! define log file
+      CALL logger_open(TRIM(cn_logfile),TRIM(cn_verbosity),in_maxerror)
       CALL logger_header()
 
       READ( il_fileid, NML = namcfg )
-      !1-2-2 get variable extra information
+      ! get variable extra information
       CALL var_def_extra(TRIM(cn_varcfg))
 
       READ( il_fileid, NML = namcrs )
       READ( il_fileid, NML = namfin )
+      READ( il_fileid, NML = namzgr )
       READ( il_fileid, NML = namvar )
-      !1-2-3 add user change in extra information
+      ! add user change in extra information
       CALL var_chg_extra(cn_varinfo)
-      !1-2-4 match variable with file
+      ! match variable with file
       tl_multi=multi_init(cn_varfile)
       
@@ -231,11 +396,19 @@
 
       PRINT *,"ERROR in create_restart: can't find "//TRIM(cl_namelist)
-
-   ENDIF
-
-   !2- open files
+      STOP
+
+   ENDIF
+
+   ! 
+   CALL multi_print(tl_multi)
+   IF( tl_multi%i_nvar <= 0 )THEN
+      CALL logger_fatal("CREATE RESTART: no variable to be used."//&
+      &  " check namelist.")
+   ENDIF
+
+   ! open files
    IF( cn_coord0 /= '' )THEN
-      tl_coord0=file_init(TRIM(cn_coord0),id_perio=in_perio0)
-      CALL iom_open(tl_coord0)
+      tl_coord0=mpp_init( file_init(TRIM(cn_coord0)), id_perio=in_perio0)
+      CALL grid_get_info(tl_coord0)
    ELSE
       CALL logger_fatal("CREATE RESTART: no coarse grid coordinate found. "//&
@@ -244,6 +417,6 @@
 
    IF( TRIM(cn_coord1) /= '' )THEN
-      tl_coord1=file_init(TRIM(cn_coord1),id_perio=in_perio1)
-      CALL iom_open(tl_coord1)
+      tl_coord1=mpp_init( file_init(TRIM(cn_coord1)), id_perio=in_perio1)
+      CALL grid_get_info(tl_coord1)
    ELSE
       CALL logger_fatal("CREATE RESTART: no fine grid coordinate found. "//&
@@ -252,6 +425,6 @@
 
    IF( TRIM(cn_bathy1) /= '' )THEN
-      tl_bathy1=file_init(TRIM(cn_bathy1),id_perio=in_perio1)
-      CALL iom_open(tl_bathy1)
+      tl_bathy1=mpp_init( file_init(TRIM(cn_bathy1)), id_perio=in_perio1)
+      CALL grid_get_info(tl_bathy1)
    ELSE
       CALL logger_fatal("CREATE RESTART: no fine grid bathymetry found. "//&
@@ -259,6 +432,14 @@
    ENDIF
 
-   !3- check
-   !3-2-1 check refinement factor
+   ! check
+   ! check output file do not already exist
+   cl_fileout=file_rename(cn_fileout,1)
+   INQUIRE(FILE=TRIM(cl_fileout), EXIST=ll_exist)
+   IF( ll_exist )THEN
+      CALL logger_fatal("CREATE RESTART: output file "//TRIM(cl_fileout)//&
+      &  " already exist.")
+   ENDIF
+
+   ! check refinement factor
    il_rho(:)=1
    IF( in_rhoi < 1 .OR. in_rhoj < 1 )THEN
@@ -270,163 +451,164 @@
    ENDIF
 
-   IF( cn_coord0 /= '' )THEN !.OR. cn_bathy0 /= '' )THEN
-
-      !3-1 check namelist
-      IF( in_imin0 < 1 .OR. in_imax0 < 1 .OR. in_jmin0 < 1 .OR. in_jmax0 < 1)THEN
-         ! compute coarse grid indices around fine grid
-         IF( cn_coord0 /= '' )THEN
-            il_ind(:,:,:)=grid_get_coarse_index(tl_coord0, tl_coord1 )
-         ENDIF
-
-         il_imin0=il_ind(1,1,1) ; il_imax0=il_ind(1,2,1)
-         il_jmin0=il_ind(2,1,1) ; il_jmax0=il_ind(2,2,1)
-
-         il_offset(:,:)=il_ind(:,:,2)
-      ELSE
-         il_imin0=in_imin0 ; il_imax0=in_imax0
-         il_jmin0=in_jmin0 ; il_jmax0=in_jmax0
-
-         il_offset(1,:)=FLOOR(REAL(il_rho(jp_I)-1,dp)*0.5)
-         il_offset(2,:)=FLOOR(REAL(il_rho(jp_J)-1,dp)*0.5)         
-      ENDIF
-
-      !3-2 check domain validity
-      IF( cn_coord0 /= '' )THEN
-         CALL grid_check_dom(tl_coord0, il_imin0, il_imax0, il_jmin0, il_jmax0)
-      ENDIF
-
-      !3-3 check coordinate file
-      IF( cn_coord0 /= '' )THEN
-         CALL grid_check_coincidence( tl_coord0, tl_coord1, &
-         &                            il_imin0, il_imax0, &
-         &                            il_jmin0, il_jmax0, &
-         &                            il_rho(:) )
-      ENDIF
-
-   ENDIF
+   ! check domain indices
+   ! compute coarse grid indices around fine grid
+   il_ind(:,:)=grid_get_coarse_index(tl_coord0, tl_coord1, &
+   &                                 id_rho=il_rho(:))
+
+   il_imin0=il_ind(1,1) ; il_imax0=il_ind(1,2)
+   il_jmin0=il_ind(2,1) ; il_jmax0=il_ind(2,2)
+
+   ! check domain validity
+   CALL grid_check_dom(tl_coord0, il_imin0, il_imax0, il_jmin0, il_jmax0)
+
+   !3-2-4 check coincidence between coarse and fine grid
+   CALL grid_check_coincidence( tl_coord0, tl_coord1, &
+   &                            il_imin0, il_imax0, &
+   &                            il_jmin0, il_jmax0, &
+   &                            il_rho(:) )
 
    ! compute level
-   ALLOCATE(tl_level(ig_npoint))
+   ALLOCATE(tl_level(ip_npoint))
    tl_level(:)=vgrid_get_level(tl_bathy1, cl_namelist )
 
    ! remove ghost cell
-   il_xghost(:)=grid_get_ghost(tl_bathy1)
-
-   DO ji=1,ig_npoint
-      CALL grid_del_ghost(tl_level(ji), il_xghost(1), il_xghost(2))
+   il_xghost(:,:)=grid_get_ghost(tl_bathy1)
+   DO ji=1,ip_npoint
+      CALL grid_del_ghost(tl_level(ji), il_xghost(:,:))
    ENDDO
 
-   ! close
-   CALL iom_close(tl_bathy1)
-
-   !4- work on variables
-   IF( .NOT. ASSOCIATED(tl_multi%t_file) )THEN
+   ! clean
+   CALL mpp_clean(tl_bathy1)
+
+   ! work on variables
+   IF( .NOT. ASSOCIATED(tl_multi%t_mpp) )THEN
       CALL logger_error("CREATE RESTART: no file to work on. "//&
       &                 "check cn_varfile in namelist.")
    ELSE
       ALLOCATE( tl_var( tl_multi%i_nvar ) )
+
       jvar=0
       ! for each file
-      DO ji=1,tl_multi%i_nfile
-         WRITE(cl_data,'(a,i2.2)') 'data_',jvar+1
-
-         IF( .NOT. ASSOCIATED(tl_multi%t_file(ji)%t_var) )THEN
+      DO ji=1,tl_multi%i_nmpp
+         WRITE(cl_data,'(a,i2.2)') 'data-',jvar+1
+
+         IF( .NOT. ASSOCIATED(tl_multi%t_mpp(ji)%t_proc(1)%t_var) )THEN
 
             CALL logger_error("CREATE RESTART: no variable to work on for "//&
-            &                 "file "//TRIM(tl_multi%t_file(ji)%c_name)//&
+            &                 "mpp "//TRIM(tl_multi%t_mpp(ji)%c_name)//&
             &                 ". check cn_varfile in namelist.")
 
-         ELSEIF( TRIM(tl_multi%t_file(ji)%c_name) == TRIM(cl_data) )THEN
-         !4-1 use input matrix to fill variable
-
+         ELSEIF( TRIM(tl_multi%t_mpp(ji)%c_name) == TRIM(cl_data) )THEN
+         !- use input matrix to fill variable
+
+            WRITE(*,'(a)') "work on data"
             ! for each variable initialise from matrix
-            DO jj=1,tl_multi%t_file(ji)%i_nvar
+            DO jj=1,tl_multi%t_mpp(ji)%t_proc(1)%i_nvar
+
                jvar=jvar+1
-               tl_tmp=tl_multi%t_file(ji)%t_var(jj)
-               !4-1-1 fill value with matrix data
-               ! pb voir comment gerer nb de dimension
-               tl_var(jvar)=create_restart_matrix(tl_tmp, tl_coord1)
-
-               !4-1-2 use mask
-               CALL create_restart_mask(tl_var(jvar), tl_level(:))
+               
+               WRITE(*,'(2x,a,a)') "work on variable "//&
+               &  TRIM(tl_multi%t_mpp(ji)%t_proc(1)%t_var(jj)%c_name)
+
+               ! fill value with matrix data
+               tl_var(jvar) = create_restart_matrix( &
+               &  tl_multi%t_mpp(ji)%t_proc(1)%t_var(jj), tl_coord1, &
+               &  in_nlevel, tl_level(:) )
+
             ENDDO
-
+         !- end of use input matrix to fill variable
          ELSE
-         !4-2 use file to fill variable
-
-            ! open file
-            tl_file=file_init(TRIM(tl_multi%t_file(ji)%c_name))
-            CALL iom_open(tl_file)
+         !- use mpp file to fill variable
+
+            WRITE(*,'(a)') "work on file "//TRIM(tl_multi%t_mpp(ji)%c_name)
+            !
+            tl_mpp=mpp_init( file_init(TRIM(tl_multi%t_mpp(ji)%t_proc(1)%c_name)) )
+            CALL grid_get_info(tl_mpp)
+
+            ! check vertical dimension
+            IF( tl_mpp%t_dim(jp_K)%l_use .AND. &
+            &   tl_mpp%t_dim(jp_K)%i_len /= in_nlevel  )THEN
+               CALL logger_error("CREATE RESTART: dimension in file "//&
+               &  TRIM(tl_mpp%c_name)//" not agree with namelist in_nlevel ")
+            ENDIF
+
+            ! open mpp file
+            CALL iom_mpp_open(tl_mpp)
 
             ! get or check depth value
-            IF( tl_file%i_depthid /= 0 )THEN
-               IF( ASSOCIATED(tl_depth%d_value) )THEN
-                  IF( ANY( tl_depth%d_value(:,:,:,:) /= &
-                  &        tl_tmp%d_value(:,:,:,:) ) )THEN
-                     CALL logger_fatal("CREATE RESTART: depth value from "//&
-                     &  TRIM(tl_multi%t_file(ji)%c_name)//" not conform "//&
-                     &  " to those from former file(s).")
-                  ENDIF
-               ELSE
-                  tl_depth=iom_read_var(tl_file,tl_file%i_depthid)
+            CALL create_restart_check_depth( tl_mpp, tl_depth )
+
+            ! get or check time value
+            CALL create_restart_check_time( tl_mpp, tl_time )
+
+            ! close mpp file
+            CALL iom_mpp_close(tl_mpp)
+
+            IF( ANY( tl_mpp%t_dim(1:2)%i_len /= &
+            &        tl_coord0%t_dim(1:2)%i_len) )THEN
+            !!! extract value from fine grid 
+
+               IF( ANY( tl_mpp%t_dim(1:2)%i_len <= &
+               &        tl_coord1%t_dim(1:2)%i_len) )THEN
+                  CALL logger_fatal("CREATE RESTART: dimension in file "//&
+                  &  TRIM(tl_mpp%c_name)//" smaller than those in fine"//&
+                  &  " grid coordinates.")
                ENDIF
-            ENDIF
-
-            ! get or check time value
-            IF( tl_file%i_timeid /= 0 )THEN
-               IF( ASSOCIATED(tl_time%d_value) )THEN
-                  IF( ANY( tl_time%d_value(:,:,:,:) /= &
-                  &        tl_tmp%d_value(:,:,:,:) ) )THEN
-                     CALL logger_fatal("CREATE RESTART: time value from "//&
-                     &  TRIM(tl_multi%t_file(ji)%c_name)//" not conform "//&
-                     &  " to those from former file(s).")
-                  ENDIF
-               ELSE
-                  tl_time=iom_read_var(tl_file,tl_file%i_timeid)
-               ENDIF
-            ENDIF
-
-            IF( ANY( tl_file%t_dim(1:2)%i_len /= &
-            &      tl_coord0%t_dim(1:2)%i_len) )THEN
-            !4-2-1 extract value from fine grid 
-
-               !4-2-1-1 compute domain on fine grid
-               tl_dom1=create__restart_get_dom_coord(tl_file, tl_coord1)
+
+               ! compute domain on fine grid
+               il_ind(:,:)=grid_get_coarse_index(tl_mpp, tl_coord1 )
+
+               il_imin1=il_ind(1,1) ; il_imax1=il_ind(1,2)
+               il_jmin1=il_ind(2,1) ; il_jmax1=il_ind(2,2)
+
+               !- check grid coincidence
+               CALL grid_check_coincidence( tl_mpp, tl_coord1, &
+               &                            il_imin1, il_imax1, &
+               &                            il_jmin1, il_jmax1, &
+               &                            il_rho(:) )
+
+               ! compute domain
+               tl_dom1=dom_init(tl_mpp,         &
+               &                il_imin1, il_imax1, &
+               &                il_jmin1, il_jmax1)
                
-               ! open mpp file on domain
-               !4-2-1-2 init mpp structure
-               tl_mpp=mpp_init(tl_file)
-
-               !4-2-1-3 get processor to be used
-               CALL mpp_get_use( tl_mpp, tl_dom1 )
-               !4-2-1-4 open mpp files
-               CALL iom_mpp_open(tl_mpp)
+               ! open mpp files
+               CALL iom_dom_open(tl_mpp, tl_dom1)
 
                ! for each variable of this file
-               DO jj=1,tl_multi%t_file(ji)%i_nvar
+               DO jj=1,tl_multi%t_mpp(ji)%t_proc(1)%i_nvar
+
+                  WRITE(*,'(2x,a,a)') "work on variable "//&
+                  &  TRIM(tl_multi%t_mpp(ji)%t_proc(1)%t_var(jj)%c_name)
+
                   jvar=jvar+1
-                  cl_name=tl_multi%t_file(ji)%t_var(jj)%c_name
-                  !4-2-1-5 read variable over domain
-                  tl_var(jvar)=iom_mpp_read_var( tl_mpp, TRIM(cl_name), &
-                  &                              td_dom=tl_dom1 )
-
-                  !4-2-1-7 add attribute to variable
+                  cl_name=tl_multi%t_mpp(ji)%t_proc(1)%t_var(jj)%c_name
+                  ! read variable over domain
+                  tl_var(jvar)=iom_dom_read_var(tl_mpp, TRIM(cl_name), tl_dom1)
+
+                  ! add attribute to variable
                   tl_att=att_init('src_file',TRIM(fct_basename(tl_mpp%c_name)))
                   CALL var_move_att(tl_var(jvar), tl_att)
 
+                  tl_att=att_init('src_i_indices',(/il_imin0, il_imax0/))
+                  CALL var_move_att(tl_var(jvar), tl_att)
+
+                  tl_att=att_init('src_j_indices',(/il_jmin0, il_jmax0/))
+                  CALL var_move_att(tl_var(jvar), tl_att)
+
                   ! clean structure
                   CALL att_clean(tl_att)
 
-                  !4-2-1-8 use mask
+                  ! use mask
                   CALL create_restart_mask(tl_var(jvar), tl_level(:))
 
-                  !4-2-1-9 add ghost cell
-                  CALL grid_add_ghost( tl_var(jvar), &
-                  &                    tl_dom1%i_ighost,tl_dom1%i_jghost )
+                  ! add ghost cell
+                  CALL grid_add_ghost( tl_var(jvar), tl_dom1%i_ghost(:,:) )
 
                ENDDO
 
-               !4-2-1-2 close mpp file
-               CALL iom_mpp_close(tl_mpp)
+               ! close mpp file
+               CALL iom_dom_close(tl_mpp)
+
                ! clean structure
                CALL mpp_clean(tl_mpp)
@@ -434,64 +616,75 @@
 
             ELSE
-            !4-2-2 get value from coarse grid 
-
-               !4-2-2-1 compute domain on coarse grid
-               tl_dom0=create__restart_get_dom_index(tl_file, il_imin0, il_jmin0, &
-               &                                              il_imax0, il_jmax0)
-
-               !4-2-2-2 add extra band (if possible) to compute interpolation
+            !!! get value from coarse grid 
+
+               ! compute domain on coarse grid
+               tl_dom0=dom_init(tl_mpp,             &
+               &                il_imin0, il_imax0, &
+               &                il_jmin0, il_jmax0 )
+
+               ! add extra band (if possible) to compute interpolation
                CALL dom_add_extra(tl_dom0)
 
-               ! open mpp file on domain
-               !4-2-2-3 init mpp structure
-               tl_mpp=mpp_init(tl_file)
-
-               !4-2-2-4 get processor to be used
-               CALL mpp_get_use( tl_mpp, tl_dom0 )
-
-               !4-2-2-5 open mpp files
-               CALL iom_mpp_open(tl_mpp)
-
+               ! open mpp files
+               CALL iom_dom_open(tl_mpp, tl_dom0)
                ! for each variable of this file
-               DO jj=1,tl_multi%t_file(ji)%i_nvar
+               DO jj=1,tl_multi%t_mpp(ji)%t_proc(1)%i_nvar
+
+                  WRITE(*,'(2x,a,a)') "work on variable "//&
+                  &  TRIM(tl_multi%t_mpp(ji)%t_proc(1)%t_var(jj)%c_name)
 
                   jvar=jvar+1
-                  cl_name=tl_multi%t_file(ji)%t_var(jj)%c_name
-                  print *,'work on ',trim(cl_name)
-                  !4-2-2-6 read variable over domain
-                  tl_var(jvar)=iom_mpp_read_var( tl_mpp, TRIM(cl_name), &
-                  &                              td_dom=tl_dom0 )
-
-                  !4-2-2-7 interpolate variable
+                  cl_name=tl_multi%t_mpp(ji)%t_proc(1)%t_var(jj)%c_name
+
+                  ! read variable over domain
+                  tl_var(jvar)=iom_dom_read_var(tl_mpp, TRIM(cl_name), tl_dom0)
+
+                  il_offset(:,:)=grid_get_fine_offset(tl_coord0, &
+                  &                                   il_imin0, il_jmin0, &
+                  &                                   il_imax0, il_jmax0, &
+                  &                                   tl_coord1, &
+                  &                                   id_rho=il_rho(:), &
+                  &                                   cd_point=TRIM(tl_var(jvar)%c_point))
+                  
+
+                  ! interpolate variable
                   CALL create_restart_interp(tl_var(jvar), tl_level(:), &
                   &                          il_rho(:), &
                   &                          id_offset=il_offset(:,:))
 
-                  !tl_att=att_init('add_offset',0.)
-                  !CALL var_move_att(tl_var(jvar), tl_att)
-                  !tl_att=att_init('scale_factor',1.)
-                  !CALL var_move_att(tl_var(jvar), tl_att)
-
-                  !4-2-2-8 remove extraband added to domain
+                  ! remove extraband added to domain
                   CALL dom_del_extra( tl_var(jvar), tl_dom0, il_rho(:) )
 
-                  !4-2-2-10 add attribute to variable
+                  ! add attribute to variable
                   tl_att=att_init('src_file',TRIM(fct_basename(tl_mpp%c_name)))
                   CALL var_move_att(tl_var(jvar), tl_att)
 
+                  tl_att=att_init('src_i-indices',(/il_imin0, il_imax0/))
+                  CALL var_move_att(tl_var(jvar), tl_att)
+
+                  tl_att=att_init('src_j-indices',(/il_jmin0, il_jmax0/))
+                  CALL var_move_att(tl_var(jvar), tl_att)
+
+                  IF( ANY(il_rho(:)/=1) )THEN
+                     tl_att=att_init("refinment_factor", &
+                     &               (/il_rho(jp_I),il_rho(jp_J)/))
+                     CALL var_move_att(tl_var(jvar), tl_att)
+                  ENDIF
+
                   ! clean structure
                   CALL att_clean(tl_att)
 
-                  !4-2-2-11 use mask
+                  ! use mask
                   CALL create_restart_mask(tl_var(jvar), tl_level(:))
 
-                  !4-2-2-12 add ghost cell
-                  CALL grid_add_ghost( tl_var(jvar), &
-                  &                    tl_dom0%i_ighost,tl_dom0%i_jghost )
+                  ! add ghost cell
+                  CALL grid_add_ghost( tl_var(jvar), il_xghost(:,:) )
+
 
                ENDDO
 
-               !4-2-2-2 close mpp file
-               CALL iom_mpp_close(tl_mpp)
+               ! close mpp file
+               CALL iom_dom_close(tl_mpp)
+
                ! clean structure
                CALL mpp_clean(tl_mpp)
@@ -500,29 +693,33 @@
             ENDIF
 
-            ! close file
-            CALL iom_close(tl_file)
             ! clean structure
-            CALL file_clean(tl_file)
+            CALL mpp_clean(tl_mpp)
          ENDIF
       ENDDO
    ENDIF
 
-   !5- use additional request
-   DO jvar=1,tl_multi%i_nvar
-
-         !5-1 forced min and max value
-         CALL var_limit_value(tl_var(jvar))
-
-         !5-2 filter
-         CALL filter_fill_value(tl_var(jvar))
-
-         !5-3 extrapolate
-         CALL extrap_fill_value(tl_var(jvar), id_iext=in_extrap, &
-         &                                    id_jext=in_extrap, &
-         &                                    id_kext=in_extrap)
+   il_nvar=tl_multi%i_nvar
+
+   ! clean
+   CALL multi_clean(tl_multi)
+   CALL mpp_clean(tl_coord0)
+
+   ! use additional request
+   DO jvar=1,il_nvar
+
+      ! forced min and max value
+      CALL var_limit_value(tl_var(jvar))
+
+      ! filter
+      CALL filter_fill_value(tl_var(jvar))
+
+      ! extrapolate
+      CALL extrap_fill_value(tl_var(jvar), id_iext=in_extrap, &
+      &                                    id_jext=in_extrap, &
+      &                                    id_kext=in_extrap)
 
    ENDDO
 
-   !6- create file
+   ! create file
    IF( in_niproc == 0 .AND. &
    &   in_njproc == 0 .AND. &
@@ -532,10 +729,18 @@
       in_nproc = 1
    ENDIF
-   tl_mppout=mpp_init( TRIM(cn_fileout), tl_var(1), &
-   &                   in_niproc, in_njproc, in_nproc, &
-   &                   cd_type=cn_type)
-
-   !6-1 add dimension
+
+   ! add dimension
    tl_dim(:)=var_max_dim(tl_var(:))
+
+   DO ji=1,il_nvar
+
+      IF( ALL(tl_var(ji)%t_dim(:)%i_len == tl_dim(:)%i_len) )THEN
+         tl_mppout=mpp_init( TRIM(cn_fileout), tl_var(ji), &
+         &                   in_niproc, in_njproc, in_nproc, &
+         &                   cd_type=cn_type)
+         EXIT
+      ENDIF
+
+   ENDDO
 
    DO ji=1,ip_maxdim
@@ -551,43 +756,61 @@
    ENDDO
 
-   !6-2 add variables
-
-   !IF( ALL( tl_dim(1:2)%l_use ) )THEN
-   !   ! add longitude
-   !   tl_lon=iom_read_var(tl_coord1,'longitude')
-
-   !   CALL mpp_add_var(tl_mppout, tl_lon)
-   !   CALL var_clean(tl_lon)
-
-   !   ! add latitude
-   !   tl_lat=iom_read_var(tl_coord1,'latitude')
-
-   !   CALL mpp_add_var(tl_mppout, tl_lat)
-   !   CALL var_clean(tl_lat)
-   !ENDIF
-
-   !IF( tl_dim(3)%l_use )THEN
-   !   ! add depth
-   !   CALL mpp_add_var(tl_mppout, tl_depth)
-   !   CALL var_clean(tl_depth)
-   !ENDIF
-
-   !IF( tl_dim(4)%l_use )THEN
-   !   ! add time
-   !   CALL mpp_add_var(tl_mppout, tl_time)
-   !   CALL var_clean(tl_time)
-   !ENDIF
+   ! add variables
+   IF( ALL( tl_dim(1:2)%l_use ) )THEN
+
+      ! open mpp files
+      CALL iom_mpp_open(tl_coord1)
+
+      ! add longitude
+      tl_lon=iom_mpp_read_var(tl_coord1,'longitude')
+      CALL mpp_add_var(tl_mppout, tl_lon)
+      CALL var_clean(tl_lon)
+
+      ! add latitude
+      tl_lat=iom_mpp_read_var(tl_coord1,'latitude')
+      CALL mpp_add_var(tl_mppout, tl_lat)
+      CALL var_clean(tl_lat)
+
+      ! close mpp files
+      CALL iom_mpp_close(tl_coord1)
+
+   ENDIF
+
+   IF( tl_dim(3)%l_use )THEN
+      IF( ASSOCIATED(tl_depth%d_value) )THEN
+         ! add depth
+         CALL mpp_add_var(tl_mppout, tl_depth)
+      ELSE
+         CALL logger_error("CREATE RESTART: no value for depth variable.")
+      ENDIF
+   ENDIF
+   IF( ASSOCIATED(tl_depth%d_value) ) CALL var_clean(tl_depth)
+
+   IF( tl_dim(4)%l_use )THEN
+      IF( ASSOCIATED(tl_time%d_value) )THEN
+         ! add time
+         CALL mpp_add_var(tl_mppout, tl_time)
+      ELSE
+         CALL logger_error("CREATE RESTART: no value for time variable.")
+      ENDIF
+   ENDIF
+   IF( ASSOCIATED(tl_time%d_value) ) CALL var_clean(tl_time)
 
    ! add other variable
-   DO jvar=1,tl_multi%i_nvar
-      CALL mpp_add_var(tl_mppout, tl_var(jvar))
-      CALL var_clean(tl_var(jvar))
+   DO jvar=1,il_nvar
+      ! check if variable already add
+      il_index=var_get_index(tl_mppout%t_proc(1)%t_var(:), tl_var(jvar)%c_name)
+      IF( il_index == 0 )THEN
+         CALL mpp_add_var(tl_mppout, tl_var(jvar))
+         CALL var_clean(tl_var(jvar))
+      ENDIF
    ENDDO
 
-   DO ji=1,4
-      CALL mpp_add_var(tl_mppout,tl_level(ji))
-   ENDDO
-
-   !6-3 add some attribute
+!   DO ji=1,4
+!      CALL grid_add_ghost( tl_level(ji), il_xghost(:,:) )
+!      CALL var_clean(tl_level(ji))
+!   ENDDO
+
+   ! add some attribute
    tl_att=att_init("Created_by","SIREN create_restart")
    CALL mpp_add_att(tl_mppout, tl_att)
@@ -616,20 +839,24 @@
    ENDIF
 
-   !6-4 create file
+   ! create file
    CALL iom_mpp_create(tl_mppout)
 
-   !6-5 write file
+   ! write file
    CALL iom_mpp_write_file(tl_mppout)
-
-   !6-6 close file
+   ! close file
    CALL iom_mpp_close(tl_mppout)
-   IF( cn_coord0 /= '' ) CALL iom_close(tl_coord0)
-
-   !7- clean
+
+   ! print
+   CALL mpp_print(tl_mppout)
+
+   ! clean
+   CALL att_clean(tl_att)
+   CALL var_clean(tl_var(:))
    DEALLOCATE(tl_var)
+   CALL var_clean(tl_level(:))
+   DEALLOCATE(tl_level)
 
    CALL mpp_clean(tl_mppout)
-   CALL file_clean(tl_coord1)
-   CALL file_clean(tl_coord0)
+   CALL mpp_clean(tl_coord1)
 
    ! close log file
@@ -637,60 +864,5 @@
    CALL logger_close()
 
-!> @endcode
 CONTAINS
-   !-------------------------------------------------------------------
-   !> @brief
-   !> 
-   !> @details 
-   !>
-   !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !>
-   !-------------------------------------------------------------------
-   !> @code
-   FUNCTION create_restart_level(td_level1)
-      IMPLICIT NONE
-      ! Argument
-      TYPE(TFILE), INTENT(IN) :: td_level1
-
-      ! function
-      TYPE(TVAR), DIMENSION(4) :: create_restart_level
-
-      ! local variable
-      TYPE(TFILE)              :: tl_level1
-      TYPE(TVAR), DIMENSION(4) :: tl_var
-      TYPE(TMPP)               :: tl_mpplevel1
-
-      ! loop indices
-      !----------------------------------------------------------------
-
-      !0- compute domain
-      tl_dom1=dom_init(td_level1)
-
-      !1 init mpp structure
-      tl_level1=td_level1
-      tl_mpplevel1=mpp_init(tl_level1)
-      
-      CALL file_clean(tl_level1)
-
-      !2 get processor to be used
-      CALL mpp_get_use( tl_mpplevel1, tl_dom1 )
-
-      !3 open mpp files
-      CALL iom_mpp_open(tl_mpplevel1)
-      tl_var(jp_T)=iom_mpp_read_var(tl_mpplevel1,'tlevel',td_dom=tl_dom1)
-      tl_var(jp_U)=iom_mpp_read_var(tl_mpplevel1,'ulevel',td_dom=tl_dom1)
-      tl_var(jp_V)=iom_mpp_read_var(tl_mpplevel1,'vlevel',td_dom=tl_dom1)
-      tl_var(jp_F)=iom_mpp_read_var(tl_mpplevel1,'flevel',td_dom=tl_dom1)
-
-      !4 save result
-      create_restart_level(:)=tl_var(:)
-
-      !5 clean
-      CALL iom_mpp_close(tl_mpplevel1)
-      CALL mpp_clean(tl_mpplevel1)
-
-   END FUNCTION create_restart_level
-   !> @endcode
    !-------------------------------------------------------------------
    !> @brief
@@ -699,21 +871,24 @@
    !> @details 
    !> A variable is create with the same name that the input variable, 
-   !> and with dimension of the coordinate file. 
-   !> Then the variable table of value is split into equal subdomain.
-   !> Each subdomain is fill with the linked value of the matrix.
+   !> and with dimension of the coordinate file.<br/> 
+   !> Then the variable array of value is split into equal subdomain.
+   !> Each subdomain is filled with the corresponding value of the matrix.
    !>
    !> @author J.Paul
-   !> - Nov, 2013- Initial Version
+   !> - November, 2013- Initial Version
    !>
-   !> @param[in] td_var : variable structure 
-   !> @param[in] td_coord : coordinate 
+   !> @param[in] td_var    variable structure 
+   !> @param[in] td_coord  coordinate file structure 
+   !> @param[in] id_nlevel number of vertical level  
+   !> @param[in] td_level  array of level on T,U,V,F point (variable structure) 
    !> @return variable structure 
    !-------------------------------------------------------------------
-   !> @code
-   FUNCTION create_restart_matrix(td_var, td_coord)
+   FUNCTION create_restart_matrix(td_var, td_coord, id_nlevel, td_level)
       IMPLICIT NONE
       ! Argument
-      TYPE(TVAR) , INTENT(IN) :: td_var
-      TYPE(TFILE), INTENT(IN) :: td_coord
+      TYPE(TVAR)              , INTENT(IN) :: td_var
+      TYPE(TMPP)              , INTENT(IN) :: td_coord
+      INTEGER(i4)             , INTENT(IN) :: id_nlevel
+      TYPE(TVAR), DIMENSION(:), INTENT(IN) :: td_level
 
       ! function
@@ -721,10 +896,8 @@
 
       ! local variable
-      INTEGER(i4)                                        :: il_ighost
-      INTEGER(i4)                                        :: il_jghost
-      INTEGER(i4)      , DIMENSION(2)                    :: il_xghost
       INTEGER(i4)      , DIMENSION(3)                    :: il_dim
       INTEGER(i4)      , DIMENSION(3)                    :: il_size
       INTEGER(i4)      , DIMENSION(3)                    :: il_rest
+      INTEGER(i4)      , DIMENSION(2,2)                  :: il_xghost
 
       INTEGER(i4)      , DIMENSION(:)      , ALLOCATABLE :: il_ishape
@@ -734,7 +907,4 @@
       REAL(dp)         , DIMENSION(:,:,:,:), ALLOCATABLE :: dl_value
 
-      TYPE(TVAR)                                         :: tl_lon
-      TYPE(TVAR)                                         :: tl_lat
-      TYPE(TVAR)                                         :: tl_var
       TYPE(TDIM)       , DIMENSION(ip_maxdim)            :: tl_dim
 
@@ -745,25 +915,22 @@
       !----------------------------------------------------------------
 
-      !1- read output grid
-      tl_lon=iom_read_var(td_coord,'longitude')
-      tl_lat=iom_read_var(td_coord,'latitude')
-
-      !2- look for ghost cell
-      il_xghost(:)=grid_get_ghost( tl_lon, tl_lat )
-
-      il_ighost=il_xghost(1)*ig_ghost
-      il_jghost=il_xghost(2)*ig_ghost
-      
-      !3- write value on grid
-      !3-1 get matrix dimension
+      ! look for ghost cell
+      il_xghost(:,:)=grid_get_ghost( td_coord )
+
+      ! write value on grid
+      ! get matrix dimension
       il_dim(:)=td_var%t_dim(1:3)%i_len
-      !3-2 output dimension
-      tl_dim(:)=tl_lon%t_dim(:)
+
+      ! output dimension
+      tl_dim(jp_I:jp_J)=dim_copy(td_coord%t_dim(jp_I:jp_J))
+      IF( id_nlevel >= 1 )THEN
+         tl_dim(jp_K)=dim_init('Z',id_nlevel)
+      ENDIF
 
       ! remove ghost cell
-      tl_dim(1)%i_len=tl_dim(1)%i_len - 2*il_xghost(1)*ig_ghost
-      tl_dim(2)%i_len=tl_dim(2)%i_len - 2*il_xghost(2)*ig_ghost
-
-      !3-3 split output domain in N subdomain depending of matrix dimension 
+      tl_dim(jp_I)%i_len=tl_dim(jp_I)%i_len - SUM(il_xghost(jp_I,:))*ip_ghost
+      tl_dim(jp_J)%i_len=tl_dim(jp_J)%i_len - SUM(il_xghost(jp_J,:))*ip_ghost
+
+      ! split output domain in N subdomain depending of matrix dimension 
       il_size(:) = tl_dim(1:3)%i_len / il_dim(:)
       il_rest(:) = MOD(tl_dim(1:3)%i_len, il_dim(:))
@@ -776,5 +943,4 @@
       ! add rest to last cell
       il_ishape(il_dim(1)+1)=il_ishape(il_dim(1)+1)+il_rest(1)
-      
 
       ALLOCATE( il_jshape(il_dim(2)+1) )
@@ -794,5 +960,5 @@
       il_kshape(il_dim(3)+1)=il_kshape(il_dim(3)+1)+il_rest(3)
 
-      !3-3 write ouput table of value 
+      ! write ouput array of value 
       ALLOCATE(dl_value( tl_dim(1)%i_len, &
       &                  tl_dim(2)%i_len, &
@@ -815,160 +981,37 @@
       ENDDO
 
-      !3-4 initialise variable with value
-      tl_var=var_init(TRIM(td_var%c_name),dl_value(:,:,:,:))
+      ! keep attribute and type
+      create_restart_matrix=var_copy(td_var)
+      DEALLOCATE( create_restart_matrix%d_value )
+      ! save new dimension
+      create_restart_matrix%t_dim(:)=dim_copy(tl_dim(:))
+      ! add variable value
+      CALL var_add_value( create_restart_matrix, dl_value(:,:,:,:), &
+      &                   id_type=td_var%i_type)
 
       DEALLOCATE(dl_value)
 
-      !4- add ghost cell
-      CALL grid_add_ghost(tl_var,il_ighost,il_jghost)
-
-      !5- save result
-      create_restart_matrix=tl_var
+      ! use mask
+      CALL create_restart_mask(create_restart_matrix, td_level(:))
+
+      ! add ghost cell
+      CALL grid_add_ghost( create_restart_matrix, il_xghost(:,:) )
+
+      ! clean 
+      DEALLOCATE(il_ishape)
+      DEALLOCATE(il_jshape)
+      DEALLOCATE(il_kshape)
 
    END FUNCTION create_restart_matrix
-   !> @endcode
    !-------------------------------------------------------------------
    !> @brief
+   !> This subroutine use mask to filled land point with _FillValue
    !> 
-   !> @details 
+   !> @author J.Paul
+   !> - November, 2013- Initial Version
    !>
-   !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !>
+   !> @param[inout] td_var variable structure
+   !> @param[in] td_mask   mask variable structure
    !-------------------------------------------------------------------
-   !> @code
-   FUNCTION create__restart_get_dom_coord( td_file, td_coord, &
-   &                                       id_rho )
-      IMPLICIT NONE
-      ! Argument
-      TYPE(TFILE),               INTENT(IN) :: td_file
-      TYPE(TFILE),               INTENT(IN) :: td_coord
-      INTEGER(i4), DIMENSION(:), INTENT(IN), OPTIONAL :: id_rho
-
-      ! function
-      TYPE(TDOM) :: create__restart_get_dom_coord
-
-      ! local variable
-      INTEGER(i4)                 :: il_pivot
-      INTEGER(i4)                 :: il_perio
-
-      INTEGER(i4)                 :: il_imin
-      INTEGER(i4)                 :: il_imax
-      INTEGER(i4)                 :: il_jmin
-      INTEGER(i4)                 :: il_jmax
-
-      INTEGER(i4), DIMENSION(2,2,2) :: il_ind
-
-      TYPE(TFILE)                 :: tl_file
-
-      TYPE(TDOM)                  :: tl_dom
-      ! loop indices
-      !----------------------------------------------------------------
-
-      tl_file=td_file
-      !1- open file
-      CALL iom_open(tl_file)
-
-      ! get periodicity
-      il_pivot=grid_get_pivot(tl_file)
-      il_perio=grid_get_perio(tl_file,il_pivot)
-
-      tl_file%i_perio=il_perio
-
-      !2- compute file grid indices around coord grid
-      il_ind(:,:,:)=grid_get_coarse_index(tl_file, td_coord )
-
-      il_imin=il_ind(1,1,1) ; il_imax=il_ind(1,2,1)
-      il_jmin=il_ind(2,1,1) ; il_jmax=il_ind(2,2,1)
-
-      !3- check grid coincidence
-      CALL grid_check_coincidence( tl_file, td_coord, &
-      &                            il_imin, il_imax, &
-      &                            il_jmin, il_jmax, &
-      &                            id_rho(:) )
-
-      !4- compute domain
-      tl_dom=dom_init(tl_file,         &
-      &               il_imin, il_imax, &
-      &               il_jmin, il_jmax)
-
-      ! close file
-      CALL iom_close(tl_file)
-
-      ! save result
-      create__restart_get_dom_coord=tl_dom
-
-   END FUNCTION create__restart_get_dom_coord
-   !> @endcode
-   !-------------------------------------------------------------------
-   !> @brief
-   !> 
-   !> @details 
-   !>
-   !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !>
-   !-------------------------------------------------------------------
-   !> @code
-   FUNCTION create__restart_get_dom_index( td_file, id_imin, id_jmin, &
-   &                                                id_imax, id_jmax )
-      IMPLICIT NONE
-      ! Argument
-      TYPE(TFILE), INTENT(IN) :: td_file
-      INTEGER(i4), INTENT(IN) :: id_imin
-      INTEGER(i4), INTENT(IN) :: id_imax
-      INTEGER(i4), INTENT(IN) :: id_jmin
-      INTEGER(i4), INTENT(IN) :: id_jmax
-
-      ! function
-      TYPE(TDOM) :: create__restart_get_dom_index
-
-      ! local variable
-      INTEGER(i4)                 :: il_pivot
-      INTEGER(i4)                 :: il_perio
-
-      TYPE(TFILE)                 :: tl_file
-
-      TYPE(TDOM)                  :: tl_dom
-      ! loop indices
-      !----------------------------------------------------------------
-
-      ! init
-      tl_file=td_file
-      !1- open file
-      CALL iom_open(tl_file)
-
-      ! get periodicity
-      il_pivot=grid_get_pivot(tl_file)
-      il_perio=grid_get_perio(tl_file,il_pivot)
-
-      tl_file%i_perio=il_perio
-
-      !2- compute domain
-      tl_dom=dom_init(tl_file,         &
-      &               id_imin, id_imax, &
-      &               id_jmin, id_jmax)
-
-      ! close file
-      CALL iom_close(tl_file)
-
-      ! save result
-      create__restart_get_dom_index=tl_dom
-
-   END FUNCTION create__restart_get_dom_index
-   !> @endcode
-   !-------------------------------------------------------------------
-   !> @brief
-   !> This subroutine
-   !> 
-   !> @details 
-   !>
-   !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !>
-   !> @param[in] 
-   !> @todo 
-   !-------------------------------------------------------------------
-   !> @code
    SUBROUTINE create_restart_mask( td_var, td_mask )
 
@@ -987,192 +1030,54 @@
       !----------------------------------------------------------------
 
-      IF( ANY(td_var%t_dim(1:2)%i_len /= td_mask(1)%t_dim(1:2)%i_len) )THEN
-         CALL logger_error("CREATE RESTART MASK: dimension differ between "//&
-         &                 "variable ("//&
-         &                 TRIM(fct_str(td_var%t_dim(1)%i_len))//","//&
-         &                 TRIM(fct_str(td_var%t_dim(2)%i_len))//&
-         &                 ") and level ("//&
-         &                 TRIM(fct_str(td_mask(1)%t_dim(1)%i_len))//","//&
-         &                 TRIM(fct_str(td_mask(1)%t_dim(2)%i_len))//")")
-      ELSE
-         ALLOCATE( il_mask(td_var%t_dim(1)%i_len, &
-         &                 td_var%t_dim(2)%i_len) )
-
-         SELECT CASE(TRIM(td_var%c_point))
-         CASE('T')
-            il_mask(:,:)=INT(td_mask(jp_T)%d_value(:,:,1,1))
-         CASE('U')
-            il_mask(:,:)=INT(td_mask(jp_U)%d_value(:,:,1,1))
-         CASE('V')
-            il_mask(:,:)=INT(td_mask(jp_V)%d_value(:,:,1,1))
-         CASE('F')
-            il_mask(:,:)=INT(td_mask(jp_F)%d_value(:,:,1,1))
-         END SELECT
-
-         DO jl=1,td_var%t_dim(4)%i_len
-            DO jk=1,td_var%t_dim(3)%i_len
-               WHERE( il_mask(:,:) < jk ) td_var%d_value(:,:,jk,jl)=99 !td_var%d_fill
-                  !.AND. &
-                  !&   td_var%d_value(:,:,jk,jl) == td_var%d_fill .OR. &
-                  !&   il_mask(:,:) < jk .AND. &
-                  !&   td_var%d_value(:,:,jk,jl) == 1 ) td_var%d_value(:,:,jk,jl)=99 !td_var%d_fill
+      IF( ALL(td_var%t_dim(1:2)%l_use) )THEN
+         IF( ANY(td_var%t_dim(1:2)%i_len /= td_mask(1)%t_dim(1:2)%i_len) )THEN
+            CALL logger_error("CREATE RESTART MASK: dimension differ between"//&
+            &                 " variable "//TRIM(td_var%c_name)//" ("//&
+            &                 TRIM(fct_str(td_var%t_dim(1)%i_len))//","//&
+            &                 TRIM(fct_str(td_var%t_dim(2)%i_len))//&
+            &                 ") and level ("//&
+            &                 TRIM(fct_str(td_mask(1)%t_dim(1)%i_len))//","//&
+            &                 TRIM(fct_str(td_mask(1)%t_dim(2)%i_len))//")")
+         ELSE
+            ALLOCATE( il_mask(td_var%t_dim(1)%i_len, &
+            &                 td_var%t_dim(2)%i_len) )
+
+            SELECT CASE(TRIM(td_var%c_point))
+            CASE('T')
+               il_mask(:,:)=INT(td_mask(jp_T)%d_value(:,:,1,1))
+            CASE('U')
+               il_mask(:,:)=INT(td_mask(jp_U)%d_value(:,:,1,1))
+            CASE('V')
+               il_mask(:,:)=INT(td_mask(jp_V)%d_value(:,:,1,1))
+            CASE('F')
+               il_mask(:,:)=INT(td_mask(jp_F)%d_value(:,:,1,1))
+            END SELECT
+
+            DO jl=1,td_var%t_dim(4)%i_len
+               DO jk=1,td_var%t_dim(3)%i_len
+                  WHERE( il_mask(:,:) < jk )
+                     td_var%d_value(:,:,jk,jl)=td_var%d_fill
+                  END WHERE
+               ENDDO
             ENDDO
-         ENDDO
-
-         DEALLOCATE( il_mask )
+
+            DEALLOCATE( il_mask )
+         ENDIF
       ENDIF
    END SUBROUTINE create_restart_mask
-   !> @endcode
    !-------------------------------------------------------------------
    !> @brief
-   !> This subroutine
+   !> This subroutine interpolate variable
    !> 
-   !> @details 
-   !>
    !> @author J.Paul
    !> - Nov, 2013- Initial Version
    !>
-   !> @param[in] 
+   !> @param[inout] td_var    variable structure 
+   !> @param[inout] td_level  fine grid level, array of variable structure
+   !> @param[in] id_rho       array of refinment factor
+   !> @param[in] id_offset    array of offset between fine and coarse grid
+   !> @param[in] id_iext      i-direction size of extra bands (default=im_minext)
+   !> @param[in] id_jext      j-direction size of extra bands (default=im_minext)
    !-------------------------------------------------------------------
-   !> @code
-   FUNCTION create_restart_extract(td_var, td_file, &
-   &                             td_coord)
-      IMPLICIT NONE
-      ! Argument
-      TYPE(TVAR) , INTENT(IN) :: td_var  
-      TYPE(TFILE), INTENT(IN) :: td_file
-      TYPE(TFILE), INTENT(IN) :: td_coord
-
-      ! function
-      TYPE(TVAR) :: create_restart_extract
-
-      ! local variable
-      INTEGER(i4), DIMENSION(2,2,2) :: il_ind
-
-      INTEGER(i4) :: il_pivot
-      INTEGER(i4) :: il_perio
-
-      INTEGER(i4) :: il_imin
-      INTEGER(i4) :: il_jmin
-      INTEGER(i4) :: il_imax
-      INTEGER(i4) :: il_jmax
-
-      TYPE(TFILE) :: tl_file
-
-      TYPE(TMPP)  :: tl_mpp
-
-      TYPE(TATT)  :: tl_att
-
-      TYPE(TVAR)  :: tl_var
-      
-      TYPE(TDOM)  :: tl_dom
-      ! loop indices
-      !----------------------------------------------------------------
-
-      IF( td_file%i_id == 0 )THEN
-         CALL logger_error("CREATE RESTART EXTRACT: file "//&
-         &  TRIM(td_file%c_name)//" not opened ")
-      ELSE
-
-         !init
-         tl_file=td_file
-
-         !1- open file
-         CALL iom_open(tl_file)
-
-         ! get periodicity
-         il_pivot=grid_get_pivot(tl_file)
-         il_perio=grid_get_perio(tl_file,il_pivot)
-
-         tl_file%i_perio=il_perio
-
-         !2- compute file grid indices around coord grid
-         il_ind(:,:,:)=grid_get_coarse_index(tl_file, td_coord )
-
-         il_imin=il_ind(1,1,1) ; il_imax=il_ind(1,2,1)
-         il_jmin=il_ind(2,1,1) ; il_jmax=il_ind(2,2,1)
-   
-         !3- check grid coincidence
-         CALL grid_check_coincidence( tl_file, td_coord, &
-         &                            il_imin, il_imax, &
-         &                            il_jmin, il_jmax, &
-         &                            (/1, 1, 1/) )
-
-         !4- compute domain
-         tl_dom=dom_init(tl_file,         &
-         &               il_imin, il_imax, &
-         &               il_jmin, il_jmax)
-
-         ! close file
-         CALL iom_close(tl_file)
-
-         !5- read bathymetry on domain (ugly way to do it, have to work on it)
-         !5-1 init mpp structure
-         tl_mpp=mpp_init(tl_file)
-
-         CALL file_clean(tl_file)
-
-         !5-2 get processor to be used
-         CALL mpp_get_use( tl_mpp, tl_dom )
-
-         !5-3 open mpp files
-         CALL iom_mpp_open(tl_mpp)
-
-         !5-4 read variable on domain
-         tl_var=iom_mpp_read_var(tl_mpp,TRIM(td_var%c_name),td_dom=tl_dom)
-
-         !5-5 close mpp file
-         CALL iom_mpp_close(tl_mpp)
-
-         !6- add ghost cell
-         CALL grid_add_ghost(tl_var,tl_dom%i_ighost,tl_dom%i_jghost)
-
-         !7- check result
-         IF( ANY( tl_var%t_dim(:)%l_use .AND. &
-         &        tl_var%t_dim(:)%i_len /= td_coord%t_dim(:)%i_len) )THEN
-            CALL logger_debug("CREATE BATHY EXTRACT: "//&
-            &        "dimensoin of variable "//TRIM(td_var%c_name)//" "//&
-            &        TRIM(fct_str(tl_var%t_dim(1)%i_len))//","//&
-            &        TRIM(fct_str(tl_var%t_dim(2)%i_len))//","//&
-            &        TRIM(fct_str(tl_var%t_dim(3)%i_len))//","//&
-            &        TRIM(fct_str(tl_var%t_dim(4)%i_len)) )
-            CALL logger_debug("CREATE BATHY EXTRACT: "//&
-            &        "dimensoin of coordinate file "//&
-            &        TRIM(fct_str(td_coord%t_dim(1)%i_len))//","//&
-            &        TRIM(fct_str(td_coord%t_dim(2)%i_len))//","//&
-            &        TRIM(fct_str(td_coord%t_dim(3)%i_len))//","//&
-            &        TRIM(fct_str(td_coord%t_dim(4)%i_len)) )
-            CALL logger_fatal("CREATE BATHY EXTRACT: "//&
-            &  "dimensoin of extracted "//&
-            &  "variable and coordinate file dimension differ")
-         ENDIF
-
-         !8- add attribute to variable
-         tl_att=att_init('src_file',TRIM(fct_basename(tl_mpp%c_name)))
-         CALL var_move_att(tl_var, tl_att)         
-
-         !9- save result
-         create_restart_extract=tl_var
-
-         ! clean structure
-         CALL var_clean(tl_var)
-         CALL mpp_clean(tl_mpp)
-      ENDIF
-
-   END FUNCTION create_restart_extract
-   !> @endcode
-   !-------------------------------------------------------------------
-   !> @brief
-   !> This subroutine
-   !> 
-   !> @details 
-   !>
-   !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !>
-   !> @param[in] 
-   !> @todo 
-   !-------------------------------------------------------------------
-   !> @code
    SUBROUTINE create_restart_interp( td_var, td_level,&
    &                                 id_rho,          &
@@ -1191,6 +1096,4 @@
 
       ! local variable
-      TYPE(TVAR)  :: tl_var
-
       INTEGER(i4) :: il_iext
       INTEGER(i4) :: il_jext
@@ -1198,7 +1101,4 @@
       ! loop indices
       !----------------------------------------------------------------
-
-      ! copy variable
-      tl_var=td_var
 
       il_iext=3
@@ -1220,31 +1120,128 @@
       ENDIF
 
-      il_iext=0
-      il_jext=0
-
       ! work on variable
-      !1 add extraband
-      CALL extrap_add_extrabands(tl_var, il_iext, il_jext)
-
-      !2 extrapolate variable
-      CALL extrap_fill_value( tl_var, td_level(:),    &
+      ! add extraband
+      CALL extrap_add_extrabands(td_var, il_iext, il_jext)
+
+      ! extrapolate variable
+      CALL extrap_fill_value( td_var, td_level(:),    &
       &                               id_offset(:,:), &
       &                               id_rho(:),      &
       &                               id_iext=il_iext, id_jext=il_jext )
 
-      !3 interpolate variable
-      CALL interp_fill_value( tl_var, id_rho(:), &
+      ! interpolate variable
+      CALL interp_fill_value( td_var, id_rho(:), &
       &                       id_offset=id_offset(:,:) )
 
-      !4 remove extraband
-      CALL extrap_del_extrabands(tl_var, il_iext*id_rho(jp_I), il_jext*id_rho(jp_J))
-
-      !5- save result
-      td_var=tl_var
-
-      ! clean variable structure
-      CALL var_clean(tl_var)
+      ! remove extraband
+      CALL extrap_del_extrabands(td_var, il_iext*id_rho(jp_I), il_jext*id_rho(jp_J))
 
    END SUBROUTINE create_restart_interp
-   !> @endcode
+   !-------------------------------------------------------------------
+   !> @brief
+   !> This subroutine get depth variable value in an open mpp structure
+   !> and check if agree with already input depth variable.
+   !> 
+   !> @details 
+   !>
+   !> @author J.Paul
+   !> - November, 2014- Initial Version
+   !>
+   !> @param[in] td_mpp       mpp structure
+   !> @param[inout] td_depth  depth variable structure 
+   !-------------------------------------------------------------------
+   SUBROUTINE create_restart_check_depth( td_mpp, td_depth )
+
+      IMPLICIT NONE
+
+      ! Argument
+      TYPE(TMPP), INTENT(IN   ) :: td_mpp
+      TYPE(TVAR), INTENT(INOUT) :: td_depth
+
+      ! local variable
+      INTEGER(i4) :: il_varid
+      TYPE(TVAR)  :: tl_depth
+      ! loop indices
+      !----------------------------------------------------------------
+
+      ! get or check depth value
+      IF( td_mpp%t_proc(1)%i_depthid /= 0 )THEN
+
+         il_varid=td_mpp%t_proc(1)%i_depthid
+         IF( ASSOCIATED(td_depth%d_value) )THEN
+
+            tl_depth=iom_mpp_read_var(td_mpp, il_varid)
+            IF( ANY( td_depth%d_value(:,:,:,:) /= &
+            &        tl_depth%d_value(:,:,:,:) ) )THEN
+
+               CALL logger_fatal("CREATE BOUNDARY: depth value from "//&
+               &  TRIM(tl_multi%t_mpp(ji)%c_name)//" not conform "//&
+               &  " to those from former file(s).")
+
+            ENDIF
+            CALL var_clean(tl_depth)
+
+         ELSE
+            td_depth=iom_mpp_read_var(td_mpp,il_varid)
+         ENDIF
+
+      ENDIF
+      
+   END SUBROUTINE create_restart_check_depth
+   !-------------------------------------------------------------------
+   !> @brief
+   !> This subroutine get date and time in an open mpp structure
+   !> and check if agree with date and time already read.
+   !> 
+   !> @details 
+   !>
+   !> @author J.Paul
+   !> - November, 2014- Initial Version
+   !>
+   !> @param[in] td_mpp      mpp structure
+   !> @param[inout] td_time  time variable structure 
+   !-------------------------------------------------------------------
+   SUBROUTINE create_restart_check_time( td_mpp, td_time )
+
+      IMPLICIT NONE
+
+      ! Argument
+      TYPE(TMPP), INTENT(IN   ) :: td_mpp
+      TYPE(TVAR), INTENT(INOUT) :: td_time
+
+      ! local variable
+      INTEGER(i4) :: il_varid
+      TYPE(TVAR)  :: tl_time
+
+      TYPE(TDATE) :: tl_date1
+      TYPE(TDATE) :: tl_date2
+      ! loop indices
+      !----------------------------------------------------------------
+
+      ! get or check depth value
+      IF( td_mpp%t_proc(1)%i_timeid /= 0 )THEN
+
+         il_varid=td_mpp%t_proc(1)%i_timeid
+         IF( ASSOCIATED(td_time%d_value) )THEN
+
+            tl_time=iom_mpp_read_var(td_mpp, il_varid)
+
+            tl_date1=var_to_date(td_time)
+            tl_date2=var_to_date(tl_time)
+            IF( tl_date1 - tl_date2 /= 0 )THEN
+
+               CALL logger_fatal("CREATE BOUNDARY: date from "//&
+               &  TRIM(tl_multi%t_mpp(ji)%c_name)//" not conform "//&
+               &  " to those from former file(s).")
+
+            ENDIF
+            CALL var_clean(tl_time)
+
+         ELSE
+            td_time=iom_mpp_read_var(td_mpp,il_varid)
+         ENDIF
+
+      ENDIF
+      
+   END SUBROUTINE create_restart_check_time
 END PROGRAM create_restart
