      SUBROUTINE LBLCHK(LEN,NWDS,NPACK,IBUF) 
C 
C     * JUL 07/03 - A.IRWIN,F.MAJAESS (REVISED FOR F90 AND LINUX PLATFORMS)
C     * MAR 05/02 - F.MAJAESS- REVISE FOR "CHAR" KIND RECORDS.
C     * AUG 31/92 - E.CHAN   - REVISE CALCULATION OF LEN WHEN IBUF(8)=0.
C     * MAY 22/92 - J.STACEY - LBLCHK3 RENAMED TO LBLCHK FOR RELEASE.
C     * MAY 21/92 - J.STACEY - MODIFIED TO ACCEPT IBUF IN NATIVE MACHINE
C     *                        INTEGER WORDS FORMAT.
C     * APR 15/92 - J.STACEY - RENAMED FROM LBLCHK TO LBLCHK3.
C     * APR  1/92 - J.STACEY - MODIFIED TO RUN ON EITHER 32- OR 64-BIT
C     *                        COMPUTERS.  NOTE THAT IBUF *MUST* BE
C     *                        IN CCRD/N STANDARD 64-BIT FORMAT BEFORE
C     *                        ENTERING THIS ROUTINE.
C     * JAN 20/84 - R.LAPRISE, J.F.FORTIN.
C     * CHECKS STANDARD 8 WORDS FILE LABEL (IBUF) AND RETURNS...
C     * LEN  =  TOTAL LENGTH OF THE RECORD (IN INTEGERS). 
C     * NWDS =  LENGTH OF THE UNPACKED FIELD (REAL WORDS).
C     * NPACK=  PACKING DENSITY.
C 
      IMPLICIT REAL (A-H,O-Z),
     +INTEGER (I-N)
      DIMENSION IBUF(*)
      LOGICAL OK, FOUR, SPEC
      COMMON /MACHTYP/ MACHINE,INTSIZE
      DATA NBW, LHEAD/64,8/ 
C ------------------------------------------------------------------
C     * STOP IF NPACK IS NOT BETWEEN 1 AND INT(NBW/2).
C 
      NPACK = IABS (IBUF(8))
      OK = (NPACK .GE. 0 .AND. NPACK .LE. NBW/2)
      IF (.NOT. OK) WRITE(6,6025) (IBUF(I),I=1,8)
      IF (.NOT. OK) CALL XIT ('LBLCHK',-1)
C 
C     * DETERMINE THE SIZE OF THE FIELD IN REAL WORDS.
C     * (FOURIER AND SPECTRAL ARE COMPLEX). 
C 
      FOUR = (IBUF(1) .EQ. NC4TO8("FOUR"))
      SPEC = (IBUF(1) .EQ. NC4TO8("SPEC"))
      NWDS =  IBUF(5) * IBUF(6) 
      IF (SPEC .OR. FOUR) NWDS = NWDS * 2 
      IF(IBUF(1).EQ.NC4TO8("LABL")) NWDS=10
C 
C     * COMPUTE THE LENGTH OF THE COMPLETE RECORD IMAGE.
C     * THE LENGTH IS FIRST CALCULATED IN UNITS OF 64-BIT WORDS WITH
C     * A PACKING FACTOR "NPACK64", THEN SCALED BY THE MACHINE TYPE.
C 
      NBITS   = NBW/MAX(1,NPACK)
      NPACK32 = 32/NBITS         !IF NBITS=64, THEN NPACK32 BECOMES 0.
      NPACK64 = MAX(1,2*NPACK32) !IF NPACK32 IS ZERO, THEN NPACK64=1.
C
C     * SPECTRAL FIELDS ARE HANDLED SOMEWHAT DIFFERENTLY IN THAT THE (0,0)
C     * COEFFICIENT IS NOT PACKED BUT IS RATHER STORED AS A REAL VALUE
C     * (SINCE THE IMAGINARY PART IS ALWAYS ZERO, THERE IS NO NEED TO WASTE
C     * EXTRA SPACE) FOLLOWING THE MIN/MAX VALUES. HENCE LENWDS AND LEN ARE
C     * ADJUSTED ACCORDINGLY.
C
      IF(SPEC)                                 THEN
        LENWDS  = (((NWDS-1)/NPACK64) +
     1            ((MOD((NWDS-1),NPACK64)+NPACK64-1)/NPACK64))
        LEN = LHEAD + LENWDS + 3
      ELSE
        LENWDS  = ((NWDS/NPACK64) +
     1            ((MOD(NWDS,NPACK64)+NPACK64-1)/NPACK64))
        LEN = LHEAD + LENWDS + 2
      ENDIF
C
C     * THE HEADER IS ALWAYS IN 64-BIT FORMAT, BUT IF NPACK=0, DATA 
C     * IS STORED IN NATIVE MACHINE FORMAT, WHICH MAY BE 32 OR 64 BIT DATA.
C     * LEN IS THE TOTAL LENGTH OF THE RECORD IN TERMS OF INTEGERS. THIS 
C     * NUMBER IS REQUIRED IN LOW LEVEL I/O ROUTINES SINCE I/O IS DONE
C     * USING INTEGER ARRAYS. CONSEQUENTLY, MACHINE AND INTSIZE APPEAR
C     * IN THE CALCULATION IN ORDER TO ENSURE PORTABILITY OF CODE ACROSS
C     * DIFFERENT MACHINES AND COMPILERS. INTEGERS AND REALS MAY BE 32 OR
C     * 64-BIT.
C
      IF (NPACK .EQ. 0) THEN
       IF(IBUF(1).EQ.NC4TO8("CHAR")) THEN
        LEN   = LHEAD*MACHINE + NWDS*MACHINE
       ELSE
        LEN   = LHEAD*MACHINE + NWDS*INTSIZE
       ENDIF
      END IF
      IF (NPACK .EQ. 1) THEN
        LEN = LHEAD + NWDS
      END IF
      IF (NPACK .NE. 0)  LEN = LEN*MACHINE
      RETURN
C ------------------------------------------------------------------
 6025 FORMAT (' ',A4,I10,2X,A4,5I6) 
      END 
