Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
function self = read(self, varargin)
%
%varargin{1} ........... waitbar (boolean)
%
% $Id$
% no waitbar per default
% ----------------------
wbool = false;
% check varargin
% --------------
if nargin > 1
if varargin{1}
wbool = true;
end
end
% check file validity
% -------------------
self.nc_id = netcdf.open(self.file, self.mode);
if self.nc_id == -1
error(['netcdf:netcdf', ...
'The specified data file ''%s'' does not exist\n' ...
'or is not in the directory which is on the MATLAB path'],...
self.file);
end
% update waitbar title with Tex mode disable
% ------------------------------------------
if wbool
%set(0,'DefaulttextInterpreter','none')
wb = waitbar(0, ['Loading file. Please wait...']);
hchild = get(wb,'children');
htitle = get(hchild,'title');
set(htitle,'Interpreter','None');
end
% create a temporary hashtable use to store data read in file
% -----------------------------------------------------------
hash = hashtable;
% returns the number of dimensions, variables, global attributes and
% eventually unlimited dimension in a netCDF file.
% ------------------------------------------------------------------
[n_dims, nvars, ngatts, unlimdimid] = netcdf.inq(self.nc_id);
% get dimensions and store to netcdf hashtable
% --------------------------------------------
for dimid = 0:n_dims-1
% initialize empty structure
% --------------------------
s = [];
% Get name and length of id dimension
% -------------------------------------
[dimname, dimlen] = netcdf.inqDim(self.nc_id, dimid);
% Get dimid (dimension id) from name
% normalement pas besoin, a verifier....
% ----------------------------------
%dimid = netcdf.inqDimId(self.nc_id, dimname);
% fill dimension structure with name and boolean for unlimdimid
% -------------------------------------------------------------
s.dimlen = dimlen;
if(dimid == unlimdimid)
s.unlimited = true;
else
s.unlimited = false;
end
% put dimension name and length to temporary hashtable
% ----------------------------------------------------
hash = put(hash, dimname, s);
end
% put dimensions hashtable to netcdf hashtable
% --------------------------------------------
self = put(self, 'DIMENSIONS', hash);
% clear temporary hashtable for next use
% --------------------------------------
hash = clear(hash);
% bug (TODO), on ne peut pas utiliser hash = hash.clear !!!!
% 09/03/2009: ce n'est pas un bug, voir subsref de hashtable
% get variables and store to netcdf hashtable
% -------------------------------------------
for id = 0:nvars-1
% moves the waitbar cursor
% ------------------------
if wbool
waitbar( id/nvars, wb);
end
% initialize empty structure
% --------------------------
s = [];
% return information about variable
% ---------------------------------
[varName, xtype, dimids, natts] = netcdf.inqVar(self.nc_id, id);
% fill structure s with variable attribute names and values
% ---------------------------------------------------------
for attid = 0:natts-1
% get attribute name
% ------------------
attName = netcdf.inqAttName(self.nc_id, id, attid);
% if attribute name is '_FillValue', transforme to 'FillValue,
% because Matlab dosn't handle member name begining with '_'
% ------------------------------------------------------------
if strcmp(attName, '_FillValue')
s.('FillValue_') = netcdf.getAtt(self.nc_id, id, attName);
else
% otherwise, dynamically fill attribute member of structure s
% with it's value
% -----------------------------------------------------------
s.(attName) = netcdf.getAtt(self.nc_id, id, attName);
end
end % end for loop over variable attributes
% add internal type__ member from xtype 2->char, 6->double
% --------------------------------------------------------
s.type__ = getConstantNames(self, xtype);
% fill temporary structure s with value
% -------------------------------------

jacques.grelet_ird.fr
committed
try
s.data__ = netcdf.getVar(self.nc_id, id);
% under Matlab R2011b, getVar thrown an error when all _EXT dimensions
% are 0. In that case, set data to empty
% ------------------------------------------------------------------
catch ME

jacques.grelet_ird.fr
committed
fprintf('Notice: netcdf_native:read: %s set to empty\n', varName);
% fprintf('Notice: netcdf_native:read: %s: %s\n', varName, ME.message);

jacques.grelet_ird.fr
committed
s.data__ = [];
end
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
% If a NetCDF variable has valid scale_factor and add_offset
% attributes, then the data is scaled accordingly.
% ----------------------------------------------------------
if self.autoscale
if isfield(s, 'scale_factor') && isfield(s, 'add_offset')
s.data__ = s.data__ * s.scale_factor + s.add_offset;
end
end
% replace FillValue_ by NaN only for numeric variable
% autonan mode is set to true by default
% ---------------------------------------------------
if self.autonan && isfield(s, 'FillValue_')
switch(xtype)
case self.NC_CHAR
% For now, do nothing.
case { self.NC_DOUBLE, self.NC_FLOAT, self.NC_LONG,...
self.NC_SHORT, self.NC_BYTE }
% sometimes, FillValue could be a char a badformed netcdf
% files
% --------------------------------------------------------
if isnumeric(s.('FillValue_'))
s.data__(s.data__ == s.('FillValue_')) = NaN;
else
s.data__(s.data__ == str2double(s.('FillValue_'))) = NaN;
% verrue, pour les fichiers Roscop netcdf mal forms
% --------------------------------------------------
if str2double(s.('FillValue_')) > 1e35
s.data__(s.data__ >= 1e35) = NaN;
end
end
otherwise
error( 'unhandled datatype %d\n', xtype );
end
end
% % if var is char and as vertical alignment, transpose it
% % ------------------------------------------------------
% % if xtype == self.NC_CHAR && (size(s.data__, 1) ~= 1)
% % s.data__ = s.data__';
% % end
%
% % add internal dimension__ member
% % Because MATLAB uses FORTRAN-style ordering, however, the order of
% % the dimension IDs is reversed relative to what would be obtained
% % from the C API
% % ----------------------------
% if length(dimids) > 1
% s.data__ = permute(s.data__, fliplr(1:length(dimids)));
% dimids = fliplr(dimids);
% end
% if var is char and as vertical alignment, transpose it
% ------------------------------------------------------
if xtype == self.NC_CHAR && (size(s.data__', 1) == 1)
s.data__ = s.data__';
% Because MATLAB uses FORTRAN-style indexing, we need to transpose
% N-D array (k,i,j,...) to (i,j,k,...) however, the order of
% the dimension IDs is reversed relative to what would be obtained
% from the C API
% If s.data__ is a vector, NetCDF API return vertical vector,
% do nothing, it's OK
% -----------------------------------------------------------------
elseif length(dimids) > 1
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
s.data__ = permute(s.data__, fliplr(1:length(dimids)));
dimids = fliplr(dimids);
end
for dimid = 1:numel(dimids)
dimname = netcdf.inqDim(self.nc_id, dimids(dimid));
% we need to reverse dimid
% a verifier imperativement avec plusieurs dimensions
% ---------------------------------------
s.dimension__{dimid} = dimname;
end
% put variable name and value to temporary hashtable
% --------------------------------------------------
hash = put(hash, varName, s);
end
% put variable temporary hash to netcdf hashtable
% -----------------------------------------------
self = put(self, 'VARIABLES', hash);
% clear temporary hashtable for next use
% --------------------------------------
hash = clear(hash);
% get gloabal attributes and store to netcdf hashtable
% -----------------------------------------------------
for id = 0:ngatts-1
% initialize empty structure
% --------------------------
s = [];
% Get the name of the global attribute associated with the
% variable.
% --------------------------------------------------------
gattName = netcdf.inqAttName(self.nc_id, ...
self.NC_GLOBAL, id);
% Get value of global attribute.
% ------------------------------
attVal = netcdf.getAtt(self.nc_id, ...
self.NC_GLOBAL, gattName);
% fill global attribute structure
% -------------------------------
s.key__ = gattName;
s.data__ = attVal;
% put attribute name and value to temporary hashtable
% ---------------------------------------------------
hash = put(hash, gattName, s);
end
% put attribute temporary hash to netcdf hashtable
% ------------------------------------------------
self = put(self, 'ATTRIBUTES', hash);
% Close waitbar
% -------------
if wbool
close(wb)
end
% close netcdf file
% -----------------
netcdf.close(self.nc_id)

jacques.grelet_ird.fr
committed
end % end of read function