Versions Compared

Key

  • 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|http://www.python.org/] also know as *CPython* is the original implementation of the Python programming language.
* [Jython|http://www.jython.org/] 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|http://jepp.sourceforge.net/] embeds CPython in Java and gives access to the high quality Python modules, both native and Python-based.
* [JPype|http://jpype.sourceforge.net/] 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

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.
{code:title=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
{code}
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:
{code:title=jep-test.bat}
@echo off

set BEAM4_HOME=C:\Program Files\beam\beam-4.69.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.37/lib/*;^
%BEAM4_HOME%/modules/lib-netcdf-2.2.16/lib/*;^
%JEPP_HOME%/jep.jar" jep.Run %*
exit /B 0
{code}

Unix:
{code:title=jep-test.sh}
#! /bin/sh

BEAM4_HOME=/opt/Beam/beam-4.69.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.37/lib/*:\
$BEAM4_HOME/modules/lib-netcdf-2.2.16/lib/*:\
$JEPP_HOME/jep.jar" jep.Run "$@"

exit 0
{code}

This script gets as its first argument the name of the Python script to execute. The product writing script from above is called with:
{code}
> jep-test.bat writeProduct.py grunz.dim
{code}

----
{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] ||
{table-plus}