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

Annotation of /MITgcm/utils/matlab/rdmeta.m

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


Revision 1.2 - (hide annotations) (download)
Tue May 29 14:01:41 2001 UTC (22 years, 10 months ago) by adcroft
Branch: MAIN
CVS Tags: checkpoint46n_post, checkpoint51k_post, checkpoint47e_post, checkpoint57m_post, checkpoint52l_pre, checkpoint44e_post, hrcube4, hrcube5, checkpoint46l_post, checkpoint57g_pre, checkpoint46g_pre, checkpoint47c_post, release1_p13_pre, checkpoint50c_post, checkpoint57s_post, checkpoint58b_post, checkpoint57b_post, checkpoint46f_post, checkpoint52d_pre, checkpoint57g_post, checkpoint48e_post, checkpoint56b_post, checkpoint50c_pre, checkpoint57y_post, checkpoint44f_post, checkpoint46b_post, checkpoint52j_pre, checkpoint43a-release1mods, checkpoint51o_pre, checkpoint54d_post, checkpoint54e_post, ecco_c50_e32, ecco_c50_e33, ecco_c50_e30, ecco_c50_e31, release1_p13, checkpoint40pre3, checkpoint40pre1, checkpoint51l_post, checkpoint40pre7, checkpoint40pre6, checkpoint48i_post, checkpoint57r_post, checkpoint40pre9, checkpoint40pre8, checkpoint46l_pre, checkpoint57d_post, checkpoint57i_post, checkpoint52l_post, checkpoint52k_post, chkpt44d_post, checkpoint58, checkpoint55, checkpoint54, checkpoint57, checkpoint56, checkpoint51, checkpoint50, checkpoint53, checkpoint52, release1_p8, release1_p9, checkpoint50d_post, release1_p1, release1_p2, release1_p3, release1_p4, release1_p5, release1_p6, release1_p7, checkpoint58f_post, checkpoint52f_post, checkpoint57n_post, checkpoint58d_post, checkpoint58a_post, checkpoint50b_pre, checkpoint44e_pre, checkpoint57z_post, checkpoint54f_post, checkpoint51f_post, release1_b1, ecco_c51_e34d, ecco_c51_e34e, ecco_c51_e34f, ecco_c51_e34g, ecco_c51_e34a, ecco_c51_e34b, ecco_c51_e34c, checkpoint48b_post, checkpoint43, checkpoint51d_post, checkpoint48c_pre, checkpoint47d_pre, checkpoint51t_post, checkpoint58t_post, checkpoint51n_post, release1_chkpt44d_post, checkpoint55i_post, checkpoint58m_post, checkpoint57l_post, checkpoint52i_pre, hrcube_1, hrcube_2, hrcube_3, checkpoint51s_post, checkpoint47a_post, checkpoint57t_post, checkpoint55c_post, checkpoint48d_pre, checkpoint51j_post, checkpoint47i_post, checkpoint52e_pre, checkpoint57v_post, checkpoint57f_post, checkpoint52e_post, checkpoint51n_pre, checkpoint47d_post, icebear5, icebear4, icebear3, icebear2, checkpoint53d_post, checkpoint46d_pre, checkpoint40pre2, checkpoint57a_post, checkpoint48d_post, release1-branch_tutorials, checkpoint57h_pre, checkpoint48f_post, checkpoint45d_post, checkpoint52b_pre, checkpoint54b_post, checkpoint46j_pre, checkpoint57h_post, checkpoint51l_pre, checkpoint52m_post, checkpoint57y_pre, chkpt44a_post, checkpoint55g_post, checkpoint44h_pre, checkpoint48h_post, checkpoint51q_post, checkpoint40pre4, ecco_c50_e29, checkpoint51b_pre, checkpoint46a_post, checkpoint47g_post, checkpoint52b_post, checkpoint52c_post, checkpoint46j_post, checkpoint51h_pre, checkpoint46k_post, ecco_c50_e28, chkpt44c_pre, checkpoint58o_post, checkpoint48a_post, checkpoint45a_post, checkpoint57c_post, checkpoint50f_post, checkpoint50a_post, checkpoint50f_pre, checkpoint58p_post, checkpoint58q_post, checkpoint52f_pre, checkpoint55d_post, ecco_c44_e19, ecco_c44_e18, ecco_c44_e17, ecco_c44_e16, release1_p12, checkpoint58e_post, release1_p10, release1_p11, release1_p16, release1_p17, release1_p14, release1_p15, checkpoint47j_post, checkpoint54a_pre, ecco_c50_e33a, checkpoint53c_post, checkpoint55d_pre, checkpoint57c_pre, checkpoint58r_post, checkpoint55j_post, branch-exfmods-tag, checkpoint44g_post, branchpoint-genmake2, checkpoint54a_post, checkpoint46e_pre, checkpoint55h_post, checkpoint58n_post, checkpoint51r_post, checkpoint48c_post, checkpoint45b_post, checkpoint46b_pre, checkpoint51i_post, checkpoint57e_post, release1-branch-end, release1_final_v1, checkpoint55b_post, checkpoint51b_post, checkpoint51c_post, checkpoint46c_pre, checkpoint53a_post, checkpoint46, checkpoint47b_post, checkpoint44b_post, checkpoint55f_post, ecco_c51_e34, checkpoint46h_pre, checkpoint52d_post, checkpoint53g_post, checkpoint46m_post, checkpoint57p_post, checkpint57u_post, checkpoint46a_pre, checkpoint50g_post, checkpoint45c_post, checkpoint57q_post, ecco_ice2, ecco_ice1, checkpoint44h_post, eckpoint57e_pre, checkpoint46g_post, release1_p12_pre, checkpoint58k_post, checkpoint52a_pre, ecco_c44_e22, checkpoint58v_post, checkpoint50h_post, checkpoint52i_post, checkpoint50e_pre, checkpoint50i_post, ecco_c44_e25, checkpoint51i_pre, checkpoint52h_pre, checkpoint56a_post, checkpoint58l_post, checkpoint53f_post, checkpoint40pre5, checkpoint57h_done, checkpoint52j_post, checkpoint47f_post, checkpoint50e_post, chkpt44a_pre, checkpoint46i_post, ecco_c44_e23, ecco_c44_e20, ecco_c44_e21, ecco_c44_e26, ecco_c44_e27, ecco_c44_e24, checkpoint57j_post, checkpoint57f_pre, checkpoint46c_post, checkpoint58g_post, ecco-branch-mod1, ecco-branch-mod2, ecco-branch-mod3, ecco-branch-mod4, ecco-branch-mod5, branch-netcdf, checkpoint50d_pre, checkpoint52n_post, checkpoint53b_pre, checkpoint46e_post, release1_beta1, checkpoint58h_post, checkpoint56c_post, checkpoint58j_post, checkpoint51e_post, checkpoint44b_pre, checkpoint42, checkpoint57a_pre, checkpoint40, checkpoint41, checkpoint55a_post, checkpoint47, checkpoint44, checkpoint45, checkpoint48, checkpoint49, checkpoint57o_post, checkpoint46h_post, checkpoint51o_post, checkpoint57k_post, checkpoint51f_pre, chkpt44c_post, checkpoint48g_post, checkpoint53b_post, checkpoint47h_post, checkpoint52a_post, checkpoint57w_post, checkpoint44f_pre, checkpoint58i_post, checkpoint51g_post, ecco_c52_e35, checkpoint57x_post, checkpoint46d_post, checkpoint50b_post, checkpoint58c_post, checkpoint58u_post, release1-branch_branchpoint, checkpoint51m_post, checkpoint53d_pre, checkpoint58s_post, checkpoint55e_post, checkpoint54c_post, checkpoint51a_post, checkpoint51p_post, checkpoint51u_post
Branch point for: c24_e25_ice, branch-exfmods-curt, release1_final, release1-branch, branch-genmake2, release1, branch-nonh, tg2-branch, ecco-branch, release1_50yr, netcdf-sm0, icebear, checkpoint51n_branch, release1_coupled
Changes since 1.1: +1 -0 lines
Merge from branch pre38:
 o essential mods for cubed sphere
 o debugged atmosphere, dynamcis + physics (aim)
 o new packages (mom_vecinv, mom_fluxform, ...)

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

  ViewVC Help
Powered by ViewVC 1.1.22