Skip to content

grdselect -Ai unable to report intersection when one grid is global? #8894

Closed

Description

@mwoot

Description

grdselect -Ai returns an unexpected error when trying to find the intersection between a global grid and another grid. Issue comes from my forum post.

Full script that generated the error

gmt grdcut @earth_geoid_01d_p -Rg -Gglobal.grd
gmt grdcut global.grd -R0/1/0/1 -Gsubset.grd
gmt grdselect -Ai global.grd subset.grd

Full error message

grdselect [WARNING]: No common area for all grids is possible.

Expected outcome

-R0/1/0/1

Running the intersection between two non-global grids works as expected:

gmt grdcut global.grd -R0/15/0/15 -Gbigger_subset.grd
gmt grdselect -Ai subset.grd bigger_subset.grd

System information

  • Operating system: Ubuntu 22.04.5 LTS
  • GMT version (gmt --version): 6.6.0

Activity

  1. added
    bug
    on Feb 8, 2026
  2. github-actions commented on Feb 8, 2026

    @github-actions
    Contributor

    👋 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.

  3. ES-Solar commented on Feb 11, 2026

    @ES-Solar
    Collaborator

    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.

  4. joa-quim commented on Mar 20, 2026

    @joa-quim
    Member

    Claude's contribution

    ● 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.
    
  5. added 2 commits that reference this issue on Apr 12, 2026
    e379d52
    9880a31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions