% This script takes all the 'science*.mat' 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.
%
% J. G. Bellingham
% Feb 13, 2014

clear;
fix_time = 0;
interp_ok = 0;
folder = dir('20*');

for i_folder = 1:length(folder)
    if (folder(i_folder).isdir)
        cd(folder(i_folder).name);
        folder(i_folder).name
        files = dir('science_*.mat');
        
        for i = 1:length(files)
            disp(['file ' num2str(i)]);
            % Get the variables from the file we care about...
            try load(files(i).name, 'depth'); catch; end;
            try load(files(i).name, 'volume_scattering_470_nm');catch; end;
            try load(files(i).name, 'volume_scattering_650_nm');catch; end;
            try load(files(i).name, 'mass_concentration_of_chlorophyll_in_sea_water');catch; end;
            try load(files(i).name, 'sea_water_temperature');catch; end;
            try load(files(i).name, 'sea_water_salinity');catch; end;
            try load(files(i).name, 'sea_water_density');catch; end;
            try load(files(i).name, 'mole_concentration_of_nitrate_in_sea_water');catch; end;
            try load(files(i).name, 'mole_concentration_of_oxygen_in_sea_water');catch; end;
            try load(files(i).name, 'downwelling_photosynthetic_photon_flux_in_sea_water');catch; end;
            try load(files(i).name, 'latitude');catch; end;
            try load(files(i).name, 'longitude');catch; end;
            try
                load(files(i).name, 'DVL_micro');
                alt_value = DVL_micro.height_above_sea_floor.value;
                alt_time = DVL_micro.height_above_sea_floor.time;
            catch
                try
                    load(files(i).name, 'height_above_sea_floor');
                    alt_value = height_above_sea_floor.value;
                    alt_time = height_above_sea_floor.time;
                catch end;
            end;
            try load(files(1).name, 'platform_pitch_angle');catch; end;
            try load(files(1).name, 'platform_orientation');catch; 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('depth')
                if ~exist('dep')
                    if length(depth.value) > 1
                        dep = depth.value;
                        tdep = depth.time;
                    end
                else
                    if length(depth.value) > 1
                        dep = [dep depth.value];
                        tdep = [tdep depth.time];
                    end;
                end;
            end;
            
            if exist('volume_scattering_470_nm')
                if ~exist('eco470')
                    if length(volume_scattering_470_nm.value) > 1
                        eco470 = volume_scattering_470_nm.value;
                        t470 = volume_scattering_470_nm.time;
                        eco650 = volume_scattering_650_nm.value;
                        t650 = volume_scattering_650_nm.time;
                        ecochl = mass_concentration_of_chlorophyll_in_sea_water.value;
                        tchl = mass_concentration_of_chlorophyll_in_sea_water.time;
                    end;
                else
                    if length(volume_scattering_470_nm.value) > 1
                        eco470 = [eco470 volume_scattering_470_nm.value];
                        t470 = [t470 volume_scattering_470_nm.time];
                        eco650 = [eco650 volume_scattering_650_nm.value];
                        t650 = [t650 volume_scattering_650_nm.time];
                        ecochl = [ecochl mass_concentration_of_chlorophyll_in_sea_water.value];
                        tchl = [tchl mass_concentration_of_chlorophyll_in_sea_water.time];
                    end;
                end;
            end;
            
            if exist('sea_water_temperature')
                if ~exist('swtemp')
                    if length(sea_water_temperature.value) > 1
                        swtemp = sea_water_temperature.value;
                        tsw = sea_water_temperature.time;
                        swsalt = sea_water_salinity.value;
                        swdens = sea_water_density.value;
                    end;
                else
                    if length(sea_water_temperature.value) > 1
                        swtemp = [swtemp sea_water_temperature.value];
                        tsw = [tsw sea_water_temperature.time];
                        swsalt = [swsalt sea_water_salinity.value];
                        swdens = [swdens sea_water_density.value];
                    end;
                end;
            end;
            
            if exist('mole_concentration_of_nitrate_in_sea_water')
                if ~exist('nit')
                    if length(mole_concentration_of_nitrate_in_sea_water.value) > 1
                        nit = mole_concentration_of_nitrate_in_sea_water.value;
                        tnit = mole_concentration_of_nitrate_in_sea_water.time;
                    end;
                else
                    if length(mole_concentration_of_nitrate_in_sea_water.value) > 1
                        nit = [nit mole_concentration_of_nitrate_in_sea_water.value];
                        tnit = [tnit mole_concentration_of_nitrate_in_sea_water.time];
                    end;
                end;
            end;
            
            if exist('mole_concentration_of_oxygen_in_sea_water')
                if ~exist('o2')
                    if length(mole_concentration_of_oxygen_in_sea_water.value) > 1
                        o2 = mole_concentration_of_oxygen_in_sea_water.value;
                        to2 = mole_concentration_of_oxygen_in_sea_water.time;
                    end;
                else
                    if length(mole_concentration_of_oxygen_in_sea_water.value) > 1
                        o2 = [o2 mole_concentration_of_oxygen_in_sea_water.value];
                        to2 = [to2 mole_concentration_of_oxygen_in_sea_water.time];
                    end;
                end;
            end;
            
            if exist('downwelling_photosynthetic_photon_flux_in_sea_water')
                if ~exist('par')
                    if length(downwelling_photosynthetic_photon_flux_in_sea_water.value) > 1
                        par = downwelling_photosynthetic_photon_flux_in_sea_water.value;
                        tpar = downwelling_photosynthetic_photon_flux_in_sea_water.time;
                    end;
                else
                    if length(downwelling_photosynthetic_photon_flux_in_sea_water.value) > 1
                        par = [par downwelling_photosynthetic_photon_flux_in_sea_water.value];
                        tpar = [tpar downwelling_photosynthetic_photon_flux_in_sea_water.time];
                    end;
                end;
            end;
            
            if exist('latitude')
                if ~exist('lat')
                    if length(latitude.value) > 1
                        lat = latitude.value;
                        tlat = latitude.time;
                        
                        long = longitude.value;
                        tlong = latitude.time;
                    end;
                else
                    if length(latitude.value) > 1
                        lat = [lat latitude.value];
                        tlat = [tlat latitude.time];
                        
                        long = [long longitude.value];
                        tlong = [tlong latitude.time];
                    end;
                end;
            end;
            
            if exist('alt_value')
                if ~exist('alt')
                    if length(alt_value) > 1
                        alt = alt_value;
                        talt = alt_time;
                    end
                else
                    if length(alt_value) > 1
                        alt = [alt alt_value];
                        talt = [talt alt_time];
                    end;
                end;
            end;
            
            if exist('platform_orientation')
                if ~exist('head')
                    if length(platform_orientation.value) > 1
                        head = platform_orientation.value;
                        thead = platform_orientation.time;
                    end
                else
                    if length(platform_orientation.value) > 1
                        head = [head platform_orientation.value];
                        thead = [thead platform_orientation.time];
                    end;
                end;
            end;
            
            if exist('platform_pitch_angle')
                if ~exist('pitch')
                    if length(platform_pitch_angle.value) > 1
                        pitch = platform_pitch_angle.value;
                        tpitch = platform_pitch_angle.time;
                    end
                else
                    if length(platform_pitch_angle.value) > 1
                        pitch = [pitch platform_pitch_angle.value];
                        tpitch = [tpitch platform_pitch_angle.time];
                    end;
                end;
            end;
        end;
        cd('..');
    end;
end;

% Clear variables that are not needed....

try; clear('depth'); catch end;
try; clear('volume_scattering_470_nm'); catch end;
try; clear('volume_scattering_650_nm'); catch end;
try; clear('mass_concentration_of_chlorophyll_in_sea_water'); catch end;
try; clear('sea_water_temperature'); catch end;
try; clear('sea_water_salinity'); catch end;
try; clear('sea_water_density'); catch end;
try; clear('mole_concentration_of_nitrate_in_sea_water'); catch end;
try; clear('mole_concentration_of_oxygen_in_sea_water'); catch end;
try; clear('downwelling_photosynthetic_photon_flux_in_sea_water'); catch end;
try; clear('latitude'); catch end;
try; clear('longitude'); catch end;
try; clear('DVL_micro'); catch end;
try; clear('height_above_sea_floor'); catch end;
try; clear('platform_pitch_angle'); catch end;
try; clear('platform_orientation'); catch end;
