SUBROUTINE Cloud_Detect_Setup

!**** Cloud detection setup
!        A. Collard  ECMWF 01/02/06   

!     PURPOSE
!     -------
!        Initialise cloud detection parameters for advanced infrared sounders

!**   INTERFACE
!     ---------
!        cloud_detect_setup is called from defrun

!     METHOD
!     ------
!        Default values are assigned to the cloud detections setup structure.

!     EXTERNALS
!     ---------

!     MODIFICATIONS
!     -------------
!     01/02/06  Original code.     A. Collard
!     27/04/10  Modified the default channel selection for AIRS: Channel
!               1852 to band 3m and 1937 to band 5.  R. Eresmaa

USE PARKIND1,  ONLY : JPIM     ,JPRB

USE YOMHOOK,   ONLY : LHOOK,   DR_HOOK
USE YOMLUN ,   ONLY : NULOUT, NULUSR3

USE YOMCT0,    ONLY : NPROC
USE YOMMP,     ONLY : MYPROC 
USE MPL_MODULE,ONLY : MPL_BROADCAST

USE YOMIASI, ONLY   : S__Cloud_Detect_Setup, &
&                     JP__Min_Sensor_Index,  &
&                     JP__Max_Sensor_Index,  &
&                     MSENSOR_AIRS,          &
&                     MSENSOR_IASI,          &
&                     JP__Digital_Filter


IMPLICIT NONE

! Local variables

CHARACTER(LEN=5)   :: CL__InstrumentName
CHARACTER(LEN=15)  :: CL__Cloud_Detection_File

INTEGER(KIND=JPIM)   :: J, J__Sensor      ! Loop variables
INTEGER(KIND=JPIM)   :: IOMASTER, ITAG
INTEGER(KIND=JPIM)   :: INIU1, IOS
INTEGER(KIND=JPIM)   :: I__MaxAeroChans

REAL(KIND=JPRB)      :: ZHOOK_HANDLE

!-----------------------
! Namelist variables 
!-----------------------

! N.B. Max_Bands must be greater than 5
INTEGER(KIND=JPIM), PARAMETER :: JP__Max_Bands    =         8_JPIM
INTEGER(KIND=JPIM), PARAMETER :: JP__Max_Channels =      8461_JPIM
INTEGER(KIND=JPIM), PARAMETER :: JP__Max_Aerosol_Chans =  200_JPIM

INTEGER(KIND=JPIM)  :: M__Sensor 
INTEGER(KIND=JPIM)  :: N__Filter_Method 
INTEGER(KIND=JPIM)  :: N__Num_Bands 
INTEGER(KIND=JPIM)  :: N__GradChkInterval(JP__Max_Bands)
INTEGER(KIND=JPIM)  :: N__Band_Size(JP__Max_Bands)
INTEGER(KIND=JPIM)  :: N__Bands(JP__Max_Channels,JP__Max_Bands) 
INTEGER(KIND=JPIM)  :: N__Window_Width(JP__Max_Bands)
INTEGER(KIND=JPIM)  :: N__Window_Bounds(JP__Max_Bands,2)
REAL(KIND=JPRB)     :: R__BT_Threshold(JP__Max_Bands) 
REAL(KIND=JPRB)     :: R__Grad_Threshold(JP__Max_Bands)
REAL(KIND=JPRB)     :: R__Window_Grad_Threshold(JP__Max_Bands)
LOGICAL             :: L__Do_Quick_Exit  
LOGICAL             :: L__Do_CrossBand
INTEGER(KIND=JPIM)  :: N__BandToUse(JP__Max_Bands)
! Aerosol:
LOGICAL            :: L__Do_AerosolDetection   
INTEGER(KIND=JPIM) :: N__Num_Aerosol_Tests
INTEGER(KIND=JPIM) :: N__Num_Aerosol_Chans(JP__Max_Bands)
INTEGER(KIND=JPIM) :: N__Aerosol_Chans(JP__Max_Aerosol_Chans,JP__Max_Bands)
REAL(KIND=JPRB)    :: R__Aerosol_Abscissae(JP__Max_Aerosol_Chans,JP__Max_Bands)
REAL(KIND=JPRB)    :: R__Aerosol_Threshold(JP__Max_Bands)
REAL(KIND=JPRB)    :: R__AerosolMinNorm(JP__Max_Bands)

! Buffers used in broadcasting between processing elements.
INTEGER(KIND=JPIM) :: IBUF(8,JP__Max_Bands)
REAL(KIND=JPRB)    :: ZBUF(5,JP__Max_Bands)


#include "abor1.intfb.h"
#include "namclddet.h"

!============================================================================

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

IF (JP__Max_Bands < 5) CALL ABOR1 ('JP__Max_Bands must be greater than 5')

!============================================================================
!   Loop through sensors setting up cloud detection
!============================================================================

SensorLoop : DO J__Sensor = JP__Min_Sensor_Index, JP__Max_Sensor_Index

   
   SELECT CASE (J__Sensor)
      
   CASE(MSENSOR_AIRS)
      !====================
      ! Set up AIRS
      !====================

      CL__InstrumentName='AIRS'
      CL__Cloud_Detection_File = 'AIRS_CLDDET.NL'

      N__Filter_Method = JP__Digital_Filter
      
      N__Num_Bands = 5

      N__Band_Size(:) = 0
      N__Band_Size(1:N__Num_Bands) =(/141, 36, 54, 23, 65 /)

      IF (MAXVAL(N__Band_Size(:)) > JP__Max_Channels) &
&              CALL ABOR1('Too many channels specified in cloud '//&
&                      'detection - increase JP__Max_Channels')
 
      N__Bands(:,:)= 0 

      N__Bands(1:N__Band_Size(1),1) = &
&    (/ 1,   6,   7,  10,  11,  15,  16,  17,  20,  21, &
&      22,  24,  27,  28,  30,  36,  39,  40,  42,  51, &
&      52,  54,  55,  56,  59,  62,  63,  68,  69,  71, &
&      72,  73,  74,  75,  76,  77,  78,  79,  80,  82, &
&      83,  84,  86,  92,  93,  98,  99, 101, 104, 105, &
&     108, 110, 111, 113, 116, 117, 123, 124, 128, 129, &
&     138, 139, 144, 145, 150, 151, 156, 157, 159, 162, &
&     165, 168, 169, 170, 172, 173, 174, 175, 177, 179, &
&     180, 182, 185, 186, 190, 192, 193, 198, 201, 204, &
&     207, 210, 213, 215, 216, 218, 221, 224, 226, 227, &
&     232, 239, 248, 250, 251, 252, 253, 256, 257, 261, &
&     262, 267, 272, 295, 299, 300, 305, 308, 309, 310, &
&     318, 321, 325, 333, 338, 355, 362, 375, 453, 475, &
&     484, 497, 528, 587, 672, 787, 791, 843, 870, 914, &
&     950 /)

      N__Bands(1:N__Band_Size(2),2) = &
&    (/ 1003, 1012, 1019, 1024, 1030, 1038, 1048, 1069, 1079, 1082,  &
&       1083, 1088, 1090, 1092, 1095, 1104, 1111, 1115, 1116, 1119,  &
&       1120, 1123, 1130, 1138, 1142, 1178, 1199, 1206, 1221, 1237,  &
&       1252, 1260, 1263, 1266, 1278, 1285 /)

      N__Bands(1:N__Band_Size(3),3) = &
&    (/ 1290, 1301, 1304, 1329, 1371, 1382, 1415, 1424, 1449, 1455, &
&       1466, 1471, 1477, 1479, 1488, 1500, 1519, 1520, 1538, 1545, &
&       1565, 1574, 1583, 1593, 1614, 1627, 1636, 1644, 1652, 1669, &
&       1674, 1681, 1694, 1708, 1717, 1723, 1740, 1748, 1751, 1756, &
&       1763, 1766, 1771, 1777, 1780, 1783, 1794, 1800, 1803, 1806, &
&       1812, 1826, 1843, 1852 /)

      N__Bands(1:N__Band_Size(4),4) = &
&    (/ 1865, 1866, 1867, 1868, 1869, 1872, 1873, 1875, 1876, 1877, &
&       1881, 1882, 1883, 1884, 1897, 1901, 1911, 1917, 1918, 1921, &
&       1923, 1924, 1928 /)

      N__Bands(1:N__Band_Size(5),5) = &
&    (/ 1937, 1938, 1939, 1941, 1946, 1947, 1948, 1958, 1971, 1973, &
&       1988, 1995, 2084, 2085, 2097, 2098, 2099, 2100, 2101, 2103, &
&       2104, 2106, 2107, 2108, 2109, 2110, 2111, 2112, 2113, 2114, &
&       2115, 2116, 2117, 2118, 2119, 2120, 2121, 2122, 2123, 2128, &
&       2134, 2141, 2145, 2149, 2153, 2164, 2189, 2197, 2209, 2226, &
&       2234, 2280, 2318, 2321, 2325, 2328, 2333, 2339, 2348, 2353, &
&       2355, 2363, 2370, 2371, 2377  /)  

   
      N__GradChkInterval(:) = 0
      N__GradChkInterval(1:N__Num_Bands) = (/ 5,5,5,5,5 /)

      N__Window_Width(:) = 0
      N__Window_Width(1:N__Num_Bands) = (/ 10,6,8,5,8 /)

      N__Window_Bounds(:,:) = 0
      N__Window_Bounds(1,1) = 475
      N__Window_Bounds(1,2) = 950

      R__BT_Threshold(:) = 0.
      R__BT_Threshold(1:N__Num_Bands) = (/ 0.5, 0.5, 0.5, 0.5, 0.5/)

      R__Grad_Threshold(:) = 0.
      R__Grad_Threshold(1:N__Num_Bands) = (/ 0.02, 0.02, 0.02, 0.02, 0.02 /)

      R__Window_Grad_Threshold(:) = 0.
      R__Window_Grad_Threshold(1) = 0.4

      L__Do_Quick_Exit = .TRUE.

      ! This is cross-band:
      
      L__Do_CrossBand = .TRUE.

      N__BandToUse(:) = 0
      N__BandToUse(1:N__Num_Bands) = (/ 1,2,1,4,5 /)

      ! This is aerosol:

      L__Do_AerosolDetection = .TRUE.

      N__Num_Aerosol_Tests = 1
      N__Num_Aerosol_Chans(:) = 0
      N__Num_Aerosol_Chans(1:N__Num_Aerosol_Tests) = (/ 8 /)
      N__Aerosol_Chans(:,:) = 0
      N__Aerosol_Chans(1:N__Num_Aerosol_Chans(1),1) = &
&           (/ 1178, 1199, 1221, 1237, 1252, 1263, 1285, 1290 /)
      R__Aerosol_Abscissae(:,:) = 0.0
      R__Aerosol_Abscissae(1:N__Num_Aerosol_Chans(1),1) = &
&           (/ 1.70747, 1.58687, 1.47237, 1.34777, 1.23624, 0.518735, &
&              0.508225, 0.522959 /)
      R__Aerosol_Threshold(:) = 0.0
      R__Aerosol_Threshold(1:N__Num_Aerosol_Tests) = (/ 0.4 /)
      R__AerosolMinNorm(:) = 0.0
      R__AerosolMinNorm(1:N__Num_Aerosol_Tests) = (/ 2.0 /)

   CASE(MSENSOR_IASI)
      !====================
      ! Set up IASI
      !====================

      CL__InstrumentName='IASI'
      CL__Cloud_Detection_File = 'IASI_CLDDET.NL'

      N__Filter_Method = JP__Digital_Filter
      
      N__Num_Bands = 5

      N__Band_Size(:) = 0
      N__Band_Size(1:N__Num_Bands) =(/ 193, 15, 116, 4, 15 /)

      IF (MAXVAL(N__Band_Size(:)) > JP__Max_Channels) &
&              CALL ABOR1('Too many channels specified in cloud '//&
&                      'detection - increase JP__Max_Channels')
 
      N__Bands(:,:)= 0 

      ! Use the "IASI 366" Subset
      N__Bands(1:N__Band_Size(1),1) = &
&      (/    16,   38,   49,   51,   55,   57,   59,   61,   63,   66, &
&            70,   72,   74,   79,   81,   83,   85,   87,   89,   92, &
&            95,   97,   99,  101,  104,  106,  109,  111,  113,  116, &
&           119,  122,  125,  128,  131,  133,  135,  138,  141,  144, &
&           146,  148,  151,  154,  157,  159,  161,  163,  165,  167, &
&           170,  173,  176,  178,  179,  180,  183,  185,  187,  189, &
&           191,  193,  195,  197,  199,  201,  203,  205,  207,  210, &
&           212,  214,  217,  219,  222,  224,  226,  228,  230,  232, &
&           234,  236,  239,  241,  242,  243,  246,  249,  252,  254, &
&           256,  258,  260,  262,  265,  267,  269,  271,  272,  273, &
&           275,  278,  280,  282,  284,  286,  288,  290,  292,  294, &
&           296,  299,  301,  303,  306,  308,  310,  312,  314,  316, &
&           318,  320,  323,  325,  327,  329,  331,  333,  335,  337, &
&           339,  341,  343,  345,  347,  350,  352,  354,  356,  358, &
&           360,  362,  364,  366,  369,  371,  373,  375,  377,  379, &
&           381,  383,  386,  389,  398,  401,  404,  407,  410,  414, &
&           416,  426,  428,  432,  434,  439,  445,  457,  515,  546, &
&           552,  559,  566,  571,  573,  646,  662,  668,  756,  867, &
&           921, 1027, 1090, 1133, 1191, 1194, 1271, 1805, 1884, 1946, &
&          1991, 2094, 2239 /)

 N__Bands(1:N__Band_Size(2),2) = &
&      (/ 1479, 1509, 1513, 1521, 1536, 1574, 1579, 1585, 1587, 1626, &
&         1639, 1643, 1652, 1658, 1671  /)

      N__Bands(1:N__Band_Size(3),3) = &
&      (/ 2119, 2213, 2271, 2321, 2398, 2701, 2741, 2819, 2889, 2907, 2910, &
&         2919, 2939, 2944, 2948, 2951, 2958, 2977, 2985, 2988, 2991, &
&         2993, 3002, 3008, 3014, 3027, 3029, 3036, 3047, 3049, 3053, &
&         3058, 3064, 3069, 3087, 3093, 3098, 3105, 3107, 3110, 3127, &
&         3136, 3151, 3160, 3165, 3168, 3175, 3178, 3207, 3228, 3244, &
&         3248, 3252, 3256, 3263, 3281, 3303, 3309, 3312, 3322, 3375, &
&         3378, 3411, 3438, 3440, 3442, 3444, 3446, 3448, 3450, 3452, &
&         3454, 3458, 3467, 3476, 3484, 3491, 3497, 3499, 3504, 3506, &
&         3509, 3518, 3527, 3555, 3575, 3577, 3580, 3582, 3586, 3589, &
&         3599, 3653, 3658, 3661, 4032, 5368, 5371, 5379, 5381, 5383, &
&         5397, 5399, 5401, 5403, 5405, 5455, 5480, 5483, 5485, 5492, &
&         5502, 5507, 5509, 5517, 5558  /)

      N__Bands(1:N__Band_Size(4),4) = &
&      (/   5988, 5992, 5994, 6003  /)


      N__Bands(1:N__Band_Size(5),5) = &
&      (/  6982, 6985, 6987, 6989, 6991, 6993, 6995, 6997, 7267, 7269, &
&          7424, 7426, 7428, 7885, 8007 /)  
   
      N__GradChkInterval(:) = 0
      N__GradChkInterval(1:N__Num_Bands) = (/ 5,5,5,5,5 /)

      N__Window_Width(:) = 0
      N__Window_Width(1:N__Num_Bands) = (/ 10,6,8,5,8 /)

      N__Window_Bounds(:,:) = 0
      N__Window_Bounds(1,1) = 573
      N__Window_Bounds(1,2) = 2239

      R__BT_Threshold(:) = 0.
      R__BT_Threshold(1:N__Num_Bands) = (/ 0.5, 0.5, 0.5, 0.5, 0.5/)

      R__Grad_Threshold(:) = 0.
      R__Grad_Threshold(1:N__Num_Bands) = (/ 0.02, 0.02, 0.02, 0.02, 0.02 /)

      R__Window_Grad_Threshold(:) = 0.
      R__Window_Grad_Threshold(1) = 0.4

      L__Do_Quick_Exit = .TRUE.

      ! This is cross-band:
      
      L__Do_CrossBand = .TRUE.

      N__BandToUse(:) = 0
      N__BandToUse(1:N__Num_Bands) = (/ 1,1,1,1,1 /)

      ! This is aerosol:
      L__Do_AerosolDetection = .TRUE.

      N__Num_Aerosol_Tests = 1
      N__Num_Aerosol_Chans(:) = 0
      N__Num_Aerosol_Chans(1:N__Num_Aerosol_Tests) = (/ 8 /)
      N__Aerosol_Chans(:,:) = 0
      N__Aerosol_Chans(1:N__Num_Aerosol_Chans(1),1) = &
&           (/ 1791, 1834, 1880, 1914, 1946, 2289, 2334, 2344 /)
      R__Aerosol_Abscissae(:,:) = 0.0
      R__Aerosol_Abscissae(1:N__Num_Aerosol_Chans(1),1) = &
&           (/ 1.70747, 1.58687, 1.47237, 1.34777, 1.23624, 0.518735, &
&              0.508225, 0.522959 /)
      R__Aerosol_Threshold(:) = 0.0
      R__Aerosol_Threshold(1:N__Num_Aerosol_Tests) = (/ 0.4 /)
      R__AerosolMinNorm(:) = 0.0
      R__AerosolMinNorm(1:N__Num_Aerosol_Tests) = (/ 2.0 /)

   CASE DEFAULT
      CYCLE
   END SELECT

   !------------------------------------------------------------------
   ! Open and read file containing cloud detection setup for the 
   ! current instrument
   !------------------------------------------------------------------
   IOMASTER=1
   MasterProcessor : IF(MYPROC == IOMASTER) THEN
      INIU1=NULUSR3
      WRITE(NULOUT,*)'READING CLOUD DETECTION FILE FOR ',CL__InstrumentName
      OPEN(INIU1,STATUS='OLD',FORM='FORMATTED',FILE=CL__Cloud_Detection_File, &
&                   IOSTAT=IOS)
      IF (IOS == 0) THEN
         READ(INIU1,nml=Cloud_Detect_Coeffs,IOSTAT=IOS)
         IF (IOS == 0) THEN
            WRITE(NULOUT,*) CL__InstrumentName,' CLOUD DETECTION FILE READ OK'
         ELSE
            WRITE(NULOUT,*)'PROBLEM READING '//CL__InstrumentName//&
&                'CLOUD DETECTION FILE: Using Default Values'
         ENDIF
         CLOSE(INIU1)
      ELSE
         WRITE(NULOUT,*)'NO '//CL__InstrumentName//&
&              ' CLOUD DETECTION FILE : Using Default Values'
      ENDIF
      
      IF (MAXVAL(N__Band_Size(:)) > JP__Max_Channels) &
&              CALL ABOR1('Too many channels specified in cloud '//&
&                      'detection - increase JP__Max_Channels')
      
      
      M__Sensor = J__Sensor
      
      !------------------------------------------------------------------
      ! Broadcast values if using multiple PEs
      !------------------------------------------------------------------

      IF (NPROC > 1) THEN
         !   Load buffers used in MPL
         IBUF(1,1)   = M__Sensor
         IBUF(1,2)   = N__Filter_Method 
         IBUF(1,3)   = N__Num_Bands    
         IF (L__Do_Quick_Exit) THEN
            IBUF(1,4)   = 1
         ELSE
            IBUF(1,4)   = 0
         ENDIF
         IF (L__Do_CrossBand) THEN
            IBUF(1,5)   = 1
         ELSE
            IBUF(1,5)   = 0
         ENDIF
         IF (L__Do_AerosolDetection) THEN
            IBUF(1,6)   = 1
         ELSE
            IBUF(1,6)   = 0
         ENDIF
         IBUF(1,7) = N__Num_Aerosol_Tests  
         IBUF(2,:) = N__Band_Size 
         IBUF(3,:) = N__Window_Width 
         IBUF(4,:) = N__GradChkInterval
         IBUF(5,:) = N__BandToUse
         IBUF(6,:) = N__Num_Aerosol_Chans
         IBUF(7,:) = N__Window_Bounds(:,1)
         IBUF(8,:) = N__Window_Bounds(:,2)
         
         ZBUF(1,:)                = R__BT_Threshold  
         ZBUF(2,:)                = R__Grad_Threshold 
         ZBUF(3,:)                = R__Aerosol_Threshold 
         ZBUF(4,:)                = R__AerosolMinNorm
         ZBUF(5,:)                = R__Window_Grad_Threshold 
      ENDIF
      
   ENDIF MasterProcessor
   
   
   IF(NPROC > 1) THEN
      ITAG=5555
      CALL MPL_BROADCAST(  &
&          IBUF,           &
&          KROOT=IOMASTER, &
&          KTAG=ITAG,      &
&          CDSTRING='CLOUD_DETECT_SETUP:')

      ITAG=5556
      CALL MPL_BROADCAST(  &
&          ZBUF,           &
&          KROOT=IOMASTER, &
&          KTAG=ITAG,      &
&          CDSTRING='CLOUD_DETECT_SETUP:')
      
      !   Unload buffers used in MPL
      M__Sensor           = IBUF(1,1)
      N__Filter_Method    = IBUF(1,2)
      N__Num_Bands        = IBUF(1,3)
      IF (IBUF(1,4) == 1) THEN
         L__Do_Quick_Exit = .TRUE.
      ELSE
         L__Do_Quick_Exit = .FALSE.
      ENDIF
      IF (IBUF(1,5) == 1) THEN
         L__Do_CrossBand = .TRUE.
      ELSE
         L__Do_CrossBand = .FALSE.
      ENDIF
      IF (IBUF(1,6) == 1) THEN
         L__Do_AerosolDetection = .TRUE.
      ELSE
         L__Do_AerosolDetection = .FALSE.
      ENDIF
      N__Num_Aerosol_Tests  = IBUF(1,7)  
      N__Band_Size          = IBUF(2,:)
      N__Window_Width       = IBUF(3,:)
      N__GradChkInterval    = IBUF(4,:)
      N__BandToUse          = IBUF(5,:)  
      N__Num_Aerosol_Chans  = IBUF(6,:)
      N__Window_Bounds(:,1) = IBUF(7,:)
      N__Window_Bounds(:,2) = IBUF(8,:)

      R__BT_Threshold      = ZBUF(1,:)
      R__Grad_Threshold    = ZBUF(2,:)
      R__Aerosol_Threshold = ZBUF(3,:)
      R__AerosolMinNorm    = ZBUF(4,:)
      R__Window_Grad_Threshold = ZBUF(5,:)
     
      ITAG=5557
      CALL MPL_BROADCAST(  &
&          N__Bands(1:MAXVAL(N__Band_Size(:)),1:N__Num_Bands), &
&          KROOT=IOMASTER, &
&          KTAG=ITAG,      &
&          CDSTRING='CLOUD_DETECT_SETUP:')


      IF (L__Do_AerosolDetection) THEN
         I__MaxAeroChans = MAXVAL(N__Num_Aerosol_Chans(:))

         ITAG=5558
         CALL MPL_BROADCAST(                                             &
&            N__Aerosol_Chans(1:I__MaxAeroChans,1:N__Num_Aerosol_Tests), &
&            KROOT=IOMASTER,                                             &
&            KTAG=ITAG,                                                  &
&            CDSTRING='CLOUD_DETECT_SETUP:')

         ITAG=5559
         CALL MPL_BROADCAST(                                                 &
&            R__Aerosol_Abscissae(1:I__MaxAeroChans,1:N__Num_Aerosol_Tests), &
&            KROOT=IOMASTER,                                                 &
&            KTAG=ITAG,                                                      &
&            CDSTRING='CLOUD_DETECT_SETUP:')
      ENDIF


   ENDIF

   !------------------------------------------------------------------
   ! Set up the S__Cloud_Detect_Setup structure for current sensor
   !------------------------------------------------------------------

   S__Cloud_Detect_Setup(J__SENSOR) % M__SENSOR = M__Sensor
   
   S__Cloud_Detect_Setup(J__SENSOR) % N__Filter_Method = N__Filter_Method
   
   S__Cloud_Detect_Setup(J__SENSOR) % N__Num_Bands = N__Num_Bands
   
   ALLOCATE( S__Cloud_Detect_Setup(J__SENSOR) % N__Band_Size(N__Num_Bands) )
   
   S__Cloud_Detect_Setup(J__SENSOR) % N__Band_Size(:) = &
&             N__Band_Size(1:N__Num_Bands)
   
   ALLOCATE(S__Cloud_Detect_Setup(J__SENSOR) % N__Bands & 
&            (MAXVAL(N__Band_Size(:)), N__Num_Bands))
      
   S__Cloud_Detect_Setup(J__SENSOR) % N__Bands(:,:) = 0

   DO J = 1, N__Num_Bands
      S__Cloud_Detect_Setup(J__SENSOR) % N__Bands(1:N__Band_Size(J),J) = &
&          N__Bands(1:N__Band_Size(J),J)
      IF (MYPROC == 1) THEN
         WRITE(NULOUT,*) 'Sensor ',M__Sensor,' Band ',J,' has',&
&          N__Band_Size(J),' Cloud Detection Channels :'
         WRITE(NULOUT,*) N__Bands(1:N__Band_Size(J),J)
      ENDIF
   ENDDO
      
   ALLOCATE( S__Cloud_Detect_Setup(J__SENSOR) % N__Window_Width(N__Num_Bands) )
      
   S__Cloud_Detect_Setup(J__SENSOR) % N__Window_Width(:) = &
&        N__Window_Width(1:N__Num_Bands) 
      
      
   ALLOCATE( S__Cloud_Detect_Setup(J__SENSOR) % R__BT_Threshold(N__Num_Bands) )
   S__Cloud_Detect_Setup(J__SENSOR) % R__BT_Threshold(:) = &
&        R__BT_Threshold(1:N__Num_Bands) 
      
   ALLOCATE(S__Cloud_Detect_Setup(J__SENSOR) % R__Grad_Threshold(N__Num_Bands))
   S__Cloud_Detect_Setup(J__SENSOR) % R__Grad_Threshold(:) = &
&        R__Grad_Threshold(1:N__Num_Bands) 
      
   ALLOCATE(S__Cloud_Detect_Setup(J__SENSOR) % &
&        R__Window_Grad_Threshold(N__Num_Bands))
   S__Cloud_Detect_Setup(J__SENSOR) % R__Window_Grad_Threshold(:) = &
&        R__Window_Grad_Threshold(1:N__Num_Bands) 
      
   ALLOCATE(S__Cloud_Detect_Setup(J__SENSOR) % N__GradChkInterval(N__Num_Bands))
   S__Cloud_Detect_Setup(J__SENSOR) % N__GradChkInterval(:) = &
&        N__GradChkInterval(1:N__Num_Bands) 
      
   ALLOCATE(S__Cloud_Detect_Setup(J__SENSOR) % N__Window_Bounds(N__Num_Bands,2))
   S__Cloud_Detect_Setup(J__SENSOR) % N__Window_Bounds(:,:) = &
&        N__Window_Bounds(1:N__Num_Bands,:) 
      
   S__Cloud_Detect_Setup(J__SENSOR) % L__Do_Quick_Exit = L__Do_Quick_Exit
   
   !-------------
   ! Cross Band
   !-------------

   S__Cloud_Detect_Setup(J__SENSOR) % L__Do_CrossBand = L__Do_CrossBand
      
   ALLOCATE( S__Cloud_Detect_Setup(J__SENSOR) % N__BandToUse(N__Num_Bands) )
   S__Cloud_Detect_Setup(J__SENSOR) % N__BandToUse(:) = &
&        N__BandToUse(1:N__Num_Bands) 
      
   !-------------
   ! Aerosol
   !-------------

   S__Cloud_Detect_Setup(J__SENSOR) % L__Do_AerosolDetection = &
&        L__Do_AerosolDetection

   S__Cloud_Detect_Setup(J__SENSOR) % N__Num_Aerosol_Tests = &
&        N__Num_Aerosol_Tests

   ALLOCATE( S__Cloud_Detect_Setup(J__SENSOR) % &
&        N__Num_Aerosol_Chans(N__Num_Aerosol_Tests) )

   S__Cloud_Detect_Setup(J__SENSOR) % N__Num_Aerosol_Chans(:) = &
&        N__Num_Aerosol_Chans(1:N__Num_Aerosol_Tests) 

   ALLOCATE(S__Cloud_Detect_Setup(J__SENSOR) % N__Aerosol_Chans & 
&            (MAXVAL(N__Num_Aerosol_Chans(:)), N__Num_Aerosol_Tests))

   S__Cloud_Detect_Setup(J__SENSOR) % N__Aerosol_Chans(:,:) = 0
   DO J = 1, N__Num_Aerosol_Tests
      S__Cloud_Detect_Setup(J__SENSOR) % &
&          N__Aerosol_Chans(1:N__Num_Aerosol_Chans(J),J) = &
&          N__Aerosol_Chans(1:N__Num_Aerosol_Chans(J),J)
   ENDDO

   ALLOCATE(S__Cloud_Detect_Setup(J__SENSOR) % R__Aerosol_Abscissae & 
&            (MAXVAL(N__Num_Aerosol_Chans(:)), N__Num_Aerosol_Tests))

   S__Cloud_Detect_Setup(J__SENSOR) % R__Aerosol_Abscissae(:,:) = 0.0
   DO J = 1, N__Num_Aerosol_Tests
      S__Cloud_Detect_Setup(J__SENSOR) % &
&          R__Aerosol_Abscissae(1:N__Num_Aerosol_Chans(J),J) = &
&          R__Aerosol_Abscissae(1:N__Num_Aerosol_Chans(J),J)
   ENDDO

   ALLOCATE( S__Cloud_Detect_Setup(J__SENSOR) % &
&        R__Aerosol_Threshold(N__Num_Aerosol_Tests) )

   S__Cloud_Detect_Setup(J__SENSOR) % R__Aerosol_Threshold(:) = &
&        R__Aerosol_Threshold(1:N__Num_Aerosol_Tests) 

   ALLOCATE( S__Cloud_Detect_Setup(J__SENSOR) % &
&        R__AerosolMinNorm(N__Num_Aerosol_Tests) )

   S__Cloud_Detect_Setup(J__SENSOR) % R__AerosolMinNorm(:) = &
&        R__AerosolMinNorm(1:N__Num_Aerosol_Tests) 

ENDDO SensorLoop

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

END SUBROUTINE CLOUD_DETECT_SETUP
