FUNCTION Fourier, imageP, $
	POWER = power,  $
	BAKWARD = backward, FORWARD = forward, $
	FMAG = fMag, BMAG = bMag, $
	XY = xy, X = x, Y = y, STATUS = status
	

;+
; NAME:
; 	Fourrier
; PURPOSE:
;	Performs fourrier transformation, forward or backward,
;	and returns the result either in  a  real image
;	and an imaginary image, or in a power-spectrum image
;	(real) or in a magnitude image. With a keyword specified,
;	the action will be done directly, else it will be
;	asked interactively.
; CALLING SEQUENCE:
;	result = Fourrier(image)
; INPUT ARGUMENT:
;	image: a 2D array of any type, including complex type
;		but string.
; OUTPUT ARGUMENT:
;	image: a complex 2D array when normal forward fourrier-
;		transformation is chosen real in the case
;		of magnitude, backward and power spectrum.
; KEYWORDS:
;	/POWER: power spectrum transformation is performed
;	/BACKWARD: backward transformation is performed
;	/FMAG: forward magnitude transformation performed
;	/FORWARD: forward transformation is performed
;	/BMAG: backward magnitude transformation is performed
; 	/XY: two-dimensionnal transformation (default)
;	/X: x-direction transformation
;	/Y: y-direction transformation
;	STATUS: "Done" or "Not Done"
; PROCEDURE:
;	The core of this function is the WAVE procedure FFT.
;	Power spectrum:
;		y' = Abs( FFT( image ) )^2. 
;		a factor 1/N_Elements is applied.
; MODIFICATION HISTORY:
;	Created by A.Csillaghy in September 1991
;		Inst. of Astronomy, ETH Zurich
;	Modified in Feb 93, A.Cs.
;-

status= 'Not Done'

IF NOT Keyword_Set( POWER) THEN power = 0 $
ELSE BEGIN dir = -1 & res = 'p' & END
IF NOT Keyword_Set( BACKWARD) THEN backward = 0 $
ELSE BEGIN dir = 1 & res = 'c' & END
IF NOT Keyword_Set( FMAG) THEN fMag = 0  $
ELSE BEGIN dir = -1 & res = 'm' & END
IF NOT Keyword_Set( FORWARD) THEN forward = 0  $
ELSE BEGIN dir = -1 & res = 'c' & END
IF NOT Keyword_Set( BMAG) THEN bMag = 0 $
ELSE BEGIN dir = 1 & res = 'm' & END

x = 0 & y = 0 & xy = 1
IF NOT Keyword_Set( X ) THEN x = 1
IF NOT Keyword_Set( Y ) THEN y = 1
IF Keyword_Set(XY) THEN xy = 1
kwds = power +  backward + fMag + forward + bMag

choice = 0
image = imageP

IF NOT kwds THEN BEGIN

  mainMenu = StrArr(6)
  
  mainMenu(0) = 'Direction: Forward'
  mainMenu(1) = 'Result: Complex'
  mainMenu(2) = 'Transformation: Two-Dimensional'
  mainMenu(3) = 'Perform Operation'
  mainMenu(4) = 'Do NOT Transform'
  mainMenu(5) = 'Redisplay this Menu'

  dir = -1
  res = 'c'
  xy = 1 & x= 0 & y = 0
  
  REPEAT BEGIN
    
      choice = General_Menu( mainMenu, $
	'Fourrier: Change the Options or Do Operation' )

      CASE choice OF 
        1: BEGIN
              IF dir EQ -1 THEN BEGIN
                mainMenu(0) = StrMid( mainMenu(0), 0, 11)+'Backward'
                dir = 1
              ENDIF ELSE BEGIN
                mainMenu(0) = StrMid( mainMenu(0), 0, 11)+'Forward'
                dir = -1
              ENDELSE
            END
        2: BEGIN
              IF  res EQ 'c' THEN BEGIN
                mainMenu(1) = StrMid( mainMenu(1), 0, 8 )+ 'Magnitude' 
                res = 'm'
              ENDIF ELSE IF res EQ 'm' THEN BEGIN
                 mainMenu(1) = StrMid( mainMenu(1), 0, 8 )+ $
	'Power Spectrum (only in forward direction)'  
                  res = 'p'
               ENDIF ELSE BEGIN
                  mainMenu(1) = StrMid( mainMenu(1), 0, 8 ) + 'Complex'
                  res = 'c'
               ENDELSE
           END
       3: BEGIN
             IF xy EQ 1 THEN BEGIN
               mainMenu(2) = StrMid( mainMenu(2), 0, 16 ) + $
		'X Direction Only'
               xy = 0 & x = 1 & y = 0
             ENDIF ELSE IF x EQ 1 THEN BEGIN
               mainMenu(2) = StrMid( mainMenu(2), 0, 16 ) + $
		'Y Direction Only'
               xy = 0 & x = 0 & y = 1
             ENDIF ELSE BEGIN
               mainMenu(2) = StrMid( mainMenu(2), 0, 16 ) + $
		'Two-Dimensional'
               xy = 1 & x=0 & y = 0
              ENDELSE
            END
      ELSE:
      ENDCASE

  END UNTIL choice EQ 4 OR choice EQ 5

ENDIF
    
  nx = N_Elements( image( *, 0 ) ) & ny = N_Elements( image(0,*))

;  IF dir EQ -1 THEN BEGIN
;    answ = ''
;    Read, 'Do you want to apply the hanning window [n] ? ', answ
;    answ = StrUpcase( answ )
;    IF answ EQ 'Y' THEN  BEGIN
;      IF xy EQ 1 THEN image = image*Hanning( nx, ny ) $
;      ELSE IF x EQ 1 THEN BEGIN
;        han = Hanning( nx )
;        FOR i =0,ny-1 DO image(*,i) = image(*,i)*han
;      ENDIF ELSE BEGIN
;        han = Transpose( Hanning( ny ) )
;        FOR i =0,nx-1 DO image(i,*) = FFT( image(i,*), dir )
;      ENDELSE
;    ENDIF
;  ENDIF
  
;And now let's transform (happily):

  IF choice EQ 5 THEN RETURN, image

  image = Complex( image )
  IF xy EQ 1 THEN  image = FFT( image , dir ) $
  ELSE IF x EQ 1 THEN BEGIN
    FOR i = 0, ny-1 DO BEGIN
      temporary=  image(*,i) 
      image(*,i) = FFT( temporary, dir )
    ENDFOR
  ENDIF ELSE BEGIN
    FOR i = 0, nx-1 DO BEGIN
      temporary = image(i,*) 
      image(i,*) = FFT( temporary, dir )
    ENDFOR
  ENDELSE


;And now let's compute power spectra or magnitude:
 
  status = 'Done'

  IF (res EQ 'p') AND (dir EQ -1) THEN RETURN,ABS(image)^2 
  IF (res EQ 'm') OR $
	((res EQ'p') AND (dir EQ 1)) THEN $
	RETURN,ABS(image)

  RETURN, image

; bye
END
               