SUBROUTINE ONEDVAR_RAINTB_HLP()
!****-------------------------------------------------------------------
!**** *ONEDVAR_RAINTB_HLP* - ONEDVAR LOAD BALANCING
!****-------------------------------------------------------------------
!     Purpose.           IF MASTER: Send Done Signal
!                        IF HELPER: Send Ready To Help Signal
!                        IF HELPER: Get and Do Work until Done

!     --------

!**   Interface.
!     ----------

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

!     Method.
!     -------

!     Externals.
!     ----------
!       Called from GP_MODEL
!       Calls ONEDVAR_RAINTB

!     Author.
!     -------
!        John Hague   *ECMWF*

!     Modifications.
!     --------------
!        J. Hague      17-Feb-2005  Multiple Helpers for Load balancing
!        J. Hague      22-Nov-2005  Clean Load balancing
!        A. Geer        4-Apr-2006  Retrieve 10m wind
!        A. Geer       23-Oct-2006  Check M1QN3 failure codes, info to ODB
!        A. Geer       27-Feb-2007  Store cloud diagnostics
!     ------------------------------------------------------------------

USE PARKIND1 ,ONLY : JPIM     ,JPRB
USE YOMHOOK  ,ONLY : LHOOK    ,DR_HOOK
USE YOMDIM   ,ONLY : NFLEVG, NPROMA 
USE OML_MOD, ONLY : OML_MY_THREAD
USE MPL_MODULE  ,ONLY : MPL_SEND, MPL_RECV, JP_BLOCKING_STANDARD
USE YOM_PTR_SSMI, ONLY : MFLDS_1DDIAG
USE PARONEDVAR, ONLY : NMAX_CHANNELS
USE YOMRAIN_LB, ONLY : ICOPROCS, CLPROC

IMPLICIT NONE

INTEGER(KIND=JPIM) :: J,IT,ISTJ,IENDJ,INC1,INC2,II,ISTREAM1D_MODE
INTEGER(KIND=JPIM) :: IOFF1,IOFF2,IOFF3,IOFF4,IOFF5

REAL(KIND=JPRB),    ALLOCATABLE  :: ZSBF(:), ZRBF(:) , ZNBF(:),  ZDUM(:,:,:), ZDIAG(:,:)
INTEGER(KIND=JPIM), ALLOCATABLE  :: ISBF(:), IRBF(:)
REAL(KIND=JPRB) :: ZBUF
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "onedvar_raintb.intfb.h"

IF (LHOOK) CALL DR_HOOK('ONEDVAR_RAINTB_HLP',0,ZHOOK_HANDLE)
CALL GSTATS(1003,0)
!$OMP PARALLEL PRIVATE(II,ZDUM,ZBUF,ZSBF,IRBF,ISBF,ZNBF,ZDIAG,ZRBF,IT,J,ISTJ,IENDJ,INC1,INC2,IOFF1,IOFF2,IOFF3,IOFF4,IOFF5)
    IT=OML_MY_THREAD()
    IF(CLPROC == "M") THEN
!     ------ MASTER: Send Done Signal --------------
      ZBUF=999.0_JPRB
      DO J=1,ICOPROCS(1)
        CALL MPL_SEND(ZBUF,KTAG=IT,KDEST=ICOPROCS(J+1),&
          &KMP_TYPE=JP_BLOCKING_STANDARD,CDSTRING='ONEDVAR_RAINTB_HLP:')
      ENDDO
    ENDIF
    IF(CLPROC == "H") THEN
!     ----- HELPER: Send Ready To Help Signal ---------
      ZBUF=999.0_JPRB
      CALL MPL_SEND(ZBUF,KTAG=IT,KDEST=ICOPROCS(1),&
        &KMP_TYPE=JP_BLOCKING_STANDARD,CDSTRING='ONEDVAR_RAINTB_HLP:')
!     ------ HELPER: Get and Do Work until Done --------------
      ALLOCATE(ZRBF(4+9*NPROMA*NFLEVG+19*NPROMA+NPROMA*NMAX_CHANNELS))
      ALLOCATE(IRBF(2*NPROMA))
      ALLOCATE(ISBF(4*NPROMA))
      ALLOCATE(ZNBF(1*NPROMA))
      ALLOCATE(ZDIAG(NPROMA,MFLDS_1DDIAG))
      ALLOCATE(ZSBF(3+11*NPROMA+5*NPROMA*NMAX_CHANNELS))
      ALLOCATE(ZDUM(NPROMA,NFLEVG,13))
      CALL MPL_RECV(ZRBF,KTAG=IT,KSOURCE=ICOPROCS(1),CDSTRING='ONEDVAR_RAINTB_HLP:')
      DO WHILE (ZRBF(1) /= 999.0_JPRB)
        ISTJ=1
        IENDJ=ISTJ+INT(ZRBF(3))-INT(ZRBF(2))
        ISTREAM1D_MODE=INT(ZRBF(4))
        INC1=NPROMA*NFLEVG
        INC2=NPROMA*NMAX_CHANNELS
        IOFF1=5
        IOFF2=IOFF1+9*INC1
        IOFF3=IOFF2+13*NPROMA+INC2
        IOFF4=4
        IOFF5=IOFF4+11*NPROMA

        DO II=ISTJ,IENDJ
          IRBF(II)=ZRBF(IOFF3+3*NPROMA+II-1)
          IRBF(II+NPROMA)=ZRBF(IOFF3+4*NPROMA+II-1) 
        ENDDO
     
        CALL ONEDVAR_RAINTB( ISTJ,IENDJ,ISTREAM1D_MODE, &
          & ZRBF(IOFF1)              ,& !TEMPERATURE
          & ZRBF(IOFF1+  INC1)       ,& !HUMIDITY
          & ZRBF(IOFF1+2*INC1)       ,&  !TENDENCY BEFORE CONVECTION
          & ZRBF(IOFF1+3*INC1)       ,&  !TENDENCY BEFORE CONVECTION
          & ZRBF(IOFF1+4*INC1)       ,&  !PRECIPITATION AS RAIN ZFPLCL+ ZFPLSL
          & ZRBF(IOFF1+5*INC1)       ,&  !PRECIPITATION AS SNOW ZFPLCN+ ZFPLSN
          & ZRBF(IOFF1+6*INC1)       ,&  !CLOUD FRACTION
          & ZRBF(IOFF1+7*INC1)       ,&  !CLOUD LIQUID WATER
          & ZRBF(IOFF1+8*INC1)       ,&  !CLOUD ICE WATER          
          & ZRBF(IOFF2)              ,&  !TEMPERATURE AT 2 METERS
          & ZRBF(IOFF2+1*NPROMA)       ,&  !SPECIFIC HUMIDITY AT 2 METERS
          & ZRBF(IOFF2+2*NPROMA)       ,&  !DEW POINT TEMPERATURE AT 2 METERS
          & ZRBF(IOFF2+3*NPROMA)       ,&  !GRAVITY WAVE DRAG SURFACE U-STRESS
          & ZRBF(IOFF2+4*NPROMA)       ,&  !GRAVITY WAVE DRAG SURFACE V-STRESS
          & ZRBF(IOFF2+5*NPROMA)       ,&  !U-COMPONENT OF WIND AT 10 METERS
          & ZRBF(IOFF2+6*NPROMA)       ,&  !V-COMPONENT OF WIND AT 10 METERS
          & ZRBF(IOFF2+7*NPROMA)       ,&  !SURFACE SENSIBLE HEAT FLUX
          & ZRBF(IOFF2+8*NPROMA)       ,&  !SURFACE MOISTURE FLUX
          & ZRBF(IOFF2+9*NPROMA)       ,&  !SEA-ICE MASK
          & ZRBF(IOFF2+10*NPROMA)      ,&  !LAND/SEA MASK
          & ZRBF(IOFF2+11*NPROMA)      ,&  !PRESSURE AT SURFACE
          & ZRBF(IOFF2+12*NPROMA)      ,&  !SKIN TEMPERATURE
          & ZRBF(IOFF2+13*NPROMA)      ,&  !OBSERVATIONS
          & ZRBF(IOFF3)                ,&  !SINE OF LATITUDE
          & ZRBF(IOFF3+1*NPROMA)       ,&  !COSINE OF LONGITUDE
          & ZRBF(IOFF3+2*NPROMA)       ,&  !SINE OF LONGITUDE
          & IRBF(1)                    ,&  ! SATID
          & IRBF(1+NPROMA)             ,&  ! FOV
          & ZRBF(IOFF3+5*NPROMA)       ,&  !ZENITH ANGLE
          ! OUTPUTS
          & ISBF(1)                   ,& ! IID, FAILURE CODE (0=OK)
          & ISBF(1+1*NPROMA)          ,& ! IIT, NO. ITERATIONS
          & ISBF(1+2*NPROMA)          ,& ! IIS, NO. SIMULATIONS
          & ZNBF(1)                   ,& ! EPSG, convergence test for minimisation
          & ISBF(1+3*NPROMA)          ,& ! IIM, MINIMIZATION STATUS CODE 
          & ZDUM(1,1,1), ZDUM(1,1,2)  ,& ! DUMMY
          & ZDUM(1,1,3), ZDUM(1,1,4)  ,& ! DUMMY
          & ZDUM(1,1,5), ZDUM(1,1,6)  ,& ! DUMMY
          & ZDUM(1,1,7), ZDUM(1,1,8)  ,& ! DUMMY
          & ZDUM(1,1,9), ZDUM(1,1,10) ,& ! DUMMY
          & ZDUM(1,1,11), ZDUM(1,1,12),& ! DUMMY              
          & ZDUM(1,1,13)              ,& ! DUMMY
          & ZSBF(IOFF4)                 ,& ! ZRAIN_1D(1,M_U10_AN)
          & ZSBF(IOFF4+  NPROMA)        ,& ! ZRAIN_1D(1,M_V10_AN)
          & ZSBF(IOFF4+2*NPROMA)        ,& ! ZRAIN_1D(1,M_PS_FG)
          & ZSBF(IOFF4+3*NPROMA)        ,& ! ZRAIN_1D(1,M_PC_FG)
          & ZSBF(IOFF4+4*NPROMA)        ,& ! ZRAIN_1D(1,M_PS_AN)
          & ZSBF(IOFF4+5*NPROMA)        ,& ! ZRAIN_1D(1,M_PC_AN)
          & ZSBF(IOFF4+6*NPROMA)        ,& ! ZRAIN_1D(1,M_WV_FG)
          & ZSBF(IOFF4+7*NPROMA)        ,& ! ZRAIN_1D(1,M_WV_AN)
          & ZSBF(IOFF4+8*NPROMA)        ,& ! ZRAIN_1D(1,M_ER_FG)
          & ZSBF(IOFF4+9*NPROMA)        ,& ! ZRAIN_1D(1,M_ER_AN)
          & ZSBF(IOFF4+10*NPROMA)       ,& ! ZRAIN_1D(1,M_CO_AN)
          & ZSBF(IOFF5)               ,& ! ZRAIN_2DCHAN(1,1,M_TB_FG)
          & ZSBF(IOFF5+  INC2)        ,& ! ZRAIN_2DCHAN(1,1,M_TB_AN)
          & ZSBF(IOFF5+2*INC2)        ,& ! ZRAIN_2DCHAN(1,1,M_TB_ER)
          & ZSBF(IOFF5+3*INC2)        ,& ! ZRAIN_2DCHAN(1,1,M_TB_BI)
          & ZSBF(IOFF5+4*INC2)        ,& ! ZRAIN_2DCHAN(1,1,M_TB_3D)
          & ZDIAG                     )
          
        CALL MPL_SEND(ISBF,KTAG=IT+100,KDEST=ICOPROCS(1),&
          &KMP_TYPE=JP_BLOCKING_STANDARD,CDSTRING='ONEDVAR_RAINTB_HLP:')
        CALL MPL_SEND(ZSBF,KTAG=IT,KDEST=ICOPROCS(1),&
          &KMP_TYPE=JP_BLOCKING_STANDARD,CDSTRING='ONEDVAR_RAINTB_HLP:')
        CALL MPL_SEND(ZNBF,KTAG=IT+101,KDEST=ICOPROCS(1),&
          &KMP_TYPE=JP_BLOCKING_STANDARD,CDSTRING='ONEDVAR_RAINTB_HLP:')
        CALL MPL_SEND(ZDIAG,KTAG=IT+102,KDEST=ICOPROCS(1),&
          &KMP_TYPE=JP_BLOCKING_STANDARD,CDSTRING='ONEDVAR_RAINTB_HLP:')
        CALL MPL_RECV(ZRBF,KTAG=IT,KSOURCE=ICOPROCS(1),CDSTRING='ONEDVAR_RAINTB_HLP:')
        
      ENDDO
      DEALLOCATE(ZRBF)
      DEALLOCATE(ZNBF)
      DEALLOCATE(ZDIAG)
      DEALLOCATE(IRBF)
      DEALLOCATE(ISBF)
      DEALLOCATE(ZSBF)
      DEALLOCATE(ZDUM)
    ENDIF
!$OMP END PARALLEL
CALL GSTATS(1003,1)

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

END SUBROUTINE ONEDVAR_RAINTB_HLP
