Index: branches/2016/dev_CNRS_MERCATOR_2016/NEMOGCM/TOOLS/SIREN/src/grid.f90
===================================================================
--- branches/2016/dev_CNRS_MERCATOR_2016/NEMOGCM/TOOLS/SIREN/src/grid.f90	(revision 7389)
+++ branches/2016/dev_CNRS_MERCATOR_2016/NEMOGCM/TOOLS/SIREN/src/grid.f90	(revision 7390)
@@ -218,7 +218,9 @@
 !> @date February, 2015
 !> - add function grid_fill_small_msk to fill small domain inside bigger one
-!> @February, 2016
+!> @date February, 2016
 !> - improve way to check coincidence (bug fix)
 !> - manage grid cases for T,U,V or F point, with even or odd refinment (bug fix)
+!> @date April, 2016
+!> - add function to get closest grid point using coarse grid coordinates strucutre 
 !
 !> @note Software governed by the CeCILL licence     (NEMOGCM/NEMO_CeCILL.txt)
@@ -271,4 +273,8 @@
    PRIVATE :: grid__get_coarse_index_cc ! - using coarse and fine grid array of lon,lat
 
+                                     ! return closest coarse grid point from another point
+   PRIVATE :: grid__get_closest_str    ! - using coarse grid coordinates strucutre
+   PRIVATE :: grid__get_closest_arr    ! - using coarse grid array of lon,lat
+
                                      ! get offset between fine and coarse grid
    PRIVATE :: grid__get_fine_offset_ff ! - using coarse and fine grid coordinates files
@@ -333,4 +339,9 @@
       MODULE PROCEDURE grid__get_ghost_mpp
    END INTERFACE  grid_get_ghost
+
+   INTERFACE  grid_get_closest
+      MODULE PROCEDURE grid__get_closest_str
+      MODULE PROCEDURE grid__get_closest_arr
+   END INTERFACE  grid_get_closest
 
    INTERFACE  grid_get_coarse_index
@@ -467,5 +478,5 @@
    !-------------------------------------------------------------------
    !> @brief This subroutine get information about global domain, given mpp
-   !> strucutre.
+   !> structure.
    !>
    !> @details
@@ -536,10 +547,10 @@
 
       SELECT CASE(il_perio)
-      CASE(3,4)
-         il_pivot=1
-      CASE(5,6)
-         il_pivot=0
-      CASE(0,1,2)
-         il_pivot=1
+         CASE(3,4)
+            il_pivot=1
+         CASE(5,6)
+            il_pivot=0
+         CASE(0,1,2)
+            il_pivot=1
       END SELECT
 
@@ -1365,4 +1376,5 @@
             END SELECT
          ELSE
+            il_perio=-1
             ! check periodicity
             IF(ANY(td_var%d_value(   1     ,:,1,1)/=td_var%d_fill).OR.&
@@ -1656,5 +1668,5 @@
    !> else return the size of the ovarlap band.
    !> East-West overlap is computed comparing longitude value of the  
-   !> South" part of the domain, to avoid  north fold boundary.
+   !> South part of the domain, to avoid  north fold boundary.
    !>
    !
@@ -1663,4 +1675,6 @@
    !> @date October, 2014
    !> - work on mpp file structure instead of file structure
+   !> @date October, 2016
+   !> - check longitude as longname
    !>
    !> @param[in] td_lon longitude variable structure 
@@ -1712,12 +1726,13 @@
             ALLOCATE( dl_vare(il_jmax-il_jmin+1) )
             ALLOCATE( dl_varw(il_jmax-il_jmin+1) )
-            
+
             dl_vare(:)=dl_value(il_east,il_jmin:il_jmax)
             dl_varw(:)=dl_value(il_west,il_jmin:il_jmax)
-            
-            IF( .NOT.(  ALL(dl_vare(:)==td_var%d_fill) .AND. &
-            &           ALL(dl_varw(:)==td_var%d_fill) ) )THEN
-         
-               IF( TRIM(td_var%c_stdname) == 'longitude' )THEN
+
+            IF( .NOT.( ALL(dl_vare(:)==td_var%d_fill) .AND. &
+            &          ALL(dl_varw(:)==td_var%d_fill) ) )THEN
+
+               IF( TRIM(td_var%c_stdname) == 'longitude' .OR. &
+                 & SCAN( TRIM(td_var%c_longname), 'longitude') == 0 )THEN
                   WHERE( dl_value(:,:) > 180._dp .AND. &
                   &      dl_value(:,:) /= td_var%d_fill ) 
@@ -1743,5 +1758,5 @@
                      ELSE
                         dl_vare(:)=dl_value(il_east-ji,il_jmin:il_jmax)
-                        
+
                         IF( ALL( dl_varw(:) == dl_vare(:) ) )THEN
                            grid__get_ew_overlap_var=ji+1
@@ -1769,8 +1784,10 @@
    !> else return the size of the ovarlap band.
    !> East-West overlap is computed comparing longitude value of the  
-   !> South" part of the domain, to avoid  north fold boundary.
+   !> South part of the domain, to avoid  north fold boundary.
    !>
    !> @author J.Paul
    !> @date October, 2014 - Initial Version
+   !> @date October, 2016
+   !> - check varid for longitude_T
    !>
    !> @param[in] td_file file structure 
@@ -1793,8 +1810,8 @@
       !----------------------------------------------------------------
 
-      il_varid=var_get_index(td_file%t_var(:), 'longitude')
+      il_varid=var_get_id(td_file%t_var(:), 'longitude', 'longitude_T')
       IF( il_varid /= 0 )THEN
          ! read longitude on boundary
-         tl_var=iom_read_var(td_file, 'longitude')
+         tl_var=iom_read_var(td_file, il_varid)
       ELSE
          DO ji=1,td_file%i_nvar
@@ -1819,5 +1836,5 @@
    !> else return the size of the ovarlap band.
    !> East-West overlap is computed comparing longitude value of the  
-   !> South" part of the domain, to avoid  north fold boundary.
+   !> South part of the domain, to avoid  north fold boundary.
    !>
    !
@@ -1826,4 +1843,6 @@
    !> @date October, 2014
    !> - work on mpp file structure instead of file structure
+   !> @date October, 2016
+   !> - check varid for longitude_T
    !>
    !> @param[in] td_mpp mpp structure 
@@ -1850,7 +1869,7 @@
 
       ! read longitude on boundary
-      il_varid=var_get_index(td_mpp%t_proc(1)%t_var(:),'longitude')
+      il_varid=var_get_id(td_mpp%t_proc(1)%t_var(:),'longitude', 'longitude_T')
       IF( il_varid /= 0 )THEN
-         tl_var=iom_mpp_read_var(td_mpp, 'longitude')
+         tl_var=iom_mpp_read_var(td_mpp, il_varid)
       ELSE
          DO ji=1,td_mpp%t_proc(1)%i_nvar
@@ -1866,5 +1885,4 @@
          grid__get_ew_overlap_mpp=il_ew
       ENDIF
-
 
       ! clean
@@ -3020,4 +3038,123 @@
    !>
    !> @author J.Paul
+   !> @date April, 2016 - Initial Version
+   !> @date October, 2016
+   !> - use max of zero and east-west overlap instead of east-west overlap
+   !>
+   !> @param[in] td_coord0 coarse grid coordinate mpp structure
+   !> @param[in] dd_lon1   fine   grid longitude
+   !> @param[in] dd_lat1   fine   grid latitude
+   !> @param[in] cd_pos    relative position of grid point from point 
+   !> @param[in] dd_fill   fill value
+   !> @return coarse grid indices of closest point of fine grid point
+   !-------------------------------------------------------------------
+   FUNCTION grid__get_closest_str( td_coord0, dd_lon1, dd_lat1, cd_pos, dd_fill ) &
+   &  RESULT(id_res)
+
+      IMPLICIT NONE
+      ! Argument
+      TYPE(TMPP )     , INTENT(IN) :: td_coord0
+      REAL(dp),         INTENT(IN) :: dd_lon1
+      REAL(dp),         INTENT(IN) :: dd_lat1
+      CHARACTER(LEN=*), INTENT(IN), OPTIONAL :: cd_pos
+      REAL(dp),         INTENT(IN), OPTIONAL :: dd_fill
+
+      ! function
+      INTEGER(i4), DIMENSION(2) :: id_res
+
+      ! local variable
+      CHARACTER(LEN=lc)                        :: cl_point
+      CHARACTER(LEN=lc)                        :: cl_name
+
+      INTEGER(i4)                              :: il_ind
+      INTEGER(i4)                              :: il_ew
+
+      REAL(dp)   , DIMENSION(:,:), ALLOCATABLE :: dl_lon0
+      REAL(dp)   , DIMENSION(:,:), ALLOCATABLE :: dl_lat0
+
+      TYPE(TVAR)                               :: tl_lon0
+      TYPE(TVAR)                               :: tl_lat0
+      TYPE(TMPP)                               :: tl_coord0
+      !----------------------------------------------------------------
+
+      id_res(:)=-1
+      cl_point='T'
+
+      ! copy structure
+      tl_coord0=mpp_copy(td_coord0)
+
+      IF( .NOT. ASSOCIATED(tl_coord0%t_proc) )THEN
+
+         CALL logger_error("GRID GET CLOSEST: decompsition of mpp "//&
+         &  "file "//TRIM(tl_coord0%c_name)//" not defined." )
+
+      ELSE
+
+         ! open mpp files
+         CALL iom_mpp_open(tl_coord0)
+ 
+         ! read coarse longitue and latitude
+         WRITE(cl_name,*) 'longitude_'//TRIM(cl_point)
+         il_ind=var_get_id(tl_coord0%t_proc(1)%t_var(:), cl_name)
+         IF( il_ind == 0 )THEN
+            CALL logger_warn("GRID GET CLOSEST: no variable "//&
+            &  TRIM(cl_name)//"in file "//TRIM(tl_coord0%c_name)//". &
+            &  try to use longitude.")
+            WRITE(cl_name,*) 'longitude'
+         ENDIF
+         tl_lon0=iom_mpp_read_var(tl_coord0, TRIM(cl_name))
+ 
+         WRITE(cl_name,*) 'latitude_'//TRIM(cl_point)
+         il_ind=var_get_id(tl_coord0%t_proc(1)%t_var(:), cl_name)
+         IF( il_ind == 0 )THEN
+            CALL logger_warn("GRID GET CLOSEST: no variable "//&
+            &  TRIM(cl_name)//"in file "//TRIM(tl_coord0%c_name)//". &
+            &  try to use latitude.")
+            WRITE(cl_name,*) 'latitude'
+         ENDIF
+         tl_lat0=iom_mpp_read_var(tl_coord0, TRIM(cl_name))
+
+         ! close mpp files
+         CALL iom_mpp_close(tl_coord0)
+
+         il_ew=MAX(0,tl_coord0%i_ew)
+         ALLOCATE(dl_lon0(tl_coord0%t_dim(jp_I)%i_len-il_ew, &
+            &             tl_coord0%t_dim(jp_J)%i_len) )             
+         ALLOCATE(dl_lat0(tl_coord0%t_dim(jp_I)%i_len-il_ew, &
+            &             tl_coord0%t_dim(jp_J)%i_len) )
+
+         dl_lon0(:,:)=tl_lon0%d_value(il_ew+1:,:,1,1)
+         dl_lat0(:,:)=tl_lat0%d_value(il_ew+1:,:,1,1)
+
+         id_res(:)=grid_get_closest( dl_lon0, dl_lat0, dd_lon1, dd_lat1, cd_pos, dd_fill )
+
+         DEALLOCATE(dl_lon0, dl_lat0)
+         CALL var_clean(tl_lon0)
+         CALL var_clean(tl_lat0)
+         CALL mpp_clean(tl_coord0)
+
+      ENDIF
+
+   END FUNCTION  grid__get_closest_str
+   !-------------------------------------------------------------------
+   !> @brief This function return grid indices of the closest point
+   !> from point (lon1,lat1) 
+   !> 
+   !> @details
+   !>
+   !> @note overlap band should have been already removed from coarse grid array 
+   !> of longitude and latitude, before running this function
+   !>
+   !> if you add cd_pos argument, you could choice to return closest point at
+   !> - lower left  (ll) of the point
+   !> - lower right (lr) of the point
+   !> - upper left  (ul) of the point
+   !> - upper right (ur) of the point
+   !> - lower       (lo) of the point
+   !> - upper       (up) of the point
+   !> -       left  (le) of the point
+   !> -       right (ri) of the point
+   !>
+   !> @author J.Paul
    !> @date November, 2013 - Initial Version
    !> @date February, 2015
@@ -3034,5 +3171,5 @@
    !> @return coarse grid indices of closest point of fine grid point
    !-------------------------------------------------------------------
-   FUNCTION grid_get_closest( dd_lon0, dd_lat0, dd_lon1, dd_lat1, cd_pos, dd_fill )
+   FUNCTION grid__get_closest_arr( dd_lon0, dd_lat0, dd_lon1, dd_lat1, cd_pos, dd_fill )
       IMPLICIT NONE
       ! Argument
@@ -3045,5 +3182,5 @@
 
       ! function
-      INTEGER(i4), DIMENSION(2) :: grid_get_closest
+      INTEGER(i4), DIMENSION(2) :: grid__get_closest_arr
 
       ! local variable
@@ -3261,13 +3398,13 @@
          END SELECT
       ENDIF
-      grid_get_closest(:)=MINLOC(dl_dist(:,:),dl_dist(:,:)/=NF90_FILL_DOUBLE)
-
-      grid_get_closest(1)=grid_get_closest(1)+il_iinf-1
-      grid_get_closest(2)=grid_get_closest(2)+il_jinf-1
+      grid__get_closest_arr(:)=MINLOC(dl_dist(:,:),dl_dist(:,:)/=NF90_FILL_DOUBLE)
+
+      grid__get_closest_arr(1)=grid__get_closest_arr(1)+il_iinf-1
+      grid__get_closest_arr(2)=grid__get_closest_arr(2)+il_jinf-1
 
       DEALLOCATE( dl_dist )
       DEALLOCATE( dl_lon0 )
 
-   END FUNCTION grid_get_closest
+   END FUNCTION grid__get_closest_arr
    !-------------------------------------------------------------------
    !> @brief This function compute the distance between a point A and grid points.  
@@ -3475,5 +3612,5 @@
          ENDIF
          tl_lat0=iom_mpp_read_var(tl_coord0, TRIM(cl_name))
-         
+
          ! close mpp files
          CALL iom_mpp_close(tl_coord0)
@@ -4647,4 +4784,5 @@
 
       IF( ll_even )THEN
+
          ! look for variable value on domain for F point
          il_ind=var_get_index(tl_coord0%t_proc(1)%t_var(:), 'longitude_F')
