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
grdselect -Ai unable to report intersection when one grid is global? #8894
👋 Thanks for opening your first issue here! Please make sure you filled out the template with as much detail as possible. We appreciate that you took the time to contribute!
I tried a few other variations with the subset grd. When I used -R1/2/0/1 and placed the subset before the global grd eg.
gmt grdselect -Ai subset1_2_0_1.grd global.grd
then i got the subset region as the intersection. However if I put global.grd ahead of subset...grd then there is 'No common area'
After trying a few other variations it seems that if the first grid has an xmin=0, then the no common area warning results.
● Key insight: The intersection code at line 759-788 runs for every grid including the first one! There's no if (!first) guard. So for the first grid
(global 0/360), it intersects wesn (initialized from the same grid at line 735) with itself.
For the first grid:
- wesn = [0, 360, -90, 90] (from line 735)
- header->wesn = [0, 360, -90, 90] (same grid)
Line 780-783:
w = 0 - 720 = -720
e = 360 - 720 = -360
while (e < wesn[XLO]) // e < 0
-360 < 0 → w=-360, e=0
0 < 0? No → stop! w=-360, e=0
There's the bug! e = 0 and wesn[XLO] = 0, so 0 < 0 is false and the loop stops with w = -360, e = 0.
Then:
wesn[XLO] = MAX(0, -360) = 0
wesn[XHI] = MIN(360, 0) = 0 // ← BUG! XHI becomes 0!
Now wesn[XHI] = 0 and wesn[XLO] = 0, so at line 785: 0 <= 0 is true → "No common area"!
Issue #8894: Root Cause
The bug is in grdselect.c line 780-781, in the geographic longitude wrapping logic for intersection mode.
When the first grid has xmin = 0 (e.g., a global -Rg grid with wesn = [0, 360, -90, 90]), the code intersects it with itself (there's no skip for the
first grid). The wrapping loop:
double w = header->wesn[XLO] - 720.0, e = header->wesn[XHI] - 720.0;
while (e < wesn[XLO]) w += 360.0, e += 360.0;
With header->wesn = [0, 360] and wesn[XLO] = 0:
- w = -720, e = -360
- Iteration: e = -360 < 0 → w = -360, e = 0
- Check: e = 0 < 0? No → loop ends with e = 0
Then wesn[XHI] = MIN(360, 0) = 0, which collapses the X range to zero → "No common area".
The condition e < wesn[XLO] should be e <= wesn[XLO] — or more precisely, the loop should use < but account for the fact that a 360-degree span where e
equals wesn[XLO] is actually a valid full wrap. The simplest fix:
while (e <= wesn[XLO]) w += 360.0, e += 360.0;
But this would break the case where e should exactly equal wesn[XLO] (touching but not overlapping). A safer fix would be to skip the intersection for
the first grid entirely, or to check if the grid spans 360 degrees before applying the wrapping logic.
Activity
github-actions commentedon Feb 8, 2026
👋 Thanks for opening your first issue here! Please make sure you filled out the template with as much detail as possible. We appreciate that you took the time to contribute!
Please make sure you read our Contributing Guide and abide by our Code of Conduct.
ES-Solar commentedon Feb 11, 2026
I tried a few other variations with the subset grd. When I used -R1/2/0/1 and placed the subset before the global grd eg.
gmt grdselect -Ai subset1_2_0_1.grd global.grd
then i got the subset region as the intersection. However if I put global.grd ahead of subset...grd then there is 'No common area'
After trying a few other variations it seems that if the first grid has an xmin=0, then the no common area warning results.
joa-quim commentedon Mar 20, 2026
Claude's contribution
gmtselect: fix a missing intersection case.
gmtselect: fix a missing intersection case. (#8970)