Index: branches/2015/dev_r5021_UKMO1_CICE_coupling/NEMOGCM/TOOLS/SIREN/src/file.f90
===================================================================
--- branches/2015/dev_r5021_UKMO1_CICE_coupling/NEMOGCM/TOOLS/SIREN/src/file.f90	(revision 5024)
+++ branches/2015/dev_r5021_UKMO1_CICE_coupling/NEMOGCM/TOOLS/SIREN/src/file.f90	(revision 5213)
@@ -7,15 +7,23 @@
 !> @brief 
 !> This module manage file structure.
-!
+!>
 !> @details
-!>
 !>    define type TFILE:<br/>
-!>    TYPE(TFILE) :: tl_file<br/>
-!>
-!>    to initialise a file structure:<br/>
-!>    tl_file=file_init(cd_file [,cd_type] [,ld_wrt])
+!> @code
+!>    TYPE(TFILE) :: tl_file
+!> @endcode
+!>
+!>    to initialize a file structure:<br/>
+!> @code
+!>    tl_file=file_init(cd_file [,cd_type] [,ld_wrt] [,cd_grid])
+!%    tl_file=file_init(cd_file [,cd_type] [,ld_wrt] [,id_ew] [,id_perio] [,id_pivot] [,cd_grid])
+!> @endcode
 !>       - cd_file is the file name
-!>       - cd_type is the type of the file ('cdf', 'dimg') (optional)
-!>       - ld_wrt  file in write mode or not (optional)
+!>       - cd_type is the type of the file ('cdf', 'dimg') [optional]
+!>       - ld_wrt  file in write mode or not [optional]
+!%       - id_ew is the number of point for east-west overlap [optional]
+!%       - id_perio is the NEMO periodicity index [optional]
+!%       - id_pivot is the NEMO pivot point index F(0),T(1) [optional]
+!>       - cd_grid is the grid type (default 'ARAKAWA-C')
 !>
 !>    to get file name:<br/>
@@ -38,5 +46,5 @@
 !>    - tl_file\%i_nvar
 !>
-!>    to get the table of variable structure associated to the file:<br/>
+!>    to get the array of variable structure associated to the file:<br/>
 !>    - tl_file\%t_var(:)
 !>
@@ -45,5 +53,5 @@
 !>    - tl_file\%i_natt
 !>
-!>    to get the table of attributes structure associated to the file:<br/>
+!>    to get the array of attributes structure associated to the file:<br/>
 !>    - tl_file\%t_att(:)
 !>
@@ -52,49 +60,76 @@
 !>    - tl_file\%i_ndim
 !>
-!>    to get the table of dimension structure (4 elts) associated to the
+!>    to get the array of dimension structure (4 elts) associated to the
 !>    file:<br/>
 !>    - tl_file\%t_dim(:)
 !>
 !>    to print information about file structure:<br/>
+!> @code
 !>    CALL file_print(td_file)
+!> @endcode
+!>
+!>    to clean file structure:<br/>
+!> @code
+!>    CALL file_clean(td_file)
+!> @endcode
 !>
 !>    to add a global attribute structure in file structure:<br/>
+!> @code
 !>    CALL file_add_att(td_file, td_att)
+!> @endcode
 !>       - td_att is an attribute structure
 !>
 !>    to add a dimension structure in file structure:<br/>
+!> @code
 !>    CALL file_add_dim(td_file, td_dim)
+!> @endcode
 !>       - td_dim is a dimension structure
 !>
 !>    to add a variable structure in file structure:<br/>
+!> @code
 !>    CALL file_add_var(td_file, td_var)
+!> @endcode
 !>       - td_var is a variable structure
 !>
 !>    to delete a global attribute structure in file structure:<br/>
+!> @code
 !>    CALL file_del_att(td_file, td_att)
+!> @endcode
 !>       - td_att is an attribute structure
 !>
 !>    to delete a dimension structure in file structure:<br/>
+!> @code
 !>    CALL file_del_dim(td_file, td_dim)
+!> @endcode
 !>       - td_dim is a dimension structure
 !>
 !>    to delete a variable structure in file structure:<br/>
+!> @code
 !>    CALL file_del_var(td_file, td_var)
+!> @endcode
 !>       - td_var is a variable structure
 !>
 !>    to overwrite one attribute structure in file structure:<br/>
+!> @code
 !>    CALL file_move_att(td_file, td_att)
+!> @endcode
 !>       - td_att is an attribute structure
 !>
 !>    to  overwrite one dimension strucutre in file structure:<br/>
+!> @code
 !>    CALL file_move_dim(td_file, td_dim)
+!> @endcode
 !>       - td_dim is a dimension structure
 !>
 !>    to overwrite one variable  structure in file structure:<br/>
+!> @code
 !>    CALL file_move_var(td_file, td_var)
+!> @endcode
 !>       - td_var is a variable structure
 !> 
 !>    to check if file and variable structure share same dimension:<br/>
+!> @code
 !>    ll_check_dim = file_check_var_dim(td_file, td_var)
+!> @endcode
 !>       - td_var is a variable structure
 !>
@@ -102,10 +137,8 @@
 !> J.Paul
 ! REVISION HISTORY:
-!> @date Nov, 2013- Initial Version
+!> @date November, 2013- Initial Version
+!> @date November, 2014 - Fix memory leaks bug
 !>
 !> @note Software governed by the CeCILL licence     (NEMOGCM/NEMO_CeCILL.txt)
-!> @todo
-!> - file_get_var(td_file, varname)
-!> - add description generique de l'objet file
 !----------------------------------------------------------------------
 MODULE file
@@ -113,100 +146,107 @@
    USE global                          ! global variable
    USE fct                             ! basic useful function
-   USE logger                             ! log file manager
+   USE logger                          ! log file manager
    USE dim                             ! dimension manager
    USE att                             ! attribute manager
    USE var                             ! variable manager
    IMPLICIT NONE
-   PRIVATE
    ! NOTE_avoid_public_variables_if_possible
 
    ! type and variable
-   PUBLIC :: TFILE   ! file structure
+   PUBLIC :: TFILE   !< file structure
 
    ! function and subroutine
-   PUBLIC :: ASSIGNMENT(=)   !< copy file structure
-   PUBLIC :: file_print      !< print information about file structure
-   PUBLIC :: file_clean      !< clean file structure
-   PUBLIC :: file_init       !< initialise file structure
-   PUBLIC :: file_add_att    !< add one attribute structure in file structure
-   PUBLIC :: file_add_var    !< add one variable  structure in file structure
-   PUBLIC :: file_add_dim    !< add one dimension strucutre in file structure
-   PUBLIC :: file_del_att    !< delete one attribute structure of file structure
-   PUBLIC :: file_del_var    !< delete one variable  structure of file structure
-   PUBLIC :: file_del_dim    !< delete one dimension strucutre of file structure
-   PUBLIC :: file_move_att   !< overwrite one attribute structure in file structure
-   PUBLIC :: file_move_var   !< overwrite one variable  structure in file structure
-   PUBLIC :: file_move_dim   !< overwrite one dimension strucutre in file structure
+   PUBLIC :: file_copy           !< copy file structure
+   PUBLIC :: file_print          !< print information about file structure
+   PUBLIC :: file_clean          !< clean file structure
+   PUBLIC :: file_init           !< initialize file structure
+   PUBLIC :: file_add_att        !< add one attribute structure in file structure
+   PUBLIC :: file_add_var        !< add one variable  structure in file structure
+   PUBLIC :: file_add_dim        !< add one dimension strucutre in file structure
+   PUBLIC :: file_del_att        !< delete one attribute structure of file structure
+   PUBLIC :: file_del_var        !< delete one variable  structure of file structure
+   PUBLIC :: file_del_dim        !< delete one dimension strucutre of file structure
+   PUBLIC :: file_move_att       !< overwrite one attribute structure in file structure
+   PUBLIC :: file_move_var       !< overwrite one variable  structure in file structure
+   PUBLIC :: file_move_dim       !< overwrite one dimension strucutre in file structure
    PUBLIC :: file_check_var_dim  !< check if file and variable structure use same dimension.
-   PUBLIC :: file_get_type   !< get type of file
-   PUBLIC :: file_get_id     !< get file id
-   PUBLIC :: file_rename     !< rename file name
-   PUBLIC :: file_add_suffix !< add suffix to file name
+   PUBLIC :: file_get_type       !< get type of file
+   PUBLIC :: file_get_id         !< get file id
+   PUBLIC :: file_rename         !< rename file name
+   PUBLIC :: file_add_suffix     !< add suffix to file name
  
-   PRIVATE :: file__del_var_name !< delete a variable structure in file structure, given variable name or standard name
-   PRIVATE :: file__del_var_str  !< delete a variable structure in file structure, given variable structure
-   PRIVATE :: file__del_att_name !< delete a attribute structure in file structure, given attribute name
-   PRIVATE :: file__del_att_str  !< delete a attribute structure in file structure, given attribute structure
-   PRIVATE :: file__get_number   !< get number in file name without suffix
-   PRIVATE :: file__get_suffix   !< get suffix of file name
-   PRIVATE :: file__copy_unit    !< copy file structure
-   PRIVATE :: file__copy_tab     !< copy file structure
-
-   !> @struct
-   TYPE TFILE
+   PRIVATE :: file__clean_unit    ! clean file structure
+   PRIVATE :: file__clean_arr     ! clean array of file structure
+   PRIVATE :: file__del_var_name ! delete a variable structure in file structure, given variable name or standard name
+   PRIVATE :: file__del_var_str  ! delete a variable structure in file structure, given variable structure
+   PRIVATE :: file__del_att_name ! delete a attribute structure in file structure, given attribute name
+   PRIVATE :: file__del_att_str  ! delete a attribute structure in file structure, given attribute structure
+   PRIVATE :: file__get_number   ! get number in file name without suffix
+   PRIVATE :: file__get_suffix   ! get suffix of file name
+   PRIVATE :: file__copy_unit    ! copy file structure
+   PRIVATE :: file__copy_arr     ! copy array of file structure
+   PRIVATE :: file__rename_char  ! rename file name, given processor number.
+   PRIVATE :: file__rename_str   ! rename file name, given file structure.
+
+   TYPE TFILE !< file structure
 
       ! general 
-      CHARACTER(LEN=lc)                 :: c_name = "" !< file name
-      CHARACTER(LEN=lc)                 :: c_type = "" !< type of the file (cdf, cdf4, dimg)
-      INTEGER(i4)                       :: i_id   = 0         !< file id
-      LOGICAL                           :: l_wrt  = .FALSE.   !< read or write mode
-      INTEGER(i4)                       :: i_nvar = 0         !< number of variable
-      TYPE(TVAR), DIMENSION(:), POINTER :: t_var  => NULL()   !< file variables
+      CHARACTER(LEN=lc)                 :: c_name = ""       !< file name
+      CHARACTER(LEN=lc)                 :: c_type = ""       !< type of the file (cdf, cdf4, dimg)
+      INTEGER(i4)                       :: i_id   = 0        !< file id
+      LOGICAL                           :: l_wrt  = .FALSE.  !< read or write mode
+      INTEGER(i4)                       :: i_nvar = 0        !< number of variable
+      TYPE(TVAR), DIMENSION(:), POINTER :: t_var  => NULL()  !< file variables
 
       CHARACTER(LEN=lc)                 :: c_grid = 'ARAKAWA-C' !< grid type
 
-      INTEGER(i4)                       :: i_ew    =-1   !< east-west overlap
-      INTEGER(i4)                       :: i_perio =-1   !< NEMO periodicity index
-      INTEGER(i4)                       :: i_pivot =-1   !< NEMO pivot point index F(0),T(1)
-
-      INTEGER(i4)                       :: i_depthid = 0         !< variable id of depth
-      INTEGER(i4)                       :: i_timeid  = 0         !< variable id of time
+      INTEGER(i4)                       :: i_ew    =-1       !< east-west overlap
+      INTEGER(i4)                       :: i_perio =-1       !< NEMO periodicity index
+      INTEGER(i4)                       :: i_pivot =-1       !< NEMO pivot point index F(0),T(1)
+
+      INTEGER(i4)                       :: i_depthid = 0     !< variable id of depth
+      INTEGER(i4)                       :: i_timeid  = 0     !< variable id of time
 
       ! netcdf file
-      INTEGER(i4)                       :: i_ndim  = 0        !< number of dimensions used in the file
-      INTEGER(i4)                       :: i_natt  = 0        !< number of global attributes in the file
-      INTEGER(i4)                       :: i_uldid = 0        !< id of the unlimited dimension in the file
-      LOGICAL                           :: l_def   = .FALSE.  !< define mode or not
-      TYPE(TATT), DIMENSION(:), POINTER :: t_att   => NULL()  !< global attributes
-      TYPE(TDIM), DIMENSION(ip_maxdim)  :: t_dim              !< dimension structure
+      INTEGER(i4)                       :: i_ndim  = 0       !< number of dimensions used in the file
+      INTEGER(i4)                       :: i_natt  = 0       !< number of global attributes in the file
+      INTEGER(i4)                       :: i_uldid = 0       !< id of the unlimited dimension in the file
+      LOGICAL                           :: l_def   = .FALSE. !< define mode or not
+      TYPE(TATT), DIMENSION(:), POINTER :: t_att   => NULL() !< global attributes
+      TYPE(TDIM), DIMENSION(ip_maxdim)  :: t_dim             !< dimension structure
       
       ! dimg file
-      INTEGER(i4)                       :: i_recl = 0         !< record length (binary file)
-      INTEGER(i4)                       :: i_n0d  = 0         !< number of scalar variable
-      INTEGER(i4)                       :: i_n1d  = 0         !< number of 1D variable
-      INTEGER(i4)                       :: i_n2d  = 0         !< number of 2D variable
-      INTEGER(i4)                       :: i_n3d  = 0         !< number of 3D variable
-      INTEGER(i4)                       :: i_rhd  = 0         !< record of the header infos (last record)
+      INTEGER(i4)                       :: i_recl = 0        !< record length (binary file)
+      INTEGER(i4)                       :: i_n0d  = 0        !< number of scalar variable
+      INTEGER(i4)                       :: i_n1d  = 0        !< number of 1D variable
+      INTEGER(i4)                       :: i_n2d  = 0        !< number of 2D variable
+      INTEGER(i4)                       :: i_n3d  = 0        !< number of 3D variable
+      INTEGER(i4)                       :: i_rhd  = 0        !< record of the header infos (last record)
 
       ! mpp
       ! only use for massively parallel processing
-      INTEGER(i4)                       :: i_pid  = -1        !< processor id (start to 1)
-      INTEGER(i4)                       :: i_impp = 0         !< i-indexes for mpp-subdomain left bottom
-      INTEGER(i4)                       :: i_jmpp = 0         !< j-indexes for mpp-subdomain left bottom
-      INTEGER(i4)                       :: i_lci  = 0         !< i-dimensions of subdomain
-      INTEGER(i4)                       :: i_lcj  = 0         !< j-dimensions of subdomain
-      INTEGER(i4)                       :: i_ldi  = 0         !< first indoor i-indices
-      INTEGER(i4)                       :: i_ldj  = 0         !< first indoor j-indices
-      INTEGER(i4)                       :: i_lei  = 0         !< last  indoor i-indices
-      INTEGER(i4)                       :: i_lej  = 0         !< last  indoor j-indices
-
-      LOGICAL                           :: l_ctr  = .FALSE.   !< domain is on border
-      LOGICAL                           :: l_use  = .FALSE.   !< domain is used
-
-      ! only use to draw domain decomposition when initialise with mpp_init
-      INTEGER(i4)                       :: i_iind = 0         !< i-direction indices
-      INTEGER(i4)                       :: i_jind = 0         !< j-direction indices
+      INTEGER(i4)                       :: i_pid  = -1       !< processor id (start to 1)
+      INTEGER(i4)                       :: i_impp = 0        !< i-indexes for mpp-subdomain left bottom
+      INTEGER(i4)                       :: i_jmpp = 0        !< j-indexes for mpp-subdomain left bottom
+      INTEGER(i4)                       :: i_lci  = 0        !< i-dimensions of subdomain
+      INTEGER(i4)                       :: i_lcj  = 0        !< j-dimensions of subdomain
+      INTEGER(i4)                       :: i_ldi  = 0        !< first indoor i-indices
+      INTEGER(i4)                       :: i_ldj  = 0        !< first indoor j-indices
+      INTEGER(i4)                       :: i_lei  = 0        !< last  indoor i-indices
+      INTEGER(i4)                       :: i_lej  = 0        !< last  indoor j-indices
+
+      LOGICAL                           :: l_ctr  = .FALSE.  !< domain is on border
+      LOGICAL                           :: l_use  = .FALSE.  !< domain is used
+
+      ! only use to draw domain decomposition when initialize with mpp_init
+      INTEGER(i4)                       :: i_iind = 0        !< i-direction indices
+      INTEGER(i4)                       :: i_jind = 0        !< j-direction indices
 
    END TYPE TFILE
+
+   INTERFACE file_clean
+      MODULE PROCEDURE file__clean_unit
+      MODULE PROCEDURE file__clean_arr
+   END INTERFACE file_clean
 
    INTERFACE file_del_var
@@ -225,7 +265,7 @@
    END INTERFACE file_rename
 
-    INTERFACE ASSIGNMENT(=)
-      MODULE PROCEDURE file__copy_unit   ! copy file structure
-      MODULE PROCEDURE file__copy_tab    ! copy file structure
+    INTERFACE file_copy
+      MODULE PROCEDURE file__copy_unit   
+      MODULE PROCEDURE file__copy_arr    
    END INTERFACE
 
@@ -233,8 +273,7 @@
    !-------------------------------------------------------------------
    !> @brief
-   !> This function copy file structure in another file
-   !> structure
+   !> This subroutine copy file structure in another one
    !> @details 
-   !> file variable and attribute value are copied in a temporary table, 
+   !> file variable and attribute value are copied in a temporary array, 
    !> so input and output file structure value do not point on the same 
    !> "memory cell", and so on are independant. 
@@ -242,19 +281,29 @@
    !> @note new file is assume to be closed.
    !>
+   !> @warning do not use on the output of a function who create or read an
+   !> structure (ex: tl_file=file_copy(file_init()) is forbidden).
+   !> This will create memory leaks.
    !> @warning to avoid infinite loop, do not use any function inside 
    !> this subroutine
    !>   
    !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[out] td_file1  : file structure
-   !> @param[in] td_file2  : file structure
-   !-------------------------------------------------------------------
-   !> @code
-   SUBROUTINE file__copy_unit( td_file1, td_file2 )
+   !> - November, 2013- Initial Version
+   !> @date November, 2014
+   !>    - use function instead of overload assignment operator 
+   !> (to avoid memory leak)
+   !
+   !> @param[in] td_file  file structure
+   !> @return copy of input file structure
+   !-------------------------------------------------------------------
+   FUNCTION file__copy_unit( td_file )
       IMPLICIT NONE
       ! Argument
-      TYPE(TFILE), INTENT(  OUT) :: td_file1
-      TYPE(TFILE), INTENT(IN   )  :: td_file2
+      TYPE(TFILE), INTENT(IN) :: td_file
+      ! function
+      TYPE(TFILE) :: file__copy_unit
+
+      ! local variable
+      TYPE(TVAR) :: tl_var
+      TYPE(TATT) :: tl_att
 
       ! loop indices
@@ -262,78 +311,90 @@
       !----------------------------------------------------------------
 
-      CALL logger_trace("COPY: file "//TRIM(td_file2%c_name) )
+      CALL logger_trace("FILE COPY: file "//TRIM(td_file%c_name) )
 
       ! copy file variable
-      td_file1%c_name = TRIM(td_file2%c_name)
-      td_file1%c_type = TRIM(td_file2%c_type)
+      file__copy_unit%c_name = TRIM(td_file%c_name)
+      file__copy_unit%c_type = TRIM(td_file%c_type)
       ! file1 should be closed even if file2 is opened right now
-      td_file1%i_id   = 0
-      td_file1%l_wrt  = td_file2%l_wrt
-      td_file1%i_nvar = td_file2%i_nvar
-
-      td_file1%c_grid = td_file2%c_grid
-
-      td_file1%i_ew   = td_file2%i_ew
-      td_file1%i_perio= td_file2%i_perio
-      td_file1%i_pivot= td_file2%i_pivot
+      file__copy_unit%i_id   = 0
+      file__copy_unit%l_wrt  = td_file%l_wrt
+      file__copy_unit%i_nvar = td_file%i_nvar
+
+      file__copy_unit%c_grid = td_file%c_grid
+
+      file__copy_unit%i_ew   = td_file%i_ew
+      file__copy_unit%i_perio= td_file%i_perio
+      file__copy_unit%i_pivot= td_file%i_pivot
+
+      file__copy_unit%i_depthid = td_file%i_depthid
+      file__copy_unit%i_timeid  = td_file%i_timeid
 
       ! copy variable structure
-      IF( ASSOCIATED(td_file1%t_var) ) DEALLOCATE(td_file1%t_var)
-      IF( ASSOCIATED(td_file2%t_var) .AND. td_file1%i_nvar > 0 )THEN
-         ALLOCATE( td_file1%t_var(td_file1%i_nvar) )
-         DO ji=1,td_file1%i_nvar
-            td_file1%t_var(ji) = td_file2%t_var(ji)
+      IF( ASSOCIATED(file__copy_unit%t_var) )THEN
+         CALL var_clean(file__copy_unit%t_var(:))
+         DEALLOCATE(file__copy_unit%t_var)
+      ENDIF
+      IF( ASSOCIATED(td_file%t_var) .AND. file__copy_unit%i_nvar > 0 )THEN
+         ALLOCATE( file__copy_unit%t_var(file__copy_unit%i_nvar) )
+         DO ji=1,file__copy_unit%i_nvar
+            tl_var = var_copy(td_file%t_var(ji))
+            file__copy_unit%t_var(ji) = var_copy(tl_var)
          ENDDO
       ENDIF
       
       ! copy netcdf variable
-      td_file1%i_ndim    = td_file2%i_ndim
-      td_file1%i_natt    = td_file2%i_natt
-      td_file1%i_uldid   = td_file2%i_uldid
-      td_file1%l_def     = td_file2%l_def
+      file__copy_unit%i_ndim   = td_file%i_ndim
+      file__copy_unit%i_natt   = td_file%i_natt
+      file__copy_unit%i_uldid  = td_file%i_uldid
+      file__copy_unit%l_def    = td_file%l_def
 
       ! copy dimension
-      td_file1%t_dim(:) = td_file2%t_dim(:)
+      file__copy_unit%t_dim(:) = dim_copy(td_file%t_dim(:))
       
       ! copy attribute structure
-      IF( ASSOCIATED(td_file1%t_att) ) DEALLOCATE(td_file1%t_att)
-      IF( ASSOCIATED(td_file2%t_att) .AND. td_file1%i_natt > 0 )THEN
-         ALLOCATE( td_file1%t_att(td_file1%i_natt) )
-         DO ji=1,td_file1%i_natt
-            td_file1%t_att(ji) = td_file2%t_att(ji)
+      IF( ASSOCIATED(file__copy_unit%t_att) )THEN
+         CALL att_clean(file__copy_unit%t_att(:))
+         DEALLOCATE(file__copy_unit%t_att)
+      ENDIF
+      IF( ASSOCIATED(td_file%t_att) .AND. file__copy_unit%i_natt > 0 )THEN
+         ALLOCATE( file__copy_unit%t_att(file__copy_unit%i_natt) )
+         DO ji=1,file__copy_unit%i_natt
+            tl_att = att_copy(td_file%t_att(ji))
+            file__copy_unit%t_att(ji) = att_copy(tl_att)
          ENDDO
       ENDIF
 
+      ! clean
+      CALL att_clean(tl_att)
+
       ! copy dimg variable
-      td_file1%i_recl = td_file2%i_recl
-      td_file1%i_n0d  = td_file2%i_n0d
-      td_file1%i_n1d  = td_file2%i_n1d
-      td_file1%i_n2d  = td_file2%i_n2d
-      td_file1%i_n3d  = td_file2%i_n3d 
-      td_file1%i_rhd  = td_file2%i_rhd
+      file__copy_unit%i_recl = td_file%i_recl
+      file__copy_unit%i_n0d  = td_file%i_n0d
+      file__copy_unit%i_n1d  = td_file%i_n1d
+      file__copy_unit%i_n2d  = td_file%i_n2d
+      file__copy_unit%i_n3d  = td_file%i_n3d 
+      file__copy_unit%i_rhd  = td_file%i_rhd
       
       ! copy mpp variable
-      td_file1%i_pid  = td_file2%i_pid
-      td_file1%i_impp = td_file2%i_impp
-      td_file1%i_jmpp = td_file2%i_jmpp
-      td_file1%i_lci  = td_file2%i_lci
-      td_file1%i_lcj  = td_file2%i_lcj
-      td_file1%i_ldi  = td_file2%i_ldi
-      td_file1%i_ldj  = td_file2%i_ldj
-      td_file1%i_lei  = td_file2%i_lei
-      td_file1%i_lej  = td_file2%i_lej
-      td_file1%l_ctr  = td_file2%l_ctr
-      td_file1%l_use  = td_file2%l_use
-      td_file1%i_iind = td_file2%i_iind
-      td_file1%i_jind = td_file2%i_jind
-
-   END SUBROUTINE file__copy_unit
-   !> @endcode
+      file__copy_unit%i_pid  = td_file%i_pid
+      file__copy_unit%i_impp = td_file%i_impp
+      file__copy_unit%i_jmpp = td_file%i_jmpp
+      file__copy_unit%i_lci  = td_file%i_lci
+      file__copy_unit%i_lcj  = td_file%i_lcj
+      file__copy_unit%i_ldi  = td_file%i_ldi
+      file__copy_unit%i_ldj  = td_file%i_ldj
+      file__copy_unit%i_lei  = td_file%i_lei
+      file__copy_unit%i_lej  = td_file%i_lej
+      file__copy_unit%l_ctr  = td_file%l_ctr
+      file__copy_unit%l_use  = td_file%l_use
+      file__copy_unit%i_iind = td_file%i_iind
+      file__copy_unit%i_jind = td_file%i_jind
+
+   END FUNCTION file__copy_unit
    !-------------------------------------------------------------------
    !> @brief
-   !> This function copy file structure in another file
-   !> structure
+   !> This subroutine copy a array of file structure in another one
    !> @details 
-   !> file variable and attribute value are copied in a temporary table, 
+   !> file variable and attribute value are copied in a temporary array, 
    !> so input and output file structure value do not point on the same 
    !> "memory cell", and so on are independant. 
@@ -341,19 +402,25 @@
    !> @note new file is assume to be closed.
    !>
+   !> @warning do not use on the output of a function who create or read an
+   !> structure (ex: tl_file=file_copy(file_init()) is forbidden).
+   !> This will create memory leaks.
    !> @warning to avoid infinite loop, do not use any function inside 
    !> this subroutine
    !>   
    !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[out] td_file1  : file structure
-   !> @param[in] td_file2  : file structure
-   !-------------------------------------------------------------------
-   !> @code
-   SUBROUTINE file__copy_tab( td_file1, td_file2 )
+   !> - November, 2013- Initial Version
+   !> @date November, 2014
+   !>    - use function instead of overload assignment operator 
+   !> (to avoid memory leak)
+   !
+   !> @param[in] td_file  file structure
+   !> @return copy of input array of file structure
+   !-------------------------------------------------------------------
+   FUNCTION file__copy_arr( td_file )
       IMPLICIT NONE
       ! Argument
-      TYPE(TFILE), DIMENSION(:)                , INTENT(IN   )  :: td_file2
-      TYPE(TFILE), DIMENSION(SIZE(td_file2(:))), INTENT(  OUT) :: td_file1
+      TYPE(TFILE), DIMENSION(:)                , INTENT(IN   ) :: td_file
+      ! function
+      TYPE(TFILE), DIMENSION(SIZE(td_file(:))) :: file__copy_arr
 
       ! loop indices
@@ -361,26 +428,35 @@
       !----------------------------------------------------------------
 
-      DO ji=1,SIZE(td_file2(:))
-         td_file1(ji)=td_file2(ji)
+      DO ji=1,SIZE(td_file(:))
+         file__copy_arr(ji)=file_copy(td_file(ji))
       ENDDO
 
-   END SUBROUTINE file__copy_tab
-   !> @endcode
-   !-------------------------------------------------------------------
-   !> @brief This function initialise file structure.<br/>
+   END FUNCTION file__copy_arr
+   !-------------------------------------------------------------------
+   !> @brief This function initialize file structure.<br/> 
+   !> @details
    !> If cd_type is not specify, check if file name include '.nc' or
-   !> .'dimg'<br/>
+   !> '.dimg'<br/>
+   !> Optionally, you could specify:<br/>
+   !> - write mode (default .FALSE., ld_wrt)
+   !% - East-West overlap (id_ew)
+   !% - NEMO periodicity index (id_perio)
+   !% - NEMO pivot point index F(0),T(1) (id_pivot)
+   !> - grid type (default: 'ARAKAWA-C')
    !
    !> @details
    !
    !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[in] cd_file : file name
-   !> @param[in] cd_type : file type ('cdf', 'dimg')
-   !> @param[in] ld_wrt  : write mode (default .FALSE.)
+   !> - November, 2013- Initial Version
+   !
+   !> @param[in] cd_file   file name
+   !> @param[in] cd_type   file type ('cdf', 'dimg')
+   !> @param[in] ld_wrt    write mode (default .FALSE.)
+   !> @param[in] id_ew     east-west overlap
+   !> @param[in] id_perio  NEMO periodicity index
+   !> @param[in] id_pivot  NEMO pivot point index F(0),T(1)
+   !> @param[in] cd_grid   grid type (default 'ARAKAWA-C')
    !> @return file structure
    !-------------------------------------------------------------------
-   !> @code
    TYPE(TFILE) FUNCTION file_init( cd_file, cd_type, ld_wrt, &
    &                               id_ew, id_perio, id_pivot,&
@@ -397,5 +473,5 @@
 
       ! local variable
-      TYPE(TATT) :: tl_att
+      TYPE(TATT)  :: tl_att
       !----------------------------------------------------------------
 
@@ -404,12 +480,5 @@
 
       file_init%c_name=TRIM(ADJUSTL(cd_file))
-      CALL logger_trace("INIT: initialise file "//TRIM(file_init%c_name))
-
-      ! create some global attribute
-      tl_att=att_init("Conventions","CF-1.5")
-      CALL file_add_att(file_init,tl_att)
-     
-      tl_att=att_init("Grid",TRIM(file_init%c_grid))
-      CALL file_add_att(file_init,tl_att)
+      CALL logger_trace("FILE INIT: initialize file "//TRIM(file_init%c_name))
 
       ! check type
@@ -421,5 +490,5 @@
                file_init%c_type='dimg'
             CASE DEFAULT
-               CALL logger_error( " INIT: can't initialise file "//&
+               CALL logger_error( " FILE INIT: can't initialize file "//&
                &               TRIM(file_init%c_name)//" : type unknown " )
          END SELECT
@@ -427,4 +496,13 @@
          file_init%c_type=TRIM(file_get_type(cd_file))
       ENDIF
+
+      ! create some global attribute
+      IF( TRIM(file_init%c_type) == 'cdf' )THEN
+         tl_att=att_init("Conventions","CF-1.5")
+         CALL file_add_att(file_init,tl_att)
+      ENDIF
+     
+      tl_att=att_init("Grid",TRIM(file_init%c_grid))
+      CALL file_add_att(file_init,tl_att)
 
       IF( PRESENT(ld_wrt) )THEN
@@ -460,6 +538,8 @@
       ENDIF
 
+      ! clean
+      CALL att_clean(tl_att)
+
    END FUNCTION file_init
-   !> @endcode
    !-------------------------------------------------------------------
    !> @brief 
@@ -473,14 +553,14 @@
    !
    !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[in] cd_file : file name
+   !> - November, 2013- Initial Version
+   !
+   !> @param[in] cd_file   file name
    !> @return type of file
    !-------------------------------------------------------------------
-   !> @code
    CHARACTER(LEN=lc) FUNCTION file_get_type(cd_file)
       IMPLICIT NONE
       ! Argument      
       CHARACTER(LEN=*), INTENT(IN) :: cd_file
+
       !local variable
       CHARACTER(LEN=lc) :: cl_suffix
@@ -490,11 +570,11 @@
       SELECT CASE( TRIM(fct_lower(cl_suffix)) )
          CASE('.nc','.cdf')
-            CALL logger_debug(" GET TYPE: file "//TRIM(cd_file)//" is cdf")
+            CALL logger_debug(" FILE GET TYPE: file "//TRIM(cd_file)//" is cdf")
             file_get_type='cdf'
          CASE('.dimg')
-            CALL logger_debug(" GET TYPE: file "//TRIM(cd_file)//" is dimg" )
+            CALL logger_debug(" FILE GET TYPE: file "//TRIM(cd_file)//" is dimg" )
             file_get_type='dimg'
          CASE DEFAULT
-            CALL logger_warn(" GET TYPE: type unknown, we assume file: "//&
+            CALL logger_warn(" FILE GET TYPE: type unknown, we assume file: "//&
             &              TRIM(cd_file)//" is dimg ")
             file_get_type='dimg'
@@ -502,5 +582,4 @@
 
    END FUNCTION file_get_type
-   !> @endcode
    !-------------------------------------------------------------------
    !> @brief This function check if variable dimension to be used 
@@ -510,11 +589,10 @@
    !
    !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[in] td_file : file structure
-   !> @param[in] td_var : variable structure
-   !> @return dimension of variable and file structure agree (or not)
-   !-------------------------------------------------------------------
-   !> @code
+   !> - November, 2013- Initial Version
+   !
+   !> @param[in] td_file   file structure
+   !> @param[in] td_var    variable structure
+   !> @return true if dimension of variable and file structure agree
+   !-------------------------------------------------------------------
    LOGICAL FUNCTION file_check_var_dim(td_file, td_var)
       IMPLICIT NONE
@@ -524,5 +602,8 @@
 
       ! local variable
-      INTEGER(i4) :: il_ndim
+      CHARACTER(LEN=lc) :: cl_dim
+      LOGICAL           :: ll_error
+
+      INTEGER(i4) :: il_ind
 
       ! loop indices
@@ -530,7 +611,21 @@
       !----------------------------------------------------------------
       file_check_var_dim=.TRUE.
+
       ! check used dimension 
-      IF( ANY( td_var%t_dim(:)%l_use .AND. &
-      &        td_var%t_dim(:)%i_len /= td_file%t_dim(:)%i_len) )THEN
+      ll_error=.FALSE.
+      DO ji=1,ip_maxdim
+         il_ind=dim_get_index( td_file%t_dim(:), &
+         &                     TRIM(td_var%t_dim(ji)%c_name), &
+         &                     TRIM(td_var%t_dim(ji)%c_sname))
+         IF( il_ind /= 0 )THEN
+         IF( td_var%t_dim(ji)%l_use  .AND. &
+         &   td_file%t_dim(il_ind)%l_use .AND. &
+         &   td_var%t_dim(ji)%i_len /= td_file%t_dim(il_ind)%i_len )THEN
+            ll_error=.TRUE.
+         ENDIF
+         ENDIF
+      ENDDO
+
+      IF( ll_error )THEN
 
          file_check_var_dim=.FALSE.
@@ -542,30 +637,37 @@
 
 
-         CALL logger_debug( &
-         &  " file dimension: "//TRIM(fct_str(td_file%i_ndim))//&
-         &  " variable dimension: "//TRIM(fct_str(td_var%i_ndim)) )
-         il_ndim=MIN(td_var%i_ndim, td_file%i_ndim )
-         DO ji = 1, il_ndim
-            CALL logger_debug( &
-            &  " FILE CHECK VAR DIM: for dimension "//&
-            &  TRIM(td_file%t_dim(ji)%c_name)//&
-            &  ", file length: "//&
-            &  TRIM(fct_str(td_file%t_dim(ji)%i_len))//&
-            &  ", variable length: "//&
-            &  TRIM(fct_str(td_var%t_dim(ji)%i_len))//&
-            &  ", variable used "//TRIM(fct_str(td_var%t_dim(ji)%l_use)))
+         cl_dim='(/'
+         DO ji = 1, td_file%i_ndim
+            IF( td_file%t_dim(ji)%l_use )THEN
+               cl_dim=TRIM(cl_dim)//&
+               &  TRIM(fct_upper(td_file%t_dim(ji)%c_sname))//':'//&
+               &  TRIM(fct_str(td_file%t_dim(ji)%i_len))//','
+            ENDIF
          ENDDO
+         cl_dim=TRIM(cl_dim)//'/)'
+         CALL logger_debug( " file dimension: "//TRIM(cl_dim) )
+
+         cl_dim='(/'
+         DO ji = 1, td_var%i_ndim
+            IF( td_var%t_dim(ji)%l_use )THEN
+               cl_dim=TRIM(cl_dim)//&
+               &  TRIM(fct_upper(td_var%t_dim(ji)%c_sname))//':'//&
+               &  TRIM(fct_str(td_var%t_dim(ji)%i_len))//','
+            ENDIF
+         ENDDO
+         cl_dim=TRIM(cl_dim)//'/)'
+         CALL logger_debug( " variable dimension: "//TRIM(cl_dim) )
+
       ELSE
-         IF( ANY( td_var%t_dim(:)%l_use .AND. &
-         &  .NOT. td_file%t_dim(:)%l_use ) )THEN
-            
-            CALL logger_info("FILE CHECK VAR DIM: variable use more dimension "//&
-            &  " than file do until now. file dimension use will change.")
-
-         ENDIF
+
+         IF( td_var%i_ndim >  td_file%i_ndim )THEN
+            CALL logger_info("FILE CHECK VAR DIM: variable "//&
+            &  TRIM(td_var%c_name)//" use more dimension than file "//&
+            &  TRIM(td_file%c_name)//" do until now.")
+         ENDIF
+
       ENDIF
 
    END FUNCTION file_check_var_dim
-   !> @endcode   
    !-------------------------------------------------------------------
    !> @brief This subroutine add a variable structure in a file structure.<br/>
@@ -577,14 +679,12 @@
    !
    !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[inout] td_file : file structure
-   !> @param[in] td_var : variable structure
-   !
-   !> @todo 
-   !> - check dimension order
-   !> - voir pour ajouter variable avec plus de dim que deja presente dans fichier
-   !-------------------------------------------------------------------
-   !> @code
+   !> - November, 2013- Initial Version
+   !> @date September, 2014
+   !> - add dimension to file if need be
+   !> - do not reorder dimension from variable, before put in file
+   !
+   !> @param[inout] td_file   file structure
+   !> @param[in] td_var       variable structure
+   !-------------------------------------------------------------------
    SUBROUTINE file_add_var(td_file, td_var)
       IMPLICIT NONE
@@ -596,6 +696,5 @@
       ! local variable
       INTEGER(i4) :: il_status
-      INTEGER(i4) :: il_varid
-      INTEGER(i4) :: il_rec
+      !INTEGER(i4) :: il_rec
       INTEGER(i4) :: il_ind
 
@@ -606,9 +705,8 @@
       !----------------------------------------------------------------
       ! check if file opened
-      !IF( TRIM(td_file%c_name) == "unknown" )THEN
       IF( TRIM(td_file%c_name) == '' )THEN
 
-         CALL logger_error( " ADD VAR: structure file unknown" )
-         CALL logger_debug( " ADD VAR: you should have used file_init before "//&
+         CALL logger_error( " FILE ADD VAR: structure file unknown" )
+         CALL logger_debug( " FILE ADD VAR: you should have used file_init before "//&
          & "running file_add_var" )
 
@@ -617,17 +715,17 @@
          IF( TRIM(td_var%c_name) == '' .AND. &
          &   TRIM(td_var%c_stdname) == '' )THEN
-            CALL logger_error(" ADD VAR: variable not define ")
+            CALL logger_error(" FILE ADD VAR: variable without name ")
          ELSE
             ! check if variable already in file structure
-            il_varid=0
+            il_ind=0
             IF( ASSOCIATED(td_file%t_var) )THEN
-               il_varid=var_get_id( td_file%t_var(:), td_var%c_name,   &
-               &                                      td_var%c_stdname )
+               il_ind=var_get_index( td_file%t_var(:), td_var%c_name,   &
+               &                                       td_var%c_stdname )
             ENDIF
 
-            IF( il_varid /= 0 )THEN
+            IF( il_ind /= 0 )THEN
 
                CALL logger_error( &
-               &  " ADD VAR: variable "//TRIM(td_var%c_name)//&
+               &  " FILE ADD VAR: variable "//TRIM(td_var%c_name)//&
                &  ", standard name "//TRIM(td_var%c_stdname)//&
                &  ", already in file "//TRIM(td_file%c_name) )
@@ -641,31 +739,37 @@
             ELSE
 
-               CALL logger_info( &
-               &  " ADD VAR: add variable "//TRIM(td_var%c_name)//&
+               CALL logger_trace( &
+               &  " FILE ADD VAR: add variable "//TRIM(td_var%c_name)//&
                &  ", standard name "//TRIM(td_var%c_stdname)//&
                &  ", in file "//TRIM(td_file%c_name) )
 
-               ! if none, force to use variable dimension
-               IF( ALL( .NOT. td_file%t_dim(:)%l_use) )THEN
-                  td_file%t_dim(:)=td_var%t_dim(:)
-               ENDIF
-
                ! check used dimension 
                IF( file_check_var_dim(td_file, td_var) )THEN
 
+                  ! update dimension if need be
+                  DO ji=1,ip_maxdim
+                     IF( td_var%t_dim(ji)%l_use .AND. &
+                     &   .NOT. td_file%t_dim(ji)%l_use )THEN
+                        CALL file_add_dim(td_file,td_var%t_dim(ji))
+                     ENDIF
+                  ENDDO
+
+                  ! get index of new variable
                   SELECT CASE(td_var%i_ndim)
                      CASE(0)
                         il_ind=td_file%i_n0d+1
-                        il_rec=0
+                        !il_rec=0
                      CASE(1)
                         il_ind=td_file%i_n0d+td_file%i_n1d+1
-                        il_rec=1
+                        !il_rec=1
                      CASE(2)
                         il_ind=td_file%i_n0d+td_file%i_n1d+td_file%i_n2d+1
-                        il_rec=1
+                        !il_rec=1
                      CASE(3,4)
                         il_ind=td_file%i_n0d+td_file%i_n1d+td_file%i_n2d+td_file%i_n3d+1
-                        il_rec=td_file%t_dim(3)%i_len
+                        !il_rec=td_file%t_dim(3)%i_len
                   END SELECT
+                  CALL logger_info( &
+                     &  " FILE ADD VAR: variable index "//TRIM(fct_str(il_ind)))
 
                   IF( td_file%i_nvar > 0 )THEN
@@ -675,5 +779,5 @@
 
                         CALL logger_error( &
-                        &  " ADD VAR: not enough space to put variables "//&
+                        &  " FILE ADD VAR: not enough space to put variables "//&
                         &  "from "//TRIM(td_file%c_name)//&
                         &  " in variable structure")
@@ -682,7 +786,8 @@
 
                         ! save temporary variable of file structure
-                        tl_var(:)=td_file%t_var(:)
-
-                        DEALLOCATE( td_file%t_var )
+                        tl_var(:)=var_copy(td_file%t_var(:))
+
+                        CALL var_clean( td_file%t_var(:) )
+                        DEALLOCATE(td_file%t_var)
                         ALLOCATE( td_file%t_var(td_file%i_nvar+1), &
                         &         stat=il_status)
@@ -690,5 +795,5 @@
 
                            CALL logger_error( &
-                           &  " ADD VAR: not enough space to put variable "//&
+                           &  " FILE ADD VAR: not enough space to put variable "//&
                            &  "in file structure "//TRIM(td_file%c_name) )
 
@@ -697,18 +802,16 @@
                         ! copy variable in file before
                         ! variable with less than or equal dimension that new variable
-                        td_file%t_var( 1:il_ind-1 ) = tl_var( 1:il_ind-1 )
-
-                        ! variable with greater dimension than new variable
-                        td_file%t_var( il_ind+1 : td_file%i_nvar+1 ) = &
-                        &                 tl_var( il_ind : td_file%i_nvar )
-
-                        ! update id
-                        td_file%t_var( il_ind+1 : td_file%i_nvar+1 )%i_id = &
-                        &           tl_var( il_ind : td_file%i_nvar )%i_id + 1
-
-                        ! update record index
-                        td_file%t_var( il_ind+1 : td_file%i_nvar+1 )%i_rec = &
-                        & tl_var( il_ind : td_file%i_nvar )%i_rec + il_rec
-
+                        IF( il_ind > 1 )THEN
+                           td_file%t_var( 1:il_ind-1 ) = var_copy(tl_var(1:il_ind-1))
+                        ENDIF
+
+                        IF( il_ind < td_file%i_nvar )THEN
+                           ! variable with more dimension than new variable
+                           td_file%t_var( il_ind+1 : td_file%i_nvar+1 ) = &
+                           &        var_copy( tl_var(il_ind : td_file%i_nvar) )
+                        ENDIF
+
+                        ! clean
+                        CALL var_clean(tl_var(:))
                         DEALLOCATE(tl_var)
                      ENDIF
@@ -717,4 +820,5 @@
                   ! no variable in file structure
                      IF( ASSOCIATED(td_file%t_var) )THEN
+                        CALL var_clean(td_file%t_var(:))
                         DEALLOCATE(td_file%t_var)
                      ENDIF
@@ -723,5 +827,5 @@
 
                         CALL logger_error( &
-                        &  " ADD VAR: not enough space to put variable "//&
+                        &  " FILE ADD VAR: not enough space to put variable "//&
                         &  "in file structure "//TRIM(td_file%c_name) )
 
@@ -730,13 +834,14 @@
                   ENDIF
 
+                  ! add new variable in array of variable
                   ALLOCATE( tl_var(1), stat=il_status )
                   IF(il_status /= 0 )THEN
 
                      CALL logger_error( &
-                     &  " ADD VAR: not enough space to put variables from "//&
+                     &  " FILE ADD VAR: not enough space to put variables from "//&
                      &  TRIM(td_var%c_name)//" in variable structure")
 
                   ELSE
-                     tl_var(1)=td_var
+                     tl_var(1)=var_copy(td_var)
 
                      ! update dimension name in new variable
@@ -744,5 +849,5 @@
                   
                      ! add new variable
-                     td_file%t_var(il_ind)=tl_var(1)
+                     td_file%t_var(il_ind)=var_copy(tl_var(1))
 
                      ! update number of variable
@@ -755,21 +860,10 @@
                         CASE(2)
                            td_file%i_n2d=td_file%i_n2d+1
-                        CASE(3)
+                        CASE(3,4)
                            td_file%i_n3d=td_file%i_n3d+1
                      END SELECT
 
                      ! update variable id
-                     td_file%t_var(il_ind)%i_id=il_ind
-
-                     ! update record header index
-                     td_file%i_rhd=td_file%i_rhd+il_rec
-
-                     ! update record index
-                     IF( il_ind > 1 )THEN
-                        td_file%t_var(il_ind)%i_rec = &
-                        &     td_file%t_var(il_ind-1)%i_rec+il_rec
-                     ELSE
-                        td_file%t_var(il_ind)%i_rec = il_rec
-                     ENDIF
+                     td_file%t_var(il_ind)%i_id=var_get_unit(td_file%t_var(:))
 
                      ! update dimension used
@@ -780,11 +874,12 @@
                         ENDIF
                      ENDDO
-                     CALL dim_reorder(td_file%t_dim(:))
+
                      ! update number of dimension
                      td_file%i_ndim=COUNT(td_file%t_dim(:)%l_use)
 
-                     DEALLOCATE( tl_var )
+                     ! clean
+                     CALL var_clean( tl_var(:) )
+                     DEALLOCATE(tl_var)
                   ENDIF
-
                ENDIF
             ENDIF
@@ -793,18 +888,14 @@
 
    END SUBROUTINE file_add_var
-   !> @endcode
    !-------------------------------------------------------------------
    !> @brief This subroutine delete a variable structure 
-   !> in file structure.
-   !
-   !> @details
-   !
-   !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[inout] td_file : file structure
-   !> @param[in] cd_name : variable name or standard name
-   !-------------------------------------------------------------------
-   !> @code
+   !> in file structure, given variable name or standard name.
+   !
+   !> @author J.Paul
+   !> - November, 2013- Initial Version
+   !
+   !> @param[inout] td_file   file structure
+   !> @param[in] cd_name      variable name or standard name
+   !-------------------------------------------------------------------
    SUBROUTINE file__del_var_name(td_file, cd_name )
       IMPLICIT NONE
@@ -815,5 +906,5 @@
 
       ! local variable
-      INTEGER(i4)       :: il_varid
+      INTEGER(i4)       :: il_ind
       !----------------------------------------------------------------
 
@@ -821,6 +912,6 @@
       IF( TRIM(td_file%c_name) == '' )THEN
 
-         CALL logger_error( " DEL VAR NAME: file structure unknown ")
-         CALL logger_debug( " DEL VAR NAME: you should have used file_init before "//&
+         CALL logger_error( " FILE DEL VAR NAME: file structure unknown ")
+         CALL logger_debug( " FILE DEL VAR NAME: you should have used file_init before "//&
          & "running file_del_var" )
 
@@ -829,17 +920,18 @@
          IF( td_file%i_nvar /= 0 )THEN
 
-            ! get the variable id, in file variable structure
-            il_varid=0
+            ! get the variable index, in file variable structure
+            il_ind=0
             IF( ASSOCIATED(td_file%t_var) )THEN
-               il_varid=var_get_id(td_file%t_var(:), cd_name )
+               il_ind=var_get_index(td_file%t_var(:), cd_name )
             ENDIF
-            IF( il_varid /= 0 )THEN
+
+            IF( il_ind /= 0 )THEN
    
-               CALL file_del_var(td_file, td_file%t_var(il_varid))
+               CALL file_del_var(td_file, td_file%t_var(il_ind))
 
             ELSE
 
                CALL logger_warn( &
-               &  " DEL VAR NAME: there is no variable with name or "//&
+               &  " FILE DEL VAR NAME: there is no variable with name or "//&
                &  "standard name "//TRIM(cd_name)//" in file "//&
                &  TRIM(td_file%c_name))
@@ -848,6 +940,7 @@
 
          ELSE
-            CALL logger_debug( " DEL VAR NAME: no variable associated to file "//&
-            &               TRIM(td_file%c_name) )
+            CALL logger_debug( " FILE DEL VAR NAME: "//&
+            &        "no variable associated to file "//&
+            &        TRIM(td_file%c_name) )
          ENDIF
 
@@ -855,20 +948,14 @@
 
    END SUBROUTINE file__del_var_name
-   !> @endcode
    !-------------------------------------------------------------------
    !> @brief This subroutine delete a variable structure 
    !> in file structure, given variable structure.
-   !
-   !> @details
-   !
-   !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[inout] td_file : file structure
-   !> @param[in] td_var : variable structure
-   !> @todo
-   !> - verifier pose pas de souci de ne pas modifier id
-   !-------------------------------------------------------------------
-   !> @code
+   !>
+   !> @author J.Paul
+   !> - November, 2013- Initial Version
+   !>
+   !> @param[inout] td_file   file structure
+   !> @param[in] td_var       variable structure
+   !-------------------------------------------------------------------
    SUBROUTINE file__del_var_str(td_file, td_var)
       IMPLICIT NONE
@@ -880,5 +967,5 @@
       ! local variable
       INTEGER(i4) :: il_status
-      INTEGER(i4) :: il_varid
+      INTEGER(i4) :: il_ind
       INTEGER(i4) :: il_rec
       TYPE(TVAR), DIMENSION(:), ALLOCATABLE :: tl_var
@@ -889,25 +976,204 @@
 
       ! check if file opened
-      !IF( TRIM(td_file%c_name) == "unknown" )THEN
       IF( TRIM(td_file%c_name) == '' )THEN
 
-         CALL logger_error( " DEL VAR: file structure unknown ")
-         CALL logger_debug( " DEL VAR: you should have used file_init before "//&
-         & "running file_del_var" )      
+         CALL logger_error( " FILE DEL VAR: file structure unknown ")
+         CALL logger_debug( " FILE DEL VAR: you should have used "//&
+         &  "file_init before running file_del_var" )      
 
       ELSE
 
-         ! check if variable already in file structure
-         il_varid=var_get_id(td_file%t_var(:), td_var%c_name, td_var%c_stdname )
-         IF( il_varid == 0 )THEN
-
+         ! check if variable is member of a file
+         IF( td_var%l_file )THEN
+            CALL logger_warn( &
+            &  " FILE DEL VAR: variable "//TRIM(td_var%c_name)//&
+            &  ", belong to file "//TRIM(td_file%c_name)//&
+            &  " and can not be removed.")
+         ELSE
+            ! check if variable already in file structure
+            il_ind=0
+            IF( ASSOCIATED(td_file%t_var) )THEN
+               il_ind=var_get_index( td_file%t_var(:), td_var%c_name, &
+               &                                       td_var%c_stdname )
+            ENDIF
+
+            IF( il_ind == 0 )THEN
+
+               CALL logger_warn( "FILE DEL VAR: no variable "//&
+               &     TRIM(td_var%c_name)//", in file "//TRIM(td_file%c_name) )
+
+               DO ji=1,td_file%i_nvar
+                  CALL logger_debug( "FILE DEL VAR: in file "//&
+                  &  TRIM(td_file%t_var(ji)%c_name)//", standard name "//&
+                  &  TRIM(td_file%t_var(ji)%c_stdname) )
+               ENDDO
+
+            ELSE
+               
+               CALL logger_trace( "FILE DEL VAR: delete variable "//&
+               &  TRIM(td_var%c_name)//", from file "//TRIM(td_file%c_name) )
+
+               ALLOCATE( tl_var(td_file%i_nvar-1), stat=il_status )
+               IF(il_status /= 0 )THEN
+
+                  CALL logger_error( &
+                  &  " FILE DEL VAR: not enough space to put variables from "//&
+                  &  TRIM(td_file%c_name)//" in temporary variable structure")
+
+               ELSE
+
+                  ! save temporary variable's file structure
+                  IF( il_ind > 1 )THEN
+                     tl_var(1:il_ind-1)=var_copy(td_file%t_var(1:il_ind-1))
+                  ENDIF
+
+                  IF( il_ind < td_file%i_nvar )THEN
+                     tl_var(il_ind:)=var_copy(td_file%t_var(il_ind+1:))
+                  ENDIF
+
+                  ! new number of variable in file
+                  td_file%i_nvar=td_file%i_nvar-1
+
+                  SELECT CASE(td_var%i_ndim)
+                     CASE(0)
+                        td_file%i_n0d=td_file%i_n0d-1
+                        il_rec=0
+                     CASE(1)
+                        td_file%i_n1d=td_file%i_n1d-1
+                        il_rec=1
+                     CASE(2)
+                        td_file%i_n2d=td_file%i_n2d-1
+                        il_rec=1
+                     CASE(3,4)
+                        td_file%i_n3d=td_file%i_n3d-1
+                        il_rec=td_file%t_dim(3)%i_len
+                  END SELECT
+
+                  CALL var_clean( td_file%t_var(:) )
+                  DEALLOCATE(td_file%t_var)
+
+                  IF( td_file%i_nvar > 0 )THEN
+                     ALLOCATE( td_file%t_var(td_file%i_nvar), stat=il_status )
+                     IF(il_status /= 0 )THEN
+
+                        CALL logger_error( " FILE DEL VAR: not enough space"//&
+                        &  "to put variables in file structure "//&
+                        &  TRIM(td_file%c_name) )
+
+                     ENDIF
+
+                     ! copy attribute in file before
+                     td_file%t_var(:)=var_copy(tl_var(:))
+
+                     ! update dimension used
+                     td_file%t_dim(:)%l_use=.FALSE.
+                     DO ji=1,ip_maxdim
+                        IF( ANY(td_file%t_var(:)%t_dim(ji)%l_use) )THEN
+                           td_file%t_dim(ji)%l_use=.TRUE.
+                        ENDIF
+                     ENDDO
+
+                     ! update number of dimension
+                     td_file%i_ndim=COUNT(td_file%t_dim(:)%l_use)
+
+                  ENDIF
+
+                  ! clean
+                  CALL var_clean(tl_var(:))
+                  DEALLOCATE(tl_var)
+                  
+               ENDIF 
+            ENDIF
+         ENDIF
+      ENDIF
+
+   END SUBROUTINE file__del_var_str
+   !-------------------------------------------------------------------
+   !> @brief This subroutine overwrite variable structure 
+   !> in file structure.
+   !
+   !> @warning change variable id in file structure.
+   !
+   !> @author J.Paul
+   !> - November, 2013- Initial Version
+   !
+   !> @param[inout] td_file   file structure
+   !> @param[in] td_var       variable structure
+   !-------------------------------------------------------------------
+   SUBROUTINE file_move_var(td_file, td_var)
+      IMPLICIT NONE
+
+      ! Argument      
+      TYPE(TFILE), INTENT(INOUT) :: td_file
+      TYPE(TVAR),  INTENT(IN)    :: td_var
+
+      ! local variable
+      TYPE(TVAR) :: tl_var
+      !----------------------------------------------------------------
+
+      ! copy variable
+      tl_var=var_copy(td_var)
+
+      ! remove variable with same name or standard name
+      CALL file_del_var(td_file, tl_var)
+
+      ! add new variable
+      CALL file_add_var(td_file, tl_var)
+
+      ! clean
+      CALL var_clean(tl_var)
+
+   END SUBROUTINE file_move_var
+   !-------------------------------------------------------------------
+   !> @brief This subroutine add a global attribute 
+   !> in a file structure.<br/>
+   !> Do not overwrite, if attribute already in file structure.
+   !
+   !> @author J.Paul
+   !> - November, 2013- Initial Version
+   !
+   !> @param[inout] td_file   file structure
+   !> @param[in] td_att       attribute structure
+   !-------------------------------------------------------------------
+   SUBROUTINE file_add_att(td_file, td_att)
+      IMPLICIT NONE
+
+      ! Argument      
+      TYPE(TFILE), INTENT(INOUT) :: td_file
+      TYPE(TATT),  INTENT(IN)    :: td_att
+
+      ! local variable
+      INTEGER(i4) :: il_status
+      INTEGER(i4) :: il_ind
+      TYPE(TATT), DIMENSION(:), ALLOCATABLE :: tl_att
+
+      ! loop indices
+      INTEGER(i4) :: ji
+      !----------------------------------------------------------------
+
+      ! check if file opened
+      IF( TRIM(td_file%c_name) == '' )THEN
+
+         CALL logger_error( " FILE ADD ATT: file structure unknown ")
+         CALL logger_debug( " FILE ADD ATT: you should have used file_init before "//&
+         & "running file_add_att" )      
+
+      ELSE
+
+         ! check if attribute already in file structure
+         il_ind=0
+         IF( ASSOCIATED(td_file%t_att) )THEN
+            il_ind=att_get_index( td_file%t_att(:), td_att%c_name )
+         ENDIF
+
+         IF( il_ind /= 0 )THEN
+         
             CALL logger_error( &
-            &  " DEL VAR: no variable "//TRIM(td_var%c_name)//&
-            &  ", in file "//TRIM(td_file%c_name) )
-
-            DO ji=1,td_file%i_nvar
+            &  " FILE ADD ATT: attribute "//TRIM(td_att%c_name)//&
+            &  ", already in file "//TRIM(td_file%c_name) )
+
+            DO ji=1,td_file%i_natt
                CALL logger_debug( &
-               &  " DEL VAR: in file "//TRIM(td_file%t_var(ji)%c_name)//&
-               &  ", standard name "//TRIM(td_file%t_var(ji)%c_stdname) )
+               &  " FILE ADD ATT: in file "//TRIM(td_file%t_att(ji)%c_name) )
             ENDDO
 
@@ -915,189 +1181,5 @@
             
             CALL logger_trace( &
-            &  " DEL VAR: delete variable "//TRIM(td_var%c_name)//&
-            &  ", from file "//TRIM(td_file%c_name) )
-
-            ALLOCATE( tl_var(td_file%i_nvar-1), stat=il_status )
-            IF(il_status /= 0 )THEN
-
-               CALL logger_error( &
-               &  " DEL VAR: not enough space to put variables from "//&
-               &  TRIM(td_file%c_name)//" in temporary variable structure")
-
-            ELSE
-
-               ! save temporary variable's file structure
-               tl_var(1:il_varid-1)=td_file%t_var(1:il_varid-1)
-               tl_var(il_varid:)=td_file%t_var(il_varid+1:)
-
-               ! new number of variable in file
-               td_file%i_nvar=td_file%i_nvar-1
-
-               SELECT CASE(td_var%i_ndim)
-                  CASE(0)
-                     td_file%i_n0d=td_file%i_n0d-1
-                     il_rec=0
-                  CASE(1)
-                     td_file%i_n1d=td_file%i_n1d-1
-                     il_rec=1
-                  CASE(2)
-                     td_file%i_n2d=td_file%i_n2d-1
-                     il_rec=1
-                  CASE(3,4)
-                     td_file%i_n3d=td_file%i_n3d-1
-                     il_rec=td_file%t_dim(3)%i_len
-               END SELECT
-
-               DEALLOCATE( td_file%t_var )
-
-               IF( td_file%i_nvar > 0 )THEN
-                  ALLOCATE( td_file%t_var(td_file%i_nvar), stat=il_status )
-                  IF(il_status /= 0 )THEN
-
-                     CALL logger_error( &
-                     &  " DEL VAR: not enough space to put variables "//&
-                     &  "in file structure "//TRIM(td_file%c_name) )
-
-                  ENDIF
-
-                  ! copy attribute in file before
-                  td_file%t_var(:)=tl_var(:)
-
-                  ! update record header index
-                  td_file%i_rhd = td_file%i_rhd - il_rec
-
-!                  ! update id
-!                  td_file%t_var( il_varid : td_file%i_nvar )%i_id = &
-!                  &     td_file%t_var( il_varid : td_file%i_nvar )%i_id - 1
-
-                  ! update record index
-                  td_file%t_var( il_varid : td_file%i_nvar )%i_rec = &
-                  &     td_file%t_var( il_varid : td_file%i_nvar )%i_rec - il_rec
-
-                  ! update dimension used
-                  td_file%t_dim(:)%l_use=.FALSE.
-                  DO ji=1,ip_maxdim
-                     IF( ANY(td_file%t_var(:)%t_dim(ji)%l_use) )THEN
-                        td_file%t_dim(ji)%l_use=.TRUE.
-                     ENDIF
-                  ENDDO
-                  CALL dim_reorder(td_file%t_dim(:))
-                  ! update number of dimension
-                  td_file%i_ndim=COUNT(td_file%t_dim(:)%l_use)
-
-               ENDIF
-               DEALLOCATE(tl_var)
-               
-            ENDIF 
-         ENDIF
-      ENDIF
-
-   END SUBROUTINE file__del_var_str
-   !> @endcode
-   !-------------------------------------------------------------------
-   !> @brief This subroutine overwrite variable structure 
-   !> in file structure.
-   !
-   !> @details
-   !
-   !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[inout] td_file : file structure
-   !> @param[in] td_var : variable structure
-   !> @todo
-   !> - check independance td_var (cf move dim variable)
-   !-------------------------------------------------------------------
-   !> @code
-   SUBROUTINE file_move_var(td_file, td_var)
-      IMPLICIT NONE
-
-      ! Argument      
-      TYPE(TFILE), INTENT(INOUT) :: td_file
-      TYPE(TVAR),  INTENT(IN)    :: td_var
-
-      ! local variable
-      TYPE(TVAR) :: tl_var
-      INTEGER(i4):: il_varid
-      !----------------------------------------------------------------
-
-      ! copy variable
-      tl_var=td_var
-
-      IF( ASSOCIATED(td_file%t_var) )THEN
-         il_varid=var_get_id(td_file%t_var(:),TRIM(tl_var%c_name))
-         IF( il_varid /= 0 )THEN
-            ! remove variable with same name or standard name
-            CALL file_del_var(td_file, tl_var)
-         ENDIF
-      ENDIF
-
-      ! add new variable
-      CALL file_add_var(td_file, tl_var)
-
-   END SUBROUTINE file_move_var
-   !> @endcode
-   !-------------------------------------------------------------------
-   !> @brief This subroutine add a global attribute 
-   !> in a file structure.<br/>
-   !> Do not overwrite, if attribute already in file structure.
-   !
-   !> @details
-   !
-   !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[inout] td_file : file structure
-   !> @param[in] td_att : attribute structure
-   !-------------------------------------------------------------------
-   !> @code
-   SUBROUTINE file_add_att(td_file, td_att)
-      IMPLICIT NONE
-
-      ! Argument      
-      TYPE(TFILE), INTENT(INOUT) :: td_file
-      TYPE(TATT),  INTENT(IN)    :: td_att
-
-      ! local variable
-      INTEGER(i4) :: il_status
-      INTEGER(i4) :: il_attid
-      TYPE(TATT), DIMENSION(:), ALLOCATABLE :: tl_att
-
-      ! loop indices
-      INTEGER(i4) :: ji
-      !----------------------------------------------------------------
-
-      ! check if file opened
-      !IF( TRIM(td_file%c_name) == "unknown" )THEN
-      IF( TRIM(td_file%c_name) == '' )THEN
-
-         CALL logger_error( " ADD ATT: file structure unknown ")
-         CALL logger_debug( " ADD ATT: you should have used file_init before "//&
-         & "running file_add_att" )      
-
-      ELSE
-
-         ! check if attribute already in file structure
-         il_attid=0
-         IF( ASSOCIATED(td_file%t_att) )THEN
-            il_attid=att_get_id( td_file%t_att(:), td_att%c_name )
-         ENDIF
-
-         IF( il_attid /= 0 )THEN
-         
-            CALL logger_error( &
-            &  " ADD ATT: attribute "//TRIM(td_att%c_name)//&
-            &  ", already in file "//TRIM(td_file%c_name) )
-            CALL logger_flush()
-
-            DO ji=1,td_file%i_natt
-               CALL logger_debug( &
-               &  " ADD ATT: in file "//TRIM(td_file%t_att(ji)%c_name) )
-            ENDDO
-
-         ELSE
-            
-            CALL logger_debug( &
-            &  " ADD ATT: add attribute "//TRIM(td_att%c_name)//&
+            &  " FILE ADD ATT: add attribute "//TRIM(td_att%c_name)//&
             &  ", in file "//TRIM(td_file%c_name) )
 
@@ -1108,5 +1190,5 @@
 
                   CALL logger_error( &
-                  &  " ADD ATT: not enough space to put attributes from "//&
+                  &  " FILE ADD ATT: not enough space to put attributes from "//&
                   &  TRIM(td_file%c_name)//" in temporary attribute structure")
 
@@ -1114,12 +1196,13 @@
 
                   ! save temporary global attribute's file structure
-                  tl_att(:)=td_file%t_att(:)
-
-                  DEALLOCATE( td_file%t_att )
+                  tl_att(:)=att_copy(td_file%t_att(:))
+
+                  CALL att_clean( td_file%t_att(:) )
+                  DEALLOCATE(td_file%t_att)
                   ALLOCATE( td_file%t_att(td_file%i_natt+1), stat=il_status )
                   IF(il_status /= 0 )THEN
 
                      CALL logger_error( &
-                     &  " ADD ATT: not enough space to put attributes "//&
+                     &  " FILE ADD ATT: not enough space to put attributes "//&
                      &  "in file structure "//TRIM(td_file%c_name) )
 
@@ -1127,28 +1210,29 @@
 
                   ! copy attribute in file before
-                  td_file%t_att(1:td_file%i_natt)=tl_att(:)
-
+                  td_file%t_att(1:td_file%i_natt)=att_copy(tl_att(:))
+
+                   ! clean
+                  CALL att_clean(tl_att(:))
                   DEALLOCATE(tl_att)
+
                ENDIF
             ELSE
             ! no attribute in file structure
                IF( ASSOCIATED(td_file%t_att) )THEN
+                  CALL att_clean(td_file%t_att(:))
                   DEALLOCATE(td_file%t_att)
                ENDIF
-               CALL logger_debug(" natt "//TRIM(fct_str(td_file%i_natt)) )
+
                ALLOCATE( td_file%t_att(td_file%i_natt+1), stat=il_status )
                IF(il_status /= 0 )THEN
 
                   CALL logger_error( &
-                  &  " ADD ATT: not enough space to put attributes "//&
+                  &  " FILE ADD ATT: not enough space to put attributes "//&
                   &  "in file structure "//TRIM(td_file%c_name) )
 
                ENDIF
             ENDIF
-            ! add new attributes
-            td_file%t_att(td_file%i_natt+1)=td_att
-
-            ! update attributes id
-            td_file%t_att(td_file%i_natt+1)%i_id=td_file%i_natt+1
+            ! add new attribute
+            td_file%t_att(td_file%i_natt+1)=att_copy(td_att)
 
             ! update number of attribute
@@ -1158,18 +1242,14 @@
 
    END SUBROUTINE file_add_att
-   !> @endcode
-   !-------------------------------------------------------------------
-   !> @brief This subroutine delete a variable structure 
-   !> in file structure.
-   !
-   !> @details
-   !
-   !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[inout] td_file : file structure
-   !> @param[in] cd_name : variable name or standard name
-   !-------------------------------------------------------------------
-   !> @code
+   !-------------------------------------------------------------------
+   !> @brief This subroutine delete a global attribute structure 
+   !> in file structure, given attribute name.
+   !
+   !> @author J.Paul
+   !> - November, 2013- Initial Version
+   !
+   !> @param[inout] td_file   file structure
+   !> @param[in] cd_name      attribute name
+   !-------------------------------------------------------------------
    SUBROUTINE file__del_att_name(td_file, cd_name )
       IMPLICIT NONE
@@ -1180,5 +1260,5 @@
 
       ! local variable
-      INTEGER(i4)       :: il_attid
+      INTEGER(i4)       :: il_ind
       !----------------------------------------------------------------
 
@@ -1186,7 +1266,7 @@
       IF( TRIM(td_file%c_name) == '' )THEN
 
-         CALL logger_error( " DEL ATT NAME: file structure unknown ")
-         CALL logger_debug( " DEL ATT NAME: you should have used file_init before "//&
-         & "running file_del_var" )
+         CALL logger_error( " FILE DEL ATT NAME: file structure unknown ")
+         CALL logger_debug( " FILE DEL ATT NAME: you should have "//&
+         &  "used file_init before running file_del_att" )
 
       ELSE
@@ -1195,16 +1275,17 @@
 
             ! get the variable id, in file variable structure
-            il_attid=0
+            il_ind=0
             IF( ASSOCIATED(td_file%t_att) )THEN
-               il_attid=att_get_id(td_file%t_att(:), cd_name )
+               il_ind=att_get_index(td_file%t_att(:), cd_name )
             ENDIF
-            IF( il_attid /= 0 )THEN
+
+            IF( il_ind /= 0 )THEN
    
-               CALL file_del_att(td_file, td_file%t_att(il_attid))
+               CALL file_del_att(td_file, td_file%t_att(il_ind))
 
             ELSE
 
                CALL logger_warn( &
-               &  " DEL ATT NAME: there is no attribute with name "//&
+               &  " FILE DEL ATT NAME: there is no attribute with name "//&
                &  TRIM(cd_name)//" in file "//TRIM(td_file%c_name))
 
@@ -1212,6 +1293,6 @@
 
          ELSE
-            CALL logger_debug( " DEL ATT NAME: no attribute associated to file "//&
-            &               TRIM(td_file%c_name) )
+            CALL logger_debug( " FILE DEL ATT NAME: no attribute "//&
+            &  "associated to file "//TRIM(td_file%c_name) )
          ENDIF
 
@@ -1219,18 +1300,14 @@
 
    END SUBROUTINE file__del_att_name
-   !> @endcode
    !-------------------------------------------------------------------
    !> @brief This subroutine delete a global attribute structure 
-   !> from file structure.
-   !
-   !> @details
-   !
-   !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[inout] td_file : file structure
-   !> @param[in] td_att : attribute structure
-   !-------------------------------------------------------------------
-   !> @code
+   !> from file structure, given attribute structure.
+   !
+   !> @author J.Paul
+   !> - November, 2013- Initial Version
+   !
+   !> @param[inout] td_file   file structure
+   !> @param[in] td_att       attribute structure
+   !-------------------------------------------------------------------
    SUBROUTINE file__del_att_str(td_file, td_att)
       IMPLICIT NONE
@@ -1242,37 +1319,35 @@
       ! local variable
       INTEGER(i4) :: il_status
-      INTEGER(i4) :: il_attid
+      INTEGER(i4) :: il_ind
       TYPE(TATT), DIMENSION(:), ALLOCATABLE :: tl_att
 
       ! loop indices
-      INTEGER(i4) :: ji
       !----------------------------------------------------------------
 
       ! check if file opened
-      !IF( TRIM(td_file%c_name) == "unknown" )THEN
       IF( TRIM(td_file%c_name) == '' )THEN
 
-         CALL logger_error( " DEL ATT: file structure unknown ")
-         CALL logger_debug( " DEL ATT: you should have used file_init before "//&
-         & "running file_del_att" )      
+         CALL logger_error( " FILE DEL ATT: file structure unknown ")
+         CALL logger_debug( " FILE DEL ATT: you should have used "//&
+         &  "file_init before running file_del_att" )      
 
       ELSE
 
          ! check if attribute already in file structure
-         il_attid=0
+         il_ind=0
          IF( ASSOCIATED(td_file%t_att) )THEN
-            il_attid=att_get_id( td_file%t_att(:), td_att%c_name )
-         ENDIF
-
-         IF( il_attid == 0 )THEN
+            il_ind=att_get_index( td_file%t_att(:), td_att%c_name )
+         ENDIF
+
+         IF( il_ind == 0 )THEN
 
             CALL logger_error( &
-            &  " DEL ATT: no attribute "//TRIM(td_att%c_name)//&
+            &  " FILE DEL ATT: no attribute "//TRIM(td_att%c_name)//&
             &  ", in file "//TRIM(td_file%c_name) )
 
          ELSE
             
-            CALL logger_debug( &
-            &  " DEL ATT: del attribute "//TRIM(td_att%c_name)//&
+            CALL logger_trace( &
+            &  " FILE DEL ATT: del attribute "//TRIM(td_att%c_name)//&
             &  ", in file "//TRIM(td_file%c_name) )
 
@@ -1281,5 +1356,5 @@
 
                CALL logger_error( &
-               &  " ADD ATT: not enough space to put attributes from "//&
+               &  " FILE ADD ATT: not enough space to put attributes from "//&
                &  TRIM(td_file%c_name)//" in temporary attribute structure")
 
@@ -1287,8 +1362,14 @@
 
                ! save temporary global attribute's file structure
-               tl_att(1:il_attid-1)=td_file%t_att(1:il_attid-1)
-               tl_att(il_attid:)=td_file%t_att(il_attid+1:)
-
-               DEALLOCATE( td_file%t_att )
+               IF( il_ind > 1 )THEN
+                  tl_att(1:il_ind-1)=att_copy(td_file%t_att(1:il_ind-1))
+               ENDIF
+
+               IF( il_ind < td_file%i_natt )THEN
+                  tl_att(il_ind:)=att_copy(td_file%t_att(il_ind+1:))
+               ENDIF
+
+               CALL att_clean( td_file%t_att(:) )
+               DEALLOCATE(td_file%t_att)
 
                ! new number of attribute in file
@@ -1299,5 +1380,5 @@
 
                   CALL logger_error( &
-                  &  " ADD ATT: not enough space to put attributes "//&
+                  &  " FILE ADD ATT: not enough space to put attributes "//&
                   &  "in file structure "//TRIM(td_file%c_name) )
 
@@ -1305,12 +1386,10 @@
 
                ! copy attribute in file before
-               td_file%t_att(1:td_file%i_natt)=tl_att(:)
-
-               ! update attribute id
-               DO ji=1,td_file%i_natt
-                  td_file%t_att(ji)%i_id=ji
-               ENDDO
-
+               td_file%t_att(1:td_file%i_natt)=att_copy(tl_att(:))
+
+               ! clean 
+               CALL att_clean(tl_att(:))
                DEALLOCATE(tl_att)
+
             ENDIF 
          ENDIF
@@ -1318,20 +1397,15 @@
 
    END SUBROUTINE file__del_att_str
-   !> @endcode
    !-------------------------------------------------------------------
    !> @brief This subroutine move a global attribute structure 
    !> from file structure.
-   !> @note attribute id could be change
-   !
-   !> @details
-   !
-   !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[inout] td_file : file structure
-   !> @param[in] td_att : attribute structure
-   !> @todo
-   !-------------------------------------------------------------------
-   !> @code
+   !> @warning change attribute id in file structure.
+   !
+   !> @author J.Paul
+   !> - November, 2013- Initial Version
+   !
+   !> @param[inout] td_file   file structure
+   !> @param[in] td_att       attribute structure
+   !-------------------------------------------------------------------
    SUBROUTINE file_move_att(td_file, td_att)
       IMPLICIT NONE
@@ -1343,13 +1417,13 @@
       ! local variable
       TYPE(TATT)  :: tl_att
-      INTEGER(i4) :: il_attid
+      INTEGER(i4) :: il_ind
       !----------------------------------------------------------------
 
       ! copy attribute
-      tl_att=td_att
+      tl_att=att_copy(td_att)
 
       IF( ASSOCIATED(td_file%t_att) )THEN
-         il_attid=att_get_id(td_file%t_att(:),TRIM(tl_att%c_name))
-         IF( il_attid /= 0 )THEN
+         il_ind=att_get_index(td_file%t_att(:),TRIM(tl_att%c_name))
+         IF( il_ind /= 0 )THEN
             ! remove attribute with same name
             CALL file_del_att(td_file, tl_att)
@@ -1360,6 +1434,8 @@
       CALL file_add_att(td_file, tl_att)
 
+       ! clean
+       CALL att_clean(tl_att)
+
    END SUBROUTINE file_move_att
-   !> @endcode
    !-------------------------------------------------------------------
    !> @brief This subroutine add a dimension structure in file 
@@ -1367,85 +1443,79 @@
    !> Do not overwrite, if dimension already in file structure.
    !
-   !> @details
-   !
-   !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[inout] td_file : file structure
-   !> @param[in] td_dim : dimension structure
-   !
-   !> @todo 
-   !-------------------------------------------------------------------
-   !> @code
+   !> @author J.Paul
+   !> - November, 2013- Initial Version
+   !> @date September, 2014
+   !> - do not reorder dimension, before put in file
+   !
+   !> @param[inout] td_file   file structure
+   !> @param[in] td_dim       dimension structure
+   !-------------------------------------------------------------------
    SUBROUTINE file_add_dim(td_file, td_dim)
       IMPLICIT NONE
 
       ! Argument      
-      TYPE(TFILE), INTENT(INOUT) :: td_file
-      TYPE(TDIM),  INTENT(IN)    :: td_dim
-
-      ! local variable
-      INTEGER(i4) :: il_dimid
+      TYPE(TFILE)     , INTENT(INOUT) :: td_file
+      TYPE(TDIM)      , INTENT(IN   ) :: td_dim
+
+      ! local variable
+      INTEGER(i4) :: il_ind
+
+      ! loop indices
+      INTEGER(i4) :: ji
       !----------------------------------------------------------------
       ! check if file opened
-      !IF( TRIM(td_file%c_name) == "unknown" )THEN
       IF( TRIM(td_file%c_name) == '' )THEN
 
-         CALL logger_error( " ADD DIM: file structure unknown ")
-         CALL logger_debug( " ADD DIM: you should have used file_init before "//&
-         & "running file_add_dim" )      
+         CALL logger_error( " FILE ADD DIM: file structure unknown ")
+         CALL logger_debug( " FILE ADD DIM: you should have used "//&
+         &  "file_init before running file_add_dim" )      
 
       ELSE
 
-         IF( td_file%i_ndim <= 4 )THEN
+         IF( td_file%i_ndim <= ip_maxdim )THEN
 
             ! check if dimension already in file structure
-            il_dimid=dim_get_id(td_file%t_dim(:), td_dim%c_name, td_dim%c_sname)
-            IF( il_dimid /= 0 )THEN
-
-               CALL logger_warn("ADD DIM: dimension "//TRIM(td_dim%c_name)//&
-               &  ", short name "//TRIM(td_dim%c_sname)//&
-               &  ", already in file "//TRIM(td_file%c_name) )
-
-               IF( td_file%t_dim(il_dimid)%i_len /= td_dim%i_len )THEN
+            il_ind=dim_get_index(td_file%t_dim(:), td_dim%c_sname)
+            IF( il_ind /= 0 )THEN
+               IF( td_file%t_dim(il_ind)%l_use )THEN
                   CALL logger_error( &
-                  &  "ADD DIM: dimension "//TRIM(td_dim%c_name)//&
-                  &  " already in file "//TRIM(td_file%c_name)//&
-                  &  " differ from added dimension ")
+                  &  "FILE ADD DIM: dimension "//TRIM(td_dim%c_name)//&
+                  &  ", short name "//TRIM(td_dim%c_sname)//&
+                  &  ", already used in file "//TRIM(td_file%c_name) )
+               ELSE
+                  ! replace dimension
+                  td_file%t_dim(il_ind)=dim_copy(td_dim)
+                  td_file%t_dim(il_ind)%i_id=il_ind
+                  td_file%t_dim(il_ind)%l_use=.TRUE.
                ENDIF
-
             ELSE
-
-               CALL logger_debug( &
-               &  " ADD DIM: add dimension "//TRIM(td_dim%c_name)//&
-               &  ", short name "//TRIM(td_dim%c_sname)//&
-               &  ", in file "//TRIM(td_file%c_name) )
-
-               IF( td_file%i_ndim == 4 )THEN
+               IF( td_file%i_ndim == ip_maxdim )THEN
+                  CALL logger_error( &
+                  &  "FILE ADD DIM: can not add dimension "//TRIM(td_dim%c_name)//&
+                  &  ", short name "//TRIM(td_dim%c_sname)//&
+                  &  ", in file "//TRIM(td_file%c_name)//". Already "//&
+                  &  TRIM(fct_str(ip_maxdim))//" dimensions." )
+               ELSE
                   ! search empty dimension
-                  il_dimid=dim_get_void_id(td_file%t_dim(:),TRIM(td_dim%c_name), &
-                  &                                         TRIM(td_dim%c_sname))
-                  ! replace empty dimension
-                  td_file%t_dim(il_dimid)=td_dim
-                  td_file%t_dim(il_dimid)%i_id=il_dimid
-                  td_file%t_dim(il_dimid)%l_use=.TRUE.
-               ELSE
-                  ! add new dimension
-                  il_dimid=dim_get_void_id(td_file%t_dim(:),TRIM(td_dim%c_name), &
-                  &                                         TRIM(td_dim%c_sname))
-                  td_file%t_dim(il_dimid)=td_dim
-                  td_file%t_dim(il_dimid)%i_id=td_file%i_ndim+1
-                  td_file%t_dim(il_dimid)%l_use=.TRUE.
+                  DO ji=1,ip_maxdim
+                     IF( td_file%t_dim(ji)%i_id == 0 )THEN
+                        il_ind=ji 
+                        EXIT
+                     ENDIF
+                  ENDDO
+ 
+                  ! add new dimension    
+                  td_file%t_dim(il_ind)=dim_copy(td_dim)
                   ! update number of attribute
                   td_file%i_ndim=COUNT(td_file%t_dim(:)%l_use)
-               ENDIF
-
-               ! reorder dimension to ('x','y','z','t')
-               CALL dim_reorder(td_file%t_dim)
-               
+
+                  td_file%t_dim(il_ind)%i_id=td_file%i_ndim
+                  td_file%t_dim(il_ind)%l_use=.TRUE.
+               ENDIF               
             ENDIF
+
          ELSE
             CALL logger_error( &
-            &  " ADD DIM: too much dimension in file "//&
+            &  " FILE ADD DIM: too much dimension in file "//&
             &  TRIM(td_file%c_name)//" ("//TRIM(fct_str(td_file%i_ndim))//")")
          ENDIF
@@ -1454,172 +1524,136 @@
 
    END SUBROUTINE file_add_dim
-   !> @endcode
    !-------------------------------------------------------------------
    !> @brief This subroutine delete a dimension structure in file 
-   !> structure.<br/>
-   !
-   !> @details
-   !
-   !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[inout] td_file : file structure
-   !> @param[in] td_dim : dimension structure
-   !
-   !> @todo 
-   !-------------------------------------------------------------------
-   !> @code
+   !> structure.
+   !>
+   !> @author J.Paul
+   !> - November, 2013- Initial Version
+   !
+   !> @param[inout] td_file   file structure
+   !> @param[in] td_dim       dimension structure
+   !-------------------------------------------------------------------
    SUBROUTINE file_del_dim(td_file, td_dim)
       IMPLICIT NONE
 
       ! Argument      
-      TYPE(TFILE), INTENT(INOUT) :: td_file
-      TYPE(TDIM),  INTENT(IN)    :: td_dim
+      TYPE(TFILE)     , INTENT(INOUT) :: td_file
+      TYPE(TDIM)      , INTENT(IN   ) :: td_dim
 
       ! local variable
       INTEGER(i4) :: il_status
-      INTEGER(i4) :: il_dimid
+      INTEGER(i4) :: il_ind
+
       TYPE(TDIM), DIMENSION(:), ALLOCATABLE  :: tl_dim
+
+      ! loop indices
+      INTEGER(i4) :: ji
       !----------------------------------------------------------------
       ! check if file opened
-      !IF( TRIM(td_file%c_name) == "unknown" )THEN
       IF( TRIM(td_file%c_name) == '' )THEN
 
-         CALL logger_error( " DEL DIM: file structure unknown ")
-         CALL logger_debug( " DEL DIM: you should have used file_init before "//&
-         & "running file_del_dim" )      
+         CALL logger_error( " FILE DEL DIM: file structure unknown ")
+         CALL logger_debug( " FILE DEL DIM: you should have used "//&
+         &  "file_init before running file_del_dim" )      
 
       ELSE
 
-         IF( td_file%i_ndim <= 4 )THEN
-
-            ! check if dimension already in file structure
-            il_dimid=dim_get_id(td_file%t_dim(:), td_dim%c_name, td_dim%c_sname)
-            IF( il_dimid == 0 )THEN
+         ! check if dimension already in file structure
+         il_ind=dim_get_index(td_file%t_dim(:), td_dim%c_sname)
+         IF( il_ind == 0 )THEN
+
+            CALL logger_error( &
+            &  "FILE DEL DIM: no dimension "//TRIM(td_dim%c_name)//&
+            &  ", short name "//TRIM(td_dim%c_sname)//&
+            &  ", in file "//TRIM(td_file%c_name) )
+
+         ELSE
+            ALLOCATE( tl_dim(td_file%i_ndim-1), stat=il_status )
+            IF(il_status /= 0 )THEN
 
                CALL logger_error( &
-               &  " DEL DIM: no dimension "//TRIM(td_dim%c_name)//&
-               &  ", short name "//TRIM(td_dim%c_sname)//&
-               &  ", in file "//TRIM(td_file%c_name) )
-
-            ELSE
-
-               CALL logger_debug( &
-               &  " DEL DIM: delete dimension "//TRIM(td_dim%c_name)//&
-               &  ", short name "//TRIM(td_dim%c_sname)//&
-               &  ", in file "//TRIM(td_file%c_name) )
-
-               IF( td_file%i_ndim == 4 )THEN
-                  ALLOCATE( tl_dim(1), stat=il_status )
-                  IF(il_status /= 0 )THEN
-                     CALL logger_error( &
-                     &  " DEL DIM: not enough space to put dimensions from "//&
-                     &  TRIM(td_file%c_name)//" in temporary dimension structure")
-                  ELSE
-                     ! replace dimension by empty one
-                     td_file%t_dim(il_dimid)=tl_dim(1)
-                  ENDIF
-                  DEALLOCATE(tl_dim)
-               ELSE
-                  ! 
-                  !ALLOCATE( tl_dim(td_file%i_ndim), stat=il_status )
-                  ALLOCATE( tl_dim(ip_maxdim), stat=il_status )
-                  IF(il_status /= 0 )THEN
-
-                     CALL logger_error( &
-                     &  " DEL DIM: not enough space to put dimensions from "//&
-                     &  TRIM(td_file%c_name)//" in temporary dimension structure")
-
-                  ELSE
-
-                     print *,"<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
-                     print *,'il_dimid '//TRIM(fct_str(il_dimid))
-                     CALL dim_print(td_file%t_dim(:))
-                     ! save temporary dimension's file structure
-                     tl_dim( 1 : il_dimid-1 ) = td_file%t_dim( 1 : il_dimid-1 )
-                     !tl_dim( il_dimid : td_file%i_ndim-1 ) = &
-                     !&           td_file%t_dim( il_dimid+1 : td_file%i_ndim )
-                     tl_dim( il_dimid : ip_maxdim-1 ) = &
-                     &           td_file%t_dim( il_dimid+1 : ip_maxdim )
-                     print *,"<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
-                     CALL dim_print(tl_dim(:))
-
-                     ! copy dimension in file, except one
-                     !td_file%t_dim(1:td_file%i_ndim)=tl_dim(:)
-                     td_file%t_dim(:)=tl_dim(:)
-                     print *,"<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
-                     CALL dim_print(td_file%t_dim(:))
-
-                     ! update number of dimension
-                     td_file%i_ndim=td_file%i_ndim-1
-
-                  ENDIF
-               ENDIF
-
-               ! reorder dimension to ('x','y','z','t')
-               CALL dim_reorder(td_file%t_dim)
-
+               &  "FILE DEL DIM: not enough space to put dimensions from "//&
+               &  TRIM(td_file%c_name)//" in temporary dimension structure")
+
+            ELSE            
+               ! save temporary dimension's mpp structure
+               tl_dim( 1 : il_ind-1 ) = dim_copy(td_file%t_dim(1 : il_ind-1))
+               tl_dim( il_ind : td_file%i_ndim-1 ) = &
+               &      dim_copy(td_file%t_dim(il_ind+1 : td_file%i_ndim))
+
+               ! remove dimension from file
+               CALL dim_clean(td_file%t_dim(:))
+               ! copy dimension in file, except one
+               td_file%t_dim(1:td_file%i_ndim)=dim_copy(tl_dim(:))
+
+               ! update number of dimension
+               td_file%i_ndim=td_file%i_ndim-1
+
+               ! update dimension id
+               DO ji=1,td_file%i_ndim
+                  td_file%t_dim(ji)%i_id=ji
+               ENDDO
+
+               ! clean
+               CALL dim_clean(tl_dim(:))
+               DEALLOCATE(tl_dim)
             ENDIF
-         ELSE
-            CALL logger_error( &
-            &  " DEL DIM: too much dimension in file "//&
-            &  TRIM(td_file%c_name)//" ("//TRIM(fct_str(td_file%i_ndim))//")")
-         ENDIF
-
+         ENDIF
       ENDIF
 
    END SUBROUTINE file_del_dim
-   !> @endcode
    !-------------------------------------------------------------------
    !> @brief This subroutine move a dimension structure 
    !> in file structure.
-   !> @warning dimension order Nov have changed
-   !
-   !> @details
-   !
-   !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[inout] td_file : file structure
-   !> @param[in] td_dim : dimension structure
-   !> @todo
-   !-------------------------------------------------------------------
-   !> @code
+   !> @warning change dimension order in file structure. 
+   !
+   !> @author J.Paul
+   !> - November, 2013- Initial Version
+   !
+   !> @param[inout] td_file   file structure
+   !> @param[in] td_dim       dimension structure
+   !-------------------------------------------------------------------
    SUBROUTINE file_move_dim(td_file, td_dim)
       IMPLICIT NONE
 
       ! Argument      
-      TYPE(TFILE), INTENT(INOUT) :: td_file
-      TYPE(TDIM),  INTENT(IN)    :: td_dim
-
-      ! local variable
-      TYPE(TDIM)  :: tl_dim
+      TYPE(TFILE)     , INTENT(INOUT) :: td_file
+      TYPE(TDIM)      , INTENT(IN   ) :: td_dim
+
+      ! local variable
+      INTEGER(i4) :: il_ind
       INTEGER(i4) :: il_dimid
       !----------------------------------------------------------------
-
-      ! copy dimension
-      tl_dim=td_dim
-
-      il_dimid=dim_get_id(td_file%t_dim(:), TRIM(td_dim%c_name), &
-      &                                     TRIM(td_dim%c_sname))
-      IF( il_dimid /= 0 )THEN
-         ! remove dimension with same name
-         CALL file_del_dim(td_file, tl_dim)
-      ENDIF
-
-      ! add new dimension
-      CALL file_add_dim(td_file, tl_dim)
+      IF( td_file%i_ndim <= ip_maxdim )THEN
+
+         ! check if dimension already in mpp structure
+         il_ind=dim_get_index(td_file%t_dim(:), td_dim%c_name, td_dim%c_sname)
+         IF( il_ind /= 0 )THEN
+
+            il_dimid=td_file%t_dim(il_ind)%i_id
+            ! replace dimension
+            td_file%t_dim(il_ind)=dim_copy(td_dim)
+            td_file%t_dim(il_ind)%i_id=il_dimid
+            td_file%t_dim(il_ind)%l_use=.TRUE.
+
+         ELSE
+            CALL file_add_dim(td_file, td_dim)
+         ENDIF
+
+      ELSE
+         CALL logger_error( &
+         &  "FILE MOVE DIM: too much dimension in mpp "//&
+         &  TRIM(td_file%c_name)//" ("//TRIM(fct_str(td_file%i_ndim))//")")
+      ENDIF
 
    END SUBROUTINE file_move_dim
-   !> @endcode
    !-------------------------------------------------------------------
    !> @brief This subroutine print some information about file strucutre.
    !
    !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[in] td_file : file structure
-   !-------------------------------------------------------------------
-   !> @code
+   !> - November, 2013- Initial Version
+   !
+   !> @param[in] td_file   file structure
+   !-------------------------------------------------------------------
    SUBROUTINE file_print(td_file)
       IMPLICIT NONE
@@ -1688,19 +1722,17 @@
 
    END SUBROUTINE file_print
-   !> @endcode
    !-------------------------------------------------------------------
    !> @brief This function get suffix of file name.
    !> @details
    !> we assume suffix is define as alphanumeric character following the
-   !> last '.' in file name
+   !> last '.' in file name.<br/>
    !> If no suffix is found, return empty character.
    !
    !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[in] cd_file : file structure
+   !> - November, 2013- Initial Version
+   !
+   !> @param[in] cd_file   file structure
    !> @return suffix
    !-------------------------------------------------------------------
-   !> @code
    CHARACTER(LEN=lc) FUNCTION file__get_suffix(cd_file)
       IMPLICIT NONE
@@ -1713,5 +1745,5 @@
       !----------------------------------------------------------------
 
-      CALL logger_trace( "GET SUFFIX: look for suffix in file name "//&
+      CALL logger_trace( "FILE GET SUFFIX: look for suffix in file name "//&
       &               TRIM(cd_file) )
 
@@ -1721,5 +1753,5 @@
          READ( cd_file(il_ind:),'(a)' ) file__get_suffix
 
-         IF( fct_is_num(file__get_suffix) )THEN
+         IF( fct_is_num(file__get_suffix(2:)) )THEN
             file__get_suffix=''
          ENDIF
@@ -1730,18 +1762,16 @@
 
    END FUNCTION file__get_suffix
-   !> @endcode
    !-------------------------------------------------------------------
    !> @brief This function get number in file name without suffix.
    !> @details
    !> Actually it get the number following the last separator.
-   !> separator could be '.' or '_'
-   !
-   !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[in] cd_file : file name (without suffix)
-   !> @return file structure
-   !-------------------------------------------------------------------
-   !> @code
+   !> separator could be '.' or '_'.
+   !
+   !> @author J.Paul
+   !> - November, 2013- Initial Version
+   !
+   !> @param[in] cd_file   file name (without suffix)
+   !> @return character file number.
+   !-------------------------------------------------------------------
    CHARACTER(LEN=lc) FUNCTION file__get_number(cd_file)
       IMPLICIT NONE
@@ -1760,6 +1790,6 @@
       ! get number position in file name
       il_indmax=0
-      DO ji=1,ig_nsep
-         il_ind=INDEX(TRIM(cd_file),TRIM(cg_sep(ji)),BACK=.TRUE.)
+      DO ji=1,ip_nsep
+         il_ind=INDEX(TRIM(cd_file),TRIM(cp_sep(ji)),BACK=.TRUE.)
          IF( il_ind > il_indmax )THEN
             il_indmax=il_ind
@@ -1779,7 +1809,6 @@
 
    END FUNCTION file__get_number
-   !> @endcode
-   !-------------------------------------------------------------------
-   !> @brief This function rename file name.
+   !-------------------------------------------------------------------
+   !> @brief This function rename file name, given processor number.
    !> @details
    !> If no processor number is given, return file name without number
@@ -1787,11 +1816,10 @@
    !
    !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[in] td_file : file structure
-   !> @param[in] id_num : processor number (start to 1)
-   !> @return file structure
-   !-------------------------------------------------------------------
-   !> @code
+   !> - November, 2013- Initial Version
+   !
+   !> @param[in] td_file   file structure
+   !> @param[in] id_num    processor number (start to 1)
+   !> @return file name
+   !-------------------------------------------------------------------
    CHARACTER(LEN=lc) FUNCTION file__rename_char(cd_file, id_num)
       IMPLICIT NONE
@@ -1841,8 +1869,7 @@
          WRITE(file__rename_char,'(a,a)') TRIM(cl_base),TRIM(cl_suffix)
       ENDIF
-      CALL logger_trace(" RENAME : "//TRIM(file__rename_char))
+      CALL logger_trace(" FILE RENAME : "//TRIM(file__rename_char))
 
    END FUNCTION file__rename_char
-   !> @endcode
    !-------------------------------------------------------------------
    !> @brief This function rename file name, given file structure.
@@ -1852,11 +1879,10 @@
    !
    !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[in] td_file : file structure
-   !> @param[in] id_num : processor number (start to 1)
+   !> - November, 2013- Initial Version
+   !
+   !> @param[in] td_file   file structure
+   !> @param[in] id_num    processor number (start to 1)
    !> @return file structure
    !-------------------------------------------------------------------
-   !> @code
    TYPE(TFILE) FUNCTION file__rename_str(td_file, id_num)
       IMPLICIT NONE
@@ -1876,15 +1902,13 @@
 
    END FUNCTION file__rename_str
-   !> @endcode
    !-------------------------------------------------------------------
    !> @brief This function add suffix to file name.
    !
    !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[in] td_file : file structure
-   !> @return file structure
-   !-------------------------------------------------------------------
-   !> @code
+   !> - November, 2013- Initial Version
+   !
+   !> @param[in] td_file   file structure
+   !> @return file name
+   !-------------------------------------------------------------------
    CHARACTER(LEN=lc) FUNCTION file_add_suffix(cd_file, cd_type)
       IMPLICIT NONE
@@ -1918,20 +1942,18 @@
             ENDIF
          CASE DEFAULT
-            CALL logger_error( " ADD SUFFIX: type unknown "//TRIM(cd_type) )
+            CALL logger_error(" FILE ADD SUFFIX: type unknown "//TRIM(cd_type))
       END SELECT
 
    END FUNCTION file_add_suffix
-   !> @endcode
    !-------------------------------------------------------------------
    !> @brief 
-   !>  This subroutine clean mpp strcuture.
-   !
-   !> @author J.Paul
-   !> @date Nov, 2013
-   !
-   !> @param[inout] td_mpp : mpp strcuture
-   !-------------------------------------------------------------------
-   !> @code
-   SUBROUTINE file_clean( td_file )
+   !>  This subroutine clean file strcuture.
+   !
+   !> @author J.Paul
+   !> @date November, 2013 - Inital version
+   !
+   !> @param[inout] td_file   file strcuture
+   !-------------------------------------------------------------------
+   SUBROUTINE file__clean_unit( td_file )
       IMPLICIT NONE
       ! Argument
@@ -1942,50 +1964,65 @@
 
       ! loop indices
-      INTEGER(i4) :: ji
-      !----------------------------------------------------------------
-
-      CALL logger_info( &
-      &  " CLEAN: reset file "//TRIM(td_file%c_name) )
+      !----------------------------------------------------------------
+
+      CALL logger_trace( &
+      &  " FILE CLEAN: reset file "//TRIM(td_file%c_name) )
 
       ! del attribute
       IF( ASSOCIATED( td_file%t_att ) )THEN
-         DO ji=td_file%i_natt,1,-1
-            CALL att_clean( td_file%t_att(ji) )
-         ENDDO
-         DEALLOCATE( td_file%t_att )
+         CALL att_clean( td_file%t_att(:) )
+         DEALLOCATE(td_file%t_att)
       ENDIF
 
       ! del dimension
       IF( td_file%i_ndim /= 0 )THEN
-         DO ji=td_file%i_ndim,1,-1
-            CALL dim_clean( td_file%t_dim(ji) )
-         ENDDO
+         CALL dim_clean( td_file%t_dim(:) )
       ENDIF
 
       ! del variable
       IF( ASSOCIATED( td_file%t_var ) )THEN
-         DO ji=td_file%i_nvar,1,-1
-            CALL var_clean( td_file%t_var(ji) )
-         ENDDO
-         DEALLOCATE( td_file%t_var )
+         CALL var_clean( td_file%t_var(:) )
+         DEALLOCATE(td_file%t_var)
       ENDIF
 
       ! replace by empty structure
-      td_file=tl_file
-
-   END SUBROUTINE file_clean
-   !> @endcode
-   !-------------------------------------------------------------------
-   !> @brief This function return the file id, in a table of file
-   !> structure,  given file name 
-   !
-   !> @author J.Paul
-   !> - Nov, 2013- Initial Version
-   !
-   !> @param[in] td_file : table of file structure
-   !> @param[in] cd_name : file name
-   !> @return file id in table of file structure (0 if not found)
-   !-------------------------------------------------------------------
-   !> @code
+      td_file=file_copy(tl_file)
+
+   END SUBROUTINE file__clean_unit
+   !-------------------------------------------------------------------
+   !> @brief 
+   !>  This subroutine clean file array of file strcuture.
+   !
+   !> @author J.Paul
+   !> @date Marsh, 2014 - Inital version
+   !
+   !> @param[inout] td_file   array file strcuture
+   !-------------------------------------------------------------------
+   SUBROUTINE file__clean_arr( td_file )
+      IMPLICIT NONE
+      ! Argument
+      TYPE(TFILE), DIMENSION(:), INTENT(INOUT) :: td_file
+
+      ! local variable
+      ! loop indices
+      INTEGER(i4) :: ji
+      !----------------------------------------------------------------
+
+      DO ji=SIZE(td_file(:)),1,-1
+         CALL file_clean(td_file(ji))
+      ENDDO
+
+   END SUBROUTINE file__clean_arr
+   !-------------------------------------------------------------------
+   !> @brief This function return the file id, in a array of file
+   !> structure,  given file name. 
+   !
+   !> @author J.Paul
+   !> - November, 2013- Initial Version
+   !
+   !> @param[in] td_file   array of file structure
+   !> @param[in] cd_name   file name
+   !> @return file id in array of file structure (0 if not found)
+   !-------------------------------------------------------------------
    INTEGER(i4) FUNCTION file_get_id(td_file, cd_name)
       IMPLICIT NONE
@@ -2003,11 +2040,10 @@
       il_size=SIZE(td_file(:))
 
-      ! check if file is in table of file structure
+      ! check if file is in array of file structure
       DO ji=1,il_size
          ! look for file name
-         CALL logger_debug(" cd_name "//TRIM(fct_lower(cd_name)) )
          IF( fct_lower(td_file(ji)%c_name) == fct_lower(cd_name) )THEN
          
-            file_get_id=ji
+            file_get_id=td_file(ji)%i_id
             EXIT
 
@@ -2016,5 +2052,28 @@
 
    END FUNCTION file_get_id
-   !> @endcode
+   !-------------------------------------------------------------------
+   !> @brief
+   !> This function get the next unused unit in array of file structure.
+   !> 
+   !> @author J.Paul
+   !> - September, 2014- Initial Version
+   !
+   !> @param[in] td_file   array of file 
+   !-------------------------------------------------------------------
+   FUNCTION file_get_unit(td_file)
+      IMPLICIT NONE
+      ! Argument
+      TYPE(TFILE), DIMENSION(:), INTENT(IN   ) :: td_file
+
+      ! function
+      INTEGER(i4) :: file_get_unit
+
+      ! local variable
+      ! loop indices
+      !----------------------------------------------------------------
+
+      file_get_unit=MAXVAL(td_file(:)%i_id)+1
+
+   END FUNCTION file_get_unit
 END MODULE file
 
