Repository navigation
Description
Related to this post in the GMT Forum: https://forum.generic-mapping-tools.org/t/how-to-create-a-geotiff-with-pygmt/4113
The img_out (A) parameter of Figure.grdimage does not allow to save a grid to a raster format (.bmp, .gif, .jpg, .png, or .tif) as proposed by the documentation https://www.pygmt.org/dev/api/generated/pygmt.Figure.grdimage.html:
import pygmt
fig = pygmt.Figure()
fig.grdimage(grid="@earth_age_01d_g", img_out="test_pygmt_grdimage.tif")The relatd GMT code works (see https://docs.generic-mapping-tools.org/dev/grdimage.html#a):
gmt begin
gmt grdimage @earth_age_01d_g -Atest_gmt_grdimage.tif
gmt end
In PyGMT, a figure can be saved as a .bmp, .gif, .jpg, .png, or .tif file via Figure.savefig (https://www.pygmt.org/dev/api/generated/pygmt.Figure.savefig.html). But I am not sure whether this is identical to using img_out of pygmt.Figure.grdimage.
import pygmt
fig = pygmt.Figure()
fig.grdimage(grid="@earth_age_01d_g")
fig.savefig(fname="test_pygmt_savefig.tif") Error message
GMTCLibError: Module 'grdimage' failed with status code 72:
grdimage [ERROR]: Option -A: No output argument allowed
grdimage [ERROR]: Option -A: Must provide an output filename for image
System information
PyGMT information:
version: v0.9.1.dev98
System information:
python: 3.11.4 | packaged by conda-forge | (main, Jun 10 2023, 17:59:51) [MSC v.1935 64 bit (AMD64)]
executable: C:\ProgramData\Anaconda3\envs\pygmt_env_dev\python.exe
machine: Windows-10-10.0.19045-SP0
Dependency information:
numpy: 1.24.3
pandas: 2.0.2
xarray: 2023.1.1.dev17
netCDF4: 1.6.2
packaging: 23.1
contextily: 1.3.0
geopandas: 0.13.2
IPython: 8.14.0
rioxarray: 0.14.1
ghostscript: 9.54.0
GMT library information:
binary version: 6.4.0
cores: 4
grid layout: rows
image layout:
library path: C:/ProgramData/Anaconda3/envs/pygmt_env_dev/Library/bin/gmt.dll
padding: 2
plugin dir: C:/ProgramData/Anaconda3/envs/pygmt_env_dev/Library/bin/gmt_plugins
share dir: C:/Program Files (x86)/gmt6/share
version: 6.4.0
GDAL: gdal 3.7.0
Activity
[-]Figure.grdimage: "img_out" ("A"") does not work[/-][+]Figure.grdimage: "img_out" ("A") does not work[/+]weiji14 commentedon Jul 13, 2023
Thanks @yvonnefroehlich for writing up this detailed bug report! I can reproduce the error locally, and am wondering if this is a case of #896, whereby the 'plotting' function
grdimagedoesn't output a figure, but to a file instead. Will need to look into theclibpart of PyGMT to see if this is an issue on the PyGMT side, or with the GMT C API, cc @seisman.In the meantime, it might be better to recommend users to use other libraries like
rioxarray's to_raster if saving to GeoTIFF. Other image formats like bmp/gif/png might be better handled by OpenCV, scikit-image or others.seisman commentedon Oct 7, 2023
I tried the following script:
It produces two files: "map.png" and "test_out.tif". "map.png" only has a frame, and "test_out.tif" has no frame. Looking at the source codes, it seems grdimage -A produces an raster image of the current grid, without writing anything to a PS file.
@PaulWessel Could you please explain what the purpose and use case of the -A option, so that we can decide if we should have this feature on the PyGMT side?
PaulWessel commentedon Oct 7, 2023
Predate wrappers. @joa-quim added it, but cannot imagine it should be available in modern mode?
PaulWessel commentedon Oct 7, 2023
The purpose was to use grdimage to directly give a raster image from the grid, with no border etc. But clearly, I think it should be disallowed in modern mode since we cannot tell we are building a PS file. In classic mode I think it is fine and backwards compatible. OK with this, @joa-quim ?
joa-quim commentedon Oct 7, 2023
Why remove? Let me recall that this option let us create GeoTIFF or GeoPDF images independently of the Ghostscript machinery. And if removed it would have to be removed from the grdimage modern mode docs that is the main source of our documentation (the one that opens by default ).
I’m not sure how it works in modern mode. Maybe it needs to have a full file name to prevent it to be written in the session directory.
PaulWessel commentedon Oct 9, 2023
I agree, just leave it. However, with -A it skips all the PS setup so perhaps @seisman can tell us what confused him - should we improve the docs to make it very clear that -A basically disable the normal PS flow and only writes a raster file and -B etc are not available?
seisman commentedon Oct 9, 2023
I'm OK with leaving it as it is.
What's the difference between
and
As I understand it, they're the same, but grdimage -A writes TIFF directly without producing a PS file.
Is it equivalent to calling
gdal_translatedirectly to convert a netCDF file into a TIFF file?joa-quim commentedon Oct 10, 2023
Almost, but the first form, the one involving -A, lets us do reprojection, which
gdal_translatedoes not do. And one can apply shading as well in -A.PaulWessel commentedon Oct 10, 2023
Also, we should give error if -A is used with -B.
seisman commentedon Nov 20, 2023
@PaulWessel In PR #2765, I was working on the
grdimage's-Aoption. For me, the new test related to-Aworks well but then other tests crash (which should not happen).When I read the
grdimagesource code, I see that theGMT_IMAGE *Outobject (https://github.com/GenericMappingTools/gmt/blob/a8f05c9bc6aed955be8f974928b0abf008d8f42c/src/grdimage.c#L1275) is the one that holds the output image, but it seems it's never freed (I don't see aGMT_Destroy_Datacall). Is it intentional or is it a potential bug?PaulWessel commentedon Nov 20, 2023
Don't think so. Lots of places were we dont call GMT_Destroy_Data in the module. They are all handled by the garbage collector gmtlib_garbage_collection when the module ends.
2 remaining items
joa-quim commentedon Dec 11, 2023
PDF is a vector format, not an image.
seisman commentedon Dec 11, 2023
I have no idea about what it really does, but the docs says:
I think it means
=PDFis allowed?joa-quim commentedon Dec 11, 2023
Funny that it even works in some cases in PyGMT because in Julia
and that comes from the condition
so I don't see how it can work at all for PyGMT. Is it not detected as an
externalat that point?seisman commentedon Dec 11, 2023
In PyGMT, when
-Aimg_out.pngis given, we call thegrdimagemodule using the following syntax:PaulWessel commentedon Dec 11, 2023
@joa-quim would know but grdimage -A ends up calling GDAL for writing and I dont know if it is OK there to write a binary image to stdout.
joa-quim commentedon Dec 11, 2023
I don't remember any details but we certainly pass a file name to GDAL for it to save the image. No idea what redirecting to stdout does.
Tried and it errors on CLI
seisman commentedon Dec 12, 2023
This doesn't work for GMT CLI. For external wrappers, there are some special handling with
> lixo.png. Related codes are:https://github.com/GenericMappingTools/gmt/blob/29001d863941e6e4fd3f98a347dba93d63b86250/src/grdimage.c#L257C9-L257C9
https://github.com/GenericMappingTools/gmt/blob/29001d863941e6e4fd3f98a347dba93d63b86250/src/grdimage.c#L1299
joa-quim commentedon Dec 12, 2023
OK, but actually this complained but worked for me.
seisman commentedon Dec 12, 2023
Yes, it also works in PyGMT, but it crashes if I call
gmt imageafter this command. In other words, this one complains but works:This one also works:
but this one crashes:
I think the equivalent Julia code is:
joa-quim commentedon Dec 12, 2023
So it crashes when you call
imageaftergrdimage -A?Can´t reproduce it. This works.
But this is puzzling me
There is no
+pin -D option in thepsimagecall as can be seen in the generated command.joa-quim commentedon Dec 12, 2023
There must be a bug in
psimageProbably -F is not being parsed
seisman commentedon Apr 8, 2024
Closing the issue because we won't implement it in PyGMT.