Hi all,
I wanted to try my hand at doing some calculations on csv files using gmt math and I discovered I couldn’t even add two columns together. Naturally I came here to ask the most basic of questions: How do I add column 1 to column 2?
I’ve tried the following:
gmt begin
sws=distances_Y.csv
gmt math $sws -C0 $sws -C1 ADD = solution2.txt
gmt end
This didn’t add column 1 to column 2, instead it pasted the columns one next to another. I can add a column to itself by selecting -C1 twice for example, but I’m trying to do column 1 plus column 2, write result in new column (3)
I remember a couple of questions similar to this and I think Paul Wessel always said that, no, gmtmath doesn’t do column operations. I personally find gmtmath so complicated when now we have other ways of doing most of what it does but much easier.
I would just use awk.
awk -F, '{print $1, $2, $1+$2}' distances_Y.csv > solution2.txt
That should put columns 1 and 2 and then add them in the 3rd column
Here is how you can almost do it (but not exactly as you want), though personally I’d use the awk
solution posted by @EvanPi
Suppose your data is in a file with two columns, called data.txt
:
1 3
3 9
4 12
2 6
You want to add column 0 (first column) and 1 (second column):
gmt math data.txt 0 COL ADD = twocolsum.txt
The file twocolsum.txt
looks like this:
1 4
3 12
4 16
2 8
You can see the sum of the first two columns of data.txt
are now in the second column of twocolsum.txt
. Here is what happened with the gmt math
command:
- Use
data.txt
as the tabular data: gmt math data.txt
- Take the first (column 0) column and add it to the stack:
0 COL
- Add the contents of the stack to the tabular data:
ADD
Note that this addition is to the second and subsequent columns, gmt math
treats the first column as a special time column.
- Write the results to
twocolsum.txt
: = twocolsum.txt
Reverse Polish notation forces one to rewire one’s mind after having been taught BEDMAS/BODMAS or whatever for years. However, when one gets the hang of it, it can be quite powerful. Though for your application of adding two columns, honestly use awk
or similar. @Joaquim is an advocate for Julia language, which is very nice, but would be overkill for this task.
Edit: A friend at university used to always espouse the virtues of RPN. At the time I thought it stood for “randomly processes numbers”. But now, many years later, I’ve got an old HP 11C calculator in a drawer at home. Confession - it stays in the drawer, and I use a bog standard Casio when I still want to use a calculator.
Here’s how to do what you want exactly as you want.
gmt math -N3 data.txt -C2 0 COL ADD 1 COL ADD = foo.txt
This works because -N3 specifies three columns. Since the input data only had two columns, the third will start life as a column of zeros. Then the following RPN commands add the values of columns 0 and 1 to the last column.
Agree. And that is why I was vague when said “we have other ways”. Awk was one the options I had in mind. And the other was ofc GMT.jl. Not for this in particular, but for everything GMT related.
awk supposedly dates back to 1977. I’ve always thought it was under-appreciated - perl became popular, and then languages like Python. But for many things awk is all that is needed.
A long time ago (in the1990s some time) I wrote a program in awk which converted tables of pressure, temperature and dew-point temperature (the temperature at which dew forms if air is cooled - a sort of humidity measure) into tephigrams: Tephigram - Wikipedia (which were of course plotted using our favourite plotting software!) The charts are still on my old university’s website:
1 Like
Tim, thank you for this reply (which works) but also your previous reply which walks me through the logic of COL. I’m ok with RPN (not super comfortable, but I understand it enough to appreciate its advantages over infix notation). I think my stumbling block was not knowing the COL function (I was too focused on -C). The other thing was your clever use of -N3 to create a fresh column.
Thanks Evan, I’ve checked this and it works. The reason I was asking about gmt math in particular is because I was trying to practice using that module a bit. But I appreciate you showing me an alternative approach, I don’t want you to think I’m being ungrateful.