function PositionOut=QC_Position(Position,filename)
%QC Position:

% (1) Time-series plot
% This is used to manually NaN data, for example, if the GPS started
%  reporting bad data.
% This has been updated to manually identify BOG Line data

 dn = datenum([1970 1 1]);
 
 BogLine = zeros(size(Position(:,1)));
 figPos = figure('Name','Pos','Units','inches','Position',[0 0 11 8.5],...
     'CloseRequestFcn', @(h,e) fprintf(1, 'Not allowed, use "close Pos force"\n.'));
 ha = tight_subplot(2,1,0.075,0.2,0.1);
 g = get(ha,'Position');
 set(ha,'Box','on','NextPlot','add');
 
 axes(ha(1));
   plot(Position(:,1),Position(:,2),'.-');
   ylabel(['[' char(186) 'E]']);
   title('Longitude');
   fUnNaN=@qcWG_UnNaNcb;
   fNaN=@qcWG_NaNcb;
   ha(1).UserData=zeros(length(Position(:,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,Position(:,1),Position(:,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,Position(:,1),Position(:,2),ha(1)});
 
 axes(ha(2));
   plot(Position(:,1),Position(:,3),'.-');
   ylabel(['[' char(186) 'N]']);
   title('Latitude');
   ha(2).UserData=zeros(length(Position(:,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,Position(:,1),Position(:,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,Position(:,1),Position(:,3),ha(2)});
 
 set(ha,'XLim',[min(Position(:,1)) max(Position(:,1))] + [-1 1]*0.1);
 
 linkaxes(ha,'x');
 sprintf('Paused, Hit any key to save data and continue')
 pause;
 for n1=1:2
     bool=zeros(size(Position,1),1);
     for n=1:size(Position,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=Position(:,n1+1);
     filtered(bool==1)=NaN;
     Position(:,n1+1)=filtered;
 end

 close Pos force
 
 PositionOut=Position;

%save QC'd .csv file
 %Position
  time = (Position(:,1) - dn) * 86400*1000;
  fid = fopen(filename,'w');
  fprintf(fid, ['Time[GMT/Unix*1000],longitude[E],latitude[N]']);
  for n=1:size(Position,1)
    fprintf(fid, '\n%.0f,%.5f,%.5f', [time(n) Position(n,2:3)]);
  end
  fclose(fid);
  clear time fid n;
  
end