      SUBROUTINE BINDVR
      IMPLICIT NONE
C***********************************************************************
C
C   NAME- BINDVR
C
C   LANGUAGE- VS FORTRAN    TYPE- SUBROUTINE    COMPUTER-  CRAY J916
C
C   VERSION- 1.0    DATE- 7/19/93    PROGRAMMER- CARLA A. NEIGH (SMSRC)
C   VERSION- 2.0    DATE- 05/05/94   PROGRAMMER- FRANK TILLEY (SMSRC)
C                XATOV-1 REPLACE COMMON BLOCK /RETGRD/ WITH PARAMETER
C                FILE RTVPARM AND COMMON BLOCK /RTVGRD/.  REPLACE
C                COMMON BLOCK /BUFFER/ WITH PARAMETER FILE INSPARM
C                AND COMMON BLOCK /INSCOM/.
C   VERSION- 3.0    DATE- 05/31/96   PROGRAMMER- TIM GARDNER (HSTX)
C   VERSION- 3.1    DATE- 06/03/96   PROGRAMMER- TIM GARDNER (HSTX)
C   VERSION- 3.2    DATE- 04/20/97   PROGRAMMER- LEAH WILSON (RSTX)
C   VERSION- 3.3    DATE- 03/10/00   PROGRAMMER- LEAH WILSON (RAYTHEON)
C   VERSION- 3.4    DATE- 02/27/03   PROGRAMMER- Rickey Rollins (CSC)
C
C   FUNCTION- THIS IS THE DRIVER ROUTINE FOR DETERMINING THE RETRIEVAL
C             OPERATOR CLASS NUMBERS FOR A DATA FRAME.  THE CLASS NUMBER
C             IS PLACED IN THE RETRIEVAL ANCILLARY BUFFER IN THE
C             POSITION FOR THE CLASS ID.
C
C   VERSION 3.0- DOCUMENTATION AND SYNTAX CHANGED FOR ATOVS BASELINE
C                ON THE CRAY.
C   VERSION 3.1- MODIFICATIONS TO PLACE THE CLASS ID IN THE CORRECT
C                LOCATION OF THE HIRANC BUFFER.
C   VERSION 3.2- ADD /MONSTA/ TO COUNT NUMBER OF BINS = 0 (I.E. NO-HIRS)
C   VERSION 3.3- COUNT NUMBER OF BINS = 0 (I.E. NO-HIRS) PER FRAME ALSO.
C                ADD /FRMINF/ TO GET FRAME NUMBER.
C   VERSION 3.4-     Replace R4SPV equivalence test with ISR4SPV function  
C
C   COMMON AREA PARAMETERS- RTVPARM, RTVGRD, INSPARM, INSCOM, SPVAL, FRMINF
C
C   LOCAL PARAMETERS-
C   NAME    TYPE   DESCRIPTION
C   ----    ----   -----------
C   LAT     REAL   LATITUDE OF RETRIEVAL GRID POINT.
C   SOLZEN  REAL   SOLAR ZENITH ANGLE OF RETRIEVAL GRID POINT.
C   R4CLAS  REAL   REAL EQUIVALENT OF CLASS ID.
C   LINE    INT    DO LOOP INDEX FOR LINE.
C   GRDPNT  INT    DO LOOP INDEX FOR GRID POINT.
C   HSPTNO  INT    HIRS SPOT NUMBER FROM RETRIEVAL ANCILLARY BUFFER.
C   HBFLIN  INT    HIRS LINE NUMBER IN HIRS DATA BUFFER.
C   HBFSPT  INT    HIRS SPOT NUMBER IN HIRS DATA BUFFER.
C   TERAIN  INT    TERRAIN FLAG.
C   OPCLS   INT    RETRIEVAL OPERATOR COMPONENT CLASS NUMBER.
C   I2CLAS  INT    ARRAY (DIM 2) CONTAINING THE CLASS ID TO PLACE
C                  IN THE RETRIEVAL ANCILLARY BUFFER.  THE FIRST
C                  TWO BYTES CONTAIN THE IDENTIFICATION NUMBER FOR
C                  THE RETRIEVAL OPERATOR CLASS.  THE SECOND TWO BYTES
C                  ARE CURRENTLY FILLED WITH ZERO.
C
C   SUBPROGRAMS CALLED- RETCLS
C
C   FUNCTIONS USED- NINT, MOD
C
C***********************************************************************
C
      INCLUDE'INSPARM'
      INCLUDE'RTVPARM'
      INCLUDE'INSCOM'
      INCLUDE'RTVGRD'
      INCLUDE'MONSTA'
C
      REAL LAT, SOLZEN, RCLASS
      INTEGER LINE, GRDPNT, HSPTNO, HBFLIN, HBFSPT, TERAIN, OPCLS
      LOGICAL ISR4SPV
C
      INCLUDE'SPVAL'
      INCLUDE'FRMINF'
C
      EQUIVALENCE(OPCLS,RCLASS)
C
C   LOOP OVER ALL GRID POINTS IN RETRIEVAL DATA BUFFER.
C
      DO 20 LINE = 1, RTOTLN
         DO 10 GRDPNT = 1, RNGRDP
C
C         EXTRACT THE HIRS SPOT NUMBER FROM THE RETRIEVAL ANCILLARY
C         BUFFER.  IF THE HIRS SPOT NUMBER IS MISSING, SKIP OVER
C         THE RETRIEVAL GRID POINT AND LEAVE THE CLASS ID MISSING.
C
           IF (ISR4SPV(RETANC(GRDPNT,LINE,1))) GOTO 10
C
           HSPTNO = NINT(RETANC(GRDPNT,LINE,1))
C
           HBFLIN = (HSPTNO / HNSPOT) + 1
           HBFSPT = MOD(HSPTNO,HNSPOT)
           IF (HBFSPT .EQ. 0) THEN
              HBFLIN = HBFLIN - 1
              HBFSPT = HNSPOT
           ENDIF
C
C         CURRENTLY, THE COEFFICIENTS NEEDED TO MAKE A NOHIRS RETRIEVAL
C         DO NOT EXIST.  IF THE RETRIEVAL FLAG INDICATES A NOHIRS
C         RETRIEVAL, THE CLASS ID IS SET TO ZERO SO THAT THE CHANNEL
C         DATA STILL GOES TO THE ODB BUT THE RETRIEVAL IS NOT DONE.
C
            IF (NINT(RETANC(GRDPNT,LINE,4))/16 .EQ. 3) THEN
               RETANC(GRDPNT,LINE,5) = 0.0
               BINRET0 = BINRET0 + 1
               GOTO 10
            ENDIF
C
C         EXTRACT THE LATITUDE, TERRAIN FLAG, AND SOLAR ZENITH ANGLE
C         FROM THE HIRS ANCILLARY BUFFER.
C
           LAT = HIRANC(HBFSPT,HBFLIN,1)
           TERAIN = HIRANC(HBFSPT,HBFLIN,9)
           SOLZEN = HIRANC(HBFSPT,HBFLIN,3)
C
           CALL RETCLS(LAT,TERAIN,SOLZEN,OPCLS)
C
C         PLACE THE RETRIEVAL OPERATOR CLASS NUMBER IN THE RETRIEVAL
C         ANCILLARY BUFFER IN THE POSITION FOR THE CLASS ID IN THE
C         SECOND TWO-BYTES OF THE WORD.
C
           OPCLS = ISHFT(OPCLS,16)
           RETANC(GRDPNT,LINE,5) = RCLASS
c          print *,'RCLASS for GRDPNT ',GRDPNT,' LINE ',LINE,' = ',RCLASS
C
   10    CONTINUE
   20 CONTINUE
C
      RETURN
      END
