SUBROUTINE SUALLT

!**** *SUALLT * - Allocate additional space for trajectory.

!     Purpose.
!     --------
!           Allocate space for the grid-point and spectral trajectory.

!**   Interface.
!     ----------
!        *CALL* *SUALLT*

!     Explicit arguments :  None
!     --------------------

!     Implicit arguments :
!     --------------------
!        Common  YOMDIM
!        Pointer block  YOMSP
!        Pointer block  YOMSPT

!     Method.
!     -------
!        See documentation

!     Externals.
!     ----------

!     Reference.
!     ----------
!        ECMWF Research Department documentation of the IFS

!     Author.
!     -------
!        Mats Hamrud and Philippe Courtier  *ECMWF*

!     Modifications.
!     --------------
!        Original : 88-05-19
!        M.Hamrud      01-Oct-2003 CY28 Cleaning

!        Modifications:
!      Modified 91-07-03 by Philippe Courtier cleaning
!      Modified 93-04-13 by Laurent Labbe - allocate trajectory boundary array -
!      Modified 95-06-06 by L. Isaksen - allocate trajectory boundary array -
!      Modified 98-03-25 by C. Fischer and C. Soci - add test for allocations -
!      Modified 97-12-01 by E. Holm - ozone included
!      Modified 98-10-27 by G. Mozdzynski - allocate grid-point trajectory
!      Modified 99-12-06 by C. Fischer - allocate sp5velt1 pointer
!      Modified 00-02-15 by C. Temperton - allocate semi-Lag. trajectory
!      Modified 00-08-09 by Y. Tremolet - use module TRAJECTORY
!      Modified 01-05-03 by C. Fischer - allocate_traj under key for 801
!      Modified 03-08-01 by M.Hamrud - GFL introduction
!      Modified 03-06-02 by C. Soci - reintroduce test on allocate_traj under 801
!                        needed because suallt is called twice under sim4d in 801
!      Modified 03-09-30 by C. Fischer - introduce Aladin mean wind profiles
!     ------------------------------------------------------------------

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK

USE YOMCT0   , ONLY :    NSTOP    ,LRUBC    ,LSLAG    ,NCONF    ,&
 & LELAM  
USE YOMLUN   , ONLY : NULOUT
USE YOMDIM   , ONLY : NFTHER   ,NFD2D    ,&
 & NFLSUR   ,NSPEC2   ,NS3D     ,NS2D     ,LSPT     ,&
 & NPROMA   ,&
 & NFLEVG   ,NGPBLKS  ,NFLEVL  
USE YOMSP    , ONLY : SP5A3    ,SP5A2    ,&
 & SP5VOR   ,SP5DIV   ,SP5HV    ,SP5T     ,SP5Q     ,&
 & SP5O3    ,SP5L     ,SP5I     ,SP5D2D   ,&
 & SP5SP    ,SP5VELT1 ,SP5GFL   ,SP5A1    ,SP5UB    ,SP5VB  
USE YOMTRSL  , ONLY : NLENGTSLB,TRAJSL
USE YOMVAR   , ONLY : L3DFGAT
USE TRAJECTORY_MOD, ONLY: ALLOCATE_TRAJECTORY
USE YOMTRAJ, ONLY : LTRAJSAVE
USE YOM_YGFL , ONLY : YGFL, YQ, YL, YI, YO3
USE YOMGMV   , ONLY : GMV_DEPART,GMVS_DEPART,YT0
USE YOMGFL   , ONLY : GFL_DEPART

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

IMPLICIT NONE

INTEGER(KIND=JPIM) ::  IPT,IOFF
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

!*       1.    ALLOCATE SPACE FOR ARRAYS.
!              --------------------------

IF (LHOOK) CALL DR_HOOK('SUALLT',0,ZHOOK_HANDLE)
IF (.NOT.ALLOCATED(SP5A3)) THEN
  ALLOCATE(SP5A3(NFLSUR,NSPEC2,NS3D))
ENDIF
WRITE(NULOUT,9990) 'SP5A3    ',SIZE(SP5A3    ),SHAPE(SP5A3    )
SP5VOR => SP5A3(1:NFLSUR,1:NSPEC2,1)
SP5DIV => SP5A3(1:NFLSUR,1:NSPEC2,2)
IF(NFTHER > 0) THEN
  SP5HV => SP5A3(1:NFLSUR,1:NSPEC2,3:2+NFTHER)
ENDIF
IPT = 3
IF(LSPT) THEN
  SP5T => SP5A3(1:NFLSUR,1:NSPEC2,IPT)
  IPT = IPT+1
ENDIF

IOFF=2+NFTHER
IF(YGFL%NUMSPFLDS > 0) THEN
  SP5GFL => SP5A3(1:NFLSUR,1:NSPEC2,IOFF+1:IOFF+YGFL%NUMSPFLDS)
ELSE
  SP5GFL => SP5A3(1:1,1:1,1:1)
ENDIF
IF(YQ%LSP) THEN
  SP5Q => SP5A3(1:NFLSUR,1:NSPEC2,IOFF+YQ%MPSP)
ELSE
  SP5Q => SP5A3(1:1,1:1,1)
ENDIF
IF(YO3%LSP) THEN
  SP5O3 => SP5A3(1:NFLSUR,1:NSPEC2,IOFF+YO3%MPSP)
ELSE
  SP5O3 => SP5A3(1:1,1:1,1)
ENDIF
IF(YL%LSP) THEN
  SP5L => SP5A3(1:NFLSUR,1:NSPEC2,IOFF+YL%MPSP)
ELSE
  SP5L => SP5A3(1:1,1:1,1)
ENDIF
IF(YI%LSP) THEN
  SP5I => SP5A3(1:NFLSUR,1:NSPEC2,IOFF+YI%MPSP)
ELSE
  SP5I => SP5A3(1:1,1:1,1)
ENDIF

IF (.NOT.ALLOCATED(SP5A2)) THEN
  ALLOCATE(SP5A2(NSPEC2,NS2D))
ENDIF
WRITE(NULOUT,9990) 'SP5A2    ',SIZE(SP5A2    ),SHAPE(SP5A2    )
IF(NFD2D > 0) THEN
  SP5D2D => SP5A2(1:NSPEC2,1:NFD2D)
  SP5SP  => SP5A2(1:NSPEC2,1)
  IF (LRUBC) THEN
    SP5VELT1 => SP5A2(1:NSPEC2,2)
  ELSE
    SP5VELT1 => SP5A2(1:NSPEC2,1)
  ENDIF
ENDIF

IF (.NOT.ALLOCATED(SP5A1)) THEN
  IF(LELAM) THEN
    ALLOCATE(SP5A1(NFLEVL,2))
  ELSE
! to prevent out of bounds -rek
    ALLOCATE(SP5A1(NFLEVL,2))
  ENDIF
ENDIF
WRITE(NULOUT,9990) 'SP5A1    ',SIZE(SP5A1    ),SHAPE(SP5A1    )
IF(LELAM) THEN
  SP5UB => SP5A1(1:NFLEVL,1)
  SP5VB => SP5A1(1:NFLEVL,2)
ELSE
  IF(ASSOCIATED(SP5UB)) NULLIFY(SP5UB)
  IF(ASSOCIATED(SP5UB)) NULLIFY(SP5VB)
ENDIF

! Dont understand this/makes all fail/MH
IF (.NOT.(NCONF==801.AND.LTRAJSAVE)) THEN
  CALL ALLOCATE_TRAJECTORY
ENDIF

IF (LSLAG.AND..NOT.ALLOCATED(TRAJSL)) THEN
  ALLOCATE(TRAJSL(NLENGTSLB,0:NSTOP))
  WRITE(NULOUT,9990) 'TRAJSL   ',SIZE(TRAJSL),SHAPE(TRAJSL)
ENDIF

IF (L3DFGAT .AND. .NOT.ALLOCATED(GMV_DEPART)) THEN
  ALLOCATE(GMV_DEPART(NPROMA,NFLEVG,YT0%NDIM,NGPBLKS))
  WRITE(NULOUT,9990) 'GMV_DEPART',SIZE(GMV_DEPART ),SHAPE(GMV_DEPART)
  ALLOCATE(GMVS_DEPART(NPROMA,YT0%NDIMS,NGPBLKS))
  WRITE(NULOUT,9990) 'GMVS_DEPART',SIZE(GMVS_DEPART ),SHAPE(GMVS_DEPART)
  ALLOCATE(GFL_DEPART(NPROMA,NFLEVG,YGFL%NDIM0,NGPBLKS))
  WRITE(NULOUT,9990) 'GFL_DEPART',SIZE(GFL_DEPART ),SHAPE(GFL_DEPART)
ENDIF

!     ------------------------------------------------------------------
9990 FORMAT(1X,'ARRAY ',A10,' ALLOCATED ',9I8)

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