SUBROUTINE SMOS_PROCESS(PSMOS_OBS_BUF,KSMOS_BUF,PSMOS_ANGLE)

!**** *SMOS_PROCESS* - 
!
!     J. Muñoz Sabater   ECMWF    Oct 2009
!
!     PURPOSE.
!     -------
!     GET SMOS OBS FROM ODB TO GRID-POINT SPACE
!
!**   INTERFACE.
!**   ---------
!**   *CALL* * SMOS_PROCESS()*  (FROM GP_MODEL) 
!
!     Explicit arguments :
!
!     Modifications :
!          J. Munoz Sabater  23-11-2009 : New dimension por polarisation 
!     ------------------------------------------------------------------

 USE PARKIND1  ,ONLY : JPIM     ,JPRB
 USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
 USE YOMDB
 USE YOMGEM    ,ONLY : NGPTOT
 USE YOMMP     ,ONLY : MYPROC
 USE YOMECTAB  ,ONLY : NTSLOTNOS, NTSLOTNOE
 USE YOMSMOS   ,ONLY : NPOL_MAX
 
 IMPLICIT NONE

 REAL(KIND=JPRB)    ,INTENT(INOUT) :: PSMOS_OBS_BUF (NGPTOT, 0:NPOL_MAX-1)
 REAL(KIND=JPRB)    ,INTENT(INOUT) :: PSMOS_ANGLE   (NGPTOT, 0:NPOL_MAX-1)
 INTEGER(KIND=JPIM) ,INTENT(INOUT) :: KSMOS_BUF     (NGPTOT, 0:NPOL_MAX-1)

 REAL(KIND=JPRB)    ,ALLOCATABLE   :: ZLONOBS(:), ZLATOBS(:)
 REAL(KIND=JPRB)    ,ALLOCATABLE   :: ZOB_DIST_TO_GP(:)
 REAL(KIND=JPRB)    ,ALLOCATABLE   :: ZOBSV_OBS(:)
 REAL(KIND=JPRB)    ,ALLOCATABLE   :: ZOBSV_ANG(:)
 REAL(KIND=JPRB)                   :: ZHOOK_HANDLE, ZINFO(0)
 INTEGER(KIND=JPIM) ,ALLOCATABLE   :: ISMOS_OBS(:)
 INTEGER(KIND=JPIM) ,ALLOCATABLE   :: ISEQNO(:)
 INTEGER(KIND=JPIM)                :: ILEN, JOBS, INLENMAX
 INTEGER(KIND=JPIM)                :: IPOL
 INTEGER(KIND=JPIM)                :: JBODY, INFO(3), IRET, IBODY
 INTEGER(KIND=JPIM) ,ALLOCATABLE   :: ICMBDY(:)


INTERFACE
#include "getdb.h"
#include "putdb.h"
END INTERFACE
!
#include "smos_nearest.intfb.h"
#include "smos_obs2gp.intfb.h"
#include "smos_iobs2gp.intfb.h"
#include "openmp_obs.h"

 IF (LHOOK) CALL DR_HOOK('SMOS_PROCESS',0,ZHOOK_HANDLE)

INLENMAX=0
DO IPOL = 0, NPOL_MAX-1
!Search with polarisation has maximum number of observations
 INFO(1) =NTSLOTNOS
 INFO(2) =NTSLOTNOE
 INFO(3) =IPOL
 CALL GETDB('MWAVE_COUNT_SMOS',1, ILEN, INFO, 3, ZINFO, 0, &
             & -1, -1, -1, -1, -1, -1)
 IF(ILEN > INLENMAX) INLENMAX=ILEN
 CALL PUTDB('MWAVE_COUNT_SMOS',1, IRET, INFO, 0, ZINFO, 0)

ENDDO

DO IPOL = 0, NPOL_MAX-1

! This variables are not used in GETDB, since they are hardcoded in SQL request
! IOBSTYPE= NSMOS    ! SMOS obstype
! ICDTYP  = NSMOS     ! SMOS codetype
! ISENSOR = 18        ! SMOS sensor identification number

 INFO(1) =NTSLOTNOS
 INFO(2) =NTSLOTNOE
 INFO(3) =IPOL 

 CALL GETDB('MWAVE_PROCESS_SMOS',0, ILEN, INFO, 3, ZINFO, 0, &
              & -1, -1, -1, -1, -1, -1)

 ALLOCATE(ICMBDY(ILEN))
 ALLOCATE(ZOBSV_OBS(ILEN))
 ALLOCATE(ZOBSV_ANG(ILEN))
 ALLOCATE(ZLATOBS(ILEN))
 ALLOCATE(ZLONOBS(ILEN))
 ALLOCATE(ISEQNO(ILEN))
 ALLOCATE(ZOB_DIST_TO_GP(ILEN))
 ALLOCATE(ISMOS_OBS(ILEN))

 ZOBSV_OBS          =  0.0_JPRB
 ZOBSV_ANG          =  0.0_JPRB
 ZOB_DIST_TO_GP (:) = -1.0_JPRB

 CALL GSTATS(2014,0)

 DO JOBS=1,ILEN
   ZLATOBS(JOBS)=ROBHDR(JOBS,MDBLAT)
   ZLONOBS(JOBS)=ROBHDR(JOBS,MDBLON)
   ISEQNO(JOBS)=ROBHDR(JOBS,MDBONM)
   ICMBDY(JOBS)=MLNKH2B(JOBS+1)-MLNKH2B(JOBS)
   DO JBODY=1,ICMBDY(JOBS)
     IBODY=MLNKH2B(JOBS)+(JBODY-1)
     ZOBSV_OBS(JOBS)=ROBODY(IBODY,MDBVAR)
     ZOBSV_ANG(JOBS)=ROBODY(IBODY,MDB_TB_ANG_SMOS)
   ENDDO
 ENDDO

 CALL GSTATS(2014,1)

! Map from observation to grid point space
! Compute distance to nearest point and store it in PUTDB
  CALL SMOS_NEAREST(ZLATOBS, ZLONOBS, ZOB_DIST_TO_GP, &
    & ISEQNO, ILEN, IPOL, INLENMAX, LDALL = .FALSE., LDSUM = .FALSE.)

DO JOBS=1,ILEN
  ROBHDR(JOBS,MDB_GP_DIST)=ZOB_DIST_TO_GP(JOBS)
ENDDO

! Initialise status flag in obs space
 ISMOS_OBS(:) = 0

 CALL SMOS_OBS2GP(ZOBSV_OBS, PSMOS_OBS_BUF(:,IPOL), IPOL,&
       & LDSUM = .FALSE., LDMEAN = .FALSE.)
 CALL SMOS_OBS2GP(ZOBSV_ANG, PSMOS_ANGLE(:,IPOL),   IPOL,&
       & LDSUM = .FALSE., LDMEAN = .FALSE.)  
       ! LDSUM and LDMEAN always to FALSE, or angle averages !
 CALL SMOS_IOBS2GP(ISMOS_OBS, KSMOS_BUF(:,IPOL),    IPOL)

 DEALLOCATE(ZOBSV_OBS)
 DEALLOCATE(ZOBSV_ANG)
 DEALLOCATE(ICMBDY)
 DEALLOCATE(ZLATOBS)
 DEALLOCATE(ZLONOBS)
 DEALLOCATE(ISEQNO)
 DEALLOCATE(ZOB_DIST_TO_GP)
 DEALLOCATE(ISMOS_OBS)

 CALL PUTDB('MWAVE_PROCESS_SMOS', 0, IRET, INFO, 0, ZINFO, 0)

ENDDO ! ipol-loop
 
 IF (LHOOK) CALL DR_HOOK('SMOS_PROCESS',1,ZHOOK_HANDLE)
 
END SUBROUTINE SMOS_PROCESS
