/[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.6 - (show annotations) (download)
Tue Aug 28 17:58:49 2001 UTC (22 years, 9 months ago) by adcroft
Branch: MAIN
Changes since 1.5: +2 -2 lines
Added missing ;

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

  ViewVC Help
Powered by ViewVC 1.1.22