The Recipes for Earth Sciences Go Trilingual: MATLAB, Python and Julia

It’s been almost 30 years since I started with MATLAB, coming from FORTRAN77, and at a time when Python was still in its infancy and 20 years before Julia was developed. In the meantime, Python seems to be more popular than MATLAB in the geosciences, but may soon be replaced by Julia – time for change, also for me!

I’ve been learning Python for a few weeks now, with the goal of writing a Python version of my MATLAB Recipes for Earth Sciences textbook. No, don’t worry, I will probably never use Python seriously in my research. I appreciate the benefits of MATLAB all too much for that: MATLAB and >90 tested toolboxes from one source, a professional support with a 24 hour response time instead of googling and browsing a forum such as Stackoverflow, a warranty and certified code on request. My ~30 year old code still runs flawlessly, as I have shown elsewhere, while on Twitter people discuss whether 3 year old Python still runs – unbelievable.

However, I recognize the popularity of Python, including the growing prevalence of Julia, and find that a bilingual and trilingual version of my books and my courses could be beneficial, not only because of the expected higher sales. Now, my experience in the courses shows that people get away from a particular software or programming language and focus instead on the method they want to implement. This is an advantage, also helps a lot to produce sustainable and reproducible code.

This is the first chapter of this experiment, and I want to push a Python version first and foremost. I will take care of Julia later, but will not lose sight of it. The first step is to set up a working environment for all three languages, although I would like to point out that for Python and Julia I prefer an environment similar to the MATLAB desktop: an editor, a command window and a graphics window.

MATLAB

There are multiple ways to use MATLAB: in a text terminal / console, using MATLAB Desktop either with the Classic Editor or the Live Editor, and with MATLAB Online in a browser; there are also tools to use MATLAB remotely on a smartphone or tablet computer with MATLAB Mobile.

After having created a MathWorks account, e.g. with your university email address if your university has a total academic headcount license. This is great as you then can download and install MATLAB and Simulink, together with ~90 toolboxes (for Python and Julia users: packages) that you do not have to import before running a script. The documentation for MATLAB, Simulink and the toolboxes are all in one place and can be view with the help browser.

You can then launch MATLAB in a text terminal / console using ./matlab -nojvm on a computer running macOS and similar commands on other operating systems. Alternatively, and that’s what most people prefer, you can launch the software by double clicking the MATLAB shortcut icon. Then the MATLAB desktop comes up with a Command Window, Workspace Browser and so forth. You should then change the current working directory permanently using the Preferences > MATLAB > General > Initial working folder and use Set Path in the HOME tab of the toolbar to add further directories to your search path.

  MATLAB Desktop with the Classic Editor

MATLAB Desktop with the Live  Editor

PYTHON

You can either download and install Python 3 from the Python website and run it in a terminal /console or – recommended – install the free Anaconda Individual Edition. You will, however, meet other Python users making other recommendations. After announcing the blog post on Twitter, Saul Arciniega (@zaul_arciniega) recommended WinPython which provides an easy way to run Python, Spyder with SciPy and friends out of the box on any Windows PC, without installing anything, with a MATLAB-style desktop. Anaconda is a business, providing the best Python distribution and professional support, but it is expensive starting at rates of 10,000 USD. However, the Anaconda Individual Edition is free and good for most of you. It includes Python and other software, but also Spyder which provides a MATLAB-style desktop for Python.

Another advantage of Anaconda is that it includes a large number of the most popular packages (for MATLAB users: toolboxes) and takes care of versions. Instead, but then you have to install and manage the packages since it contains only a very small number of packages, you can install Spyder without Anaconda. Having Python included as part of the macOS operating system, I was running in to problems with different versions and locations were Python and the packages where installed and therefore I recommend to use the Anaconda Individual Edition without trying to install Python or Spyder separate from this distribution.

In Spyder, you can also change the working directory permanently, similar to MATLAB, in the Preferences > Startup. You can use the menu Tools > PYTHONPATH Manager to add a directory to the search path of Python. Since the packages are not an integral part of Python, but are maintained by independent development teams, they must be imported at the beginning of a Python script. If they are not included in the Anaconda distribution, they must be installed later, for which there are instructions on the corresponding package websites.

Spyder desktop for Python

JULIA

You can install Julia from its website and run Julia, similar to MATLAB and Python, in a text terminal / console. I prefer, however, running Julia in a MATLAB-style desktop in Visual Studio (VS) Code that you download for free from the Microsoft website. Within VS Code you can install Julia as an extension, browsing the list of optional plugins/extensions/languages. Alternatively JuliaUp by David Anthoff and others provides a good way of keeping an up-to-date installation in Windows, as recommended by Argel Ramírez Reyes (@aramirezreyes) on Twitter. Together with an MSIX installer it can be downloaded from the Windows Store. Another option is JuliaWin like WinPython for Windows users by GitHub user heetbeet, recommended by Royi Avital in an email message to me.

You can change the working directory by using cd and use the menu File > Add Folder to Workspace to add a directory to the search path of Julia. As with Python, the packages are not an integral part of Julia, but are maintained by independent development teams, they must be loaded at the beginning of a Julia script. Somewhat unusually, and unlike MATLAB and Python, after launching Julia it starts loading and precompiling its components when trying to run code for the first time, and then it resolves package versions every time when this code is run again. This has nothing to do with the often praised computing speed of Julia compared to MATLAB and Python, but it slows down working with Julia somewhat, at least in VS Code.

Julia in Visual Studio Code