# Calling grdfft with pygmt

**URL:** <https://forum.generic-mapping-tools.org/t/calling-grdfft-with-pygmt/1926>\
**Category:** PyGMT Q&A\
**Created:** [July 26, 2021, 4:49am UTC](https://forum.generic-mapping-tools.org/t/calling-grdfft-with-pygmt/1926 "2021-07-26T04:49:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigm](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@craigm](https://forum.generic-mapping-tools.org/u/craigm)\
**Post date:** [July 26, 2021, 4:49am UTC](https://forum.generic-mapping-tools.org/t/calling-grdfft-with-pygmt/1926/1 "2021-07-26T04:49:09Z")

</div>

I’m having trouble calling grdfft from pygmt (v0.4.0). Basically i want to loop through a directory (windows) and calculate the 2d FFT (radial average power spectrum) on a bunch of grid files and save the output to file for each grid. I use gdal to read the grids and then convert them to xarray. I use the virtualfile\_from\_grid to load the grid into GMT and then call grdfft with the arguments (args).

```
   for fn in os.listdir(griddir):
        if fn.endswith(".grd"):
            print("Doing fft on file:", fn)
            datagrid = gdal.Open(griddir + dsep + fn)
            band = datagrid.GetRasterBand(1)
            array = band.ReadAsArray()
            xgrid = xr.DataArray(array)

            with pygmt.clib.Session() as session:
                with session.virtualfile_from_grid(xgrid) as fin:
                    with pygmt.helpers.GMTTempFile() as fout:
                        args = "{} -Na -Er -V ->{}".format(fin, fout.name)
                        session.call_module('grdfft', args)
                        print(fout.read().strip())

```

This is the output and errror I get. Interestingly if i replace “grdfft” with “grdinfo” (with appropriate args) I get the expected output from grdinfo. So my basic pipeline seems ok, just some issue with grdfft???

```
grdfft [INFORMATION]: Processing input grid(s)
grdfft [INFORMATION]: Cartesian input grid
grdfft [INFORMATION]: Cartesian input grid
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\xxxx\curiepointdepth.py", line 334, in runfft
    session.call_module('grdfft', args)
  File "C:\Users\xxxxx\Anaconda3\envs\cpd\lib\site-packages\pygmt\clib\session.py", line 501, in call_module
    status = c_call_module(
OSError: exception: access violation reading 0x0000000000000000
```

---

<div class="post-metadata">

**Author:** ![Joaquim](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/joaquim/32/16_2.png) [@Joaquim](https://forum.generic-mapping-tools.org/u/Joaquim)\
**Post date:** [July 26, 2021, 6:42pm UTC](https://forum.generic-mapping-tools.org/t/calling-grdfft-with-pygmt/1926/2 "2021-07-26T18:42:59Z")

</div>

I think your life would be much simpler if you called grdfft directly on the grid file names and avoid all that gdal+xarrays stuff

---

<div class="post-metadata">

**Author:** ![craigm](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@craigm](https://forum.generic-mapping-tools.org/u/craigm)\
**Post date:** [July 26, 2021, 10:00pm UTC](https://forum.generic-mapping-tools.org/t/calling-grdfft-with-pygmt/1926/3 "2021-07-26T22:00:11Z")

</div>

ok thanks, after some experimentation I found I can in fact do as you suggest:

```
with pygmt.clib.Session() as session:
    fout="fft_result.txt"
    args = f"{fn} -Na -Er -G{fout}"
    session.call_module('grdfft', args)

```

where fn is my input grid file name.
