/[MITgcm]/MITgcm/utils/matlab/rdmds.m
ViewVC logotype

Annotation of /MITgcm/utils/matlab/rdmds.m

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


Revision 1.9 - (hide annotations) (download)
Wed Nov 21 17:56:01 2001 UTC (22 years, 6 months ago) by adcroft
Branch: MAIN
Changes since 1.8: +16 -3 lines
Small mod. to read "all" files in one go.

1 adcroft 1.3 function [AA] = rdmds(fnamearg,varargin)
2 adcroft 1.4 % RDMDS Read MITgcmUV meta/data files
3 adcroft 1.1 %
4 adcroft 1.3 % A = RDMDS(FNAME)
5     % A = RDMDS(FNAME,ITER)
6     % A = RDMDS(FNAME,[ITER1 ITER2 ...])
7 adcroft 1.1 %
8 adcroft 1.4 % A = RDMDS(FNAME) reads data described by meta/data file format.
9     % FNAME is a string containing the "head" of the file names.
10     %
11     % eg. To load the meta-data files
12     % T.0000002880.000.000.meta, T.0000002880.000.000.data
13     % T.0000002880.001.000.meta, T.0000002880.001.000.data
14     % T.0000002880.002.000.meta, T.0000002880.002.000.data
15     % T.0000002880.003.000.meta, T.0000002880.003.000.data
16     % use
17     % >> A=rdmds('T.0000002880');
18     % >> size(A)
19     % ans =
20     % 64 32 5
21     % eg. To load a multiple record file
22     % >> A=rdmds('pickup.0000002880');
23     % >> size(A)
24     % ans =
25     % 64 32 5 61
26     %
27     %
28     % A = RDMDS(FNAME,ITER) reads data described by meta/data file format.
29     % FNAME is a string containing the "head" of the file name excluding the
30     % 10-digit iterartion number.
31     % ITER is a vector of positive integers that will expand to the 10-digit
32     % number in the file name.
33     %
34     % eg. To repeat above operation
35     % >> A=rdmds('T',2880);
36     % eg. To read multiple time steps
37     % >> A=rdmds('T',[0 1440 2880]);
38     % Note: this form can not read files with no iteration count in file name.
39     %
40     % A = RDMDS(FNAME,MACHINEFORMAT)
41     % A = RDMDS(FNAME,ITER,MACHINEFORMAT) allows the machine format to be
42     % specified which MACHINEFORMAT is on of the following strings:
43     % 'n' 'l' 'b' 'd' 'g' 'c' 'a' 's' - see FOPEN for more details
44     %
45     %
46 adcroft 1.9 % $Header: /u/gcmpack/models/MITgcmUV/utils/matlab/rdmds.m,v 1.8 2001/09/10 14:09:42 adcroft Exp $
47 adcroft 1.1
48     % Default options
49     ieee='b';
50 adcroft 1.3 fname=fnamearg;
51     iters=-1;
52 adcroft 1.1
53     % Check optional arguments
54 adcroft 1.3 for ind=1:size(varargin,2);
55     arg=varargin{ind};
56     if ischar(arg)
57     if strcmp(arg,'n') | strcmp(arg,'native')
58     ieee='n';
59     elseif strcmp(arg,'l') | strcmp(arg,'ieee-le')
60     ieee='l';
61     elseif strcmp(arg,'b') | strcmp(arg,'ieee-be')
62     ieee='b';
63     elseif strcmp(arg,'c') | strcmp(arg,'cray')
64     ieee='c';
65     elseif strcmp(arg,'a') | strcmp(arg,'ieee-le.l64')
66     ieee='a';
67     elseif strcmp(arg,'s') | strcmp(arg,'ieee-be.l64')
68     ieee='s';
69     else
70     error(['Optional argument ' arg ' is unknown'])
71     end
72 adcroft 1.1 else
73 adcroft 1.3 if arg>=9999999999
74     error(sprintf('Argument %i > 9999999999',arg))
75     end
76 adcroft 1.9 if isnan(arg)
77     iters=scanforfiles(fname);
78     elseif prod(arg>=0) & prod(round(arg)==arg)
79 adcroft 1.3 iters=arg;
80     else
81     error(sprintf('Argument %i must be a positive integer',arg))
82     end
83 adcroft 1.1 end
84 adcroft 1.3 end
85    
86     % Loop over each iteration
87     for iter=1:size(iters,2);
88     if iters(iter)>=0
89 adcroft 1.6 fname=sprintf('%s.%10.10i',fnamearg,iters(iter));
90 adcroft 1.1 end
91    
92 adcroft 1.7 % Figure out if there is a path in the filename
93     NS=findstr('/',fname);
94     if size(NS)>0
95     Dir=fname(1:NS(end));
96     else
97     Dir='./';
98     end
99    
100 adcroft 1.1 % Match name of all meta-files
101 adcroft 1.8 allfiles=dir( sprintf('%s*.meta',fname) );
102 adcroft 1.1
103 adcroft 1.3 if size(allfiles,1)==0
104     disp(sprintf('No files match the search: %s.*.meta',fname));
105 adcroft 1.9 %allow partial reads% error('No files found.')
106 adcroft 1.3 end
107 adcroft 1.1
108     % Loop through allfiles
109 adcroft 1.3 for j=1:size(allfiles,1);
110 adcroft 1.1
111     % Read meta- and data-file
112 adcroft 1.7 [A,N] = localrdmds([Dir allfiles(j).name],ieee);
113 adcroft 1.1
114     bdims=N(1,:);
115     r0=N(2,:);
116     rN=N(3,:);
117     ndims=prod(size(bdims));
118     if (ndims == 1)
119 adcroft 1.3 AA(r0(1):rN(1),iter)=A;
120 adcroft 1.1 elseif (ndims == 2)
121 adcroft 1.3 AA(r0(1):rN(1),r0(2):rN(2),iter)=A;
122 adcroft 1.1 elseif (ndims == 3)
123 adcroft 1.3 AA(r0(1):rN(1),r0(2):rN(2),r0(3):rN(3),iter)=A;
124 adcroft 1.1 elseif (ndims == 4)
125 adcroft 1.3 AA(r0(1):rN(1),r0(2):rN(2),r0(3):rN(3),r0(4):rN(4),iter)=A;
126 adcroft 1.5 elseif (ndims == 5)
127 adcroft 1.3 AA(r0(1):rN(1),r0(2):rN(2),r0(3):rN(3),r0(4):rN(4),r0(5):rN(5),iter)=A;
128 adcroft 1.1 else
129     error('Dimension of data set is larger than currently coded. Sorry!')
130     end
131    
132 adcroft 1.3 end % files
133     end % iterations
134 adcroft 1.1
135     %-------------------------------------------------------------------------------
136    
137     function [A,N] = localrdmds(fname,ieee)
138    
139     mname=strrep(fname,' ','');
140     dname=strrep(mname,'.meta','.data');
141    
142     % Read and interpret Meta file
143     fid = fopen(mname,'r');
144     if (fid == -1)
145     error(['File' mname ' could not be opened'])
146     end
147    
148     % Scan each line of the Meta file
149     allstr=' ';
150     keepgoing = 1;
151     while keepgoing > 0,
152     line = fgetl(fid);
153     if (line == -1)
154     keepgoing=-1;
155     else
156     % Strip out "(PID.TID *.*)" by finding first ")"
157     %old ind=findstr([line ')'],')'); line=line(ind(1)+1:end);
158     ind=findstr(line,')');
159     if size(ind) ~= 0
160     line=line(ind(1)+1:end);
161     end
162     % Remove comments of form //
163     line=[line ' //']; ind=findstr(line,'//'); line=line(1:ind(1)-1);
164     % Add to total string
165     allstr=[allstr line];
166     end
167     end
168    
169     % Close meta file
170     fclose(fid);
171    
172     % Strip out comments of form /* ... */
173     ind1=findstr(allstr,'/*'); ind2=findstr(allstr,'*/');
174     if size(ind1) ~= size(ind2)
175     error('The /* ... */ comments are not properly paired')
176     end
177     while size(ind1,2) > 0
178     allstr=[allstr(1:ind1(1)-1) allstr(ind2(1)+3:end)];
179     ind1=findstr(allstr,'/*'); ind2=findstr(allstr,'*/');
180     end
181    
182     % This is a kludge to catch whether the meta-file is of the
183     % old or new type. nrecords does not exist in the old type.
184     nrecords = -987;
185    
186     % Everything in lower case
187     allstr=lower(allstr);
188    
189     % Fix the unfortunate choice of 'format'
190     allstr=strrep(allstr,'format','dataprec');
191    
192     % Evaluate meta information
193     eval(allstr);
194    
195     N=reshape( dimlist , 3 , prod(size(dimlist))/3 );
196 adcroft 1.4 if nrecords ~= -987 & nrecords > 1
197     N=[N,[nrecords 1 nrecords]'];
198     end
199 adcroft 1.1
200     if nrecords == -987
201     % This is the old 'meta' method that used sequential access
202    
203     A=allstr;
204     % Open data file
205     fid=fopen(dname,'r',ieee);
206    
207     % Read record size in bytes
208     recsz=fread(fid,1,'uint32');
209     ldims=N(3,:)-N(2,:)+1;
210     numels=prod(ldims);
211    
212     rat=recsz/numels;
213     if rat == 4
214     A=fread(fid,numels,'real*4');
215     elseif rat == 8
216     A=fread(fid,numels,'real*8');
217     else
218     sprintf(' Implied size in meta-file = %d', numels )
219     sprintf(' Record size in data-file = %d', recsz )
220     error('Ratio between record size and size in meta-file inconsistent')
221     end
222    
223     erecsz=fread(fid,1,'uint32');
224     if erecsz ~= recsz
225     sprintf('WARNING: Record sizes at beginning and end of file are inconsistent')
226     end
227    
228     fclose(fid);
229    
230     A=reshape(A,ldims);
231    
232     else
233     % This is the new MDS format that uses direct access
234    
235     ldims=N(3,:)-N(2,:)+1;
236     if dataprec == 'float32'
237     A=myrdda(dname,ldims,1,'real*4',ieee);
238     elseif dataprec == 'float64'
239     A=myrdda(dname,ldims,1,'real*8',ieee);
240     else
241     error(['Unrecognized format in meta-file = ' format]);
242     end
243    
244     end
245    
246     %-------------------------------------------------------------------------------
247    
248     % result = RDDA( file, dim, irec [options] )
249     %
250     % This routine reads the irec'th record of shape 'dim' from the
251     % direct-access binary file (float or double precision) 'file'.
252     %
253     % Required arguments:
254     %
255     % file - string - name of file to read from
256     % dim - vector - dimensions of the file records and the resulting array
257     % irec - integer - record number in file in which to write data
258     %
259     % Optional arguments (must appear after the required arguments):
260     % prec - string - precision of storage in file. Default = 'real*8'.
261     % ieee - string - IEEE bit-wise representation in file. Default = 'b'.
262     %
263     % 'prec' may take the values:
264     % 'real*4' - floating point, 32 bits.
265     % 'real*8' - floating point, 64 bits - the efault.
266     %
267     % 'ieee' may take values:
268     % 'ieee-be' or 'b' - IEEE floating point with big-endian
269     % byte ordering - the default
270     % 'ieee-le' or 'l' - IEEE floating point with little-endian
271     % byte ordering
272     % 'native' or 'n' - local machine format
273     % 'cray' or 'c' - Cray floating point with big-endian
274     % byte ordering
275     % 'ieee-le.l64' or 'a' - IEEE floating point with little-endian
276     % byte ordering and 64 bit long data type
277     % 'ieee-be.l64' or 's' - IEEE floating point with big-endian byte
278     % ordering and 64 bit long data type.
279     %
280     % eg. T=rdda('t.data',[64 64 32],1);
281     % T=rdda('t.data',[256],4,'real*4');
282     % T=rdda('t.data',[128 64],2,'real*4','b');
283     function [arr] = myrdda(file,N,k,varargin)
284    
285     % Defaults
286     WORDLENGTH=8;
287     rtype='real*8';
288     ieee='b';
289    
290     % Check optional arguments
291     args=char(varargin);
292     while (size(args,1) > 0)
293     if deblank(args(1,:)) == 'real*4'
294     WORDLENGTH=4;
295     rtype='real*4';
296     elseif deblank(args(1,:)) == 'real*8'
297     WORDLENGTH=8;
298     rtype='real*8';
299     elseif deblank(args(1,:)) == 'n' | deblank(args(1,:)) == 'native'
300     ieee='n';
301     elseif deblank(args(1,:)) == 'l' | deblank(args(1,:)) == 'ieee-le'
302     ieee='l';
303     elseif deblank(args(1,:)) == 'b' | deblank(args(1,:)) == 'ieee-be'
304     ieee='b';
305     elseif deblank(args(1,:)) == 'c' | deblank(args(1,:)) == 'cray'
306     ieee='c';
307     elseif deblank(args(1,:)) == 'a' | deblank(args(1,:)) == 'ieee-le.l64'
308     ieee='a';
309     elseif deblank(args(1,:)) == 's' | deblank(args(1,:)) == 'ieee-be.l64'
310     ieee='s';
311     else
312     error(['Optional argument ' args(1,:) ' is unknown'])
313     end
314     args=args(2:end,:);
315     end
316    
317     nnn=prod(N);
318    
319     [fid mess]=fopen(file,'r',ieee);
320     if fid == -1
321     error('Error while opening file:\n%s',mess)
322     end
323     st=fseek(fid,nnn*(k-1)*WORDLENGTH,'bof');
324     if st ~= 0
325     mess=ferror(fid);
326     error('There was an error while positioning the file pointer:\n%s',mess)
327     end
328     [arr count]=fread(fid,nnn,rtype);
329     if count ~= nnn
330     error('Not enough data was available to be read: off EOF?')
331     end
332     st=fclose(fid);
333     arr=reshape(arr,N);
334 adcroft 1.9
335     %
336     function [iters] = scanforfiles(fname)
337    
338     allfiles=dir([fname '.*.meta']);
339     for k=1:size(allfiles,1);
340     hh=allfiles(k).name;
341     iters(k)=str2num( hh(end-14:end-5) );
342     end
343    
344     disp([ sprintf('Reading %i time levels:',size(allfiles,1)) sprintf(' %i',iters) ]);

  ViewVC Help
Powered by ViewVC 1.1.22