      subroutine chtrfm1(n,x,cosn,iflag)
**************************************************
*** 1-D Chebyshev transform !! MUST call costi(n+1,cosn) to initialize !!
***  iflag=1: Spectral  to Physical
***  iflag=0: Physical  to Spectral  
***        x: input and output
*************************************************
      implicit double precision (a-h,o-z)
      dimension x(0:n),cosn(1)
      if (iflag.eq.1) then
         val=.5d0
         do i=1,n-1
            x(i)=val*x(i)
         enddo
         call cost(n+1,x,cosn)
      else
         call cost(n+1,x,cosn)
         val=1.d0/n
         do i=1,n-1
            x(i)=x(i)*val
         enddo
         x(0)=x(0)*val*.5d0
         x(n)=x(n)*val*.5d0
      endif
      return
      end

