Load grid in order to flip coordinates

Hello, I’m trying to use some raster grids that were provided to me, but I’m having a problem with the format.

I was trying to run pygmt.grdtrack() but I got an error every time. Then tried to plot my data with pygmt.Figure.grdimage() and realized that the dimensions were read reversed (longitude in the y axis, latitude in the x axis). I wanted to open the grid in order to flip the dimensions, but pygmt.load_dataarray() returned did not find a match in any of xarray’s currently installed IO backends [‘netcdf4’, ‘scipy’].

Is there a different way to open a grid in order to flip the dimensions, or maybe a way to indicate to grdtrack that the dimensions are flipped? My last resort would be to run grdtrack with the points of interest in [‘lat’,‘lon’], but I believe that would make for a different geometry.

Thank you very much in advance.

specifying coltypes='0y,1x' should swap grid lon and lat, both for Figure.grdimage and Figure.grdtrack, details: gmt — GMT 6.5.0 documentation

Thank you very much!

Edit: Tried it but didn’t help, it feels as if pygmt is ignoring my request for changing the columns. Both coltypes=‘0y,1x’ and coltypes=‘0x,1y’ return the exact same output. I don’t really get if it’s an issue of the file format, maybe because it doesn’t have columns? It’s a binary

time to post your code, (presumably wrong) plots and error messages.

otherwise it is not clear what is going on.

Thank you for the help. I was told the error was actually on the grid files. I still believe it’s weird the coordinates wouldn’t flip with the coltypes option, but I’ve now got the correct grids and expected output. The grids are not mine so I’m not comfortable sharing them, but I’ll try to make a synthetic example to post later.

The coltypes options are for tables not for grids. The grid is a matrix of zz’s

From the docs (grdedit):

The grid bad.nc has latitude as x-coordinates an longitude as y-coordinates. We can exchange the two dimension by running:

gmt grdedit bad.nc -Ee -Gnew.nc

1 Like

Thanks for explaining. Presence of -f option for grdimage appears irrelevant then.

My simple guess was that reading a matrix row after row or column after column can be configurable. It seems not, as it follows from your explanations.

While that could be done, and in fact it can in MEX and Julia, it makes no sense for plain GMT where the internal memory layout is fixed (row major scanline).

Regarding -f, it does more than only x,y. It can, for example, be used also to set a time axis.

OK so grdimage seems to support a limited subset of -f option. Thanks again for the explanations.

how can this be useful in practice? netcdf and geotiff seem to always specify axis data type and interval. Or is it for some binary grid format with no metadata included? For someone not very experienced it is difficult to imagine a practical use case at once.

I’m not 100% sure but I think I’ve seen it already in GMT. An absolute time axis is a number of seconds since a reference (default is Unix time) so nothing prevents having xx as time and yy as some-value. To plot date/time annotations we would need to tell grdimage that xx is time.

does -R not do exactly this, i.e. setting data range and format incl time axis? Of course, -R could be omitted to plot the full extent of the grid. And projection specification, must it not explicitly be specified as cartesian if one axis is time? Unless -f is used to set all of the above.