!-------------------------------------- LICENCE BEGIN ------------------------------------
!Environment Canada - Atmospheric Science and Technology License/Disclaimer, 
!                     version 3; Last Modified: May 7, 2008.
!This is free but copyrighted software; you can use/redistribute/modify it under the terms 
!of the Environment Canada - Atmospheric Science and Technology License/Disclaimer 
!version 3 or (at your option) any later version that should be found at: 
!http://collaboration.cmc.ec.gc.ca/science/rpn.comm/license.html 
!
!This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
!without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
!See the above mentioned License/Disclaimer for more details.
!You should have received a copy of the License/Disclaimer along with this software; 
!if not, you can write to: EC-RPN COMM Group, 2121 TransCanada, suite 500, Dorval (Quebec), 
!CANADA, H9P 1J3; or send e-mail to service.rpn@ec.gc.ca
!-------------------------------------- LICENCE END --------------------------------------
***s/r set_fft - determine if fast fourier transforms is needed and
*                if so, initialize and prepare the FFT projector
*
#include "model_macros_f.h"
*

      subroutine set_fft 1,3
*
      implicit none
*
*author 
*     michel roch - rpn - june 1993
*
*revision
* v2_00 - Lee V.            - initial MPI version (from setfft v1_03)
* v2_40 - Qaddouri A.       - adjust for LAM version
* v3_00 - Desgagne & Lee    - Lam configuration
* v3_30 - Tanguay M.        - Abort if LAM adjoint not FFT 
*
*object
*     See above id.
*	
*arguments
*	none
*
*implicits
#include "glb_ld.cdk"
#include "glb_pil.cdk"
#include "dcst.cdk"
#include "fft.cdk"
#include "sol.cdk"
#include "grd.cdk"
#include "lun.cdk"
#include "v4dg.cdk"
*
**
      real*8 two
      parameter ( two  = 2.0 )
*
      integer i
*     ---------------------------------------------------------------
*
      if (Lun_out.gt.0) write(Lun_out,1000)
      Fft_fast_L = .false.
*
*C       1.    test if grid is uniform in x
*              ----------------------------
      if ( .not. sol_fft_L ) then
         if (Lun_out.gt.0) 
     $   write(Lun_out,*)'Fft_fast_L = .false. ====> sol_fft_L=.f.'
         goto 999
      endif
*
      if ( Grd_ni .ne. Grd_nila ) then
         if (Lun_out.gt.0) 
     $   write(Lun_out,*)'Fft_fast_L = .false. ====> non-uniform grid'
         goto 999
      endif
*
*C       2.    test grid factorization
*              -----------------------
      i = G_ni-Lam_pil_w-Lam_pil_e
      call ngfft ( i )
      if ( i.ne.G_ni-Lam_pil_w-Lam_pil_e ) then
         if (Lun_out.gt.0) 
     $   write(Lun_out,3001) G_ni-Lam_pil_w-Lam_pil_e,i
         goto 999
      endif
*
      Fft_fast_L = .true.
      if (Lun_out.gt.0) write(Lun_out,*) 'Fft_fast_L = ',Fft_fast_L
*
*C       3.    calculate inverse projector in Fourier space 'Fft_pri_8'
*              --------------------------------------------------------
      if (G_lam) then
         call setscqr(i, 'QCOS' )
         i=G_ni-Lam_pil_w-Lam_pil_e
         Fft_pri_8 = dble(i)/(G_xg_8(G_ni-Lam_pil_e)-G_xg_8(Lam_pil_w))
      else
         call setfft8 ( Grd_ni )
         Fft_pri_8 = dble(Grd_ni) / (  two * Dcst_pi_8 )
      endif
*
      if (Lun_out.gt.0) write(Lun_out,*) 'Fft_pri_8 = ',Fft_pri_8
*
 999  if (V4dg_conf.ne.0.and.G_lam.and..not.Fft_fast_L) then
         if (Lun_out.gt.0) write(Lun_out,*) 'ABORT: LAM adjoint not FFT'
         call gem_stop ('LAM adjoint non FFT',-1)
      endif
*
 1000 format(
     %/,'COMMON INITIALIZATION AND PREPARATION FOR FFT (S/R SET_FFT)',
     %/,'===========================================================')
 3001 format ('Fft_fast_L = .false. ====> NI = ',i6,' NOT FACTORIZABLE'
     $        /'NEXT FACTORIZABLE G_NI = ',i6)
*
*     ---------------------------------------------------------------
*
      return
      end