GenericMappingTools / GenericMappingTools/pygmt

Cylindrical projections 'Q' plot incorrectly in pygmt when using xarrays

Open
#390 9 comments 0 reactions 0 assignees View on GitHub
bug longterm upstream
Dominant language
Python
Stars
874
Forks
255
Avg merge
1d 21h
Merged PRs (30d)
40

Description

**Pygmt incorrectly plots xarrays when using cylindrical projections ('Q').**

This is not an issue with gmt: if the dataarray is first exported to a netcdf file, pygmt works as intended when using the file. Thus, this appears to be a problem with how pygmt treats internal xarrays, and only for the case of cylindrical projections (the most basic of all the projections!). This is demonstrated in the following script.

The script generates a simple dataset, and the plots the following:

* upper row: 'Q0/0/6i' and 'Q180/0/6i' reading data from a file.
* middle row: 'W0/6i' and 'W180/6i' using internal xarray.
* bottom row: 'Q0/0/6i' and 'Q180/0/6i' using internal xarray.

As you see, the upper two rows plot the data correctly. However, for the bottom left image, the data are **not** plotted with the correct `central_longitude`. Furthermore, there is a problem with the first longitude band not being plotted.

It is possible that this is related to this issue: https://github.com/GenericMappingTools/pygmt/issues/375

```
# Creata a data array in gridline coordinates of sin(lon) * cos(lat)
interval = 10
lat = np.arange(90, -90-interval, -interval)
lon = np.arange(0, 360+interval, interval)
longrid, latgrid = np.meshgrid(lon, lat)
data = np.sin(np.deg2rad(longrid)) * np.cos(np.deg2rad(latgrid))

# create a DataArray
dataarray = xr.DataArray(data, coords=[('latitude', lat,
{'units': 'degrees_north'}),
('longitude', lon,
{'units': 'degrees_east'})],
attrs = {'actual_range': [-1, 1]})
dataset = dataarray.to_dataset(name='dataarray')
dataset.to_netcdf('test.grd')

# create projected images using a cylindrical and mollweide projection.
fig = pygmt.Figure()
fig.grdimage(dataset.dataarray, region='g', projection='Q0/0/6i', frame='a90f30g30')
fig.shift_origin(xshift='7i')
fig.grdimage(dataset.dataarray, region='g', projection='Q180/0/6i', frame='a90f30g30')
fig.shift_origin(xshift='-7i', yshift='4i')
fig.grdimage(dataset.dataarray, region='g', projection='W0/6i', frame='a90f30g30')
fig.shift_origin(xshift='7i')
fig.grdimage(dataset.dataarray, region='g', projection='W180/6i', frame='a90f30g30')
fig.shift_origin(xshift='-7i', yshift='4i')
fig.grdimage('test.grd', region='g', projection='Q0/0/6i', frame='a90f30g30')
fig.shift_origin(xshift='7i')
fig.grdimage('test.grd', region='g', projection='Q180/0/6i', frame='a90f30g30')

fig.savefig('test.png')
fig.show()
```
![test](https://user-images.githubusercontent.com/8697285/72362496-913f0280-36f3-11ea-85c5-39385f383811.png)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.