/[MITgcm]/MITgcm/utils/matlab/mnc_assembly.m
ViewVC logotype

Contents of /MITgcm/utils/matlab/mnc_assembly.m

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download)
Wed Feb 2 10:39:38 2005 UTC (19 years, 3 months ago) by mlosch
Branch: MAIN
Changes since 1.1: +25 -9 lines
o make mnc_assembly also work with Matlab 6.1
  -replace all "||" by "|" and "&&" by "&"
o add a quick fix for multiple processors as opposed to multiple tiles
  (for exch=1)

1 function [nt,nf] = mnc_assembly(fpat,vars, fout,fsize)
2
3 % Function [nt,nf] = mnc_assembly(fpat,vars, fout,fsize)
4 %
5 % INPUTS
6 % fpat string containing the file pattern
7 % vars cell array of variable names
8 %
9 % fout output file pattern (DEF: "all.%05d.nc")
10 % fsize max output file size (DEF: 2.0e+9 = +/-2GB)
11 %
12 % OUTPUTS
13 % nt number of usable tiles found
14 % nf number of output files written
15 %
16 % This function "assembles" MNC output. It finds all the per-tile
17 % NetCDF files that match the input pattern, does some basic "sanity"
18 % tests to determine whether the files have compatible sizes, and
19 % then assembles all of the requested data (all of the variables)
20 % into one or more "global" NetCDF files. The global files have
21 % the following dimension conventions:
22 %
23 % "exch 1": all values are within a global horizontal grid
24 % and indicies are (X,Y,Z,T)
25 %
26 % "exch 2": all values are within one of up to six "faces"
27 % of a global cube with indicies (Xf,Yf,F,Z,T)
28 %
29 % where "X,Y.Z,T" are global space/time indicies, "Xf,Yf" are local
30 % per-face spatial indicies, and "F" is a face index.
31
32
33 %===== Argument checking and defaults =====
34
35 if nargin < 2
36 disp('Error: there must be at least 2 arguments!');
37 return
38 end
39
40 if nargin < 3
41 fout = 'all.%05d.nc';
42 end
43 if nargin < 4
44 fsize = 2.0e+9;
45 end
46
47
48 %===== Find and open all the matching files =====
49
50 nt = 0;
51 nf = 0;
52 all_ncf = struct([]);
53
54 % Find all of the files
55 exch2_msg = 0;
56 tmax = 200;
57 frdone = 0;
58 it = 0;
59 while frdone == 0
60
61 it = it + 1;
62 fnm = sprintf(fpat,it);
63 % disp(fnm);
64
65 % Check that the file exists
66 fid = fopen(fnm, 'r');
67 if fid < 0
68 if it >= tmax
69 frdone = 1;
70 end
71 continue;
72 end
73
74 % Open the NetCDF file
75 fnc = netcdf(fnm, 'nowrite');
76 if length(fnc) == 0
77 continue;
78 end
79
80 % Check for exch1/exch2 grid
81 exch = 1;
82 exch2_myFace = fnc.exch2_myFace(:);
83 if length(exch2_myFace) ~= 0
84 exch = 2;
85 if exch2_msg == 0
86 exch2_msg = 1;
87 disp(' Grid type appears to be: "exch2"');
88 end
89 end
90
91 n = length(all_ncf) + 1;
92 all_ncf(n).name = fnm;
93 all_ncf(n).nc = {fnc};
94 all_ncf(n).exch = exch;
95 all_ncf(n).tile_number = fnc.tile_number(1);
96 all_ncf(n).bi = fnc.bi(1);
97 all_ncf(n).bj = fnc.bj(1);
98 all_ncf(n).sNx = fnc.sNx(1);
99 all_ncf(n).sNy = fnc.sNy(1);
100 all_ncf(n).Nx = fnc.Nx(1);
101 all_ncf(n).Ny = fnc.Ny(1);
102 all_ncf(n).Z = fnc.Z(1);
103
104 if exch == 2
105 all_ncf(n).exch2_myFace = exch2_myFace;
106 all_ncf(n).exch2_tbasex = fnc.exch2_tbasex(1);
107 all_ncf(n).exch2_tbasey = fnc.exch2_tbasex(1);
108 end
109
110 clear fnc
111 end
112
113
114 %===== Do some basic sanity checks =====
115
116 % check for number of files/tiles found
117 if length(all_ncf) == 0
118 disp('Error: no tiles found--no need to do any assembly!');
119 return
120 elseif length(all_ncf) == 1
121 disp('Error: one tile found--no need to do any assembly!');
122 return
123 else
124 disp(sprintf(' Found %d files matching the pattern: "%s"', ...
125 length(all_ncf), fpat ));
126 end
127
128 % check for consistent "exch" version
129 if prod(double([all_ncf.exch] == all_ncf(1).exch)) ~= 1
130 disp('Error: not all the "exch" types of the files match.');
131 return;
132 end
133
134 % check for consistent sNx,sNy
135 if (prod(double([all_ncf.sNx] == all_ncf(1).sNx)) ~= 1) ...
136 | (prod(double([all_ncf.sNy] == all_ncf(1).sNy)) ~= 1)
137 disp('Error: the "sNx,sNy" values for all the tiles are not');
138 disp(' uniform. Future versions of this function will be');
139 disp(' able to handle non-uniform grid sizes but this');
140 disp(' feature is not yet implemented.');
141 return;
142 end
143
144 % check for redundant tiles and "time series" output
145 if length(all_ncf) ~= length(unique([all_ncf.tile_number]))
146 disp('Error: redundant tiles were found. Please check that');
147 disp(' the file pattern does not specify output spanning');
148 disp(' multiple model runs or even multiple time series');
149 disp(' within a single model run. For multi-time-series');
150 disp(' data sets, EACH "LEVEL" IN THE OUTPUT SERIES MUST');
151 disp(' BE ASSEMBLED SEPARATERLY.');
152 return
153 end
154
155
156 %===== Get the dims/vars associations =====
157
158 mydims = struct('names', {}, 'lens', {});
159 myvars = struct([]);
160 clear tncf;
161 for ivar = 1:length(vars)
162 mydim_names = {};
163 mydim_sizes = {};
164 myatt.names = {};
165 myatt.types = {};
166 myatt.data = {};
167 myname = vars(ivar).name;
168 disp([' Looking for variable: ' myname]);
169
170 itile = 1;
171 tncf = all_ncf(itile).nc{1};
172 ncv = tncf{myname};
173 len = length(ncv);
174 if length(ncv) == 0
175 warns = [' Warning: variable "%s" is not defined in "%s"\n' ...
176 ' so it will be ignored.'];
177 disp(sprintf(warns,myname,all_ncf(itile).name));
178 continue
179 end
180 mytype = datatype(ncv);
181 tmpdims = dim(ncv);
182 for inm = 1:length(tmpdims)
183 mydim_names{inm} = name(tmpdims{inm});
184 mydim_sizes{inm} = tmpdims{inm}(:);
185 end
186 for iat = 1:length(att(ncv))
187 aaa = att(ncv);
188 myatt.names(iat) = { name(aaa{iat}) };
189 myatt.types(iat) = { datatype(aaa{iat}) };
190 aab = aaa{iat};
191 myatt.data(iat) = { aab(:) };
192 end
193
194 % confirm: vars have same dim names across all files
195 ierr = 0;
196 for itile = 2:length(all_ncf)
197 tncf = all_ncf(itile).nc{1};
198 ncv = tncf{myname};
199 len = length(ncv);
200 if length(ncv) == 0
201 warns = [' Warning: variable "%s" is not defined in "%s"\n' ...
202 ' so it will be ignored.'];
203 disp(sprintf(warns,myname,all_ncf(itile).name));
204 continue
205 end
206 tmpdims = dim(ncv);
207 for inm = 1:length(tmpdims)
208 if mydim_names{inm} ~= name(tmpdims{inm})
209 warns = ...
210 [' Warning: variable "%s" is not CONSISTENTLY defined.\n' ...
211 ' It has different dimensions in different files so\n' ...
212 ' so it will be ignored.'];
213 disp(sprintf(warns,myname));
214 ierr = 1;
215 break
216 end
217 mydim_sizes{inm} = max([ tmpdims{inm}(:) mydim_sizes{inm} ]);
218 end
219
220 end
221
222 if ierr == 0
223 % check: does the variable have a "horizontal" component
224 has_horiz = 0;
225 horiz_names = { 'X' 'Y' 'Xp1' 'Yp1' };
226 for id = 1:length(mydim_names)
227 if length([intersect(horiz_names,mydim_names{id})]) > 0
228 has_horiz = 1;
229 end
230 end
231 % disp([ ' ' myname ' ' sprintf('%d',has_horiz) ]);
232
233 imy = length(myvars) + 1;
234 myvars(imy).name = myname;
235 myvars(imy).type = mytype;
236 myvars(imy).dim_names = mydim_names;
237 myvars(imy).dim_sizes = mydim_sizes;
238 myvars(imy).atts = myatt;
239 myvars(imy).has_horiz = has_horiz;
240
241 addl = setdiff(mydim_names,[mydims.names]);
242 for iaddl = 1:length(addl)
243 np1 = length(mydims) + 1;
244 mydims(np1).names = addl(iaddl);
245 mydims(np1).lens = mydim_sizes(find(strcmp(addl(iaddl),mydim_names)));
246 end
247
248 end
249 end
250
251 % For exch == 2, we need to add a "face" dimension
252 if all_ncf(1).exch == 2
253 np1 = length(mydims) + 1;
254 mydims(np1).names = { 'iface' };
255 mydims(np1).lens = { length(unique([all_ncf.exch2_myFace])) };
256 end
257
258 % myvars.name
259 % myvars.dim_names
260 % myvars.dim_sizes
261 % myvars(2).dim_names
262 % myvars(2).dim_names(4)
263
264 % mydims
265 % length(mydims)
266 % [ mydims.names ]
267 % [ mydims.lens ]
268
269
270 %===== Assemble! =====
271
272
273 if all_ncf(1).exch == 1
274
275 % exch "1":
276
277 % $$$ bi_max = max([all_ncf.bi]);
278 % $$$ bj_max = max([all_ncf.bj]);
279 % $$$ Xmax = bi_max * all_ncf(1).sNx;
280 % $$$ Ymax = bj_max * all_ncf(1).sNy;
281 Xmax = all_ncf(1).Nx;
282 Ymax = all_ncf(1).Ny;
283 % at this point I have to make some assumptions about the domain
284 % decomposition
285 bi_max = Xmax/all_ncf(1).sNx;
286 bj_max = Ymax/all_ncf(1).sNy;
287 itile = 0;
288 for bj=1:bj_max
289 for bi=1:bi_max
290 itile = itile+1;
291 all_ncf(itile).bi=bi;
292 all_ncf(itile).bj=bj;
293 end
294 end
295
296 horzdim = struct('names',{},'lens',{});
297 horzdim(1).names = { 'X' }; horzdim(1).lens = { Xmax };
298 horzdim(2).names = {'Xp1'}; horzdim(2).lens = { Xmax + 1 };
299 horzdim(3).names = { 'Y' }; horzdim(3).lens = { Ymax };
300 horzdim(4).names = {'Yp1'}; horzdim(4).lens = { Ymax + 1 };
301 horzdim(5).names = { 'T' }; horzdim(5).lens = { 0 };
302
303 iseq = 0;
304 foutnm = sprintf(fout, iseq);
305 fonc = netcdf(foutnm,'clobber'); % Should append-or-create!
306
307 for idim = 1:length(mydims)
308 dname = mydims(idim).names{1};
309 ind = find(strcmp(dname,[horzdim.names]));
310 if length(ind) ~= 0
311 dlen = horzdim(ind).lens{1};
312 else
313 dlen = mydims(idim).lens{1};
314 end
315 comm = sprintf('fonc(''%s'') = %d;',dname,dlen);
316 eval(comm);
317 end
318
319 for ivar = 1:length(myvars)
320 comm = sprintf('fonc{''%s''} = nc%s( ',myvars(ivar).name,myvars(ivar).type);
321 id = 1;
322 comm = [ comm sprintf('''%s''',myvars(ivar).dim_names{id}) ];
323 for id = 2:length(myvars(ivar).dim_names)
324 comm = [ comm sprintf(',''%s''',myvars(ivar).dim_names{id}) ];
325 end
326 comm = [ comm ' );' ];
327 eval(comm);
328 for iat = 1:length(myvars(ivar).atts.names)
329 comm = sprintf( ...
330 'fonc{''%s''}.%s = nc%s( myvars(ivar).atts.data{iat} );', ...
331 myvars(ivar).name, ...
332 myvars(ivar).atts.names{iat}, ...
333 myvars(ivar).atts.types{iat} );
334 eval(comm);
335 end
336 end
337
338 % for itime = 1:Tmax
339
340 % Here is where we need to check the output file size and start
341 % another file in the sequence, if necessary.
342
343 for ivar = 1:length(myvars)
344 disp(sprintf(' Copying variable: %s',myvars(ivar).name))
345 for itile = 1:length(all_ncf)
346
347 if (myvars(ivar).has_horiz == 1) | (itile == 1)
348
349 clear nct;
350 nct = all_ncf(itile).nc{1};
351 ox_off = (all_ncf(itile).bi - 1)*all_ncf(itile).sNx;
352 oy_off = (all_ncf(itile).bj - 1)*all_ncf(itile).sNy;
353 diml_in = '';
354 diml_out = '';
355 for jj = 1:length(myvars(ivar).dim_names)
356 doff = 1;
357 if jj > 1
358 diml_in = sprintf('%s,',diml_in);
359 diml_out = sprintf('%s,',diml_out);
360 end
361 dlen = myvars(ivar).dim_sizes{jj};
362 diml_in = sprintf('%s%s',diml_in, ':');
363 fchar = myvars(ivar).dim_names{jj}(1);
364 % disp([' fchar = ' fchar ' ' myvars(ivar).dim_names{jj}]);
365 if strcmp(myvars(ivar).dim_names{jj}(1),'X') == 1
366 doff = ox_off + doff;
367 dlen = ox_off + dlen;
368 end
369 if strcmp(myvars(ivar).dim_names{jj}(1),'Y') == 1
370 doff = oy_off + doff;
371 dlen = oy_off + dlen;
372 end
373 diml_out = sprintf('%s%d%s%d',diml_out,doff,':',dlen);
374 end
375
376 comm = sprintf( ...
377 'fonc{''%s''}(%s) = nct{''%s''}(%s);', ...
378 myvars(ivar).name, diml_out, myvars(ivar).name, diml_in );
379 % disp([ ' comm: ' comm ]);
380 eval(comm);
381
382 end
383
384 end
385 end
386 % end
387
388 fonc = close(fonc);
389
390 elseif all_ncf(1).exch == 2
391
392 % exch "2":
393 Xmax = 0;
394 Ymax = 0;
395 for ii = 1:length(all_ncf)
396 Xmax = max(Xmax, (all_ncf(ii).exch2_tbasex + all_ncf(ii).sNx));
397 Ymax = max(Ymax, (all_ncf(ii).exch2_tbasey + all_ncf(ii).sNy));
398 end
399
400 horzdim = struct('names',{},'lens',{});
401 horzdim(1).names = { 'X' }; horzdim(1).lens = { Xmax };
402 horzdim(2).names = {'Xp1'}; horzdim(2).lens = { Xmax + 1 };
403 horzdim(3).names = { 'Y' }; horzdim(3).lens = { Ymax };
404 horzdim(4).names = {'Yp1'}; horzdim(4).lens = { Ymax + 1 };
405 horzdim(5).names = { 'T' }; horzdim(5).lens = { 0 };
406
407 iseq = 0;
408 foutnm = sprintf(fout, iseq);
409 fonc = netcdf(foutnm,'clobber'); % Should append-or-create!
410
411 for idim = 1:length(mydims)
412 dname = mydims(idim).names{1};
413 ind = find(strcmp(dname,[horzdim.names]));
414 if length(ind) ~= 0
415 dlen = horzdim(ind).lens{1};
416 else
417 dlen = mydims(idim).lens{1};
418 end
419 comm = sprintf('fonc(''%s'') = %d;',dname,dlen);
420 eval(comm);
421 end
422
423 for ivar = 1:length(myvars)
424 comm = sprintf('fonc{''%s''} = nc%s( ',myvars(ivar).name,myvars(ivar).type);
425 id = 1;
426 comm = [ comm sprintf('''%s''',myvars(ivar).dim_names{id}) ];
427 for id = 2:length(myvars(ivar).dim_names)
428 dname = myvars(ivar).dim_names{id};
429 if (dname(1) == 'Y') & (myvars(ivar).has_horiz == 1)
430 comm = [ comm sprintf(',''%s''','iface') ];
431 end
432 comm = [ comm sprintf(',''%s''',dname) ];
433 end
434 comm = [ comm ' );' ];
435 eval(comm);
436 for iat = 1:length(myvars(ivar).atts.names)
437 comm = sprintf( ...
438 'fonc{''%s''}.%s = nc%s( myvars(ivar).atts.data{iat} );', ...
439 myvars(ivar).name, ...
440 myvars(ivar).atts.names{iat}, ...
441 myvars(ivar).atts.types{iat} );
442 eval(comm);
443 end
444 end
445
446 % Here is where we need to check the output file size and start
447 % another file in the sequence, if necessary.
448
449 for ivar = 1:length(myvars)
450 disp(sprintf(' Copying variable: %s',myvars(ivar).name))
451 for itile = 1:length(all_ncf)
452
453 if (myvars(ivar).has_horiz == 1) | (itile == 1)
454
455 clear nct;
456 nct = all_ncf(itile).nc{1};
457 ox_off = all_ncf(itile).exch2_tbasex;
458 oy_off = all_ncf(itile).exch2_tbasey;
459 diml_tin = '';
460 diml_res = '';
461 diml_in = '';
462 diml_out = '';
463 if length(myvars(ivar).dim_names) < 2
464 comm = sprintf( ...
465 'fonc{''%s''}(%s%d) = nct{''%s''}(:);', ...
466 myvars(ivar).name, '1:', myvars(ivar).dim_sizes{1}, ...
467 myvars(ivar).name );
468 % disp([ ' ' comm ]);
469 eval(comm);
470 else
471 for jj = 1:length(myvars(ivar).dim_names)
472 doff = 1;
473 if jj > 1
474 diml_tin = sprintf('%s,',diml_tin);
475 diml_res = sprintf('%s,',diml_res);
476 diml_in = sprintf('%s,',diml_in);
477 diml_out = sprintf('%s,',diml_out);
478 end
479 dnam = myvars(ivar).dim_names{jj};
480 dlen = myvars(ivar).dim_sizes{jj};
481 dlenr = dlen;
482 fchar = myvars(ivar).dim_names{jj}(1);
483 % disp([' fchar = ' fchar ' ' myvars(ivar).dim_names{jj}]);
484 if strcmp(dnam(1),'X') == 1
485 doff = ox_off + doff;
486 dlen = ox_off + dlen;
487 end
488 if strcmp(dnam(1),'Y') == 1
489 diml_res = sprintf('%s%s',diml_res, '[],');
490 diml_in = sprintf('%s%s',diml_in, ':,');
491 diml_out = sprintf('%s%d%s',diml_out,all_ncf(itile).exch2_myFace,',');
492 doff = oy_off + doff;
493 dlen = oy_off + dlen;
494 end
495 diml_tin = sprintf('%s%s',diml_tin, ':');
496 diml_res = sprintf('%s%d',diml_res, dlenr);
497 diml_in = sprintf('%s%s',diml_in, ':');
498 diml_out = sprintf('%s%d%s%d',diml_out,doff,':',dlen);
499 end
500
501 comm = sprintf( ...
502 'tmp = reshape(nct{''%s''}(%s), %s); fonc{''%s''}(%s) = tmp(%s);', ...
503 myvars(ivar).name, diml_tin, diml_res, myvars(ivar).name, ...
504 diml_out, diml_in );
505 % disp([ ' ' comm ]);
506 eval(comm);
507 end
508
509 end
510
511 end
512 end
513 % end
514
515 fonc = close(fonc);
516
517 end
518

  ViewVC Help
Powered by ViewVC 1.1.22