%%
% General Stats on waves near the Wave energy Buoy
clc
clear
close all
%%
% Load the netCDF dataset
url = 'http://thredds.cdip.ucsd.edu/thredds/dodsC/cdip/archive/156p1/156p1_historic.nc'; % historic all time

finfo = ncinfo(url); % struct to see what is there

% Read the waveHs variables
waveHs = ncread(url, 'waveHs');
wavetime = ncread(url, 'waveTime');
waveTp = ncread(url, 'waveTp');
waveDp = ncread(url, 'waveDp');

% Read the location variables

gpsLatitude = ncread(url, 'gpsLatitude');
gpsLongitude = ncread(url, 'gpsLongitude');
gpsTime = ncread(url, 'gpsTime');

% Convert time to MATLAB datetime (assuming it's in Unix time or some numeric format)
% Adjust the conversion based on the actual time units in the dataset
time_datetime = datetime(wavetime, 'ConvertFrom', 'posixtime');

%%
fig1=figure

% Subplot 1: Wave Height
sgtitle('156p1_historic.nc','Interpreter', 'none')
subplot(3,1,1);
plot(time_datetime, waveHs);
ylabel('Wave Height (m)');
title('Significant Wave Height');
grid on;

% Subplot 2: Wave Period
subplot(3,1,2);
plot(time_datetime, waveTp);
ylabel('Wave Period (s)');
title('Significant Wave Period');
grid on;

% Subplot 3: Wave Direction
subplot(3,1,3);
plot(time_datetime, waveDp);
xlabel('Time');
ylabel('Wave Direction (°)');
title('Significant Wave Direction');
grid on;
% Link the x-axes of all three subplots
linkaxes(findall(gcf, 'Type', 'axes'), 'x');

set(fig1, 'PaperUnits', 'inches'); % 2. Set units to inches
set(fig1, 'PaperSize', [8.5 11]); % 3. Set paper size to 8.5x11 inches
set(fig1, 'PaperPosition', [0.5 0.5 7.5 10]); % 4. Set the position of the figure on the paper (left, bottom, width, height)
print(fig1, 'SignificantWaveStats.pdf', '-dpdf');
%% Subplot 4: Buoy Location

% location plot
LatWEC=36.7;
LongWEC=-122.0;
fig2=figure;
subplot(1,2,1)
geoscatter(gpsLatitude,gpsLongitude,[])
hold on
geoscatter(LatWEC,LongWEC,[],"red","filled")
label = 'WEC (place holder location)';
text(LatWEC+0.0001, LongWEC+0.0002, label); % Offset applied [1]
geolimits([36.5 37],[-122.1 -121.7])
geobasemap topographic
title("Buoy location")

% direction plot
hps=subplot(1,2,2,polaraxes);
nbins = 100;
polarhistogram(waveDp*(pi/180),nbins)       
hps.ThetaZeroLocation = 'top'; % Change Angle Origin
hps.ThetaDir = 'clockwise';    % Change Angle Direction
title('Significant Wave Direction histogram')



set(fig2, 'PaperUnits', 'inches'); % 2. Set units to inches
set(fig2, 'PaperSize', [11 8.5]); % 3. Set paper size to 8.5x11 inches
set(fig2, 'PaperPosition', [0.5 0.5 10 7.5]); % 4. Set the position of the figure on the paper (left, bottom, width, height)

% Print to PDF
print(fig2, 'WaveLocationDirection.pdf', '-dpdf');
%exportgraphics(fig2,'WaveLocationDirectionVect.pdf','ContentType','vector')