From 7b77e9e4df5dddc0e2676290df7905fe956a0f53 Mon Sep 17 00:00:00 2001 From: Esteban82 Date: Tue, 20 Oct 2026 10:37:18 -0300 Subject: [PATCH 1/2] grdcontour: Stop duplicating contours at phase singularities with -Z+p With -Z+p, a contour that ends at a phase singularity (e.g. the source and antipode of an SBAZ grid) dead-ends inside the grid without hitting NaNs, so the interior scan in gmt_contours only traced it one way. Every later start edge on the untraced part then retraced the whole line, since the tracer only checks used edges at the start. In test/grdcontour/periodic.sh the 20 contour was drawn 37 times and 140/160 twice, and the extra copies ran through the labels of the first one. Treat such a dead end like a NaN dead end so the other direction is traced and spliced, and stop that reverse trace when it runs into an edge that is already used. Output for non-periodic grids, including NaN-holed ones, is unchanged. Remove the known-failure flag from periodic.sh; its 2016 baseline must be regenerated. Co-Authored-By: Claude Opus 5.5 --- src/gmt_support.c | 5 +++++ test/grdcontour/periodic.sh | 10 ++-------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/gmt_support.c b/src/gmt_support.c index 9476630b76e..795a07691ff 100644 --- a/src/gmt_support.c +++ b/src/gmt_support.c @@ -2683,6 +2683,7 @@ GMT_LOCAL uint64_t gmtsupport_trace_contour (struct GMT_CTRL *GMT, struct GMT_GR if (n_exits == 0) { /* We have hit a field of NaNs, finish contour */ more = false; *nan_flag = n_nan; + if (n_nan == 0 && GMT->current.map.z_periodic) *nan_flag = 1; /* Dead end at a phase singularity, so also trace the other way */ continue; } else if (n_exits == 3) { /* Saddle point: Turn to the correct side */ @@ -2713,6 +2714,10 @@ GMT_LOCAL uint64_t gmtsupport_trace_contour (struct GMT_CTRL *GMT, struct GMT_GR ij0 = gmt_M_ij0 (G->header, row + d_row[side], col + d_col[side]); /* Non-padded index used for edge array */ edge_word = (unsigned int)(ij0 / 32 + d_side[side] * offset); edge_bit = (unsigned int)(ij0 % 32); + if (!test && (edge[edge_word] & bit[edge_bit])) { /* Ran into an already traced contour */ + more = false; + continue; + } edge[edge_word] |= bit[edge_bit]; if ((side == 0 && row == G->header->n_rows - 1) || (side == 2 && row == 1)) { /* Going out of grid at N or S */ diff --git a/test/grdcontour/periodic.sh b/test/grdcontour/periodic.sh index 9f7417cb24c..af79e2e5f47 100755 --- a/test/grdcontour/periodic.sh +++ b/test/grdcontour/periodic.sh @@ -1,16 +1,10 @@ #!/usr/bin/env bash -# GMT_KNOWN_FAILURE -# Update 2020/08/11 PW: Currently, the problem with this script is this: -# When plotting with -Zp, the top plot is mostly OK but the labels for 140 and 160 -# degrees have contours cutting right through them. The bottom plot without -Zp -# plots as expected. I added the debug drawing of the helper lines in red, which -# required the C-code changes to allow a debug pen. +# Test contouring of phase data with and without -Z+p ps=periodic.ps gmt grdmath -Rg -I1 32 22 SBAZ = t.nc # Plot as non-periodic. This plots labels. The pile of blue contours around # 180 occurs because the grid goes rapidly from -180 to +180 here and -Z+p is not used # This is expected and correct. gmt grdcontour t.nc -JG0/0/5.0i -A20+u@.+d0.75p,red -Bgaf -K -Wa1,blue -Gl-90/0/0/0,0/0/90/0 -P -Xc -Y0.3i > $ps -# Plot as periodic with -Zp. Transition from -180 to +180 goes well for contouring -# but lon 140, 160 has labels overdrawn by contour. This is the main issue here. +# Plot as periodic with -Z+p. Transition from -180 to +180 goes well for contouring gmt grdcontour t.nc -J -A20+u@.+d0.75p,red -Wa1,blue -Bgaf -Gl-90/0/0/0,0/0/90/0 -Z+p -O -Y5.2i >> $ps From 1f5779cff49f8afb0e4d5f4ff53331f7d49d56c6 Mon Sep 17 00:00:00 2001 From: Esteban82 Date: Mon, 16 Nov 2026 22:31:05 -0300 Subject: [PATCH 2/2] Update the grdcontour/periodic baseline The old baseline (GMT 5.3, 2016) still showed the contours through the 140 and 160 degree labels and lacked the red debug helper line added to the script in 2020. Co-Authored-By: Claude Opus 5.5 --- test/baseline/grdcontour.dvc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/baseline/grdcontour.dvc b/test/baseline/grdcontour.dvc index 7735fe26422..a749623d89f 100644 --- a/test/baseline/grdcontour.dvc +++ b/test/baseline/grdcontour.dvc @@ -1,5 +1,6 @@ outs: -- md5: aceec929e8ab2a939a5cfacf0dbf3b4c.dir +- md5: 086ead5b15b5bca2ca31537f94ccf4bf.dir nfiles: 23 path: grdcontour hash: md5 + size: 3965721