You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
3.8 KiB
Python

import matlab.engine
import sys
import os
import json
import mainMapEcharts
import shutil
# 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"
# 执行一次求解
def calculation(eng: matlab.engine):
# 执行一次求解
eng.main(nargout=0)
def readInputGeo():
filePath = "input_geo.txt"
paramNameList = ""
paramValueList = []
with open(filePath, "r", encoding="utf-8") as file:
lines = file.readlines()
paramNameList = lines[0]
paramValueList = lines[1]
return [paramNameList, paramValueList.split(",")]
# 生成参数组合
def genParamList(filePath: str):
paramList = []
with open(filePath, "r", encoding="utf-8") as file:
content = ""
for line in file:
paramList.append(line.split()[1:])
return paramList
# 生成一个求解参数文件
def genParamFile(paramVector: list, geoParamNameList: list, geoParamValueList: list):
paramValueList = [geoParamValueList[0]] + paramVector + geoParamValueList[1:]
print(paramValueList)
print(geoParamNameList)
with open("input.txt", "w") as file:
file.write(geoParamNameList)
file.write(",".join(map(str, paramValueList)))
pass
# 将输出文件转换为json格式
def genGeoJsonFileFromOutput(outputFilePath: str):
if not os.path.exists(outputFilePath):
return
jsonFilePath = outputFilePath.replace(".txt", ".json")
geoDataDict = {}
i = 1
with open(outputFilePath, "r", encoding="utf-8") as file:
for line in file:
geoDataDict[str(i)] = [float(x) for x in line.split()]
i += 1
with open(jsonFilePath, "w") as file:
file.write(json.dumps(geoDataDict))
pass
def isDir(dirPath: str):
return os.path.exists(dirPath) and os.path.isdir(dirPath)
def rmPath(fileOrDirPath: str):
if os.path.exists(fileOrDirPath):
if os.path.isdir(fileOrDirPath):
shutil.rmtree(fileOrDirPath)
elif os.path.isfile(fileOrDirPath):
os.remove(fileOrDirPath)
# 主函数
def main():
# 平台性能模块参数部分
aSatFilePath = sys.argv[1]
# 结果文件的目录地址
outputDistDir = sys.argv[2]
# 读取参数组合
paramList = genParamList(aSatFilePath)
# TODO检查是否有 input_geo.txt、平台性能模块文件有参数
# 读取 geo模块的参数
paramDict = readInputGeo()
paramNameList = paramDict[0]
paramValueList = paramDict[1]
# 判断output目录是否存在不存在在创建
rmPath(outputDir)
os.mkdir(outputDir)
eng = matlab.engine.start_matlab()
for index, paramVector in enumerate(paramList):
# 生成求解参数文件
genParamFile(paramVector, paramNameList, paramValueList)
# 调用求解器求解
calculation(eng)
# 将结果文件移动到output目录中
outputFileName = outputDir + "/output" + str(index) + ".txt"
if os.path.exists(outputFileName):
os.remove(outputFileName)
os.rename("output.txt", outputFileName)
# 将结果数据生成html
mainMapEcharts.rendOutputGeo(outputFileName)
# 将结果数据生成为json文件
# genGeoJsonFileFromOutput(outputFilePath=outputFileName)
# break
# 删除旧的目录
rmPath(outputDistDir)
# 将结果拷贝的目标目录下
shutil.copytree(outputDir, outputDistDir)
if __name__ == "__main__":
try:
# 尝试执行的代码块
main()
except Exception as e:
# 捕获除了ZeroDivisionError外的所有异常
print("发生异常:", str(e))
finally:
# 无论是否发生异常,最终都会执行该代码块
print("求解执行完毕!")