Skip to content

Commit 242a54b

Browse files
authored
sample1d: fix NaNs in gaps zones (#9039)
Fix #8684 Assisted-by: Claude Opus 4.7
1 parent c84abf0 commit 242a54b

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

src/sample1d.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ EXTERN_MSC int GMT_sample1d(void *V_API, int mode, void *args) {
343343

344344
double *t_out = NULL, *dist_in = NULL, *ttime = NULL, *data = NULL;
345345
double low_t, high_t, *lon = NULL, *lat = NULL, *weight = NULL;
346+
double T_orig_min = 0.0, T_orig_max = 0.0; /* Snapshot of user's -T range so we can restore between segments */
346347

347348
struct GMT_DATASET *Din = NULL, *Dout = NULL;
348349
struct GMT_DATATABLE *Tout = NULL;
@@ -473,10 +474,12 @@ EXTERN_MSC int GMT_sample1d(void *V_API, int mode, void *args) {
473474
Dout->n_tables = Din->n_tables;
474475

475476
nan_flag = gmt_M_memory (GMT, NULL, Din->n_columns, unsigned char);
477+
T_orig_min = Ctrl->T.T.min; T_orig_max = Ctrl->T.T.max; /* gmt_create_array overwrites these per segment */
476478
for (tbl = 0; tbl < Din->n_tables; tbl++) {
477479
Tout = gmt_create_table(GMT, Din->table[tbl]->n_segments, 0, Dout->n_columns, 0U, false);
478480
Dout->table[tbl] = Tout;
479481
for (seg = 0; seg < Din->table[tbl]->n_segments; seg++) {
482+
Ctrl->T.T.min = T_orig_min; Ctrl->T.T.max = T_orig_max; /* Restore user's range for this segment */
480483
S = Din->table[tbl]->segment[seg]; /* Current segment */
481484
if (S->n_rows < 2) {
482485
GMT_Report(API, GMT_MSG_WARNING, "Table %" PRIu64 " Segment %" PRIu64 " has %" PRIu64 " record - no interpolation possible\n", tbl, seg, S->n_rows);
@@ -510,6 +513,7 @@ EXTERN_MSC int GMT_sample1d(void *V_API, int mode, void *args) {
510513
}
511514
else { /* Generate evenly spaced output */
512515
double min, max;
516+
bool multi_seg = (Din->n_tables > 1 || Din->table[tbl]->n_segments > 1);
513517
if (S->data[Ctrl->N.col][0] > S->data[Ctrl->N.col][S->n_rows-1]) { /* t-column is monotonically decreasing */
514518
min = (Ctrl->T.T.delay[GMT_X]) ? floor(S->data[Ctrl->N.col][0] / Ctrl->T.T.inc) * Ctrl->T.T.inc : Ctrl->T.T.min;
515519
max = (Ctrl->T.T.delay[GMT_Y]) ? ceil(S->data[Ctrl->N.col][S->n_rows-1] / Ctrl->T.T.inc) * Ctrl->T.T.inc : Ctrl->T.T.max;
@@ -520,6 +524,20 @@ EXTERN_MSC int GMT_sample1d(void *V_API, int mode, void *args) {
520524
max = (Ctrl->T.T.delay[GMT_Y]) ? floor(S->data[Ctrl->N.col][S->n_rows-1] / Ctrl->T.T.inc) * Ctrl->T.T.inc : Ctrl->T.T.max;
521525
Ctrl->T.T.reverse = false; /* Flag we are monotonically increasing in time for this segment */
522526
}
527+
if (multi_seg && !Ctrl->T.T.delay[GMT_X] && !Ctrl->T.T.delay[GMT_Y]) {
528+
/* Input split into multiple segments (e.g. by -g): clip this segment's output range to its
529+
* data extremes so gap zones produce no rows (avoids NaN fill that would otherwise need -s) */
530+
double s_lo = MIN(S->data[Ctrl->N.col][0], S->data[Ctrl->N.col][S->n_rows-1]);
531+
double s_hi = MAX(S->data[Ctrl->N.col][0], S->data[Ctrl->N.col][S->n_rows-1]);
532+
double clip_lo = ceil(s_lo / Ctrl->T.T.inc) * Ctrl->T.T.inc;
533+
double clip_hi = floor(s_hi / Ctrl->T.T.inc) * Ctrl->T.T.inc;
534+
if (clip_lo > clip_hi || clip_lo > max || clip_hi < min) {
535+
GMT_Report(API, GMT_MSG_INFORMATION, "Segment %" PRIu64 " in table %" PRIu64 " has no overlap with -T range; skipped.\n", seg, tbl);
536+
continue;
537+
}
538+
if (clip_lo > min) min = clip_lo;
539+
if (clip_hi < max) max = clip_hi;
540+
}
523541
if (gmt_create_array(GMT, 'T', &(Ctrl->T.T), &min, &max) != GMT_NOERROR) {
524542
GMT_Report(API, GMT_MSG_WARNING, "Segment %" PRIu64 " in table %" PRIu64 " had troubles.\n", seg, tbl);
525543
continue;

0 commit comments

Comments
 (0)