# Appending a new column in gmtmath

**URL:** <https://forum.generic-mapping-tools.org/t/appending-a-new-column-in-gmtmath/220>\
**Category:** Q&A\
**Created:** [January 28, 2020, 9:58pm UTC](https://forum.generic-mapping-tools.org/t/appending-a-new-column-in-gmtmath/220 "2020-01-28T21:58:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Geodesy](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/john_geodesy/32/116_2.png) [@John\_Geodesy](https://forum.generic-mapping-tools.org/u/John_Geodesy)\
**Post date:** [January 28, 2020, 9:58pm UTC](https://forum.generic-mapping-tools.org/t/appending-a-new-column-in-gmtmath/220/1 "2020-01-28T21:58:16Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![pwessel](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/pwessel/32/30_2.png) [@pwessel](https://forum.generic-mapping-tools.org/u/pwessel)\
**Post date:** [January 28, 2020, 10:18pm UTC](https://forum.generic-mapping-tools.org/t/appending-a-new-column-in-gmtmath/220/2 "2020-01-28T22:18:04Z")

</div>

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 =**

---

<div class="post-metadata">

**Author:** ![John\_Geodesy](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/john_geodesy/32/116_2.png) [@John\_Geodesy](https://forum.generic-mapping-tools.org/u/John_Geodesy)\
**Post date:** [January 29, 2020, 5:01pm UTC](https://forum.generic-mapping-tools.org/t/appending-a-new-column-in-gmtmath/220/3 "2020-01-29T17:01:04Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![jidanni](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/jidanni/32/3164_2.png) [@jidanni](https://forum.generic-mapping-tools.org/u/jidanni)\
**Post date:** [July 12, 2023, 1:25pm UTC](https://forum.generic-mapping-tools.org/t/appending-a-new-column-in-gmtmath/220/4 "2023-07-12T13:25:49Z")

</div>

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

How can I get

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

```

to instead give

```auto
$ 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,

```auto
$ 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.)

---

<div class="post-metadata">

**Author:** ![John\_Geodesy](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/john_geodesy/32/116_2.png) [@John\_Geodesy](https://forum.generic-mapping-tools.org/u/John_Geodesy)\
**Post date:** [July 12, 2023, 3:19pm UTC](https://forum.generic-mapping-tools.org/t/appending-a-new-column-in-gmtmath/220/5 "2023-07-12T15:19:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pwessel](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/pwessel/32/30_2.png) [@pwessel](https://forum.generic-mapping-tools.org/u/pwessel)\
**Post date:** [July 12, 2023, 3:57pm UTC](https://forum.generic-mapping-tools.org/t/appending-a-new-column-in-gmtmath/220/6 "2023-07-12T15:57:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pwessel](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/pwessel/32/30_2.png) [@pwessel](https://forum.generic-mapping-tools.org/u/pwessel)\
**Post date:** [July 12, 2023, 3:59pm UTC](https://forum.generic-mapping-tools.org/t/appending-a-new-column-in-gmtmath/220/7 "2023-07-12T15:59:59Z")

</div>

Now in master.

---

<div class="post-metadata">

**Author:** ![jidanni](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/jidanni/32/3164_2.png) [@jidanni](https://forum.generic-mapping-tools.org/u/jidanni)\
**Post date:** [July 16, 2023, 11:55am UTC](https://forum.generic-mapping-tools.org/t/appending-a-new-column-in-gmtmath/220/8 "2023-07-16T11:55:41Z")

</div>

OK, I found it

> <https://github.com/GenericMappingTools/gmt/commit/1cd2561aa8c2992eb5976304cc1bdfd11d0a95c6>

---

<div class="post-metadata">

**Author:** ![jidanni](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/jidanni/32/3164_2.png) [@jidanni](https://forum.generic-mapping-tools.org/u/jidanni)\
**Post date:** [July 16, 2023, 12:04pm UTC](https://forum.generic-mapping-tools.org/t/appending-a-new-column-in-gmtmath/220/9 "2023-07-16T12:04:58Z")

</div>

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](https://proj.org/en/9.2/operations/conversions/noop.html).)
