📄 netcdf.m
字号:
% ncexample() creates, populates, and reads a NetCDF file.
% tnetcdf() exercizes the NetCDF Toolbox extensively.
%
% WWW: <http://crusty.er.usgs.gov/~cdenham/MexCDF/nc4ml5.html>.
%
% <<<<<<<<<<<<<<<<< End of NetCDF Language Synopsis >>>>>>>>>>>>>>>>>
% Copyright (C) 1996-7 Dr. Charles R. Denham, ZYDECO.
% All Rights Reserved.
% Disclosure without explicit written consent from the
% copyright owner does not constitute publication.
% Updated 18-Aug-1997 09:39:52.
% Updated 20-May-1999 09:33:04.
% Updated 24-Aug-1999 09:12:11.
% Updated 10-Apr-2000 21:07:48.
ncwhatsnew
if nargin == 1 & nargout < 1 & isequal(theFilename, 'version')
version(ncitem)
return
end
if nargin < 1 & nargout < 1
help(mfilename)
ncversion
disp(' ')
return
end
if nargout > 0, self = []; end
% Open with dialog as 'nowrite'.
if nargout > 0 & nargin < 1
result = netcdf('nowrite');
if ~isempty(result)
ncregister(result)
result = ncregister(result);
end
self = result;
return
end
if nargin < 2, thePermission = ''; end
result = [];
% Randomly named file.
if nargin == 1 & strcmp(theFilename, 'random')
result = [];
count = 0;
while isempty(result) & count < 32
thePWD = pwd;
if thePWD(length(thePWD)) ~= filesep
thePWD = [thePWD filesep];
end
theFilename = [thePWD 'temp' int2str(rand(1, 1) .* 10000) '.nc'];
result = netcdf(theFilename, 'noclobber');
end
if ~isempty(result)
ncregister(result)
result = ncregister(result);
end
if nargout > 0
self = result;
else
ncans(result)
end
return
end
if nargin == 1
switch class(theFilename)
case 'double'
theNCid = theFilename;
theStruct.itsPermission = 'unknown';
theStruct.itsDefineMode = 'unknown';
theStruct.itsFillMode = 'unknown';
theStruct.itsMaxNameLen = 0;
result = class(theStruct, 'netcdf', ncitem('', theNCid));
result = ncregister(result);
if nargout > 0
self = result;
else
ncans(result)
end
return
case 'char'
thePermission = '';
thePerm = ncpermission(theFilename);
switch thePerm
case {'nowrite', 'write', 'noclobber', 'clobber', ...
'readonly', 'define'}
thePermission = thePerm;
theFilename = '*';
otherwise
thePermission = 'nowrite';
end
otherwise
return
end
end
if isempty(thePermission), thePermission = 'nowrite'; end
if any(theFilename == '*')
switch thePermission
case {'noclobber', 'clobber'}
theSuggested = theFilename;
if isequal(theFilename, '*')
theSuggested = 'unnamed.nc';
else
theSuggested(theSuggested == '*') = '';
end
[theFile, thePath] = uiputfile(theSuggested, 'Save NetCDF As');
if ~any(theFile), return, end
case {'nowrite', 'write', 'readonly', 'define'}
[theFile, thePath] = uigetfile(theFilename, 'Select NetCDF File');
if ~any(theFile), return, end
otherwise
return
end
theFilename = [thePath theFile];
result = netcdf(theFilename, thePermission);
if ~isempty(result)
ncregister(result)
result = ncregister(result);
end
if nargout > 0
self = result;
else
ncans(result)
end
return
end
thePermission = ncpermission(thePermission);
switch thePermission
case 'define'
result = netcdf(theFilename, 'write');
if ~isempty(result), redef(result), end
if nargout > 0
self = result;
else
ncans(result)
end
return
otherwise
end
theDefineMode = '';
theFillMode = 'fill'; % The NetCDF default.
theMaxNameLen = 0;
theStruct.itsPermission = thePermission;
theStruct.itsDefineMode = theDefineMode;
theStruct.itsFillMode = theFillMode;
theStruct.itsMaxNameLen = theMaxNameLen;
result = class(theStruct, 'netcdf', ncitem(theFilename));
switch thePermission
case {'clobber', 'noclobber'}
result = create(result, thePermission);
if ncid(result) >= 0, theDefineMode = 'define'; end
case {'write', 'nowrite'}
result = open(result, thePermission);
if ncid(result) >= 0
theDefineMode = 'data';
elseif exist(name(result)) ~= 2
result = open(result, 'clobber');
if ncid(result) >= 0, theDefineMode = 'define'; end
end
otherwise
help netcdf
warning([' ## No such permission: ' thePermission])
end
if ncid(result) >= 0
result.itsDefineMode = theDefineMode;
ncregister(result)
result = ncregister(result);
else
disp([' ## NetCDF file not opened: ' theFilename])
result = [];
end
if nargout > 0
self = result;
else
ncans(result)
end
function theResult = ncpermission(thePermission)
% netcdf/ncpermission -- Resolve permission string.
% netcdf/ncpermission('thePermission') returns the NetCDF permission
% string corresponding to 'thePermission', which may be uniquely
% abbreviated.
thePerm = lower(thePermission);
% Lists of shortened permissions: must pre-allocate.
clobber = [];
noclobber = [];
write = [];
nowrite = [];
readonly = [];
define = [];
perms = {'clobber', 'noclobber', 'write', 'nowrite', 'readonly', 'define'};
for j = 1:length(perms)
p = perms{j};
c = cell(1, length(p));
for i = 1:length(perms{j})
c{i} = p;
p(length(p)) = '';
end
eval([perms{j} ' = c;']);
end
% Formal name of thePermission.
switch thePerm
case clobber
thePermission = 'clobber';
case noclobber
thePermission = 'noclobber';
case write
thePermission = 'write';
case [nowrite readonly]
thePermission = 'nowrite';
case [define]
thePermission = 'define';
otherwise
end
if nargout > 0, theResult = thePermission; end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -