      SUBROUTINE DataSmoothSG (dataset, numdat, smoothnum, derivnum,
     +   smoothdata)
      INCLUDE 'STDHDR.FOR'
      REAL dataset(0:maxv), smoothdata(0:maxv)
      INTEGER numdat, smoothnum, derivnum


      INTEGER normcoef(0: 2, 0: 4)
      INTEGER smoothcoef[ALLOCATABLE](:,:,:)
      INTEGER numcoef, smoothdeg, startconv
      INTEGER stopconv, i, j, k, iErr

      ALLOCATE(smoothcoef(0 : 2, 0 : 4, 0 : 12), STAT=iErr)
      CALL CheckMem(iErr)

      normcoef(0, 0) = 35
      normcoef(0, 1) = 21
      normcoef(0, 2) = 231
      normcoef(0, 3) = 429
      normcoef(0, 4) = 143
      normcoef(1, 0) = 10
      normcoef(1, 1) = 28
      normcoef(1, 2) = 60
      normcoef(1, 3) = 110
      normcoef(1, 4) = 182
      normcoef(2, 0) = 7
      normcoef(2, 1) = 42
      normcoef(2, 2) = 462
      normcoef(2, 3) = 429
      normcoef(2, 4) = 1001


      smoothcoef(0, 0, 0) = -3
      smoothcoef(0, 0, 1) = 12
      smoothcoef(0, 0, 2) = 17
      smoothcoef(0, 0, 3) = 12
      smoothcoef(0, 0, 4) = -3
      smoothcoef(0, 0, 5) = 0
      smoothcoef(0, 0, 6) = 0
      smoothcoef(0, 0, 7) = 0
      smoothcoef(0, 0, 8) = 0
      smoothcoef(0, 0, 9) = 0
      smoothcoef(0, 0, 10) = 0
      smoothcoef(0, 0, 11) = 0
      smoothcoef(0, 0, 12) = 0


      smoothcoef(0, 1, 0) = -2
      smoothcoef(0, 1, 1) = 3
      smoothcoef(0, 1, 2) = 6
      smoothcoef(0, 1, 3) = 7
      smoothcoef(0, 1, 4) = 6
      smoothcoef(0, 1, 5) = 3
      smoothcoef(0, 1, 6) = -2
      smoothcoef(0, 1, 7) = 0
      smoothcoef(0, 1, 8) = 0
      smoothcoef(0, 1, 9) = 0
      smoothcoef(0, 1, 11) = 0
      smoothcoef(0, 1, 11) = 0
      smoothcoef(0, 1, 12) = 0

      smoothcoef(0, 2, 0) = -21
      smoothcoef(0, 2, 1) = 14
      smoothcoef(0, 2, 2) = 39
      smoothcoef(0, 2, 3) = 54
      smoothcoef(0, 2, 4) = 59
      smoothcoef(0, 2, 5) = 54
      smoothcoef(0, 2, 6) = 39
      smoothcoef(0, 2, 7) = 14
      smoothcoef(0, 2, 8) = -21
      smoothcoef(0, 2, 9) = 0
      smoothcoef(0, 2, 10) = 0
      smoothcoef(0, 2, 11) = 0
      smoothcoef(0, 2, 12) = 0

      smoothcoef(0, 3, 0) = -36
      smoothcoef(0, 3, 1) = 9
      smoothcoef(0, 3, 2) = 44
      smoothcoef(0, 3, 3) = 69
      smoothcoef(0, 3, 4) = 84
      smoothcoef(0, 3, 5) = 89
      smoothcoef(0, 3, 6) = 84
      smoothcoef(0, 3, 7) = 69
      smoothcoef(0, 3, 8) = 44
      smoothcoef(0, 3, 9) = 9
      smoothcoef(0, 3, 10) = -36
      smoothcoef(0, 3, 11) = 0
      smoothcoef(0, 3, 12) = 0


      smoothcoef(0, 4, 0) = -11
      smoothcoef(0, 4, 1) = 0
      smoothcoef(0, 4, 2) = 9
      smoothcoef(0, 4, 3) = 16
      smoothcoef(0, 4, 4) = 21
      smoothcoef(0, 4, 5) = 24
      smoothcoef(0, 4, 6) = 25
      smoothcoef(0, 4, 7) = 24
      smoothcoef(0, 4, 8) = 21
      smoothcoef(0, 4, 9) = 16
      smoothcoef(0, 4, 10) = 9
      smoothcoef(0, 4, 11) = 0
      smoothcoef(0, 4, 12) = -11

      smoothcoef(1, 0, 0) = -2
      smoothcoef(1, 0, 1) = -1
      smoothcoef(1, 0, 2) = 0
      smoothcoef(1, 0, 3) = 1
      smoothcoef(1, 0, 4) = 2
      smoothcoef(1, 0, 5) = 0
      smoothcoef(1, 0, 6) = 0
      smoothcoef(1, 0, 7) = 0
      smoothcoef(1, 0, 8) = 0
      smoothcoef(1, 0, 9) = 0
      smoothcoef(1, 0, 10) = 0
      smoothcoef(1, 0, 11) = 0
      smoothcoef(1, 0, 12) = 0

      smoothcoef(1, 1, 0) = -3
      smoothcoef(1, 1, 1) = -2
      smoothcoef(1, 1, 2) = -1
      smoothcoef(1, 1, 3) = 0
      smoothcoef(1, 1, 4) = 1
      smoothcoef(1, 1, 5) = 2
      smoothcoef(1, 1, 6) = 3
      smoothcoef(1, 1, 7) = 0
      smoothcoef(1, 1, 8) = 0
      smoothcoef(1, 1, 9) = 0
      smoothcoef(1, 1, 10) = 0
      smoothcoef(1, 1, 11) = 0
      smoothcoef(1, 1, 12) = 0

      smoothcoef(1, 2, 0) = -4
      smoothcoef(1, 2, 1) = -3
      smoothcoef(1, 2, 2) = -2
      smoothcoef(1, 2, 3) = -1
      smoothcoef(1, 2, 4) = 0
      smoothcoef(1, 2, 5) = 1
      smoothcoef(1, 2, 6) = 2
      smoothcoef(1, 2, 7) = 3
      smoothcoef(1, 2, 8) = 4
      smoothcoef(1, 2, 0) = 0
      smoothcoef(1, 2, 10) = 0
      smoothcoef(1, 2, 11) = 0
      smoothcoef(1, 2, 12) = 0


      smoothcoef(1, 3, 0) = -5
      smoothcoef(1, 3, 1) = -4
      smoothcoef(1, 3, 2) = -3
      smoothcoef(1, 3, 3) = -2
      smoothcoef(1, 3, 4) = -1
      smoothcoef(1, 3, 5) = 0
      smoothcoef(1, 3, 6) = 1
      smoothcoef(1, 3, 7) = 2
      smoothcoef(1, 3, 8) = 3
      smoothcoef(1, 3, 9) = 4
      smoothcoef(1, 3, 10) = 5
      smoothcoef(1, 3, 11) = 0
      smoothcoef(1, 3, 12) = 0


      smoothcoef(1, 4, 0) = -6
      smoothcoef(1, 4, 1) = -5
      smoothcoef(1, 4, 2) = -4
      smoothcoef(1, 4, 3) = -3
      smoothcoef(1, 4, 4) = -2
      smoothcoef(1, 4, 5) = -1
      smoothcoef(1, 4, 6) = 0
      smoothcoef(1, 4, 7) = 1
      smoothcoef(1, 4, 8) = 2
      smoothcoef(1, 4, 9) = 3
      smoothcoef(1, 4, 10) = 4
      smoothcoef(1, 4, 11) = 5
      smoothcoef(1, 4, 12) = 6


      smoothcoef(2, 0, 0) = 2
      smoothcoef(2, 0, 1) = -1
      smoothcoef(2, 0, 2) = -2
      smoothcoef(2, 0, 3) = -1
      smoothcoef(2, 0, 4) = 2
      smoothcoef(2, 0, 5) = 0
      smoothcoef(2, 0, 6) = 0
      smoothcoef(2, 0, 7) = 0
      smoothcoef(2, 0, 8) = 0
      smoothcoef(2, 0, 9) = 0
      smoothcoef(2, 0, 10) = 0
      smoothcoef(2, 0, 11) = 0
      smoothcoef(2, 0, 12) = 0


      smoothcoef(2, 1, 0) = 5
      smoothcoef(2, 1, 1) = 0
      smoothcoef(2, 1, 2) = -3
      smoothcoef(2, 1, 3) = -4
      smoothcoef(2, 1, 4) = -3
      smoothcoef(2, 1, 5) = 0
      smoothcoef(2, 1, 6) = 5
      smoothcoef(2, 1, 7) = 0
      smoothcoef(2, 1, 8) = 0
      smoothcoef(2, 1, 9) = 0
      smoothcoef(2, 1, 10) = 0
      smoothcoef(2, 1, 11) = 0
      smoothcoef(2, 1, 12) = 0


      smoothcoef(2, 2, 0) = 28
      smoothcoef(2, 2, 1) = 7
      smoothcoef(2, 2, 2) = -8
      smoothcoef(2, 2, 3) = -17
      smoothcoef(2, 2, 4) = -20
      smoothcoef(2, 2, 5) = -17
      smoothcoef(2, 2, 6) = -8
      smoothcoef(2, 2, 7) = 7
      smoothcoef(2, 2, 8) = 28
      smoothcoef(2, 2, 9) = 0
      smoothcoef(2, 2, 10) = 0
      smoothcoef(2, 2, 11) = 0
      smoothcoef(2, 2, 12) = 0


      smoothcoef(2, 3, 0) = 15
      smoothcoef(2, 3, 1) = 6
      smoothcoef(2, 3, 2) = -1
      smoothcoef(2, 3, 3) = -6
      smoothcoef(2, 3, 4) = -9
      smoothcoef(2, 3, 5) = -10
      smoothcoef(2, 3, 6) = -9
      smoothcoef(2, 3, 7) = -6
      smoothcoef(2, 3, 8) = -1
      smoothcoef(2, 3, 9) = 6
      smoothcoef(2, 3, 10) = 15
      smoothcoef(2, 3, 11) = 0
      smoothcoef(2, 3, 12) = 0


      smoothcoef(2, 4, 0) = 22
      smoothcoef(2, 4, 1) = 11
      smoothcoef(2, 4, 2) = 2
      smoothcoef(2, 4, 3) = -5
      smoothcoef(2, 4, 4) = -10
      smoothcoef(2, 4, 5) = -13
      smoothcoef(2, 4, 6) = -14
      smoothcoef(2, 4, 7) = -13
      smoothcoef(2, 4, 8) = -10
      smoothcoef(2, 4, 9) = -5
      smoothcoef(2, 4, 10) = 2
      smoothcoef(2, 4, 11) = 11
      smoothcoef(2, 4, 12) = 22


      IF (smoothnum .ge. 1 .AND. smoothnum .LE. 5
     +   .AND. derivnum .ge. 0 .AND. derivnum .LE. 2) THEN
        numcoef = 2 * smoothnum + 3
        smoothdeg = (numcoef - 1) / 2
        startconv = smoothdeg
        stopconv = numdat - smoothdeg - 1
        DO i = 0, numdat - 1
          smoothdata(i) = 0.0
        END DO
        DO i = startconv, stopconv
          DO j = 0, numcoef - 1
           smoothdata(i) = smoothdata(i) + dataset(i-smoothdeg+j)
     +               * smoothcoef(derivnum, smoothnum - 1, j)
          END DO
          smoothdata(i) =
     +          smoothdata(i)/REAL(normcoef(derivnum, smoothnum-1))
        END DO
        j = smoothdeg - 1
        k = numdat - smoothdeg
        DO i = 0, smoothdeg - 1
          smoothdata(j) = smoothdata(j + 1)
          smoothdata(k) = smoothdata(k - 1)
          j = j - 1
          k = k + 1
        END DO
      END IF

      DEALLOCATE(smoothcoef, STAT=iErr)
      CALL CheckDealloc(iErr)
      END !SUB

      SUBROUTINE DataSmoothWeights(dataset, numdat, smoothnum, weights,
     +  wdivisor, smoothdata)
      INCLUDE 'STDHDR.FOR'
      REAL dataset(0:maxv), weights(0:maxc)
      REAL smoothdata(0:maxv), wdivisor
      INTEGER numdat, smoothnum
      INTEGER smoothdeg, startconv, stopconv,i, j, k

      IF (smoothnum .ge. 1 .AND. MOD(smoothnum, 2) .EQ. 1) THEN
        smoothdeg = smoothnum / 2
        startconv = smoothdeg
        stopconv = numdat - smoothdeg - 1
        DO i = 0, numdat - 1
          smoothdata(i) = 0.0
        END DO
        DO i = startconv, stopconv
          DO j = 0, smoothnum - 1
            smoothdata(i) = smoothdata(i) +
     +             dataset(i - smoothdeg + j) * weights(j)
          END DO
          smoothdata(i) = smoothdata(i) / wdivisor
        END DO
        j = smoothdeg - 1
        k = numdat - smoothdeg
        DO i = 1, smoothdeg
          smoothdata(j) = smoothdata(j + 1)
          smoothdata(k) = smoothdata(k - 1)
          j = j - 1
          k = k + 1
        END DO
      END IF
      END !SUB

