function maskAqaCdf(outFileRoot)
% maskAqACdf.m  A function to mask bad data in Aquascat ABS burst and
%               statistic netCDF files.
%
% usage:    maskAqaCdf(outFileRoot);
%
%       where:  outFileRoot is the name given to the output netCDF files,
%                           surrounded by single quotes without the file
%                           extension .cdf
%
% Copyright 2005 
% USGS Woods Hole Field Center
% Written by Charlene Sullivan
% csullivan@usgs.gov
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Use of this program is self described.
% Program written in Matlab v7.1.0 SP3
% Program ran on PC with Windows XP Professional OS.
%
% "Although this program has been used by the USGS, no warranty, 
% expressed or implied, is made by the USGS or the United States 
% Government as to the accuracy and functioning of the program 
% and related program material nor shall the fact of distribution 
% constitute any such warranty, and no responsibility is assumed 
% by the USGS in connection therewith."
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
% Dependencies:
%   plot_ABS_stats.m

% TODO: Enable masking of bursts mid-deployment?
% C. Sullivan   03/13/06,   version 1.2
% Update file name handling per M. Martini. Fix glitch that causes the last
% GOOD burst to become masked. Run code with Matlab R2006a to check for
% compatibility issues, and use M-lint code analyzer for suggestions on how
% to improve code speed.
% C. Sullivan   08/30/05,   version 1.1
% Update code for newer variable names (ie: 'abs_trans#*).  Get rid of 
% 'isunix' loop. Use plot_ABS_stats.m to plot the mean amplitudes. The
% users will use this plot to determine bad data. Create the variable
% 'badBursts'. It is an array of 0's and 1's which indicate bursts that are
% bad.  badBursts = 0 indicates the burst is good; badBursts = 1 indicates
% the burst is bad.
% C. Sullivan   06/29/05,   version 1.0
% This function masks ABS data in the raw data (.cdf) netCDF files.  It
% creates a plot of ABS data for the user to view and asks the user to enter
% the first and last good bursts. All data between the first and last good
% bursts will be left as is, while data prior to the first good burst and 
% after the last good burst will be masked (set to the netCDF fill value of
% 1e35). Masking takes place in copies of the burst and statistic netCDF
% files.


more off

version = '1.2';

tic; 

% Check existence of ABS burst and statistic netCDF files
stats = dir('*s.cdf');
if isempty(stats)
    fprintf('\n')
    error(['Could not find the ABS statistics netCDF file.  ',...
        'Have you run aqa2cdf.m?']);
else
    statsFile = stats.name;
end
burst = dir('*b.cdf');
if isempty(burst)
    fprintf('\n')
    error(['Could not find the ABS burst netCDF file(s).  ',...
        'Have you run aqa2cdf.m?']);
else
    nBurstFiles = length(burst);
    burstFiles = cell(nBurstFiles);
    for n = 1:nBurstFiles
        burstFiles{n} = burst(n).name; % MM 2/1/06
    end
end

% Create a 3-panel pcolor plot of ABS mean amplitudes. This plot is used to
% determine bad data
plot_ABS_stats(statsFile, {'mean'}, 'burst', 0)

% Load the stats file to get the number of bursts.
cdfs = netcdf(statsFile);
burst = cdfs{'burst'}(:);
nBursts = size(burst,1);
close(cdfs)

% Ask the user to look for the first and last good bursts of data.
fprintf('\n')
fprintf('Please view the figure (feel free to zoom!) and look for bad\n')
fprintf('bursts collected during instrument deployment and recovery.\n')
fprintf('\n')
answer = input('Would you like to mask any bad bursts? y/n :  ','s');
if ~strcmp(answer,'Y') && ~strcmp(answer,'y') && ...
   ~strcmp(answer,'N') && ~strcmp(answer,'n')
        fprintf('Valid answers are either yes ("y") or no ("n")')
        answer = input('Would you like to mask any bad bursts? y/n :  ','s');
end
if strcmp(answer,'N') || strcmp(answer,'n')
    first_good = 1;
    last_good = nBursts;
    bad_bursts = [];
    fprintf('\n')
    fprintf('Not masking any bursts\n')
    fprintf('Bursts 1:%d will remain un-masked in %s-mask.cdf',nBursts,outFileRoot)
elseif strcmp(answer,'Y') || strcmp(answer,'y')
    fprintf('\n')
    first_good = input('Please view the figure and enter the first GOOD burst or [] to mask all bursts:  ');
    fprintf('\n')
    last_good = input('Please view the figure and enter the last GOOD burst or [] to mask all bursts:  ');
    % Set indices for filling bad data
    if isequal(first_good,1) && ~isequal(last_good,nBursts) % end is bad
        first = []; 
        last = last_good+1;
        badBursts = (last:nBursts)';
        fprintf('\n')
        fprintf('Bursts %d:%d will be masked in:\n',last,nBursts)
    elseif isequal(last_good,nBursts) && ~isequal(first_good,1) % beginning is bad
        first = first_good-1;
        last = []; 
        badBursts = (1:first)';
        fprintf('\n')
        fprintf('Bursts 1:%d will be masked in:\n',first)
    elseif ~isequal(first_good,1) && ~isequal(last_good,nBursts) % the beginning and end are bad
        first = first_good-1;
        last = last_good+1;
        badBursts = [1:first last:nBursts]';
        fprintf('\n')
        fprintf('Bursts 1:%d and bursts %d:%d will be masked in:\n',first,last,nBursts)
    end
    [fpath, fname, ext] = fileparts(statsFile); % MM 2/1/06
    fprintf('\n')
    fprintf('     %s-mask.cdf\n',fname); % MM 2/1/06
    fprintf('\n')
    for n=1:length(burstFiles) % MM 2/1/06
        [fpath, fname, ext] = fileparts(burstFiles{n}); % MM 2/1/06
        fprintf('     %s-mask.cdf\n',fname) % MM 2/1/06
        fprintf('\n')
    end
end

% Mask the data in the statistics netCDF file.  Be sure to mask data in a
% copy of the statistics netCDF file
statsFileRoot = statsFile(1:end-4);
copyfile([statsFileRoot,'.cdf'],[statsFileRoot,'-mask.cdf'])
fprintf('Working on %s-mask.cdf\n',statsFileRoot)
cdfs = netcdf([statsFileRoot,'-mask.cdf'],'w');
burst = cdfs{'burst'}(:);
% 'badBursts' is an array of 0's and 1's and indicates
% bursts that are bad.  badBursts = 1 indicates the burst
% is good.  badBursts = 0 indicates the burst is bad
cdfs{'badBursts'} = ncfloat('burst');
cdfs{'badBursts'}.FORTRAN_format = ncchar('F10.2');
cdfs{'badBursts'}.units = ncchar('counts');
cdfs{'badBursts'}.type = ncchar('EVEN');
cdfs{'badBursts'}.FillValue_ = ncfloat(1.00000004091848e+035);
cdfs{'badBursts'}(:) = ones(size(burst)); % initialize w/ all 1's (good bursts)
if ~isempty(badBursts)
    theVars = var(cdfs);
    for v = 1:length(theVars)
        varName = char(ncnames(theVars{v}));
        theFillValue = theVars{v}.FillValue_(:);
        if  ~strcmp(varName,'time') && ...
                ~strcmp(varName,'time2') && ...
                ~strcmp(varName,'r') && ...
                ~strcmp(varName,'pctile') && ...
                ~strcmp(varName,'burst') 
            dims = size(theVars{v});
            if length(dims) == 2
                if strcmp(varName,'badBursts')
                    if ~isempty(first)
                        theVars{v}(1:first,:,:) = 0;
                    end
                    if ~isempty(last)
                        theVars{v}(last:nBursts,:,:) = 0;
                    end
                else
                    if ~isempty(first)
                        theVars{v}(1:first,:,:) = theFillValue;
                    end
                    if ~isempty(last)
                        theVars{v}(last:nBursts,:,:) = theFillValue;
                    end
                end
            elseif length(dims) == 3
                if ~isempty(first)
                    theVars{v}(1:first,:,:) = theFillValue;
                end
                if ~isempty(last)
                    theVars{v}(last:nBursts,:,:) = theFillValue;
                end
            end
        end
    end
end
hist = cdfs.history(:);
hist_new = ['Bad data masked by maskAqaCdf.m V ',version,'; ',hist];
cdfs.history = ncchar(hist_new);
cdfs.CREATION_DATE = ncchar(datestr(now,0));
close(cdfs)

% Mask the data in the burst netCDF files. Be sure to mask data in a copy
% of the burst netCDF file
for n = 1:nBurstFiles
    [fpath, burstFileRoot, ext] = fileparts(burstFiles{n}); % MM 2/1/06
    %burstFileRoot = burstFiles(n,1:end-4);
    copyfile([burstFileRoot,'.cdf'],[burstFileRoot,'-mask.cdf'])
    fprintf('Working on %s-mask.cdf\n',burstFileRoot)
    cdfb = netcdf([burstFileRoot,'-mask.cdf'],'w');
    burst = cdfb{'burst'}(:); 
    nBursts = length(burst);
    % 'badBursts' is an array of 0's and 1's and indicates
    % bursts that are bad.  badBursts = 1 indicates the burst
    % is good.  badBursts = 0 indicates the burst is bad
    cdfb{'badBursts'} = ncfloat('burst');
    cdfb{'badBursts'}.FORTRAN_format = ncchar('F10.2');
    cdfb{'badBursts'}.units = ncchar('counts');
    cdfb{'badBursts'}.type = ncchar('EVEN');
    cdfb{'badBursts'}.FillValue_ = ncfloat(1.00000004091848e+035);
    cdfb{'badBursts'}(:) = ones(size(burst)); % initialize w/ all 1's (good bursts)
    [TF, LOC] = ismember(badBursts,burst);
    f = find(LOC ~= 0);
    if ~isempty(f)
        % set indices of bad data
        badInd = LOC(f);
        if any(diff(badInd)>1) %the beginning and end are bad
            theInd=find(diff(badInd)>1);
            first=badInd(theInd);
            last=badInd(theInd+1);
        else
            if badInd(1) == 1 && badInd(end) ~= nBursts %the beginning is bad
                first = badInd(end);
                last = [];
            elseif badInd(1) ~= 1 && badInd(end) == nBursts %the end is bad
                first = [];
                last = badInd(1);
            elseif badInd(1) == 1 && badInd(end) == nBursts %its all bad
                first = nBursts;
                last = [];
            end
        end
        % fill the bad data
        theVars = var(cdfb);
        for v = 1:length(theVars)
            varName = char(ncnames(theVars{v}));
            theFillValue = theVars{v}.FillValue_(:);
            if strcmp(varName,'badBursts')
               if ~isempty(first)
                   theVars{v}(1:first) = 0;
               end
               if ~isempty(last)
                   theVars{v}(last:nBursts) = 0;
               end
            elseif  strcmp(varName,'abs_trans1') || ...
                    strcmp(varName,'abs_trans2') || ...
                    strcmp(varName,'abs_trans3')
                        if ~isempty(first)
                            theVars{v}(1:first,:,:) = theFillValue;
                        end
                        if ~isempty(last)
                            theVars{v}(last:nBursts,:,:) = theFillValue;
                        end
            end
        end
    end
    hist = cdfb.history(:);
    hist_new = ['Bad data masked by maskAqaCdf.m V ',version,'; ',hist];
    cdfb.history = ncchar(hist_new);
    cdfb.CREATION_DATE = ncchar(datestr(now,0));
    close(cdfb)
    clear first last badInd
end

disp('Finished masking files')
toc

