/[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.18 - (show annotations) (download)
Fri Dec 29 04:33:16 2006 UTC (17 years, 5 months ago) by jmc
Branch: MAIN
CVS Tags: checkpoint58t_post, checkpoint58u_post
Changes since 1.17: +56 -12 lines
add a 3rd output (1 long string), which is the content of meta files

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

  ViewVC Help
Powered by ViewVC 1.1.22