Index: branches/2014/dev_r4704_NOC5_MPP_BDY_UPDATE/NEMOGCM/TOOLS/SIREN/src/merge_bathy.f90
===================================================================
--- branches/2014/dev_r4704_NOC5_MPP_BDY_UPDATE/NEMOGCM/TOOLS/SIREN/src/merge_bathy.f90	(revision 4705)
+++ branches/2014/dev_r4704_NOC5_MPP_BDY_UPDATE/NEMOGCM/TOOLS/SIREN/src/merge_bathy.f90	(revision 6225)
@@ -7,25 +7,128 @@
 !
 ! DESCRIPTION:
+!> @file
 !> @brief 
 !> This program merge bathymetry file at boundaries.
 !>
 !> @details
+!> @section sec1 method
 !> Coarse grid Bathymetry is interpolated on fine grid. 
-!> Then fine Bathymetry and refined coarse bathymetry are merged at boundaries.
-!>
-!> BathyFine= weight * BathyCoarse + (1-weight)*BathyFine
-!>
-!> The weight function used is : 0.5 + 0.5*COS( (pi*dist) / width ) 
-!>
-!> @author
-!> J.Paul
+!> Then fine Bathymetry and refined coarse bathymetry are merged at boundaries.<br/>
+!>    @f[BathyFine= Weight * BathyCoarse + (1-Weight)*BathyFine@f]
+!> The weight function used is :<br/>
+!>       @f[Weight = 0.5 + 0.5*COS( \frac{\pi*dist}{width} )@f]<br/>
+!> with
+!> - dist : number of point to border 
+!> - width : boundary size
+!>
+!> @section sec2 how to
+!>    to merge bathymetry file:<br/>
+!> @code{.sh}
+!>    ./SIREN/bin/merge_bathy merge_bathy.nam
+!> @endcode
+!>    
+!> @note 
+!>    you could find a template of the namelist in templates directory.
+!>
+!>    merge_bathy.nam comprise 8 namelists:
+!>       - logger namelist (namlog)
+!>       - config namelist (namcfg)
+!>       - coarse grid namelist (namcrs)
+!>       - fine grid namelist (namfin)
+!>       - variable namelist (namvar)
+!>       - nesting namelist (namnst)
+!>       - boundary namelist (nambdy)
+!>       - output namelist (namout)
+!> 
+!>    @note 
+!>       All namelists have to be in file merge_bathy.nam, 
+!>       however variables of those namelists are all optional.
+!>
+!>    * _logger namelist (namlog)_:
+!>       - cn_logfile   : logger filename
+!>       - cn_verbosity : verbosity ('trace','debug','info',
+!>  'warning','error','fatal','none')
+!>       - in_maxerror  : maximum number of error allowed
+!>
+!>    * _config namelist (namcfg)_:
+!>       - cn_varcfg : variable configuration file (see ./SIREN/cfg/variable.cfg)
+!>
+!>    * _coarse grid namelist (namcrs)_:
+!>       - cn_bathy0 : bathymetry 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)_:
+!>       - cn_bathy1 : bathymetry file
+!>       - in_perio1 : NEMO periodicity index
+!>
+!>    * _variable namelist (namvar)_:
+!>       - cn_varinfo : list of variable and extra information about request(s) 
+!>       to be used (separated by ',').<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:
+!>             - int = interpolation method
+!> 
+!>                requests must be separated by ';'.<br/>
+!>                order of requests does not matter.<br/>
+!>
+!>          informations about available method could be find in 
+!>          @ref interp modules.<br/>
+!>          Example: 'bathymetry: int=cubic'
+!>          @note 
+!>             If you do not specify a method which is required, 
+!>             default one is apply.
+!>          @warning 
+!>             variable name must be __Bathymetry__ here.
+!>
+!>    * _nesting namelist (namnst)_:
+!>       - in_rhoi  : refinement factor in i-direction
+!>       - in_rhoj  : refinement factor in j-direction
+!>
+!>    * _boundary namelist (nambdy)_:
+!>       - ln_north : use north boundary or not
+!>       - ln_south : use south boundary or not
+!>       - ln_east  : use east  boundary or not
+!>       - ln_west  : use west  boundary or not
+!>       - cn_north : north boundary indices on fine grid<br/>
+!>          *cn_north* is a string character defining boundary
+!>          segmentation.<br/>
+!>          segments are separated by '|'.<br/>
+!>          each segments of the boundary is composed of:
+!>             - indice of velocity (orthogonal to boundary .ie. 
+!>                for north boundary, J-indice). 
+!>             - indice of segment start (I-indice for north boundary) 
+!>             - indice of segment end   (I-indice for north boundary)<br/>
+!>                indices must be separated by ':' .<br/>
+!>             - optionally, boundary size could be added between '(' and ')' 
+!>             in the first segment defined.
+!>                @note 
+!>                   boundary size is the same for all segments of one boundary.
+!>
+!>          Examples:
+!>             - cn_north='index1,first1:last1(width)'
+!>             - cn_north='index1(width),first1:last1|index2,first2:last2'
+!>
+!>       - cn_south : south boundary indices on fine grid<br/>
+!>       - cn_east  : east  boundary indices on fine grid<br/>
+!>       - cn_west  : west  boundary indices on fine grid<br/>
+!>       - ln_oneseg: use only one segment for each boundary or not
+!>
+!>    * _output namelist (namout)_:
+!>       - cn_fileout : merged bathymetry file
+!>
+!> @author J.Paul
 ! REVISION HISTORY:
-!> @date Nov, 2013 - Initial Version
-!
+!> @date November, 2013 - Initial Version
+!> @date Sepember, 2014 
+!> - add header for user
+!> @date July, 2015 
+!> - extrapolate all land points
+!> - add attributes with boundary string character (as in namelist)
+!>
 !> @note Software governed by the CeCILL licence     (NEMOGCM/NEMO_CeCILL.txt)
-!>
-!> @todo
 !----------------------------------------------------------------------
-!> @code
 PROGRAM merge_bathy
 
@@ -43,5 +146,4 @@
    USE boundary                        ! boundary manager
    USE iom                             ! I/O manager
-   USE dom                             ! domain manager
    USE grid                            ! grid manager
    USE extrap                          ! extrapolation manager
@@ -49,5 +151,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
@@ -56,17 +160,17 @@
    CHARACTER(LEN=lc)                                  :: cl_namelist
    CHARACTER(LEN=lc)                                  :: cl_date
+   CHARACTER(LEN=lc)                                  :: cl_tmp
 
    INTEGER(i4)                                        :: il_narg
    INTEGER(i4)                                        :: il_status
    INTEGER(i4)                                        :: il_fileid
-   INTEGER(i4)                                        :: il_attid
+   INTEGER(i4)                                        :: il_attind
    INTEGER(i4)                                        :: il_imin0
    INTEGER(i4)                                        :: il_imax0
    INTEGER(i4)                                        :: il_jmin0
    INTEGER(i4)                                        :: il_jmax0
+   INTEGER(i4)                                        :: il_shift
    INTEGER(i4)      , DIMENSION(ip_maxdim)            :: il_rho
-   INTEGER(i4)      , DIMENSION(2,2)                  :: il_offset
-   INTEGER(i4)      , DIMENSION(2,2,2)                :: il_ind
-!   INTEGER(i4)      , DIMENSION(:,:,:,:), ALLOCATABLE :: il_value
+   INTEGER(i4)      , DIMENSION(2,2)                  :: il_ind
 
    LOGICAL                                            :: ll_exist
@@ -76,6 +180,6 @@
    REAL(dp)         , DIMENSION(:,:,:,:), ALLOCATABLE :: dl_weight
 
-   TYPE(TFILE)                                        :: tl_bathy0
-   TYPE(TFILE)                                        :: tl_bathy1
+   TYPE(TMPP)                                         :: tl_bathy0
+   TYPE(TMPP)                                         :: tl_bathy1
    TYPE(TFILE)                                        :: tl_fileout
    
@@ -85,6 +189,4 @@
    TYPE(TVAR)                                         :: tl_lon
    TYPE(TVAR)                                         :: tl_lat
-!   TYPE(TVAR)                                         :: tl_depth
-!   TYPE(TVAR)                                         :: tl_time
    
    TYPE(TDIM)       , DIMENSION(ip_maxdim)            :: tl_dim
@@ -98,24 +200,28 @@
 
    ! namelist variable
+   ! namlog
    CHARACTER(LEN=lc)                       :: cn_logfile = 'merge_bathy.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_bathy0 = '' 
    INTEGER(i4)                             :: in_perio0 = -1
 
+   ! namfin
    CHARACTER(LEN=lc)                       :: cn_bathy1 = '' 
    INTEGER(i4)                             :: in_perio1 = -1
 
-   CHARACTER(LEN=lc)                       :: cn_varcfg = 'variable.cfg' 
-
-   CHARACTER(LEN=lc), DIMENSION(ig_maxvar) :: cn_varinfo = ''
-
-   INTEGER(i4)                             :: in_imin0 = 0
-   INTEGER(i4)                             :: in_imax0 = 0
-   INTEGER(i4)                             :: in_jmin0 = 0
-   INTEGER(i4)                             :: in_jmax0 = 0
+   ! namvar
+   CHARACTER(LEN=lc), DIMENSION(ip_maxvar) :: cn_varinfo = ''
+
+   ! namnst
    INTEGER(i4)                             :: in_rhoi  = 0
    INTEGER(i4)                             :: in_rhoj  = 0
 
+   ! nambdy
    LOGICAL                                 :: ln_north = .TRUE.
    LOGICAL                                 :: ln_south = .TRUE.
@@ -128,4 +234,5 @@
    CHARACTER(LEN=lc)                       :: cn_west  = ''
 
+   ! namout
    CHARACTER(LEN=lc)                       :: cn_fileout = 'bathy_merged.nc' 
    !-------------------------------------------------------------------
@@ -133,5 +240,6 @@
    NAMELIST /namlog/ &  !< logger namelist
    &  cn_logfile,    &  !< log file
-   &  cn_verbosity      !< log verbosity
+   &  cn_verbosity,  &  !< log verbosity
+   &  in_maxerror       !< logger maximum error
 
    NAMELIST /namcfg/ &  !< config namelist
@@ -152,8 +260,4 @@
    
    NAMELIST /namnst/ &  !< nesting namelist
-   &  in_imin0,   &     !< i-direction lower left  point indice on coarse grid 
-   &  in_imax0,   &     !< i-direction upper right point indice on coarse grid
-   &  in_jmin0,   &     !< j-direction lower left  point indice on coarse grid
-   &  in_jmax0,   &     !< j-direction upper right point indice on coarse grid
    &  in_rhoi,    &     !< refinement factor in i-direction
    &  in_rhoj           !< refinement factor in j-direction
@@ -171,9 +275,9 @@
 
    NAMELIST /namout/ &  !< output namelist
-   &  cn_fileout       !< fine grid merged bathymetry file   
+   &  cn_fileout        !< fine grid merged bathymetry file   
    !-------------------------------------------------------------------
 
-   !1- namelist
-   !1-1 get namelist
+   ! namelist
+   ! get namelist
    il_narg=COMMAND_ARGUMENT_COUNT() !f03 intrinsec
    IF( il_narg/=1 )THEN
@@ -184,5 +288,5 @@
    ENDIF
    
-   !1-2 read namelist
+   ! read namelist
    INQUIRE(FILE=TRIM(cl_namelist), EXIST=ll_exist)
    IF( ll_exist )THEN
@@ -203,10 +307,10 @@
 
       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))
 
@@ -214,5 +318,5 @@
       READ( il_fileid, NML = namfin )
       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)
 
@@ -234,8 +338,8 @@
    ENDIF
 
-   !2- open files
+   ! open files
    IF( TRIM(cn_bathy0) /= '' )THEN
-      tl_bathy0=file_init(TRIM(cn_bathy0),id_perio=in_perio0)
-      CALL iom_open(tl_bathy0)
+      tl_bathy0=mpp_init( file_init(TRIM(cn_bathy0)), id_perio=in_perio0)
+      CALL grid_get_info(tl_bathy0)
    ELSE
       CALL logger_fatal("MERGE BATHY: can not find coarse grid bathymetry "//&
@@ -244,6 +348,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("MERGE BATHY: can not find fine grid bathymetry "//&
@@ -251,6 +355,6 @@
    ENDIF
 
-   !3- check
-   !3-1 check output file do not already exist
+   ! check
+   ! check output file do not already exist
    INQUIRE(FILE=TRIM(cn_fileout), EXIST=ll_exist)
    IF( ll_exist )THEN
@@ -259,6 +363,6 @@
    ENDIF
 
-   !3-2 check namelist
-   !3-2-1 check refinament factor
+   ! check namelist
+   ! check refinament factor
    il_rho(:)=1
    IF( in_rhoi < 1 .OR. in_rhoj < 1 )THEN
@@ -270,25 +374,16 @@
    ENDIF
 
-   !3-2-2 check domain indices
-   IF( in_imin0 < 1 .OR. in_imax0 < 1 .OR. in_jmin0 < 1 .OR. in_jmax0 < 1)THEN
-      ! compute coarse grid indices around fine grid
-      il_ind(:,:,:)=grid_get_coarse_index(tl_bathy0, tl_bathy1 )
-
-      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-3 check domain validity
+   ! check domain indices
+   ! compute coarse grid indices around fine grid
+   il_ind(:,:)=grid_get_coarse_index(tl_bathy0, tl_bathy1, &
+   &                                 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_bathy0, il_imin0, il_imax0, il_jmin0, il_jmax0)
 
-   !3-4 check coincidence between coarse and fine grid
+   ! check coincidence between coarse and fine grid
    CALL grid_check_coincidence( tl_bathy0, tl_bathy1, &
    &                            il_imin0, il_imax0, &
@@ -296,6 +391,12 @@
    &                            il_rho(:) )
 
-   !4- read or compute boundary
-   tl_var=iom_read_var(tl_bathy1,'Bathymetry')
+   ! open mpp files
+   CALL iom_mpp_open(tl_bathy1)
+
+   ! read or compute boundary
+   tl_var=iom_mpp_read_var(tl_bathy1,'Bathymetry')
+
+   ! close mpp files
+   CALL iom_mpp_close(tl_bathy1)
 
    tl_bdy(:)=boundary_init(tl_var, ln_north, ln_south, ln_east, ln_west, &
@@ -303,6 +404,6 @@
    &                               ln_oneseg ) 
 
-   !5- get boundary on coarse grid
-   !5-1 define refined bathymetry table (for coarse grid)
+   ! get boundary on coarse grid
+   ! define refined bathymetry array (for coarse grid)
    dl_fill=tl_var%d_fill
    ALLOCATE( dl_refined(tl_var%t_dim(1)%i_len, &
@@ -313,5 +414,5 @@
    dl_refined(:,:,:,:)=dl_fill
 
-   !5-2 define weight table
+   ! define weight array
    ALLOCATE( dl_weight(tl_var%t_dim(1)%i_len, &
    &                   tl_var%t_dim(2)%i_len, &
@@ -320,7 +421,6 @@
    dl_weight(:,:,:,:)=dl_fill 
 
-   !5-3 compute coarse grid refined bathymetry on boundary.
+   ! compute coarse grid refined bathymetry on boundary.
    DO jk=1,ip_ncard
-
       CALL merge_bathy_get_boundary(tl_bathy0, tl_bathy1, tl_bdy(jk), &
       &                             il_rho(:),                        &
@@ -330,5 +430,5 @@
    ENDDO
 
-   !6- merge bathy on boundary
+   ! merge bathy on boundary
    DO jl=1,tl_var%t_dim(4)%i_len
       DO jk=1,tl_var%t_dim(3)%i_len
@@ -348,9 +448,9 @@
    DEALLOCATE(dl_refined)
 
-   !7- create file
+   ! create file
    tl_fileout=file_init(TRIM(cn_fileout),id_perio=in_perio1)
 
-   !7-1 add dimension
-   tl_dim(:)=tl_var%t_dim(:)
+   ! add dimension
+   tl_dim(:)=dim_copy(tl_var%t_dim(:))
 
    DO ji=1,ip_maxdim
@@ -358,15 +458,21 @@
    ENDDO
 
-   !7-2 add variables
+   ! add variables
    IF( ALL( tl_dim(1:2)%l_use ) )THEN
-
-      tl_lon=iom_read_var(tl_bathy1,'longitude')
+      ! open mpp files
+      CALL iom_mpp_open(tl_bathy1)
+
+      ! add longitude
+      tl_lon=iom_mpp_read_var(tl_bathy1,'longitude')
       CALL file_add_var(tl_fileout, tl_lon)
       CALL var_clean(tl_lon)
 
-      tl_lat=iom_read_var(tl_bathy1,'latitude')
+      ! add latitude
+      tl_lat=iom_mpp_read_var(tl_bathy1,'latitude')
       CALL file_add_var(tl_fileout, tl_lat)
       CALL var_clean(tl_lat)
 
+      ! close mpp files
+      CALL iom_mpp_close(tl_bathy1)      
    ENDIF
 
@@ -375,5 +481,5 @@
 
    ! only 2 first dimension to be used
-   tl_dim(:)=tl_fileout%t_dim(:)
+   tl_dim(:)=dim_copy(tl_fileout%t_dim(:))
    tl_dim(3:4)%l_use=.FALSE.
    tl_var=var_init('weight',dl_weight(:,:,:,:),td_dim=tl_dim(:),dd_fill=dl_fill)
@@ -381,5 +487,5 @@
    CALL var_clean(tl_var)
 
-   !7-3 add some attribute
+   ! add some attribute
    tl_att=att_init("Created_by","SIREN merge_bathy")
    CALL file_add_att(tl_fileout, tl_att)
@@ -395,41 +501,113 @@
    CALL file_add_att(tl_fileout, tl_att)
 
-   ! a voir
    ! add attribute periodicity
-   il_attid=0
+   il_attind=0
    IF( ASSOCIATED(tl_fileout%t_att) )THEN
-      il_attid=att_get_id(tl_fileout%t_att(:),'periodicity')
-   ENDIF
-   IF( tl_bathy1%i_perio >= 0 .AND. il_attid == 0 )THEN
+      il_attind=att_get_index(tl_fileout%t_att(:),'periodicity')
+   ENDIF
+   IF( tl_bathy1%i_perio >= 0 .AND. il_attind == 0 )THEN
       tl_att=att_init('periodicity',tl_bathy1%i_perio)
       CALL file_add_att(tl_fileout,tl_att)
    ENDIF
 
-   il_attid=0
+   il_attind=0
    IF( ASSOCIATED(tl_fileout%t_att) )THEN
-      il_attid=att_get_id(tl_fileout%t_att(:),'ew_overlap')
-   ENDIF
-   IF( tl_bathy1%i_ew >= 0 .AND. il_attid == 0 )THEN
+      il_attind=att_get_index(tl_fileout%t_att(:),'ew_overlap')
+   ENDIF
+   IF( tl_bathy1%i_ew >= 0 .AND. il_attind == 0 )THEN
       tl_att=att_init('ew_overlap',tl_bathy1%i_ew)
       CALL file_add_att(tl_fileout,tl_att)
    ENDIF
 
-   !7-4 create file
+
+   IF( tl_bdy(jp_north)%l_use )THEN
+      ! add shift on north boundary
+      ! boundary compute on T point but express on U or V point
+      il_shift=1
+
+      cl_tmp=TRIM(fct_str(tl_bdy(jp_north)%t_seg(1)%i_index-il_shift))//','//&
+         &   TRIM(fct_str(tl_bdy(jp_north)%t_seg(1)%i_first))//':'//&
+         &   TRIM(fct_str(tl_bdy(jp_north)%t_seg(1)%i_last))//&
+         &   '('//TRIM(fct_str(tl_bdy(jp_north)%t_seg(1)%i_width))//')'
+      DO ji=2,tl_bdy(jp_north)%i_nseg
+         cl_tmp=TRIM(cl_tmp)//'|'//&
+            &   TRIM(fct_str(tl_bdy(jp_north)%t_seg(ji)%i_index-il_shift))//','//&
+            &   TRIM(fct_str(tl_bdy(jp_north)%t_seg(ji)%i_first))//':'//&
+            &   TRIM(fct_str(tl_bdy(jp_north)%t_seg(ji)%i_last))
+      ENDDO
+      tl_att=att_init("bdy_north",TRIM(cl_tmp))
+      CALL file_add_att(tl_fileout, tl_att)
+   ENDIF
+
+   IF( tl_bdy(jp_south)%l_use )THEN
+      
+      cl_tmp=TRIM(fct_str(tl_bdy(jp_south)%t_seg(1)%i_index))//','//&
+         &   TRIM(fct_str(tl_bdy(jp_south)%t_seg(1)%i_first))//':'//&
+         &   TRIM(fct_str(tl_bdy(jp_south)%t_seg(1)%i_last))//&
+         &   '('//TRIM(fct_str(tl_bdy(jp_south)%t_seg(1)%i_width))//')'
+      DO ji=2,tl_bdy(jp_south)%i_nseg
+         cl_tmp=TRIM(cl_tmp)//'|'//&
+            &   TRIM(fct_str(tl_bdy(jp_south)%t_seg(ji)%i_index))//','//&
+            &   TRIM(fct_str(tl_bdy(jp_south)%t_seg(ji)%i_first))//':'//&
+            &   TRIM(fct_str(tl_bdy(jp_south)%t_seg(ji)%i_last))
+      ENDDO
+
+      tl_att=att_init("bdy_south",TRIM(cl_tmp))
+      CALL file_add_att(tl_fileout, tl_att)
+   ENDIF
+
+   IF( tl_bdy(jp_east)%l_use )THEN
+      ! add shift on east boundary
+      ! boundary compute on T point but express on U or V point
+      il_shift=1
+
+      cl_tmp=TRIM(fct_str(tl_bdy(jp_east)%t_seg(1)%i_index-il_shift))//','//&
+         &   TRIM(fct_str(tl_bdy(jp_east)%t_seg(1)%i_first))//':'//&
+         &   TRIM(fct_str(tl_bdy(jp_east)%t_seg(1)%i_last))//&
+         &   '('//TRIM(fct_str(tl_bdy(jp_east)%t_seg(1)%i_width))//')'
+      DO ji=2,tl_bdy(jp_east)%i_nseg
+         cl_tmp=TRIM(cl_tmp)//'|'//&
+            &   TRIM(fct_str(tl_bdy(jp_east)%t_seg(ji)%i_index-il_shift))//','//&
+            &   TRIM(fct_str(tl_bdy(jp_east)%t_seg(ji)%i_first))//':'//&
+            &   TRIM(fct_str(tl_bdy(jp_east)%t_seg(ji)%i_last))
+      ENDDO
+
+      tl_att=att_init("bdy_east",TRIM(cl_tmp))
+      CALL file_add_att(tl_fileout, tl_att)
+   ENDIF
+
+   IF( tl_bdy(jp_west)%l_use )THEN
+
+      cl_tmp=TRIM(fct_str(tl_bdy(jp_west)%t_seg(1)%i_index))//','//&
+         &   TRIM(fct_str(tl_bdy(jp_west)%t_seg(1)%i_first))//':'//&
+         &   TRIM(fct_str(tl_bdy(jp_west)%t_seg(1)%i_last))//&
+         &   '('//TRIM(fct_str(tl_bdy(jp_west)%t_seg(1)%i_width))//')'
+      DO ji=2,tl_bdy(jp_west)%i_nseg
+         cl_tmp=TRIM(cl_tmp)//'|'//&
+            &   TRIM(fct_str(tl_bdy(jp_west)%t_seg(ji)%i_index))//','//&
+            &   TRIM(fct_str(tl_bdy(jp_west)%t_seg(ji)%i_first))//':'//&
+            &   TRIM(fct_str(tl_bdy(jp_west)%t_seg(ji)%i_last))
+      ENDDO
+
+      tl_att=att_init("bdy_west",TRIM(cl_tmp))
+      CALL file_add_att(tl_fileout, tl_att)
+   ENDIF
+
+   ! create file
    CALL iom_create(tl_fileout)
 
-   !7-5 write file
+   ! write file
    CALL iom_write_file(tl_fileout)
 
-   !7-6 close file
+   ! close file
    CALL iom_close(tl_fileout)
 
-   CALL iom_close(tl_bathy1)
-   CALL iom_close(tl_bathy0)
-
-   !8- clean
+   ! clean
+   CALL att_clean(tl_att)
    CALL file_clean(tl_fileout)
-   CALL file_clean(tl_bathy1)
-   CALL file_clean(tl_bathy0)
+   CALL mpp_clean(tl_bathy1)
+   CALL mpp_clean(tl_bathy0)
    DEALLOCATE(dl_weight)
+   CALL boundary_clean(tl_bdy(:))
 
    ! close log file
@@ -437,19 +615,20 @@
    CALL logger_close()
 
-!> @endcode
 CONTAINS
    !-------------------------------------------------------------------
    !> @brief
-   !> This subroutine
+   !> This subroutine compute refined bathymetry on boundary from coarse grid.
    !> 
-   !> @details 
+   !> @author J.Paul
+   !> @date November, 2013 - Initial Version
    !>
-   !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !>
-   !> @param[in] 
-   !> @todo 
+   !> @param[in] td_bathy0       coarse grid bathymetry file structure 
+   !> @param[in] td_bathy1       fine grid bathymetry file structure
+   !> @param[in] td_bdy          boundary structure
+   !> @param[in] id_rho          array of refinement factor
+   !> @param[inout] dd_refined   array of refined bathymetry 
+   !> @param[inout] dd_weight    array of weight
+   !> @param[in] dd_fill         fillValue
    !-------------------------------------------------------------------
-   !> @code
    SUBROUTINE merge_bathy_get_boundary( td_bathy0, td_bathy1, td_bdy, &
    &                                    id_rho,                       &
@@ -459,6 +638,6 @@
 
       ! Argument
-      TYPE(TFILE)                    , INTENT(IN   ) :: td_bathy0
-      TYPE(TFILE)                    , INTENT(IN   ) :: td_bathy1
+      TYPE(TMPP)                     , INTENT(IN   ) :: td_bathy0
+      TYPE(TMPP)                     , INTENT(IN   ) :: td_bathy1
       TYPE(TBDY)                     , INTENT(IN   ) :: td_bdy
       INTEGER(i4), DIMENSION(:)      , INTENT(IN   ) :: id_rho
@@ -478,11 +657,6 @@
       INTEGER(i4) :: il_jmax0
 
-      INTEGER(i4) :: il_imin
-      INTEGER(i4) :: il_imax
-      INTEGER(i4) :: il_jmin
-      INTEGER(i4) :: il_jmax
-
       INTEGER(i4), DIMENSION(2,2)         :: il_offset
-      INTEGER(i4), DIMENSION(2,2,2)       :: il_ind
+      INTEGER(i4), DIMENSION(2,2)         :: il_ind
 
       REAL(dp)   , DIMENSION(:)      , ALLOCATABLE :: dl_tmp1d
@@ -494,9 +668,6 @@
       TYPE(TVAR) :: tl_lat1
 
-      TYPE(TFILE) :: tl_bathy1
-      TYPE(TFILE) :: tl_bathy0
-
-      TYPE(TMPP)  :: tl_mppbathy1
-      TYPE(TMPP)  :: tl_mppbathy0
+      TYPE(TMPP)  :: tl_bathy1
+      TYPE(TMPP)  :: tl_bathy0
 
       TYPE(TDOM)  :: tl_dom1
@@ -510,6 +681,5 @@
       IF( td_bdy%l_use )THEN
          DO jl=1,td_bdy%i_nseg
-            
-            !1- get boundary definition
+            ! get boundary definition
             SELECT CASE(TRIM(td_bdy%c_card))
             CASE('north')
@@ -520,9 +690,4 @@
                il_jmax1=td_bdy%t_seg(jl)%i_index
 
-               il_imin=1
-               il_imax=il_imax1-il_imin1+1
-               il_jmin=td_bdy%t_seg(jl)%i_width
-               il_jmax=1
-
             CASE('south')
 
@@ -532,9 +697,4 @@
                il_jmax1=td_bdy%t_seg(jl)%i_index+(td_bdy%t_seg(jl)%i_width-1)
 
-               il_imin=1
-               il_imax=il_imax1-il_imin1+1
-               il_jmin=1
-               il_jmax=td_bdy%t_seg(jl)%i_width
-               
             CASE('east')
 
@@ -544,9 +704,4 @@
                il_jmax1=td_bdy%t_seg(jl)%i_last 
 
-               il_imin=td_bdy%t_seg(jl)%i_width
-               il_imax=1
-               il_jmin=1
-               il_jmax=il_jmax1-il_jmin1+1
-
             CASE('west')
 
@@ -556,117 +711,102 @@
                il_jmax1=td_bdy%t_seg(jl)%i_last 
 
-               il_imin=1
-               il_imax=td_bdy%t_seg(jl)%i_width
-               il_jmin=1
-               il_jmax=il_jmax1-il_jmin1+1
-
             END SELECT
 
-            !2 -read fine grid domain
-            tl_bathy1=td_bathy1
-            CALL iom_open(tl_bathy1)
-
-            !2-1 compute domain
+            ! -read fine grid domain
+            tl_bathy1=mpp_copy(td_bathy1)
+
+            ! compute domain
             tl_dom1=dom_init( tl_bathy1,         &
             &                 il_imin1, il_imax1,&
-            &                 il_jmin1, il_jmax1 )
-
-            !2-2 close file
-            CALL iom_close(tl_bathy1)
-
-            !2-3 read variables on domain (ugly way to do it, have to work on it)
-            !2-3-1 init mpp structure
-            tl_mppbathy1=mpp_init(tl_bathy1)
- 
-            CALL file_clean(tl_bathy1)
-
-            !2-3-2 get processor to be used
-            CALL mpp_get_use( tl_mppbathy1, tl_dom1 )
-
-            !2-3-3 open mpp files
-            CALL iom_mpp_open(tl_mppbathy1)
-
-            !2-3-4 read variable value on domain
-            tl_lon1=iom_mpp_read_var(tl_mppbathy1,'longitude',td_dom=tl_dom1)
-            tl_lat1=iom_mpp_read_var(tl_mppbathy1,'latitude' ,td_dom=tl_dom1)
-
-            !2-3-5 close mpp files
-            CALL iom_mpp_close(tl_mppbathy1)
-
-            !2-3-6 clean structure
-            CALL mpp_clean(tl_mppbathy1)
-
-            !3- get coarse grid indices
-            il_ind(:,:,:)=grid_get_coarse_index(td_bathy0, tl_lon1, tl_lat1, &
-            &                                   id_rho=id_rho(:))
-
-            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)
-
-            !4- read coarse grid bathymetry on domain
-            tl_bathy0=td_bathy0
-            CALL iom_open(tl_bathy0)
-
-            !4-1 compute domain
+            &                 il_jmin1, il_jmax1,&
+            &                 TRIM(td_bdy%c_card))
+
+            ! add extra band to fine grid domain (if possible)
+            ! to avoid dimension of one and so be able to compute offset
+            CALL dom_add_extra(tl_dom1, id_rho(jp_I), id_rho(jp_J))
+
+            ! open mpp files over domain
+            CALL iom_dom_open(tl_bathy1, tl_dom1)
+
+            ! read variable value on domain
+            tl_lon1=iom_dom_read_var(tl_bathy1,'longitude',tl_dom1)
+            tl_lat1=iom_dom_read_var(tl_bathy1,'latitude' ,tl_dom1)
+
+            ! close mpp files
+            CALL iom_dom_close(tl_bathy1)
+
+            ! clean structure
+            CALL mpp_clean(tl_bathy1)
+
+            ! get coarse grid indices
+            il_ind(:,:)=grid_get_coarse_index(td_bathy0, tl_lon1, tl_lat1, &
+            &                                 id_rho=id_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)
+
+            ! read coarse grid bathymetry on domain
+            tl_bathy0=mpp_copy(td_bathy0)
+
+            ! compute domain
             tl_dom0=dom_init( tl_bathy0,         &
             &                 il_imin0, il_imax0,&
             &                 il_jmin0, il_jmax0 )
 
-            !4-2 close file
-            CALL iom_close(tl_bathy0)
-
-            !4-3 add extra band (if possible) to compute interpolation
+            il_offset(:,:)= grid_get_fine_offset(tl_bathy0,         &
+            &                                    il_imin0, il_jmin0,&
+            &                                    il_imax0, il_jmax0,&
+            &                                    tl_lon1%d_value(:,:,1,1), &
+            &                                    tl_lat1%d_value(:,:,1,1), &
+            &                                    id_rho=id_rho(:))
+
+            ! clean
+            CALL var_clean(tl_lon1)
+            CALL var_clean(tl_lat1)
+
+            ! add extra band (if possible) to compute interpolation
             CALL dom_add_extra(tl_dom0)
 
-            !4-4 read variables on domain (ugly way to do it, have to work on it)
-            !4-4-1 init mpp structure
-            tl_mppbathy0=mpp_init(tl_bathy0)
-            
-            CALL file_clean(tl_bathy0)
-
-            !4-4-2 get processor to be used
-            CALL mpp_get_use( tl_mppbathy0, tl_dom0 )
-
-            !4-4-3 open mpp files
-            CALL iom_mpp_open(tl_mppbathy0)
-
-            !4-4-4 read variable value on domain
-            tl_var0=iom_mpp_read_var(tl_mppbathy0,'Bathymetry',td_dom=tl_dom0)
-
-            !4-4-5 close mpp files
-            CALL iom_mpp_close(tl_mppbathy0)
-
-            !4-4-6 clean structure
-            CALL mpp_clean(tl_mppbathy0)
-
-            !5- interpolate variable
+            ! open mpp files over domain
+            CALL iom_dom_open(tl_bathy0, tl_dom0)
+
+            ! read variable value on domain
+            tl_var0=iom_dom_read_var(tl_bathy0,'Bathymetry',tl_dom0)
+
+            ! close mpp files
+            CALL iom_dom_close(tl_bathy0)
+
+            ! clean structure
+            CALL mpp_clean(tl_bathy0)
+
+            ! interpolate variable
             CALL merge_bathy_interp( tl_var0,         &
             &                        id_rho(:),       &
             &                        il_offset(:,:) )
 
-            !6- remove extraband added to domain
+            ! remove extraband added to domain
             CALL dom_del_extra( tl_var0, tl_dom0, id_rho(:) )
 
-            !6-1 remove extraband added to domain
+            ! remove extraband added to domain
             CALL dom_clean_extra( tl_dom0 )
 
-            !7- fill refined table  
-            !7-1 keep only useful point
-            ! interpolation could create more point than necessary
-            CALL boundary_clean_interp(tl_var0, td_bdy )
-
-            ! use add request ????
-
-            !7-2 fill refined table
+            ! remove extraband added to fine grid domain
+            CALL dom_del_extra( tl_var0, tl_dom1 )
+
+            ! remove extraband added to fine grid domain
+            CALL dom_clean_extra( tl_dom1 )
+
+            ! fill refined array
             dd_refined( il_imin1:il_imax1, &
             &           il_jmin1:il_jmax1, &
             &           :,: )= tl_var0%d_value(:,:,:,:)
 
-            !8- compute weight function
+            ! clean
+            CALL var_clean(tl_var0)
+
+            ! compute weight function
             ALLOCATE( dl_tmp1d(td_bdy%t_seg(jl)%i_width) )
 
@@ -678,5 +818,5 @@
 
                ! compute weight on segment
-               dl_tmp1d(:)= 0.5 + 0.5*COS( (dg_pi*dl_tmp1d(:)) / &
+               dl_tmp1d(:)= 0.5 + 0.5*COS( (dp_pi*dl_tmp1d(:)) / &
                &                           (td_bdy%t_seg(jl)%i_width) )
 
@@ -694,5 +834,5 @@
 
                ! compute weight on segment
-               dl_tmp1d(:)= 0.5 + 0.5*COS( (dg_pi*dl_tmp1d(:)) / &
+               dl_tmp1d(:)= 0.5 + 0.5*COS( (dp_pi*dl_tmp1d(:)) / &
                &                           (td_bdy%t_seg(jl)%i_width) )
 
@@ -710,5 +850,5 @@
 
                ! compute weight on segment
-               dl_tmp1d(:)= 0.5 + 0.5*COS( (dg_pi*dl_tmp1d(:)) / &
+               dl_tmp1d(:)= 0.5 + 0.5*COS( (dp_pi*dl_tmp1d(:)) / &
                &                           (td_bdy%t_seg(jl)%i_width) )
 
@@ -726,5 +866,5 @@
 
                ! compute weight on segment
-               dl_tmp1d(:)= 0.5 + 0.5*COS( (dg_pi*dl_tmp1d(:)) / &
+               dl_tmp1d(:)= 0.5 + 0.5*COS( (dp_pi*dl_tmp1d(:)) / &
                &                           (td_bdy%t_seg(jl)%i_width) )
 
@@ -740,5 +880,5 @@
             DEALLOCATE( dl_tmp1d )
 
-            !8-1 fill weight table
+            ! fill weight array
             ALLOCATE( dl_tmp2d( tl_dom1%t_dim(1)%i_len, &
             &                   tl_dom1%t_dim(2)%i_len) )
@@ -764,18 +904,17 @@
       ENDIF
    END SUBROUTINE merge_bathy_get_boundary
-   !> @endcode
    !-------------------------------------------------------------------
    !> @brief
-   !> This subroutine
+   !> This subroutine interpolate variable.
    !> 
-   !> @details 
+   !> @author J.Paul
+   !> @date November, 2013 - Initial Version
    !>
-   !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !>
-   !> @param[in] 
-   !> @todo 
+   !> @param[inout] td_var 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
    SUBROUTINE merge_bathy_interp( td_var,          &
    &                              id_rho,          &
@@ -793,5 +932,4 @@
 
       ! local variable
-      TYPE(TVAR)  :: tl_var
       TYPE(TVAR)  :: tl_mask
 
@@ -803,7 +941,4 @@
       ! loop indices
       !----------------------------------------------------------------
-
-      ! copy variable
-      tl_var=td_var
 
       !WARNING: two extrabands are required for cubic interpolation
@@ -815,5 +950,5 @@
 
       IF( il_iext < 2 .AND. td_var%c_interp(1) == 'cubic' )THEN
-         CALL logger_warn("CREATE BATHY INTERP: at least extrapolation "//&
+         CALL logger_warn("MERGE BATHY INTERP: at least extrapolation "//&
          &  "on two points are required with cubic interpolation ")
          il_iext=2
@@ -821,64 +956,55 @@
 
       IF( il_jext < 2 .AND. td_var%c_interp(1) == 'cubic' )THEN
-         CALL logger_warn("CREATE BATHY INTERP: at least extrapolation "//&
+         CALL logger_warn("MERGE BATHY INTERP: at least extrapolation "//&
          &  "on two points are required with cubic interpolation ")
          il_jext=2
       ENDIF
 
-      !1- work on mask
-      !1-1 create mask
-      ALLOCATE(bl_mask(tl_var%t_dim(1)%i_len, &
-      &                tl_var%t_dim(2)%i_len, &
-      &                tl_var%t_dim(3)%i_len, &
-      &                tl_var%t_dim(4)%i_len) )
+      ! work on mask
+      ! create mask
+      ALLOCATE(bl_mask(td_var%t_dim(1)%i_len, &
+      &                td_var%t_dim(2)%i_len, &
+      &                td_var%t_dim(3)%i_len, &
+      &                td_var%t_dim(4)%i_len) )
 
       bl_mask(:,:,:,:)=1
-      WHERE(tl_var%d_value(:,:,:,:)==tl_var%d_fill) bl_mask(:,:,:,:)=0      
-
-      SELECT CASE(TRIM(tl_var%c_point))
+      WHERE(td_var%d_value(:,:,:,:)==td_var%d_fill) bl_mask(:,:,:,:)=0      
+
+      SELECT CASE(TRIM(td_var%c_point))
       CASE DEFAULT ! 'T'
-         tl_mask=var_init('tmask', bl_mask(:,:,:,:), td_dim=tl_var%t_dim(:))
-      CASE('U')
-         tl_mask=var_init('umask', bl_mask(:,:,:,:), td_dim=tl_var%t_dim(:))
-      CASE('V')
-         tl_mask=var_init('vmask', bl_mask(:,:,:,:), td_dim=tl_var%t_dim(:))
-      CASE('F')
-         tl_mask=var_init('fmask', bl_mask(:,:,:,:), td_dim=tl_var%t_dim(:))
+         tl_mask=var_init('tmask',bl_mask(:,:,:,:),td_dim=td_var%t_dim(:),&
+         &                id_ew=td_var%i_ew )
+      CASE('U','V','F')
+         CALL logger_fatal("MERGE BATHY INTERP: can not computed "//&
+         &                 "interpolation on "//TRIM(td_var%c_point)//&
+         &                 " grid point (variable "//TRIM(td_var%c_name)//&
+         &                 "). check namelist.")
       END SELECT
 
       DEALLOCATE(bl_mask)
 
-      !1-2 interpolate mask
+      ! interpolate mask
       CALL interp_fill_value( tl_mask, id_rho(:),  &
       &                       id_offset=id_offset(:,:) )
 
-      !2- work on variable
-      !2-0 add extraband
-      CALL extrap_add_extrabands(tl_var, il_iext, il_iext)
-
-      !2-1 extrapolate variable
-      CALL extrap_fill_value( tl_var, id_offset=id_offset(:,:), &
-      &                               id_rho=id_rho(:),         &
-      &                               id_iext=il_iext, id_jext=il_jext )
-
-      !2-2 interpolate Bathymetry
-      CALL interp_fill_value( tl_var, id_rho(:), &
+      ! work on variable
+      ! add extraband
+      CALL extrap_add_extrabands(td_var, il_iext, il_iext)
+
+      ! extrapolate variable
+      CALL extrap_fill_value( td_var )
+
+      ! interpolate Bathymetry
+      CALL interp_fill_value( td_var, id_rho(:), &
       &                       id_offset=id_offset(:,:) )
 
-      !2-3 remove extraband
-      CALL extrap_del_extrabands(tl_var, il_iext*id_rho(jp_I), il_jext*id_rho(jp_J))
-
-      !2-4 keep original mask 
+      ! remove extraband
+      CALL extrap_del_extrabands(td_var, il_iext*id_rho(jp_I), il_jext*id_rho(jp_J))
+
+      ! keep original mask 
       WHERE( tl_mask%d_value(:,:,:,:) == 0 )
-         tl_var%d_value(:,:,:,:)=tl_var%d_fill
+         td_var%d_value(:,:,:,:)=td_var%d_fill
       END WHERE
 
-      !3- save result
-      td_var=tl_var
-
-      ! clean variable structure
-      CALL var_clean(tl_var)
-
    END SUBROUTINE merge_bathy_interp
-   !> @endcode
 END PROGRAM merge_bathy
