!     ######################################################################
SUBROUTINE ARO_SURF_DIAGH     
!     #######################################################################

!!****  *ARO_SURF_DIAGH*  
!!
!!    PURPOSE
!!    -------
!       Interface to call externalized surface diagnostics from AROME via 
!       aro_surf_diag 
!       Created for the externalisation of the call to aro_surf_diag under stepo 
!!**  METHOD
!!    ------

!!    EXTERNAL
!!    --------
!!
!!    IMPLICIT ARGUMENTS
!!    ------------------
!!
!!
!!    REFERENCE
!!    ---------
!!
!!   
!!      
!!    AUTHOR
!!    ------
!!    R. Zaaboul
!!
!!    MODIFICATIONS
!!    -------------
!!      creation 16-Feb-2006
!       Sep 2006 L.Kraljevic : Check for LMPA to distinguish between ALADIN/AROME
!       Nov 2008 Y.Seity : Test if proc deals only with E zone before calling diag
!-------------------------------------------------------------------------------
USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK

!*       0.     DECLARATIONS
!               ------------

USE YOMARAR    ,ONLY : NGPAR, MINPRR, MINPRS, MINPRG, &
                &     MACPRR,MACPRS, MACPRG,          &
                &     MRAIN, MSNOW
USE YOMARPHY   ,ONLY : LMPA


USE YOMGPPB, ONLY : GPARBUF
USE YOMDIM   , ONLY : NPROMA,NDGLG,NDGUXG,NDLON
USE YOMGEM, ONLY : NGPTOT, NGPTOT_CAP, NGPTOTL
USE YOMCT0   , ONLY : N_REGIONS_NS,N_REGIONS_EW,NPROC,LELAM
USE YOMCT3   , ONLY : NSTEP
USE YOMMP    , ONLY : MYPROC
USE YOMTRANS , ONLY : RDISTR_E
USE YOMOP    , ONLY : LINC
USE YOMDYN   , ONLY : TSTEP


IMPLICIT NONE

!*      0.1    declarations of arguments

!* Array dimensions
!  ----------------
REAL(KIND=JPRB), DIMENSION (:,:),ALLOCATABLE :: ZGPAR
REAL(KIND=JPRB), DIMENSION (:,:),ALLOCATABLE :: ZGPAR2
REAL(KIND=JPRB), DIMENSION (NGPTOT,1) :: ZINPRR,ZINPRS,ZINPRG,ZACPRR,ZACPRG,ZACPRS

!integer
!-------
INTEGER(KIND=JPIM) :: INC,IGPCOMP,JLON,JF,IENDC,JKGLO,IE,IPTE,JA,IPROCNS
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "sc2rdg.intfb.h"
#include "aro_surf_diag.h"

!initialisations
IF (LHOOK) CALL DR_HOOK('ARO_SURF_DIAGH',0,ZHOOK_HANDLE)
ZINPRR=0.0_JPRB
ZINPRS=0.0_JPRB
ZINPRG=0.0_JPRB
ZACPRR=0.0_JPRB
ZACPRS=0.0_JPRB
ZACPRG=0.0_JPRB

! Compute number of processors dealing only with E-Zone exclusively (not surfex)
! for them aro_surfdiagh is not called
IPROCNS=N_REGIONS_NS
IF ( N_REGIONS_NS == NPROC ) THEN
  IPTE=0
  IE=INT(RDISTR_E*(NDGLG-NDGUXG))*NDLON
  DO JA = NPROC,1,-1
    IPTE=IPTE+NGPTOTL(JA,1)
    IF (IPTE <= IE) THEN
      IPROCNS=IPROCNS-1
    ENDIF
  ENDDO
ENDIF

    
IF (MYPROC <= IPROCNS*N_REGIONS_EW) THEN
 ! allocation du buffer
IF (NGPAR /= 0) THEN
  ALLOCATE (ZGPAR(NGPTOT,NGPAR))
  ALLOCATE (ZGPAR2(NPROMA,NGPAR))
ENDIF

! calcul de la dimension des blocks NGPTOT
IF(.NOT.LELAM) THEN
   IGPCOMP=NGPTOT
ELSE
   IGPCOMP=MIN(NGPTOT,NGPTOT_CAP)
ENDIF

!lecture du buffer
DO JKGLO=1,NGPTOT,NPROMA
  IENDC=MIN(NPROMA,NGPTOT-JKGLO+1)
  CALL SC2RDG (GPARBUF,JKGLO,1 ,IENDC , ZGPAR2)
  DO JF=1,NGPAR
    DO JLON=1,IENDC
      ZGPAR(JKGLO+JLON-1,JF)=ZGPAR2(JLON,JF)
    ENDDO
  ENDDO
ENDDO

!initialisation of fields necessary for surface diagnostics 

IF ( LMPA ) THEN
  DO JLON=1,NGPTOT_CAP
    ZINPRR(JLON,1)=ZGPAR(JLON,MINPRR)
    ZINPRS(JLON,1)=ZGPAR(JLON,MINPRS)
    ZINPRG(JLON,1)=ZGPAR(JLON,MINPRG)
    ZACPRR(JLON,1)=ZGPAR(JLON,MACPRR)
    ZACPRS(JLON,1)=ZGPAR(JLON,MACPRS)
    ZACPRG(JLON,1)=ZGPAR(JLON,MACPRG)
  ENDDO
ELSE
  DO JLON=1,NGPTOT_CAP
    ZINPRR(JLON,1)=ZGPAR(JLON,MRAIN)
    ZINPRS(JLON,1)=ZGPAR(JLON,MSNOW)
  ENDDO
ENDIF

IF (LINC) THEN
  INC=NINT(REAL(NSTEP,JPRB)*TSTEP/3600._JPRB)
ELSE
  INC=NSTEP
ENDIF

!diagnostiques de surface
CALL ARO_SURF_DIAG(IGPCOMP,INC,MYPROC,NGPTOT,&
     &ZINPRR,ZACPRR,ZINPRS,ZACPRS,ZINPRG,ZACPRG)  

IF (ALLOCATED(ZGPAR)) THEN
  DEALLOCATE(ZGPAR)
ENDIF
IF (ALLOCATED(ZGPAR2)) THEN
  DEALLOCATE(ZGPAR2)
ENDIF
ENDIF

IF (LHOOK) CALL DR_HOOK('ARO_SURF_DIAGH',1,ZHOOK_HANDLE)

END SUBROUTINE ARO_SURF_DIAGH
