%
%  gliderCTD_2DPlots.m
%
%  Purpose: Generate set of 2D plots of glider CTD data
%
%  Author:  William Stark
%           Marine Sciences Department
%           UNC-Chapel Hill
%
%  Created: 11 June 2012
%
%////////////////////////////////////////////////////////////////////////////////////

function gliderCTD_2DPlots(config, nLegEndPoints, strLegEndPoints,...
                           strDeploymentNumber, strLegNumber, legNumber, ptime_datenum,...
                           dens, densBounds, densCorrected, depth, pres,...
                           salin, salinBounds, salinCorrected,...
                           temp, tempBounds, chlor, chlorBounds,...
                           plotFlag_TS, plotFlag_TS_AllLegs, plotFlag_TS_Chlorophyll,...
                           plotFlag_Temperature, plotFlag_Salinity, plotFlag_Density,...
                           plotFlag_Chlorophyll, plotFlag_TemperatureProfile,...
                           plotFlag_SalinityProfile, plotFlag_DensityProfile,...
                           plotFlag_ChlorophyllProfile)
                       
                       

    % set glider and deployment strings for plot titles...
    strGliderName = config.glider_name;
    strDeployment = ['Deployment ', strDeploymentNumber];

    % colored markers to distinguish legs...
    coloredMarker = {'r.'; 'g.'; 'b.'; 'y.'; 'm.'; 'c.'};

    
                       
    if (plotFlag_TS_AllLegs==1)
        % TS plot for all legs (separate color for each leg)...
        lastLeg = length(nLegEndPoints);
        figure('Position', [500,500,1600,900]);
        for i=2:lastLeg
            % get start/end points strings and indices for this leg...
            nLegStart = nLegEndPoints(i, 1);
            nLegEnd = nLegEndPoints(i, 2);
            strLegStart = char(strLegEndPoints(i, 1));
            strLegEnd = char(strLegEndPoints(i, 2));

            % select data corresponding to the passed-in leg endpoints...
            temp_thisLeg = temp(nLegStart:nLegEnd);
            salinCorrected_thisLeg = salinCorrected(nLegStart:nLegEnd);

            % set marker color and plot TS for this leg...
            strColoredMarker = char(coloredMarker(i-1));
            plot(salinCorrected_thisLeg, temp_thisLeg, strColoredMarker, 'MarkerSize', 10);
            hold on;
        end
        % scale the x-axis and y-axis...
        ax = axis;
        axis([salinBounds(1) salinBounds(2) tempBounds(1) tempBounds(2)]);
        ylabel('Temperature (°C)', 'fontsize', 18);
        xlabel('Salinity (psu)', 'fontsize', 18);
        title([strGliderName, '  -  All legs for ', strDeployment, '   '], 'fontsize', 20, 'fontweight', 'bold');
        x = [ax(1):0.1:ax(2)];
        y = [ax(3):0.3:ax(4)];
        [X,Y] = meshgrid(x,y);
        rho = sw_pden(X, Y, 0, 0);
        [C, H] = contour(X, Y, rho, [1024:0.2:1028], 'k--');
        clabel(C, H, 'FontSize', 16);
    end
    
    
    
    
    % get start/end points strings and indices for this leg...
    nLegStart = nLegEndPoints(legNumber+1, 1);
    nLegEnd = nLegEndPoints(legNumber+1, 2);
    strLegStart = char(strLegEndPoints(legNumber+1, 1));
    strLegEnd = char(strLegEndPoints(legNumber+1, 2));
                       
    % select data corresponding to the passed-in leg endpoints...
    ptime_datenum = ptime_datenum(nLegStart:nLegEnd);
    pres = pres(nLegStart:nLegEnd);
    depth = depth(nLegStart:nLegEnd);
    temp = temp(nLegStart:nLegEnd);
    salin = salin(nLegStart:nLegEnd);
    salinCorrected = salinCorrected(nLegStart:nLegEnd);
    dens = dens(nLegStart:nLegEnd);
    densCorrected = densCorrected(nLegStart:nLegEnd);
    chlor = chlor(nLegStart:nLegEnd);
    
    % set leg strings for plot titles...
    if (strcmp(strLegNumber, '0'))
        strLeg = 'Complete';
    else
        strLeg = ['Leg ', strLegNumber, '   ( ', strLegStart, ' to ', strLegEnd, ' )'];
    end

    
    if (plotFlag_TS==1)
        % TS plot...
        figure('Position', [500,500,1600,900]);
        % set marker color and plot TS for this leg...
        if legNumber==0
            strColoredMarker = char(coloredMarker(1));
        else
            strColoredMarker = char(coloredMarker(legNumber));
        end
        plot(salinCorrected, temp, strColoredMarker, 'MarkerSize', 10);
        hold on;
        % scale the x-axis and y-axis...
        ax = axis;
        axis([salinBounds(1) salinBounds(2) tempBounds(1) tempBounds(2)]);
        ylabel('Temperature (°C)', 'fontsize', 18);
        xlabel('Salinity (psu)', 'fontsize', 18);
%         legend('Original', 'Corrected');
        title([strGliderName, ' , ', strDeployment, ' , ', strLeg, '  '], 'fontsize', 20, 'fontweight', 'bold');
        x = [ax(1):0.1:ax(2)];
        y = [ax(3):0.3:ax(4)];
        [X,Y] = meshgrid(x,y);
        rho = sw_pden(X, Y, 0, 0);
        [C, H] = contour(X, Y, rho, [1024:0.2:1028], 'k--');
        clabel(C, H, 'FontSize', 16);
    end

    
    if (plotFlag_TS_Chlorophyll==1)    
        % TS plot with color mapped to chlorophyll...
        figure('Position', [500,500,1600,900]);
        ccplot(salinCorrected, temp, chlor, chlorBounds, '.', 10);
        hold on;
        hc = colorbar;
        set(hc, 'fontsize', 12);
        % scale the x-axis and y-axis...
        ax = axis;
        axis([salinBounds(1) salinBounds(2) tempBounds(1) tempBounds(2)]);
        ylabel('Temperature (°C)', 'fontsize', 18);
        xlabel('Salinity (psu)', 'fontsize', 18);
        title([strGliderName, ' , ', strDeployment, ' , ', strLeg, '  with CHLOROPHYLL (ug/l)  '], 'fontsize', 20, 'fontweight', 'bold');
        x = [ax(1):0.1:ax(2)];
        y = [ax(3):0.3:ax(4)];
        [X,Y] = meshgrid(x,y);
        rho = sw_pden(X, Y, 0, 0);
        [C, H] = contour(X, Y, rho, [1024:0.2:1028], 'k--');
        clabel(C, H, 'FontSize', 16);
    end
    
    if (plotFlag_Temperature==1)
        % temperature vs depth...
        figure('Position', [500,500,1600,900]);
        plot(temp, depth, 'b.');
        grid on;
        % scale the x-axis...
        ax = axis;
        axis([tempBounds(1) tempBounds(2) ax(3) ax(4)]);
        set(gca, 'fontsize', 13, 'ydir', 'reverse');
        ylabel('Depth (m)', 'fontsize', 18);
        xlabel('Temperature (°C)', 'fontsize', 18);
        title(['TEMPERATURE  -  ', strGliderName, ' , ', strDeployment, ' , ', strLeg, '  '], 'fontsize', 20, 'fontweight', 'bold');
    end
    
    if (plotFlag_Salinity==1)
        % corrected salinity vs depth...
        figure('Position', [500,500,1600,900]);
        plot(salinCorrected, depth, 'b.');
        grid on;
        % scale the x-axis...
        ax = axis;
        axis([salinBounds(1) salinBounds(2) ax(3) ax(4)]);
        set(gca, 'fontsize', 13, 'ydir', 'reverse');
        ylabel('Depth (m)', 'fontsize', 18);
        xlabel('Salinity (psu)', 'fontsize', 18);
        title(['SALINITY  -  ', strGliderName, ' , ', strDeployment, ' , ', strLeg, '  '], 'fontsize', 20, 'fontweight', 'bold');
    end
    
    if (plotFlag_Density==1)
        % corrected density vs depth...
        figure('Position', [500,500,1600,900]);
        plot(densCorrected, depth, 'b.');
        grid on;
        % scale the x-axis...
        ax = axis;
        axis([densBounds(1) densBounds(2) ax(3) ax(4)]);
        set(gca, 'fontsize', 13, 'ydir', 'reverse');
        ylabel('Depth (m)', 'fontsize', 18);
        xlabel('Density (kg m-3)', 'fontsize', 18);
        title(['DENSITY  -  ', strGliderName, ' , ', strDeployment, ' , ', strLeg, '  '], 'fontsize', 20, 'fontweight', 'bold');
    end
    
    if (plotFlag_Chlorophyll==1)
        % chlorophyll vs depth...
        figure('Position', [500,500,1600,900]);
        plot(chlor, depth, 'b.');
        grid on;
        % scale the x-axis...
        ax = axis;
        axis([chlorBounds(1) chlorBounds(2) ax(3) ax(4)]);
        set(gca, 'fontsize', 13, 'ydir', 'reverse');
        ylabel('Depth (m)', 'fontsize', 18);
        xlabel('Chlorophyll (ug/l)', 'fontsize', 18);
        title(['CHLOROPHYLL  -  ', strGliderName, ' , ', strDeployment, ' , ', strLeg, '  '], 'fontsize', 20, 'fontweight', 'bold');
    end
        
    if (plotFlag_TemperatureProfile==1)
        % temperature profile...
        figure('Position', [500,500,1600,900]);
        ccplot(ptime_datenum, depth, temp, tempBounds, '.', 10);
        set(gca, 'fontsize', 12, 'ydir', 'reverse');
        datetick('x', 6, 'keeplimits', 'keepticks');
        hc = colorbar;
        set(hc, 'fontsize', 12);
        ylabel('Depth (m)', 'fontsize', 18);
        xlabel('Temperature (°C)', 'fontsize', 18);
        title(['TEMPERATURE  -  ', strGliderName, ' , ', strDeployment, ' , ', strLeg, '  '], 'fontsize', 20, 'fontweight', 'bold');
    end

    if (plotFlag_SalinityProfile==1)
        % corrected salinity profile...
        figure('Position', [500,500,1600,900]);
        ccplot(ptime_datenum, depth, salinCorrected, salinBounds, '.', 10);
        set(gca, 'fontsize', 12, 'ydir', 'reverse');
        datetick('x', 6, 'keeplimits', 'keepticks');
        hc = colorbar;
        set(hc, 'fontsize', 12);
        ylabel('Depth (m)', 'fontsize', 18);
        xlabel('Salinity (psu)', 'fontsize', 18);
        title(['SALINITY  -  ', strGliderName, ' , ', strDeployment, ' , ', strLeg, '  '], 'fontsize', 20, 'fontweight', 'bold');
    end

    if (plotFlag_DensityProfile==1)
        % corrected density profile...
        figure('Position', [500,500,1600,900]);
        ccplot(ptime_datenum, depth, densCorrected, densBounds, '.', 10);
        set(gca, 'fontsize', 12, 'ydir', 'reverse');
        datetick('x', 6, 'keeplimits', 'keepticks');
        hc = colorbar;
        set(hc, 'fontsize', 12);
        ylabel('Depth (m)', 'fontsize', 18);
        xlabel('Density (kg m-3)', 'fontsize', 18);
        title(['DENSITY  -  ', strGliderName, ' , ', strDeployment, ' , ', strLeg, '  '], 'fontsize', 20, 'fontweight', 'bold');
    end

    if (plotFlag_ChlorophyllProfile==1)
        % chlorophyll profile...
        figure('Position', [500,500,1600,900]);
        ccplot(ptime_datenum, depth, chlor, chlorBounds, '.', 10);
        set(gca, 'fontsize', 12, 'ydir', 'reverse');
        datetick('x', 6, 'keeplimits', 'keepticks');
        hc = colorbar;
        set(hc, 'fontsize', 12);
        ylabel('Depth (m)', 'fontsize', 18);
        xlabel('Chlorophyll (ug/l)', 'fontsize', 18);
        title(['CHLOROPHYLL  -  ', strGliderName, ' , ', strDeployment, ' , ', strLeg, '  '], 'fontsize', 20, 'fontweight', 'bold');
    end
        
end