      SUBROUTINE AVG1RD(RADINC,RNGITT,NUMCHN,NUMSPT,ITEMID,RETCOD)
      IMPLICIT NONE
C
C**********************************************************************
C
C   NAME- AVG1RD
C
C   LANGUAGE- FORTRAN 77  TYPE- SUBROUTINE      COMPUTER- CRAY
C
C   VERSION- 1.0    DATE- 07/19/95   PROGRAMMER- DIANE BEITZELL (SMSRC)
C   VERSION- 2.0    DATE- 01/30/96   PROGRAMMER- ROBERT FELDMAN (SMSRC)
C      ATOVS- 301:  CHANGED SELECT_STATUS_3A_3B TO CHANNEL3_INDICATOR
C   VERSION- 2.1    DATE- 02/26/96   PROGRAMMER- DAVE MOREL (SMSRC)
C      ATOVS- 332:  ADDED ARGUMENT RNGITT.  ALSO ADDED A CALL TO AVG1I2
C             TO PULL OUT THE INTERNAL TARGET DATA COUNTS AND A CALL
C             TO RAD2BT TO COMPUTE KELVIN TEMPERATURES BEFORE STORING
C             THEM IN CELSIUS.
C   VERSION- 3.0    DATE- 05/14/96   PROGRAMMER- ALEX POZDNYAK (FDC)
C      FOR REHOSTING TO THE CRAY, CHANGED ALL I*4 DECLARATIONS TO
C      INTEGER AND ALL R*4 DECLARATIONS TO REAL.
C   VERSION- 3.1    DATE- 11/14/97   PROGRAMMER- LEAH WILSON (HSTX)
C      DUE TO GAC 1B* STRUCTURE CHANGES, CHANGE INTERNAL_TARGET_DATA
C      TO CALIBRATION_TARGET_DATA, AND DIMENSION OF TARGET FROM 3X10
C      TO 10X3 - ALSO MODIFY ALL REFERENCE TO TARGET TO REFLECT THIS.
C   VERSION- 3.2    DATE- 04/03/98   PROGRAMMER- LEAH WILSON (RSTX)
C      CHANGE EQUATIONS THAT APPLY IRCOEF AND VSCOEF DUE TO DIFFERENCE
C      IN COEFFICIENT ORDER BETWEEN SIMULATED AND ACTUAL NOAA-K DATA.
C   VERSION- 3.3    DATE- 06/12/98   PROGRAMMER- LEAH WILSON (RSTX)
C      CORRECT VALUE OF ABFLAG.  SHOULD BE ABFLAG=0 -> 3B AND ABFLAG=1
C      -> 3A.  DETERMINE IF UNKNOWN LINES ARE 3A OR 3B BY AVERAGING
C      CH. 3 COUNTS AND COMPARING TO LIMITS.
C   VERSION- 3.4    DATE- 08/07/98   PROGRAMMER- LEAH WILSON (RSTX)
C       ADD ITEMID TO CALLING LIST FOR CORRECT ERROR MESSAGING.
C   VERSION- 3.5    DATE- 07/23/03   PROGRAMMER- F. TILLEY (RAYTHEON)
C       UNDER WR #230 (NOAA-N READINESS) CHANGED THE CALL FOR
C       CALIBRATION COEFFICIENTS FROM AVG1R4 TO AVG1R8 AND EXPLICITLY
C       DECLARED THE VSCOEF AND IRCOEF ARRAYS AS REAL*8. CHANGED
C       SHAPE FROM INTEGER TO INTEGER*4. SUBSTITUTED I*4 'LENGTH'
C       FOR DATA LENGTH IN 1B* UTILITY CALLS. CHANGED 'RETCOD' TO
C       I*4 'STATUS' IN 1B* UTILITY CALLS.
C   VERSION- 3.6    DATE- 09/21/04   PROGRAMMER- F. TILLEY (RAYTHEON)
C       CHANGED AVG1R8 BACK TO AVG1R4 AND CHANGED VSCOEF AND IRCOEF
C       DECLARATIONS BACK TO DEFAULT REAL FROM REAL*8. ADDED DECLARATIONS
C       FOR VSCOE4 AND IRCOE4 FOR EXTRACTING REAL*4 CALIBRATION 
C       COEFFICIENTS FROM THE 1B*.
C
C   FUNCTION-  THIS SUBROUTINE RETRIEVES THE AVHRR GAC DATA COUNTS
C              AND CALIBRATION COEFFICIENTS AND OUTPUTS THE RADIANCES
C              FOR A GAC SCAN LINE.
C
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE          TYPE I/O  FUNCTION
C     --------          ---- ---  --------
C     RADINC            REAL O    ARRAY CONTAINING THE RADIANCES.
C                                 DIMENSIONED NUMCHN X NUMSPT.
C
C     RNGITT            REAL O    VECTOR (LENGTH 3) CONTAINING THE INTER
C                                 TARGET DATA.
C
C     NUMCHN            INT  I    THE NUMBER OF INPUT CHANNELS.
C
C     NUMSPT            INT  I    THE NUMBER OF SPOTS.
C
C     ITEMID           CH*32 O    THE DESIRED DATA ITEM NAME HELD IN THE
C                                 AVHRR 1B* DATA SET RECORD.
C
C     RETCOD            INT  O    ERROR STATUS RETURN CODE FROM 1B* UTIL
C
C   COMMON AREA PARAMETERS-
      INCLUDE 'SPVAL'
C
C   LOCAL PARAMETERS-
C     VARIABLE          TYPE  FUNCTION
C     --------          ----  --------
C     ABFLAG            INT   FLAG THAT INDICATES WHETHER CHANNEL 3A
C                             OR CHANNEL 3B IS BEING USED.
C                             (0=3B, 1=3A, -1=UNKNOWN)
C
C     COUNT             I*2   ARRAY OF COUNTS. DIMENSIONED NUMCHN X
C                             NUMSPT.
C
C     COUNTS            INT   DEFAULT INTEGER SIZE ARRAY OF COUNTS.
C
C     I,J               INT   LOOP INDEXES.
C
C     INT1VL            I*1   VARIABLE FOR EXTRACTING I*1 VALUES FROM 1B*
C
C     IRCOEF            REAL  ARRAY CONTAINING CALIBRATION
C                             COEFFICIENTS FOR CHANNELS 3B, 4, AND 5.
C                             DIMENSIONED 3 (COEFF) X 3 (IR CHANNELS).
C                             FIRST COEFFICIENT IS THE ZEROTH ORDER
C                             TERM, SECOND COEFFICIENT IS THE
C                             FIRST ORDER TERM, AND THE THIRD
C                             COEFFICIENT IS THE SECOND ORDER TERM.
C
C     IRCOE4            R*4   FOUR-BYTE VERSION OF IRCOEF.
C     LENGTH            I*4   DATA LENGTH FOR 1B* UTILITY CALLS.
C
C     LLL               INT   LOOP INDEX.
C
C     RAVG3             REAL  AVERAGE OF CHANNEL 3 COUNTS.
C
C     RSUM3             REAL  SUM OF CHANNEL 3 COUNTS.
C
C     SHAPE             I*4   VECTOR OF LENGTH 8 TO  HOLD THE
C                             SHAPE OF R4RAY. (DIMENSION 0:7).
C                             THE ZERO'TH ELEMENT IS THE
C                             DIMENSION OF THE ARRAY.  THE
C                             NEXT ELEMENT IS THE EXTENT OF
C                             THE FIRST DIMENSION.  THE NEXT
C                             ELEMENT IS THE EXTENT OF THE
C                             SECOND DIMENSION, ETC.
C
C     STATUS            I*4   RETURN CODE FROM 1B* UTILITIES.
C
C     TARGI2            I*2   ARRAY FOR PULLING OUT INTERNAL TARGET
C                             COUNTS FROM THE 1B*.
C 
C     VSCOEF            REAL  ARRAY CONTAINING CALIBRATION COEFFICIENTS
C                             FOR CHANNELS 1, 2, AND 3A. DIMENSIONED
C                             5 (COEFF) X 3 (VISIBLE CHANNELS). FIRST
C                             TWO COEFFICIENTS ARE THE INTERCEPT AND
C                             SLOPE FOR COUNTS LESS THAN LAST
C                             COEFFICIENT.
C                             SECOND TWO COEFFICIENTS ARE THE INTERCEPT
C                             AND SLOPE FOR COUNTS GREATER THAN OR 
C                             EQUAL TO LAST COEFFICIENT.
C     VSCOE4            R*4   FOUR-BYTE VERSION OF VSCOEF.
C
C
C     SUBPROGRAMS CALLED- AVG1R4,AVG1I1,AVG1I2,RAD2BT
C
C     FUNCTIONS USED- FLOAT
C
C     EXIT STATES-ROUTINE WILL RETURN WITH RETURN_CODE OF 0 IF EXECUTED
C                 CORRECTLY.  OTHERWISE, ROUTINE WILL RETURN PREMATURELY
C                 WITH RETURN_CODE EQUAL TO THAT OF THE 1B* UTILITY IN
C                 WHICH ERROR OCCURRED.
C
C*********************************************************************
C
      INTEGER*4  SHAPE(0:7), LENGTH, STATUS
      INTEGER  I,J,NUMCHN,NUMSPT,RETCOD
C     INTEGER  COUNTS(NUMCHN,NUMSPT),TARGET(3,10)
      INTEGER  COUNTS(5,409),TARGET(10,3)
      INTEGER  ABFLAG, LLL
      INTEGER*1 INT1VL
      INTEGER*2 COUNT(5,409), TARGI2(10,3)
C
      REAL     RADINC(NUMCHN,NUMSPT),RNGITT(3),TEMPR(3)
      REAL     RAVG3, RSUM3
      REAL     VSCOEF(5,3),IRCOEF(3,3)
      REAL*4   VSCOE4(5,3),IRCOE4(3,3)
C
      CHARACTER*32 ITEMID
C
C  INITIALIZE SHAPE ARRAY.
C
      DO 10 I = 1,7
         SHAPE(I) = 0
   10 CONTINUE
C
C  GET COUNTS AND CALIBRATION COEFFICIENTS FOR THIS SCAN LINE.
C
      SHAPE(0) = 1
      SHAPE(1) = 1
      ITEMID = 'CHANNEL3_INDICATOR              '
      LENGTH = 1
      CALL AVG1I1(ITEMID,INT1VL,LENGTH,SHAPE,STATUS)
      RETCOD = STATUS
      IF (RETCOD .NE. 0) RETURN
      ABFLAG = INT1VL
C
      ITEMID = 'OBSERVATIONS                    '
      SHAPE(0) = 2
      SHAPE(1) = NUMCHN
      SHAPE(2) = NUMSPT
      LENGTH = NUMCHN*NUMSPT
      CALL AVG1I2(ITEMID,COUNT,LENGTH,SHAPE,STATUS)
      RETCOD = STATUS
      IF (RETCOD .NE. 0) RETURN
      DO I = 1, NUMCHN
          DO J = 1, NUMSPT
             COUNTS(I,J) = COUNT(I,J)
          END DO
      END DO 
      
C
C   IF THE 3A/3B SWITCH DOES NOT IDENTIFY THIS AS 3A OR 3B, THEN
C   AVERAGE THE CHANNEL 3 COUNTS.  IF THE AVERAGE IS IN THE RANGE
C   0-450, CONSIDER IT 3A AND CHANGE THE ABFLAG.  IF THE AVERAGE
C   EXCEEDS 700, CONSIDER IT 3B AND CHANGE THE ABFLAG.  IF THE
C   AVERAGE IS MISSING, LEAVE THE ABFLAG ALONE.
C
      IF(ABFLAG.NE.1.AND.ABFLAG.NE.0) THEN
         RSUM3 = 0.0
         RAVG3 = 0.0
         DO LLL=1,NUMSPT
            RSUM3 = RSUM3 + COUNTS(3,LLL)
         ENDDO
         RAVG3 = RSUM3/NUMSPT
         IF(RAVG3.GE.0.AND.RAVG3.LE.450.0) THEN
            ABFLAG = 1
            PRINT *, 'RAVG3 = ',RAVG3,' guessing it is 3A (0-450)'
         ELSEIF(RAVG3.GE.700.0) THEN
            ABFLAG = 0
            PRINT *, 'RAVG3 = ',RAVG3,' guessing it is 3B (700+)'
         ELSE
            PRINT *, 'RAVG3 = ',RAVG3,' cannot determine 3A/3B'
         ENDIF
      ENDIF
C
      ITEMID = 'VIS_CALIBRATION_COEFF_ACTIVE    '
      SHAPE(1) = 5
      SHAPE(2) = 3
      LENGTH = 5*3
      CALL AVG1R4(ITEMID,VSCOE4,LENGTH,SHAPE,STATUS)
      RETCOD = STATUS
      IF (RETCOD .NE. 0) RETURN
C
C  CONVERT VIS CALIBRATION COEFFICIENTS FROM R*4 TO R*8
C
      DO I = 1, 5
         DO J = 1, 3
            VSCOEF(I,J) = VSCOE4(I,J)
         END DO
      END DO
 
C
      ITEMID = 'IR_CALIBRATION_COEFF_PRIMARY    '
      SHAPE(1) = 3
      SHAPE(2) = 3
      LENGTH = 3*3
      CALL AVG1R4(ITEMID,IRCOE4,LENGTH,SHAPE,STATUS)
      RETCOD = STATUS
      IF (RETCOD .NE. 0) RETURN
C
C  CONVERT IR CALIBRATION COEFFICIENTS FROM R*4 TO R*8
C
      DO I = 1, 3
         DO J = 1, 3
            IRCOEF(I,J) = IRCOE4(I,J)
         END DO
      END DO
C
      ITEMID = 'CALIBRATION_TARGET_DATA         '
      SHAPE(1) = 10
      SHAPE(2) = 3
      LENGTH = 30
      CALL AVG1I2(ITEMID,TARGI2,LENGTH,SHAPE,STATUS)
      RETCOD = STATUS
      IF (RETCOD .NE. 0) RETURN
      DO I = 1, 10
         DO J = 1, 3
            TARGET(I,J) = TARGI2(I,J)
         END DO
      END DO 
C
C  CONVERT FROM COUNTS TO RADIANCES.
C
      DO 20 J = 1, 3
        DO 20 I = 1, NUMSPT
         IF(J.LT.3 .OR. ABFLAG.EQ.1) THEN
          IF(COUNTS(J,I) .LT. VSCOEF(5,J)) THEN
           RADINC(J,I) = VSCOEF(2,J) + VSCOEF(1,J)*FLOAT(COUNTS(J,I))
          ELSE
           RADINC(J,I) = VSCOEF(4,J) + VSCOEF(3,J)*FLOAT(COUNTS(J,I))
          ENDIF
         ENDIF
   20 CONTINUE
C
      DO 30 J = 3, 5
       DO 30 I = 1, NUMSPT
        IF(J.GT.3 .OR. ABFLAG.EQ.0) THEN
         RADINC(J,I) = IRCOEF(3,J-2) + IRCOEF(2,J-2)*FLOAT(COUNTS(J,I))
     &                        + IRCOEF(1,J-2)*FLOAT((COUNTS(J,I)**2))
        ELSEIF(J.EQ.3 .AND. ABFLAG.EQ.-1) THEN
         RADINC(J,I) = R4SPV
        ENDIF
   30 CONTINUE
C
      DO 50 J = 1, 3
       RNGITT(J) = 0.0
       DO 40 I = 1, 10
          RNGITT(J) = RNGITT(J) + TARGET(I,J)
   40  CONTINUE
       RNGITT(J) = RNGITT(J) / 10.0
       TEMPR(J) = IRCOEF(3,J) + IRCOEF(2,J) * RNGITT(J)
       CALL RAD2BT(4,J+3,RNGITT(J),TEMPR(J),1)
       RNGITT(J) = RNGITT(J) - 273.15
   50 CONTINUE
C
      RETURN
      END
