Using Python to calculate northern hemisphere’s surface land coverage

Yesterday during my lunch break I was rather bored; it is unseasonably cold for the fall, even in Calgary, and a bit foggy too.
For something to do I browsed the Earth Science beta on Stack Exchange looking for interesting questions (as an aside, I encourage readers to look at the unanswered questions).
There was one that piqued my curiosity, “In the northern hemisphere only, what percentage of the surface is land?”.
It occurred to me that I could get together an answer using an equal area projection map and a few lines of Python code; and indeed in 15 minutes I whipped-up this workflow:

  • Invert and import this B/W image of equal area projection (Peters) for the Northern hemisphere (land = white pixels).

Source of original image (full globe): Wikimedia Commons

  • Store the image as a Numpy array.
  • Calculate the total number of pixels in the image array (black + white).
  • Calculate the total number of white pixels (1s) by summing the entire array. Black pixels (0s) will not contribute.
  • Calculate percentage of white pixels.

The result I got is 40.44%. Here’s the code:

# import libraries
import numpy as np
from skimage import io
from matplotlib import pyplot as plt

# import image
url = ''
north_equal_area = io.imread(url, as_grey=True)

# check the image
fig = plt.figure(figsize=(20, 10))
ax = fig.add_subplot(1, 1, 1)
plt.imshow(north_equal_area, cmap = 'gray');

# Do the calculations
r, c = np.shape(north_equal_area)
sz =  r*c
s = np.sum(north_equal_area)
print(np.round(s/sz*100, decimals=2))
>>> 40.44

As suggested in a comment to my initial answer, I run the same Python script for the entire globe and got the expected 30% land coverage:

# import image 
url = ''
equal_area = io.imread(url1, as_grey=True)

# Do the calculations 
r1, c1= np.shape(equal_area)
sz1 =  r1*c1
s1 = np.sum(equal_area)
print(np.round(s1/sz1*100, decimals=2))
>>> 30.08


Machine learning in Planetary Science: compressing Pluto images with scikit-learn and PCA

In a previous post I showed some of the beautiful new images of Pluto from New Horizon’s mission,  coloured using the new Matplotlib perceptual colormaps:


More recently I was experimenting with Principal Component Analysis in scikit-learn, and one of the things I used it for was compression of some of these Pluto images. Below is an example of the first two components from the False Color Pluto image:


You can take a look at the Python code available on this Jupyter Notebook. There are of course better ways of compressing images, but this was a fun way to play around with PCA.

In a follow-up post I will use image registration and image processing techniques to reproduce from the raw channels NASA’s Psychedelic Pluto Image.



Python, Einstein, and the discovery of Gravitational Waves at PyCon Italia


At 11:53 am of September 14 2015, Marco Drago, an Italian postdoctoral scientist at the Max Planck Institute for Gravitational Physics (AKA Albert Einstein Institute) in Hannover, Germany, was the first person to see it: the sophisticated instruments at the Laser Interferometer Gravitational-Wave Observatory (LIGO) had detected a very likely candidate signal from gravitational waves. The signal, caused by the collision and merger of two massive black holes, proved to be real; the discovery, announced to the public a few months later, demonstrated one of Albert Einstein’s predictions exactly 100 years after he formulated his general theory of relativity. How exciting!

I was aware that Python was used in both LIGO’s control room and for some of the scientific work and data analysis, through a Python LIGO thread on Reddit. Also, one of the main figures in the discovery paper was made entirely in Python, and used a Matplotlib perceptual colormap (Viridis, the new default in mpl 2.0).

It was not, however, until I decided to attend (virtually, on Youtube) this year’s PyCon Italia that I realized how big a role Python had played. In this post, I will briefly summarize what Franco Carbognani, keynote speaker for day 2 (Python and the dawn of gravitational-wave astronomy), and Tito dal Canton (Python’s role in the detection of gravitational waves) presented on the science and technology of gravitational waves detection, and on Python’s contributions.

The science

Most of us have studied that gravity produces a curvature in spacetime. A less generally known, but major prediction of general relativity is that a perturbation of spacetime produces gravitational waves, which are detected because they stretch and squeeze space producing a measurable strain; this however requires a violent cosmological phenomenon involving large masses, relativistic speeds (approaching the speed of light), and asymmetrical acceleration (an abrupt change in those speeds, such as in a collision).

One such event is that which generated the signal detected in September of last year. It occurred about 1300 million years ago when two large black holes spiralled towards one another and then merged into a single, stationary black hole, losing energy by way of radiating gravitational waves (ripples in space-time) exactly as predicted by Einstein; the whole process took only a few tenths of a second, but involved a total of 65 solar masses (36 + 29), 3 of which were converted, mostly in the final instant of the merger, to gravitational wave energy according to the famous relationship E = mc2. This was so much energy that, had it been  visible (electromagnetic), it would’ve been 50 times brighter than the entire universe.  Yet, even a huge event like that created very small waves (gravity is the weakest of the four fundamental interactions of nature), which merely displaced a pair of free-falling masses placed 3 km apart by a length 10,000 smaller than the diameter of a proton. Measuring this displacement requires very complex arrays of laser beams, mirrors, and detectors measuring interference (interferometers), such as the 2 LIGO in the United States, and VIRGO in Italy.

This video illustrates in detail how VIRGO (and LIGO) is able to detects the very weak  waves; it was produced by Marco Kraan of the National Institute for Subatomic Physics in Amsterdam, and shown during Carbognani’s talk:

The figure below shows an illustration of the event’s 3 main phases and the corresponding, matched signals from the 2 LIGO interferometers.

LIGO detects gravitational waves from merging black holes. Illustration credit: LIGO, NSF, Aurore Simonnet (Sonoma State University).

Python’s many contributions

Franco Carbognani is VIRGO integration manager with the European Gravitational Observatory in Pisa. The portion of his talk focusing on Python and VIRGO’s control systems starts here. He told the audience that where Python played a major role was in the building of a complete automation layer on top of real-time interferometer control, with analysis of data online and warning to the operator in case of anomalies, and also in GUI development and unification. Python was the obvious choice for these tasks because it is a compact, clear language, easy for beginners to learn, and yet allowing very complex programming (functional, object oriented, imperative, exceptions, etc.); its Numpy and Scipy libraries allow handling of the complex math required, without sacrificing speed (thanks to the optimized Fortran and C under the hood); a large collection of other libraries allows for almost any task to be carried out, including (as mentioned above) Matplotlib for publication quality graphs; finally, it is open-source, and many in the community already used it (commissioning, computing, and data analytics groups).

Tito dal Canton is a postdoctoral scientist at the Max Planck Institute in Hannover. The data analysis part of his talk starts here. The workflow he outlined, involving the use of several separate pipelines, consists of retrieving the data, deciding which data can be analyzed, decide if it contains a signal, estimate its statistical significance, and estimating the signal parameters (e.g. masses of stars, spin velocity, and distance) by comparison with a model. A lot of this work is run entirely in Python using either the GWpy and PyCBC packages. For the keener reader, one of the Jupyter Notebooks on the LIGO Python tutorials page, replicates some of the signal processing and data analysis shown during his talk.

Additional resources

MIT-Caltech video on Gravitational Waves detection.

New Horizons truecolor Pluto recolored in Viridis and Inferno

Oh, the new, perceptual MatplotLib colormaps…..

Here’s one stunning, recent Truecolor image of Pluto from the New Horizons mission:


Original image: The Rich Color Variations of Pluto. Credit: NASA/JHUAPL/SwRI. Click on the image to view the full feature on New Horizon’s site

Below, I recolored using two of the new colormaps:


Recolored images: I like Viridis, by it is Inferno that really brings to life this image, because of its wider hue and lightness range!


NASA’s beautiful ‘Planet On Fire’ images and video

Please give credit for this item to: NASA's Goddard Space Flight Center and NASA Center for Climate Simulation Australia photo courtesy of Flagstaffotos

Credits: NASA’s Goddard Space Flight Center and NASA Center for Climate Simulation.



Click on the image to watch the original video on NASA’s Visualization Explorer site.

Read the full story on NASA’s Visualization Explorer site.

Going for the Moon


While on a flight to Denmark a couple of years ago I happened to read this interview with Swedish astronaut Christer Fuglesang. Towards the end he talks about feasibility (and proximity) of our future missions to the Moon. This subject always gest me excited. If there’s one thing I’m dying to see is a manned missions to the Moon and Mars in my lifetime (I was born in 1971, so I missed by a split hair the first Moon Landing).

I hope we do it soon Christer!

Why go back?

My personal take is that of the dreamer, the 12 years old: why go back to the Moon? Because it’s there…. I mean look at it (photo credits: my uncle, Andrea Niccoli)!!


Beautiful Moon. In evidence the Mare Crisium, and craters Cleomedes, Langrenus, and Vendelinus.

Red Moon

Beautiful red Moon.

Copernicus - Appennines

Beautiful Moon. In evidence the Sinus Iridum in the top left, the Copernicus crater in the centre of the image, and the Apenninus Montes just North of it with the Eratosthenes crater.

Gassendi - Tycho

Beautiful Moon. In evidence the Gassendi crater in the centre of the image, and the Tycho crater to the right with one of the Rays.

On a more serious note, this is what Lunar scientist Paul Spudis has to say about why we should go back:


Moon exploration resources, and educational and vintage material

Rift valleys rewrite moon’s fiery history


Moon gravity Grail

NASA’s LRO Creating Unprecedented Topographic Map of Moon

Moon composition mosaic Galileo

Recent geological activity Lunar Reconnaissance Orbiter

Fresh Crater

NASA’s Beyond Earth Solar System exploration program

We choose the Moon – Wonderful, interactive recreation of Apollo 11 Lunar Landing  – 40th Anniversary celebration

Raw Video: Restored Video of Apollo 11 Moonwalk

All Apollos’ Lunar Surface Journals

BBC – In video: When man went to the Moon

BBC –Aldrin: I was relieved to be second

Fun / idiotic stuff

Space 1999 (cheesy, but unforgotten ’70s TV show, full pilot) – Moon breaks away from Earth.

Dumb and dumber  – We landed on the moon – pure Jim Carey’s genius!!!.

Beautiful Geology from space

In my post Our Earth truly is art I talked about Earth as Art, NASA’s  e-book collection of wonderful satellite images of our planet, and posted my top 3 picks.

In NASA’s Perpetual Ocean animation I talk about a beautiful convergence of maps and art: The Turbulence of Van Gogh and the Labrador Shelf Current, and NASA’s Perpetual Ocean animation.

Here’s another gem: Van Gogh from Space Landsat 7 Acquired 7/13/2005, winner of NASA’s public contest to select the Top Five ‘Earth as Art’ Winners


NASA’s Perpetual Ocean animation

A couple of months ago AGU blogger Dan Satterfield posted a great article titled The Turbulence of Van Gogh and the Labrador Shelf Current.

For getting maps and art together, I thought it could not be topped. Then today, I stumbled into NASA’s Perpetual Ocean animation: beautiful!

From the original Source: This is an animation of ocean surface currents from June 2005 to December 2007 from NASA satellites. Watch how bigger currents like the Gulf Stream in the Atlantic Ocean and the Kuroshio in the Pacific carry warm waters across thousands of miles at speeds greater than four miles per hour (six kilometers per hour); how coastal currents like the Agulhas in the Southern Hemisphere move equatorial waters toward Earth’s poles; and how thousands of other ocean currents are confined to particular regions and form slow-moving, circular pools called eddies. Credit: NASA/SVS

Related sites

More media options, including a 20 minute version at 30 fps can be found here

MIT general circulation model MITgcm

ECCO2: Phase II of MIT/JPL project Estimating the Circulation and Climate of the Ocean