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

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

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


Revision 1.12 - (show annotations) (download)
Tue Nov 27 19:21:56 2001 UTC (22 years, 7 months ago) by adcroft
Branch: MAIN
CVS Tags: checkpoint44e_post, checkpoint44f_post, checkpoint43a-release1mods, chkpt44d_post, checkpoint44e_pre, release1-branch_tutorials, chkpt44a_post, checkpoint44h_pre, chkpt44c_pre, checkpoint45a_post, checkpoint44g_post, checkpoint45b_post, release1-branch-end, release1_final_v1, checkpoint44b_post, checkpoint45c_post, checkpoint44h_post, chkpt44a_pre, checkpoint44b_pre, checkpoint44, checkpoint45, chkpt44c_post, checkpoint44f_pre, release1-branch_branchpoint
Branch point for: release1_final, release1-branch
Changes since 1.11: +3 -3 lines
removed debugging

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

  ViewVC Help
Powered by ViewVC 1.1.22