  SUBROUTINE NEW_THINN(KTSLOT)

!**** SUBROUTINE THINN - CONTROL OF THINNING OF SATELLITE DATA

!          HEIKKI JARVINEN   ECMWF          15/AUG/1995
!          DRASKO VASILJEVIC ECMWF          25/OCT/1999

!*    PURPOSE
!     -------

!        REDUCE THE NUMBER OF SATELLITE REPORTS (TOVS/SATEM-TCWC/SATOB/GPSRO/RADAR)
!     TO AN AFFORDABLE LEVEL. ADVAR QUALITY CONTROL HAS ALREADY BEEN

!**** SUBROUTINE NEW_THINN - CONTROL OF THINNING OF SATELLITE DATA

!          HEIKKI JARVINEN   ECMWF          15/AUG/1995
!          DRASKO VASILJEVIC ECMWF          25/OCT/1999

!*    PURPOSE
!     -------

!        REDUCE THE NUMBER OF SATELLITE REPORTS (TOVS/SATEM-TCWC/SATOB)
!     TO AN AFFORDABLE LEVEL. ADVAR QUALITY CONTROL HAS ALREADY BEEN
!     PERFORMED AT THIS STAGE.

!**   INTERFACE
!     ---------

!     ***CALL*NEW_THINN(KTSLOT)
!     WHERE KTSLOT = TIME SLOT

!        NEW_THINN IS CALLED BY SUBROUTINE DECIS

!*    METHOD
!     ------

!*    EXTERNALS
!     ---------

!**   MODIFICATIONS
!     -------------
!     E.Gerard    150199 : SSMI PWC and WSP have their own thinning parameter
!     E.Andersson 040200 : Introduced generic thinner routine
!     A.Dethof    050804 : Introduced thinning for REO3 data
!     M Szyndel   230305 : Added MTSAT and FY-2
!     P Bauer, G Kelly, N Bormann  070206 : Added AMSRE, SSMIS, TMI
!     P Poli      071206 : Added GPSRO
!     H. Hersbach 121106 : Add thinning for ASCAT and set equal to ERS thinning
!     N. Bormann  300307 : Combined thinning for (SSMI, AMSRE, SSMIS, TMI) and
!                          (AMSUB, MHS)
!     A Collard   130207   Add IASI.  Andrew Collard
!     A.Dethof    141008 : Add varno as criterion for reo3 thinning
!     C Payan     111208   Add IDPTU=173 case (EARS MODIS SATAM)
!     W.Bell      050209 : Add Windsat.
!     09-03-09 Qifeng Lu     : add FY3A instrument (IRAS, MWTS, MWHS, and MWRI)
!     N Bormann   011209   Rationalisation of AMV thinning
!     A. Fouilloux   31/03/10: use new codetype for MERIS data (replaced NSSMI by NTCWC)
!                              SSMI Rain data not thinned but MERIS data thinned
!     ------------------------------------------------------------------

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

USE YOMLUN   , ONLY : NULOUT   ,NULERR
USE YOMSCC   , ONLY : RMIND_TOV,   RFIND_TOV, RMIND_SSMI,&
 & RMIND_SATOB, RFIND_SATOB,&
 & RMIND_SATAM, RFIND_SATAM, RMIND_RAD1C, RFIND_RAD1C,&
 & RMIND_SCATT_ERS, RFIND_SCATT_ERS,&
 & RMIND_GPSRO, RFIND_GPSRO,&
 & RMIND_RADAR, RFIND_RADAR
USE YOMCOCTP , ONLY : NSATEM,   NTCWC,    NGTHRB,   NGTHR1,&
 & NGTHR2,   NSATOB,   NSTB87,   NSCATT,   NSCAT2,  NSCAT3,&
 & NSTMCD, NSTRO3, NRADAR, NLIMB, NGPSRO
USE YOMTVRAD , ONLY : MSENSOR_METEOSAT,MSENSOR_AMSUA,MSENSOR_AMSUB,&
 & MSENSOR_IRAS,MSENSOR_MWTS,MSENSOR_MWHS,MSENSOR_MWRI, &
 & MSENSOR_HIRS,MSENSOR_MSU,MSENSOR_SSU,MSENSOR_SSMI,MSENSOR_SSMIS,&
 & MSENSOR_AMSRE,MSENSOR_TMI,MSENSOR_MHS,&
 & MSENSOR_VTPR1,MSENSOR_VTPR2,MSENSOR_GOESIMG,MXSENSOR,&
 & MSENSOR_AIRS, MSENSOR_MSG,MSENSOR_MSG_HR, MSENSOR_MTSATIMG,&
 & MSENSOR_VISSR, MSENSOR_IASI,NSENSOR_MERIS,MSENSOR_WINDSAT
USE YOMOBS   , ONLY : LMFSCREEN,&
 & LSATOBTHIN_NO_SQ, LSATAMTHIN_NO_SQ, LTOVSTHIN_NO_SQ ,&
 & LRAD1CTHIN_NO_SQ, LSSMITHIN_NO_SQ , LSCATTTHIN_NO_SQ,&
 & LSATEMTHIN_NO_SQ, LREO3THIN_NO_SQ , LGPSROTHIN_NO_SQ
USE yom_reo3_thin, ONLY : O3THIN_INFO,N_REO3_THIN_INFO

IMPLICIT NONE

!     DUMMY SCALARS
INTEGER(KIND=JPIM), INTENT(IN)   :: KTSLOT
INTEGER(KIND=JPIM),PARAMETER :: JLMXSENSOR_PER_GROUP=6, IMAXCODETYPE=2
INTEGER(KIND=JPIM) :: ISENSOR_LIST(JLMXSENSOR_PER_GROUP,MXSENSOR+1)
INTEGER(KIND=JPIM) :: ICODETYPE_LIST(IMAXCODETYPE),ICODETYPE
INTEGER(KIND=JPIM) :: ILEN,INFO(1),ISENSOR,J,IL,IG,JBOX,IBOX,IVNM_RQ,IVARNO
INTEGER(KIND=JPIM) :: ISENSOR_RQ

REAL(KIND=JPRB) :: ZINFO(1),ZBOX

LOGICAL :: LL_NEW_SATOB_THINNING
LOGICAL :: LL_NEW_SATAM_THINNING
LOGICAL :: LL_NEW_TOVS_THINNING
LOGICAL :: LL_NEW_RAD1C_THINNING
LOGICAL :: LL_NEW_TCWC_THINNING
LOGICAL :: LL_NEW_SCATT_THINNING
LOGICAL :: LL_NEW_SATEM_THINNING
LOGICAL :: LL_NEW_REO3_THINNING
LOGICAL :: LL_NEW_GPSRO_THINNING

LOGICAL :: LL_TOVS_RTOVS_THINNING
LOGICAL :: LL_TOVS_RTOVS_THINNING_PRE
LOGICAL :: LL_TOVS_RTOVS_THINNING_THIN
LOGICAL :: LL_TOVS_RTOVS_THINNING_POST

LOGICAL :: LL_GEOS_RAD1C_THINNING
LOGICAL :: LL_GEOS_RAD1C_THINNING_PRE
LOGICAL :: LL_GEOS_RAD1C_THINNING_THIN
LOGICAL :: LL_GEOS_RAD1C_THINNING_POST

LOGICAL :: LL_RAD1C_RAD1C_THINNING
LOGICAL :: LL_RAD1C_RAD1C_THINNING_PRE
LOGICAL :: LL_RAD1C_RAD1C_THINNING_THIN
LOGICAL :: LL_RAD1C_RAD1C_THINNING_POST

LOGICAL :: LL_TCWC_THINNING
LOGICAL :: LL_TCWC_THINNING_PRE
LOGICAL :: LL_TCWC_THINNING_THIN
LOGICAL :: LL_TCWC_THINNING_POST

LOGICAL :: LL_SATOB_SATOB_THINNING
LOGICAL :: LL_SATOB_SATOB_THINNING_PRE
LOGICAL :: LL_SATOB_SATOB_THINNING_THIN
LOGICAL :: LL_SATOB_SATOB_THINNING_POST

LOGICAL :: LL_SATAM_SATOB_THINNING
LOGICAL :: LL_SATAM_SATOB_THINNING_PRE
LOGICAL :: LL_SATAM_SATOB_THINNING_THIN
LOGICAL :: LL_SATAM_SATOB_THINNING_POST

LOGICAL :: LL_SCATT_THINNING
LOGICAL :: LL_SCATT_THINNING_PRE
LOGICAL :: LL_SCATT_THINNING_THIN
LOGICAL :: LL_SCATT_THINNING_POST

LOGICAL :: LL_SATEM_THINNING
LOGICAL :: LL_SATEM_THINNING_PRE
LOGICAL :: LL_SATEM_THINNING_THIN
LOGICAL :: LL_SATEM_THINNING_POST

LOGICAL :: LL_REO3_THINNING
LOGICAL :: LL_REO3_THINNING_PRE
LOGICAL :: LL_REO3_THINNING_THIN
LOGICAL :: LL_REO3_THINNING_POST

LOGICAL :: LL_GPSRO_THINNING
LOGICAL :: LL_GPSRO_THINNING_PRE
LOGICAL :: LL_GPSRO_THINNING_THIN
LOGICAL :: LL_GPSRO_THINNING_POST
LOGICAL :: LL_RADAR_THINNING

REAL(KIND=JPRB) :: ZHOOK_HANDLE

INTERFACE
#include "getdb.h"
#include "putdb.h"
END INTERFACE

#include "new_thinner.intfb.h"
#include "new_thinner_no_sq.intfb.h"
#include "post_thinner.intfb.h"
#include "pre_thinner.intfb.h"
#include "abor1.intfb.h"
#include "pre_thinn_radar.intfb.h"
#include "new_thinn_radar.intfb.h"

!*
!     ------------------------------------------------------------------

!        1.       INITIALIZE
!                 ----------

IF (LHOOK) CALL DR_HOOK('NEW_THINN',0,ZHOOK_HANDLE)
WRITE(NULOUT,'(''     NEW_THINN; NEW STYLE THINNING OF OBSERVATIONS '')')

LL_NEW_SATOB_THINNING = LSATOBTHIN_NO_SQ
LL_NEW_SATAM_THINNING = LSATAMTHIN_NO_SQ
LL_NEW_TOVS_THINNING  = LTOVSTHIN_NO_SQ
LL_NEW_RAD1C_THINNING = LRAD1CTHIN_NO_SQ
!*AF to be cleaned should have LTCWCTHIN_NO_SQ (to be defined in yomobs)
LL_NEW_TCWC_THINNING  = LSSMITHIN_NO_SQ

LL_NEW_SCATT_THINNING = LSCATTTHIN_NO_SQ
LL_NEW_SATEM_THINNING = LSATEMTHIN_NO_SQ
LL_NEW_REO3_THINNING  = LREO3THIN_NO_SQ
LL_NEW_GPSRO_THINNING = LGPSROTHIN_NO_SQ

!!--LL_TOVS_RTOVS_THINNING       = .TRUE.
LL_TOVS_RTOVS_THINNING       = .FALSE.
LL_TOVS_RTOVS_THINNING_PRE   = .TRUE.
LL_TOVS_RTOVS_THINNING_THIN  = .TRUE.
LL_TOVS_RTOVS_THINNING_POST  = .FALSE.

LL_GEOS_RAD1C_THINNING       = .TRUE.
LL_GEOS_RAD1C_THINNING_PRE   = .TRUE.
LL_GEOS_RAD1C_THINNING_THIN  = .TRUE.
LL_GEOS_RAD1C_THINNING_POST  = .FALSE.

LL_RAD1C_RAD1C_THINNING      = .TRUE.
LL_RAD1C_RAD1C_THINNING_PRE  = .TRUE.
LL_RAD1C_RAD1C_THINNING_THIN = .TRUE.
LL_RAD1C_RAD1C_THINNING_POST = .TRUE.

LL_TCWC_THINNING             = .TRUE.
LL_TCWC_THINNING_PRE         = .TRUE.  
LL_TCWC_THINNING_THIN        = .TRUE.
LL_TCWC_THINNING_POST        = .TRUE.

LL_SATOB_SATOB_THINNING      = .TRUE.
LL_SATOB_SATOB_THINNING_PRE  = .TRUE.
LL_SATOB_SATOB_THINNING_THIN = .TRUE.
LL_SATOB_SATOB_THINNING_POST = .FALSE.

LL_SATAM_SATOB_THINNING      = .TRUE.
LL_SATAM_SATOB_THINNING_PRE  = .TRUE.
LL_SATAM_SATOB_THINNING_THIN = .TRUE.
LL_SATAM_SATOB_THINNING_POST = .FALSE.

LL_SCATT_THINNING            = .TRUE.
LL_SCATT_THINNING_PRE        = .TRUE.
LL_SCATT_THINNING_THIN       = .TRUE.
LL_SCATT_THINNING_POST       = .FALSE.

LL_SATEM_THINNING            = .TRUE.
LL_SATEM_THINNING_PRE        = .TRUE.
LL_SATEM_THINNING_THIN       = .TRUE.
LL_SATEM_THINNING_POST       = .FALSE.

LL_REO3_THINNING             = .TRUE.
LL_REO3_THINNING_PRE         = .TRUE.  
LL_REO3_THINNING_THIN        = .TRUE.
LL_REO3_THINNING_POST        = .FALSE. 

LL_GPSRO_THINNING             = .TRUE.
LL_GPSRO_THINNING_PRE         = .TRUE.
LL_GPSRO_THINNING_THIN        = .TRUE.
LL_GPSRO_THINNING_POST        = .TRUE.

LL_RADAR_THINNING            = .TRUE.

!*
!     ------------------------------------------------------------------

!        2.       THINNING OF ACTIVE (R)TOVS DATA (NGTHR1=211, NGTHR2=212)
!                 --------------------------------------------------------

2 CONTINUE
IF(LL_TOVS_RTOVS_THINNING) THEN
  WRITE(NULOUT,'(''     THINNING (R)TOVS DATA '')')
  CALL FLUSH(NULOUT)
ELSE
  GO TO 3
ENDIF

!           2.0       LOOP OVER 2 THINNING BOXES:

TWO_TOVS_THINNING_BOXES: DO JBOX = 1 , 2
  IBOX  = JBOX
  IF(IBOX == 1) THEN
    ZBOX = RMIND_TOV
  ELSE
    ZBOX = RFIND_TOV
  ENDIF
  WRITE(NULOUT,'(''     ----------------------- START (R)TOVS THINNING BOX:'',I3,1X,F10.2)') IBOX,ZBOX
  CALL FLUSH(NULOUT)

!           2.1       PREPARE DATA LOCALLY

  IF(LL_TOVS_RTOVS_THINNING_PRE) THEN
    ILEN = 0

!           2.1.1     GET DATA

    CALL GETDB('PRE_THINN',1,ILEN,INFO,0,ZINFO,0,&
     & -1, KTSLOT, -1, NSATEM, -1, -1)  
    WRITE(NULOUT,'(''     PRE_THINN_1; (GETDB) (R)TOVS REPORTS FOUND: '',I10)') ILEN
    CALL FLUSH(NULOUT)
    IF(ILEN == 0) GO TO 220

!           2.1.2     PREPARE

    IF(ZBOX > 0.0_JPRB) THEN
      CALL PRE_THINNER(ILEN,IBOX,ZBOX     ,'TOVS',NSATEM,NGTHR1,-1,-1)
      CALL PRE_THINNER(ILEN,IBOX,ZBOX     ,'TOVS',NSATEM,NGTHR2,-1,-1)
    ENDIF

!           2.1.3     PUT THE DB AWAY

    CALL PUTDB('PRE_THINN',1,ILEN,INFO,0,ZINFO,0)
    WRITE(NULOUT,'(''     PRE_THINN_1; (PUTDB) (R)TOVS REPORTS: '',I10)') ILEN
    CALL FLUSH(NULOUT)

!           2.1.4     DONE

  ENDIF

!           2.2       GLOBAL THINNING

  220 CONTINUE
  IF(LL_TOVS_RTOVS_THINNING_THIN) THEN
    ILEN = 0

!           2.2.1     GET DATA

    CALL GETDB('NEW_THINN',1,ILEN,INFO,0,ZINFO,0,&
     & -1, KTSLOT, -1, NSATEM, -1, -1)  
    WRITE(NULOUT,'(''     NEW_THINN_1; (GETDB) (R)TOVS REPORTS FOUND: '',I10)') ILEN
    CALL FLUSH(NULOUT)
    IF(ILEN == 0) GO TO 225

!           2.2.2     THINN

    IF(ZBOX > 0.0_JPRB) THEN
      IF(LL_NEW_RAD1C_THINNING ) THEN
        CALL NEW_THINNER_NO_SQ(ILEN,IBOX,     'TOVS',NSATEM,NGTHR1,-1,-1)
        CALL NEW_THINNER_NO_SQ(ILEN,IBOX,     'TOVS',NSATEM,NGTHR2,-1,-1)
      ELSE
        CALL NEW_THINNER      (ILEN,IBOX,ZBOX,'TOVS',NSATEM,NGTHR1,-1,-1)
        CALL NEW_THINNER      (ILEN,IBOX,ZBOX,'TOVS',NSATEM,NGTHR2,-1,-1)
      ENDIF
    ENDIF

!           2.2.3     PUT THE DB AWAY

    CALL PUTDB('NEW_THINN',1,ILEN,INFO,0,ZINFO,0)
    WRITE(NULOUT,'(''     NEW_THINN_1; (PUTDB) (R)TOVS REPORTS: '',I10)') ILEN
    CALL FLUSH(NULOUT)

!           2.2.4     DONE


!           2.2.5     BYPASS

    225  CONTINUE
  ENDIF

!           2.3       POST PROCESS DATA LOCALLY

  230 CONTINUE
  IF(LL_TOVS_RTOVS_THINNING_POST) THEN
    ILEN = 0

!           2.3.1     GET DATA

    CALL GETDB('PRE_THINN',1,ILEN,INFO,0,ZINFO,0,&
     & -1, KTSLOT, -1, NSATEM, -1, -1)  
    WRITE(NULOUT,'(''     POST_THINN_1; (GETDB) (R)TOVS REPORTS FOUND: '',I10)') ILEN
    CALL FLUSH(NULOUT)
    IF(ILEN == 0) GO TO 240

!           2.3.2     POSTPROCESS

    CALL POST_THINNER(ILEN,'TOVS',NSATEM,NGTHR1,-1,-1)
    CALL POST_THINNER(ILEN,'TOVS',NSATEM,NGTHR2,-1,-1)

!           2.3.3     PUT THE DB AWAY

    CALL PUTDB('POST_THINN',1,ILEN,INFO,0,ZINFO,0)
    WRITE(NULOUT,'(''     POST_THINN_1; (PUTDB) (R)TOVS REPORTS: '',I10)') ILEN
    CALL FLUSH(NULOUT)

!           2.3.4     DONE

  ENDIF

!           2.4       CLOSE TWO BOX LOOP

  240 CONTINUE
  WRITE(NULOUT,'(''     ----------------------- END (R)TOVS THINNING BOX LOOP:'',I3,1X,F10.2)') IBOX,ZBOX
  CALL FLUSH(NULOUT)
ENDDO TWO_TOVS_THINNING_BOXES

!*
!     ------------------------------------------------------------------

!        3.       THINNING OF ACTIVE RAD1C (GEOS) DATA
!                 ------------------------------------

3 CONTINUE
IF(LL_GEOS_RAD1C_THINNING) THEN
  WRITE(NULOUT,'(''     THINNING OF RAD1C (GEOS) DATA '')')
  WRITE(NULOUT,'(''     TWO SENSORS '')')
  CALL FLUSH(NULOUT)
ELSE
  GO TO 4
ENDIF

!*          3.0       LOOP OVER TWO GEOS SENSORS

ISENSOR_LIST=-9
IL=5
! Thin all separately
ISENSOR_LIST(1,1:IL)=(/MSENSOR_METEOSAT,MSENSOR_GOESIMG,MSENSOR_MSG,MSENSOR_MTSATIMG,MSENSOR_VISSR/)
TWO_GEO_SENSORS: DO J=1,IL
  ISENSOR=ISENSOR_LIST(1,J)   ! Use first sensor in group as representative
  IG=COUNT(ISENSOR_LIST(:,J) /= -9)

  WRITE(NULOUT,*) '     ----------------------- START GEOS SENSOR LOOP:',ISENSOR_LIST(1:IG,J)

  CALL FLUSH(NULOUT)

!           3.0.1     LOOP OVER 2 GEOS THINNING BOXES:

  TWO_GEOS_THINNING_BOXES: DO JBOX = 1 , 2
    IBOX  = JBOX
    IF(IBOX == 1) THEN
      ZBOX = RMIND_RAD1C(ISENSOR)
    ELSE
      ZBOX = RFIND_RAD1C(ISENSOR)
    ENDIF
    WRITE(NULOUT,'(''     ----------------------- START GEOS THINNING BOX:'',I3,1X,F10.2)') IBOX,ZBOX
    CALL FLUSH(NULOUT)

!           3.1       PREPARE DATA LOCALLY

    IF(LL_GEOS_RAD1C_THINNING_PRE) THEN
      ILEN = 0

!           3.1.1     GET DATA

      CALL GETDB('PRE_THINN',2,ILEN,INFO,0,ZINFO,0,&
       & -1, KTSLOT, -1, NSATEM, NGTHRB, -1, &
       & KSENSOR_V=ISENSOR_LIST(1:IG,J))
      WRITE(NULOUT,'(''     PRE_THINN_2; (GETDB) GEOS REPORTS FOUND: '',I10)') ILEN
      CALL FLUSH(NULOUT)
      IF(ILEN == 0) GO TO 320

!           3.1.2     PREPARE

      IF(ZBOX > 0.0_JPRB) THEN
        CALL PRE_THINNER(ILEN,IBOX,ZBOX,'GEOS',NSATEM,NGTHRB,ISENSOR,-1)
      ENDIF

!           3.1.3     PUT THE DB AWAY

      CALL PUTDB('PRE_THINN',2,ILEN,INFO,0,ZINFO,0)
      WRITE(NULOUT,'(''     PRE_THINN_2; (PUTDB) GEOS REPORTS: '',I10)') ILEN
      CALL FLUSH(NULOUT)

!           3.1.4     DONE

    ENDIF

!           3.2       GLOBAL THINNING

    320 CONTINUE
    IF(LL_GEOS_RAD1C_THINNING_THIN) THEN
      ILEN = 0

!           3.2.1     GET DATA

      CALL GETDB('NEW_THINN',2,ILEN,INFO,0,ZINFO,0,&
       & -1, KTSLOT, -1, NSATEM, NGTHRB, -1, &
       & KSENSOR_V=ISENSOR_LIST(1:IG,J))  
      WRITE(NULOUT,'(''     NEW_THINN_2; (GETDB) GEOS REPORTS FOUND: '',I10)') ILEN
      CALL FLUSH(NULOUT)
      IF(ILEN == 0) GO TO 325

!           3.2.2     THINN

      IF(ZBOX > 0.0_JPRB) THEN
        IF(LL_NEW_RAD1C_THINNING ) THEN
          CALL NEW_THINNER_NO_SQ(ILEN,IBOX,     'GEOS',NSATEM,NGTHRB,ISENSOR,-1)
        ELSE
          CALL NEW_THINNER      (ILEN,IBOX,ZBOX,'GEOS',NSATEM,NGTHRB,ISENSOR,-1)
        ENDIF
      ENDIF

!           3.2.3     PUT THE DB AWAY

      CALL PUTDB('NEW_THINN',2,ILEN,INFO,0,ZINFO,0)
      WRITE(NULOUT,'(''     NEW_THINN_2; (PUTDB) GEOS REPORTS: '',I10)') ILEN
      CALL FLUSH(NULOUT)

!           3.2.4     DONE


!           3.2.5     BYPASS

      325  CONTINUE
    ENDIF 

!           4.3       POST PROCESS DATA LOCALLY

    330 CONTINUE
    IF(LL_GEOS_RAD1C_THINNING_POST) THEN
      ILEN = 0

!           3.3.1     GET DATA

      CALL GETDB('POST_THINN',2,ILEN,INFO,0,ZINFO,0,&
       & -1, KTSLOT, -1, NSATEM, NGTHRB, -1, &
         & KSENSOR_V=ISENSOR_LIST(1:IG,J))
      WRITE(NULOUT,'(''     POST_THINN_2; (GETDB) GEOS REPORTS FOUND: '',I10)') ILEN
      CALL FLUSH(NULOUT)
      IF(ILEN == 0) GO TO 340

!           3.3.2     POSTPROCESS

      CALL POST_THINNER(ILEN,'GEOS',NSATEM,NGTHRB,ISENSOR,-1)

!           3.3.3     PUT THE DB AWAY

      CALL PUTDB('POST_THINN',2,ILEN,INFO,0,ZINFO,0)
      WRITE(NULOUT,'(''     POST_THINN_2; (PUTDB) GEOS REPORTS: '',I10)') ILEN
      CALL FLUSH(NULOUT)

!           3.3.4     DONE

    ENDIF

!           3.4       CLOSE TWO BOX LOOP

    340 CONTINUE
    WRITE(NULOUT,'(''     ----------------------- END GEOS THINNING BOX LOOP:'',I3,1X,F10.2)') IBOX,ZBOX
    CALL FLUSH(NULOUT)
  ENDDO TWO_GEOS_THINNING_BOXES

!           3.5       CLOSE TWO SENSOR LOOP

  350 CONTINUE
  WRITE(NULOUT,'(''     ----------------------- END GEOS SENSOR LOOP:'',I10)') ISENSOR
  CALL FLUSH(NULOUT)
ENDDO TWO_GEO_SENSORS

!*
!     ------------------------------------------------------------------

!        4.       THINNING OF ACTIVE RAD1C (RAD1C) DATA
!                 -------------------------------------

4 CONTINUE
IF(LL_RAD1C_RAD1C_THINNING) THEN
  WRITE(NULOUT,'(''     THINNING OF RAD1C (RAD1C) DATA '')')
  WRITE(NULOUT,'(''     TEN SENSORS '')')
  CALL FLUSH(NULOUT)
ELSE
  GO TO 5
ENDIF

!*          4.0       LOOP OVER SENSORS

ISENSOR_LIST=-9
IL=12
! Thin these groups of instruments together
ISENSOR_LIST(1:6,1)=(/MSENSOR_SSMI,MSENSOR_SSMIS,MSENSOR_AMSRE,MSENSOR_TMI,MSENSOR_MWRI,MSENSOR_WINDSAT/)
ISENSOR_LIST(1:3,2)=(/MSENSOR_AMSUB, MSENSOR_MHS, MSENSOR_MWHS/)
ISENSOR_LIST(1:2,3)=(/MSENSOR_HIRS, MSENSOR_IRAS/)
! Thin the rest separately
ISENSOR_LIST(1,4:IL)=(/MSENSOR_MSU ,&
 & MSENSOR_SSU,MSENSOR_MWTS,MSENSOR_AMSUA,&
 & MSENSOR_VTPR1,MSENSOR_VTPR2,MSENSOR_AIRS,MSENSOR_MSG_HR,MSENSOR_IASI/)  

ALL_SENSORS: DO J=1,IL
  ISENSOR=ISENSOR_LIST(1,J)   ! Use first sensor in group as representative
  IG=COUNT(ISENSOR_LIST(:,J) /= -9)

  WRITE(NULOUT,*) '     ----------->>>>>>>>>>>> START RAD1C SENSOR LOOP:',ISENSOR_LIST(1:IG,J)
  CALL FLUSH(NULOUT)

!           4.1.0     LOOP OVER 2 RAD1C THINNING BOXES-

  TWO_RAD1C_THINNING_BOXES: DO JBOX = 1 , 2
    IBOX  = JBOX
    IF(IBOX == 1) THEN
      ZBOX = RMIND_RAD1C(ISENSOR)
    ELSE
      ZBOX = RFIND_RAD1C(ISENSOR)
    ENDIF
    WRITE(NULOUT,'(''     ----------------------- START RAD1C THINNING BOX:'',I3,1X,F10.2)') IBOX,ZBOX
    CALL FLUSH(NULOUT)

!           4.1       PREPARE DATA LOCALLY

    IF(LL_RAD1C_RAD1C_THINNING_PRE) THEN
      ILEN = 0

!           4.1.1     GET DATA

      CALL GETDB('PRE_THINN',2,ILEN,INFO,0,ZINFO,0,&
       & -1, KTSLOT, -1, NSATEM, NGTHRB, -1, &
       & KSENSOR_V=ISENSOR_LIST(1:IG,J))  
      WRITE(NULOUT,'(''     PRE_THINN_2; (GETDB) RAD1C REPORTS FOUND: '',I10)') ILEN
      CALL FLUSH(NULOUT)
      IF(ILEN == 0) GO TO 420

!           4.1.2     PREPARE

      IF(ZBOX > 0.0_JPRB) THEN
        CALL PRE_THINNER(ILEN,IBOX,ZBOX,'RAD1C',NSATEM,NGTHRB,ISENSOR,-1)
      ENDIF

!           4.1.3     PUT THE DB AWAY

      CALL PUTDB('PRE_THINN',2,ILEN,INFO,0,ZINFO,0)
      WRITE(NULOUT,'(''     PRE_THINN_2; (PUTDB) RAD1C REPORTS: '',I10)') ILEN
      CALL FLUSH(NULOUT)

!           4.1.4     DONE

    ENDIF

!           4.2       GLOBAL THINNING

    420 CONTINUE
    IF(LL_RAD1C_RAD1C_THINNING_THIN) THEN
      ILEN = 0

!           4.2.1     GET DATA

      CALL GETDB('NEW_THINN',2,ILEN,INFO,0,ZINFO,0,&
       & -1, KTSLOT, -1, NSATEM, NGTHRB, -1, &
       & KSENSOR_V=ISENSOR_LIST(1:IG,J))  
      WRITE(NULOUT,'(''     NEW_THINN_2; (GETDB) RADIC REPORTS FOUND: '',I10)') ILEN
      CALL FLUSH(NULOUT)
      IF(ILEN == 0) GO TO 425

!           4.2.2     THINN

      IF(ZBOX > 0.0_JPRB) THEN
        IF(LL_NEW_RAD1C_THINNING ) THEN
          CALL NEW_THINNER_NO_SQ(ILEN,IBOX,     'RAD1C',NSATEM,NGTHRB,ISENSOR,-1)
        ELSE
          CALL NEW_THINNER      (ILEN,IBOX,ZBOX,'RAD1C',NSATEM,NGTHRB,ISENSOR,-1)
        ENDIF
      ENDIF

!           4.2.3     PUT THE DB AWAY

      CALL PUTDB('NEW_THINN',2,ILEN,INFO,0,ZINFO,0)
      WRITE(NULOUT,'(''     NEW_THINN_2; (PUTDB) RAD1C REPORTS: '',I10)') ILEN
      CALL FLUSH(NULOUT)

!           4.2.4     DONE


!           4.2.5     BYPASS

      425  CONTINUE
    ENDIF

!           4.3       POST PROCESS DATA LOCALLY

    430 CONTINUE
    IF(LL_RAD1C_RAD1C_THINNING_POST) THEN
      IF(ISENSOR == MSENSOR_HIRS .OR. ISENSOR == MSENSOR_IRAS)      THEN
        ILEN = 0

!           4.3.1     GET DATA

        CALL GETDB('POST_THINN',2,ILEN,INFO,0,ZINFO,0,&
         & -1, KTSLOT, -1, NSATEM, NGTHRB, -1, &
         & KSENSOR_V=ISENSOR_LIST(1:IG,J))  
        WRITE(NULOUT,'(''     POST_THINN_2; (GETDB) RADIC REPORTS FOUND: '',I10)') ILEN
        CALL FLUSH(NULOUT)
        IF(ILEN == 0) GO TO 440

!           4.3.2     POSTPROCESS

        CALL POST_THINNER(ILEN,'RAD1C',NSATEM,NGTHRB,ISENSOR,-1)

!           4.3.3     PUT THE DB AWAY

        CALL PUTDB('POST_THINN',2,ILEN,INFO,0,ZINFO,0)
        WRITE(NULOUT,'(''     POST_THINN_2; (PUTDB) RADIC REPORTS: '',I10)') ILEN
        CALL FLUSH(NULOUT)

!           4.3.4     DONE

      ENDIF
    ENDIF

!           4.4       CLOSE RAD1C TWO BOX LOOP

    440 CONTINUE
    WRITE(NULOUT,'(''     ----------------------- END RAD1C THINNING BOX LOOP:'',I3,1X,F10.2)') IBOX,ZBOX
    CALL FLUSH(NULOUT)
  ENDDO TWO_RAD1C_THINNING_BOXES

!           4.5       CLOSE RAD1C  all SENSOR LOOP

  450 CONTINUE
  WRITE(NULOUT,'(''     ----------------------- END RAD1C SENSOR LOOP:'',I10)') ISENSOR_LIST(1:IG,J) 
  CALL FLUSH(NULOUT)
ENDDO  ALL_SENSORS

!*
!     ------------------------------------------------------------------

!        5.       THINNING OF ACTIVE TCWC DATA
!                 ----------------------------

5 CONTINUE
IF(LL_TCWC_THINNING) THEN
  WRITE(NULOUT,'(''     THINN_3; THINNING OF TCWC (MERIS,...) DATA '')')
  CALL FLUSH(NULOUT)
ELSE
  GO TO 6
ENDIF

!           5.0       LOOP OVER 3 THINNING BOXES:

THREE_TCWC_THINNING_BOXES: DO JBOX = 1 , 1
  IVNM_RQ = -1
  ! Do "TCWC" thinning for MERIS.
  ISENSOR_RQ = NSENSOR_MERIS 
  IBOX    = JBOX
  IF(     IBOX == 1) THEN
    ZBOX = RMIND_SSMI  !*AF to be cleaned...
  ENDIF
  WRITE(NULOUT,'(''     ----------------------- START TCWC THINNING BOX:'',I3,1X,F10.2)') IBOX,ZBOX
  WRITE(NULOUT,'(''     ----------------------- FOR VARIABLE NO.       :'',I3)') IVNM_RQ
  CALL FLUSH(NULOUT)

!           5.1       PREPARE DATA LOCALLY

  IF(LL_TCWC_THINNING_PRE) THEN
    ILEN = 0

!           5.1.1     GET DATA

    CALL GETDB('PRE_THINN',3,ILEN,INFO,0,ZINFO,0,&
     & -1, KTSLOT, -1, NSATEM, NTCWC, -1)  
    WRITE(NULOUT,'(''     TCWC REPORTS FOUND: '',I10)') ILEN
    IF(ILEN == 0) GO TO 520

!           5.1.2     PREPARE

    IF(ZBOX > 0.0_JPRB) THEN
      CALL PRE_THINNER(ILEN,IBOX,ZBOX,'TCWC',NSATEM,NTCWC,ISENSOR_RQ,IVNM_RQ)
    ENDIF

!           5.1.3     PUT THE DB AWAY

    CALL PUTDB('PRE_THINN',3,ILEN,INFO,0,ZINFO,0)
    WRITE(NULOUT,'(''     PRE_THINN_3; (PUTDB) TCWC (MERIS,...) REPORTS: '',I10)') ILEN
    CALL FLUSH(NULOUT)

!           5.1.4     DONE

  ENDIF

!           5.2       GLOBAL THINNING

  520 CONTINUE
  IF(LL_TCWC_THINNING_THIN) THEN
    ILEN = 0

!           5.2.1     GET DATA

    CALL GETDB('NEW_THINN',3,ILEN,INFO,0,ZINFO,0,&
     & -1, KTSLOT, -1, NSATEM, NTCWC, -1)  
    WRITE(NULOUT,'(''     NEW_THINN_3; (GETDB) TCWC (MERIS,...) REPORTS FOUND: '',I10)') ILEN
    CALL FLUSH(NULOUT)
    IF(ILEN == 0) GO TO 525

!           5.2.2     THINN

    IF(ZBOX > 0.0_JPRB) THEN
      IF(LL_NEW_TCWC_THINNING ) THEN
        CALL NEW_THINNER_NO_SQ(ILEN,IBOX,     'TCWC',NSATEM,NTCWC,ISENSOR_RQ,IVNM_RQ)
      ELSE
        CALL NEW_THINNER      (ILEN,IBOX,ZBOX,'TCWC',NSATEM,NTCWC,ISENSOR_RQ,IVNM_RQ)
      ENDIF
    ENDIF

!           5.2.3     PUT THE DB AWAY

    CALL PUTDB('NEW_THINN',3,ILEN,INFO,0,ZINFO,0)
    WRITE(NULOUT,'(''     NEW_THINN_3; (PUTDB) TCWC (MERIS,...) REPORTS: '',I10)') ILEN
    CALL FLUSH(NULOUT)

!           5.2.4     DONE


!           5.2.5     BYPASS

    525  CONTINUE
  ENDIF

!           5.3       POST PROCESS DATA LOCALLY

  530 CONTINUE
  IF(LL_TCWC_THINNING_POST) THEN
    IF(IVNM_RQ /= -1)         THEN
      ILEN = 0

!           5.3.1     GET DATA

      CALL GETDB('POST_THINN',3,ILEN,INFO,0,ZINFO,0,&
       & -1, KTSLOT, -1, NSATEM, NTCWC, -1)  
      WRITE(NULOUT,'(''     TCWC REPORTS FOUND: '',I10)') ILEN
      IF(ILEN == 0) GO TO 540

!           5.3.2     POSTPROCESS

      CALL POST_THINNER(ILEN,'TCWC',NSATEM,NTCWC,ISENSOR_RQ,IVNM_RQ)

!           5.3.3     PUT THE DB AWAY

      CALL PUTDB('POST_THINN',3,ILEN,INFO,0,ZINFO,0)
      WRITE(NULOUT,'(''     POST_THINN_3; (PUTDB) TCWC (MERIS,...) REPORTS: '',I10)') ILEN
      CALL FLUSH(NULOUT)

!           5.3.4     DONE

    ENDIF
  ENDIF

!           5.4       CLOSE THREE BOX LOOP

  540 CONTINUE
  WRITE(NULOUT,'(''     ----------------------- END TCWC THINNING BOX LOOP:'',I3,1X,F10.2)') IBOX,ZBOX
  CALL FLUSH(NULOUT)
ENDDO THREE_TCWC_THINNING_BOXES

!*
!     ------------------------------------------------------------------

!        6.       THINNING OF ACTIVE SATOB DATA
!                 -----------------------------

6 CONTINUE
IF(LL_SATOB_SATOB_THINNING) THEN
  WRITE(NULOUT,'(''     THINN_4; THINNING OF SATOB (NSTBCD, NSTB86) DATA'')')
  CALL FLUSH(NULOUT)
ELSE
  GO TO 7
ENDIF

!           6.0       LOOP OVER 2 THINNING BOXES:

TWO_SATOB_THINNING_BOXES: DO JBOX = 1 , 2
  IBOX  = JBOX
  IF(IBOX == 1) THEN
    ZBOX = RMIND_SATOB
  ELSE
    ZBOX = RFIND_SATOB
  ENDIF
  WRITE(NULOUT,'(''     ----------------------- START SATOB THINNING BOX:'',I3,1X,F10.2)') IBOX,ZBOX
  CALL FLUSH(NULOUT)

!           6.1       PREPARE DATA LOCALLY

  IF(LL_SATOB_SATOB_THINNING_PRE) THEN
    ILEN = 0

!           6.1.1     GET DATA

    CALL GETDB('PRE_THINN',4,ILEN,INFO,0,ZINFO,0,&
     & -1, KTSLOT, -1, NSATOB, -1, -1)  
    WRITE(NULOUT,'(''     PRE_THINN_4; (GETDB) SATOB REPORTS FOUND: '',I10)') ILEN
    IF(ILEN == 0) GO TO 620

!           6.1.2     PREPARE

    IF(ZBOX > 0.0_JPRB) THEN
      CALL PRE_THINNER(ILEN,IBOX,ZBOX,'SATOB',NSATOB,-1,-1,-1)
    ENDIF

!           6.1.3     PUT THE DB AWAY

    CALL PUTDB('PRE_THINN',4,ILEN,INFO,0,ZINFO,0)
    WRITE(NULOUT,'(''     PRE_THINN_4; (PUTDB) SATOB REPORTS: '',I10)') ILEN
    CALL FLUSH(NULOUT)

!           6.1.4     DONE

  ENDIF

!           6.2       GLOBAL THINNING

  620 CONTINUE
  IF(LL_SATOB_SATOB_THINNING_THIN) THEN
    ILEN = 0

!           6.2.1     GET DATA

    CALL GETDB('NEW_THINN',4,ILEN,INFO,0,ZINFO,0,&
     & -1, KTSLOT, -1, NSATOB, -1, -1)  
    WRITE(NULOUT,'(''     NEW_THINN_4; (GETDB) SATOB REPORTS FOUND: '',I10)') ILEN
    CALL FLUSH(NULOUT)
    IF(ILEN == 0) GO TO 625

!           6.2.2     THINN

    IF(ZBOX > 0.0_JPRB) THEN
      IF(LL_NEW_SATOB_THINNING) THEN
        CALL NEW_THINNER_NO_SQ(ILEN,IBOX,     'SATOB',NSATOB,-1,-1,-1)
      ELSE
        CALL NEW_THINNER      (ILEN,IBOX,ZBOX,'SATOB',NSATOB,-1,-1,-1)
      ENDIF
    ENDIF

!           6.2.3     PUT THE DB AWAY

    CALL PUTDB('NEW_THINN',4,ILEN,INFO,0,ZINFO,0)
    WRITE(NULOUT,'(''     NEW_THINN_4; (PUTDB) SATOB REPORTS: '',I10)') ILEN
    CALL FLUSH(NULOUT)

!           6.2.4     DONE


!           6.2.5     BYPASS

    625  CONTINUE
  ENDIF

!           6.3       POST PROCESS DATA LOCALLY

  630 CONTINUE
  IF(LL_SATOB_SATOB_THINNING_POST) THEN
    ILEN = 0

!           6.3.1     GET DATA

    CALL GETDB('POST_THINN',4,ILEN,INFO,0,ZINFO,0,&
     & -1, KTSLOT, -1, NSATOB, -1, -1)  
    WRITE(NULOUT,'(''     POST_THINN_4; (GETDB) SATOB REPORTS FOUND: '',I10)') ILEN
    CALL FLUSH(NULOUT)

!           6.3.2     POSTPROCESS

    CALL POST_THINNER(ILEN,'SATOB',NSATOB,-1,-1,-1)

!           6.3.3     PUT THE DB AWAY

    CALL PUTDB('POST_THINN',4,ILEN,INFO,0,ZINFO,0)
    WRITE(NULOUT,'(''     POST_THINN_4; (PUTDB) SATOB REPORTS: '',I10)') ILEN
    CALL FLUSH(NULOUT)

!           6.3.4     DONE

  ENDIF

!           6.4       CLOSE TWO BOX LOOP

  640 CONTINUE
  WRITE(NULOUT,'(''     ----------------------- END SATOB THINNING BOX LOOP:'',I3,1X,F10.2)') IBOX,ZBOX
  CALL FLUSH(NULOUT)
ENDDO TWO_SATOB_THINNING_BOXES

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

!        7.       THINNING OF ACTIVE SATOB DATA
!                 -----------------------------

7 CONTINUE
IF(LL_SATAM_SATOB_THINNING) THEN
  WRITE(NULOUT,'(''     THINN_5; THINNING OF SATOB/SATAM (NSTB87) DATA'')')
  CALL FLUSH(NULOUT)
ELSE
  GO TO 8
ENDIF

!           7.0       LOOP OVER 2 THINNING BOXES:

TWO_SATAM_THINNING_BOXES: DO JBOX = 1 , 2
  IBOX  = JBOX
  IF(IBOX == 1) THEN
    ZBOX = RMIND_SATAM
  ELSE
    ZBOX = RFIND_SATAM
  ENDIF
  WRITE(NULOUT,'(''     ----------------------- START SATOB (SATAM) THINNING BOX:'',I3,1X,F10.2)') IBOX,ZBOX
  CALL FLUSH(NULOUT)

!           7.1       PREPARE DATA LOCALLY

  IF(LL_SATAM_SATOB_THINNING_PRE) THEN
    ILEN = 0

!           7.1.1     GET DATA

    CALL GETDB('PRE_THINN',5,ILEN,INFO,0,ZINFO,0,&
     & -1, KTSLOT, -1, NSATOB, -1, -1)  
    WRITE(NULOUT,'(''     PRE_THINN_5; (GETDB) SATOB (SATAM) REPORTS FOUND: '',I10)') ILEN
    CALL FLUSH(NULOUT)
    IF(ILEN == 0) GO TO 720

!           7.1.2     PREPARE

    IF(ZBOX > 0.0_JPRB) THEN
      CALL PRE_THINNER(ILEN,IBOX,ZBOX,'SATAM',NSATOB,NSTB87,-1,-1)
    ENDIF

!           7.1.3     PUT THE DB AWAY

    CALL PUTDB('PRE_THINN',5,ILEN,INFO,0,ZINFO,0)
    WRITE(NULOUT,'(''     PRE_THINN_5; (PUTDB) SATOB (SATAM) REPORTS: '',I10)') ILEN
    CALL FLUSH(NULOUT)

!           7.1.4     DONE

  ENDIF

!           7.2       GLOBAL THINNING

  720 CONTINUE
  IF(LL_SATAM_SATOB_THINNING_THIN) THEN
    ILEN = 0

!           7.2.1     GET DATA

    CALL GETDB('NEW_THINN',5,ILEN,INFO,0,ZINFO,0,&
     & -1, KTSLOT, -1, NSATOB, -1, -1)  
    WRITE(NULOUT,'(''     NEW_THINN_5; (GETDB) SATOB (SATAM) REPORTS FOUND: '',I10)') ILEN
    CALL FLUSH(NULOUT)
    IF(ILEN == 0) GO TO 725

!           7.2.2     THINN

    IF(ZBOX > 0.0_JPRB) THEN
      IF(LL_NEW_SATOB_THINNING) THEN
        CALL NEW_THINNER_NO_SQ(ILEN,IBOX,     'SATAM',NSATOB,NSTB87,-1,-1)
      ELSE
        CALL NEW_THINNER      (ILEN,IBOX,ZBOX,'SATAM',NSATOB,NSTB87,-1,-1)
      ENDIF
    ENDIF

!           7.2.3     PUT THE DB AWAY

    CALL PUTDB('NEW_THINN',5,ILEN,INFO,0,ZINFO,0)
    WRITE(NULOUT,'(''     NEW_THINN_5; (PUTDB) SATOB (SATAM) REPORTS: '',I10)') ILEN
    CALL FLUSH(NULOUT)

!           7.2.4     DONE


!           7.2.5     BYPASS

    725  CONTINUE
  ENDIF

!           7.3       POST PROCESS DATA LOCALLY

  730 CONTINUE
  IF(LL_SATAM_SATOB_THINNING_POST) THEN
    ILEN = 0

!           7.3.1     GET DATA

    CALL GETDB('POST_THINN',5,ILEN,INFO,0,ZINFO,0,&
     & -1, KTSLOT, -1, NSATOB, -1, -1)  
    WRITE(NULOUT,'(''     POST_THINN_5; (GETDB) SATOB (SATAM) REPORTS FOUND: '',I10)') ILEN
    CALL FLUSH(NULOUT)

!           7.3.2     POSTPROCESS

    CALL POST_THINNER(ILEN,'SATOB',NSATOB,-1,-1,-1)

!           7.3.3     PUT THE DB AWAY

    CALL PUTDB('POST_THINN',4,ILEN,INFO,0,ZINFO,0)
    WRITE(NULOUT,'(''     POST_THINN_5; (PUTDB) SATOB (SATAM) REPORTS: '',I10)') ILEN
    CALL FLUSH(NULOUT)

!           7.3.4     DONE

  ENDIF

!           7.4       CLOSE TWO BOX LOOP

  740 CONTINUE
  WRITE(NULOUT,'(''     ----------------------- END SATOB (SATAM) THINNING BOX LOOP:'',I3,1X,F10.2)') IBOX,ZBOX
  CALL FLUSH(NULOUT)
ENDDO TWO_SATAM_THINNING_BOXES

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

!        8.       THINNING OF ACTIVE SCAT DATA.
!                 -----------------------------

8 CONTINUE
IF(LL_SCATT_THINNING) THEN
  WRITE(NULOUT,'(''     THINN_6; THINNING OF SCAT DATA'')')
  CALL FLUSH(NULOUT)
ELSE
  GO TO 9
ENDIF

! Thin ERS2 and ASCAT separately
IL=2
IF(IL > IMAXCODETYPE) THEN
    WRITE(NULERR,'('' IMAXCODETYPE EXCEEDED '')')
    CALL FLUSH(NULOUT)
    CALL ABOR1(' NEW_THINN : IMAXCODETYPE EXCEEDED ')
  ENDIF

ICODETYPE_LIST(1:IL)=(/NSCAT2,NSCAT3/)
  TWO_SCATT_CODETYPES: DO J=1,IL
  ICODETYPE=ICODETYPE_LIST(J)

!           8.0       LOOP OVER 2 THINNING BOXES:

  TWO_SCATT_THINNING_BOXES: DO JBOX = 1 , 2
    IBOX  = JBOX
    IF(IBOX == 1) THEN
      ZBOX = RMIND_SCATT_ERS
    ELSE
      ZBOX = RFIND_SCATT_ERS
    ENDIF
    WRITE(NULOUT,'(''     ----------------------- START SCATT THINNING BOX:'',I3,1X,F10.2)') IBOX,ZBOX
    CALL FLUSH(NULOUT)

!           8.1       PREPARE DATA LOCALLY

    IF(LL_SCATT_THINNING_PRE) THEN
      ILEN = 0

!           8.1.1     GET DATA

      CALL GETDB('PRE_THINN',6,ILEN,INFO,0,ZINFO,0,&
       & -1, KTSLOT, -1, NSCATT, ICODETYPE, -1)  
      WRITE(NULOUT,'(''     PRE_THINN_6; (GETDB) SCATT REPORTS FOUND: '',I10)') ILEN
      IF(ILEN == 0) GO TO 820

!           8.1.2     PREPARE

      IF(ZBOX > 0.0_JPRB) THEN
        CALL PRE_THINNER(ILEN,IBOX,ZBOX,'SCATT',NSCATT,ICODETYPE,-1,-1)
      ENDIF

!           8.1.3     PUT THE DB AWAY

      CALL PUTDB('PRE_THINN',6,ILEN,INFO,0,ZINFO,0)
      WRITE(NULOUT,'(''     PRE_THINN_6; (PUTDB) SCATT REPORTS: '',I10)') ILEN
      CALL FLUSH(NULOUT)

!           8.1.4     DONE

    ENDIF

!           8.2       GLOBAL THINNING

  820   CONTINUE
    IF(LL_SCATT_THINNING_THIN) THEN
      ILEN = 0

!           8.2.1     GET DATA

      CALL GETDB('NEW_THINN',6,ILEN,INFO,0,ZINFO,0,&
       & -1, KTSLOT, -1, NSCATT, ICODETYPE, -1)  
      WRITE(NULOUT,'(''     NEW_THINN_6; (GETDB) SCATT REPORTS FOUND: '',I10)') ILEN
      CALL FLUSH(NULOUT)
      IF(ILEN == 0) GO TO 825

!           8.2.2     THINN

      IF(ZBOX > 0.0_JPRB) THEN
        IF(LL_NEW_SCATT_THINNING) THEN
          CALL NEW_THINNER_NO_SQ(ILEN,IBOX,     'SCATT',NSCATT,ICODETYPE,-1,-1)
        ELSE
          CALL NEW_THINNER      (ILEN,IBOX,ZBOX,'SCATT',NSCATT,ICODETYPE,-1,-1)
        ENDIF
      ENDIF

!           8.2.3     PUT THE DB AWAY

      CALL PUTDB('NEW_THINN',6,ILEN,INFO,0,ZINFO,0)
      WRITE(NULOUT,'(''     NEW_THINN_6; (PUTDB) SCATT REPORTS: '',I10)') ILEN
      CALL FLUSH(NULOUT)

!           8.2.4     DONE


!           8.2.5     BYPASS

    825    CONTINUE
    ENDIF

!           8.3       POST PROCESS DATA LOCALLY

  830   CONTINUE
    IF(LL_SCATT_THINNING_POST) THEN
      ILEN = 0

!           6.3.1     GET DATA

      CALL GETDB('POST_THINN',6,ILEN,INFO,0,ZINFO,0,&
       & -1, KTSLOT, -1, NSCATT, ICODETYPE, -1)  
      WRITE(NULOUT,'(''     POST_THINN_6; (GETDB) SCATT REPORTS FOUND: '',I10)') ILEN
      CALL FLUSH(NULOUT)

!           8.3.2     POSTPROCESS

      CALL POST_THINNER(ILEN,'SCATT',NSCATT,ICODETYPE,-1,-1)

!           8.3.3     PUT THE DB AWAY

      CALL PUTDB('POST_THINN',6,ILEN,INFO,0,ZINFO,0)
      WRITE(NULOUT,'(''     POST_THINN_6; (PUTDB) SCATT REPORTS: '',I10)') ILEN
      CALL FLUSH(NULOUT)

!           8.3.4     DONE

    ENDIF

!           8.4       CLOSE TWO BOX LOOP

    840 CONTINUE
    WRITE(NULOUT,'(''     ----------------------- END SCATT THINNING BOX LOOP:'',I3,1X,F10.2)') IBOX,ZBOX
    CALL FLUSH(NULOUT)
  ENDDO TWO_SCATT_THINNING_BOXES
ENDDO TWO_SCATT_CODETYPES

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

!        9.       THINNING OF ACTIVE SATEM/STMCD DATA
!                 -------------------------------------

9 CONTINUE
IF(.NOT.LMFSCREEN) GO TO 10
IF(LL_SATEM_THINNING) THEN
  WRITE(NULOUT,'(''     THINN_7; THINNING SATEM/STMCD DATA '')')
  CALL FLUSH(NULOUT)
ELSE
  GO TO 10
ENDIF

!           9.0       LOOP OVER 2 THINNING BOXES:

TWO_SATEM_THINNING_BOXES: DO JBOX = 1 , 2
  IBOX  = JBOX
  IF(IBOX == 1) THEN
    ZBOX = RMIND_TOV
  ELSE
    ZBOX = RFIND_TOV
  ENDIF
  WRITE(NULOUT,'(''     ----------------------- START SATEM THINNING BOX:'',I3,1X,F10.2)') IBOX,ZBOX
  CALL FLUSH(NULOUT)

!           9.1       PREPARE DATA LOCALLY

  IF(LL_SATEM_THINNING_PRE) THEN
    ILEN = 0

!           9.1.1     GET DATA

    CALL GETDB('PRE_THINN',7,ILEN,INFO,0,ZINFO,0,&
     & -1, KTSLOT, -1, -1, -1, -1)  
    WRITE(NULOUT,'(''     PRE_THINN_7; (GETDB) SATEM REPORTS FOUND: '',I10)') ILEN
    IF(ILEN == 0) GO TO 920

!           9.1.2     PREPARE

    IF(ZBOX > 0.0_JPRB) THEN
      CALL PRE_THINNER(ILEN,IBOX,ZBOX,'STMCD',NSATEM,NSTMCD,-1,-1)
    ENDIF

!           9.1.3     PUT THE DB AWAY

    CALL PUTDB('PRE_THINN',7,ILEN,INFO,0,ZINFO,0)
    WRITE(NULOUT,'(''     PRE_THINN_7; (PUTDB) SATEM REPORTS: '',I10)') ILEN
    CALL FLUSH(NULOUT)

!           9.1.4     DONE

  ENDIF

!           9.2       GLOBAL THINNING

  920 CONTINUE
  IF(LL_SATEM_THINNING_THIN) THEN
    ILEN = 0

!           9.2.1     GET DATA

    CALL GETDB('NEW_THINN',7,ILEN,INFO,0,ZINFO,0,&
     & -1, KTSLOT, -1, -1, -1, -1)  
    WRITE(NULOUT,'(''     NEW_THINN_7; (GETDB) SATEM REPORTS FOUND: '',I10)') ILEN
    CALL FLUSH(NULOUT)
    IF(ILEN == 0) GO TO 925

!           9.2.2     THINN

    IF(ZBOX > 0.0_JPRB) THEN
      IF(LL_NEW_SATEM_THINNING) THEN
        CALL NEW_THINNER_NO_SQ(ILEN,IBOX,     'STMCD',NSATEM,NSTMCD,-1,-1)
      ELSE
        CALL NEW_THINNER      (ILEN,IBOX,ZBOX,'STMCD',NSATEM,NSTMCD,-1,-1)
      ENDIF
    ENDIF

!           9.2.3     PUT THE DB AWAY

    CALL PUTDB('NEW_THINN',7,ILEN,INFO,0,ZINFO,0)
    WRITE(NULOUT,'(''     NEW_THINN_7; (PUTDB) SATEM REPORTS: '',I10)') ILEN
    CALL FLUSH(NULOUT)

!           9.2.4     DONE


!           9.2.5     BYPASS

    925  CONTINUE
  ENDIF

!           9.3       POST-PROCESS DATA LOCALLY

  930 CONTINUE
  IF(LL_SATEM_THINNING_POST) THEN
    ILEN = 0

!           9.3.1     GET DATA

    CALL GETDB('POST_THINN',7,ILEN,INFO,0,ZINFO,0,&
     & -1, KTSLOT, -1, -1, -1, -1)  
    WRITE(NULOUT,'(''     POST_THINN_7; (GETDB) SATEM REPORTS FOUND: '',I10)') ILEN
    CALL FLUSH(NULOUT)

!           9.3.2     POSTPROCESS

    CALL POST_THINNER(ILEN,'STMCD',NSATEM,NSTMCD,-1,-1)

!           9.3.3     PUT THE DB AWAY

    CALL PUTDB('POST_THINN',7,ILEN,INFO,0,ZINFO,0)
    WRITE(NULOUT,'(''     POST_THINN_7; (PUTDB) SATEM REPORTS: '',I10)') ILEN
    CALL FLUSH(NULOUT)

!           9.3.4     DONE

  ENDIF

!           9.4       CLOSE TWO BOX LOOP

  940 CONTINUE
  WRITE(NULOUT,'(''     ----------------------- END SATEM THINNING BOX LOOP:'',I3,1X,F10.2)') IBOX,ZBOX
  CALL FLUSH(NULOUT)
ENDDO TWO_SATEM_THINNING_BOXES

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

!        10.       THINNING OF ACTIVE REO3 DATA
!                 ------------------------------------

10 CONTINUE
IF(LL_REO3_THINNING) THEN
  IF(n_reo3_thin_info <= 0) GO TO 11
  WRITE(NULOUT,'(''     THINN_8; THINNING OF REO3 DATA '')')
  WRITE(NULOUT,'(3X,I2,'' SENSORS '')') n_reo3_thin_info
  CALL FLUSH(NULOUT)
ELSE
  GO TO 11
ENDIF

!*          10.0       LOOP OVER REO3 SENSORS

IL=n_reo3_thin_info
REO3_SENSORS: DO J=1,IL
  ISENSOR=o3thin_info(j)%sensor
  IVARNO=o3thin_info(j)%varno
  WRITE(NULOUT,'(''     ----------------------- START REO3 SENSOR LOOP:'',I10,x,I3)') &
   & ISENSOR,IVARNO
  CALL FLUSH(NULOUT)

!           10.0.1     LOOP OVER 2 REO3 THINNING BOXES:

  TWO_REO3_THINNING_BOXES: DO JBOX = 1 , 2
    IBOX  = JBOX
    IF(IBOX == 1) THEN
      ZBOX = o3thin_info(j)%rmind
    ELSE
      ZBOX = o3thin_info(j)%rfind
    ENDIF
    WRITE(NULOUT,'(''     ----------------------- START REO3 THINNING BOX:'',I3,1X,F10.2)') IBOX,ZBOX
    CALL FLUSH(NULOUT)

!           10.1       PREPARE DATA LOCALLY

    IF(LL_REO3_THINNING_PRE) THEN
      ILEN = 0

!           10.1.1     GET DATA

      CALL GETDB('PRE_THINN',8,ILEN,INFO,0,ZINFO,0,&
       & -1, KTSLOT, -1, NSATEM, NSTRO3, ISENSOR)
      WRITE(NULOUT,'(''     PRE_THINN_8; (GETDB) REO3 REPORTS FOUND: '',I10)') ILEN
      CALL FLUSH(NULOUT)
      IF(ILEN == 0) GO TO 1020

!           10.1.2     PREPARE

      IF(ZBOX > 0.0_JPRB) THEN
        CALL PRE_THINNER(ILEN,IBOX,ZBOX,'REO3',NSATEM,NSTRO3,ISENSOR,IVARNO)
      ENDIF

!           10.1.3     PUT THE DB AWAY

      CALL PUTDB('PRE_THINN',8,ILEN,INFO,0,ZINFO,0)
      WRITE(NULOUT,'(''     PRE_THINN_8; (PUTDB) REO3 REPORTS: '',I10)') ILEN
      CALL FLUSH(NULOUT)

!           10.1.4     DONE

    ENDIF

!           10.2       GLOBAL THINNING

    1020 CONTINUE
    IF(LL_REO3_THINNING_THIN) THEN
      ILEN = 0

!           10.2.1     GET DATA

      CALL GETDB('NEW_THINN',8,ILEN,INFO,0,ZINFO,0,&
       & -1, KTSLOT, -1, NSATEM, NSTRO3, ISENSOR)
      WRITE(NULOUT,'(''     NEW_THINN_8; (GETDB) REO3 REPORTS FOUND: '',I10)') ILEN
      CALL FLUSH(NULOUT)
      IF(ILEN == 0) GO TO 1025

!           10.2.2     THINN

      IF(ZBOX > 0.0_JPRB) THEN
        IF(LL_NEW_REO3_THINNING ) THEN
          CALL NEW_THINNER_NO_SQ(ILEN,IBOX,     'REO3',NSATEM,NSTRO3,ISENSOR,IVARNO)
        ELSE
          CALL NEW_THINNER      (ILEN,IBOX,ZBOX,'REO3',NSATEM,NSTRO3,ISENSOR,IVARNO)
        ENDIF
      ENDIF

!           10.2.3     PUT THE DB AWAY

      CALL PUTDB('NEW_THINN',8,ILEN,INFO,0,ZINFO,0)
      WRITE(NULOUT,'(''     NEW_THINN_8; (PUTDB) REO3 REPORTS: '',I10)') ILEN
      CALL FLUSH(NULOUT)

!           10.2.4     DONE


!           10.2.5     BYPASS

      1025  CONTINUE
    ENDIF
!           10.3       POST PROCESS DATA LOCALLY

    1030 CONTINUE
    IF(LL_REO3_THINNING_POST) THEN
      ILEN = 0

!           10.3.1     GET DATA

      CALL GETDB('POST_THINN',8,ILEN,INFO,0,ZINFO,0,&
       & -1, KTSLOT, -1, NSATEM, NSTRO3, ISENSOR)
      WRITE(NULOUT,'(''     POST_THINN_2; (GETDB) REO3 REPORTS FOUND: '',I10)') ILEN
      CALL FLUSH(NULOUT)
      IF(ILEN == 0) GO TO 1040

!           10.3.2     POSTPROCESS

      CALL POST_THINNER(ILEN,'REO3',NSATEM,NSTRO3,ISENSOR,IVARNO)

!           10.3.3     PUT THE DB AWAY

      CALL PUTDB('POST_THINN',8,ILEN,INFO,0,ZINFO,0)
      WRITE(NULOUT,'(''     POST_THINN_8; (PUTDB) REO3 REPORTS: '',I10)') ILEN
      CALL FLUSH(NULOUT)

!           10.3.4     DONE

    ENDIF

!           10.4       CLOSE TWO BOX LOOP

    1040 CONTINUE
    WRITE(NULOUT,'(''     ----------------------- END REO3 THINNING BOX LOOP:'',I3,1X,F10.2)') IBOX,ZBOX
    CALL FLUSH(NULOUT)
  ENDDO TWO_REO3_THINNING_BOXES

!           10.5       CLOSE TWO SENSOR LOOP

  1050 CONTINUE
  WRITE(NULOUT,'(''     ----------------------- END REO3 SENSOR LOOP:'',I10)') &
   & ISENSOR,IVARNO
  CALL FLUSH(NULOUT)
ENDDO REO3_SENSORS

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

!        11.       THINNING OF ACTIVE GPSRO DATA
!                 ------------------------------------

11 CONTINUE

IF(.NOT.LMFSCREEN) GO TO 12
IF(LL_GPSRO_THINNING) THEN
  WRITE(NULOUT,'(''     THINN_9; THINNING GPSRO DATA'')')
  CALL FLUSH(NULOUT)
ELSE
  GO TO 12
ENDIF

!          11.0       LOOP OVER 2 THINNING BOXES:

TWO_GPSRO_THINNING_BOXES: DO JBOX = 1 , 2
  IBOX  = JBOX
  IF(IBOX == 1) THEN
    ZBOX = RMIND_GPSRO
  ELSE
    ZBOX = RFIND_GPSRO
  ENDIF
  WRITE(NULOUT,'(''     ----------------------- START GPSRO THINNING BOX:'',I3,1X,F10.2)') IBOX,ZBOX
  CALL FLUSH(NULOUT)

!          11.1       PREPARE DATA LOCALLY

  IF(LL_GPSRO_THINNING_PRE) THEN
    ILEN = 0

!          11.1.1     GET DATA

    CALL GETDB('PRE_THINN',9,ILEN,INFO,0,ZINFO,0,&
     & -1, KTSLOT, -1, NLIMB, NGPSRO, -1)
    WRITE(NULOUT,'(''     PRE_THINN_9; (GETDB) GPSRO REPORTS FOUND: '',I10)') ILEN
    IF(ILEN == 0) GO TO 1120

!          11.1.2     PREPARE

    IF(ZBOX > 0.0_JPRB) THEN
      CALL PRE_THINNER(ILEN,IBOX,ZBOX,'GPSRO',NLIMB,NGPSRO,-1,-1)
    ENDIF

!          11.1.3     PUT THE DB AWAY

    CALL PUTDB('PRE_THINN',9,ILEN,INFO,0,ZINFO,0)
    WRITE(NULOUT,'(''     PRE_THINN_9; (PUTDB) GPSRO REPORTS: '',I10)') ILEN
    CALL FLUSH(NULOUT)

!          11.1.4     DONE

  ENDIF

!          11.2       GLOBAL THINNING

 1120 CONTINUE
  IF(LL_GPSRO_THINNING_THIN) THEN
    ILEN = 0

!          11.2.1     GET DATA

    CALL GETDB('NEW_THINN',9,ILEN,INFO,0,ZINFO,0,&
     & -1, KTSLOT, -1, NLIMB, NGPSRO, -1)
    WRITE(NULOUT,'(''     NEW_THINN_9; (GETDB) GPSRO REPORTS FOUND: '',I10)') ILEN
    CALL FLUSH(NULOUT)
    IF(ILEN == 0) GO TO 1125

!          11.2.2     THINN

    IF(ZBOX > 0.0_JPRB) THEN
      IF(LL_NEW_GPSRO_THINNING) THEN
        CALL NEW_THINNER_NO_SQ(ILEN,IBOX,     'GPSRO',NLIMB,NGPSRO,-1,-1)
      ELSE
        CALL NEW_THINNER      (ILEN,IBOX,ZBOX,'GPSRO',NLIMB,NGPSRO,-1,-1)
      ENDIF
    ENDIF

!          11.2.3     PUT THE DB AWAY

    CALL PUTDB('NEW_THINN',9,ILEN,INFO,0,ZINFO,0)
    WRITE(NULOUT,'(''     NEW_THINN_9; (PUTDB) GPSRO REPORTS: '',I10)') ILEN
    CALL FLUSH(NULOUT)

!          11.2.4     DONE


!          11.2.5     BYPASS

   1125  CONTINUE
  ENDIF

!          11.3       POST PROCESS DATA LOCALLY

 1130 CONTINUE
  IF(LL_GPSRO_THINNING_POST) THEN
    ILEN = 0

!          11.3.1     GET DATA

    CALL GETDB('POST_THINN',9,ILEN,INFO,0,ZINFO,0,&
     & -1, KTSLOT, -1, NLIMB, NGPSRO, -1)
    WRITE(NULOUT,'(''     POST_THINN_9; (GETDB) GPSRO REPORTS FOUND: '',I10)') ILEN
    CALL FLUSH(NULOUT)

!          11.3.2     POSTPROCESS

    CALL POST_THINNER(ILEN,'GPSRO',NLIMB,NGPSRO,-1,-1)

!          11.3.3     PUT THE DB AWAY

    CALL PUTDB('POST_THINN',9,ILEN,INFO,0,ZINFO,0)
    WRITE(NULOUT,'(''     POST_THINN_9; (PUTDB) GPSRO REPORTS: '',I10)') ILEN
    CALL FLUSH(NULOUT)

!          11.3.4     DONE

  ENDIF

!          11.4       CLOSE TWO BOX LOOP

 1140 CONTINUE
  WRITE(NULOUT,'(''     ----------------------- END GPSRO THINNING BOX LOOP:'',I3,1X,F10.2)') IBOX,ZBOX
  CALL FLUSH(NULOUT)
ENDDO TWO_GPSRO_THINNING_BOXES

!*
!     ------------------------------------------------------------------

!       12.       THINNING OF ACTIVE RADAR DATA
!                 -----------------------------

12 CONTINUE
IF(.NOT.LMFSCREEN) GO TO 13
IF(LL_RADAR_THINNING) THEN
  WRITE(NULOUT,'(''     THINN_10; THINNING RADAR DATA '')')
  CALL FLUSH(NULOUT)
ELSE
  GO TO 13
ENDIF

!          12.0       LOOP OVER 2 THINNING BOXES:

TWO_RADAR_THINNING_BOXES: DO JBOX = 1 , 2
  IBOX  = JBOX
  IF(IBOX == 1) THEN
    ZBOX = RMIND_RADAR
  ELSE
    ZBOX = RFIND_RADAR
  ENDIF
  WRITE(NULOUT,'(''     ----------------------- START RADAR THINNING BOX:'',I3,1X,F10.2)') IBOX,ZBOX

!          12.1       THINN DATA GLOBALY

  ILEN = 0

!          12.1.1     GET DATA

  CALL GETDB('PRE_THINN',10,ILEN,INFO,0,ZINFO,0,&
   & -1, KTSLOT, -1, NRADAR, -1, -1)
  WRITE(NULOUT,'(''     PRE_THINN_10; (GETDB) RADAR REPORTS FOUND: '',I10)') ILEN
  CALL FLUSH(NULOUT)
  IF(ILEN == 0) GO TO 2120

!          12.1.2     PREPARE

  IF(ZBOX > 0.0_JPRB) THEN
    CALL PRE_THINN_RADAR(ILEN,IBOX,ZBOX,NRADAR)
  ENDIF

!          12.1.3     PUT THE DB AWAY

  CALL PUTDB('PRE_THINN',10,ILEN,INFO,0,ZINFO,0)
  WRITE(NULOUT,'(''     PRE_THINN_10; (PUTDB) RADAR REPORTS: '',I10)') ILEN
  CALL FLUSH(NULOUT)

  2120 CONTINUE

!          12.2       THINN DATA GLOBALY

  ILEN = 0

!          12.2.1     GET DATA

  CALL GETDB('NEW_THINN',10,ILEN,INFO,0,ZINFO,0,&
   & -1, KTSLOT, -1, NRADAR, -1, -1)
  WRITE(NULOUT,'(''     NEW_THINN_10; (GETDB) RADAR REPORTS FOUND: '',I10)') ILEN
  CALL FLUSH(NULOUT)
  IF(ILEN == 0) GO TO 2125

!          12.2.2     PREPARE

  IF(ZBOX > 0.0_JPRB) THEN
    CALL NEW_THINN_RADAR(ILEN,IBOX,ZBOX,NRADAR)
  ENDIF

!          12.2.3     PUT THE DB AWAY

  CALL PUTDB('NEW_THINN',10,ILEN,INFO,0,ZINFO,0)
  WRITE(NULOUT,'(''     NEW_THINN_10; (PUTDB) RADAR REPORTS: '',I10)') ILEN
  CALL FLUSH(NULOUT)

  2125 CONTINUE

!          12.3       CLOSE TWO BOX LOOP

  WRITE(NULOUT,'(''     ----------------------- END RADAR THINNING BOX LOOP:'',I3,1X,F10.2)') IBOX,ZBOX
  CALL FLUSH(NULOUT)

ENDDO TWO_RADAR_THINNING_BOXES
!------------------------------------------------------------------------------------------------

!                 ------
13 CONTINUE
IF (LHOOK) CALL DR_HOOK('NEW_THINN',1,ZHOOK_HANDLE)
END SUBROUTINE NEW_THINN
