Running snaphu import in snappy error

I’m trying to unwrap a filtered interferogram using snappy, but I get the following error when trying to run SnaphuImport:

ValueError: cannot convert a Python 'str' to a Java 'org.esa.snap.core.datamodel.Product'

My code is:

def read_input_file(filename):
    return ProductIO.readProduct(filename)

def unwrap(product):
    parameters = HashMap()
    parameters.put("doNoKeepWrapped", "false")
    return GPF.createProduct("SnaphuImport", parameters, product)

def write_dim(product, filename):
    ProductIO.writeProduct(product, filename, "BEAM-DIMAP")
    return

in_file = "/xxx/*_filt_ifg.dim"
snaphu_file = "/xxx/UnwPhase_*.snaphu.hdr"
out_file = "/xxx/*_unw_ifg.dim"

filt_in = read_input_file(in_file)
snaphu_in = read_input_file(snaphu_file)
source_products = "{},{}".format(filt_in, snaphu_in)
unw = unwrap(source_products)
write_dim(unw, out_file)

The snaphu img file was created and is the same size as the other img files in the snaphu and filt ifg directories. I get the error when running:

unw = unwrap(source_products)

The source_products variable appears okay:

org.esa.snap.core.datamodel.Product[name=*_filt_ifg],org.esa.snap.core.datamodel.Product[name=UnwPhase_*]

Any ideas what might be causing this error?
Thanks

I think you have to use the full name of the file.

what are you importing before unwrapping?

I am also facing several problems. how you running those from python?

Can anyone help me to perform snaphu unwrapping in python?

I do use full paths, I just truncated them here.

I solved my problem by using ‘prodset’: My functions stayed the same, just modified my code:

filt_in = read_input_file(in_file)
snaphu_in = read_input_file(snaphu_file)

prodset = []
prodset.append(filt_in)
prodset.append(snaphu_in)
unw = unwrap(prodset)

write_dim(unw, out_file)

Can you share full process of use snaphu from python?
I am really confused with it.

Sure. I can’t share my scripts in full as they won’t make sense because they’re customised to run on my system. However I’ll outline the SNAPHU/unwrapping processing section:

I use functions to call snappy commands. The default parameters are listed here, they may need to modified to suit your processing.

After creating your filtered interferogram, you first need to create the SNAPHU unwrapping file with ‘SnaphuExport’:

def snaphu_file(product, snaphu_dir):
        parameters = HashMap()
        parameters.put("targetFolder", snaphu_dir_path)
        parameters.put("statCostMode", "DEFO")
        parameters.put("initMethod", "MCF")
        parameters.put("numberOfTileRows", 10)
        parameters.put("numberOfTileCols", 10)
        parameters.put("numberOfProcessors", 4)
        parameters.put("rowOverlap", 200)
        parameters.put("colOverlap", 200)
        parameters.put("tileCostThreshold", 500)
        return GPF.createProduct("SnaphuExport", parameters, product)

snaphu_path = "/xxxxx/SNAPHU"
snaphu_dir = "/xxxxx/SNAPHU/xxx_snaphu"
filtered_file = read_input_file("/path/of/filtered/ifg/file.dim")
snaphu_out = snaphu_file(filtered_file, snaphu_path)
write_snaphu(snaphu_out,snaphu_dir)

Next you need to run snaphu separately on the command line. It currently can’t be run via SNAP. The command is at the top of the snaphu.conf file (it’s commented out). For example:

# snaphu -f snaphu.conf Phase_ifg_<dates>.snaphu.img 8306

Note: You may need to update the snaphu.conf file with correct paths (if they are wrong). The full path to the snaphu executable may be required.

Next the unwrapped interferogram can be created with ‘SnaphuImport’:

def unwrap(product):
    parameters = HashMap()
    parameters.put("doNotKeepWrapped", "false")
    return GPF.createProduct("SnaphuImport", parameters, product)

filt_in = read_input_file(in_file)
snaphu_in = read_input_file(snaphu_file)

prodset = []
prodset.append(filt_in)
prodset.append(snaphu_in)
unw = unwrap(prodset)

write_dim(unw, out_file)

Now you should have the unwrapped interferogram.

Here I am getting error. cmd can’t recognising snaphu. I can’t understand, where I put snaphu because it will be used a a windows program which I will use from cmd and snap also. I don’t understand why snaphu developers make it like this. Hope, they may be find something to make it simple or may be it is the final.

Can you give me any suggestions how to configure snaphu in this case ?

If you’re using Windows, I can’t help as I’m using Linux. I suggest having a look at the SNAPHU install page and see if that helps: https://step.esa.int/main/third-party-plugins-2/snaphu/

If you’re still stuck, I suggest making a new topic in the forum to see if anyone else can help