Python wrappers for the Generic Mapping Tools on the way

Standard

An interface for interoperability between the Generic Mapping Tools (GMT), a tool used by geophysicists to create research-quality figures, and Matlab has recently been developed that allows GMT users to interact with Matlab and Matlab users to make use of GMT.

GMT wrappers are currently also being developed for the Python programming language, particularly to be used in the IPython/Jupyter notebook due to an initiative by Leonardo Uieda (and his professor Paul Wessel) whose Postdoc is being funded by the NSF. You can watch his talk at the SciPy 2017 conference below.

Some of the mentioned advantages to which I attest  include:

  • Begin and End statements are introduced to eliminate the need to pipe postscript results into a file in each line of code being written. This also eliminates the need to use the -K and -O flags which keep the file open and updates it, respectively. The -K and -O flags are a major confusion for newcomers to GMT.
  • temporary files are created under the /tmp directory, in Linux, so they will automatically be cleaned once the jupyter notebook is closed or the operating system is rebooted. Moreover, every project will have its own  directory so files from different projects don’t get mixed up.
  • GMT documentation straight in the Jupyter notebook
  • Matplotlib- & Basemap-like behaviour, particularly inline viewing of figures, using gmt.show()
  • Pythonic aliases make the compact GMT flags

To contribute: github.com/GenericMappingTools

Reference

Cook, T. (2017), A powerful new tool for research, Eos, 98, https://doi.org/10.1029/2017EO077489. Published on 17 July 2017.

Matlab – Symbolic & Function Handles

Standard

Consider you want to define a function in Matlab, plot it, and differentiate it. This can be done in two ways. Let’s demonstrate the two methods on the function

f(x) = x - 3 * log(x)

whose derivative is

f'(x) = 1 - \frac{3}{x}

The first is using function handles (ie; numerically) that take & return values as input & output. Function handles require the inputs to be initialized. Here’s an example:


x = linspace(0.5, 5.0)'; % range of x as column vector
f = x - 3 * log(x); % returns numerical values
df = diff(f) ./ diff(x); % also numerical values of the derivative

The other is symbolically (ie; like you do in your math class) as such


syms x % define symbolic variables
f = x - 3 * log(x); % symbolic function
df = diff(f, x); % gives the symbolic derivative

which returns

f = x - 3*log(x)
df = 1 - 3/x

But this way you cannot give the funtion numerical inputs and hence can’t plot it. To do so you’ll have to convert the function to a function handle which is easy using matlabFunction():


f_handle = matlabFunction(f); % convert symbolic fn to a handle
f_handle(2); % value of f @ x = 2
x = linspace(0.5, 5.0)'; % define range for x as column vector
plot(x, f_handle(x) ) % plot f on the range x

which return

f_handle = @(x)x-log(x).*3.0
df_handle = @(x)-3.0./x+1.0