393 lines
13 KiB
Python
393 lines
13 KiB
Python
import logging
|
||
import os
|
||
import shutil
|
||
import subprocess
|
||
|
||
logger = logging.getLogger("mylog")
|
||
|
||
|
||
class SurfaceInversionDubois:
|
||
"""
|
||
调用polsarpro4.2.0的surface_inversion_dubois.exe做土壤水分反演
|
||
"""
|
||
|
||
def __init__(self, exeFilterName='surface_inversion_dubois.exe'):
|
||
self.__exeName = exeFilterName
|
||
pass
|
||
|
||
def api_surface_inversion_dubois(
|
||
self,
|
||
exeDir,
|
||
inT3Dir,
|
||
outDir,
|
||
incidence,
|
||
rectX,
|
||
rectY,
|
||
row,
|
||
col,
|
||
frequency, # GHZ
|
||
angleFlag, # 0:deg, 1:rad
|
||
):
|
||
"""
|
||
:param exeDir:exe所在目录
|
||
:param inT3Dir:T3矩阵目录
|
||
:param outDir:输出目录
|
||
:param rectX:有效区域x
|
||
:param rectY:有效区域y
|
||
:param rectWidth:有效区域宽
|
||
:param rectHeight:有效区域高
|
||
:param Nwin:滤波窗口大小 3 5 7 9 11
|
||
:param Nlook:一般是1
|
||
"""
|
||
if len(exeDir) == 0:
|
||
if not os.path.exists(self.__exeName):
|
||
raise Exception(self.__exeName + ' not exists.')
|
||
exePath = self.__exeName
|
||
else:
|
||
if not os.path.exists(exeDir + '\\' + self.__exeName):
|
||
raise Exception(
|
||
exeDir +
|
||
'\\' +
|
||
self.__exeName +
|
||
' not exists.')
|
||
exePath = exeDir + '\\' + self.__exeName
|
||
|
||
if not self._checkT3Matrix(inT3Dir):
|
||
raise Exception('T3 Matrix check failed.')
|
||
if not os.path.exists(outDir):
|
||
os.makedirs(outDir)
|
||
|
||
Off_lig = rectX
|
||
Off_col = rectY
|
||
Sub_Nlig = row
|
||
Sub_Ncol = col
|
||
dataFormat = 'T3'
|
||
calibration_flag = 1
|
||
calibration_coefficient = 0.0
|
||
threshold_HHHH_VVVV = 0.0
|
||
threshold_HVHV_VVVV = 0.0
|
||
|
||
para_list = [
|
||
exePath,
|
||
inT3Dir,
|
||
outDir,
|
||
dataFormat,
|
||
incidence,
|
||
Off_lig,
|
||
Off_col,
|
||
Sub_Nlig,
|
||
Sub_Ncol,
|
||
frequency, # GHZ
|
||
angleFlag,
|
||
]
|
||
|
||
cmd = "surface_inversion_dubois.exe -id {} -od {} -iodf {} -ang {} -ofr {} -ofc {} -fnr {} -fnc {} -fr {} -un {} -caf {} -cac {} -th1 {} -th2 {}".format(
|
||
inT3Dir, outDir, dataFormat, incidence, Off_lig, Off_col, Sub_Nlig, Sub_Ncol, frequency, angleFlag,
|
||
calibration_flag, calibration_coefficient, threshold_HHHH_VVVV, threshold_HVHV_VVVV)
|
||
|
||
logger.info('surface_inversion_dubois:{}'.format(cmd))
|
||
result = os.system(cmd)
|
||
logger.info('cmd_result:{}'.format(result))
|
||
logger.info('surface_inversion_dubois finish!')
|
||
|
||
config_path = os.path.join(inT3Dir, 'config.txt')
|
||
shutil.copyfile(config_path, os.path.join(outDir, 'config.txt'))
|
||
|
||
# cmd = ' '.join(str(i) for i in para_list)
|
||
# config_path = os.path.join(inT3Dir, 'config.txt')
|
||
# shutil.copyfile(config_path, os.path.join(outDir, 'config.txt'))
|
||
# result_tuple = subprocess.getstatusoutput(cmd)
|
||
#
|
||
# if result_tuple[0] != 1 or result_tuple[1].find('error') != -1:
|
||
# raise Exception(result_tuple[1])
|
||
@staticmethod
|
||
def _checkT3Matrix(T3Dir):
|
||
# 检测T3矩阵
|
||
if not os.path.exists(T3Dir):
|
||
return False
|
||
file_name_in_out = ['T11.bin', 'T12_real.bin', 'T12_imag.bin',
|
||
'T13_real.bin', 'T13_imag.bin', 'T22.bin',
|
||
'T23_real.bin', 'T23_imag.bin', 'T33.bin']
|
||
for item in file_name_in_out:
|
||
if not os.path.exists(T3Dir + "\\" + item):
|
||
return False
|
||
return True
|
||
|
||
|
||
class SurfaceInversionHisto:
|
||
"""
|
||
调用polsarpro4.2.0的surface_inversion_histo.exe做土壤水分反演
|
||
"""
|
||
|
||
def __init__(self, exeFilterName='surface_inversion_histo.exe'):
|
||
self.__exeName = exeFilterName
|
||
pass
|
||
|
||
def api_surface_inversion_histo(
|
||
self,
|
||
exeDir,
|
||
inT3Dir,
|
||
outDir,
|
||
rectX,
|
||
rectY,
|
||
rectWidth,
|
||
rectHeight,
|
||
Nwin=7,
|
||
Nlook=1):
|
||
"""
|
||
:param exeDir:exe所在目录
|
||
:param inT3Dir:T3矩阵目录
|
||
:param outDir:输出目录
|
||
:param rectX:有效区域x
|
||
:param rectY:有效区域y
|
||
:param rectWidth:有效区域宽
|
||
:param rectHeight:有效区域高
|
||
:param Nwin:滤波窗口大小 3 5 7 9 11
|
||
:param Nlook:一般是1
|
||
"""
|
||
if len(exeDir) == 0:
|
||
if not os.path.exists(self.__exeName):
|
||
raise Exception(self.__exeName + ' not exists.')
|
||
exePath = self.__exeName
|
||
else:
|
||
if not os.path.exists(exeDir + '\\' + self.__exeName):
|
||
raise Exception(
|
||
exeDir +
|
||
'\\' +
|
||
self.__exeName +
|
||
' not exists.')
|
||
exePath = exeDir + '\\' + self.__exeName
|
||
|
||
if not self._checkT3Matrix(inT3Dir):
|
||
raise Exception('T3 Matrix check failed.')
|
||
if not os.path.exists(outDir):
|
||
os.makedirs(outDir)
|
||
if (Nwin % 2) == 0 or Nwin < 0: # 如果为偶数或小于0,则使用默认值
|
||
Nwin = 7
|
||
|
||
Off_lig = rectX
|
||
Off_col = rectY
|
||
Sub_Nlig = rectWidth
|
||
Sub_Ncol = rectHeight
|
||
|
||
para_list = [
|
||
exePath,
|
||
inT3Dir,
|
||
outDir,
|
||
Nlook,
|
||
Nwin,
|
||
Off_lig,
|
||
Off_col,
|
||
Sub_Nlig,
|
||
Sub_Ncol]
|
||
cmd = ' '.join(str(i) for i in para_list)
|
||
config_path = os.path.join(inT3Dir, 'config.txt')
|
||
shutil.copyfile(config_path, os.path.join(outDir, 'config.txt'))
|
||
result_tuple = subprocess.getstatusoutput(cmd)
|
||
|
||
if result_tuple[0] != 1 or result_tuple[1].find('error') != -1:
|
||
raise Exception(result_tuple[1])
|
||
@staticmethod
|
||
def _checkT3Matrix(T3Dir):
|
||
# 检测T3矩阵
|
||
if not os.path.exists(T3Dir):
|
||
return False
|
||
file_name_in_out = ['T11.bin', 'T12_real.bin', 'T12_imag.bin',
|
||
'T13_real.bin', 'T13_imag.bin', 'T22.bin',
|
||
'T23_real.bin', 'T23_imag.bin', 'T33.bin']
|
||
for item in file_name_in_out:
|
||
if not os.path.exists(T3Dir + "\\" + item):
|
||
return False
|
||
return True
|
||
|
||
|
||
class SurfaceInversionOh:
|
||
"""
|
||
调用polsarpro4.2.0的surface_inversion_oh.exe做土壤水分反演
|
||
"""
|
||
|
||
def __init__(self, exeFilterName='surface_inversion_oh.exe'):
|
||
self.__exeName = exeFilterName
|
||
pass
|
||
|
||
def api_surface_inversion_oh(
|
||
self,
|
||
exeDir,
|
||
inT3Dir,
|
||
outDir,
|
||
rectX,
|
||
rectY,
|
||
rectWidth,
|
||
rectHeight,
|
||
Nwin=7,
|
||
Nlook=1):
|
||
"""
|
||
:param exeDir:exe所在目录
|
||
:param inT3Dir:T3矩阵目录
|
||
:param outDir:输出目录
|
||
:param rectX:有效区域x
|
||
:param rectY:有效区域y
|
||
:param rectWidth:有效区域宽
|
||
:param rectHeight:有效区域高
|
||
:param Nwin:滤波窗口大小 3 5 7 9 11
|
||
:param Nlook:一般是1
|
||
"""
|
||
if len(exeDir) == 0:
|
||
if not os.path.exists(self.__exeName):
|
||
raise Exception(self.__exeName + ' not exists.')
|
||
exePath = self.__exeName
|
||
else:
|
||
if not os.path.exists(exeDir + '\\' + self.__exeName):
|
||
raise Exception(
|
||
exeDir +
|
||
'\\' +
|
||
self.__exeName +
|
||
' not exists.')
|
||
exePath = exeDir + '\\' + self.__exeName
|
||
|
||
if not self._checkT3Matrix(inT3Dir):
|
||
raise Exception('T3 Matrix check failed.')
|
||
if not os.path.exists(outDir):
|
||
os.makedirs(outDir)
|
||
if (Nwin % 2) == 0 or Nwin < 0: # 如果为偶数或小于0,则使用默认值
|
||
Nwin = 7
|
||
|
||
Off_lig = rectX
|
||
Off_col = rectY
|
||
Sub_Nlig = rectWidth
|
||
Sub_Ncol = rectHeight
|
||
|
||
para_list = [
|
||
exePath,
|
||
inT3Dir,
|
||
outDir,
|
||
Nlook,
|
||
Nwin,
|
||
Off_lig,
|
||
Off_col,
|
||
Sub_Nlig,
|
||
Sub_Ncol]
|
||
cmd = ' '.join(str(i) for i in para_list)
|
||
config_path = os.path.join(inT3Dir, 'config.txt')
|
||
shutil.copyfile(config_path, os.path.join(outDir, 'config.txt'))
|
||
result_tuple = subprocess.getstatusoutput(cmd)
|
||
|
||
if result_tuple[0] != 1 or result_tuple[1].find('error') != -1:
|
||
raise Exception(result_tuple[1])
|
||
@staticmethod
|
||
def _checkT3Matrix(T3Dir):
|
||
# 检测T3矩阵
|
||
if not os.path.exists(T3Dir):
|
||
return False
|
||
file_name_in_out = ['T11.bin', 'T12_real.bin', 'T12_imag.bin',
|
||
'T13_real.bin', 'T13_imag.bin', 'T22.bin',
|
||
'T23_real.bin', 'T23_imag.bin', 'T33.bin']
|
||
for item in file_name_in_out:
|
||
if not os.path.exists(T3Dir + "\\" + item):
|
||
return False
|
||
return True
|
||
|
||
|
||
class SurfaceInversionOh2004:
|
||
"""
|
||
调用polsarpro4.2.0的surface_inversion_oh2004.exe做土壤水分反演
|
||
"""
|
||
|
||
def __init__(self, exeFilterName='surface_inversion_oh2004.exe'):
|
||
self.__exeName = exeFilterName
|
||
pass
|
||
|
||
def api_surface_inversion_oh2004(
|
||
self,
|
||
exeDir,
|
||
inT3Dir,
|
||
outDir,
|
||
incidence,
|
||
rectY,
|
||
rectX,
|
||
row,
|
||
col,
|
||
frequency, # GHZ
|
||
angleFlag):
|
||
"""
|
||
:param exeDir:exe所在目录
|
||
:param inT3Dir:T3矩阵目录
|
||
:param outDir:输出目录
|
||
:param rectX:有效区域x
|
||
:param rectY:有效区域y
|
||
:param rectWidth:有效区域宽
|
||
:param rectHeight:有效区域高
|
||
:param Nwin:滤波窗口大小 3 5 7 9 11
|
||
:param Nlook:一般是1
|
||
"""
|
||
if len(exeDir) == 0:
|
||
if not os.path.exists(self.__exeName):
|
||
raise Exception(self.__exeName + ' not exists.')
|
||
exePath = self.__exeName
|
||
else:
|
||
if not os.path.exists(exeDir + '\\' + self.__exeName):
|
||
raise Exception(
|
||
exeDir +
|
||
'\\' +
|
||
self.__exeName +
|
||
' not exists.')
|
||
exePath = exeDir + '\\' + self.__exeName
|
||
|
||
if not self._checkT3Matrix(inT3Dir):
|
||
raise Exception('T3 Matrix check failed.')
|
||
if not os.path.exists(outDir):
|
||
os.makedirs(outDir)
|
||
|
||
Off_lig = rectX
|
||
Off_col = rectY
|
||
Sub_Nlig = row
|
||
Sub_Ncol = col
|
||
dataFormat = 'T3'
|
||
threshold_mv = 1.0
|
||
threshold_s = 7.0
|
||
|
||
para_list = [
|
||
exePath,
|
||
inT3Dir,
|
||
outDir,
|
||
dataFormat,
|
||
incidence,
|
||
Off_lig,
|
||
Off_col,
|
||
Sub_Nlig,
|
||
Sub_Ncol,
|
||
frequency, # GHZ
|
||
angleFlag,
|
||
threshold_mv,
|
||
threshold_s]
|
||
cmd = "surface_inversion_oh2004.exe -id {} -od {} -iodf {} -ang {} -ofr {} -ofc {} -fnr {} -fnc {} -fr {} -un {} -th1 {} -th2 {}".format(
|
||
inT3Dir, outDir, dataFormat, incidence, Off_lig, Off_col, Sub_Nlig, Sub_Ncol, frequency, angleFlag, threshold_mv, threshold_s)
|
||
|
||
logger.info('surface_inversion_oh2004:{}'.format(cmd))
|
||
result = os.system(cmd)
|
||
logger.info('cmd_result:{}'.format(result))
|
||
logger.info('surface_inversion_oh2004 finish!')
|
||
|
||
|
||
config_path = os.path.join(inT3Dir, 'config.txt')
|
||
shutil.copyfile(config_path, os.path.join(outDir, 'config.txt'))
|
||
# cmd = ' '.join(str(i) for i in para_list)
|
||
# result_tuple = subprocess.getstatusoutput(cmd)
|
||
# #
|
||
# if result_tuple[0] != 1 or result_tuple[1].find('error') != -1:
|
||
# raise Exception(result_tuple[1])
|
||
@staticmethod
|
||
def _checkT3Matrix(T3Dir):
|
||
# 检测T3矩阵
|
||
if not os.path.exists(T3Dir):
|
||
return False
|
||
file_name_in_out = ['T11.bin', 'T12_real.bin', 'T12_imag.bin',
|
||
'T13_real.bin', 'T13_imag.bin', 'T22.bin',
|
||
'T23_real.bin', 'T23_imag.bin', 'T33.bin']
|
||
# file_name_in_out = ['T11.img', 'T12_real.img', 'T12_imag.img',
|
||
# 'T13_real.img', 'T13_imag.img', 'T22.img',
|
||
# 'T23_real.img', 'T23_imag.img', 'T33.img']
|
||
for item in file_name_in_out:
|
||
if not os.path.exists(T3Dir + "\\" + item):
|
||
return False
|
||
return True |