📄 getnc_s.m
字号:
else if length(order) == nd values = permute(values, order); else error('The order vector does not match the rank of the variable') end end % Make sure that all vectors are column vectors. size_val = size(values); nd = length(size_val); if (nd == 2) && any(size_val == 1) values = values(:); end % Deal with rescaling and change_miss stuff now. % Get all of the attributes that are required and store them in suitable % variables. att_list = varJ.getAttributes(); num_atts = att_list.size(); miss_val_list = []; min_val = -Inf; max_val = Inf; scale_factor = []; add_offset = []; for ii = 0:(num_atts - 1) att = att_list.get(ii); att_name = char(att.getName()); switch att_name case '_FillValue' fillvalue = get_attribute_value(att); miss_val_list = [miss_val_list fillvalue]; case 'missing_value' missing_value = get_attribute_value(att); miss_val_list = [miss_val_list missing_value]; case 'valid_range' valid_range = get_attribute_value(att); min_val = max([min_val valid_range(1)]); max_val = min([max_val valid_range(2)]); case 'valid_min' valid_min = get_attribute_value(att); min_val = max([min_val valid_min]); case 'valid_max' valid_max = get_attribute_value(att); max_val = min([max_val valid_max]); case 'scale_factor' scale_factor = get_attribute_value(att); case 'add_offset' add_offset = get_attribute_value(att); end end % Deal with rescaling first if rescale_opts(1) % Rescale the variable. if ~isempty(scale_factor) values = values*scale_factor; end if ~isempty(add_offset) values = values + add_offset; end end if rescale_opts(2) % Rescale the variable. if ~isempty(scale_factor) min_val = min_val*scale_factor; max_val = max_val*scale_factor; miss_val_list = miss_val_list*scale_factor; end if ~isempty(add_offset) min_val = min_val + add_offset; max_val = max_val + add_offset; miss_val_list = miss_val_list + add_offset; end end % Now do the missing value calculations. if change_miss == 1 replace_missing_values = 0; elseif change_miss == 3 new_miss_val = new_miss; replace_missing_values = 1; else new_miss_val = NaN; replace_missing_values = 1; end if replace_missing_values if isnumeric(values) for ii = 1:length(miss_val_list) ff = find(abs(values - miss_val_list(ii)) < 1e-5); values(ff) = new_miss_val; end values(find(values < min_val)) = new_miss_val; values(find(values > max_val)) = new_miss_val; else if isnumeric(new_miss_val) if isnan(new_miss_val) new_miss_val = char(0); else new_miss_val = char(new_miss_val); end end si = size(values); values = values(:)'; for ii = 1:length(miss_val_list) ff = strfind(values, miss_val_list(ii)); values(ff) = new_miss_val; end values = reshape(values, si); end end % Make sure that all numeric arrays are returned as double. if isnumeric(values) values = double(values); end % Close the file object ncdJ.close(); case 'none' error(['Couldn''t find a suitable mex-file for reading ' file])endfunction att_val = get_attribute_value(att)% Get the value of an attribute object. if att.isString() att_val = char(att.getStringValue()); else num = double(att.getLength); att_val = zeros(1, num); for mm = 1:num att_val(mm) = double(att.getNumericValue(mm - 1)); end endfunction new_val = remove_quotes(val)% If val is a string then we replace any 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 new_val = char(s); else new_val = val; endfunction values = error_handle(fid, mess_str, rcode, err_opt)% error_handle is called after a mexnc or java call has failed. It ensures% that an open netcdf file is closed. The value of err_opt determines what% else is done. For a mexnc call fid is cdfid, the handle to the open% file. For a java call fid is the opened file object.% err_opt == 0 prints an error message and then aborts% == 1 prints a warning message and then returns an empty% array. This is the default.% == 2 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(fid) called_by = 'loadd'; else if isnumeric(fid) called_by = 'mexnc'; else called_by = 'java'; end end% Close an open netcdf of java file. switch called_by case 'mexnc' if fid >= 0 [rcode_sub] = mexnc('ncclose', fid); end case 'java' if isjava(fid) fid.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 0 if isempty(rcode) str = ['ERROR: ' mess_str]; else str = ['ERROR: ' mess_str ' : rcode = ' num2str(rcode)]; end error(str) case 1 if isempty(rcode) str = ['WARNING: ' mess_str]; else str = ['WARNING: ' mess_str ' : rcode = ' num2str(rcode)]; end disp(str) case 2 return otherwise error(['error_handle was called with err_opt = ' num2str(err_opt)]) endfunction [file, varid, bl_corner, tr_corner, stride, order, change_miss, ... new_miss, squeeze_it, rescale_opts, err_opt] = parse_args(cell_args); % parse_args receives the cell that was passed to getnc via the varargin % method. It parses it to get all of the output arguments. It also sets the % defaults for these. % Specify all of the default values. file_def = []; % 1 varid_def = []; % 2 bl_corner_def = -1; % 3 tr_corner_def = -1; % 4 stride_def = -1; % 5 order_def = -1; % 6 change_miss_def = 2; % 7 new_miss_def = 0; % 8 squeeze_it_def = 1; % 9 rescale_opts_def = -1; % 10 err_opt_def = 1; % 11 % Set the variables to their default values. file = file_def; varid = varid_def; bl_corner = bl_corner_def; tr_corner = tr_corner_def; stride = stride_def; order = order_def; change_miss = change_miss_def; new_miss = new_miss_def; squeeze_it = squeeze_it_def; rescale_opts = rescale_opts_def; err_opt = err_opt_def; % Check that we do not have too many arguments passed. len_cell_args = length(cell_args); if len_cell_args > 11 error([num2str(len_cell_args) ' input arguments (too many) were passed']) end % Work through cell_args, overwriting variables as the input arguments are % read. % 1) The last element of cell_args may be a structure containing multiple % cases of values to be assigned to variables. % 2) If the passed value is an empty array or -1 then it is assumed that % the user wanted to pass the default. The exception to this is new_miss % where -1 would be an acceptable value to use. for ii = 1:len_cell_args if isstruct(cell_args{ii}) if ii == len_cell_args fname = fieldnames(cell_args{ii}); for jj = 1:length(fname) val = getfield(cell_args{ii}, fname{jj}); switch fname{jj} case 'file' file = substitute_new(val, order_def, 1); case 'varid' varid = substitute_new(val, order_def, 1); case 'bl_corner' bl_corner = substitute_new(val, order_def, 1); case 'tr_corner' tr_corner = substitute_new(val, order_def, 1); case 'stride' stride = substitute_new(val, order_def, 1); case 'order' order = substitute_new(val, order_def, 1); case 'change_miss' change_miss = substitute_new(val, change_miss_def, 1); case 'new_miss' new_miss = substitute_new(val, new_miss_def, 0); case 'squeeze_it' squeeze_it = substitute_new(val, squeeze_it_def, 1); case 'rescale_opts' rescale_opts = substitute_new(val, rescale_opts_def, 1); case 'err_opt' err_opt = substitute_new(val, err_opt_def, 1); otherwise error(['Don''t recognise the field ' fname{jj}]) end end else error('Only the last argument in getnc can be a structure') end else switch ii case 1 file = substitute_new(cell_args{ii}, order_def, 1); case 2 varid = substitute_new(cell_args{ii}, order_def, 1); case 3 bl_corner = substitute_new(cell_args{ii}, order_def, 1); case 4 tr_corner = substitute_new(cell_args{ii}, order_def, 1); case 5 stride = substitute_new(cell_args{ii}, order_def, 1); case 6 order = substitute_new(cell_args{ii}, order_def, 1); case 7 change_miss = substitute_new(cell_args{ii}, change_miss_def, 1); case 8 new_miss = substitute_new(cell_args{ii}, new_miss_def, 0); case 9 squeeze_it = substitute_new(cell_args{ii}, squeeze_it_def, 1); case 10 rescale_opts = substitute_new(cell_args{ii}, rescale_opts_def, 1); case 11 err_opt = substitute_new(cell_args{ii}, err_opt_def, 1); otherwise error('There can only be 11 arguments passed to getnc') end end end % Flag errors if some necessary argument has not been passed or now % contains an unacceptable value. % 1: file if ~ischar(file) error(' FILE is not a string'); end % 2: varid if isnumeric(varid) size_var = size(varid); if prod(size(varid)) == 1 if varid < 0 error('ERROR: varid is less than zero'); end else error('varid must be a scalar or a string') end elseif ~ischar(varid) error('varid must be a scalar or a string') end % 7: change_miss is_bad = 1; if isnumeric(change_miss) if length(change_miss) == 1 if ((change_miss >= 1) & (change_miss <= 3)) | (change_miss <= -1) is_bad = 0; end end end if is_bad error('change_miss must be a scalar') end % 8: new_miss is_bad = 1; if isnumeric(new_miss) if length(new_miss) == 1 is_bad = 0; end end if is_bad error('new_miss must be a scalar') end % 9: squeeze_it is_bad = 1; if isnumeric(squeeze_it) if length(squeeze_it) == 1 is_bad = 0; end end if is_bad error('squeeze_it must be a scalar') end % 11: err_opt is_bad = 1; if isnumeric(err_opt) if length(err_opt) == 1 if (err_opt >= -1) & (err_opt <= 2) is_bad = 0; end end end if is_bad error('err_opt must be a scalar == 0, 1 or 2') end % disp(['a:bl_corner = ' num2str(bl_corner)]) % disp(['a:tr_corner = ' num2str(tr_corner)]) % disp(['a:stride = ' num2str(stride)])function return_val = substitute_new(new_val, default_val, check_neg1) % If new_val is empty set return_val == default_val.% If new_val == -1 and check_neg1 is .true. set return_val = default_val.% Otherwise set return_val = new_val. if isempty(new_val) return_val = default_val; elseif (check_neg1 & all(new_val == -1)) return_val = default_val; else return_val = new_val; end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -