11907
|
1 ## Copyright (C) 2013 Alexander Barth |
|
2 ## |
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2 of the License, or |
|
6 ## (at your option) any later version. |
|
7 ## |
|
8 ## This program is distributed in the hope that it will be useful, |
|
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 ## GNU General Public License for more details. |
|
12 ## |
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this program; If not, see <http://www.gnu.org/licenses/>. |
|
15 |
|
16 ## -*- texinfo -*- |
|
17 ## @deftypefn {Function File} {@var{x} =} ncread (@var{filename}, @var{varname}) |
|
18 ## @deftypefnx {Function File} {@var{x} =} ncread (@var{filename}, @var{varname},@var{start},@var{count},@var{stride}) |
|
19 ## |
|
20 ## Read the variable @var{varname} from the NetCDF file @var{filename}. |
|
21 ## |
|
22 ## If @var{start},@var{count} and @var{stride} are present, a subset of the |
|
23 ## variable is loaded. The parameter @var{start} contains the starting indices |
|
24 ## (1-based), @var{count} is the number of elements and @var{stride} the |
|
25 ## increment between two successive elements. These parameters are vectors whose |
|
26 ## length is equal to the number of dimension of the variable. Elements of |
|
27 ## @var{count} might be Inf which means that as many values as possible are |
|
28 ## loaded. |
|
29 ## |
|
30 ## If the variable has the _FillValue attribute, then the corresponding values |
|
31 ## are replaced by NaN. NetCDF attributes scale_factor (default 1) and |
|
32 ## add_offset (default 0) are use the transform the variable during the loading: |
|
33 ## |
|
34 ## x = scale_factor * x_in_file + add_offset |
|
35 ## |
|
36 ## The output data type matches the NetCDF datatype, except when the attributes |
|
37 ## _FillValue, add_offset or scale_factor are defined in which case the output |
|
38 ## is a array in double precision. |
|
39 ## |
|
40 ## @seealso{ncwrite,ncinfo,ncdisp} |
|
41 ## |
|
42 ## @end deftypefn |
|
43 |
|
44 function x = ncread(filename,varname,start,count,stride) |
|
45 |
|
46 ncid = netcdf_open(filename,'NC_NOWRITE'); |
|
47 [gid,varid] = ncvarid(ncid,varname); |
|
48 [varname_,xtype,dimids,natts] = netcdf_inqVar(gid,varid); |
|
49 |
|
50 % number of dimenions |
|
51 nd = length(dimids); |
|
52 |
|
53 sz = zeros(1,nd); |
|
54 for i=1:length(dimids) |
|
55 [dimname, sz(i)] = netcdf_inqDim(gid,dimids(i)); |
|
56 end |
|
57 |
|
58 if nargin < 3 |
|
59 start = ones(1,nd); |
|
60 end |
|
61 |
|
62 if nargin < 4 |
|
63 count = inf*ones(1,nd); |
|
64 end |
|
65 |
|
66 if nargin < 5 |
|
67 stride = ones(1,nd); |
|
68 end |
|
69 |
|
70 % replace inf in count |
|
71 i = count == inf; |
|
72 count(i) = (sz(i)-start(i))./stride(i) + 1; |
|
73 |
|
74 x = netcdf_getVar(gid,varid,start-1,count,stride); |
|
75 |
|
76 % apply attributes |
|
77 |
|
78 factor = []; |
|
79 offset = []; |
|
80 fv = []; |
|
81 |
|
82 for i = 0:natts-1 |
|
83 attname = netcdf_inqAttName(gid,varid,i); |
|
84 |
|
85 if strcmp(attname,'scale_factor') |
|
86 factor = netcdf_getAtt(gid,varid,'scale_factor'); |
|
87 elseif strcmp(attname,'add_offset') |
|
88 offset = netcdf_getAtt(gid,varid,'add_offset'); |
|
89 elseif strcmp(attname,'_FillValue') |
|
90 fv = netcdf_getAtt(gid,varid,'_FillValue'); |
|
91 end |
|
92 end |
|
93 |
|
94 if !isempty(factor) || !isempty(factor) || !isempty(offset) |
|
95 if !isa(x,'double') |
|
96 x = double(x); |
|
97 end |
|
98 end |
|
99 |
|
100 if !isempty(fv) |
|
101 x(x == fv) = NaN; |
|
102 end |
|
103 |
|
104 if !isempty(factor) |
|
105 x = x * factor; |
|
106 end |
|
107 |
|
108 if !isempty(offset) |
|
109 x = x + offset; |
|
110 end |
|
111 |
|
112 netcdf_close(ncid); |