├── README.md └── ParseVissimOutput_Fzp_Rsr.py /README.md: -------------------------------------------------------------------------------- 1 | **ParseVissimOutput_Fzp_Rsr.py** 2 | 3 | **2 functions _parseFzp_ and _parseRsr_ included** 4 | ### 5 | *parseFzp(fileNames, currentDate)* 6 | 7 | A python function for parsing PTV VISSIM vehicle trajectory outputs. 8 | The main purpose of using this function is as follows: 9 | 10 | 1. remove the commentary content from the beginning of fzp file (VISSIM vehicle record outputs) 11 | 2. set up the header row properly 12 | 3. Add three columes: X, Y, Z coordinates for "Position" (an attribute in vehichle records) 13 | 4. save the outputs into .csv file for further aggregation or analysis 14 | 15 | ### 16 | *parseRsr(fileNames, currentDate)* 17 | 18 | A python function for parsing raw PTV VISSIM vehicle travel time outputs. 19 | The main purpose of using this script is as follows: 20 | 21 | 1. remove the commentary content from the beginning of rsr file. 22 | 2. Add proper headers including the time instant each vehicle reaching the upstream and downstream boundary. 23 | 3. Save the outputs into .csv file for further aggregation or analysis 24 | ### 25 | The main functions reads all the fzp and rsr file in the current directory. 26 | -------------------------------------------------------------------------------- /ParseVissimOutput_Fzp_Rsr.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- """ Modified on November 8, 2018 @author: wenfu_wang """ import os import glob import csv import os.path import time import sys import datetime import string def parseRsr(fileNames, curDate): """ This function parse the input rsr files, remove redundant header lines save the data into a .csv file """ headerWritten = False outputName = 'Rsr_' + date + '.csv' # print outputName dest = open(outputName, 'wb') # attribute b avoids extra row in file myWriter = csv.writer(dest) for f in fileNames: targetLine = False idx = f.split(".")[0].split('_')[-1].lstrip('0') # print idx, type(idx) with open(f, 'rb') as myFile: for line in myFile: if line.startswith(" Time"): header = line.split(";") header = header[0:-1] headerC = [elem.strip(' .') for elem in header] headerC.insert(0, 'SimIdx') travelTimeIndex = headerC.index('Trav') timeFirstDownIndex = headerC.index('Time') headerC[timeFirstDownIndex] = "timeFirstDown" headerC.append('timeFirstUp') if not headerWritten: myWriter.writerow(headerC) headerWritten = True targetLine = True continue if targetLine: row = line.split(";") row = row[0:-1] rowC = [elem.strip(' ') for elem in row] rowC.insert(0, idx) timeFirstUpVal = float(rowC[timeFirstDownIndex]) - float(rowC[travelTimeIndex]) rowC.append(timeFirstUpVal) myWriter.writerow(rowC) dest.close() def parseFzp(fileNames, curDate): """ This function parse the input fzp files, remove redundant header lines save the data into a .csv file """ headerWritten = False outputName = 'Fzp_' + date + '.csv' # print outputName dest = open(outputName, 'wb') # attribute b avoids extra row in file myWriter = csv.writer(dest) for f in fileNames: targetLine = False coorLoc = -1 with open(f, 'rb') as myFile: for line in myFile: if line.startswith("$VEHICLE"): line = line.replace("$VEHICLE:", "") header = line.split(";") header = header[0:-1] headerC = [elem.replace("\\", "-") for elem in header] headerC.append('X') headerC.append('Y') headerC.append('Z') coorLoc = headerC.index("COORDFRONT") if not headerWritten: myWriter.writerow(headerC) headerWritten = True targetLine = True continue if targetLine: row = line.split(";") row = row[0:-1] rowC = [elem.strip(' ') for elem in row] coor = rowC[coorLoc].split(" ") rowC.append(coor[0]) rowC.append(coor[1]) rowC.append(coor[2]) myWriter.writerow(rowC) dest.close() if __name__ == "__main__": date = datetime.date.today() date = str(date) fzpFileNames = glob.glob('*.fzp') rsrFileNames = glob.glob('*.rsr') print 'rsr parsing...' parseRsr(rsrFileNames, date) print 'rsr parsing done!' print 'fzp parsing...' parseFzp(fzpFileNames, date) print 'fzp parsing done!' --------------------------------------------------------------------------------