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