% from_to script takes all the slate files in a folder, opens them,
% extracts key variables, and concatenates the variables into continuous
% vectors.   Ends up with a much smaller, more managable variable space.
%
% ALSO - GOES STRAIGHT TO COMMON TIME BASE!
%
% To run from_to, should already be in Logs folder.
%
% J. G. Bellingham
% Feb 13, 2014

% clear;
fix_time = 0;
interp_ok = 0;

folder = dir('20*');
mission_folder = pwd;

% Variables to concatenate, and names for resulting concatenated variables....
from_to = {'sea_water_temperature', 'ctemp',
    'sea_water_salinity', 'csalt',
    'sea_water_density' , 'cdens',
    'mole_concentration_of_nitrate_in_sea_water', 'cnit',
    'mass_concentration_of_oxygen_in_sea_water', 'co2',
    'platform_orientation', 'chead',
    'platform_pitch_angle', 'cpitch',
    'height_above_sea_floor', 'calt',
    'depth', 'cdep',
    'latitude', 'clat',
    'longitude', 'clong',
    'volume_scattering_650_nm', 'c650',
    'volume_scattering_470_nm','c470',
    'mass_concentration_of_chlorophyll_in_sea_water', 'cchl'}
nfrom_to = length(from_to);

for i_folder = 1:length(folder)
    if (folder(i_folder).isdir)
        cd(mission_folder);  % Go back to top directory...
        cd(folder(i_folder).name);  % Now open mission segment
        folder(i_folder).name
        
        % Use old loadDataLR to get data from slate...
        var_list = 1;
        do_not_delete = who;  % Remember variables from before load...
        
        try loadDataLR;
            blew_up = false;
        catch ME
            % End up if if loadDataLR blew up...
            % Go to next file, if one...
            disp('Error LR_Concat_DataServer_Slate: loadDataLR blew up...');
            disp(ME.message);
            blew_up = true;
        end;
        
        if ~blew_up
            % Determine which variables should be eliminated
            var_list = whos;
            for var_index = 1:length(var_list)
                var_list(var_index).kill = 1;
                for dont_delete_index = 1:length(do_not_delete);
                    if strcmp(var_list(var_index).name,do_not_delete{dont_delete_index})
                        var_list(var_index).kill = 0;
                    end
                end;
            end;
            
            % Concatenate the variables for the entire section...if the variable
            % does not exist yet, then create it.  After that, just tack the new
            % values on.  Make sure there is actually data to be added on,
            % however...
            
            if exist('Time') & exist('startTime')
                if ~isempty(Time)
                    if ~exist('ct')
                        ct = (Time' + startTime)/3600/24;
                    else
                        ct = [ct (Time' + startTime)/3600/24];
                    end;
                end;
                
                for j = 1:nfrom_to
                    if exist(from_to{j,1})
                        if ~exist(from_to{j,2})
                            if length(eval(from_to{j,1})) > 1
                                eval([from_to{j,2} '=' from_to{j,1} char(39) ';']);
                            end;
                        else
                            if length(eval(from_to{j,1})) > 1
                                eval([from_to{j,2} '=[' from_to{j,2} ' ' from_to{j,1} char(39) ']' ';']);
                            end;
                        end;
                    end;
                end;
                
                % Eliminate all the variables created by loadDataLR
                for var_index = 1:length(var_list)
                    if var_list(var_index).kill == 1
                        clear(var_list(var_index).name)
                    end;
                end;
            end;
        end;
    end;
end;

% Convert temperture to Centigrate!!!
ctemp = ctemp-273;

% common = whos('c*');  save('CommonTime.mat', common(1:length(common)).name);


