/[MITgcm]/MITgcm/verification/tutorial_global_oce_latlon/diags_matlab/rdmds.m
ViewVC logotype

Contents of /MITgcm/verification/tutorial_global_oce_latlon/diags_matlab/rdmds.m

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


Revision 1.3 - (show annotations) (download)
Sat Aug 12 20:25:13 2006 UTC (17 years, 9 months ago) by jmc
Branch: MAIN
CVS Tags: checkpoint58u_post, checkpoint58w_post, checkpoint60, checkpoint61, checkpoint62, checkpoint58r_post, checkpoint58n_post, checkpoint58x_post, checkpoint58t_post, checkpoint58q_post, checkpoint59q, checkpoint59p, checkpoint59r, checkpoint59e, checkpoint59d, checkpoint59g, checkpoint59f, checkpoint59a, checkpoint59c, checkpoint59b, checkpoint59m, checkpoint59l, checkpoint59o, checkpoint59n, checkpoint59i, checkpoint59h, checkpoint59k, checkpoint59j, checkpoint59, checkpoint58o_post, checkpoint62c, checkpoint62b, checkpoint62a, checkpoint62g, checkpoint62f, checkpoint62e, checkpoint62d, checkpoint62k, checkpoint62j, checkpoint62i, checkpoint62h, checkpoint62o, checkpoint62n, checkpoint62m, checkpoint62l, checkpoint62s, checkpoint62r, checkpoint62q, checkpoint62p, checkpoint62v, checkpoint62u, checkpoint62t, checkpoint58y_post, checkpoint58v_post, checkpoint58s_post, checkpoint61f, checkpoint61g, checkpoint61d, checkpoint61e, checkpoint61b, checkpoint61c, checkpoint58p_post, checkpoint61a, checkpoint61n, checkpoint61o, checkpoint61l, checkpoint61m, checkpoint61j, checkpoint61k, checkpoint61h, checkpoint61i, checkpoint61v, checkpoint61w, checkpoint61t, checkpoint61u, checkpoint61r, checkpoint61s, checkpoint61p, checkpoint61q, checkpoint61z, checkpoint61x, checkpoint61y
Changes since 1.2: +1 -1 lines
accidentally removed ; put them back.

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

  ViewVC Help
Powered by ViewVC 1.1.22