<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">import matplotlib.dates as mdates
from scipy.interpolate import griddata
import pandas as pd
import pdb
import numpy as np
import matplotlib.pyplot as plt 
#from datetime import datetime, timedelta
import datetime
import sys

def process_waves_app():
   n_days = 7
   # Read data from CSV

   df = pd.read_csv(
    '/tmp/Waves_edge_all.txt',
    delimiter=',',            # Specify delimiter if needed
    header=None
   )

   df.columns = ['NMEA_Type','Date','Time','Hs','Hmax','Current_mean','Current_bearing','zero_crossing_frequency','Peak_wave_frequency_mlm','wave_bearing_mlm','Peak_wave_frequency','Wave_bearing','Checksum']
   df['datetime_str'] = df['Date'].astype(str) + ',' + df['Time'].astype(str).str.zfill(6)
   df['Datetime'] = pd.to_datetime(df['datetime_str'], format="%y%m%d,%H%M%S")  
    
   # Prepare data for plots
   times = mdates.date2num(pd.to_datetime(df['Datetime']))  # Convert timestamps to numerical format for plotting
   # Save the dataframe to useable csv file
   df = df[['Datetime','Hs','Hmax','Current_mean','Current_bearing','zero_crossing_frequency','Peak_wave_frequency_mlm','wave_bearing_mlm','Peak_wave_frequency','Wave_bearing']]
   
   df.to_csv('/tmp/L4_buoy_sonardyne_waves.csv', index=False) 
   
   # Filter data to get the last n_days
   end_time = np.max(times)
   start_time = end_time - n_days

   # Create a mask for the time
   time_mask = (times &gt;= start_time)

   Hs = df['Hs']
   Hmax = df['Hmax']
   peak_period = df['Peak_wave_frequency']
   direction = df['Wave_bearing']
   current = df['Current_mean']
   current_bearing = df['Current_bearing']   

   # Filter the data based on the time mask
   filtered_time = times[time_mask]
   filtered_Hs = Hs[time_mask]
   filtered_Hmax = Hmax[time_mask]
   filtered_peak_period = peak_period[time_mask]
   filtered_current = current[time_mask]
   filtered_current_bearing = current_bearing[time_mask]
      
   # Wave height (Significant height, Max height)
   plt.figure(figsize=(7.0,2.20), dpi=100)
   fig, ax1 = plt.subplots(figsize=(7.0,2.20))
   ax1.plot_date(filtered_time, filtered_Hs, '+', color='purple', markersize=5, label='Sig Height')
   ax1.plot_date(filtered_time, filtered_Hmax, '*', color='blue', markersize=5, label='Max Height')
   ax1.set_ylabel('Wave Height (m)', color='black', fontsize=8)
   ax1.tick_params(axis='y', labelcolor='black', size=7)
   ax1.set_title('L4 Buoy (Sonardyne)', fontsize=10)
   ax1.set_ylim(0, 10)
   # Set custom y-ticks and add grid lines
   #plt.yticks(np.arange(0, 1, 5))  # Set y-tick marks
   plt.grid(axis='y', linestyle='--', linewidth=0.5)  # Grid lines only for y-axis
   
   # Formatting the x-axis
   ax1.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%y \n %j'))
   ax1.xaxis.set_major_locator(mdates.DayLocator(interval=1))
   #plt.gca().xaxis.set_minor_locator(mdates.HourLocator(interval=6))  # Minor ticks for every 6 hours
   plt.minorticks_on()  # Enable minor ticks
   plt.xticks(fontsize=8)

   # Add grid and legend
   ax1.grid(True)
   fig.tight_layout()  # Adjust layout
   ax1.legend(loc='upper right', fontsize=7)
   #ax2.legend(loc='lower right', fontsize=7)

   # Save the plot
   plt.savefig('/tmp/wave_height_sonardyne.png', format='png', dpi=100)   
   #pdb.set_trace()
   
   # Wave Period 
   plt.figure(figsize=(7.0,2.20), dpi=100)
   fig, ax1 = plt.subplots(figsize=(7.0,2.20))
   ax1.plot_date(filtered_time, filtered_peak_period, '+', color='purple', markersize=5, label='Peak Period')
   ax1.set_ylabel('Wave Period (s)', color='black', fontsize=8)
   ax1.tick_params(axis='y', labelcolor='black', size=7)
   ax1.set_title('L4 Buoy (Sonardyne)', fontsize=10)
   ax1.set_ylim(0, 20)
   # Set custom y-ticks and add grid lines
   #plt.yticks(np.arange(0, 1, 5))  # Set y-tick marks
   plt.grid(axis='y', linestyle='--', linewidth=0.5)  # Grid lines only for y-axis
   
   # Formatting the x-axis
   ax1.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%y \n %j'))
   ax1.xaxis.set_major_locator(mdates.DayLocator(interval=1))
   #plt.gca().xaxis.set_minor_locator(mdates.HourLocator(interval=6))  # Minor ticks for every 6 hours
   plt.minorticks_on()  # Enable minor ticks
   plt.xticks(fontsize=8)

   # Add grid and legend
   ax1.grid(True)
   fig.tight_layout()  # Adjust layout
   ax1.legend(loc='upper right', fontsize=7)

   # Save the plot
   plt.savefig('/tmp/wave_period_sonardyne.png', format='png', dpi=100)   
   
   # Plot current speed and direction
   plt.figure(figsize=(7.0,2.20), dpi=100)
   fig, ax1 = plt.subplots(figsize=(7.0,2.20))
   ax1.plot_date(filtered_time, filtered_current, '-', color='purple', label='Current speed')
   ax1.set_ylabel('Current Speed (ms$^{-1}$)', color='purple', fontsize=8)
   ax1.tick_params(axis='y', labelcolor='purple', size=7)
   ax1.set_title('L4 Buoy (Sonardyne)', fontsize=10)
   ax1.set_ylim(0,1)
   # Set custom y-ticks and add grid lines
   plt.grid(axis='y', linestyle='--', linewidth=0.5)  # Grid lines only for y-axis
   
   # Create a second y-axis for current bearing
   ax2 = ax1.twinx()
   ax2.plot_date(filtered_time, filtered_current_bearing, '.', color='blue', markersize=5, label='Bearing')
   ax2.set_ylabel('Bearing ($^{o}$)', color='b', fontsize=8)
   ax2.tick_params(axis='y', labelcolor='b', size=7)
   ax2.set_ylim(0,360)

   # Formatting the x-axis
   ax1.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%y \n %j'))
   ax1.xaxis.set_major_locator(mdates.DayLocator(interval=1))
   #plt.gca().xaxis.set_minor_locator(mdates.HourLocator(interval=6))  # Minor ticks for every 6 hours
   plt.minorticks_on()  # Enable minor ticks
   plt.xticks(fontsize=8)

   # Add grid and legend
   ax1.grid(True)
   fig.tight_layout()  # Adjust layout
   #ax1.legend(loc='lower left', fontsize=7)
   #ax2.legend(loc='lower right', fontsize=7)

   # Save the plot
   plt.savefig('/tmp/L4_buoy_currents_sonardyne.png', format='png', dpi=100)

   latest_Hs = np.array(Hs[times == end_time])
   latest_Hmax = np.array(Hmax[times == end_time])
   latest_peak_period = np.array(peak_period[times == end_time])
   latest_direction = np.array(direction[times == end_time])
   latest_current = np.array(current[times == end_time])
   latest_current_bearing = np.array(current_bearing[times == end_time])

   return latest_Hs[0], latest_Hmax[0], latest_peak_period[0], latest_direction[0], latest_current[0], latest_current_bearing[0]



def main():

   Hs, Hmax, peak_period, wave_direction, current_speed, current_bearing = process_waves_app()
   
   sys.exit(0)
   
if  __name__=='__main__':
   main()

   
   
   

</pre></body></html>