/[MITgcm]/MITgcm_contrib/SOSE/BoxAdj/input/rdmds.m
ViewVC logotype

Annotation of /MITgcm_contrib/SOSE/BoxAdj/input/rdmds.m

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


Revision 1.2 - (hide annotations) (download)
Mon Apr 18 23:55:43 2011 UTC (14 years, 3 months ago) by mmazloff
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +1 -1 lines
FILE REMOVED
updating

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

  ViewVC Help
Powered by ViewVC 1.1.22