function [theNewADCPFile, theMaskFile] = runmask (rawcdf,theMaskFile,theNewADCPFile)

%function [theMaskFile, theMaskFile] = runmask (rawcdf,theMaskFile)
%This function runs the masking functions to produce a newly masked file.
%This will use the settings in the cdf file to mask and then allow the user
%to pick additional bad points.  
%If no data file is given, the uigetfile is invoked
%If no mask file is given the file is created based on the rawcdf filename
%If a name is not give for the New ADCP File, the uigetfile dialog is invoked
%
%INPUTS:
%	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 = a name for the output
%
%OUTPUTS:
%	theNewADCPFile = the masked data file with same attributes as the raw data file
%	theMaskFile = netcdf file containing 0's if the data is good 
%		and 1's if it is bad
%
%Here is a list of the functions in the order of call: 
%ncmkmask.m, fillmask.m (premask.m), StareBare Browser, postmask.m


% 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
%	ncmkmask.m
%	fillmsk.m
%	Starbare directory, including postmask.m and modified premask 

%updated 10-Aug-1999 16:20:42

ncquiet

%tell us what function is running
Mname=mfilename;
disp('')
disp([ Mname ' is currently running']);


if nargin < 1, help(mfilename), end

if nargin< 1, rawcdf = ''; end
if nargin < 2, theMaskFile = ''; end
if nargin < 3, theNewADCPFile = ''; end

if isempty(rawcdf), rawcdf = '*'; end
if isempty(theNewADCPFile), theNewADCPFile = '*'; end

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

%create the mask file.
if isempty(theMaskFile)
	[p, outFile, ext, v] = fileparts(rawcdf);
	theMaskFile = fullfile(p,[outFile '.msk']);
	ncmkmask(rawcdf,theMaskFile);
	disp('Created mask file, '), disp(theMaskFile);
end

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

%fill it based on the rdi criteria
[theMaskFile,velR,corT,echI,Pgd]=fillmsk(rawcdf,theMaskFile);

%check it out
StarBare(rawcdf,theMaskFile);

disp('In the following figure bad velocity data are displayed in black');
disp('Mark any additional bad data points');
disp('Then click "Done" on the Starbare menu, and hit enter');

	pause
   
%If the starbare window is not closed, make sure masking is complete   
h=findobj('Name','StarBare Browser');
if ~isempty(h);
		disp('Please close StarBare window if masking is complete.');
      wait(1)
      figure(gcf)
      disp(' ');
      disp('Hit any key to continue "runmask.m"');
      pause
end
   
      
%Write the masked data file(still in beam coordinates)
postmask(rawcdf,theMaskFile,theNewADCPFile);
   disp('Masking is complete based on the read mask file');  
   
   %Add in history comment
   thecomment = 'The data were filtered using rdi quality control factors in runmask.m ';
   history(theNewADCPFile,thecomment);
   
   
   ncclose