1 |
function [S] = rdmnc(varargin) |
2 |
|
3 |
% Usage: |
4 |
% S=RDMNC(FILE1,FILE2,...) |
5 |
% S=RDMNC(FILE1,...,ITER) |
6 |
% S=RDMNC(FILE1,...,'VAR1','VAR2',...) |
7 |
% S=RDMNC(FILE1,...,'VAR1','VAR2',...,ITER) |
8 |
% |
9 |
% Input: |
10 |
% FILE1 Either a single file name (e.g. 'state.nc') or a wild-card |
11 |
% strings expanding to a group of file names (e.g. 'state.*.nc'). |
12 |
% There are no assumptions about suffices (e.g. 'state.*' works). |
13 |
% VAR1 Model variable names as written in the MNC/netcdf file. |
14 |
% ITER Vector of iterations in the MNC/netcdf files, not model time. |
15 |
% |
16 |
% Output: |
17 |
% S Structure with fields corresponding to 'VAR1', 'VAR2', ... |
18 |
% |
19 |
% Description: |
20 |
% This function is a rudimentary wrapper for joining and reading netcdf |
21 |
% files produced by MITgcm. It does not give the same flexibility as |
22 |
% opening the netcdf files directly using netcdf(), but is useful for |
23 |
% quick loading of entire model fields which are distributed in multiple |
24 |
% netcdf files. |
25 |
% |
26 |
% Example: |
27 |
% >> S=rdmnd('state.*','XC','YC','T'); |
28 |
% >> imagesc( S.XC, S.YC, S.T(:,:,1)' ); |
29 |
% |
30 |
% Author: Alistair Adcroft |
31 |
% Modifications: Daniel Enderton |
32 |
|
33 |
% $Header: /u/gcmpack/MITgcm/utils/matlab/rdmnc.m,v 1.20 2010/05/14 11:29:16 mlosch Exp $ |
34 |
% $Name: $ |
35 |
|
36 |
% Initializations |
37 |
dBug=0; |
38 |
file={}; |
39 |
filepaths={}; |
40 |
files={}; |
41 |
varlist={}; |
42 |
iters=[]; |
43 |
|
44 |
% find out if matlab's generic netcdf API is available |
45 |
% if not assume that Chuck Denham's netcdf toolbox is installed |
46 |
usemexcdf = isempty(which('netcdf.open')); |
47 |
|
48 |
% Process function arguments |
49 |
for iarg=1:nargin; |
50 |
arg=varargin{iarg}; |
51 |
if ischar(arg) |
52 |
if isempty(dir(char(arg))) |
53 |
varlist{end+1}=arg; |
54 |
else |
55 |
file{end+1}=arg; |
56 |
end |
57 |
else |
58 |
if isempty(iters) |
59 |
iters=arg; |
60 |
else |
61 |
error(['The only allowed numeric argument is iterations',... |
62 |
' to read in as a vector for the last argument']); |
63 |
end |
64 |
end |
65 |
end |
66 |
if isempty(file) |
67 |
if isempty(varlist), |
68 |
fprintf( 'No file name in argument list\n'); |
69 |
else |
70 |
fprintf(['No file in argument list:\n ==> ',char(varlist(1))]); |
71 |
for i=2:size(varlist,2), fprintf([' , ',char(varlist(i))]); end |
72 |
fprintf(' <==\n'); |
73 |
end |
74 |
error(' check argument list !!!'); |
75 |
end |
76 |
|
77 |
% Create list of filenames |
78 |
for eachfile=file |
79 |
filepathtemp=eachfile{:}; |
80 |
indecies = find(filepathtemp==filesep); |
81 |
if ~isempty(indecies) |
82 |
filepathtemp = filepathtemp(1:indecies(end)); |
83 |
else |
84 |
filepathtemp = ''; |
85 |
end |
86 |
expandedEachFile=dir(char(eachfile{1})); |
87 |
for i=1:size(expandedEachFile,1); |
88 |
if expandedEachFile(i).isdir==0 |
89 |
files{end+1}=expandedEachFile(i).name; |
90 |
filepaths{end+1}=filepathtemp; |
91 |
end |
92 |
end |
93 |
end |
94 |
|
95 |
|
96 |
% If iterations unspecified, open all the files and make list of all the |
97 |
% iterations that appear, use this iterations list for data extraction. |
98 |
if isempty(iters) |
99 |
iters = []; |
100 |
for ieachfile=1:length(files) |
101 |
eachfile = [filepaths{ieachfile},files{ieachfile}]; |
102 |
if usemexcdf |
103 |
nc=netcdf(char(eachfile),'read'); |
104 |
nciters = nc{'iter'}(:); |
105 |
if isempty(nciters), nciters = nc{'T'}(:); end |
106 |
close(nc); |
107 |
else |
108 |
% the parser complains about "netcdf.open" when the matlab netcdf |
109 |
% API is not available, even when it is not used so we have to |
110 |
% avoid the use of "netcdf.open", etc in this function |
111 |
nciters = ncgetvar(char(eachfile),'iter'); |
112 |
if isempty(nciters), nciters = ncgetvar(char(eachfile),'T'); end |
113 |
end |
114 |
iters = [iters,nciters']; |
115 |
end |
116 |
iters = unique(iters'); |
117 |
end |
118 |
|
119 |
% Cycle through files for data extraction. |
120 |
S.attributes=[]; |
121 |
for ieachfile=1:length(files) |
122 |
eachfile = [filepaths{ieachfile},files{ieachfile}]; |
123 |
if dBug > 0, fprintf([' open: ',eachfile]); end |
124 |
if usemexcdf |
125 |
nc=netcdf(char(eachfile),'read'); |
126 |
S=rdmnc_local(nc,varlist,iters,S,dBug); |
127 |
close(nc); |
128 |
else |
129 |
% the parser complains about "netcdf.open" when the matlab netcdf |
130 |
% API is not available, even when it is not used so we have to |
131 |
% avoid the use of "netcdf.open", etc in this function |
132 |
S=rdmnc_local_matlabAPI(char(eachfile),varlist,iters,S,dBug); |
133 |
end |
134 |
end |
135 |
|
136 |
return |
137 |
|
138 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
139 |
% Local functions % |
140 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
141 |
|
142 |
function [A] = read_att(nc); |
143 |
allatt=ncnames(att(nc)); |
144 |
if ~isempty(allatt) |
145 |
for attr=allatt; |
146 |
A.(char(attr))=nc.(char(attr))(:); |
147 |
end |
148 |
else |
149 |
A = 'none'; |
150 |
end |
151 |
|
152 |
function [i0,j0,fn] = findTileOffset(S); |
153 |
fn=0; |
154 |
if isfield(S,'attributes') & isfield(S.attributes,'global') |
155 |
G=S.attributes.global; |
156 |
tn=G.tile_number; |
157 |
snx=G.sNx; sny=G.sNy; nsx=G.nSx; nsy=G.nSy; npx=G.nPx; npy=G.nPy; |
158 |
ntx=nsx*npx;nty=nsy*npy; |
159 |
gbi=mod(tn-1,ntx); gbj=(tn-gbi-1)/ntx; |
160 |
i0=snx*gbi; j0=sny*gbj; |
161 |
if isfield(S.attributes.global,'exch2_myFace') |
162 |
fn=G.exch2_myFace; |
163 |
end |
164 |
else |
165 |
i0=0;j0=0; |
166 |
end |
167 |
%[snx,sny,nsx,nsy,npx,npy,ntx,nty,i0,j0,fn]; |
168 |
|
169 |
function [S] = rdmnc_local(nc,varlist,iters,S,dBug) |
170 |
|
171 |
fiter = nc{'iter'}(:); % File iterations present |
172 |
if isempty(fiter), fiter = nc{'T'}(:); end |
173 |
if isinf(iters); iters = fiter(end); end |
174 |
if isnan(iters); iters = fiter; end |
175 |
[fii,dii] = ismember(fiter,iters); fii = find(fii); % File iteration index |
176 |
dii = dii(find(dii ~= 0)); % Data interation index |
177 |
if dBug > 0, |
178 |
fprintf(' ; fii='); fprintf(' %i',fii); |
179 |
fprintf(' ; dii='); fprintf(' %i',dii); fprintf(' \n'); |
180 |
end |
181 |
|
182 |
% No variables specified? Default to all |
183 |
if isempty(varlist), varlist=ncnames(var(nc)); end |
184 |
|
185 |
% Attributes for structure |
186 |
if iters>0; S.iters_from_file=iters; end |
187 |
S.attributes.global=read_att(nc); |
188 |
[pstr,netcdf_fname,ext] = fileparts(name(nc)); |
189 |
if strcmp(netcdf_fname(end-3:end),'glob') |
190 |
% assume it is a global file produced by gluemnc and change some |
191 |
% attributes |
192 |
S.attributes.global.sNx = S.attributes.global.Nx; |
193 |
S.attributes.global.sNy = S.attributes.global.Ny; |
194 |
S.attributes.global.nPx = 1; |
195 |
S.attributes.global.nSx = 1; |
196 |
S.attributes.global.nPy = 1; |
197 |
S.attributes.global.nSy = 1; |
198 |
S.attributes.global.tile_number = 1; |
199 |
S.attributes.global.nco_openmp_thread_number = 1; |
200 |
end |
201 |
|
202 |
% Read variable data |
203 |
for ivar=1:size(varlist,2) |
204 |
|
205 |
cvar=char(varlist{ivar}); |
206 |
if isempty(nc{cvar}) |
207 |
disp(['No such variable ''',cvar,''' in MNC file ',name(nc)]); |
208 |
continue |
209 |
end |
210 |
|
211 |
dims = ncnames(dim(nc{cvar})); % Dimensions |
212 |
sizVar = size(nc{cvar}); nDims=length(sizVar); |
213 |
if dims{1} == 'T' |
214 |
if isempty(find(fii)), error('Iters not found'); end |
215 |
it = length(dims); |
216 |
tmpdata = nc{cvar}(fii,:); |
217 |
% leading unity dimensions get lost; add them back: |
218 |
tmpdata=reshape(tmpdata,[length(fii) sizVar(2:end)]); |
219 |
else |
220 |
it = 0; |
221 |
tmpdata = nc{cvar}(:); |
222 |
% leading unity dimensions get lost; add them back: |
223 |
tmpdata=reshape(tmpdata,sizVar); |
224 |
end |
225 |
|
226 |
if dBug > 1, |
227 |
fprintf([' var:',cvar,': nDims=%i ('],nDims);fprintf(' %i',size(nc{cvar})); |
228 |
fprintf('):%i,nD=%i,it=%i ;',length(size(tmpdata)),length(dims),it); |
229 |
end |
230 |
if length(dims) > 1, |
231 |
tmpdata=permute(tmpdata,[nDims:-1:1]); |
232 |
end |
233 |
if dBug > 1, |
234 |
% fprintf('(tmpdata:');fprintf(' %i',size(tmpdata)); fprintf(')'); |
235 |
end |
236 |
[ni nj nk nm nn no np]=size(tmpdata); |
237 |
|
238 |
[i0,j0,fn]=findTileOffset(S); |
239 |
cdim=dims{end}; if cdim(1)~='X'; i0=0; end |
240 |
cdim=dims{end}; if cdim(1)=='Y'; i0=j0; j0=0; end |
241 |
if length(dims)>1; |
242 |
cdim=dims{end-1}; if cdim(1)~='Y'; j0=0; end |
243 |
else |
244 |
j0=0; |
245 |
end |
246 |
if dBug > 1, |
247 |
fprintf(' i0,ni= %i %i; j,nj= %i %i; nk=%i :',i0,ni,j0,nj,nk); |
248 |
end |
249 |
|
250 |
Sstr = ''; |
251 |
for istr = 1:max(nDims,length(dims)), |
252 |
if istr == it, Sstr = [Sstr,'dii,']; |
253 |
elseif istr == 1, Sstr = [Sstr,'i0+(1:ni),']; |
254 |
elseif istr == 2, Sstr = [Sstr,'j0+(1:nj),']; |
255 |
elseif istr == 3, Sstr = [Sstr,'(1:nk),']; |
256 |
elseif istr == 4, Sstr = [Sstr,'(1:nm),']; |
257 |
elseif istr == 5, Sstr = [Sstr,'(1:nn),']; |
258 |
elseif istr == 6, Sstr = [Sstr,'(1:no),']; |
259 |
elseif istr == 7, Sstr = [Sstr,'(1:np),']; |
260 |
else, error('Can''t handle this many dimensions!'); |
261 |
end |
262 |
end |
263 |
eval(['S.(cvar)(',Sstr(1:end-1),')=tmpdata;']) |
264 |
%S.(cvar)(i0+(1:ni),j0+(1:nj),(1:nk),(1:nm),(1:nn),(1:no),(1:np))=tmpdata; |
265 |
if dBug > 1, fprintf(' %i',size(S.(cvar))); fprintf('\n'); end |
266 |
|
267 |
S.attributes.(cvar)=read_att(nc{cvar}); |
268 |
% replace missing or FillValues with NaN |
269 |
if isfield(S.attributes.(cvar),'missing_value'); |
270 |
misval = S.attributes.(cvar).missing_value; |
271 |
S.(cvar)(S.(cvar) == misval) = NaN; |
272 |
end |
273 |
if isfield(S.attributes.(cvar),'FillValue_'); |
274 |
misval = S.attributes.(cvar).FillValue_; |
275 |
S.(cvar)(S.(cvar) == misval) = NaN; |
276 |
end |
277 |
|
278 |
end % for ivar |
279 |
|
280 |
if isempty(S) |
281 |
error('Something didn''t work!!!'); |
282 |
end |
283 |
|
284 |
return |
285 |
|
286 |
function [S] = rdmnc_local_matlabAPI(fname,varlist,iters,S,dBug) |
287 |
|
288 |
fiter = ncgetvar(fname,'iter'); % File iterations present |
289 |
if isempty(fiter), fiter = ncgetvar(fname,'T'); end |
290 |
if isinf(iters); iters = fiter(end); end |
291 |
if isnan(iters); iters = fiter; end |
292 |
[fii,dii] = ismember(fiter,iters); fii = find(fii); % File iteration index |
293 |
dii = dii(find(dii ~= 0)); % Data interation index |
294 |
if dBug > 0, |
295 |
fprintf(' ; fii='); fprintf(' %i',fii); |
296 |
fprintf(' ; dii='); fprintf(' %i',dii); fprintf(' \n'); |
297 |
end |
298 |
|
299 |
% now open the file for reading |
300 |
nc = netcdf.open(fname,'NC_NOWRITE'); |
301 |
% get basic information about netcdf file |
302 |
[ndims nvars natts dimm] = netcdf.inq(nc); |
303 |
|
304 |
% No variables specified? Default to all |
305 |
if isempty(varlist), |
306 |
for k=0:nvars-1 |
307 |
varlist{k+1} = netcdf.inqVar(nc,k); |
308 |
end |
309 |
end |
310 |
|
311 |
% Attributes for structure |
312 |
if iters>0; S.iters_from_file=iters; end |
313 |
S.attributes.global=ncgetatt(nc,'global'); |
314 |
[pstr,netcdf_fname,ext] = fileparts(fname); |
315 |
if strcmp(netcdf_fname(end-3:end),'glob') |
316 |
% assume it is a global file produced by gluemnc and change some |
317 |
% attributes |
318 |
S.attributes.global.sNx = S.attributes.global.Nx; |
319 |
S.attributes.global.sNy = S.attributes.global.Ny; |
320 |
S.attributes.global.nPx = 1; |
321 |
S.attributes.global.nSx = 1; |
322 |
S.attributes.global.nPy = 1; |
323 |
S.attributes.global.nSy = 1; |
324 |
S.attributes.global.tile_number = 1; |
325 |
S.attributes.global.nco_openmp_thread_number = 1; |
326 |
end |
327 |
|
328 |
% Read variable data |
329 |
for ivar=1:size(varlist,2) |
330 |
|
331 |
cvar=char(varlist{ivar}); |
332 |
varid=ncfindvarid(nc,cvar); |
333 |
if isempty(varid) |
334 |
disp(['No such variable ''',cvar,''' in MNC file ',fname]); |
335 |
continue |
336 |
end |
337 |
|
338 |
[varname,xtype,dimids,natts] = netcdf.inqVar(nc,varid); |
339 |
|
340 |
% Dimensions |
341 |
clear sizVar dims |
342 |
for k=1:length(dimids) |
343 |
[dims{k}, sizVar(k)] = netcdf.inqDim(nc,dimids(k)); |
344 |
end |
345 |
if length(sizVar)==1; sizVar(2) = 1; end; |
346 |
nDims=length(sizVar); |
347 |
if dims{1} == 'T' |
348 |
if isempty(find(fii)), error('Iters not found'); end |
349 |
it = length(dims); |
350 |
tmpdata = netcdf.getVar(nc,varid,'double'); |
351 |
tmpdata = tmpdata(fii,:); |
352 |
% leading unity dimensions get lost; add them back: |
353 |
tmpdata=reshape(tmpdata,[length(fii) sizVar(2:end)]); |
354 |
else |
355 |
it = 0; |
356 |
tmpdata = netcdf.getVar(nc,varid,'double'); |
357 |
% leading unity dimensions get lost; add them back: |
358 |
tmpdata=reshape(tmpdata,sizVar); |
359 |
end |
360 |
|
361 |
if dBug > 1, |
362 |
fprintf([' var:',cvar,': nDims=%i ('],nDims);fprintf(' %i',sizVar); |
363 |
fprintf('):%i,nD=%i,it=%i ;',length(size(tmpdata)),length(dims),it); |
364 |
end |
365 |
[ni nj nk nm nn no np]=size(tmpdata); |
366 |
|
367 |
[i0,j0,fn]=findTileOffset(S); |
368 |
cdim=dims{1}; if cdim(1)~='X'; i0=0; end |
369 |
cdim=dims{1}; if cdim(1)=='Y'; i0=j0; j0=0; end |
370 |
if length(dims)>1; |
371 |
cdim=dims{2}; if cdim(1)~='Y'; j0=0; end |
372 |
else |
373 |
j0=0; |
374 |
end |
375 |
if dBug > 1, |
376 |
fprintf(' i0,ni= %i %i; j,nj= %i %i; nk=%i :',i0,ni,j0,nj,nk); |
377 |
end |
378 |
|
379 |
Sstr = ''; |
380 |
for istr = 1:max(nDims,length(dims)), |
381 |
if istr == it, Sstr = [Sstr,'dii,']; |
382 |
elseif istr == 1, Sstr = [Sstr,'i0+(1:ni),']; |
383 |
elseif istr == 2, Sstr = [Sstr,'j0+(1:nj),']; |
384 |
elseif istr == 3, Sstr = [Sstr,'(1:nk),']; |
385 |
elseif istr == 4, Sstr = [Sstr,'(1:nm),']; |
386 |
elseif istr == 5, Sstr = [Sstr,'(1:nn),']; |
387 |
elseif istr == 6, Sstr = [Sstr,'(1:no),']; |
388 |
elseif istr == 7, Sstr = [Sstr,'(1:np),']; |
389 |
else, error('Can''t handle this many dimensions!'); |
390 |
end |
391 |
end |
392 |
eval(['S.(cvar)(',Sstr(1:end-1),')=tmpdata;']) |
393 |
%S.(cvar)(i0+(1:ni),j0+(1:nj),(1:nk),(1:nm),(1:nn),(1:no),(1:np))=tmpdata; |
394 |
if dBug > 1, fprintf(' %i',size(S.(cvar))); fprintf('\n'); end |
395 |
|
396 |
S.attributes.(cvar)=ncgetatt(nc,cvar); |
397 |
% replace missing or FillValues with NaN |
398 |
if isfield(S.attributes.(cvar),'missing_value'); |
399 |
misval = S.attributes.(cvar).missing_value; |
400 |
S.(cvar)(S.(cvar) == misval) = NaN; |
401 |
end |
402 |
if isfield(S.attributes.(cvar),'FillValue_'); |
403 |
misval = S.attributes.(cvar).FillValue_; |
404 |
S.(cvar)(S.(cvar) == misval) = NaN; |
405 |
end |
406 |
|
407 |
end % for ivar |
408 |
|
409 |
% close the file |
410 |
netcdf.close(nc); |
411 |
|
412 |
if isempty(S) |
413 |
error('Something didn''t work!!!'); |
414 |
end |
415 |
|
416 |
return |
417 |
|
418 |
function vf = ncgetvar(fname,varname) |
419 |
% read a netcdf variable |
420 |
|
421 |
nc=netcdf.open(fname,'NC_NOWRITE'); |
422 |
% find out basics about the files |
423 |
[ndims nvars natts dimm] = netcdf.inq(nc); |
424 |
vf = []; |
425 |
varid = []; |
426 |
for k=0:nvars-1 |
427 |
if strcmp(netcdf.inqVar(nc,k),varname) |
428 |
varid = netcdf.inqVarId(nc,varname); |
429 |
end |
430 |
end |
431 |
if ~isempty(varid); |
432 |
[varn,xtype,dimids,natts] = netcdf.inqVar(nc,varid); |
433 |
% get data |
434 |
vf = double(netcdf.getVar(nc,varid)); |
435 |
else |
436 |
% do nothing |
437 |
end |
438 |
netcdf.close(nc); |
439 |
|
440 |
return |
441 |
|
442 |
function misval = ncgetmisval(nc,varid) |
443 |
|
444 |
[varname,xtype,dimids,natts] = netcdf.inqVar(nc,varid); |
445 |
misval = []; |
446 |
for k=0:natts-1 |
447 |
attn = netcdf.inqAttName(nc,varid,k); |
448 |
if strcmp(attn,'missing_value') | strcmp(attn,'_FillValue') |
449 |
misval = double(netcdf.getAtt(nc,varid,attname)); |
450 |
end |
451 |
end |
452 |
|
453 |
function A = ncgetatt(nc,varname) |
454 |
% get all attributes and put them into a struct |
455 |
|
456 |
% 1. get global properties of file |
457 |
[ndims nvars natts dimm] = netcdf.inq(nc); |
458 |
|
459 |
% get variable ID and properties |
460 |
if strcmp('global',varname) |
461 |
% "variable" is global |
462 |
varid = netcdf.getConstant('NC_GLOBAL'); |
463 |
else |
464 |
% find variable ID and properties |
465 |
varid = ncfindvarid(nc,varname); |
466 |
if ~isempty(varid) |
467 |
[varn,xtype,dimids,natts] = netcdf.inqVar(nc,varid); |
468 |
else |
469 |
warning(sprintf('variable %s not found',varname)) |
470 |
end |
471 |
end |
472 |
|
473 |
if natts > 1 |
474 |
for k=0:natts-1 |
475 |
attn = netcdf.inqAttName(nc,varid,k); |
476 |
[xtype attlen]=netcdf.inqAtt(nc,varid,attn); |
477 |
attval = netcdf.getAtt(nc,varid,attn); |
478 |
if ~ischar(attval) |
479 |
attval = double(attval); |
480 |
end |
481 |
A.(char(attn))=attval; |
482 |
end |
483 |
else |
484 |
A = 'none'; |
485 |
end |
486 |
|
487 |
return |
488 |
|
489 |
|
490 |
function varid = ncfindvarid(nc,varname) |
491 |
|
492 |
[ndims nvars natts dimm] = netcdf.inq(nc); |
493 |
varid=[]; |
494 |
for k=0:nvars-1 |
495 |
if strcmp(netcdf.inqVar(nc,k),varname); |
496 |
varid = netcdf.inqVarId(nc,varname); |
497 |
break |
498 |
end |
499 |
end |
500 |
|
501 |
return |