1 |
jmc |
1.1 |
function [AA] = rdmds(fname,varargin) |
2 |
|
|
% |
3 |
|
|
% Read MITgcmUV Meta/Data files |
4 |
|
|
% |
5 |
|
|
% A = RDMDS(FNAME) reads data described by meta/data file format. |
6 |
|
|
% FNAME is a string containing the "head" of the file names. |
7 |
|
|
% |
8 |
|
|
% eg. To load the meta-data files |
9 |
|
|
% T.0000002880.000.000.meta, T.0000002880.000.000.data |
10 |
|
|
% T.0000002880.001.000.meta, T.0000002880.001.000.data |
11 |
|
|
% T.0000002880.002.000.meta, T.0000002880.002.000.data |
12 |
|
|
% T.0000002880.003.000.meta, T.0000002880.003.000.data |
13 |
|
|
% use |
14 |
|
|
% >> A=rdmds('T.0000002880'); |
15 |
|
|
% |
16 |
|
|
% A = RDMDS(FNAME,MACHINEFORMAT) allows the machine format to be specified |
17 |
|
|
% which MACHINEFORMAT is on of the following strings: |
18 |
|
|
% |
19 |
|
|
% 'native' or 'n' - local machine format - the default |
20 |
|
|
% 'ieee-le' or 'l' - IEEE floating point with little-endian |
21 |
|
|
% byte ordering |
22 |
|
|
% 'ieee-be' or 'b' - IEEE floating point with big-endian |
23 |
|
|
% byte ordering |
24 |
|
|
% 'vaxd' or 'd' - VAX D floating point and VAX ordering |
25 |
|
|
% 'vaxg' or 'g' - VAX G floating point and VAX ordering |
26 |
|
|
% 'cray' or 'c' - Cray floating point with big-endian |
27 |
|
|
% byte ordering |
28 |
|
|
% 'ieee-le.l64' or 'a' - IEEE floating point with little-endian |
29 |
|
|
% byte ordering and 64 bit long data type |
30 |
|
|
% 'ieee-be.l64' or 's' - IEEE floating point with big-endian byte |
31 |
|
|
% ordering and 64 bit long data type. |
32 |
|
|
% |
33 |
|
|
|
34 |
jmc |
1.3 |
% $Header: $ |
35 |
jmc |
1.1 |
% $Name: $ |
36 |
|
|
|
37 |
|
|
% Default options |
38 |
|
|
ieee='b'; |
39 |
|
|
|
40 |
|
|
% Check optional arguments |
41 |
|
|
args=char(varargin); |
42 |
|
|
while (size(args,1) > 0) |
43 |
|
|
if deblank(args(1,:)) == 'n' | deblank(args(1,:)) == 'native' |
44 |
|
|
ieee='n'; |
45 |
|
|
elseif deblank(args(1,:)) == 'l' | deblank(args(1,:)) == 'ieee-le' |
46 |
|
|
ieee='l'; |
47 |
|
|
elseif deblank(args(1,:)) == 'b' | deblank(args(1,:)) == 'ieee-be' |
48 |
|
|
ieee='b'; |
49 |
|
|
elseif deblank(args(1,:)) == 'c' | deblank(args(1,:)) == 'cray' |
50 |
|
|
ieee='c'; |
51 |
|
|
elseif deblank(args(1,:)) == 'a' | deblank(args(1,:)) == 'ieee-le.l64' |
52 |
|
|
ieee='a'; |
53 |
|
|
elseif deblank(args(1,:)) == 's' | deblank(args(1,:)) == 'ieee-be.l64' |
54 |
|
|
ieee='s'; |
55 |
|
|
else |
56 |
|
|
error(['Optional argument ' args(1,:) ' is unknown']) |
57 |
|
|
end |
58 |
|
|
args=args(2:end,:); |
59 |
|
|
end |
60 |
|
|
|
61 |
|
|
% Match name of all meta-files |
62 |
|
|
eval(['ls ' fname '*.meta;']); |
63 |
|
|
allfiles=ans; |
64 |
|
|
|
65 |
|
|
% Beginning and end of strings |
66 |
|
|
Iend=findstr(allfiles,'.meta')+4; |
67 |
|
|
Ibeg=[1 Iend(1:end-1)+2]; |
68 |
|
|
|
69 |
|
|
% Loop through allfiles |
70 |
|
|
for j=1:prod(size(Ibeg)), |
71 |
|
|
|
72 |
|
|
% Read meta- and data-file |
73 |
|
|
[A,N] = localrdmds(allfiles(Ibeg(j):Iend(j)),ieee); |
74 |
|
|
|
75 |
|
|
bdims=N(1,:); |
76 |
|
|
r0=N(2,:); |
77 |
|
|
rN=N(3,:); |
78 |
|
|
ndims=prod(size(bdims)); |
79 |
|
|
if (ndims == 1) |
80 |
|
|
AA(r0(1):rN(1))=A; |
81 |
|
|
elseif (ndims == 2) |
82 |
|
|
AA(r0(1):rN(1),r0(2):rN(2))=A; |
83 |
|
|
elseif (ndims == 3) |
84 |
|
|
AA(r0(1):rN(1),r0(2):rN(2),r0(3):rN(3))=A; |
85 |
|
|
elseif (ndims == 4) |
86 |
|
|
AA(r0(1):rN(1),r0(2):rN(2),r0(3):rN(3),r0(4):rN(4))=A; |
87 |
|
|
else |
88 |
|
|
error('Dimension of data set is larger than currently coded. Sorry!') |
89 |
|
|
end |
90 |
|
|
|
91 |
|
|
end |
92 |
|
|
|
93 |
|
|
%------------------------------------------------------------------------------- |
94 |
|
|
|
95 |
|
|
function [A,N] = localrdmds(fname,ieee) |
96 |
|
|
|
97 |
|
|
mname=strrep(fname,' ',''); |
98 |
|
|
dname=strrep(mname,'.meta','.data'); |
99 |
|
|
|
100 |
|
|
% Read and interpret Meta file |
101 |
|
|
fid = fopen(mname,'r'); |
102 |
|
|
if (fid == -1) |
103 |
|
|
error(['File' mname ' could not be opened']) |
104 |
|
|
end |
105 |
|
|
|
106 |
|
|
% Scan each line of the Meta file |
107 |
|
|
allstr=' '; |
108 |
|
|
keepgoing = 1; |
109 |
|
|
while keepgoing > 0, |
110 |
|
|
line = fgetl(fid); |
111 |
|
|
if (line == -1) |
112 |
|
|
keepgoing=-1; |
113 |
|
|
else |
114 |
|
|
% Strip out "(PID.TID *.*)" by finding first ")" |
115 |
|
|
%old ind=findstr([line ')'],')'); line=line(ind(1)+1:end); |
116 |
|
|
ind=findstr(line,')'); |
117 |
|
|
if size(ind) ~= 0 |
118 |
|
|
line=line(ind(1)+1:end); |
119 |
|
|
end |
120 |
|
|
% Remove comments of form // |
121 |
|
|
line=[line ' //']; ind=findstr(line,'//'); line=line(1:ind(1)-1); |
122 |
|
|
% Add to total string |
123 |
|
|
allstr=[allstr line]; |
124 |
|
|
end |
125 |
|
|
end |
126 |
|
|
|
127 |
|
|
% Close meta file |
128 |
|
|
fclose(fid); |
129 |
|
|
|
130 |
|
|
% Strip out comments of form /* ... */ |
131 |
|
|
ind1=findstr(allstr,'/*'); ind2=findstr(allstr,'*/'); |
132 |
|
|
if size(ind1) ~= size(ind2) |
133 |
|
|
error('The /* ... */ comments are not properly paired') |
134 |
|
|
end |
135 |
|
|
while size(ind1,2) > 0 |
136 |
|
|
allstr=[allstr(1:ind1(1)-1) allstr(ind2(1)+3:end)]; |
137 |
|
|
ind1=findstr(allstr,'/*'); ind2=findstr(allstr,'*/'); |
138 |
|
|
end |
139 |
|
|
|
140 |
|
|
% This is a kludge to catch whether the meta-file is of the |
141 |
|
|
% old or new type. nrecords does not exist in the old type. |
142 |
|
|
nrecords = -987; |
143 |
|
|
|
144 |
|
|
% Everything in lower case |
145 |
|
|
allstr=lower(allstr); |
146 |
|
|
|
147 |
|
|
% Fix the unfortunate choice of 'format' |
148 |
|
|
allstr=strrep(allstr,'format','dataprec'); |
149 |
|
|
|
150 |
|
|
% Evaluate meta information |
151 |
|
|
eval(allstr); |
152 |
|
|
|
153 |
|
|
N=reshape( dimlist , 3 , prod(size(dimlist))/3 ); |
154 |
|
|
|
155 |
|
|
if nrecords == -987 |
156 |
|
|
% This is the old 'meta' method that used sequential access |
157 |
|
|
|
158 |
|
|
A=allstr; |
159 |
|
|
% Open data file |
160 |
|
|
fid=fopen(dname,'r',ieee); |
161 |
|
|
|
162 |
|
|
% Read record size in bytes |
163 |
|
|
recsz=fread(fid,1,'uint32'); |
164 |
|
|
ldims=N(3,:)-N(2,:)+1; |
165 |
|
|
numels=prod(ldims); |
166 |
|
|
|
167 |
|
|
rat=recsz/numels; |
168 |
|
|
if rat == 4 |
169 |
|
|
A=fread(fid,numels,'real*4'); |
170 |
|
|
elseif rat == 8 |
171 |
|
|
A=fread(fid,numels,'real*8'); |
172 |
|
|
else |
173 |
|
|
sprintf(' Implied size in meta-file = %d', numels ) |
174 |
|
|
sprintf(' Record size in data-file = %d', recsz ) |
175 |
|
|
error('Ratio between record size and size in meta-file inconsistent') |
176 |
|
|
end |
177 |
|
|
|
178 |
|
|
erecsz=fread(fid,1,'uint32'); |
179 |
|
|
if erecsz ~= recsz |
180 |
|
|
sprintf('WARNING: Record sizes at beginning and end of file are inconsistent') |
181 |
|
|
end |
182 |
|
|
|
183 |
|
|
fclose(fid); |
184 |
|
|
|
185 |
|
|
A=reshape(A,ldims); |
186 |
|
|
|
187 |
|
|
else |
188 |
|
|
% This is the new MDS format that uses direct access |
189 |
|
|
|
190 |
|
|
ldims=N(3,:)-N(2,:)+1; |
191 |
|
|
if dataprec == 'float32' |
192 |
|
|
A=myrdda(dname,ldims,1,'real*4',ieee); |
193 |
|
|
elseif dataprec == 'float64' |
194 |
|
|
A=myrdda(dname,ldims,1,'real*8',ieee); |
195 |
|
|
else |
196 |
|
|
error(['Unrecognized format in meta-file = ' format]); |
197 |
|
|
end |
198 |
|
|
|
199 |
|
|
end |
200 |
|
|
|
201 |
|
|
%------------------------------------------------------------------------------- |
202 |
|
|
|
203 |
|
|
% result = RDDA( file, dim, irec [options] ) |
204 |
|
|
% |
205 |
|
|
% This routine reads the irec'th record of shape 'dim' from the |
206 |
|
|
% direct-access binary file (float or double precision) 'file'. |
207 |
|
|
% |
208 |
|
|
% Required arguments: |
209 |
|
|
% |
210 |
|
|
% file - string - name of file to read from |
211 |
|
|
% dim - vector - dimensions of the file records and the resulting array |
212 |
|
|
% irec - integer - record number in file in which to write data |
213 |
|
|
% |
214 |
|
|
% Optional arguments (must appear after the required arguments): |
215 |
|
|
% prec - string - precision of storage in file. Default = 'real*8'. |
216 |
|
|
% ieee - string - IEEE bit-wise representation in file. Default = 'b'. |
217 |
|
|
% |
218 |
|
|
% 'prec' may take the values: |
219 |
|
|
% 'real*4' - floating point, 32 bits. |
220 |
|
|
% 'real*8' - floating point, 64 bits - the efault. |
221 |
|
|
% |
222 |
|
|
% 'ieee' may take values: |
223 |
|
|
% 'ieee-be' or 'b' - IEEE floating point with big-endian |
224 |
|
|
% byte ordering - the default |
225 |
|
|
% 'ieee-le' or 'l' - IEEE floating point with little-endian |
226 |
|
|
% byte ordering |
227 |
|
|
% 'native' or 'n' - local machine format |
228 |
|
|
% 'cray' or 'c' - Cray floating point with big-endian |
229 |
|
|
% byte ordering |
230 |
|
|
% 'ieee-le.l64' or 'a' - IEEE floating point with little-endian |
231 |
|
|
% byte ordering and 64 bit long data type |
232 |
|
|
% 'ieee-be.l64' or 's' - IEEE floating point with big-endian byte |
233 |
|
|
% ordering and 64 bit long data type. |
234 |
|
|
% |
235 |
|
|
% eg. T=rdda('t.data',[64 64 32],1); |
236 |
|
|
% T=rdda('t.data',[256],4,'real*4'); |
237 |
|
|
% T=rdda('t.data',[128 64],2,'real*4','b'); |
238 |
|
|
function [arr] = myrdda(file,N,k,varargin) |
239 |
|
|
|
240 |
|
|
% Defaults |
241 |
|
|
WORDLENGTH=8; |
242 |
|
|
rtype='real*8'; |
243 |
|
|
ieee='b'; |
244 |
|
|
|
245 |
|
|
% Check optional arguments |
246 |
|
|
args=char(varargin); |
247 |
|
|
while (size(args,1) > 0) |
248 |
|
|
if deblank(args(1,:)) == 'real*4' |
249 |
|
|
WORDLENGTH=4; |
250 |
|
|
rtype='real*4'; |
251 |
|
|
elseif deblank(args(1,:)) == 'real*8' |
252 |
|
|
WORDLENGTH=8; |
253 |
|
|
rtype='real*8'; |
254 |
|
|
elseif deblank(args(1,:)) == 'n' | deblank(args(1,:)) == 'native' |
255 |
|
|
ieee='n'; |
256 |
|
|
elseif deblank(args(1,:)) == 'l' | deblank(args(1,:)) == 'ieee-le' |
257 |
|
|
ieee='l'; |
258 |
|
|
elseif deblank(args(1,:)) == 'b' | deblank(args(1,:)) == 'ieee-be' |
259 |
|
|
ieee='b'; |
260 |
|
|
elseif deblank(args(1,:)) == 'c' | deblank(args(1,:)) == 'cray' |
261 |
|
|
ieee='c'; |
262 |
|
|
elseif deblank(args(1,:)) == 'a' | deblank(args(1,:)) == 'ieee-le.l64' |
263 |
|
|
ieee='a'; |
264 |
|
|
elseif deblank(args(1,:)) == 's' | deblank(args(1,:)) == 'ieee-be.l64' |
265 |
|
|
ieee='s'; |
266 |
|
|
else |
267 |
|
|
error(['Optional argument ' args(1,:) ' is unknown']) |
268 |
|
|
end |
269 |
|
|
args=args(2:end,:); |
270 |
|
|
end |
271 |
|
|
|
272 |
|
|
nnn=prod(N); |
273 |
|
|
|
274 |
|
|
[fid mess]=fopen(file,'r',ieee); |
275 |
|
|
if fid == -1 |
276 |
|
|
error('Error while opening file:\n%s',mess) |
277 |
|
|
end |
278 |
|
|
st=fseek(fid,nnn*(k-1)*WORDLENGTH,'bof'); |
279 |
|
|
if st ~= 0 |
280 |
|
|
mess=ferror(fid); |
281 |
|
|
error('There was an error while positioning the file pointer:\n%s',mess) |
282 |
|
|
end |
283 |
|
|
[arr count]=fread(fid,nnn,rtype); |
284 |
|
|
if count ~= nnn |
285 |
|
|
error('Not enough data was available to be read: off EOF?') |
286 |
|
|
end |
287 |
|
|
st=fclose(fid); |
288 |
|
|
arr=reshape(arr,N); |