Snaphu Export process cannot generate the snaphu.conf file

import os
from subprocess import run
import esa_snappy
from esa_snappy import ProductIO, GPF, HashMap, jpy

获取 Java 类型

Integer = jpy.get_type(‘java.lang.Integer’)
Double = jpy.get_type(‘java.lang.Double’)
String = jpy.get_type(‘java.lang.String’)

读取产品

def read_product(product_path):
try:
print(f"正在读取产品: {product_path}“)
return ProductIO.readProduct(product_path)
except Exception as e:
print(f"读取产品时出错: {e}”)
raise

Snaphu Export

def export_to_snaphu(product, output_directory, dem_file_path):
try:
print(“正在导出到SNAPHU格式…”)
parameters = HashMap()
parameters.put(“targetFolder”, output_directory)
parameters.put(“statCostMode”, String(“DEFO”))
parameters.put(“initialMethod”, String(“MCF”))
parameters.put(“nTileRows”, Integer(10))
parameters.put(“nTileCols”, Integer(10))
parameters.put(“numberOfProcessors”, Integer(4))
parameters.put(“rowOverlap”, Integer(100))
parameters.put(“columnOverlap”, Integer(100))
parameters.put(“tileCostThreshold”, Integer(500))
parameters.put(“dem”, dem_file_path)

    print("导出参数: ", parameters)  # 输出参数信息

    export_product = GPF.createProduct("SnaphuExport", parameters, product)

    # 保存导出结果
    save_product(export_product, os.path.join(output_directory, "snaphu_exported_product.dim"))
    print(f"SNAPHU导出已完成。结果目录: {output_directory}")

    # 检查snaphu.conf是否生成
    config_file_path = os.path.join(output_directory, "snaphu.conf")
    if not os.path.isfile(config_file_path):
        print(f"警告: 配置文件未找到: {config_file_path}")
    else:
        print(f"配置文件已生成: {config_file_path}")

except Exception as e:
    print(f"导出到SNAPHU格式时出错: {e}")
    raise

运行Snaphu工具进行解缠

def run_snaphu(snaphu_cmd, output_directory, config_file):
try:
# 确认配置文件生成
if not os.path.isfile(config_file):
raise FileNotFoundError(f"配置文件未找到: {config_file}")

    print("正在运行SNAPHU工具进行解缠...")
    cmd = [snaphu_cmd, "-f", config_file, output_directory]
    print("运行命令: ", ' '.join(cmd))
    run(cmd, check=True)
    print("SNAPHU解缠已完成。")
except Exception as e:
    print(f"运行SNAPHU工具时出错: {e}")
    raise

Snaphu Import

def import_snaphu_result(exported_product_path, unwrapped_phase_file, output_path):
try:
print(“正在导入SNAPHU解缠结果…”)
# 读取导出的产品
exported_product = read_product(exported_product_path)

    parameters = HashMap()
    parameters.put("snaphuUnwrappingFolder", os.path.dirname(unwrapped_phase_file))

    snaphu_imported_product = GPF.createProduct("SnaphuImport", parameters, exported_product)

    save_product(snaphu_imported_product, output_path)
except Exception as e:
    print(f"导入SNAPHU解缠结果时出错: {e}")
    raise

保存产品

def save_product(product, output_path):
try:
if os.path.exists(output_path):
print(f"删除已存在的文件: {output_path}“)
os.remove(output_path)
ProductIO.writeProduct(product, output_path, ‘BEAM-DIMAP’)
print(f"产品已保存到: {output_path}”)
except Exception as e:
print(f"保存产品时出错: {e}")
raise

数据处理流程

def process_snaphu_unwrapping(interferogram_path, output_directory, snaphu_cmd, dem_file_path, output_path):
try:
# 读取干涉图产品
interferogram_product = read_product(interferogram_path)

    # 步骤1:导出到SNAPHU格式
    export_to_snaphu(interferogram_product, output_directory, dem_file_path)

    # 步骤2:检查SNAPHU配置文件是否已生成
    config_file_path = os.path.join(output_directory, "snaphu.conf")
    print(f"检查SNAPHU配置文件: {config_file_path}")

    if not os.path.isfile(config_file_path):
        raise FileNotFoundError(f"配置文件未找到: {config_file_path}")

    # 步骤2:运行SNAPHU工具进行相位解缠
    run_snaphu(snaphu_cmd, output_directory, config_file_path)

    # 步骤3:导入SNAPHU解缠结果
    exported_product_path = os.path.join(output_directory, "snaphu_exported_product.dim")
    unwrapped_phase_file = os.path.join(output_directory, "snaphu.unw")
    import_snaphu_result(exported_product_path, unwrapped_phase_file, output_path)
except Exception as e:
    print(f"处理过程中出错: {e}")
    raise

调用示例

if name == “main”:
interferogram_path = “/data/wangfengmao_file/aipy/py/test/input/S1A_IW_SLC__1SDV_20220110T231926_20220110T231953_041405_04EC57_103E_interferogram_goldstein.dim”
output_directory = “/data/wangfengmao_file/aipy/py/test/output/snaphu”
snaphu_cmd = “/home/administrator/.snap/auxdata/snaphu-v2.0.4_linux/bin/snaphu”
dem_file_path = “/data/wangfengmao_file/aipy/py/test/dem/utm_srtm_57_05.tif”
output_path = “/data/wangfengmao_file/aipy/py/test/output/S1A_IW_SLC__1SDV_20220110T231926_20220110T231953_041405_04EC57_103E_unwrapped.dim”

if not os.path.exists(output_directory):
    os.makedirs(output_directory)

# 执行SNAPHU解缠流程
process_snaphu_unwrapping(interferogram_path, output_directory, snaphu_cmd, dem_file_path, output_path)

——————————————————————————————————————————
INFO: org.esa.snap.core.util.EngineVersionCheckActivator: Please check regularly for new updates for the best SNAP experience.
正在读取产品: /data/wangfengmao_file/aipy/py/test/input/S1A_IW_SLC__1SDV_20220110T231926_20220110T231953_041405_04EC57_103E_interferogram_goldstein.dim
正在导出到SNAPHU格式…
导出参数: {targetFolder=/data/wangfengmao_file/aipy/py/test/output/snaphu, initialMethod=MCF, numberOfProcessors=4, rowOverlap=100, tileCostThreshold=500, nTileCols=10, dem=/data/wangfengmao_file/aipy/py/test/dem/utm_srtm_57_05.tif, columnOverlap=100, statCostMode=DEFO, nTileRows=10}

100% done.
删除已存在的文件: /data/wangfengmao_file/aipy/py/test/output/snaphu/snaphu_exported_product.dim
产品已保存到: /data/wangfengmao_file/aipy/py/test/output/snaphu/snaphu_exported_product.dim
SNAPHU导出已完成。结果目录: /data/wangfengmao_file/aipy/py/test/output/snaphu
警告: 配置文件未找到: /data/wangfengmao_file/aipy/py/test/output/snaphu/snaphu.conf
检查SNAPHU配置文件: /data/wangfengmao_file/aipy/py/test/output/snaphu/snaphu.conf
处理过程中出错: 配置文件未找到: /data/wangfengmao_file/aipy/py/test/output/snaphu/snaphu.conf
Traceback (most recent call last):
File “Unwrapping.py”, line 139, in
process_snaphu_unwrapping(interferogram_path, output_directory, snaphu_cmd, dem_file_path, output_path)
File “Unwrapping.py”, line 114, in process_snaphu_unwrapping
raise FileNotFoundError(f"配置文件未找到: {config_file_path}")
FileNotFoundError: 配置文件未找到: /data/wangfengmao_file/aipy/py/test/output/snaphu/snaphu.conf
(DInSAR) administrator@g220:/data/wangfengmao_file/aipy/py/test/py$

——————————————————————————————————————————

Hello, I’ve been trying to use Snaphu Export, but I can’t seem to generate the snaphu.conf file. Could you please help me check what’s going on? Is it a problem with my operation or is there something wrong with the software?What should I do?

Snaphu Export:
(DInSAR) administrator@g220:/data/wangfengmao_file/aipy/py/test/py$ ll -r /data/wangfengmao_file//aipy//py//test/output/snaphu/
total 2620
-rw-r–r-- 1 root root 2668942 Dec 6 10:36 snaphu_exported_product.dim
drwxr-xr-x 4 root root 4096 Dec 6 09:45 snaphu_exported_product.data/
drwxrwxr-x 3 root root 4096 Dec 5 18:15 …/
drwxrwxr-x 3 root root 4096 Dec 6 10:36 ./
(DInSAR) administrator@g220:/data/wangfengmao_file/aipy/py/test/py$ ll -r /data/wangfengmao_file//aipy//py//test/output/snaphu/snaphu_exported_product.data/
total 208320
drwxr-xr-x 2 root root 4096 Dec 6 10:36 vector_data/
drwxr-xr-x 2 root root 4096 Dec 6 09:45 tie_point_grids/
-rw-r–r-- 1 root root 106645032 Dec 6 10:36 Phase_ifg_IW2_VV_29Dec2021_10Jan2022.img
-rw-r–r-- 1 root root 293 Dec 6 10:36 Phase_ifg_IW2_VV_29Dec2021_10Jan2022.hdr
-rw-r–r-- 1 root root 106645032 Dec 6 10:36 coh_IW2_VV_29Dec2021_10Jan2022.img
-rw-r–r-- 1 root root 301 Dec 6 10:36 coh_IW2_VV_29Dec2021_10Jan2022.hdr
drwxrwxr-x 3 root root 4096 Dec 6 10:36 …/
drwxr-xr-x 4 root root 4096 Dec 6 09:45 ./
(DInSAR) administrator@g220:/data/wangfengmao_file/aipy/py/test/py$

Hi Wangfengmao,

I had the same problem. Snappy does not communicate well with snap to export.

My solution is to use SNAP’s gpt command line.

path_SE = os.path.join(path, ‘snaphu_export’) " #folder where you are working
stat_cost_mode = “DEFO”
init_method = “MCF”
tile_rows = 10
tile_cols = 10
n_processors = 4
overlap_row = 2
overlap_col = 2
cost_threshold = 500

#path_filtered is the path to your .dim file, for example: C:\Users\xxx\filtered.dim

os.system(“gpt SnaphuExport -Ssource=” + str(path_filtered) +
" -PtargetFolder=" + str(path_SE) +
" -PstatCostMode=" + str(stat_cost_mode) +
" -PinitMethod=" + str(init_method) +
" -PnumberOfTileRows=“+str(tile_rows) +
" -PnumberOfTileCols=”+str(tile_cols) +
" -PnumberOfProcessors=“+str(n_processors) +
" -ProwOverlap=”+str(overlap_row) +
" -PcolOverlap=“+str(overlap_col) +
" -PtileCostThreshold=”+str(cost_threshold) )

#0, exported
#1, failed export

Hello!
Really interisting code. I’m also having problems with snapu export because the taget folder doesn’t contain anything, not even the .conf file.
Did it work for you in the end? Could you share the code?
Thanks a lot,
Beatriz

Hi @beatrizperes.

How are you doing snapu export? from snap, gpt command line or API?

Regards

Hello @JJeri!
Over API and command line it doesn’t work (it creates the target folder but no file image). Using SNAP I can only do it after changing the user profile variable (like in the picture). I wonder if can not do it with API and command line because of this…
Any help apreciated :slight_smile:

Hi @beatrizperes ,

I had the same problem, I don’t know why snaphu doesn’t connect good with Snap over API.

My configuration for snaphu is as follows:

Snaphu.exe is the same folter as my working folfer. So you should install snaphu in the working folder.

My code is:

Export snaphu
path_SE = os.path.join(path, ‘snaphu_export’) " #folder where you are working
stat_cost_mode = “DEFO”
init_method = “MCF”
tile_rows = 10
tile_cols = 10
n_processors = 4
overlap_row = 2
overlap_col = 2
cost_threshold = 500

#path_filtered is the path to your .dim file, for example: C:\Users\xxx\filtered.dim

os.system(“gpt SnaphuExport -Ssource=” + str(path_filtered) +
" -PtargetFolder=" + str(path_SE) +
" -PstatCostMode=" + str(stat_cost_mode) +
" -PinitMethod=" + str(init_method) +
" -PnumberOfTileRows=“+str(tile_rows) +
" -PnumberOfTileCols=”+str(tile_cols) +
" -PnumberOfProcessors=“+str(n_processors) +
" -ProwOverlap=”+str(overlap_row) +
" -PcolOverlap=“+str(overlap_col) +
" -PtileCostThreshold=”+str(cost_threshold) )

#0, exported
#1, failed export

Unwrapping snaphu Here I work with SNAP command line → gpt (Graph Processing Tool)
path_snaphu = os.path.join(path_SE, ‘filtered’) # See the first line in export snaphu

infile = os.path.join(path_SE, “filtered\snaphu.conf”)
with open(str(infile)) as lines:
line = lines.readlines()[6]
snaphu_string = line[1:].strip()
snaphu_args = snaphu_string.split()
process = subprocess.run(snaphu_string, shell=True , cwd= path_snaphu)
print(‘done’)

Import snaphu
unwrapped_list = glob.glob(str(path_snaphu) + “/UnwPhase*.hdr”) # Generates the path to the .hdr file

unwrapped_hdr = str(unwrapped_list[0])

unwrapped_read = ProductIO.readProduct(unwrapped_hdr)

snaphu_files = jpy.array(‘org.esa.snap.core.datamodel.Product’, 2)

snaphu_files[0] = filtered_product

snaphu_files[1] = unwrapped_read

result_SI = GPF.createProduct(“SnaphuImport”, parameters, snaphu_files)

path_import = os.path.join(path, ‘snaphu_import’)

ProductIO.writeProduct(result_SI, path_import, ‘BEAM-DIMAP’)

I hope the code will help you. Any question tell me.

Regards.