pygmt: The "incols" (-i) parameter doesn't works for vector input correctly

Description of the problem

Using the incols parameter does not allow to swap columns (seems like the parameter is ignored at all so far). I believe it’s an upstream GMT bug. This was discovered in https://github.com/GenericMappingTools/pygmt/pull/1303#discussion_r642213335.

Full code that generated the error

import pygmt
import numpy as np

# generate sample data
region = [10, 70, -5, 10]
x, y = np.meshgrid(
np.linspace(region[0], region[1]), np.linspace(region[2], region[3])
    )
x = x.flatten()
y = y.flatten()
z = (x - 0.5 * (region[0] + region[1])) ** 2 + 4 * y ** 2
z = np.exp(-z / 10 ** 2 * np.log(2))

# generate dataframe
data = np.array([x, y, z]).T

# correct figure
fig1 = pygmt.Figure()
fig1.contour(data=data,
            projection="X10c",
            frame="a",
            pen=True)

fig1.show()

# generate second dataframe, switch x and y from here onwards to simulate 
# different column order
data = np.array([y, x, z]).T

# wrong figure
fig2 = pygmt.Figure()
fig2.contour(data=data,
            incols=[1, 0, 2], # use incols to assign column order
            projection="X10c",
            frame="a",
            pen=True)

fig2.show()

Full output

fig1: expected fig2: actual (wrong)
correct incorrect

System information

Please paste the output of python -c "import pygmt; pygmt.show_versions()":

PyGMT information:
  version: v0.2.2.dev80+g132757a
System information:
  python: 3.6.12 | packaged by conda-forge | (default, Dec  9 2020, 00:36:02)  [GCC 9.3.0]
  executable: /home/mgrund/anaconda3/envs/pygmt/bin/python
  machine: Linux-4.4.0-19041-Microsoft-x86_64-with-debian-buster-sid
Dependency information:
  numpy: 1.19.4
  pandas: 1.1.5
  xarray: 0.16.2
  netCDF4: 1.5.6
  packaging: 20.8
  ghostscript: 9.53.3
  gmt: 6.2.0rc1
GMT library information:
  binary dir: /home/mgrund/anaconda3/envs/pygmt/bin
  cores: 8
  grid layout: rows
  library path: /home/mgrund/anaconda3/envs/pygmt/lib/libgmt.so
  padding: 2
  plugin dir: /home/mgrund/anaconda3/envs/pygmt/lib/gmt/plugins
  share dir: /home/mgrund/anaconda3/envs/pygmt/share/gmt
  version: 6.2.0rc1

@PaulWessel @meghanrjones could you please check if you can debug it?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 24 (24 by maintainers)

Commits related to this issue

Most upvoted comments

I think to use incols Paul would need to build from this branch (https://github.com/GenericMappingTools/pygmt/pull/1303) and that the master branch uses columns as the alias name. Rather than checkout out that branch and building, you could just change incols= to columns= or i=.

Yes, I think this is an upstream bug, but it only affects wrappers, that’s why the GMT tests pass.

Here is a modified example based on @michaelgrund’s example:

import pygmt
import numpy as np

# generate sample data
region = [10, 70, -5, 10]
x, y = np.meshgrid(np.linspace(region[0], region[1]), np.linspace(region[2], region[3]))
x = x.flatten()
y = y.flatten()
z = (x - 0.5 * (region[0] + region[1])) ** 2 + 4 * y ** 2
z = np.exp(-z / 10 ** 2 * np.log(2))

# generate dataframe
data = np.array([x, y, z]).T

# correct figure
fig1 = pygmt.Figure()
fig1.contour(data=data, projection="X10c", frame="a", pen=True)
fig1.savefig("array-xyz.png")

# correct
np.savetxt("xyz.dat", data)
fig3 = pygmt.Figure()
fig3.contour(data="xyz.dat", projection="X10c", frame="a", pen=True)
fig3.savefig("file-xyz.png")


# generate second dataframe, switch x and y from here onwards to simulate
# different column order
data = np.array([y, x, z]).T

# wrong figure
fig2 = pygmt.Figure()
fig2.contour(data=data, projection="X10c", frame="a", pen=True, incols=[1, 0, 2])
fig2.savefig("array-yxz.png")

# correct
np.savetxt("yxz.dat", data)
fig4 = pygmt.Figure()
fig4.contour(data="yxz.dat", projection="X10c", frame="a", pen=True, incols=[1, 0, 2])
fig4.savefig("file-yxz.png")
array-xyz file-xyz array-yxz file-yxz
array-xyz file-xyz array-yxz file-yxz

As you can see, passing “YXZ” data and using incols=[1, 0, 2] works well for file input, but not for an array input.