/[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.19 - (show annotations) (download)
Mon Jan 22 19:00:31 2007 UTC (17 years, 4 months ago) by molod
Branch: MAIN
Changes since 1.18: +5 -3 lines
Remove reference to strtrim - it croaks with old matlab version,
                              the older syntax works fine with both versions

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.18 2006/12/29 04:33:16 jmc 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 if isempty(findstr(M2,Mj)), M2=[M2(1:end-1) Mj]; end
182 end
183 end
184 % save modifications:
185 if ind1>0 & j==size(allfiles,1) & iter==size(iters,2), MM=[MM(1:ind1-1),M2,M3]; end
186 end
187
188 %- put local data file content in global array AA:
189 bdims=N(1,:);
190 r0=N(2,:);
191 rN=N(3,:);
192 ndims=prod(size(bdims));
193 if (ndims == 1)
194 AA(r0(1):rN(1),iter)=A;
195 elseif (ndims == 2)
196 AA(r0(1):rN(1),r0(2):rN(2),iter)=A;
197 elseif (ndims == 3)
198 AA(r0(1):rN(1),r0(2):rN(2),r0(3):rN(3),iter)=A;
199 elseif (ndims == 4)
200 AA(r0(1):rN(1),r0(2):rN(2),r0(3):rN(3),r0(4):rN(4),iter)=A;
201 elseif (ndims == 5)
202 AA(r0(1):rN(1),r0(2):rN(2),r0(3):rN(3),r0(4):rN(4),r0(5):rN(5),iter)=A;
203 else
204 error('Dimension of data set is larger than currently coded. Sorry!')
205 end
206
207 end % files
208 end % iterations
209
210 %-------------------------------------------------------------------------------
211
212 function [A,N,M] = localrdmds(fname,ieee,recnum)
213
214 mname=strrep(fname,' ','');
215 dname=strrep(mname,'.meta','.data');
216
217 % Read and interpret Meta file
218 fid = fopen(mname,'r');
219 if (fid == -1)
220 error(['File' mname ' could not be opened'])
221 end
222
223 % Scan each line of the Meta file
224 allstr=' ';
225 keepgoing = 1;
226 while keepgoing > 0,
227 line = fgetl(fid);
228 if (line == -1)
229 keepgoing=-1;
230 else
231 % Strip out "(PID.TID *.*)" by finding first ")"
232 %old ind=findstr([line ')'],')'); line=line(ind(1)+1:end);
233 ind=findstr(line,')');
234 if size(ind) ~= 0
235 line=line(ind(1)+1:end);
236 end
237 % Remove comments of form //
238 line=[line ' //']; ind=findstr(line,'//'); line=line(1:ind(1)-1);
239 % Add to total string
240 allstr=[allstr line];
241 % allstr=[allstr,strtrim(line),' ']
242 end
243 end
244
245 % Close meta file
246 fclose(fid);
247
248 % Strip out comments of form /* ... */
249 ind1=findstr(allstr,'/*'); ind2=findstr(allstr,'*/');
250 if size(ind1) ~= size(ind2)
251 error('The /* ... */ comments are not properly paired')
252 end
253 while size(ind1,2) > 0
254 allstr=[allstr(1:ind1(1)-1) allstr(ind2(1)+3:end)];
255 ind1=findstr(allstr,'/*'); ind2=findstr(allstr,'*/');
256 end
257
258 % This is a kludge to catch whether the meta-file is of the
259 % old or new type. nrecords does not exist in the old type.
260 nrecords = NaN;
261
262 %- store the full string for output:
263 M=strrep(allstr,'format','dataprec');
264
265 % Everything in lower case
266 allstr=lower(allstr);
267
268 % Fix the unfortunate choice of 'format'
269 allstr=strrep(allstr,'format','dataprec');
270
271 % Evaluate meta information
272 eval(allstr);
273
274 N=reshape( dimlist , 3 , prod(size(dimlist))/3 );
275 rep=[' dimList = [ ',sprintf('%i ',N(1,:)),']'];
276 if ~isnan(nrecords) & nrecords > 1 & isempty(recnum)
277 N=[N,[nrecords 1 nrecords]'];
278 elseif ~isempty(recnum) & recnum>nrecords
279 error('Requested record number is higher than the number of available records')
280 end
281
282 %- make "dimList" shorter (& fit output array size) in output "M":
283 pat=' dimList = \[(\s*\d+\,?)*\s*\]';
284 M=regexprep(M,pat,rep);
285 % and remove double space within sq.brakets:
286 ind1=findstr(M,'['); ind2=findstr(M,']');
287 if length(ind1) == length(ind2),
288 for i=length(ind1):-1:1, if ind1(i) < ind2(i),
289 M=[M(1:ind1(i)),regexprep(M(ind1(i)+1:ind2(i)-1),'(\s+)',' '),M(ind2(i):end)];
290 end; end
291 else error('The [ ... ] brakets are not properly paired')
292 end
293
294 if isempty(recnum)
295 recnum=1;
296 end
297
298 if isnan(nrecords)
299 % This is the old 'meta' method that used sequential access
300
301 A=allstr;
302 % Open data file
303 fid=fopen(dname,'r',ieee);
304
305 % Read record size in bytes
306 recsz=fread(fid,1,'uint32');
307 ldims=N(3,:)-N(2,:)+1;
308 numels=prod(ldims);
309
310 rat=recsz/numels;
311 if rat == 4
312 A=fread(fid,numels,'real*4');
313 elseif rat == 8
314 A=fread(fid,numels,'real*8');
315 else
316 sprintf(' Implied size in meta-file = %d', numels )
317 sprintf(' Record size in data-file = %d', recsz )
318 error('Ratio between record size and size in meta-file inconsistent')
319 end
320
321 erecsz=fread(fid,1,'uint32');
322 if erecsz ~= recsz
323 sprintf('WARNING: Record sizes at beginning and end of file are inconsistent')
324 end
325
326 fclose(fid);
327
328 A=reshape(A,ldims);
329
330 else
331 % This is the new MDS format that uses direct access
332
333 ldims=N(3,:)-N(2,:)+1;
334 for r=1:size(recnum(:),1);
335 if dataprec == 'float32'
336 A(:,r)=myrdda(dname,ldims,recnum(r),'real*4',ieee);
337 elseif dataprec == 'float64'
338 A(:,r)=myrdda(dname,ldims,recnum(r),'real*8',ieee);
339 else
340 error(['Unrecognized format in meta-file = ' format]);
341 end
342 end
343
344 A=reshape(A,[ldims size(recnum(:),1)]);
345 if size(recnum(:),1)>1
346 N(1,end+1)=size(recnum(:),1);
347 N(2,end)=1;
348 N(3,end)=size(recnum(:),1);
349 end
350
351 end
352
353 %-------------------------------------------------------------------------------
354
355 % result = RDDA( file, dim, irec [options] )
356 %
357 % This routine reads the irec'th record of shape 'dim' from the
358 % direct-access binary file (float or double precision) 'file'.
359 %
360 % Required arguments:
361 %
362 % file - string - name of file to read from
363 % dim - vector - dimensions of the file records and the resulting array
364 % irec - integer - record number in file in which to write data
365 %
366 % Optional arguments (must appear after the required arguments):
367 % prec - string - precision of storage in file. Default = 'real*8'.
368 % ieee - string - IEEE bit-wise representation in file. Default = 'b'.
369 %
370 % 'prec' may take the values:
371 % 'real*4' - floating point, 32 bits.
372 % 'real*8' - floating point, 64 bits - the efault.
373 %
374 % 'ieee' may take values:
375 % 'ieee-be' or 'b' - IEEE floating point with big-endian
376 % byte ordering - the default
377 % 'ieee-le' or 'l' - IEEE floating point with little-endian
378 % byte ordering
379 % 'native' or 'n' - local machine format
380 % 'cray' or 'c' - Cray floating point with big-endian
381 % byte ordering
382 % 'ieee-le.l64' or 'a' - IEEE floating point with little-endian
383 % byte ordering and 64 bit long data type
384 % 'ieee-be.l64' or 's' - IEEE floating point with big-endian byte
385 % ordering and 64 bit long data type.
386 %
387 % eg. T=rdda('t.data',[64 64 32],1);
388 % T=rdda('t.data',[256],4,'real*4');
389 % T=rdda('t.data',[128 64],2,'real*4','b');
390 function [arr] = myrdda(file,N,k,varargin)
391
392 % Defaults
393 WORDLENGTH=8;
394 rtype='real*8';
395 ieee='b';
396
397 % Check optional arguments
398 args=char(varargin);
399 while (size(args,1) > 0)
400 if deblank(args(1,:)) == 'real*4'
401 WORDLENGTH=4;
402 rtype='real*4';
403 elseif deblank(args(1,:)) == 'real*8'
404 WORDLENGTH=8;
405 rtype='real*8';
406 elseif deblank(args(1,:)) == 'n' | deblank(args(1,:)) == 'native'
407 ieee='n';
408 elseif deblank(args(1,:)) == 'l' | deblank(args(1,:)) == 'ieee-le'
409 ieee='l';
410 elseif deblank(args(1,:)) == 'b' | deblank(args(1,:)) == 'ieee-be'
411 ieee='b';
412 elseif deblank(args(1,:)) == 'c' | deblank(args(1,:)) == 'cray'
413 ieee='c';
414 elseif deblank(args(1,:)) == 'a' | deblank(args(1,:)) == 'ieee-le.l64'
415 ieee='a';
416 elseif deblank(args(1,:)) == 's' | deblank(args(1,:)) == 'ieee-be.l64'
417 ieee='s';
418 else
419 error(['Optional argument ' args(1,:) ' is unknown'])
420 end
421 args=args(2:end,:);
422 end
423
424 nnn=prod(N);
425
426 [fid mess]=fopen(file,'r',ieee);
427 if fid == -1
428 error('Error while opening file:\n%s',mess)
429 end
430 st=fseek(fid,nnn*(k-1)*WORDLENGTH,'bof');
431 if st ~= 0
432 mess=ferror(fid);
433 error('There was an error while positioning the file pointer:\n%s',mess)
434 end
435 [arr count]=fread(fid,nnn,rtype);
436 if count ~= nnn
437 error('Not enough data was available to be read: off EOF?')
438 end
439 st=fclose(fid);
440 %arr=reshape(arr,N);
441
442 %
443 function [iters] = scanforfiles(fname)
444
445 iters=[];
446 allfiles=dir([fname '.*.001.001.meta']);
447 if isempty(allfiles)
448 allfiles=dir([fname '.*.meta']);
449 ioff=0;
450 else
451 ioff=8;
452 end
453 for k=1:size(allfiles,1);
454 hh=allfiles(k).name;
455 iters(k)=str2num( hh(end-14-ioff:end-5-ioff) );
456 end
457 iters=sort(iters);

  ViewVC Help
Powered by ViewVC 1.1.22