def log_generator(state_queue, log_path): """Write logs to CSV files, and update it whenever the state is changed. Parameters ---------- state_queue : multiprocessing.Queue Queue where the state should be put. log_path : path_like Relative path to which to write the bus and line log. """ log_path_bus = path.join(log_path, 'grid_bus.csv') log_path_line = path.join(log_path, 'grid_line.csv') log_file_bus = open(log_path_bus, 'w', buffering=1, newline='') log_writer_bus = DictWriter( log_file_bus, ('Timestamp', 'BusIndex', 'P', 'Q', 'Vm', 'Va')) log_writer_bus.writeheader() log_file_line = open(log_path_line, 'w', buffering=1, newline='') log_writer_line = DictWriter(log_file_line, ('Timestamp', 'Line #', 'LineCurrent')) log_writer_line.writeheader() while True: # Retrieve the state from the queue. state = state_queue.get() assert len({ len(state['P']), len(state['Q']), len(state['Vm']), len(state['Va']) }) == 1 row = {'Timestamp': state['Ts']} for index, (P, Q, Vm, Va) in enumerate( zip(state['P'], state['Q'], state['Vm'], state['Va'])): # Write the state of the current bus. row.update({'BusIndex': index, 'P': P, 'Q': Q, 'Vm': Vm, 'Va': Va}) log_writer_bus.writerow(row) row = {'Timestamp': state['Ts']} for index, LineCurrent in enumerate((state['LineCurrents'])): # Write the state of the current line. row.update({'Line #': index, 'LineCurrent': LineCurrent}) log_writer_line.writerow(row) log_writer_bus.close() log_writer_line.close()
def log_generator(state_queue, log_path): """Write logs to CSV files, and update it whenever the state is changed. Parameters ---------- state_queue : multiprocessing.Queue Queue where the state should be put. log_path : path_like Relative path to which to write the sensor log bus and freq. """ log_path_bus = path.join(log_path, 'sensor_bus.csv') log_path_freq = path.join(log_path, 'sensor_freq.csv') log_file_bus = open(log_path_bus, 'w', buffering=1, newline='') log_writer_bus = DictWriter( log_file_bus, ('Timestamp', 'BusIndex', 'PhaseIndex', 'P', 'Q', 'Vreal', 'Vimag')) log_writer_bus.writeheader() log_file_freq = open(log_path_freq, 'w', buffering=1, newline='') log_writer_freq = DictWriter(log_file_freq, ('Timestamp', 'Line #', 'frequency')) log_writer_freq.writeheader() while True: # Retrieve the state from the queue. state = state_queue.get() # curr_time = datetime.now() # row = {'Timestamp': curr_time} row = {'Timestamp': state['Ts']} if 'buses' in state: for line in state['buses']: row.update({ 'BusIndex': line['bus_index'], 'PhaseIndex': line['phase_index'], 'P': line['P'], 'Q': line['Q'], 'Vreal': line['v_bus_real'], 'Vimag': line['v_bus_imag'] }) log_writer_bus.writerow(row) # row = {'Timestamp': curr_time} row = {'Timestamp': state['Ts']} row.update({'Line #': 0, 'frequency': state['freq']}) log_writer_freq.writerow(row) else: log_writer_bus.writerow(row) log_writer_freq.writerow(row) log_writer_bus.close() log_writer_freq.close()