/[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.3 - (hide annotations) (download)
Thu Sep 11 19:38:24 2014 UTC (10 years, 10 months ago) by gforget
Branch: MAIN
Changes since 1.2: +7 -5 lines
- fix for pc (when '\' may be used instead of '/')

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

  ViewVC Help
Powered by ViewVC 1.1.22