SUBROUTINE SUALLR

!**** *SUALLR * - Allocate additional space for first guess.

!     Purpose.
!     --------
!           Allocate space for the grid-point and spectral first guess.
!           Compute the absolute address for each field for future use
!           in POINTER statements.

!**   Interface.
!     ----------
!        *CALL* *SUALLR*

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

!     Implicit arguments :
!     --------------------
!        Common  YOMDIM
!      ( Pointer block  YOMGT7 ,to be added)
!        Pointer block  YOMSP

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

!     Externals.
!     ----------
!        ALLOCA - from Memory Manager

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

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

!     Modifications.
!     --------------
!        Original : 88-05-19
!        MODIFIED BY D. VASILJEVIC : 90-11-15
!        Modified by L.Labbe : 93-04-15 - reference array including orography -
!        Roberto Buizza : 93-10-28 To apply the covariance matrix constraint
!                                  when computing the optimal perturbations
!                                  in conf=601 
!        L. Isaksen : 95-06-06 - Reordering of spectral arrays
!        E. Holm    : 97-12-01 - Ozone included 
!        C. Fischer : 99-12-06 - allocate sp7velt1 pointer
!        M.Hamrud   : 03-08-01 - GFL introduction
!        C. Fischer : 03-09-30 - introduce Aladin mean wind
!        M.Hamrud   : 01-Oct-2003 CY28 Cleaning
!        M. Jidane  : 06-04-2006 - CY31 Phasing
!     ------------------------------------------------------------------

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

USE YOMCT0    ,ONLY : LELAM    ,NCONF
USE YOMDIM    ,ONLY : NPROMA   ,NFLEVG  ,NGPBLKS  ,NFLSUR   ,NSPEC2  ,NFLEVL 
USE YOMLUN    ,ONLY : NULOUT
USE YOMSPJB   ,ONLY : SP7A3    ,SP7A2   ,SP7VOR   ,SP7DIV   ,SP7HV   ,SP7T    ,SP7Q    &
 & ,SP7O3  , SP7L    ,SP7I     ,SP7D2D  ,SP7SP    ,SP7VELT1 ,SP7A1   ,SP7UB   ,SP7VB   &
 & ,GP7A3
USE YOMJG     ,ONLY : N_SPJB_VARS, SPJB_VARS_INFO
USE YOMGRB    ,ONLY : NGRBVO, NGRBD, NGRBT, NGRBQ, NGRBO3, NGRBLNSP

IMPLICIT NONE

INTEGER(KIND=JPIM) :: JFIELD, IDIM_SP7A2, IDIM_SP7A3, IDIM_GP7A3, IPTBG
REAL(KIND=JPRB)    :: ZHOOK_HANDLE

#include "abor1.intfb.h"

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

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

!*       1.1   ALL FIELDS.
IF (LHOOK) CALL DR_HOOK('SUALLR',0,ZHOOK_HANDLE)

NULLIFY(SP7VOR)
NULLIFY(SP7DIV)
NULLIFY(SP7HV)
NULLIFY(SP7T)
NULLIFY(SP7Q)
NULLIFY(SP7O3)
NULLIFY(SP7L)
NULLIFY(SP7I)
NULLIFY(SP7D2D)
NULLIFY(SP7SP)
NULLIFY(SP7VELT1)
NULLIFY(SP7UB)
NULLIFY(SP7VB)

IF (NCONF==131 .OR. NCONF/100==4 .OR. NCONF/100==5 .OR. NCONF/100==6 .OR. NCONF/100==8) THEN

  IDIM_SP7A2=0
  IDIM_SP7A3=0
  IDIM_GP7A3=0

  DO JFIELD=1,N_SPJB_VARS
    IF (SPJB_VARS_INFO(JFIELD)%L_IN_SPA2) THEN
      IDIM_SP7A2=MAX(IDIM_SP7A2,SPJB_VARS_INFO(JFIELD)%IPTBG)
    ELSEIF (SPJB_VARS_INFO(JFIELD)%L_IN_SPA3) THEN
      IDIM_SP7A3=MAX(IDIM_SP7A3,SPJB_VARS_INFO(JFIELD)%IPTBG)
    ELSEIF (SPJB_VARS_INFO(JFIELD)%L_IN_SPGFL) THEN
      IDIM_SP7A3=MAX(IDIM_SP7A3,SPJB_VARS_INFO(JFIELD)%IPTBG)
    ELSEIF (SPJB_VARS_INFO(JFIELD)%L_IN_GPGFL) THEN
      IDIM_GP7A3=MAX(IDIM_GP7A3,SPJB_VARS_INFO(JFIELD)%IPTBG)
    ELSE
      CALL ABOR1 ('THIS SHOULD NEVER HAPPEN')
    ENDIF
  ENDDO

  ALLOCATE(SP7A2(NSPEC2,IDIM_SP7A2))
  WRITE(NULOUT,9990) 'SP7A2    ',SIZE(SP7A2    ),SHAPE(SP7A2    )

  ALLOCATE(SP7A3(NFLSUR,NSPEC2,IDIM_SP7A3))
  WRITE(NULOUT,9990) 'SP7A3    ',SIZE(SP7A3    ),SHAPE(SP7A3    )

  ALLOCATE(GP7A3(NPROMA,NFLEVG,IDIM_GP7A3,NGPBLKS))
  WRITE(NULOUT,9990) 'GP7A3    ',SIZE(GP7A3    ),SHAPE(GP7A3    )

  IF(LELAM) THEN
    ALLOCATE(SP7A1(NFLEVL,2))
  ELSE
    ALLOCATE(SP7A1(1,2))
  ENDIF
    SP7UB => SP7A1(:,1)
    SP7VB => SP7A1(:,2)
  WRITE(NULOUT,9990) 'SP7A1    ',SIZE(SP7A1    ),SHAPE(SP7A1    )

  DO JFIELD=1,N_SPJB_VARS
    IPTBG=SPJB_VARS_INFO(JFIELD)%IPTBG
    IF (SPJB_VARS_INFO(JFIELD)%IGRIBCODE==NGRBVO) THEN
      SP7VOR => SP7A3(:,:,IPTBG)
    ELSEIF (SPJB_VARS_INFO(JFIELD)%IGRIBCODE==NGRBD) THEN
      SP7DIV => SP7A3(:,:,IPTBG)
    ELSEIF (SPJB_VARS_INFO(JFIELD)%IGRIBCODE==NGRBT) THEN
      SP7T => SP7A3(:,:,IPTBG)
    ELSEIF (SPJB_VARS_INFO(JFIELD)%IGRIBCODE==NGRBLNSP) THEN
      SP7SP => SP7A2(:,IPTBG)
    ELSEIF (SPJB_VARS_INFO(JFIELD)%IGRIBCODE==NGRBQ) THEN
      IF (SPJB_VARS_INFO(JFIELD)%L_IN_SPGFL) THEN
        SP7Q => SP7A3(:,:,IPTBG)
      ENDIF
    ELSEIF (SPJB_VARS_INFO(JFIELD)%IGRIBCODE==NGRBO3) THEN
      IF (SPJB_VARS_INFO(JFIELD)%L_IN_SPGFL) THEN
        SP7O3 => SP7A3(:,:,IPTBG)
      ENDIF
    ENDIF
  ENDDO

ELSE
  ALLOCATE(GP7A3(1,1,1,1))
  ALLOCATE(SP7A3(1,1,1))
  ALLOCATE(SP7A2(1,1))
  ALLOCATE(SP7A1(1,2))
ENDIF
!     ------------------------------------------------------------------
GP7A3(:,:,:,:)=0.0_JPRB
SP7A3(:,:,:)  =0.0_JPRB
SP7A2(:,:)    =0.0_JPRB
SP7A1(:,:)    =0.0_JPRB
!     ------------------------------------------------------------------
9990 FORMAT(1X,'ARRAY ',A10,' ALLOCATED ',9I8)

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