-
Notifications
You must be signed in to change notification settings - Fork 368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement image subsets within GMT and via grdcut #5798
Conversation
As a way to debug and implement subsets of images, I am letting grdcut -I get the ability to cut a subset out of an image. This also makes it easier to debug this feature which is still not working in GMT (and affects grdimage, for instance, where the entire image must be read in). Derives bits and pieces from stalled PR #4004.
Not sure that I understand your last post - are you saying to ignore the problems with the colors? |
No, there are two things:
|
Hi @joa-quim, towards the end of gmtlib_read_image in gmt_grdio.c there is
In our case, the image I has TRBa which the to_gdalread version says BRPa, so we copy it over. This gives the above wrong coloring. Should it be another string? |
OK, this is an indexed image so not wrong R/G/B order, but upon comparing the image produced with grdcut to the equivalent derived via
I find that the two colortables are just very different. How can we get that wrong? |
Stepping through to see what happens to the indexed colors. Line 539 gmt_gdalread.c: We are getting the colors one by one from GDAL and storing in the Ctrl->ColorMap 4x256 array. For each color i = 0, 1, ...you do this
That means the RGBA for the first entry in the colormap is the first 4 values in Ctrl->ColorMap. For my example that is 224,164,84,255. Then in Line 177 (gmt_gdalwrite.c) you place these values into another array, this time 4x256 float:
So here, let k = 0. Then you get the red (colormap[0]) but not the green from the first (since that would be colormap[1], divide by 255 and store it in the same place in the cpt array. I guess this is OK but not sure why you do it this way since it does not follow the structure of colormap. Finally, just before we call GDAL to write (line 375) you do this:
Again, try i = 0. This picks supposedly an r, g, b from the ptr (which is the cpt above) and then loads up the sEntry structure. For i = 0 these become 224,72,72,255. So no longer 224,164,84,255. I think that is why the image looks junk. Please let me know what you were trying to do here, @joa-quim |
I think there must be missing some step in the middle. While the first case stores the color matrix as row-major the other two are accessing it as column major. The column major ordering is quite likely because when that part was written the main idea was to interface with the MEX. |
OK, I can ensure the ColorMap is always stored internally in column order throughout and see how that goes. |
There are other places in GMT where the assumtion is that colormap is rows, e.g. in gmtapi_expand_index_image we do
I guess I will need to go through all of GMT where ->colormap is referenced and ensure it is set and used as columns. It is not that many places I think. |
OK, I have gotten grdcut -I to work. Not tested extensively on different images though, just earth_day, but output matches gdal_translate. This is reading and returning an image with the pad (as expected for instance in grdimage) but when we write we strip off the pad.
Of course, it cannot handle periodic boundary at ±180:
|
The read sub-regions part originated long ago in Mirone and either the number of options were more limited, or more likely my knowledge. It seems we should be using -a_ullr instead of -srcwin since former let us specify the coordinates in geogs while latter uses only row/col. GDAL can also deal with round Earth but it often needs to be told to do so. |
Would it be possible to get this to work without the -I argument being required, similar to how grdimage accepts grid | image without any extra user input? |
This branch also has a lot of test failures caused by problems similar to 'grdimage [ERROR]: Computed -srcwin falls outside raster size of ...', which could be caused by this. |
Yes, probably. Did -I in this branch just to get things going. |
Yes, it looks like we need to upgrade the gdalread to specify geographic coordinates instead when the image is global. Can @joa-quim get us started on that, i.e., how to pass different arguments with coordinates instead? |
Needed to turn on data padding in grdcut.
Is this suppose to solve #4003 ? I try the following script. For some resolutions, it worked. But failed for other. I try it before the "update grdcut.c" commit. Failed: 01d, 20m. 06m, 05m, 03m, 02m
|
I see the 01d crashes. Will have a look - I did not test specifically with intensity. We may turn your example into a test once it is working. |
Now I try it after the commit. These are the errors (indicated by resolution of the grid/image): 02m, 03m and 01d: 05m and 06m: 20m: |
This may drag into tomorrow as I have been busy moving my UH home office back to UH today. Setting up my computer rig in the office for the first time since March 2020... |
The three test differences look fine. |
Better use irint to get nearest integer than rely on adding noise and cast to int.
I think I just fixed that @Esteban82 - update and give it a check. |
Ok. I will check them as soon as posible. |
I am getting a segfault in a 6.2 script that I have used to plot images created by grdmix. I'll send an email with more details so that I can share the data since it's not using a remote file. |
It works with all the resolution, even 30s. Is there any other any test besides Meghan issues. |
Add to gmt_raster_type since also needed in grdcut.
Test based on Federico's Caribbean case plus a similar one straddling 180. Passes.
Thanks, I have added a test based on @Esteban82 example above (same plus one that straddles the Dateline) and it passes. Also fixed the things @meghanrjones mentioned related to her non-global large TIFF. Hopefully we are ready to approve. |
I'm getting a crash when trying to visualize and indexed image whose that is supposed to have transparency set to one of the rows of the cpt. That is done a bit under the hood by setting
However, it also crashes in master so the breaking must have occurred after I implemented it but before the work in this branch. So it should not be a merge stopper. I'll look to what's happening when I have time. |
As a way to debug and implement subsets of images, I am letting gmt grdcut -I get the ability to cut a subset out of an image. This also makes it easier to debug this feature which is still not working in GMT (and affects grdimage, for instance, where the entire image must be read in, leading to excessive run times and memory requirements for high-resolution global grids). Derives bits and pieces from stalled PR #4004. Note: I also updated the KEYS and GMT_Encode_Options so it should be possible to receive the image back from the externals as well.
Trying this command:
gmt grdcut @earth_day_01m -R0/10/0/10 -I -Gcut.png
Yields an image with the correct region, increment meta data but likely bad info on the order or RGB, yet the pixels are in the right layout (since Africa outline etc is correct) and the region matches that of a pscoast plot:
Hoping @joa-quim can advise where to make changes in this PR.
Note: This PR allocates memory only for the subset and pad, and currently grdimage commands like the one below will crash in the PR because it assumes a full image. This will be dealt with once we correctly can read subsets of images. However, in master, this comand
gmt grdimage @earth_day_01m -R0/10/0/10 -JQ5.07614213198c -png out
yields this (same-size) image (for comparison with above):
Clearly, this is an incorrect region despite having the RGB in the right order, so this seems to be a bug in master. I suggest we work on getting the subset to work correctly via grdcut and then the rest will be simpler.