%Routine for calculating mean and maximum tension values on the uppermost node in the data array
close all 
clear all
%First, load correct data file
%Pierson Moskowitz Spectrum
           H_S = [ 0.0  6.9   12.3  13.0];  %meters
           PER = [ 0.0  13.5  17.2  17.6];  %Seconds
           SPD = [ 5    17.1  27.8  29.1];  %meters/sec
          

AMP = H_S/2;   %height -> amp

  
suffix = 'IFREMER-600lbs-400d-1Snubber';   
 
  path = ['./',suffix,'/'];
  
  if(exist(path) == 7)
      fprintf(['Directory ',path,' exists\n\n']);
  else
      error([path ' not found']);
  end
    
CURRENTS{1} = [.1 .08 .07 .06 .05 .02 .01 .01 .01];
CURRENTS{2} = [.35 .27 .2 .17 .15 .05 .05 .10 .15];
CURRENTS{3} = [.7 .55 .4 .35 .3 .1 .1 .15 .3];
CURRENTS{4} = CURRENTS{3};

SNUB_LENGTHS = [8 16];

scope = 1.25;

cat_bot = 400;

for snub_length = SNUB_LENGTHS

for run_num = [1 2 3 4] 

wind_spd = SPD(run_num);  
wave_per = PER(run_num)
wave_amp = H_S(run_num);
results_filenm = [path,'Res-',num2str(run_num),'c-',num2str(scope),'s-',num2str(cat_bot),'d-',num2str(wave_amp),'A-',num2str(wave_per),'T-',num2str(wind_spd),'W-',num2str(snub_length),'SL-']



load([results_filenm,'.mat']);

xx{snub_length,run_num} = x;    %horizontal position
zz{snub_length,run_num} = z;    %vertical position  
ss{snub_length,run_num} = s;    %distance along cable, Lagrangian coordinate
TT{snub_length,run_num} = T;    %tension, each node
TT_t{snub_length,run_num} = T_t;    %tension against time, each node
tt{snub_length,run_num} = t;    %time in seconds
end
end

% Calculate mean and maximum tension values at the anchor, above the floats
% and at the snubber

for snub_length = SNUB_LENGTHS
for run_num = [1 2 3 4]
  %tension values at last node in array (top node)  
fprintf('Snubber length is %d Run number is %d\n',snub_length,run_num);
fprintf('Upper node value')

MeanTension = mean(TT_t{snub_length,run_num}(150:1501,end)) %Calculate mean starting after 30 seconds have elapsed in run (150 cells)

MaxTension = MeanTension + 4*(std(TT_t{snub_length,run_num}(150:1501,end))) %Max tension is calculated on mean + 4*std deviation

 %tension values above cable floats in array (top node - 1)
fprintf('Snubber length is %d Run number is %d\n',snub_length,run_num);
fprintf('Upper node value')

MeanTension = mean(TT_t{snub_length,run_num}(150:1501,end)) %Calculate mean starting after 30 seconds have elapsed in run (150 cells)

MaxTension = MeanTension + 4*(std(TT_t{snub_length,run_num}(150:1501,end))) %Max tension is calculated on mean + 4*std deviation


fprintf('Snubber length is %d Run number is %d\n',snub_length,run_num);
fprintf('Upper node value')

MeanTension = mean(TT_t{snub_length,run_num}(150:1501,end)) %Calculate mean starting after 30 seconds have elapsed in run (150 cells)

MaxTension = MeanTension + 4*(std(TT_t{snub_length,run_num}(150:1501,end))) %Max tension is calculated on mean + 4*std deviation

end
end