      SUBROUTINE UNPK_SECT6(KFILDO,IPACK,ND5,IS6,NS6,IB,ND2X3,
     1                      NX,NY,L3264B,LOCN,IPOS,IBITMAP,IER,
     2                      ISEVERE,*)
C
C        MARCH   2000   LAWRENCE   GSC/TDL    ORIGINAL CODING
C        JANUARY 2001   GLAHN      COMMENTS; CHANGED IER = 15 TO 608;
C                                  OMITTED EXISTS FROM CALL SEQUENCE;
C                                  ADDED IER = 601; ADDED TEST FOR
C                                  SECTION LENGTH WITH IER = 608
C        FEBRUARY 2001   GLAHN     CHECKED SIZE OF NS6; COMMENTS
C        NOVEMBER 2001   GLAHN     MOVED ISEVERE=2 TO TOP, CHANGED
C                                  IER=605 TO IER=608 ON IS6(6) TEST
C
C        PURPOSE
C            UNPACKS SECTION 6, THE BIT MAP SECTION, OF A GRIB2 MESSAGE.
C
C        DATA SET USE
C           KFILDO - UNIT NUMBER FOR OUTPUT (PRINT) FILE. (OUTPUT
C                    FILE)
C
C        VARIABLES
C              KFILDO = UNIT NUMBER FOR OUTPUT (PRINT) FILE. (INPUT)
C            IPACK(J) = THE ARRAY THAT HOLDS THE ACTUAL PACKED MESSAGE
C                       (J=1,ND5). (INPUT/OUTPUT)
C                 ND5 = THE SIZE OF THE ARRAY IPACK( ). (INPUT)
C              IS6(J) = THE BIT-MAP DATA THAT IS UNPACKED FROM
C                       IPACK( ) IS PLACED INTO THIS ARRAY (J=1,NS6).
C                       (OUTPUT)
C                 NS6 = SIZE OF IS6( ). (INPUT)
C               IB(J) = THE UNPACKED BIT-MAP GOES IN HERE.
C                       (J=1,ND2X3). (OUTPUT)
C               ND2X3 = THE DIMENSIONS OF IB( ). (INPUT)
C               NX,NY = THE ACTUAL NUMBER OF ROWS AND COLUMNS IN
C                       THIS PRODUCT. (INPUT)
C              L3264B = THE INTEGER WORD LENGTH IN BITS OF THE MACHINE
C                       BEING USED. VALUES OF 32 AND 64 ARE
C                       ACCOMMODATED. (INPUT)
C                LOCN = THE WORD POSITION FROM WHICH TO UNPACK THE
C                       NEXT VALUE. (INPUT/OUTPUT)
C                IPOS = THE BIT POSITION IN LOCN FROM WHICH TO START
C                       UNPACKING THE NEXT VALUE.  (INPUT/OUTPUT)
C             IBITMAP = 1 IF THERE WAS A BIT-MAP IN THIS SECTION.
C                       0 IF THERE WAS NOT A BIT-MAP IN THIS
C                       SECTION.  (OUTPUT)
C                 IER = RETURN STATUS CODE. (OUTPUT)
C                         0 = GOOD RETURN.
C                       6-8 = ERROR CODES GENERATED BY UNPKBG. SEE THE
C                             DOCUMENTATION IN THE PKBG ROUTINE.
C                       601 = IS5(5) DOES NOT INDICATE SECTION 6.
C                       602 = IS6( ) HAS NOT BEEN DIMENSIONED LARGE
C                             ENOUGH TO CONTAIN THE ENTIRE TEMPLATE. 
C                       605 = DIMENSION ND2X3 NOT LARGE ENOUGH FOR
C                             IB(NX*NY)
C                       608 = BIT MAP INDICATOR IN IS6(6) NOT IN CORRECT
C                             RANGE.
C                       699 = UNEXPECTED END OF MESSAGE.
C             ISEVERE = THE SEVERITY LEVEL OF THE ERROR.  THE ONLY
C                       VALUE RETUNED IS:
C                       2 = A FATAL ERROR  (OUTPUT)
C                   * = ALTERNATE RETURN WHEN IER NE 0.
C
C             LOCAL VARIABLES
C             LOCN6_1 = SAVES THE WORD POSITION LOCN IN IPACK
C                       UPON ENTRY TO STORE BACK TO LOCN IN CASE
C                       THERE IS A FATAL ERROR.
C             IPOS6_1 = SAVES THE BIT POSITION IPOS IN LOCN
C                       UPON ENTRY TO STORE BACK TO IPOS IN CASE
C                       THERE IS A FATAL ERROR.
C               LSECT = CONTAINS THE LENGTH OF SECTION 6
C                       AS UNPACKED FROM THE FIRST FOUR
C                       BYTES IN THE SECTION.
C               NSECT = SECTION NUMBER.
C                   K = A LOOPING INDEX VARIABLE.
C                   N = L3264B = THE INTEGER WORD LENGTH IN BITS OF
C                       THE MACHINE BEING USED. VALUES OF 32 AND
C                       64 ARE ACCOMMODATED.
C
C        NON SYSTEM SUBROUTINES CALLED
C           ENDOMESS, UNPKBG, UNPKBMAP
C
      DIMENSION IPACK(ND5),IS6(NS6),IB(ND2X3)
      LOGICAL ENDOMESS
C
      N=L3264B
      IER=0
C
C        ALL ERRORS GENERATED BY THIS ROUTINE ARE FATAL.
      ISEVERE=2
C
C        CHECK THE DIMENSIONS OF IS6( ).
C
      IF(NS6.LT.6)THEN
         IER=602
         GO TO 900
      ENDIF
C
      LOCN6_1=LOCN
      IPOS6_1=IPOS
C
C        UNPACK THE LENGTH OF THE SECTION, LSECT.
      CALL UNPKBG(KFILDO,IPACK,ND5,LOCN,IPOS,LSECT,32,N,IER,*900)
C
C        CHECK FOR AN UNEXPECTED END OF MESSAGE,
C        ACCOMMODATING FOR A 64-BIT WORD.
C
      IF(ENDOMESS(LSECT,N))THEN
         IER=699
         GO TO 900
      ENDIF
C
C        UNPACK THE NUMBER OF THE SECTION, NSECT. CHECK
C        TO MAKE SURE THAT THIS IS SECTION 6.
      CALL UNPKBG(KFILDO,IPACK,ND5,LOCN,IPOS,NSECT,8,N,IER,*900)
C
      IF(NSECT.NE.6)THEN
         IER=601
         LOCN=LOCN6_1
         IPOS=IPOS6_1
         GO TO 900
      ENDIF
C
      DO K=1,NS6
         IS6(K)=0
      ENDDO
C
      IS6(1)=LSECT
      IS6(5)=NSECT
C
C        UNPACK THE BIT-MAP INDICATOR.
      CALL UNPKBG(KFILDO,IPACK,ND5,LOCN,IPOS,IS6(6),8,N,IER,*900)
C
C        DO A BOUNDS CHECK ON THE BIT-MAP INDICATOR.
C
      IF((IS6(6).LT.0).OR.(IS6(6).GT.255))THEN
         IER=608
      ELSE
C
C           IS THERE A BIT-MAP TO UNPACK?
C
         IF(IS6(6).EQ.0)THEN
C
            IF(ND2X3.LT.NX*NY)THEN
               IER=605
               GO TO 900
            ENDIF
C
            CALL UNPKBMAP(KFILDO,IPACK,ND5,IB,NX*NY,N,LOCN,IPOS,
     1                    IER,*900)
            IBITMAP=1
         ELSE
            IBITMAP=0
         ENDIF
C
      ENDIF
C
C        ERROR RETURN SECTION
 900  IF(IER.NE.0)RETURN 1
C
      RETURN
      END
