function [theMaskFile,velR,corT,echI,Pgd]=fillmsk(theDataFile,theMaskFile,velR,corT,echI,Pgd);

%function [theMaskFile,velR,corT,echI,Pgd]=...
%...fillmsk(theDataFile,theMaskFile,velR,corT,echI,Pgd);
%Scans a raw ADCP Workhorse data file and marks the mask file to 
%meet the good data criteria standards set by RDI; 
%correlation threshold, echo intensity threshold, minimum percent good.
%Where:
%		theDataFile= the raw ADCP data file in netcdf
%		theMaskFile= Mask file created with ncmakemsk.m
%			If no mask is given you will be asked to create one.
%	
%	The min and max range for the following variables are optional inputs.
%	If not given the function will look in the netcdf file and find 
%	the pre-recorded values set prior to deployment.
%		velR=[min max] velocity
%		corT= [min max] corrleation threshold
%		echI= [min max] echo intensity
%		Pgd= [min max] percent good
%


% 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
%	premask.m-5/21/99 need the version of premask.m edited by JMC

if nargin<1, theDataFile = ''; end
if nargin<2, theMaskFile = ''; end

if isempty(theDataFile), theDataFile = '*'; end
if isempty(theMaskFile), theMaskFile = '*'; end

if any(theDataFile == '*')
   help(mfilename)
   thePrompt = theDataFile;
   [theFile, thePath] = uigetfile(thePrompt, 'Select ADCP Data File');
   if ~any(theFile), return, end
   if thePath(end) ~= filesep, thePath(end+1) = filesep; end
   theDataFile = [thePath theFile];
   cd(thePath)
end

if any(theMaskFile == '*')
   thePrompt = theMaskFile;
   [theFile, thePath] = uigetfile(thePrompt, 'Select ADCP Mask File');
   
   %Check to see if there is a mask file, if not ask to create
   if ~any(theFile) 
      prompt={'Do you want to create a mask file now?'};
   title='Mask File Not Found';
   lineNo=1;
   DefAns={'Yes'};
   dlgresult=inputdlg(prompt,title,lineNo,DefAns);
   if char(dlgresult{:})=='Yes';
      [p, outFile, ext, v] = fileparts(theDataFile);
		theMaskFile = [outFile '.msk'];
      ncmkmask(theDataFile,theMaskFile);
      disp(['The Mask file ' theMaskFile ' was created'])
      else 
         disp(['You must create a mask file to run fillmsk function'])
         return, end
   end
   if thePath(end) ~= filesep, thePath(end+1) = filesep; end
   theMaskFile = [thePath theFile];
end

f = netcdf(theDataFile, 'write'); % Open the file.
if isempty(f), return, end 

	if nargin < 3
%Autonan everything
        %v = var(f);   % Get the list of variables.
        %for i = 1:length(v);
        %        v{i} = autonan(v{i}, 1);   % Set each autonan flag.
        %end
        
%Just autonan the velocities
for ii=1:4;
  v=autonan(f{['vel' int2str(ii)]},1);
  eval(['vd' num2str(ii) '=v(:);'])
end

%check to make sure that the autonan worked
check=length(isnan(vd1));
if isempty(check)
   disp(['error executing autonan function'])
end


%Get the max and min of the velocities of the four beams 
%to set some criteria for masking
disp(['Pulling out the needed Global attributes from the Data file'])
disp(['This information sets the criteria for masking'])
vlim=ones(4,2);
[ii,jj]=size(vlim);
for i=1:ii;
   eval(['vlim(i,1)=min(min(vd' int2str(i) '));'])
   eval(['vlim(i,jj)=max(max(vd' int2str(i) '));'])
end
vmin=min(min(vlim))/10; %in cm/sec for premask
vmax=max(max(vlim))/10;
velR=[vmin vmax];

%Undo autonan the velocities
for ii=1:4;
  v=autonan(f{['vel' int2str(ii)]},0);
  eval(['vd' num2str(ii) '=v(:);'])
end

%To get global attribute data for use in premask
Pgd=f.minmax_percent_good(:);
echI=f.false_target_reject_values(:);
errV=f.minmax_error_velocity(:);
corT=f.valid_correlation_range(:);
   
   end %if nargin<3;  
   
%Let's run premask based on these criteria
%premask(theDataFile, theMaskFile, vel, cor, agc, good)
premask(theDataFile,theMaskFile,velR,corT,echI,Pgd);

disp(['The mask is filled '])

ncclose