      SUBROUTINE UNPK_SECT4(KFILDO,IPACK,ND5,IS4,NS4,L3264B,
     1                      LOCN,IPOS,IER,ISEVERE,*)
C
C        MARCH    2000   LAWRENCE  GSC/TDL    ORIGINAL CODING
C        JANUARY  2001   GLAHN     COMMENTS; CHANGED TO STANDARD 
C                                  RETURN SEQUENCE; ADDED IER = 402;
C                                  OMITTED EXISTS FROM CALL SEQUENCE
C        FEBRUARY 2001   GLAHN     CHECKED SIZE OF NS4; COMMENTS
C        NOVEMBER 2001   GLAHN     IER=1 CHANGED TO IER=3; REMOVED
C                                  DUPLICATE CHECK ON NS4; MOVED
C                                  IER AND ISEVERE TO TOP
C        JULY     2002   RUDACK    ADDED ADDTIONAL CASE STATEMENT TO
C                                  ACCOMMODATE TEMPLATE 4.9.
C
C        PURPOSE
C            UNPACKS SECTION 4, THE PRODUCT DEFINITION SECTION,
C            OF A GRIB2 MESSAGE.
C
C            THIS ROUTINE IS CAPABLE OF UNPACKING THE FOLLOWING
C            PRODUCT DEFINITION TEMPLATES:
C               TEMPLATE 4.0  ANALYSIS OR FORECAST AT A LEVEL AND POINT
C               TEMPLATE 4.1  INDIVIDUAL ENSEMBLE
C               TEMPLATE 4.2  DERIVED FORECAST BASED ON ENSEMBLES
C               TEMPLATE 4.8  AVERAGE, ACCUMULATION, EXTREMES
C               TEMPLATE 4.9  PROBABILITY FORECASTS AT A HORIZONTAL LEVEL
C               TEMPLATE 4.20 RADAR
C               TEMPLATE 4.30 SATELLITE
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              IS4(J) = THE GRID DEFINITION DATA THAT IS UNPACKED FROM
C                       IPACK( ) IS PLACED INTO THIS ARRAY (J=1,NS4).
C                       (OUTPUT)
C                 NS4 = SIZE OF IS4( ). (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                 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                       401 = IS4(5) DOES NOT INDICATE SECTION 4.
C                       402 = IS4( ) HAS NOT BEEN DIMENSIONED LARGE
C                             ENOUGH TO CONTAIN THE TEMPLATE. 
C                       403 = IS4(8) DOES NOT CONTAIN A SUPPORTED 
C                             TEMPLATE NUMBER.
C                       499 = 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             LOCN4_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             IPOS4_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 4
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, UNPK_TEMP40, UNPK_TEMP41, UNPK_TEMP42,
C           UNPK_TEMP48, UNPK_TEMP420, UNPK_TEMP430, UNPK_TEMP49
C
      LOGICAL ENDOMESS
C
      DIMENSION IPACK(ND5),IS4(NS4)
C
      IER=0
      N=L3264B
C
C        ALL ERRORS GENERATED BY THIS ROUTINE ARE FATAL.
      ISEVERE=2
C
C        CHECK SIZE OF IS4( ).
C
      IF(NS4.LT.8)THEN
         IER=402
         GO TO 900
      ENDIF
C
C        UNPACK THE LENGTH OF THE SECTION, LSECT.
      LOCN4_1=LOCN
      IPOS4_1=IPOS
      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.
      IF(ENDOMESS(LSECT,N))THEN
         IER=499
         GO TO 900
      ENDIF
C
C        UNPACK THE NUMBER OF THE SECTION, NSECT. CHECK
C        TO MAKE SURE THAT THIS IS SECTION 4.
      CALL UNPKBG(KFILDO,IPACK,ND5,LOCN,IPOS,NSECT,8,N,IER,*900)
C
      IF(NSECT.NE.4)THEN
         IER=401
         LOCN=LOCN4_1
         IPOS=IPOS4_1
         GO TO 900
      ENDIF
C
      DO K=1,NS4
         IS4(K)=0
      ENDDO
C
      IS4(1)=LSECT
      IS4(5)=NSECT
C
C        UNPACK THE NUMBER OF COORDINATES VALUES AFTER
C        THE TEMPLATE.  THIS VALUE IS NOT USED IN THIS SOFTWARE.
      CALL UNPKBG(KFILDO,IPACK,ND5,LOCN,IPOS,IS4(6),16,N,IER,*900)
C
C        UNPACK PRODUCT DEFINITION TEMPLATE NUMBER
      CALL UNPKBG(KFILDO,IPACK,ND5,LOCN,IPOS,IS4(8),16,N,IER,*900)
C
c      SELECT CASE(IS4(8))
C
c      CASE(0)
       IF (IS4(8).EQ.0) THEN
C
C            AN ANALYSIS OR FORECAST AT A HORIZONTAL
C            LEVEL OR IN A HORIZONTAL LAYER AT A POINT
C            IN TIME (TEMPLATE 4.0).
         CALL UNPK_TEMP40(KFILDO,IPACK,ND5,IS4,NS4,L3264B,
     1                    LOCN,IPOS,IER,*900)
C
c      CASE(1)
       ELSEIF (IS4(8).EQ.1) THEN
C
C            AN INDIVIDUAL ENSEMBLE FORECAST, CONTROL AND
C            PERTURBED, AT A HORIZONTAL LEVEL OR IN A
C            HORIZONTAL LEVEL OR IN A HORIZONTAL LAYER AT
C            A POINT IN TIME (TEMPLATE 4.1).
         CALL UNPK_TEMP41(KFILDO,IPACK,ND5,IS4,NS4,L3264B,
     1                    LOCN,IPOS,IER,*900)
C
c      CASE(2)
       ELSEIF (IS4(8).EQ.2) THEN
C
C            A DERIVED FORECAST BASED ON ALL ENSEMBLE MEMBERS
C            AT A HORIZONTAL LEVEL OR IN A HORIZONTAL LAYER
C            AT A POINT IN TIME (TEMPLATE 4.2).
         CALL UNPK_TEMP42(KFILDO,IPACK,ND5,IS4,NS4,L3264B,
     1                    LOCN,IPOS,IER,*900)
C
c      CASE(8)
       ELSEIF (IS4(8).EQ.8) THEN
C
C           AVERAGE, ACCUMULATION, AND/OR EXTREME VALUES AT A
C           HORIZONTAL LEVEL OR IN A HORIZONTAL LAYER IN A
C           CONTINUOUS OR NON-CONTINUOUS TIME INTERVAL. 
         CALL UNPK_TEMP48(KFILDO,IPACK,ND5,IS4,NS4,L3264B,
     1                    LOCN,IPOS,IER,*900)
C
c      CASE(9)
       ELSEIF (IS4(8).EQ.9) THEN
C
C            A PROBABILITY FORECAST AT A HORIZONTAL LEVEL
C            OR IN A HORIZONTAL LAYER IN A CONTINUOUS OR
C            NON-CONTINUOUS TIME INTERVAL. (TEMPLATE 4.9)
C            (POP12)
         CALL UNPK_TEMP49(KFILDO,IPACK,ND5,IS4,NS4,L3264B,
     1                    LOCN,IPOS,IER,*900)
C
c      CASE(20)
       ELSEIF (IS4(8).EQ.20) THEN
C
C            A RADAR PRODUCT (TEMPLATE 4.20).
         CALL UNPK_TEMP420(KFILDO,IPACK,ND5,IS4,NS4,L3264B,
     1                     LOCN,IPOS,IER,*900)
C
c      CASE(30)
       ELSEIF (IS4(8).EQ.30) THEN
C
C            A SATELLITE PRODUCT (TEMPLATE 4.30).
         CALL UNPK_TEMP430(KFILDO,IPACK,ND5,IS4,NS4,L3264B,
     1                     LOCN,IPOS,IER,*900)
C
c      CASE DEFAULT 
       ELSE
C
C            THE PRODUCT TEMPLATE IS NOT SUPPORTED.
         IER = 403
C
c      END SELECT
       ENDIF
C
C       ERROR RETURN SECTION
C
 900  IF(IER.NE.0)RETURN 1
C
      RETURN
      END
