import pandas as pd
import matplotlib.pyplot as plt
import pdb
import matplotlib.dates as mdates
import numpy as np

# --- Load data ---
df = pd.read_csv("/tmp/BYL4_CAL_CTD_Level3.csv", header=0, skiprows=[1])

# --- Parse time column ---
df['time'] = pd.to_datetime(df['DateTime'])   # change 'time' if your file uses a different name

# --- Parse time column ---
df['time'] = pd.to_datetime(df['time'])

# --- Determine year and filter full year ---
year = df['time'].dt.year.min()   # or set manually, e.g. year = 2023
start = pd.Timestamp(f"{year}-01-01")
end   = pd.Timestamp(f"{year}-12-31")

df_year = df[(df['time'] >= start) & (df['time'] <= end)]

# --- Font size ---
plt.rcParams.update({'font.size': 10})

# --- Create figure (650 ?? 220 px) ---
fig, ax1 = plt.subplots(figsize=(6.5, 2.2), dpi=100)

# --- Temperature on left axis ---
ax1.plot(df_year['time'], df_year['Temperature'],
         marker='o', linestyle='none', markersize=3,
         color='red')
ax1.set_ylabel("SST ($^{\\circ}$C)", fontsize=8)


ax1.set_ylim(5, 25)
plt.yticks(np.arange(5,26, 5)) 
plt.grid(axis='y', linestyle='--', linewidth=0.5)  # Grid lines only for y-axis
ax1.set_xlim(start, end)
ax1.grid(True)

# --- Salinity on right axis ---
ax2 = ax1.twinx()
ax2.plot(df_year['time'], df_year['Salinity'],
         marker='.', linestyle='none', markersize=3,
         color='blue', alpha=0.4)
ax2.set_ylabel("Salinity (PSU)", fontsize=8)
ax2.set_ylim(34.5, 35.2)
ax2.set_xlim(start, end)

# --- Format x-axis as mm/yy ---
ax1.xaxis.set_major_formatter(mdates.DateFormatter("%m/%y"))
fig.autofmt_xdate()

# --- Title ---
plt.title("L4 Buoy", fontsize=10)

# --- Save image ---
plt.tight_layout()

plt.savefig("/tmp/temp_sal_profile.png", dpi=100)
#plt.show()


