The Joy of Fortran

Well, maybe “joy” is a strong word. But over the past couple of days, I’ve been programming in Fortran a bit for the first time in about two years. Fortran, for the uninitiated, is the oldest programming language still in widespread use today. It gets a lot of grief (much of it deserved, some not) for being a kind of blockheaded dinosaur language, written, so its critics would have you believe, for the sole use of crew-cut IBM engineers wearing pocket protectors and skinny blue ties. I do a lot of programming for my research, but most of it is using Python/SciPy and R. Both are much easier to write in than Fortran, and they are both wonderful for 99% of what I need to do.

But I hit a snag this week: I need to compute empirical semivariograms for a large dataset. This is a statistic that requires comparing every point in the dataset to every other point. SciPy and R do vector-based math very efficiently, but pairwise comparisons do not lend themselves to vector-based math. The number of calculations increases as the number of datapoints squared, and trying to do this kind of brute-force calculation in pure Python or R is just too damn slow.

Fortran to the rescue. It may be a blockheaded dinosaur, it may have been invented to calculate the trajectories of missiles launched at the Russkies, but it isn’t slow. I wrote the relevant section of the program in Fortran, and then, using a tool called f2py, packaged it as a Python module. The result: an empirical semivariogram, in about 1/70 th the time. This process is kind of a hassle—not least because it required struggling with f2py for two days to get it working properly. I wouldn’t want to write large programs in Fortran. But I can see myself using it to clear certain computational bottlenecks now and then.

It’s kind of cool to still be coding in the direct descendent of the first real programming language. There are many scientists out there still using Fortran, especially in number-crunchy fields like weather and climate modeling, both because of its speed, and because it has been the lingua franca of these models for decades. And there is a kind of stark beauty in Fortran code. I had a little moment last winter at the Museum of Science in Boston, where I found, in a corner of the permanent “computers” exhibit, a copy of the program that guided the Apollo lander down to the Moon. It was a stack of Fortran punch-cards in a glass case.

You’ve got to know your roots…

This entry was posted in Uncategorized and tagged , , , , , , . Bookmark the permalink.

2 Responses to The Joy of Fortran

  1. Ioan Ferencik says:

    Interesting topic. I had my time with Fortran last winter, trying to port a hydraulic model to python(bindings). Do you mind sharing the fpy source? I am currently working with LiDAR data and your post gave me ideas.
    Regards

  2. Having read this I believed it was very informative.
    I appreciate you finding the time and energy to put this informative article together.

    I once again find myself spending a significant amount of time both
    reading and commenting. But so what, it was still worthwhile!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>