1 
function fld=readbin(fnam,siz,typ,prec) 
2 

3 
% Function fld=readbin(fnam,siz,typ,prec) 
4 
% read in ND binary field 
5 
% 
6 
% INPUTS 
7 
% fnam input path and file name 
8 
% siz grid dimension (default [360 224 46]) 
9 
% typ 0: sequential FORTRAN (default); 1: plain binary 
10 
% prec numeric precision (default 'real*4') 
11 
% 
12 
% OUTPUTS 
13 
% fld output array of dimension siz 
14 
% 
15 
% SEE ALSO 
16 
% writebin 
17 

18 
if nargin<4, prec='real*4'; end 
19 
if nargin<3, typ=0; end 
20 
if nargin<2, siz=[360 224 46]; end 
21 
if nargin<1, t=1; end 
22 
if nargin<0, error('please specify input file name'); end 
23 

24 
fid=fopen(fnam,'r','ieeebe'); 
25 
switch typ 
26 
case 0 
27 
tmp=read_record(fid,prec); 
28 
case 1 
29 
tmp=fread(fid,[siz(1),prod(siz(2:length(siz)))],prec); 
30 
end 
31 
fid=fclose(fid); 
32 

33 
switch length(siz) 
34 
case 2 
35 
fld=reshape(tmp,siz(1),siz(2)); 
36 
case 3 
37 
fld=reshape(tmp,siz(1),siz(2),siz(3)); 
38 
case 4 
39 
fld=reshape(tmp,siz(1),siz(2),siz(3),siz(4)); 
40 
case 5 
41 
fld=reshape(tmp,siz(1),siz(2),siz(3),siz(4),siz(5)); 
42 
otherwise 
43 
fld=tmp; 
44 
end 