/[MITgcm]/MITgcm_contrib/gael/matlab_class/gcmfaces_IO/rdmds.m
ViewVC logotype

Annotation of /MITgcm_contrib/gael/matlab_class/gcmfaces_IO/rdmds.m

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


Revision 1.2 - (hide annotations) (download)
Thu Sep 11 19:35:35 2014 UTC (10 years, 10 months ago) by gforget
Branch: MAIN
Changes since 1.1: +15 -11 lines
- update according to MITgcm/utils/matlab rev. 1.27

1 gforget 1.1 function [AA,itrs,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 gforget 1.2 % $Header: /u/gcmpack/MITgcm_contrib/gael/matlab_class/gcmfaces_IO/rdmds.m,v 1.1 2010/02/10 14:46:49 gforget Exp $
67 gforget 1.1 % $Name: $
68    
69     AA=[];
70     itrs=[];
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(itrs)
104     if isnan(arg)
105     itrs=scanforfiles(fname);
106     disp([ sprintf('Reading %i time levels:',size(itrs,2)) sprintf(' %i',itrs) ]);
107     elseif isinf(arg)
108     itrs=scanforfiles(fname);
109     if isempty(itrs)
110     AA=[];itrs=[];return;
111     end
112     disp([ sprintf('Found %i time levels, reading %i',size(itrs,2),itrs(end)) ]);
113     itrs=itrs(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     itrs=arg;
121 gforget 1.2 elseif length(arg) == 1 & arg == -1
122     itrs=arg;
123 gforget 1.1 else
124     error(sprintf('Argument %i must be a positive integer',arg))
125     end
126     else
127     error('Multiple iterations should be specified as a vector')
128     end
129     end
130     end
131    
132     if isempty(itrs)
133     itrs=-1;
134     end
135    
136     % Loop over each iteration
137     for iter=1:size(itrs,2);
138     if itrs(iter)>=0
139     fname=sprintf('%s.%10.10i',fnamearg,itrs(iter));
140     end
141    
142     % Figure out if there is a path in the filename
143     NS=findstr('/',fname);
144     if size(NS)>0
145     Dir=fname(1:NS(end));
146     else
147     Dir='./';
148     end
149    
150     % Match name of all meta-files
151 gforget 1.2 %fprintf(' search for file "%s".*meta\n',fname);
152     allfiles=dir( sprintf('%s.*meta',fname) );
153 gforget 1.1
154     if size(allfiles,1)==0
155 gforget 1.2 disp(sprintf('No files match the search: %s.*meta',fname));
156 gforget 1.1 %allow partial reads% error('No files found.')
157     end
158    
159     % Loop through allfiles
160     for j=1:size(allfiles,1);
161 gforget 1.2 %fprintf(' file # %3i : %s\n',j,allfiles(j).name);
162 gforget 1.1
163     % Read meta- and data-file
164     [A,N,M,mG] = localrdmds([Dir allfiles(j).name],ieee,recnum);
165    
166     %- Merge local Meta file content (M) to MM string:
167     if j > 0, %- to comment out this block: "if j < 0" (since j is always > 0)
168     ii=findstr(M,' timeStepNumber');
169     if isempty(ii), ii1=0; ii2=0;
170     else ii1=ii; ii2=ii+min(findstr(M(1+ii:end),'];')); end
171     ii=findstr(M,' timeInterval');
172     if isempty(ii), jj1=0; jj2=0;
173     else jj1=ii; jj2=ii+min(findstr(M(1+ii:end),'];')); end
174     if iter==1 & j==1,
175 gforget 1.2 MM=M; ind1=0; ind2=0; is1=ii1; js1=jj1; M3='';
176     if ii1*jj1 > 0,
177 gforget 1.1 %ind1=min(ii1,jj1); ind2=max(ii2,jj2);
178     %if ii1 < jj1, ii3=ii2+1; jj3=jj1-1;
179     %else ii3=jj2+1; jj3=ii1-1; end
180     order=sort([ii1 ii2 jj1 jj2]);
181     ind1=order(1); ii3=order(2)+1; jj3=order(3)-1; ind2=order(4);
182     M2=M(ii1:ii2); M4=M(jj1:jj2); M3=M(ii3:jj3);
183 gforget 1.2 elseif ii1 > 0,
184 gforget 1.1 ind1=ii1; ind2=ii2;
185     M2=M(ii1:ii2); M4='';
186 gforget 1.2 elseif jj1 > 0,
187 gforget 1.1 ind1=jj1; ind2=jj2;
188     M4=M(jj1:jj2); M2='';
189     end
190     M5=M(1+ind2:end);
191     %fprintf(' ii1,ii2 = %i %i ; jj1,jj2= %i %i ;', ii1,ii2, jj1,jj2);
192     %fprintf(' ii3,jj3= %i %i ; ind1,ind2= %i %i\n',ii3,jj3,ind1,ind2);
193     %fprintf('M(1:ind1)=%s<\n',M(1:ind1));
194     %fprintf(' M2=%s<\n',M2);
195     %fprintf(' M3=%s<\n',M3);
196     %fprintf(' M4=%s<\n',M4);
197     %fprintf(' M5=%s<\n',M5);
198     else
199 gforget 1.2 if ii1*jj1 > 0,
200 gforget 1.1 order=sort([ii1 ii2 jj1 jj2]);
201     ind=order(1); ii3=order(2)+1; jj3=order(3)-1; ind2=order(4);
202     else ind=max(ii1,jj1); ind2=ii2+jj2; end
203     compar=(ind == ind1); ii=0;
204     if compar & ind1 == 0, ii=1; compar=strcmp (MM,M); end
205     if compar & ind1 > 0, ii=2; compar=strncmp(MM,M,ind1) ; end
206     if compar & ind1 > 0, ii=3; compar=strcmp(M5,M(1+ind2:end)); end
207     if compar & ii1*jj1 > 0, ii=4; compar=strcmp(M3,M(ii3:jj3)); end
208     if ~compar,
209     fprintf('WARNING: Meta file (%s) is different (%i) from 1rst one:\n', ...
210     allfiles(j).name,ii);
211     fprintf(' it=%i :MM:%s\n',itrs(1),MM);
212     fprintf(' it=%i :M :%s\n\n',itrs(iter),M);
213     elseif ind1 > 0,
214     if ii1 > 0,
215     Mj=M(ii1:ii2); ii=findstr(Mj,'['); Mj=Mj(1+ii:end);
216     % add it-number from Mj to M2 (if different):
217     if isempty(findstr(M2,Mj)), M2=[deblank(M2(1:end-1)),Mj]; end
218     end
219     if jj1 > 0,
220     Mj=M(jj1:jj2); ii=findstr(Mj,'['); Mj=Mj(1+ii:end);
221     % add time interval from Mj to M4 (if different):
222     if isempty(findstr(M4,Mj)), M4=[deblank(M4(1:end-1)),' ;',Mj]; end
223     end
224     end
225     end
226     % save modifications:
227 gforget 1.2 if ind1>0 & j==size(allfiles,1) & iter==size(itrs,2),
228     if ii1 < jj1, MM=[MM(1:ind1-1),M2,M3,M4,M5];
229     else MM=[MM(1:ind1-1),M4,M3,M2,M5]; end
230 gforget 1.1 end
231     end
232    
233     %- put local data file content in global array AA:
234     bdims=N(1,:);
235     r0=N(2,:);
236     rN=N(3,:);
237     ndims=prod(size(bdims));
238     if j==1 & iter==1, AA=zeros([bdims size(itrs,2)]); end
239     if mG(1)==0 & mG(2)==1,
240     if (ndims == 1)
241     AA(r0(1):rN(1),iter)=A;
242     elseif (ndims == 2)
243     AA(r0(1):rN(1),r0(2):rN(2),iter)=A;
244     elseif (ndims == 3)
245     AA(r0(1):rN(1),r0(2):rN(2),r0(3):rN(3),iter)=A;
246     elseif (ndims == 4)
247     AA(r0(1):rN(1),r0(2):rN(2),r0(3):rN(3),r0(4):rN(4),iter)=A;
248     elseif (ndims == 5)
249     AA(r0(1):rN(1),r0(2):rN(2),r0(3):rN(3),r0(4):rN(4),r0(5):rN(5),iter)=A;
250     else
251     error('Dimension of data set is larger than currently coded. Sorry!')
252     end
253     elseif (ndims == 1)
254     AA(r0(1):rN(1),iter)=A;
255     else
256     %- to debug: do simple stransfert (with a loop on 2nd index);
257     % will need to change this later, to improve efficiency:
258     for i=0:rN(2)-r0(2),
259     if (ndims == 2)
260     AA(r0(1)+i*mG(1):rN(1)+i*mG(1),r0(2)+i*mG(2),iter)=A(:,1+i);
261     elseif (ndims == 3)
262     AA(r0(1)+i*mG(1):rN(1)+i*mG(1),r0(2)+i*mG(2), ...
263     r0(3):rN(3),iter)=A(:,1+i,:);
264     elseif (ndims == 4)
265     AA(r0(1)+i*mG(1):rN(1)+i*mG(1),r0(2)+i*mG(2), ...
266     r0(3):rN(3),r0(4):rN(4),iter)=A(:,1+i,:,:);
267     elseif (ndims == 5)
268     AA(r0(1)+i*mG(1):rN(1)+i*mG(1),r0(2)+i*mG(2), ...
269     r0(3):rN(3),r0(4):rN(4),r0(5):rN(5),iter)=A(:,1+i,:,:,:);
270     else
271     error('Dimension of data set is larger than currently coded. Sorry!')
272     end
273     end
274     end
275    
276     end % files
277     end % iterations
278    
279     %-------------------------------------------------------------------------------
280    
281     function [A,N,M,map2glob] = localrdmds(fname,ieee,recnum)
282    
283     mname=strrep(fname,' ','');
284     dname=strrep(mname,'.meta','.data');
285    
286     %- set default mapping from tile to global file:
287     map2glob=[0 1];
288    
289     % Read and interpret Meta file
290     fid = fopen(mname,'r');
291     if (fid == -1)
292     error(['File' mname ' could not be opened'])
293     end
294    
295     % Scan each line of the Meta file
296     allstr=' ';
297     keepgoing = 1;
298     while keepgoing > 0,
299     line = fgetl(fid);
300     if (line == -1)
301     keepgoing=-1;
302     else
303     % Strip out "(PID.TID *.*)" by finding first ")"
304     %old ind=findstr([line ')'],')'); line=line(ind(1)+1:end);
305     ind=findstr(line,')');
306     if size(ind) ~= 0
307     line=line(ind(1)+1:end);
308     end
309     % Remove comments of form //
310     line=[line,' //']; ind=findstr(line,'//'); line=line(1:ind(1)-1);
311     % Add to total string (without starting & ending blanks)
312     while line(1:1) == ' ', line=line(2:end); end
313     if strncmp(line,'map2glob',8), eval(line);
314     else allstr=[allstr,deblank(line),' '];
315     end
316     end
317     end
318    
319     % Close meta file
320     fclose(fid);
321    
322     % Strip out comments of form /* ... */
323     ind1=findstr(allstr,'/*'); ind2=findstr(allstr,'*/');
324     if size(ind1) ~= size(ind2)
325     error('The /* ... */ comments are not properly paired')
326     end
327     while size(ind1,2) > 0
328     allstr=[deblank(allstr(1:ind1(1)-1)) allstr(ind2(1)+2:end)];
329     %allstr=[allstr(1:ind1(1)-1) allstr(ind2(1)+3:end)];
330     ind1=findstr(allstr,'/*'); ind2=findstr(allstr,'*/');
331     end
332    
333     % This is a kludge to catch whether the meta-file is of the
334     % old or new type. nrecords does not exist in the old type.
335     nrecords = NaN;
336    
337     %- store the full string for output:
338     M=strrep(allstr,'format','dataprec');
339    
340     % Everything in lower case
341     allstr=lower(allstr);
342    
343     % Fix the unfortunate choice of 'format'
344     allstr=strrep(allstr,'format','dataprec');
345    
346     % Evaluate meta information
347     eval(allstr);
348    
349     N=reshape( dimlist , 3 , prod(size(dimlist))/3 );
350     rep=[' dimList = [ ',sprintf('%i ',N(1,:)),']'];
351     if ~isnan(nrecords) & nrecords > 1 & isempty(recnum)
352     N=[N,[nrecords 1 nrecords]'];
353     elseif ~isempty(recnum) & recnum>nrecords
354     error('Requested record number is higher than the number of available records')
355     end
356    
357     %- make "dimList" shorter (& fit output array size) in output "M":
358     pat=' dimList = \[(\s*\d+\,?)*\s*\]';
359     M=regexprep(M,pat,rep);
360     % and remove double space within sq.brakets:
361     ind1=findstr(M,'['); ind2=findstr(M,']');
362     if length(ind1) == length(ind2),
363     for i=length(ind1):-1:1, if ind1(i) < ind2(i),
364     M=[M(1:ind1(i)),regexprep(M(ind1(i)+1:ind2(i)-1),'(\s+)',' '),M(ind2(i):end)];
365     end; end
366     else error('The [ ... ] brakets are not properly paired')
367     end
368    
369     if isempty(recnum)
370     recnum=1;
371     end
372    
373     if isnan(nrecords)
374     % This is the old 'meta' method that used sequential access
375    
376     A=allstr;
377     % Open data file
378     fid=fopen(dname,'r',ieee);
379    
380     % Read record size in bytes
381     recsz=fread(fid,1,'uint32');
382     ldims=N(3,:)-N(2,:)+1;
383     numels=prod(ldims);
384    
385     rat=recsz/numels;
386     if rat == 4
387     A=fread(fid,numels,'real*4');
388     elseif rat == 8
389     A=fread(fid,numels,'real*8');
390     else
391     sprintf(' Implied size in meta-file = %d', numels )
392     sprintf(' Record size in data-file = %d', recsz )
393     error('Ratio between record size and size in meta-file inconsistent')
394     end
395    
396     erecsz=fread(fid,1,'uint32');
397     if erecsz ~= recsz
398     sprintf('WARNING: Record sizes at beginning and end of file are inconsistent')
399     end
400    
401     fclose(fid);
402    
403     A=reshape(A,ldims);
404    
405     else
406     % This is the new MDS format that uses direct access
407    
408     ldims=N(3,:)-N(2,:)+1;
409     for r=1:size(recnum(:),1);
410     if dataprec == 'float32'
411     A(:,r)=myrdda(dname,ldims,recnum(r),'real*4',ieee);
412     elseif dataprec == 'float64'
413     A(:,r)=myrdda(dname,ldims,recnum(r),'real*8',ieee);
414     else
415     error(['Unrecognized format in meta-file = ' format]);
416     end
417     end
418    
419     A=reshape(A,[ldims size(recnum(:),1)]);
420     if size(recnum(:),1)>1
421     N(1,end+1)=size(recnum(:),1);
422     N(2,end)=1;
423     N(3,end)=size(recnum(:),1);
424     end
425    
426     end
427    
428     %-------------------------------------------------------------------------------
429    
430     % result = RDDA( file, dim, irec [options] )
431     %
432     % This routine reads the irec'th record of shape 'dim' from the
433     % direct-access binary file (float or double precision) 'file'.
434     %
435     % Required arguments:
436     %
437     % file - string - name of file to read from
438     % dim - vector - dimensions of the file records and the resulting array
439     % irec - integer - record number in file in which to write data
440     %
441     % Optional arguments (must appear after the required arguments):
442     % prec - string - precision of storage in file. Default = 'real*8'.
443     % ieee - string - IEEE bit-wise representation in file. Default = 'b'.
444     %
445     % 'prec' may take the values:
446     % 'real*4' - floating point, 32 bits.
447     % 'real*8' - floating point, 64 bits - the efault.
448     %
449     % 'ieee' may take values:
450     % 'ieee-be' or 'b' - IEEE floating point with big-endian
451     % byte ordering - the default
452     % 'ieee-le' or 'l' - IEEE floating point with little-endian
453     % byte ordering
454     % 'native' or 'n' - local machine format
455     % 'cray' or 'c' - Cray floating point with big-endian
456     % byte ordering
457     % 'ieee-le.l64' or 'a' - IEEE floating point with little-endian
458     % byte ordering and 64 bit long data type
459     % 'ieee-be.l64' or 's' - IEEE floating point with big-endian byte
460     % ordering and 64 bit long data type.
461     %
462     % eg. T=rdda('t.data',[64 64 32],1);
463     % T=rdda('t.data',[256],4,'real*4');
464     % T=rdda('t.data',[128 64],2,'real*4','b');
465     function [arr] = myrdda(file,N,k,varargin)
466    
467     % Defaults
468     WORDLENGTH=8;
469     rtype='real*8';
470     ieee='b';
471    
472     % Check optional arguments
473     args=char(varargin);
474     while (size(args,1) > 0)
475     if deblank(args(1,:)) == 'real*4'
476     WORDLENGTH=4;
477     rtype='real*4';
478     elseif deblank(args(1,:)) == 'real*8'
479     WORDLENGTH=8;
480     rtype='real*8';
481     elseif deblank(args(1,:)) == 'n' | deblank(args(1,:)) == 'native'
482     ieee='n';
483     elseif deblank(args(1,:)) == 'l' | deblank(args(1,:)) == 'ieee-le'
484     ieee='l';
485     elseif deblank(args(1,:)) == 'b' | deblank(args(1,:)) == 'ieee-be'
486     ieee='b';
487     elseif deblank(args(1,:)) == 'c' | deblank(args(1,:)) == 'cray'
488     ieee='c';
489     elseif deblank(args(1,:)) == 'a' | deblank(args(1,:)) == 'ieee-le.l64'
490     ieee='a';
491     elseif deblank(args(1,:)) == 's' | deblank(args(1,:)) == 'ieee-be.l64'
492     ieee='s';
493     else
494     error(['Optional argument ' args(1,:) ' is unknown'])
495     end
496     args=args(2:end,:);
497     end
498    
499     nnn=prod(N);
500    
501     [fid mess]=fopen(file,'r',ieee);
502     if fid == -1
503     error('Error while opening file:\n%s',mess)
504     end
505     st=fseek(fid,nnn*(k-1)*WORDLENGTH,'bof');
506     if st ~= 0
507     mess=ferror(fid);
508     error('There was an error while positioning the file pointer:\n%s',mess)
509     end
510     [arr count]=fread(fid,nnn,rtype);
511     if count ~= nnn
512     error('Not enough data was available to be read: off EOF?')
513     end
514     st=fclose(fid);
515     %arr=reshape(arr,N);
516    
517     %
518     function [itrs] = scanforfiles(fname)
519    
520     itrs=[];
521     allfiles=dir([fname '.*.001.001.meta']);
522     if isempty(allfiles)
523     allfiles=dir([fname '.*.meta']);
524     ioff=0;
525     else
526     ioff=8;
527     end
528     for k=1:size(allfiles,1);
529     hh=allfiles(k).name;
530     itrs(k)=str2num( hh(end-14-ioff:end-5-ioff) );
531     end
532     itrs=sort(itrs);

  ViewVC Help
Powered by ViewVC 1.1.22