source: issm/trunk/src/m/model/parseresultsfromdisk.m@ 9381

Last change on this file since 9381 was 9381, checked in by Mathieu Morlighem, 14 years ago

removed some leaks in C and fixed marshall and parseresult for new IoModel interface

File size: 4.7 KB
Line 
1function results=parseresultsfromdisk(filename,iosplit)
2%PARSERESULTSFROMDISK - ...
3%
4% Usage:
5% results=parseresultsfromdisk(filename,iosplit)
6
7if iosplit,
8 results=parseresultsfromdiskiosplit(filename);
9else
10 results=parseresultsfromdiskioserial(filename);
11end
12
13%process patch if necessary
14results=MatlabProcessPatch(results);
15
16function results=parseresultsfromdiskioserial(filename) % {{{
17%PARSERESULTSFROMDISK - ...
18%
19% Usage:
20% results=parseresultsfromdiskioserial(filename)
21
22
23%Open file
24fid=fopen(filename,'rb');
25if(fid==-1),
26 error(['loadresultsfromdisk error message: could not open ',filename,' for binary reading']);
27end
28results=struct();
29
30%Read fields until the end of the file.
31result=ReadData(fid);
32while ~isempty(result),
33 %Get time and step
34 results(result.step).step=result.step;
35 results(result.step).time=result.time;
36
37 %Add result
38 if (length(results)>=result.step & isfield(results,result.fieldname) & ~strcmp(result.fieldname,'SolutionType')),
39 results(result.step).(result.fieldname)=[ results(result.step).(result.fieldname); result.field];
40 else
41 results(result.step).(result.fieldname)=result.field;
42 end
43
44 %read next result
45 result=ReadData(fid);
46
47end
48
49fclose(fid);
50% }}}
51function results=parseresultsfromdiskiosplit(filename) % {{{
52%PARSERESULTSFROMDISKIOSPLIT - ...
53%
54% Usage:
55% results=parseresultsfromdiskiosplit(filename)
56
57
58%Open file
59fid=fopen(filename,'rb');
60if(fid==-1),
61 error(['loadresultsfromdisk error message: could not open ',filename,' for binary reading']);
62end
63results=struct();
64
65%if we have done split I/O, ie, we have results that are fragmented across patches,
66%do a first pass, and figure out the structure of results
67result=ReadDataDimensions(fid);
68while ~isempty(result),
69
70 %Get time and step
71 results(result.step).step=result.step;
72 results(result.step).time=result.time;
73
74 %Add result
75 if strcmpi(result.fieldname,'Patch'),
76 results(result.step).(result.fieldname)=[0 result.N];
77 else
78 results(result.step).(result.fieldname)=NaN;
79 end
80
81 %read next result
82 result=ReadDataDimensions(fid);
83end
84
85%do a second pass, and figure out the size of the patches
86fseek(fid,0,-1); %rewind
87result=ReadDataDimensions(fid);
88while ~isempty(result),
89
90 %Add result
91 if strcmpi(result.fieldname,'Patch'),
92 patchdimensions=results(result.step).(result.fieldname);
93 results(result.step).(result.fieldname)=[patchdimensions(1)+result.M result.N];
94 end
95
96 %read next result
97 result=ReadDataDimensions(fid);
98end
99
100%allocate patches
101for i=1:length(results),
102 results(i).Patch=zeros(results(i).Patch(1),results(i).Patch(2));
103 results(i).counter=1; %use to index into the patch
104end
105
106%third pass, this time to read the real information
107fseek(fid,0,-1); %rewind
108result=ReadData(fid);
109while ~isempty(result),
110
111 %Get time and step
112 results(result.step).step=result.step;
113 results(result.step).time=result.time;
114
115 %Add result
116 if strcmpi(result.fieldname,'Patch'),
117 counter=results(result.step).counter;
118 counter2=counter+size(result.field,1)-1;
119 results(result.step).(result.fieldname)(counter:counter2,:)=result.field;
120
121 %increment counter:
122 results(result.step).counter=counter2+1;
123 else
124 results(result.step).(result.fieldname)=result.field;
125 end
126
127 %read next result
128 result=ReadData(fid);
129
130end
131
132%close file
133fclose(fid);
134 % }}}
135function result=ReadData(fid) % {{{
136%READDATA - ...
137%
138% Usage:
139% field=ReadData(fid)
140
141
142%read field
143[length,count]=fread(fid,1,'int');
144
145if count==0,
146 result=struct([]);
147else
148 fieldname=fread(fid,length,'char');
149 fieldname=fieldname(1:end-1)';
150 fieldname=char(fieldname);
151 time=fread(fid,1,'double');
152 step=fread(fid,1,'int');
153
154 type=fread(fid,1,'int');
155 M=fread(fid,1,'int');
156 if type==1,
157 field=fread(fid,M,'double');
158 elseif type==2,
159 field=fread(fid,M,'char');
160 field=char(field(1:end-1)');
161 elseif type==3,
162 N=fread(fid,1,'int');
163 field=transpose(fread(fid,[N M],'double'));
164 else
165 error(['cannot read data of type ' num2str(type) ]);
166 end
167
168 result.fieldname=fieldname;
169 result.time=time;
170 result.step=step;
171 result.field=field;
172end
173% }}}
174function result=ReadDataDimensions(fid) % {{{
175%READDATA - read data dimensions, step and time, but not the data itself.
176%
177% Usage:
178% field=ReadDataDimensions(fid)
179
180
181%read field
182[length,count]=fread(fid,1,'int');
183
184if count==0,
185 result=struct([]);
186else
187 fieldname=fread(fid,length,'char');
188 fieldname=fieldname(1:end-1)';
189 fieldname=char(fieldname);
190 time=fread(fid,1,'double');
191 step=fread(fid,1,'int');
192
193 type=fread(fid,1,'int');
194 M=fread(fid,1,'int');
195 N=1; %default
196 if type==1,
197 fseek(fid,M*8,0);
198 elseif type==2,
199 fseek(fid,M,0);
200 elseif type==3,
201 N=fread(fid,1,'int');
202 fseek(fid,N*M*8,0);
203 else
204 error(['cannot read data of type ' num2str(type) ]);
205 end
206
207 result.fieldname=fieldname;
208 result.time=time;
209 result.step=step;
210 result.M=M;
211 result.N=N;
212end
213% }}}
Note: See TracBrowser for help on using the repository browser.