Coupling ESA SNAP to Python on an EO-Lab VM

“Snappy” is the name of the Python interface to SNAP, which allows users to access the functionality of SNAP directly from Python code. Snappy provides a more powerful and flexible way to interact with SNAP than the graphical user interface (GUI) and enables automation of more complex processing workflows. The installation of a desktop environment is not required.

Prerequisites

No 1. Account

You need a EO-Lab hosting account with access to the Horizon interface: https://cloud.fra1-1.cloudferro.com/auth/login/?next=/. Choose “EO-Lab - SAML2” to login.

No 2. Virtual machine with Linux distribution and access to /codede repository

You need to operate a Linux distribution VM with access to the /codede repository. In this case we going to use Ubuntu 22.04 LTS and connect to it via ssh protocol (more about this here: How to connect to your virtual machine via SSH in Linux on EO-Lab)

The flavour eo2.2xlarge is sufficient for installing ESA SNAP (even with all possible toolboxes), but more advanced processing require stronger flavours (especially when it comes to RAM usage).

No 3. Successfully installed ESA SNAP on a Linux distribution VM

Installation of ESA SNAP on an EO-Lab VM

Creating Conda Virtual Environment running required Python version

Because SNAP supports only Python 2.7, 3.3 to 3.6, we will use a conda virtual env as an easy way for supplying an executable python version (3.5 in this case).

To install conda on linux please download the appropriate file from https://www.anaconda.com/products/distribution and follow instruction: https://docs.conda.io/projects/conda/en/latest/user-guide/install/linux.html or execute the following commands.

Commands below create virtual environment running python 3.5

Downloading and installing Anaconda

wget https://repo.anaconda.com/archive/Anaconda3-2022.10-Linux-x86_64.sh # download Anaconda installation file
chmod +x Anaconda3-2022.10-Linux-x86_64.sh # make it executable
bash Anaconda3-2022.10-Linux-x86_64.sh # execute file to start installation

After installation please exit the terminal to enable changes and then open a new SSH connection!

Now we will create the virtual environment running python 3.5:

conda create -n py35 python=3.5

to activate the new environment please do:

conda activate py35

We can check if Python 3.5 is used by running:

which python
python --version

Manual configuration of snappy module

Let’s start from accessing the snap installation directory on terminal (/home/eouser/snap/ in this case).

cd /home/eouser/snap/

Now move to folder storing SNAP executable files

cd bin

Generate the Python module snappy configured for the current SNAP installation and your conda env Python interpreter.

./snappy-conf /home/eouser/anaconda3/envs/py35/bin/python /home/eouser/anaconda3/envs/py35/lib/

Warning

The command might hang when finished and does not return to the prompt. In this case press CTRL + C and answer the question if you want to abort with no (‘n’).

And that’s it, SNAP is configured for use with python.

To test it move to snappy folder:

cd  /home/eouser/anaconda3/envs/py35/lib/

and activate python 3.5 environment

conda activate py35

Open python interface and execute

from snappy import ProductIO

The easiest way for using snappy within code is to append snappy-dir to the sys.path variable in Python code before importing snappy:

import sys
sys.path.append('<snappy-dir>') # or sys.path.insert(1, '<snappy-dir>')
import snappy

And that’s it! You have successfully connected python to SNAP API!