You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow the use of a separate file for shading (grdimage -I) #618
The gmt command grdimage has an option -I that adds shading to another image by modulating the intensity. Historically, (up until gmt v5), this command has only taken as an option another grd filename. One common use of this feature was to plot something like a gravity, the geoid, or magnetic field in color, and then apply shading using a topography file to provide geologic context.
Since gmt 5, there has been the option of using the input grd file along with grdgradient to automatically generate an intensity file that would be used with the -I command. I believe that pygmt only accepts the option shading=True which automatically generates such an intensity file using gmt defined default illumination angle parameters.
I am asking that pygmt fully implement the grdimage -I option. This is the only feature that I am currently missing in pygmt, and which is forcing me to use both gmt and pygmt separately to generate publication quality images for journal articles.
For reference, here is the relevant part of the gmt grdimage man page:
-I Apply directional illumination. Append name of intensity grid file.
For a constant intensity (i.e., change the ambient light), append a value.
To derive intensities from <grd_z> instead, append +a<azim> [-45], +n<method> [t1], and +m<ambient> [0]
or use -I+d to accept the default values (see grdgradient for details).
Hi @MarkWieczorek, I agree that this will be a useful feature. Do you have an example for this/a map illustration? Even just the pure GMT command will do (best if we can access the data too).
Well, here is what it should look like (Earth geoid with topography illumination). Sorry for the rainbow colormap, this is a few years old:
I could look through my files, but the scripts will most likely be for gmt 5, and the grd files are pretty big. The only peculiarity with specifying a -I file is that the grid size and grid registration need to be exactly the same. If you really need something to test, I could upload them somewhere. But, the easiest test would be to just use the same grid for the colormap as for -I and see if you get the same thing as -I=True.
I was thinking more along the lines of a GMT Remote Data-Set (i.e. the @ files), but not sure if there's one that would be suitable? Could you please post the code too for generating this map? Sorry for the fuss, just that I'm not familiar with shading.
I believe that pygmt only accepts the option shading=True which automatically generates such an intensity file using gmt defined default illumination angle parameters.
I don't test it, but I believe you can already use shading="/Users/lunokhod/Mars/Topography/Mars_16_srm.grd" to speify the intensity grid. Please let us know if it works for you.
Of course, there are more things to do:
the shading is not well documented in the grdimage function
currently we can't pass an xarray grid to shading.
I don't test it, but I believe you can already use shading="/Users/lunokhod/Mars/Topography/Mars_16_srm.grd" to speify the intensity grid. Please let us know if it works for you.
I can confirm that this works, here's a very minimal working example:
the shading is not well documented in the grdimage function
currently we can't pass an xarray grid to shading.
Agree with both points, we can better document grdimage and allow for xarray grids to shading in the same PR. But note that for point 2 (shading with xarray grids), whatever we do won't work properly until we fix #364/#581 (an upstream GMT issue).
I finally got around to testing this, and grdimage with shading works fine for me as long as I specify a pre-existing filename. For my pipeline, a hack solution is to save an xarray DataArray as a netcdf file, and then to use this filename with the shading option.
Obviously, it would be better if we could just specify the xarray DataArray directly in the shading option.
@MarkWieczorek I've implemented the requested feature in #750 and it seems working well (I only did a few simple tests and eye-checking the images). It would be good if you can test it and give some feedbacks.
I've opened a GMT conda-forge dev build at conda-forge/gmt-feedstock#173 which will contain these patches, and allow us to test things easily using conda install -c conda-forge/label/dev gmt. So we should be able to close this issue once PyGMT bumps the minimum GMT version to 6.3, and the xfail is removed from the test below:
Activity
weiji14 commentedon Sep 18, 2020
Hi @MarkWieczorek, I agree that this will be a useful feature. Do you have an example for this/a map illustration? Even just the pure GMT command will do (best if we can access the data too).
MarkWieczorek commentedon Sep 18, 2020
Well, here is what it should look like (Earth geoid with topography illumination). Sorry for the rainbow colormap, this is a few years old:
I could look through my files, but the scripts will most likely be for gmt 5, and the grd files are pretty big. The only peculiarity with specifying a
-Ifile is that the grid size and grid registration need to be exactly the same. If you really need something to test, I could upload them somewhere. But, the easiest test would be to just use the same grid for the colormap as for-Iand see if you get the same thing as-I=True.weiji14 commentedon Sep 18, 2020
I was thinking more along the lines of a GMT Remote Data-Set (i.e. the
@files), but not sure if there's one that would be suitable? Could you please post the code too for generating this map? Sorry for the fuss, just that I'm not familiar with shading.MarkWieczorek commentedon Sep 18, 2020
Here is something for a different image
In essence, if you are creating an image with grdimage, you just add the option
-Ifile.grdweiji14 commentedon Sep 18, 2020
Cool, thanks for the example. This sounds very similar to the
drapegrid/-Goption inpygmt.grdview, and it could perhaps be implemented similarly:pygmt/pygmt/base_plotting.py
Lines 433 to 444 in d3e131a
I'll take a closer look over the weekend (getting late for me here), but you're more than welcome to open up a Pull Request to speed up the process 😄.
seisman commentedon Sep 18, 2020
I don't test it, but I believe you can already use
shading="/Users/lunokhod/Mars/Topography/Mars_16_srm.grd"to speify the intensity grid. Please let us know if it works for you.Of course, there are more things to do:
shadingis not well documented in thegrdimagefunctionshading.MarkWieczorek commentedon Sep 18, 2020
I'll test (1) monday, but (2) is what I really want!
seisman commentedon Sep 18, 2020
Yes, (2) is definitely a missing feature!
weiji14 commentedon Sep 20, 2020
I can confirm that this works, here's a very minimal working example:
However, shading an
xarraygrid with anintensfiledoesn't work, likely due to same issue at #364/#581:Agree with both points, we can better document
grdimageand allow forxarraygrids toshadingin the same PR. But note that for point 2 (shading with xarray grids), whatever we do won't work properly until we fix #364/#581 (an upstream GMT issue).seisman commentedon Oct 15, 2020
It seems that the upstream GMT PR GenericMappingTools/gmt#4328 also fixes this issue.
Running @weiji14's script above gives me a plot which looks correct (with the GMT master branch
version: 6.2.0_8a4275f_2020.10.14):But we need some more tests.
MarkWieczorek commentedon Dec 18, 2020
I finally got around to testing this, and grdimage with shading works fine for me as long as I specify a pre-existing filename. For my pipeline, a hack solution is to save an xarray DataArray as a netcdf file, and then to use this filename with the shading option.
Obviously, it would be better if we could just specify the xarray DataArray directly in the shading option.
seisman commentedon Dec 19, 2020
@MarkWieczorek I've implemented the requested feature in #750 and it seems working well (I only did a few simple tests and eye-checking the images). It would be good if you can test it and give some feedbacks.
seisman commentedon Jun 10, 2021
The feature request has been implemented in #750 and now merged in master. Now
grdimagecan accept a netCDF grid or a xarray.DataArray as shading.However, the grdimage shading with a xarray.DataArray has known upstream bugs (see #750 (comment), #750 (comment), and GenericMappingTools/gmt#5294).
I'm reopening this issue to better track the upstream bug.
weiji14 commentedon Nov 5, 2021
The last bits of the upstream CPT stretching issue (GenericMappingTools/gmt#5294) should be fixed with GenericMappingTools/gmt#5947 and GenericMappingTools/gmt#5948 (thanks Meghan and Paul!) and will be available in GMT 6.3.
I've opened a GMT conda-forge dev build at conda-forge/gmt-feedstock#173 which will contain these patches, and allow us to test things easily using
conda install -c conda-forge/label/dev gmt. So we should be able to close this issue once PyGMT bumps the minimum GMT version to 6.3, and the xfail is removed from the test below:pygmt/pygmt/tests/test_grdimage.py
Lines 120 to 125 in 5dc135b
The test is currently XPASS at https://github.com/GenericMappingTools/pygmt/runs/4111894254?check_suite_focus=true#step:15:295 😁