1
0
mirror of https://github.com/Ahp06/SUMO_Emissions.git synced 2024-11-22 03:26:30 +00:00

Fix CSV export code

This commit is contained in:
Thibaud Gasser 2018-12-16 21:35:37 +01:00
parent 229ab72279
commit 2a4ce7f935

View File

@ -1,5 +1,6 @@
import argparse import argparse
import csv import csv
import itertools
import sys import sys
import time import time
from traci import trafficlight from traci import trafficlight
@ -15,6 +16,9 @@ import actions
from config import Config from config import Config
from model import Area, Vehicle, Lane , TrafficLight , Phase , Logic, Emission from model import Area, Vehicle, Lane , TrafficLight , Phase , Logic, Emission
# Absolute path of the directory the script is in
SCRIPTDIR = os.path.dirname(__file__)
def init_grid(simulation_bounds, areas_number,window_size): def init_grid(simulation_bounds, areas_number,window_size):
grid = list() grid = list()
@ -125,31 +129,24 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle], current_step, confi
def get_reduction_percentage(ref,total): def get_reduction_percentage(ref,total):
return (ref - total) / ref * 100 return (ref - total) / ref * 100
def export_data_to_csv(config, grid): def export_data_to_csv(config, grid):
csv_dir = os.path.join(SCRIPTDIR, 'csv')
now = datetime.datetime.now() if not os.path.exists(csv_dir):
current_date = now.strftime("%Y_%m_%d_%H_%M_%S") os.mkdir(csv_dir)
now = datetime.datetime.utcnow().isoformat()
'''if not os.path.exists(f'csv/{current_date}'):
os.makedirs(f'csv/{current_date}')''' with open(os.path.join(csv_dir, f'{now}.csv'), 'w') as f:
writer = csv.writer(f)
with open(f'test.csv', mode='w', newline = '') as emission_file: # Write CSV headers
try: writer.writerow(itertools.chain(('Step',), (a.name for a in grid)))
csv_writer = csv.writer(emission_file, delimiter = ' ', quoting=csv.QUOTE_MINIMAL) emissions = (a.emissions_by_step for a in grid)
csv_writer.writerow(['steps']) step = 0
a = list(range(config.n_steps)) for em in emissions:
for item in a: writer.writerow(itertools.chain((step,), (e.value() for e in em)))
csv_writer.writerow([item]) step += 1
for area in grid:
csv_writer.writerow([f'{area.name}'])
for emission in area.emissions_by_step:
csv_writer.writerow([emission.value()])
finally:
emission_file.close()
def run(config, logger): def run(config, logger):
grid = list() grid = list()
try: try: