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