      SUBROUTINE GETTIM ( INSTNB, CDAY, MSEC, SCANNR, ERSTAT )
      IMPLICIT NONE
C
C**********************************************************************
C
C   NAME- GETTIM
C
C   LANGUAGE- FORTRAN 77    TYPE- SUBROUTINE   COMPUTER- CRAY
C
C   VERSION- 1.0    DATE- 04/03/91   PROGRAMMER- DUANE NELSON (SMSRC)
C   VERSION- 2.0    DATE- 04/30/95   PROGRAMMER- ALEX POZDNYAK(SMSRC)
C     ATOVS-106:    READ OF 1B SCAN LINE WAS REPLACED BY APPROPRIATE
C                   XXXGT1 ROUTINE. EXTRACTION OF TIME AND QUALITY
C                   FLAGS WERE REPLACED BY 1B* EXTRACTION ROUTINES.
C                   ARGUMENTS IUNIT AND TIMBYT WERE REMOVED. ARGUMENT
C                   WAS ADDED FOR INSTRUMENT NUMBER, WHERE 1 = HIRS,
C                   2 = AMSU-A AND 3 = AMSU-B. $-ROUTINE WAS MODIFIED
C                   TO WRITE 1B* ERROR MESSAGE BUFFER INTO KIDS.
C                   CLEARING OF BITS IN CONFIG WAS MOVED FROM $-ROUTINE
C                   TO GETTIM.
C   VERSION- 2.1    DATE- 09/14/95   PROGRAMMER- DAVE MOREL(SMSRC)
C     ATOVS-212:    REMOVE CALLING ARGUMENT FATMSK.  REPLACE H1SQBT,
C                   A1SQBT AND B1SQBT WITH SCNCNF. REPLACE CALLS TO
C                   HIRGT1, AMAGT1, AND AMBGT1 WITH HRSGT1, AASGT1,
C                   AND ABSGT1.
C   VERSION- 2.2    DATE- 12/18/95   PROGRAMMER- CHARLES DEAN (SMSRC)
C     ATOVS-290:    REPLACE CALLS TO SCNCNF WITH DO LOOPS AS IN SCNCNF.
C   VERSION- 3.0    DATE- 04/16/96   PROGRAMMER- ALEX POZDNYAK (FDC)
C      MODIFIED TO RUN ON THE CRAY.  REPLACED I*4 WITH INT.
C   VERSION- 3.1    DATE- 11/28/97   PROGRAMMER- LEAH WILSON (HSTX)
C      MODIFY HOW HIRS FATAL FLAGS ARE READ AND DECODED.
C   VERSION- 3.2    DATE- 02/23/98   PROGRAMMER- LEAH WILSON (RSTX)
C      ADD ERROR CHECKING FOR BOGUS JULIAN DAY, YEAR, OR MILLISECOND
C      TIMES READ FROM ANY 1B*.  STOP PROCESSING IN ORDER TO AVOID
C      OPERAND RANGE ERRORS IN CENDAY.
C   VERSION- 3.4    DATE- 12/28/98   PROGRAMMER- LEAH WILSON (RAYTHEON)
C      UNCOMMENT CHECKING OF HIRS FLAGS.  THESE WERE COMMENTED OUT DURING
C      THE NOAA-15 BRING-UP.  CHANGE CHECKING OF CNFDNC FROM ".EQ.1" TO
C      ".GE.1".  REMOVE ALL REFERENCES TO AMSU-B UTILITIES.
C   VERSION- 3.5    DATE- 12/16/99   PROGRAMMER- LEAH WILSON (RAYTHEON)
C      ONLY USE DO_NOT_USE_SCAN FLAG WHEN CHECKING FOR USABILITY.  REMOVE
C      REFERNCES TO ANY OTHER SCAN LINE FLAG.
C   VERSION- 3.6    DATE- 08/29/00   PROGRAMMER- LEAH WILSON (RAYTHEON)
C      DO NOT USE DO_NOT_USE_SCAN FLAG FOR CHECKING USABILITY. THIS
C      ALLOWS SYNCHRONIZATION REGARDLESS OF THE SCAN QUALITY, WHICH
C      SHOULD PREVENT GROSS MISLOCATIONS (GREATER THAN A FRAME) BETWEEN
C      THE HIRS AND AMSU-A.
C   VERSION- 3.7    DATE- 07/23/03   PROGRAMMER- F. TILLEY (RAYTHEON)
C      UNDER WR #230 (NOAA-N READINESS) THE CALLS TO AASGT1 AND HRSGT1
C      WERE CHANGED TO AADGT1 AND HRDGT1 BECAUSE THE 1B* FILES FOR
C      NOAA-N ARE BEING CREATED FOR 'DIRECT' ACCESS. REMOVED CALLS TO
C      THE 'BACKSPACE' ROUTINES. ADDED CALLING ARGUMENT SCANNR AS A
C      SCAN POINTER. DECLARED 'SHAPE' AS INTEGER*4. SUBSTITUTED I*4
C      'LENGTH' FOR DATA LENGTH IN 1B* UTILITY CALLS. CHANGED 'STATUS'
C      TO I*4 'RETCOD' IN 1B* UTILITY CALLS.
C   VERSION- 3.8    DATE- 01/16/04   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C      ADDED AMSU-B.
C
C   FUNCTION- THIS ROUTINE CHECKS THE FIRST USABLE SCAN LINE OF DATA
C             FOR A GIVEN INSTRUMENT TO RETRIEVE THE STARTING TIME OF
C             FIRST USABLE SCAN.  SCAN QUALITY FLAGS ARE CHECKED FOR
C             FATAL ERRORS.  IF AN UNUSABLE OR MISSING LINE IS FOUND,
C             THEN THE NEXT LINE IS READ.  THE TIME IN CENTURY DAY
C             AND MILLISECONDS IS RETURNED.  INSTRUMENT BIT IN ERINFO:
C             CONFIG IS SET TO 0 IF READ ERROR OR END-OF-FILE OCCURS.
C
C
C   FILES-     INSTRUMENT 1B* FILE
C
C
C   CALLING SEQUENCE PARAMETERS-
C
C   VARIABLE  TYPE  I/O   DESCRIPTION
C   --------  ----  ---   -----------
C   INSTNB     INT   I    INSTRUMENT NUMBER.
C   CDAY       INT   O    FIRST GOOD SCANLINE TIME (DAY-OF-CENTURY).
C   MSEC       INT   O    FIRST GOOD SCANLINE TIME (MSEC OF DAY).
C   SCANNR     I*4   O    SCAN NUMBER
C   ERSTAT     INT   O    STATUS CODE PASSED BACK BY THE ERROR HANDLING
C                         ROUTINE.
C
C
C   COMMON AREA PARAMETERS -
C
      INCLUDE 'ERPARM'
      INCLUDE 'ERINFO'
      INCLUDE 'INPCDB'
C
C   LOCAL PARAMETERS -
C
C   VARIABLE  TYPE    DESCRIPTION
C   --------  ----    ---------------------------------------------
C   CNFDNC     INT    SCAN LINE QUALITY FLAG (1=BAD).
C   CNFDN4     INT    ARRAY (DIM 4) USED FOR DECODING HIRS SCAN LINE
C                     FLAGS WITH SHAPE = 4.
C   CNFDN6     INT    ARRAY (DIM 6) USED FOR DECODING HIRS SCAN LINE
C                     FLAGS WITH SHAPE = 6.
C   CNFDN64    INT    ARRAY (DIM 64) USED FOR DECODING HIRS SCAN LINE
C                     FLAGS WITH SHAPE = 64.
C   ITEMID   CH*32    ITEM ID USED TO RETRIEVE DATE AND TIME BY CALLING
C                     HRG1I2 AND HRG1I4.
C   I2RAY      I*2    TWO BYTES INTEGER BUFFER HOLDING RETRIEVED DATE
C   I          INT    DO LOOP INDEX.
C   INT1VL     I*1    VARIABLE FOR EXTRACTING I*1 VALUES FROM THE 1B*
C   INT4VL     I*4    VARIABLE FOR EXTRACTING I*4 VALUES FROM THE 1B*
C   LENGTH     I*4    DATA LENGTH FOR 1B* UTILITY CALLS.
C   SHAPE      I*4    VECTOR OF LENGTH 8 TO HOLD THE SHAPE OF YEAR OR
C                     SHAPE OF DATE OR SHAPE OF TIME RETRIEVED FROM
C                     THE NEXT SCAN LINE.
C   SYEAR      INT    YEAR OF SCAN.
C   SMONTH     INT    MONTH OF SCAN.
C   SDAY       INT    DAY_OF_MONTH OF SCAN.
C   JDAY       INT    JULIAN DAY OF SCAN.
C   BSHFT      INT    USED TO SHIFT BITS OF VARIABLE CONFIG.
C   RETCOD     I*4    RETURN CODE FROM 1B* UTILITIES.
C   STATUS     INT    ERROR STATUS CODE.
C   ELMT      CH*4    DESCRIBES ELEMENT WHERE ERROR OCCURRED IN 1B*.
C   ELMTVAL    INT    GIVES VALUE OF ELMT FOR ERROR HANDLING.
C
C
C   SUBROUTINES CALLED - HRG1I2, HRG1I4, AAG1I2, AAG1I4,
C                        HRDGT1, AADGT1,
C                        ABDGT1, AMB1BS, ABG1I2, ABG1I4,
C                        HRG1I1, AAG1I1, JULDAY, CENDAY, GETTI$
C
C   FUNCTIONS USED - IBCLR, MOD
C
C
C   EXIT STATES- ERSTAT=0 FOR AN ERROR FREE READ
C
************************************************************************
C
      INTEGER  INSTNB, CDAY, MSEC, ERSTAT
      INTEGER*4 SCANNR
C
      INTEGER  BSHFT, STATUS
      INTEGER*4 SHAPE(0:7), LENGTH, INT4VL, RETCOD
      INTEGER  JDAY, SYEAR, SMONTH, SDAY, I, ELMTVAL
      INTEGER*1 INT1VL
      INTEGER*2 I2RAY
      CHARACTER*4 ELMT
      CHARACTER*32 ITEMID
      INTEGER CNFDNC, CNFDN4(4), CNFDN6(6), CNFDN64(64)
C
C  INITIALIZE VARIABLES.
C
      ERSTAT = 0
      SHAPE(0) = 1
      SHAPE(1) = 1
      SHAPE(2) = 0
      SHAPE(3) = 0
      SHAPE(4) = 0
      SHAPE(5) = 0
      SHAPE(6) = 0
      SHAPE(7) = 0
C
      CDAY = 0
      MSEC = 0
      ELMT = '    '
      ELMTVAL = -1
C
C  READ THE NEXT SCAN LINE INTO THE DATA HOLD BUFFER, EXTRACT
C  DATE AND TIME.
C
   20 CONTINUE
C
      SCANNR = SCANNR+1
      IF (INSTNB .EQ. 1) THEN
          CALL HRDGT1(SCANNR,RETCOD)
          STATUS = RETCOD
          IF (STATUS.EQ.1) GOTO 900
          IF (STATUS.LT.0) GOTO 800
C
C                   12345678901234567890123456789012
          ITEMID = 'DAY_OF_YEAR_OF_SCAN             '
          SHAPE(0) = 1
          SHAPE(1) = 1
          LENGTH = 1
          CALL HRG1I2(ITEMID,I2RAY,LENGTH,SHAPE,RETCOD)
          STATUS = RETCOD
          IF (STATUS.LT.0) GOTO 800
          JDAY = I2RAY
          IF(JDAY.LE.0) THEN
             ELMT = 'JDAY'
             ELMTVAL = JDAY
             GO TO 700
          ENDIF
C
C                   12345678901234567890123456789012
          ITEMID = 'YEAR_OF_SCAN                    '
          CALL HRG1I2(ITEMID,I2RAY,LENGTH,SHAPE,RETCOD)
          STATUS = RETCOD
          IF (STATUS.LT.0) GOTO 800
          SYEAR = I2RAY
          IF(SYEAR.LT.0) THEN
             ELMT = 'YEAR'
             ELMTVAL = SYEAR
             GO TO 700
          ENDIF
C
C                   12345678901234567890123456789012
          ITEMID = 'TIME_OF_DAY_OF_SCAN             '
          CALL HRG1I4(ITEMID,INT4VL,LENGTH,SHAPE,RETCOD)
          STATUS = RETCOD
          IF (STATUS.LT.0) GOTO 800
          MSEC = INT4VL
          IF(MSEC.LT.0) THEN
             ELMT = 'MSEC'
             ELMTVAL = MSEC
             GO TO 700
          ENDIF
C
C   CHECK FOR ANY FATAL HIRS SCAN LINE FLAGS.
C   IF CNFDNC IS NOT ZERO, READ ANOTHER RECORD.
C
C                   12345678901234567890123456789012
          ITEMID = 'DO_NOT_USE_SCAN                 '
          CALL HRG1I1(ITEMID,INT1VL,LENGTH,SHAPE,RETCOD)
          STATUS = RETCOD
          IF (STATUS .LT. 0) GO TO 800
          CNFDNC = INT1VL
          IF (CNFDNC .NE. 0) GO TO 20
C
      ELSE IF (INSTNB .EQ. 2) THEN
          CALL AADGT1(SCANNR,RETCOD)
          STATUS = RETCOD
          IF (STATUS.EQ.1) GOTO 900
          IF (STATUS.LT.0) GOTO 800
C
C                   12345678901234567890123456789012
          ITEMID = 'DAY_OF_YEAR_OF_SCAN             '
          LENGTH = 1
          CALL AAG1I2(ITEMID,I2RAY,LENGTH,SHAPE,RETCOD)
          STATUS = RETCOD
          IF (STATUS.LT.0) GOTO 800
          JDAY = I2RAY
          IF(JDAY.LE.0) THEN
             ELMT = 'JDAY'
             ELMTVAL = JDAY
             GO TO 700
          ENDIF
C
C                   12345678901234567890123456789012
          ITEMID = 'YEAR_OF_SCAN                    '
          CALL AAG1I2(ITEMID,I2RAY,LENGTH,SHAPE,RETCOD)
          STATUS = RETCOD
          IF (STATUS.LT.0) GOTO 800
          SYEAR = I2RAY
          IF(SYEAR.LE.0) THEN
             ELMT = 'YEAR'
             ELMTVAL = SYEAR
             GO TO 700
          ENDIF
C
C                   12345678901234567890123456789012
          ITEMID = 'TIME_OF_DAY_OF_SCAN             '
          CALL AAG1I4(ITEMID,INT4VL,LENGTH,SHAPE,RETCOD)
          STATUS = RETCOD
          IF (STATUS.LT.0) GOTO 800
          MSEC = INT4VL
          IF(MSEC.LE.0) THEN
             ELMT = 'MSEC'
             ELMTVAL = MSEC
             GO TO 700
          ENDIF
C
C   CHECK FOR ANY FATAL AMSU-A SCAN LINE FLAGS.
C   IF CNFDNC IS NOT ZERO, READ ANOTHER RECORD.
C
C                   12345678901234567890123456789012
          ITEMID = 'DO_NOT_USE_SCAN                 '
          CALL AAG1I1(ITEMID,INT1VL,LENGTH,SHAPE,RETCOD)
          STATUS = RETCOD
          IF (STATUS .LT. 0) GO TO 800
          CNFDNC = INT1VL
          IF (CNFDNC .NE. 0) GO TO 20
C
      ELSE IF (INSTNB .EQ. 3) THEN
          CALL ABDGT1(SCANNR,RETCOD)
          STATUS = RETCOD
          IF (STATUS.EQ.1) GOTO 900
          IF (STATUS.LT.0) GOTO 800
C
C                   12345678901234567890123456789012
          ITEMID = 'DAY_OF_YEAR_OF_SCAN             '
          CALL ABG1I2(ITEMID,I2RAY,LENGTH,SHAPE,RETCOD)
          STATUS = RETCOD
          IF (STATUS.LT.0) GOTO 800
          JDAY = I2RAY
          IF(JDAY.LE.0) THEN
             ELMT = 'JDAY'
             ELMTVAL = JDAY
             GO TO 700
          ENDIF
C
C                   12345678901234567890123456789012
          ITEMID = 'YEAR_OF_SCAN                    '
          CALL ABG1I2(ITEMID,I2RAY,LENGTH,SHAPE,RETCOD)
          STATUS = RETCOD
          IF (STATUS.LT.0) GOTO 800
          SYEAR = I2RAY
          IF(SYEAR.LE.0) THEN
             ELMT = 'YEAR'
             ELMTVAL = SYEAR
             GO TO 700
          ENDIF
C
C                   12345678901234567890123456789012
          ITEMID = 'TIME_OF_DAY_OF_SCAN             '
          CALL ABG1I4(ITEMID,INT4VL,LENGTH,SHAPE,RETCOD)
          STATUS = RETCOD
          IF (STATUS.LT.0) GOTO 800
          MSEC = INT4VL
          IF(MSEC.LE.0) THEN
             ELMT = 'MSEC'
             ELMTVAL = MSEC
             GO TO 700
          ENDIF
C
C  CHECK FOR ANY FATAL AMSU-B SCAN LINE FLAGS.
C  IF CNFDNC IS NOT ZERO, READ ANOTHER RECORD.
C
C                   12345678901234567890123456789012
          ITEMID = 'DO_NOT_USE_SCAN                 '
          CALL ABG1I1(ITEMID,INT1VL,LENGTH,SHAPE,RETCOD)
          STATUS = RETCOD
          IF (STATUS.LT.0) GOTO 800
          CNFDNC = INT1VL
          IF (CNFDNC .NE. 0) GO TO 20
C
      END IF
C
C  CALCULATE CENTURY DAY
C
      CALL JULDAY(JDAY,SYEAR,SMONTH,SDAY)
C
      SYEAR = MOD (SYEAR, 100)
C
      CALL CENDAY(SYEAR,SMONTH,SDAY,CDAY)
C
C  RETURN
C
      RETURN
C
C  INPUT READ ERROR - WRITE MESSAGE TO LOG AND DISCONTINUE PROCESSING
C  OF INSTRUMENT.
C
  700 CONTINUE
      IF (INSTNB.EQ.3) THEN
         BSHFT=32-(INSTNB+1)
      ELSE
         BSHFT=32-INSTNB
      END IF
      CONFIG=IBCLR(CONFIG,BSHFT)
      CALL GETTI$(302,INSTNB,ELMT,ELMTVAL,STATUS)
      ERSTAT = 99
      RETURN
C
C
C    IBCLR TREATS THE BITS OF A 4 BYTE WORD AS BEING ORDERED
C    RIGHT TO LEFT FROM 0 TO 31.  FOR OUR PURPOSES IN FRMCNF
C    AND CONFIG, WE CONSIDER BITS AS BEING ORDERED FROM LEFT
C    TO RIGHT FROM 1 TO 32.  THUS TO SET WHAT WE CONSIDER TO
C    BE THE CORRECT BIT CORRESPONDING TO INSTR, THE BIT SET
C    WITH IBCLR MUST BE 32-INSTR. (BIT 1 CORRESPONDS TO BIT
C    31, 2 TO 30, 3 TO 29....)
C
  800 CONTINUE
      IF (INSTNB.EQ.3) THEN
         BSHFT=32-(INSTNB+1)
      ELSE
         BSHFT=32-INSTNB
      END IF
      CONFIG=IBCLR(CONFIG,BSHFT)
      CALL GETTI$(301,INSTNB,ELMT,ELMTVAL,STATUS)
      ERSTAT = 99
      RETURN
C
C  INPUT END OF FILE - WRITE MESSAGE TO LOG AND DISCONTINUE PROCESSING
C  OF INSTRUMENT.
C
  900 CONTINUE
      IF (INSTNB.EQ.3) THEN
         BSHFT=32-(INSTNB+1)
      ELSE
         BSHFT=32-INSTNB
      END IF
      CONFIG=IBCLR(CONFIG,BSHFT)
      CALL GETTI$(304,INSTNB,ELMT,ELMTVAL,STATUS)
      IF (STATUS .EQ. 99) ERSTAT = 99
      RETURN
      END
C
C*********************************************************************
      SUBROUTINE GETTI$ (MSGCOD, INSTNB, ELMT, ELMTVAL, ERSTAT)
C
C*********************************************************************
C   NAME- GETTI$
C
C   LANGUAGE- VS FORTRAN   TYPE- SUBROUTINE
C
C   VERSION- 1.0    DATE- 03/09/91    PROGRAMMER- DAVE MOREL (SMSRC)
C   VERSION- 3.0    DATE- 08/01/97    PROGRAMMER- LEAH WILSON (HSTX)
C   VERSION- 3.1    DATE- 02/23/98    PROGRAMMER- LEAH WILSON (RSTX)
C   VERSION- 1.2    DATE- 02/08/02    PROGRAMMER- RICKEY ROLLINS (CSC)
C   VERSION- 7.0    DATE- 01/10/05    PROGRAMMER- FRANK TILLEY (RAYTHEON)
C
C   FILES-
C     DSN                    I/O  UNIT  FUNCTIONAL DESCRIPTION
C     ---                    ---  ----  ------------------------------
C     ...KIDS                 O  KUNIT  KRAY I/A DOCUMENTATION FILE.
C
C   FUNCTION- THIS SUBROUTINE IS THE ERROR HANDLING ROUTINE FOR
C             ROUTINE GETTIM.
C   VERSION- 3.0 CONVERT FROM MMLF TO KIDS CONVENTION.
C   VERSION- 3.1 ADD ELMT AND ELMTVAL FOR MSGCOD=302.
C   VERSION- 1.2 PORTED TO CRAY REDEFINED I*4 to I*8
C   VERSION- 7.0 ADDED COMMON 'MSG1BS', REMOVED COMMON 'B1ERR',
C                ADDED CALL TO B1SERR TO DUMP CONTENTS OF
C                1B* ERROR MESSAGE BUFFER.
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE  TYPE  I/O  DESCRIPTION
C     --------  ----  ---  -----------
C     MSGCOD     INT   I   MESSAGE CODE USED TO IDENTIFY THE ERROR
C                          PATTERN.
C     INSTNB     INT   I   INSTRUMENT NUMBER.
C     ELMT      CH*4   I   DESCRIBES ELEMENT WHERE ERROR OCCURRED IN 1B*.
C     ELMTVAL    INT   I   GIVES VALUE OF ELMT FOR ERROR HANDLING.
C     ERSTAT     INT   O   STATUS CODE IF AN ERROR OCCURS DURING I/O.
C
C     INTEGER*4   MSGCOD, INSTNB, ERSTAT
      INTEGER*8   MSGCOD, INSTNB, ERSTAT
C
C   COMMON AREA PARAMETERS-
C
      INCLUDE  'ERPARM'
      INCLUDE  'ERINFO'
      INCLUDE  'MSG1BS'
      INCLUDE  'SPGUNT'
C
C   LOCAL PARAMETERS-
C     VARIABLE  TYPE    DESCRIPTION
C     --------  ----    -----------
C     INSNAM    CH*6    ARRAY (DIMENSIONED 3) CONTAINING INSTRUMENT
C                       NAMES.
C     I         INT     DO LOOP COUNTER.
C     KMSG      CH*100  KIDS MESSAGE BUFFER
C     C4TEMP    CH*4    HOLDING BUFFER FOR WRITING TO KIDS.
C
C   SUBPROGRAMS CALLED- KIDS
C
C   EXIT STATES-
C       ERSTAT =  0; INDICATING NO ERRORS FOUND.
C       ERSTAT = 99; INDICATING A FATAL ERROR WAS DETECTED.
C
C*********************************************************************
C
      INTEGER I,ELMTVAL
      CHARACTER*2 C2TEMP
      CHARACTER*4 C4TEMP,ELMT
      CHARACTER*6 INSNAM(3)
      CHARACTER*100 KMSG
C
      DATA INSNAM/'HIRS  ', 'AMSU-A', 'MHS   '/
C
C
C    SUBNAM IS SET TO MAIN MODULE NAME, SINCE KIDS SUBROUTINE DETERMINES
C    NAME OF CURRENT SUBROUTINE.
C
      SUBNAM = 'SPGDVR'
C
C  INITIALIZE VARIABLE.
C
      ERSTAT = 0
C
C  IF MSGCOD = 301, WRITE B1MSG INTO KIDS FILE.
C
      IF(MSGCOD .EQ. 301) THEN
        CALL B1SERR(ERRBUF,LINES,NOUT,ERSTAT)
        IF (NOUT.GT.1) THEN
           DO I = 1, NOUT
              KMSG(1:60) = ERRBUF(I)
              PRINT *,KMSG
              CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
           END DO
        END IF
C
C  IF MSGCOD=302, WRITE MSG INTO KIDS FILE.
C
      ELSE IF(MSGCOD .EQ. 302) THEN
        KMSG(1:50)='* FATAL ERROR * Instr.    has      =     .  Stop-'
        KMSG(51:100) = 'PROBLEM WITH 1BSTAR!!!!!!!!!!!!'
        WRITE(C2TEMP,'(I2)') INSTNB
        KMSG(24:25) = C2TEMP
        KMSG(31:34) = ELMT
        WRITE(C4TEMP,'(I4)') ELMTVAL
        KMSG(38:41) = C4TEMP
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
        ERSTAT=99
C
C
C  IF MSGCOD=304, WRITE B1MSG INTO KIDS FILE.
C
      ELSE IF(MSGCOD .EQ. 304) THEN
ccft    KMSG = 'EOF error on        instrument - shut down'
        KMSG = 'No good scans found for the        instrument'
        KMSG(29:34) = INSNAM(INSTNB)
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C
      ELSE
C
C NOT A VALID MSGCOD
C
        STAT=0
        KMSG = 'Invalid message code      '
        WRITE(C4TEMP,'(I4)') MSGCOD
        KMSG(22:25) = C4TEMP
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
      ENDIF
      RETURN
      END
