function  fig = PLplotsetup(action, varargin)

global b basepathcruise fn fhc

if nargin<1,
  action='InitializePlotsetup';
end;

feval(action,varargin{:});
return;



function InitializePlotsetup

global b fhc

vs=str2mat('Latitude','Longitude','Depth','Temperature','Salinity','Sigma-t','Fluorescence','Turbidity','PAR');

%Edit 2018-04-17  JAE
%vs=str2mat('Latitude','Longitude','Depth','Temperature','Salinity','Sigma-t','Fluorescence','Oxygen','PAR');

fhc=findobj('tag','plotsetup');
if isempty(fhc),
    fhc=figure('tag','plotsetup','units','normalized','position',[.01 .01 .98 .9]);
else
    figure(fhc);
    clf reset
end;
ts=min(b(:,1));
tf=max(b(:,1));
stdb=std(b(:,2:10))';
meanb=mean(b(:,2:10))';
ulim=meanb+4*stdb;
llim=meanb-4*stdb;
for i=1:3%Added so that default does not remove geographical data on a long transect
    if ulim(i)<max(b(:,i+1))+0.25*stdb(i),ulim(i)=max(b(:,i+1))+0.25*stdb(i);end
    if llim(i)>min(b(:,i+1))-0.25*stdb(i),llim(i)=min(b(:,i+1))-0.25*stdb(i);end
end
idx=find(ulim==llim);%check for upper and lower limits equal to each other
ulim(idx)=ulim(idx)+1;
llim(idx)=llim(idx)-1;
for j=1:9,
    ah(j)=subplot(3,3,j);
    ap(j,:)=get(ah(j),'position');
    plot(b(:,1),b(:,j+1));
    hold on;
    ph=plot([ts tf],[llim(j) llim(j)],'r');%lower limit
    set(ph,'buttondownfcn','PLplotsetup(''changelimits'',1)');
    ph=plot([ts tf],[ulim(j) ulim(j)],'r');%upper limit
    set(ph,'buttondownfcn','PLplotsetup(''changelimits'',2)');
    axissnug;
    title(deblank(vs(j,:)));tp=get(get(gca,'title'),'position');
end;
for j=1:9,
    if j==1 | j==4 | j==7,
        set(ah(j),'position',[ap(j,1)+.10 ap(j,2:4)]);
    elseif j==2 | j==5 | j==8,
        set(ah(j),'position',[ap(j,1)+.08 ap(j,2:4)]);
    elseif j==3 | j==6 | j==9,
        set(ah(j),'position',[ap(j,1)+.06 ap(j,2:4)]);
    end
end

h1 = uicontrol('Parent',fhc, ...
	'Units','normalized', ...
	'BackgroundColor',[1 1 1], ...
    'callback','PLplotsetup(''changelimits'',0)', ...
	'ListboxTop',0, ...
    'max',10, ...
    'min',1, ...
	'Position',[0.01 0.6 0.17 0.2], ...
	'String',[vs(:,1:3) blanks(9)' num2str([llim ulim],5)], ...
    'Style','edit','fontunits','normalized','fontsize',.08,'fontweight','normal', ...
    'horizontalalignment','left', ...
	'Tag','EditText MinMax', ...
	'TooltipString',['Edit min and max values and press CTL+ENTER.  The new limits are replotted.' char(10) ...
        ' Press Crop Data button to crop the data within the new limits.']);

h1 = uicontrol('Parent',fhc, ...
	'Units','normalized', ...
    'callback','PLplotsetup rescaleplot', ...
	'ListboxTop',0, ...
    'max',10, ...
    'min',1, ...
	'Position',[0.03 0.5 0.1 0.05], ...
	'String','Rescale Plot', ...
    'Style','pushbutton','fontunits','normalized','fontsize',.33,'fontweight','normal', ...
    'horizontalalignment','left', ...
	'Tag','Pushbutton rescaleplot', ...
	'TooltipString',['Replot the data using the new limits (does not change the data)']);

h1 = uicontrol('Parent',fhc, ...
	'Units','normalized', ...
    'callback','PLplotsetup previewcroppeddata', ...
	'ListboxTop',0, ...
    'max',10, ...
    'min',1, ...
	'Position',[0.03 0.4 0.1 0.05], ...
	'String','Preview Cropped', ...
    'Style','pushbutton','fontunits','normalized','fontsize',.33,'fontweight','normal', ...
    'horizontalalignment','left', ...
	'Tag','Pushbutton PreviewCroppedData', ...
	'TooltipString',['Preview data cropped to the new limits']);


h1 = uicontrol('Parent',fhc, ...
	'Units','normalized', ...
    'callback','close(gcbf);global toggle;toggle=2;', ...
	'ListboxTop',0, ...
    'max',10, ...
    'min',1, ...
	'Position',[0.03 0.3 0.1 0.05], ...
	'String','Cancel', ...
    'Style','pushbutton','fontunits','normalized','fontsize',.33,'fontweight','normal', ...
    'horizontalalignment','left', ...
	'Tag','Pushbutton Cancel', ...
	'TooltipString','Quit without changing anything');


h1 = uicontrol('Parent',fhc, ...
	'Units','normalized', ...
    'callback','PLplotsetup cropsavedata', ...
	'ListboxTop',0, ...
	'Position',[0.03 0.2 0.1 0.05], ...
	'String','Accept', ...
    'Style','pushbutton','fontunits','normalized','fontsize',.33,'fontweight','normal', ...
    'horizontalalignment','left', ...
	'Tag','Pushbutton Accept', ...
	'TooltipString','Accept new limits and crop data matrix. (saves to file if "Save Processed Data" checkbox was selected)');


h1 = uicontrol('Parent',fhc, ...
	'Units','normalized', ...
    'callback','PLplotsetup', ...
	'ListboxTop',0, ...
	'Position',[0.03 0.1 0.1 0.05], ...
	'String','Reset', ...
    'Style','pushbutton','fontunits','normalized','fontsize',.33,'fontweight','normal', ...
    'horizontalalignment','left', ...
	'Tag','Pushbutton Reset', ...
	'TooltipString','Start over again');




function changelimits(mousepress)

if mousepress,%change limits using mouse, 1 for lower limit and 2 for upper limit
    [x,y]=ginput(1);
    set(gcbo,'ydata',[y y]);
    cah=get(gcbo,'parent');%axis handle for subplot where mouse was pressed
    tit=get(get(cah,'title'),'string'); tit=tit(1:3);%first 3 chars of title
    lims=get(findobj(gcbf,'tag','EditText MinMax'),'string');
    idx=strmatch(tit,lims);%finds the row that needs changing
    limn=str2num(lims(:,4:end));
    limn(idx,mousepress)=y;
    set(findobj(gcbf,'tag','EditText MinMax'),'string',[lims(:,1:3) blanks(9)' num2str(limn,5)]);
else %limits were changed using the Edit Text GUI
    lims=get(gcbo,'string');
    limn=str2num(lims(:,4:end));
    ah=flipud(get(gcbf,'children'));%makes the order of subplot handles the same as the edit box list
    for j=1:9,
        ch=get(ah(j),'children');
        set(ch(1),'ydata',[limn(j,2) limn(j,2)]);%repositions the upper limit
        set(ch(2),'ydata',[limn(j,1) limn(j,1)]);%repositions the lower limit
    end
end


function rescaleplot

lims=get(findobj(gcbf,'tag','EditText MinMax'),'string');
limn=str2num(lims(:,4:end));
ah=flipud(get(gcbf,'children'));%makes the order of subplot handles the same as the edit box list
for j=1:9,
    f=(limn(j,2)-limn(j,1))*.05;
    set(ah(j),'ylim',[limn(j,1)-f limn(j,2)+f]);
end


function previewcroppeddata

lims=get(findobj(gcbf,'tag','EditText MinMax'),'string');
limn=str2num(lims(:,4:end));

ah=flipud(get(gcbf,'children'));
for j=1:9,
    ch=get(ah(j),'children');
    ydata=get(ch(3),'ydata');
    xdata=get(ch(3),'xdata');
    if j==1,idxall=ones(size(ydata));end
    idx1=ydata>=limn(j,1)&ydata<limn(j,2);
    idxall=idx1&idxall;
end
for j=1:9,
    ch=get(ah(j),'children');
    ydata=get(ch(3),'ydata');
    xdata=get(ch(3),'xdata');
    set(ch(3),'ydata',ydata(idxall),'xdata',xdata(idxall));
    f=(limn(j,2)-limn(j,1))*.05;
    set(ah(j),'ylim',[limn(j,1)-f limn(j,2)+f]);
end


function cropsavedata

global b basepathcruise fn lims

lims=get(findobj(gcbf,'tag','EditText MinMax'),'string');
limn=str2num(lims(:,4:end));
for j=1:9, b=b((b(:,j+1)>=limn(j,1)&b(:,j+1)<=limn(j,2)),:); end%crops rows in b that are outside limits
close(gcbf)