       SUBROUTINE MUNPK(A,LDA,MA,MVALUE,N,ERSTAT)
C
C **********************************************************************
C
C    NAME- MUNPK
C
C    LANGUAGE- VS FORTRAN        TYPE-SUBROUTINE
C
C    VERSION-1.0   DATE-10/18/90     PROGRAMMER-T. SIMMS, SMSRC
C    VERSION-2.0   DATE-07/19/94     PROGRAMMER- FRANK TILLEY (SMSRC)
C        ATOVS-12  EXPLICITLY DECLARED I*4 VARIABLE "LDA".
C    VERSION-3.0   DATE-06/04/96     PROGRAMMER- TIM GARDNER (HSTX)
C
C    FUNCTION-RESTORES THE ROWS AND COLUMNS CORRESPONDING TO UNSAMPLED
C              DATA OBSERVATIONS INTO MATRIX A
C
C   VERSION 3.0- DOCUMENTATION AND SYNTAX CHANGES FOR ATOVS BASELINE
C                ON THE CRAY.
C
C    CALLING PARAMETERS-
C
C    NAME    TYPE   I/O  DESCRIPTION
C   ------   ----   ---  ----------------------------------
C     A      REAL    I/O  MATRIX THAT IS UNPACKED
C     LDA    INT     I   LEADING DIMENSION OF MATRIX, AS DECLARED
C                              BY THE CALLING ROUTINE
C     MA     INT    I/O  SIZE OF MATRIX A
C   MVALUE   INT     I   ORDERED LIST ROW & COL #'S CORRESPONDING TO
C                            UNSAMPLED DATA OBSERVATIONS
C     N      INT     I   # OF UNSAMPLED DATA OBSERVATIONS
C     ERSTAT INT     O   ERROR STATUS CODE RETURNED FROM MUNPK$
C                          99=A FATAL ERROR
C
C     LOCAL PARAMETERS-
C
C      NAME    TYPE        DESCRIPTION
C     ------   ----        ----------------------------------
C     I        INT         DO LOOP INDEX
C     J        INT         DO LOOP INDEX
C     K        INT         DO LOOP INDEX
C     IBEG     INT         DO LOOP INDEX
C     LM       INT         SIZE OF OUTPUT MATRIX
C
C     SUBPROGRAMS CALLED - MUNPK$
C
C     EXIT STATES - THE ROUTINE WILL RETURN IF THE NUMBER OF
C     UNSAMPLED OBSERVATIONS IS NEGATIVE OR IF MUNPK$ RETURNS
C     A FATAL ERROR
C
C     ASSUMPTIONS - THE LIST OF ROWS/COLUMNS IN VECTOR MVALUE ARE
C     ASSUMED TO BE IN INCREASING ORDER.  IF THEY ARE NOT, THIS
C     ROUTINE WILL NOT FUNCTION PROPERLY.
C
C **********************************************************************
C
      INTEGER N,MVALUE(N),IBEG,LM,MA,I,J,K,ERSTAT,LDA
      REAL A(LDA,LDA)
C
      DATA IBEG /0/, LM /0/
C
C  CHECK THAT N IS GREATER THAN 0
C
      IF (N .LE. 0)  RETURN
C
C   COMPUTE THE SIZE OF THE OUTPUT MATRIX
      LM=MA+N
C
C   IF OUTPUT MATRIX TOO LARGE FOR WORKSPACE, WRITE ERROR MESSAGE TO
C   KIDS MESSAGE LOG FILE
      IF (LM .GT. LDA) THEN
         CALL MUNPK$(304,LM,LDA,ERSTAT)
         RETURN
      ENDIF
C
      DO 60 K=1,N
C
C            SET IBEG = ROW,COL # OF THE MISSING VALUE AND EXPAND MATRIX
C            AFTER FIRST CHECKING TO ENSURE THAT IT IS VALID, SKIP IF
C            NOT VALID
C
          IBEG=MVALUE(K)
          IF ((IBEG.GT.0) .AND. (IBEG.LE.LM)) THEN
            MA=MA+1
C
C           SHIFT COLUMNS OF MATRIX & REPLACE MISSING COLUMNS WITH 0'S
C
            DO 20 J=MA,IBEG,-1
               DO 10 I=1,MA
C
C                      FOR THE MISSING VALUE REPLACE COL WITH 0'S
C
                  IF (J.EQ.IBEG) THEN
                    A(I,J)=0.
                  ELSE
C
C                    OTHERWISE REPLACE COL WITH VALUE OF COL TO THE LEFT
C
                    A(I,J)=A(I,J-1)
                  ENDIF
   10          CONTINUE
   20       CONTINUE
C
C                   SHIFT ROWS OF MATRIX & REPLACE MISSING ROWS WITH 0'S
C
            DO 50 I=MA,IBEG,-1
               DO 40 J=1,MA
                  IF (I.EQ.IBEG) THEN
                    A(I,J)=0.
                  ELSE
                    A(I,J)=A(I-1,J)
                  ENDIF
   40          CONTINUE
   50       CONTINUE
         ELSE
         ENDIF
   60 CONTINUE
C
      RETURN
      END
C*********************************************************************
      SUBROUTINE MUNPK$(MSGCOD,OUTSZE,WKSIZE,ERSTAT)
C*********************************************************************
C
C   NAME- MUNPK$
C
C   LANGUAGE- VS FORTRAN   TYPE- SUBROUTINE        COMPUTER- CRAY J916
C
C   VERSION- 1.0    DATE- 11/14/91    PROGRAMMER- TERESA SIMMS, SMSRC
C   VERSION- 3.0    DATE- 06/04/96    PROGRAMMER- TIM GARDNER (HSTX)
C   VERSION- 3.1    DATE- 10/07/97    PROGRAMMER- LEAH WILSON (HSTX)
C
C   FILES-
C     DSN                       I/O  UNIT  FUNCTIONAL DESCRIPTION
C     ---                       ---  ----  -----------------------
C     ...KIDS                 O    KUNIT   KRAY INTERACTIVE DOCUMENTATION
C
C   FUNCTION- THIS SUBROUTINE IS THE ERROR HANDLING ROUTINE FOR
C             ROUTINE MUNPK
C
C   VERSION 3.0- DOCUMENTATION AND SYNTAX CHANGES FOR ATOVS BASELINE
C                ON THE CRAY.
C   VERSION 3.1- WRITE ERROR MESSAGES TO THE KIDS FILE.  INCLUDE /SPGUNT/.
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE               TYPE  I/O  FUNCTION
C     --------               ----  ---  --------
C     MSGCOD                 INT    I   MESSAGE CODE USED TO
C                                       IDENTIFY THE ERROR PATTERN
C     OUTSZE                 INT    I   SIZE OF OUTPUT MATRIX
C                                       COMPUTED BY MUNPK
C     ASIZE                  INT    I   SIZE OF WORKSPACE MATRIX
C     ERSTAT                 INT    O   STATUS CODE IF AN ERROR
C                                       OCCURS DURING I/O
C
      INTEGER   MSGCOD, OUTSZE, WKSIZE, ERSTAT
C
C   COMMON AREA PARAMETERS-
C
      INCLUDE 'SPGUNT'
      INCLUDE 'ERPARM'
      INCLUDE 'ERINFO'
C
C   LOCAL PARAMETERS-
C     VARIABLE                TYPE    FUNCTION
C     --------                ----    --------
C     I                       INT     LOOP COUNTER
C     KMSG                   CH*100   KIDS MESSAGE BUFFER
C     C4TEMP                  CH*4    HOLDING BUFFER FOR WRITING TO KIDS
C
C   SUBROUTINES CALLED- KIDS
C
C   EXIT STATES-
C       ERSTAT =  0; INDICATING NO OR NON-FATAL ERRORS FOUND
C       ERSTAT = 99; INDICATING A FATAL ERROR WAS DETECTED
C
C*********************************************************************
C
      INTEGER   I
      CHARACTER*4   C4TEMP
      CHARACTER*100 KMSG
C
C  SET ERINFO:SUBNAM
C
      SUBNAM = 'SPGDVR'
C
C   INITIALIZE VARIABLES
C
      ERSTAT=0
C
C  CHECK FOR VALID MSGCOD VALUES
C
      IF(MSGCOD.EQ.304) THEN
        KMSG(1:50)=' * NOT ENOUGH WORKSPACE * Computed size of output '
        KMSG(51:100)='matrix      is greater than workspace size     .'
        WRITE(C4TEMP,'(I4)') OUTSZE
        KMSG(58:61) = C4TEMP
        WRITE(C4TEMP,'(I4)') WKSIZE
        KMSG(93:96) = C4TEMP
        STAT = 99
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C
      ELSE
C
C NOT A VALID MSGCOD
C
        STAT=0
        KMSG = ' Invalid error message code = '
        WRITE(C4TEMP,'(I4)') MSGCOD
        KMSG(31:34) = C4TEMP
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
      ENDIF
      RETURN
      END


