Appending a new column in gmtmath

I think I once upon a time asked this before, but I don’t remember the answer. RPN is probably playing with my head!

I’m trying to figure out a way to take a values from certain columns of a table, do something to them, and write the result out as an appended column. Here’s an example, that doesn’t do what I’m expecting (because I forgot something in the command line, I’m sure!):

echo “1 2 3 4” | gmtmath -i0-3 -o0-4 STDIN -C1 3 COL ADD =

this results in (placing the answer in column 1):

1.0000000000 6.0000000000 3.0000000000 4.0000000000 NaN

Another try:

echo “1 2 3 4” | gmtmath -i0-3 -o0-4 STDIN -C4 3 COL 1 COL ADD -Ca =
1.0000000000 0.0000000000 0.0000000000 0.0000000000 NaN

What I’m trying to do is create column 4 (in GMT columns) that consists of the sum of columns 1 and 3. Instead it does something else. In awk, it would go like this:

echo “1 2 3 4” | awk ’ { print $0, $2 + $4 } ', giving the answer expected (in awk, col nums start at 1):
1 2 3 4 6

The reason I’m asking this, is that I want to do something like this with a huge binary file, otherwise, I’d just use awk! THanks!

This is the most confusing part of gmtmath since it was never set up to do stuff between columns. But this works

echo 1 2 3 4 | gmt math -N5 STDIN -C4 3 COL ADD 1 COL ADD =

1 Like

Thanks, Paul. I’m always pushing the envelope. And I see that little comment at the end of the -N option description: “… then -N will add any missing columns.” Super!

I have a minimalist -Q question. No STDIN involved.

How can I get

$ gmt math -Q 1 7 =
7
gmtmath [WARNING]: 1 more operands left on the stack!

to instead give

$ gmt math -Q 1 STACKTOCOL 7 =
1      7

Above I have used STACKTOCOL, which does the opposite of

COL: Places column A on the stack

The only problem is STACKTOCOL only exists in my brain. Nobody has
invented it yet. Isn’t it odd that there is no opposite function of COL?

By the way, in the documentation,

$ gmt math 2>&1 | grep -B 1 Shorthand
  -Q[c|i|p|n]
     Quick scalar calculator (Shorthand for -Ca -N1/0 -T0/0/1).

$ gmt math -Q 1 1 ADD =
2
$ gmt math -Ca -N1/0 -T0/0/1 1 1 ADD =
gmtmath [ERROR]: Option T: min >= max

Thus “Shorthand for …” is wrong.

It should say instead

Quick scalar calculator (Shorthand for -Ca -N1/0, and disables -T requirement.)

(One might say for my above question, I need to forget about -Q, and
employ -T0/.1/1 hack, as -T0/0/1 sets off alarm bells.)

The -Q option provides a simple means to make a quick scalar calculation. Scalars are, by definition, single values, they can’t be considered a column (or a vector of values), per se.

The expression, gmt math -Q 1 1 ADD = is equivalent to echo "1 1" | awk ' { print $1 + $2 } '.

The COL operator is only applicable when working with columnar data. It isn’t applicable when using the -Q option for scalars. So, no “opposite function,” like STACKTOCOL, meaningfully exists.

Perhaps this does what is needed, in the first example (but any real purpose for doing this is elusive):

~ > echo "1 7" | awk ' { print $1, $2 } '
1 7

Finally, the -T option is only useful for creating a columnar sequence of values.

However, it may be better to adopt the suggested shorthand phrasing. The -Q option certainly does negate the need for the -T option.

Also, the docs for gmtmath is way behind the actual coding. Yes, internally the -Q sets the equivalent of -Ca -N1/0 -T0/0/1 but there is never any parsing of a -T option - we just set those values directly. Now that we have checks on -T so max > min you get an error trying to run it that way. I will just change it to -T1 to avoid to have others get confused.

Now in master.

2 Likes

OK, I found it

Never mind about -Q. How about in general, if there is

COL: Places column A on the stack

shouldn’t there also be a “antidote” (some new name, XXX) operator to it?:
Whereas “COL XXX” would be equivalent to a NOOP (which by the way would be useful too, so that is why even proj.org implements it.)