import os
import esa_snappy
from esa_snappy import ProductIO, GPF, HashMap, jpy
import subprocess
获取 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:
return ProductIO.readProduct(product_path)
except Exception as e:
print(f"读取产品时出错: {e}")
exit(1)
应用 Multi-looking
def apply_multilook(product):
try:
parameters = HashMap()
parameters.put(‘nRgLooks’, Integer(4)) # Range方向上的looks数量
parameters.put(‘nAzLooks’, Integer(1)) # Azimuth方向上的looks数量
parameters.put(‘grSquarePixel’, True) # 是否使用正方形像素
parameters.put(‘outputIntensity’, False) # 是否输出强度信息(如果不需要设置为False)
return GPF.createProduct('Multilook', parameters, product)
except Exception as e:
print(f"应用 Multi-looking 时出错: {e}")
exit(1)
应用 Goldstein Phase Filtering
def apply_goldstein_phase_filtering(product):
try:
parameters = HashMap()
parameters.put(‘adaptiveFilterExponent’, Double(1.0))
parameters.put(‘fftSize’, Integer(64))
parameters.put(‘windowSize’, Integer(3))
parameters.put(‘coherenceThreshold’, Double(0.2))
return GPF.createProduct('GoldsteinPhaseFiltering', parameters, product)
except Exception as e:
print(f"应用 Goldstein Phase Filtering 时出错: {e}")
exit(1)
应用 Snaphu 导出
def apply_snaphu_export(product, export_path):
try:
parameters = HashMap()
parameters.put(‘targetFolder’, export_path)
parameters.put(‘statisticalCostMode’, ‘DEFO’)
parameters.put(‘initialMethod’, ‘MCF’)
parameters.put(‘numberOfTileRows’, Integer(10))
parameters.put(‘numberOfTileCols’, Integer(10))
parameters.put(‘numberOfProcessors’, Integer(4))
parameters.put(‘rowOverlap’, Integer(100))
parameters.put(‘colOverlap’, Integer(100))
parameters.put(‘tileCostThreshold’, Integer(500))
return GPF.createProduct('SnaphuExport', parameters, product)
except Exception as e:
print(f"应用 Snaphu Export 时出错: {e}")
exit(1)
解缠操作
def run_snaphu_unwrapping(snaphu_data_path):
try:
# 构造 Snaphu 解缠命令
# 假设你已经有合适的 Snaphu 配置文件 (.cfg),也可以根据需要手动设置
snaphu_cmd = f"snaphu {snaphu_data_path}/coh_IW2_VV_29Dec2021_10Jan2022.img {snaphu_data_path}/Phase_ifg_IW2_VV_29Dec2021_10Jan2022.img"
# 运行解缠命令
subprocess.run(snaphu_cmd, shell=True, check=True)
print("解缠操作完成")
except subprocess.CalledProcessError as e:
print(f"解缠操作出错: {e}")
exit(1)
保存产品
def save_product(product, output_path):
try:
if os.path.exists(output_path):
os.remove(output_path)
ProductIO.writeProduct(product, output_path, ‘BEAM-DIMAP’)
except Exception as e:
print(f"保存产品时出错: {e}")
exit(1)
主函数 - 处理干涉图产品
def process_multilook(interferogram_product_path, output_path, temp_path):
try:
# 读取干涉图产品
interferogram_product = read_product(interferogram_product_path)
# 应用 Multi-looking
multilooked_product = apply_multilook(interferogram_product)
multilooked_output_file_path = os.path.join(output_path, os.path.basename(interferogram_product_path).replace('.dim', '_multilook.dim'))
save_product(multilooked_product, multilooked_output_file_path)
print(f"Multi-looking 处理完成,结果已保存到 {multilooked_output_file_path}")
# 应用 Goldstein Phase Filtering
goldstein_product = apply_goldstein_phase_filtering(multilooked_product)
goldstein_output_file_path = os.path.join(output_path, os.path.basename(interferogram_product_path).replace('.dim', '_goldstein.dim'))
save_product(goldstein_product, goldstein_output_file_path)
print(f"Goldstein Phase Filtering 处理完成,结果已保存到 {goldstein_output_file_path}")
# 应用 Snaphu 导出
snaphu_exported_product = apply_snaphu_export(goldstein_product, temp_path)
snaphu_export_file_path = os.path.join(temp_path, os.path.basename(interferogram_product_path).replace('.dim', '_snaphuExport.dim'))
save_product(snaphu_exported_product, snaphu_export_file_path)
print(f"Snaphu Export 处理完成,结果已保存到 {snaphu_export_file_path}")
# 执行解缠操作
run_snaphu_unwrapping(os.path.join(temp_path, os.path.basename(interferogram_product_path).replace('.dim', '_snaphuExport.data')))
except Exception as e:
print(f"处理 Multi-looking 或 Goldstein Phase Filtering 或 Snaphu Export 时出错: {e}")
exit(1)
调用示例
if name == “main”:
# 输入干涉图产品路径(这个路径是你生成干涉图的结果路径)
interferogram_product_path = ‘/data/wangfengmao_file/aipy/output/S1A_IW_SLC__1SDV_20220110T231926_20220110T231953_041405_04EC57_103E_interferogram.dim’
# 输出路径(保存处理后的Multi-looking和Goldstein Phase Filtering产品)
output_path = '/data/wangfengmao_file/aipy/output'
# 临时路径(保存中间处理结果:Snaphu Export)
temp_path = '/data/wangfengmao_file/aipy/temp_path'
# 运行 Multi-looking、Goldstein Phase Filtering 和 Snaphu Export 处理
process_multilook(interferogram_product_path, output_path, temp_path)
NFO: org.esa.snap.core.util.EngineVersionCheckActivator: Please check regularly for new updates for the best SNAP experience.
100% done.
Multi-looking 处理完成,结果已保存到 /data/wangfengmao_file/aipy/output/S1A_IW_SLC__1SDV_20220110T231926_20220110T231953_041405_04EC57_103E_interferogram_multilook.dim
100% done.
Goldstein Phase Filtering 处理完成,结果已保存到 /data/wangfengmao_file/aipy/output/S1A_IW_SLC__1SDV_20220110T231926_20220110T231953_041405_04EC57_103E_interferogram_goldstein.dim
100% done.
Snaphu Export 处理完成,结果已保存到 /data/wangfengmao_file/aipy/temp_path/S1A_IW_SLC__1SDV_20220110T231926_20220110T231953_041405_04EC57_103E_interferogram_snaphuExport.dim
snaphu v2.0.3
line length must be positive integer
解缠操作出错: Command ‘snaphu /data/wangfengmao_file/aipy/temp_path/S1A_IW_SLC__1SDV_20220110T231926_20220110T231953_041405_04EC57_103E_interferogram_snaphuExport.data/coh_IW2_VV_29Dec2021_10Jan2022.img /data/wangfengmao_file/aipy/temp_path/S1A_IW_SLC__1SDV_20220110T231926_20220110T231953_041405_04EC57_103E_interferogram_snaphuExport.data/Phase_ifg_IW2_VV_29Dec2021_10Jan2022.img’ returned non-zero exit status 1.
(DInSAR) administrator@g220:/data/wangfengmao_file/aipy/py$
(DInSAR) administrator@g220:/data/wangfengmao_file/aipy/py$ ll -R /data/wangfengmao_file/aipy/temp_path/
/data/wangfengmao_file/aipy/temp_path/:
total 2620
drwxrwxr-x 3 administrator administrator 4096 Dec 4 11:42 ./
drwxrwxr-x 9 administrator administrator 4096 Dec 3 20:24 …/
drwxrwxr-x 4 administrator administrator 4096 Dec 4 10:56 S1A_IW_SLC__1SDV_20220110T231926_20220110T231953_041405_04EC57_103E_interferogram_snaphuExport.data/
-rw-rw-r-- 1 administrator administrator 2669491 Dec 4 11:42 S1A_IW_SLC__1SDV_20220110T231926_20220110T231953_041405_04EC57_103E_interferogram_snaphuExport.dim
/data/wangfengmao_file/aipy/temp_path/S1A_IW_SLC__1SDV_20220110T231926_20220110T231953_041405_04EC57_103E_interferogram_snaphuExport.data:
total 208320
drwxrwxr-x 4 administrator administrator 4096 Dec 4 10:56 ./
drwxrwxr-x 3 administrator administrator 4096 Dec 4 11:42 …/
-rw-rw-r-- 1 administrator administrator 301 Dec 4 11:42 coh_IW2_VV_29Dec2021_10Jan2022.hdr
-rw-rw-r-- 1 administrator administrator 106645032 Dec 4 11:42 coh_IW2_VV_29Dec2021_10Jan2022.img
-rw-rw-r-- 1 administrator administrator 293 Dec 4 11:42 Phase_ifg_IW2_VV_29Dec2021_10Jan2022.hdr
-rw-rw-r-- 1 administrator administrator 106645032 Dec 4 11:42 Phase_ifg_IW2_VV_29Dec2021_10Jan2022.img
drwxrwxr-x 2 administrator administrator 4096 Dec 4 10:56 tie_point_grids/
drwxrwxr-x 2 administrator administrator 4096 Dec 4 11:42 vector_data/
/data/wangfengmao_file/aipy/temp_path/S1A_IW_SLC__1SDV_20220110T231926_20220110T231953_041405_04EC57_103E_interferogram_snaphuExport.data/tie_point_grids:
total 40
drwxrwxr-x 2 administrator administrator 4096 Dec 4 10:56 ./
drwxrwxr-x 4 administrator administrator 4096 Dec 4 10:56 …/
-rw-rw-r-- 1 administrator administrator 273 Dec 4 11:42 incident_angle.hdr
-rw-rw-r-- 1 administrator administrator 484 Dec 4 11:42 incident_angle.img
-rw-rw-r-- 1 administrator administrator 267 Dec 4 11:42 latitude.hdr
-rw-rw-r-- 1 administrator administrator 484 Dec 4 11:42 latitude.img
-rw-rw-r-- 1 administrator administrator 268 Dec 4 11:42 longitude.hdr
-rw-rw-r-- 1 administrator administrator 484 Dec 4 11:42 longitude.img
-rw-rw-r-- 1 administrator administrator 275 Dec 4 11:42 slant_range_time.hdr
-rw-rw-r-- 1 administrator administrator 484 Dec 4 11:42 slant_range_time.img
/data/wangfengmao_file/aipy/temp_path/S1A_IW_SLC__1SDV_20220110T231926_20220110T231953_041405_04EC57_103E_interferogram_snaphuExport.data/vector_data:
total 16
drwxrwxr-x 2 administrator administrator 4096 Dec 4 11:42 ./
drwxrwxr-x 4 administrator administrator 4096 Dec 4 10:56 …/
-rw-rw-r-- 1 administrator administrator 209 Dec 4 11:42 ground_control_points.csv
-rw-rw-r-- 1 administrator administrator 225 Dec 4 11:42 pins.csv
(DInSAR) administrator@g220:/data/wangfengmao_file/aipy/py$