Skip to content

Commit 5b33672

Browse files
authored
Let -d set starting column via modifier (#5966)
* Let -d set starting column via modifier See #5630 for background discussion. The best solution is to add an optional modifier to override the default settings for the starting column, which for backwards compatible reasons shall remain 2 for input and 0 for output. * Update opt list
1 parent 1792ae6 commit 5b33672

14 files changed

+39
-25
lines changed

doc/rst/source/common_SYN_OPTs.rst_

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828

2929
.. |SYN_OPT-c| replace:: **-c**\ :ref:`row,col|index <-c_full>`
3030

31-
.. |SYN_OPT-d| replace:: **-d**\ :ref:`nodata <-d_full>`
31+
.. |SYN_OPT-d| replace:: **-d**\ [**+c**\ *col*]\ :ref:`nodata <-d_full>`
3232

33-
.. |SYN_OPT-di| replace:: **-di**\ :ref:`nodata <-di_full>`
33+
.. |SYN_OPT-di| replace:: **-di**\ [**+c**\ *col*]\ :ref:`nodata <-di_full>`
3434

35-
.. |SYN_OPT-do| replace:: **-do**\ :ref:`nodata <-do_full>`
35+
.. |SYN_OPT-do| replace:: **-do**\ [**+c**\ *col*]\ :ref:`nodata <-do_full>`
3636

3737
.. |SYN_OPT-e| replace:: **-e**\ :ref:`regexp <-e_full>`
3838

doc/rst/source/explain_-d.rst_

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
**-d**\ [**i**\|\ **o**]\ *nodata* :ref:`(more ...) <-d_full>`
1+
**-d**\ [**i**\|\ **o**]\ [**+c**\ *col*]\ *nodata* :ref:`(more ...) <-d_full>`
22
Replace input columns that equal *nodata* with NaN and do the reverse on output. |Add_-d|

doc/rst/source/explain_-d_full.rst_

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ The **-d** option
33

44
**Syntax**
55

6-
**-di**\|\ **o**\ *nodata*
6+
**-di**\|\ **o**\ *nodata*\ [**+c**\ *col*]
77
Substitute specific values with NaN.
88

99
**Description**
@@ -14,8 +14,10 @@ an unlikely value (e.g., -99999). Since GMT cannot guess this special missing da
1414
option to have such values replaced with NaNs. Similarly, the **-d** option can replace all NaNs with the chosed
1515
*nodata* value should the GMT output need to conform to such a requirement.
1616

17-
For input only, use **-di**\ *nodata* to examine all input columns. If any item equals *nodata*, the value is
18-
interpreted as a missing data item and is substituted with the value NaN.
17+
For input only, use **-di**\ *nodata* to examine all input columns after the first two. If any item equals *nodata*,
18+
the value is interpreted as a missing data item and is substituted with the value NaN.
1919

2020
For output only, use **-do**\ *nodata* to examine all output columns. If any item equals NaN, the NaN value is
2121
substituted with the chosen missing data value *nodata*.
22+
23+
Use the **+c** modifier to override the starting column used for the examinations [2 for input, 0 for output].

doc/rst/source/explain_-di.rst_

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
**-di**\ *nodata* :ref:`(more ...) <-di_full>`
1+
**-di**\ *nodata*\ [**+c**\ *col*] :ref:`(more ...) <-di_full>`
22
Replace input columns that equal *nodata* with NaN. |Add_-di|

doc/rst/source/explain_-do.rst_

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
**-do**\ *nodata* :ref:`(more ...) <-do_full>`
1+
**-do**\ *nodata*\ [**+c**\ *col*] :ref:`(more ...) <-do_full>`
22
Replace output columns that equal NaN with *nodata*. |Add_-do|

doc/rst/source/std-opts-classic.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Common Options (Classic Mode)
3434
- :ref:`Select binary input <-bi_full>`
3535
* - **-bo**\ *record*\ [**+b**\|\ **l**]
3636
- :ref:`Select binary output <-bo_full>`
37-
* - **-d**\ [**i**\|\ **o**]\ *nodata*
37+
* - **-d**\ [**i**\|\ **o**]\ *nodata*\ [**+c**\ *col*]
3838
- :ref:`Replace columns with nodata with NaN <-d_full>`
3939
* - **-e**\ [**~**]\ *"pattern"* \| **-e**\ [**~**]/\ *regexp*/[**i**]
4040
- :ref:`Filter data records that match the given pattern <-e_full>`

doc/rst/source/std-opts.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Common Options
3030
- :ref:`Select binary output <-bo_full>`
3131
* - **-c**\ [*row*\ ,\ *col*\|\ *index*]
3232
- :ref:`Advance plot focus to selected (or next) subplot panel <-c_full>`
33-
* - **-d**\ [**i**\|\ **o**]\ *nodata*
33+
* - **-d**\ [**i**\|\ **o**]\ *nodata*\ [**+c**\ *col*]
3434
- :ref:`Replace columns with nodata with NaN <-d_full>`
3535
* - **-e**\ [**~**]\ *"pattern"* \| **-e**\ [**~**]/\ *regexp*/[**i**]
3636
- :ref:`Filter data records that match the given pattern <-e_full>`

src/gmt_common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ struct GMT_COMMON {
162162
struct d { /* -d[i][o]<nan_proxy> */
163163
bool active[2];
164164
bool is_zero[2];
165+
unsigned int first_col[2]; /* Only apply from this column onward */
165166
double nan_proxy[2];
166167
char string[GMT_LEN64];
167168
} d;

src/gmt_init.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7645,6 +7645,7 @@ void gmtlib_explain_options (struct GMT_CTRL *GMT, char *options) {
76457645
case 'k': /* -di option to tell GMT the relationship between NaN and a nan-proxy for input */
76467646

76477647
GMT_Usage (API, 1, "\n%s Replace any <nodata> values in input data with NaN.", GMT_di_OPT);
7648+
GMT_Usage (API, 3, "+c Append first column to be affected [2].");
76487649
break;
76497650

76507651
case 'l': /* -l option to set up auto-legend items*/
@@ -7671,6 +7672,7 @@ void gmtlib_explain_options (struct GMT_CTRL *GMT, char *options) {
76717672

76727673
GMT_Usage (API, 1, "\n%s", GMT_do_OPT);
76737674
GMT_Usage (API, -2, "Replace any NaNs in output data with <nodata>.");
7675+
GMT_Usage (API, 3, "+c Append first column to be affected [0].");
76747676
break;
76757677

76767678
case 'f': /* -f option to tell GMT which columns are time (and optionally geographical) */
@@ -9064,29 +9066,38 @@ GMT_LOCAL int gmtinit_parse_data_range (struct GMT_CTRL *GMT, char *arg, unsigne
90649066

90659067
/*! . */
90669068
unsigned int gmt_parse_d_option (struct GMT_CTRL *GMT, char *arg) {
9067-
unsigned int dir, first, last;
9068-
char *c = NULL;
9069+
unsigned int dir, first, last, col, def_col[2] = {GMT_Z, GMT_X};
9070+
char *c = NULL, *q = NULL;
90699071

90709072
if (!arg || !arg[0]) return (GMT_PARSE_ERROR); /* -d requires an argument */
9073+
if ((q = strstr (arg, "+c"))) { /* Want to override start columns */
9074+
col = atoi (&q[2]);
9075+
q[0] = '\0'; /* Chop off for now */
9076+
}
90719077
if (arg[0] == 'i') {
90729078
first = last = GMT_IN;
90739079
c = &arg[1];
9080+
if (q) def_col[GMT_IN] = col;
90749081
}
90759082
else if (arg[0] == 'o') {
90769083
first = last = GMT_OUT;
90779084
c = &arg[1];
9085+
if (q) def_col[GMT_OUT] = col;
90789086
}
90799087
else {
90809088
first = GMT_IN; last = GMT_OUT;
90819089
c = arg;
9090+
if (q) def_col[GMT_IN] = def_col[GMT_OUT] = col;
90829091
}
90839092

90849093
for (dir = first; dir <= last; dir++) {
90859094
GMT->common.d.active[dir] = true;
90869095
GMT->common.d.nan_proxy[dir] = atof (c);
90879096
/* Need to know if 0 is used as NaN proxy since we must use a different comparison macro later */
90889097
GMT->common.d.is_zero[dir] = doubleAlmostEqualZero (0.0, GMT->common.d.nan_proxy[dir]);
9098+
GMT->common.d.first_col[dir] = def_col[dir];
90899099
}
9100+
if (q) q[0] = '+'; /* Restore modifier */
90909101
if (first == GMT_IN) strncpy (GMT->common.d.string, arg, GMT_LEN64-1); /* Verbatim copy */
90919102
return (GMT_NOERROR);
90929103
}

src/gmt_io.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3661,8 +3661,7 @@ GMT_LOCAL void *gmtio_ascii_input (struct GMT_CTRL *GMT, FILE *fp, uint64_t *n,
36613661
else /* Default column order */
36623662
col_pos = col_no;
36633663
n_convert = gmt_scanf (GMT, token, gmt_M_type (GMT, GMT_IN, col_pos), &val);
3664-
//n_convert = gmt_scanf (GMT, token, gmt_M_type (GMT, GMT_IN, in_col), &val);
3665-
if (n_convert != GMT_IS_NAN && col_pos > GMT_Y && gmt_input_is_nan_proxy (GMT, val)) /* Input matched no-data setting, so change to NaN */
3664+
if (n_convert != GMT_IS_NAN && gmt_input_is_nan_proxy (GMT, val, col_pos)) /* Input matched no-data setting, so change to NaN */
36663665
n_convert = GMT_IS_NAN;
36673666
if (n_convert == GMT_IS_NAN) { /* Got a NaN or it failed to decode the string */
36683667
if (GMT->current.setting.io_nan_records || !GMT->current.io.skip_if_NaN[col_pos]) { /* This field (or all fields) can be NaN so we pass it on */
@@ -4322,8 +4321,9 @@ void gmt_set_cartesian (struct GMT_CTRL *GMT, unsigned int dir) {
43224321
/*! Handles non-proxy checking for input z values. If the input value equals
43234322
* the non_proxy then we return true so the value can be replaced by a NaN.
43244323
*/
4325-
bool gmt_input_is_nan_proxy (struct GMT_CTRL *GMT, double value) {
4324+
bool gmt_input_is_nan_proxy (struct GMT_CTRL *GMT, double value, unsigned int col) {
43264325
if (!GMT->common.d.active[GMT_IN]) return false; /* Not active */
4326+
if (col < GMT->common.d.first_col[GMT_IN]) return false; /* Not in column range */
43274327

43284328
if (GMT->common.d.is_zero[GMT_IN]) return doubleAlmostEqualZero (0.0, value); /* Change to NaN if proxy value is ~zero */
43294329
return doubleAlmostEqual (GMT->common.d.nan_proxy[GMT_IN], value); /* Change to NaN if value ~nan_proxy for a non-zero proxy */
@@ -4613,7 +4613,7 @@ int gmtlib_process_binary_input (struct GMT_CTRL *GMT, uint64_t n_read) {
46134613
/* Determine if this was a segment header, and if so return */
46144614
for (col_no = n_NaN = 0; col_no < n_read; col_no++) {
46154615
if (!gmt_M_is_dnan (GMT->current.io.curr_rec[col_no])) { /* Clean data */
4616-
if (col_no > GMT_Y && gmt_input_is_nan_proxy (GMT, GMT->current.io.curr_rec[col_no])) /* Input matched no-data setting, so change to NaN */
4616+
if (gmt_input_is_nan_proxy (GMT, GMT->current.io.curr_rec[col_no], col_no)) /* Input matched no-data setting, so change to NaN */
46174617
GMT->current.io.curr_rec[col_no] = GMT->session.d_NaN;
46184618
else if (GMT->common.i.select) /* Cannot check here, done in gmtio_bin_colselect instead when order is set */
46194619
continue;

0 commit comments

Comments
 (0)