import pandas as pd from datetime import datetime import numpy as np from numpy import diff from scipy.interpolate import make_interp_spline from scipy.signal import savgol_filter import matplotlib.pyplot as plt timestamp_start = None def unix_to_datetime(unix_string): return datetime.strptime(unix_string, "%H:%M:%S.%f") def deltatime(time): global timestamp_start return (time - timestamp_start).total_seconds() def timestamp_converter(timestamp): time = unix_to_datetime(timestamp) global timestamp_start if timestamp_start == None: timestamp_start = time delta = deltatime(time) return delta shots = [ { 'filename': 'Elisabeth/16_06_2022_1.xlsx', 'title': 'Elisabeth 16.06.2022 #1', 'cutoff': 38, }, # [0] { 'filename': 'Elisabeth/19_06_2022_1.xlsx', 'title': 'Elisabeth 19.06.2022 #1', }, # [1] { 'filename': 'Laura/20_06_2022_1.xlsx', 'title': 'Laura 20.06.2022 #1', 'cutoff': 38.7, }, # [2] { 'filename': 'Laura/20_06_2022_2.xlsx', 'title': 'Laura 20.06.2022 #2', 'cutoff': 32.6, }, # [3] ] comparison = { 'Elisabeth 19.06.22 Shot #1 vs. #2': (0, 1), #[0] 'Laura 20.06.22 Shot #1 vs #2': (2, 3), #[1] #'comparison': { # 'Elisabeth Shots 06/13/22 - 06/10/22': (1, 2), #[0] # 'Elisabeth Shots 06/15/22 - 06/10/22': (1, 3), #[1] # 'Elisabeth Shots 06/15/22 - 06/13/22': (2, 3), #[2] # } } curr_figure = 0 fig1 = plt.figure(curr_figure) ax1 = plt.subplot(2, 2, 1) # ax1.minorticks_on() # plt.title('Shotweight over time') plt.ylabel('Weight (g)') plt.grid(visible=True, which='both', axis='both', linewidth=0.5) ax2 = plt.subplot(2, 2, 3, sharex=ax1) # plt.title('Flow-rate over time') plt.ylabel('Flow-rate (g/s)') plt.grid(visible=True, which='both', axis='both', linewidth=0.5) ax3 = plt.subplot(2, 2, 2, sharex=ax1, sharey=ax1) # plt.title('Shotweight over time') plt.grid(visible=True, which='both', axis='both', linewidth=0.5) ax4 = plt.subplot(2, 2, 4, sharex=ax2, sharey=ax2) # plt.title('Flow-rate over time') plt.grid(visible=True, which='both', axis='both', linewidth=0.5) fig1.supxlabel('Time (s)') plt.tight_layout() plt.subplot(2, 2, 1) for shot in shots: timestamp_start = None df_raw = pd.read_excel(shot['filename'], converters={ 0: lambda x: timestamp_converter(x) }, sheet_name=0) df_calc = pd.read_excel(shot['filename'], converters={ 0: lambda x: timestamp_converter(x) }, sheet_name=1) time_col_raw = df_raw.keys()[0] profile_col_raw = df_raw.keys()[5] time_col_calc = df_calc.keys()[0] profile_col_calc = df_calc.keys()[2] if 'cutoff' in shot: if shot['cutoff'] != -1: df_raw = df_raw.loc[df_raw[time_col_raw] < shot['cutoff']] df_calc = df_calc.loc[df_calc[time_col_calc] < shot['cutoff']] shot['data'] = {} shot['data']['profile'] = df_raw shot['data']['flowrate'] = df_calc time = df_raw[time_col_raw].tolist() weight = df_raw[profile_col_raw].tolist() # print("time: ", time) # print("weight: ", time) plt.plot(time, weight, label = shot['title'], linewidth=1) plt.subplot(2, 2, 3) flow_time = df_calc[time_col_calc].tolist() flowrate = df_calc[profile_col_calc].tolist() # print("flow_time: ", flow_time) # print("flowrate: ", flowrate) plt.plot(flow_time, flowrate, label = shot['title'], linewidth=1) plt.subplot(2, 2, 1) plt.subplot(2, 2, 4) for key in comparison: calc1 = shots[comparison[key][0]]['data']['flowrate'] calc2 = shots[comparison[key][1]]['data']['flowrate'] t1 = np.array(calc1[calc1.keys()[0]].tolist()) r1 = np.array(calc1[calc1.keys()[2]].tolist()) t2 = np.array(calc2[calc2.keys()[0]].tolist()) r2 = np.array(calc2[calc2.keys()[2]].tolist()) dt = None size_diff = t2.size - t1.size if size_diff > 0: dt = t2 r1 = np.pad(r1, (0, size_diff), 'constant') elif size_diff < 0: dt = t1 r2 = np.pad(r2, (0, -size_diff), 'constant') dr = r2 - r1 plt.plot(dt, dr, label = key, linewidth=1) plt.legend(loc='best') plt.show()