BEAM JavaScript Examples

Skip to end of metadata
Go to start of metadata

To run these examples paste them into the experimental VISAT Scripting Console tool window and press the Run button (VISAT Menu / View / Tool Windows / Script). The default language is JavaScript. VISAT defines a global JavaScript variable visat whose interface is defined the corresponding Java class VisatApp.

Refer to the BEAM Java API docs in order to write JavaScript code for other BEAM classes. The entire API is accessible from within JavaScript.

Refer to http://www.mozilla.org/rhino/ScriptingJava.html to find information of how Java maps to JavaScript (BEAM uses the Mozilla Rhino JavaScript engine).

Overview

Examples

Create an artificial product and open it in VISAT

create-product.js
w = 512;
h = 512;
p = new Product("P1", "T1", w, h);
b1 = new VirtualBand("B1", ProductData.TYPE_FLOAT32, w, h, "sin(20*PI*(X*Y/(512*512)))");
b2 = new Band("B2", ProductData.TYPE_FLOAT32, w, h);
b2.ensureRasterData();
p.addBand(b1);
p.addBand(b2);
var x, y, v; 
for (y = 0; y < h; y++) {
    for (x = 0; x < w; x++) {
        v = 0.1 * x * y;
        b2.setPixelFloat(x, y, v);
    }
}
visat.addProduct(p);

Note: Due to a bug in the band arithmetic as of version 4.6.0, the virtual band "B1" will produce only zeros. See issue BEAM-1004. This will be fixed in 4.6.1.

Open a file selection box to open a product in VISAT

open-product.js
if (fc == null) {
	fc = new javax.swing.JFileChooser();
}
fc.showOpenDialog(null);
f = fc.getSelectedFile();
if (f != null) {
   fc.setCurrentDirectory(f.getParentFile());
   p = ProductIO.readProduct(f, null);
   if (p != null) {
       visat.addProduct(p);
   }
}

Call the subset operator and open resulting product in VISAT

call-operator.js
sp = visat.getSelectedProduct();

op = new SubsetOp();
op.setSourceProduct(sp);
op.setBandList(["radiance_1", "radiance_2", "radiance_3"]);

tp = op.getTargetProduct();
tp.setName(sp.getName() + "_SUBSET");

visat.addProduct(tp);

Auto-rotate the select view

rotate-view.js
psv = visat.getSelectedProductSceneView();
lc = psv.getLayerCanvas();
vp = lc.getViewport();

for (i = 0; i < 1000; i++) {
   java.lang.Thread.currentThread().sleep(5);
   vp.setOrientation(i/1000.0 * 2 * Math.PI) ;
}

Add a pixel position listener (PPL) to the current view

add-pixel-position-listener.js

// Before adding a new PPL, remove the one already registered
if (psv != null && ppl != null) {
	psv.removePixelPositionListener(ppl);
}

pplImpl = { 
	pixelPosChanged:function(imageLayer, x, y, level, valid, event) { 
		if (!valid) {
			message = "Hello Michael, x=" + x + ", y=" + y + " is invalid!";
			visat.showMessageDialog("Invalid", message, 0, "");
		}      
	}, 

	pixelPosNotAvailable:function() { 
	},

	// Without equals, I get an exception!
	equals:function(object) { 
		return this == object; 
	}
}

ppl = new org.esa.beam.framework.ui.PixelPositionListener(pplImpl);
psv = visat.getSelectedProductSceneView();
psv.addPixelPositionListener(ppl);

Open Landsat bands and combine them as calibrated radiance bands

The following example opens 6 Landsat bands (not the thermal one) and combines them as calibrated radiances in the first TIFF file:

read6tiffs.js
var baseName = "F:\\data\\LT51900182007153MOR00\\L5190018_01820070602";

var minL = [
   -1.520, 
   -2.840, 
   -1.170, 
   -1.510, 
   -0.370, 
    1.238,
   -0.150
];

var maxL = [
   193.000, 
   365.000, 
   264.000, 
   221.000, 
    30.200, 
    15.303,
    16.500
];

var sourceProduct = null;
var targetProduct = null;
var expression;
var targetBand;
for (i = 0; i < 7; i++) {
   if (i == 5) {
       continue;
   }
   sourceProduct = ProductIO.readProduct(baseName + "_B" + (i+1) + "0.TIF", null);
   visat.addProduct(sourceProduct); 

   if (targetProduct == null) {
       targetProduct = sourceProduct;
   }
   expression = 
           minL[i] 
         + " + $" 
         + sourceProduct.getRefNo() 
         + ".band_1 * (" 
         + maxL[i] 
         + " - " 
         + minL[i] 
         + ") / 254.0";

   targetBand = new VirtualBand("channel_" + (i+1), 
                                ProductData.TYPE_FLOAT32,
                                targetProduct.getSceneRasterWidth(),
                                targetProduct.getSceneRasterHeight(),
                                expression);
   targetProduct.addBand(targetBand);
}

visat.addProduct(targetProduct);

Add pins to the current product.

addPins.js
p = visat.getSelectedProduct();
w = p.getSceneRasterWidth();
h = p.getSceneRasterHeight();
pinGroup = p.getPinGroup();
for(j=0; j< h; j += 32) {
  for(i=0; i< w; i +=32) {
    pMark = new Placemark("p_"+i+"_"+j, "", "", new PixelPos(i, j), null, PinDescriptor.INSTANCE, p.getGeoCoding());
    pinGroup.add(pMark);
  }
}

Compare bands of two products

create-diff.js
pm = visat.getProductManager();
if(pm.getProductCount() != 2) {
  throw new IllegalStateException("pm.getProductCount() != 2");
}
p1 = pm.getProduct(0);
p2 = pm.getProduct(1);
w = p1.getSceneRasterWidth();
h = p1.getSceneRasterHeight();
pDiff = new Product("Diff", "Diff Product", w, h);
p1Prefix = '$' + p1.getRefNo() + '.';
p2Prefix = '$' + p2.getRefNo() + '.';
for(i = 0; i < p1.getNumBands(); i++) {
  p1Band = p1.getBandAt(i);
  p2Band = p2.getBandAt(i);
  p1BandName = p1Band.getName();
  p2BandName = p2Band.getName();
  expression = "'" + p1Prefix + p1BandName + "'" + " - " + "'" + p2Prefix + p2BandName + "'";
  diffBand = new VirtualBand(p1BandName + "_Diff", ProductData.TYPE_FLOAT32, w, h, expression);
  pDiff.addBand(diffBand);
}

pm.addProduct(pDiff);
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.