
function A=avsread(name)
% AVSread(filename) - reads in AVS files
%    i.e.: A = avsread('test.fld');

% Web Stayman - July, 1997
%  - really doesn't handle more than a few cases
%  - but structure is there for much more...

if (~exist('name')) disp('Usage: A = avsread(filename);'); break; end

fid = fopen(name,'r','ieee-be');

header=1;

while header,
	F = fread(fid,1);
	s = '';

	if (F==12) break; end
	if (F==35)
		while (fread(fid,1)~=10); end
	else
		while (1)
			s=[s setstr(F)];
			F = fread(fid,1);
			if (F==61) break; end
		end
		param=s; s='';
		while (1)
			F = fread(fid,1);
			if (F==10) break; end
			s=[s setstr(F)];
		end
		val=s;

		nval=str2num(val);
		if (length(nval)>0)
			eval([param ' = ' val ';']);
		else
			eval([param ' =  '''val ''';']);
		end
	end
end

F = fread(fid,1);

% --- Data portion of file ---

switch data
	case 'xdr_float', type='float32';
	case 'short_sun', type='short';
	case 'short',	type='short';
	case 'double', type='double';
	otherwise, disp('Unknown data type'); break;
end

switch ndim
	case 1, A = fread(fid,dim1,type);
	case 2, A = fread(fid,[dim1,dim2],type);
	case 3, A = reshape(fread(fid,[dim1,dim2*dim3],type),dim1,dim2,dim3);
	case 4, A = reshape(fread(fid,[dim1,dim2*dim3*dim4],type),dim1,dim2,dim3,dim4);
	otherwise, disp('ndim >4 or <1 - write more avsread...'); break;
end

