Index: branches/2014/dev_r4650_UKMO14.12_STAND_ALONE_OBSOPER/NEMOGCM/TOOLS/SIREN/src/iom_rstdimg.f90
===================================================================
--- branches/2014/dev_r4650_UKMO14.12_STAND_ALONE_OBSOPER/NEMOGCM/TOOLS/SIREN/src/iom_rstdimg.f90	(revision 4810)
+++ branches/2014/dev_r4650_UKMO14.12_STAND_ALONE_OBSOPER/NEMOGCM/TOOLS/SIREN/src/iom_rstdimg.f90	(revision 5600)
@@ -10,66 +10,73 @@
 !>
 !> @details
-!>
 !>    to open dimg file (create file structure):<br/>
+!> @code
 !>    CALL iom_rstdimg_open(td_file)
+!> @endcode
 !>       - td_file is file structure (see file.f90)
 !>
 !>    to write in dimg file:<br/>
+!> @code
 !>    CALL  iom_rstdimg_write_file(td_file)
+!> @endcode
 !>
 !>    to close dimg file:<br/>
+!> @code
 !>    CALL iom_rstdimg_close(tl_file)
+!> @endcode
 !>
 !>    to read one dimension in dimg file:<br/>
-!>    tl_dim = iom_rstdimg_read_dim(tl_file, id_dimid)<br/>
-!>    or<br/>
+!> @code
+!>    tl_dim = iom_rstdimg_read_dim(tl_file, id_dimid)
+!> @endcode
+!>    or
+!> @code
 !>    tl_dim = iom_rstdimg_read_dim(tl_file, cd_name)
+!> @endcode
 !>       - id_dimid is dimension id<br/>
 !>       - cd_name is dimension name
 !>
-!>    to read one global attribute in dimg file:<br/>
-!>    tl_att = iom_rstdimg_read_att(tl_file, id_varid, id_attid)<br/>
-!>    or<br/>
-!>    tl_att = iom_rstdimg_read_att(tl_file, id_varid, cd_name)
-!>       - id_varid is variable id
-!>       - id_attid is attribute id<br/>
-!>       - cd_name is attribute name
-!>  
 !>    to read one variable in dimg file:<br/>
-!>    tl_var = iom_rstdimg_read_var(td_file, id_varid, [id_start, id_count])<br/>
-!>    or<br/>
-!>    tl_var = iom_rstdimg_read_var(td_file, cd_name, [id_start, [id_count,]] [cd_stdname])
+!> @code
+!>    tl_var = iom_rstdimg_read_var(td_file, id_varid, [id_start, id_count])
+!> @endcode
+!>    or
+!> @code
+!>    tl_var = iom_rstdimg_read_var(td_file, cd_name, [id_start, [id_count]])
+!> @endcode
 !>       - id_varid is variabale id
-!>       - cd_name is variabale name
-!>       - id_start is a integer(4) 1D table of index from which the data 
-!>          values will be read (optional)
-!>       - id_count is a integer(4) 1D table of the number of indices selected
-!>          along each dimension (optional)
-!>       - cd_stdname is variable standard name (optional)
+!>       - cd_name is variabale name or standard name
+!>       - id_start is a integer(4) 1D array of index from which the data 
+!>          values will be read [optional]
+!>       - id_count is a integer(4) 1D array of the number of indices selected
+!>          along each dimension [optional]
+!>
+!>    to get sub domain decomppistion in a dimg file:<br/>
+!> @code
+!>    CALL iom_rstdimg_get_mpp(td_file)
+!> @endcode
 !>
 !> @author
 !> J.Paul
 ! REVISION HISTORY:
-!> @date Nov, 2013 - Initial Version
-!
-!> @param MyModule_type : brief_description
+!> @date November, 2013 - Initial Version
 !
 !> @note Software governed by the CeCILL licence     (NEMOGCM/NEMO_CeCILL.txt)
-!
-!> @todo
 !----------------------------------------------------------------------
 MODULE iom_rstdimg
    USE netcdf                          ! nf90 library
+   USE global                          ! global parameter
    USE kind                            ! F90 kind parameter
    USE fct                             ! basic useful function
-   USE logger                             ! log file manager
+   USE logger                          ! log file manager
    USE att                             ! attribute manager
    USE dim                             ! dimension manager
    USE var                             ! variable manager
    USE file                            ! file manager
-   USE dom                             ! domain manager
    IMPLICIT NONE
-   PRIVATE
    ! NOTE_avoid_public_variables_if_possible
+
+   ! type and variable
+   PRIVATE ::  im_vnl !< variable name length 
 
    ! function and subroutine
@@ -78,27 +85,24 @@
    PUBLIC :: iom_rstdimg_read_dim    !< read one dimension in an opened dimg file, return variable structure
    PUBLIC :: iom_rstdimg_read_var    !< read one variable  in an opened dimg file, return dimension structure
-   PUBLIC :: iom_rstdimg_fill_var    !< fill variable value in an opened dimg file
    PUBLIC :: iom_rstdimg_write_file  !< write file structure contents in an opened dimg file
    PUBLIC :: iom_rstdimg_get_mpp     !< get sub domain decomppistion in a dimg file
 
-   PRIVATE :: iom_rstdimg__get_info       !< get global information in an opened dimg file
-   PRIVATE :: iom_rstdimg__get_file_var   !< read information about variable on an opened dimg file.
-   PRIVATE :: iom_rstdimg__get_file_var_0d !< put information about scalar variable in file structure
-   PRIVATE :: iom_rstdimg__get_file_var_1d !< put information about variable 1D in file structure
-   PRIVATE :: iom_rstdimg__get_file_var_2d !< put information about variable 2D in file structure
-   PRIVATE :: iom_rstdimg__get_file_var_3d !< put information about variable 3D in file structure
-   PRIVATE :: iom_rstdimg__read_dim_id    !< read dimension structure in an opened dimg file, given variable id.
-   PRIVATE :: iom_rstdimg__read_dim_name  !< read dimension structure in an opened dimg file, given variable name or standard name.
-   PRIVATE :: iom_rstdimg__read_var_id    !< read variable value in an opened dimg file, given variable id.
-   PRIVATE :: iom_rstdimg__read_var_name  !< read variable value in an opened dimg file, given variable name or standard name.
-   PRIVATE :: iom_rstdimg__read_var_value !< read variable value in an opened dimg file, for variable 1,2,3d
-   PRIVATE :: iom_rstdimg__write_header   !< write header in an opened dimg file
-   PRIVATE :: iom_rstdimg__write_var      !< write variables in an opened dimg file 
-   PRIVATE :: iom_rstdimg__fill_var_id    !< fill variable value in an opened dimg file, given variable id
-   PRIVATE :: iom_rstdimg__fill_var_name  !< fill variable value in an opened dimg file, given variable name
-   PRIVATE :: iom_rstdimg__fill_var_all   !< fill all variable value in an opened dimg file
+   PRIVATE :: iom_rstdimg__get_info        ! get global information in an opened dimg file
+   PRIVATE :: iom_rstdimg__get_file_var    ! read information about variable on an opened dimg file.
+   PRIVATE :: iom_rstdimg__get_file_var_0d ! put information about scalar variable in file structure
+   PRIVATE :: iom_rstdimg__get_file_var_1d ! put information about variable 1D in file structure
+   PRIVATE :: iom_rstdimg__get_file_var_2d ! put information about variable 2D in file structure
+   PRIVATE :: iom_rstdimg__get_file_var_3d ! put information about variable 3D in file structure
+   PRIVATE :: iom_rstdimg__read_dim_id     ! read dimension structure in an opened dimg file, given variable id.
+   PRIVATE :: iom_rstdimg__read_dim_name   ! read dimension structure in an opened dimg file, given variable name or standard name.
+   PRIVATE :: iom_rstdimg__read_var_id     ! read variable value in an opened dimg file, given variable id.
+   PRIVATE :: iom_rstdimg__read_var_name   ! read variable value in an opened dimg file, given variable name or standard name.
+   PRIVATE :: iom_rstdimg__read_var_value  ! read variable value in an opened dimg file, for variable 1,2,3d
+   PRIVATE :: iom_rstdimg__get_rec         ! compute record number before writing file
+   PRIVATE :: iom_rstdimg__write_header    ! write header in an opened dimg file
+   PRIVATE :: iom_rstdimg__write_var       ! write variables in an opened dimg file 
 
    ! module variable
-   INTEGER(i4), PARAMETER :: ip_vnl = 32 ! variable name length
+   INTEGER(i4), PARAMETER :: im_vnl = 32 ! variable name length
 
    INTERFACE iom_rstdimg_read_dim
@@ -112,13 +116,8 @@
    END INTERFACE iom_rstdimg_read_var
 
-   INTERFACE iom_rstdimg_fill_var
-      MODULE PROCEDURE iom_rstdimg__fill_var_id
-      MODULE PROCEDURE iom_rstdimg__fill_var_name
-      MODULE PROCEDURE iom_rstdimg__fill_var_all
-   END INTERFACE iom_rstdimg_fill_var   
-
 CONTAINS
    !-------------------------------------------------------------------
-   !> @brief This subroutine open a dimg file in read or write mode<br/>
+   !> @brief This subroutine open a dimg file in read or write mode.
+   !> @details
    !> if try to open a file in write mode that did not exist, create it.<br/>
    !> if file already exist, get information about:
@@ -128,12 +127,12 @@
    !> - the ID of the unlimited dimension
    !> - the file format
-   !> and finally read dimensions.
+   !> Finally it read dimensions, and 'longitude' variable to compute East-West
+   !> overlap.
    !>
    !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[inout] td_file : file structure
-   !-------------------------------------------------------------------
-   !> @code
+   !> - November, 2013- Initial Version
+   !
+   !> @param[inout] td_file   file structure
+   !-------------------------------------------------------------------
    SUBROUTINE iom_rstdimg_open(td_file)
       IMPLICIT NONE
@@ -146,6 +145,4 @@
 
       INTEGER(i4)       :: il_status
-      
-      TYPE(TVAR)        :: tl_lon
       !----------------------------------------------------------------
 
@@ -180,5 +177,4 @@
             ENDIF
 
-
          ENDIF
 
@@ -224,5 +220,4 @@
             ENDIF
 
-
             IF( .NOT. td_file%l_wrt )THEN
 
@@ -240,5 +235,8 @@
                CALL fct_err(il_status)
                IF( il_status /= 0 )THEN
-                  CALL logger_error("OPEN: file "//TRIM(td_file%c_name)&
+                  CALL logger_debug("IOM RSTDIMG OPEN: open staus "//&
+                  &  TRIM(fct_str(il_status)))
+                  CALL logger_fatal("IOM RSTDIMG OPEN: file "//&
+                  &  TRIM(td_file%c_name)&
                   &  //" with record length "//TRIM(fct_str(td_file%i_recl)))
                ENDIF               
@@ -260,5 +258,8 @@
                CALL fct_err(il_status)
                IF( il_status /= 0 )THEN
-                  CALL logger_error("OPEN: file "//TRIM(td_file%c_name))
+                  CALL logger_debug("IOM RSTDIMG OPEN: open staus "//&
+                  &  TRIM(fct_str(il_status)))
+                  CALL logger_error("IOM RSTDIMG  OPEN: file "//&
+                  & TRIM(td_file%c_name))
                ENDIF        
 
@@ -274,12 +275,4 @@
             CALL iom_rstdimg__get_file_var(td_file)
 
-            ! get ew overlap
-            tl_lon=iom_rstdimg_read_var(td_file,'longitude')
-            td_file%i_ew=dom_get_ew_overlap(tl_lon)
-            WHERE( td_file%t_var(:)%t_dim(1)%l_use )
-               td_file%t_var(:)%i_ew=td_file%i_ew
-            ENDWHERE
-            CALL var_clean(tl_lon) 
-
          ENDIF
 
@@ -287,14 +280,12 @@
 
    END SUBROUTINE iom_rstdimg_open
-   !> @endcode
-   !-------------------------------------------------------------------
-   !> @brief This subroutine close dimg file
+   !-------------------------------------------------------------------
+   !> @brief This subroutine close dimg file.
    !>
    !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[in] td_file : file structure
-   !-------------------------------------------------------------------
-   !> @code
+   !> - November, 2013- Initial Version
+   !
+   !> @param[inout] td_file   file structure
+   !-------------------------------------------------------------------
    SUBROUTINE iom_rstdimg_close(td_file)
       IMPLICIT NONE
@@ -327,21 +318,18 @@
 
    END SUBROUTINE iom_rstdimg_close
-   !> @endcode
    !-------------------------------------------------------------------
    !> @brief This subroutine get global information in an opened dimg 
-   !> file.<br/>
+   !> file.
    !> @details
    !> It gets the number of variables, the  domain decompistion, 
-   !> the record of the header infos.<br/>
+   !> the record of the header.<br/>
    !> It read dimensions, and add it to dimension structure inside 
    !> file structure.
    !>
    !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[inout] td_file : file structure
-   !> @return file structure completed 
-   !-------------------------------------------------------------------
-   !> @code
+   !> - November, 2013- Initial Version
+   !
+   !> @param[inout] td_file   file structure
+   !-------------------------------------------------------------------
    SUBROUTINE iom_rstdimg__get_info(td_file)
       IMPLICIT NONE
@@ -360,5 +348,5 @@
 
       CALL logger_debug( &
-      &  " GET INFO: about dimg file "//TRIM(td_file%c_name))
+      &  " IOM RSTDIMG GET INFO: about dimg file "//TRIM(td_file%c_name))
 
       ! read first record 
@@ -370,9 +358,8 @@
       CALL fct_err(il_status)
       IF( il_status /= 0 )THEN
-         CALL logger_error("GET INFO: read first line of "//TRIM(td_file%c_name))
-      ENDIF
-
-      CALL logger_trace( &
-      &  " GET INFO: about dimg file "//TRIM(td_file%c_name))
+         CALL logger_debug(" READ status: "//TRIM(fct_str(il_status)))
+         CALL logger_fatal("IOM RSTDIMG GET INFO: read first line of "//&
+         &  TRIM(td_file%c_name))
+      ENDIF
 
       td_file%c_type='dimg'
@@ -380,9 +367,9 @@
       ! add dimension to file structure
       tl_dim=dim_init('X', il_nx)
-      CALL file_add_dim(td_file, tl_dim)
+      CALL file_move_dim(td_file, tl_dim)
       tl_dim=dim_init('Y', il_ny)
-      CALL file_add_dim(td_file, tl_dim)
+      CALL file_move_dim(td_file, tl_dim)
       tl_dim=dim_init('Z', il_nz)
-      CALL file_add_dim(td_file, tl_dim)
+      CALL file_move_dim(td_file, tl_dim)
 
       ! reorder dimension to ('x','y','z','t')
@@ -401,17 +388,14 @@
 
    END SUBROUTINE iom_rstdimg__get_info
-   !> @endcode
-   !-------------------------------------------------------------------
-   !> @brief This subroutine get sub domain decomposition in a dimg file.<br/>
+   !-------------------------------------------------------------------
+   !> @brief This subroutine get sub domain decomposition in a dimg file.
    !> @details
    !> domain decomposition informations are saved in attributes.
    !>
    !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[inout] td_file : file structure
-   !> @return mpp structure 
-   !-------------------------------------------------------------------
-   !> @code
+   !> - November, 2013- Initial Version
+   !
+   !> @param[inout] td_file   file structure
+   !-------------------------------------------------------------------
    SUBROUTINE iom_rstdimg_get_mpp(td_file)
       IMPLICIT NONE
@@ -440,5 +424,6 @@
       !----------------------------------------------------------------
 
-      CALL logger_trace( " GET MPP: dimg file "//TRIM(td_file%c_name))
+      CALL logger_debug( " IOM RSTDIMG GET MPP: dimg file "//&
+      &  TRIM(td_file%c_name))
 
       ! read first record 
@@ -453,22 +438,25 @@
       CALL fct_err(il_status)
       IF( il_status /= 0 )THEN
-         CALL logger_error("GET MPP: read first line of "//TRIM(td_file%c_name))
+         CALL logger_debug(" IOM RSTDIMG GET MPP: read status: "//&
+         &  TRIM(fct_str(il_status)))
+         CALL logger_error(" IOM RSTDIMG GET MPP: read first line of "//&
+         &  TRIM(td_file%c_name))
       ENDIF
 
       ! create attributes to save mpp value
       tl_att=att_init( "DOMAIN_number_total", il_nproc)
-      CALL file_add_att(td_file, tl_att)
+      CALL file_move_att(td_file, tl_att)
 
       tl_att=att_init( "DOMAIN_I_number_total", il_niproc)
-      CALL file_add_att(td_file, tl_att)
+      CALL file_move_att(td_file, tl_att)
 
       tl_att=att_init( "DOMAIN_J_number_total", il_njproc)
-      CALL file_add_att(td_file, tl_att)
+      CALL file_move_att(td_file, tl_att)
 
       tl_att=att_init( "DOMAIN_number", il_area)
-      CALL file_add_att(td_file, tl_att)
+      CALL file_move_att(td_file, tl_att)
 
       tl_att=att_init( "DOMAIN_size_global", (/il_iglo, il_jglo/))
-      CALL file_add_att(td_file, tl_att)
+      CALL file_move_att(td_file, tl_att)
 
       ! allocate local variable
@@ -480,6 +468,6 @@
       IF(il_status /= 0 )THEN
 
-         CALL logger_error( " GET MPP: not enough space to put domain &
-         &  decomposition in file "//TRIM(td_file%c_name) )
+         CALL logger_error( " IOM RSTDIMG GET MPP: not enough space to put "//&
+         &  "domain decomposition in file "//TRIM(td_file%c_name) )
 
       ENDIF
@@ -500,39 +488,44 @@
       CALL fct_err(il_status)
       IF( il_status /= 0 )THEN
-         CALL logger_error("GET INFO: read domain decomposition on first &
-         &              line of "//TRIM(td_file%c_name))
+         CALL logger_debug(" IOM RSTDIMG GET MPP: read status: "//&
+         &  TRIM(fct_str(il_status)))
+         CALL logger_fatal("IOM RSTDIMG GET MPP: read domain decomposition "//&
+         &           "on first line of "//TRIM(td_file%c_name))
       ENDIF
 
       tl_att=att_init( "DOMAIN_position_first", (/il_impp(il_area), il_jmpp(il_area)/))
-      CALL file_add_att(td_file, tl_att)
+      CALL file_move_att(td_file, tl_att)
 
       tl_att=att_init( "DOMAIN_position_last", (/il_lci(il_area), il_lcj(il_area)/))
-      CALL file_add_att(td_file, tl_att)
+      CALL file_move_att(td_file, tl_att)
 
       tl_att=att_init( "DOMAIN_halo_size_start", (/il_ldi(il_area), il_ldj(il_area)/))
-      CALL file_add_att(td_file, tl_att)
+      CALL file_move_att(td_file, tl_att)
 
       tl_att=att_init( "DOMAIN_halo_size_end", (/il_lei(il_area), il_lej(il_area)/))
-      CALL file_add_att(td_file, tl_att)
+      CALL file_move_att(td_file, tl_att)
 
       tl_att=att_init( "DOMAIN_I_position_first", il_impp(:) )
-      CALL file_add_att(td_file, tl_att)
+      CALL file_move_att(td_file, tl_att)
       tl_att=att_init( "DOMAIN_J_position_first", il_jmpp(:) )
-      CALL file_add_att(td_file, tl_att)
+      CALL file_move_att(td_file, tl_att)
 
       tl_att=att_init( "DOMAIN_I_position_last", il_lci(:) )
-      CALL file_add_att(td_file, tl_att)
+      CALL file_move_att(td_file, tl_att)
       tl_att=att_init( "DOMAIN_J_position_last", il_lcj(:) )
-      CALL file_add_att(td_file, tl_att)
+      CALL file_move_att(td_file, tl_att)
 
       tl_att=att_init( "DOMAIN_I_halo_size_start", il_ldi(:) )
-      CALL file_add_att(td_file, tl_att)
+      CALL file_move_att(td_file, tl_att)
       tl_att=att_init( "DOMAIN_J_halo_size_start", il_ldj(:) )
-      CALL file_add_att(td_file, tl_att)
+      CALL file_move_att(td_file, tl_att)
 
       tl_att=att_init( "DOMAIN_I_halo_size_end", il_lei(:) )
-      CALL file_add_att(td_file, tl_att)
+      CALL file_move_att(td_file, tl_att)
       tl_att=att_init( "DOMAIN_J_halo_size_end", il_lej(:) )
-      CALL file_add_att(td_file, tl_att)
+      CALL file_move_att(td_file, tl_att)
+
+      ! clean
+      CALL att_clean(tl_att)
 
       DEALLOCATE( il_impp, il_jmpp,&
@@ -542,18 +535,17 @@
 
    END SUBROUTINE iom_rstdimg_get_mpp
-   !> @endcode
    !-------------------------------------------------------------------
    !> @brief This subroutine read information about variable on an 
-   !> opened dimg file.<br/>
-   !> The variable structure inside file structure is then completed.
+   !> opened dimg file.
+   !> @details
+   !> The variables structures inside file structure are then completed.
+   !> Variables no0d, no1d, no2d, no3d are deleted from file strucutre.
    !> @note variable value are read only for scalar variable (0d).
    !
    !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[inout] td_file : file structure
-   !> @return file structure completed 
-   !-------------------------------------------------------------------
-   !> @code
+   !> - November, 2013- Initial Version
+   !
+   !> @param[inout] td_file   file structure
+   !-------------------------------------------------------------------
    SUBROUTINE iom_rstdimg__get_file_var(td_file)
       IMPLICIT NONE
@@ -562,5 +554,5 @@
 
       ! local variable
-      CHARACTER(LEN=ip_vnl), DIMENSION(:), ALLOCATABLE :: cl_name
+      CHARACTER(LEN=im_vnl), DIMENSION(:), ALLOCATABLE :: cl_name
 
       REAL(dp)             , DIMENSION(:), ALLOCATABLE :: dl_value
@@ -605,4 +597,5 @@
 
          IF(ASSOCIATED(td_file%t_var))THEN
+            CALL var_clean(td_file%t_var(:))
             DEALLOCATE(td_file%t_var)
          ENDIF
@@ -638,23 +631,20 @@
 
    END SUBROUTINE iom_rstdimg__get_file_var
-   !> @endcode
-   !-------------------------------------------------------------------
-   !> @brief This subroutine put information about scalar variable 
+   !-------------------------------------------------------------------
+   !> @brief This subroutine put informations about scalar variable 
    !> inside file structure.
    !
    !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[inout] td_file : file structure
-   !> @param[in] cd_name : table of variable name
-   !> @param[in] dd_value : table of variable value
-   !> @return file structure completed 
-   !-------------------------------------------------------------------
-   !> @code
+   !> - November, 2013- Initial Version
+   !
+   !> @param[inout] td_file   file structure
+   !> @param[in] cd_name      array of variable name
+   !> @param[in] dd_value     array of variable value
+   !-------------------------------------------------------------------
    SUBROUTINE iom_rstdimg__get_file_var_0d(td_file, cd_name, dd_value)
       IMPLICIT NONE
       ! Argument      
       TYPE(TFILE),                         INTENT(INOUT) :: td_file
-      CHARACTER(LEN=ip_vnl), DIMENSION(:), INTENT(IN)    :: cd_name
+      CHARACTER(LEN=im_vnl), DIMENSION(:), INTENT(IN)    :: cd_name
       REAL(dp),              DIMENSION(:), INTENT(IN)    :: dd_value
 
@@ -667,5 +657,5 @@
 
       ! define same dimension as in file
-      tl_dim(:)=td_file%t_dim(:)
+      tl_dim(:)=dim_copy(td_file%t_dim(:))
       ! do not use any dimension
       tl_dim(:)%l_use=.FALSE.
@@ -676,5 +666,6 @@
    
          td_file%t_var(ji)=var_init( TRIM(cd_name(ji)), NF90_DOUBLE, &
-         &                           tl_dim(:), id_id=ji, id_rec=1 )
+         &                           tl_dim(:), dd_fill=0._dp,       &
+         &                           id_id=ji, id_rec=1 )
 
          ! get value of scalar
@@ -688,24 +679,24 @@
       ENDDO
 
+      ! clean
+      CALL dim_clean(tl_dim(:))
+
    END SUBROUTINE iom_rstdimg__get_file_var_0d
-   !> @endcode   
-   !-------------------------------------------------------------------
-   !> @brief This subroutine put information about variable 1D 
+   !-------------------------------------------------------------------
+   !> @brief This subroutine put informations about variable 1D 
    !> inside file structure.
    !
    !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[inout] td_file : file structure
-   !> @param[in] cd_name : table of variable name
-   !> @param[in] dd_value : table of variable record
-   !> @return file structure completed 
-   !-------------------------------------------------------------------
-   !> @code
+   !> - November, 2013- Initial Version
+   !
+   !> @param[inout] td_file   file structure
+   !> @param[in] cd_name      array of variable name
+   !> @param[in] dd_value     array of variable record
+   !-------------------------------------------------------------------
    SUBROUTINE iom_rstdimg__get_file_var_1d(td_file, cd_name, dd_value)
       IMPLICIT NONE
       ! Argument      
       TYPE(TFILE),                         INTENT(INOUT) :: td_file
-      CHARACTER(LEN=ip_vnl), DIMENSION(:), INTENT(IN)    :: cd_name
+      CHARACTER(LEN=im_vnl), DIMENSION(:), INTENT(IN)    :: cd_name
       REAL(dp),              DIMENSION(:), INTENT(IN)    :: dd_value
 
@@ -722,5 +713,5 @@
 
          ! define same dimension as in file
-         tl_dim(:)=td_file%t_dim(:)
+         tl_dim(:)=dim_copy(td_file%t_dim(:))
          ! do not use X and Y dimension
          td_file%t_var(ji)%t_dim(1:2)%l_use=.FALSE.
@@ -728,29 +719,29 @@
       
          td_file%t_var(ji)=var_init( TRIM(cd_name(ji)), NF90_DOUBLE, &
-         &                           tl_dim(:), id_id=ji,            &
-         &                           id_rec=INT(dd_value(ji),i4) )
+         &                           tl_dim(:), dd_fill=0._dp,       &
+         &                           id_id=ji, id_rec=INT(dd_value(ji),i4) )
+
+         ! clean
+         CALL dim_clean(tl_dim(:))
 
       ENDDO
 
    END SUBROUTINE iom_rstdimg__get_file_var_1d
-   !> @endcode
-   !-------------------------------------------------------------------
-   !> @brief This subroutine put information about variable 2D 
+   !-------------------------------------------------------------------
+   !> @brief This subroutine put informations about variable 2D 
    !> inside file structure.
    !
    !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[inout] td_file : file structure
-   !> @param[in] cd_name : table of variable name
-   !> @param[in] dd_value : table of variable record
-   !> @return file structure completed 
-   !-------------------------------------------------------------------
-   !> @code
+   !> - November, 2013- Initial Version
+   !
+   !> @param[inout] td_file   file structure
+   !> @param[in] cd_name      array of variable name
+   !> @param[in] dd_value     array of variable record
+   !-------------------------------------------------------------------
    SUBROUTINE iom_rstdimg__get_file_var_2d(td_file, cd_name, dd_value)
       IMPLICIT NONE
       ! Argument      
       TYPE(TFILE),                         INTENT(INOUT) :: td_file
-      CHARACTER(LEN=ip_vnl), DIMENSION(:), INTENT(IN)    :: cd_name
+      CHARACTER(LEN=im_vnl), DIMENSION(:), INTENT(IN)    :: cd_name
       REAL(dp),              DIMENSION(:), INTENT(IN)    :: dd_value
 
@@ -767,5 +758,5 @@
 
          ! define same dimension as in file
-         tl_dim(:)=td_file%t_dim(:)
+         tl_dim(:)=dim_copy(td_file%t_dim(:))
          ! do not use Z dimension
          tl_dim(3)%l_use=.FALSE.
@@ -773,29 +764,29 @@
 
          td_file%t_var(ji)=var_init( TRIM(cd_name(ji)), NF90_DOUBLE, &
-         &                           tl_dim(:), id_id=ji,            &
-         &                           id_rec=INT(dd_value(ji),i4) )
+         &                           tl_dim(:), dd_fill=0._dp,       &
+         &                           id_id=ji, id_rec=INT(dd_value(ji),i4) )
+
+         ! clean
+         CALL dim_clean(tl_dim(:))
 
       ENDDO
 
    END SUBROUTINE iom_rstdimg__get_file_var_2d
-   !> @endcode
-   !-------------------------------------------------------------------
-   !> @brief This subroutine put information about variable 3D 
+   !-------------------------------------------------------------------
+   !> @brief This subroutine put informations about variable 3D 
    !> inside file structure.
    !
    !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[inout] td_file : file structure
-   !> @param[in] cd_name : table of variable name
-   !> @param[in] dd_value : table of variable record
-   !> @return file structure completed 
-   !-------------------------------------------------------------------
-   !> @code
+   !> - November, 2013- Initial Version
+   !
+   !> @param[inout] td_file   file structure
+   !> @param[in] cd_name      array of variable name
+   !> @param[in] dd_value     array of variable record
+   !-------------------------------------------------------------------
    SUBROUTINE iom_rstdimg__get_file_var_3d(td_file, cd_name, dd_value)
       IMPLICIT NONE
       ! Argument      
       TYPE(TFILE),                         INTENT(INOUT) :: td_file
-      CHARACTER(LEN=ip_vnl), DIMENSION(:), INTENT(IN)    :: cd_name
+      CHARACTER(LEN=im_vnl), DIMENSION(:), INTENT(IN)    :: cd_name
       REAL(dp),              DIMENSION(:), INTENT(IN)    :: dd_value
 
@@ -812,14 +803,16 @@
 
          ! define same dimension as in file
-         tl_dim(:)=td_file%t_dim(:) 
+         tl_dim(:)=dim_copy(td_file%t_dim(:)) 
 
          td_file%t_var(ji)=var_init( TRIM(cd_name(ji)), NF90_DOUBLE, &
-         &                           tl_dim(:), id_id=ji,            &
-         &                           id_rec=INT(dd_value(ji),i4) )
+         &                           tl_dim(:), dd_fill=0._dp,       &
+         &                           id_id=ji, id_rec=INT(dd_value(ji),i4) )
+
+         ! clean
+         CALL dim_clean(tl_dim(:))
 
       ENDDO
 
    END SUBROUTINE iom_rstdimg__get_file_var_3d
-   !> @endcode
    !-------------------------------------------------------------------
    !> @brief This function read one dimension in an opened netcdf file, 
@@ -829,9 +822,8 @@
    !> - Nov, 2013- Initial Version
    !
-   !> @param[in] td_file : file structure
-   !> @param[in] id_dimid : dimension id
+   !> @param[in] td_file   file structure
+   !> @param[in] id_dimid  dimension id
    !> @return  dimension structure 
    !-------------------------------------------------------------------
-   !> @code
    TYPE(TDIM) FUNCTION iom_rstdimg__read_dim_id(td_file, id_dimid)
       IMPLICIT NONE
@@ -866,5 +858,4 @@
 
    END FUNCTION iom_rstdimg__read_dim_id
-   !> @endcode
    !-------------------------------------------------------------------
    !> @brief This function read one dimension in an opened netcdf file, 
@@ -874,9 +865,8 @@
    !> - Nov, 2013- Initial Version
    !
-   !> @param[in] td_file : file structure
-   !> @param[in] cd_name : dimension name
+   !> @param[in] td_file   file structure
+   !> @param[in] cd_name   dimension name
    !> @return  dimension structure 
    !-------------------------------------------------------------------
-   !> @code
    TYPE(TDIM) FUNCTION iom_rstdimg__read_dim_name(td_file, cd_name)
       IMPLICIT NONE
@@ -909,22 +899,21 @@
 
    END FUNCTION iom_rstdimg__read_dim_name
-   !> @endcode
    !-------------------------------------------------------------------
    !> @brief This function read variable value in an opened 
-   !> dimg file, given variable id.</br/>
-   !> start indices and number of indices selected along each dimension 
-   !> could be specify in a 4 dimension table (/'x','y','z','t'/)
+   !> dimg file, given variable id.
+   !> @details
+   !> Optionaly, start indices and number of indices selected along each dimension 
+   !> could be specify in a 4 dimension array (/'x','y','z','t'/)
    !
    !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[in] td_file : file structure
-   !> @param[in] id_varid : variable id
-   !> @param[in] id_start : index in the variable from which the data values 
+   !> - November, 2013- Initial Version
+   !
+   !> @param[in] td_file   file structure
+   !> @param[in] id_varid  variable id
+   !> @param[in] id_start  index in the variable from which the data values 
    !> will be read
-   !> @param[in] id_count : number of indices selected along each dimension
+   !> @param[in] id_count  number of indices selected along each dimension
    !> @return  variable structure 
    !-------------------------------------------------------------------
-   !> @code
    TYPE(TVAR) FUNCTION iom_rstdimg__read_var_id(td_file, id_varid,&
    &                                            id_start, id_count)
@@ -935,9 +924,7 @@
       INTEGER(i4), DIMENSION(:), INTENT(IN), OPTIONAL :: id_start
       INTEGER(i4), DIMENSION(:), INTENT(IN), OPTIONAL :: id_count
-      !INTEGER(i4), DIMENSION(ip_maxdim), INTENT(IN), OPTIONAL :: id_start
-      !INTEGER(i4), DIMENSION(ip_maxdim), INTENT(IN), OPTIONAL :: id_count
 
       ! local variable
-      INTEGER(i4), DIMENSION(1) :: il_ind
+      INTEGER(i4), DIMENSION(1) :: il_varid
       !----------------------------------------------------------------
       ! check if file opened
@@ -950,8 +937,8 @@
 
          ! look for variable id
-         il_ind(:)=MINLOC(td_file%t_var(:)%i_id,mask=(td_file%t_var(:)%i_id==id_varid))
-         IF( il_ind(1) /= 0 )THEN
-
-            iom_rstdimg__read_var_id=td_file%t_var(il_ind(1))
+         il_varid(:)=MINLOC(td_file%t_var(:)%i_id,mask=(td_file%t_var(:)%i_id==id_varid))
+         IF( il_varid(1) /= 0 )THEN
+
+            iom_rstdimg__read_var_id=var_copy(td_file%t_var(il_varid(1)))
 
             IF( iom_rstdimg__read_var_id%i_ndim /= 0 )THEN
@@ -962,5 +949,5 @@
             ELSE
                CALL logger_debug( " READ VAR: variable 0d "//&
-               &               TRIM(td_file%t_var(il_ind(1))%c_name)//&
+               &               TRIM(td_file%t_var(il_varid(1))%c_name)//&
                &               " should be already read ")
             ENDIF            
@@ -974,27 +961,24 @@
       ENDIF
    END FUNCTION iom_rstdimg__read_var_id
-   !> @endcode   
    !-------------------------------------------------------------------
    !> @brief This function read variable value in an opened 
-   !> dimg file, given variable name or standard name.</br/>
-   !> start indices and number of indices selected along each dimension 
-   !> could be specify in a 4 dimension table (/'x','y','z','t'/)
-   !
+   !> dimg file, given variable name or standard name.
    !> @details
+   !> Optionaly, start indices and number of indices selected along each dimension 
+   !> could be specify in a 4 dimension array (/'x','y','z','t'/)
+   !
    !> look first for variable name. If it doesn't
    !> exist in file, look for variable standard name.<br/>
-   !> If variable name is not present, check variable standard name.<br/>
    !
    !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[in] td_file : file structure
-   !> @param[in] cd_name  : variable name or standard name 
-   !> @param[in] id_start : index in the variable from which the data values 
+   !> - November, 2013- Initial Version
+   !
+   !> @param[in] td_file   file structure
+   !> @param[in] cd_name   variable name or standard name 
+   !> @param[in] id_start  index in the variable from which the data values 
    !> will be read
-   !> @param[in] id_count : number of indices selected along each dimension
+   !> @param[in] id_count  number of indices selected along each dimension
    !> @return  variable structure 
    !-------------------------------------------------------------------
-   !> @code
    TYPE(TVAR) FUNCTION iom_rstdimg__read_var_name(td_file, cd_name,   &
    &                                              id_start, id_count  )
@@ -1005,9 +989,7 @@
       INTEGER(i4),      DIMENSION(:),  INTENT(IN), OPTIONAL :: id_start
       INTEGER(i4),      DIMENSION(:),  INTENT(IN), OPTIONAL :: id_count
-      !INTEGER(i4),      DIMENSION(ip_maxdim),  INTENT(IN), OPTIONAL :: id_start
-      !INTEGER(i4),      DIMENSION(ip_maxdim),  INTENT(IN), OPTIONAL :: id_count
 
       ! local variable
-      INTEGER(i4)       :: il_ind
+      INTEGER(i4)       :: il_varid
       !----------------------------------------------------------------
       ! check if file opened
@@ -1019,10 +1001,10 @@
       ELSE
 
-         il_ind=var_get_id(td_file%t_var(:), cd_name)
-         IF( il_ind /= 0 )THEN
-
-            iom_rstdimg__read_var_name=td_file%t_var(il_ind)
-
-            IF( td_file%t_var(il_ind)%i_ndim /= 0 )THEN
+         il_varid=var_get_index(td_file%t_var(:), cd_name)
+         IF( il_varid /= 0 )THEN
+
+            iom_rstdimg__read_var_name=var_copy(td_file%t_var(il_varid))
+
+            IF( td_file%t_var(il_varid)%i_ndim /= 0 )THEN
                !!! read variable value
                CALL iom_rstdimg__read_var_value( td_file, &
@@ -1031,5 +1013,5 @@
             ELSE
                CALL logger_debug( " READ VAR: variable 0d "//&
-               &               TRIM(td_file%t_var(il_ind)%c_name)//&
+               &               TRIM(td_file%t_var(il_varid)%c_name)//&
                &               " should have been already read ")
             ENDIF
@@ -1047,197 +1029,19 @@
       
    END FUNCTION iom_rstdimg__read_var_name
-   !> @endcode
-   !-------------------------------------------------------------------
-   !> @brief This subroutine fill all variable value in an opened 
-   !> dimg file.</br/>
-   !> start indices and number of indices selected along each dimension 
-   !> could be specify in a 4 dimension table (/'x','y','z','t'/)
-   !
-   !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[inout] td_file : file structure
-   !> @param[in] id_start : index in the variable from which the data values 
-   !> will be read
-   !> @param[in] id_count : number of indices selected along each dimension
-   !-------------------------------------------------------------------
-   !> @code
-   SUBROUTINE iom_rstdimg__fill_var_all(td_file, id_start, id_count)
-      IMPLICIT NONE
-      ! Argument      
-      TYPE(TFILE),               INTENT(INOUT) :: td_file
-      INTEGER(i4), DIMENSION(:), INTENT(IN),   OPTIONAL :: id_start
-      INTEGER(i4), DIMENSION(:), INTENT(IN),   OPTIONAL :: id_count
-      !INTEGER(i4), DIMENSION(ip_maxdim), INTENT(IN),   OPTIONAL :: id_start
-      !INTEGER(i4), DIMENSION(ip_maxdim), INTENT(IN),   OPTIONAL :: id_count
-
-      ! local variable
-
-      ! loop indices
-      INTEGER(i4) :: ji
-      !----------------------------------------------------------------
-      ! check if file opened
-      IF( td_file%i_id == 0 )THEN
-
-         CALL logger_error( &
-         &  " FILL VAR: no id associated to file "//TRIM(td_file%c_name))
-
-      ELSE
-
-         DO ji=1,td_file%i_nvar
-            CALL iom_rstdimg_fill_var(td_file, ji, id_start, id_count)
-         ENDDO
-
-      ENDIF
-   END SUBROUTINE iom_rstdimg__fill_var_all
-   !> @endcode
-   !-------------------------------------------------------------------
-   !> @brief This subroutine fill variable value in an opened 
-   !> dimg file, given variable id.</br/>
-   !> start indices and number of indices selected along each dimension 
-   !> could be specify in a 4 dimension table (/'x','y','z','t'/)
-   !
-   !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[inout] td_file : file structure
-   !> @param[in] id_varid : variable id
-   !> @param[in] id_start : index in the variable from which the data values 
-   !> will be read
-   !> @param[in] id_count : number of indices selected along each dimension
-   !-------------------------------------------------------------------
-   !> @code
-   SUBROUTINE iom_rstdimg__fill_var_id(td_file, id_varid, id_start, id_count)
-      IMPLICIT NONE
-      ! Argument      
-      TYPE(TFILE),               INTENT(INOUT) :: td_file
-      INTEGER(i4),               INTENT(IN)    :: id_varid
-      INTEGER(i4), DIMENSION(:), INTENT(IN),   OPTIONAL :: id_start
-      INTEGER(i4), DIMENSION(:), INTENT(IN),   OPTIONAL :: id_count
-      !INTEGER(i4), DIMENSION(ip_maxdim), INTENT(IN),   OPTIONAL :: id_start
-      !INTEGER(i4), DIMENSION(ip_maxdim), INTENT(IN),   OPTIONAL :: id_count
-
-      ! local variable
-      INTEGER(i4), DIMENSION(1) :: il_ind
-      TYPE(TVAR)                :: tl_var
-      !----------------------------------------------------------------
-      ! check if file opened
-      IF( td_file%i_id == 0 )THEN
-
-         CALL logger_error( &
-         &  " FILL VAR: no id associated to file "//TRIM(td_file%c_name))
-
-      ELSE
-
-         ! look for variable id
-         il_ind(:) = MINLOC( td_file%t_var(:)%i_id, &
-         &                   mask=(td_file%t_var(:)%i_id==id_varid))
-         IF( il_ind(1) /= 0 )THEN
-
-            IF( tl_var%i_ndim /= 0 )THEN
-               !!! read variable value
-               CALL iom_rstdimg__read_var_value(td_file, td_file%t_var(il_ind(1)), &
-               &                                id_start, id_count)
-
-            ELSE
-               CALL logger_debug( " FILL VAR: variable 0d "//&
-               &               TRIM(td_file%t_var(il_ind(1))%c_name)//&
-               &               " should be already read ")
-            ENDIF            
-
-         ELSE
-            CALL logger_error( &
-            &  " FILL VAR: there is no variable with id "//&
-            &  TRIM(fct_str(id_varid))//" in file "//TRIM(td_file%c_name))
-         ENDIF
-
-      ENDIF
-   END SUBROUTINE iom_rstdimg__fill_var_id
-   !> @endcode
-   !-------------------------------------------------------------------
-   !> @brief This subroutine fill variable value in an opened 
-   !> dimg file, given variable name or standard name.</br/>
-   !> start indices and number of indices selected along each dimension 
-   !> could be specify in a 4 dimension table (/'x','y','z','t'/)
-   !
-   !> @details
-   !> look first for variable name. If it doesn't
-   !> exist in file, look for variable standard name.<br/>
-   !> If variable name is not present, check variable standard name.<br/>
-   !
-   !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[inout] td_file : file structure
-   !> @param[in] cd_name  : variable name or standard name
-   !> @param[in] id_start : index in the variable from which the data values 
-   !> will be read
-   !> @param[in] id_count : number of indices selected along each dimension
-   !> @return  variable structure 
-   !-------------------------------------------------------------------
-   !> @code
-   SUBROUTINE iom_rstdimg__fill_var_name(td_file, cd_name, id_start, id_count )
-      IMPLICIT NONE
-      ! Argument 
-      TYPE(TFILE),                    INTENT(INOUT) :: td_file
-      CHARACTER(LEN=*),               INTENT(IN)    :: cd_name
-      INTEGER(i4),      DIMENSION(:), INTENT(IN), OPTIONAL :: id_start
-      INTEGER(i4),      DIMENSION(:), INTENT(IN), OPTIONAL :: id_count
-      !INTEGER(i4),      DIMENSION(ip_maxdim), INTENT(IN), OPTIONAL :: id_start
-      !INTEGER(i4),      DIMENSION(ip_maxdim), INTENT(IN), OPTIONAL :: id_count
-      !CHARACTER(LEN=*),                       INTENT(IN), OPTIONAL :: cd_stdname
-
-      ! local variable
-      INTEGER(i4)       :: il_ind
-      !----------------------------------------------------------------
-      ! check if file opened
-      IF( td_file%i_id == 0 )THEN
-
-         CALL logger_error( &
-         &  " FILL VAR: no id associated to file "//TRIM(td_file%c_name))
-
-      ELSE
-
-         il_ind=var_get_id(td_file%t_var, cd_name)
-         IF( il_ind /= 0 )THEN
-
-            IF( td_file%t_var(il_ind)%i_ndim /= 0 )THEN
-               !!! read variable value
-               CALL iom_rstdimg__read_var_value( td_file, td_file%t_var(il_ind), &
-               &                                 id_start, id_count)
-
-            ELSE
-               CALL logger_debug( " FILL VAR: variable 0d "//&
-               &               TRIM(td_file%t_var(il_ind)%c_name)//&
-               &               " should have been already read ")
-            ENDIF
-
-         ELSE
-
-            CALL logger_error( &
-            &  " FILL VAR: there is no variable with "//&
-            &  " name or standard name "//TRIM(cd_name)//&
-            &  " in file "//TRIM(td_file%c_name))
-
-         ENDIF
-
-      ENDIF
-      
-   END SUBROUTINE iom_rstdimg__fill_var_name
-   !> @endcode
    !-------------------------------------------------------------------
    !> @brief This subroutine read variable value in an opened dimg file, for
    !> variable 1,2,3d.
-   !
+   !> @details
+   !> Optionaly,start indices and number of indices selected along each dimension 
+   !> could be specify in a 4 dimension array (/'x','y','z','t'/)
+   !>
    !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[in] td_file : file structure
-   !> @param[inout] td_var : variable structure
-   !> @param[in] id_start : index in the variable from which the data values will be read
-   !> @param[in] id_count : number of indices selected along each dimension
-   !> @return variable structure completed 
-   !-------------------------------------------------------------------
-   !> @code
+   !> - November, 2013- Initial Version
+   !
+   !> @param[in] td_file   file structure
+   !> @param[inout] td_var variable structure
+   !> @param[in] id_start  index in the variable from which the data values will be read
+   !> @param[in] id_count  number of indices selected along each dimension
+   !-------------------------------------------------------------------
    SUBROUTINE iom_rstdimg__read_var_value(td_file, td_var, &
    &                                      id_start, id_count )
@@ -1248,6 +1052,4 @@
       INTEGER(i4), DIMENSION(:), INTENT(IN),  OPTIONAL :: id_start
       INTEGER(i4), DIMENSION(:), INTENT(IN),  OPTIONAL :: id_count
-      !INTEGER(i4), DIMENSION(ip_maxdim), INTENT(IN),  OPTIONAL :: id_start
-      !INTEGER(i4), DIMENSION(ip_maxdim), INTENT(IN),  OPTIONAL :: id_count
 
       ! local variable
@@ -1273,5 +1075,5 @@
          IF( SIZE(id_start(:)) /= ip_maxdim .OR. &
          &   SIZE(id_count(:)) /= ip_maxdim )THEN
-            CALL logger_error("READ VAR: dimension of table start or count "//&
+            CALL logger_error("READ VAR: dimension of array start or count "//&
             &      " are invalid to read variable "//TRIM(td_var%c_name)//&
             &      " in file "//TRIM(td_file%c_name) )
@@ -1328,5 +1130,5 @@
             &  " READ VAR VALUE: not enough space to put variable "//&
             &  TRIM(td_var%c_name)//&
-            &  " in temporary table")
+            &  " in temporary array")
 
          ENDIF
@@ -1358,5 +1160,5 @@
             ENDIF
          ELSEIF( td_var%t_dim(3)%l_use )THEN
-            ! 1d variable (Z)
+            ! 1D variable (Z)
             READ(td_file%i_id, IOSTAT=il_status, REC=td_var%i_rec ) &
             &  dl_value(:,:,:,:)
@@ -1403,16 +1205,17 @@
 
    END SUBROUTINE iom_rstdimg__read_var_value
-   !> @endcode
-   !-------------------------------------------------------------------
-   !> @brief This subroutine write file structure in an opened dimg file.
+   !-------------------------------------------------------------------
+   !> @brief This subroutine write dimg file from file structure.
    !
    !> @details
-   !
+   !> dimg file have to be already opened in write mode.
+   !>
    !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[in] td_file : file structure
-   !-------------------------------------------------------------------
-   !> @code
+   !> - November, 2013- Initial Version
+   !> @date September, 2014
+   !> - use iom_rstdimg__get_rec
+   !
+   !> @param[inout] td_file   file structure
+   !-------------------------------------------------------------------
    SUBROUTINE iom_rstdimg_write_file(td_file)
       IMPLICIT NONE
@@ -1421,6 +1224,6 @@
 
       ! local variable
-      INTEGER(i4) :: il_status
-      INTEGER(i4) :: il_attid
+      INTEGER(i4)           :: il_status
+      INTEGER(i4)           :: il_ind
       !----------------------------------------------------------------
       ! check if file opened
@@ -1433,17 +1236,36 @@
          IF( td_file%l_wrt )THEN
 
+            ! check dimension
+            IF( td_file%t_dim(jp_L)%l_use .AND. &
+            &   td_file%t_dim(jp_L)%i_len /= 1 )THEN
+               CALL logger_fatal("WRITE FILE: can not write dimg file with "//&
+               &  " several time step.")
+            ENDIF
+
             ! close and open file with right record length
             CALL iom_rstdimg_close(td_file)
 
+            ! compute record number to be used 
+            ! and add variable no0d, no1d,.. if need be
+            CALL iom_rstdimg__get_rec(td_file)
+
             ! compute record length
-            il_attid=att_get_id(td_file%t_att(:),"DOMAIN_number_total")
-            IF( il_attid /= 0 )THEN
+            il_ind=att_get_index(td_file%t_att(:),"DOMAIN_number_total")
+            IF( il_ind /= 0 )THEN
                td_file%i_recl = MAX( &
                &     td_file%t_dim(1)%i_len * td_file%t_dim(2)%i_len * 8, &
-               &     ( 8 * INT(td_file%t_att(il_attid)%d_value(1)) + 15 ) * 4 )
+               &     ( 8 * INT(td_file%t_att(il_ind)%d_value(1)) + 15 ) * 4 )
             ELSE
                td_file%i_recl = td_file%t_dim(1)%i_len * &
                &                td_file%t_dim(2)%i_len * 8
             ENDIF
+            ! check record length
+            IF( td_file%i_nvar*(im_vnl+dp) > td_file%i_recl )THEN
+               CALL logger_fatal("WRITE FILE: record length is too small. "//&
+               &  " Try to reduce the output number of processor.")
+            ENDIF
+
+            ! get free unit
+            td_file%i_id=fct_getunit()
 
             OPEN( td_file%i_id, FILE=TRIM(td_file%c_name),&
@@ -1456,8 +1278,8 @@
             CALL fct_err(il_status)
             IF( il_status /= 0 )THEN
-               CALL logger_error("REPLACE: file "//TRIM(td_file%c_name)//&
+               CALL logger_error("WRITE FILE: REPLACE file "//TRIM(td_file%c_name)//&
                &  " with record length "//TRIM(fct_str(td_file%i_recl)))
             ELSE
-               CALL logger_debug("REPLACE: file "//TRIM(td_file%c_name)//&
+               CALL logger_debug("WRITE FILE: REPLACE file "//TRIM(td_file%c_name)//&
                &  " with record length "//TRIM(fct_str(td_file%i_recl)))
             ENDIF
@@ -1479,5 +1301,111 @@
 
    END SUBROUTINE iom_rstdimg_write_file
-   !> @endcode
+   !-------------------------------------------------------------------
+   !> @brief This subroutine compute record number to be used.
+   !>
+   !> @details
+   !> Moreover it adds variable no0d, no1d, no2d and no3d if need be.
+   !>
+   !> @author J.Paul
+   !> - September, 2014- Initial Version
+   !
+   !> @param[inout] td_file   file structure
+   !-------------------------------------------------------------------
+   SUBROUTINE iom_rstdimg__get_rec(td_file)
+      IMPLICIT NONE
+      ! Argument      
+      TYPE(TFILE), INTENT(INOUT) :: td_file
+
+      ! local variable
+      INTEGER(i4) :: il_rec
+      TYPE(TVAR)  :: tl_var
+
+      INTEGER(i4), DIMENSION(:)    , ALLOCATABLE :: il_tmp1d
+      INTEGER(i4), DIMENSION(:,:)  , ALLOCATABLE :: il_tmp2d
+      INTEGER(i4), DIMENSION(:,:,:), ALLOCATABLE :: il_tmp3d
+
+      ! loop indices
+      INTEGER(i4) :: ji
+      !----------------------------------------------------------------
+
+      ! add dummy variable if necessary
+      IF( td_file%i_n0d == 0 )THEN
+         ! create var
+         tl_var=var_init('no0d')
+
+         CALL file_add_var( td_file, tl_var )
+      ENDIF
+
+      IF( td_file%i_n1d == 0 )THEN
+         ! create var
+         ALLOCATE( il_tmp1d( td_file%t_dim(3)%i_len ) )
+         il_tmp1d(:)=-1
+
+         tl_var=var_init( 'no1d', il_tmp1d(:)) 
+
+         DEALLOCATE( il_tmp1d )
+
+         CALL file_add_var( td_file, tl_var ) 
+      ENDIF
+ 
+      IF( td_file%i_n2d == 0 )THEN
+         ! create var
+         ALLOCATE( il_tmp2d( td_file%t_dim(1)%i_len, &
+         &                   td_file%t_dim(2)%i_len ) )
+         il_tmp2d(:,:)=-1
+
+         tl_var=var_init('no2d', il_tmp2d(:,:) )
+
+         DEALLOCATE( il_tmp2d )
+
+         CALL file_add_var( td_file, tl_var ) 
+
+      ENDIF
+ 
+      IF( td_file%i_n3d == 0 )THEN
+         ! create var
+         ALLOCATE( il_tmp3d( td_file%t_dim(1)%i_len, &
+         &                   td_file%t_dim(2)%i_len, &
+         &                   td_file%t_dim(3)%i_len ) )
+         il_tmp3d(:,:,:)=-1
+
+         tl_var=var_init('no3d', il_tmp3d(:,:,:) )
+
+         DEALLOCATE( il_tmp3d )
+
+         CALL file_add_var( td_file, tl_var ) 
+      ENDIF
+
+      ! clean
+      CALL var_clean(tl_var)
+
+      il_rec=2
+      DO ji=1,td_file%i_nvar
+         SELECT CASE(td_file%t_var(ji)%i_ndim)
+            CASE(0)
+               IF( INDEX(td_file%t_var(ji)%c_name, 'no0d' ) == 0 )THEN
+                  td_file%t_var(ji)%i_rec=il_rec
+                  il_rec = il_rec  + 0
+               ENDIF
+            CASE(1)
+               IF( INDEX(td_file%t_var(ji)%c_name, 'no1d' ) == 0 )THEN
+                  td_file%t_var(ji)%i_rec=il_rec
+                  il_rec = il_rec  + 1
+               ENDIF
+            CASE(2)
+               IF( INDEX(td_file%t_var(ji)%c_name, 'no2d' ) == 0 )THEN
+                  td_file%t_var(ji)%i_rec=il_rec
+                  il_rec = il_rec  + 1
+               ENDIF
+            CASE(3)
+               IF( INDEX(td_file%t_var(ji)%c_name, 'no3d' ) == 0 )THEN
+                  td_file%t_var(ji)%i_rec=il_rec
+                  il_rec = il_rec  + td_file%t_dim(3)%i_len
+               ENDIF
+         END SELECT
+      ENDDO
+      td_file%i_rhd  = il_rec
+
+      END SUBROUTINE iom_rstdimg__get_rec
    !-------------------------------------------------------------------
    !> @brief This subroutine write header in an opened dimg 
@@ -1485,11 +1413,8 @@
    !
    !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[in] td_file : file structure
-   !> @param[in] td_dim : dimension structure
-   !> @return  dimension id 
-   !-------------------------------------------------------------------
-   !> @code
+   !> - November, 2013- Initial Version
+   !
+   !> @param[inout] td_file   file structure
+   !-------------------------------------------------------------------
    SUBROUTINE iom_rstdimg__write_header(td_file)
       IMPLICIT NONE
@@ -1499,5 +1424,5 @@
       ! local variable
       INTEGER(i4) :: il_status
-      INTEGER(i4) :: il_attid
+      INTEGER(i4) :: il_ind
       INTEGER(i4) :: il_nproc
       INTEGER(i4) :: il_niproc
@@ -1537,20 +1462,20 @@
 
       ! get domain decomposition
-      il_attid=att_get_id( td_file%t_att, "DOMAIN_number_total" )
+      il_ind=att_get_index( td_file%t_att, "DOMAIN_number_total" )
       il_nproc = 1
-      IF( il_attid /= 0 )THEN
-         il_nproc = INT(td_file%t_att(il_attid)%d_value(1))
-      ENDIF
-
-      il_attid=att_get_id( td_file%t_att, "DOMAIN_I_number_total" )
+      IF( il_ind /= 0 )THEN
+         il_nproc = INT(td_file%t_att(il_ind)%d_value(1))
+      ENDIF
+
+      il_ind=att_get_index( td_file%t_att, "DOMAIN_I_number_total" )
       il_niproc = 0
-      IF( il_attid /= 0 )THEN
-         il_niproc = INT(td_file%t_att(il_attid)%d_value(1))
-      ENDIF
-
-      il_attid=att_get_id( td_file%t_att, "DOMAIN_J_number_total" )
+      IF( il_ind /= 0 )THEN
+         il_niproc = INT(td_file%t_att(il_ind)%d_value(1))
+      ENDIF
+
+      il_ind=att_get_index( td_file%t_att, "DOMAIN_J_number_total" )
       il_njproc = 0
-      IF( il_attid /= 0 )THEN
-         il_njproc = INT(td_file%t_att(il_attid)%d_value(1))
+      IF( il_ind /= 0 )THEN
+         il_njproc = INT(td_file%t_att(il_ind)%d_value(1))
       ENDIF
 
@@ -1570,17 +1495,17 @@
       
       ! get domain number
-      il_attid=att_get_id( td_file%t_att, "DOMAIN_number" )
+      il_ind=att_get_index( td_file%t_att, "DOMAIN_number" )
       il_area = 0
-      IF( il_attid /= 0 )THEN
-         il_area = INT(td_file%t_att(il_attid)%d_value(1))
+      IF( il_ind /= 0 )THEN
+         il_area = INT(td_file%t_att(il_ind)%d_value(1))
       ENDIF
 
       ! get domain global size
-      il_attid=att_get_id( td_file%t_att, "DOMAIN_size_global" )
+      il_ind=att_get_index( td_file%t_att, "DOMAIN_size_global" )
       il_iglo = 0
       il_jglo = 0
-      IF( il_attid /= 0 )THEN
-         il_iglo = INT(td_file%t_att(il_attid)%d_value(1))
-         il_jglo = INT(td_file%t_att(il_attid)%d_value(2))
+      IF( il_ind /= 0 )THEN
+         il_iglo = INT(td_file%t_att(il_ind)%d_value(1))
+         il_jglo = INT(td_file%t_att(il_ind)%d_value(2))
       ENDIF
 
@@ -1600,19 +1525,19 @@
       ! allocate local variable
       ALLOCATE( il_impp(il_nproc), il_jmpp(il_nproc),&
-      &         il_lci(il_niproc),  il_lcj(il_njproc), &
-      &         il_ldi(il_niproc),  il_ldj(il_njproc), &
-      &         il_lei(il_niproc),  il_lej(il_njproc) )
+      &         il_lci(il_nproc),  il_lcj(il_nproc), &
+      &         il_ldi(il_nproc),  il_ldj(il_nproc), &
+      &         il_lei(il_nproc),  il_lej(il_nproc) )
 
       ! get domain first poistion
-      il_attid=att_get_id( td_file%t_att, "DOMAIN_I_position_first" )
+      il_ind=att_get_index( td_file%t_att, "DOMAIN_I_position_first" )
       il_impp(:) = 0
-      IF( il_attid /= 0 )THEN
-         il_impp(:) = INT(td_file%t_att(il_attid)%d_value(:))
-      ENDIF
-
-      il_attid=att_get_id( td_file%t_att, "DOMAIN_J_position_first" )
+      IF( il_ind /= 0 )THEN
+         il_impp(:) = INT(td_file%t_att(il_ind)%d_value(:))
+      ENDIF
+
+      il_ind=att_get_index( td_file%t_att, "DOMAIN_J_position_first" )
       il_jmpp(:) = 0
-      IF( il_attid /= 0 )THEN
-         il_jmpp(:) = INT(td_file%t_att(il_attid)%d_value(:))
+      IF( il_ind /= 0 )THEN
+         il_jmpp(:) = INT(td_file%t_att(il_ind)%d_value(:))
       ENDIF
       
@@ -1623,14 +1548,14 @@
 
       ! get domain last poistion
-      il_attid=att_get_id( td_file%t_att, "DOMAIN_I_position_last" )
+      il_ind=att_get_index( td_file%t_att, "DOMAIN_I_position_last" )
       il_lci(:) = 0
-      IF( il_attid /= 0 )THEN
-         il_lci(:) = INT(td_file%t_att(il_attid)%d_value(:))
-      ENDIF
-
-      il_attid=att_get_id( td_file%t_att, "DOMAIN_J_position_last" )
+      IF( il_ind /= 0 )THEN
+         il_lci(:) = INT(td_file%t_att(il_ind)%d_value(:))
+      ENDIF
+
+      il_ind=att_get_index( td_file%t_att, "DOMAIN_J_position_last" )
       il_lcj(:) = 0
-      IF( il_attid /= 0 )THEN
-         il_lcj(:) = INT(td_file%t_att(il_attid)%d_value(:))
+      IF( il_ind /= 0 )THEN
+         il_lcj(:) = INT(td_file%t_att(il_ind)%d_value(:))
       ENDIF
 
@@ -1641,14 +1566,14 @@
 
       ! get halo size start
-      il_attid=att_get_id( td_file%t_att, "DOMAIN_I_halo_size_start" )
+      il_ind=att_get_index( td_file%t_att, "DOMAIN_I_halo_size_start" )
       il_ldi(:) = 0
-      IF( il_attid /= 0 )THEN
-         il_ldi(:) = INT(td_file%t_att(il_attid)%d_value(:))
-      ENDIF
-
-      il_attid=att_get_id( td_file%t_att, "DOMAIN_J_halo_size_start" )
+      IF( il_ind /= 0 )THEN
+         il_ldi(:) = INT(td_file%t_att(il_ind)%d_value(:))
+      ENDIF
+
+      il_ind=att_get_index( td_file%t_att, "DOMAIN_J_halo_size_start" )
       il_ldj(:) = 0
-      IF( il_attid /= 0 )THEN
-         il_ldj(:) = INT(td_file%t_att(il_attid)%d_value(:))
+      IF( il_ind /= 0 )THEN
+         il_ldj(:) = INT(td_file%t_att(il_ind)%d_value(:))
       ENDIF
       
@@ -1659,14 +1584,14 @@
 
       ! get halo size end
-      il_attid=att_get_id( td_file%t_att, "DOMAIN_I_halo_size_end" )
+      il_ind=att_get_index( td_file%t_att, "DOMAIN_I_halo_size_end" )
       il_lei(:) = 0
-      IF( il_attid /= 0 )THEN
-         il_lei(:) = INT(td_file%t_att(il_attid)%d_value(:))
-      ENDIF
-
-      il_attid=att_get_id( td_file%t_att, "DOMAIN_J_halo_size_end" )
+      IF( il_ind /= 0 )THEN
+         il_lei(:) = INT(td_file%t_att(il_ind)%d_value(:))
+      ENDIF
+
+      il_ind=att_get_index( td_file%t_att, "DOMAIN_J_halo_size_end" )
       il_lej(:) = 0
-      IF( il_attid /= 0 )THEN
-         il_lej(:) = INT(td_file%t_att(il_attid)%d_value(:))
+      IF( il_ind /= 0 )THEN
+         il_lej(:) = INT(td_file%t_att(il_ind)%d_value(:))
       ENDIF
 
@@ -1690,8 +1615,8 @@
       &  il_area,                &
       &  il_iglo, il_jglo,       &
-      &  il_impp(:), il_jmpp(:), &
       &  il_lci(:), il_lcj(:),   &
       &  il_ldi(:), il_ldj(:),   &
-      &  il_lei(:), il_lej(:)
+      &  il_lei(:), il_lej(:),   &
+      &  il_impp(:), il_jmpp(:)
 
       DEALLOCATE( il_impp, il_jmpp,&
@@ -1701,14 +1626,12 @@
 
       END SUBROUTINE iom_rstdimg__write_header
-   !> @endcode   
-   !-------------------------------------------------------------------
-   !> @brief This subroutine write variables in an opened dimg file.</br/>
-   !
+   !-------------------------------------------------------------------
+   !> @brief This subroutine write variables in an opened dimg file.
+   !>
    !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[in] id_fileid : file id
-   !-------------------------------------------------------------------
-   !> @code
+   !> - November, 2013- Initial Version
+   !>
+   !> @param[in] id_fileid file id
+   !-------------------------------------------------------------------
    SUBROUTINE iom_rstdimg__write_var(td_file)
       IMPLICIT NONE
@@ -1718,91 +1641,52 @@
       ! local variable
       INTEGER(i4) :: il_status
-      TYPE(TVAR)  :: tl_var
+      INTEGER(i4) :: il_rec
 
       INTEGER(i4),            DIMENSION(:), ALLOCATABLE :: il_start
       INTEGER(i4),            DIMENSION(:), ALLOCATABLE :: il_count
-      CHARACTER(LEN=ip_vnl),  DIMENSION(:), ALLOCATABLE :: cl_name
+      CHARACTER(LEN=im_vnl),  DIMENSION(:), ALLOCATABLE :: cl_name
       REAL(dp),               DIMENSION(:), ALLOCATABLE :: dl_value
-
-      INTEGER(i4),            DIMENSION(:,:,:,:), ALLOCATABLE :: il_tmp
 
       ! loop indices
       INTEGER(i4) :: ji
+      INTEGER(i4) :: jk
       !----------------------------------------------------------------
-
-      ! add dummy variable if necessary
-      IF( td_file%i_n0d == 0 )THEN
-         ! create var
-         tl_var=var_init('no0d')
-         ! add value
-         ALLOCATE( il_tmp(1,1,1,1) )
-         il_tmp(:,:,:,:)=-1
-         CALL var_add_value(tl_var, il_tmp)
-         DEALLOCATE( il_tmp )
-
-         CALL file_add_var( td_file, tl_var )
-      ENDIF
-
-      IF( td_file%i_n1d == 0 )THEN
-         ! create var
-         tl_var=var_init('no1d')
-         ! add dimension
-         CALL var_add_dim(tl_var, td_file%t_dim(3))
-         ! add value
-         ALLOCATE( il_tmp(1,1,td_file%t_dim(3)%i_len, 1) )
-         il_tmp(:,:,:,:)=-1
-         CALL var_add_value(tl_var, il_tmp)
-         DEALLOCATE( il_tmp )
-
-         CALL file_add_var( td_file, tl_var ) 
-      ENDIF
- 
-      IF( td_file%i_n2d == 0 )THEN
-         ! create var
-         tl_var=var_init('no2d' )
-         ! add dimension
-         CALL var_add_dim(tl_var, td_file%t_dim(1))
-         CALL var_add_dim(tl_var, td_file%t_dim(2))
-         ! add value
-         ALLOCATE( il_tmp( td_file%t_dim(1)%i_len, &
-         &                 td_file%t_dim(2)%i_len, &
-         &                            1,             &
-         &                            1            ) )
-         il_tmp(:,:,:,:)=-1
-         CALL var_add_value(tl_var, il_tmp)
-         DEALLOCATE( il_tmp )
-
-         CALL file_add_var( td_file, tl_var ) 
-      ENDIF
- 
-      IF( td_file%i_n3d == 0 )THEN
-         ! create var
-         tl_var=var_init('no3d' )
-         ! add dimension
-         CALL var_add_dim(tl_var, td_file%t_dim(1))
-         CALL var_add_dim(tl_var, td_file%t_dim(2))
-         CALL var_add_dim(tl_var, td_file%t_dim(3))
-         ! add value
-         ALLOCATE( il_tmp( td_file%t_dim(1)%i_len, &
-         &                 td_file%t_dim(2)%i_len, &
-         &                 td_file%t_dim(3)%i_len, &
-         &                            1            ) )
-         il_tmp(:,:,:,:)=-1
-         CALL var_add_value(tl_var, il_tmp)
-         DEALLOCATE( il_tmp )
-
-         CALL file_add_var( td_file, tl_var ) 
-      ENDIF
 
       ! reform name and record
       ALLOCATE( cl_name(td_file%i_nvar), dl_value(td_file%i_nvar) )
+
       DO ji=1,td_file%i_nvar
+
+         ! change FillValue to 0.
+         CALL var_chg_FillValue(td_file%t_var(ji),0._dp)
+
          cl_name(ji)  = TRIM(td_file%t_var(ji)%c_name)
          dl_value(ji) = REAL(td_file%t_var(ji)%i_rec,dp)
-      ENDDO
-
-      ! special case for 0d
-      DO ji=1,td_file%i_n0d
-         dl_value(ji)=td_file%t_var(ji)%d_value(1,1,1,1)
+         
+         SELECT CASE (TRIM(td_file%t_var(ji)%c_name))
+            CASE('no0d','no1d','no2d','no3d')
+            CASE DEFAULT
+               DO jk=1,td_file%t_var(ji)%t_dim(3)%i_len
+                  SELECT CASE (td_file%t_var(ji)%i_ndim)
+                     CASE(0)
+                        ! special case for 0d, value save in rec
+                        dl_value(ji)=td_file%t_var(ji)%d_value(1,1,1,1)
+                        il_rec = td_file%t_var(ji)%i_rec
+                     CASE(1,2)
+                        il_rec = td_file%t_var(ji)%i_rec
+                     CASE(3)
+                        il_rec = td_file%t_var(ji)%i_rec + jk -1
+                  END SELECT 
+                  WRITE( td_file%i_id, IOSTAT=il_status, REC=il_rec ) &
+                  &  td_file%t_var(ji)%d_value(:,:,jk,1)
+                  CALL fct_err(il_status)
+                  IF( il_status /= 0 )THEN
+                     CALL logger_error("IOM RSTDIMG WRITE VAR: can not "//&
+                     &  "write variable "//TRIM(td_file%t_var(ji)%c_name)//&
+                     &  " in record "//TRIM(fct_str(il_rec)))
+                  ENDIF
+               ENDDO
+            END SELECT
+
       ENDDO
 
@@ -1820,5 +1704,4 @@
       il_start(4) = 1 + il_count(3)
       il_count(4) = il_start(4) - 1 + td_file%i_n3d
- 
 
       WRITE(td_file%i_id, IOSTAT=il_status, REC=td_file%i_rhd )&
@@ -1827,8 +1710,14 @@
       &  cl_name(il_start(3):il_count(3)), dl_value(il_start(3):il_count(3)),&
       &  cl_name(il_start(4):il_count(4)), dl_value(il_start(4):il_count(4))
-
+      CALL fct_err(il_status)
+      IF( il_status /= 0 )THEN
+         CALL logger_error("IOM RSTDIMG WRITE VAR: can not "//&
+         &  "write restart header in record "//TRIM(fct_str(td_file%i_rhd)))
+      ENDIF
+
+      ! clean
+      DEALLOCATE( cl_name, dl_value )
       DEALLOCATE( il_start, il_count )
 
    END SUBROUTINE iom_rstdimg__write_var
-   !> @endcode   
 END MODULE iom_rstdimg
