一、.OUT文件概述

SWMM中.OUT输出文件为二进制文件,用记事本打开会呈现如下图所示的乱码状态。但是.OUT文件中有很多重要结果信息(例如:节点全模拟时段水深、侧向来水、出口总流量等数据),用官方提供的pyswmm库对.OUT文件进行解析。将二进制数据输出为Excel。

用python解析out python读取.out文件_数据

文件主要包括三种类型数据,节点类型数据、管道类型数据、汇水区类型数据。节点类型包括检查井、排口、调蓄池、分流器;管道类型包括管道、泵、孔口、堰、出口;汇水区包括汇水分区。节点输出数据包括:水深、水头、拦截容积、侧入流量、节点总入流量、节点溢流量及污染物,7个输出数据;管道输出数据包括:流量、水深、流速、Froude数、过流能力及污染物,6个输出数据;子汇水区输出数据包括:降雨量、积雪深度、蒸发量、入渗量、地表径流量、地下水径流量、地下水位、土壤含水量及污染物浓度,9个输出数据。

用python解析out python读取.out文件_数据_02

二、.OUT文件解析

 01.OUT头文件解析,头文件包含以下几个数据:

版本号、流量单位、污染物单位、汇水分区数量、节点数量、管段数量、污染物数量、报告开始时间及报告结束时间。代码如下:

# 文件初始化内容
    def push_s(self):

        file = str(self.lineEdit.text())
        start = time.time()
        output_Subcatch_data(file)
        end = time.time()
        all_time = "Subcatch数据导出时长为:" + str("%.3f" % (end - start)) + 's'

        with Output(file) as out:
            version = '版本号为:' + str(out.version)
            flow_units = "流量单位:" + out.units['flow']
            poll_units = "污染物单位:" + out.units['pollutant']
            sub_num = "汇水分区数量:" + str(len(out.subcatchments))
            nodes_num = "节点数量:" + str(len(out.nodes))
            links_num = "管段数量:" + str(len(out.links))
            poll_num = "污染物数量:" + str(len(out.pollutants))
            rep_start = "报告开始时间:" + str(out.start)
            rep_end = "报告结束时间:" + str(out.end)

            head_file_list = [version, flow_units, poll_units, sub_num, nodes_num, links_num, poll_num, rep_start,
                              rep_end]

02管段输出数据解析

将管段模拟数据(流量、水深、流速等)输出到Excel中,方便后期处理。

输出结果:

用python解析out python读取.out文件_python_03

 代码如下:

def output_link_data(file):
    '''
    路径不能有中文字符,否则会报错。
    头文件内容读取可以做为一个装饰器
    F:\test_file_SWMM\Example1.out
    '''

    '''所有管段模拟时间内数据输出'''
    with Output(file) as out:
        # data_dict = {}
        year_s = int(str(out.start)[0:4])
        month_s = int(str(out.start)[5:7])
        day_s = int(str(out.start)[8:10])
        hour_s = int(str(out.start)[11:13])

        if hour_s == 0:
            hour_s = hour_s + 1

        year_e = int(str(out.end)[0:4])
        month_e = int(str(out.end)[5:7])
        day_e = int(str(out.end)[8:10])
        hour_e = int(str(out.end)[11:13])

        data_list = []
        link_id = []
        for i in out.links:
            '''--------------------全模拟时段下单一管段流量数据--------------------'''
            ts = out.link_series(i, LinkAttribute.FLOW_RATE, datetime(year_s, month_s, day_s, hour_s), datetime(year_e, month_e, day_e, hour_e))

            time_index = []
            flow_index = []
            for index in ts:
                time_index.append(index)
                flow_index.append(ts[index])
                # dict_FLOW_RATE[index] = ts[index]
                # print(index, ts[index])
            dict_FLOW_RATE = {"time":time_index,'流量(flow_rate)':flow_index}
            data_FLOW_RATE = pd.DataFrame(dict_FLOW_RATE)
            # print(data_FLOW_RATE)

            '''--------------------全模拟时段下单一管段水深数据--------------------'''
            ts1 = out.link_series(i, LinkAttribute.FLOW_DEPTH, datetime(year_s, month_s, day_s, hour_s), datetime(year_e, month_e, day_e, hour_e))

            time_index1 = []
            flow_index1 = []
            for index in ts1:
                time_index1.append(index)
                flow_index1.append(ts1[index])
                # dict_FLOW_DEPTH[index] = ts1[index]

            dict_FLOW_DEPTH = {"time":time_index1,"水深(flow_depth)":flow_index1}
            data_FLOW_DEPTH = pd.DataFrame(dict_FLOW_DEPTH)
            # print(data_FLOW_DEPTH.index)
            # u = input()
            '''--------------------全模拟时段下单一管段流速数据--------------------'''
            ts2 = out.link_series(i, LinkAttribute.FLOW_VELOCITY, datetime(year_s, month_s, day_s, hour_s), datetime(year_e, month_e, day_e, hour_e))

            time_index2 = []
            flow_index2 = []
            for index in ts2:
                time_index2.append(index)
                flow_index2.append(ts2[index])
                # dict_FLOW_DEPTH[index] = ts1[index]

            dict_FLOW_VELOCITY = {"time": time_index2, "流速(flow_velocity)": flow_index2}
            data_FLOW_VELOCITY = pd.DataFrame(dict_FLOW_VELOCITY)
            # print(data_FLOW_DEPTH.index)

            '''--------------------全模拟时段下单一管段体积数据--------------------'''
            ts3 = out.link_series(i, LinkAttribute.FLOW_VOLUME, datetime(year_s, month_s, day_s, hour_s), datetime(year_e, month_e, day_e, hour_e))

            time_index3 = []
            flow_index3 = []
            for index in ts3:
                time_index3.append(index)
                flow_index3.append(ts3[index])

            dict_FLOW_VOLUME = {"time": time_index3, "体积(flow_volume)": flow_index3}
            data_FLOW_VOLUME = pd.DataFrame(dict_FLOW_VOLUME)
            '''--------------------全模拟时段下单一管段过流能力数据--------------------'''
            ts4 = out.link_series('16', LinkAttribute.CAPACITY, datetime(year_s, month_s, day_s, hour_s), datetime(year_e, month_e, day_e, hour_e))

            time_index4 = []
            flow_index4 = []
            for index in ts4:
                time_index4.append(index)
                flow_index4.append(ts4[index])

            dict_CAPACITY = {"time": time_index4, "过流能力(capacity)": flow_index4}
            data_CAPACITY = pd.DataFrame(dict_CAPACITY)
            '''--------------------全模拟时段下单一管段污染物数据--------------------'''
            ts5 = out.link_series(i, LinkAttribute.POLLUT_CONC_0, datetime(year_s, month_s, day_s, hour_s), datetime(year_e, month_e, day_e, hour_e))

            time_index5 = []
            flow_index5 = []
            for index in ts5:
                time_index5.append(index)
                flow_index5.append(ts5[index])

            dict_POLLUT_CONC_0 = {"time": time_index5, "水质(pollut_conc)": flow_index5}
            data_POLLUT_CONC_0 = pd.DataFrame(dict_POLLUT_CONC_0)
            '''--------------------数据合并--------------------'''
            data_all1 = pd.merge(data_FLOW_RATE,data_FLOW_DEPTH,on='time',how='outer')
            data_all2 = pd.merge(data_all1,data_FLOW_VELOCITY,on='time',how='outer')
            data_all3 = pd.merge(data_all2,data_FLOW_VOLUME,on='time',how='outer')
            data_all4 = pd.merge(data_all3, data_CAPACITY, on='time', how='outer')
            data_all = pd.merge(data_all4, data_POLLUT_CONC_0,on='time', how='outer')

            link_id.append(i)
            data_list.append(data_all)


        data_list1 = pd.concat(data_list,axis=1,keys=link_id)
        data_list1.to_excel(file[:-4] + '_links.xlsx')

节点及汇水分区输出数据解析类似上述操作,不在赘述。