function salts  = read_autosal_dat_wward3(this_file)
% read_autosal_dat_wward3 - Read the wward3 *.dat files
% 
% CTD Calibration toolbox
% 
% INPUT: 
%   fileName: path and name of the *.dat file to be translated.
%
% OUTPUT:
%   salts: sample structure containing the averaged autosal analysis.
%
% DESCRIPTION:	
%  There are multiple ways to approach the salinity sample data provided
% by the autosal system on the Ron Brown.  The RB system uses an automatic
% logging system which logs ten sample points for each analysis in an ascii file
% with a *.raw filenaming convention.  Typically, the analyst performs three 
% analyses per sample bottle for a total of 30 data points per sample bottle.  
% The computer logging system averages each ten element ensemble into 
% a mean value which is written to the *.xls Excel spreadsheet.  The standard
% deviation of the ten element ensemble is also recorded in the spreadsheet.
% This procedure is repeated for every sample bottle in the current session.  A 
% The beginning and ending points of a session a determined by the user/analyst.
% Each session must begin and end with a (minimum) three sample standardization
% using standard seawater of known salinity.  The before/after analyses are used
% to determine and correct for the instrumental drift during the session.  If the 
% analyst terminates the session properly, with a post standardization, each sample 
% reading recorded in the *.xls spreadsheets are then corrected automatically by
% the logging software.  
%
% There is some indication that the automatic correction procedure performed
% by the logging software is not perfect.  However, this program assumes that the
% values in the *xls files have been adequately corrected for instrumental drift
% and are without outlying values.  This is partly for pragmmatic concerns to facilitate
% the software development.  Determining the adequacy of the logging 
% software's drift correction is a subject of future work.  Please advise me of your
% experiences so that better procedures can be developed.

%
% CHANGELOG: 
%   08-Jul-2004, Version 1.0
%		* Initial version.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

error(nargchk(1,1,nargin))
% This meta-data parsing is specific to these files only....
year = 2004;
std_water_batch_label = 'P140';
[pathstr,this_name,ext,versn] = fileparts(this_file);
[mnth,day,startHr] = strread(this_name,'%02d%02d%02dn');


% First count the number of lines and pre allocated the output cell arrays.
% This is only temporary since we will separate standards from salts later.
nLines = count_lines(this_file);
nLines = nLines - 1; % skip the header line.
if isempty(nLines)
	disp(sprintf('Unable to open %s',this_file))
	disp('Passing control to the keyboard for troubleshooting.')
	keyboard
else
% Preallocate the output variables.
	station_id = NaN.*ones(nLines,1);
	fire_order = NaN.*ones(nLines,1);
	samp_nbr = station_id;
	tank_temp = fire_order;
	autosal_ratio = fire_order; % two times conductivity ratio
	autosal_ratio_corr = fire_order; % correction due to drift.
	cond_ratio = fire_order;
	salt = fire_order;
	salt_stddev = fire_order;
	hours = fire_order;
	minutes = hours;
	seconds = hours;
	niskin_btl_pos = fire_order;
	qc_flag = salt;	
	% standards
	stds_station_id = NaN.*ones(nLines,1);
	stds_fire_order = NaN.*ones(nLines,1);
	stds_samp_nbr = station_id;
	stds_tank_temp = fire_order;
	stds_autosal_ratio = fire_order; % two times conductivity ratio
	stds_autosal_ratio_corr = fire_order; % correction due to drift.
	stds_cond_ratio = fire_order;
	stds_salt = fire_order;
	stds_salt_stddev = fire_order;
	stds_hours = fire_order;
	stds_minutes = hours;
	stds_seconds = hours;
	stds_niskin_btl_pos = fire_order;
	stds_qc_flag = salt;	
end

% Open the file for reading.
fid = fopen(this_file,'r');
if fid < 0 
	disp(sprintf('Unable to open %s',this_file))
	disp('Passing control to the keyboard for troubleshooting.')
	keyboard
end

% Skip the first header line
theLine = fgetl(fid);
cnt_lines = 0;
cnt_standards = 0;
cnt_salts = 0;
while 1
	theLine = fgetl(fid);
	cnt_lines = cnt_lines+1; % 
	if theLine < 0
		fclose(fid); % end of file.
		break
	end
	theLine = splitjoin(theLine);
	% Now start saving the data..
	% First remove the colons from the time string  foor easier parsing
	theLine = strsplit(sprintf(' '),rstrrep(theLine,':',' '));

	% This effectively catches the lines with the words standards, disregard, possible error,
	% in the comments columns and removes them.
	if any(findstr(lower(strjoin(' ',theLine{:})),'standard'))
		% remove this
		theLine(strmatch('standard',lower(theLine)):end) = [];
	end
	if any(findstr(lower(strjoin(' ',theLine{:})),'disregard'))
		qc = '4';
		theLine(strmatch('disregard',lower(theLine)):end) = [];
	elseif any(findstr(lower(strjoin(' ',theLine{:})),'possible'))
		qc = '3';
		theLine(strmatch('possible',lower(theLine)):end) = [];
	elseif any(findstr(lower(strjoin(' ',theLine{:})),'no h20'))
		qc = '3';
		theLine(strmatch('no',lower(theLine)):end) = [];
	else
		qc = '1';
	end
	% NOTE: I added a column to most of the lines here.
	theLine = {theLine{:} qc};

	% This traps standard water analysis. 
	if any(findstr(strjoin(' ',theLine{:}),std_water_batch_label))
	% for this cruise, P140 was the batch label
	% the length of the lines are always 13 here except one line which has a comment in the 
	% last (14th) column.  It says Standards.  Thanks....
		cnt_standards = cnt_standards + 1;
		sample_id = '1000';
		theLine{1} = sample_id;
		isstandards = true;
	else
		cnt_salts = cnt_salts + 1;
		isstandards = false;
	end
	if length(theLine)==12
		sample_id = theLine{1};
	end
	if length(theLine)==11
		theLine = {sample_id theLine{:}};
	end
	% At this point the entire file can be assembled with all rows having an equal # of cols
	station_id(cnt_lines) = str2num(theLine{1});
	%fire_order(cnt_lines) = str2num(theLine{2});
	% NOTE: This requires that all samplebtlnbrs are numeric not alphanumeric.
	% There is one on the stc02 cruise
	if strcmp(theLine{2},'MG2')
		samp_nbr(cnt_lines) = 777;
	else
		samp_nbr(cnt_lines) = str2num(theLine{2});
	end
	%samp_nbr{cnt_lines} = theLine{3};
	tank_temp(cnt_lines) = str2num(theLine{3});
	autosal_ratio(cnt_lines) = str2num(theLine{4});
	autosal_ratio_corr(cnt_lines) = str2num(theLine{5}); % correction due to drift.
	cond_ratio(cnt_lines) = str2num(theLine{6});
	salt(cnt_lines) = str2num(theLine{7});
	salt_stddev(cnt_lines) = str2num(theLine{8});
	hours(cnt_lines) = str2num(theLine{9});
	minutes(cnt_lines) = str2num(theLine{10});
	seconds(cnt_lines) = str2num(theLine{11});
	%niskin_btl_pos(cnt_lines) = str2num(theLine{13});
	%qc_flag(cnt_lines) = str2num(theLine{14});	
	% Write it to an ascii file, then reload it and save it as a structure. 

end % while looping through this file
% replacing the second standardization bottle number
  std_idx=find(samp_nbr==1000);
  end_idx=find(diff(std_idx)>1);
  end_idx=end_idx+1;
  samp_nbr(std_idx(end_idx):length(samp_nbr))=1001;
% 
	salts.station_id = station_id;
	%salts.fire_order = fire_order;
	salts.samp_nbr = samp_nbr;
	salts.tank_temp =tank_temp ;
	salts.autosal_ratio = autosal_ratio;
	salts.autosal_ratio_corr = autosal_ratio_corr;
	salts.cond_ratio = cond_ratio;
	salts.salt =salt ;
	salts.salt_stddev =salt_stddev ;
	salts.hours = hours;
	salts.minutes =minutes ;
	salts.seconds = seconds;
	%salts.niskin_btl_pos = niskin_btl_pos;
	%salts.qc_flag = qc_flag;
	salts = trimstruct(salts);
return


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function out  = splitjoin(line)
% splitjoin: Clean up a line of text for easier parsing.
%

line = ddewhite(line); % rm leading/trailing whitespace 
noTab = untabify(line); % The stc02 files are tab separated columns.
if ~isempty(noTab)  % ...hopefully
	%noTab = del_dup_spaces(noTab);
	noTab = rstrrep(noTab,'  ',' ');
end
line = ddewhite(noTab); % clean ends again.
line = strsplit(sprintf(' '),line); % this is overkill.
line = strjoin(' ',line{:}); % but just in case...
out = line;
return

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function out  = trimstruct(in)
% trimstruct: Trim unused elements from preallocated arrays.
%

theNames = fieldnames(in);
for idx = 1:length(theNames);
	theData = in.(theNames{idx});
	if iscell(theData)
		outData = {};
		for idxCell = 1:length(theData)
			if ~(isnan(theData{idxCell}) | isempty(theData{idxCell}))
				outData{idxCell} = theData{idxCell};
			end
		end
		theData = outData;
	else
		theData(isnan(theData)) = [];
	end
	out.(theNames{idx}) = theData;
end
return


