SUBROUTINE SUALSPA1

!**** *SUALSPA1 * - Routine to allocate space for arrays where
!                   the local number of spectral coefficents need to be
!                   known

!     Purpose.
!     --------
!           Allocate space

!**   Interface.
!     ----------
!        *CALL* *SUALSPA1*

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

!     Implicit arguments :
!     --------------------

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

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

!     Author.
!     -------
!        MPP Group *ECMWF*

!     Modifications.
!     --------------
!        Original : 95-10-01
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!     ------------------------------------------------------------------

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

USE YOMCT0   , ONLY : NCONF    ,NPRINTLEV,LALLOPR  ,LELAM    ,&
 & LRUBC  
USE YOMDIM   , ONLY : NFD2D    ,NFC2D    ,NSMAX    ,NSEFRE   ,&
 & NSPEC2   ,NS2D    ,NTCMAX   ,NCPEC2   ,NTMAX    ,&
 & NTPEC2     
USE YOMSP    , ONLY : SPA2     ,SPD2D    ,SPSP     ,SPC2D    ,&
 & SPOR     ,SPEVELT1  
USE YOMLAP   , ONLY : NVALUE   ,NCVALUE  ,NTVALUE  ,NSTREL   ,NSPINDXG ,NSPNSUM  ,NTCINDX
USE YOMLUN   , ONLY : NULOUT
USE YOMGEM   , ONLY : NSTTYP   ,RSTRET
USE YOMJG    , ONLY : NGEOBAL  ,NGEOCOR
USE YEMSPBC  , ONLY : SPEVELT9 ,SPEVELT0
USE YOMGRB   , ONLY : NGRBLNSP, NGRBZ, NGRBSP2
USE YOMSP_PTRS , ONLY : MSPTR_OR, MSPTR_SP

IMPLICIT NONE

INTEGER(KIND=JPIM) :: ICONF, IU, JN, JF

LOGICAL :: LLP
REAL(KIND=JPRB) :: ZHOOK_HANDLE

IF (LHOOK) CALL DR_HOOK('SUALSPA1',0,ZHOOK_HANDLE)
LLP = NPRINTLEV >= 1.OR. LALLOPR
IU = NULOUT

ALLOCATE(SPA2(NSPEC2,NS2D))
IF(LLP)WRITE(IU,9) 'SPA2     ',SIZE(SPA2     ),SHAPE(SPA2     )
DO JN=1,NSPEC2
  DO JF=1,NS2D
    SPA2(JN,JF)=0.0_JPRB
  ENDDO
ENDDO
IF (.NOT.ALLOCATED(NGRBSP2)) ALLOCATE(NGRBSP2(NS2D))
IF(LLP)WRITE(IU,9) 'NGRBSP2',SIZE(NGRBSP2),SHAPE(NGRBSP2)
IF(NFD2D > 0) THEN
  SPD2D => SPA2(1:NSPEC2,1:NFD2D)
  SPSP  => SPA2(1:NSPEC2,1)
  MSPTR_SP = 1
  NGRBSP2(1)=NGRBLNSP
  IF(LRUBC) THEN
    SPEVELT1  => SPA2(1:NSPEC2,2)
    SPEVELT1(:)=0.0_JPRB
    ALLOCATE(SPEVELT0 (1:NSPEC2 ))
    WRITE(IU,9) 'SPEVELT0',SIZE(SPEVELT0),SHAPE(SPEVELT0)
    ALLOCATE(SPEVELT9 (1:NSPEC2 ))
    WRITE(IU,9) 'SPEVELT9',SIZE(SPEVELT9),SHAPE(SPEVELT9)
  ELSE
    SPEVELT1  => SPA2(1:NSPEC2,1)
  ENDIF
ENDIF
IF(NFC2D > 0) THEN
  SPC2D  => SPA2(1:NSPEC2,NFD2D+1:NFD2D+NFC2D)
  SPOR   => SPA2(1:NSPEC2,NFD2D+1)
  MSPTR_OR = NFD2D+1
  NGRBSP2(NFD2D+1)=NGRBZ
ENDIF

ICONF=NCONF/100
ALLOCATE(NVALUE (NSPEC2))
IF(LLP)WRITE(IU,9) 'NVALUE   ',SIZE(NVALUE   ),SHAPE(NVALUE   )
IF (NGEOCOR == 2.OR.NSTTYP /= 1.OR.ABS(RSTRET-1.0_JPRB) >= 1.E-14_JPRB.AND.&
   & .NOT.LELAM) THEN  
  ALLOCATE(NCVALUE(NCPEC2))
  IF(LLP)WRITE(IU,9) 'NCVALUE  ',SIZE(NCVALUE  ),SHAPE(NCVALUE  )
  ALLOCATE(NTVALUE(NTPEC2))
  IF(LLP)WRITE(IU,9) 'NTVALUE  ',SIZE(NTVALUE  ),SHAPE(NTVALUE  )
ENDIF
IF (ICONF /= 0.AND.ICONF /= 2.AND.NCONF /= 302) THEN
  ALLOCATE(NSPINDXG(NSEFRE))
  IF(LLP)WRITE(IU,9) 'NSPINDXG ',SIZE(NSPINDXG ),SHAPE(NSPINDXG )
ENDIF
ALLOCATE(NTCINDX(NTCMAX))
IF(LLP)WRITE(IU,9) 'NTCINDX  ',SIZE(NTCINDX  ),SHAPE(NTCINDX  )
IF (NGEOBAL == 2.OR.NGEOCOR == 2.OR.NSMAX < NTMAX) THEN
  ALLOCATE(NSTREL(NSPEC2))
  IF(LLP)WRITE(IU,9) 'NSTREL   ',SIZE(NSTREL   ),SHAPE(NSTREL   )
ENDIF
ALLOCATE(NSPNSUM(0:NSMAX))
IF(LLP)WRITE(IU,9) 'NSPNSUM  ',SIZE(NSPNSUM  ),SHAPE(NSPNSUM  )

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

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

