SUBROUTINE SAVE_TEST4DINC(kstep)

!     Purpose.
!     --------
!       Saves field for test of TL approximation.

!     Author.
!     -------
!       Y. Tremolet

!     Modifications.
!     --------------
!       Original    06-08-01
!       Y.Tremolet  20-11-03 Moved out of TESTVAR_MIX module
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        Y.Tremolet    07-Jun-2004 Re-wrote for GMV/GFL
!        Y.Tremolet    17-Feb-2005 Conserving interp., change file names
!       M.Drusch      18-Jan-2007 introduce nconf 302
! ------------------------------------------------------------------

USE PARKIND1, ONLY: JPIM, JPRB
USE YOMHOOK , ONLY: LHOOK, DR_HOOK
USE YOMLUN  , ONLY: NULOUT
USE YOMCT0  , ONLY: NCONF, LOBSC1
USE YOMVAR  , ONLY: NUPTRA
USE YOMDYN  , ONLY: TSTEP
USE YOM_YGFL, ONLY: YGFLC, YGFL
USE YOMCVA  , ONLY: NVA2D, NVA3D, NGRBVAR
USE YOMGRB  , ONLY: NGRBVO, NGRBD, NGRBU, NGRBV, NGRBT, NGRBLNSP, NGRBSP
USE TESTVAR_MIX , ONLY: lstep_testinc, interp_test, ygrb_testinc, ltestsppa
USE YOMGMV  , ONLY: GMV, GMVS, YT0, GMV5S, YT5
USE YOMGFL  , ONLY: GFL
USE GRIDPOINT_FIELDS_MIX

IMPLICIT NONE

INTEGER(KIND=JPIM), INTENT(IN) :: KSTEP 

TYPE(GRIDPOINT_FIELD) :: YLGRID
REAL(KIND=JPRB), ALLOCATABLE :: ZLNSPREF(:,:)
CHARACTER(LEN=26) :: clfile
INTEGER(KIND=JPIM) :: jl,jb,ih,im,is,ilen,jg,ii,jf
INTEGER(KIND=JPIM), ALLOCATABLE :: igrib(:)
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "abor1.intfb.h"
#include "write_grid_grib.intfb.h"
#include "prtime.intfb.h"

! ------------------------------------------------------------------

IF (LHOOK) CALL DR_HOOK('SAVE_TEST4DINC',0,ZHOOK_HANDLE)

IF (lstep_testinc(kstep)) THEN
  IF (NCONF==131) THEN
    clfile(1:26)='TEST4DINC/minXXggXXXXhXXXX'
    WRITE(clfile(14:15),'(I2.2)')NUPTRA
  ELSEIF (NCONF==1.OR.NCONF==302) THEN
    IF (LOBSC1) THEN
    clfile(1:26)='TEST4DINC/traXXggXXXXhXXXX'
      WRITE(clfile(14:15),'(I2.2)')NUPTRA
    ELSE
      clfile(1:26)='TEST4DINC/fcastggXXXXhXXXX'
    ENDIF
  ELSE
    CALL ABOR1('SAVE_TEST4DINC: NCONF neither 1 nor 131')
  ENDIF
  is=kstep*NINT(TSTEP)
  ih=is/3600
  is=is-3600*ih
  im=is/60
  is=is-60*im
  WRITE(clfile(18:21),'(I4.4)')ih
  WRITE(clfile(23:24),'(I2.2)')im
  WRITE(clfile(25:26),'(I2.2)')is

  ALLOCATE(igrib(NVA3D+NVA2D))
  igrib(:)=NGRBVAR(:)
  WHERE (igrib(:)==NGRBVO) igrib(:)=NGRBU
  WHERE (igrib(:)==NGRBD ) igrib(:)=NGRBV
  IF (ltestsppa) WHERE (igrib(:)==NGRBLNSP) igrib(:)=NGRBSP
  write(nulout,*)'SAVE_TEST4DINC: igrib=',igrib

  CALL ALLOCATE_GRID(YLGRID,NVA3D,NVA2D,igrib)
  DEALLOCATE(igrib)

  DO jf=1,YLGRID%NG3D
    IF (YLGRID%NGRIB3(jf)==NGRBU.OR.YLGRID%NGRIB3(jf)==NGRBV.OR. &
      & YLGRID%NGRIB3(jf)==NGRBT) THEN
      IF (YLGRID%NGRIB3(jf)==NGRBU) THEN
        jg=YT0%MU
      ELSEIF (YLGRID%NGRIB3(jf)==NGRBV) THEN
        jg=YT0%MV
      ELSEIF (YLGRID%NGRIB3(jf)==NGRBT) THEN
        jg=YT0%MT
      ELSE
        CALL ABOR1('SAVE_TEST4DINC: Error GMV')
      ENDIF
      write(nulout,*)'SAVE_TEST4DINC: GMV jf,jg=',jf,jg
      DO jb=1,YLGRID%NGPBLKS
        ilen=MIN(YLGRID%NPROMA, YLGRID%NGPTOT-(jb-1)*YLGRID%NPROMA)
        DO jl=1,YLGRID%NFLEVG
          YLGRID%GP3D(1:ilen,jl,jf,jb)=GMV(1:ilen,jl,jg,jb)
        ENDDO
      ENDDO
    ELSE
      jg=-999
      ii=0
      DO WHILE (ii<YGFL%NUMFLDS .AND. jg<0)
        ii=ii+1
        IF (YLGRID%NGRIB3(jf)==YGFLC(ii)%IGRBCODE) jg=YGFLC(ii)%MP
      ENDDO
      write(nulout,*)'SAVE_TEST4DINC: GFL jf,jg=',jf,jg
      IF (jg<1.OR.jg>YGFL%NUMFLDS) CALL ABOR1('SAVE_TEST4DINC: Error GFL')
      DO jb=1,YLGRID%NGPBLKS
        ilen=MIN(YLGRID%NPROMA, YLGRID%NGPTOT-(jb-1)*YLGRID%NPROMA)
        DO jl=1,YLGRID%NFLEVG
          YLGRID%GP3D(1:ilen,jl,jf,jb)=GFL(1:ilen,jl,jg,jb)
        ENDDO
      ENDDO
    ENDIF
  ENDDO
  DO jf=1,YLGRID%NG2D
    IF (YLGRID%NGRIB2(jf)==NGRBSP.OR.YLGRID%NGRIB2(jf)==NGRBLNSP) THEN
      jg=YT0%MSP
      write(nulout,*)'SAVE_TEST4DINC: GMVS jf,jg=',jf,jg
      DO jb=1,YLGRID%NGPBLKS
        ilen=MIN(YLGRID%NPROMA, YLGRID%NGPTOT-(jb-1)*YLGRID%NPROMA)
        YLGRID%GP2D(1:ilen,jf,jb)=GMVS(1:ilen,jg,jb)
      ENDDO
    ELSE
      CALL ABOR1('SAVE_TEST4DINC: 2D field not found.')
    ENDIF
  ENDDO
  CALL HUGIFY_ENDGRID(YLGRID)

  ALLOCATE(ZLNSPREF(YLGRID%NPROMA,YLGRID%NGPBLKS))
  IF (NCONF==131) THEN
    ZLNSPREF(:,:)=GMV5S(:,YT5%MSP,:)
  ELSE
    ZLNSPREF(:,:)=GMVS(:,YT0%MSP,:)
  ENDIF

  IF (LTESTSPPA) THEN
    IF (NCONF==131) THEN
      DO jb=1,YLGRID%NGPBLKS
        ilen=MIN(YLGRID%NPROMA, YLGRID%NGPTOT-(jb-1)*YLGRID%NPROMA)
        DO jl=1,ilen
          YLGRID%GP2D(jl,1,jb)=YLGRID%GP2D(jl,1,jb)*EXP(ZLNSPREF(jl,jb))
        ENDDO
      ENDDO
    ELSE
      DO jb=1,YLGRID%NGPBLKS
        ilen=MIN(YLGRID%NPROMA, YLGRID%NGPTOT-(jb-1)*YLGRID%NPROMA)
        DO jl=1,ilen
          YLGRID%GP2D(jl,1,jb)=EXP(YLGRID%GP2D(jl,1,jb))
        ENDDO
      ENDDO
    ENDIF
  ENDIF

  CALL PRTIME('SAVE_TEST4DINC')
  write(nulout,*)'SAVE_TEST4DINC: writing to file ',clfile
  CALL WRITE_GRID_GRIB(clfile,YLGRID,ygrb_testinc,interp_test, &
                     & PGPSP=ZLNSPREF)
  CALL DEALLOCATE_GRID(YLGRID)

  IF (ALLOCATED(ZLNSPREF)) DEALLOCATE(ZLNSPREF)
ENDIF

IF (LHOOK) CALL DR_HOOK('SAVE_TEST4DINC',1,ZHOOK_HANDLE)
END SUBROUTINE SAVE_TEST4DINC
