/[MITgcm]/MITgcm/verification/tutorial_global_oce_latlon/diags_matlab/rdmeta.m
ViewVC logotype

Contents of /MITgcm/verification/tutorial_global_oce_latlon/diags_matlab/rdmeta.m

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


Revision 1.4 - (show annotations) (download)
Mon Apr 25 18:20:40 2011 UTC (13 years ago) by jmc
Branch: MAIN
CVS Tags: checkpoint64y, checkpoint64x, checkpoint64z, checkpoint64q, checkpoint64p, checkpoint64s, checkpoint64r, checkpoint64u, checkpoint64t, checkpoint64w, checkpoint64v, checkpoint64i, checkpoint64h, checkpoint64k, checkpoint64j, checkpoint64m, checkpoint64l, checkpoint64o, checkpoint64n, checkpoint64a, checkpoint64c, checkpoint64b, checkpoint64e, checkpoint64d, checkpoint64g, checkpoint64f, checkpoint63p, checkpoint63q, checkpoint63r, checkpoint63s, checkpoint63l, checkpoint63m, checkpoint63n, checkpoint63o, checkpoint63h, checkpoint63i, checkpoint63j, checkpoint63k, checkpoint63d, checkpoint63e, checkpoint63f, checkpoint63g, checkpoint63a, checkpoint63b, checkpoint63c, checkpoint64, checkpoint65, checkpoint63, checkpoint66g, checkpoint66f, checkpoint66e, checkpoint66d, checkpoint66c, checkpoint66b, checkpoint66a, checkpoint66o, checkpoint66n, checkpoint66m, checkpoint66l, checkpoint66k, checkpoint66j, checkpoint66i, checkpoint66h, checkpoint65z, checkpoint65x, checkpoint65y, checkpoint65r, checkpoint65s, checkpoint65p, checkpoint65q, checkpoint65v, checkpoint65w, checkpoint65t, checkpoint65u, checkpoint65j, checkpoint65k, checkpoint65h, checkpoint65i, checkpoint65n, checkpoint65o, checkpoint65l, checkpoint65m, checkpoint65b, checkpoint65c, checkpoint65a, checkpoint65f, checkpoint65g, checkpoint65d, checkpoint65e, checkpoint62w, checkpoint62z, checkpoint62y, checkpoint62x, HEAD
Changes since 1.3: +2 -3 lines
up-to-date version from MITgcm/utils/matlab

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

  ViewVC Help
Powered by ViewVC 1.1.22