function hmeWrite (hme,fname)
% hmeWrite (hme,fname)
%
% Write a HME model to a file.
%
% INPUTS
%	hme	HME model.
%	fname	File name (string).
%
% OUTPUTS
%	none
%
% See also hme.
%
% David Martin <dmartin@eecs.berkeley.edu>
% May 2002

% Copyright (C) 2002 David R. Martin <dmartin@eecs.berkeley.edu>
%
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License as
% published by the Free Software Foundation; either version 2 of the
% License, or (at your option) any later version.
% 
% This program is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
% General Public License for more details.
% 
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
% 02111-1307, USA, or see http://www.gnu.org/copyleft/gpl.html.

error(nargchk(2,2,nargin));

[fid,msg] = fopen(fname,'w');
if fid == -1, error(msg); end
hmeWriteTree(hme,fid);
fclose(fid);

function hmeWriteTree(hme,fid)
  fwrite(fid,hme.leaf,'int32');
  fwrite(fid,size(hme.param),'int32');
  fwrite(fid,hme.param,'double');
  if ~hme.leaf,
    fwrite(fid,length(hme.children),'int32');
    for i = 1:length(hme.children),
      hmeWriteTree(hme.children{i},fid);
    end
  end
