% Script to compute attenuance images from SES back-lit images

%% Set up paths and load SESdata.mat variables
datadir = '/Users/mestapa/Documents/Research_Maine/NSF-STTR/MBARI_SES_data/';
outdatadir = [datadir 'processed/'];
load([datadir 'SESdata.mat'],'bgr','imgdates','imgfnams');

%% Read in each image, compute the attenuance image, save it, and compute the mean across the image
for i = 1:length(imgfnams)
    infile = [datadir imgfnams{i}];
    outfile = [outdatadir imgfnams{i}(1:end-3) 'mat'];
    junk = imread(infile);
    thisimg = double(junk(:,:,1));
    atn = -log(thisimg./bgr);
    meanatn(i,1) = mean(atn(:),'omitnan');
    save(outfile,'atn');
    clear infile outfile junk thisimg atn;
    if rem(i,100)==0;disp(num2str(i));end
end    
clear i 

%% Save the output
save('SESatnsumm.mat','imgdates','imgfnams','meanatn');