utilities
[wrf-fire-matlab.git] / util1_jan / private / read_rec.m
blob84ec3307d9cd77f99e9860cd8f0abe1347fe5ba6
1 function [varargout]=read_rec(fid,n,types)
2 if n == 0,
3         n = nargout;
4 end
5 if ischar(types),
6         type=types;
7         begbytes=n*type2len(type);
8 else
9         items=length(types);
10         if items ~= n, error('bad number of types'), end
11         ilen=zeros(items,1);
12         for  i=1:n,
13                 ilen(i)=type2len(types{i});
14         end
15         begbytes=sum(ilen);
16 end
17         
18 bytes=fread(fid,1,'int');
19 if(bytes ~= begbytes),
20         error(sprintf('record length is %i instead of %i',bytes,begbytes))
21 end
22 if ischar(types),
23         rec=fread(fid,n,type);
24 else
25         for i=1:n,
26                 rec(i)=fread(fid,1,types{i});
27         end
28 end
29 bytend=fread(fid,1,'int');
30 if bytes ~= bytend,
31         error(sprintf('end byte count %i instead of %i',bytend,bytes))
32 end
33 if nargout == 1,
34         varargout{1}=rec;
35 else
36         if nargout == n,
37                 for i=1:n,
38                         varargout{i}=rec(i);
39                 end
40         else
41                 error(sprintf('record items %i should be %i\n',n,nargout))
42         end
43 end
44 return
46 function len=type2len(type)
47 if strcmp(type,'int'),
48         len=4;
49 elseif strcmp(type,'double'),
50         len = 8;
51 else
52         error('bad type')
53 end
54 return