Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.
Wiki Markup
h1. Learning Targets

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

See also [Scripting for BEAM] and [BEAM JavaScript Examples].

h1. 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.

h1. Using Jepp to call Java from Python

* 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.
import jep
from jep import *
from import *
from org.esa.beam.framework.dataio import *
from org.esa.beam.framework.datamodel import *
from com.bc.ceres.core import ProgressMonitor

    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)

    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.

@echo off

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

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

#! /bin/sh


. "$BEAM4_HOME/bin/"

"$app_java_home/bin/java" -Xmx1024M \
-cp "./cloud_mask.jar:\
$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 grunz.dim

{table-plus:width=100%|enhableHighlighting=false| columnAttributes=style="width:33%;text-align:left;", style="width:33%;text-align:center;", style="width:33%;text-align:right;"}
|| [Previous - Cloud Masking Operator|Exercise 3 - Cloud Masking Operator] || [Up -  BEAM 4 Programming Tutorial| BEAM 4 Programming Tutorial] || [Next - BEAM Modules|Exercise 5 - BEAM Modules] ||