%% Get the names of all files in the directory
folder = 'C:\Users\bwerb\Documents\thoon_ww\ncfiles';
fileList = dir(folder);
fileNames = {fileList.name};
fileNames = fileNames(~ismember(fileNames, {'.', '..'}));
savefile = 1;
% read all nc files and calc k0 w/ canyonB. Save as mat files.
n = length(fileNames);
for i = 1:n

    file = char(fileNames(i));
    
    [ww] = readncTHOON(file,-9.66638e-04,-4.11e-06,0); % read nc file, don't save
    
    [ww] = THOONK0Calc(ww); % recalculate k0 with CanyonB estimated pH
    
    [ww] = THOONpH_adj(ww,-1.397911490796328); % cheating. need to fix the k0 calc method
    
    % dir = char('C:\Users\bwerb\Documents\thoon_ww\mat files');
    filename = file(1:end-3);
    if savefile == 1
        % save([dir, filesep, filename,'.mat'], 'ww'); % Save the struct as a .mat file
        save([filename,'.mat'], 'ww'); % Save the struct as a .mat file
    end

end

% % % stitch all files in the directory together, calculate warm up time,
% % % k0_adj, k0_std, and pH uncertainty for full timeseries
% % [wwf] = fulltimeseriesanalysis(wwf);
% folder = 'C:\Users\bwerb\Documents\thoon_ww\mat files';
% fileList = dir(folder);
% fileNames = {fileList.name};
% fileNames = fileNames(~ismember(fileNames, {'.', '..'}));
% % % apply k0_adj to recalculate pH and resave mat files.
% k0_adj = NaN(size(fileNames));
% k0_adj_std = NaN(size(fileNames));
% for i = 1:n
% 
%     file = char(fileNames(i));
%     load(file,'ww')
% 
%     % k0_adj = -1.397911490796328;
%     k0_adj(i) = ww.k0_adj;
%     k0_adj_std(i) = ww.k0_std;
% 
% end
% 
% k0_adj_std = std(k0_adj);
% k0_adj = mean(k0_adj);
% 
% 
% for i = 1:n
% 
%     file = char(fileNames(i));
%     load(file,'ww')
%     [ww] = THOONpH_adj(ww,k0_adj);
%     filename = file(1:end-3);
%     if savefile == 1
%         % save([dir, filesep, filename,'.mat'], 'ww'); % Save the struct as a .mat file
%         save([filename,'.mat'], 'ww'); % Save the struct as a .mat file
%     end
% end