interactive HALO tracks#

This notebook shows how to plot the flight tracks of the HALO aircraft. First, we’ll have only a quick look and later on we do some more work in order to get the data onto an interactive map.


First, we’ll import pylab and the EUREC4A data catalog.

import eurec4a
cat = eurec4a.get_intake_catalog(use_ipfs="QmahMN2wgPauHYkkiTGoG2TpPBmj3p5FoYJAq9uE9iXT9N")

Inspecting the dataset#

ds = cat.HALO.BAHAMAS.PositionAttitude['HALO-0122'].to_dask()

Just to have a better visual impression, we can create a quick overview plot of the data:

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt"./mplstyle/book")

center_lat = float(
plt.xlabel("longitude / °")
plt.ylabel("latitude / °")

Reducing the size of the dataset#

Later on, we want to plot all flights on an interactive map. Currently the dataset is rather large as the aircraft location has been recorded continuously at a high data rate. While this is good for quantitative analysis, this leads to poor interactive performance. So before going further, it is a good idea to reduce the amount of data while keeping the visual impression.

A possible idea to reduce the amount of required data is that plotting a line already does linear interpolation between two coordinate points. So if we would remove those points which are close to the linear interpolation between its neighboring points, the visual impression will stay almost the same. This idea has already been stated by Ramer Douglas and Peucker and is illustrated at Wikipedia. While the algorithm is not hard to write, it is difficult to do it efficiently in python. Thus, I’ve skipped it and use the simplification library in stead.


Many algorithms for shape processing are contained in the beautiful shapely library. In general, I’d recommend using that library, but it requies the GEOS library which is a bit tricky to install.

from simplification.cutil import simplify_coords_idx
def simplify_dataset(ds, tolerance):
    indices_to_take = simplify_coords_idx(np.stack([, ds.lon.values], axis=1), tolerance)
    return ds.isel(time=indices_to_take)

We can now use that algorithm to generate a simplified version of the dataset with a tolerance of \(10^{-5}\) degrees, which otherwise looks the same to the previous version.

dssimplified = simplify_dataset(ds, 1e-5)

We can now compare those two tracks side by side while keeping a look at the number of data points required.

fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.set_title(f"{len(ds.time)} data points")
ax2.set_title(f"{len(dssimplified.time)} data points")

for ax in (ax1, ax2):
    ax.set_xlabel("longitude / °")
    ax.set_ylabel("latitude / °")

ratio = len(dssimplified.time) / len(ds.time)
print(f"compression ratio: {ratio*100:.2f} %")

The dataset size has been substantially reduced while the visual impression stayed the same.

First interactive map#

In order to show the map a little bit more interactively, we use the ipyleaflet library which creates a bridge between ipython and the Leaflet JavaScript library.

import ipyleaflet

As we will need to convert many tracks to ipyleaflet layers later on, the easiest is to create a little function for that purpose right away:

def track2layer(track, color="green", name=""):
    return ipyleaflet.Polyline(
        locations=np.stack([, track.lon.values], axis=1).tolist(), 

With the help of that little function, creating a map is now like a breeze:

testmap = ipyleaflet.Map(center=(13.3, -57), zoom=7)

All in one#

Let’s see if we can add all the flights and provide a layer switcher so that we can have a look at all flights individually. We’ll start by loading and simplifying all out track data into a local dictionary. Requesting the datasets from the server is IO bound and thus can simply be accellerated using a ThreadPool:

from multiprocessing.pool import ThreadPool
pool = ThreadPool(20)

def get_dataset(flight_id):
    ds = cat.HALO.BAHAMAS.PositionAttitude[flight_id].to_dask()
    return flight_id, ds.load()

full_tracks = dict(, cat.HALO.BAHAMAS.PositionAttitude))

We still have to simplify the dataset, which is done here:

tracks = {flight_id: simplify_dataset(ds, 1e-5)
          for flight_id, ds in full_tracks.items()}

Let’s also quickly grab some colors from a matplotlib colorbar, such that we can show all tracks in individual colors:

import matplotlib
colors = [matplotlib.colors.to_hex(c)
          for c in, 1, len(tracks)))]

We can now start with a new empty map. Let’s also have a different basemap.

m = ipyleaflet.Map(
    center=(13.3, -57), zoom=7

We’ll add all the tracks as individual layers to the map

for (flight_id, track), color in zip(tracks.items(), colors):
    m.add_layer(track2layer(track, color, flight_id))

and add a scale, a legend, layer controls and a full screen button to the map and show it. If you want to zoom in, you can for example shift-click and drag a rectangle over the area you want to zoom in more closely.

m.add_control(ipyleaflet.LegendControl(dict(zip(tracks, colors)),