function [] = cpl_update(figNum)
%
% cpl_update.m--Updates the graphical elements of the cpl GUI following a
% line drag, etc.
%
% Syntax: cpl_update(figNum)
% Developed in Matlab 7.0.1.24704 (R14) Service Pack 1 on GLNX86.
% Kevin Bartlett (kpb@uvic.ca), 2006-03-20 14:09
%-------------------------------------------------------------------------
%cpl_constants = get(figNum,'userdata');
cpl_properties = getappdata(figNum,'cpl_properties');
boxWidthPct = cpl_properties.boxWidthPct;
axLimsBufferPct = cpl_properties.axLimsBufferPct;
x = cpl_properties.x;
z = cpl_properties.z;
startIndex = cpl_properties.startIndex;
endIndex = cpl_properties.endIndex;
% Set axes limits to be slightly larger than required to contain data.
if isempty(z)
xlims = [0 1];
ylims = [0 1];
else
xlims = [min(x) - (axLimsBufferPct/100)*range(x) max(x) + (axLimsBufferPct/100)*range(x)];
ylims = [min(z) - (axLimsBufferPct/100)*range(z) max(z) + (axLimsBufferPct/100)*range(z)];
end % if
set(findobj(figNum,'Tag','full_ax'),'xlim',xlims,'ylim',ylims);
% If empty data, set line objects to be invisible and bail out now.
if isempty(z)
uiresume;
keyboard
return;
else
end % if
% Set start and end axes' x-limits to the same scaling.
boxWidth = range(x) * (boxWidthPct/100);
%startXLims = [x(startIndex)-boxWidth/2 x(startIndex)+boxWidth/2];
startCropVal = unique(get(findobj(figNum,'Tag','full_ax_StartLine'),'xdata'));
startXLims = [startCropVal-boxWidth/2 startCropVal+boxWidth/2];
%set(findobj(figNum,'Tag','start_ax'),'xlim',startXLims,'ylimmode','auto');
startBoxZ = z(find(x>=startXLims(1) & x<=startXLims(2)));
% If there are one or no data points found between the current box limits,
% use adjacent points in determining suitable y-limits.
if length(startBoxZ)<2
if any(startXLims < xlims(1))
% Box lies before data; use first two data points.
startBoxZ = z(1:2);
elseif any(startXLims > xlims(2))
% Box lies after data; use last two data points.
startBoxZ = z(end-1:end);
else
% Box is between points; use points to either side.
startBoxZ = z([max(find(x<=startXLims(1))) min(find(x>=startXLims(2)))]);
end % if
end % if
%endXLims = [x(endIndex)-boxWidth/2 x(endIndex)+boxWidth/2];
endCropVal = unique(get(findobj(figNum,'Tag','full_ax_EndLine'),'xdata'));
endXLims = [endCropVal-boxWidth/2 endCropVal+boxWidth/2];
%set(findobj(figNum,'Tag','end_ax'),'xlim',endXLims,'ylimmode','auto');
endBoxZ = z(find(x>=endXLims(1) & x<=endXLims(2)));
% If there are one or no data points found between the current box limits,
% use adjacent points in determining suitable y-limits.
if length(endBoxZ)<2
if any(endXLims < xlims(1))
% Box lies before data; use first two data points.
endBoxZ = z(1:2);
elseif any(endXLims > xlims(2))
% Box lies after data; use last two data points.
endBoxZ = z(end-1:end);
else
% Box is between points; use points to either side.
endBoxZ = z([max(find(x<=endXLims(1))) min(find(x>=endXLims(2)))]);
end % if
end % if
% Find the required y-limits to show the data in the start and end axes.
%drawnow;
% startYLims = get(findobj(figNum,'Tag','start_ax'),'ylim');
% endYLims = get(findobj(figNum,'Tag','end_ax'),'ylim');
startYDataLims = [min(startBoxZ) max(startBoxZ)];
if isempty(startYDataLims)
startYDataLims = [NaN NaN];
end % if
endYDataLims = [min(endBoxZ) max(endBoxZ)];
if isempty(endYDataLims)
endYDataLims = [NaN NaN];
end % if
startYLims = startYDataLims + [-1 1] .* (axLimsBufferPct/100)*range(startYDataLims);
endYLims = endYDataLims + [-1 1] .* (axLimsBufferPct/100)*range(endYDataLims);
% Use a common y-scaling for both axes.
boxHeight = max(range(startYLims),range(endYLims));
% If the range of data in the boxes is zero, use a default value for the
% height of the boxes.
if boxHeight == 0
boxHeight = (axLimsBufferPct/100)*range(z);
end % if
startYLims = mean(startYLims) + [-boxHeight/2 boxHeight/2];
endYLims = mean(endYLims) + [-boxHeight/2 boxHeight/2];
% Want the boxes to lie completely within the full-profile axis y-limits;
% translate the boxes upwards or downwards if they are outside or close to
% outside the full-profile axis y-limits.
% ...Top of boxes should be no shallower than halfway between top of
% data and top of axis.
boxUpperLimit = (ylims(1) - min(z))/2;
% ...Bottom of boxes should be no deeper than halfway between bottom of
% data and bottom of axis.
boxLowerLimit = ylims(2) - (ylims(2) - max(z))/2;
if startYLims(1) <= boxUpperLimit
% Box top is shallower than top of full-profile axis. Put box top
% halfway between top of data and top of axis.
startYLims = startYLims + (boxUpperLimit - startYLims(1));
end % if
if startYLims(2) >= boxLowerLimit
% Box bottom is deeper than bottom of full-profile axis. Put box bottom
% halfway between bottom of data and bottom of axis.
startYLims = startYLims + (boxLowerLimit - startYLims(2));
end % if
% ...End box:
if endYLims(1) <= boxUpperLimit
% Box top is shallower than top of full-profile axis. Put box top
% halfway between top of data and top of axis.
endYLims = endYLims + (boxUpperLimit - endYLims(1));
end % if
if endYLims(2) >= boxLowerLimit
% Box bottom is deeper than bottom of full-profile axis. Put box bottom
% halfway between bottom of data and bottom of axis.
endYDataLims = endYDataLims + (boxLowerLimit - endYLims(2));
end % if
set(findobj(figNum,'Tag','start_ax'),'xlim',startXLims,'ylim',startYLims);
set(findobj(figNum,'Tag','end_ax'),'xlim',endXLims,'ylim',endYLims);
% Boxes in full-profile axes indicate regions covered by start and end
% axes.
set(findobj(figNum,'Tag','startBoxL'),'xdata',[startXLims(1) startXLims(1)],'ydata',startYLims);
set(findobj(figNum,'Tag','startBoxR'),'xdata',[startXLims(2) startXLims(2)],'ydata',startYLims);
set(findobj(figNum,'Tag','startBoxT'),'xdata',startXLims,'ydata',[startYLims(1) startYLims(1)]);
set(findobj(figNum,'Tag','startBoxB'),'xdata',startXLims,'ydata',[startYLims(2) startYLims(2)]);
set(findobj(figNum,'Tag','endBoxL'),'xdata',[endXLims(1) endXLims(1)],'ydata',endYLims);
set(findobj(figNum,'Tag','endBoxR'),'xdata',[endXLims(2) endXLims(2)],'ydata',endYLims);
set(findobj(figNum,'Tag','endBoxT'),'xdata',endXLims,'ydata',[endYLims(1) endYLims(1)]);
set(findobj(figNum,'Tag','endBoxB'),'xdata',endXLims,'ydata',[endYLims(2) endYLims(2)]);
% Start/end lines for full-profile axis:
axes(findobj(figNum,'Tag','full_ax'));
set(findobj(figNum,'Tag','full_ax_StartLine'),'ydata',ylims);
set(findobj(figNum,'Tag','full_ax_EndLine'),'ydata',ylims);
set(findobj(figNum,'Tag','start_ax_StartLine'),'ydata',startYLims);
set(findobj(figNum,'Tag','start_ax_EndLine'),'ydata',startYLims);
set(findobj(figNum,'Tag','end_ax_StartLine'),'ydata',endYLims);
set(findobj(figNum,'Tag','end_ax_EndLine'),'ydata',endYLims);
% Show what data points are selected and which aren't.
set(findobj(figNum,'Tag','full_ax_selectDepthLine'),'xdata',x(startIndex:endIndex),'ydata',z(startIndex:endIndex));
set(findobj(figNum,'Tag','start_ax_selectDepthLine'),'xdata',x(startIndex:endIndex),'ydata',z(startIndex:endIndex));
set(findobj(figNum,'Tag','end_ax_selectDepthLine'),'xdata',x(startIndex:endIndex),'ydata',z(startIndex:endIndex));
if startIndex > 1
i = 1:startIndex;
set(findobj(figNum,'Tag','full_ax_preStartDepthLine'),'xdata',x(i),'ydata',z(i));
set(findobj(figNum,'Tag','start_ax_preStartDepthLine'),'xdata',x(i),'ydata',z(i));
set(findobj(figNum,'Tag','end_ax_preStartDepthLine'),'xdata',x(i),'ydata',z(i));
else
set(findobj(figNum,'Tag','full_ax_preStartDepthLine'),'xdata',[],'ydata',[]);
set(findobj(figNum,'Tag','start_ax_preStartDepthLine'),'xdata',[],'ydata',[]);
set(findobj(figNum,'Tag','end_ax_preStartDepthLine'),'xdata',[],'ydata',[]);
end % if
if endIndex < length(x)
i = (endIndex):length(x);
set(findobj(figNum,'Tag','full_ax_postEndDepthLine'),'xdata',x(i),'ydata',z(i));
set(findobj(figNum,'Tag','start_ax_postEndDepthLine'),'xdata',x(i),'ydata',z(i));
set(findobj(figNum,'Tag','end_ax_postEndDepthLine'),'xdata',x(i),'ydata',z(i));
else
set(findobj(figNum,'Tag','full_ax_postEndDepthLine'),'xdata',[],'ydata',[]);
set(findobj(figNum,'Tag','start_ax_postEndDepthLine'),'xdata',[],'ydata',[]);
set(findobj(figNum,'Tag','end_ax_postEndDepthLine'),'xdata',[],'ydata',[]);
end % if
%keyboard