%% Import data from text file.
% Script for importing data from the following text file:
%
%    /Users/Grace/Desktop/GOM Aug 2010 recalculated/GUST matlab
%    software/S4882 8FH4 core B_results.csv
%
% To extend the code to different selected data or a different text file,
% generate a function instead of a script.

% Auto-generated by MATLAB on 2014/05/20 12:48:47

%% Initialize variables.
filename = [
    'S5680_CoreA_results.csv';
 

    ]

station= [
        'AA';
  
       
        ]

    [r,c]=size(filename)
for i=1:r
delimiter = ',';
startRow = 2;

%% Format string for each line of text:
%   column1: double (%f)
%	column2: double (%f)
%   column3: double (%f)
%	column4: double (%f)
%   column5: double (%f)
%	column6: double (%f)
%   column7: double (%f)
%	column8: double (%f)
%   column9: double (%f)
%	column10: double (%f)
% For more information, see the TEXTSCAN documentation.
formatSpec = '%f%f%f%f%f%f%f%f%f%f%[^\n\r]';

%% Open the text file.
fileID = fopen(filename(i,:),'r');

%% Read columns of data according to format string.
% This call is based on the structure of the file used to generate this
% code. If an error occurs for a different file, try regenerating the code
% from the Import Tool.
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'EmptyValue' ,NaN,'HeaderLines' ,startRow-1, 'ReturnOnError', false);

%% Close the text file.
fclose(fileID);

%% Post processing for unimportable data.
% No unimportable data rules were applied during the import, so no post
% processing code is included. To generate code which works for
% unimportable data, select unimportable cells in a file and regenerate the
% script.

%% Allocate imported array to column variable names
step1(:,i) = dataArray{:, 1};
tau_applied(:,i) = dataArray{:, 2};
time(:,i) = dataArray{:, 3};
e_cb(:,i) = dataArray{:, 4};
e_ce(:,i) = dataArray{:, 5};
lambda(:,i) = dataArray{:, 6};
tau_ce(:,i) = dataArray{:, 7};
tau_e (:,i)= dataArray{:, 8};
E_0 (:,i)= dataArray{:, 9};
M(:,i) = dataArray{:, 10};

%% Clear temporary variables
clearvars delimiter startRow formatSpec fileID dataArray ans;
end

% Create figure
figure1 = figure;

% Create axes
axes1 = axes('Parent',figure1,'YDir','reverse');
box(axes1,'on');
hold(axes1,'all');

plot(tau_applied(:,1),e_ce(:,1),'b-*','markersize', 8)


%Linear Interpolation for 0.2 Pa and 0.4 Pa
%Tau Applied Figure 1
 
x1 = tau_applied (:,1);
y1 = e_ce (:,1);
 
xi1 = 0.2;
xii1 = 0.4;
 
interp02a = interp1 (x1, y1, xi1);
interp04a = interp1 (x1, y1, xii1);

figure (1)
hold on 
plot(xi1, interp02a, 'ro', 'MarkerFaceColor', 'r', 'MarkerSize', 8)
plot(xii1, interp04a, 'ro',  'MarkerFaceColor', 'r', 'MarkerSize', 8)


legend('AA','location','SouthWest')
xlabel('Tau Applied  (Pa)')
ylabel('Eroded Mass  (Kg/m{2})')

print (figure(1), '-dpdf', 'Mud_tau_applied_VS_eroded_M')

% Create figure 2
figure2 = figure;

% Create axes
axes2 = axes('Parent',figure2,'YDir','reverse');
box(axes2,'on');
hold(axes2,'all');



plot(tau_ce(:,1),e_ce(:,1),'b-*','markersize', 8)


%TauCrit Linear Interpolation Figure 2
 
x3 = tau_ce (3:end,1);
y3 = e_ce (3:end,1);
 
xi3 = 0.2;
xii3 = 0.4;
 
interp02c = interp1 (x3, y3, xi3);
interp04c = interp1 (x3, y3, xii3);
 
figure (2)
hold on 
plot(xi3, interp02c, 'ro', 'MarkerFaceColor', 'r', 'MarkerSize', 8)
plot(xii3, interp04c, 'ro', 'MarkerFaceColor', 'r', 'MarkerSize', 8)


legend('AA','location','SouthWest')
xlabel('Tau_{cr}  (Pa)')
ylabel('Eroded Mass  (Kg/m^{2})')

print (figure(2), '-dpdf', 'Mud_tau_crit_VS_eroded_M')

%regression curve tau_ce vs eroded mass for 2014 paper with Kevin Briggs

AA = [tau_ce(:,1),e_ce(:,1)]




figure3 = figure;

% Create axes
axes1 = axes('Parent',figure3);
box(axes1,'on');
hold(axes1,'all');

plot(AA(:,1),AA(:,2),'bo','markersize',8)




legend('AA','location','NorthWest')
xlabel('Tau_{cr}  (Pa)', 'fontsize', 16)
ylabel('Eroded Mass  (Kg/m^{2})', 'fontsize', 16, 'rotation', 90)

% Find x values for plotting the fit based on xlim
axesLimits1 = xlim(axes1);
xplot1 = linspace(axesLimits1(1), axesLimits1(2));


fitResultsAA = polyfit(AA(:,1),AA(:,2), 2);
% Evaluate polynomial
yplot1 = polyval(fitResultsAA, xplot1);
% Plot the fit
fitLine1 = plot(xplot1,yplot1,'--','linewidth', 1.5, 'DisplayName','   quadratic','Parent',axes1,...
    'Tag','quadratic',...
    'Color',[0 0 1]);




set(gca, 'Box', 'off' ); % here gca means get current axis
set(gca, 'TickDir', 'out', 'XTick', [0:0.1:0.7], 'YTick', [-.2:.2:1.6]);  %set the tick marks so they are outside the box


print (figure(3), '-dpdf', 'Mud_tau_crit_VS_eroded_M_ASregression_updated')


ouput1 = [interp02a, interp04a, interp02c, interp04c]