1 |
function [AA] = rdmds(fnamearg,varargin) |
function [AA,iters] = rdmds(fnamearg,varargin) |
2 |
% RDMDS Read MITgcmUV meta/data files |
% RDMDS Read MITgcmUV meta/data files |
3 |
% |
% |
4 |
% A = RDMDS(FNAME) |
% A = RDMDS(FNAME) |
5 |
% A = RDMDS(FNAME,ITER) |
% A = RDMDS(FNAME,ITER) |
6 |
% A = RDMDS(FNAME,[ITER1 ITER2 ...]) |
% 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. |
% A = RDMDS(FNAME) reads data described by meta/data file format. |
13 |
% FNAME is a string containing the "head" of the file names. |
% FNAME is a string containing the "head" of the file names. |
34 |
% 10-digit iterartion number. |
% 10-digit iterartion number. |
35 |
% ITER is a vector of positive integers that will expand to the 10-digit |
% ITER is a vector of positive integers that will expand to the 10-digit |
36 |
% number in the file name. |
% 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 |
% eg. To repeat above operation |
41 |
% >> A=rdmds('T',2880); |
% >> A=rdmds('T',2880); |
42 |
% eg. To read multiple time steps |
% eg. To read multiple time steps |
43 |
% >> A=rdmds('T',[0 1440 2880]); |
% >> 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. |
% 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 |
% |
% |
|
% A = RDMDS(FNAME,MACHINEFORMAT) |
|
60 |
% A = RDMDS(FNAME,ITER,MACHINEFORMAT) allows the machine format to be |
% 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: |
% specified which MACHINEFORMAT is on of the following strings: |
63 |
% 'n' 'l' 'b' 'd' 'g' 'c' 'a' 's' - see FOPEN for more details |
% 'n' 'l' 'b' 'd' 'g' 'c' 'a' 's' - see FOPEN for more details |
64 |
% |
% |
65 |
% |
% |
66 |
% $Header$ |
% $Header$ |
67 |
|
|
68 |
|
AA=[]; |
69 |
|
iters=[]; |
70 |
|
|
71 |
% Default options |
% Default options |
72 |
ieee='b'; |
ieee='b'; |
73 |
fname=fnamearg; |
fname=fnamearg; |
74 |
iters=-1; |
userecords=0; |
75 |
|
recnum=[]; |
76 |
|
|
77 |
% Check optional arguments |
% Check optional arguments |
78 |
for ind=1:size(varargin,2); |
for ind=1:size(varargin,2); |
90 |
ieee='a'; |
ieee='a'; |
91 |
elseif strcmp(arg,'s') | strcmp(arg,'ieee-be.l64') |
elseif strcmp(arg,'s') | strcmp(arg,'ieee-be.l64') |
92 |
ieee='s'; |
ieee='s'; |
93 |
|
elseif strcmp(arg,'rec') |
94 |
|
userecords=1; |
95 |
else |
else |
96 |
error(['Optional argument ' arg ' is unknown']) |
error(['Optional argument ' arg ' is unknown']) |
97 |
end |
end |
98 |
else |
else |
99 |
|
if userecords==1 |
100 |
|
recnum=arg; |
101 |
|
elseif isempty(iters) |
102 |
if isnan(arg) |
if isnan(arg) |
103 |
iters=scanforfiles(fname); |
iters=scanforfiles(fname); |
104 |
disp([ sprintf('Reading %i time levels:',size(iters,2)) sprintf(' %i',iters) ]); |
disp([ sprintf('Reading %i time levels:',size(iters,2)) sprintf(' %i',iters) ]); |
105 |
elseif isinf(arg) |
elseif isinf(arg) |
106 |
iters=scanforfiles(fname); |
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)) ]); |
disp([ sprintf('Found %i time levels, reading %i',size(iters,2),iters(end)) ]); |
111 |
iters=iters(end); |
iters=iters(end); |
112 |
elseif prod(arg>=0) & prod(round(arg)==arg) |
elseif prod(arg>=0) & prod(round(arg)==arg) |
117 |
else |
else |
118 |
error(sprintf('Argument %i must be a positive integer',arg)) |
error(sprintf('Argument %i must be a positive integer',arg)) |
119 |
end |
end |
120 |
|
else |
121 |
|
error('Multiple iterations should be specified as a vector') |
122 |
|
end |
123 |
end |
end |
124 |
end |
end |
125 |
|
|
126 |
|
if isempty(iters) |
127 |
|
iters=-1; |
128 |
|
end |
129 |
|
|
130 |
% Loop over each iteration |
% Loop over each iteration |
131 |
for iter=1:size(iters,2); |
for iter=1:size(iters,2); |
132 |
if iters(iter)>=0 |
if iters(iter)>=0 |
145 |
allfiles=dir( sprintf('%s*.meta',fname) ); |
allfiles=dir( sprintf('%s*.meta',fname) ); |
146 |
|
|
147 |
if size(allfiles,1)==0 |
if size(allfiles,1)==0 |
148 |
disp(sprintf('No files match the search: %s.*.meta',fname)); |
disp(sprintf('No files match the search: %s*.meta',fname)); |
149 |
%allow partial reads% error('No files found.') |
%allow partial reads% error('No files found.') |
150 |
end |
end |
151 |
|
|
153 |
for j=1:size(allfiles,1); |
for j=1:size(allfiles,1); |
154 |
|
|
155 |
% Read meta- and data-file |
% Read meta- and data-file |
156 |
[A,N] = localrdmds([Dir allfiles(j).name],ieee); |
[A,N] = localrdmds([Dir allfiles(j).name],ieee,recnum); |
157 |
|
|
158 |
bdims=N(1,:); |
bdims=N(1,:); |
159 |
r0=N(2,:); |
r0=N(2,:); |
178 |
|
|
179 |
%------------------------------------------------------------------------------- |
%------------------------------------------------------------------------------- |
180 |
|
|
181 |
function [A,N] = localrdmds(fname,ieee) |
function [A,N] = localrdmds(fname,ieee,recnum) |
182 |
|
|
183 |
mname=strrep(fname,' ',''); |
mname=strrep(fname,' ',''); |
184 |
dname=strrep(mname,'.meta','.data'); |
dname=strrep(mname,'.meta','.data'); |
225 |
|
|
226 |
% This is a kludge to catch whether the meta-file is of the |
% This is a kludge to catch whether the meta-file is of the |
227 |
% old or new type. nrecords does not exist in the old type. |
% old or new type. nrecords does not exist in the old type. |
228 |
nrecords = -987; |
nrecords = NaN; |
229 |
|
|
230 |
% Everything in lower case |
% Everything in lower case |
231 |
allstr=lower(allstr); |
allstr=lower(allstr); |
237 |
eval(allstr); |
eval(allstr); |
238 |
|
|
239 |
N=reshape( dimlist , 3 , prod(size(dimlist))/3 ); |
N=reshape( dimlist , 3 , prod(size(dimlist))/3 ); |
240 |
if nrecords ~= -987 & nrecords > 1 |
if ~isnan(nrecords) & nrecords > 1 & isempty(recnum) |
241 |
N=[N,[nrecords 1 nrecords]']; |
N=[N,[nrecords 1 nrecords]']; |
242 |
|
elseif ~isempty(recnum) & recnum>nrecords |
243 |
|
error('Requested record number is higher than the number of available records') |
244 |
end |
end |
245 |
|
|
246 |
if nrecords == -987 |
if isempty(recnum) |
247 |
|
recnum=1; |
248 |
|
end |
249 |
|
|
250 |
|
if isnan(nrecords) |
251 |
% This is the old 'meta' method that used sequential access |
% This is the old 'meta' method that used sequential access |
252 |
|
|
253 |
A=allstr; |
A=allstr; |
283 |
% This is the new MDS format that uses direct access |
% This is the new MDS format that uses direct access |
284 |
|
|
285 |
ldims=N(3,:)-N(2,:)+1; |
ldims=N(3,:)-N(2,:)+1; |
286 |
|
for r=1:size(recnum(:),1); |
287 |
if dataprec == 'float32' |
if dataprec == 'float32' |
288 |
A=myrdda(dname,ldims,1,'real*4',ieee); |
A(:,r)=myrdda(dname,ldims,recnum(r),'real*4',ieee); |
289 |
elseif dataprec == 'float64' |
elseif dataprec == 'float64' |
290 |
A=myrdda(dname,ldims,1,'real*8',ieee); |
A(:,r)=myrdda(dname,ldims,recnum(r),'real*8',ieee); |
291 |
else |
else |
292 |
error(['Unrecognized format in meta-file = ' format]); |
error(['Unrecognized format in meta-file = ' format]); |
293 |
end |
end |
294 |
|
end |
295 |
|
|
296 |
|
A=reshape(A,[ldims size(recnum(:),1)]); |
297 |
|
if size(recnum(:),1)>1 |
298 |
|
N(1,end+1)=size(recnum(:),1); |
299 |
|
N(2,end)=1; |
300 |
|
N(3,end)=size(recnum(:),1); |
301 |
|
end |
302 |
|
|
303 |
end |
end |
304 |
|
|
389 |
error('Not enough data was available to be read: off EOF?') |
error('Not enough data was available to be read: off EOF?') |
390 |
end |
end |
391 |
st=fclose(fid); |
st=fclose(fid); |
392 |
arr=reshape(arr,N); |
%arr=reshape(arr,N); |
393 |
|
|
394 |
% |
% |
395 |
function [iters] = scanforfiles(fname) |
function [iters] = scanforfiles(fname) |
396 |
|
|
397 |
|
iters=[]; |
398 |
allfiles=dir([fname '.*.001.001.meta']); |
allfiles=dir([fname '.*.001.001.meta']); |
399 |
if isempty(allfiles) |
if isempty(allfiles) |
400 |
allfiles=dir([fname '.*.meta']); |
allfiles=dir([fname '.*.meta']); |
406 |
hh=allfiles(k).name; |
hh=allfiles(k).name; |
407 |
iters(k)=str2num( hh(end-14-ioff:end-5-ioff) ); |
iters(k)=str2num( hh(end-14-ioff:end-5-ioff) ); |
408 |
end |
end |
409 |
|
iters=sort(iters); |