 MODULE toolerror

   !!======================================================================
   !!                   ***  MODULE  tool_error  ***
   !! Error management : print error or warning messages
   !!
   !! ** History :
   !!       !  2009-11  (S. Michel, HOCER)  original code
   !!
   !!======================================================================
   !! * Modules used

#include "toolcpp.h"

   USE parameters,ONLY : rlg,rsh,riosh,riolg
   USE comvars2d, ONLY : iscreenlog,ierrorlog,iwarnlog
   USE_MPI toolmpi, ONLY : MASTER
   IMPLICIT NONE

   !! * Accessibility
   PUBLIC date_check,step_check ! routines called by output.F90
   PUBLIC para_check,para_error,para_warn,range_check   ! interfaces called by output.F90
   PUBLIC check_value_tab2d     ! routines called by initfromfile.F90

   !! * Interfaces
   INTERFACE para_check
     MODULE PROCEDURE dparacheck,rparacheck,iparacheck,lparacheck
   END INTERFACE para_check

   INTERFACE para_error
     MODULE PROCEDURE cparaerror,dparaerror,rparaerror,iparaerror,lparaerror
   END INTERFACE para_error

   INTERFACE para_warn
     MODULE PROCEDURE cparawarn,dparawarn,rparawarn,iparawarn,lparawarn
   END INTERFACE para_warn

   INTERFACE range_check
     MODULE PROCEDURE trangecheck,rrangecheck,irangecheck
   END INTERFACE range_check

   !! * Shared or public module variables

   !! * Private variables

 CONTAINS

   !!======================================================================

  SUBROUTINE date_check(timsec,charline,nerror,namfile,numfile)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE date_check  ***
   !&E
   !&E ** Purpose : extract a date and hour from a character line 
   !&E              and check its format
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used

   IMPLICIT NONE

   !! * Arguments
   REAL(KIND=rlg)           ,INTENT(out  ) :: timsec
   CHARACTER(LEN=*)         ,INTENT(inout) :: charline
   INTEGER         ,OPTIONAL,INTENT(inout) :: nerror
   CHARACTER(LEN=*),OPTIONAL,INTENT(in   ) :: namfile
   INTEGER         ,OPTIONAL,INTENT(in   ) :: numfile

   !! * Local declarations
   REAL(KIND=rlg)    :: tool_datosec

   !!----------------------------------------------------------------------
   !! * Executable part
   charline=ADJUSTL(charline) ! remove eventual spaces at the line beginning
   IF ((charline(3:3)/='/').OR.(charline(6:6)/='/').OR.(charline(11:11)/='').OR.&
       (charline(14:14)/=':').OR.(charline(17:17)/=':')) THEN
      WRITE(ierrorlog,*) ' '
      WRITE(ierrorlog,*) 'ERROR: Improper date format: ',TRIM(charline)
      WRITE(ierrorlog,*) 'The date must be in the form DD/MM/YYYY hh:mm:ss'
      IF (PRESENT(namfile)) WRITE(ierrorlog,*) 'in parameter file: ',TRIM(namfile)
      IF (PRESENT(numfile)) WRITE(ierrorlog,*) 'for file number: ',numfile
      IF (PRESENT(nerror)) nerror=nerror+1
    ELSE
      timsec=tool_datosec(charline(1:19))
    ENDIF

  END SUBROUTINE date_check

   !!======================================================================

  SUBROUTINE dparacheck(vecdbl,nsize,charline,nerror,namfile,numfile)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE dparacheck  ***
   !&E
   !&E ** Purpose : extract double precision reals from a character line
   !&E              and check its format
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used

   IMPLICIT NONE

   !! * Arguments
   INTEGER                          ,INTENT(in   ) :: nsize
   REAL(KIND=riolg),DIMENSION(nsize),INTENT(out  ) :: vecdbl
   CHARACTER(LEN=*)                 ,INTENT(inout) :: charline
   INTEGER         ,OPTIONAL        ,INTENT(inout) :: nerror
   CHARACTER(LEN=*),OPTIONAL        ,INTENT(in   ) :: namfile
   INTEGER         ,OPTIONAL        ,INTENT(in   ) :: numfile

   !! * Local declarations
   INTEGER :: i,istatus,nerror0,ispace

   !!----------------------------------------------------------------------
   !! * Executable part
   charline=ADJUSTL(charline) ! remove eventual spaces at the line beginning
   nerror0=0
   DO i=1,nsize
     ispace=INDEX(charline,' ')
#ifdef key_gfortran
!     READ(charline(1:ispace-1),'(Dw.d)',iostat=istatus) vecdbl(i)
     READ(charline(1:ispace-1),*,iostat=istatus) vecdbl(i)
#else
     READ(charline(1:ispace-1),'(d)',iostat=istatus) vecdbl(i)
#endif
     IF (istatus/=0) nerror0=nerror0+1
     charline=ADJUSTL(charline(ispace+1:))
   ENDDO
   IF (nerror0>0) THEN
      WRITE(ierrorlog,*) ' '
      WRITE(ierrorlog,*) 'ERROR: Improper numeric format: ',TRIM(charline)
      WRITE(ierrorlog,*) 'This line must contain ',nsize,' double precision real(s)'
      IF (PRESENT(namfile)) WRITE(ierrorlog,*) 'in parameter file: ',TRIM(namfile)
      IF (PRESENT(numfile)) WRITE(ierrorlog,*) 'for file number: ',numfile
      IF (PRESENT(nerror)) nerror=nerror+1
    ENDIF

  END SUBROUTINE dparacheck

   !!======================================================================

  SUBROUTINE rparacheck(vecreal,nsize,charline,nerror,namfile,numfile)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE rparacheck  ***
   !&E
   !&E ** Purpose : extract single precision reals from a character line
   !&E              and check its format
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used

   IMPLICIT NONE

   !! * Arguments
   INTEGER                          ,INTENT(in   ) :: nsize
   REAL(KIND=riosh),DIMENSION(nsize),INTENT(out  ) :: vecreal
   CHARACTER(LEN=*)                 ,INTENT(inout) :: charline
   INTEGER         ,OPTIONAL        ,INTENT(inout) :: nerror
   CHARACTER(LEN=*),OPTIONAL        ,INTENT(in   ) :: namfile
   INTEGER         ,OPTIONAL        ,INTENT(in   ) :: numfile

   !! * Local declarations
   INTEGER :: i,istatus,nerror0,ispace

   !!----------------------------------------------------------------------
   !! * Executable part
   charline=ADJUSTL(charline) ! remove eventual spaces at the line beginning
   nerror0=0
   DO i=1,nsize
     ispace=INDEX(charline,' ')
#ifdef key_gfortran
!     READ(charline(1:ispace-1),'(Fw.d)',iostat=istatus) vecreal(i)
     READ(charline(1:ispace-1),*,iostat=istatus) vecreal(i)
#else
     READ(charline(1:ispace-1),'(f)',iostat=istatus) vecreal(i)
#endif
     IF (istatus/=0) nerror0=nerror0+1
     charline=ADJUSTL(charline(ispace+1:))
   ENDDO
   IF (nerror0>0) THEN
      WRITE(ierrorlog,*) ' '
      WRITE(ierrorlog,*) 'ERROR: Improper numeric format: ',TRIM(charline)
      WRITE(ierrorlog,*) 'This line must contain ',nsize,' single precision real(s)'
      IF (PRESENT(namfile)) WRITE(ierrorlog,*) 'in parameter file: ',TRIM(namfile)
      IF (PRESENT(numfile)) WRITE(ierrorlog,*) 'for file number: ',numfile
      IF (PRESENT(nerror)) nerror=nerror+1
    ENDIF

  END SUBROUTINE rparacheck

   !!======================================================================

  SUBROUTINE iparacheck(vecint,nsize,charline,nerror,namfile,numfile)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE iparacheck  ***
   !&E
   !&E ** Purpose : extract integers from a character line
   !&E              and check its format
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used

   IMPLICIT NONE

   !! * Arguments
   INTEGER                          ,INTENT(in   ) :: nsize
   INTEGER         ,DIMENSION(nsize),INTENT(out  ) :: vecint
   CHARACTER(LEN=*)                 ,INTENT(inout) :: charline
   INTEGER         ,OPTIONAL        ,INTENT(inout) :: nerror
   CHARACTER(LEN=*),OPTIONAL        ,INTENT(in   ) :: namfile
   INTEGER         ,OPTIONAL        ,INTENT(in   ) :: numfile

   !! * Local declarations
   INTEGER :: i,istatus,nerror0,ispace

   !!----------------------------------------------------------------------
   !! * Executable part
   charline=ADJUSTL(charline) ! remove eventual spaces at the line beginning
   nerror0=0
   DO i=1,nsize
     ispace=INDEX(charline,' ')
#ifdef key_gfortran
!     READ(charline(1:ispace-1),'(Iw)',iostat=istatus) vecint(i)
     READ(charline(1:ispace-1),*,iostat=istatus) vecint(i)
#else
     READ(charline(1:ispace-1),'(i)',iostat=istatus) vecint(i)
#endif
     IF (istatus/=0) nerror0=nerror0+1
     charline=ADJUSTL(charline(ispace+1:))
   ENDDO
   IF (nerror0>0) THEN
      WRITE(ierrorlog,*) ' '
      WRITE(ierrorlog,*) 'ERROR: Improper numeric format: ',TRIM(charline)
      WRITE(ierrorlog,*) 'This line must contain ',nsize,' integer(s)'
      IF (PRESENT(namfile)) WRITE(ierrorlog,*) 'in parameter file: ',TRIM(namfile)
      IF (PRESENT(numfile)) WRITE(ierrorlog,*) 'for file number: ',numfile
      IF (PRESENT(nerror)) nerror=nerror+1
    ENDIF

  END SUBROUTINE iparacheck

   !!======================================================================

  SUBROUTINE lparacheck(veclog,nsize,charline,nerror,namfile,numfile)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE lparacheck  ***
   !&E
   !&E ** Purpose : extract logicals from a character line
   !&E              and check its format
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used

   IMPLICIT NONE

   !! * Arguments
   INTEGER                          ,INTENT(in   ) :: nsize
   LOGICAL         ,DIMENSION(nsize),INTENT(out  ) :: veclog
   CHARACTER(LEN=*)                 ,INTENT(inout) :: charline
   INTEGER         ,OPTIONAL        ,INTENT(inout) :: nerror
   CHARACTER(LEN=*),OPTIONAL        ,INTENT(in   ) :: namfile
   INTEGER         ,OPTIONAL        ,INTENT(in   ) :: numfile

   !! * Local declarations
   INTEGER :: i,istatus,nerror0,ispace

   !!----------------------------------------------------------------------
   !! * Executable part
   charline=ADJUSTL(charline) ! remove eventual spaces at the line beginning
   nerror0=0
   DO i=1,nsize
     ispace=INDEX(charline,' ')
     READ(charline(1:ispace-1),'(l6)',iostat=istatus) veclog(i)
     IF (istatus/=0) nerror0=nerror0+1
     charline=ADJUSTL(charline(ispace+1:))
   ENDDO
   IF (nerror0>0) THEN
      WRITE(ierrorlog,*) ' '
      WRITE(ierrorlog,*) 'ERROR: Improper text format: ',TRIM(charline)
      WRITE(ierrorlog,*) 'This line must contain ',nsize,' logical(s) (.true. or .false.)'
      IF (PRESENT(namfile)) WRITE(ierrorlog,*) 'in parameter file: ',TRIM(namfile)
      IF (PRESENT(numfile)) WRITE(ierrorlog,*) 'for file number: ',numfile
      IF (PRESENT(nerror)) nerror=nerror+1
    ENDIF

  END SUBROUTINE lparacheck

   !!======================================================================

  SUBROUTINE cparaerror(valchar,namchar,namfile,numfile)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE cparaerror  ***
   !&E
   !&E ** Purpose : print an error message and stop the simulation
   !&E              in a case of an uncorrect characters string parameter
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used

   IMPLICIT NONE

   !! * Arguments
   CHARACTER(LEN=*)         ,INTENT(in) :: valchar
   CHARACTER(LEN=*)         ,INTENT(in) :: namchar
   CHARACTER(LEN=*),OPTIONAL,INTENT(in) :: namfile
   INTEGER         ,OPTIONAL,INTENT(in) :: numfile

   !! * Local declarations

   !!----------------------------------------------------------------------
   !! * Executable part
   WRITE(ierrorlog,*) ' '
   WRITE(ierrorlog,*) 'ERROR: The "',TRIM(namchar),'" text parameter'
   WRITE(ierrorlog,*) 'has a forbidden value'
   IF (PRESENT(namfile)) WRITE(ierrorlog,*) 'in parameter file: ',TRIM(namfile)
   IF (PRESENT(numfile)) WRITE(ierrorlog,*) 'for file number: ',numfile
   WRITE(ierrorlog,*) TRIM(namchar),' = ',valchar
   WRITE(ierrorlog,*) 'Simulation stopped.'
   STOP

  END SUBROUTINE cparaerror

   !!======================================================================

  SUBROUTINE dparaerror(valdbl,namdbl,namfile,numfile)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE dparaerror  ***
   !&E
   !&E ** Purpose : print an error message and stop the simulation
   !&E              in a case of an uncorrect double precision real parameter
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used

   IMPLICIT NONE

   !! * Arguments
   REAL(KIND=riolg)         ,INTENT(in) :: valdbl
   CHARACTER(LEN=*)         ,INTENT(in) :: namdbl
   CHARACTER(LEN=*),OPTIONAL,INTENT(in) :: namfile
   INTEGER         ,OPTIONAL,INTENT(in) :: numfile

   !! * Local declarations

   !!----------------------------------------------------------------------
   !! * Executable part
   WRITE(ierrorlog,*) ' '
   WRITE(ierrorlog,*) 'ERROR: The "',TRIM(namdbl),'" double precision parameter'
   WRITE(ierrorlog,*) 'has a forbidden value'
   IF (PRESENT(namfile)) WRITE(ierrorlog,*) 'in parameter file: ',TRIM(namfile)
   IF (PRESENT(numfile)) WRITE(ierrorlog,*) 'for file number: ',numfile
   WRITE(ierrorlog,*) TRIM(namdbl),' = ',valdbl
   WRITE(ierrorlog,*) 'Simulation stopped.'
   STOP

  END SUBROUTINE dparaerror

   !!======================================================================

  SUBROUTINE rparaerror(valreal,namreal,namfile,numfile)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE rparaerror  ***
   !&E
   !&E ** Purpose : print an error message and stop the simulation
   !&E              in a case of an uncorrect single precision real parameter
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used

   IMPLICIT NONE

   !! * Arguments
   REAL(KIND=riosh)         ,INTENT(in) :: valreal
   CHARACTER(LEN=*)         ,INTENT(in) :: namreal
   CHARACTER(LEN=*),OPTIONAL,INTENT(in) :: namfile
   INTEGER         ,OPTIONAL,INTENT(in) :: numfile

   !! * Local declarations

   !!----------------------------------------------------------------------
   !! * Executable part
   WRITE(ierrorlog,*) ' '
   WRITE(ierrorlog,*) 'ERROR: The "',TRIM(namreal),'" single precision parameter'
   WRITE(ierrorlog,*) 'has a forbidden value'
   IF (PRESENT(namfile)) WRITE(ierrorlog,*) 'in parameter file: ',TRIM(namfile)
   IF (PRESENT(numfile)) WRITE(ierrorlog,*) 'for file number: ',numfile
   WRITE(ierrorlog,*) TRIM(namreal),' = ',valreal
   WRITE(ierrorlog,*) 'Simulation stopped.'
   STOP

  END SUBROUTINE rparaerror

   !!======================================================================

  SUBROUTINE iparaerror(valint,namint,namfile,numfile)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE iparaerror  ***
   !&E
   !&E ** Purpose : print an error message and stop the simulation
   !&E              in a case of an uncorrect integer parameter
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used

   IMPLICIT NONE

   !! * Arguments
   INTEGER                  ,INTENT(in) :: valint
   CHARACTER(LEN=*)         ,INTENT(in) :: namint
   CHARACTER(LEN=*),OPTIONAL,INTENT(in) :: namfile
   INTEGER         ,OPTIONAL,INTENT(in) :: numfile

   !! * Local declarations

   !!----------------------------------------------------------------------
   !! * Executable part
   WRITE(ierrorlog,*) ' '
   WRITE(ierrorlog,*) 'ERROR: The "',TRIM(namint),'" integer parameter'
   WRITE(ierrorlog,*) 'has a forbidden value'
   IF (PRESENT(namfile)) WRITE(ierrorlog,*) 'in parameter file: ',TRIM(namfile)
   IF (PRESENT(numfile)) WRITE(ierrorlog,*) 'for file number: ',numfile
   WRITE(ierrorlog,*) TRIM(namint),' = ',valint
   WRITE(ierrorlog,*) 'Simulation stopped.'
   STOP

  END SUBROUTINE iparaerror

   !!======================================================================

  SUBROUTINE lparaerror(vallog,namlog,namfile,numfile)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE lparaerror  ***
   !&E
   !&E ** Purpose : print an error message and stop the simulation
   !&E              in a case of an uncorrect logical parameter
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used

   IMPLICIT NONE

   !! * Arguments
   LOGICAL                  ,INTENT(in) :: vallog
   CHARACTER(LEN=*)         ,INTENT(in) :: namlog
   CHARACTER(LEN=*),OPTIONAL,INTENT(in) :: namfile
   INTEGER         ,OPTIONAL,INTENT(in) :: numfile

   !! * Local declarations

   !!----------------------------------------------------------------------
   !! * Executable part
   WRITE(ierrorlog,*) ' '
   WRITE(ierrorlog,*) 'ERROR: The "',TRIM(namlog),'" logical parameter'
   WRITE(ierrorlog,*) 'has a forbidden value'
   IF (PRESENT(namfile)) WRITE(ierrorlog,*) 'in parameter file: ',TRIM(namfile)
   IF (PRESENT(numfile)) WRITE(ierrorlog,*) 'for file number: ',numfile
   WRITE(ierrorlog,*) TRIM(namlog),' = ',vallog
   WRITE(ierrorlog,*) 'Simulation stopped.'
   STOP

  END SUBROUTINE lparaerror

   !!======================================================================

  SUBROUTINE cparawarn(valchar,namchar,nwarn,namfile,numfile)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE cparawarn  ***
   !&E
   !&E ** Purpose : print a warning message in case of missing or multiple
   !&E              definitions of a characters string parameter
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used

   IMPLICIT NONE

   !! * Arguments
   CHARACTER(LEN=*)         ,INTENT(in) :: valchar
   CHARACTER(LEN=*)         ,INTENT(in) :: namchar
   INTEGER                  ,INTENT(in) :: nwarn
   CHARACTER(LEN=*),OPTIONAL,INTENT(in) :: namfile
   INTEGER         ,OPTIONAL,INTENT(in) :: numfile

   !! * Local declarations

   !!----------------------------------------------------------------------
   !! * Executable part
   IF (nwarn/=1) THEN
     IF_MPI (MASTER) THEN
     WRITE(iwarnlog,*) ' '
     WRITE(iwarnlog,*) 'WARNING: The "',TRIM(namchar),'" text parameter'
     IF (PRESENT(namfile)) WRITE(iwarnlog,*) 'in parameter file: ',TRIM(namfile)
     IF (PRESENT(numfile)) WRITE(iwarnlog,*) 'for file number: ',numfile
     IF (nwarn==0) THEN
       WRITE(iwarnlog,*) 'has not been defined.'
       WRITE(iwarnlog,*) 'This parameter is set to its default value:'
     ELSE
       WRITE(iwarnlog,*) 'has been defined ',nwarn,' times.'
       WRITE(iwarnlog,*) 'This parameter is set to its last value:'
     ENDIF
     WRITE(iwarnlog,*) TRIM(namchar),' = ',TRIM(valchar)
     ENDIF_MPI
   ENDIF

  END SUBROUTINE cparawarn

   !!======================================================================

  SUBROUTINE dparawarn(valdbl,namdbl,nwarn,namfile,numfile)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE dparawarn  ***
   !&E
   !&E ** Purpose : print a warning message in case of missing or multiple
   !&E              definitions of a double precision real parameter
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used

   IMPLICIT NONE

   !! * Arguments
   REAL(KIND=riolg)         ,INTENT(in) :: valdbl
   CHARACTER(LEN=*)         ,INTENT(in) :: namdbl
   INTEGER                  ,INTENT(in) :: nwarn
   CHARACTER(LEN=*),OPTIONAL,INTENT(in) :: namfile
   INTEGER         ,OPTIONAL,INTENT(in) :: numfile

   !! * Local declarations

   !!----------------------------------------------------------------------
   !! * Executable part
   IF (nwarn/=1) THEN
     IF_MPI (MASTER) THEN
     WRITE(iwarnlog,*) ' '
     WRITE(iwarnlog,*) 'WARNING: The "',TRIM(namdbl),'" double precision parameter'
     IF (PRESENT(namfile)) WRITE(iwarnlog,*) 'in parameter file: ',TRIM(namfile)
     IF (PRESENT(numfile)) WRITE(iwarnlog,*) 'for file number: ',numfile
     IF (nwarn==0) THEN
       WRITE(iwarnlog,*) 'has not been defined.'
       WRITE(iwarnlog,*) 'This parameter is set to its default value:'
     ELSE
       WRITE(iwarnlog,*) 'has been defined ',nwarn,' times.'
       WRITE(iwarnlog,*) 'This parameter is set to its last value:'
     ENDIF
     WRITE(iwarnlog,*) TRIM(namdbl),' = ',valdbl
     ENDIF_MPI
   ENDIF

  END SUBROUTINE dparawarn

   !!======================================================================

  SUBROUTINE rparawarn(valreal,namreal,nwarn,namfile,numfile)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE rparawarn  ***
   !&E
   !&E ** Purpose : print a warning message in case of missing or multiple
   !&E              definitions of a single precision real parameter
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used

   IMPLICIT NONE

   !! * Arguments
   REAL(KIND=riosh)         ,INTENT(in) :: valreal
   CHARACTER(LEN=*)         ,INTENT(in) :: namreal
   INTEGER                  ,INTENT(in) :: nwarn
   CHARACTER(LEN=*),OPTIONAL,INTENT(in) :: namfile
   INTEGER         ,OPTIONAL,INTENT(in) :: numfile

   !! * Local declarations

   !!----------------------------------------------------------------------
   !! * Executable part
   IF (nwarn/=1) THEN
     IF_MPI (MASTER) THEN
     WRITE(iwarnlog,*) ' '
     WRITE(iwarnlog,*) 'WARNING: The "',TRIM(namreal),'" single precision parameter'
     IF (PRESENT(namfile)) WRITE(iwarnlog,*) 'in parameter file: ',TRIM(namfile)
     IF (PRESENT(numfile)) WRITE(iwarnlog,*) 'for file number: ',numfile
     IF (nwarn==0) THEN
       WRITE(iwarnlog,*) 'has not been defined.'
       WRITE(iwarnlog,*) 'This parameter is set to its default value:'
     ELSE
       WRITE(iwarnlog,*) 'has been defined ',nwarn,' times.'
       WRITE(iwarnlog,*) 'This parameter is set to its last value:'
     ENDIF
     WRITE(iwarnlog,*) TRIM(namreal),' = ',valreal
     ENDIF_MPI
   ENDIF

  END SUBROUTINE rparawarn

   !!======================================================================

  SUBROUTINE iparawarn(valint,namint,nwarn,namfile,numfile)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE iparawarn  ***
   !&E
   !&E ** Purpose : print a warning message in case of missing or multiple
   !&E              definitions of a integer parameter
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used

   IMPLICIT NONE

   !! * Arguments
   INTEGER                  ,INTENT(in) :: valint
   CHARACTER(LEN=*)         ,INTENT(in) :: namint
   INTEGER                  ,INTENT(in) :: nwarn
   CHARACTER(LEN=*),OPTIONAL,INTENT(in) :: namfile
   INTEGER         ,OPTIONAL,INTENT(in) :: numfile

   !! * Local declarations

   !!----------------------------------------------------------------------
   !! * Executable part
   IF (nwarn/=1) THEN
     IF_MPI (MASTER) THEN
     WRITE(iwarnlog,*) ' '
     WRITE(iwarnlog,*) 'WARNING: The "',TRIM(namint),'" logical parameter'
     IF (PRESENT(namfile)) WRITE(iwarnlog,*) 'in parameter file: ',TRIM(namfile)
     IF (PRESENT(numfile)) WRITE(iwarnlog,*) 'for file number: ',numfile
     IF (nwarn==0) THEN
       WRITE(iwarnlog,*) 'has not been defined.'
       WRITE(iwarnlog,*) 'This parameter is set to its default value:'
     ELSE
       WRITE(iwarnlog,*) 'has been defined ',nwarn,' times.'
       WRITE(iwarnlog,*) 'This parameter is set to its last value:'
     ENDIF
     WRITE(iwarnlog,*) TRIM(namint),' = ',valint
     ENDIF_MPI
   ENDIF

  END SUBROUTINE iparawarn

   !!======================================================================

  SUBROUTINE lparawarn(vallog,namlog,nwarn,namfile,numfile)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE lparawarn  ***
   !&E
   !&E ** Purpose : print a warning message in case of missing or multiple
   !&E              definitions of a logical parameter
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used

   IMPLICIT NONE

   !! * Arguments
   LOGICAL                  ,INTENT(in) :: vallog
   CHARACTER(LEN=*)         ,INTENT(in) :: namlog
   INTEGER                  ,INTENT(in) :: nwarn
   CHARACTER(LEN=*),OPTIONAL,INTENT(in) :: namfile
   INTEGER         ,OPTIONAL,INTENT(in) :: numfile

   !! * Local declarations

   !!----------------------------------------------------------------------
   !! * Executable part
   IF (nwarn/=1) THEN
     IF_MPI (MASTER) THEN
     WRITE(iwarnlog,*) ' '
     WRITE(iwarnlog,*) 'WARNING: The "',TRIM(namlog),'" logical parameter'
     IF (PRESENT(namfile)) WRITE(iwarnlog,*) 'in parameter file: ',TRIM(namfile)
     IF (PRESENT(numfile)) WRITE(iwarnlog,*) 'for file number: ',numfile
     IF (nwarn==0) THEN
       WRITE(iwarnlog,*) 'has not been defined.'
       WRITE(iwarnlog,*) 'This parameter is set to its default value:'
     ELSE
       WRITE(iwarnlog,*) 'has been defined ',nwarn,' times.'
       WRITE(iwarnlog,*) 'This parameter is set to its last value:'
     ENDIF
     WRITE(iwarnlog,*) TRIM(namlog),' = ',vallog
     ENDIF_MPI
   ENDIF

  END SUBROUTINE lparawarn

   !!======================================================================

  SUBROUTINE trangecheck(tbeg,tstart,tstop,tend,namdate,namfile,numfile)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE trangecheck  ***
   !&E
   !&E ** Purpose : check that a time interval ranges between
   !&E              the beginning and the end of the simulation
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used

   IMPLICIT NONE

   !! * Arguments
   REAL(KIND=rlg)           ,INTENT(in)    :: tbeg,tend
   REAL(KIND=rlg)           ,INTENT(inout) :: tstart,tstop
   CHARACTER(LEN=*)         ,INTENT(in)    :: namdate
   CHARACTER(LEN=*),OPTIONAL,INTENT(in)    :: namfile
   INTEGER         ,OPTIONAL,INTENT(in)    :: numfile

   !! * Local declarations
   CHARACTER(LEN=19) :: tool_sectodat,datebeg,datestart,datestop,dateend

   !!----------------------------------------------------------------------
   !! * Executable part
   datebeg  =tool_sectodat(tbeg)
   datestart=tool_sectodat(tstart)
   datestop =tool_sectodat(tstop)
   dateend  =tool_sectodat(tend)

   ! Check the start date.
   IF ((tstart<tbeg).OR.(tstart>tend)) THEN
     IF_MPI (MASTER) THEN
     WRITE(iwarnlog,*) ' '
     WRITE(iwarnlog,*) 'WARNING: The start "',TRIM(namdate),'" date is uncorrect'
     IF (PRESENT(namfile)) WRITE(iwarnlog,*) 'in parameter file: ',TRIM(namfile)
     IF (PRESENT(numfile)) WRITE(iwarnlog,*) 'for file number: ',numfile
     ENDIF_MPI
     IF (tstart<tbeg) THEN
       IF_MPI (MASTER) THEN
       WRITE(iwarnlog,*) 'The start date is anterior to the simulation beginning:'
       WRITE(iwarnlog,*) 'date_start',TRIM(namdate),' = ',TRIM(datestart), &
                         ' < date_beg = ',TRIM(datebeg)
       WRITE(iwarnlog,*) 'The start date is set to the simulation beginning.'
       ENDIF_MPI
       tstart=tbeg
     ELSEIF (tstart>tend) THEN
       WRITE(ierrorlog,*) 'The start date is posterior to the simulation end:'
       WRITE(ierrorlog,*) 'date_start',TRIM(namdate),' = ',TRIM(datestart), &
                         ' > date_end = ',TRIM(dateend)
       WRITE(ierrorlog,*) 'The valid date interval is empty.'
       WRITE(ierrorlog,*) 'Simulation stopped.'
       STOP
     ENDIF
   ENDIF

   ! Check the end date.
   IF ((tstop>tend).OR.(tstop<tstart)) THEN
     IF_MPI (MASTER) THEN
     WRITE(iwarnlog,*) ' '
     WRITE(iwarnlog,*) 'WARNING: The stop "',TRIM(namdate),'" date is uncorrect'
     IF (PRESENT(namfile)) WRITE(iwarnlog,*) 'in parameter file: ',TRIM(namfile)
     IF (PRESENT(numfile)) WRITE(iwarnlog,*) 'for file number: ',numfile
     ENDIF_MPI
     IF (tstop>tend) THEN
       IF_MPI (MASTER) THEN
       WRITE(iwarnlog,*) 'The stop date is posterior to the simulation end:'
       WRITE(iwarnlog,*) 'date_stop',TRIM(namdate),' = ',TRIM(datestop), &
                         ' > date_end = ',TRIM(dateend)
       WRITE(iwarnlog,*) 'The stop date is set to the simulation end.'
       ENDIF_MPI
       tstop=tend
     ELSEIF (tstop<tstart) THEN
       WRITE(ierrorlog,*) 'The stop date is anterior to the start date:'
       WRITE(ierrorlog,*) 'date_stop',TRIM(namdate),' = ',TRIM(datestop), &
                         ' < date_start',TRIM(namdate),' = ',TRIM(datestart)
       WRITE(ierrorlog,*) 'The valid date interval is empty.'
       WRITE(ierrorlog,*) 'Simulation stopped.'
       STOP
!       tstop=tstart
     ENDIF
   ENDIF

  END SUBROUTINE trangecheck

   !!======================================================================

  SUBROUTINE rrangecheck(rbottom,rlow,rhigh,rtop,namcoord,namfile,numfile)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE rrangecheck  ***
   !&E
   !&E ** Purpose : check that a real interval ranges between
   !&E              the min and max coordinates of the domain.
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used

   IMPLICIT NONE

   !! * Arguments
   REAL(KIND=riosh)         ,INTENT(in)    :: rbottom,rtop
   REAL(KIND=riosh)         ,INTENT(inout) :: rlow,rhigh
   CHARACTER(LEN=*)         ,INTENT(in)    :: namcoord
   CHARACTER(LEN=*),OPTIONAL,INTENT(in)    :: namfile
   INTEGER         ,OPTIONAL,INTENT(in)    :: numfile

   !! * Local declarations

   !!----------------------------------------------------------------------
   !! * Executable part

   ! Ckeck the lower coordinate.
   IF ((rlow<rbottom).OR.(rlow>rtop)) THEN
     IF_MPI (MASTER) THEN
     WRITE(iwarnlog,*) ' '
     WRITE(iwarnlog,*) 'WARNING: The lower "',TRIM(namcoord),'" coordinate is uncorrect'
     IF (PRESENT(namfile)) WRITE(iwarnlog,*) 'in parameter file: ',TRIM(namfile)
     IF (PRESENT(numfile)) WRITE(iwarnlog,*) 'for file number: ',numfile
     ENDIF_MPI
     IF (rlow<rbottom) THEN
       IF_MPI (MASTER) THEN
       WRITE(iwarnlog,*) 'The lower coordinate is inferior to the valid minimum:'
       WRITE(iwarnlog,*) TRIM(namcoord),'min = ',rlow,' < ',namcoord(1:3),'min = ',rbottom
       WRITE(iwarnlog,*) 'The lower coordinate is set to "',namcoord(1:3),'min" value.'
       ENDIF_MPI
       rlow=rbottom
     ELSEIF (rlow>rtop) THEN
       IF_MPI (MASTER) THEN
       WRITE(iwarnlog,*) 'The lower coordinate is superior to the valid maximum:'
       WRITE(iwarnlog,*) TRIM(namcoord),'min = ',rlow,' > ',namcoord(1:3),'max = ',rtop
       WRITE(iwarnlog,*) 'The lower coordinate is set to "',namcoord(1:3),'max" value.'
       ENDIF_MPI
       rlow=rtop
     ENDIF
   ENDIF

   ! Ckeck the higher coordinate.
   IF ((rhigh>rtop).OR.(rhigh<rlow)) THEN
     IF_MPI (MASTER) THEN
     WRITE(iwarnlog,*) ' '
     WRITE(iwarnlog,*) 'WARNING: The higher "',TRIM(namcoord),'" coordinate is uncorrect'
     IF (PRESENT(namfile)) WRITE(iwarnlog,*) 'in parameter file: ',TRIM(namfile)
     IF (PRESENT(numfile)) WRITE(iwarnlog,*) 'for file number: ',numfile
     ENDIF_MPI
     IF (rhigh>rtop) THEN
       IF_MPI (MASTER) THEN
       WRITE(iwarnlog,*) 'The higher coordinate is superior to the valid maximum:'
       WRITE(iwarnlog,*) TRIM(namcoord),'max = ',rhigh,' > ',namcoord(1:3),'max = ',rtop
       WRITE(iwarnlog,*) 'The higher coordinate is set to "',namcoord(1:3),'max" value.'
       ENDIF_MPI
       rhigh=rtop
     ELSEIF (rtop<rlow) THEN
       IF_MPI (MASTER) THEN
       WRITE(iwarnlog,*) 'The higher coordinate is inferior to the lower coordinate:'
       WRITE(iwarnlog,*) TRIM(namcoord),'max = ',rhigh,' < ',TRIM(namcoord),'min = ',rlow
       WRITE(iwarnlog,*) 'The higher coordinate is set to the lower coordinate.'
       ENDIF_MPI
       rhigh=rlow
     ENDIF
   ENDIF

  END SUBROUTINE rrangecheck

   !!======================================================================

  SUBROUTINE irangecheck(ibottom,ilow,ihigh,itop,namindex,namfile,numfile)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE irangecheck  ***
   !&E
   !&E ** Purpose : check that an index interval ranges between
   !&E              the min and max index of the domain.
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used

   IMPLICIT NONE

   !! * Arguments
   INTEGER                  ,INTENT(in)    :: ibottom,itop
   INTEGER                  ,INTENT(inout) :: ilow,ihigh
   CHARACTER(LEN=*)         ,INTENT(in)    :: namindex
   CHARACTER(LEN=*),OPTIONAL,INTENT(in)    :: namfile
   INTEGER         ,OPTIONAL,INTENT(in)    :: numfile

   !! * Local declarations

   !!----------------------------------------------------------------------
   !! * Executable part

   ! Ckeck the lower index.
   IF ((ilow<ibottom).OR.(ilow>itop)) THEN
     IF_MPI (MASTER) THEN
     WRITE(iwarnlog,*) ' '
     WRITE(iwarnlog,*) 'WARNING: The lower "',TRIM(namindex),'" index is uncorrect'
     IF (PRESENT(namfile)) WRITE(iwarnlog,*) 'in parameter file: ',TRIM(namfile)
     IF (PRESENT(numfile)) WRITE(iwarnlog,*) 'for file number: ',numfile
     ENDIF_MPI
     IF (ilow<ibottom) THEN
       IF_MPI (MASTER) THEN
       WRITE(iwarnlog,*) 'The lower index is inferior to the valid minimum:'
       WRITE(iwarnlog,*) TRIM(namindex),'min = ',ilow,' < ',namindex(1:1),'min = ',ibottom
       WRITE(iwarnlog,*) 'The lower index is set to "',namindex(1:1),'min" value.'
       ENDIF_MPI
       ilow=ibottom
     ELSEIF (ilow>itop) THEN
       IF_MPI (MASTER) THEN
       WRITE(iwarnlog,*) 'The lower index is superior to the valid maximum:'
       WRITE(iwarnlog,*) TRIM(namindex),'min = ',ilow,' > ',namindex(1:1),'max = ',itop
       WRITE(iwarnlog,*) 'The lower index is set to "',namindex(1:1),'max" value.'
       ENDIF_MPI
       ilow=itop
     ENDIF
   ENDIF

   ! Ckeck the higher index.
   IF ((ihigh>itop).OR.(ihigh<ilow)) THEN
     IF_MPI (MASTER) THEN
     WRITE(iwarnlog,*) ' '
     WRITE(iwarnlog,*) 'WARNING: The higher "',TRIM(namindex),'" index is uncorrect'
     IF (PRESENT(namfile)) WRITE(iwarnlog,*) 'in parameter file: ',TRIM(namfile)
     IF (PRESENT(numfile)) WRITE(iwarnlog,*) 'for file number: ',numfile
     ENDIF_MPI
     IF (ihigh>itop) THEN
       IF_MPI (MASTER) THEN
       WRITE(iwarnlog,*) 'The higher index is superior to the valid maximum:'
       WRITE(iwarnlog,*) TRIM(namindex),'max = ',ihigh,' > ',namindex(1:1),'max = ',itop
       WRITE(iwarnlog,*) 'The higher index is set to "',namindex(1:1),'max" value.'
       ENDIF_MPI
       ihigh=itop
     ELSEIF (itop<ilow) THEN
       IF_MPI (MASTER) THEN
       WRITE(iwarnlog,*) 'The higher index is inferior to the lower index:'
       WRITE(iwarnlog,*) TRIM(namindex),'max = ',ihigh,' < ',TRIM(namindex),'min = ',ilow
       WRITE(iwarnlog,*) 'The higher index is set to the lower index.'
       ENDIF_MPI
       ihigh=ilow
     ENDIF
   ENDIF

  END SUBROUTINE irangecheck

   !!======================================================================

  SUBROUTINE step_check(nmin,nstep,nmax,namstep,namfile,numfile)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE step_check  ***
   !&E
   !&E ** Purpose : check that a sampling step ranges between
   !&E              the grid step and the dimension of the domain.
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used

   IMPLICIT NONE

   !! * Arguments
   INTEGER                  ,INTENT(in)    :: nmin,nmax
   INTEGER                  ,INTENT(inout) :: nstep
   CHARACTER(LEN=*)         ,INTENT(in)    :: namstep
   CHARACTER(LEN=*),OPTIONAL,INTENT(in)    :: namfile
   INTEGER         ,OPTIONAL,INTENT(in)    :: numfile

   !! * Local declarations

   !!----------------------------------------------------------------------
   !! * Executable part

   ! Ckeck the sampling step.
   IF ((nstep<nmin).OR.(nstep>nmax)) THEN
     IF_MPI (MASTER) THEN
     WRITE(iwarnlog,*) ' '
     WRITE(iwarnlog,*) 'WARNING: The lower "',TRIM(namstep),'" step is uncorrect'
     IF (PRESENT(namfile)) WRITE(iwarnlog,*) 'in parameter file: ',TRIM(namfile)
     IF (PRESENT(numfile)) WRITE(iwarnlog,*) 'for file number: ',numfile
     ENDIF_MPI
     IF (nstep<nmin) THEN
       IF_MPI (MASTER) THEN
       WRITE(iwarnlog,*) 'The step is inferior to the grid step:'
       WRITE(iwarnlog,*) TRIM(namstep),' = ',nstep,' < ',namstep(1:1),'grid = ',nmin
       WRITE(iwarnlog,*) 'The step is set to the grid step.'
       ENDIF_MPI
       nstep=nmin
     ELSEIF (nstep>nmax) THEN
       IF_MPI (MASTER) THEN
       WRITE(iwarnlog,*) 'The step is superior to the domain extension:'
       WRITE(iwarnlog,*) TRIM(namstep),' = ',nstep,' > ',namstep(1:1),'dim = ',nmax
       WRITE(iwarnlog,*) 'The step is set to the domain extension.'
       ENDIF_MPI
       nstep=nmax
     ENDIF
   ENDIF

  END SUBROUTINE step_check

   !!======================================================================

  SUBROUTINE check_value_tab2d(tab,mask,value_land,varname,valid_min,valid_max)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE check_value_tab2d  ***
   !&E
   !&E ** Purpose : check that values of a 2D array (liminm2:limaxp2,ljminm2:ljmaxp2)
   !&E              are correct on wet-points.
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used
   USE parameters,ONLY : liminm2,limaxp2,ljminm2,ljmaxp2,liminp1,ljminp1

   !! * Arguments
   CHARACTER(LEN=*)         ,INTENT(in)    :: varname
   REAL(KIND=riosh)         ,INTENT(in)    :: valid_min,valid_max
   REAL(KIND=rsh)           ,INTENT(in)    :: value_land
   REAL(KIND=rsh), DIMENSION(liminm2:limaxp2,ljminm2:ljmaxp2), INTENT(inout) :: tab
   REAL(KIND=rsh), DIMENSION(liminm2:limaxp2,ljminm2:ljmaxp2), INTENT(in)    :: mask

   !! * Local declarations
   INTEGER :: IERR_MPI

   !!----------------------------------------------------------------------
   !! * Executable part

   WHERE (MASK(liminm2:limaxp2,ljminm2:ljmaxp2) == value_land) tab(liminm2:limaxp2,ljminm2:ljmaxp2)=0.0_rsh
   IF (MAXVAL(tab(liminp1:limaxp2,ljminp1:ljmaxp2)) > valid_max .OR. MINVAL(tab(liminp1:limaxp2,ljminp1:ljmaxp2)) < valid_min) THEN
     WRITE(ierrorlog,*) 'ERROR : Initfromfile : the values of ',TRIM(varname),' are not correct on wet point'
     WRITE(ierrorlog,*) 'MAX value : ', MAXVAL(tab(liminp1:limaxp2,ljminp1:ljmaxp2))
     WRITE(ierrorlog,*) 'MIN value : ', MINVAL(tab(liminp1:limaxp2,ljminp1:ljmaxp2))
     WRITE(ierrorlog,*) 'while VALID_MAX value is ',valid_max,' and VALID_MIN value is ',valid_min
     WRITE(ierrorlog,*) 'Are the results from extract corrects ?'
     IF_AGRIF (.NOT.Agrif_Root()) THEN
     WRITE(ierrorlog,*) 'Simulation stopped'
     CALL_MPI MPI_FINALIZE(IERR_MPI)
     STOP
     ENDIF_AGRIF
   END IF

  END SUBROUTINE check_value_tab2d

   !!======================================================================

  SUBROUTINE check_value_tab2d_u(tab,mask,value_land,varname,valid_min,valid_max)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE check_value_tab2d_u  ***
   !&E
   !&E ** Purpose : check that values of a 2D array (liminm2:limaxp2,ljminm2:ljmaxp2)
   !&E              are correct on wet-points.
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used
   USE parameters,ONLY : liminm2,limaxp2,ljminm2,ljmaxp2,ljminp1

   !! * Arguments
   CHARACTER(LEN=*)         ,INTENT(in)    :: varname
   REAL(KIND=riosh)         ,INTENT(in)    :: valid_min,valid_max
   REAL(KIND=rsh)           ,INTENT(in)    :: value_land
   REAL(KIND=rsh), DIMENSION(liminm2:limaxp2,ljminm2:ljmaxp2), INTENT(inout) :: tab
   REAL(KIND=rsh), DIMENSION(liminm2:limaxp2,ljminm2:ljmaxp2), INTENT(in)    :: mask

   !! * Local declarations
   INTEGER :: IERR_MPI

   !!----------------------------------------------------------------------
   !! * Executable part

   WHERE (MASK(liminm2:limaxp2,ljminm2:ljmaxp2) == value_land) tab(liminm2:limaxp2,ljminm2:ljmaxp2)=0.0_rsh
   IF (MAXVAL(tab(liminm2:limaxp2,ljminp1:ljmaxp2)) > valid_max .OR. MINVAL(tab(liminm2:limaxp2,ljminp1:ljmaxp2)) < valid_min) THEN
     WRITE(ierrorlog,*) 'ERROR : Initfromfile : the values of ',TRIM(varname),' are not correct on wet point'
     WRITE(ierrorlog,*) 'MAX value : ', MAXVAL(tab(liminm2:limaxp2,ljminp1:ljmaxp2))
     WRITE(ierrorlog,*) 'MIN value : ', MINVAL(tab(liminm2:limaxp2,ljminp1:ljmaxp2))
     WRITE(ierrorlog,*) 'while VALID_MAX value is ',valid_max,' and VALID_MIN value is ',valid_min
     WRITE(ierrorlog,*) 'Are the results from extract corrects ?'
     WRITE(ierrorlog,*) 'Simulation stopped'
     CALL_MPI MPI_FINALIZE(IERR_MPI)
     STOP
   END IF

  END SUBROUTINE check_value_tab2d_u

   !!======================================================================

  SUBROUTINE check_value_tab2d_v(tab,mask,value_land,varname,valid_min,valid_max)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE check_value_tab2d_v  ***
   !&E
   !&E ** Purpose : check that values of a 2D array (liminm2:limaxp2,ljminm2:ljmaxp2)
   !&E              are correct on wet-points.
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used
   USE parameters,ONLY : liminm2,limaxp2,ljminm2,ljmaxp2,liminp1

   !! * Arguments
   CHARACTER(LEN=*)         ,INTENT(in)    :: varname
   REAL(KIND=riosh)         ,INTENT(in)    :: valid_min,valid_max
   REAL(KIND=rsh)           ,INTENT(in)    :: value_land
   REAL(KIND=rsh), DIMENSION(liminm2:limaxp2,ljminm2:ljmaxp2), INTENT(inout) :: tab
   REAL(KIND=rsh), DIMENSION(liminm2:limaxp2,ljminm2:ljmaxp2), INTENT(in)    :: mask

   !! * Local declarations
   INTEGER :: IERR_MPI

   !!----------------------------------------------------------------------
   !! * Executable part

   WHERE (MASK(liminm2:limaxp2,ljminm2:ljmaxp2) == value_land) tab(liminm2:limaxp2,ljminm2:ljmaxp2)=0.0_rsh
   IF (MAXVAL(tab(liminp1:limaxp2,ljminm2:ljmaxp2)) > valid_max .OR. MINVAL(tab(liminp1:limaxp2,ljminm2:ljmaxp2)) < valid_min) THEN
     WRITE(ierrorlog,*) 'ERROR : Initfromfile : the values of ',TRIM(varname),' are not correct on wet point'
     WRITE(ierrorlog,*) 'MAX value : ', MAXVAL(tab(liminp1:limaxp2,ljminm2:ljmaxp2))
     WRITE(ierrorlog,*) 'MIN value : ', MINVAL(tab(liminp1:limaxp2,ljminm2:ljmaxp2))
     WRITE(ierrorlog,*) 'while VALID_MAX value is ',valid_max,' and VALID_MIN value is ',valid_min
     WRITE(ierrorlog,*) 'Are the results from extract corrects ?'
     WRITE(ierrorlog,*) 'Simulation stopped'
     CALL_MPI MPI_FINALIZE(IERR_MPI)
     STOP
   END IF

  END SUBROUTINE check_value_tab2d_v

   !!======================================================================

 END MODULE toolerror
