# [ncl-talk] NCL fft2df/fft2db conversion to Python

Dennis Shea shea at ucar.edu
Thu Feb 9 10:43:39 MST 2023

```Perhaps the best approach:   Contact NCAR's "*GeoCAT*
<https://geocat.ucar.edu/>"
They should have the Python expertise to help you.

Good Luck

On Wed, Feb 8, 2023 at 1:36 PM Christophersen, Dr. Jonathan, Contractor,
Code 7532 via ncl-talk <ncl-talk at mailman.ucar.edu> wrote:

> Hi NCL users,
>
>
>
> Apologies if this is more of a Pythonic question than NCL, but I’m in need
> of some help with converting NCL’s fft2df/fft2db over to Python NumPy’s (or
> SciPy’s) fft2/ifft2. I’ve gotten pretty close to a workable solution, but
> there are still some conversion problems.
>
>
>
> The Python (pseudo-)code that I’ve been able to reproduce NCL’s fft2df
> function with breaks the result of the 2D-FFT into components, but this is
> just for investigative convenience for now :-). Code is as follows:
>
>
>
> fftData = np.fft.fft2(F_x, axes=(-2, -1), norm='forward')
>
> real_fft_tmp = np.real(fftData)
>
> imag_fft_tmp = np.imag(fftData)
>
> real_fft = 2*np.roll(np.flipud(real_fft_tmp), shift=1, axis=0)
>
> imag_fft = 2*np.roll(np.flipud(imag_fft_tmp), shift=1, axis=0)
>
>
>
> And if I’m only considering the data up to the Nyquist frequency (NCL’s
> fft2df only returns half the spectrum) then I get a pretty close match in
> spectral space for both real and imaginary components. However, the
> differences that are present (error within 10^-5) seem to throw off the
> inverse FFT2 and I end up with large errors. Is anybody working on a 2D-FFT
> conversion from NCL to Python that might be able to answer this or shed
> some light on what I might be doing wrong?
>
>
>
> Thanks so much for any help, and I’m happy to answer whatever questions
> there might be. I’m not permitted to share my actual source code, so
> apologies for any inconvenience.
>
> Jon
>
>
>
>
>
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at mailman.ucar.edu
> List instructions, subscriber options, unsubscribe:
> https://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20230209/74576f54/attachment.htm>
```