%
%  gliderEcopuck_Generate_L1_Data.m
%
%  Purpose: Generate Level 1 data mat files for Ecopuck data.
%
%  Requires:
%  MATLAB folder - contains util
%
%  Authors:  Chris Calloway
%            Marine Sciences Department
%            UNC-Chapel Hill
%
%  Created: January 2014
%
%//////////////////////////////////////////////////////////////////////////

clear all;

% add paths for required files...
addpath('MATLAB/util/');

% populate arrays for the deployment start and end dates...
% ex. strStart(2, 3) is start date for Ramses, Deployment 3
strStart = {'26-Jan-2012', '16-Feb-2012', '16-Mar-2012'; ...
            '26-Jan-2012', '16-Feb-2012', '16-Mar-2012'};
strEnd   = {'14-Feb-2012', '08-Mar-2012', '04-Apr-2012'; ...
            '14-Feb-2012', '12-Mar-2012', '03-Apr-2012'};

% SET THE GLIDER INDEX (Pelagia = 1, Ramses = 2) ...
for gliderIndex=1:2
    
    % SET THE DEPLOYMENT NUMBER (1, 2 or 3) ...
    for deploymentNumber=1:3
        
        clearvars -except gliderIndex deploymentNumber strStart strEnd;

        % glider name string...
        if (gliderIndex==1)
            strGliderName = 'Pelagia';
        else
            strGliderName = 'Ramses';
        end

        % deployment number string...
        strDeploymentNumber = num2str(deploymentNumber);

        % deployment start date string...
        strStartDate = strStart(gliderIndex, deploymentNumber);

        % deployment end date string...
        strEndDate = strEnd(gliderIndex, deploymentNumber);

        % define the path to the glider ascii files...
        datadir = strcat('GLIDER_DATA_LEVEL0/', strGliderName, ...
                         '_Deployment', strDeploymentNumber, '/');

        disp(['Processing Ecopuck data for ', strGliderName, ' Deployment ', strDeploymentNumber]);
        
        %*** READ IN EBD DATA ****
        % declare arrays for accumulating data
        chlor = [];
        cdom = [];
        scatter = [];
        ptime_ebd = [];
        
        % different var names per glider
        switch gliderIndex
            case 1 % Pelagia
                chlor_name     = 'sci_bbfl2s_chlor_scaled';
                cdom_name      = 'sci_bbfl2s_cdom_scaled';
                scatter_name   = 'sci_bbfl2s_bb_scaled';
            case 2 % Ramses
                chlor_name     = 'sci_flbbcd_chlor_units';
                cdom_name      = 'sci_flbbcd_cdom_units';
                scatter_name   = 'sci_flbbcd_bb_units';
        end

        % try to load all *.ebdasc files at once...
        [files, Dstruct] = wilddir(datadir, '.ebdasc');
        nfile = size(files, 1);

        for i=1:nfile-1
            % protect against empty ebd file
            if(Dstruct(i).bytes>0)
                data = read_gliderasc2([datadir, files(i,:)]);

                % if the number of values (in data.data) is less than the number
                % of vars (in data.vars), this means that the data were not
                % completely read in. To correct this, pad data.data with NaNs
                % until its length equals that of data.vars...
                if (length(data.data) < length(data.vars))
                    data.data = padarray(data.data, ...
                        [0 length(data.vars)-length(data.data)], ...
                        NaN, 'post');
                end

                % concatenate variables with data...
                if(~isempty(data.data))
                    chlor = [chlor; ...
                        data.data(:,strmatch(chlor_name,...
                                             data.vars, 'exact'))];
                    cdom = [cdom; ...
                        data.data(:,strmatch(cdom_name,...
                                             data.vars, 'exact'))];
                    scatter = [scatter; ...
                        data.data(:,strmatch(scatter_name,...
                                             data.vars, 'exact'))];
                    ptime_ebd = [ptime_ebd; ...
                        data.data(:,strmatch('sci_m_present_time',...
                                             data.vars, 'exact'))];
                end

                data = [];
            end  
        end
        %*** END READ IN EBD DATA ****
        
        % remove nans from EBD data...
        i = find(~isnan(chlor));
        chlor = chlor(i);
        cdom = cdom(i);
        scatter = scatter(i);
        ptime_ebd = ptime_ebd(i);
        
        % apply the sort() function to ptime_oxy
        % to make sure it increases monotonically...
        [Y,I] = sort(ptime_ebd);
        ptime_ebd = Y;
        chlor = chlor(I);
        cdom = cdom(I);
        scatter = scatter(I);

        % convert ptime into datenum style...
        ptime_ebd_datenum = (ptime_ebd/3600/24) + datenum(1970, 1, 1, 0, 0, 0);

        % create configuration struct...
        units = struct( ...
            'chlor', '10e-6 g/l', ...
            'cdom', 'ppb', ...
            'scatter', '1/mm', ...
            'ptime_ebd', 'seconds since 0000-01-01T00:00', ...
            'ptime_ebd_datenum', 'days since 1970-01-01T00:00');
        
        variable_description = struct( ...
            'chlor', 'chlorophyll', ...
            'cdom', 'chromophoric dissolved organic matter', ...
            'scatter', 'backscatter', ...
            'ptime_ebd', 'science computer time', ...
            'ptime_ebd_datenum', 'science computer date');

        config = struct('glider_name', strGliderName,...
                        'deployment_number', strDeploymentNumber,...
                        'start_date', strStartDate,...
                        'end_date', strEndDate,...
                        'var_descriptions', variable_description,...
                        'var_units', units);

        % set Level 1 data mat file name...
        strMatFileName = strcat(strGliderName, '_Deployment', strDeploymentNumber, '_ECO_L1.mat');

        % save flight data to mat file...
        save(strMatFileName,...
             'config', ...
             'chlor', ...
             'cdom', ...
             'scatter', ...
             'ptime_ebd', ...
             'ptime_ebd_datenum');

    end
end