I’m trying to plot an xarray grid on a map with pygmt. Here’s a constructed example using the same array data structure plotted with pygmt as well as saved as netcdf and plotted using GMT in the OS shell.
The pygmt version contains a gap at 0 zero longitude. I would have liked to use pygmt to create a plot that wraps in longitude like in the OS shell version. Is this possible? Is this a bug in pygmt, or do I need to do something to the input in pygmt to achieve this?
Thank you Yvonne, yes I knew of the np.arange behaviour, but in my example I’m recreating the grid of the output from a global atmospheric/ionospheric model, so if that is the reason, is the model in error concerning the longitude output? Does this mean I have to repeat the data at 0 and 360 longitude in input to PyGMT? Is this a documented convention? The incomplete latitude grid is my mistake in this example, 90N is in the model output.
And why would this behaviour be different between command-line GMT and pyGMT? I think the command-line GMT behaviour makes more sense. Grdimage uses interpolation (nearest-neighbour in this case), and I expected GMT to know that for a pixel at ~359 deg longitude, the nearest neighbour is at 0 deg longitude in this grid. It seems to do so in the command-line version.
There seems to be a difference in PyGMT between passing a xr.DataArray or a netCDF file through the grid parameter of pygmt.grdimage. When using the netCDF file the gap at 360° or 0° longitude does not occur. Currently, I do not know why this is.
That’s a good workaround using NetCDF, but of course it should work with XArray input as well. There seems to be a more general problem with grdimage and the perspective projection in PyGMT using XArray input instead of a NetCDF file. I’ve now tried a geostationary satellite perspective: projection="G-47/0/35792.29/0/0/0/50/50/10c"
This results in the entire western hemisphere becoming empty when using XArray, not when using the NetCDF file saved via XArray. When changing the central latitude of the projection (the first zero above) to about 9 or higher, the issue goes away, perhaps because the pole comes in view.
I wonder if internally pygmt does some sort of preprocessing of the longitude coordinates in XArray grid data that it does not apply to data read from a NetCDF file (or vice versa). If so, I expect that there’s a bug in that part of the code. Perhaps that code is also where the warning comes from. I have not been able to find it yet.
Thanks @seisman, I had been looking for something like that. I did some more testing and the gap in the original problem only appears when both registration and gtype are set to 0. In all other cases, there is no gap. This makes sense.
When plotting the NetCDF file, there is never a gap, however. When using grdinfo and playing around with variants of xarray data saved to NetCDF files, I can also see that GMT can infer whether a geographic or Cartesian grid type is used, by whether or not there are coordinates named “longitude” and “latitude” (COARDS conventions I guess).
I think it would be helpful if grdimage would infer this as well, when fed with a fresh xarray structure, because the current solution of manually setting the gtype is not very easy to discover. I’m a 20+ year GMT user trying to convince some of my colleagues to try out pygmt, but these are the type of things that make them stop using it.
At the same time, the problem with grdimage using an xarray in the low-latitude perspective (geostationary satellite) projection seems to be a separate problem, which only gets worse when setting the gtype. When gtype=0 (Cartesian) only the Eastern hemisphere is plotted (as was shown above). When I now set gtype=1 (geographic), it does not even seem to read the z-values correctly, as it triggers a GMTCLibError with the following message:
grdimage [ERROR]: Failed to read CPT (null).
psconvert [ERROR]: System call [gs -q -dNOSAFER -dNOPAUSE -dBATCH -sDEVICE=bbox -DPSL_no_pagefill -dMaxBitmap=2147483647 -dUseFastColor=true '/Users/doornbos/.gmt/sessions/gmt_session.43073/gmt_112.ps-' 2> '/Users/doornbos/.gmt/sessions/gmt_session.43073/psconvert_43073c.bb'] returned error 256.
Error: /undefined in PSL_movie_label_completion
Operand stack:
PSL_eoc
Execution stack:
%interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push 1990 1 3 %oparray_pop 1989 1 3 %oparray_pop 1977 1 3 %oparray_pop 1833 1 3 %oparray_pop --nostringval-- %errorexec_pop .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval--
Dictionary stack:
--dict:770/1123(ro)(G)-- --dict:0/20(G)-- --dict:75/200(L)-- --dict:147/250(L)--
Current allocation mode is local
Last OS error: Invalid argument
Current file position is 20517
Error plotting reg=0,gtype=1
I find it weird that this error depends on the projection central latitude. When explicitly passing a CPT with the makecpt command, this error message goes away, but it results in an empty plot. Only the grdimage incantation which provides a NetCDF-filename as input seems to work with these projection parameters.
When plotting the NetCDF file, there is never a gap, however. When using grdinfo and playing around with variants of xarray data saved to NetCDF files, I can also see that GMT can infer whether a geographic or Cartesian grid type is used, by whether or not there are coordinates named “longitude” and “latitude” (COARDS conventions I guess).
Yes, GMT is more clever about guessing the grid type and registration, while PyGMT doesn’t have the ability now. c
I think it would be helpful if grdimage would infer this as well, when fed with a fresh xarray structure, because the current solution of manually setting the gtype is not very easy to discover. I’m a 20+ year GMT user trying to convince some of my colleagues to try out pygmt, but these are the type of things that make them stop using it.
Could you please open a feature request in the PyGMT repository?
At the same time, the problem with grdimage using an xarray in the low-latitude perspective (geostationary satellite) projection seems to be a separate problem, which only gets worse when setting the gtype. When gtype=0 (Cartesian) only the Eastern hemisphere is plotted (as was shown above). When I now set gtype=1 (geographic), it does not even seem to read the z-values correctly, as it triggers a GMTCLibError with the following message: