function theResult = runADCP(rawdata,rawcdf,theMaskFile,theNewADCPFile,...
   trimFile);

%%function [theResult] = runADCP(rawdata,rawcdf,theMaskFile);
%%Runs the important ADCP functions in the following order:
% rdi2cdf.m
% runmask.m
% goodends.m
% trimBins.m
% 
%%Ocassionally the program will display some results in figures and
% stops to ask if it is ok to continue. If the program seems to be waiting
% and you are unsure what to do, check the matlab command window for
% a message!
%%Type 'pause off' prior to running function if would like to run unattended.
%
%%If you do not wish to run rdi2cdf.m, to create the first netcdf file 
%from this data than, you must type in the first 2 inputs in the command
%line.  i.e. theResult = runADCP('999wh.000','999wh.cdf').
%
%Although you will be prompted for the INPUT files, be sure to have all 
%%of them accessable before beginning.
%
%%INPUTS:
%		rawdata =  RDI ADCP binary output file (*.000)
%		rawcdf = the Netcdf file created directly from the binary ADCP file
%		theMaskFile = identical in size and structure to the cdf file 
%			containing 0's if the data is good and 1's if it is bad
%		theNewADCPFile = the "masked" data file after initial filtering
%		trimFile = the last file created by trimming the bad ensembles
%
%If file names are not given, they will be requested.
%
%%
%%OUTPUTS:
%%		theResult = is the trimFile, the last file created

% Written by Jessica M. Cote
% for the U.S. Geological Survey
% Coastal and Marine Geology Program
% Woods Hole, MA
% http://woodshole.er.usgs.gov/
% Please report bugs to jcote@usgs.gov
%
%Sub-programs
%	Rdi2cdf.m
%	Starbeam directory
%	runmask.m (incorporates all masking functions)
%
%
%updated 10-Aug-1999 16:20:42

if nargin < 1, help(mfilename), end

if nargin< 1, rawdata = ''; end
if nargin < 2, rawcdf = '';, end
if nargin < 3, theMaskFile = ''; end
if nargin < 4, theNewADCPFile = ''; end
if nargin < 5, trimFile = ''; end

if isempty(rawdata), rawdata = '*'; end
%if isempty(theMaskFile), theMaskFile = '*'; end
if isempty(theNewADCPFile), theNewADCPFile = '*'; end
if isempty(trimFile), trimFile = '*'; end

% Get ADCP raw data filename.
if any(rawdata == '*')
	[theFile, thePath] = uigetfile(rawdata, 'Select Binary ADCP File:');
	if ~any(theFile), return, end
	if thePath(end) ~= filesep, thePath(end+1) = filesep; end
	rawdata = [thePath theFile]
end

% Get ADCP netcdf filename if not given
if isempty(rawcdf)
   [PATH,NAME,EXT,VER] = FILEPARTS(rawdata);
   [theFile, thePath] = uiputfile([NAME '.cdf'], 'Save Netcdf ADCP File As:');
		if ~any(theFile), return, end
		if thePath(end) ~= filesep, thePath(end+1) = filesep; end
		rawcdf = [thePath theFile];
end

if ~isempty(rawcdf) & isequal(exist(rawcdf),0)
disp(['Converting RDI data file to netcdf'])
disp('You will be asked for some inputs from the mooring log')
disp(' ')
   rdi2cdf(rawdata,rawcdf);
   starbeam(rawcdf);
   disp('Please close the starbeam browser when finished')
   disp('and press any key to continue');   
	pause   
else
   disp(' ')
   disp('Rdi2cdf.m was skipped.')
   disp(['Use existing netcdf file ' rawcdf ]);
   disp(' ')  
end

%Give mask file a name
if any(theMaskFile == '*') 
   mask='*.msk';
   	[theFile, thePath] = uiputfile(mask, 'Create Mask File As:');
	if ~any(theFile), return, end
	if thePath(end) ~= filesep, thePath(end+1) = filesep; end
	theMaskFile = [thePath theFile];
end

% Get ADCP filename.
if any(theNewADCPFile == '*')
	[theFile, thePath] = uiputfile('*M.cdf', 'Save masked ADCP File As:');
	if ~any(theFile), return, end
	if thePath(end) ~= filesep, thePath(end+1) = filesep; end
	theNewADCPFile = [thePath theFile];
end

%Mask the data file based on RDI criteria
disp('')
disp('Running mask functions to remove bad data points')
disp('')
[theNewADCPFile, theMaskFile] = runmask(rawcdf,theMaskFile,theNewADCPFile);

%Find the first and last good ensemble and trim the data record
[minens, maxens, nens, trimFile] = goodends(theNewADCPFile,theMaskFile,trimFile);

%Scan data for bins out of water based on depth+tidal variation
[MSL, Dstd] = trimBins(rawdata,trimFile);

theResult = trimFile;