annotate main/netcdf/inst/ncread.m @ 12561:8476336c120c octave-forge

ncread: do not replace _FillValue by NaNs for characters
author abarth93
date Fri, 20 Feb 2015 16:30:08 +0000
parents 24f335b0c8df
children f2cf7070ccb5
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 ##
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
41 ## @seealso{ncwrite,ncinfo,ncdisp}
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
42 ##
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
43 ## @end deftypefn
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
44
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
45 function x = ncread(filename,varname,start,count,stride)
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
46
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
47 ncid = netcdf_open(filename,'NC_NOWRITE');
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
48 [gid,varid] = ncvarid(ncid,varname);
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
49 [varname_,xtype,dimids,natts] = netcdf_inqVar(gid,varid);
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
50
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
51 % number of dimenions
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
52 nd = length(dimids);
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
53
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
54 sz = zeros(1,nd);
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
55 for i=1:length(dimids)
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
56 [dimname, sz(i)] = netcdf_inqDim(gid,dimids(i));
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
57 end
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
58
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
59 if nargin < 3
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
60 start = ones(1,nd);
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
61 end
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
62
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
63 if nargin < 4
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
64 count = inf*ones(1,nd);
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
65 end
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
66
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
67 if nargin < 5
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
68 stride = ones(1,nd);
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
69 end
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
70
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
71 % replace inf in count
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
72 i = count == inf;
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
73 count(i) = (sz(i)-start(i))./stride(i) + 1;
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
74
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
75 x = netcdf_getVar(gid,varid,start-1,count,stride);
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
76
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
77 % apply attributes
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
78
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
79 factor = [];
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
80 offset = [];
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
81 fv = [];
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
82
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
83 for i = 0:natts-1
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
84 attname = netcdf_inqAttName(gid,varid,i);
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
85
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
86 if strcmp(attname,'scale_factor')
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
87 factor = netcdf_getAtt(gid,varid,'scale_factor');
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
88 elseif strcmp(attname,'add_offset')
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
89 offset = netcdf_getAtt(gid,varid,'add_offset');
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
90 elseif strcmp(attname,'_FillValue')
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
91 fv = netcdf_getAtt(gid,varid,'_FillValue');
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
92 end
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
93 end
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
94
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
95 if !isempty(factor) || !isempty(factor) || !isempty(offset)
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
96 if !isa(x,'double')
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
97 x = double(x);
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
98 end
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
99 end
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
100
12561
8476336c120c ncread: do not replace _FillValue by NaNs for characters
abarth93
parents: 11907
diff changeset
101 if !isempty(fv) && xtype != netcdf_getConstant('char') && ...
8476336c120c ncread: do not replace _FillValue by NaNs for characters
abarth93
parents: 11907
diff changeset
102 xtype != netcdf_getConstant('string')
11907
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
103 x(x == fv) = NaN;
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
104 end
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
105
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
106 if !isempty(factor)
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
107 x = x * factor;
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
108 end
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
109
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
110 if !isempty(offset)
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
111 x = x + offset;
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
112 end
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
113
24f335b0c8df source code restructuration (1)
abarth93
parents:
diff changeset
114 netcdf_close(ncid);