Welcome to the BEAM Forum!

We encourage you to sign in our forum and participate in the BEAM community. The forum is maintained by the BEAM project team who will most likely answer your questions within 24 hours (except during common holidays) - if not done by other community members. Collaborate, share your knowledge and learn from other users!

If you don't find what you are looking for, please also consider the following external forums:

Combination View Flat View Tree View
Threads [ Previous | Next ]
RE: Landsat Geotiff - reading, subsetting and combine in one file
toggle
Good morning,

First of all congratulations with launching the forum !

I just started to learn BEAM automation in Java. For the moment I have been following
the instructions at the
http://www.brockmann-consult.de/beam-wiki/display/BEAM/Exercise+1+-+Quicklook+Generator.

I have installed the Eclipse, and so far most of the things on that
page seem to work. However, I am a bit lost when I want to do some
things that are different. For example, when I try to load a
Landsat GEOTIFF instead of AATSR mentioned as an example, it doesn't work
- apparently I should specify myself the ProductReader? And when I do want a subset - where can I find the info
on how and where SubsetDef should be formulated? There is a mention that it can
be done but no specific example...

Basically I need to do just some very few simple steps that will directly benefit
two of my students - one in Benin and one in France.
For a set of Landsat Geotiff scenes (one TIF for each
channel) I would like to read a subset (to minimise the final database
volume, and I know the pixel X and Y range of the zone of interest) of each channel, then combine
them in one DIMAP file (using BandArithmetic) and
write out this file. Do you have somewhere an example of this?

Sorry to bother you with this naive question, but I hope that some users have
already written something similar and we can benefit from their experience.

Sincerely, Alexei Kouraev, Toulouse
Flag Flag
RE: Landsat Geotiff - reading, subsetting and combine in one file
6/4/09 7:14 PM as a reply to Alexei Kouraev.
Hi Alexei, I provide here the source code for a tool which should do exactly what you want. Hopefully it is useful to other users as well.

Norman

 1
 2package org.esa.beam.ex;
 3
 4import org.esa.beam.framework.dataio.*;
 5import org.esa.beam.framework.datamodel.*;
 6import org.esa.beam.util.ProductUtils;
 7
 8import java.io.*;
 9
10/**
11 * Reads the first band of all products found in {@code sourceDir} and writes
12 * a subset given by {@code rx ry rw rh} to a product named {@code targetName} in BEAM-DIMAP format.</p>
13 * <p/>
14 * Usage: org.esa.beam.ex.BandMerge {@code <sourceDir> <rx> <ry> <rw> <rh> <targetName>}
15 */
16public class BandMerge {
17
18    /**
19     * Runs the tool.
20     *
21     * @param args The 6 command-line arguments: {@code <sourceDir> <rx> <ry> <rw> <rh> <targetName>}
22     */
23    public static void main(String[] args) {
24        if (args.length != 6) {
25            System.err.println("Usage: <sourceDir> <rx> <ry> <rw> <rh> <targetName>");
26            return;
27        }
28        try {
29            File sourceDir = new File(args[0]);
30            int rx = Integer.parseInt(args[1]);
31            int ry = Integer.parseInt(args[2]);
32            int rw = Integer.parseInt(args[3]);
33            int rh = Integer.parseInt(args[4]);
34            String targetName = args[5];
35            mergeBands(sourceDir, rx, ry, rw, rh, targetName);
36        } catch (IOException e) {
37            System.err.println("I/O error: " + e.getMessage());
38        } catch (Exception e) {
39            e.printStackTrace(System.err);
40        }
41    }
42
43    /**
44     * Does the job.
45     *
46     * @param sourceDir  The source directory where the GeoTIFFs are located
47     * @param rx         start X of subset region in pixels
48     * @param ry         start Y of subset region in pixels
49     * @param rw         width of subset region in pixels
50     * @param rh         height of subset region in pixels
51     * @param targetName The name of the target product.
52     *
53     * @throws IOException if an I/O error occurs
54     */
55    public static void mergeBands(File sourceDir,
56                                  int rx, int ry, int rw, int rh,
57                                  String targetName) throws IOException {
58        Product targetProduct = new Product(targetName, "unknown-type", rw, rh);
59        File targetFile = new File(targetName + ".dim");
60        ProductWriter targetWriter = ProductIO.getProductWriter("BEAM-DIMAP");
61        targetProduct.setProductWriter(targetWriter);
62        targetWriter.writeProductNodes(targetProduct, targetFile);
63
64        File[] sourceFiles = sourceDir.listFiles();
65        for (int i = 0; i < sourceFiles.length; i++) {
66            File sourceFile = sourceFiles;
67            Product sourceProduct = ProductIO.readProduct(sourceFile, null);
68
69            Band sourceBand = sourceProduct.getBandAt(0);  // First band in GeoTIFF
70            Band targetBand = targetProduct.addBand("band_" + (i + 1), sourceBand.getDataType());
71            ProductUtils.copyRasterDataNodeProperties(sourceBand, targetBand);
72
73            ProductData rasterData = sourceBand.createCompatibleRasterData(rw, rh);
74            sourceBand.readRasterData(rx, ry, rw, rh, rasterData);
75            targetBand.writeRasterData(0, 0, rw, rh, rasterData);
76
77            sourceProduct.dispose();
78        }
79
80        targetWriter.writeProductNodes(targetProduct, targetFile);
81        targetProduct.dispose();
82    }
83}
Flag Flag
RE: Landsat Geotiff - reading, subsetting and combine in one file
Answer Answer (Unmark)
6/5/09 12:58 PM as a reply to Norman Fomferra.
Dear Norman,

thanks a lot for the code ! I am putting it now to work.

I have a message error at line 66:
File sourceFile = sourceFiles;
shouldn't it be
File sourceFile = sourceFiles AND i in Brackets ;
instead?
Or did I mess something before?

Strange, I typed "i" in square brackets in the new line but it disappears once I save the post...
Flag Flag
RE: Landsat Geotiff - reading, subsetting and combine in one file
6/5/09 1:22 PM as a reply to Alexei Kouraev.
Alexei Kouraev:
Strange, I typed "i" in square brackets in the new line but it disappears once I save the post...


This is due to the markup that this forum supports. Square brackets are used to encode formatting, e.g. [ i ] marks italics (and is ended with [ /i ]). To disable interpretation of the markup code, just place a space right after the square bracket (as I did to show [ i ]).

Also available: [ b ] for bold, [ u ] for underline and various others - available in the editor toolbar.
Flag Flag
RE: Landsat Geotiff - reading, subsetting and combine in one file
6/5/09 2:01 PM as a reply to Alexei Kouraev.
See lets try it. The correct statement in line 59 is of course

File sourceFile = sourceFiles[ i ];
Flag Flag
RE: Landsat Geotiff - reading, subsetting and combine in one file
6/5/09 4:50 PM as a reply to Norman Fomferra.
OK, now the [ i ] issue is clear, thanks Olaf and Norman. We are advancing forward!

Two more simple questions:

1) Is the first line
package org.esa.beam.ex;
a) necessary
b) specific to the name of the project I use or specific to some default beam library?
I have a message saying "The declared package "org.esa.beam.ex" does not match the expected package ""

2) I don't know (yet :-))) how to launch the application in command line. If I want to declare the 6 arguments in the "Run configuration/Arguments" of Eclipse, how exactly should I declare them?
For example is the sequence
"D:\\Test1986\\" 1 1 1000 1000 "testoutfile"
a correct entry to arguments? Or should I use comma, ' instead of ", or else?

Alexei
Flag Flag
RE: Landsat Geotiff - reading, subsetting and combine in one file
6/8/09 9:06 AM as a reply to Alexei Kouraev.
Alexei Kouraev:
I have a message saying "The declared package "org.esa.beam.ex" does not match the expected package"

With this package the file should be located in the directory org/esa/beam/ex in your source folder - the package you give in this file must match the location in the source folder.
Alexei Kouraev:
For example is the sequence
"D:\\Test1986\\" 1 1 1000 1000 "testoutfile"
a correct entry to arguments?

The arguments are separated by whitespace, e.g. space. The "Program Arguments" input field is the correct location.

To get rid of the \ or \\ ambiguities you can usually use / as path separator in java applications. You'll need "quotes" if one argument contains spaces like "C:/Documents and Settings", which would otherwise be counted as three arguments.

Parameters that start with "-D" or "-X" usually go in the "VM arguments" input in Eclipse.

So you gave 6 parameters and wouldn't need the quotes. Regarding the slashes or backslashes one backslash might be enough, I'm not exactly sure, so I recommend slashes.
Flag Flag
RE: Landsat Geotiff - reading, subsetting and combine in one file
6/8/09 9:25 AM as a reply to Alexei Kouraev.
Please note that BEAM uses zero-based pixel indices. x=0,y=0 addresses the upper left pixel.

The continuous image coordinate system has its origin at the upper left edge of the upper left pixel, X axis goes to the right, Y axis down. The centre of the upper left pixel has therefore the coordinate x=0.5, y=0.5.
Flag Flag
RE: Landsat Geotiff - reading, subsetting and combine in one file
6/9/09 12:35 PM as a reply to Norman Fomferra.
IT WORKS !!!

After all the suggested modifications it still was not working, until I added two more user libraries to the project - beam-landsat-reader and beam-geotiff (I am not sure if both are needed or just one of them). Once it was done, the code was running.

Thanks again for all your help!
Flag Flag
RE: Landsat Geotiff - reading, subsetting and combine in one file
4/1/11 1:47 AM as a reply to Alexei Kouraev.
I have been trying this code out, and it works well.
The files I am loading are already geocoded, so I added the following to the process:
1                sourceProduct.transferGeoCodingTo(targetProduct, null;

to ensure the new files are also geocoded.

I want to use it with Landsat 7 data, and include both the 30m and 15m bands in a single product. I tried this:

1        ProductSubsetDef _subset = new ProductSubsetDef();       
2                 ...
3               //              apply geocoding to the new band
4               _subset.addNodeName(targetBand.getName());
5               sourceProduct.transferGeoCodingTo(targetProduct, _subset);
6               _subset.removeNodeName(targetBand.getName());



But it doesn't work. I see that the SceneFactory class code checks the result of isUsingSingleGeoCoding and decides to geocode the entire product rather than just the band.

How do I ensure that my target product uses band geocoding?
Flag Flag
RE: Landsat Geotiff - reading, subsetting and combine in one file
4/4/11 9:54 AM as a reply to David Currie.
Hello David,

if your source product is geo-coded per band, the target product will be geo-coded per band too. The bands in the target product must have the same name as the ones in the source product.
For the two source product you have you have to call the transferGeoCoding(...) method twice.
You can do it also manually. Use the SceneFactory.createScene(...) to create a scene for each source and target band and call on each sourceScene transferGeoCodingTo(targetScene, subsetDef).

Please note, if you want to merge the different Landsat bands into one product you have make sure that all bands have the same size (width, height). Maybe you have to re-sample one of the products first.


I hope this helps to get you further.

best wishes
Marco
Flag Flag