Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let -d set starting column via modifier #5966

Merged
merged 2 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions doc/rst/source/common_SYN_OPTs.rst_
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@

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

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

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion doc/rst/source/explain_-d.rst_
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
**-d**\ [**i**\|\ **o**]\ *nodata* :ref:`(more ...) <-d_full>`
**-d**\ [**i**\|\ **o**]\ [**+c**\ *col*]\ *nodata* :ref:`(more ...) <-d_full>`
Replace input columns that equal *nodata* with NaN and do the reverse on output. |Add_-d|
8 changes: 5 additions & 3 deletions doc/rst/source/explain_-d_full.rst_
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ The **-d** option

**Syntax**

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

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

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

For output only, use **-do**\ *nodata* to examine all output columns. If any item equals NaN, the NaN value is
substituted with the chosen missing data value *nodata*.

Use the **+c** modifier to override the starting column used for the examinations [2 for input, 0 for output].
2 changes: 1 addition & 1 deletion doc/rst/source/explain_-di.rst_
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
**-di**\ *nodata* :ref:`(more ...) <-di_full>`
**-di**\ *nodata*\ [**+c**\ *col*] :ref:`(more ...) <-di_full>`
Replace input columns that equal *nodata* with NaN. |Add_-di|
2 changes: 1 addition & 1 deletion doc/rst/source/explain_-do.rst_
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
**-do**\ *nodata* :ref:`(more ...) <-do_full>`
**-do**\ *nodata*\ [**+c**\ *col*] :ref:`(more ...) <-do_full>`
Replace output columns that equal NaN with *nodata*. |Add_-do|
2 changes: 1 addition & 1 deletion doc/rst/source/std-opts-classic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Common Options (Classic Mode)
- :ref:`Select binary input <-bi_full>`
* - **-bo**\ *record*\ [**+b**\|\ **l**]
- :ref:`Select binary output <-bo_full>`
* - **-d**\ [**i**\|\ **o**]\ *nodata*
* - **-d**\ [**i**\|\ **o**]\ *nodata*\ [**+c**\ *col*]
- :ref:`Replace columns with nodata with NaN <-d_full>`
* - **-e**\ [**~**]\ *"pattern"* \| **-e**\ [**~**]/\ *regexp*/[**i**]
- :ref:`Filter data records that match the given pattern <-e_full>`
Expand Down
2 changes: 1 addition & 1 deletion doc/rst/source/std-opts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Common Options
- :ref:`Select binary output <-bo_full>`
* - **-c**\ [*row*\ ,\ *col*\|\ *index*]
- :ref:`Advance plot focus to selected (or next) subplot panel <-c_full>`
* - **-d**\ [**i**\|\ **o**]\ *nodata*
* - **-d**\ [**i**\|\ **o**]\ *nodata*\ [**+c**\ *col*]
- :ref:`Replace columns with nodata with NaN <-d_full>`
* - **-e**\ [**~**]\ *"pattern"* \| **-e**\ [**~**]/\ *regexp*/[**i**]
- :ref:`Filter data records that match the given pattern <-e_full>`
Expand Down
1 change: 1 addition & 0 deletions src/gmt_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ struct GMT_COMMON {
struct d { /* -d[i][o]<nan_proxy> */
bool active[2];
bool is_zero[2];
unsigned int first_col[2]; /* Only apply from this column onward */
double nan_proxy[2];
char string[GMT_LEN64];
} d;
Expand Down
15 changes: 13 additions & 2 deletions src/gmt_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -7645,6 +7645,7 @@ void gmtlib_explain_options (struct GMT_CTRL *GMT, char *options) {
case 'k': /* -di option to tell GMT the relationship between NaN and a nan-proxy for input */

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

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

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

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

/*! . */
unsigned int gmt_parse_d_option (struct GMT_CTRL *GMT, char *arg) {
unsigned int dir, first, last;
char *c = NULL;
unsigned int dir, first, last, col, def_col[2] = {GMT_Z, GMT_X};
char *c = NULL, *q = NULL;

if (!arg || !arg[0]) return (GMT_PARSE_ERROR); /* -d requires an argument */
if ((q = strstr (arg, "+c"))) { /* Want to override start columns */
col = atoi (&q[2]);
q[0] = '\0'; /* Chop off for now */
}
if (arg[0] == 'i') {
first = last = GMT_IN;
c = &arg[1];
if (q) def_col[GMT_IN] = col;
}
else if (arg[0] == 'o') {
first = last = GMT_OUT;
c = &arg[1];
if (q) def_col[GMT_OUT] = col;
}
else {
first = GMT_IN; last = GMT_OUT;
c = arg;
if (q) def_col[GMT_IN] = def_col[GMT_OUT] = col;
}

for (dir = first; dir <= last; dir++) {
GMT->common.d.active[dir] = true;
GMT->common.d.nan_proxy[dir] = atof (c);
/* Need to know if 0 is used as NaN proxy since we must use a different comparison macro later */
GMT->common.d.is_zero[dir] = doubleAlmostEqualZero (0.0, GMT->common.d.nan_proxy[dir]);
GMT->common.d.first_col[dir] = def_col[dir];
}
if (q) q[0] = '+'; /* Restore modifier */
if (first == GMT_IN) strncpy (GMT->common.d.string, arg, GMT_LEN64-1); /* Verbatim copy */
return (GMT_NOERROR);
}
Expand Down
8 changes: 4 additions & 4 deletions src/gmt_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -3661,8 +3661,7 @@ GMT_LOCAL void *gmtio_ascii_input (struct GMT_CTRL *GMT, FILE *fp, uint64_t *n,
else /* Default column order */
col_pos = col_no;
n_convert = gmt_scanf (GMT, token, gmt_M_type (GMT, GMT_IN, col_pos), &val);
//n_convert = gmt_scanf (GMT, token, gmt_M_type (GMT, GMT_IN, in_col), &val);
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 */
if (n_convert != GMT_IS_NAN && gmt_input_is_nan_proxy (GMT, val, col_pos)) /* Input matched no-data setting, so change to NaN */
n_convert = GMT_IS_NAN;
if (n_convert == GMT_IS_NAN) { /* Got a NaN or it failed to decode the string */
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 */
Expand Down Expand Up @@ -4322,8 +4321,9 @@ void gmt_set_cartesian (struct GMT_CTRL *GMT, unsigned int dir) {
/*! Handles non-proxy checking for input z values. If the input value equals
* the non_proxy then we return true so the value can be replaced by a NaN.
*/
bool gmt_input_is_nan_proxy (struct GMT_CTRL *GMT, double value) {
bool gmt_input_is_nan_proxy (struct GMT_CTRL *GMT, double value, unsigned int col) {
if (!GMT->common.d.active[GMT_IN]) return false; /* Not active */
if (col < GMT->common.d.first_col[GMT_IN]) return false; /* Not in column range */

if (GMT->common.d.is_zero[GMT_IN]) return doubleAlmostEqualZero (0.0, value); /* Change to NaN if proxy value is ~zero */
return doubleAlmostEqual (GMT->common.d.nan_proxy[GMT_IN], value); /* Change to NaN if value ~nan_proxy for a non-zero proxy */
Expand Down Expand Up @@ -4613,7 +4613,7 @@ int gmtlib_process_binary_input (struct GMT_CTRL *GMT, uint64_t n_read) {
/* Determine if this was a segment header, and if so return */
for (col_no = n_NaN = 0; col_no < n_read; col_no++) {
if (!gmt_M_is_dnan (GMT->current.io.curr_rec[col_no])) { /* Clean data */
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 */
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 */
GMT->current.io.curr_rec[col_no] = GMT->session.d_NaN;
else if (GMT->common.i.select) /* Cannot check here, done in gmtio_bin_colselect instead when order is set */
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/gmt_prototypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ EXTERN_MSC int gmt_remove_file (struct GMT_CTRL *GMT, const char *file);
EXTERN_MSC int gmt_rename_file (struct GMT_CTRL *GMT, const char *oldfile, const char *newfile, unsigned int mode);
EXTERN_MSC void gmt_format_abstime_output (struct GMT_CTRL *GMT, double dt, char *text);
EXTERN_MSC int gmt_ascii_output_col (struct GMT_CTRL *GMT, FILE *fp, double x, uint64_t col);
EXTERN_MSC bool gmt_input_is_nan_proxy (struct GMT_CTRL *GMT, double value);
EXTERN_MSC bool gmt_input_is_nan_proxy (struct GMT_CTRL *GMT, double value, unsigned int col);
EXTERN_MSC bool gmt_is_a_blank_line (char *line);
EXTERN_MSC void gmt_set_geographic (struct GMT_CTRL *GMT, unsigned int dir);
EXTERN_MSC void gmt_set_cartesian (struct GMT_CTRL *GMT, unsigned int dir);
Expand Down
6 changes: 3 additions & 3 deletions src/gmt_synopsis.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@

#define GMT_bi_OPT "-bi<record>[+b|l]"
#define GMT_bo_OPT "-bo<record>[+b|l]"
#define GMT_di_OPT "-di<nodata>"
#define GMT_do_OPT "-do<nodata>"
#define GMT_di_OPT "-di<nodata>[+c<col>]"
#define GMT_do_OPT "-do<nodata>[+c<col>]"
#define GMT_ho_OPT "-ho[<nrecs>][+c][+d][+m<segheader>][+r<remark>][+t<title>]"
#define GMT_qi_OPT "-qi[~]<rows>|<limits>[,...][+c<col>][+a|f|s]"
#define GMT_qo_OPT "-qo[~]<rows>|<limits>[,...][+c<col>][+a|f|s]"
Expand Down Expand Up @@ -121,7 +121,7 @@
#define GMT_a_OPT "-a[[<col>=]<name>[,...]]"
#define GMT_b_OPT "-b[i|o][<ncols>][<type>][w][+l|b]"
#define GMT_c0_OPT "-c[<row>,<col>|<index>]"
#define GMT_d_OPT "-d[i|o]<nodata>"
#define GMT_d_OPT "-d[i|o]<nodata>[+c<col>]"
#define GMT_e_OPT "-e[~]<pattern>|/<regexp>/[i]|+f<file>"
#define GMT_f_OPT "-f[i|o]<colinfo>"
#define GMT_g_OPT "-gx|y|z|d|X|Y|D<gap>[<unit>][+a][+c<col>][+n|p]"
Expand Down
6 changes: 3 additions & 3 deletions src/grd2xyz.c
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ EXTERN_MSC int GMT_grd2xyz (void *V_API, int mode, void *args) {
if ((io.x_missing && io.gmt_i == io.x_period) || (io.y_missing && io.gmt_j == 0)) continue;
if (GMT->common.d.active[GMT_OUT] && gmt_M_is_dnan (d_value)) /* Grid node is NaN and -d was set, so change to nan-proxy */
d_value = GMT->common.d.nan_proxy[GMT_OUT];
else if (gmt_input_is_nan_proxy (GMT, d_value)) /* The inverse: Grid node is nan-proxy and -di was set, so change to NaN */
else if (gmt_input_is_nan_proxy (GMT, d_value, GMT_X)) /* The inverse: Grid node is nan-proxy and -di was set, so change to NaN */
d_value = GMT->session.d_NaN;
write_error = GMT_Put_Record (API, GMT_WRITE_DATA, Out);
if (write_error == GMT_NOTSET) n_suppressed++; /* Bad value caught by -s[r] */
Expand Down Expand Up @@ -774,14 +774,14 @@ EXTERN_MSC int GMT_grd2xyz (void *V_API, int mode, void *args) {
out[GMT_Y] = G->data[ij];
if (GMT->common.d.active[GMT_OUT] && gmt_M_is_dnan (out[GMT_Y])) /* Input matched no-data setting, so change to NaN */
out[GMT_Y] = GMT->common.d.nan_proxy[GMT_OUT];
else if (gmt_input_is_nan_proxy (GMT, out[GMT_Y]))
else if (gmt_input_is_nan_proxy (GMT, out[GMT_Y], GMT_Y))
out[GMT_Y] = GMT->session.d_NaN;
}
else {
out[GMT_X] = x[col]; out[GMT_Y] = y[row]; out[GMT_Z] = G->data[ij];
if (GMT->common.d.active[GMT_OUT] && gmt_M_is_dnan (out[GMT_Z])) /* Input matched no-data setting, so change to NaN */
out[GMT_Z] = GMT->common.d.nan_proxy[GMT_OUT];
else if (gmt_input_is_nan_proxy (GMT, out[GMT_Z]))
else if (gmt_input_is_nan_proxy (GMT, out[GMT_Z], GMT_Z))
out[GMT_Z] = GMT->session.d_NaN;
}
if (Ctrl->W.area) out[w_col] = W->data[ij] * A_scale; /* Converts area from km^2 to user-selected unit (if active) */
Expand Down
2 changes: 1 addition & 1 deletion src/xyz2grd.c
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ EXTERN_MSC int GMT_xyz2grd (void *V_API, int mode, void *args) {
Return (GMT_RUNTIME_ERROR);
}
ij_gmt = io.get_gmt_ij (&io, Grid, ij); /* Convert input order to output node (with padding) as per -Z */
Grid->data[ij_gmt] = (gmt_input_is_nan_proxy (GMT, in[zcol])) ? GMT->session.f_NaN : (gmt_grdfloat)in[zcol];
Grid->data[ij_gmt] = (gmt_input_is_nan_proxy (GMT, in[zcol], zcol)) ? GMT->session.f_NaN : (gmt_grdfloat)in[zcol];
ij++;
}
else { /* Get x, y, z */
Expand Down