%
%  gliderCTD_GeneratePlots.m
%
%  Purpose: Generate plots of glider CTD data
%
%  Author:  William Stark
%           Marine Sciences Department
%           UNC-Chapel Hill
%
%  Created: 18 June 2012
%
%//////////////////////////////////////////////////////////////////////////

function gliderCTD_GeneratePlots(gliderIndex, deploymentNumber, legNumber,...
                                 plotFlag_TS, plotFlag_TS_AllLegs,...
                                 plotFlag_Temperature, plotFlag_Salinity, plotFlag_Density,...
                                 plotFlag_TemperatureProfile,...
                                 plotFlag_SalinityProfile, plotFlag_DensityProfile,...
                                 plotFlag_Temperature3D,...
                                 plotFlag_Salinity3D, plotFlag_Density3D,...
                                 plotFlag_AllGliderTracks)
                             

    
    % add paths for required files...
    addpath('MATLAB/util/');
    addpath('MATLAB/matutil/');
    addpath('MATLAB/seawater/');
    addpath('MATLAB/plots/');
    addpath('MATLAB/strfun/');
    addpath('MATLAB/opnml/');
    addpath('MATLAB/opnml/FEM/');
    

    % glider name string...
    if (gliderIndex==1)
        strGliderName = 'Pelagia';
    else
        strGliderName = 'Ramses';
    end

    % deployment number string...
    strDeploymentNumber = num2str(deploymentNumber);

    % leg number string...
    strLegNumber = num2str(legNumber);

    % mat file path string...
    strMatFilePath = strcat('GLIDER_DATA_LEVEL1/', strGliderName, '_Deployment', strDeploymentNumber, '_CTD_L1.mat');

    
    % load glider sensor data from mat file...
    load(strMatFilePath);

    
    % set the default bounding box lat/lon's for plots
    lonlim = [-78.6 -77.8];  latlim = [32.7 33.3];


    % set start/end date-time strings and other variables required for plots of the 
    % current glider/deployment/leg combination...
    switch gliderIndex
        case 1  % Pelagia
            ztop = 0;  zbottom = -120;
            switch deploymentNumber
                case 1  % Deployment 1 (Pelagia)
                    % (row 1 is for complete deployment, row 2 is for Leg 1, etc...)
                    strLegEndPoints = {'26-Jan-2012 01:57:14', '14-Feb-2012 17:10:38';...
                                       '26-Jan-2012 01:57:14', '31-Jan-2012 12:00:00';...
                                       '31-Jan-2012 12:00:01', '08-Feb-2012 12:00:00';...
                                       '08-Feb-2012 12:00:01', '14-Feb-2012 17:10:38'};
                    strLegStart = strLegEndPoints(legNumber+1, 1);
                    strLegEnd = strLegEndPoints(legNumber+1, 2);
                case 2  % Deployment 2 (Pelagia)
                    % (row 1 is for complete deployment, row 2 is for Leg 1, etc...)
                    strLegEndPoints = {'16-Feb-2012 13:47:27', '08-Mar-2012 12:36:28';...
                                       '16-Feb-2012 13:47:27', '18-Feb-2012 10:30:00';...
                                       '18-Feb-2012 10:30:01', '20-Feb-2012 06:00:00';...
                                       '20-Feb-2012 06:00:01', '25-Feb-2012 05:30:00';...
                                       '25-Feb-2012 05:30:01', '29-Feb-2012 02:30:00';...
                                       '29-Feb-2012 02:30:01', '08-Mar-2012 12:36:28'};
                    strLegStart = strLegEndPoints(legNumber+1, 1);
                    strLegEnd = strLegEndPoints(legNumber+1, 2);
                case 3  % Deployment 3 (Pelagia)
                    % (row 1 is for complete deployment, row 2 is for Leg 1, etc...)
                    strLegEndPoints = {'16-Mar-2012 12:58:45', '04-Apr-2012 10:36:01';...
                                       '16-Mar-2012 12:58:45', '18-Mar-2012 21:40:00';...
                                       '18-Mar-2012 21:40:01', '22-Mar-2012 17:30:00';...
                                       '22-Mar-2012 17:30:01', '25-Mar-2012 14:00:00';...
                                       '25-Mar-2012 14:00:01', '28-Mar-2012 22:00:00';...
                                       '28-Mar-2012 22:00:01', '31-Mar-2012 19:00:00';...
                                       '31-Mar-2012 19:00:01', '04-Apr-2012 10:36:01'};
                    strLegStart = strLegEndPoints(legNumber+1, 1);
                    strLegEnd = strLegEndPoints(legNumber+1, 2);
            end
        case 2  % Ramses
            ztop = 0;  zbottom = -200;
            switch deploymentNumber
                case 1  % Deployment 1 (Ramses)
                    lonlim = [-78.3 -77.9];  latlim = [32.7 33.2];
                    
                    % (row 1 is for complete deployment, row 2 is for Leg 1, etc...)
                    strLegEndPoints = {'26-Jan-2012 01:06:22', '14-Feb-2012 12:35:11';...
                                       '26-Jan-2012 01:06:22', '28-Jan-2012 20:00:00';...
                                       '28-Jan-2012 20:00:01', '30-Jan-2012 03:00:00';...
                                       '30-Jan-2012 03:00:01', '31-Jan-2012 14:00:00';...
                                       '31-Jan-2012 14:00:01', '07-Feb-2012 10:30:00';...
                                       '07-Feb-2012 10:30:01', '14-Feb-2012 12:35:11'};
                    strLegStart = strLegEndPoints(legNumber+1, 1);
                    strLegEnd = strLegEndPoints(legNumber+1, 2);
                case 2  % Deployment 2 (Ramses)
                    % (row 1 is for complete deployment, row 2 is for Leg 1, etc...)
                    strLegEndPoints = {'16-Feb-2012 01:29:39', '12-Mar-2012 16:24:18';...
                                       '16-Feb-2012 01:29:39', '28-Feb-2012 12:00:00';...
                                       '28-Feb-2012 12:00:01', '04-Mar-2012 12:00:00';...
                                       '04-Mar-2012 12:00:01', '07-Mar-2012 11:30:00';...
                                       '07-Mar-2012 11:30:01', '12-Mar-2012 16:24:18'};
                    strLegStart = strLegEndPoints(legNumber+1, 1);
                    strLegEnd = strLegEndPoints(legNumber+1, 2);
                case 3  % Deployment 3 (Ramses)
                    % (row 1 is for complete deployment, row 2 is for Leg 1, etc...)
                    strLegEndPoints = {'16-Mar-2012 15:41:35', '03-Apr-2012 19:24:45';...
                                       '16-Mar-2012 15:41:35', '22-Mar-2012 12:00:00';...
                                       '22-Mar-2012 12:00:01', '27-Mar-2012 03:00:00';...
                                       '27-Mar-2012 03:00:01', '29-Mar-2012 02:00:00';...
                                       '29-Mar-2012 02:00:01', '03-Apr-2012 19:24:45'};
                    strLegStart = strLegEndPoints(legNumber+1, 1);
                    strLegEnd = strLegEndPoints(legNumber+1, 2);
            end
    end

    
    % use start/end SDNs and the ptime_datenum vector to find start/end indices...
    for i = 1:length(strLegEndPoints)
        if (i == 0)
            nLegEndPoints(i,1) = 1;
            nLegEndPoints(i,2) = length(ptime_datenum);
        else
            I = find(ptime_datenum >= datenum(strLegEndPoints(i,1)));
            nLegEndPoints(i,1) = I(1);
            I = find(ptime_datenum <= datenum(strLegEndPoints(i,2)));
            nLegEndPoints(i,2) = I(end);
        end
    end

    
    % FOR PECS TS PLOTS
    tempBounds =  [17.0 24.0];
    salinBounds = [36.0 36.7];


    % draw 2D plots...
    gliderCTD_2DPlots(config, nLegEndPoints, strLegEndPoints,...
                      strDeploymentNumber, strLegNumber, legNumber, ptime_datenum,...
                      dens, densBounds, densCorrected, depth, pres,...
                      salin, salinBounds, salinCorrected,...
                      temp, tempBounds,...
                      plotFlag_TS, plotFlag_TS_AllLegs,...
                      plotFlag_Temperature, plotFlag_Salinity, plotFlag_Density,...
                      plotFlag_TemperatureProfile,...
                      plotFlag_SalinityProfile, plotFlag_DensityProfile);

    % draw 3D plots...
    gliderCTD_3DPlots(config, nLegEndPoints, strLegEndPoints,...
                      strDeploymentNumber, strLegNumber, legNumber, lonlim, latlim,...
                      ztop, zbottom, gpsLat, gpsLon, depth, pres,...
                      salinCorrected, salinBounds, densCorrected, densBounds,...
                      temp, tempBounds,...
                      plotFlag_Temperature3D, plotFlag_Salinity3D,...
                      plotFlag_Density3D,...
                      plotFlag_AllGliderTracks);


end
                