/[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.8 - (show annotations) (download)
Mon Sep 10 14:09:42 2001 UTC (23 years, 10 months ago) by adcroft
Branch: MAIN
CVS Tags: checkpoint40pre9, release1_b1, checkpoint43, ecco-branch-mod1, release1_beta1, checkpoint42, checkpoint40, checkpoint41
Branch point for: release1, ecco-branch, release1_coupled
Changes since 1.7: +2 -2 lines
Type in string for listing all relevent files; was broken for global files.

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

  ViewVC Help
Powered by ViewVC 1.1.22