senbox-org / esa-snappy

GNU General Public License v3.0
9 stars 1 forks source link

[bug] S1tbx Operation Remote-GRD-Border-Noise converts parameters to something other than int when int parameters are required #3

Open catchSheep opened 1 month ago

catchSheep commented 1 month ago

Bug Description

When calling esa_snappy.GPF.createProduct("Remove-GRD-Border-Noise", parameters, source), the "borderLimit" parameter seems to be converted to something other than an int regardless of its initial type by either esa_snappy or jpy. Some rough code I extracted to demonstrate this is:

from esa_snappy import ProductIO, HashMap, GPF
import logging
log = logging.getLogger(__name__)

source = ProductIO.readProduct('sentinel 1 GRD filename')

grd_border_noise_param = {"trimThreshold": 0.5, "borderLimit": 1000}

parameters = HashMap()
GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()

for key, value in grd_border_noise_param.items():
    if key == "borderLimit" and value is not None: 
        # making sure the moment I stop handling borderLimit, it should be an int.
        log.info("Forcing border noise borderLimit to int")
        parameters.put(key, int(value))
        log.info(f"borderLimit type is {type(parameters.get(key))}")
    elif value is not None:
        parameters.put(key, value)

output = GPF.createProduct("Remove-GRD-Border-Noise", parameters, source)

My python script will crash-exit with the exception:

2024-06-26 05:43:04 INFO     Docker Output: WARNING: org.esa.snap.core.util.ServiceLoader: org.esa.snap.core.gpf.OperatorSpi: Provider eu.esa.opt.meris.sdr.aerosol.AerosolMergerOp$Spi not found
2024-06-26 05:43:04 INFO     Docker Output: WARNING: org.esa.snap.core.util.ServiceLoader: org.esa.snap.core.gpf.OperatorSpi: Provider eu.esa.opt.meris.sdr.aerosol.ModisAerosolOp$Spi not found
2024-06-26 05:43:04 INFO     Docker Output: 2024-06-26 05:43:04 utils INFO     Forcing border noise borderLimit to int
2024-06-26 05:43:04 INFO     Docker Output: 2024-06-26 05:43:04 utils INFO     borderLimit type is <class 'int'>
2024-06-26 05:43:04 INFO     Docker Output: Traceback (most recent call last):
2024-06-26 05:43:04 INFO     Docker Output:   File "/app/main.py", line 186, in <module>
2024-06-26 05:43:04 INFO     Docker Output:     main()
2024-06-26 05:43:04 INFO     Docker Output:   File "/usr/local/lib/python3.10/dist-packages/click/core.py", line 1157, in __call__
2024-06-26 05:43:04 INFO     Docker Output:     return self.main(*args, **kwargs)
2024-06-26 05:43:04 INFO     Docker Output:   File "/usr/local/lib/python3.10/dist-packages/click/core.py", line 1078, in main
2024-06-26 05:43:04 INFO     Docker Output:     rv = self.invoke(ctx)
2024-06-26 05:43:04 INFO     Docker Output:   File "/usr/local/lib/python3.10/dist-packages/click/core.py", line 1434, in invoke
2024-06-26 05:43:04 INFO     Docker Output:     return ctx.invoke(self.callback, **ctx.params)
2024-06-26 05:43:04 INFO     Docker Output:   File "/usr/local/lib/python3.10/dist-packages/click/core.py", line 783, in invoke
2024-06-26 05:43:04 INFO     Docker Output:     return __callback(*args, **kwargs)
2024-06-26 05:43:04 INFO     Docker Output:   File "/app/main.py", line 45, in main
2024-06-26 05:43:04 INFO     Docker Output:     process_file(filename, filelist, shapefile)
2024-06-26 05:43:04 INFO     Docker Output:   File "/app/main.py", line 148, in process_file
2024-06-26 05:43:04 INFO     Docker Output:     border_noise_removed_product = utils.grd_border_noise(input_prod, cfg.grd_border_noise_param)
2024-06-26 05:43:04 INFO     Docker Output:   File "/app/utils.py", line 193, in grd_border_noise
2024-06-26 05:43:04 INFO     Docker Output:     output = GPF.createProduct("Remove-GRD-Border-Noise", parameters, source)
2024-06-26 05:43:04 INFO     Docker Output: RuntimeError: org.esa.snap.core.gpf.OperatorException: Operator 'RemoveGRDBorderNoiseOp': Value for 'Border margin limit[pixels]' must be of type 'int'.
2024-06-26 05:43:06 ERROR        Exitcode nonzero for file: /home/<user>/data/S1_data/data_raw/S1A_IW_GRDH_1SDV_20230131T104608_20230131T104643_047026_05A40B_7F91.zip
2024-06-26 05:43:06 ERROR        Exitcode was: 1

The program does not crash exit if instead you do not set the borderLimit, presumably because it defaults to 500 as an int, see here or here (?). e.g. to not get the issue replace grd_border_noise_param above with:

grd_border_noise_param = {"trimThreshold": 0.5, "borderLimit": None}

This is all run inside an Ubuntu 22.04 docker container with Python 3.10.12, and the latest version of SNAP 10 from here (unsure how to check esa_snappy version or otherwise, esa_snappy.__version__ is not defined, neither for jpy)


This issue does not happen on ubuntu 18.04 with python 3.6 & SNAP9 (however Snap9's S1 orbitfile correction API query for the orbitfile is broken now).

This also doesn't seem to affect other s1tbx/microwave operations such as "Speckle-Filter", etc

--

Temporary fix

This issue seems to be able to be fixed by using jpy to cast a java.lang.Integer and passing that directly to the esa_snappy function:

from esa_snappy import ProductIO, HashMap, GPF
import jpy
import logging
log = logging.getLogger(__name__)

source = ProductIO.readProduct('sentinel 1 GRD filename')

grd_border_noise_param = {"trimThreshold": 0.5, "borderLimit": 1000}

parameters = HashMap()
GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()

for key, value in grd_border_noise_param.items():
    if key == "borderLimit" and value is not None: 
        # making sure the moment I stop handling borderLimit, it should be an int.
        JInt = jpy.get_type("java.lang.Integer")
        JborderLimit = JInt(value)
        parameters.put(key, JborderLimit)
    elif value is not None:
        parameters.put(key, value)

output = GPF.createProduct("Remove-GRD-Border-Noise", parameters, source)

although it still gives the

2024-06-26 06:26:37 INFO     Docker Output: WARNING: org.esa.snap.core.util.ServiceLoader: org.esa.snap.core.gpf.OperatorSpi: Provider eu.esa.opt.meris.sdr.aerosol.AerosolMergerOp$Spi not found
2024-06-26 06:26:37 INFO     Docker Output: WARNING: org.esa.snap.core.util.ServiceLoader: org.esa.snap.core.gpf.OperatorSpi: Provider eu.esa.opt.meris.sdr.aerosol.ModisAerosolOp$Spi not found

errors