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

Fixed csv export

This commit is contained in:
Ahp06 2019-01-16 17:26:02 +01:00
parent d6436a941f
commit 390ce6ef2c

View File

@ -15,10 +15,6 @@ 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()
width = simulation_bounds[1][0] / areas_number width = simulation_bounds[1][0] / areas_number
@ -137,17 +133,17 @@ def export_data_to_csv(config, grid):
csv_dir = os.path.join(SCRIPTDIR, 'csv') csv_dir = os.path.join(SCRIPTDIR, 'csv')
if not os.path.exists(csv_dir): if not os.path.exists(csv_dir):
os.mkdir(csv_dir) os.mkdir(csv_dir)
now = datetime.datetime.utcnow().isoformat()
with open(os.path.join(csv_dir, f'{now}.csv'), 'w') as f: now = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
with open(f'csv/{now}.csv', 'w') as f:
writer = csv.writer(f) writer = csv.writer(f)
# Write CSV headers # Write CSV headers
writer.writerow(itertools.chain(('Step',), (a.name for a in grid))) writer.writerow(itertools.chain(('Step',), (a.name for a in grid)))
emissions = (a.emissions_by_step for a in grid) # Write all areas emission value for each step
step = 0 for step in range(config.n_steps):
for em in emissions: em_for_step = (f'{a.emissions_by_step[step].value():.3f}' for a in grid)
writer.writerow(itertools.chain((step,), (e.value() for e in em))) writer.writerow(itertools.chain((step,), em_for_step))
step += 1
def run(config, logger): def run(config, logger):