      SUBROUTINE INPDVR(FSTFRM,SYNCTR,SYNTIM,OBSHIR,ERSTAT,N16HIR,
     +                  SCNHIR,SCNAMA,SCNAMB,INSTON,FULFRM,METOP)
      IMPLICIT NONE
C
C***********************************************************************
C    NAME- INPDVR
C
C    LANGUAGE- FORTRAN 77     TYPE- SUBROUTINE    COMPUTER- CRAY
C
C    VERSION- 1.0    DATE- 2/18/91    PROGRAMMER- CARLA NEIGH (SMSRC)
C    VERSION- 2.0    DATE- 05/06/94   PROGRAMMER- FRANK TILLEY (SMSRC)
C           XATOV-3: REPLACE COMMON BLOCK /BUFFER/ WITH PARAMETER
C                    FILE INSPARM AND COMMON BLOCK /INSCOM/.
C    VERSION- 2.1 DATE- 05/25/94   PROGRAMMER- SURYA JANDHYALA (SMSRC)
C           XATOV-6   *** REPLACED LOCAL VARIABLE DIMENSIONS    ***
C                     *** WITH PARAMETERS FROM THE INSTRUMENT   ***
C                     *** PARAMETER FILE 'INSPARM'.             ***
C    VERSION- 2.2     DATE- 12/8/95   PROGRAMMER- CHARLES A. DEAN (SMSRC
C           ATOVS-276: ALL REFERENCES TO MSU AND SSU ARE CHANGED TO
C                     AMSU-A AND AMSU-B.  REPLACED CALL TO INMSU WITH
C                     INAMA.  REMOVED CALL FOR SSU INPUT AND ADDED
C                     COMMENT ABOUT FUTURE AMSU-B INPUT.  CALLS TO
C                     AVGCAL AND FILFRM REMOVED.  CALLING ARGUMENTS TO
C                     ROUTINES UPDATED.
C    VERSION- 2.3     DATE- 02/12/96   PROGRAMMER- DAVE MOREL (SMSRC)
C           ATOVS-337: ADDING LOGICAL FIRST FOR THE CALL TO INGAC.
C                     ALSO DELETING THE USE OF LOGICAL NOTPRS.
C    VERSION- 3.0    DATE- 05/09/96   PROGRAMMER- ALEX POZDNYAK (FDC)
C      MODIFIED TO RUN ON THE CRAY.  REPLACED I*4 WITH INT, L*4 WITH
C      LOG AND R*4 WITH REAL.
C    VERSION- 3.1    DATE- 07/06/96   PROGRAMMER- TIM GARDNER (HSTX) 
C                     LOGICAL VALUES FIRST AND FEOF WERE ADDED TO 
C                     A DATA STATEMENT RATHER THAN BEING SET EACH TIME
C                     THE ROUTINE IS CALLED. 
C    VERSION- 3.2    DATE- 06/19/97   PROGRAMMER- LEAH WILSON (HSTX) 
C                     ADD OBSHIR TO CALL TO INHIRS FOR OBTAINING NODE.
C    VERSION- 3.3    DATE- 01/08/99   PROGRAMMER- LEAH WILSON, RAYTHEON
C                     ADD ORIGINAL HIRS SYNCTR TO CALL TO INAMA.
C    VERSION- 3.4    DATE- 01/26/99   PROGRAMMER- LEAH WILSON, RAYTHEON
C                     ADD USE_CAL_PROB FLAG FOR PASSING TO INHIRS.
C    VERSION- 3.5    DATE- 05/19/99   PROGRAMMER- BRIAN BARIL, RAYTHEON
C                     ADDED CODE TO SET ALL NODE VALUES TO ZERO FOR EACH 
C                     FRAME.
C    VERSION- 3.6    DATE- 03/10/00   PROGRAMMER- LEAH WILSON, RAYTHEON
C                     REMOVE USE_CAL_PROB AND FIRST_SYNCTR. MODIFY CALL
C                     TO CHKFRM FOR REMOVAL OF 1B*-RELATED FLAGS.  ADD
C                     GOOD2CAL FOR INHIRS AND INAMA.
C    VERSION- 3.7    DATE- 08/04/00   PROGRAMMER- FRANK TILLEY, RAYTHEON
C                     FOR WR #100 ADDED COMMON /NOHIRSCF/ TO PICK UP
C                     THE NOH1BS FLAG THAT IS SET TO TRUE WHEN NO HIRS
C                     1B* FILE IS AVAILABLE.
C    VERSION- 3.8    DATE- 12/15/00   PROGRAMMER- FRANK TILLEY, RAYTHEON
C                     ADDED CALLING ARGUMENT 'N16HIR' TO PASS TO
C                     SUBROUTINE 'INHIRS' TO SHIFT THE HIRS BRIGHTNESS
C                     TEMPERATURES ONE SPOT TO THE RIGHT FOR EACH
C                     SCAN LINE TO CORRECT FOR INSTRUMENT MISALIGNMENT.
C                     (WR #131)
C    VERSION- 3.9    DATE- 03/02/01  PROGRAMMER- FRANK TILLEY (RAYTHEON)
C                     ADDED ARGUMENT GOOD2CAL TO THE CALL TO INGAC.
C                     (WR #136)
C    VERSION- 3.10   DATE- 12/31/01   PROGRAMMER- FRANK TILLEY, RAYTHEON
C                     REPLACED CALLS TO CMXADJ TO CMXADH FOR HIRS AND
C                     CMAADJ FOR AMSU-A. (PR #55)
C    VERSION- 5.0    DATE- 05/30/01  PROGRAMMER- FRANK TILLEY (RAYTHEON)
C                     FOR WR #22, MODIFIED INPDVR FOR INCLUSION OF
C                     AMSU-B DATA.  REPLACED CALL TO INAMA WITH A CALL
C                     TO INAMSU.  INCLUDED 'SPVAL'. CHANGED THE 
C                     DIMENSION OF FSTFRM FROM (3) TO (4).
C    VERSION- 5.1    DATE- 01/15/04  PROGRAMMER- F. TILLEY (RAYTHEON)
C                     REPLACED INAMSU WITH INAMA AND ADDED A CALL TO
C                     INAMB TO BRING IN AMSU-B DATA AT FULL RESOLUTION.
C    VERSION- 5.2    DATE- 12/02/04  PROGRAMMER- F. TILLEY (RAYTHEON)
C                     UNDER WR #230 (NOAA-N READINESS) ADDED SCNHIR, SCNAMA,
C                     AND SCNAMB TO THE CALLING ARGUMENTS AND TO THE CALLING
C                     ARGUMENTS TO INHIRS, INAMA, AND INAMB. 
C    VERSION- 5.3    DATE- 02/06/06  PROGRAMMER- F. TILLEY (ERT)
C                     UNDER WR #300, ADDED CALLING ARGUMENT 'FULFRM'. THIS
C                     LOGICAL FLAG IS SET TO .TRUE. BEFORE CALLING SUBROUTINE
C                     INHIRS. SUBROUTINE INHIRS SETS THE FLAG TO .FALSE.
C                     FOR PARTIAL DATA FRAMES AT THE BEGINNING AND END OF
C                     A 1B* FILE.
C
C    FUNCTION- THIS SUBROUTINE ACTS AS THE DRIVER ROUTINE FOR INPUT OF
C              DATA FOR ONE 256 SECOND DATA FRAME.  TWO BUFFERS FOR
C              EACH OF THE 3 SOUNDINGS INSTRUMENTS (HIRS, AMSU-A, AMSU-B
C              LOCATED IN COMMON /INSCOM/ ARE FILLED. ONE OF THE BUFFERS
C              IS AN EXTENDED DATA FRAME BUFFER (256 SECOND DATA FRAME
C              PLUS AT LEAST 32 SECONDS OF THE PREVIOUS FRAME AND 32
C              SECONDS OF THE SUBSEQUENT FRAME) CONTAINING BRIGHTNESS
C              TEMPERATURES.  THE OTHER BUFFER IS AN EXTENDED DATA
C              FRAME CONTAINING ANCILLARY DATA FOR THE INSTRUMENT.
C
C    CALLING SEQUENCE PARAMETERS-
C      VARIABLE  TYPE  I/O  DESCRIPTION
C      --------  ----  ---  -----------
C      FSTFRM    LOG    I   ARRAY OF FLAGS FOR EACH SOUNDING INSTRUMENT
C                           SET TO TRUE IF DATA FRAME IS FIRST FRAME
C                           INPUT FOLLOWING SYNCHRONIZATION.
C      SYNCTR    INT    I   ARRAY OF NUMBER OF MISSING LINES FOR EACH
C                           SOUNDING INSTRUMENT TO BE INSERTED INTO
C                           THE DATA FRAME BEFORE ACTUAL DATA INPUT
C                           SUCH THAT THE FIRST LINE OF THE FRAME IS A
C                           SYNCHRONIZATION POINT.
C      SYNTIM    INT    I   2D ARRAY OF TIMES (IN CENTURY DAY, MILLISEC
C                           OF DAY) ASSOCIATED WITH INSTRUMENT SYNC
C                           POINTS.
C      OBSHIR    REAL  I/O  ARRAY (DIMENSIONED HNSPOT X HTOTLN X 20)
C                           CONTAINING THE NODE IN THE 20TH ELEMENT.
C      ERSTAT    INT    O   ERROR STATUS RETURN CODE 
C      N16HIR    INT    I   FLAG TO PASS TO INHIRS TO SHIFT BRIGHTNESS
C                           TEMPERATURES ONE SPOT TO THE RIGHT TO
C                           CORRECT FOR INSTRUMENT MISALIGNMENT.
C      SCNHIR    I*4   I/O  HIRS SCAN NUMBER
C      SCNAMA    I*4   I/O  AMSU-A SCAN NUMBER
C      SCNAMB    I*4   I/O  AMSU-B SCAN NUMBER
C      INSTON    I*4    I   VECTOR HOLDING THE STATUS (1=ON, 0=OFF)
C                           OF THE HIRS, AMSU-A, AMSU-B, AND AVHRR
C                           INSTRUMENTS, RESPECTIVELY
C      FULFRM    LOG   I/O  'FULL FRAME' FLAG RETURNED BY THE CALL
C                           TO INHIRS.
C

C
C
C    COMMON AREA PARAMETERS-
      INCLUDE'INSPARM'
      INCLUDE'INPARM'
      INCLUDE'INPCDB'
      INCLUDE'FRMINF'
      INCLUDE'COLLOC'
      INCLUDE'ERPARM'
      INCLUDE'ERINFO'
      INCLUDE'INSCOM'
      INCLUDE'NOHIRSCF'
      INCLUDE'SPVAL'
C
C    LOCAL VARIABLES-
C      VARIABLE   TYPE   DESCRIPTION
C      --------   ----   -----------
C      AMXGDL     INT    MAX NUMBER OF ADJACENT GOOD LINES IN AMSU-A FRAME
C      BCALCF    REAL    3D ARRAY OF CALIBRATION COEFFICIENTS FOR EACH
C                        AMSU-B CHANNEL FOR EACH LINE OF THE EXTENDED
C                        DATA FRAME 
C      BMXGDL     INT    MAX NUMBER OF ADJACENT GOOD LINES IN AMSU-B FRAME
C      HCALCF    REAL    3D ARRAY OF CALIBRATION COEFFICIENTS FOR EACH
C                        HIRS CHANNEL FOR EACH LINE OF THE EXTENDED
C                        DATA FRAME.
C      I, J, K   INT     LOOP INDEX VARIABLES.
C      ILNNUM    INT     LINE NUMBER.
C      ISPNUM    INT     SPOT NUMBER.
C      ACALCF    REAL    3D ARRAY OF CALIBRATION COEFFICIENTS FOR EACH
C                        AMSU-A CHANNEL FOR EACH LINE OF THE EXTENDED
C                        DATA FRAME.
C      FEOF       LOG    SET TO .TRUE. WHEN THE AVHRR INSTRUMENT IS
C                        NOT PRESENT.
C      FIRST      LOG    SET TO .TRUE. FOR THE FIRST CALL TO INGAC.
C      GOOD2CAL   INT    NUMBER OF BUFFER LINES SKIPPED BEFORE PUTTING
C                        THE FIRST HIRS SCAN LINE DATA INTO THE HIRS
C                        BUFFERS.
C      HMXGDL     INT    MAXIMUM NUMBER OF ADJACENT GOOD LINES IN HIRS
C                        FRAME.
C
C   SUBPROGRAMS CALLED- INHIRS, INAMA, INGAC, CMHADJ, CHKFRM, INPDV$
C                       AMA2HR, CMAADJ, INAMB, CMBADJ
C
C   FUNCTIONS USED-  LCONFG, BTEST
C
C   EXIT STATES-
C       ERSTAT =  0; INDICATING NO ERRORS FOUND.
C       ERSTAT = 99; INDICATING A FATAL ERROR WAS DETECTED.
C
C***********************************************************************
C
      INTEGER SYNTIM(2,3), SYNCTR(3), N16HIR, INSTON(4)
      INTEGER HMXGDL, AMXGDL, BMXGDL, ERSTAT, GOOD2CAL
      INTEGER ISPNUM, ILNNUM, I, J, K, LINAMA, TIMAMA(2)
      INTEGER*4 SCNAMA, SCNAMB, SCNHIR
C
      REAL HCALCF(3,HNCHAN,HTOTLN), ACALCF(3,ANCHAN,ATOTLN)
      REAL BCALCF(3,BNCHAN,BTOTLN)
      REAL OBSHIR(HNSPOT,HTOTLN,20)
C
      LOGICAL FSTFRM(4), LCONFG, FIRST, FEOF, FULFRM, METOP
C
      SAVE FEOF,FIRST,BCALCF
C
      DATA FIRST /.TRUE./, FEOF /.FALSE./
C
C   INITIALIZE THE NODE TO ZERO FOR EACH FRAME
C
      DO ISPNUM=1,HNSPOT
       DO ILNNUM=1,HTOTLN
        OBSHIR(ISPNUM,ILNNUM,20) = 0
       ENDDO
      ENDDO     

C   SET THE 'AVHRR OFF' FLAG TO TRUE IF THE INSTON FLAG FOR THE AVHRR
C   INDICATES THE 1B* FILE IS MISSING OR IS NOT TO BE PROCESSED.
C
      IF (INSTON(4).NE.1) FEOF = .TRUE.

C
C   INITIALIZE THE DUMMY CALIBRATION COEFFICIENT ARRAY FOR AMSU-B.
C
      IF (FSTFRM(4)) THEN
         DO I = 1, 3
            DO J = 1, BNCHAN
               DO K = 1, BTOTLN
                  BCALCF(I,J,K) = R4SPV
               END DO
            END DO
         END DO
      END IF
C
C   INITIALIZE FRAME CONFIGURATION TO VALUE OF INSTRUMENT CONFIGURATION.
C
      FRMCNF = CONFIG
C
      ERSTAT = 0
C
C   NOTE:  BTEST IS A BIT INTRISIC FUNCTION WHICH RETURNS A VALUE OF
C   TRUE IF THE BIT SPECIFIED OF A 4-BYTE INTEGER IS SET TO 1.  BTEST
C   TREATS THE BITS OF A 4-BYTE WORD AS BEING ORDERED RIGHT TO LEFT
C   FROM 0 TO 31.  FOR OUR PURPOSES IN FRMCNF AND CONFIG, WE CONSIDER
C   BITS AS BEING ORDERED FROM LEFT TO RIGHT FROM 1 TO 32.  THUS TO
C   CHECK WHAT WE CONSIDER TO BE THE CORRECT BIT CORRESPONDING TO AN
C   INSTRUMENT, THE BIT CHECKED MUST BE 32 MINUS THE INSTRUMENT
C   NUMBER.  (BIT 1 CORRESPONDS TO BIT 31, 2 TO 30, 3 TO 29 ....)
C
C  CHECK TO SEE IF HIRS DATA IS AVAILABLE, AND IF SO INGEST A DATA
C  FRAME OF HIRS DATA.
C
      GOOD2CAL = 0
      FULFRM = .TRUE.
      IF (NOH1BS) THEN
c        GOOD2CAL = 9
         GO TO 10
      END IF
      IF (BTEST(FRMCNF,31)) THEN
        INSTR = 1
        CALL INHIRS(FSTFRM(1),SYNCTR(1),SYNTIM(1,1),HCALCF,HIRDAT,
     1              HIRANC,ERSTAT,GOOD2CAL,OBSHIR,N16HIR,SCNHIR,
     2              FULFRM,METOP)
        IF (ERSTAT.EQ.99) RETURN
C
C  COMPUTE MAX NUMBER OF ADJACENT GOOD LINES IN HIRS DATA FRAME, THEN
C  CHECK AGAINST THE ALLOWABLE LIMIT.
C
        CALL CMHADJ(HGOODL,HTOTLN,HTOTBF,HMXGDL)
        CALL CHKFRM(HMXGDL,HLINLM,HGOODL,HQFLAG,HTOTLN,HTOTBF,1,ERSTAT)
        IF (ERSTAT.EQ.99) RETURN
      ENDIF
C
C  PROCESS GAC DATA, IF PRESENT, AND PUT DATA INTO THE HIRDAT BUFFER.
C
   10 CONTINUE
ccft
      IF (.NOT.BTEST(FRMCNF,28)) FEOF = .TRUE.
      IF(FEOF) THEN
         FIRST = .FALSE.
         GOTO 20
      END IF
      CALL INGAC(HIRDAT,FIRST,FEOF,GOOD2CAL,ERSTAT)
      FIRST = .FALSE.
      IF (ERSTAT .EQ. 99) RETURN
C
C  CHECK TO SEE IF AMSU-A DATA ARE AVAILABLE, AND IF SO INGEST A DATA
C  FRAME OF AMSU-A DATA.
C
   20 IF (BTEST(FRMCNF,30)) THEN
        INSTR = 2
        CALL INAMA(FSTFRM(2),SYNCTR(2),SYNTIM(1,2),ACALCF,AMADAT,
     1             AMAANC,GOOD2CAL,SCNAMA,LINAMA,TIMAMA,ERSTAT)
        IF (ERSTAT.EQ.99) RETURN
C
C  COMPUTE MAX NUMBER OF ADJACENT GOOD LINES IN AMSU-A DATA FRAME, THEN
C  CHECK AGAINST THE ALLOWABLE LIMIT.
C
        CALL CMAADJ(AGOODL,ATOTLN,ATOTBF,AMXGDL)
        CALL CHKFRM(AMXGDL,ALINLM,AGOODL,AQFLAG,ATOTLN,ATOTBF,2,ERSTAT)
        IF (ERSTAT.EQ.99) RETURN
C
C  IF NO HIRS 1B* IS AVAILABLE, INTERPOLATE AMSU-A LATITUDES AND
C  LONGITUDES TO HIRS SPOTS TO PROVIDE HIRS SPOTS FOR THE RETRIEVAL
C  GRID.
C
        IF (NOH1BS) THEN
           CALL AMA2HR(AMAANC,HIRDAT,HIRANC,OBSHIR,SYNTIM)
        END IF
      END IF
C
C  CHECK TO SEE IF AMSU-B DATA ARE AVAILABLE, AND IF SO INGEST A DATA
C  FRAME OF AMSU-B DATA.
C
      IF (BTEST(FRMCNF,29)) THEN
        INSTR = 3
        CALL INAMB(FSTFRM(3),SYNCTR(3),SYNTIM(1,3),BCALCF,AMBDAT,
     1              AMBANC,LINAMA,TIMAMA,SCNAMB,ERSTAT)
        IF (ERSTAT.EQ.99) RETURN
C
C  COMPUTE MAX NUMBER OF ADJACENT GOOD LINES IN AMSU-B DATA FRAME, THEN
C  CHECK AGAINST THE ALLOWABLE LIMIT.
C
        CALL CMBADJ(BGOODL,BTOTLN,BTOTBF,BMXGDL)
        CALL CHKFRM(BMXGDL,BLINLM,BGOODL,BQFLAG,BTOTLN,BTOTBF,3,ERSTAT)
        IF (ERSTAT.EQ.99) RETURN
      END IF
C    
C
C  AT SPOP MEETING OF 11/8/95 IT WAS DECIDED THERE WOULD BE AMSU-A
C   ONLY RETRIEVALS, HOWEVER A HIRS 1B* WITH EARTH LOCATION WOULD BE
C   PROVIDED.  THEREFORE FILFRM IS NOT REQUIRED.
C
C  CHECK FRAME CONFIGURATION.  IF MINIMUM CONFIGURATION IS NOT
C   MET, DO NOT CONTINUE FRAME PROCESSING.
C
      IF (.NOT.(LCONFG(FRMCNF,CDBCFG))) THEN
         CALL INPDV$(301,ERSTAT)
         IF (ERSTAT .NE. 99) ERSTAT = 1
         RETURN
      ENDIF
C
      RETURN
      END
C
C*********************************************************************
      SUBROUTINE INPDV$(MSGCOD,STAT)
C
C*********************************************************************
C   NAME- INPDV$
C
C   LANGUAGE- VS FORTRAN   TYPE- SUBROUTINE 
C
C   VERSION- 1.0    DATE- 04/04/91    PROGRAMMER- CARLA NEIGH (SMSRC)
C   VERSION- 3.0    DATE- 07/09/97    PROGRAMMER- LEAH WILSON (HSTX)
C
C   FILES-
C     DSN                       I/O  UNIT  FUNCTIONAL DESCRIPTION
C     ---                       ---  ----  -----------------------
C     .../KIDS                   0   KUNIT KRAY INTERACTIVE DOCUMENTATION
C                                          SYSTEM FILE.
C
C   FUNCTION- THIS SUBROUTINE IS THE ERROR HANDLING ROUTINE FOR
C             ROUTINE INPDVR
C   VERSION- 3.0 CONVERT FROM MMLF TO KIDS CONVENTION.
C
C   PROGRAM CONSTANTS-
      INCLUDE 'ERPARM'
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE               TYPE  I/O  FUNCTION
C     --------               ----  ---  --------
C     MSGCOD                  INT   I   MESSAGE CODE USED TO
C                                       IDENTIFY THE ERROR
C                                       PATTERN
C     STAT                    INT   O   STATUS CODE IF AN ERROR
C                                       OCCURS DURING I/O
C
      INTEGER   MSGCOD,STAT
C
C   COMMON AREA PARAMETERS-
C
      INCLUDE 'ERINFO'
      INCLUDE 'SPGUNT'
C
C   LOCAL PARAMETERS-
C     VARIABLE                TYPE    FUNCTION
C     --------                ----    --------
C     KMSG                   CH*100   KIDS MESSAGE BUFFER.
C     C4TEMP                  CH*4    HOLDING BUFFER FOR WRITING TO KMSG.
C     C12TMP                  CH*12   HOLDING BUFFER FOR WRITING TO KMSG.
C
C   SUBPROGRAMS CALLED- KIDS 
C
C   EXIT STATES-
C       STAT =  0; INDICATING NO OR NON_FATAL ERROR FOUND
C       STAT = 99; INDICATING A FATAL ERROR WAS DETECTED
C
C   ASSUMPTIONS-  THIS ROUTINE ASSUMES KIDS HAS BEEN CALLED PREVIOUSLY
C                 TO OPEN THE KIDS FILE.
C
C*********************************************************************
C
      CHARACTER*4 C4TEMP
      CHARACTER*12 C12TMP
      CHARACTER*100 KMSG
C
C  SET ERINFO:SUBNAM
C
      SUBNAM = 'SPGDVR'
C
      IF(MSGCOD.EQ.301) THEN
        KMSG(1:50)='* FRAME ERROR * Min. frame configuration not met '
        KMSG(51:100)='for frame #      configuration =             '
        WRITE(C4TEMP,'(I4)') FRMNBR
        KMSG(63:66) = C4TEMP
        WRITE(C12TMP,'(I12)') FRMCNF
        KMSG(84:95) = C12TMP
        PRINT *,KMSG
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
        STAT=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
