function data = rdvlead(fid, verbose, select);

% function data = rdvlead(fid, verbose, select);
%	Read the variable leader data from a raw ADCP
%	data file opened for binary reading
%	Returns the contents of the variable leader
%	as elements of the vector 'data' or an
%	empty matrix if the fixed leader ID is not
%	identified (error condition).
%	If the variable select is provided as a vector
%	of zeros and ones, the function will return
%	only the elements of data which correspond to
%	a one in the vector select.  Select must be the
%   the same length as the number of fields in the
%	record, currently 32.
%	Set verbose=1 for a text output.

% Written by Marinna Martini
% for the U.S. Geological Survey
% Atlantic Marine Geology, Woods Hole, MA
% 1/7/95

NFIELDS = 32;
data=zeros(1,NFIELDS);
fld=1;  

if exist('verbose') ~= 1,
	verbose = 0;
end
if exist('select') ~= 1,
	select = [];
end

% make sure we're looking at the beginning of
% the variable leader record by testing for it's ID
data(fld)=fread(fid,1,'int16');
if(data(fld)~=128),
	disp('Variable Leader ID not found');
	data=[];
	return;
end
fld=fld+1;
% ensemble number
data(fld)=fread(fid,1,'ushort');
fld=fld+1;
% Time of ensemble
data(fld)=fread(fid,1,'uchar');
fld=fld+1;
data(fld)=fread(fid,1,'uchar');
fld=fld+1;
data(fld)=fread(fid,1,'uchar');
fld=fld+1;
data(fld)=fread(fid,1,'uchar');
fld=fld+1;
data(fld)=fread(fid,1,'uchar');
fld=fld+1;
data(fld)=fread(fid,1,'uchar');
fld=fld+1;
data(fld)=fread(fid,1,'uchar');
if verbose, disp(sprintf('Time of ensemble %d/%d/%d %d:%d:%d.%d',...
	data(fld-5), data(fld-4), data(fld-6), data(fld-3), data(fld-2),...
	data(fld-1), data(fld))); end;
fld=fld+1;
% ensemble number rollover
data(fld)=fread(fid,1,'uchar');
if verbose, disp(sprintf('Ensemble %d',data(fld-8)+(65536.*(data(fld))))); end;
fld=fld+1;
% built in test results
data(fld)=fread(fid,1,'ushort');
fld=fld+1;
% speed of sound (EC)
data(fld)=fread(fid,1,'ushort');
if verbose, disp(sprintf('Speed of sound %d m/s',data(fld))); end;
fld=fld+1;
% depth of transducer (ED) in decimeters
data(fld)=fread(fid,1,'ushort');
fld=fld+1;
% Heading (EH)
data(fld)=fread(fid,1,'uint16');
if verbose, disp(sprintf('Heading %4.2f deg.',data(fld).*0.01)); end;
fld=fld+1;
% Pitch (EP)
data(fld)=fread(fid,1,'int16');
if verbose, disp(sprintf('Pitch %4.2f deg.',data(fld).*0.01)); end;
fld=fld+1;
% Roll (ER)
data(fld)=fread(fid,1,'int16');
if verbose, disp(sprintf('Roll %4.2f deg.',data(fld).*0.01)); end
fld=fld+1;
% Salinity (ES)
data(fld)=fread(fid,1,'ushort');
if verbose, disp(sprintf('Salinity %d ppt',data(fld))); end
fld=fld+1;
% Temperature (ET)
data(fld)=fread(fid,1,'ushort');
if verbose, disp(sprintf('Temperature %4.2f deg.',data(fld).*0.01)); end
fld=fld+1;
% Maximum ping time
data(fld)=fread(fid,1,'uchar');
fld=fld+1;
data(fld)=fread(fid,1,'uchar');
fld=fld+1;
data(fld)=fread(fid,1,'uchar');
fld=fld+1;
% Accuracy (STD) of heading, pitch and roll
% heading (1 deg/count), pitch and roll (0.1 deg/count)
data(fld)=fread(fid,1,'uchar');
fld=fld+1;
data(fld)=fread(fid,1,'uchar');
fld=fld+1;
data(fld)=fread(fid,1,'uchar');
fld=fld+1;
% ADC channels
data(fld)=fread(fid,1,'uchar');
fld=fld+1;
data(fld)=fread(fid,1,'uchar');
fld=fld+1;
data(fld)=fread(fid,1,'uchar');
fld=fld+1;
data(fld)=fread(fid,1,'uchar');
fld=fld+1;
data(fld)=fread(fid,1,'uchar');
fld=fld+1;
data(fld)=fread(fid,1,'uchar');
fld=fld+1;
data(fld)=fread(fid,1,'uchar');

if length(select) == length(data),
	data(find(select==0))=[];
end
