  SUBROUTINE init_fromfile(u,uz,v,vz,xev,sal,temp,              &
                                intz_exp_vstar,igdv_old,        &
#ifdef key_substance
                                         cv_wat,                &
#endif
                                         ect,psi,eps)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE init_fromfile  ***
   !&E
   !&E ** Purpose : read model variables from a NetCDF file to restart a simulation
   !&E
   !&E ** Description :
   !&E
   !&E ** Called by : step
   !&E
   !&E ** External calls : ionc4_openr, ionc4_read_time, ionc4_read
   !&E                     ionc4_read_subxyt, ionc4_read_subzxyt
   !&E                     ionc4_read_subxyt, ionc4_read_subzxyt
   !&E                     ionc4_close
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !           (F. Dumas)  Original code
   !&E       !  2007-12  (V. Garnier) Management of simple and double precision
   !&E       !  2008-08  (T. Odaka) MPI
   !&E       !  2008-10  (V. Garnier) output managing relative to displacement of init_fromfile from step to main
   !&E       !  2009-10  (V. Garnier) reproductibility when using restart file
   !&E       !  2009-11  (S. Michel, HOCER) check the consistency of bathy and Z coord.
   !&E       !  2011-01 (R. Ramel Alyotech) Ionc library in fortran 90/95, netcdf4 and MPI
   !&E       !  2011-11  (V. Garnier) Spatial extension of ssh(liminm2:limaxp2,ljminm2:ljmaxp2)
   !&E
   !&E---------------------------------------------------------------------

#include "toolcpp.h"

   !! * Modules used
   USE parameters
   USE comvars2d
   USE comvars3d
   USE comfluxdo
   USE comturb,      ONLY : cmu
   USE ionc4, ONLY : ionc4_openr,ionc4_read_time,ionc4_read,ionc4_read_t,ionc4_read_subxyt,&
                     ionc4_read_subzxyt,ionc4_read_subxy,ionc4_read_z,ionc4_close
   USE output,ONLY : tsort,tinter,tdebsor,nb_out
#if defined key_siggen || defined key_gencoord
   USE comsiggen,    ONLY : hzex,hzey,hc_sig,Csu_sig,Csw_sig,b_sig,theta_sig
#endif
#ifdef key_substance
   USE comsubstance, ONLY : cini_wat,nv_state,init_cv_name,nv_tot,name_var,irk_fil,unit_modif_mudbio_N2dw
#ifdef key_parsub_macho3d
   USE comsubstance, ONLY : nb_ws_max,nbouc_machop
#endif
#endif
   USE toolerror,    ONLY : check_value_tab2d,check_value_tab2d_u,check_value_tab2d_v
   USE_MPI toolmpi,  ONLY : ex_i_rsh,ex_j_rsh,MASTER
#if defined key_agrif && ! defined key_agrif_1way
   USE_MPI toolmpi,  ONLY : ex_i_rlg,ex_j_rlg
#endif

   IMPLICIT NONE

   !! * Arguments
   INTEGER,       INTENT(out),DIMENSION(     limin:limax,ljmin:ljmax)         :: igdv_old
   REAL(kind=rsh),INTENT(out),DIMENSION(liminm2:limaxp2,ljminm2:ljmaxp2) :: xev
   REAL(kind=rsh),INTENT(out),DIMENSION(     liminm2:limaxp2,ljminm2:ljmaxp2) :: u,v
   REAL(kind=rsh),INTENT(out),DIMENSION(     limin  :limax  ,ljmin  :ljmax  ) :: intz_exp_vstar
   REAL(kind=rsh),INTENT(out),DIMENSION(kmax,liminm2:limaxp2,ljminm2:ljmaxp2) :: uz,vz
   REAL(kind=rsh),INTENT(out),DIMENSION(kmax,limin:limax,ljmin:ljmax) :: ect,psi
   REAL(kind=rsh),INTENT(inout),DIMENSION(kmax,limin:limax,ljmin:ljmax) :: eps
   REAL(kind=rsh),INTENT(out),DIMENSION(kmax,liminm1:limaxp2,ljminm1:ljmaxp2) ::sal,temp
#ifdef key_substance
   REAL(kind=rsh),INTENT(out),DIMENSION(nb_var,kmax,liminm1:limaxp2,ljminm1:ljmaxp2) :: cv_wat
#endif

   !! * Local declarations
   INTEGER                                              :: l,n,ncerror,nmax
    ! ligne suivante a eliminer avec nv version de agrif ?
   INTEGER                                              :: i,j,k,irec,kmin
   REAL(kind=rsh)                                       :: difmin,difmax,hp
   REAL(kind=rsh)  ,DIMENSION(kmax,liminm2:limaxp2,ljminm2:ljmaxp2) :: tmp_rsh
   REAL(kind=rsh)  ,DIMENSION(0:kmax,liminm2:limaxp2,ljminm2:ljmaxp2) :: tmp0_rsh
   REAL(kind=rlg)  ,DIMENSION(kmax,liminm2:limaxp2,ljminm2:ljmaxp2) :: tmp_rlg
   REAL(kind=rsh)  ,DIMENSION(liminm2:limaxp2,ljminm2:ljmaxp2)      :: tmp_2d_rsh
   REAL(kind=rsh)  ,DIMENSION(liminm1:limaxp1,ljminm1:ljmaxp1) :: ref_2d_rsh
   REAL(kind=riosh),DIMENSION(liminm1:limaxp1,ljminm1:ljmaxp1)      :: tmpigd
   REAL(KIND=riosh)  ,DIMENSION(0:kmax)                 :: sigtmp
   REAL(KIND=rsh)  ,DIMENSION(0:kmax+1)                 :: sigref
   REAL(KIND=rsh)                                       :: valtmp,valref
   CHARACTER(LEN=19)                                    :: tool_sectodat,date
   CHARACTER(LEN=lchain)                                :: namvar
   REAL(KIND=rlg)                                       :: tdb,tfi
#if defined key_substance && defined key_parsub_macho3d
   CHARACTER(LEN=2)  :: namiv
   CHARACTER(LEN=15) :: nam
#endif

   !*****************************************************
   !     Executable statements
   !*****************************************************

   PRINT_DBG*, 'enter INIT_FROMFILE routine'

   irec=1

   CALL ionc4_openr(file_init,l_out_nc4par=.TRUE.)

  ! read time and time-step
   IF (l_init_rtime) CALL ionc4_read_time(file_init,1,t)
#ifdef key_agrif
   tdebagrif_messat=t
#endif
   IF (l_init_restart .AND. .NOT.l_modele2d) THEN
     CALL ionc4_read(file_init,'tdebobs',tdebobs)
     CALL ionc4_read_subxyt(file_init,'igdv_old',tmpigd,liminm1,limaxp1,ljminm1,ljmaxp1,irec,0,0)
     igdv_old(limin:limax,ljmin:ljmax)=NINT(tmpigd(limin:limax,ljmin:ljmax))
#ifdef key_tssub_macho3d
     CALL ionc4_read(file_init,'nbouc_macho',nbouc_macho)
#endif
   END IF
   IF (l_init_rdt) CALL ionc4_read(file_init,name_in_dt,dt)
   halfdt=0.5_rlg*dt

   IF (t<tdeb) THEN
     WRITE(ierrorlog,*) ' '
     WRITE(ierrorlog,*) 'ERROR: INITFROMFILE.F90 subroutine'
     WRITE(ierrorlog,*) 'The variable "date_start" is uncorrect,'
     WRITE(ierrorlog,*) 'because the date in the restart file is earlier.'
     WRITE(ierrorlog,*) 'Simulation stopped.'
     date = tool_sectodat(t)
     WRITE(ierrorlog,*) 'The date is ',date,' in the restart file'
     date = tool_sectodat(tdeb)
     WRITE(ierrorlog,*) 'while date_start is ',date
     STOP
   ELSE IF (.NOT.l_modele2d .AND. t<tdeb3d) THEN
     IF_MPI (MASTER) THEN
     date = tool_sectodat(t)
     WRITE(iwarnlog,*) ' '
     WRITE(iwarnlog,*) 'WARNING: INITFROMFILE.F90 subroutine'
     WRITE(iwarnlog,*) 'The run will start as a 2D simulation'
     WRITE(iwarnlog,*) 'from the date of the restart file : ',date
     date = tool_sectodat(tdeb3d)
     WRITE(iwarnlog,*) 'and the 3D simulation will start at ',date
     ENDIF_MPI
   END IF

     IF (l_init_restart .AND. .NOT.l_modele2d) THEN
       CALL ionc4_read_t(file_init,'cflumaxmax',cflumaxmax,irec)
       CALL ionc4_read_t(file_init,'cflvmaxmax',cflvmaxmax,irec)
     END IF

     IF (l_init_rssh) THEN
       CALL ionc4_read_subxyt(file_init,name_in_xev,tmp_2d_rsh,liminm2,limaxp2,ljminm2,ljmaxp2,irec,0,0,fillvar=valmanq)
       xev(liminm2:limaxp2,ljminm2:ljmaxp2) = tmp_2d_rsh(liminm2:limaxp2,ljminm2:ljmaxp2)
       CALL check_value_tab2d(tmp_2d_rsh(liminm2:limaxp2,ljminm2:ljmaxp2),h0(liminm2:limaxp2,ljminm2:ljmaxp2),-valmanq, &
                              TRIM(name_in_xev),riog_valid_min_xe,riog_valid_max_xe)
     END IF

   ! Initialize height variables for vertical integration
     CALL init_height(xev)

     IF (l_init_rbtvel) THEN
       CALL ionc4_read_subxyt(file_init,name_in_u,tmp_2d_rsh,liminm2,limaxp2,ljminm2,ljmaxp2,irec,0,0,fillvar=0.0_rsh)
       WHERE (hx(liminm2:limaxp2,ljminm2:ljmaxp2) /= -valmanq -h0fond) &
         u(liminm2:limaxp2,ljminm2:ljmaxp2) = tmp_2d_rsh(liminm2:limaxp2,ljminm2:ljmaxp2)
       CALL check_value_tab2d_u(tmp_2d_rsh(liminm2:limaxp2,ljminm2:ljmaxp2),hx(liminm2:limaxp2,ljminm2:ljmaxp2),-valmanq-h0fond, &
                              TRIM(name_in_u),riog_valid_min_u,riog_valid_max_u)
       CALL ionc4_read_subxyt(file_init,name_in_v,tmp_2d_rsh,liminm2,limaxp2,ljminm2,ljmaxp2,irec,0,0,fillvar=0.0_rsh)
       WHERE (hy(liminm2:limaxp2,ljminm2:ljmaxp2) /= -valmanq -h0fond) &
         v(liminm2:limaxp2,ljminm2:ljmaxp2) = tmp_2d_rsh(liminm2:limaxp2,ljminm2:ljmaxp2)
       CALL check_value_tab2d_v(tmp_2d_rsh(liminm2:limaxp2,ljminm2:ljmaxp2),hy(liminm2:limaxp2,ljminm2:ljmaxp2),-valmanq-h0fond, &
                              TRIM(name_in_v),riog_valid_min_u,riog_valid_max_u)
     END IF

     IF (l_init_restart .AND. .NOT.l_modele2d) THEN
       CALL ionc4_read_subxyt(file_init,'intz_exp_vstar',tmp_2d_rsh,liminm2,limaxp2,ljminm2,ljmaxp2,irec,0,0)
       intz_exp_vstar(limin:limax,ljmin:ljmax) = tmp_2d_rsh(limin:limax,ljmin:ljmax)
     END IF

     IF (l_init_r3dvel) THEN
       CALL ionc4_read_subzxyt(file_init,name_in_uz,tmp_rsh,liminm2,limaxp2,ljminm2,ljmaxp2,1,kmax,irec,0,0,1,fillvar=0.0_rsh)
       DO k=1,kmax
         WHERE (hx(liminm2:limaxp2,ljminm2:ljmaxp2) /= -valmanq -h0fond) &
           uz(k,liminm2:limaxp2,ljminm2:ljmaxp2) = tmp_rsh(k,liminm2:limaxp2,ljminm2:ljmaxp2)
       END DO

       tmp_2d_rsh(liminm2:limaxp2,ljminm2:ljmaxp2)=MAXVAL( ABS(uz(:,liminm2:limaxp2,ljminm2:ljmaxp2)) , dim=1 )
       CALL check_value_tab2d_u(tmp_2d_rsh(liminm2:limaxp2,ljminm2:ljmaxp2),hx(liminm2:limaxp2,ljminm2:ljmaxp2),-valmanq-h0fond, &
                              TRIM(name_in_uz),riog_valid_min_u,riog_valid_max_u)

       CALL ionc4_read_subzxyt(file_init,name_in_vz,tmp_rsh,liminm2,limaxp2,ljminm2,ljmaxp2,1,kmax,irec,0,0,1,fillvar=0.0_rsh)
       DO k=1,kmax
         WHERE (hy(liminm2:limaxp2,ljminm2:ljmaxp2) /= -valmanq -h0fond) &
           vz(k,liminm2:limaxp2,ljminm2:ljmaxp2) = tmp_rsh(k,liminm2:limaxp2,ljminm2:ljmaxp2)
       END DO
       tmp_2d_rsh(liminm2:limaxp2,ljminm2:ljmaxp2)=MAXVAL( ABS(vz(:,liminm2:limaxp2,ljminm2:ljmaxp2)) , dim=1 )
       CALL check_value_tab2d_v(tmp_2d_rsh(liminm2:limaxp2,ljminm2:ljmaxp2),hy(liminm2:limaxp2,ljminm2:ljmaxp2),-valmanq-h0fond, &
                              TRIM(name_in_vz),riog_valid_min_u,riog_valid_max_u)

       IF (.NOT.l_init_rbtvel) THEN
         DO j=ljmin,ljmax
           DO i=limin,limax
#if defined key_siggen || defined key_gencoord
             IF (hex(i,j)/=0.0_rsh) &
               u(i,j)=SUM(uz(:,i,j)*hzex(:,i,j)*dsigu(:))/hex(i,j)
             IF (hey(i,j)/=0.0_rsh) &
               v(i,j)=SUM(vz(:,i,j)*hzey(:,i,j)*dsigu(:))/hey(i,j)

#else
             u(i,j)=SUM(uz(:,i,j)*dsigu(:))
             v(i,j)=SUM(vz(:,i,j)*dsigu(:))
#endif
           END DO
         END DO
       END IF
     END IF
     IF (l_init_rwz) THEN
       CALL ionc4_read_subzxyt(file_init,name_in_wz,tmp0_rsh,liminm2,limaxp2,ljminm2,ljmaxp2,0,kmax,irec,0,0,0,fillvar=0.0_rsh)
       DO k=1,kmax
         WHERE (h0(liminm1:limaxp1,ljminm1:ljmaxp1) /= -valmanq -h0fond) &
           wz(k,liminm1:limaxp1,ljminm1:ljmaxp1)=tmp0_rsh(k,liminm1:limaxp1,ljminm1:ljmaxp1)
       END DO
     END IF
     IF (l_init_rsal) THEN
       CALL ionc4_read_subzxyt(file_init,name_in_sal,tmp_rsh,liminm2,limaxp2,ljminm2,ljmaxp2,1,kmax,irec,0,0,1,fillvar=valmanq)
       sal(:,liminm1:limaxp2,ljminm1:ljmaxp2) = tmp_rsh(:,liminm1:limaxp2,ljminm1:ljmaxp2)
       tmp_2d_rsh(liminm2:limaxp2,ljminm2:ljmaxp2)=MAXVAL( ABS(tmp_rsh(:,liminm2:limaxp2,ljminm2:ljmaxp2)) , dim=1 )
       CALL check_value_tab2d(tmp_2d_rsh(liminm2:limaxp2,ljminm2:ljmaxp2),h0(liminm2:limaxp2,ljminm2:ljmaxp2),-valmanq, &
                              TRIM(name_in_sal),riog_valid_min_sal,riog_valid_max_sal)
     END IF
     IF (l_init_rtemp) THEN
       CALL ionc4_read_subzxyt(file_init,name_in_temp,tmp_rsh,liminm2,limaxp2,ljminm2,ljmaxp2,1,kmax,irec,0,0,1,fillvar=valmanq)
       temp(:,liminm1:limaxp2,ljminm1:ljmaxp2) = tmp_rsh(:,liminm1:limaxp2,ljminm1:ljmaxp2)
       tmp_2d_rsh(liminm2:limaxp2,ljminm2:ljmaxp2)=MAXVAL( ABS(tmp_rsh(:,liminm2:limaxp2,ljminm2:ljmaxp2)) , dim=1 )
       CALL check_value_tab2d(tmp_2d_rsh(liminm2:limaxp2,ljminm2:ljmaxp2),h0(liminm2:limaxp2,ljminm2:ljmaxp2),-valmanq, &
                              TRIM(name_in_temp),riog_valid_min_temp,riog_valid_max_temp)
     END IF
     IF (l_init_rturb) THEN
       CALL ionc4_read_subzxyt(file_init,name_in_ect,tmp_rsh,liminm2,limaxp2,ljminm2,ljmaxp2,1,kmax,irec,0,0,1,fillvar=valmanq)
       ect(:,limin:limax,ljmin:ljmax) = tmp_rsh(:,limin:limax,ljmin:ljmax)

       CALL ionc4_read_subzxyt(file_init,name_in_psi,tmp_rsh,liminm2,limaxp2,ljminm2,ljmaxp2,1,kmax,irec,0,0,1,fillvar=valmanq)
       psi(:,limin:limax,ljmin:ljmax) = tmp_rsh(:,limin:limax,ljmin:ljmax)

       IF (turb_nbeq == 2) THEN
         DO j=ljminp2,ljmaxm1
           DO i=MAX0(limin,ig(j)+1),MIN0(limax,id(j)-1)

             hp=h0(i,j)+xev(i,j)
             IF (hp > hm) THEN
               IF (turb_2eq_option==1) eps(:,i,j)=(cmu**3)*(ect(:,i,j)**2.5_rsh)/psi(:,i,j)
               IF (turb_2eq_option==2) eps(:,i,j)=psi(:,i,j)
               IF (turb_2eq_option==3) eps(:,i,j)=cmu**4*ect(:,i,j)*psi(:,i,j)
               IF (turb_2eq_option==4) eps(:,i,j)=psi(:,i,j)**1.5_rsh
               eps(:,i,j) = MAX(eps(:,i,j),1.e-12)
             END IF
           END DO
         END DO
       END IF

       CALL ionc4_read_subzxyt(file_init,name_in_kz,tmp0_rsh,liminm2,limaxp2,ljminm2,ljmaxp2,0,kmax,irec,0,0,0,fillvar=valmanq)
       DO j=ljminm1,ljmaxp1
       DO i=liminm1,limaxp1
       DO k=1,kmax
         kz(k,i,j)=tmp0_rsh(k,i,j)
       END DO
       END DO
       END DO
       tmp_2d_rsh(liminm2:limaxp2,ljminm2:ljmaxp2)=MAXVAL( ABS(tmp_rsh(:,liminm2:limaxp2,ljminm2:ljmaxp2)) , dim=1 )
       CALL check_value_tab2d(tmp_2d_rsh(liminm2:limaxp2,ljminm2:ljmaxp2),h0(liminm2:limaxp2,ljminm2:ljmaxp2),-valmanq, &
                              TRIM(name_in_kz),riog_valid_min_kz,riog_valid_max_kz)

       CALL ionc4_read_subzxyt(file_init,name_in_nz,tmp0_rsh,liminm2,limaxp2,ljminm2,ljmaxp2,0,kmax,irec,0,0,0,fillvar=valmanq)
       DO j=ljmin,ljmaxp1
       DO i=limin,limaxp1
       DO k=1,kmax
         nz(k,i,j)=tmp0_rsh(k,i,j)
       END DO
       END DO
       END DO
       tmp_2d_rsh(liminm2:limaxp2,ljminm2:ljmaxp2)=MAXVAL( ABS(tmp_rsh(:,liminm2:limaxp2,ljminm2:ljmaxp2)) , dim=1 )
       CALL check_value_tab2d(tmp_2d_rsh(liminm2:limaxp2,ljminm2:ljmaxp2),h0(liminm2:limaxp2,ljminm2:ljmaxp2),-valmanq, &
                              TRIM(name_in_nz),riog_valid_min_nz,riog_valid_max_nz)

       CALL ionc4_read_subxyt(file_init,name_in_vis2d,tmp_2d_rsh,liminm2,limaxp2,ljminm2,ljmaxp2,irec,0,0,fillvar=valmanq)
       DO j=ljminm1,ljmaxp1
       DO i=liminm1,limaxp1
         vis2d(i,j)=tmp_2d_rsh(i,j)
       END DO
       END DO
       CALL check_value_tab2d(tmp_2d_rsh(liminm2:limaxp2,ljminm2:ljmaxp2),h0(liminm2:limaxp2,ljminm2:ljmaxp2),-valmanq, &
                              TRIM(name_in_vis2d),riog_valid_min_visc,riog_valid_max_visc)

       IF (.NOT.l_modele2d) THEN
       CALL ionc4_read_subzxyt(file_init,name_in_vis3d_xe,tmp_rsh,liminm2,limaxp2,ljminm2,ljmaxp2,1,kmax,irec,0,0,1,fillvar=valmanq)
!cva       vish_xe(:,:,ljminm1:ljmaxp1)=tmp_rsh(:,:,ljminm1:ljmaxp1)
 ! du a agrif
       DO j=ljmin,ljmaxp1
       DO i=limin,limaxp1
       DO k=1,kmax
         vish_xe(k,i,j)=tmp_rsh(k,i,j)
       END DO
       END DO
       END DO
       tmp_2d_rsh(liminm2:limaxp2,ljminm2:ljmaxp2)=MAXVAL( ABS(tmp_rsh(:,liminm2:limaxp2,ljminm2:ljmaxp2)) , dim=1 )
       CALL check_value_tab2d(tmp_2d_rsh(liminm2:limaxp2,ljminm2:ljmaxp2),h0(liminm2:limaxp2,ljminm2:ljmaxp2),-valmanq, &
                              TRIM(name_in_vis3d_xe),riog_valid_min_visc,riog_valid_max_visc)

       CALL ionc4_read_subzxyt(file_init,name_in_vis3d_phi,tmp_rsh,liminm2,limaxp2,ljminm2,ljmaxp2,1,kmax,irec,0,0,1, &
                               fillvar=valmanq)
!cva       vish_phi(:,:,ljminm1:ljmaxp1)=tmp_rsh(:,:,ljminm1:ljmaxp1)
 ! du a agrif
       DO j=ljminm1,ljmax
       DO i=liminm1,limax
       DO k=1,kmax
         vish_phi(k,i,j)=tmp_rsh(k,i,j)
       END DO
       END DO
       END DO
       END IF
       tmp_2d_rsh(liminm2:limaxp2,ljminm2:ljmaxp2)=MAXVAL( ABS(tmp_rsh(:,liminm2:limaxp2,ljminm2:ljmaxp2)) , dim=1 )
       CALL check_value_tab2d(tmp_2d_rsh(liminm2:limaxp2,ljminm2:ljmaxp2),h0(liminm2:limaxp2,ljminm2:ljmaxp2),-valmanq, &
                              TRIM(name_in_vis3d_phi),riog_valid_min_visc,riog_valid_max_visc)

     END IF
!
#ifdef key_substance
#if defined key_parsub_macho3d
     IF (l_init_restart) THEN
       DO l=1,nb_ws_max
         write(namiv,"(i2)")l
         nam='nbouc_machop_'//namiv
         CALL ionc4_read(file_init,nam,nbouc_machop(l))
       ENDDO
     ENDIF
#endif
     IF (l_restart_subs) THEN
       DO l = 1,nv_tot
         CALL ionc4_read_subzxyt(file_init,name_var(irk_fil(l)),tmp_rsh,liminm2,limaxp2,ljminm2,ljmaxp2,1,kmax,irec,0,0,1, &
                                 fillvar=valmanq)
         DO j=ljminm1,ljmaxp2
         DO i=liminm1,limaxp2
         DO k=1,kmax
           cv_wat(l,k,i,j) = tmp_rsh(k,i,j)
         END DO
         END DO
         END DO
       ENDDO
     ELSE
       DO l=1,nv_state
         IF (TRIM(init_cv_name(l))/='none') THEN
           CALL ionc4_read_subzxyt(file_init,init_cv_name(l),tmp_rsh,liminm2,limaxp2,ljminm2,ljmaxp2,1,kmax,irec,0,0,1, &
                                   fillvar=valmanq)
           cv_wat(l,:,liminm1:limaxp2,ljminm1:ljmaxp2)=tmp_rsh(:,liminm1:limaxp2,ljminm1:ljmaxp2)*unit_modif_mudbio_N2dw(irk_fil(l))
         ELSE
           cv_wat(l,:,liminm1:limaxp2,ljminm1:ljmaxp2)=cini_wat(l)
         END IF
       ENDDO
     END IF
#endif

     ! Compare the current bathymetry and the bathymetry read from the restart file.
     nmax=3
#if defined key_siggen || defined key_gencoord
     nmax=4
#endif
     DO n=1,nmax
       IF (n==1) THEN
         namvar=name_bathy_h0
         ref_2d_rsh(liminm1:limaxp1,ljminm1:ljmaxp1)=h0(liminm1:limaxp1,ljminm1:ljmaxp1)
       ELSEIF (n==2 .AND. l_init_restart) THEN
         namvar=name_bathy_hx
         ref_2d_rsh(liminm1:limaxp1,ljminm1:ljmaxp1)=hx(liminm1:limaxp1,ljminm1:ljmaxp1)
       ELSEIF (n==3 .AND. l_init_restart) THEN
         namvar=name_bathy_hy
         ref_2d_rsh(liminm1:limaxp1,ljminm1:ljmaxp1)=hy(liminm1:limaxp1,ljminm1:ljmaxp1)
#if defined key_siggen || defined key_gencoord
       ELSEIF (n==4) THEN
         namvar='hc'
         ref_2d_rsh(liminm1:limaxp1,ljminm1:ljmaxp1)=hc_sig(liminm1:limaxp1,ljminm1:ljmaxp1)
#endif
       ENDIF
       tmp_2d_rsh(:,:)=-valmanq
       CALL ionc4_read_subxy(file_init,namvar,tmp_2d_rsh,liminm2,limaxp2,ljminm2,ljmaxp2,0,0)
       ncerror=0
       difmin=0.0_rsh
       difmax=0.0_rsh
       DO j=ljminm1,ljmaxp1
         DO i=liminm1,limaxp1
           IF ((tmp_2d_rsh(i,j)/=ref_2d_rsh(i,j)).AND.(ref_2d_rsh(i,j)/=-valmanq)) THEN
             ncerror=ncerror+1
             difmin=MIN(difmin,ref_2d_rsh(i,j)-tmp_2d_rsh(i,j))
             difmax=MAX(difmax,ref_2d_rsh(i,j)-tmp_2d_rsh(i,j))
           ENDIF
         ENDDO
       ENDDO
       IF (ncerror>0) THEN
         IF_MPI (MASTER) THEN
         WRITE(iwarnlog,*) ' '
         WRITE(iwarnlog,*) 'ERROR: INITFROMFILE.F90 subroutine'
         WRITE(iwarnlog,*) 'The bathymetric variable "',TRIM(namvar),'" is not consistent'
         WRITE(iwarnlog,*) 'with the variable "',TRIM(namvar),'" read from the restart file.'
         WRITE(iwarnlog,*) ncerror,' differences have been found.' 
         WRITE(iwarnlog,*) TRIM(namvar),'_simu - ',TRIM(namvar),'_restart:',&
                            ' min = ',difmin,', max = ',difmax
         WRITE(iwarnlog,*) 'Simulation should be stopped.'
         ENDIF_MPI
!         STOP
       ENDIF
     ENDDO

     ! Compare the current vertical coordinate and the coordinate read from the restart file.
     nmax=1
#if defined key_siggen || defined key_gencoord
     nmax=3
#endif
     DO n=1,nmax
       IF (n==1) THEN
         namvar='SIG'
         sigref(:)=sig(:)
         kmin=1
#if defined key_siggen || defined key_gencoord
       ELSEIF (n==2) THEN
         namvar='Csu_sig'
         sigref(:)=Csu_sig(:)
         kmin=1
       ELSEIF (n==3 .AND. l_init_restart) THEN
         namvar='Csw_sig'
         sigref(:)=Csw_sig(:)
         kmin=0
#endif
       ENDIF
       CALL ionc4_read_z(file_init,namvar,sigtmp(kmin:kmax),kmin,kmax)
       ncerror=0
       DO k=1,kmax
         IF (ABS(sigtmp(k)-sigref(k)) > 0.001_riosh) ncerror=ncerror+1
       ENDDO
       IF (ncerror>0) THEN
         IF_MPI (MASTER) THEN
         WRITE(iwarnlog,*) ' '
         WRITE(iwarnlog,*) 'ERROR: INITFROMFILE.F90 subroutine'
         WRITE(iwarnlog,*) 'The vertical coordinate "',TRIM(namvar),'" is not consistent'
         WRITE(iwarnlog,*) 'with the coordinate read from the restart file.'
         WRITE(iwarnlog,*) ncerror,' differences have been found.'
         WRITE(iwarnlog,*) 'Simulation should be stopped.'
         ENDIF_MPI
!         STOP
       ENDIF
     ENDDO

#if defined key_siggen || defined key_gencoord
     ncerror=0
     DO n=1,2
       IF (n==1) THEN
         namvar='b'
         valref=b_sig
       ELSEIF (n==2) THEN
         namvar='theta'
         valref=theta_sig
       ENDIF
       CALL ionc4_read(file_init,namvar,valtmp)
       IF (valtmp/=valref) THEN
         ncerror=ncerror+1
         IF_MPI (MASTER) THEN
         WRITE(iwarnlog,*) ' '
         WRITE(iwarnlog,*) 'ERROR: INITFROMFILE.F90 subroutine'
         WRITE(iwarnlog,*) 'The vertical parameter "',TRIM(namvar),'" is not consistent'
         WRITE(iwarnlog,*) 'with the parameter read from the restart file:'
         WRITE(iwarnlog,*) TRIM(namvar),'_simu = ',valref,' /= ',TRIM(namvar),'_restart = ',valtmp
         ENDIF_MPI
       ENDIF
     ENDDO
     IF (ncerror>0) THEN
       IF_MPI (MASTER) THEN
       WRITE(iwarnlog,*) 'Simulation should be stopped.'
       ENDIF_MPI
!       STOP
     ENDIF
#endif

   IF (l_init_restart) THEN
     CALL ionc4_read_subzxyt(file_init,'phieau',tmp_rlg,liminm2,limaxp2,ljminm2,ljmaxp2,1,kmax,irec,0,0,1,fillvar=0.0_rlg)
 ! du a agrif
     DO j=ljmin,ljmax
     DO i=limin,limax
     DO k=1,kmax
       phieau(k,i,j)=tmp_rlg(k,i,j)
     END DO
     END DO
     END DO
   END IF

#if defined key_agrif && ! defined key_agrif_1way
   IF (l_init_restart .AND. .NOT. Agrif_GrandMother() ) THEN

       CALL ionc4_read_subzxyt(file_init,'fxeau',tmp_rlg,liminm2,limaxp2,ljminm2,ljmaxp2,1,kmax,irec,0,0,1,fillvar=0.0_rlg)
       DO j=ljmin,ljmax
       DO i=limin,limax
       DO k=1,kmax
         fxeau(k,i,j)=tmp_rlg(k,i,j)
       END DO
       END DO
       END DO

       CALL ionc4_read_subzxyt(file_init,'fyeau',tmp_rlg,liminm2,limaxp2,ljminm2,ljmaxp2,1,kmax,irec,0,0,1,fillvar=0.0_rlg)
       DO j=ljmin,ljmax
       DO i=limin,limax
       DO k=1,kmax
         fyeau(k,i,j)=tmp_rlg(k,i,j)
       END DO
       END DO
       END DO

       CALL ionc4_read_subzxyt(file_init,'fzeau',tmp_rlg,liminm2,limaxp2,ljminm2,ljmaxp2,1,kmax,irec,0,0,1,fillvar=0.0_rlg)
       DO j=ljmin,ljmax
       DO i=limin,limax
       DO k=1,kmax
         fzeau(k,i,j)=tmp_rlg(k,i,j)
       END DO
       END DO
       END DO

       CALL_MPI ex_i_rlg(-1,0,kmax,liminm1,limax,ljmin,ljmax,fxeau(:,liminm1:limax,ljmin:ljmax))
       CALL_MPI ex_j_rlg(-1,0,kmax,limin,limax,ljminm1,ljmax,fyeau(:,limin:limax,ljminm1:ljmax))

   END IF
#endif

   IF (l_init_restart) THEN
     CALL ionc4_read(file_init,'tchrono_deb',tchrono_deb)
     tdb=tchrono_deb-perchrono
     tfi=tfin+perchrono
     nvalmax=NINT((tfi-tdb)/perchrono+0.5_rlg)+1
     DEALLOCATE(tchrono)
     ALLOCATE(tchrono(nvalmax))
     DO i=1,nvalmax 
       tchrono(i)=tdb+(i-1.0_rlg)*perchrono
     END DO
     indchrono=1
   END IF

   CALL ionc4_close(file_init)

   tsauv=t+saverestart_step
   DO j=ljmin,ljmax
     DO i=limin,limax
       xeadv(i,j)=xev(i,j)
     END DO
   END DO

   CALL_MPI ex_i_rsh(-2,2,1,liminm2,limaxp2,ljminm2,ljmaxp2,u(liminm2:limaxp2,ljminm2:ljmaxp2))
   CALL_MPI ex_j_rsh(-2,2,1,liminm2,limaxp2,ljminm2,ljmaxp2,u(liminm2:limaxp2,ljminm2:ljmaxp2))
   CALL_MPI ex_i_rsh(-2,2,1,liminm2,limaxp2,ljminm2,ljmaxp2,v(liminm2:limaxp2,ljminm2:ljmaxp2))
   CALL_MPI ex_j_rsh(-2,2,1,liminm2,limaxp2,ljminm2,ljmaxp2,v(liminm2:limaxp2,ljminm2:ljmaxp2))

   PRINT_DBG*, 'exit INIT_FROMFILE routine'

  END SUBROUTINE init_fromfile
