      SUBROUTINE WINCHN(NIGHT,OVRLND,TSFC8,TSFC18,TSFC19,HIRS20,DAYLIM,
     1                 DAYLM2,NTLIM1,NTLIM2,NTLIM3,NTLIM4,HIRS8,TS8LIM,
     2                TS8LM2,TS8LM3,TS8LM4,TS8LM5,TROPDF,TROPLT,ICEFRE,
     3                 SNWFRE,SPOT,LINE,RESULT,ERSTAT)
C***********************************************************************
C
C   NAME- WINCHN
C
C   LANGUAGE- VS FORTRAN  TYPE- SUBROUTINE      COMPUTER- CRAY J916
C
C   VERSION- 1.0    DATE- 2/11/93     PROGRAMMER- CARLA NEIGH, SMSRC
C   VERSION- 3.0    DATE- 05/29/96   PROGRAMMER- LEAH WILSON (HSTX)
C         SYNTAX MODS FOR ATOVS BASELINE ON CRAY.
C   VERSION- 3.1    DATE- 09/23/97   PROGRAMMER- LEAH WILSON (HSTX)
C   VERSION- 3.2    DATE- 04/06/98   PROGRAMMER- LEAH WILSON (RSTX)
C   VERSION- 3.3    DATE- 11/18/98   PROGRAMMER- M.P.FERGUSON (RITSS)
C   VERSION- 3.4    DATE- 11/30/98   PROGRAMMER- M.P.FERGUSON (RITSS)
C   VERSION- 3.5    DATE- 09/10/99   PROGRAMMER- M.P.FERGUSON (RITSS)
C   VERSION- 3.6    DATE- 12/12/01   PROGRAMMER- M.P.FERGUSON (RITSS)
C   VERSION- 3.7    DATE- 12/16/01   PROGRAMMER- M.P.FERGUSON (RITSS)
C   VERSION- 3.8    DATE- 03/26/03   PROGRAMMER- R. ROLLINS (CSC)
C
C   FUNCTION-  THIS SUBROUTINE PERFORMS THE WINDOW CHANNEL TEST ON A
C              HIRS SPOT TO DETERMINE IF CLOUDS ARE PRESENT.
C   VERSION- 3.1 RTOVS UPGRADES INVOLVING 8 NEW PARAMETERS ADDED TO
C                SUBROUTINE CALL.  CHANGE WINCH$ TO USE KIDS FILE.
C   VERSION- 3.2 RTOVS UPGRADE TO ALLOW TSEST 8 - HIRS 8 TEST OVER
C                ICE.
C   VERSION- 3.3 MODIFY USAGEOF TSEST 8 - HIRS 8 TEST OVER ICE AND
C                ALLOW IT'S USE OVER LAND AS WELL, WITH NEW THRES-
C                HOLD TS8LM2.
C   VERSION- 3.4 ALLOW USAGE OF TSEST 8 - HIRS 8 TEST OVER SNOW, ICE,
C                AND LAND WITH DIFFERING THRESHOLDS FOR EACH.
C   VERSION- 3.5 FOR ATOVS, ALBEDO (HIRS 20) WILL NOT BE USED AS A 
C                CRITICAL CRITERION FOR EXCLUDING USE OF DAYTIME
C                WINDOW CHANNEL TESTS; THE THRESHOLD WILL THEREFORE
C                BE CHANGED FROM 20 TO 60 TO ELIMINATE ERRONEOUS NON-
C                USE OF THE TESTS.
C   VERSION- 3.6 USE LATITUDE DELIMETER FOR LAND AND COAST FOVS INSTEAD
C                OF SEA FOVS.
C   VERSION- 3.7 TURN OFF ALBEDO CHECKING TO DETERMINE DAY TEST USE.
C                (WR # 183).
C   VERSION- 3.8 USE ISR4SPV Function to test for R4SPV
C
C   CALLING SEQUENCE PARAMETERS-
C   NAME    TYPE   I/O   DESCRIPTION
C   ----    ----   ---   -----------
C   NIGHT  LOGICAL  I    LOGICAL FLAG SPECIFYING WHETHER HIRS SPOT
C                        IS IN NIGHTTIME (TRUE = NIGHTTIME, FALSE =
C                        DAYTIME).
C   OVRLND LOGICAL  I    LOGICAL FLAG SPECIFYING WHETHER HIRS SPOT
C                        IS OVER LAND OR WATER (TRUE = OVER LAND,
C                        FALSE = OVER WATER).
C   TSFC8    REAL   I    SURFACE TEMPERATURE VALUE COMPUTED FROM HIRS
C                        CHANNEL 8.
C   TSFC18   REAL   I    SURFACE TEMPERATURE VALUE COMPUTED FROM HIRS
C                        CHANNEL 18.
C   TSFC19   REAL   I    SURFACE TEMPERATURE VALUE COMPUTED FROM HIRS
C                        CHANNEL 19.
C   HIRS20   REAL   I    HIRS CHANNEL 20 PERCENT ALBEDO FOR THE SPOT.
C   DAYLIM   REAL   I    WINDOW CHANNEL TEST LIMIT FOR DAYLIGHT TESTS.
C   DAYLM2   REAL   I    WINDOW CHANNEL TEST LIMIT FOR DAYLIGHT TESTS.
C   NTLIM1   REAL   I    WINDOW CHANNEL TEST LIMIT FOR NIGHTTIME AREAS,
C                        TEST CASE ONE.
C   NTLIM2   REAL   I    WINDOW CHANNEL TEST LIMIT FOR NIGHTTIME AREAS,
C                        TEST CASE TWO.
C   NTLIM3   REAL   I    WINDOW CHANNEL TEST LIMIT FOR NIGHTTIME AREAS,
C                        TEST CASE THREE.
C   NTLIM4   REAL   I    WINDOW CHANNEL TEST LIMIT FOR NIGHTTIME AREAS,
C                        TEST CASE FOUR.
C   HIRS8    REAL   I    HIRS CHANNEL 8 VALUE.
C   TS8LIM   REAL   I    WINDOW CHANNEL TEST LIMIT FOR THE DIFFERENCE
C                        BETWEEN OBSERVED HIRS 8 AND THE TSE8 VALUE.
C   TROPDF   REAL   I
C   TROPLT   REAL   I
C   ICEFRE   LOG    I    TRUE IF HIRS SPOT IS OVER ICE, ELSE FALSE.
C   SPOT     INT    I    HIRS SPOT NUMBER.
C   LINE     INT    I    HIRS LINE NUMBER.
C   RESULT   INT   I/O   RESULT OF THE WINDOW CHANNEL TEST.  THE VALUE
C                        IS 0 IF THE TEST DETERMINED SPOT TO BE CLOUDY,
C                        1 IF THE TEST DID NOT DETERMINE THE SPOT TO
C                        BE CLOUDY, AND 2 IF A DETERMINATION COULD NOT
C                        BE MADE.  IT IS ASSUMED THAT RESULT HAS BEEN
C                        INITIALIZED TO 2 BEFORE THIS ROUTINE IS CALLED.
C   ERSTAT   INT    O    ERROR STATUS CODE
C                         = 0 IF NO ERROR OCCURRED,
C                         = 10 IF NON-FATAL ERROR OCCURRED,
C                         = 99 IF FATAL ERROR OCCURRED.
C
C   LOCAL PARAMETERS-
C   NAME    TYPE   DESCRIPTION
C   ----    ----   -----------
C   STATUS  INT    STATUS CODE RETURNED FROM WINCH$.
C   DIFF1   REAL   DIFFERENCE BETWEEN SURFACE TEMPERATURE VALUES.
C   DIFF2   REAL   DIFFERENCE BETWEEN SURFACE TEMPERATURE VALUES.
C   DIFF3   REAL   DIFFERENCE BETWEEN SURFACE TEMPERATURE VALUES.
C   DIFF4   REAL   DIFFERENCE BETWEEN SURFACE TEMPERATURE VALUES.
C
C   SUBPROGRAMS CALLED- WINCH$
C
C   FUNCTIONS USED- ABS
C
C   EXIT STATES-
C    ERSTAT = 10 : NON-FATAL ERROR - A WINDOW CHANNEL TEST LIMIT
C                  IS A MISSING VALUE.
C    ERSTAT = 99 : FATAL ERROR RETURNED FROM WINCH$.
C
C   COMMENTS-
C    IT IS ASSUMED THAT RESULT HAS BEEN INITIALIZED TO 2 BEFORE THIS
C    ROUTINE IS CALLED.
C
C    THE REFERENCES FOR THIS CLOUD TEST ARE:
C     WAHICHE, SCOTT, AND CHEDIN, "CLOUD DETECTION AND CLOUD PARAMETERS
C     RETRIEVAL FROM THE SATELLITES OF THE TIROS-N SERIES".  ANNALES
C     GEOPHYSICAE, 1986, 4, B, 2, 207-222.
C
C     CHEDIN ET AL, "RECENT PROGRESS IN THE DETERMINATION OF
C     METEOROLOGICAL PARAMETERS FROM THE SATELLITES OF THE TIROS-N
C     SERIES". TECHNICAL PROCEEDINGS THIRD ITOVS STUDY CONFERENCE, 13-19
C     AUGUST 1986, MADISON, WI.
C
C***********************************************************************
C
      LOGICAL NIGHT, OVRLND, ICEFRE, SNWFRE
      LOGICAL ISR4SPV
      REAL TSFC8, TSFC18, TSFC19, HIRS20, DAYLIM, NTLIM1, NTLIM2
      REAL NTLIM3, NTLIM4, D, DAYLM2, HIRS8, TS8LIM, TS8LM2
      REAL TROPDF, TROPLT, TS8LM3, TS8LM4, TS8LM5
      INTEGER RESULT, ERSTAT, SPOT, LINE
C
      INTEGER STATUS
      REAL DIFF1, DIFF2, DIFF3, DIFF4, DIFF5
C
      INCLUDE 'SPVAL'
      INCLUDE 'INSPARM'
      INCLUDE 'INSCOM'
C
      ERSTAT = 0
C
C    THE WINDOW CHANNEL TEST IS DIFFERENT FOR SPOTS THAT ARE IN DAYLIGHT
C    AND SPOTS THAT ARE IN NIGHTTIME.  CHECK FOR DAY OR NIGHT TEST.
C
      IF (NIGHT) THEN
C
C       IF ANY OF THE NIGHT TEST LIMITS ARE MISSING, WRITE A WARNING
C       MESSAGE TO THE KIDS MESSAGE LOG FILE AND SKIP THIS TEST.
C
C        IF ((NTLIM1 .EQ. R4SPV) .OR. (NTLIM2 .EQ. R4SPV) .OR.
C    1       (NTLIM3 .EQ. R4SPV) .OR. (NTLIM4 .EQ. R4SPV)) THEN
         IF ((ISR4SPV(NTLIM1)) .OR. (ISR4SPV(NTLIM2)) .OR.
     1       (ISR4SPV(NTLIM3)) .OR. (ISR4SPV(NTLIM4))) THEN
            CALL WINCH$(101,STATUS)
            ERSTAT = 10
            IF (STATUS.EQ.99) ERSTAT = 99
            RETURN
         ENDIF
C
C       COMPUTE THE DIFFERENCE VALUES USED FOR THE NIGHTTIME TEST CASES.
C
         IF ((.NOT. ISR4SPV(TSFC8)) .AND. (.NOT. ISR4SPV(HIRS8)) .AND.
     +       (.NOT. OVRLND) .AND. (ICEFRE) .AND. (SNWFRE)
     +       .AND. HIRANC(SPOT,LINE,8).LE.1500.) THEN
            DIFF5 = ABS (TSFC8 - HIRS8)
         ELSE IF ((.NOT. ISR4SPV(TSFC8)) .AND. (.NOT. ISR4SPV(HIRS8)) .AND.
     +      ((OVRLND) .AND. (ICEFRE) .AND. (SNWFRE))
     +      .AND. HIRANC(SPOT,LINE,8).LE.1500.) THEN
            DIFF5 = ABS (TSFC8 - HIRS8) + (TS8LIM - TS8LM2)
            IF (ABS (HIRANC(SPOT,LINE,1)) .LE. TROPLT) THEN
              IF(DIFF5 .LE. TROPDF)THEN
                 DIFF5 = 0.
              ELSE
                 DIFF5 = DIFF5 - TROPDF
              ENDIF
            ENDIF
         ELSE IF ((.NOT. ISR4SPV(TSFC8)) .AND. (.NOT. ISR4SPV(HIRS8)) .AND.
     +      (.NOT. ICEFRE)
     +      .AND. HIRANC(SPOT,LINE,8).LE.1500.) THEN
            DIFF5 = ABS (TSFC8 - HIRS8) + (TS8LIM - TS8LM3)
         ELSE IF ((.NOT. ISR4SPV(TSFC8)) .AND. (.NOT. ISR4SPV(HIRS8)) .AND.
     +      (.NOT. SNWFRE)
     +      .AND. HIRANC(SPOT,LINE,8).LE.1500.) THEN
            DIFF5 = ABS (TSFC8 - HIRS8) + (TS8LIM - TS8LM4)
         ELSE IF ((.NOT. ISR4SPV(TSFC8)) .AND. (.NOT. ISR4SPV(HIRS8)) 
     +      .AND. HIRANC(SPOT,LINE,8).GT.1500.) THEN
            DIFF5 = ABS (TSFC8 - HIRS8) + (TS8LIM - TS8LM5)
         ELSE
            DIFF5 = R4SPV
         ENDIF
         IF ((.NOT. ISR4SPV(TSFC18)) .AND. (.NOT. ISR4SPV(TSFC8))) THEN
            DIFF1 = TSFC18 - TSFC8
            DIFF2 = TSFC8 - TSFC18
         ELSE
            DIFF1 = R4SPV
            DIFF2 = R4SPV
         ENDIF
C
         IF ((.NOT. ISR4SPV(TSFC18)) .AND. (.NOT. ISR4SPV(TSFC19))) THEN
            DIFF3 = TSFC19 - TSFC18
            DIFF4 = TSFC18 - TSFC19
         ELSE
            DIFF3 = R4SPV
            DIFF4 = R4SPV
         ENDIF
C
C       IF ANY OF THE DIFFERENCES ARE GREATER THAN THE TEST CASE
C       LIMIT, THE HIRS SPOT IS CLOUDY.
C
         IF (((.NOT.ISR4SPV(DIFF1)) .AND. (DIFF1 .GT. NTLIM1)) .OR.
     1       ((.NOT. ISR4SPV(DIFF2)) .AND. (DIFF2 .GT. NTLIM2)) .OR.
     2       ((.NOT. ISR4SPV(DIFF3)) .AND. (DIFF3 .GT. NTLIM3)) .OR.
     3       ((.NOT. ISR4SPV(DIFF4)) .AND. (DIFF4 .GT. NTLIM4)) .OR.
     4       ((.NOT. ISR4SPV(DIFF5)) .AND. (DIFF5 .GT. TS8LIM))) THEN
            RESULT = 0
         ELSE
            RESULT = 1
         ENDIF
C
C
      ELSE
C
C       IF THE DAY TEST LIMIT IS MISSING, WRITE A WARNING MESSAGE
C       TO THE KIDS MESSAGE LOG FILE AND SKIP THIS TEST.
C
         IF (ISR4SPV(DAYLIM).OR.ISR4SPV(DAYLM2)) THEN
            CALL WINCH$(102,STATUS)
            ERSTAT = 10
            IF (STATUS.EQ.99) ERSTAT = 99
            RETURN
         ENDIF
C
C       COMPUTE THE DIFFERENCE BETWEEN THE COMPUTED SURFACE TEMPERATURES
C       FROM HIRS CHANNELS 18 AND 8 TO USE FOR THE DAYLIGHT TEST.
C
         IF ((.NOT. ISR4SPV(TSFC8)) .AND. (.NOT. ISR4SPV(HIRS8)) .AND.
     +      (.NOT. OVRLND) .AND. (ICEFRE) .AND. (SNWFRE)
     +      .AND. HIRANC(SPOT,LINE,8).LE.1500.) THEN
            DIFF5 = ABS (TSFC8 - HIRS8)
         ELSE IF ((.NOT. ISR4SPV(TSFC8)) .AND. (.NOT. ISR4SPV(HIRS8)) .AND.
     +      ((OVRLND) .AND. (ICEFRE) .AND. (SNWFRE))
     +      .AND. HIRANC(SPOT,LINE,8).LE.1500.) THEN
            DIFF5 = ABS (TSFC8 - HIRS8) + (TS8LIM - TS8LM2)
            IF (ABS (HIRANC(SPOT,LINE,1)) .LE. TROPLT) THEN
               IF(DIFF5 .LE. TROPDF)THEN
                  DIFF5 = 0.
               ELSE
                  DIFF5 = DIFF5 - TROPDF
               ENDIF
            ENDIF
         ELSE IF ((.NOT. ISR4SPV(TSFC8)) .AND. (.NOT. ISR4SPV(HIRS8)) .AND.
     +      (.NOT. ICEFRE)
     +      .AND. HIRANC(SPOT,LINE,8).LE.1500.) THEN
            DIFF5 = ABS (TSFC8 - HIRS8) + (TS8LIM - TS8LM3)
            IF (ABS (HIRANC(SPOT,LINE,1)) .LE. TROPLT) THEN
               IF(DIFF5 .LE. TROPDF)THEN
                  DIFF5 = 0.
               ELSE
                  DIFF5 = DIFF5 - TROPDF
               ENDIF
            ENDIF
         ELSE IF ((.NOT. ISR4SPV(TSFC8)) .AND. (.NOT. ISR4SPV(HIRS8)) .AND.
     +      (.NOT. SNWFRE)
     +      .AND. HIRANC(SPOT,LINE,8).LE.1500.) THEN
            DIFF5 = ABS (TSFC8 - HIRS8) + (TS8LIM - TS8LM4)
            IF (ABS (HIRANC(SPOT,LINE,1)) .LE. TROPLT) THEN
               IF(DIFF5 .LE. TROPDF)THEN
                  DIFF5 = 0.
               ELSE
                  DIFF5 = DIFF5 - TROPDF
               ENDIF
            ENDIF
         ELSE IF ((.NOT. ISR4SPV(TSFC8)) .AND. (.NOT. ISR4SPV(HIRS8)) 
     +      .AND. HIRANC(SPOT,LINE,8).GT.1500.) THEN
            DIFF5 = ABS (TSFC8 - HIRS8) + (TS8LIM - TS8LM5)
            IF (ABS (HIRANC(SPOT,LINE,1)) .LE. TROPLT) THEN
               IF(DIFF5 .LE. TROPDF)THEN
                  DIFF5 = 0.
               ELSE
                  DIFF5 = DIFF5 - TROPDF
               ENDIF
            ENDIF
         ELSE
            DIFF5 = R4SPV
         ENDIF
         IF ((.NOT. ISR4SPV(TSFC18)) .AND. (.NOT. ISR4SPV(TSFC8))) THEN
            DIFF1 = TSFC18 - TSFC8
            DIFF2 = TSFC8 - TSFC18
         ELSE
            DIFF1 = R4SPV
            DIFF2 = R4SPV
         ENDIF
C
C       IF THE DIFFERENCE IS GREATER THAN THE TEST LIMIT, THE HIRS
C       SPOT IS CLOUDY.
C
         IF (((.NOT. ISR4SPV(DIFF1)) .AND. (DIFF1 .GT. DAYLIM))
     +      .OR.((.NOT. ISR4SPV(DIFF2)) .AND. (DIFF2 .GT. DAYLM2))
     +      .OR.((.NOT. ISR4SPV(DIFF5)) .AND. (DIFF5 .GT. TS8LIM))) THEN
            RESULT = 0
         ELSE
            RESULT = 1
         ENDIF
      ENDIF
C
      RETURN
      END
C
C**********************************************************************
      SUBROUTINE WINCH$(MSGCOD,STAT)
C
C**********************************************************************
C
C   NAME- WINCH$
C
C   LANGUAGE- VS FORTRAN  TYPE- SUBROUTINE      COMPUTER- CRAY J916
C
C   VERSION- 1.0    DATE- 02/08/93    PROGRAMMER- CARLA NEIGH (SMSRC)
C   VERSION- 3.0    DATE- 09/22/97    PROGRAMMER- LEAH WILSON (HSTX)
C
C   FUNCTION- THIS SUBROUTINE WRITES OUT ERROR MESSAGES TO THE
C             KIDS MESSAGE LOG FILE FOR THE SUBROUTINE WINCHN.
C   VERSION- 3.0 CONVERSION FROM MMLF TO KIDS FILE.
C
C   FILES-
C     DSN OR DESCRIPTIVE TITLE I/O   UNIT  FUNCTIONAL DESCRIPTION
C     ------------------------ ---   ----  ----------------------
C     ...KIDS                   O   KUNIT  KRAY INTERACTIVE DOCUMENTATION
C                                          SYSTEM.
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE          TYPE I/O  FUNCTION
C     --------          ---- ---  --------
C     MSGCOD             INT  I   MESSAGE CODE OF ERROR DETECTED.
C     STAT               INT  O   ERROR STATUS CODE
C                                  = 0 IF NO ERROR OCCURRED,
C                                 <> 0 IF ERROR OCCURRED.
C
C   COMMON AREA PARAMETERS-
      INCLUDE 'SPGUNT'
      INCLUDE 'ERPARM'
      INCLUDE 'ERINFO'
C
C   LOCAL PARAMETERS-
C     VARIABLE          TYPE  FUNCTION
C     --------          ----  --------
C     NAME              CH*6  MAIN ROUTINE NAME.
C     C4TEMP            CH*4  DUMMY FOR WRITING TO KIDS FILE.
C     I                  INT  DO LOOP CONTROL INDEX.
C     COUNT              INT  COUNTER FOR NUMBER OF TIMES $-ROUTINE
C                             HAS BEEN INVOKED.
C     KMSG             CH*100 KIDS MESSAGE BUFFER
C
C   SUBPROGRAMS CALLED-  KIDS 
C
C***********************************************************************
C
      CHARACTER*4 C4TEMP
      CHARACTER*6 NAME
      CHARACTER*100 KMSG
      INTEGER   STAT, I, MSGCOD, COUNT(2)
      DATA COUNT /0,0/
      SAVE COUNT
C
C
C   SET THE MAIN NAME.
C
      SUBNAM = 'SPGDVR'
      STAT = 0
C
C     CHECK FOR VALID MESSAGE CODES.
C
      IF (MSGCOD .EQ. 101) THEN
        COUNT(1) = COUNT(1) + 1
        IF (COUNT(1).GT.1) RETURN
        KMSG(1:51)='* WARNING * Window channel test could not be done '
        KMSG(52:100)='over nighttime areas since test limit is missing.'
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C
      ELSEIF (MSGCOD .EQ. 102) THEN
        COUNT(2) = COUNT(2) + 1
        IF (COUNT(2).GT.1) RETURN
        KMSG(1:51)='* WARNING * Window channel test could not be done '
        KMSG(52:100)='over daytime areas since test limit is missing.'
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C
      ELSE
C
C NOT A VALID MSGCOD
C
         STAT=0
         KMSG = ' Invalid error message code = '
         WRITE(C4TEMP,'(I4)') MSGCOD
         KMSG(31:34) = C4TEMP
         CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
      ENDIF
      RETURN
      END

