Emacs in scientific computing

Part 5 of my series of Scientific Computing underdogs!

GNU emacs is widely know as a powerful text editor for, e.g., programming. Its indentation and syntax highlighting are excellent. However, I realized I have been using it for a while to deal with raw data – in this regard one could call this program an underdog, I guess (or whatever, I just want to keep it with the rest of the SciCompUD series). The feature I use the most is “keyboard macros”, instant macros that are easy to define and use.

For example, the other day I was given a xmgrace file, with a set of 4 graphs for the same quantity. Same x values, different ys. From them I had to produce a single graph showing, for each x, a data point corresponding to the statistical average of the several ys. Also, an error bar equal to the standard deviation. This is what I did:

  • From xmgrace, I joined all the sets, then ordered the result (increasing x).
  • Saved the result in file tmp.dat, which now has rows x1 y1 // x1 y2 // x1 y3 … on different lines. Too bad, if it was the same line we could use awk to do the trick.
  • emacs to the rescue! Edit the file, then define a macro: ctrl+x (, stuff you want to repeat, ctrl+x ) . In this case, something like “go to end of line, delete new line char to join next line, insert space … etc, three more times”. Try it, executing it with ctrl+x e. If the result is right, do it many times with ctrl+x # e, where # is some number (99, 99999, whatever). Save the result when the whole file is processed.
  • I could then run this simple awk code (awk -f code.awk tmp.dat > mean_std.dat):

{ mean=$2+$4+$6+$8






print $1,mean,msd


  • Open mean_std.dat in xmgrace, as a “X Y DY” graph, ecco!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s