py读取dat/plt
importnumpyas npimportmatplotlib.pyplotas pltimportre##1.解析函数 #defparse_tecplot_file(filepath): 解析TECPLOT BLOCK格式数据文件 参数:filepath:文件路径 返回:data_dict:数据字典 header_info:头部信息 withopen(filepath,r)as f:full_textf.read()# 解析头部 header_info{}# 提取变量名 vars_startfull_text.find(VARIABLES )vars_endfull_text.find(ZONE)ifvars_start!-1andvars_end!-1:vars_strfull_text[vars_start12:vars_end]header_info[variables]vars_str.split()# 提取网格尺寸 i_matchre.search(rI\s*\s*(\d),full_text)j_matchre.search(rJ\s*\s*(\d),full_text)ifi_match:header_info[I]int(i_match.group(1))ifj_match:header_info[J]int(j_match.group(1))# 提取数值数据 block_posfull_text.find(DATAPACKING BLOCK)ifblock_pos!-1:data_strfull_text[block_pos20:]values[]fortoken in data_str.split():try:values.append(float(token))except ValueError:continuevaluesnp.array(values)# 按变量分割数据 n_varslen(header_info[variables])n_pointsheader_info[I]*header_info[J]data_dict{}fori,var_name inenumerate(header_info[variables]):start_idxi*n_points end_idxstart_idxn_points data_dict[var_name]values[start_idx:end_idx]returndata_dict,header_info ##2.使用示例 ## 读取数据 data_dict,header_infoparse_tecplot_file(result_0040.dat)# 访问数据print(X.shape,Y.shape,P.shape)Xdata_dict[X].reshape(header_info[J],header_info[I])Ydata_dict[Y].reshape(header_info[J],header_info[I])Pdata_dict[P].reshape(header_info[J],header_info[I])# 可视化 plt.figure(figsize(10,8))plt.contourf(X,Y,P,levels200,cmapRdYlBu_r)#,antialiasedTrue)plt.colorbar(labelPressure)plt.xlabel(X)plt.ylabel(Y)plt.title(Pressure Distribution)plt.axis(equal)plt.show()##3.导出为其他格式 ## 导出为VTK格式 defexport_to_vtk(data_dict,header_info,filename): 导出为VTK格式可用ParaView打开 Iheader_info[I]Jheader_info[J]withopen(filename,w)as f:f.write(# vtk DataFile Version 3.0\n)f.write(CFD Data\n)f.write(ASCII\n)f.write(DATASET STRUCTURED_GRID\n)f.write(fDIMENSIONS {I} {J} 1\n)f.write(fPOINTS {I*J} float\n)# 写入坐标fori inrange(I*J):f.write(f{data_dict[X][i]} {data_dict[Y][i]} 0.0\n)# 写入数据 f.write(fPOINT_DATA {I*J}\n)forvar in header_info[variables]:ifvarnotin[X,Y]:f.write(fSCALARS {var} float 1\n)f.write(LOOKUP_TABLE default\n)forval in data_dict[var]:f.write(f{val}\n)# 导出为CSV格式 defexport_to_csv(data_dict,header_info,filename): 导出为CSV格式 Iheader_info[I]Jheader_info[J]n_pointsI*J withopen(filename,w)as f:# 写入表头 f.write(,.join(header_info[variables])\n)# 写入数据fori inrange(n_points):row[str(data_dict[var][i])forvar in header_info[variables]]f.write(,.join(row)\n)re提取变量值可以通过增大“level”提高分辨率
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2433346.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!