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

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

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


Revision 1.22 - (show annotations) (download)
Tue Mar 20 22:23:19 2007 UTC (17 years, 2 months ago) by jmc
Branch: MAIN
Changes since 1.21: +44 -16 lines
allow rdmds to read new compact-format files (to follow recent mdsio changes)

1 function [AA,iters,MM] = rdmds(fnamearg,varargin)
2 % RDMDS Read MITgcmUV meta/data files
3 %
4 % A = RDMDS(FNAME)
5 % A = RDMDS(FNAME,ITER)
6 % A = RDMDS(FNAME,[ITER1 ITER2 ...])
7 % A = RDMDS(FNAME,NaN)
8 % A = RDMDS(FNAME,Inf)
9 % [A,ITS,M] = RDMDS(FNAME,[...])
10 % A = RDMDS(FNAME,[...],'rec',RECNUM)
11 %
12 % A = RDMDS(FNAME) reads data described by meta/data file format.
13 % FNAME is a string containing the "head" of the file names.
14 %
15 % eg. To load the meta-data files
16 % T.0000002880.000.000.meta, T.0000002880.000.000.data
17 % T.0000002880.001.000.meta, T.0000002880.001.000.data
18 % T.0000002880.002.000.meta, T.0000002880.002.000.data
19 % T.0000002880.003.000.meta, T.0000002880.003.000.data
20 % use
21 % >> A=rdmds('T.0000002880');
22 % >> size(A)
23 % ans =
24 % 64 32 5
25 % eg. To load a multiple record file
26 % >> A=rdmds('pickup.0000002880');
27 % >> size(A)
28 % ans =
29 % 64 32 5 61
30 %
31 %
32 % A = RDMDS(FNAME,ITER) reads data described by meta/data file format.
33 % FNAME is a string containing the "head" of the file name excluding the
34 % 10-digit iterartion number.
35 % ITER is a vector of positive integers that will expand to the 10-digit
36 % number in the file name.
37 % If ITER=NaN, all iterations will be read.
38 % If ITER=Inf, the last (highest) iteration will be read.
39 %
40 % eg. To repeat above operation
41 % >> A=rdmds('T',2880);
42 % eg. To read multiple time steps
43 % >> A=rdmds('T',[0 1440 2880]);
44 % eg. To read all time steps
45 % >> [A,ITS]=rdmds('T',NaN);
46 % eg. To read the last time step
47 % >> [A,IT]=rdmds('T',Inf);
48 % Note: this form can not read files with no iteration count in file name.
49 %
50 %
51 % A = RDMDS(FNAME,[...],'rec',RECNUM) reads individual records from
52 % multiple record files.
53 %
54 % eg. To read a single record from a multi-record file
55 % >> [A,IT]=rdmds('pickup.ckptA',11);
56 % eg. To read several records from a multi-record file
57 % >> [A,IT]=rdmds('pickup',Inf,'rec',[1:5 8 12]);
58 %
59 %
60 % A = RDMDS(FNAME,ITER,MACHINEFORMAT) allows the machine format to be
61 % A = RDMDS(FNAME,MACHINEFORMAT)
62 % specified which MACHINEFORMAT is on of the following strings:
63 % 'n' 'l' 'b' 'd' 'g' 'c' 'a' 's' - see FOPEN for more details
64 %
65
66 % $Header: /u/gcmpack/MITgcm/utils/matlab/rdmds.m,v 1.21 2007/02/17 23:49:43 jmc Exp $
67 % $Name: $
68
69 AA=[];
70 iters=[];
71 MM=[];
72
73 % Default options
74 ieee='b';
75 fname=fnamearg;
76 userecords=0;
77 recnum=[];
78
79 % Check optional arguments
80 for ind=1:size(varargin,2);
81 arg=varargin{ind};
82 if ischar(arg)
83 if strcmp(arg,'n') | strcmp(arg,'native')
84 ieee='n';
85 elseif strcmp(arg,'l') | strcmp(arg,'ieee-le')
86 ieee='l';
87 elseif strcmp(arg,'b') | strcmp(arg,'ieee-be')
88 ieee='b';
89 elseif strcmp(arg,'c') | strcmp(arg,'cray')
90 ieee='c';
91 elseif strcmp(arg,'a') | strcmp(arg,'ieee-le.l64')
92 ieee='a';
93 elseif strcmp(arg,'s') | strcmp(arg,'ieee-be.l64')
94 ieee='s';
95 elseif strcmp(arg,'rec')
96 userecords=1;
97 else
98 error(['Optional argument ' arg ' is unknown'])
99 end
100 else
101 if userecords==1
102 recnum=arg;
103 elseif isempty(iters)
104 if isnan(arg)
105 iters=scanforfiles(fname);
106 disp([ sprintf('Reading %i time levels:',size(iters,2)) sprintf(' %i',iters) ]);
107 elseif isinf(arg)
108 iters=scanforfiles(fname);
109 if isempty(iters)
110 AA=[];iters=[];return;
111 end
112 disp([ sprintf('Found %i time levels, reading %i',size(iters,2),iters(end)) ]);
113 iters=iters(end);
114 % elseif prod(double(arg>=0)) & prod(double(round(arg)==arg))
115 % elseif prod(arg>=0) & prod(round(arg)==arg)
116 elseif min(arg)>=0 & isempty(find(round(arg)~=arg))
117 if arg>=9999999999
118 error(sprintf('Argument %i > 9999999999',arg))
119 end
120 iters=arg;
121 else
122 error(sprintf('Argument %i must be a positive integer',arg))
123 end
124 else
125 error('Multiple iterations should be specified as a vector')
126 end
127 end
128 end
129
130 if isempty(iters)
131 iters=-1;
132 end
133
134 % Loop over each iteration
135 for iter=1:size(iters,2);
136 if iters(iter)>=0
137 fname=sprintf('%s.%10.10i',fnamearg,iters(iter));
138 end
139
140 % Figure out if there is a path in the filename
141 NS=findstr('/',fname);
142 if size(NS)>0
143 Dir=fname(1:NS(end));
144 else
145 Dir='./';
146 end
147
148 % Match name of all meta-files
149 allfiles=dir( sprintf('%s*.meta',fname) );
150
151 if size(allfiles,1)==0
152 disp(sprintf('No files match the search: %s*.meta',fname));
153 %allow partial reads% error('No files found.')
154 end
155
156 % Loop through allfiles
157 for j=1:size(allfiles,1);
158
159 % Read meta- and data-file
160 [A,N,M,mG] = localrdmds([Dir allfiles(j).name],ieee,recnum);
161
162 %- Merge local Meta file content (M) to MM string:
163 if j > 0, %- to comment out this block: "if j < 0" (since j is always > 0)
164 ind=findstr(M,' timeStepNumber');
165 if isempty(ind), ind=0;
166 else ind2=ind+min(findstr(M(1+ind:end),'];')); end
167 if iter==1 & j==1,
168 MM=M; ind1=ind; if ind1 > 0, M2=M(ind1:ind2); M3=M(1+ind2:end); end
169 else
170 compar=(ind == ind1);
171 if compar & ind == 0, compar=strcmp(MM,M); end
172 if compar & ind1 > 0, compar=strncmp(MM,M,ind1) ; end
173 if compar & ind1 > 0, compar=strcmp(M3,M(1+ind2:end)); end
174 if ~compar,
175 fprintf('WARNING: Meta file (%s) is different from 1rst one:\n',allfiles(j).name);
176 fprintf(' it=%i :MM:%s\n',iters(1),MM);
177 fprintf(' it=%i :M :%s\n\n',iters(iter),M);
178 elseif ind1 > 0,
179 Mj=M(ind1:ind2); ind=findstr(Mj,'['); Mj=Mj(1+ind:end);
180 % add it-number from Mj to M2 (if different):
181 if isempty(findstr(M2,Mj)), M2=[deblank(M2(1:end-1)),Mj]; end
182 % if isempty(findstr(M2,Mj)), M2=[M2(1:end-1) Mj]; end
183 end
184 end
185 % save modifications:
186 if ind1>0 & j==size(allfiles,1) & iter==size(iters,2), MM=[MM(1:ind1-1),M2,M3]; end
187 end
188
189 %- put local data file content in global array AA:
190 bdims=N(1,:);
191 r0=N(2,:);
192 rN=N(3,:);
193 ndims=prod(size(bdims));
194 if j==1 & iter==1, AA=zeros([bdims size(iters,2)]); end
195 if mG(1)==0 & mG(2)==1,
196 if (ndims == 1)
197 AA(r0(1):rN(1),iter)=A;
198 elseif (ndims == 2)
199 AA(r0(1):rN(1),r0(2):rN(2),iter)=A;
200 elseif (ndims == 3)
201 AA(r0(1):rN(1),r0(2):rN(2),r0(3):rN(3),iter)=A;
202 elseif (ndims == 4)
203 AA(r0(1):rN(1),r0(2):rN(2),r0(3):rN(3),r0(4):rN(4),iter)=A;
204 elseif (ndims == 5)
205 AA(r0(1):rN(1),r0(2):rN(2),r0(3):rN(3),r0(4):rN(4),r0(5):rN(5),iter)=A;
206 else
207 error('Dimension of data set is larger than currently coded. Sorry!')
208 end
209 elseif (ndims == 1)
210 AA(r0(1):rN(1),iter)=A;
211 else
212 %- to debug: do simple stransfert (with a loop on 2nd index);
213 % will need to change this later, to improve efficiency:
214 for i=0:rN(2)-r0(2),
215 if (ndims == 2)
216 AA(r0(1)+i*mG(1):rN(1)+i*mG(1),r0(2)+i*mG(2),iter)=A(:,1+i);
217 elseif (ndims == 3)
218 AA(r0(1)+i*mG(1):rN(1)+i*mG(1),r0(2)+i*mG(2), ...
219 r0(3):rN(3),iter)=A(:,1+i,:);
220 elseif (ndims == 4)
221 AA(r0(1)+i*mG(1):rN(1)+i*mG(1),r0(2)+i*mG(2), ...
222 r0(3):rN(3),r0(4):rN(4),iter)=A(:,1+i,:,:);
223 elseif (ndims == 5)
224 AA(r0(1)+i*mG(1):rN(1)+i*mG(1),r0(2)+i*mG(2), ...
225 r0(3):rN(3),r0(4):rN(4),r0(5):rN(5),iter)=A(:,1+i,:,:,:);
226 else
227 error('Dimension of data set is larger than currently coded. Sorry!')
228 end
229 end
230 end
231
232 end % files
233 end % iterations
234
235 %-------------------------------------------------------------------------------
236
237 function [A,N,M,map2glob] = localrdmds(fname,ieee,recnum)
238
239 mname=strrep(fname,' ','');
240 dname=strrep(mname,'.meta','.data');
241
242 %- set default mapping from tile to global file:
243 map2glob=[0 1];
244
245 % Read and interpret Meta file
246 fid = fopen(mname,'r');
247 if (fid == -1)
248 error(['File' mname ' could not be opened'])
249 end
250
251 % Scan each line of the Meta file
252 allstr=' ';
253 keepgoing = 1;
254 while keepgoing > 0,
255 line = fgetl(fid);
256 if (line == -1)
257 keepgoing=-1;
258 else
259 % Strip out "(PID.TID *.*)" by finding first ")"
260 %old ind=findstr([line ')'],')'); line=line(ind(1)+1:end);
261 ind=findstr(line,')');
262 if size(ind) ~= 0
263 line=line(ind(1)+1:end);
264 end
265 % Remove comments of form //
266 line=[line,' //']; ind=findstr(line,'//'); line=line(1:ind(1)-1);
267 % Add to total string (without starting & ending blanks)
268 while line(1:1) == ' ', line=line(2:end); end
269 if strncmp(line,'map2glob',8), eval(line);
270 else allstr=[allstr,deblank(line),' '];
271 end
272 end
273 end
274
275 % Close meta file
276 fclose(fid);
277
278 % Strip out comments of form /* ... */
279 ind1=findstr(allstr,'/*'); ind2=findstr(allstr,'*/');
280 if size(ind1) ~= size(ind2)
281 error('The /* ... */ comments are not properly paired')
282 end
283 while size(ind1,2) > 0
284 allstr=[deblank(allstr(1:ind1(1)-1)) allstr(ind2(1)+2:end)];
285 %allstr=[allstr(1:ind1(1)-1) allstr(ind2(1)+3:end)];
286 ind1=findstr(allstr,'/*'); ind2=findstr(allstr,'*/');
287 end
288
289 % This is a kludge to catch whether the meta-file is of the
290 % old or new type. nrecords does not exist in the old type.
291 nrecords = NaN;
292
293 %- store the full string for output:
294 M=strrep(allstr,'format','dataprec');
295
296 % Everything in lower case
297 allstr=lower(allstr);
298
299 % Fix the unfortunate choice of 'format'
300 allstr=strrep(allstr,'format','dataprec');
301
302 % Evaluate meta information
303 eval(allstr);
304
305 N=reshape( dimlist , 3 , prod(size(dimlist))/3 );
306 rep=[' dimList = [ ',sprintf('%i ',N(1,:)),']'];
307 if ~isnan(nrecords) & nrecords > 1 & isempty(recnum)
308 N=[N,[nrecords 1 nrecords]'];
309 elseif ~isempty(recnum) & recnum>nrecords
310 error('Requested record number is higher than the number of available records')
311 end
312
313 %- make "dimList" shorter (& fit output array size) in output "M":
314 pat=' dimList = \[(\s*\d+\,?)*\s*\]';
315 M=regexprep(M,pat,rep);
316 % and remove double space within sq.brakets:
317 ind1=findstr(M,'['); ind2=findstr(M,']');
318 if length(ind1) == length(ind2),
319 for i=length(ind1):-1:1, if ind1(i) < ind2(i),
320 M=[M(1:ind1(i)),regexprep(M(ind1(i)+1:ind2(i)-1),'(\s+)',' '),M(ind2(i):end)];
321 end; end
322 else error('The [ ... ] brakets are not properly paired')
323 end
324
325 if isempty(recnum)
326 recnum=1;
327 end
328
329 if isnan(nrecords)
330 % This is the old 'meta' method that used sequential access
331
332 A=allstr;
333 % Open data file
334 fid=fopen(dname,'r',ieee);
335
336 % Read record size in bytes
337 recsz=fread(fid,1,'uint32');
338 ldims=N(3,:)-N(2,:)+1;
339 numels=prod(ldims);
340
341 rat=recsz/numels;
342 if rat == 4
343 A=fread(fid,numels,'real*4');
344 elseif rat == 8
345 A=fread(fid,numels,'real*8');
346 else
347 sprintf(' Implied size in meta-file = %d', numels )
348 sprintf(' Record size in data-file = %d', recsz )
349 error('Ratio between record size and size in meta-file inconsistent')
350 end
351
352 erecsz=fread(fid,1,'uint32');
353 if erecsz ~= recsz
354 sprintf('WARNING: Record sizes at beginning and end of file are inconsistent')
355 end
356
357 fclose(fid);
358
359 A=reshape(A,ldims);
360
361 else
362 % This is the new MDS format that uses direct access
363
364 ldims=N(3,:)-N(2,:)+1;
365 for r=1:size(recnum(:),1);
366 if dataprec == 'float32'
367 A(:,r)=myrdda(dname,ldims,recnum(r),'real*4',ieee);
368 elseif dataprec == 'float64'
369 A(:,r)=myrdda(dname,ldims,recnum(r),'real*8',ieee);
370 else
371 error(['Unrecognized format in meta-file = ' format]);
372 end
373 end
374
375 A=reshape(A,[ldims size(recnum(:),1)]);
376 if size(recnum(:),1)>1
377 N(1,end+1)=size(recnum(:),1);
378 N(2,end)=1;
379 N(3,end)=size(recnum(:),1);
380 end
381
382 end
383
384 %-------------------------------------------------------------------------------
385
386 % result = RDDA( file, dim, irec [options] )
387 %
388 % This routine reads the irec'th record of shape 'dim' from the
389 % direct-access binary file (float or double precision) 'file'.
390 %
391 % Required arguments:
392 %
393 % file - string - name of file to read from
394 % dim - vector - dimensions of the file records and the resulting array
395 % irec - integer - record number in file in which to write data
396 %
397 % Optional arguments (must appear after the required arguments):
398 % prec - string - precision of storage in file. Default = 'real*8'.
399 % ieee - string - IEEE bit-wise representation in file. Default = 'b'.
400 %
401 % 'prec' may take the values:
402 % 'real*4' - floating point, 32 bits.
403 % 'real*8' - floating point, 64 bits - the efault.
404 %
405 % 'ieee' may take values:
406 % 'ieee-be' or 'b' - IEEE floating point with big-endian
407 % byte ordering - the default
408 % 'ieee-le' or 'l' - IEEE floating point with little-endian
409 % byte ordering
410 % 'native' or 'n' - local machine format
411 % 'cray' or 'c' - Cray floating point with big-endian
412 % byte ordering
413 % 'ieee-le.l64' or 'a' - IEEE floating point with little-endian
414 % byte ordering and 64 bit long data type
415 % 'ieee-be.l64' or 's' - IEEE floating point with big-endian byte
416 % ordering and 64 bit long data type.
417 %
418 % eg. T=rdda('t.data',[64 64 32],1);
419 % T=rdda('t.data',[256],4,'real*4');
420 % T=rdda('t.data',[128 64],2,'real*4','b');
421 function [arr] = myrdda(file,N,k,varargin)
422
423 % Defaults
424 WORDLENGTH=8;
425 rtype='real*8';
426 ieee='b';
427
428 % Check optional arguments
429 args=char(varargin);
430 while (size(args,1) > 0)
431 if deblank(args(1,:)) == 'real*4'
432 WORDLENGTH=4;
433 rtype='real*4';
434 elseif deblank(args(1,:)) == 'real*8'
435 WORDLENGTH=8;
436 rtype='real*8';
437 elseif deblank(args(1,:)) == 'n' | deblank(args(1,:)) == 'native'
438 ieee='n';
439 elseif deblank(args(1,:)) == 'l' | deblank(args(1,:)) == 'ieee-le'
440 ieee='l';
441 elseif deblank(args(1,:)) == 'b' | deblank(args(1,:)) == 'ieee-be'
442 ieee='b';
443 elseif deblank(args(1,:)) == 'c' | deblank(args(1,:)) == 'cray'
444 ieee='c';
445 elseif deblank(args(1,:)) == 'a' | deblank(args(1,:)) == 'ieee-le.l64'
446 ieee='a';
447 elseif deblank(args(1,:)) == 's' | deblank(args(1,:)) == 'ieee-be.l64'
448 ieee='s';
449 else
450 error(['Optional argument ' args(1,:) ' is unknown'])
451 end
452 args=args(2:end,:);
453 end
454
455 nnn=prod(N);
456
457 [fid mess]=fopen(file,'r',ieee);
458 if fid == -1
459 error('Error while opening file:\n%s',mess)
460 end
461 st=fseek(fid,nnn*(k-1)*WORDLENGTH,'bof');
462 if st ~= 0
463 mess=ferror(fid);
464 error('There was an error while positioning the file pointer:\n%s',mess)
465 end
466 [arr count]=fread(fid,nnn,rtype);
467 if count ~= nnn
468 error('Not enough data was available to be read: off EOF?')
469 end
470 st=fclose(fid);
471 %arr=reshape(arr,N);
472
473 %
474 function [iters] = scanforfiles(fname)
475
476 iters=[];
477 allfiles=dir([fname '.*.001.001.meta']);
478 if isempty(allfiles)
479 allfiles=dir([fname '.*.meta']);
480 ioff=0;
481 else
482 ioff=8;
483 end
484 for k=1:size(allfiles,1);
485 hh=allfiles(k).name;
486 iters(k)=str2num( hh(end-14-ioff:end-5-ioff) );
487 end
488 iters=sort(iters);

  ViewVC Help
Powered by ViewVC 1.1.22