      SUBROUTINE CDFTF (N,RE,IM,IOPTC)                                          
C                                                                               
C  -------------------------------------------------------------                
C  MODULE        CDFTF                                                          
C  VERSION       1.0                                                            
C  LAST MODIFIED 12/11/81                                                       
C  AUTHOR        DAVE POWELL                                                    
C                                                                               
C  -------------------------------------------------------------                
C                                                                               
C  PARAMETERS                                                                   
C  **********                                                                   
C                                                                               
C  N        I*4  LENGTH OF SIGNAL                                               
C  RE       R*4  ARRAY CONTAINING REAL PART OF SIGNAL                           
C  IM       R*4A ARRAY CONTAINING IMAGINARY PART OF SIGNAL                      
C  IOPTC    I*4  CARRIES INFORMATION ABOUT OPTIONS REQUIRED                     
C                                                                               
C                                                                               
C       IOPTC    B(1)    B(2)    MEANING                                        
C       -----    ----    ----    -------                                        
C                                                                               
C         4       0       0       REVERSE TRANSFORM,NO SCALING                  
C         5       0       1       FORWARD TRANSFORM,NO SCALING                  
C         6       1       0       REVERSE TRANSFORM,SCALING                     
C         7       1       1       FORWARD TRANSFORM,SCALING                     
C                                                                               
C                                                                               
      INTEGER*4 N,IOPTC                                                         
      REAL*4 RE(N),IM(N)                                                        
C  ------------------------------------------------------------                 
C                                                                               
C                                                                               
C  LOCAL VARIABLES                                                              
C  ***************                                                              
C                                                                               
C                                                                               
C  MAXLEN   I*4   MAXIMUM SIGNAL LENGTH                                         
C  B(2)     I*4   =1 OR 0 (1-FORWARD TRANSFORM,0-INVERSE TRANSFORM)             
C  B(1)     I*4   =1 OR 0 (1-SCALLING,0-NO SCALLING)                            
C  IS       I*4   USED TO CALCUATE B(1)&B(2) FROM IOPT                          
C  I2,I3,J3 I*4   COUNTERS                                                      
C  THETA    R*4   =2*PI/N                                                       
C  T        R*4   =MULT. FACTOR (=1 IF NO SCALING,=1/N IF SCALING)              
C  PI       R*4   =3.14159265.........                                          
C  IOPT     I*4   COPY OF IOPTC                                                 
C  RTN      I*4   ROUTINE NUMBER (=6)                                           
C                                                                               
      INTEGER*4 I2,I3,J3,IS,B(2),MAXLEN,IOPT,RTN,IER                            
      REAL*4 THETA,T,PI,RW,IW,RWK,IWK,RSIG(2048),ISIG(2048),RWKL,IWKL           
      DATA PI/3.141592653589793/                                                
      PARAMETER  (RTN=6)                                                        
C  ------------------------------------------------------------                 
C                                                                               
C  (1) CHECK PARAMETERS                                                         
C  ********************                                                         
C                                                                               
      IOPT=IOPTC-4                                                              
      MAXLEN=2048                                                               
      IF (N.GT.MAXLEN.OR.N.LT.2) GOTO 9002                                      
      IF (IOPTC.GT.7.OR.IOPTC.LT.4) GOTO 9001                                   
C                                                                               
C                                                                               
C  (2) SELECT OPTIONS                                                           
C  ******************                                                           
C                                                                               
      IS=4                                                                      
      DO 2001 I2=1,2                                                            
          IS=IS/2                                                               
          IOPT=IOPT-IS                                                          
          IF(IOPT.GE.0) GOTO 2000                                               
          IOPT=IOPT+IS                                                          
          B(I2)=0                                                               
          GOTO 2001                                                             
2000      CONTINUE                                                              
          B(I2)=1                                                               
2001  CONTINUE                                                                  
C                                                                               
C                                                                               
C  (3) DISCRETE FOURIER TRANSFORM                                               
C  ******************************                                               
C                                                                               
      THETA=2.0*PI/FLOAT(N)                                                     
      IF (B(2).EQ.0) THEN                                                       
          IY=1.0                                                                
      ELSE                                                                      
          IY=-1.0                                                               
      ENDIF                                                                     
C                                                                               
      RW=COS(THETA)                                                             
      IW=IY*SIN(THETA)                                                          
      RWK=1.0                                                                   
      IWK=0.0                                                                   
      DO 3001 I3=1,N                                                            
          RSIG(I3)=0.0                                                          
          ISIG(I3)=0.0                                                          
          RWKL=1.0                                                              
          IWKL=0.0                                                              
          DO 3000 J3=1,N                                                        
              RSIG(I3)=RSIG(I3)+(RE(J3)*RWKL-IM(J3)*IWKL)                       
              ISIG(I3)=ISIG(I3)+(IM(J3)*RWKL+RE(J3)*IWKL)                       
              T=RWKL*RWK-IWKL*IWK                                               
              IWKL=IWKL*RWK+RWKL*IWK                                            
              RWKL=T                                                            
3000      CONTINUE                                                              
          T=RWK*RW-IWK*IW                                                       
          IWK=IWK*RW+RWK*IW                                                     
          RWK=T                                                                 
3001  CONTINUE                                                                  
C                                                                               
C                                                                               
C  (4) SCALE (IF REQUIRED) & PLACE TRANSFORMED SIGNAL IN RE & IM                
C  *************************************************************                
C                                                                               
      IF (B(1).EQ.1) THEN                                                       
          T=1.0/FLOAT(N)                                                        
      ELSE                                                                      
          T=1.0                                                                 
      ENDIF                                                                     
      DO 4000 I4=1,N                                                            
          RE(I4)=T*RSIG(I4)                                                     
          IM(I4)=T*ISIG(I4)                                                     
4000  CONTINUE                                                                  
      RETURN                                                                    
C                                                                               
C                                                                               
C  (5) END AND CALL ERROR ROUTINES IF REQUIRED                                  
C  *******************************************                                  
C                                                                               
9001  CONTINUE                                                                  
      IER=4                                                                     
      GOTO 9999                                                                 
C                                                                               
9002  CONTINUE                                                                  
      IER=5                                                                     
      GOTO 9999                                                                 
C                                                                               
9999  CONTINUE                                                                  
C                                                                               
      CALL UERERR('SIGPROC',RTN,IER,0,'E')                                      
      CALL UERREP('SIGREP',RTN,.FALSE.)                                         
C                                                                               
      RETURN                                                                    
      END                                                                       
