%
%  gliderCurrents_Generate_L1_Data.m
%
%  Purpose: Generate Level 1 data mat files for vx/vy 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 Currents data for ', strGliderName, ' Deployment ', strDeploymentNumber]);
        
        %*** READ IN DBD DATA ****
        % declare arrays for accumulating data
        final_vx = [];
        initial_vx = [];
        delta_vx = [];
        vx = [];
        prior_seg_vx = [];
        final_vy = [];
        initial_vy = [];
        delta_vy = [];
        vy = [];
        prior_seg_vy = [];
        ptime_dbd = [];
        ptime_ebd = [];
        
        % try to load all *.dbdasc files at once...
        [files, Dstruct] = wilddir(datadir, '.dbdasc');
        nfile = size(files, 1);

        for i=1:nfile-1
            % protect against empty dbd 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))
                    final_vx = [final_vx; ...
                        data.data(:,strmatch('m_final_water_vx',...
                                             data.vars, 'exact'))];
                    initial_vx = [initial_vx; ...
                        data.data(:,strmatch('m_initial_water_vx',...
                                             data.vars, 'exact'))];
                    delta_vx = [delta_vx; ...
                        data.data(:,strmatch('m_water_delta_vx',...
                                             data.vars, 'exact'))];
                    vx = [vx; ...
                        data.data(:,strmatch('m_water_vx',...
                                             data.vars, 'exact'))];
                    prior_seg_vx = [prior_seg_vx; ...
                        data.data(:,strmatch('x_prior_seg_water_vx',...
                                             data.vars, 'exact'))];
                    final_vy = [final_vy; ...
                        data.data(:,strmatch('m_final_water_vy',...
                                             data.vars, 'exact'))];
                    initial_vy = [initial_vy; ...
                        data.data(:,strmatch('m_initial_water_vy',...
                                             data.vars, 'exact'))];
                    delta_vy = [delta_vy; ...
                        data.data(:,strmatch('m_water_delta_vy',...
                                             data.vars, 'exact'))];
                    vy = [vy; ...
                        data.data(:,strmatch('m_water_vy',...
                                             data.vars, 'exact'))];
                    prior_seg_vy = [prior_seg_vy; ...
                        data.data(:,strmatch('x_prior_seg_water_vy',...
                                             data.vars, 'exact'))];
                    ptime_dbd = [ptime_dbd; ...
                        data.data(:,strmatch('m_present_time',...
                                             data.vars, 'exact'))];
                    ptime_ebd = [ptime_ebd; ...
                        data.data(:,strmatch('sci_m_present_time',...
                                             data.vars, 'exact'))];
                end

                data = [];
            end  
        end
        %*** END READ IN DBD DATA ****
        
        % remove nans from DBD data...
        i = find(~isnan(vx));
        vx = vx(i);
        final_vx = final_vx(i);
        initial_vx = initial_vx(i);
        delta_vx = delta_vx(i);
        prior_seg_vx = prior_seg_vx(i);
        vy = vy(i);
        final_vy = final_vy(i);
        initial_vy = initial_vy(i);
        delta_vy = delta_vy(i);
        prior_seg_vy = prior_seg_vy(i);
        ptime_dbd = ptime_dbd(i);
        ptime_ebd = ptime_ebd(i);
        
        % apply the sort() function to ptime_dbd
        % to make sure it increases monotonically...
        [Y,I] = sort(ptime_dbd);
        ptime_dbd = Y;
        vx = vx(I);
        final_vx = final_vx(I);
        initial_vx = initial_vx(I);
        delta_vx = delta_vx(I);
        prior_seg_vx = prior_seg_vx(I);
        vy = vy(I);
        final_vy = final_vy(I);
        initial_vy = initial_vy(I);
        delta_vy = delta_vy(I);
        prior_seg_vy = prior_seg_vy(I);
        ptime_ebd = ptime_ebd(I);

        % convert ptime into datenum style...
        ptime_dbd_datenum = (ptime_dbd/3600/24) + datenum(1970, 1, 1, 0, 0, 0);
        ptime_ebd_datenum = (ptime_ebd/3600/24) + datenum(1970, 1, 1, 0, 0, 0);

        % create configuration struct...
        units = struct( ...
            'vx', 'm/s', ...
            'final_vx', 'm/s', ...
            'initial_vx', 'm/s', ...
            'delta_vx', 'm/s', ...
            'prior_seg_vx', 'm/s', ...
            'vy', 'm/s', ...
            'final_vy', 'm/s', ...
            'initial_vy', 'm/s', ...
            'delta_vy', 'm/s', ...
            'prior_seg_vy', 'm/s', ...
            'ptime_dbd', 'seconds since 0000-01-01T00:00', ...
            'ptime_dbd_datenum', 'days since 1970-01-01T00:00', ...
            'ptime_ebd', 'seconds since 0000-01-01T00:00', ...
            'ptime_ebd_datenum', 'days since 1970-01-01T00:00');
        
        variable_description = struct( ...
            'vx', 'Eastward water velocity', ...
            'final_vx', 'Final eastward water velocity', ...
            'initial_vx', 'Initial eastward water velocity', ...
            'delta_vx', 'Change in eastward water velocity', ...
            'prior_seg_vx', 'Eastward water velocity of prior segment', ...
            'vy', 'Northward water velocity', ...
            'final_vy', 'Final northward water velocity', ...
            'initial_vy', 'Initial northward water velocity', ...
            'delta_vy', 'Change in northward water velocity', ...
            'prior_seg_vy', 'Northward water velocity of prior segment', ...
            'ptime_dbd', 'flight computer time', ...
            'ptime_dbd_datenum', 'flight computer date', ...
            '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, '_Currents_L1.mat');

        % save flight data to mat file...
        save(strMatFileName,...
             'config', ...
             'vx', ...
             'final_vx', ...
             'initial_vx', ...
             'delta_vx', ...
             'prior_seg_vx', ...
             'vy', ...
             'final_vy', ...
             'initial_vy', ...
             'delta_vy', ...
             'prior_seg_vy', ...
             'ptime_dbd', ...
             'ptime_dbd_datenum', ...
             'ptime_ebd', ...
             'ptime_ebd_datenum');

    end
end