Getting Started with SNAP Toolbox in Python | by Aditya Sharma | Towards Data Science

Developed by the European Space Agency (ESA), SNAP is a common software platform that supports the Sentinel missions. It consists of several modules that can be modified and re-used for image processing, modelling and visualization of data from earth observation satellites. SNAP can be utilized not only as a research support tool for Sentinel missions (Sentinel 1, Sentinel 2 and Sentinel 3) but also as a functional outlet for effectively processing large amounts of satellite data, including data from other missions such as Landsat, MODIS and RapidEye in various different formats. The project page of SNAP and the individual toolboxes can be found at http://step.esa.int.
In this article, I want to go through the step-by-step process of configuring your python installation to use the SNAP-Python or “snappy” interface. Doing so will allow you to efficiently analyze large volumes of satellite data by automating image processing tasks using python scripts.
The article will cover the following topics:
Download and install latest SNAP release (v7.0) on Windows,Configure snappy during and after the SNAP installation process,Setup a virtual environment,Configure optimal settings for snappy, andVisualize satellite data using snappy
Download and install latest SNAP release (v7.0) on Windows,
Configure snappy during and after the SNAP installation process,
Setup a virtual environment,
Configure optimal settings for snappy, and
Visualize satellite data using snappy
SNAP consists of several toolboxes. You can install each toolbox separately or you can install the all-in-one version. In this tutorial, we will install the latter.
SNAP Toolboxes can be downloaded from the link below: https://step.esa.int/main/download/snap-download/
Before you should install the SNAP software on your system, I would recommend creating a virtual environment either using pip or conda. For this tutorial, we will use conda. To run this command, you need to have Anaconda or Miniconda installed on your system already.
Create a new virtual environment called “snap” with python version 2.7 by executing the command below on your system’s python configured command line tool.
conda create -n snap python=2.7
The reason we are using python version 2.7 is because SNAP software only supports python versions 2.7, 3.3 and 3.4.
NOTE: If you are using pip to create a new environment you will have to install venv package. Find out more on how to create virtual environments using venv here.
Next, we can start installing the SNAP software using the downloaded executable file.
You can configure your python installation to use snappy during the SNAP installation itself by checking the tickbox and proving the path to your python directory (as shown below). However, in this tutorial, we will NOT check the tickbox and configure snappy after SNAP has finished installing.
The rest of the installation process is pretty straight forward. The first time you start up SNAP it will do some updates. Once they’ve finished you are all set to use the SNAP software.
Once SNAP has finished installing, we want to get the directory location of where our virtual environment “snap” is located. From there, we want the following two paths:
Python executable path for your virtual environmentA folder called “Lib” in your virtual environment directory
Python executable path for your virtual environment
A folder called “Lib” in your virtual environment directory
Open up the SNAP Command-Line Tool, which is installed as part of the SNAP software and run the following command:
snappy-conf {path_to_snap_env}\python.exe {path_to_snap_env}\Lib\
Change the amount of RAM available
Before we can start using snappy to create a python script, we need to change a few things in order to ensure that the snappy operators will run with highest optimality. The default setup is quite slow if you’re planning to do some image rendering and batch processing operations. This is important if you want to use snappy to automate heavy processing tasks and work with multiple files.
Go over to the {virtural_env “snap ”directory } > Lib > snappy.
Open the snappy configuration file called “snappy.ini”.
Edit the value for the parameter called “java_max_mem” and set it to a value that corresponds to about 70–80% of your system’s RAM.
java_max_mem: 26G
In the same directory, lies a file called “jpyconfig.py”. Change the parameter “jvm_maxmem” like so:
jvm_maxmem = '26G'
Change the TileCache Memory
Next, navigate to where your SNAP software is installed. By default it is set to C:\Program Files\snap. Inside it, you will find the folder called etc containing the SNAP properties file called “snap.properties”. Edit the file and change the parameter called snap.jai.tileCacheSize. Set this to a value equal to 70–80% of the java_max_mem value. The number you enter should be in megabytes. You may have to enable administrator privileges to save the changes to this file.
snap.jai.tileCacheSize = 21504
NOTE: The values shown here are for a system with 32GB RAM. Changes must be made with your system’s RAM in mind.
So for this last section, we want to explore how snappy works and visualize some sample data. Before we can start using snappy, we need some data and in this tutorial, we will use the testing data that comes pre-loaded with snappy installation.
# Import Librariesimport osimport numpy as npimport matplotlib.pyplot as pltimport snappyfrom snappy import ProductIO# Set Path to Input Satellite Data# miniconda userspath = r'C:\Users\{User}\miniconda3\envs\snap\Lib\snappy\testdata'# anaconda userspath = r'C:\Users\{User}\anaconda3\envs\snap\Lib\snappy\testdata'filename = 'MER_FRS_L1B_SUBSET.dim'# Read Filedf = ProductIO.readProduct(os.path.join(path, filename))# Get the list of Band Nameslist(df.getBandNames())# Using "radiance_3" band as an exampleband = df.getBand('radiance_3') # Assign Band to a variablew = df.getSceneRasterWidth() # Get Band Widthh = df.getSceneRasterHeight() # Get Band Height# Create an empty arrayband_data = np.zeros(w * h, np.float32)# Populate array with pixel valueband.readPixels(0, 0, w, h, band_data) # Reshapeband_data.shape = h, w# Plot the band  plt.figure(figsize=(18,10))plt.imshow(band_data, cmap = plt.cm.binary)plt.show()
I’ve also made a video showing everything that I’ve laid out in this post. So if there is any confusion concerning how to go about the process of installing and configuring SNAP Toolbox in Python, do check it out.
Github Page: https://github.com/senbox-orgConfiguring SNAP for Python: https://senbox.atlassian.net/wiki/spaces/SNAP/pages/50855941/Configure+Python+to+use+the+SNAP-Python+snappy+interfaceForum discussions that were useful while writing this post
Github Page: https://github.com/senbox-org
Configuring SNAP for Python: https://senbox.atlassian.net/wiki/spaces/SNAP/pages/50855941/Configure+Python+to+use+the+SNAP-Python+snappy+interface
Forum discussions that were useful while writing this post