ENVIRONMENTAL INFORMATICS
GEOINFORMATION PRODUCTS
  BROCKMANN CONSULT
BEAM Java Tutorial
   

Exercise 2

How to read pixel data from a band of a data product

Contents

  1. List the bands contained in a data product
  2. Get a specific band and read pixel data
  3. Dump pixel data to the console

List the bands contained in a data product

Create a new class Ex2 in the package org.esa.beam.basics. If you forgot how to create a new class in IDEA, step back to exercise 1. Copy the first lines from the example. Note that we can reuse the Ex1.readProduct method in Ex2:

We now ask the product for its contained bands. Type product.getB next and see what the Product API can do for us. If the suggestion list does not automatically pop-up, press Ctrl+Space. We select the getBands method. We then assign the resulting Band array object to the variable bands by applying the Insert Variable refactoring.

Type itar and press Ctrl+J in ordert to invoke an IDEA life template which generates code to iterate over an array:

We implement the loop body so that each product band is printed to the console. This way we get an overview of all the bands indicee and their names. Finally we dispose the product instance:

In order to run the code of Ex2 in IDEA, we copy the Ex1 run/debug configuration. We select Edit Configurations from the drop-down list of run/debug configurations.

Select configurations Ex1 and press the button Copy Configuration. Then rename configuration and name of the main class to Ex2. This time we use an Envisat MERIS Level-2 product and specify its local file path as program parameter. Press OK.

Press Shift+F10 in order to run the program. The console window shows the following output for the Envisat MERIS Level-2 product:

Get a specific band and read pixel data

Now that we know which bands we have, we select band reflec_6 (MERIS normalised surface reflectance at 619.601 nanometers) and out-comment our band dumping loop, since we dont need it anymore (but maybe later for another product type). The Product.getBand method gets the band object for a known band name:

Ctrl+Q opens the BEAM API documentation for the Product.getBand method. It informs us, that it can return null for the case that a band with the given name could not be found.

Honestly we handle the band==null case with a trivial error handler.

At last we want to read pixel data from reflec_6. Type band.read and inspect the suggestion list. Select the Band.readPixels variant using the float array parameter.

.

The method wants us to provide the offset x,y in pixel units and the rectangular area w,h to be read into the given array buffer. I've provided arbitrary parameters in this example here, but they're valid in the context of the given test data product.

We provide a pixel buffer pixels that is large enough to hold the entire rectangular area of w times h pixels. A curly, red line in the editor reminds us that we have to handle an IOException, in the case of I/O errors.

After locating the caret on the erroneous code, we again either press Alt+Enter or we click on the intention icon and select Surround with try/catch in order to generate a try/catch block. We then implement our poor-man's error handling code:

The duplication of the w,h constants and the pixel offsets is not self-explaining, so we introduce a few local variables. Again, the Insert Variable refactoring helps us to do this. Note that I've selected the Replace all occurences toggle.

Now the code is more understandable:

Dump pixel data to the console

Finally we output the contents of the pixel buffer pixels to the console. Since pixel data is obviously returned row-major order (line-by-line) in a flat, one-dimensional, numeric array where the x-coordinate varies fastest. Each element in the pixel buffer is therfore addressed by the index i = y * w + x.

Here is how the output should look like:

Exercise 2 is herewith complete so that we now can head over to Exercise 3a.

© 2005 by Brockmann Consult - Need help? Contact beam minus issues at brockmann minus consult dot de