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: /u/gcmpack/MITgcm/utils/matlab/rdmds.m,v 1.15 2002/10/11 14:29:45 adcroft Exp $ |
67 |
|
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 |
error('Requested record number is higher than the number of available records') |
245 |
end |
246 |
|
247 |
if isempty(recnum) |
248 |
recnum=1; |
249 |
end |
250 |
|
251 |
if isnan(nrecords) |
252 |
% This is the old 'meta' method that used sequential access |
253 |
|
254 |
A=allstr; |
255 |
% Open data file |
256 |
fid=fopen(dname,'r',ieee); |
257 |
|
258 |
% Read record size in bytes |
259 |
recsz=fread(fid,1,'uint32'); |
260 |
ldims=N(3,:)-N(2,:)+1; |
261 |
numels=prod(ldims); |
262 |
|
263 |
rat=recsz/numels; |
264 |
if rat == 4 |
265 |
A=fread(fid,numels,'real*4'); |
266 |
elseif rat == 8 |
267 |
A=fread(fid,numels,'real*8'); |
268 |
else |
269 |
sprintf(' Implied size in meta-file = %d', numels ) |
270 |
sprintf(' Record size in data-file = %d', recsz ) |
271 |
error('Ratio between record size and size in meta-file inconsistent') |
272 |
end |
273 |
|
274 |
erecsz=fread(fid,1,'uint32'); |
275 |
if erecsz ~= recsz |
276 |
sprintf('WARNING: Record sizes at beginning and end of file are inconsistent') |
277 |
end |
278 |
|
279 |
fclose(fid); |
280 |
|
281 |
A=reshape(A,ldims); |
282 |
|
283 |
else |
284 |
% This is the new MDS format that uses direct access |
285 |
|
286 |
ldims=N(3,:)-N(2,:)+1; |
287 |
for r=1:size(recnum(:),1); |
288 |
if dataprec == 'float32' |
289 |
A(:,r)=myrdda(dname,ldims,recnum(r),'real*4',ieee); |
290 |
elseif dataprec == 'float64' |
291 |
A(:,r)=myrdda(dname,ldims,recnum(r),'real*8',ieee); |
292 |
else |
293 |
error(['Unrecognized format in meta-file = ' format]); |
294 |
end |
295 |
end |
296 |
|
297 |
A=reshape(A,[ldims size(recnum(:),1)]); |
298 |
if size(recnum(:),1)>1 |
299 |
N(1,end+1)=size(recnum(:),1); |
300 |
N(2,end)=1; |
301 |
N(3,end)=size(recnum(:),1); |
302 |
end |
303 |
|
304 |
end |
305 |
|
306 |
%------------------------------------------------------------------------------- |
307 |
|
308 |
% result = RDDA( file, dim, irec [options] ) |
309 |
% |
310 |
% This routine reads the irec'th record of shape 'dim' from the |
311 |
% direct-access binary file (float or double precision) 'file'. |
312 |
% |
313 |
% Required arguments: |
314 |
% |
315 |
% file - string - name of file to read from |
316 |
% dim - vector - dimensions of the file records and the resulting array |
317 |
% irec - integer - record number in file in which to write data |
318 |
% |
319 |
% Optional arguments (must appear after the required arguments): |
320 |
% prec - string - precision of storage in file. Default = 'real*8'. |
321 |
% ieee - string - IEEE bit-wise representation in file. Default = 'b'. |
322 |
% |
323 |
% 'prec' may take the values: |
324 |
% 'real*4' - floating point, 32 bits. |
325 |
% 'real*8' - floating point, 64 bits - the efault. |
326 |
% |
327 |
% 'ieee' may take values: |
328 |
% 'ieee-be' or 'b' - IEEE floating point with big-endian |
329 |
% byte ordering - the default |
330 |
% 'ieee-le' or 'l' - IEEE floating point with little-endian |
331 |
% byte ordering |
332 |
% 'native' or 'n' - local machine format |
333 |
% 'cray' or 'c' - Cray floating point with big-endian |
334 |
% byte ordering |
335 |
% 'ieee-le.l64' or 'a' - IEEE floating point with little-endian |
336 |
% byte ordering and 64 bit long data type |
337 |
% 'ieee-be.l64' or 's' - IEEE floating point with big-endian byte |
338 |
% ordering and 64 bit long data type. |
339 |
% |
340 |
% eg. T=rdda('t.data',[64 64 32],1); |
341 |
% T=rdda('t.data',[256],4,'real*4'); |
342 |
% T=rdda('t.data',[128 64],2,'real*4','b'); |
343 |
function [arr] = myrdda(file,N,k,varargin) |
344 |
|
345 |
% Defaults |
346 |
WORDLENGTH=8; |
347 |
rtype='real*8'; |
348 |
ieee='b'; |
349 |
|
350 |
% Check optional arguments |
351 |
args=char(varargin); |
352 |
while (size(args,1) > 0) |
353 |
if deblank(args(1,:)) == 'real*4' |
354 |
WORDLENGTH=4; |
355 |
rtype='real*4'; |
356 |
elseif deblank(args(1,:)) == 'real*8' |
357 |
WORDLENGTH=8; |
358 |
rtype='real*8'; |
359 |
elseif deblank(args(1,:)) == 'n' | deblank(args(1,:)) == 'native' |
360 |
ieee='n'; |
361 |
elseif deblank(args(1,:)) == 'l' | deblank(args(1,:)) == 'ieee-le' |
362 |
ieee='l'; |
363 |
elseif deblank(args(1,:)) == 'b' | deblank(args(1,:)) == 'ieee-be' |
364 |
ieee='b'; |
365 |
elseif deblank(args(1,:)) == 'c' | deblank(args(1,:)) == 'cray' |
366 |
ieee='c'; |
367 |
elseif deblank(args(1,:)) == 'a' | deblank(args(1,:)) == 'ieee-le.l64' |
368 |
ieee='a'; |
369 |
elseif deblank(args(1,:)) == 's' | deblank(args(1,:)) == 'ieee-be.l64' |
370 |
ieee='s'; |
371 |
else |
372 |
error(['Optional argument ' args(1,:) ' is unknown']) |
373 |
end |
374 |
args=args(2:end,:); |
375 |
end |
376 |
|
377 |
nnn=prod(N); |
378 |
|
379 |
[fid mess]=fopen(file,'r',ieee); |
380 |
if fid == -1 |
381 |
error('Error while opening file:\n%s',mess) |
382 |
end |
383 |
st=fseek(fid,nnn*(k-1)*WORDLENGTH,'bof'); |
384 |
if st ~= 0 |
385 |
mess=ferror(fid); |
386 |
error('There was an error while positioning the file pointer:\n%s',mess) |
387 |
end |
388 |
[arr count]=fread(fid,nnn,rtype); |
389 |
if count ~= nnn |
390 |
error('Not enough data was available to be read: off EOF?') |
391 |
end |
392 |
st=fclose(fid); |
393 |
%arr=reshape(arr,N); |
394 |
|
395 |
% |
396 |
function [iters] = scanforfiles(fname) |
397 |
|
398 |
iters=[]; |
399 |
allfiles=dir([fname '.*.001.001.meta']); |
400 |
if isempty(allfiles) |
401 |
allfiles=dir([fname '.*.meta']); |
402 |
ioff=0; |
403 |
else |
404 |
ioff=8; |
405 |
end |
406 |
for k=1:size(allfiles,1); |
407 |
hh=allfiles(k).name; |
408 |
iters(k)=str2num( hh(end-14-ioff:end-5-ioff) ); |
409 |
end |
410 |
iters=sort(iters); |