CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
C   FFTPACK 5.0
C   Copyright (C) 1995-2004, Scientific Computing Division,
C   University Corporation for Atmospheric Research
C   Licensed under the GNU General Public License (GPL)
C
C   Authors:  Paul N. Swarztrauber and Richard A. Valent
C
C   $Id: costf1.f,v 1.2 2006-11-21 01:10:17 haley Exp $
C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC

      SUBROUTINE DCOSTF1(N,INC,X,WSAVE,WORK,IER)
      DOUBLE PRECISION WORK
      DOUBLE PRECISION X1H
      DOUBLE PRECISION X1P3
      DOUBLE PRECISION TX2
      DOUBLE PRECISION T1
      DOUBLE PRECISION T2
      DOUBLE PRECISION SNM1
      DOUBLE PRECISION XI
      DOUBLE PRECISION X(INC,*),WSAVE(*)
C*PT*WARNING* Already double-precision
      DOUBLE PRECISION DSUM

      IER = 0
      NM1 = N - 1
      NP1 = N + 1
      NS2 = N/2
      IF (N-2) 200,101,102
  101 X1H = X(1,1) + X(1,2)
      X(1,2) = .5D0* (X(1,1)-X(1,2))
      X(1,1) = .5D0*X1H
      GO TO 200
  102 IF (N.GT.3) GO TO 103
      X1P3 = X(1,1) + X(1,3)
      TX2 = X(1,2) + X(1,2)
      X(1,2) = .5D0* (X(1,1)-X(1,3))
      X(1,1) = .25D0* (X1P3+TX2)
      X(1,3) = .25D0* (X1P3-TX2)
      GO TO 200
  103 DSUM = X(1,1) - X(1,N)
      X(1,1) = X(1,1) + X(1,N)
      DO 104 K = 2,NS2
          KC = NP1 - K
          T1 = X(1,K) + X(1,KC)
          T2 = X(1,K) - X(1,KC)
          DSUM = DSUM + WSAVE(KC)*T2
          T2 = WSAVE(K)*T2
          X(1,K) = T1 - T2
          X(1,KC) = T1 + T2
  104 CONTINUE
      MODN = MOD(N,2)
      IF (MODN.EQ.0) GO TO 124
      X(1,NS2+1) = X(1,NS2+1) + X(1,NS2+1)
  124 LENX = INC* (NM1-1) + 1
      LNSV = NM1 + INT(LOG(DBLE(NM1))) + 4
      LNWK = NM1
C
      CALL DRFFT1F(NM1,INC,X,LENX,WSAVE(N+1),LNSV,WORK,LNWK,IER1)
      IF (IER1.NE.0) THEN
          IER = 20
          CALL DXERFFT('COSTF1',-5)
          GO TO 200
      END IF
C
      SNM1 = 1.D0/DBLE(NM1)
      DSUM = SNM1*DSUM
      IF (MOD(NM1,2).NE.0) GO TO 30
      X(1,NM1) = X(1,NM1) + X(1,NM1)
   30 DO 105 I = 3,N,2
          XI = .5D0*X(1,I)
          X(1,I) = .5D0*X(1,I-1)
          X(1,I-1) = DSUM
          DSUM = DSUM + XI
  105 CONTINUE
      IF (MODN.NE.0) GO TO 117
      X(1,N) = DSUM
  117 X(1,1) = .5D0*X(1,1)
      X(1,N) = .5D0*X(1,N)
  200 RETURN
      END
