Snappy on Travis

SNAP is a platform for processing and analysis of Earth Observation products. It exposes an interface (SNAP-Python) that enables using the platform in python projects.

This article steps through the process of installing SNAP and configuring a python environment to use SNAP-Python.


Travis dist used: bionic. python version 3.9

This workflow has been tested on multiple linux distros. No thorough test has been done on other *nix

Prerequisites

This process requires some dependencies. Have a script that installs the following:

SNAP installation works on java version >= 8

#! /bin/bash

# This script handles the download and installation of Apache Maven required to successfully build SNAP components.

set -e

echo "----- MVN INSTALLATION -----"

JDK_HOME=$(dirname $(dirname $(readlink -f $(which java))))
echo "export JAVA_HOME=$JDK_HOME" >> $TRAVIS_HOME/.profile
echo "export PATH=$JAVA_HOME/bin:$PATH" >> $TRAVIS_HOME/.profile
source $TRAVIS_HOME/.profile
echo "----- java version: $(java -version) -----"
echo "----- Updated env vars: JAVA_HOME=$(echo $JAVA_HOME) -----"

# Install maven 3.8.1
echo "----- downloading and extracting Apache Maven -----"

MAVEN_HOME=/opt/apache-maven-3.8.1
if [ -z $MAVEN_HOME ]; # /opt/apache-maven-3.8.1
then
    echo "----- downloading and extracting Apache Maven to /opt -----"
    url=https://downloads.apache.org/maven/maven-3/3.8.1/binaries/apache-maven-3.8.1-bin.tar.gz
    wget $url -O - | tar -xzf - -C /opt
fi

echo "export M2_HOME=$MAVEN_HOME" >> $TRAVIS_HOME/.profile
echo "export PATH="$MAVEN_HOME/bin:$PATH"" >> $TRAVIS_HOME/.profile
source $TRAVIS_HOME/.profile

echo "----- mvn version: $(mvn -v) -----"
echo "----- update env vars: M2_HOME=$(echo $M2_HOME) -----"

SNAP installation

Download and run the snap installer. Using the -q flag on the installer runs it without the graphical interface.

It is a good idea to cache the installer due to its large size.

SNAP installation directory is /opt/snap in this case. Usually in ~/snap

# check if snap is installed
if [ ! -f /opt/snap/VERSION.txt ];
then
    echo "----- SNAP INSTALLATION -----"

    if [ -z "$(find $TRAVIS_HOME/installer -name '*.sh' | head -1)" ];
    then
        echo "-----  Download SNAP installer -----"
        mkdir $TRAVIS_HOME/installer && cd $TRAVIS_HOME/installer
        wget https://download.esa.int/step/snap/8.0/installers/esa-snap_sentinel_unix_8_0.sh
    fi

    echo "----- Install SNAP with gpt -----"
    cd $TRAVIS_HOME/installer && chmod +x esa-snap_sentinel_unix_8_0.sh
    ./esa-snap_sentinel_unix_8_0.sh -q

    echo "----- Successful SNAP installation -----"
fi

SNAP-Python configuration

Building SNAP-Python (called snappy) requires jpy. jpy is a java-python bridge.

NOTE: If you set up the JAVA env variables fine and did source ~/.profile, this should work fine.


if [ ! -f $TRAVIS_HOME/.snap/snap-python/snappy/jpyconfig.properties ];
then
    echo "----- JPY INSTALLATION -----"

    cd $TRAVIS_HOME && git clone https://github.com/bcdev/jpy.git
    cd jpy && python3.9 setup.py build maven bdist_wheel install

The wheel generated should be copied to snap-python snappy folder and set the python executable for snap.

  echo "----- CONFIGURING SNAP-PYTHON -----"

    if [ ! -d $TRAVIS_HOME/.snap/snap-python/snappy ]; 
    then
        mkdir -p $TRAVIS_HOME/.snap/snap-python/snappy
    fi

    echo "----- Copying jpy to snappy -----"
    cp dist/*.whl $TRAVIS_HOME/.snap/snap-python/snappy && rm -rf jpy

    if [ -d $TRAVIS_HOME/snap ];
    then
    ls -la $TRAVIS_HOME/snap
    fi

    # Add to env vars
    export PATH=/opt/snap/bin:$PATH
    # Configure python interpreter
    /opt/snap/bin/snap --nogui --nosplash --python $(which python3.9)
fi

After this, simply install snappy with your python executale to be able to simply import snappy

cd $TRAVIS_HOME/.snap/snap-python/snappy && python3.9 setup.py install 

echo "----- Installation and configuration success! -----"

Remember to cache snap and generated folder .snap and/or the installer

before_install:
  # Install SNAP
  - . ./travis_scripts/java_mvn_install.sh
  - . ./travis_scripts/snap_install.sh
  
...

cache:
  directories:
    - $TRAVIS_HOME/.snap
    - /opt/snap
  

Happy coding!! :)

A review: The Pragmatic Programmer

MicroCorruption: Jakarta

comments powered by Disqus