Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pygmt/src/xyz2grd.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ def xyz2grd(data=None, x=None, y=None, z=None, **kwargs):
- :class:`xarray.DataArray`: if ``outgrid`` is not set
- None if ``outgrid`` is set (grid output will be stored in file set by
``outgrid``)

Example
-------
>>> import pygmt # doctest: +SKIP
>>> # Load a sample bathymetry file
>>> sample_bathymetry = pygmt.datasets.load_sample_data(
... name="bathymetry"
... ) # doctest: +SKIP
>>> # Create a new grid from the xyz input, set the x-range to 245-255 and
>>> # the y-range to 20-30, and the spacing to 5 degrees
>>> new_grid = pygmt.xyz2grd(
... data=sample_bathymetry, spacing=5, region=[245, 255, 20, 30]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand it, xyz2grd doesn't do interpolations, so for densely sampled data like the bathymetry data, calling xyz2grd with a coarse spacing (spacing=5) produces a grid that cannot reflect the original data. Thus, I think this is not a good example for xyz2grd which may mislead users.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would you recommend instead?

Copy link
Member

@seisman seisman Mar 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps passing a 2D numpy array is a more useful example, for example:

import numpy as np
import pygmt
# prepare input arrays
x, y = np.meshgrid([0, 1, 2, 3], [10.5, 11.0, 11.5, 12.0, 12.5])
z = x**2 + y**2
xx, yy, zz = x.flatten(), y.flatten(), z.flatten()

grid = pygmt.xyz2grd(x=xx, y=yy, z=zz, spacing=(1.0, 0.5))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a geoscientist, so I'm not sure how realistic this example is. But this seems confusing, because it uses numpy functions outside of simple math operations. Would be be easier to just pass a numpy array of data?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The equivalent 2d numpy array would be like this:

array([[  0.  ,  10.5 , 110.25],
       [  1.  ,  10.5 , 111.25],
       [  2.  ,  10.5 , 114.25],
       [  3.  ,  10.5 , 119.25],
       [  0.  ,  11.  , 121.  ],
       [  1.  ,  11.  , 122.  ],
       [  2.  ,  11.  , 125.  ],
       [  3.  ,  11.  , 130.  ],
       [  0.  ,  11.5 , 132.25],
       [  1.  ,  11.5 , 133.25],
       [  2.  ,  11.5 , 136.25],
       [  3.  ,  11.5 , 141.25],
       [  0.  ,  12.  , 144.  ],
       [  1.  ,  12.  , 145.  ],
       [  2.  ,  12.  , 148.  ],
       [  3.  ,  12.  , 153.  ],
       [  0.  ,  12.5 , 156.25],
       [  1.  ,  12.5 , 157.25],
       [  2.  ,  12.5 , 160.25],
       [  3.  ,  12.5 , 165.25]])

People usually know the X range and Y range of the grid, but may don't know how to prepare a 2D numpy array for PyGMT.

Here is a similar example in matplotlib: https://matplotlib.org/stable/plot_types/arrays/pcolormesh.html.

The main difference is that, we have to call the array.flatten() method before passing to xyz2grd.

... ) # doctest: +SKIP
"""
with GMTTempFile(suffix=".nc") as tmpfile:
with Session() as lib:
Expand Down