<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 math


def adjust_wave_direction(direction):
    #OFFSET = 180.
    OFFSET = 0. # appears that Guillaume has fixed this
    new_direction = direction + OFFSET
    if new_direction &gt; 360:
        new_direction -= 360
    return new_direction

def process_currents():
   n_days = 7

   df = pd.read_csv(
    '/tmp/CR6_EOL2,0_Current.dat',
    skiprows=[0, 2, 3],
    header=0,
    delimiter=',',            # Specify delimiter if needed
    na_values='NAN',          # Handle missing values
    dtype={'RECORD': int}     # Specify data types if necessary
   )

   depths = [4,7,10,13,16,19,22,25,28,31,34,37,40,43,46,49,52,55,58,61]
   speed_factor = 1e+4 # convert into realistic looking values (m/s)

   # Prepare data for contour plot
   times = mdates.date2num(df['TIMESTAMP'])  # Convert timestamps to numerical format for plotting
   adcp_temperature = df['ADCP_temperature']

   # Filter the columns that contain "NB_cell" in their names
   #depth_columns = [col for col in df.columns if "NB_cell" in col]
   #depths = np.arange(1, len(depth_columns) + 1)  # Depths corresponding to each depth cell

   # Create a meshgrid for the contour plot
   time_grid, depth_grid = np.meshgrid(times, depths)
   speed_columns = [col for col in df.columns if "current_speed_adcp" in col]
   speed_grid = np.array([df[col] for col in speed_columns])
   speed_grid = speed_grid/speed_factor
   
   # Filter to get the last n_days data
   end_time = np.max(time_grid)
   start_time = end_time - n_days
   
   # Create a mask for the time grid
   n_time_elements = np.where(times &gt;= start_time)
   
   # Create new filtered grids
   filtered_time_grid = time_grid[:,n_time_elements[0][0:np.max(n_time_elements)]]
   filtered_speed_grid = speed_grid[:,n_time_elements[0][0:np.max(n_time_elements)]]  # This may need adjustment based on your speed_grid's shape
   filtered_depth_grid = depth_grid[:,n_time_elements[0][0:np.max(n_time_elements)]]  # Assuming depth_grid doesn't change with time

   # Plotting the contour plot
   plt.figure(figsize=(16, 9))
   contour = plt.contourf(filtered_time_grid, filtered_depth_grid, filtered_speed_grid, cmap='viridis', levels=20)
   plt.colorbar(contour, label='Current Speed (m/s)')

   # Formatting the plot
   plt.title('Latest L4 Current Speed Profile')
   plt.xlabel('Time')
   plt.ylabel('Depth (m)')
   plt.gca().invert_yaxis()  # Invert Y-axis to have depth increasing downwards
   plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M'))  # Format the x-axis for dates
   plt.xticks(rotation=45)
   plt.grid(True)
   
   # Save the plot
   plt.tight_layout()
   plt.savefig('/tmp/adcp_contour_plot_last_week.png', format='png', dpi=300)
   
   # Plot ADCP temperature as a proxy for SST
   # Current year
   current_year = datetime.datetime.now().year

   # Start and end of the current year
   start_of_year = datetime.datetime(current_year, 1, 1)
   end_of_year = datetime.datetime(current_year, 12, 31)
   
   plt.figure(figsize=(6.5,2.20), dpi=100)
   fig, ax1 = plt.subplots(figsize=(6.5,2.20))
   ax1.plot_date(times, adcp_temperature, '.', color='purple', markersize=5, label='SST ($^{o}$C')
   ax1.set_ylabel('SST ($^{o}$C', color='purple', fontsize=8)
   ax1.tick_params(axis='y', labelcolor='purple', size=7)
   ax1.set_title('L4 Buoy', fontsize=10)
   ax1.set_ylim(5, 25)
   # Set custom y-ticks and add grid lines
   plt.yticks(np.arange(5,26, 5)) 
   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('%m/%y'))
   ax1.xaxis.set_major_locator(mdates.MonthLocator(interval=1))
   # Set x-axis range to the current year (Jan 1 to Dec 31)
   ax1.set_xlim([start_of_year, end_of_year])

   #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

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

   # Save the dataframe to useable csv file
   df.to_csv('/tmp/L4_adcp_currents.csv', index=False)
   
   latest_adcp_temperature = np.array(adcp_temperature[times == end_time])

   return latest_adcp_temperature[0]

def process_met():
   n_days = 7

   # Read data from CSV
   df = pd.read_csv(
       '/tmp/CR6_EOL2,0_Meteo_avgd.dat',
       skiprows=[0, 2, 3],
       header=0,
       delimiter=',',  # Specify delimiter if needed
       na_values='NAN',  # Handle missing values
       dtype={'RECORD': int}  # Specify data types if necessary
   )

   # Prepare data for plots
   times = mdates.date2num(pd.to_datetime(df['TIMESTAMP']))  # Convert timestamps to numerical format for plotting
   
   air_temperature = df['temperature_digital']
   pressure = df['pressure_digital']
   humidity = df['humidity_digital']
   wind_speed = df['wind_speed_digital']
   wind_direction = df['wind_direction_digital']

   # 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)

   # Filter the data based on the time mask
   filtered_time = times[time_mask]
   filtered_air_temperature = air_temperature[time_mask]
   filtered_pressure = pressure[time_mask]
   filtered_humidity = humidity[time_mask]
   filtered_wind_speed = wind_speed[time_mask]
   filtered_wind_direction = wind_direction[time_mask]

   # Create time-series plots

   # Plot Temperature and humidity
   plt.figure(figsize=(7.0,2.20), dpi=100)
   fig, ax1 = plt.subplots(figsize=(7.0,2.20))
   ax1.plot_date(filtered_time, filtered_air_temperature, '.', color='purple', markersize=5, label='Temperature ($^{o}$C)')
   ax1.set_ylabel('Air Temperature ($^{o}$C)', color='purple', fontsize=8)
   ax1.tick_params(axis='y', labelcolor='purple', size=7)
   ax1.set_title('L4 Buoy', fontsize=10)
   ax1.set_ylim(-5,25)
   # Set custom y-ticks and add grid lines
   plt.yticks(np.arange(-5, 26, 5))  # Set y-tick marks at -5, 0, 5, 10, 15, 20, 25
   plt.grid(axis='y', linestyle='--', linewidth=0.5)  # Grid lines only for y-axis
   
   # Create a second y-axis for Humidity
   ax2 = ax1.twinx()
   ax2.plot_date(filtered_time, filtered_humidity, 'b-', label='Humidity (%)')
   ax2.set_ylabel('Humidity (%)', color='b', fontsize=8)
   ax2.tick_params(axis='y', labelcolor='b', size=7)
   ax2.set_ylim(0,100)

   # 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(rotation=45, 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/temp_hum.png', format='png', dpi=100)

   # Plot Pressure
   plt.figure(figsize=(6.5,2.20), dpi=100)
   fig, ax1 = plt.subplots(figsize=(6.5,2.20))
   ax1.plot_date(filtered_time, filtered_pressure, '.', color='purple', markersize=5, label='Pressure (hPa)')
   ax1.set_ylabel('Atmospheric Pressure (hPa)', color='purple', fontsize=8)
   ax1.tick_params(axis='y', labelcolor='purple', size=7)
   ax1.set_title('L4 Buoy', fontsize=10)
   ax1.set_ylim(950,1050)
   # Set custom y-ticks and add grid lines
   plt.yticks(np.arange(950,1051, 20)) 
   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

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

   # Plot Windspeed 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_wind_speed, '.', color='purple', markersize=5, label='Windspeed (ms$^{-1}$)')
   ax1.set_ylabel('Windspeed (ms$^{-1}$)', color='purple', fontsize=8)
   ax1.tick_params(axis='y', labelcolor='purple', size=7)
   ax1.set_title('L4 Buoy', fontsize=10)
   ax1.set_ylim(0,40)
   # Set custom y-ticks and add grid lines
   plt.yticks(np.arange(0, 41, 5))  # Set y-tick marks 
   plt.grid(axis='y', linestyle='--', linewidth=0.5)  # Grid lines only for y-axis
   
   # Create a second y-axis for Humidity
   ax2 = ax1.twinx()
   ax2.plot_date(filtered_time, filtered_wind_direction, '.', color='b', markersize=5, label='Wind Direction ($^{o}$)')
   ax2.set_ylabel('Wind Direction ($^{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

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

   # Save the dataframe to useable csv file
   df.to_csv('/tmp/L4_buoy_met.csv', index=False) 
   
   latest_pressure = np.array(pressure[times == end_time])
   latest_wind_dir = np.array(wind_direction[times == end_time])
   latest_wind_speed = np.array(wind_speed[times == end_time])
   latest_air_temperature = np.array(air_temperature[times == end_time])
   
   # Your serial date (e.g., Excel or MATLAB format)
   serial_date = times[times == end_time]

   # Excel's reference date is January 1, 1900, so we need to add days to this base date
   excel_base_date = datetime.datetime(1970, 1, 1)
   
   # Convert serial date to a proper datetime by adding the days
   date_object = excel_base_date + datetime.timedelta(days=serial_date[0])  

   # Extract the hour, day, month, year
   latest_hour = date_object.hour
   latest_day = date_object.day
   latest_month = date_object.month
   latest_year = date_object.year
   
   return latest_pressure[0], latest_wind_dir[0], latest_wind_speed[0], latest_wind_dir[0], latest_air_temperature[0], latest_hour, latest_day, latest_month, latest_year   

def process_gps():

   # Read data from CSV
   df = pd.read_csv(
       '/tmp/CR6_EOL2,0_Position.dat',
       skiprows=[0, 2, 3],
       header=0,
       delimiter=',',  # Specify delimiter if needed
       na_values='NAN',  # Handle missing values
       dtype={'RECORD': int}  # Specify data types if necessary
   )

   # Prepare data for plots
   times = mdates.date2num(pd.to_datetime(df['TIMESTAMP']))  # Convert timestamps to numerical format for plotting

   # Save the dataframe to useable csv file
   df.to_csv('/tmp/L4_buoy_GPS.csv', index=False)    

   # Filter data to get the last element
   end_time = np.max(times)
   
   latitude = df['Latitude_decimal']
   longitude = df['Longitude_decimal']
   
   # Create a mask for the time
   latest_latitude = np.array(latitude[times == end_time])
   latest_longitude = np.array(longitude[times == end_time])

   return latest_latitude[0], latest_longitude[0]

def process_waves():
   n_days = 7
   # Read data from CSV
   df = pd.read_csv(
       '/tmp/CR6_EOL2,0_Wave_sensor.dat',
       skiprows=[0, 2, 3],
       header=0,
       delimiter=',',  # Specify delimiter if needed
       na_values='NAN',  # Handle missing values
       dtype={'RECORD': int}  # Specify data types if necessary
   )

   # Prepare data for plots
   times = mdates.date2num(pd.to_datetime(df['TIMESTAMP']))  # Convert timestamps to numerical format for plotting

   # sort out the issue with the direction of the waves
   # Apply the function to the DataFrame
   df['adjusted_wave_direction'] = df['direction'].apply(adjust_wave_direction)

   # Save the dataframe to useable csv file
   df.to_csv('/tmp/L4_buoy_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_Period']
   direction = df['adjusted_wave_direction']

   # 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]
      
   # 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', 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.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', 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)
   #ax2.legend(loc='lower right', fontsize=7)

   # Save the plot
   plt.savefig('/tmp/wave_period.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])
   
   return latest_Hs[0], latest_Hmax[0], latest_peak_period[0], latest_direction[0]



def main():
   adcp_temperature = process_currents()
   pressure, wind_dir, wind_speed, wind_dir, air_temperature, hour, day, month, year = process_met()
   print(pressure, wind_dir, wind_speed, wind_dir, air_temperature, hour)
   latitude, longitude = process_gps()
   Hs, Hmax, peak_period, wave_direction = process_waves()
   
   # Replace NaN values with -9.99
   pressure = -999.99 if (pressure &lt; 900.) else pressure
   wind_dir = -9.99 if math.isnan(wind_dir) else wind_dir
   wind_speed = -9.99 if math.isnan(wind_speed) else wind_speed
   air_temperature = -9.99 if math.isnan(air_temperature) else air_temperature
   adcp_temperature = -9.99 if math.isnan(adcp_temperature) else adcp_temperature
   latitude = -99.99 if math.isnan(latitude) else latitude
   longitude = -9.99 if math.isnan(longitude) else longitude
   Hs = -9.99 if math.isnan(Hs) else Hs
   Hmax = -9.99 if math.isnan(Hmax) else Hmax
   peak_period = -9.99 if math.isnan(peak_period) else peak_period
   wave_direction = -9.99 if math.isnan(wave_direction) else wave_direction
   
   formatted_string = "%6.1f %5.2f %3.1f %5.2f %4.1f %02d %4.1f -9.999 %02d %02d %04d -9.999 -9.999 -9.999 %7.4f %7.4f -9.99 %.1f %.1f %.1f %.1f" % (pressure, wind_dir, wind_speed, wind_dir, air_temperature, hour, adcp_temperature, day, month, year, latitude, longitude, Hs, Hmax, peak_period, wave_direction)

   # Print the formatted string to a file
   with open('/tmp/buoy_latest_CR6.txt', 'w') as file:
       file.write(formatted_string)

   #pdb.set_trace()
   
if  __name__=='__main__':
   main()

   
   
   

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