1、支持无缓冲区输出;

main
simonyan 11 months ago
parent d3a7d30e80
commit 53217400ec

@ -5,6 +5,15 @@ import json
import mainMapEcharts import mainMapEcharts
import shutil import shutil
# 获取当前文件的父目录的绝对路径
current_dir = os.path.dirname(os.path.abspath(__file__))
# 获取父目录的绝对路径
parent_dir = os.path.abspath(os.path.join(current_dir, os.pardir))
# 将父目录添加到 Python 模块搜索路径中
sys.path.append(parent_dir)
from Utils import utils as globalUtils
# python.exe .\main.py E:\01-Projects\01-CAE-FullLink\output\workingDir\test1\PlatformPerformance\a_sat.txt E:\01-Projects\01-CAE-FullLink\output\workingDir\test2\GeometricSightTracking\output # python.exe .\main.py E:\01-Projects\01-CAE-FullLink\output\workingDir\test1\PlatformPerformance\a_sat.txt E:\01-Projects\01-CAE-FullLink\output\workingDir\test2\GeometricSightTracking\output
outputDir = "output" outputDir = "output"
@ -31,7 +40,6 @@ def readInputGeo():
def genParamList(filePath: str): def genParamList(filePath: str):
paramList = [] paramList = []
with open(filePath, "r", encoding="utf-8") as file: with open(filePath, "r", encoding="utf-8") as file:
content = ""
for line in file: for line in file:
paramList.append(line.split()[1:]) paramList.append(line.split()[1:])
return paramList return paramList
@ -40,8 +48,8 @@ def genParamList(filePath: str):
# 生成一个求解参数文件 # 生成一个求解参数文件
def genParamFile(paramVector: list, geoParamNameList: list, geoParamValueList: list): def genParamFile(paramVector: list, geoParamNameList: list, geoParamValueList: list):
paramValueList = [geoParamValueList[0]] + paramVector + geoParamValueList[1:] paramValueList = [geoParamValueList[0]] + paramVector + geoParamValueList[1:]
print(paramValueList) globalUtils.printWithFlush(paramValueList)
print(geoParamNameList) globalUtils.printWithFlush(geoParamNameList)
with open("input.txt", "w") as file: with open("input.txt", "w") as file:
file.write(geoParamNameList) file.write(geoParamNameList)
@ -128,7 +136,7 @@ if __name__ == "__main__":
main() main()
except Exception as e: except Exception as e:
# 捕获除了ZeroDivisionError外的所有异常 # 捕获除了ZeroDivisionError外的所有异常
print("发生异常:", str(e)) globalUtils.printWithFlush("发生异常:", str(e))
finally: finally:
# 无论是否发生异常,最终都会执行该代码块 # 无论是否发生异常,最终都会执行该代码块
print("求解执行完毕!") globalUtils.printWithFlush("求解执行完毕!")

@ -35,8 +35,8 @@ def readInput() -> list:
lines = file.readlines() lines = file.readlines()
paramNameList = lines[0] paramNameList = lines[0]
publicParamValueList = [int(x) for x in lines[1].split(",")] publicParamValueList = [int(x) for x in lines[1].split(",")]
print(paramNameList) globalUtils.printWithFlush(paramNameList)
print(publicParamValueList) globalUtils.printWithFlush(publicParamValueList)
# 依次读取 Lat、Lon、SA、SZ、VA、VZ # 依次读取 Lat、Lon、SA、SZ、VA、VZ
latFilePath = inputDir + "Lat.txt" latFilePath = inputDir + "Lat.txt"
@ -57,12 +57,12 @@ def readInput() -> list:
vzFilePath = inputDir + "VZ.txt" vzFilePath = inputDir + "VZ.txt"
vzValueList = readParamValueList(vzFilePath) vzValueList = readParamValueList(vzFilePath)
print("经度参数个数 " + str(len(latValueList))) globalUtils.printWithFlush("经度参数个数 " + str(len(latValueList)))
print("纬度参数个数 " + str(len(lonValueList))) globalUtils.printWithFlush("纬度参数个数 " + str(len(lonValueList)))
print("太阳天顶角参数个数 " + str(len(saValueList))) globalUtils.printWithFlush("太阳天顶角参数个数 " + str(len(saValueList)))
print("观察天顶角参数个数 " + str(len(szValueList))) globalUtils.printWithFlush("观察天顶角参数个数 " + str(len(szValueList)))
print("太阳方位角参数个数 " + str(len(vaValueList))) globalUtils.printWithFlush("太阳方位角参数个数 " + str(len(vaValueList)))
print("观测方位角参数个数 " + str(len(vzValueList))) globalUtils.printWithFlush("观测方位角参数个数 " + str(len(vzValueList)))
if not globalUtils.are_equal( if not globalUtils.are_equal(
len(latValueList), len(latValueList),
@ -72,7 +72,7 @@ def readInput() -> list:
len(vaValueList), len(vaValueList),
len(vzValueList), len(vzValueList),
): ):
print( globalUtils.printWithFlush(
"经度、纬度、太阳天顶角、观察天顶角、太阳方位角、观测方位角的参数个数不一致,请查看参数文件。" "经度、纬度、太阳天顶角、观察天顶角、太阳方位角、观测方位角的参数个数不一致,请查看参数文件。"
) )
return [] return []
@ -98,7 +98,7 @@ def readInput() -> list:
publicParamValueList[2], publicParamValueList[2],
] ]
) )
# print(latValueList) # globalUtils.printWithFlush(latValueList)
# return [paramNameList, paramValueList.split(",")] # return [paramNameList, paramValueList.split(",")]
return [paramNameList, paramContentList] return [paramNameList, paramContentList]
@ -122,7 +122,7 @@ def copyInputFiles(projectDir: str) -> bool:
for inputFile in inputFileList: for inputFile in inputFileList:
inputFileProjectPath = os.path.join(projectDir, inputFile) inputFileProjectPath = os.path.join(projectDir, inputFile)
if not os.path.isfile(inputFileProjectPath): if not os.path.isfile(inputFileProjectPath):
print(f"{inputFileProjectPath} is not file or exist") globalUtils.printWithFlush(f"{inputFileProjectPath} is not file or exist")
return False return False
inputFileSolverPath = os.path.join(inputDir, inputFile) inputFileSolverPath = os.path.join(inputDir, inputFile)
shutil.copy(inputFileProjectPath, inputFileSolverPath) shutil.copy(inputFileProjectPath, inputFileSolverPath)
@ -181,7 +181,7 @@ def toaRunModel(surFileDir: str):
def test(testDir: str): def test(testDir: str):
currentExeDir = os.getcwd() currentExeDir = os.getcwd()
print("execute dir: ", currentExeDir) globalUtils.printWithFlush("execute dir: ", currentExeDir)
# 获取当前文件的绝对路径 # 获取当前文件的绝对路径
currentFile = os.path.realpath(__file__) currentFile = os.path.realpath(__file__)
@ -191,14 +191,14 @@ def test(testDir: str):
# 切换到'/home' 目录 # 切换到'/home' 目录
os.chdir(currentDir) os.chdir(currentDir)
print("surface dir: ", currentDir) globalUtils.printWithFlush("surface dir: ", currentDir)
inputFilePath = os.path.join(inputDistDir, "input.txt") inputFilePath = os.path.join(inputDistDir, "input.txt")
print("surface input.txt: ", inputFilePath) globalUtils.printWithFlush("surface input.txt: ", inputFilePath)
print(os.getcwd()) globalUtils.printWithFlush(os.getcwd())
# 执行一次求解 # 执行一次求解
result = toaRunModel(testDir) result = toaRunModel(testDir)
print("surface run ok:", result) globalUtils.printWithFlush("surface run ok:", result)
# 切换回之前的目录 # 切换回之前的目录
os.chdir(currentExeDir) os.chdir(currentExeDir)
@ -223,11 +223,11 @@ def generateImgPng():
def main(): def main():
# 项目目录的output求解结束后传回到项目目录下 # 项目目录的output求解结束后传回到项目目录下
projectDir = sys.argv[1] projectDir = sys.argv[1]
print(projectDir) globalUtils.printWithFlush(projectDir)
# 从项目目录下拷贝参数文件过来 # 从项目目录下拷贝参数文件过来
if not copyInputFiles(projectDir): if not copyInputFiles(projectDir):
print("参数文件不完整") globalUtils.printWithFlush("参数文件不完整")
return return
# 读取参数文件 # 读取参数文件
@ -269,7 +269,7 @@ def main():
) )
runTimestamp = time.time() runTimestamp = time.time()
print(result, index, runTimestamp - timestamp) globalUtils.printWithFlush(result, index, runTimestamp - timestamp)
timestamp = runTimestamp timestamp = runTimestamp
# 生成后处理图片 # 生成后处理图片
@ -292,7 +292,7 @@ if __name__ == "__main__":
main() main()
except Exception as e: except Exception as e:
# 捕获除了ZeroDivisionError外的所有异常 # 捕获除了ZeroDivisionError外的所有异常
print("发生异常:", str(e)) globalUtils.printWithFlush("发生异常:", str(e))
finally: finally:
# 无论是否发生异常,最终都会执行该代码块 # 无论是否发生异常,最终都会执行该代码块
print("求解执行完毕!") globalUtils.printWithFlush("求解执行完毕!")

@ -328,9 +328,9 @@ def main():
# 读取 大气参数文件input_toa.txt获取公共的参数aodmodel、atmodel # 读取 大气参数文件input_toa.txt获取公共的参数aodmodel、atmodel
toaAodmodel, toaAtmmodel = utils.readInputTOA(toaInputFilePath) toaAodmodel, toaAtmmodel = utils.readInputTOA(toaInputFilePath)
print(toaAodmodel, toaAtmmodel) globalUtils.printWithFlush(toaAodmodel, toaAtmmodel)
if None in [surMonth, surWavestart, surWaveend, toaAodmodel, toaAtmmodel]: if None in [surMonth, surWavestart, surWaveend, toaAodmodel, toaAtmmodel]:
print("surface: month wavestart waveend and toa: aodmodel atmodel has None!") globalUtils.printWithFlush("surface: month wavestart waveend and toa: aodmodel atmodel has None!")
return return
# 读取地表文件的经纬度 # 读取地表文件的经纬度
@ -365,8 +365,8 @@ def main():
ncPointSelectedList, surPointSelectedList = surToNC( ncPointSelectedList, surPointSelectedList = surToNC(
ncLatList, ncLonList, surLatList, surLonList, distanceProximityDistance ncLatList, ncLonList, surLatList, surLonList, distanceProximityDistance
) )
# print('nc points: ', len(ncPointSelectedList)) # globalUtils.printWithFlush('nc points: ', len(ncPointSelectedList))
# print('sur points: ', len(surPointSelectedList)) # globalUtils.printWithFlush('sur points: ', len(surPointSelectedList))
# globalUtils.saveDataToJSON([(float(x[0]), float(x[1])) for x in ncPointSelectedList],'json-nc-point.json') # globalUtils.saveDataToJSON([(float(x[0]), float(x[1])) for x in ncPointSelectedList],'json-nc-point.json')
# globalUtils.saveDataToJSON([(float(x[0]), float(x[1])) for x in surPointSelectedList], 'json-sur-point.json') # globalUtils.saveDataToJSON([(float(x[0]), float(x[1])) for x in surPointSelectedList], 'json-sur-point.json')
@ -391,9 +391,9 @@ def main():
distanceProximityDistance, distanceProximityDistance,
) )
) )
# print('nc final points: ', len(ncPointFinalSelectedList)) # globalUtils.printWithFlush('nc final points: ', len(ncPointFinalSelectedList))
# print('sur final points: ', len(surPointFinalSelectedList)) # globalUtils.printWithFlush('sur final points: ', len(surPointFinalSelectedList))
# print('tif final points: ', len(tifPointFinalSelectedList)) # globalUtils.printWithFlush('tif final points: ', len(tifPointFinalSelectedList))
# globalUtils.saveDataToJSON(surPointFinalSelectedList,'json-point-sur.json') # globalUtils.saveDataToJSON(surPointFinalSelectedList,'json-point-sur.json')
# globalUtils.saveDataToJSON(ncPointFinalSelectedList, 'json-point-nc.json') # globalUtils.saveDataToJSON(ncPointFinalSelectedList, 'json-point-nc.json')
# globalUtils.saveDataToJSON(tifPointFinalSelectedList, 'json-point-tif.json') # globalUtils.saveDataToJSON(tifPointFinalSelectedList, 'json-point-tif.json')
@ -428,11 +428,11 @@ def main():
latIndex = tifLatList.index(lat) latIndex = tifLatList.index(lat)
tifParamValueList.append(aodMatrix[latIndex][lonIndex]) tifParamValueList.append(aodMatrix[latIndex][lonIndex])
print(f"surParamValueList: {len(surParamValueList)}") globalUtils.printWithFlush(f"surParamValueList: {len(surParamValueList)}")
print(f"ncParamValueList: {len(ncParamValueList)}") globalUtils.printWithFlush(f"ncParamValueList: {len(ncParamValueList)}")
print(f"tifParamValueList: {len(tifParamValueList)}") globalUtils.printWithFlush(f"tifParamValueList: {len(tifParamValueList)}")
print("pre process over!") globalUtils.printWithFlush("pre process over!")
# 第一步,组成大气辐射需要的参数: # 第一步,组成大气辐射需要的参数:
# sz vz sa va 来自 surParamValueList # sz vz sa va 来自 surParamValueList
@ -469,13 +469,13 @@ def main():
surWavestart, surWavestart,
surWaveend, surWaveend,
) )
print("+++++++++++++++gen sur input.txt") globalUtils.printWithFlush("+++++++++++++++gen sur input.txt")
# 第一步,先执行地表,得到结果文件 # 第一步,先执行地表,得到结果文件
surMain.test(surfaceModuleDir) surMain.test(surfaceModuleDir)
print("+++++++++++++++surface run over") globalUtils.printWithFlush("+++++++++++++++surface run over")
# 第二步拷贝地表的结果文件到input下 # 第二步拷贝地表的结果文件到input下
surOutputFile = os.path.join(surfaceModuleDir, "output.txt") surOutputFile = os.path.join(surfaceModuleDir, "output.txt")
print(surOutputFile) globalUtils.printWithFlush(surOutputFile)
if not os.path.isfile(surOutputFile): if not os.path.isfile(surOutputFile):
continue continue
# 第三步,构造大气辐射传输参数文件 # 第三步,构造大气辐射传输参数文件
@ -494,9 +494,9 @@ def main():
ozone, ozone,
surOutputFile, surOutputFile,
) )
print("+++++++++++++++gen toa input.txt") globalUtils.printWithFlush("+++++++++++++++gen toa input.txt")
utils.executeCompute() utils.executeCompute()
print("+++++++++++++++toa run over") globalUtils.printWithFlush("+++++++++++++++toa run over")
# 处理结果文件将input_para.txt和TOASimu.txt进行编号命名后拷贝到output中 # 处理结果文件将input_para.txt和TOASimu.txt进行编号命名后拷贝到output中
rgb445, rgb565, rgb670 = handleOutput(lon, lat, index) rgb445, rgb565, rgb670 = handleOutput(lon, lat, index)
heatMapValueList.append((lon, lat, rgb445, rgb565, rgb670)) heatMapValueList.append((lon, lat, rgb445, rgb565, rgb670))
@ -522,7 +522,7 @@ if __name__ == "__main__":
main() main()
except Exception as e: except Exception as e:
# 捕获除了ZeroDivisionError外的所有异常 # 捕获除了ZeroDivisionError外的所有异常
print("发生异常:", str(e)) globalUtils.printWithFlush("发生异常:", str(e))
finally: finally:
# 无论是否发生异常,最终都会执行该代码块 # 无论是否发生异常,最终都会执行该代码块
print("求解执行完毕!") globalUtils.printWithFlush("求解执行完毕!")

@ -117,3 +117,7 @@ def saveENVIImgToPng(imgFilePath: str, pngFilePath: str):
dataset = None dataset = None
# 发货文件名 # 发货文件名
return pngFilePath return pngFilePath
def printWithFlush(*args, sep=' ', end='\n', file=None, flush=True):
print(*args, sep=sep, end=end, file=file, flush=flush)
# sys.stdout.flush()
Loading…
Cancel
Save