
function avswrite(name,dat)
% avswrite(filename,data) - Quickie avs file dumper...
%   i.e.: avswrite('test.fld',A);
% Currently hardwired to veclen=1,data=xdr_float,field=uniform

% Web Stayman - June, 1997
%    quick hack, could make this much nicer...

if ~exist('name','var')|~exist('dat','var'),
	disp('Usage: avswrite(filename,data)'); break
end

fid = fopen(name,'r');
if fid~=-1,
	yn='';
	while ~strncmp('y',yn,1)&~strncmp('n',yn,1),
		yn=input(sprintf('Overwrite ''%s''? (y/n)',name),'s');
	end

	if strncmp('n',yn,1),
		break
	end
	[s,w]=unix(sprintf('rm %s',name));
	if s~=0,
		disp('Cannot overwrite!'); break;
	end

	while 1,
		pause(1);
		fid = fopen(name,'r');
		if fid==-1, break; end
		fclose(fid);
	end
end

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

fprintf(fid,'# AVS field file\n');

% Dimension of Data
sz = size(dat);
ndim = length(sz);
fprintf(fid,'ndim=%d\n',ndim);

for nn = 1:ndim
  fprintf(fid,'dim%d=%d\n',nn,sz(nn));
end

fprintf(fid,'nspace=%d\nveclen=1\ndata=xdr_float\nfield=uniform\n\f\f',ndim);

fwrite(fid,dat,'float');

fclose(fid);

disp(sprintf('Wrote file ''%s''',name));
