function maskAqaCdf2nc(outFileRoot)
% maskAqACdf2nc.m  A function to write Aquascat ABS data to Best Basic
%                  Version (BBV) netCDF (.nc) files.
%
% usage:    maskAqaCdf2nc(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:
%   julian.m
%   gregorian.m

% C. Sullivan   03/14/05,   version 1.2
% Remove variable 'badBursts' from BBV files. 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/29/05,   version 1.1
% Remove isunix loop. Add code for creating BBV files from masked files.
% C. Sullivan   07/01/05,   version 1.0
% This function writes ABS data to BBV (.nc) netCDF files for distribution.
% The data in these BBV netCDF files has gone through QA/QC measures
% including removal of bad data collected during deployment and recovery of
% the instrument.


more off

tic

version = '1.2';

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

fprintf('Writing the best basic version statistics file, %s\n',[outFileRoot,'s-cal.nc'])

%%%% Make the BBV statistics file
cdfs = netcdf([outFileRoot,'s-mask.cdf']);
ncs = netcdf([outFileRoot,'s-cal.nc'],'clobber');

% Define dimensions for the BBV statistics file
ncs('time') = 0; %record dimension
ncs('bin') = size(dim(cdfs,'bin'),1); 
ncs('pctile') = size(dim(cdfs,'pctile'),1);
ncs('xducer') = size(dim(cdfs,'xducer'),1);

% Get indices of good bursts
badBursts = cdfs{'badBursts'}(:);
first_good = find(badBursts==1,1,'first');
last_good = find(badBursts==1,1,'last');
nGood = length((first_good:last_good)');
 
% Dimension variables in the BBV .nc file
% and copy variable attributes from the
% mask .cdf file
cdfVars = var(cdfs);
for v = 1:length(cdfVars)
     cdfVarName = char(ncnames(cdfVars{v}));
     cdfVar = cdfVars{v};
     if ~strcmp(cdfVarName,'badBursts')
            cdfVarDim = ncsize(cdfVar);
            ncVarName = cdfVarName;
            if length(cdfVarDim) == 1
                if strcmp(ncVarName,'pctile')
                    eval(['ncs{''',ncVarName,'''} = nclong(''pctile'');'])
                else
                    eval(['ncs{''',ncVarName,'''} = nclong(''time'');'])
                end
            elseif length(cdfVarDim) == 2
                if strcmp(ncVarName,'r')
                    eval(['ncs{''',ncVarName,'''} = ncfloat(''bin'',''xducer'');'])
                else
                    eval(['ncs{''',ncVarName,'''} = ncfloat(''time'',''bin'');'])
                end
            elseif length(cdfVarDim) == 3
                eval(['ncs{''',ncVarName,'''} = ncfloat(''time'',''bin'',''pctile'');'])
            end
            % Copy variable attributes from the .cdf
            % mask file to the BBV .nc file
            vAtts = att(cdfVars{v});
            for a = 1:length(vAtts)
                vAtt = vAtts{a};
                copy(vAtt,ncs{ncVarName});
            end
     end
end

fprintf('Finished dimensioning variables, %f minutes elapsed\n',toc/60)

% Write to the variables in the BBV .nc file
for v = 1:length(cdfVars)
     cdfVarName = char(ncnames(cdfVars{v}));
     cdfVar = cdfVars{v};
     % Copy good data in the .cdf mask file
     % to the BBV .nc file
     if ~strcmp(cdfVarName,'badBursts')
            cdfVarDim = ncsize(cdfVar);
            ncVarName = cdfVarName;
            if length(cdfVarDim) == 1
                if strcmp(ncVarName,'pctile')
                    ncs{ncVarName}(1:cdfVarDim(1)) = cdfs{cdfVarName}(:);
                else
                    ncs{ncVarName}(1:nGood) = cdfs{cdfVarName}(first_good:last_good);
                end
            elseif length(cdfVarDim) == 2
                if strcmp(ncVarName,'r')
                    ncs{ncVarName}(1:cdfVarDim(1),1:cdfVarDim(2)) = cdfs{cdfVarName}(:,:);
                else
                    ncs{ncVarName}(1:nGood,:) = cdfs{cdfVarName}(first_good:last_good,:);
                end
                calc_minmax_vals(ncs{ncVarName});
            elseif length(cdfVarDim) == 3
                ncs{ncVarName}(1:nGood,:,:) = cdfs{cdfVarName}(first_good:last_good,:,:);
                calc_minmax_vals(ncs{ncVarName});
            end
            fprintf('Finished writing %s, %f minutes elapsed\n',cdfVarName,toc/60)
     end
end

% Copy global attributes from the .cdf
% mask file to the BBV .nc file
gAtts = att(cdfs);
copy(gAtts,ncs)

% Update the following global attributes
% in the BBV .nc file
hist = ncs.history(:);
hist_new = ['Bad data collected during instrument deployment and ',...
            'recovery trimmed by maskAqaCdf2nc.m V ',num2str(version),...
            '; ',hist];
ncs.history = ncchar(hist_new);
ncs.CREATION_DATE = ncchar(datestr(now,0));
start = ncs{'time'}(1) + ncs{'time2'}(1)/3600/1000/24;
ncs.start_time = ncchar(datestr(gregorian(start)));
stop = ncs{'time'}(end) + ncs{'time2'}(end)/3600/1000/24;
ncs.stop_time = ncchar(datestr(gregorian(stop)));

close(cdfs)
close(ncs)

fprintf('Finished writing %s, %f minutes elapsed\n',[outFileRoot,'s-cal.nc'],toc/60)
fprintf('\n')
fprintf('\n')

%%%% Make the BBV burst file(s)
for b = 1:nBurstFiles
    cdfb = netcdf([outFileRoot,num2str(b),'b-mask.cdf']);
    burst = cdfb{'burst'}(:);
    fprintf('Looking for good bursts in %s\n',[outFileRoot,num2str(b),'b-mask.cdf'])
    % Get indices of good bursts
    badBursts = cdfb{'badBursts'}(:);
    first_good = find(badBursts==1,1,'first');
    last_good = find(badBursts==1,1,'last');
    nGood = length((first_good:last_good)');
    % If none of the bursts are good, do not create a BBV burst file
    if nGood == 0
        fprintf('No good bursts exist in %s\n',[outFileRoot,num2str(b),'b-mask.cdf'])
        fprintf('Not creating a best basic version of this file')
    else
        fprintf('Good bursts exist in %s\n',[outFileRoot,num2str(b),'b-mask.cdf'])
        fprintf('Creating a best basic version of this file')
        fprintf('Writing the best basic version burst file, %s\n',...
                 [outFileRoot,num2str(b),'b-cal.nc'])
        ncb = netcdf([outFileRoot,num2str(b),'b-cal.nc'],'clobber');
        % Define dimensions for the BBV statistics file
        ncb('time') = 0; %record dimension
        ncb('profile') = size(dim(cdfb,'profile'),1);
        ncb('bin') = size(dim(cdfb,'bin'),1);
        ncb('xducer') = size(dim(cdfb,'xducer'),1);
        % Dimension variables in the BBV .nc file
        % and copy variable attributes from the
        % mask .cdf file
        cdfVars = var(cdfb);
        for v = 1:length(cdfVars)
            cdfVarName = char(ncnames(cdfVars{v}));
            cdfVar = cdfVars{v};
            if ~strcmp(cdfVarName,'badBursts')
                cdfVarDim = ncsize(cdfVar);
                ncVarName = cdfVarName;
                if length(cdfVarDim) == 2
                    if strcmp(ncVarName,'r')
                        eval(['ncb{''',ncVarName,'''} = ncfloat(''bin'',''xducer'');'])
                    else
                        eval(['ncb{''',ncVarName,'''} = ncfloat(''time'',''profile'');'])
                    end
                elseif length(cdfVarDim) == 3
                    eval(['ncb{''',ncVarName,'''} = ncfloat(''time'',''profile'',''bin'');'])
                end
                % Copy variable attributes from the mask
                % .cdf file to the BBV .nc file
                vAtts = att(cdfVars{v});
                for a = 1:length(vAtts)
                    vAtt = vAtts{a};
                    copy(vAtt,ncb{ncVarName});
                end
            end
        end

        fprintf('Finished dimensioning variables, %f minutes elapsed\n',toc/60)

        % Write to the variables in the BBV .nc file
        for v = 1:length(cdfVars)
            cdfVarName = char(ncnames(cdfVars{v}));
            cdfVar = cdfVars{v};
            % Copy good data in the .cdf mask file
            % to the BBV .nc file
            if ~strcmp(cdfVarName,'badBursts')
                cdfVarDim = ncsize(cdfVar);
                ncVarName = cdfVarName;
                if length(cdfVarDim) == 2
                    if strcmp(ncVarName,'r')
                        ncb{ncVarName}(1:cdfVarDim(1),1:cdfVarDim(2)) = cdfb{cdfVarName}(:,:);
                    else
                        ncb{ncVarName}(1:nGood,:) = cdfb{cdfVarName}(first_good:last_good,:);
                    end
                elseif length(cdfVarDim) == 3
                    ncb{ncVarName}(1:nGood,:,:) = cdfb{cdfVarName}(first_good:last_good,:,:);
                end
                fprintf('Finished writing %s, %f minutes elapsed\n',cdfVarName,toc/60)
            elseif strcmp(cdfVarName,'r')
                copy(cdfVar,ncb)
                fprintf('Finished writing %s, %f minutes elapsed\n',cdfVarName,toc/60)
            end
        end

        % Copy global attributes from the .cdf
        % mask file to the BBV .nc file
        gAtts = att(cdfb);
        copy(gAtts,ncb)

        % Update global attributes in .nc file
        hist = ncb.history(:);
        hist_new = ['Bad data collected during instrument deployment and ',...
            'recovery trimmed by maskAqaCdf2nc.m V ',num2str(version),...
            '; ',hist];
        ncb.history = ncchar(hist_new);
        ncb.CREATION_DATE = ncchar(datestr(now,0));
        start_time = ncb{'time'}(1,1) + ncb{'time2'}(1,1)/3600/1000/24;
        ncb.start_time = ncchar(datestr(gregorian(start_time)));
        stop_time = ncb{'time'}(end,end) + ncb{'time2'}(end,end)/3600/1000/24;
        ncb.stop_time = ncchar(datestr(gregorian(stop_time)));

        fprintf('Finished writing %s, %f minutes elapsed\n',[outFileRoot,num2str(b),'b-cal.nc'],toc/60)
        fprintf('\n')
        fprintf('\n')

        close(cdfb)
        close(ncb)
    end

    
end

ncclose

% ---------------------- Subfunction ------------------------------------ %
function calc_minmax_vals(theVar)

        data = theVar(:);
        N = ndims(data);
        if N==1   %1D data
            theVar.minimum = ncfloat(min(data));
            theVar.maximum = ncfloat(max(data));
        elseif N==2 %2D data
            [row, col] = size(data);
            for icol = 1:col
                mins(icol) = min(data(:,icol));
                maxs(icol) = max(data(:,icol));
            end
            theVar.minimum = ncfloat(mins);
            theVar.maximum = ncfloat(maxs);
            clear mins maxs
        elseif N==3 %3D data
            [row, col, lvl] = size(data);
            for jlvl = 1:lvl
                    mins(:,jlvl) = min(data(:,:,jlvl))';
                    maxs(:,jlvl) = max(data(:,:,jlvl))';
            end
            theVar.minimum = ncfloat(mins);
            theVar.maximum = ncfloat(maxs);
            clear mins maxs
        end
        clear data

return
