文件
AcousticSo/cz.py
T
2023-04-07 16:11:16 +08:00

173 行
8.0 KiB
Python

# -*- coding: utf-8 -*-
import warnings
warnings.filterwarnings('ignore')
import argparse, json
from common import NoIndent, MyEncoder
import os, sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)) + '/pyat')
def _argparse():
parser = argparse.ArgumentParser(description='Average Noise Spectral Level')
parser.add_argument('-a', '--argfile', action='store', dest='argfile', required=True, help='name of arguments file')
return parser.parse_args()
def main():
parser = _argparse()
argfile = parser.argfile
if argfile[-5:] != '.json':
print('输入的参数文件名有误')
return
with open(argfile, 'r') as jsonFile:
paramData = json.load(jsonFile)
if ("ETOPO1Path" not in paramData) or ("STPath" not in paramData) or \
("freq" not in paramData) or ("sourceDepth" not in paramData) or ("receiveDepth" not in paramData) or \
("lat" not in paramData) or ("lon" not in paramData) or \
("timeType" not in paramData) or \
("outParam" not in paramData):
print(f'{argfile}中的参数有误,请核实!')
return
ETOPO1Path = paramData["ETOPO1Path"]
STPath = paramData["STPath"]
freq = float(paramData["freq"])
sourceDepth = float(paramData["sourceDepth"])
receiveDepth = float(paramData["receiveDepth"])
lat = float(paramData["lat"])
lon = float(paramData["lon"])
try:
azi = float(paramData["azi"])
except:
azi = None
timeType = paramData["timeType"]
outParam = paramData["outParam"]
if "outPath" not in outParam:
outParam["outPath"] = ""
from DataBase import DataBase
from czcore import call_cz
import numpy as np
# 定义时间段
timeIndex = timeType
# 创建类
database = DataBase(ETOPO1Path, STPath, dataSource="")
# 设置时间段
database.set_timePeriod(timeIndex)
# call_cz函数返回汇聚区计算的中间值CzData的列表
# CzData保存了方位角、距离、深度、损失、地形、会聚区等数据
# 其中czdata.azi是方位角、czdata.ranges是距离列表、czdata.depths是深度列表、
# czdata.TL是损失、czdata.bathym是地形数据
# czdata.Rays是声线数据
# czdata.CzDetail是会聚区数据,包括会聚区宽度和距离
res = call_cz(database, freq, lat, lon, azi, sourceDepth)
# call_cz(25, 18.0, 114.0, 10, 3500)
# write data to file
# 输出json文件
outData = {}
outData["param"] = paramData
if len(res) == 0:
outData["result"] = "无会聚区"
outData["data"] = []
else:
outData["result"] = "有会聚区"
outDataData, outBathymData, outTlData, outRayData = [], [], [], []
json.encoder.FLOAT_REPR = lambda x: format(x, '.3f')
for index in range(len(res)):
bathymData, tlData, rayData = {}, {}, {}
# 保存地形数据
res[index].bathym.r = np.around(res[index].bathym.r, decimals=0)
res[index].bathym.d = np.around(res[index].bathym.d, decimals=0)
bathymData = {
"param": paramData,
"azi": f'{round(res[index].azi)}',
"count": len(res[index].bathym.d),
"descript": "距离、深度",
"data": NoIndent(list(zip(res[index].bathym.r, res[index].bathym.d)))
}
# 保存传播损失数据
tlpoint = []
res[index].depths = np.around(res[index].depths, decimals=0)
res[index].ranges = np.around(res[index].ranges, decimals=0)
res[index].TL = np.around(res[index].TL, decimals=2)
tlpoint = list(
zip(np.tile(res[index].depths.reshape(len(res[index].depths), 1), len(res[index].ranges)).flatten(),
np.tile(res[index].ranges, (len(res[index].depths), 1)).flatten(), res[index].TL.flatten()))
tlData = {
"param": paramData,
"azi": f'{round(res[index].azi)}',
"count": len(res[index].ranges) * len(res[index].depths),
"descript": "距离、深度、损失",
"data": NoIndent(tlpoint)
}
# 保存声线数据
raypoint = []
for ray in res[index].Rays:
ray.xy[0] = np.around(ray.xy[0], decimals=2)
ray.xy[1] = np.around(ray.xy[1], decimals=2)
tempdata = {
"pointCount": len(ray.xy[0]),
"data": NoIndent(list(zip(ray.xy[0], ray.xy[1])))
}
raypoint.append(tempdata)
rayData = {
"param": paramData,
"azi": f'{round(res[index].azi)}',
"count": len(res[index].Rays),
"rayData": raypoint
}
# print(len(res[index].Rays))
czDeatilData = {}
czDeatilData["descript"] = res[index].CzDetail.descript
if len(res[index].CzDetail.directCz) == 0:
czDeatilData["directCz"] = []
else:
directczdetaillist = []
for directcz in res[index].CzDetail.directCz:
directczdetail = {
"minRange": f'{round(directcz.minRange, 1)}',
"maxRange": f'{round(directcz.maxRange, 1)}',
"width": f'{round(directcz.width, 1)}',
}
directczdetaillist.append(directczdetail)
czDeatilData["directCz"] = directczdetaillist
if len(res[index].CzDetail.reflectCz) == 0:
czDeatilData["reflectCz"] = []
else:
reflectczdetaillist = []
for reflectcz in res[index].CzDetail.reflectCz:
reflectczdetail = {
"minRange": f'{round(reflectcz.minRange, 1)}',
"maxRange": f'{round(reflectcz.maxRange, 1)}',
"width": f'{round(reflectcz.width, 1)}',
}
reflectczdetaillist.append(reflectczdetail)
czDeatilData["reflectCz"] = reflectczdetaillist
outDataData.append(czDeatilData)
outBathymData.append(bathymData)
outTlData.append(tlData)
outRayData.append(rayData)
with open(("" if outParam["outPath"] == "" else outParam["outPath"] + "/") + "czOut_bathym.json",
'w') as jsonFile:
json_data = json.dumps(outBathymData, cls=MyEncoder, indent=2, ensure_ascii=False)
jsonFile.write(json_data)
with open(("" if outParam["outPath"] == "" else outParam["outPath"] + "/") + "czOut_tl.json",
'w') as jsonFile:
json_data = json.dumps(outTlData, cls=MyEncoder, indent=2, ensure_ascii=False)
jsonFile.write(json_data)
with open(("" if outParam["outPath"] == "" else outParam["outPath"] + "/") + "czOut_ray.json",
'w') as jsonFile:
json_data = json.dumps(outRayData, cls=MyEncoder, indent=2, ensure_ascii=False)
jsonFile.write(json_data)
outData["data"] = outDataData
with open(("" if outParam["outPath"] == "" else outParam["outPath"] + "/") + "czOut.json", 'w') as jsonFile:
json.dump(outData, jsonFile, indent=4, ensure_ascii=False)
# import time
if __name__ == '__main__':
# start = time.perf_counter()
main()
# end = time.perf_counter()
# print('time = ',(end-start))