📄 getnc.m
字号:
function values = getnc(varargin);% function values = getnc(file, varid, bl_corner, tr_corner, stride, order, ...% change_miss, new_miss, squeeze_it, rescale_opts, err_opt)%% GETNC retrieves data from a NetCDF file or a DODS/OPEnDAP dataset.%% function values = getnc(file, varid, bl_corner, tr_corner, stride, order, ...% change_miss, new_miss, squeeze_it, rescale_opts, err_opt)%% DESCRIPTION:% getnc retrieves data either from a local NetCDF file or a DODS/OPEnDAP% dataset. The way getnc behaves depends on how many of the input arguments are% passed to it. If no arguments are passed then it returns this help% message. If one argument (the name of a netcdf file) is passed then the user% is asked questions to determine information necessary for the data% retrieval. (DODS/OPEnDAP data cannot be retrieved this way.) Other usage% (described below) is for the non-interactive behaviour. If more than one% argument is passed then getnc returns the data without needing to ask any% questions. The input arguments are listed below.%% USAGE:% getnc retrieves data in two ways. It can be used used interactively to% retrieve data from a netCDF file by simply typing:%% >> values = getnc(file);%% getnc is more commonly used as a function call - it can then retrieve data% from both netCDF and OPeNDAP files. Because many options are available% getnc can take up to 11 input arguments (although most have default% values). To make things easier for the user there are various ways of% specifying these arguments. Specifying up to 11 arguments to getnc can be% complicated and confusing. To make the process easier getnc will accept a% variety of types of input. These are given as follows:%% 1) Specify all 11 arguments. Thus we could make a call like:%% >> values = getnc(file, varid, bl_corner, tr_corner, stride, order, ...% change_miss, new_miss, squeeze_it, rescale_opts, err_opt);%% 2) Use default arguments. Only the first 2 arguments are strictly% necessary as the other arguments all have defaults. The following call% would retrieve the entire contents of the named variable:%% >> values = getnc(file, varid);%% If you want non-default behaviour for one or more of the later arguments% then you can do something like:%% >> values = getnc(file, varid, -1, -1, -1, -1, change_miss, new_miss);%% In this case there are 4 arguments specified and 7 with default values used.%% 3) Use a structure as an argument. From version 3.3 onwards it is% possible to pass a structure to getnc. This is illustrated below: % % >> x.file = 'fred.nc';% >> x.varid = 'foo';% >> x.change_miss = 1;% >> values = getnc(x);% % This specifies 3 arguments and causes defaults to be used for the other 8.% Note that it is possible to mix the usual arguments with the passing of a% structure - it is only necessary that the structure be the last argument% passed. We could achieve the same effect as above by doing:% % >> x.change_miss = 1;% >> values = getnc('fred.nc', 'foo', x);%% INPUT ARGUMENTS:% 1. file: This is a string containing the name of the netCDF file or the% URL to the OpenDAP dataset. It does not have a default. If describing a% netCDF file it is permissible to drop the ".nc" prefix but this is not% recommended.%% 2. varid: This may be a string or an integer. If it is a string then it% should be the name of the variable in the netCDF file or OPEnDAP% dataset. The use of an integer is a deprecated way of accessing netCDF% file data; if used the integer is taken from the order of the variables% returned by a call to ddsnc or inqnc (starting from 1).%% 3. bl_corner: This is a vector of length n specifying the hyperslab% corner with the lowest index values (the bottom left-hand corner in a% 2-space). The corners refer to the dimensions in the same order that% these dimensions are listed in the inqnc description of the variable. For% a netCDF file this is the same order that they are returned in a call to% "ncdump". With an OPEnDAP dataset it is the same order as in the% DDS. Note also that the indexing starts with 1 - as in matlab and% fortran, NOT 0 as in C. A negative element means that all values in that% direction will be returned. If a negative scalar (or an empty array) is% used this means that all of the elements in the array will be% returned. This is the default, i.e., all of the elements of varid will be% returned.%% 4. tr_corner: This is a vector of length n specifying the hyperslab% corner with the highest index values (the top right-hand corner in a% 2-space). A negative element means that the returned hyperslab should run% to the highest possible index (this is the default). Note, however, that% the value of an element in the end_point vector will be ignored if the% corresponding element in the corner vector is negative.%% 5. stride: This is a vector of length n specifying the interval between% accessed values of the hyperslab (sub-sampling) in each of the n% dimensions. A value of 1 accesses adjacent values in the given% dimension; a value of 2 accesses every other value; and so on. If no% sub-sampling is required in any direction then it is allowable to just% pass the scalar 1 (or -1 to be consistent with the corner and end_point% notation). Note, however, that the value of an element in the stride% vector will be ignored if the corresponding element in the corner vector% is negative.%% 6. order: % * order == -1 then the n dimensions of the array will be returned in% the same order as described by a call to inqnc(file) or "ncdump". It% therefore corresponds to the order in which the indices are specified% in corner, end_point and stride. This is the default.% * order == -2 will reverse the above order. Because matlab's array% storage is row-dominant this is actually a little quicker but the% difference is rarely significant.%% 7. change_miss: Missing data are indicated by the attributes _FillValue,% missing_value, valid_range, valid_min and valid_max. The action to be% taken with these data are determined by change_miss.% * change_miss == 1 causes missing values to be returned unchanged.% * change_miss == 2 causes missing values to be changed to NaN (the% default).% * change_miss == 3 causes missing values to be changed to new_miss% (after rescaling if that is necessary).% * change_miss < 0 produces the default (missing values to be changed to% NaN).%% 8. new_miss: This is the value given to missing data if change_miss == 3.%% 9. squeeze_it: This specifies whether the matlab function "squeeze"% should be applied to the returned array. This will eliminate any% singleton array dimensions and possibly cause the returned array to have% less dimensions than the full array.% * squeeze_it ~= 0 causes the squeeze function to be applied. This is% the default. Note also that a 1-d array is returned as a column% vector.% * squeeze_it == 0 so that squeeze will not be applied.%% 10. rescale_opts: This is a 2 element vector specifying whether or not% rescaling is carried out on retrieved variables and certain% attributes. The relevant attributes are _FillValue', 'missing_value',% 'valid_range', 'valid_min' and 'valid_max'; they are used to find missing% values of the relevant variable. The option was put in to deal with files% that do not follow the netCDF conventions (usually because the creator of% the file has misunderstood the convention). For further discussion of the% problem see here. Only use this option if you are sure that you know what% you are doing.% * rescale_opts(1) == 1 causes a variable read in by getnc.m to be% rescaled by 'scale_factor' and 'add_offset' if these are attributes of% the variable; this is the default.% * rescale_opts(1) == 0 disables rescaling of the retrieved variable.% * rescale_opts(2) == 1 causes the attributes '_FillValue', etc to be% rescaled by 'scale_factor' and 'add_offset'; this is the default.% * rescale_opts(2) == 0 disables the rescaling of the attributes% '_FillValue', etc.%% 11. err_opt: This is an integer that controls the error handling in a call% to getnc.% * err_opt == 0 on error this prints an error message and aborts.% * err_opt == 1 prints a warning message and then returns an empty% array. This is the default.% * err_opt == 2 returns an empty array. This is a dangerous option and% should only be used with caution. It might be used when getnc is called% in a loop and you want to do your own error handling without being% bothered by warning messages.%% OUTPUT:% values is a scalar, vector or array of values that is read in% from the NetCDF file or DODS/OPEnDAP dataset%% NOTES:% 1) In order for getnc to work non-interactively it is only strictly% necessary to pass the first 2 input arguments to getnc - sensible% defaults are available for the rest.% The defaults are:% bl_corner, tr_corner == [-1 ... -1], => all elements retrieved% stride == 1, => all elements retrieved% order == -1% change_miss == 2, => missing values replaced by NaNs% new_miss == 0;% squeeze_it == 1; => singleton dimensions will be removed% rescale_opts == [1 1]; => the obvious rescaling% error_opt == 1 prints a warning message and then returns an empty array.%% 2) It is not acceptable to pass only 3 input arguments since there is% no default in the case of the corner points being specified but the% end points not.%% 3) By default the order of the dimensions of a returned array will be the% same as they appear in the relevant call to 'inqnc' (from matlab) or% 'ncdump -h' (from the command line). (This is the opposite to what% happened in an earlier version of getnc.) For a netcdf file this actually% involves getnc re-arranging the returned array because the netCDF utilities% follow the C convention for data storage and matlab follows the fortran% convention. For a DODS/OPEnDAP dataset it is even weirder.%% 4) If the values are returned in a one-dimensional array then this will% always be a column vector.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -