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.

143 lines
4.2 KiB
Python

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import matlab.engine
import sys
import os
import json
import mainMapEcharts
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
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:
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:]
globalUtils.printWithFlush(paramValueList)
globalUtils.printWithFlush(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外的所有异常
globalUtils.printWithFlush("发生异常:", str(e))
finally:
# 无论是否发生异常,最终都会执行该代码块
globalUtils.printWithFlush("求解执行完毕!")