!!======================================================================
 
  SUBROUTINE out_saverestart(u,uz,v,vz,xev,sal,temp,        &
                                  intz_exp_vstar,igdv_old,  &

                                           cv_wat,          &

                                           ect)

!&E---------------------------------------------------------------------
!&E                 ***  ROUTINE out_saverestart  ***
!&E
!&E ** Purpose : save model variables in a NetCDF file for further restart
!&E
!&E ** Description :
!&E
!&E ** Called by : main, step
!&E
!&E ** External calls : ionc4_createfile, ionc4_torigine,
!&E                     ionc4_createvar, ionc4_write_time,ionc4_write_t,
!&E                     ionc4_write_xy, ionc4_write_xyt,ionc4_write_zxyt
!&E                     ionc4_write
!&E
!&E ** Reference :
!&E
!&E ** History :
!&E       !           (F. Dumas)  Original code
!&E       !  2006-04  (V. Garnier) Management of simple and double precision
!&E       !  2009-03  (V. Garnier A. Mano) MPI2D
!&E       !  2009-10  (V. Garnier) reproductibility when using restart file
!&E       !  2010-12 (R. Ramel) genericity and use of ionc4
!&E       !  2011-01 (R. Ramel, Alyotech) Ionc library in fortran 90/95, netcdf4 and MPI
!&E       !  2014-07 (M. Caillaud, Actimar) correction to the saving of tchrono
!&E
!&E---------------------------------------------------------------------





























































!! * Modules used
   USE parameters
   USE ionc4,     ONLY : ionc4_createfile,ionc4_createvar,ionc4_write_sig,ionc4_write_sigw,&
                         ionc4_vatt_standard_name,ionc4_vatt_fill,ionc4_vatt_valid_min,&
                         ionc4_vatt_valid_max,ionc4_write_xy,ionc4_write,ionc4_gatt,&
                         ionc4_torigine,ionc4_write_time,ionc4_write,ionc4_write_t,&
                         ionc4_write_xy,ionc4_write_xyt,ionc4_write_zxyt,ionc4_close,&
                         ionc4_sync,ionc4_vatt_chain
   USE outnetcdf, ONLY : out_netcdf_conv,out_zaxis_w
   USE output,    ONLY : l_out_nc4par,l_out_zaxis_w
   USE comvars2d, ONLY : name_in_dt,name_in_xev,                            &
                         name_bathy_h0,name_bathy_hx,name_bathy_hy,         &
                         name_in_u,name_in_v,name_in_uz,name_in_vz,         &
                         name_in_wz,name_in_temp,name_in_sal,               &
                         name_in_ect,name_in_psi,name_in_kz,name_in_nz,     &
                         name_in_vis2d,name_in_vis3d_xe,name_in_vis3d_phi,  &
                         l_saverestart_bydate,l_boolsauv,file_saverestart,  &
                         date_ref,tsauv,cdate,t,nbouc,dt,saverestart_step,  &
                         lon2d_g,lat2d_g,lon2d,lat2d,cflumaxmax,cflvmaxmax, &
                         l_modele2d,vish_xe,vish_phi,vis2d,h0,hx,hy,tdebobs,&
                         h0fond,nbouc_macho,        &
                         tchrono,indchrono
   USE comvars3d, ONLY : sig,sigw,wz,kz,nz
   USE comvarp3d, ONLY : psi
   USE comfluxdo, ONLY : phieau

   USE comsubstance, ONLY : nv_tot,name_var,irk_fil,unit_var




   USE toolmpi,   ONLY : ADD_TAG, MPI_COMM_MARS

   USE obccombine,ONLY : l_obc_tide

   IMPLICIT NONE

!! * Arguments
   INTEGER,INTENT(in),DIMENSION(limin:limax,ljmin:ljmax)             :: igdv_old
   REAL(KIND=rsh),INTENT(in),DIMENSION(limin:limax,ljmin:ljmax)      :: xev
   REAL(KIND=rsh),INTENT(in),DIMENSION(     liminm2:limaxp2,ljminm2:ljmaxp2) :: u,v
   REAL(KIND=rsh),INTENT(in),DIMENSION(     limin  :limax  ,ljmin  :ljmax  ) :: intz_exp_vstar
   REAL(KIND=rsh),INTENT(in),DIMENSION(kmax,liminm2:limaxp2,ljminm2:ljmaxp2) :: uz,vz
   REAL(KIND=rsh),INTENT(in),DIMENSION(kmax,limin:limax,ljmin:ljmax)         :: ect
   REAL(KIND=rsh),INTENT(in),DIMENSION(kmax,liminm1:limaxp2,ljminm1:ljmaxp2) :: sal,temp

   REAL(KIND=rsh),INTENT(in),DIMENSION(nb_var,kmax,liminm1:limaxp2,ljminm1:ljmaxp2) :: cv_wat


!! * Local declarations
   INTEGER                                                       :: l,comm_zoom
   CHARACTER(LEN=lchain) :: chain
   REAL(KIND=riosh),DIMENSION(limin:limax,ljmin:ljmax)           :: tmpigd
   REAL(KIND=rsh),  DIMENSION(limin:limax,ljmin:ljmax)           :: tmp2d
   REAL(KIND=rsh),  DIMENSION(kmax,limin:limax,ljmin:ljmax)      :: tmp3d
   REAL(KIND=rsh),  DIMENSION(0:kmax,limin:limax,ljmin:ljmax)      :: tmp3d0
   REAL(KIND=rlg),  DIMENSION(kmax,limin:limax,ljmin:ljmax)      :: tmp3drlg
   REAL(KIND=riosh),DIMENSION(0:kmax+1)                          :: sigecrit
   REAL(KIND=riosh)                                              :: vecrit
   REAL(KIND=rsh)                                                :: fillval,iozero
   LOGICAL                                                       :: l_out_nc4par_save

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

   !PRINT*, 'enter OUT_SAVERESTART routine'

   l_out_nc4par_save=.TRUE.
! l_out_nc4par_save=l_out_nc4par

   !IF (Agrif_root()) l_boolsauv=.FALSE.
   !IF (.NOT. Agrif_root()) THEN
! To save at the end of time integration
     !IF (Agrif_NbStepint() <(Agrif_IRhot()-1)) l_boolsauv=.FALSE.
     !IF (Agrif_NbStepint()==(Agrif_IRhot()-1)) l_boolsauv=Agrif_parent(l_boolsauv)
   !ENDIF

!! Management of simple and double precision
   IF(rsh==4) THEN
      fillval = rg_valmanq_io
      iozero  = 0.0_riosh
   ELSEIF(rsh==8) THEN
      fillval = dg_valmanq_io
      iozero  = 0.0_riolg
   ENDIF
      
   
     
     IF(nbouc == 0 .AND. .NOT.(l_saverestart_bydate)) THEN ! creation dans le cas non-sequentiel
!------------------------------------


       IF (.NOT. l_out_nc4par_save) CALL ADD_TAG( file_saverestart, LEN(file_saverestart) )


       sigecrit(:)=sig(:)
       IF (l_out_nc4par_save) THEN
         CALL ionc4_createfile(file_saverestart,lon2d_g,lat2d_g,imin, &
                           imax,1,jmin,jmax,1,1,kmax,1,kmax,0,l_out_nc4par = .TRUE.,zaxis_var=sigecrit(1:kmax))
       ELSE
         CALL ionc4_createfile(file_saverestart,lon2d,lat2d,limin, &
                           limax,1,ljmin,ljmax,1,1,kmax,1,kmax,0,zaxis_var=sigecrit(1:kmax))
       ENDIF

       comm_zoom=0

       comm_zoom=MPI_COMM_MARS


! Add the vertical axis at w-location
       CALL out_zaxis_w(file_saverestart,1,kmax,1,comm_zoom=comm_zoom)

! save the attributes of the spatial extension of the domain
! ----------------------------------------------------------

       CALL ionc4_gatt(file_saverestart,'global_imin',imin)
       CALL ionc4_gatt(file_saverestart,'global_imax',imax)
       CALL ionc4_gatt(file_saverestart,'global_jmin',jmin)
       CALL ionc4_gatt(file_saverestart,'global_jmax',jmax)

! save some time variables to allow a perfect continuity of the run despite
! the interruption of the run
! -------------------------------------------------------------------------

       CALL ionc4_torigine(file_saverestart,date_ref)
       CALL ionc4_createvar(file_saverestart,TRIM(name_in_dt),'s','dt',fill_value=-dg_valmanq_io)
       IF (.NOT. l_modele2d) THEN
         CALL ionc4_createvar(file_saverestart,'tdebobs','s','tdebobs',fill_value=-dg_valmanq_io)
         CALL ionc4_createvar(file_saverestart,'cflumaxmax',' ','cfl of max u',dims='t',fill_value=fillval, &
                              l_out_nc4par=l_out_nc4par_save)
         CALL ionc4_createvar(file_saverestart,'cflvmaxmax',' ','cfl of max v',dims='t',fill_value=fillval, &
                              l_out_nc4par=l_out_nc4par_save)

       END IF
       CALL ionc4_createvar(file_saverestart,'tchrono_deb','s','tchrono starting time for river-meteo interpolation', &
                            fill_value=-dg_valmanq_io)

       CALL ionc4_createvar(file_saverestart,'SIG',' ','sigma variable',&

                            standard_name= 'ocean_sigma_variable',&

                            l_auxcoordinate=.false.,&
                            valid_min= -1.0_riosh,valid_max= 0.0_riosh,fill_value= rg_valmanq_io,&
                            dims= 'z', l_out_nc4par = l_out_nc4par_save)
       CALL ionc4_write_sig(file_saverestart,'SIG',sigecrit,kmax)

       CALL ionc4_createvar(file_saverestart,'SIG_W',' ','sigma variable at the interface',&

                          standard_name= 'ocean_sigma_variable_at_w_location',&

                          l_auxcoordinate=.false.,location_in_cell='w',&
                          dims='z',valid_min= -1.0_riosh,valid_max=0.0_riosh,fill_value= rg_valmanq_io)
       sigecrit(:)=sigw(:)
       CALL ionc4_write_sigw(file_saverestart,'SIG_W',sigecrit,kmax)



! Add attributes to follow OCO netCDF convention
       CALL out_netcdf_conv(file_saverestart,1,comm_zoom,imin,imax,jmin,jmax)

       IF (l_obc_tide) THEN
         chain='model_sea_floor_depth_below_sea_level'
       ELSE
         chain='model_sea_floor_depth_below_geoid'
       END IF
       CALL ionc4_createvar(file_saverestart,TRIM(name_bathy_h0),units= 'm',dims='xy',fill_value=-fillval, &
                           long_name='bathymetry relative to the mean level',                            &
                           standard_name = TRIM(chain),                                                  &
                           l_out_nc4par=l_out_nc4par_save)
       IF (l_obc_tide) THEN
         chain='model_sea_floor_depth_below_sea_level_at_u_location'
       ELSE
         chain='model_sea_floor_depth_below_geoid_at_u_location'
       END IF
       CALL ionc4_createvar(file_saverestart,TRIM(name_bathy_hx),units= 'm',dims='xy',fill_value=-(fillval+h0fond), &
                           long_name = 'bathymetry at u-location relative to the mean level',            &
                           standard_name = TRIM(chain),                                                  &
                           location_in_cell='u',l_out_nc4par=l_out_nc4par_save)
       IF (l_obc_tide) THEN
         chain='model_sea_floor_depth_below_sea_level_at_v_location'
       ELSE
         chain='model_sea_floor_depth_below_geoid_at_v_location'
       END IF
       CALL ionc4_createvar(file_saverestart,TRIM(name_bathy_hy),units= 'm',dims='xy',fill_value=-(fillval+h0fond), &
                           long_name = 'bathymetry at v-location relative to the mean level',            &
                           standard_name = TRIM(chain),                                                  &
                           location_in_cell='v',l_out_nc4par=l_out_nc4par_save)
       IF (l_obc_tide) THEN
         chain='sea_surface_height_above_sea_level'
       ELSE
         chain='sea_surface_height_above_geoid'
       END IF
       CALL ionc4_createvar(file_saverestart,TRIM(name_in_xev),units= 'm',dims='xyt',fill_value=fillval, &
                            long_name = 'sea surface height',                                            &
                            standard_name = TRIM(chain),                                                 &
                            l_out_nc4par=l_out_nc4par_save)
       CALL ionc4_createvar(file_saverestart,TRIM(name_in_u),units= 'm s-1',dims='xyt',fill_value=iozero,&
                            long_name = 'barotropic zonal velocity',                                     &
                            standard_name = 'barotropic_sea_water_x_velocity_at_u_location',                    &
                            location_in_cell='u',                                                        &
                            l_out_nc4par=l_out_nc4par_save)
       CALL ionc4_createvar(file_saverestart,TRIM(name_in_v),units= 'm s-1',dims='xyt',fill_value=iozero,&
                            long_name = 'barotropic meridional velocity',                                &
                            standard_name = 'barotropic_sea_water_y_velocity_at_v_location',                   &
                            location_in_cell='v',&
                            l_out_nc4par=l_out_nc4par_save)
       IF (.NOT. l_modele2d) THEN
         CALL ionc4_createvar(file_saverestart,'intz_exp_vstar',units= 'm s-1',dims='xyt',fill_value=iozero,  &
                              long_name = 'RHS without the SSH gradient for the estimate of vstar',       &
                              location_in_cell='v',l_out_nc4par=l_out_nc4par_save)
         CALL ionc4_createvar(file_saverestart,'igdv_old','0-4','igdv_old',dims='xyt', &
                                               fill_value=-999,l_out_nc4par=l_out_nc4par_save)
       END IF
       CALL ionc4_createvar(file_saverestart,TRIM(name_in_vis2d),'m2 s-1','vis2d',dims='xyt', &
                                             fill_value=fillval,l_out_nc4par=l_out_nc4par_save)
       CALL ionc4_createvar(file_saverestart,TRIM(name_in_uz),units= 'm s-1',dims='zxyt',   &
                                             long_name = '3d zonal velocity',               &
                                             standard_name = 'sea_water_x_velocity_at_u_location', &
                                             location_in_cell='u',                          &
                                             fill_value=iozero,l_out_nc4par=l_out_nc4par_save)
       CALL ionc4_createvar(file_saverestart,TRIM(name_in_vz),units= 'm s-1',dims='zxyt',     &
                                             long_name = '3d meridional velocity',          &
                                             standard_name = 'sea_water_y_velocity_at_v_location',&
                                             location_in_cell='v',                          &
                                             fill_value=iozero,l_out_nc4par=l_out_nc4par_save)
       CALL ionc4_createvar(file_saverestart,TRIM(name_in_wz),'s-1','wz',dims='zxyt', &
                                             location_in_cell='w',                          &
                                             fill_value=iozero,l_out_nc4par=l_out_nc4par_save)
       CALL ionc4_createvar(file_saverestart,TRIM(name_in_sal),units= '1e-3',dims='zxyt', &
                                             long_name = 'salinity',                         &
                                             standard_name = 'sea_water_salinity',           &
                                             fill_value=fillval,l_out_nc4par=l_out_nc4par_save)
       CALL ionc4_createvar(file_saverestart,TRIM(name_in_temp),units= 'degrees_Celsius',dims='zxyt', &
                                             long_name = 'temperature',                        &
                                             standard_name = 'sea_water_potential_temperature',          &
                                             fill_value=fillval,l_out_nc4par=l_out_nc4par_save)
       CALL ionc4_createvar(file_saverestart,TRIM(name_in_ect),'m2 s-2','ect',dims='zxyt', &
                                             location_in_cell='w',                          &
                                             fill_value=fillval,l_out_nc4par=l_out_nc4par_save)
       CALL ionc4_createvar(file_saverestart,TRIM(name_in_psi),'m2 s-2','psi',dims='zxyt', &
                                             location_in_cell='w',                          &
                                             fill_value=fillval,l_out_nc4par=l_out_nc4par_save)
       CALL ionc4_createvar(file_saverestart,TRIM(name_in_kz),'m2 s-1','vertical diffusivity',dims='zxyt', &
                                             location_in_cell='w',                          &
                                             fill_value=fillval,l_out_nc4par=l_out_nc4par_save)
       CALL ionc4_createvar(file_saverestart,TRIM(name_in_nz),'m2 s-1','vertical viscosity',dims='zxyt', &
                                             location_in_cell='w',                          &
                                             fill_value=fillval,l_out_nc4par=l_out_nc4par_save)
       IF (.NOT.l_modele2d) THEN
         CALL ionc4_createvar(file_saverestart,TRIM(name_in_vis3d_xe),'m2 s-1','vish3d_xe',dims='zxyt', &
                                               fill_value=fillval,l_out_nc4par=l_out_nc4par_save)
         CALL ionc4_createvar(file_saverestart,TRIM(name_in_vis3d_phi),units= 'm2 s-1',long_name ='vish3d_phi',dims='zxyt', &
                                               location_in_cell='f',fill_value=fillval,l_out_nc4par=l_out_nc4par_save)
       END IF
       CALL ionc4_createvar(file_saverestart,'phieau','m3.s-1','water mass flux',dims='zxyt', &
                                             fill_value= 0.0_riolg,l_out_nc4par=l_out_nc4par_save)

!


       DO l = 1,nv_tot
         CALL ionc4_createvar(file_saverestart,TRIM(name_var(irk_fil(l))),unit_var(irk_fil(l)), &
                               TRIM(name_var(irk_fil(l))),dims='zxyt',fill_value=fillval,l_out_nc4par=l_out_nc4par_save)
       END DO

     END IF    ! fin creation dans le cas non-sequentiel
!----------------------------------------
!
     IF(l_saverestart_bydate .AND. (t >= tsauv.OR.l_boolsauv) ) THEN  ! creation dans le cas sequentiel
!--------------------------------
!
! cdate = DD-MM-YYYY-hh-mm-ss
       file_saverestart='./save_'//cdate(7:10)//cdate(4:5)//cdate(1:2)//cdate(12:13)// &
                    cdate(15:16)//cdate(18:19)
       !IF (.NOT. Agrif_Root()) file_saverestart='./save_agrif'//TRIM(Agrif_Cfixed())// &
       !  '_'//cdate(7:10)//cdate(4:5)//cdate(1:2)//cdate(12:13)//cdate(15:16)//cdate(18:19)
       file_saverestart=TRIM(file_saverestart)//'.nc'

       IF (.NOT. l_out_nc4par_save) CALL ADD_TAG( file_saverestart, LEN(file_saverestart) )


       sigecrit(:)=sig(:)
       IF (l_out_nc4par_save) THEN
         CALL ionc4_createfile(file_saverestart,lon2d_g,lat2d_g,imin, &
                           imax,1,jmin,jmax,1,1,kmax,1,kmax,0,l_out_nc4par = l_out_nc4par_save,zaxis_var=sigecrit(1:kmax))
       ELSE
         CALL ionc4_createfile(file_saverestart,lon2d,lat2d,limin, &
                           limax,1,ljmin,ljmax,1,1,kmax,1,kmax,0,l_out_nc4par = l_out_nc4par_save,zaxis_var=sigecrit(1:kmax))
       ENDIF  

       comm_zoom=0

       comm_zoom=MPI_COMM_MARS


! Add the vertical axis at w-location
       CALL out_zaxis_w(file_saverestart,1,kmax,1,comm_zoom=comm_zoom)

! save attribute of the spatial extension of the domain
! -----------------------------------------------------
       CALL ionc4_gatt(file_saverestart,'global_imin',imin)
       CALL ionc4_gatt(file_saverestart,'global_imax',imax)
       CALL ionc4_gatt(file_saverestart,'global_jmin',jmin)
       CALL ionc4_gatt(file_saverestart,'global_jmax',jmax)

! save some time variables to allow a perfect continuity of the run despite
! the interruption of the run
! -------------------------------------------------------------------------

       CALL ionc4_torigine(file_saverestart,date_ref)
       CALL ionc4_createvar(file_saverestart,TRIM(name_in_dt),'s','dt',fill_value=-dg_valmanq_io)
       IF (.NOT. l_modele2d) THEN
         CALL ionc4_createvar(file_saverestart,'tdebobs','s','tdebobs',fill_value=-dg_valmanq_io)
         CALL ionc4_createvar(file_saverestart,'cflumaxmax',' ','cfl of max u',dims='t',fill_value=fillval, &
                              l_out_nc4par=l_out_nc4par_save)
         CALL ionc4_createvar(file_saverestart,'cflvmaxmax',' ','cfl of max v',dims='t',fill_value=fillval, &
                              l_out_nc4par=l_out_nc4par_save)

       END IF
       CALL ionc4_createvar(file_saverestart,'tchrono_deb','s','tchrono starting time for river-meteo interpolation', &
                            fill_value=-dg_valmanq_io)

       CALL ionc4_createvar(file_saverestart,'SIG',' ','sigma variable',&

                            standard_name= 'ocean_sigma_variable',&

                            l_auxcoordinate=.false.,&
                            valid_min= -1.0_riosh,valid_max= 0.0_riosh,fill_value= rg_valmanq_io,&
                            dims= 'z', l_out_nc4par = l_out_nc4par_save)
       CALL ionc4_write_sig(file_saverestart,'SIG',sigecrit,kmax)
       
       CALL ionc4_createvar(file_saverestart,'SIG_W',' ','sigma variable at the interface',&

                          standard_name= 'ocean_sigma_variable_at_w_location',&

                          l_auxcoordinate=.false.,location_in_cell='w',&
                          dims='z',valid_min= -1.0_riosh,valid_max=0.0_riosh,fill_value= rg_valmanq_io)
       sigecrit(:)=sigw(:)
       CALL ionc4_write_sigw(file_saverestart,'SIG_W',sigecrit,kmax)



! Add attributes to follow OCO netCDF convention
       CALL out_netcdf_conv(file_saverestart,1,comm_zoom,imin,imax,jmin,jmax)

       IF (l_obc_tide) THEN
         chain='model_sea_floor_depth_below_sea_level'
       ELSE
         chain='model_sea_floor_depth_below_geoid'
       END IF
       CALL ionc4_createvar(file_saverestart,TRIM(name_bathy_h0),units='m',dims='xy',fill_value=-fillval, &
                            long_name='bathymetry relative to the mean level',                            &
                            standard_name = TRIM(chain),                                                  &
                            l_out_nc4par=l_out_nc4par_save)
       IF (l_obc_tide) THEN
         chain='model_sea_floor_depth_below_sea_level_at_u_location'
       ELSE
         chain='model_sea_floor_depth_below_geoid_at_u_location'
       END IF
       CALL ionc4_createvar(file_saverestart,TRIM(name_bathy_hx),units='m',dims='xy',fill_value=-(fillval+h0fond), &
                            long_name = 'bathymetry at u-location relative to the mean level',            &
                            standard_name = TRIM(chain),                                                  &
                            location_in_cell='u',l_out_nc4par=l_out_nc4par_save)
       IF (l_obc_tide) THEN
         chain='model_sea_floor_depth_below_sea_level_at_v_location'
       ELSE
         chain='model_sea_floor_depth_below_geoid_at_v_location'
       END IF
       CALL ionc4_createvar(file_saverestart,TRIM(name_bathy_hy),units='m',dims='xy',fill_value=-(fillval+h0fond), &
                            long_name = 'bathymetry at v-location relative to the mean level',            &
                            standard_name = TRIM(chain),                                                  &
                            location_in_cell='v',l_out_nc4par=l_out_nc4par_save)
       IF (l_obc_tide) THEN
         chain='sea_surface_height_above_sea_level'
       ELSE
         chain='sea_surface_height_above_geoid'
       END IF
       CALL ionc4_createvar(file_saverestart,TRIM(name_in_xev),units='m',dims='xyt',fill_value=fillval,  &
                            long_name = 'sea surface height',                                            &
                            standard_name = TRIM(chain),                                                 &
                            l_out_nc4par=l_out_nc4par_save)
       CALL ionc4_createvar(file_saverestart,TRIM(name_in_u),units='m s-1',dims='xyt',fill_value=iozero, &
                            long_name = 'barotropic zonal velocity',                                     &
                            standard_name = 'barotropic_sea_water_x_velocity_at_u_location',                    &
                            location_in_cell='u',                                                        &
                            l_out_nc4par=l_out_nc4par_save)
       CALL ionc4_createvar(file_saverestart,TRIM(name_in_v),units='m s-1',dims='xyt',fill_value=iozero, &
                            long_name = 'barotropic meridional velocity',                                &
                            standard_name = 'barotropic_sea_water_y_velocity_at_v_location',                   &
                            location_in_cell='v',&
                            l_out_nc4par=l_out_nc4par_save)
       IF (.NOT. l_modele2d) THEN
         CALL ionc4_createvar(file_saverestart,'intz_exp_vstar',units='m s-1',dims='xyt',fill_value=iozero, &
                              long_name = 'RHS without the SSH gradient for the estimate of vstar',     &
                              location_in_cell='v',l_out_nc4par=l_out_nc4par_save)
         CALL ionc4_createvar(file_saverestart,'igdv_old','0-4','igdv_old',dims='xyt', &
                                               fill_value=-999,l_out_nc4par=l_out_nc4par_save)
       END IF
       CALL ionc4_createvar(file_saverestart,TRIM(name_in_vis2d),'m2 s-1','vis2d',dims='xyt', &
                                             fill_value=fillval,l_out_nc4par=l_out_nc4par_save)
       CALL ionc4_createvar(file_saverestart,TRIM(name_in_uz),units='m s-1',dims='zxyt',    &
                                             long_name = '3d zonal velocity',               &
                                             standard_name = 'sea_water_x_velocity_at_u_location', &
                                             location_in_cell='u',                          &
                                             fill_value=iozero,l_out_nc4par=l_out_nc4par_save)
       CALL ionc4_createvar(file_saverestart,TRIM(name_in_vz),units='m s-1',dims='zxyt',    &
                                             long_name = '3d meridional velocity',          &
                                             standard_name = 'sea_water_y_velocity_at_v_location',&
                                             location_in_cell='v',                          &
                                             fill_value=iozero,l_out_nc4par=l_out_nc4par_save)
       CALL ionc4_createvar(file_saverestart,TRIM(name_in_wz),'s-1','wz',dims='zxyt',fill_value=iozero, &
                                             location_in_cell='w',l_out_nc4par=l_out_nc4par_save)
       CALL ionc4_createvar(file_saverestart,TRIM(name_in_sal),units='1e-3',dims='zxyt',     &
                                             long_name = 'salinity',                        &
                                             standard_name = 'sea_water_salinity',          &
                                             fill_value=fillval,l_out_nc4par=l_out_nc4par_save)
       CALL ionc4_createvar(file_saverestart,TRIM(name_in_temp),units='degrees_Celsius',dims='zxyt', &
                                             long_name = 'temperature',                       &
                                             standard_name = 'sea_water_potential_temperature',         &
                                             fill_value=fillval,l_out_nc4par=l_out_nc4par_save)
       CALL ionc4_createvar(file_saverestart,TRIM(name_in_ect),'m2 s-2','ect',dims='zxyt', &
                                             location_in_cell='w',                          &
                                             fill_value=fillval,l_out_nc4par=l_out_nc4par_save)
       CALL ionc4_createvar(file_saverestart,TRIM(name_in_psi),'m2 s-2','psi',dims='zxyt', &
                                             location_in_cell='w',                          &
                                             fill_value=fillval,l_out_nc4par=l_out_nc4par_save)
       CALL ionc4_createvar(file_saverestart,TRIM(name_in_kz),'m2 s-1','vertical diffusivity',dims='zxyt', &
                                             location_in_cell='w',                          &
                                             fill_value=fillval,l_out_nc4par=l_out_nc4par_save)
       CALL ionc4_createvar(file_saverestart,TRIM(name_in_nz),'m2 s-1','vertical viscosity',dims='zxyt', &
                                             location_in_cell='w',                          &
                                             fill_value=fillval,l_out_nc4par=l_out_nc4par_save)
       IF (.NOT.l_modele2d) THEN
         CALL ionc4_createvar(file_saverestart,TRIM(name_in_vis3d_xe),'m2 s-1','vish3d_xe',dims='zxyt', &
                                             fill_value=fillval,l_out_nc4par=l_out_nc4par_save)
         CALL ionc4_createvar(file_saverestart,TRIM(name_in_vis3d_phi),units= 'm2 s-1',long_name ='vish3d_phi',dims='zxyt', &
                                               location_in_cell='f',fill_value=fillval,l_out_nc4par=l_out_nc4par_save)
       END IF
       CALL ionc4_createvar(file_saverestart,'phieau','m3.s-1','water mass flux',dims='zxyt', &
                                             fill_value= 0.0_riolg,l_out_nc4par=l_out_nc4par_save)

!


       DO l = 1,nv_tot
         CALL ionc4_createvar(file_saverestart,TRIM(name_var(irk_fil(l))),unit_var(irk_fil(l)), &
                               TRIM(name_var(irk_fil(l))),dims='zxyt',fill_value=fillval,l_out_nc4par=l_out_nc4par_save)
       END DO

     ENDIF                    ! fin de la creation dans le cas sequentiel
!------------------------------------------
!
     IF(t >= tsauv.OR.l_boolsauv) THEN           ! ecriture
!---------
       !IF (.TRUE.) l_boolsauv = .TRUE.

! new data overlay old data
       CALL ionc4_write_time(file_saverestart,1,t)
       CALL ionc4_write(file_saverestart,TRIM(name_in_dt),dt)
       IF (.NOT. l_modele2d) THEN
         CALL ionc4_write(file_saverestart,'tdebobs',tdebobs)
         CALL ionc4_write_t(file_saverestart,'cflumaxmax',cflumaxmax,1)
         CALL ionc4_write_t(file_saverestart,'cflvmaxmax',cflvmaxmax,1)

       END IF
       CALL ionc4_write(file_saverestart,'tchrono_deb',tchrono(indchrono+1))
! in initfromfile.F90 tchrono(1)=tchrono(indchrono+1)-perchrono =  tchrono(indchrono)
! consequently, in the next run tchrono(1) < t < tchrono(2) as right now
! tchrono(indchrono) < t < tchrono(indchrono+1)
! Another solution was to add "WHILE tchrono..." as in step.F90 in initfromfile.F90

       tmp2d(:,:)=h0(limin:limax,ljmin:ljmax)
       CALL ionc4_write_xy(file_saverestart,TRIM(name_bathy_h0),tmp2d,limin,limax,ljmin,ljmax,l_out_nc4par=l_out_nc4par_save)
       tmp2d(:,:)=hx(limin:limax,ljmin:ljmax)
       CALL ionc4_write_xy(file_saverestart,TRIM(name_bathy_hx),tmp2d,limin,limax,ljmin,ljmax,l_out_nc4par=l_out_nc4par_save)
       tmp2d(:,:)=hy(limin:limax,ljmin:ljmax)
       CALL ionc4_write_xy(file_saverestart,TRIM(name_bathy_hy),tmp2d,limin,limax,ljmin,ljmax,l_out_nc4par=l_out_nc4par_save)
       CALL ionc4_write_xyt(file_saverestart,TRIM(name_in_xev),xev,limin,limax,ljmin,ljmax,1,l_out_nc4par=l_out_nc4par_save)
       tmp2d(:,:)=u(limin:limax,ljmin:ljmax)
       CALL ionc4_write_xyt(file_saverestart,TRIM(name_in_u),tmp2d,limin,limax,ljmin,ljmax,1,l_out_nc4par=l_out_nc4par_save)
       tmp2d(:,:)=v(limin:limax,ljmin:ljmax)
       CALL ionc4_write_xyt(file_saverestart,TRIM(name_in_v),tmp2d,limin,limax,ljmin,ljmax,1,l_out_nc4par=l_out_nc4par_save)
       IF (.NOT. l_modele2d) THEN
         tmp2d(:,:)=intz_exp_vstar(limin:limax,ljmin:ljmax)
         CALL ionc4_write_xyt(file_saverestart,'intz_exp_vstar',tmp2d,limin,limax,ljmin,ljmax,1,l_out_nc4par=l_out_nc4par_save)
         tmpigd=REAL(igdv_old,riosh)
         CALL ionc4_write_xyt(file_saverestart,'igdv_old',tmpigd,limin,limax,ljmin,ljmax,1,l_out_nc4par=l_out_nc4par_save)
       END IF
       tmp2d(:,:)=vis2d(limin:limax,ljmin:ljmax)
       CALL ionc4_write_xyt(file_saverestart,TRIM(name_in_vis2d),tmp2d,limin,limax,ljmin,ljmax,1,l_out_nc4par=l_out_nc4par_save)
       tmp3d(1:kmax,:,:)=uz(1:kmax,limin:limax,ljmin:ljmax)
       CALL ionc4_write_zxyt(file_saverestart,TRIM(name_in_uz),tmp3d,limin,limax,ljmin,ljmax,1,kmax,1, &
                                              l_out_nc4par=l_out_nc4par_save)
       tmp3d(1:kmax,:,:)=vz(1:kmax,limin:limax,ljmin:ljmax)
       CALL ionc4_write_zxyt(file_saverestart,TRIM(name_in_vz),tmp3d,limin,limax,ljmin,ljmax,1,kmax,1, &
                                              l_out_nc4par=l_out_nc4par_save)
       tmp3d0(0:kmax,:,:)=wz(0:kmax,limin:limax,ljmin:ljmax)
       CALL ionc4_write_zxyt(file_saverestart,TRIM(name_in_wz),tmp3d0,limin,limax,ljmin,ljmax,0,kmax,1, &
                                              l_out_nc4par=l_out_nc4par_save)
       tmp3d(1:kmax,:,:)=sal(1:kmax,limin:limax,ljmin:ljmax)
       CALL ionc4_write_zxyt(file_saverestart,TRIM(name_in_sal),tmp3d,limin,limax,ljmin,ljmax,1,kmax,1, &
                                              l_out_nc4par=l_out_nc4par_save)
       tmp3d(1:kmax,:,:)=temp(1:kmax,limin:limax,ljmin:ljmax)
       CALL ionc4_write_zxyt(file_saverestart,TRIM(name_in_temp),tmp3d,limin,limax,ljmin,ljmax,1,kmax,1, &
                                              l_out_nc4par=l_out_nc4par_save)
       CALL ionc4_write_zxyt(file_saverestart,TRIM(name_in_ect),ect,limin,limax,ljmin,ljmax,1,kmax,1,l_out_nc4par=l_out_nc4par_save)
       CALL ionc4_write_zxyt(file_saverestart,TRIM(name_in_psi),psi,limin,limax,ljmin,ljmax,1,kmax,1,l_out_nc4par=l_out_nc4par_save)
       tmp3d0(:,:,:)=kz(:,limin:limax,ljmin:ljmax)
       CALL ionc4_write_zxyt(file_saverestart,TRIM(name_in_kz),tmp3d0,limin,limax,ljmin,ljmax,0,kmax,1, &
                                              l_out_nc4par=l_out_nc4par_save)
       tmp3d0(:,:,:)=nz(:,limin:limax,ljmin:ljmax)
       CALL ionc4_write_zxyt(file_saverestart,TRIM(name_in_nz),tmp3d0,limin,limax,ljmin,ljmax,0,kmax,1, &
                                              l_out_nc4par=l_out_nc4par_save)
       IF (.NOT.l_modele2d) THEN
       tmp3d(:,:,:)=vish_xe(:,limin:limax,ljmin:ljmax)
       CALL ionc4_write_zxyt(file_saverestart,TRIM(name_in_vis3d_xe),tmp3d,limin,limax,ljmin,ljmax,1,kmax,1, &
                                              l_out_nc4par=l_out_nc4par_save)
       tmp3d(:,:,:)=vish_phi(:,limin:limax,ljmin:ljmax)
       CALL ionc4_write_zxyt(file_saverestart,TRIM(name_in_vis3d_phi),tmp3d,limin,limax,ljmin,ljmax,1,kmax,1, &
                                              l_out_nc4par=l_out_nc4par_save)
       END IF
       tmp3drlg(:,:,:)=phieau(:,limin:limax,ljmin:ljmax)
       CALL ionc4_write_zxyt(file_saverestart,'phieau',tmp3drlg,limin,limax,ljmin,ljmax,1,kmax,1,l_out_nc4par=l_out_nc4par_save)




       DO l = 1,nv_tot
         tmp3d(:,:,:) = cv_wat(l,:,limin:limax,ljmin:ljmax)
         CALL ionc4_write_zxyt(file_saverestart,TRIM(name_var(irk_fil(l))),tmp3d,limin,limax,ljmin,ljmax,1,kmax,1, &
                                                l_out_nc4par=l_out_nc4par_save)
       END DO

       tsauv=t+saverestart_step

       tsauv=t-saverestart_step


       IF (l_saverestart_bydate) THEN
         CALL ionc4_close(file_saverestart)
       ELSE
         CALL ionc4_sync(file_saverestart)
       ENDIF

     END IF

   !PRINT*, 'exit OUT_SAVERESTART routine'

  END SUBROUTINE out_saverestart
