Exercise 4 - Scripting

Skip to end of metadata
Go to start of metadata

Learning Targets

  • Introduction to Java enabled Python
  • Using Jepp to call Java from Python

See also Scripting for BEAM and BEAM JavaScript Examples.

Introduction to Java enabled Python

There are different methods to combine Python and Java. A quick overview:

  • Python also know as CPython is the original implementation of the Python programming language.
  • Jython is an implementation of the Python programming language in 100% Pure Java and seamlessly integrated with the Java platform. It thus allows you to run Python on any Java platform.
  • Jepp - Java Embedded Python embeds CPython in Java and gives access to the high quality Python modules, both native and Python-based.
  • JPype is an effort to allow python programs full access to java class libraries.

Jepp and JPype both allow the usage of Java libraries from within Python code. The JPype project started very recently so use here the Jepp project.

There are two incompatibilities between current Jython and CPython versions. First, Jython 2.2 does not implement any features added to CPython in version 2.3 or later. This includes language features such as decorators, and library modules such as optparse. A more modern version of Jython is under active development. Second, Jython programs cannot use CPython extension modules written in the C programming language. These modules usually have files with the extension .pyc, .pyd or .dll. If you want to use such a module, you should look for an equivalent written in pure Python or Java.

Using Jepp to call Java from Python

Requirements:

  • Python
  • Java (JRE or JDK)
  • Jepp

This example creates a new Product and fills its only band with a linear gradient.
This could be adapted to write from exiting code into the BEAM-DIMAP data format.

writeProduct.py
import jep
from jep import *
from java.io import *
from org.esa.beam.framework.dataio import *
from org.esa.beam.framework.datamodel import *
from com.bc.ceres.core import ProgressMonitor

try:
    rasterWidth = 200
    rasterHeight = 200
    product = Product("FOO", "BAR", rasterWidth, rasterHeight)
    band = product.addBand("linear", ProductData.TYPE_UINT16)

    productWriter = ProductIO.getProductWriter("BEAM-DIMAP")
    productWriter.writeProductNodes(product, argv[1])

    lineData = jarray(rasterWidth, JINT_ID)
    for y in range(0, rasterHeight):
        for x in range(0, rasterWidth):
            lineData[x] = x
        band.writePixels(0, y, rasterWidth, 1, lineData, ProgressMonitor.NULL)
    product.dispose()

    print "product written"

except Exception, e:
    print 'error'
    print e

To execute this Python script you need a batch script(on Windows) or a shell script (on Unix) to start the JVM with right classpath. It must contain the BEAM and the JEPP libaries.

Windows:

jep-test.bat
@echo off

set BEAM4_HOME=C:\Program Files\beam\beam-4.9.0.1
set JEPP_HOME=C:\Program Files\Jepp

"%BEAM4_HOME%\jre\bin\java.exe" ^
-Xmx1024M -cp ^
"%BEAM4_HOME%/lib/*;^
%BEAM4_HOME%/modules/*;^
%BEAM4_HOME%/modules/lib-hdf-2.7/lib/*;^
%JEPP_HOME%/jep.jar" jep.Run %*
exit /B 0

Unix:

jep-test.sh
#! /bin/sh

BEAM4_HOME=/opt/Beam/beam-4.9.0.1
JEPP_HOME=/opt/Jepp

. "$BEAM4_HOME/bin/detect_java.sh"

"$app_java_home/bin/java" -Xmx1024M \
-cp "./cloud_mask.jar:\
$BEAM4_HOME/lib/*:\
$BEAM4_HOME/modules/*:\
$BEAM4_HOME/modules/lib-hdf-2.7/lib/*:\
$JEPP_HOME/jep.jar" jep.Run "$@"

exit 0

This script gets as its first argument the name of the Python script to execute. The product writing script from above is called with:

> jep-test.bat writeProduct.py grunz.dim

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.