📄 attnc.m
字号:
return end else % Check that there are some attributes. if isfield(desc_das, var_name) %fi = desc_das.(var_name); fi = getfield(desc_das, var_name); else error(['There is no variable ' var_name ' in ' file]) end % Find values for all attributes or a specified one. Note that a numeric % column vector is transformed into a numeric row vector. This is to be % compatible with the output from the equivalent action when a netcdf % file is read directly from the local disk. if isempty(att_name) att_list = fieldnames(fi); natts = 1; while 1 if isempty(findstr(att_list{natts}, 'DODS_ML_')) att_name_list{natts} = att_list{natts}; %values = fi.(att_list{natts}); values = getfield(fi, att_list{natts}); % A numeric column vector is transformed into a numeric row vector. if isnumeric(values) && (ndims(values) == 2) [m_temp, n_temp] = size(values); if n_temp == 1 values = values'; end end att_val{natts} = values; natts = natts + 1; else natts = natts - 1; break end end if natts == 0 if verbose disp([' --- ' var_name ' has no attributes ---']) end end else if isfield(fi, att_name) %att_val = fi.(att_name); att_val = getfield(fi, att_name); % A numeric column vector is transformed into a numeric row vector. if isnumeric(att_val) && (ndims(att_val) == 2) [m_temp, n_temp] = size(att_val); if n_temp == 1 att_val = att_val'; end end att_name_list = att_name; else if verbose warning([var_name ' does not have an attribute ' att_name]) return end end end end % Call clean_up_string to replace any control characters with a # to avoid % messing up the display - null characters make a major mess. clean_up_string % also strip off extraneous quote marks. We also check att_name_list to see if % any attributes start with 'ml__'. If they do then we cut off the 'ml_' under % the assumption that it has been added to deal with the problem of matlab % variables starting with an underscore. if iscell(att_val) for ii = 1:length(att_val) att_val{ii} = clean_up_string(att_val{ii}); if length(att_name_list{ii}) > 4 if strcmp(att_name_list{ii}(1:4), 'ml__') att_name_list{ii} = att_name_list{ii}(4:end); end end end else att_val = clean_up_string(att_val); if length(att_name_list) > 4 if strcmp(att_name_list(1:4), 'ml__') att_name_list = att_name_list(4:end); end end end case 'java' % Open dataset so that all attributes will be accessible. try enhance = false; ncdJ = ucar.nc2.dataset.NetcdfDataset.openDataset(file, enhance, []); catch ss = lasterror; mess_str = ss.message; rcode = -1000000; values = error_handle('java', mess_str, rcode, err_opt); end % Initialise some flags. if strfind(var_name, 'global') var_is_global = 1; else var_is_global = 0; end if isempty(att_name) get_all_atts = 1; else get_all_atts = 0; end % Get the variable object if this is necessary. if ~var_is_global try varJ = ncdJ.findVariable(var_name); catch ss = lasterror; mess_str = ss.message; rcode = -1000000; values = error_handle(ncdJ, mess_str, rcode, err_opt); end end % Actually get the attribute values. if var_is_global if get_all_atts att_list = ncdJ.getGlobalAttributes(); num_atts = att_list.size(); att_name_list = cell(num_atts, 1); att_val = cell(1, num_atts); for ii = 1:num_atts att = att_list.get(ii - 1); [att_name_list{ii}, att_val{ii}] = get_att_val(att, err_opt); end else try att = ncdJ.findGlobalAttribute(att_name); catch ss = lasterror; mess_str = ss.message; rcode = -1000000; values = error_handle(ncdJ, mess_str, rcode, err_opt); end [att_name_list, att_val] = get_att_val(att, err_opt); end else if get_all_atts att_list = varJ.getAttributes(); num_atts = att_list.size(); att_name_list = cell(num_atts, 1); att_val = cell(1, num_atts); for ii = 1:num_atts att = att_list.get(ii - 1); [att_name_list{ii}, att_val{ii}] = get_att_val(att, err_opt); end else try att = varJ.findAttribute(att_name); catch ss = lasterror; mess_str = ss.message; rcode = -1000000; values = error_handle(varJ, mess_str, rcode, err_opt); end [att_name_list, att_val] = get_att_val(att, err_opt); end end case 'none' error(['Couldn''t find a suitable mex-file for reading ' file])endfunction new_val = clean_up_string(val)% If val is a string then we replace any control characters with a # to% avoid messing up the display - null characters make a major mess. We% also strip off extraneous quote marks. if ischar(val) s = abs(val); if (s(1) == s(end)) && ((s(1) == 34) || (s(1) == 39)) s = s(2:(end - 1)); end s(find(s < 32)) = 35; new_val = char(s); else new_val = val; endfunction [att_name_list, att_val] = get_att_val(att, err_opt) try att_name_list = char(att.getName()); catch ss = lasterror; mess_str = ss.message; rcode = -1000000; values = error_handle(att, mess_str, rcode, err_opt); end if att.isString() att_val = char(att.getStringValue()); else num = double(att.getLength); vec = zeros(1, num); for mm = 1:num vec(mm) = double(att.getNumericValue(mm - 1)); end att_val = vec; endfunction values = error_handle(cdfid, mess_str, rcode, err_opt)% error_handle is called after a mexnc call has failed. It ensures% that an open netcdf file is closed. The value of err_opt determines what% else is done.% err_opt == 1 prints an error message and then aborts% == 2 prints a warning message and then returns an empty% array. This is the default.% == 3 returns an empty array. This is a very dangerous option and% should only be used with caution. It might be used when% getnc_s is called in a loop and you want to do your own% error handling without being bothered by warning messages.% Decide what part of the code made the call. if isempty(cdfid) called_by = 'loadd'; else if isnumeric(cdfid) called_by = 'mexnc'; else called_by = 'java'; end end % Close an open netcdf of java file. switch called_by case 'mexnc' if cdfid >= 0 [rcode_sub] = mexnc('ncclose', cdfid); end case 'java' if isjava(cdfid) ncdJ.close(); end end % Handle the errors according to the value of err_opt. If rcode is empty % then this is probably because loaddap or loaddods was called. values = []; switch err_opt case 1 if isempty(rcode) str = ['ERROR: ' mess_str]; else str = ['ERROR: ' mess_str ' : rcode = ' num2str(rcode)]; end error(str) case 2 if isempty(rcode) str = ['WARNING: ' mess_str]; else str = ['WARNING: ' mess_str ' : rcode = ' num2str(rcode)]; end disp(str) case 3 return otherwise error(['error_handle was called with err_opt = ' num2str(err_opt)]) end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -