Using the SNAP Java API in an external JAVA program

Hi, I’m trying to use the SNAP API in one of my own Java programs, but when trying to open a product (Sentinel-1 SLC) using ProductIO, I end up with a Null pointer. I suspect that the reader for the file format cannot be found?

I have a succesful build of the SNAP engine and desktop, as well as the S1 Toolbox. The file opens just fine in the desktop build, but not using my own code. Do I have to do anything special to initialize a product reader mechanism? My code is as follows:

import java.io.IOException;
import org.esa.snap.core.datamodel.Product;
import org.esa.snap.core.dataio.ProductIO;

public class MyTest {
public static void main(String[] args) {
Product prod;
try {
prod = ProductIO.readProduct(“S1B_IW_SLC__1SDV_20181223T165126_20181223T165153_014172_01A561_A693.SAFE/manifest.safe”);
int numBands = prod.getNumBands();
System.out.println("numBands = " + numBands);
} catch (IOException e) {
;
}
}
}

Can anyone give me a clue? Thanks!

You code looks fine.
I think it is just a problem of the classpath.
You need to add the need one from the s1tbx to your start configuration.

I think reading this guide will help you:
How to create a bundle of SNAP jars for the usage on the classpath - SNAP Wiki

That’s it. My classpath was incomplete, thank you!

Hi,
I want to use the snap java api in an external java program.
Is there any example how to use ?
My goal is to have an .nc file locally and with the help of my java program to extract the data form this file.
Is this possible?

Indeed, there was no guide for doing this yet. I wrote a few lines just now.
Using SNAP in your Java program - SNAP Wiki
It is very rough and brief. Let us know if you are missing something specific.

Hi Marco,

Thank you for information. I manage to use snap in external Java program.

Do you know if there is any examples/ documentation in java to retrieve from a file based on latitude and longitude some sensor data (like NO2)?
File was downloaded from Esa https://s5phub.copernicus.eu/dhus/#/home

Thank you

For doing this you have mainly two options.
You can invoke the PixEx operator. You can invoke it via API or by using the gpt command line or from the Desktop app.
For API usage you can have a look at the test
snap-engine/PixExOpTest.java (github.com)

You can also use directly the Product API.
There is no specific example for your use case but there are two examples which show what needs to be done. In the repository snap-examples/snap-engine-examples (github.com) you find two classes.
One, the GeoCodingEx, shows how to retrieve for a geo-position the pixel position.
And in the NdviSimpleMain you can see how pixel values from a band can be read.
Beside readPixels methods there are also getPixel(s) methods. But to use those you need to load the raster data first. For example by using loadRasterData() first.

1 Like

Thanks for suggestion.

I started with the GeoCodingEx but I have a file from Esa Sentinel 5p for air quality. This file is of format “.nc” and from ProductIO.readProduct(“file.nc”) I receive null (screenshot attached).

The file is in the right path and it is recognised but I think the problem is the file format.
Do you know why or do you have any idea how to get data from sentinel 5p in a supported format?

This is the link that will download the file that i want.
https://s5phub.copernicus.eu/dhus/odata/v1/Products(‘d99b737b-15f4-41eb-9ff4-0da707dbd335’)/$value

some discussion taking place in this other topic. SNAP does not support S5P, you need to use the Atmospheric Toolbox.

How and for what depends on what you want to do with the data… you need to understand that S5P data is not a raster so you can’t use it like that - this is usually the first trap that people fall on.

1 Like