Facebook
From Laurens, 2 Weeks ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 113
  1. def pressurepreprocesstest(pressure,res_pressure,fcutA1,fcutB1,fcutB2,fcutA2,fs,order,Atm_OST,stormname,begintime,endtime,plot=None):
  2.    
  3.    
  4.     ### Calibration from Volt to Pascal ###
  5.     #seperated calibration for each column: !! fill in calibration factors !!
  6.    
  7.     pressure_cal = pressure.copy(deep=True) #to avoid overwriting the original pressure dataframe (A1,B1,B2,A2)
  8.     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,
  9.                        7209403.272, 7165352.954, 7184994.007, 7095088.034]
  10.     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 ]
  11.     #fill in calibration factors for every sensor
  12.     for i,j in zip(pressure_cal,range(16)):      #multiply every column with its calibration factor
  13.         pressure_cal[i] = calibrationlist[j]*pressure_cal[i] + intersectionpointlist[j]
  14.        
  15.  
  16.    
  17.  
  18.    
  19.     if stormname == 'Storm 25/02/2023':
  20.     ##  SPECIFIC PER STORM: 25/02/2023
  21.         Atm_Ost_selected = Atm_OST[12:37].reset_index(drop=True)
  22.  
  23.     if stormname == 'Storm 10/03/2023':
  24.     ##  SPECIFIC PER STORM: 10/03/2023
  25.         Atm_Ost_selected = Atm_OST[72:115].reset_index(drop=True)
  26.  
  27.     if stormname == 'Storm 25/03/2023':
  28.     ##  SPECIFIC PER STORM: 10/03/2023
  29.         Atm_Ost_selected = Atm_OST[72:115].reset_index(drop=True)
  30.        
  31.     if stormname == 'Storm 23/11/2023':
  32.     ##  SPECIFIC PER STORM: 10/03/2023
  33.         Atm_Ost_selected = Atm_OST[108:171].reset_index(drop=True)
  34.     if stormname == 'Storm 24/11/2023':
  35.     ##  SPECIFIC PER STORM: 10/03/2023
  36.         Atm_Ost_selected = Atm_OST[177:244].reset_index(drop=True)
  37.    
  38.     #TimeOST =[datetime.strptime(begintime,"%d-%m-%Y %H:%M") + timedelta(minutes=i*10) for i in Atm_OST_selected.index]
  39.     res_pressure['p_atm Ostend'] = np.nan
  40.    
  41.    
  42.     i=0
  43.     j=0
  44.     while j < len(Atm_Ost_selected)-1:
  45.         res_pressure.iloc[i,6] = Atm_Ost_selected.iloc[j,1]
  46.         i = i + 10*60*10
  47.         j = j + 1
  48.    
  49.     res_pressure['p_atm Ostend'] = res_pressure['p_atm Ostend'].interpolate(method='linear')
  50.    
  51.     #from absolute to relative pressure
  52.     p_atm_Ost = res_pressure['p_atm Ostend']*100
  53.    
  54.    
  55.     pressure_rel = pressure_cal.copy(deep=True)
  56.    
  57.     pressure_rel.columns = ['A1P1','A1P2','A1P3','A1P4','B1P1','B1P2','B1P3','B1P4','B2P1', 'B2P2', 'B2P3', 'B2P4','A2P1','A2P2','A2P3','A2P4']      
  58.    
  59.    
  60.  
  61.     for column in pressure_rel:
  62.         pressure_rel[column] = pressure_rel[column].subtract(p_atm_Ost,axis=0)
  63.         print(pressure_rel[column])
  64.  
  65.     ### Low pass filtering ###
  66.    
  67.     #default: Butterworth filter
  68.     P_A1 = pressure_rel[['A1P1','A1P2','A1P3','A1P4']]
  69.     P_A1_filt = pp.df_LPFiltButter(P_A1, fcutA1,fs,order)
  70.     P_B1 = pressure_rel[['B1P1','B1P2','B1P3','B1P4']]
  71.     P_B1_filt = pp.df_LPFiltButter(P_B1, fcutB1,fs,order)
  72.     P_B2 = pressure_rel[['B2P1','B2P2','B2P3','B2P4']]
  73.     P_B2_filt = pp.df_LPFiltButter(P_B2, fcutB2,fs,order)
  74.     P_A2 = pressure_rel[['A2P1','A2P2','A2P3','A2P4']]
  75.     P_A2_filt = pp.df_LPFiltButter(P_A2,fcutA2,fs,order)
  76.    
  77.  
  78.     ### Averaging over the four pressure sensors per overtopping box ###
  79.    
  80.     P_A1_avg = P_A1_filt.mean(axis=1)
  81.     P_B1_avg = P_B1_filt.mean(axis=1)
  82.     P_B2_avg = P_B2_filt.mean(axis=1)
  83.     P_A2_avg = P_A2_filt.mean(axis=1)
  84.  
  85.     # add different averaged & filtered pressure signals into one DataFrame
  86.     filtpressure = pd.concat([P_A1_filt,P_B1_filt,P_B2_filt,P_A2_filt],axis=1)
  87.     filtpressure.columns = ['A1P1','A1P2','A1P3','A1P4','B1P1','B1P2','B1P3','B1P4','B2P1', 'B2P2', 'B2P3', 'B2P4','A2P1','A2P2','A2P3','A2P4']
  88.     filtpressure_avg = pd.concat([P_A1_avg,P_B1_avg,P_B2_avg,P_A2_avg],axis=1)
  89.     filtpressure_avg.columns = ['P_A1','P_B1','P_B2','P_A2']
  90.  
  91.    
  92.     ### Optional plotting ###
  93.     time_data = pd.to_datetime(Atm_Ost_selected.iloc[:, 0])
  94.     pressure_data = Atm_Ost_selected.iloc[:, 1]
  95.  
  96.     # Define time steps
  97.     fs = 10  # sampling frequency
  98.      Time = [begintime + timedelta(sec for i in range(len(pressure))]
  99.  
  100.     if plot == True:
  101.         # Calculate the time difference between begintime and endtime
  102.         time_diff = endtime - begintime
  103.         num_hours = int(time_diff.total_seconds() / 3600)
  104.    
  105.         # Generate x-axis ticks with appropriate spacing
  106.         x_ticks = [begintime + timedelta(hours=i) for i in range(num_hours+1)]
  107.        
  108.         # Plot both air pressure data and calibrated, filtered, and averaged pressure data on the same graph
  109.         plt.figure(figsize=(12, 8))
  110.        
  111.         # Plot air pressure data
  112.          plt.plot(time_data, pressure_data, color='purple', line label='Air Pressure Data')
  113.        
  114.         # Plot calibrated, filtered, and averaged pressure data
  115.         plt.plot(Time, pressure_rel['A1P1'], linewidth=0.3, color='red', label='Calibrated raw data of sensor P1')
  116.         plt.plot(Time, pressure['A1P1'], linewidth=0.3, color='green', label='Raw data of sensor P1')
  117.         plt.plot(Time, P_A1_filt['A1P1'], linewidth=1.5, color='blue', label='Filtered pressures of sensor P1')
  118.         plt.plot(Time, P_A1_avg, linewidth=1.5, color='black', label='Filtered pressures (average of the four sensors P1, P2, P3 and P4)')    
  119.          plt.xlabel('Time', f
  120.          plt.ylabel('Pressure', f
  121.          plt.title('Comparison of Air Pressure Data with Calibrated, Filtered, and Averaged Pressure Data', f
  122.          plt.xticks(x_ticks, rotati  # Rotate x-axis labels for better readability
  123.         plt.gca().xaxis.set_major_formatter(plt.matplotlib.dates.DateFormatter('%Y-%m-%d %H:%M:%S')) # format the x-axis ticks
  124.         plt.legend(prop={'size': 10})
  125.         plt.grid(True)
  126.         plt.tight_layout()
  127.         plt.show()
  128.     return filtpressure,filtpressure_avg
  129.    
  130. #%%
  131.  
  132. def find_occurrences(series_of_sums, begintime, target_values, percentages, plot=False):
  133.     # Initialize dictionaries to store the times when each target value is triggered
  134.     occurrences_dict = {target_value: [] for target_value in target_values}
  135.     range_triggered = {target_value: [] for target_value in target_values}
  136.    
  137.     # Calculate the time sampling frequency
  138.     fs = 10
  139.      Time = [begintime + timedelta(sec for i in range(len(series_of_sums))]
  140.    
  141.     # Initialize a list to store the total trigger status at each timestamp
  142.     total_trigger_status = [0] * len(series_of_sums)
  143.    
  144.     # Iterate through each sum in the series
  145.     for target_value_index, sum_value in enumerate(series_of_sums):
  146.         # Check if the sum is close to any of the target values
  147.         for target_value, percentage in zip(target_values, percentages):
  148.             if abs(sum_value - target_value) <= percentage * target_value:  # Check if the difference is within the specified percentage
  149.                 range_triggered[target_value].append(Time[target_value_index])  # Append the time
  150.                 occurrences_dict[target_value].append(Time[target_value_index])  # Append the time
  151.                 total_trigger_status[target_value_index] = 1  # Set total trigger status to 1
  152.    
  153.     # Find transitions in total trigger status
  154.      transiti
  155.     for i in range(1, len(total_trigger_status)):
  156.         if total_trigger_status[i - 1] == 0 and total_trigger_status[i] != 0:
  157.             transitions.append(i)  # Store index of transition from 0 to non-zero
  158.         elif total_trigger_status[i - 1] != 0 and total_trigger_status[i] == 0:
  159.             transitions.append(i - 1)  # Store index of transition from non-zero to 0 (adjusted index)
  160.    
  161.     if plot:
  162.         plt.figure(figsize=(10, 6))
  163.         #for target_value, occurrences in occurrences_dict.items():
  164.             #plt.scatter(occurrences, [1] * len(occurrences), color='blue', marker='o', label=f'Target Value {target_value}')
  165.        
  166.         # Plot vertical lines for transitions from 0 to non-zero
  167.         for transition_index in transitions:
  168.              transiti
  169.             if total_trigger_status[transition_index-1] == 0:
  170.                  plt.axvline(x=transition_time, color='green', line linewidth=0.5)  # Add vertical line for 0 to non-zero transition
  171.             else:
  172.                  plt.axvline(x=transition_time, color='red', line linewidth=0.5)  # Add vertical line for non-zero to 0 transition
  173.        
  174.         plt.xlabel('Time')
  175.         plt.title('Transitions in Trigger Status from 0 to a Non-zero Number and Vice Versa')
  176.         plt.grid(True)
  177.          plt.xticks(rotati
  178.         plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M:%S'))
  179.         plt.tight_layout()
  180.         plt.show()
  181.          
  182.     return occurrences_dict, range_triggered