modeltrain_number=11;
OPT.method=1;
OPT.netcdffilename = fullfile(fileparts(which(mfilename)),'..','InputData','Buoy', 'data.nc');

%% from netCDF
if OPT.method==1
    waveheight = nc_varget(OPT.netcdffilename,'WVHT');
    waveheight(waveheight==nc_attget(OPT.netcdffilename, 'WVHT', 'FillValue'))=NaN;
    Time = nc_varget(OPT.netcdffilename,'time')+datenum(1970,1,1);
end
%% from .mat files
if OPT.method==2
    load('42007.mat');
    waveheight = D.stdmet.wave_height;
    Time = D.stdmet.time;
end

time_interval = datenum(2002,1,1,0,0,0);
waveheight(Time<time_interval)=[]; 
Time(Time<time_interval)=[];

inputdir = fullfile(fileparts(which(mfilename)),'..','InputData','Grids');
x= load(fullfile(inputdir,'x.grd'));
xas=x(1,:);
load(fullfile(fileparts(which(mfilename)),'..','Output',['Modeltrain_number_',num2str(modeltrain_number,'%02i')],'Analyses'))
load(fullfile(fileparts(which(mfilename)),'..','InputData',['Modeltrain_number_',num2str(modeltrain_number,'%02i')],'storms'))
load(fullfile(fileparts(which(mfilename)),'..','InputData',['Modeltrain_number_',num2str(modeltrain_number,'%02i')],'recovery'))

for i = 1:length(Analyses)  
  SRegimes{i,1} =  Analyses{1,i}.regime;
  Height.post_storm(i) = Analyses{1,i}.recovery.islandheight(1,1);
  Post_storm_Times(i) = storms{1,i}.Katrinas.time(1,end);
  Height.pre_storm(i) = Analyses{1,i}.recovery.islandheight(1,end);
  MaxHeight.post_storm(i) = Analyses{1,i}.recovery.islandheight_max(1,1);
  MaxHeight.pre_storm(i) = Analyses{1,i}.recovery.islandheight_max(1,end);
  Width.post_storm(i) = Analyses{1,i}.recovery.islandwidth(1,1);
  Width.pre_storm(i) = Analyses{1,i}.recovery.islandwidth(1,end);
  Volume.post_storm(i) = Analyses{1,i}.recovery.islandwvolume(1,1);
  Volume.pre_storm(i) = Analyses{1,i}.recovery.islandwvolume(1,end);
  Roll_over.post_storm(i) = Analyses{1,i}.recovery.islandroll_over(1,1);
  Roll_over.pre_storm(i) = Analyses{1,i}.recovery.islandroll_over(1,end);
end
for i = 1:size(storms,2)
    Post_recovery_Times(i) = Post_storm_Times(i)  + recovery.time(1,i); 
end

figure('Name','Selected Storms');
plot(Time,waveheight);
hold on;
for i=1:size(storms,2)
    plot(storms{i}.time,storms{i}.waveheight, 'r','LineWidth',2),hold on
    datetick('x')
    plot(storms{i}.surge.time,storms{i}.surge.wl,'m')
    plot(storms{i}.Katrinas.time,storms{i}.Katrinas.surge.one(2:end),'g')
    text(storms{i}.Katrinas.time(1),(max(storms{i}.waveheight)+0.2),num2str(i),'FontSize',14,'FontWeight','bold'),hold on
end
heights = [MaxHeight.post_storm,MaxHeight.pre_storm];
width = [Width.post_storm,Width.pre_storm];
Roll_overall = [Roll_over.post_storm,Roll_over.pre_storm];
[T_x xv2] =sort([Post_storm_Times,Post_recovery_Times]);

heights = heights(xv2);
width = width(xv2);
Roll_overall = Roll_overall(xv2);


data.times.all = T_x;
data.times.post = Post_storm_Times;
data.times.pre = Post_recovery_Times;

data.elevation.all = heights;
data.elevation.post = Roll_over.post_storm;
data.elevation.pre = Roll_over.pre_storm;

data.rollover.all = Roll_overall;
data.rollover.post = MaxHeight.post_storm;
data.rollover.pre = MaxHeight.pre_storm;

data.width.all = width;
data.width.post = Width.post_storm;
data.width.pre = Width.pre_storm;

% temp = genvarname(num2str(modeltrain_number)) 

save('once','data')
clear all;
clc

% 
% 
% 
% figure('Name','Elevation above max tide')
% hold on;
% plot(T_x,heights,'LineWidth',2),hold on
% plot(Post_storm_Times,MaxHeight.post_storm,'.g','Markersize',30,'LineWidth',2)
% plot(Post_recovery_Times,MaxHeight.pre_storm,'.b','Markersize',30,'LineWidth',2)
% datetick('x')
% title('Elevation above max tide')
% xlabel('Time [years]')
% ylabel('Elevation above max tide [m]')
% legend('evolution','post-storm','pre-storm/post-recovery')
% 
% figure('Name','Roll-over distance')
% hold on;
% plot(T_x,Roll_overall,'LineWidth',2),hold on
% plot(Post_storm_Times,Roll_over.post_storm,'.g','Markersize',30,'LineWidth',2)
% plot(Post_recovery_Times,Roll_over.pre_storm,'.b','Markersize',30,'LineWidth',2)
% datetick('x')
% title('Roll-over distance')
% xlabel('Time [years]')
% ylabel('Roll-over distance [m]')
% legend('evolution','post-storm','pre-storm/post-recovery')
% 
% figure('Name','Width at high tide')
% hold on;
% plot(T_x,width,'LineWidth',2),hold on
% plot(Post_storm_Times,Width.post_storm,'.g','Markersize',30,'LineWidth',2)
% plot(Post_recovery_Times,Width.pre_storm,'.b','Markersize',30,'LineWidth',2)
% datetick('x')
% title('Width at high tide')
% xlabel('Time [years]')
% ylabel('Width at high tide [m]')
% legend('evolution','post-storm','pre-storm/post-recovery')
% 
