- def pressurepreprocesstest(pressure,res_pressure,fcutA1,fcutB1,fcutB2,fcutA2,fs,order,Atm_OST,stormname,begintime,endtime,plot=None):
- ### Calibration from Volt to Pascal ###
- #seperated calibration for each column: !! fill in calibration factors !!
- pressure_cal = pressure.copy(deep=True) #to avoid overwriting the original pressure dataframe (A1,B1,B2,A2)
- calibrationlist = [7082445.026, 7076486.04, 7201259.142, 7226444.017, 7200909.636, 7282744.547, 7303428.346, 7315431.079, 7141262.614, 7066365.568, 7098082.242, 7102981.852,
- 7209403.272, 7165352.954, 7184994.007, 7095088.034]
- intersectionpointlist = [ -29639.74618 , -29601.42945 , -32664.24549 ,-32754.60478, -31204.01428, -33014.60364 , -33870.19357, -33691.06106, -27616.95765, -28568.86298, -29043.3229, -29745.91237, -31386.73127 , -30248.676, -30383.69895, -28766.79008 ]
- #fill in calibration factors for every sensor
- for i,j in zip(pressure_cal,range(16)): #multiply every column with its calibration factor
- pressure_cal[i] = calibrationlist[j]*pressure_cal[i] + intersectionpointlist[j]
- if stormname == 'Storm 25/02/2023':
- ## SPECIFIC PER STORM: 25/02/2023
- Atm_Ost_selected = Atm_OST[12:37].reset_index(drop=True)
- if stormname == 'Storm 10/03/2023':
- ## SPECIFIC PER STORM: 10/03/2023
- Atm_Ost_selected = Atm_OST[72:115].reset_index(drop=True)
- if stormname == 'Storm 25/03/2023':
- ## SPECIFIC PER STORM: 10/03/2023
- Atm_Ost_selected = Atm_OST[72:115].reset_index(drop=True)
- if stormname == 'Storm 23/11/2023':
- ## SPECIFIC PER STORM: 10/03/2023
- Atm_Ost_selected = Atm_OST[108:171].reset_index(drop=True)
- if stormname == 'Storm 24/11/2023':
- ## SPECIFIC PER STORM: 10/03/2023
- Atm_Ost_selected = Atm_OST[177:244].reset_index(drop=True)
- #TimeOST =[datetime.strptime(begintime,"%d-%m-%Y %H:%M") + timedelta(minutes=i*10) for i in Atm_OST_selected.index]
- res_pressure['p_atm Ostend'] = np.nan
- i=0
- j=0
- while j < len(Atm_Ost_selected)-1:
- res_pressure.iloc[i,6] = Atm_Ost_selected.iloc[j,1]
- i = i + 10*60*10
- j = j + 1
- res_pressure['p_atm Ostend'] = res_pressure['p_atm Ostend'].interpolate(method='linear')
- #from absolute to relative pressure
- p_atm_Ost = res_pressure['p_atm Ostend']*100
- pressure_rel = pressure_cal.copy(deep=True)
- pressure_rel.columns = ['A1P1','A1P2','A1P3','A1P4','B1P1','B1P2','B1P3','B1P4','B2P1', 'B2P2', 'B2P3', 'B2P4','A2P1','A2P2','A2P3','A2P4']
- for column in pressure_rel:
- pressure_rel[column] = pressure_rel[column].subtract(p_atm_Ost,axis=0)
- print(pressure_rel[column])
- ### Low pass filtering ###
- #default: Butterworth filter
- P_A1 = pressure_rel[['A1P1','A1P2','A1P3','A1P4']]
- P_A1_filt = pp.df_LPFiltButter(P_A1, fcutA1,fs,order)
- P_B1 = pressure_rel[['B1P1','B1P2','B1P3','B1P4']]
- P_B1_filt = pp.df_LPFiltButter(P_B1, fcutB1,fs,order)
- P_B2 = pressure_rel[['B2P1','B2P2','B2P3','B2P4']]
- P_B2_filt = pp.df_LPFiltButter(P_B2, fcutB2,fs,order)
- P_A2 = pressure_rel[['A2P1','A2P2','A2P3','A2P4']]
- P_A2_filt = pp.df_LPFiltButter(P_A2,fcutA2,fs,order)
- ### Averaging over the four pressure sensors per overtopping box ###
- P_A1_avg = P_A1_filt.mean(axis=1)
- P_B1_avg = P_B1_filt.mean(axis=1)
- P_B2_avg = P_B2_filt.mean(axis=1)
- P_A2_avg = P_A2_filt.mean(axis=1)
- # add different averaged & filtered pressure signals into one DataFrame
- filtpressure = pd.concat([P_A1_filt,P_B1_filt,P_B2_filt,P_A2_filt],axis=1)
- filtpressure.columns = ['A1P1','A1P2','A1P3','A1P4','B1P1','B1P2','B1P3','B1P4','B2P1', 'B2P2', 'B2P3', 'B2P4','A2P1','A2P2','A2P3','A2P4']
- filtpressure_avg = pd.concat([P_A1_avg,P_B1_avg,P_B2_avg,P_A2_avg],axis=1)
- filtpressure_avg.columns = ['P_A1','P_B1','P_B2','P_A2']
- ### Optional plotting ###
- time_data = pd.to_datetime(Atm_Ost_selected.iloc[:, 0])
- pressure_data = Atm_Ost_selected.iloc[:, 1]
- # Define time steps
- fs = 10 # sampling frequency
- Time = [begintime + timedelta(sec for i in range(len(pressure))]
- if plot == True:
- # Calculate the time difference between begintime and endtime
- time_diff = endtime - begintime
- num_hours = int(time_diff.total_seconds() / 3600)
- # Generate x-axis ticks with appropriate spacing
- x_ticks = [begintime + timedelta(hours=i) for i in range(num_hours+1)]
- # Plot both air pressure data and calibrated, filtered, and averaged pressure data on the same graph
- plt.figure(figsize=(12, 8))
- # Plot air pressure data
- plt.plot(time_data, pressure_data, color='purple', line label='Air Pressure Data')
- # Plot calibrated, filtered, and averaged pressure data
- plt.plot(Time, pressure_rel['A1P1'], linewidth=0.3, color='red', label='Calibrated raw data of sensor P1')
- plt.plot(Time, pressure['A1P1'], linewidth=0.3, color='green', label='Raw data of sensor P1')
- plt.plot(Time, P_A1_filt['A1P1'], linewidth=1.5, color='blue', label='Filtered pressures of sensor P1')
- plt.plot(Time, P_A1_avg, linewidth=1.5, color='black', label='Filtered pressures (average of the four sensors P1, P2, P3 and P4)')
- plt.xlabel('Time', f
- plt.ylabel('Pressure', f
- plt.title('Comparison of Air Pressure Data with Calibrated, Filtered, and Averaged Pressure Data', f
- plt.xticks(x_ticks, rotati # Rotate x-axis labels for better readability
- plt.gca().xaxis.set_major_formatter(plt.matplotlib.dates.DateFormatter('%Y-%m-%d %H:%M:%S')) # format the x-axis ticks
- plt.legend(prop={'size': 10})
- plt.grid(True)
- plt.tight_layout()
- plt.show()
- return filtpressure,filtpressure_avg
- #%%
- def find_occurrences(series_of_sums, begintime, target_values, percentages, plot=False):
- # Initialize dictionaries to store the times when each target value is triggered
- occurrences_dict = {target_value: [] for target_value in target_values}
- range_triggered = {target_value: [] for target_value in target_values}
- # Calculate the time sampling frequency
- fs = 10
- Time = [begintime + timedelta(sec for i in range(len(series_of_sums))]
- # Initialize a list to store the total trigger status at each timestamp
- total_trigger_status = [0] * len(series_of_sums)
- # Iterate through each sum in the series
- for target_value_index, sum_value in enumerate(series_of_sums):
- # Check if the sum is close to any of the target values
- for target_value, percentage in zip(target_values, percentages):
- if abs(sum_value - target_value) <= percentage * target_value: # Check if the difference is within the specified percentage
- range_triggered[target_value].append(Time[target_value_index]) # Append the time
- occurrences_dict[target_value].append(Time[target_value_index]) # Append the time
- total_trigger_status[target_value_index] = 1 # Set total trigger status to 1
- # Find transitions in total trigger status
- transiti
- for i in range(1, len(total_trigger_status)):
- if total_trigger_status[i - 1] == 0 and total_trigger_status[i] != 0:
- transitions.append(i) # Store index of transition from 0 to non-zero
- elif total_trigger_status[i - 1] != 0 and total_trigger_status[i] == 0:
- transitions.append(i - 1) # Store index of transition from non-zero to 0 (adjusted index)
- if plot:
- plt.figure(figsize=(10, 6))
- #for target_value, occurrences in occurrences_dict.items():
- #plt.scatter(occurrences, [1] * len(occurrences), color='blue', marker='o', label=f'Target Value {target_value}')
- # Plot vertical lines for transitions from 0 to non-zero
- for transition_index in transitions:
- transiti
- if total_trigger_status[transition_index-1] == 0:
- plt.axvline(x=transition_time, color='green', line linewidth=0.5) # Add vertical line for 0 to non-zero transition
- else:
- plt.axvline(x=transition_time, color='red', line linewidth=0.5) # Add vertical line for non-zero to 0 transition
- plt.xlabel('Time')
- plt.title('Transitions in Trigger Status from 0 to a Non-zero Number and Vice Versa')
- plt.grid(True)
- plt.xticks(rotati
- plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M:%S'))
- plt.tight_layout()
- plt.show()
- return occurrences_dict, range_triggered