% Calculate left and right maximum and negative maximums

clear

%% Load all *.mat files

fdir = '/Users/Colin/Documents/MATLAB/DFlow/mass_Mo_chap3/bndry_Ma_vs_t';
s = what(fdir); %change dir for your directory name 
matfiles = s.mat;
for a=1:numel(matfiles)
    load(char(matfiles(a)))
end

%% Left FP

[lb_Ma_max(1,1), lb_Ma_max_i(1,1)] = max(lb_Ma_t_s180pback(1,:));
[lb_Ma_max(1,2), lb_Ma_max_i(1,2)] = max(lb_Ma_t_m180pback(1,:));
[lb_Ma_max(1,3), lb_Ma_max_i(1,3)] = max(lb_Ma_t_l180pback(1,:));
[lb_Ma_max(1,4), lb_Ma_max_i(1,4)] = max(lb_Ma_t_s300pback(1,:));
[lb_Ma_max(1,5), lb_Ma_max_i(1,5)] = max(lb_Ma_t_m300pback(1,:));
[lb_Ma_max(1,6), lb_Ma_max_i(1,6)] = max(lb_Ma_t_l300pback(1,:));

[lb_Ma_max(2,1), lb_Ma_max_i(2,1)] = min(lb_Ma_t_s180pback(1,:));
[lb_Ma_max(2,2), lb_Ma_max_i(2,2)] = min(lb_Ma_t_m180pback(1,:));
[lb_Ma_max(2,3), lb_Ma_max_i(2,3)] = min(lb_Ma_t_l180pback(1,:));
[lb_Ma_max(2,4), lb_Ma_max_i(2,4)] = min(lb_Ma_t_s300pback(1,:));
[lb_Ma_max(2,5), lb_Ma_max_i(2,5)] = min(lb_Ma_t_m300pback(1,:));
[lb_Ma_max(2,6), lb_Ma_max_i(2,6)] = min(lb_Ma_t_l300pback(1,:));

for i = lb_Ma_max_i(1,1):577
    if lb_Ma_t_s180pback(1,i) < 0 && lb_Ma_t_s180pback(1,i-1) > 0
        lb_Ma_max_i(3,1) = i;
        break
    end
end
for i = lb_Ma_max_i(1,2):577
    if lb_Ma_t_m180pback(1,i) < 0 && lb_Ma_t_m180pback(1,i-1) > 0
        lb_Ma_max_i(3,2) = i;
        break
    end
end
for i = lb_Ma_max_i(1,3):577
    if lb_Ma_t_l180pback(1,i) < 0 && lb_Ma_t_l180pback(1,i-1) > 0
        lb_Ma_max_i(3,3) = i;
        break
    end
end

for i = lb_Ma_max_i(1,4):577
    if lb_Ma_t_s300pback(1,i) < 0 && lb_Ma_t_s300pback(1,i-1) > 0
        lb_Ma_max_i(3,4) = i;
        break
    end
end
for i = lb_Ma_max_i(1,5):577
    if lb_Ma_t_m300pback(1,i) < 0 && lb_Ma_t_m300pback(1,i-1) > 0
        lb_Ma_max_i(3,5) = i;
        break
    end
end
for i = lb_Ma_max_i(1,6):577
    if lb_Ma_t_l300pback(1,i) < 0 && lb_Ma_t_l300pback(1,i-1) > 0
        lb_Ma_max_i(3,6) = i;
        break
    end
end

%% Right FP

[rb_Ma_max(1,1), rb_Ma_max_i(1,1)] = max(rb_Ma_t_s180pback(1,:));
[rb_Ma_max(1,2), rb_Ma_max_i(1,2)] = max(rb_Ma_t_m180pback(1,:));
[rb_Ma_max(1,3), rb_Ma_max_i(1,3)] = max(rb_Ma_t_l180pback(1,:));
[rb_Ma_max(1,4), rb_Ma_max_i(1,4)] = max(rb_Ma_t_s300pback(1,:));
[rb_Ma_max(1,5), rb_Ma_max_i(1,5)] = max(rb_Ma_t_m300pback(1,:));
[rb_Ma_max(1,6), rb_Ma_max_i(1,6)] = max(rb_Ma_t_l300pback(1,:));

[rb_Ma_max(2,1), rb_Ma_max_i(2,1)] = min(rb_Ma_t_s180pback(1,:));
[rb_Ma_max(2,2), rb_Ma_max_i(2,2)] = min(rb_Ma_t_m180pback(1,:));
[rb_Ma_max(2,3), rb_Ma_max_i(2,3)] = min(rb_Ma_t_l180pback(1,:));
[rb_Ma_max(2,4), rb_Ma_max_i(2,4)] = min(rb_Ma_t_s300pback(1,:));
[rb_Ma_max(2,5), rb_Ma_max_i(2,5)] = min(rb_Ma_t_m300pback(1,:));
[rb_Ma_max(2,6), rb_Ma_max_i(2,6)] = min(rb_Ma_t_l300pback(1,:));

for i = rb_Ma_max_i(1,1):577
    if rb_Ma_t_s180pback(1,i) < 0 && rb_Ma_t_s180pback(1,i-1) > 0
        rb_Ma_max_i(3,1) = i;
        break
    end
end
for i = rb_Ma_max_i(1,2):577
    if rb_Ma_t_m180pback(1,i) < 0 && rb_Ma_t_m180pback(1,i-1) > 0
        rb_Ma_max_i(3,2) = i;
        break
    end
end
for i = rb_Ma_max_i(1,3):577
    if rb_Ma_t_l180pback(1,i) < 0 && rb_Ma_t_l180pback(1,i-1) > 0
        rb_Ma_max_i(3,3) = i;
        break
    end
end

for i = rb_Ma_max_i(1,4):577
    if rb_Ma_t_s300pback(1,i) < 0 && rb_Ma_t_s300pback(1,i-1) > 0
        rb_Ma_max_i(3,4) = i;
        break
    end
end
for i = rb_Ma_max_i(1,5):577
    if rb_Ma_t_m300pback(1,i) < 0 && rb_Ma_t_m300pback(1,i-1) > 0
        rb_Ma_max_i(3,5) = i;
        break
    end
end
for i = rb_Ma_max_i(1,6):577
    if rb_Ma_t_l300pback(1,i) < 0 && rb_Ma_t_l300pback(1,i-1) > 0
        rb_Ma_max_i(3,6) = i;
        break
    end
end

%% Load all *.mat files

fdir = '/Users/Colin/Documents/MATLAB/DFlow/mass_Mo_chap3/bndry_Mo_vs_t';
s = what(fdir); %change dir for your directory name 
matfiles = s.mat;
for a=1:numel(matfiles)
    load(char(matfiles(a)))
end

%% Left FP

[lb_Moflux_max(1,1), lb_Mo_max_i(1,1)] = max(lb_Moflux_t_s180pback(1,:));
[lb_Moflux_max(1,2), lb_Mo_max_i(1,2)] = max(lb_Moflux_t_m180pback(1,:));
[lb_Moflux_max(1,3), lb_Mo_max_i(1,3)] = max(lb_Moflux_t_l180pback(1,:));
[lb_Moflux_max(1,4), lb_Mo_max_i(1,4)] = max(lb_Moflux_t_s300pback(1,:));
[lb_Moflux_max(1,5), lb_Mo_max_i(1,5)] = max(lb_Moflux_t_m300pback(1,:));
[lb_Moflux_max(1,6), lb_Mo_max_i(1,6)] = max(lb_Moflux_t_l300pback(1,:));

[lb_Moflux_max(2,1), lb_Mo_max_i(2,1)] = min(lb_Moflux_t_s180pback(1,:));
[lb_Moflux_max(2,2), lb_Mo_max_i(2,2)] = min(lb_Moflux_t_m180pback(1,:));
[lb_Moflux_max(2,3), lb_Mo_max_i(2,3)] = min(lb_Moflux_t_l180pback(1,:));
[lb_Moflux_max(2,4), lb_Mo_max_i(2,4)] = min(lb_Moflux_t_s300pback(1,:));
[lb_Moflux_max(2,5), lb_Mo_max_i(2,5)] = min(lb_Moflux_t_m300pback(1,:));
[lb_Moflux_max(2,6), lb_Mo_max_i(2,6)] = min(lb_Moflux_t_l300pback(1,:));

for i = lb_Mo_max_i(1,1):577
    if lb_Moflux_t_s180pback(1,i) < 0 && lb_Moflux_t_s180pback(1,i-1) > 0
        lb_Mo_max_i(3,1) = i;
        break
    end
end
for i = lb_Mo_max_i(1,2):577
    if lb_Moflux_t_m180pback(1,i) < 0 && lb_Moflux_t_m180pback(1,i-1) > 0
        lb_Mo_max_i(3,2) = i;
        break
    end
end
for i = lb_Mo_max_i(1,3):577
    if lb_Moflux_t_l180pback(1,i) < 0 && lb_Moflux_t_l180pback(1,i-1) > 0
        lb_Mo_max_i(3,3) = i;
        break
    end
end

for i = lb_Mo_max_i(1,4):577
    if lb_Moflux_t_s300pback(1,i) < 0 && lb_Moflux_t_s300pback(1,i-1) > 0
        lb_Mo_max_i(3,4) = i;
        break
    end
end
for i = lb_Mo_max_i(1,5):577
    if lb_Moflux_t_m300pback(1,i) < 0 && lb_Moflux_t_m300pback(1,i-1) > 0
        lb_Mo_max_i(3,5) = i;
        break
    end
end
for i = lb_Mo_max_i(1,6):577
    if lb_Moflux_t_l300pback(1,i) < 0 && lb_Moflux_t_l300pback(1,i-1) > 0
        lb_Mo_max_i(3,6) = i;
        break
    end
end

%% Right FP

[rb_Moflux_max(1,1), rb_Mo_max_i(1,1)] = max(rb_Moflux_t_s180pback(1,:));
[rb_Moflux_max(1,2), rb_Mo_max_i(1,2)] = max(rb_Moflux_t_m180pback(1,:));
[rb_Moflux_max(1,3), rb_Mo_max_i(1,3)] = max(rb_Moflux_t_l180pback(1,:));
[rb_Moflux_max(1,4), rb_Mo_max_i(1,4)] = max(rb_Moflux_t_s300pback(1,:));
[rb_Moflux_max(1,5), rb_Mo_max_i(1,5)] = max(rb_Moflux_t_m300pback(1,:));
[rb_Moflux_max(1,6), rb_Mo_max_i(1,6)] = max(rb_Moflux_t_l300pback(1,:));

[rb_Moflux_max(2,1), rb_Mo_max_i(2,1)] = min(rb_Moflux_t_s180pback(1,:));
[rb_Moflux_max(2,2), rb_Mo_max_i(2,2)] = min(rb_Moflux_t_m180pback(1,:));
[rb_Moflux_max(2,3), rb_Mo_max_i(2,3)] = min(rb_Moflux_t_l180pback(1,:));
[rb_Moflux_max(2,4), rb_Mo_max_i(2,4)] = min(rb_Moflux_t_s300pback(1,:));
[rb_Moflux_max(2,5), rb_Mo_max_i(2,5)] = min(rb_Moflux_t_m300pback(1,:));
[rb_Moflux_max(2,6), rb_Mo_max_i(2,6)] = min(rb_Moflux_t_l300pback(1,:));

for i = rb_Mo_max_i(1,1):577
    if rb_Moflux_t_s180pback(1,i) < 0 && rb_Moflux_t_s180pback(1,i-1) > 0
        rb_Mo_max_i(3,1) = i;
        break
    end
end
for i = rb_Mo_max_i(1,2):577
    if rb_Moflux_t_m180pback(1,i) < 0 && rb_Moflux_t_m180pback(1,i-1) > 0
        rb_Mo_max_i(3,2) = i;
        break
    end
end
for i = rb_Mo_max_i(1,3):577
    if rb_Moflux_t_l180pback(1,i) < 0 && rb_Moflux_t_l180pback(1,i-1) > 0
        rb_Mo_max_i(3,3) = i;
        break
    end
end

for i = rb_Mo_max_i(1,4):577
    if rb_Moflux_t_s300pback(1,i) < 0 && rb_Moflux_t_s300pback(1,i-1) > 0
        rb_Mo_max_i(3,4) = i;
        break
    end
end
for i = rb_Mo_max_i(1,5):577
    if rb_Moflux_t_m300pback(1,i) < 0 && rb_Moflux_t_m300pback(1,i-1) > 0
        rb_Mo_max_i(3,5) = i;
        break
    end
end
for i = rb_Mo_max_i(1,6):577
    if rb_Moflux_t_l300pback(1,i) < 0 && rb_Moflux_t_l300pback(1,i-1) > 0
        rb_Mo_max_i(3,6) = i;
        break
    end
end

%% Final times

for j = 1:3
    for k = 1:6
        lb_Ma_max_times(j,k) = xs_time(lb_Ma_max_i(j,k),1)/3600;
        rb_Ma_max_times(j,k) = xs_time(rb_Ma_max_i(j,k),1)/3600;
        lb_Mo_max_times(j,k) = xs_time(lb_Mo_max_i(j,k),1)/3600;
        rb_Mo_max_times(j,k) = xs_time(rb_Mo_max_i(j,k),1)/3600;
    end
end



