graph_path = "/Users/natalie/Documents/year 3/IP/code/graphs";


%% variables
S = [1.0000    0.3201    0.2223; 
     0.1568    1.0000    0.37
     0.1095    0.4000    1.0000];

% experimental values
% S = [1.0000    0.5957    0.4641;
%      0.3588    1.0000    0.4479;
%      0.1834    0.2621    1.0000];

S = min(1, S*a + eye(3));

spill = true;
%run_id = "nonstandard_1.5spill_1.2correction";

files = ["z001_t001.cam1.tif"  "z001_t001.cam2.tif"  "z001_t001.cam3.tif"  "z001_t001.cam4.tif"];

%% making the images

output_prefix = strjoin([graph_path "/" run_id], "");
mkdir(output_prefix);

if spill
    bayer_r = [S(1, 1), S(1, 2); S(1,2), S(1, 3)];
    bayer_g = [S(2, 1) S(2, 2); S(2, 2), S(2, 3)];
    bayer_b = [S(3, 1), S(3, 2); S(3, 2), S(3, 3)];
    tcf_calib(run_id, bayer_r, bayer_g, bayer_b)
else
    tcf_calib(run_id)
end

fprintf("tcf_calib done\n");
rgb_sig_init(run_id)
fprintf("rgb_sig_init done\n");
rgb_sig(run_id)
fprintf("rgb_sig done\n");


if spill
    janitor(run_id, S*b, files)
    fprintf("janitor done\n");
end

if spill
    proc_rgblpt(run_id, spill)
end

proc_rgblpt(run_id, false)

%% postprocessing
if spill
    figure()
    cleanmean = bulk_velocity(run_id, spill);
    saveas(gca, strjoin([output_prefix, "bulk_vel_nospill.png"], "/"))

    figure()
    bl_plot(run_id, spill, "o")
    grid()
    saveas(gca, strjoin([output_prefix, "boundary_layer_nospill.png"], "/"))
    
    % clean_int = turb_int(run_id, spill);
    wall_units(run_id, spill, graph_path);
    turb_int(run_id, spill, graph_path);

end

figure()
spillmean = bulk_velocity(run_id, false);
saveas(gca, strjoin([output_prefix, "bulk_vel_spill.png"], "/"))

figure()
bl_plot(run_id, false, "o")
grid()
saveas(gca, strjoin([output_prefix, "boundary_layer_spill.png"], "/"))
%messy_int = turb_int(run_id, false);
wall_units(run_id, false, graph_path);
turb_int(run_id, false, graph_path);


fileID = fopen(strjoin([output_prefix, "report.txt"], "/"),'w');
fprintf(fileID, "datetime: %s\n", datestr(datetime));
if spill
    fprintf(fileID,"%d %d %d\n" ,S(1, 1), S(1, 2), S(1, 3));
    fprintf(fileID,"%d %d %d\n" ,S(2, 1), S(2, 2), S(2, 3));
    fprintf(fileID,"%d %d %d\n" ,S(3, 1), S(3, 2), S(3, 3));
    fprintf(fileID, "\n");
end
fprintf(fileID, "mean velocity (unclean): %d \n", spillmean);
if spill
    fprintf(fileID, "mean velocity (clean): %d \n", cleanmean);
end
fprintf(fileID, "\n");
%fprintf(fileID, "unclean rms fluctuations:\nu: %d\nv: %d\nw: %d\n", messy_int(1), messy_int(2), messy_int(3));
%if spill
%    fprintf(fileID, "clean rms fluctuations:\nu: %d\nv: %d\nw: %d\n", clean_int(1), clean_int(2), clean_int(3));
%end

fclose(fileID);

system(sprintf("say 'done running %s'", run_id)) 