      SUBROUTINE FBUFOUT(NF,IBUF,IMAX,K)
C
C     * JUL 07/03 - A.IRWIN,F.MAJAESS (REVISED FOR F90 AND LINUX PLATFORMS)
C     * NOV 02/92 - E.CHAN. (REPLACE WRITES WITH CALLS TO WRTBUF)
C     * SEP 04/92 - E.CHAN. (ADD OPTION TO WRITE OUT JUST THE LABEL)
C     * JUL 20/92 - E.CHAN. (MODIFY FOR NEW 64-BIT IEEE DATA FORMAT)
C     * MAR 13/92 - E.CHAN. 
C     * WRITES STANDARD CCRN DATA IN "IBUF" TO FILE NF (2-RECORD 
C     * FORMAT). THE FIRST RECORD CONTAINS THE 8-WORD LABEL AND THE 
C     * SECOND CONTAINS THE DATA (POSSIBLY PACKED).
C 
C     *   K  = STATUS OF THE WRITE OPERATION
C     *        = -2 : SUCCESSFUL WRITE
C     *        =  2 : ERROR DURING WRITE OPERATION
C     * IMAX = SIZE OF IBUF (I.E. 8-WORD LABEL + DATA)
C     *        ( IF IMAX <= 0 THEN ONLY THE LABEL IS WRITTEN OUT )
C
      IMPLICIT REAL (A-H,O-Z),
     +INTEGER (I-N)
      LOGICAL OK, LENDIAN
      INTEGER IBUF(9),JBUF(16),JBUFPT(8,2)
      COMMON /MACHTYP/ MACHINE,INTSIZE
C
      DATA JBUFPT/1,2,3,4,5,6,7,8,1,4,5,8,10,12,14,16/
C     DATA JBLANK/4H    /
C
C---------------------------------------------------------------------
       JBLANK=NC4TO8("    ")
      IF(LENDIAN()) THEN
C        NEED NPACK FOR LENDIAN LOGIC TO WORK
        NPACK = IABS (IBUF(8))
C        N.B. ALL THE FOLLOWING DO LOOPS ARE NOT EXECUTED IF IMAX <= 0.
        IF(IBUF(1).EQ.NC4TO8("LABL").OR.IBUF(1).EQ.NC4TO8("CHAR")) THEN
C          IN THE OVERALL CONVERSION FROM LITTLE-ENDIAN UNFORMATTED RECORDS TO
C          BIG ENDIAN, HOLLERITH CONSTANTS WILL BE INAPPROPRIATELY
C          REVERSED.  REVERSE THEM BACK.
          DO 300 I = 9,IMAX
            IF(MACHINE.EQ.2) THEN
              CALL REVERSE(IBUF(I),4)
            ELSE
              CALL REVERSE(IBUF(I),8)
            ENDIF
  300     CONTINUE
        ELSEIF(INTSIZE.EQ.2.AND.(NPACK.EQ.0.OR.NPACK.EQ.1)) THEN
C          IN THE OVERALL CONVERSION FROM LITTLE-ENDIAN UNFORMATTED RECORDS TO
C          BIG ENDIAN FOR INTSIZE = 2, THE BYTE-REVERSAL WILL BE DONE
C          FOR INDIVIDUAL 4-BYTE WORDS RATHER THAN THE 8-BYTE WORDS CONTAINING
C          FLOATING-POINT NUMBERS.  TO GET THOSE 8-BYTE NUMBERS IN THE
C          CORRECT ORDER MUST SWAP EVERY SECOND WORD.
          DO 310 I = 9,IMAX-1,2
            ISWAP = IBUF(I)
            IBUF(I) = IBUF(I+1)
            IBUF(I+1) = ISWAP
  310     CONTINUE
        ELSEIF(NPACK.NE.0.AND.NPACK.NE.1) THEN
          IMAX3 = 8+4
          IF(IBUF(1).EQ.NC4TO8("SPEC")) IMAX3 = IMAX3 + 2
          IF(INTSIZE.EQ.2) THEN
C            IN THE OVERALL CONVERSION FROM LITTLE-ENDIAN UNFORMATTED RECORDS TO
C            BIG ENDIAN FOR INTSIZE = 2, THE BYTE-REVERSAL WILL BE DONE
C            FOR INDIVIDUAL 4-BYTE WORDS RATHER THAN THE 8-BYTE WORDS CONTAINING
C            FLOATING-POINT NUMBERS.  TO GET THOSE 8-BYTE NUMBERS IN THE
C            CORRECT ORDER MUST SWAP EVERY SECOND WORD.
            DO 320 I = 9,IMAX3-1,2
              ISWAP = IBUF(I)
              IBUF(I) = IBUF(I+1)
              IBUF(I+1) = ISWAP
  320       CONTINUE
          ENDIF
C          IN THE OVERALL CONVERSION FROM LITTLE-ENDIAN UNFORMATTED RECORDS TO
C          BIG ENDIAN FOR MACHINE = 1, THE BYTE-REVERSAL WILL BE DONE
C          FOR INDIVIDUAL 8-BYTE WORDS RATHER THAN THE 4-BYTE WORDS THAT
C          ARE THE STANDARD FOR PACKED DATA.  HOWEVER, IN THIS SAME CASE
C          (LITTLE-ENDIAN, MACHINE=1, SEE SBYTESB TO VERIFY THIS) SBYTESB
C          EFFECTIVELY WORD SWAPS SO EVERYTHING WORKS OUT AND THERE IS NO
C          NEED FOR ADDITIONAL WORD SWAPPING HERE.
        ENDIF
C        IN THE OVERALL CONVERSION FROM LITTLE-ENDIAN UNFORMATTED RECORDS TO
C        BIG ENDIAN, HOLLERITH CONSTANTS WILL BE INAPPROPRIATELY
C        REVERSED.  REVERSE THEM BEFOREHAND TO COMPENSATE.
        IF(MACHINE.EQ.2) THEN
          CALL REVERSE(IBUF(1),4)
          CALL REVERSE(IBUF(3),4)
        ELSE
          CALL REVERSE(IBUF(1),8)
          CALL REVERSE(IBUF(3),8)
        ENDIF
      ENDIF
C
C     * MAP THE LABEL INTO 64-BIT INTEGER FORMAT.
C
      DO 10 I=8,1,-1
        JTEMP                     = IBUF(I)
        JBUF((I-1)*MACHINE+1)     = 0      !CLEAR IBUF(I)/HI-ORDER 32'
        JBUF(I*MACHINE)           = JBLANK !LO-ORDER 32' SET TO BLANKS.
        JBUF(JBUFPT(I,MACHINE))   = JTEMP
   10 CONTINUE
C
C     * WRITE OUT THE LABEL.
C
      LHEAD = 8*MACHINE
      CALL WRTBUF(NF,JBUF,LHEAD,OK)
      IF(LENDIAN()) THEN
C        RESTORE HEADER (IN CASE OF EXTERNAL RE-USE) TO LITTLE-ENDIAN FORM.
        IF(MACHINE.EQ.2) THEN
          CALL REVERSE(IBUF(1),4)
          CALL REVERSE(IBUF(3),4)
        ELSE
          CALL REVERSE(IBUF(1),8)
          CALL REVERSE(IBUF(3),8)
        ENDIF
      ENDIF
      IF (.NOT. OK) GO TO 901
C
C     * WRITE THE DATA RECORD ONLY IF IMAX IS POSITIVE.
C
      IF ( IMAX .LE. 0 ) GO TO 900
C
      CALL WRTBUF(NF,IBUF(9),IMAX-8,OK)
      IF(LENDIAN()) THEN
C        RESTORE DATA RECORD (IN CASE OF EXTERNAL RE-USE) TO
C        LITTLE-ENDIAN FORM.
C        REVERSE DO LOOPS FOR CACHE EFFICIENCY.
        IF(IBUF(1).EQ.NC4TO8("LABL").OR.IBUF(1).EQ.NC4TO8("CHAR")) THEN
          DO 400 I = IMAX,9,-1
            IF(MACHINE.EQ.2) THEN
              CALL REVERSE(IBUF(I),4)
            ELSE
              CALL REVERSE(IBUF(I),8)
            ENDIF
  400     CONTINUE
        ELSEIF(INTSIZE.EQ.2.AND.(NPACK.EQ.0.OR.NPACK.EQ.1)) THEN
          DO 410 I = IMAX-1,9,-2
            ISWAP = IBUF(I)
            IBUF(I) = IBUF(I+1)
            IBUF(I+1) = ISWAP
  410     CONTINUE
        ELSEIF(NPACK.NE.0.AND.NPACK.NE.1) THEN
          IF(INTSIZE.EQ.2) THEN
            DO 420 I = 9,IMAX3-1,2
              ISWAP = IBUF(I)
              IBUF(I) = IBUF(I+1)
              IBUF(I+1) = ISWAP
  420       CONTINUE
          ENDIF
        ENDIF
      ENDIF
      IF (.NOT. OK) GO TO 901
C
  900 K=-2
      RETURN
C
  901 K=2
      RETURN 
C
      END 
