      SUBROUTINE RECGET(NFL,KIND,NSTEP,NAME,LEVEL,IBUF,MAXPK,OK)
C 
C     * JUL 07/03 - A.IRWIN,F.MAJAESS (REVISED FOR F90 AND LINUX PLATFORMS)
C     * JAN 18/94 - F.MAJAESS (CORRECT FOR NEGATIVE LEVEL VALUES)
C     * NOV 02/92 - E.CHAN   (REPLACE READS WITH CALLS TO RDBUF)
C     * JUL 23/92 - E.CHAN   (ENSURE THAT DATA IS READ IN BEGINNING AT
C     *                       WORD 9 OF IBUF)
C     * APR 21/92 - J.STACEY (REVISED TO IMPLEMENT PORTABILITY ACROSS
C     *                       BOTH 32- AND 64-BIT MACHINES)
C     * FEB 11/81 - J.D.HENDERSON. 
C
C     * SCANS FILE NF FOR A RECORD WHOSE FIRST FOUR LABEL WORDS ARE...
C     *  1) KIND = 4 CHARACTER FIELD TYPE (4HSPEC,4HGRID,ETC).
C     *  2) NSTEP = TIMESTEP NUMBER.
C     *  3) NAME = 4 CHARACTER NAME.
C     *  4) LEVEL = LEVEL NUMBER. 
C 
C     * THEY ARE NOT CHECKED IF THEY ARE SET TO -1. 
C     * ORDER OF PRIORITY IN THAT CASE IS KIND,NAME,NSTEP,LEVEL. 
C     * IF NAME IS 0 OR 4HNEXT THE NEXT FIELD OF THE REQUESTED KIND 
C     * IS READ AND NSTEP ,LEVEL ARE NOT CHECKED.
C     * MAXPK = INPUT BUFFER LENGTH FOR (PACKED) FIELDS.
C 
C     * IF NFL IS NEGATIVE THE FILE IS REWOUND FIRST. 
C     * UPON RETURNING IBUF CONTAINS THE 8 WORD LABEL 
C     * FOLLOWED BY THE DATA (WHICH MAY BE PACKED).
C 
      IMPLICIT REAL (A-H,O-Z),
     +INTEGER (I-N)
      LOGICAL OK, LENDIAN
      INTEGER IBUF(9), IBUFPT(8,2)
      COMMON /MACHTYP/ MACHINE,INTSIZE
      DATA    IBUFPT/1,2,3,4,5,6,7,8,1,4,5,8,10,12,14,16/
C---------------------------------------------------------------------
      LHEAD=8*MACHINE
      MAXRL=LHEAD+MAXPK 
      NF=ABS(NFL)
C
      IF(NFL.LT.0) REWIND NF
C
C     * SUBSTITUTE 2-RECORD INPUT FORMAT.  THE FIRST RECORD CONTAINS THE
C     * RECORD HEADER.  THE ROUTINE LBLCHK WILL CALCULATE THE AMOUNT OF
C     * DATA IN THE DATA RECORD, WHICH IS READ BELOW.
C
  160 CONTINUE
      CALL RDBUF(NF,IBUF,LHEAD,OK)
      IF (.NOT. OK) RETURN
      OK=.FALSE.
C
C     * REARRANGE HEADER FOR 32-BIT MACHINES.  THE HEADER IS UNCHANGED
C     * FOR 64-BIT MACHINES.
C
      DO 1 I=1,8
        IBUF(I) = IBUF(IBUFPT(I,MACHINE))
    1 CONTINUE
      IF(LENDIAN()) THEN
C        IN THE OVERALL CONVERSION FROM BIG-ENDIAN UNFORMATTED RECORDS TO
C        LITTLE ENDIAN, HOLLERITH CONSTANTS HAVE BEEN INAPPROPRIATELY
C        REVERSED.  REVERSE THEM BACK.
        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     * MAKE SURE NEGATIVE LEVEL VALUES ARE INTERPRETED CORRECTLY.
C
C     CHECKME! AND CONVERT TO IBSET?
      IF(IBUF(4).GT.(2**30 + ((2**30)-1))) THEN
C     THIS ONLY WORKS FOR 64-BIT REAL TWOTO32 (OTHERWISE ROUNDING ERROR).
      TWOTO32 = 2.D0**32
C     THIS ONLY WORKS FOR 64-BIT NINT AND WILL CAUSE PROBLEMS FOR g77
C     (WHICH ONLY HAS 32-BIT NINT).
      IBUF(4)=IBUF(4)-NINT(TWOTO32)
      ENDIF
C
      IF( KIND.NE.-1 .AND.  KIND.NE.IBUF(1)) THEN
C         READ(NF,END=200,ERR=200)
C         INSTEAD, USE RDSKIP TO SKIP RECORDS.
         CALL RDSKIP(NF,NSKIP,OK)
         IF(.NOT.OK) GO TO 200
         GO TO 160
      ENDIF  
      IF(NAME.EQ.0 .OR. NAME.EQ.NC4TO8("NEXT")) GO TO 190
C 
      IF((NAME.NE.-1 .AND.  NAME.NE.NC4TO8("  -1"))
     1               .AND.  NAME.NE.IBUF(3)) THEN
C         READ(NF,END=200,ERR=200)
C         INSTEAD, USE RDSKIP TO SKIP RECORDS.
         CALL RDSKIP(NF,NSKIP,OK)
         IF(.NOT.OK) GO TO 200
         GO TO 160
      ENDIF  
      IF(NSTEP.NE.-1 .AND. NSTEP.NE.IBUF(2)) THEN
C         READ(NF,END=200,ERR=200)
C         INSTEAD, USE RDSKIP TO SKIP RECORDS.
         CALL RDSKIP(NF,NSKIP,OK)
         IF(.NOT.OK) GO TO 200
         GO TO 160
      ENDIF  
      IF(LEVEL.NE.-1 .AND. LEVEL.NE.IBUF(4)) THEN
C         READ(NF,END=200,ERR=200)
C         INSTEAD, USE RDSKIP TO SKIP RECORDS.
         CALL RDSKIP(NF,NSKIP,OK)
         IF(.NOT.OK) GO TO 200
         GO TO 160
      ENDIF  
C 
C     * RETURN WITH OK=.TRUE. WHEN REQUESTED RECORD IS FOUND. 
C     * STOP IF THE RECORD FOUND IS TOO LARGE FOR THE BUFFER. 
C 
  190 CONTINUE
C
      CALL LBLCHK(LEN,NWDS,NPACK,IBUF)

      IF(LEN-LHEAD.GT.MAXPK) THEN
        WRITE(6,6026)(IBUF(I),I=1,8),MAXPK 
        CALL XIT('RECGET',-1)
      END IF
C
C     * AT THIS POINT WE CAN READ IN THE DATA RECORD, SINCE WE NOW KNOW HOW
C     * LONG IT IS.
C
      LEN = LEN - LHEAD
      CALL RDBUF(NF,IBUF(9),LEN,OK)
      IF(LENDIAN()) THEN
        IMAX = LEN+8
        IF(IBUF(1).EQ.NC4TO8("LABL").OR.IBUF(1).EQ.NC4TO8("CHAR")) THEN
C          IN THE OVERALL CONVERSION FROM BIG-ENDIAN UNFORMATTED RECORDS TO
C          LITTLE ENDIAN, HOLLERITH CONSTANTS HAVE BEEN 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 BIG-ENDIAN UNFORMATTED RECORDS TO
C          LITTLE ENDIAN FOR INTSIZE = 2, THE BYTE-REVERSAL HAS BEEN 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 BIG-ENDIAN UNFORMATTED RECORDS TO
C            LITTLE ENDIAN FOR INTSIZE = 2, THE BYTE-REVERSAL HAS BEEN 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 BIG-ENDIAN UNFORMATTED RECORDS TO
C          LITTLE ENDIAN FOR MACHINE = 1, THE BYTE-REVERSAL HAS BEEN 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 GBYTESB TO VERIFY THIS) GBYTESB
C          EFFECTIVELY WORD SWAPS SO EVERYTHING WORKS OUT AND THERE IS NO
C          NEED FOR ADDITIONAL WORD SWAPPING HERE.
        ENDIF
      ENDIF
  200 CONTINUE
      RETURN
C---------------------------------------------------------------------
 6026 FORMAT('0',A4,I10,2X,A4,5I6,5X,' BUFFER IS TOO SMALL',I6)
      END 
