Mercurial > forge
annotate main/netcdf/inst/ncread.m @ 12711:a4de85b2e6a0 octave-forge
fix bug 47014 and make test code work in matlab
author | abarth93 |
---|---|
date | Fri, 19 Feb 2016 13:45:14 +0000 |
parents | f2cf7070ccb5 |
children |
rev | line source |
---|---|
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 | |
12561
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
11907
diff
changeset
|
31 ## are replaced by NaN (except for characters). NetCDF attributes scale_factor |
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
11907
diff
changeset
|
32 ## (default 1) and add_offset (default 0) are use the transform the variable |
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
11907
diff
changeset
|
33 ## during the loading: |
11907 | 34 ## |
35 ## x = scale_factor * x_in_file + add_offset | |
36 ## | |
37 ## The output data type matches the NetCDF datatype, except when the attributes | |
38 ## _FillValue, add_offset or scale_factor are defined in which case the output | |
39 ## is a array in double precision. | |
40 ## | |
12635 | 41 ## Note that values equal to the attribute missing_value are not replaced by |
42 ## NaN (for compatibility). | |
43 ## | |
11907 | 44 ## @seealso{ncwrite,ncinfo,ncdisp} |
45 ## | |
46 ## @end deftypefn | |
47 | |
48 function x = ncread(filename,varname,start,count,stride) | |
49 | |
50 ncid = netcdf_open(filename,'NC_NOWRITE'); | |
51 [gid,varid] = ncvarid(ncid,varname); | |
52 [varname_,xtype,dimids,natts] = netcdf_inqVar(gid,varid); | |
53 | |
54 % number of dimenions | |
55 nd = length(dimids); | |
56 | |
57 sz = zeros(1,nd); | |
58 for i=1:length(dimids) | |
59 [dimname, sz(i)] = netcdf_inqDim(gid,dimids(i)); | |
60 end | |
61 | |
62 if nargin < 3 | |
63 start = ones(1,nd); | |
64 end | |
65 | |
66 if nargin < 4 | |
67 count = inf*ones(1,nd); | |
68 end | |
69 | |
70 if nargin < 5 | |
71 stride = ones(1,nd); | |
72 end | |
73 | |
74 % replace inf in count | |
75 i = count == inf; | |
76 count(i) = (sz(i)-start(i))./stride(i) + 1; | |
77 | |
78 x = netcdf_getVar(gid,varid,start-1,count,stride); | |
79 | |
80 % apply attributes | |
81 | |
82 factor = []; | |
83 offset = []; | |
84 fv = []; | |
85 | |
86 for i = 0:natts-1 | |
87 attname = netcdf_inqAttName(gid,varid,i); | |
88 | |
89 if strcmp(attname,'scale_factor') | |
90 factor = netcdf_getAtt(gid,varid,'scale_factor'); | |
91 elseif strcmp(attname,'add_offset') | |
92 offset = netcdf_getAtt(gid,varid,'add_offset'); | |
93 elseif strcmp(attname,'_FillValue') | |
94 fv = netcdf_getAtt(gid,varid,'_FillValue'); | |
95 end | |
96 end | |
97 | |
12711 | 98 netcdf_close(ncid); |
99 | |
100 # the scaling does not make sense of characters | |
101 if xtype == netcdf_getConstant('char') || ... | |
102 xtype == netcdf_getConstant('string') | |
103 | |
104 return; | |
105 end | |
106 | |
107 if !isempty(fv) || !isempty(factor) || !isempty(offset) | |
11907 | 108 if !isa(x,'double') |
109 x = double(x); | |
110 end | |
111 end | |
112 | |
12711 | 113 if !isempty(fv) |
11907 | 114 x(x == fv) = NaN; |
115 end | |
116 | |
117 if !isempty(factor) | |
118 x = x * factor; | |
119 end | |
120 | |
121 if !isempty(offset) | |
122 x = x + offset; | |
123 end | |
124 |