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 % % Dependencies: % plot_ABS_stats.m % 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.1'; 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); 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') 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('All bursts (1:%d) remain un-masked in %s-mask.cdf',nBursts,outFileRoot) elseif strcmp(answer,'Y') || strcmp(answer,'y') first_good = input('Please view the figure and enter the first GOOD burst: '); if isempty(first_good) fprintf('Valid answers are numeric only\n') first_good = input('Please view the figure and enter the first GOOD burst: '); end last_good = input('Please view the figure and enter the last GOOD burst: '); if isempty(last_good) fprintf('Valid answers are numeric only\n') last_good = input('Please view the figure and enter the last GOOD burst: '); end if isequal(first_good,1) fprintf(' ') fprintf('Bursts %d:%d will be masked in:\n',last_good+1,nBursts) badBursts = [last_good+1:nBursts]'; first = 1; last = last_good+1; elseif isequal(last_good,nBursts) fprintf('Bursts 1:%d will be masked in:\n',first_good-1) badBursts = [1:first_good-1]'; first = first_good-1; last = nBursts; else fprintf('Bursts 1:%d and bursts %d:%d will be masked in:\n',first_good-1,last_good+1,nBursts) badBursts = [1:first_good-1 last_good+1:nBursts]'; first = first_good-1; last = last_good+1; end [fpath, fname, ext] = fileparts(statsFile); % MM 2/1/06 % fprintf('%s-mask.cdf\n',statsFile(1:end-4)) fprintf('%s-mask.cdf\n',fname); % MM 2/1/06 for n=1:length(burstFiles) % MM 2/1/06 [fpath, fname, ext] = fileparts(burstFiles{n}); % MM 2/1/06 %fprintf('%s-mask.cdf\n',burstFiles(n,1:end-4)) fprintf('%s-mask.cdf\n',fname) % MM 2/1/06 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'} = nclong('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) [TF, LOC] = ismember(badBursts,burst); cdfs{'badBursts'}(1:first) = 0; cdfs{'badBursts'}(last:nBursts) = 0; theVars = var(cdfs); for v = 1:length(theVars) varName = char(ncnames(theVars{v})); if ~strcmp(varName,'time') && ... ~strcmp(varName,'time2') && ... ~strcmp(varName,'r') && ... ~strcmp(varName,'pctile') && ... ~strcmp(varName,'burst') && ... ~strcmp(varName,'badBursts') theFillValue = theVars{v}.FillValue_(:); dims = size(theVars{v}); if length(dims) == 1 theVars{v}(1:first) = theFillValue; theVars{v}(last:nBursts) = theFillValue; elseif length(dims) == 2 theVars{v}(1:first,:) = theFillValue; theVars{v}(last:nBursts,:) = theFillValue; elseif length(dims) == 3 theVars{v}(1:first,:,:) = theFillValue; theVars{v}(last:nBursts,:,:) = theFillValue; 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'}(:); % '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'} = nclong('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) LOC = LOC(f); cdfs{'badBursts'}(LOC) = 0; theVars = var(cdfb); for v = 1:length(theVars) varName = char(ncnames(theVars{v})); if strcmp(varName,'abs_trans1') || ... strcmp(varName,'abs_trans2') || ... strcmp(varName,'abs_trans3') theFillValue = theVars{v}.FillValue_(:); theVars{v}(LOC,:,:) = theFillValue; 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) end disp('Finished masking files') toc