1 |
edhill |
1.1 |
function [AA] = rdmeta(fname,varargin) |
2 |
|
|
% |
3 |
|
|
% Read MITgcmUV Meta/Data files |
4 |
|
|
% |
5 |
|
|
% A = RDMETA(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.p0000.t0000.meta, T.0000002880.p0000.t0000.data |
10 |
|
|
% T.0000002880.p0001.t0000.meta, T.0000002880.p0001.t0000.data |
11 |
|
|
% T.0000002880.p0002.t0000.meta, T.0000002880.p0002.t0000.data |
12 |
|
|
% T.0000002880.p0003.t0000.meta, T.0000002880.p0003.t0000.data |
13 |
|
|
% use |
14 |
|
|
% >> A=rdmeta('T.0000002880'); |
15 |
|
|
% |
16 |
|
|
% A = RDMETA(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 |
|
|
% Default options |
35 |
|
|
ieee='n'; |
36 |
|
|
|
37 |
|
|
% Check optional arguments |
38 |
|
|
args=char(varargin); |
39 |
|
|
while (size(args,1) > 0) |
40 |
|
|
if deblank(args(1,:)) == 'n' | deblank(args(1,:)) == 'native' |
41 |
|
|
ieee='n'; |
42 |
|
|
elseif deblank(args(1,:)) == 'l' | deblank(args(1,:)) == 'ieee-le' |
43 |
|
|
ieee='l'; |
44 |
|
|
elseif deblank(args(1,:)) == 'b' | deblank(args(1,:)) == 'ieee-be' |
45 |
|
|
ieee='b'; |
46 |
|
|
elseif deblank(args(1,:)) == 'c' | deblank(args(1,:)) == 'cray' |
47 |
|
|
ieee='c'; |
48 |
|
|
elseif deblank(args(1,:)) == 'a' | deblank(args(1,:)) == 'ieee-le.l64' |
49 |
|
|
ieee='a'; |
50 |
|
|
elseif deblank(args(1,:)) == 's' | deblank(args(1,:)) == 'ieee-be.l64' |
51 |
|
|
ieee='s'; |
52 |
|
|
else |
53 |
|
|
sprintf(['Optional argument ' args(1,:) ' is unknown']) |
54 |
|
|
return |
55 |
|
|
end |
56 |
|
|
args=args(2:end,:); |
57 |
|
|
end |
58 |
|
|
|
59 |
|
|
% Match name of all meta-files |
60 |
|
|
eval(['ls ' fname '*.meta;']); |
61 |
|
|
allfiles=ans; |
62 |
|
|
|
63 |
|
|
% Beginning and end of strings |
64 |
|
|
Iend=findstr(allfiles,'.meta')+4; |
65 |
|
|
Ibeg=[1 Iend(1:end-1)+2]; |
66 |
|
|
|
67 |
|
|
% Loop through allfiles |
68 |
|
|
for j=1:prod(size(Ibeg)), |
69 |
|
|
|
70 |
|
|
% Read meta- and data-file |
71 |
|
|
[A,N] = localrdmeta(allfiles(Ibeg(j):Iend(j)),ieee); |
72 |
|
|
|
73 |
|
|
bdims=N(1,:); |
74 |
|
|
r0=N(2,:); |
75 |
|
|
rN=N(3,:); |
76 |
|
|
ndims=prod(size(bdims)); |
77 |
|
|
if (ndims == 1) |
78 |
|
|
AA(r0(1):rN(1))=A; |
79 |
|
|
elseif (ndims == 2) |
80 |
|
|
AA(r0(1):rN(1),r0(2):rN(2))=A; |
81 |
|
|
elseif (ndims == 3) |
82 |
|
|
AA(r0(1):rN(1),r0(2):rN(2),r0(3):rN(3))=A; |
83 |
|
|
elseif (ndims == 4) |
84 |
|
|
AA(r0(1):rN(1),r0(2):rN(2),r0(3):rN(3),r0(4):rN(4))=A; |
85 |
|
|
else |
86 |
|
|
sprintf('Dimension of data set is larger than currently coded. Sorry!') |
87 |
|
|
return |
88 |
|
|
end |
89 |
|
|
|
90 |
|
|
end |
91 |
|
|
|
92 |
|
|
%------------------------------------------------------------------------------- |
93 |
|
|
|
94 |
|
|
function [A,N] = localrdmeta(fname,ieee) |
95 |
|
|
|
96 |
|
|
mname=fname; |
97 |
|
|
dname=strrep(mname,'.meta','.data'); |
98 |
|
|
|
99 |
|
|
% Read and interpret Meta file |
100 |
|
|
fid = fopen(mname,'r'); |
101 |
|
|
if (fid == -1) |
102 |
|
|
sprintf(['Fila e' mname ' could not be opened']) |
103 |
|
|
return |
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 |
|
|
ind=findstr([line ')'],')'); line=line(ind(1)+1:end); |
116 |
|
|
% Remove comments of form // |
117 |
|
|
line=[line ' //']; ind=findstr(line,'//'); line=line(1:ind(1)-1); |
118 |
|
|
% Add to total string |
119 |
|
|
allstr=[allstr line]; |
120 |
|
|
end |
121 |
|
|
end |
122 |
|
|
|
123 |
|
|
% Close meta file |
124 |
|
|
fclose(fid); |
125 |
|
|
|
126 |
|
|
% Strip out comments of form /* ... */ |
127 |
|
|
ind1=findstr(allstr,'/*'); ind2=findstr(allstr,'*/'); |
128 |
|
|
if size(ind1) ~= size(ind2) |
129 |
|
|
sprintf('The /* ... */ comments are not properly paired') |
130 |
|
|
return |
131 |
|
|
end |
132 |
|
|
while size(ind1,2) > 0 |
133 |
|
|
allstr=[allstr(1:ind1(1)-1) allstr(ind2(1)+3:end)]; |
134 |
|
|
ind1=findstr(allstr,'/*'); ind2=findstr(allstr,'*/'); |
135 |
|
|
end |
136 |
|
|
|
137 |
|
|
eval(lower(allstr)); |
138 |
|
|
|
139 |
|
|
N=reshape( dimlist , 3 , prod(size(dimlist))/3 ); |
140 |
|
|
|
141 |
|
|
A=allstr; |
142 |
|
|
% Open data file |
143 |
|
|
fid=fopen(dname,'r',ieee); |
144 |
|
|
|
145 |
|
|
% Read record size in bytes |
146 |
|
|
recsz=fread(fid,1,'uint32'); |
147 |
|
|
ldims=N(3,:)-N(2,:)+1; |
148 |
|
|
numels=prod(ldims); |
149 |
|
|
|
150 |
|
|
rat=recsz/numels; |
151 |
|
|
if rat == 4 |
152 |
|
|
A=fread(fid,numels,'real*4'); |
153 |
|
|
elseif rat == 8 |
154 |
|
|
A=fread(fid,numels,'real*8'); |
155 |
|
|
else |
156 |
|
|
sprintf('Ratio between record size and size in meta-file inconsistent') |
157 |
|
|
sprintf(' Implied size in meta-file = %d', numels ) |
158 |
|
|
sprintf(' Record size in data-file = %d', recsz ) |
159 |
|
|
return |
160 |
|
|
end |
161 |
|
|
|
162 |
|
|
erecsz=fread(fid,1,'uint32'); |
163 |
|
|
if erecsz ~= recsz |
164 |
|
|
sprintf('WARNING: Record sizes at beginning and end of file are inconsistent') |
165 |
|
|
end |
166 |
|
|
|
167 |
|
|
fclose(fid); |
168 |
|
|
|
169 |
|
|
A=reshape(A,ldims); |