SUBROUTINE RAD1CEMIS(KOBS,K_ISENSOR,PANGL,P_ZANGL,&
 & P_ZLS5,P_ZTS5,P_ZCI5,P_ZSN5,P_ZWS5,&
 & P_ZLAT,P_ZLON,K_IFOV,LDRADPP,K_ISUR_M,PBATS_VEG)
! ROUTINE.
! --------
!    RAD1CEMIS

! PURPOSE.
! --------
!     ROUTINE CALLED BY IFS OBSERVATION SCREENING MODULE TO 
!     ESTIMATE SURFACE EMISSIVITY FOR LEVEL-1C RADIANCE OBSERVATIONS AND LOAD
!     INTO ODB FOR LATER USE BY ANALYSIS

! INPUT.
! ------
!     KOBS 
!     ISENSOR - input satellite sensor indicator
!     PANGL - input satellite nadir angle (deg)
!     ZANGL - input satellite zenith angle (deg)
!     PU10- input model 10m wind U component
!     PV10- input model 10m wind V component
!     PLS - input model land/sea flag
!     PTS - input model skin temperature
!     PSN - input model snow cover
!     PWS - input model soil moisture
!     CI5 - input model ice fraction
!     P_ZLAT - observation latitude
!     P_ZLON - observation longitude
!     K_IFOV - field of view number

! OUTPUT.
! -------
!     ISUR_M- surface type     
!     PBATS_VEG-BATS vegetation type (only if emissivity atlases available)

! EXTERNALS.
! ----------
!    EMIS_IR,EMIS_MW, EMISKF_ESTIMATE_EMISSIVITY

! AUTHOR.
! -------
! Tony McNally (aug 98)
! MODIFICATIONS
! Xu Li          00/05/23 : Add the handle of emissivity for VTPR 
! g kelly        00/08/23 : Add window chans for microwave emissivity   
! c koepken      00/10/17 : add ISATID as input arg. for EMIS_IR
!                00/11/06 : add IR emiss calculation (SSIREM) for VTPR
! c koepken      01/10/30 : do IR emiss computations for goes imager
! M Szyndel      15/08/03 : Added MSG
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
! M Szyndel      01/03/05 : Added MSG-2, MTSAT,FY2C
! P Bauer, G Kelly, N Bormann  05/02/06 : Added AMSRE, SSMIS, TMI
! N Bormann      17/02/06 : METOP
! N Bormann      15/08/06 : Update LWP regressions for TMI, AMSRE, SSMIS
!                           use bias corrected radiances in these regressions
! V Guidard      23/02/07 : Add IASI (after A Collard)
! A Collard      07/11/06 : Add IASI
! A Geer         17/09/07 : Move MW imager LWP regressions to shared function
! F Karbou, N Bormann   09/05/07 : to calculate land emissivities at mw frequencies 
! A Geer          9/01/09 : Minor change to MW imager LWP regression function interface      
! B Krzeminski   29-Jan-2009 : added mw land emissivity estimations from Kalman Filter atlas
! W Bell         05/02/09 : Add Windsat      
! Q Lu                  11/03/09 : Add fy3a IRAS, MWTS, MWHS, MWRI 

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

USE YOMDIMO  , ONLY : NMXTCH
USE YOMDB   
USE YOMVNMB  , ONLY : NVNUMB
USE YOMTVRAD , ONLY : MSENSOR_HIRS, MSENSOR_MSU, MSENSOR_SSU, &
 & MSENSOR_IRAS,  MSENSOR_MWTS,  MSENSOR_MWHS,MSENSOR_MWRI,   &
 & MSENSOR_AMSUA, MSENSOR_AMSUB, MSENSOR_MHS, MSENSOR_CHANOFFL, &
 & MSENSOR_VTPR1, MSENSOR_VTPR2, MSENSOR_METEOSAT, &
 & MSENSOR_GOESIMG,MSENSOR_SSMI,MSENSOR_SSMIS,MSENSOR_AIRS, MEMIS_SW, &
 & MSENSOR_MTSATIMG, MSENSOR_VISSR, MSENSOR_AMSRE, MSENSOR_TMI, &
 & MSENSOR_MSG,MSENSOR_CHANCOUNT,MSENSOR_MSG_HR, MSENSOR_IASI, &
 & MSENSOR_WINDSAT
USE YOMEMIS , ONLY : LATLAS_EMIS, LDYN_EMIS, LDYN_TSKIN, RLSTH
USE YOMCT0B  , ONLY : LECMWF

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KOBS 
INTEGER(KIND=JPIM),INTENT(IN)    :: K_ISENSOR
REAL(KIND=JPRB)   ,INTENT(IN)    :: PANGL
REAL(KIND=JPRB)   ,INTENT(IN)    :: P_ZANGL
REAL(KIND=JPRB)   ,INTENT(IN)    :: P_ZLS5
REAL(KIND=JPRB)   ,INTENT(IN)    :: P_ZTS5
REAL(KIND=JPRB)   ,INTENT(IN)    :: P_ZCI5
REAL(KIND=JPRB)   ,INTENT(IN)    :: P_ZSN5
REAL(KIND=JPRB)   ,INTENT(IN)    :: P_ZWS5
REAL(KIND=JPRB)   ,INTENT(IN)    :: P_ZLAT
REAL(KIND=JPRB)   ,INTENT(IN)    :: P_ZLON
INTEGER(KIND=JPIM),INTENT(IN)    :: K_IFOV
LOGICAL           ,INTENT(IN)    :: LDRADPP 
INTEGER(KIND=JPIM),INTENT(OUT)   :: K_ISUR_M
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PBATS_VEG

INTEGER(KIND=JPIM), PARAMETER    :: IMWCHANS = 24
REAL(KIND=JPRB)                  :: ZTB_MWIMAGER(IMWCHANS)
LOGICAL                          :: LL_D37, LL_LWP

REAL(KIND=JPRB) :: Z23R,Z31R,Z50R,Z89R,ZLWP
REAL(KIND=JPRB) :: ZEKF
REAL(KIND=JPRB) :: ZEMIS(NMXTCH)
REAL(KIND=JPRB) :: ZZEMIS(NMXTCH)
INTEGER(KIND=JPIM)   :: IPFOV

INTEGER(KIND=JPIM) :: ICHAN, INN, JBODY
REAL(KIND=JPRB) :: ZHOOK_HANDLE

LOGICAL, EXTERNAL :: MWIMAGER_CLOUD

#include "abor1.intfb.h"
#include "emis_atlas.intfb.h"
#include "emis_ir.intfb.h"
#include "emis_mw.intfb.h"
#include "emis_mw_n.intfb.h"

#include "fcobs.h"
#include "openmp_obs.h"

IF (LHOOK) CALL DR_HOOK('RAD1CEMIS',0,ZHOOK_HANDLE)
K_ISUR_M = 0
ZLWP   = 0.0_JPRB
IPFOV=K_IFOV


! Atlas emissivities over land

IF ( P_ZLS5 > RLSTH &
   &    .AND. (LDYN_EMIS(K_ISENSOR) .OR. LDYN_TSKIN(K_ISENSOR) &
   &           .OR. LATLAS_EMIS(K_ISENSOR )) ) THEN 

  IF (LECMWF) THEN

    ! Estimate emissivities from Kalman Filter atlas and store them
    ! in mwemis_atlas_1@atovs_body (can return ZEKF = RMDI in some cases!).

    DO JBODY = MLNKH2B(KOBS),MLNKH2B(KOBS+1)-1
      ICHAN = NINT(ROBODY(JBODY,MDBPPP))
      CALL EMISKF_ESTIMATE_EMISSIVITY ( K_ISENSOR, P_ZLON, P_ZLAT, ICHAN, ZEKF, IPFOV )
      SATBODY(JBODY,MDB_MWEMIS_ATLAS1) = ZEKF
    ENDDO

  ELSE

    ! get emissivity from an atlas

    CALL EMIS_ATLAS (K_ISENSOR,P_ZLAT,P_ZLON,P_ZANGL,&
     & IPFOV,ZZEMIS,PBATS_VEG) 

    DO JBODY = MLNKH2B(KOBS),MLNKH2B(KOBS+1)-1   
      ICHAN = NINT(ROBODY(JBODY,MDBPPP)) !chan # from body
      IF(LDRADPP) ICHAN=ICHAN-MSENSOR_CHANOFFL(K_ISENSOR)
      DO INN = 1,MSENSOR_CHANCOUNT(K_ISENSOR)
        IF(INN == ICHAN) THEN !chan # eq chan # from body
          SATBODY(JBODY,MDB_MWEMIS_ATLAS1) = &
           &                    ZZEMIS(INN+MSENSOR_CHANOFFL(K_ISENSOR))
        ENDIF
      ENDDO
    ENDDO

  ENDIF

ENDIF

! Microwave emissivities and cloud/rain detection

IF(       K_ISENSOR == MSENSOR_MSU  .OR. K_ISENSOR == MSENSOR_AMSUA &
   & .OR. K_ISENSOR == MSENSOR_MWTS .OR. K_ISENSOR == MSENSOR_MWHS  &
   & .OR. K_ISENSOR == MSENSOR_MWRI &
   & .OR. K_ISENSOR == MSENSOR_SSMI .OR. K_ISENSOR == MSENSOR_SSMIS &
   & .OR. K_ISENSOR == MSENSOR_AMSUB.OR. K_ISENSOR == MSENSOR_MHS &
   & .OR. K_ISENSOR == MSENSOR_TMI  .OR. K_ISENSOR == MSENSOR_AMSRE &
   & .OR. K_ISENSOR == MSENSOR_WINDSAT ) THEN  

  IF( MEMIS_SW == 1 ) THEN
    Z23R = 270._JPRB
    Z31R = 270._JPRB
    Z50R = 270._JPRB
    Z89R = 270._JPRB 

    IF(K_ISENSOR == MSENSOR_MSU .OR. K_ISENSOR == MSENSOR_AMSUA  &
     & .OR. K_ISENSOR == MSENSOR_MWTS) THEN
       DO JBODY = MLNKH2B(KOBS),MLNKH2B(KOBS+1)-1   !loop body entries (20ir/4mw)
          IF(ROBODY(JBODY,MDBVNM) == NVNUMB(50)) THEN
             ICHAN = NINT(ROBODY(JBODY,MDBPPP)) !chan # from body
             IF(ICHAN == 1  ) Z23R = ROBODY(JBODY,MDBVAR)
             IF(ICHAN == 2  ) Z31R = ROBODY(JBODY,MDBVAR)
             IF(ICHAN == 3  ) Z50R = ROBODY(JBODY,MDBVAR)
             IF(K_ISENSOR == MSENSOR_MSU   .AND. ICHAN ==  4 ) Z89R = ROBODY(JBODY,MDBVAR)
             IF(K_ISENSOR == MSENSOR_AMSUA .AND. ICHAN == 15 ) Z89R = ROBODY(JBODY,MDBVAR)
             IF(K_ISENSOR == MSENSOR_MWTS  .AND. ICHAN ==  4 ) Z89R = ROBODY(JBODY,MDBVAR)
          ENDIF
       ENDDO
    ENDIF

    CALL EMIS_MW_N (K_ISENSOR,PANGL,P_ZLS5,P_ZTS5,P_ZSN5,P_ZWS5,P_ZCI5,&
                  & Z23R,Z31R,Z50R,Z89R,K_ISUR_M,ZEMIS)  


    !* SSM/I, TMI, AMSRE, SSMIS, WindSat cloud and rain detection     
    IF(K_ISENSOR == MSENSOR_SSMI  .OR. K_ISENSOR == MSENSOR_SSMIS .OR. &
     & K_ISENSOR == MSENSOR_MWRI  .OR. &
     & K_ISENSOR == MSENSOR_AMSRE .OR. K_ISENSOR == MSENSOR_TMI .OR. &
     & K_ISENSOR == MSENSOR_WINDSAT ) THEN

      DO JBODY = MLNKH2B(KOBS),MLNKH2B(KOBS+1)-1   !loop body entries (7mw)
        IF(ROBODY(JBODY,MDBVNM) == NVNUMB(50)) THEN
          ICHAN = NINT(ROBODY(JBODY,MDBPPP)) !chan # from body
          IF(ICHAN >= 1 .AND. ICHAN <= IMWCHANS) THEN
            ZTB_MWIMAGER (ICHAN) = ROBODY(JBODY,MDBVAR)
          ENDIF
        ENDIF
      ENDDO

      IF ( P_ZLS5 < 0.01_JPRB ) THEN 
        IF ( MWIMAGER_CLOUD( IMWCHANS, ZTB_MWIMAGER, K_ISENSOR, &
          & ZLWP, LL_D37, LL_LWP, .FALSE.) ) THEN
          IF (LL_LWP) K_ISUR_M = 10 ! ~ cloudy observation
          IF (K_ISENSOR == MSENSOR_SSMIS .AND. ZLWP > 0.3_JPRB) THEN
            K_ISUR_M = 13 ! SSMIS - some sounding channels have a higher threshold
          ENDIF
          IF (LL_D37) K_ISUR_M = 11 ! ~ rainy observation 
        ENDIF  
      ENDIF

      SATHDR(KOBS,MDB_CLDPTOP1_AT_ATOVS) = ZLWP

      IF (K_ISENSOR == MSENSOR_SSMI .AND. P_ZLS5 >= 0.01_JPRB ) K_ISUR_M = 12
      IF (P_ZCI5 >= 0.01_JPRB) K_ISUR_M = 12

    ENDIF
 
  ELSE
    CALL EMIS_MW (K_ISENSOR,P_ZLS5,P_ZTS5,P_ZSN5,P_ZWS5,P_ZCI5,ZEMIS)

  ENDIF

  ! IR emissivities

ELSEIF (K_ISENSOR == MSENSOR_HIRS .OR. K_ISENSOR == MSENSOR_MSG .OR. &
   & K_ISENSOR == MSENSOR_METEOSAT .OR. K_ISENSOR == MSENSOR_GOESIMG .OR. &
   & K_ISENSOR == MSENSOR_MTSATIMG .OR. K_ISENSOR == MSENSOR_VISSR .OR. &
   & K_ISENSOR == MSENSOR_VTPR1 .OR. K_ISENSOR == MSENSOR_VTPR2 .OR. &
   & K_ISENSOR == MSENSOR_MSG_HR .OR. K_ISENSOR == MSENSOR_IRAS) THEN  

  CALL EMIS_IR(P_ZLS5,P_ZTS5,P_ZSN5,P_ZWS5,ZEMIS    )
ELSEIF (K_ISENSOR == MSENSOR_AIRS .OR. K_ISENSOR == MSENSOR_IASI) THEN

! AIRS and IASI emissivity calculations dummied for now
 
ELSEIF (K_ISENSOR == MSENSOR_SSU) THEN

! no need for an SSU emissivity calculation

ELSE
  write(0,*)'***Error in RAD1CEMIS: UNRECOGNIZED SENSOR INDICATOR=',K_ISENSOR
  CALL ABOR1('UNRECOGNIZED SENSOR INDICATOR')
ENDIF

DO JBODY = MLNKH2B(KOBS),MLNKH2B(KOBS+1)-1   !loop body entries (20ir/4mw)
  ICHAN = NINT(ROBODY(JBODY,MDBPPP)) !chan # from body
  IF(LDRADPP) ICHAN=ICHAN-MSENSOR_CHANOFFL(K_ISENSOR)

  IF(K_ISENSOR == MSENSOR_AIRS .OR. K_ISENSOR == MSENSOR_IASI) THEN

    ROBODY(JBODY,MDBPER) = 0.0_JPRB  ! should activate ISEM in RTTOV

  ELSEIF(K_ISENSOR == MSENSOR_SSU) THEN

    DO INN = 1,MSENSOR_CHANCOUNT(K_ISENSOR)
      IF(INN == ICHAN) THEN !chan # eq chan # from body
        ROBODY(JBODY,MDBPER) = 1.0_JPRB   
      ENDIF
    ENDDO
  ELSE
    ! Default: Use what was set above (e.g., in emis_ir, emis_mw_n)

    DO INN = 1,MSENSOR_CHANCOUNT(K_ISENSOR)
      IF(INN == ICHAN) THEN !chan # eq chan # from body
        ROBODY(JBODY,MDBPER) = ZEMIS(INN+MSENSOR_CHANOFFL(K_ISENSOR))
      ENDIF
    ENDDO

  ENDIF

ENDDO

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