Attach pixel geocoding in graph builder

I am trying to process several S5p images with SNAP but i can’t seem to find a way to add the tool “attach pixel geocoding” to the graph builder so i can use it in batch processing. Do you have any ideia how to do it?

This is currently only possible in the GUI or with a Python script. But it is not possible in the GraphBuilder or with gpt.

can you provide me documentation to code that? Thank you!

The code would roughly look like the following:

latBand = product.getBand(latName)
lonBand = product.getBand(lonName)
GeoCodingFactory= snappy.jpy.get_type(‘org.esa.snap.core.datamodel.GeoCodingFactory’)
# validmask can be set to None if all lat/lon values are valid in the bands
# searchRadius defines the radius in pixels when searching for the best pixel for a geo-location. A value between 3and 5 is probably good. Depends also on the resolution.
pixelGeoCoding = GeoCodingFactory.createPixelGeoCoding(latBand, lonBand, validMask, searchRadius)

For the general use of snap/python please take a look at this guide:
Using SNAP in your Python programs

Thank you so much for your help! I still need documentation to create an algorithm for S5p pre-processing.

With such an algorithm for S5p I can’t help.
What pre-processing do you want to do? What is the final goal?
Might be good to know if others want to help you.

-Apply pixel geocoding
-Export to netcdf4
-Reproject to WGS84

Currently I just want to do this.

When executing the code you provided I get this error

----> 7 pixelGeoCoding = GeoCodingFactory.createPixelGeoCoding(latBand, lonBand, validMask=None, searchRadius=6)

RuntimeError: no matching Java method overloads found

Maybe it must be an empty String in Python.
So try:
validMask = ‘’

This can all be done by operators (except the pixel geocoding)
Please have a look at this post:

For subetting there is the ‘Subset’ operator and for reprojection you use the ‘Reproject’ operator.
Saving as NetCDF4 can be done with the ‘Write’ operator.

You can also get the parameters names when you invoke the gpt from the command line:

gpt Subset -h

This will show the help for the Subset operator.

As output format for the Write operator you should use ‘NetCDF4-CF’

You were right Marco, string will do it but I still had to force searchRadius with int and validMask with str to get it to work. Thank you so much!

1 Like

Okay, good that it works now.
I’m not really a Python programmer, so I’m not very familiar with it.

Do you have any ideia how to run a graph file generated in SNAP using the gpt in a python script? I have done this in bash but i cannot run it using a similar syntax for python.

Bash Version:

gpt D:/Startup_Voucher/Projetos/Movimento_Gaio/Processing/STEP1Graph_Prep.xml -PInput=$i -POutput=$Output_pathname

Python Version:

gpt “D:/Startup_Voucher/Projetos/Artigo/Export_netcdf4.xml” -PInput=p -POutput=Output_pathname

I’ve also seen the other post from you.

I’m not sure but if you haven’t simplified the command and you use it as you have written here, than it could be that the variables are not replaced.

In order to validate the created command you could add


before executing it.

If this doesn’t help you should provide more of your code. Then it is easier to help.

I have this code. Parameters not mentioned here are all defined. The problem seems is that the Input, Bandname and Output are not recognized from the preprocessing I do in the loop. Does Input and Output only take file paths?

for i in range(0, fileNumber):

p = ProductIO.readProduct(filePathList[i])
# list(p.getBandNames())

p_basename = os.path.basename(filePathList[i]) # extract file name for each selected file

latBand = p.getBand("latitude_PRODUCT")

lonBand = p.getBand("longitude_PRODUCT")

searchRadius = int(6)

validMask = str("")

GeoCodingFactory= snappy.jpy.get_type("org.esa.snap.core.datamodel.GeoCodingFactory")

pixelGeoCoding = GeoCodingFactory.createPixelGeoCoding(latBand, lonBand, validMask,searchRadius)

p_preprocessed = p.setSceneGeoCoding(pixelGeoCoding)

p_preprocessed_name =  nameStart + p_basename
# print(p_preprocessed_name)
Output_pathname = os.path.join(output_folder,p_preprocessed_name)

command = 'cmd /k "gpt D:\Startup_Voucher\Projetos\Artigo\Export_netcdf4.xml" -PInput=p_preprocessed -PBandname=Bandname -POutput=Output_pathname'

Some suggestion you can try:

  • invoke gpt with ‘cmd /k’ and use directly “gpt …”
  • use the absolute path to the gpt executable
  • Split the command and let the variables be replaced:
    “gpt D:\Startup_Voucher\Projetos\Artigo\Export_netcdf4.xml -PInput=” + p_preprocessed + " -PBandname=" + Bandname + " -POutput=" + Output_pathname

I still get this:

C:\Users\Eduardo Godinho\p_preprocessed’ file didn’t exist

It seems it cannot input a variable but only files.

It still seems that the variable name is still used in the command string and not the content of the variable. Have you tried my third hint?

  • Split the command and let the variables be replaced:
    “gpt D:\Startup_Voucher\Projetos\Artigo\Export_netcdf4.xml -PInput=” + p_preprocessed + " -PBandname=" + Bandname + " -POutput=" + Output_pathname

The variable problem got solved I guess but now I’m get this error:

Error: Operator SPI not found for operator [graph_path]

Almost solved. I think.
graph_path is also a variable in your code, I guess.
So instead of the actual path the variable name is still included in the command.