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