/[MITgcm]/MITgcm_contrib/high_res_cube/matlab-grid-generator/bin/rdmds.m
ViewVC logotype

Annotation of /MITgcm_contrib/high_res_cube/matlab-grid-generator/bin/rdmds.m

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


Revision 1.1.1.1 - (hide annotations) (download) (vendor branch)
Tue Nov 11 18:08:08 2003 UTC (23 years ago) by cnh
Branch: MAIN, initial
CVS Tags: baseline, HEAD
Changes since 1.1: +0 -0 lines
Checking in work done with Dimitri on high-resolution cube gridding and parallel 
communications. 
   o code is in a contrib experiment for now so we can continue collaborating
     on it. However most code is general and will be moved into main branch once 
     it is fully hardened.
   o There are README files in the contrib root and in the subdirectories that
     explain the contents

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

  ViewVC Help
Powered by ViewVC 1.1.22