function Lec02_01_file(fn)
% LEC02_01_FILE: Either requests a file name or uses one
% passed to it.
% Matlab test of file operations
% This function reads GPS position files generated by the MIT processing
% software ensum (file names start with mb_ and end in 1/2 or 3)
%
fid = 0;
if nargin == 0 
	filename = input('Open file: ','s');
else
	filename = fn
end
[fid, message] = fopen(filename,'r');
if fid == -1
	disp(message)
	ok = 0;
	return
end	
% Now we will try to read the file.  These
% files have three header lines and then data
%
% Read the header lines
H1 = fgetl(fid);
H2 = fgetl(fid);
disp(H2)
H3 = fgetl(fid);
% The rest of the file is numeric with three values per lines and we
% can read all at once
[Data,tot] = fscanf(fid,' %f %f %f');
% Close the data file at this point
fclose(fid);
% Now reshape the Data into 3:tot/3 array
num = tot/3;
Data = reshape(Data,3,num);

% Now make a simple plot of the data
hold off
figure(1)
errorbar(Data(1,:),Data(2,:),Data(3,:),'ro'); 
hold on
hp = plot(Data(1,:),Data(2,:),'bo'); % Save the graphics handle
set(hp,'MarkerFaceColor',[1 1 0]);
figure(1)

%display('Press any key to continue')
%pause 
%disp('Removing large sigma points')
hq = questdlg('Paused: Remove large sigmas next');
%set(hq,'Position',[200 200 300 50]);

% Now use the find command to remove points with large error bars
GoodSig = find(Data(3,:)<0.020);
Data = [Data(1,GoodSig); Data(2,GoodSig); Data(3,GoodSig)];
hold off
errorbar(Data(1,:),Data(2,:),Data(3,:),'ro'); 
hold on
hp = plot(Data(1,:),Data(2,:),'bo'); % Save the graphics handle
set(hp,'MarkerFaceColor',[1 1 1]);

%disp('Press eny key to continue')
%pause
questdlg('Paused: Remove mean next');
% We might want to demean and change the units to mm as well
MeanData = mean(Data(2,:));
fprintf(1,'Removing mean of %8.4f m from data\n',MeanData)
Data(2,:) = (Data(2,:) - MeanData)*1000;
Data(3,:) = Data(3,:)*1000;
hold off
errorbar(Data(1,:),Data(2,:),Data(3,:),'ro'); 
hold on
hp = plot(Data(1,:),Data(2,:),'bo'); % Save the graphics handle
set(hp,'MarkerFaceColor',[1 1 0]);
ylabel('Position (mm)'); 
tlabel = ['Data file ' strrep(filename,'_','\_') ]
%title(strrep(strcat('Data file ',filename),'_','\_'))
title(tlabel)
figure(1);






