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$