function theStatus = ncdump(theNetCDFFile, theOutputFile)
% Derived from ncdump.
% Lists variables, units, descriptions, and scalar values of netcdf file.
if nargin < 1, help ncdump, theNetCDFFile = '*.*'; end
if nargin < 2, theOutputFile = 'stdout'; end % stdout.
if isa(theNetCDFFile, 'ncitem')
theNetCDFFile = name(parent(parent(theNetCDFFile)));
end
if any(theNetCDFFile == '*')
theFilterSpec = theNetCDFFile;
thePrompt = 'Select a NetCDF Input File:';
[theFile, thePath] = uigetfile(theFilterSpec, thePrompt);
if ~any(theFile), return, end
theNetCDFFile = [thePath theFile];
end
if any(theOutputFile == '*')
theFilterSpec = theOutputFile;
thePrompt = 'Select a Text Output File:';
[theFile, thePath] = uiputfile(theFilterSpec, thePrompt);
if ~any(theFile), return, end
theOutputFile = [thePath theFile];
end
nctypes = ['byte '; 'char '; 'short '; ...
'long '; 'float '; 'double '; ...;
'unknown'; 'unknown'; 'unknown'];
nc = netcdf(theNetCDFFile, 'nowrite');
theNCid = ncid(nc);
if isempty(nc)
disp([' ## Unable to open: ' theNetCDFFile])
return
end
if strcmp(theOutputFile, 'stdout')
fp = 1;
elseif strcmp(theOutputFile, 'stderr')
fp = 2;
elseif isstr(theOutputFile)
fp = fopen(theOutputFile, 'w');
else
fp = theOutputFile;
end
if fp < 0, close(nc), return, end
[ndims, nvars, ngatts, recdim] = size(nc);
dims = dim(nc); ndims = length(dims);
vars = var(nc); nvar = length(vars);
gatts = att(nc); ngatts = length(gatts);
s = ['File: ' theNetCDFFile];
fprintf(fp, '%s\n', s);
s = '%% Variables:';
fprintf(fp, '\n%s\n', s);
s = '%% (none)';
if nvars < 1, fprintf(fp, '%s\n', s), end
for j = 1:nvars;
varid = j-1;
varname = name(vars{j});
varname = strrep(varname, '''', '''''');
theDatatype = datatype(vars{j});
theDatatype = ['nc' theDatatype];
dims = dim(vars{j});
ndims = length(dims);
atts = att(vars{j});
natts = length(atts);
sz=ncsize(vars{j});
elements = prod(sz);
if elements==1, theValue=num2str(nc{j}(1));
else
% unwrap angles
if natts & strcmp(atts{1}(1:3),'deg'),
val=nc{j}(:);
val=unwrap(val); if val(1)<0, val=val+360; end
end
% compact display of arrays and matrices
if prod(sz)==max(sz) & elements>=3,
val=nc{j}(:);
[dummy,i]=max(sz);
dimname = name(dims{i});
theValue=['[' num2str(val(1)) ' ' num2str(val(2)) ' ..' dimname '.. ' num2str(val(end)) ']'];
else
for i = 1:ndims
dimname = name(dims{i});
if i==1, s=['[' dimname];
else s = [s ' x ' dimname]; end
end
s=[s ']'];
theValue=s;
end
end
if natts, theUnit=atts{1}(:); else theUnit=''; end
fprintf(fp, '%24s = %10s %s\n',varname,theValue,strrep(theUnit,'\0',''));
end
s = '%% Dimensions:';
fprintf(fp, '\n%s\n', s);
if ndims < 1, disp('%% (none)'), end
for i = 1:ndims
dimid = i-1;
dimname = name(dims{i});
dimname = strrep(dimname, '''', '''''');
dimlen = ncsize(dims{i});
fprintf(fp, '%24s = %10s\n',dimname,int2str(dimlen));
end
s = '%% Global attributes:';
fprintf(fp, '\n%s\n', s);
s = '%% (none)';
if ngatts < 1,fprintf(fp, '%s\n', s); end
for i = 1:ngatts
varid = -1;
attnum = i-1;
attname = name(gatts{i});
if any(attname ~= '_')
while attname(1) == '_'
attname = [attname(2:length(attname)) attname(1)];
end
end
attname = strrep(attname, '''', '''''');
theDatatype = datatype(gatts{i});
attlen = ncsize(gatts{i});
attvalue = gatts{i}(:);
theDatatype = ['nc' theDatatype];
s = attname;
t = mat2str(attvalue);
if length(t) > 0 & 0
if t(1) == '[' & t(length(t)) == ']'
t = [ '{' t(2:length(t)-1) '}'];
end
end
if ~isstr(attvalue)
if (0)
f = [];
k = 1:length(t)-1;
if any(k), f = find(t(k) == t(k+1)); end
if any(f), t(f) = []; end
f = find(t == ' ');
if any(f), t(f) = setstr(t(f) .* 0 + ','); end
t = strrep(t, ',', ', ');
end
end
fprintf(fp, '%24s = %10s\n',attname,strrep(attvalue,'\0',''));
end
if ischar(theOutputFile) & fp > 2, fclose(fp); end
close(nc)
if nargout > 0, theStatus = status; end