SUBROUTINE init_bottom_roughness
   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE init_bottom_roughness  ***
   !&E
   !&E ** Purpose : read a netcdf file containing spatial 
   !&E              descritption of the roughness length
   !&E
   !&E ** Description :
   !&E
   !&E ** Called by : comvars2d.F90
   !&E
   !&E ** External calls : 
   !&E
   !&E ** Used ij-arrays : 
   !&E
   !&E ** Modified variables : z0b
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !  2015-03-02 (F. Ganthy) Original code
   !&E
   !&E---------------------------------------------------------------------

#include "toolcpp.h"
   !! * Modules used
   USE parameters
   USE ionc4
   USE comvars2d
   USE comvars3d, ONLY : sig
#ifdef key_tide_saverotatedobc
   USE tidesaveobcrotated, ONLY : lon2d_child,lat2d_child,    &
                                  lon2du_child,lon2dv_child,  &
                                  lat2du_child,lat2dv_child,  &
                                  hx_child,hy_child,h0_child, &
                                  ig_child,id_child,jh_child,jb_child
#endif
#if defined key_siggen || defined key_gencoord
   USE comsiggen, ONLY : hc,hc_sig_g,hcx_sig_g,hcy_sig_g
#endif
   USE_MPI toolmpi,   ONLY : MASTER


   IMPLICIT NONE

   !! * Local declarations
   LOGICAL                                                  :: IERR_MPI
   INTEGER                                                  :: i,j
   REAL(KIND=rlg)                                           :: latmin_b,latmax_b,lonmin_b, &
                                                               lonmax_b,latmin_o,latmax_o, &
                                                               lonmin_o,lonmax_o
   REAL(KIND=rlg),DIMENSION(imin:imax,jmin:jmax)            :: lat_b,lon_b,lat_o,lon_o
   REAL(KIND=riolg),DIMENSION(imin:imax,jmin:jmax)          :: tmp2drlg
   REAL(KIND=riosh),DIMENSION(imin:imax,jmin:jmax)          :: zlit

   INTEGER :: nc_err,nc_id,ndims,ngatts,xdimid,var_id,var_type,var_ndims,var_natts,nvars
   INTEGER,DIMENSION(10):: var_ndim
   CHARACTER(len=30) :: var_name
   LOGICAL :: test

   !!----------------------------------------------------------------------
   !! * Executable part
   PRINT_DBG*, 'ENTER ROUTINE INIT_BOTTOM_ROUGHNESS'

   ! READING LAT LON FROM FILE_BATHY
   CALL ionc4_openr(file_bathy)
   CALL ionc4_read_xy(file_bathy,name_in_longitude,tmp2drlg,imin,imax,jmin,jmax)
   lon_b(:,:)=tmp2drlg(:,:)
   CALL ionc4_read_xy(file_bathy,name_in_latitude,tmp2drlg,imin,imax,jmin,jmax)
   lat_b(:,:)=tmp2drlg(:,:)
   CALL ionc4_close(file_bathy)

   latmax_b=maxval(maxval(lat_b,dim=2))
   latmin_b=minval(minval(lat_b,dim=2))
   lonmin_b=minval(minval(lon_b,dim=1))
   lonmax_b=maxval(maxval(lon_b,dim=1))

   ! READING LAT LON FROM FILE_Z0
   CALL ionc4_openr(file_z0b)
   CALL ionc4_read_xy(file_z0b,name_in_longitude,tmp2drlg,imin,imax,jmin,jmax)
   lon_o(:,:)=tmp2drlg(:,:)
   CALL ionc4_read_xy(file_z0b,name_in_latitude,tmp2drlg,imin,imax,jmin,jmax)
   lat_o(:,:)=tmp2drlg(:,:)
   CALL ionc4_close(file_z0b)

   latmax_o=maxval(maxval(lat_o,dim=2))
   latmin_o=minval(minval(lat_o,dim=2))
   lonmin_o=minval(minval(lon_o,dim=1))
   lonmax_o=maxval(maxval(lon_o,dim=1))

   ! TEST FOR VALID DOMAIN
   IF((ABS(latmax_b-latmax_o).GE.1.0E-10).OR.(ABS(latmin_b-latmin_o).GE.1.0E-10)  &
                                         .OR.(ABS(lonmax_b-lonmax_o).GE.1.0E-10)) THEN
     WRITE(ierrorlog,*) ' '
     WRITE(ierrorlog,*) '***********************************'
     WRITE(ierrorlog,*) 'Subroutine initbottomroughness     '
     WRITE(ierrorlog,*) 'When reading the file :            '
     WRITE(ierrorlog,*) TRIM(file_z0b)
     WRITE(ierrorlog,*) 'Latitude or longitude from the     '
     WRITE(ierrorlog,*) 'roughness file does not match      '
     WRITE(ierrorlog,*) 'Latitude or longitude defined      '
     WRITE(ierrorlog,*) 'by the bathymetry within the file :'
     WRITE(ierrorlog,*) TRIM(file_bathy)
     WRITE(ierrorlog,*) '     ---------------------         '
     WRITE(ierrorlog,*) ' FROM BATHYMETRY FILE:             '
     WRITE(ierrorlog,*) 'lonmin = ',lonmin_b
     WRITE(ierrorlog,*) 'lonmax = ',lonmax_b
     WRITE(ierrorlog,*) 'latmax = ',latmax_b
     WRITE(ierrorlog,*) 'latmin = ',latmin_b
     WRITE(ierrorlog,*) ' '
     WRITE(ierrorlog,*) ' FROM ROUGHNESS FILE :           '
     WRITE(ierrorlog,*) 'lonmin = ',lonmin_o
     WRITE(ierrorlog,*) 'lonmax = ',lonmax_o
     WRITE(ierrorlog,*) 'latmax = ',latmax_o
     WRITE(ierrorlog,*) 'latmin = ',latmin_o
     WRITE(ierrorlog,*) '     ---------------------         '
     WRITE(ierrorlog,*) '     SIMUMLATION IS STOPPED        '
     !CALL_MPI MPI_FINALIZE(IERR_MPI)
     !STOP
   ENDIF

   ! RETRIEVE AND CHECK VARIABLES NAME
   CALL ionc4_openr(file_z0b)
   CALL ionc4_corres(file_z0b,nc_id)
   nc_err = nf90_inquire(nc_id, ndims, nvars, ngatts, xdimid)
   test = .FALSE.
   DO var_id=1,nvars
     nc_err = nf90_inquire_variable(nc_id, var_id, var_name,var_type, var_ndims,var_ndim, var_natts)
     IF('z0b'.EQ.var_name) test=.TRUE.
   ENDDO
   IF (test) THEN
     ! READING OBSTRUCTIONS POSITIONS
     CALL ionc4_read_xy(file_z0b,'z0b',zlit,imin,imax,jmin,jmax)
     DO j=ljmin,ljmax
       DO i=limin,limax
         z0b(i,j) = zlit(i,j)
       ENDDO
     ENDDO
   ELSE
     WRITE(ierrorlog,*) ' '
     WRITE(ierrorlog,*) '***********************************'
     WRITE(ierrorlog,*) 'Subroutine initbottomroughness     '
     WRITE(ierrorlog,*) 'When reading the file :            '
     WRITE(ierrorlog,*) TRIM(file_z0b)
     WRITE(ierrorlog,*) 'Variables z0b not found within the '
     WRITE(ierrorlog,*) 'roughness file  '
     WRITE(ierrorlog,*) '     SIMUMLATION IS STOPPED        '
     CALL_MPI MPI_FINALIZE(IERR_MPI)
     STOP
   ENDIF

   CALL ionc4_close(file_z0b)

     PRINT_DBG*, 'END INIT_BOTTOM_ROUGHNESS'
END SUBROUTINE init_bottom_roughness
