SUBROUTINE SAVE4NEXT 

!     Purpose.
!     --------
!      Save actual model state in a model error type file  
!      to be the first gues in the next cycle
!     Author.
!     -------
!       G. Radnoti 

!     Modifications.
!     --------------
!        Original : 13-Jun-2007 
!     ------------------------------------------------------------------

USE PARKIND1, ONLY: JPIM, JPRB
USE YOMHOOK , ONLY: LHOOK, DR_HOOK
USE YOMVAR  , ONLY: NUPTRA
USE YOMCT3  , ONLY: NSTEP
USE YOMLUN  , ONLY: NULOUT
USE YOMMODEL_ERROR, ONLY: NVE3GP, NVE2GP, NGRBMERRGP, NSERR, NVE3SP,NVE2SP, NGRBMERRSP 
USE GRIDPOINT_FIELDS_MIX
USE SPECTRAL_FIELDS_MOD

IMPLICIT NONE

TYPE(GRIDPOINT_FIELD) :: YLGRID
TYPE(SPECTRAL_FIELD) :: YLSPEC
CHARACTER(LEN=23) :: clfile
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "wrmoderr.intfb.h"
#include "model2moderr.intfb.h" 
!     ------------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('SAVE4NEXT',0,ZHOOK_HANDLE)
CALL ALLOCATE_GRID(YLGRID,NVE3GP,NVE2GP,NGRBMERRGP)
CALL ALLOCATE_SPEC(YLSPEC,NSERR,0,NVE3SP,NVE2SP,0,NGRBMERRSP)
CALL MODEL2MODERR(1,YLSPEC,YLGRID)
clfile(1:23)='spmoderrfg_ininext_XX01'
write(clfile(20:21),'(I2.2)')NUPTRA
WRITE(NULOUT,*) ' model state of final traj saved for next cycle at time-step ',NSTEP
CALL WRMODERR(clfile,YLSPEC,YLGRID)
CALL DEALLOCATE_SPEC(YLSPEC)
CALL DEALLOCATE_GRID(YLGRID)
IF (LHOOK) CALL DR_HOOK('SAVE4NEXT',1,ZHOOK_HANDLE)
END SUBROUTINE SAVE4NEXT 
