function EPout=QC_EP(EP,savefile)
%QC Eco-Puck:

% (1) Time-series plot
% This is used to manually NaN data, for example, if the Eco-Puck sensor
%  became fouled or had barnacles attach to sensor body.

 dn = datenum([1970 1 1]);

 figEP = figure('Name','EP','Units','inches','Position',[0 0 11 7.5],...
     'CloseRequestFcn', @(h,e) fprintf(1, 'Not allowed, use "close EP force"\n.'));
 ha = tight_subplot(3,1,0.075,0.1,0.1);
 g = get(ha,'Position');
 set(ha,'Box','on','NextPlot','add');
 axes(ha(1));
   plot(EP(:,1),EP(:,2),'.-');
   ylabel(['[' char(181) 'g L^-^1]']);
   title('Chlorophyll');
   fUnNaN=@qcWG_UnNaNcb;
   fNaN=@qcWG_NaNcb;
   ha(1).UserData=zeros(length(EP(:,2)),1); 
   
   uicontrol('Units','normalized','Style','pushbutton','String','NaN',...
     'Position',[g{1}(1)+g{1}(3)-0.08 g{1}(2)+g{1}(4)+0.003 .08 .03],...
     'Callback',{fNaN,EP(:,1),EP(:,2),ha(1)});
 
   uicontrol('Units','normalized','Style','pushbutton','String','Un-NaN',...
     'Position',[g{1}(1)+g{1}(3)-0.16 g{1}(2)+g{1}(4)+0.003 .08 .03],...
     'Callback',{fUnNaN,EP(:,1),EP(:,2),ha(1)});

 axes(ha(2));
   plot(EP(:,1),EP(:,3),'.-');
   ylabel('[m^-^1]');
   title('bb470');
   ha(2).UserData=zeros(length(EP(:,2)),1);
   
   uicontrol('Units','normalized','Style','pushbutton','String','NaN',...
     'Position',[g{2}(1)+g{2}(3)-0.08 g{2}(2)+g{2}(4)+0.003 .08 .03],...
     'Callback',{fNaN,EP(:,1),EP(:,3),ha(2)});
 
   uicontrol('Units','normalized','Style','pushbutton','String','Un-NaN',...
     'Position',[g{2}(1)+g{2}(3)-0.16 g{2}(2)+g{2}(4)+0.003 .08 .03],...
     'Callback',{fUnNaN,EP(:,1),EP(:,3),ha(2)});
 
 axes(ha(3));
   plot(EP(:,1),EP(:,4),'.-');
   ylabel(['[m^-^1]']);
   title('bb650');
   ha(3).UserData=zeros(length(EP(:,2)),1);
   
   uicontrol('Units','normalized','Style','pushbutton','String','NaN',...
     'Position',[g{3}(1)+g{3}(3)-0.08 g{3}(2)+g{3}(4)+0.003 .08 .03],...
     'Callback',{fNaN,EP(:,1),EP(:,4),ha(3)});
 
   uicontrol('Units','normalized','Style','pushbutton','String','Un-NaN',...
     'Position',[g{3}(1)+g{3}(3)-0.16 g{3}(2)+g{3}(4)+0.003 .08 .03],...
     'Callback',{fUnNaN,EP(:,1),EP(:,4),ha(3)});
 
 set(ha,'XLim',[min(EP(:,1)) max(EP(:,1))] + [-1 1]*0.1);
 linkaxes(ha,'x');

 sprintf('Paused, Hit any key to save data and continue')
 pause;
 for n1=1:3
     bool=zeros(size(EP,1),1);
     for n=1:size(EP,1)
         samp=ha(n1).UserData(n,:);
         f=samp>0;
         if sum(f)==0
             bool(n)=0;
             continue
         end
         samp=samp(f);
         if samp(end)==2
             bool(n)=0;
         else
             bool(n)=1;
         end
     end
     filtered=EP(:,n1+1);
     filtered(bool==1)=NaN;
     EP(:,n1+1)=filtered;
 end

 close EP force
 
 EPout=EP;
 
%save QC'd .csv file
 time = (EP(:,1) - dn) * 86400*1000;
 fid = fopen(savefile,'w');
 fprintf(fid, ['Time[GMT/Unix*1000],chlorophyll[ug/L],bb470[1/m],' ...
   'bb650[1/m]']);
 for n=1:size(EP,1)
   fprintf(fid, '\n%.0f,%.3f,%.5f,%.5f', [time(n) EP(n,2:4)]);
 end
 fclose(fid);
 clear time fid n;
 
 clear ha figEP g;
end