! Benchmark Ferret script for FFT.  Uses analytic function of time.

! 5/99  ACM
!   FFTA function computes the FFT amplitude spectrum.  
!   FFTP function computes the FFT phase

SET MODE IGNORE_ERRORS

SAY The message *** NOTE: unknown axis units: FREQ  is informational.  fcn still works.

SET WIND/CLEAR


DEFINE AXIS/t=1-Jan-1990:31-Dec-1990:24 dayt
DEFINE GRID/T=dayt  tgrid
SH GRID tgrid
SET GRID tgrid

LET fcn1 = sin(kt*tpts - phase) /2.
LET fcn2 = cos(pt*tpts)
LET phase = 6.
LET kt = 0.5
LET pt = 0.3

!  Plot an analytic function.

LET sample_function = fcn1 - fcn2

LET tpts = t
SET VIEW ul
PLOT/TITLE="An analytic function of time" sample_function

LET days_fft = ffta(sample_function[l=1:365])

SET VIEW ur
PLOT/SET_UP days_fft
PPL YLAB Amplitude Spectrum
PPL PLOT FFTA
!  Compute the inverted axis and plot the spectrum in period: months/cycle

LET FFT_nf = `days_fft,return=lend`
LET FFT_nyquist = 0.5
LET FFT_freq1 = FFT_nyquist/ FFT_nf

DEFINE AXIS/T=`FFT_freq1`:`FFT_nyquist`:`FFT_freq1` FAXIS
DEFINE GRID/T=FAXIS gfftfreq
LET a = T[g=gfftfreq]

LET per = 1./a

SET VIEW ll
PLOT/VS/LINE/SET_UP per[l=1:`FFT_nf`],days_fft
PPL XAXIS, 0, 30, 1
PPL XFOR (I2)
PPL XLAB Period, days/cycle
PPL YLAB Amplitude Spectrum
PPL PLOT FFT


LET days_fftp = fftp(sample_function[l=1:365])

!  Compute the phase spectrum,  cyc/month
SET VIEW lr

PLOT/SET_UP days_fftp
PPL YLAB Phase Spectrum
PPL PLOT FFTP

PAUSE

SET VIEW full
SET WIND/CLEAR

USE  "coads_vwnd.cdf"
SET REGION/I=90/J=65
PLOT vwnd

SAY This should bail out w/Time axis error

LET vw_fft = ffta(vwnd[l=37:60])
PLOT vw_fft

DEFINE AXIS/T=16-Jan-1949:16-Dec-1950:730.5@ave montht
DEFINE GRID/T=montht  tgrid
LET vw_tim = vwnd[gt=tgrid]
LET vw_fft = ffta(vw_tim[l=2:23])

SAY This one is ok
PLOT vw_fft

CANCEL MODE IGNORE_ERRORS
