/[MITgcm]/MITgcm/utils/matlab/rdmeta.m
ViewVC logotype

Contents of /MITgcm/utils/matlab/rdmeta.m

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download)
Wed May 26 23:19:40 1999 UTC (24 years, 9 months ago) by adcroft
Branch: MAIN
CVS Tags: checkpoint38, branch-atmos-merge-shapiro, checkpoint28, checkpoint29, checkpoint23, checkpoint24, checkpoint25, checkpoint27, branch-atmos-merge-freeze, branch-atmos-merge-start, checkpoint26, c37_adj, checkpoint39, checkpoint33, checkpoint32, checkpoint31, checkpoint30, checkpoint37, checkpoint36, checkpoint35, checkpoint34, branch-atmos-merge-zonalfilt, branch-atmos-merge-phase5, branch-atmos-merge-phase4, branch-atmos-merge-phase7, branch-atmos-merge-phase6, branch-atmos-merge-phase1, branch-atmos-merge-phase3, branch-atmos-merge-phase2
Branch point for: branch-atmos-merge, pre38
Added matlab and scripts directories:
 o matlab contains rdmeta.m and rdmds.m, scripts for reading model
   output directly into Matlab
 o scripts contains joinds, Zhangfan's post-processing script for
   joining processor-based model output
   and joinmds, a place-holder (ie. hack) post-processing script that
   joins tiled-based model output

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

  ViewVC Help
Powered by ViewVC 1.1.22