function hipswrite(image,save_file,format)
% HIPSWRITE
% 	hipswrite(image,save_file)
% 	Save a matrix as a floating point HIPS image.
% 	This program generates temporary files named _image_save_.*

RawImage=tempnam;
SaveScript=tempnam;

% Avoids flipping/rotation by saving
image=flipud(rot90(image));

% Saves the image
fid=fopen(RawImage,'w');
fwrite(fid,image,'float');
fclose(fid);

Conversion='';
if exist('format')==1
  if strcmp(lower(format),'byte')
    Conversion='| htob';
  elseif strcmp(lower(format),'int')
    Conversion='| htoi';
  else
    error(sprintf('Unknown format "%s"',format));
  end
end

% Create a shell script file for writing the saved image
fid=fopen(SaveScript,'w');
fprintf(fid,'raw2hips -float -s %d %d < %s %s > %s\n', ...
	size(image,2), size(image,1), RawImage, Conversion, save_file);
fclose(fid);

% Execute the shell script and clean up
eval(sprintf('!sh %s',SaveScript));
eval(sprintf('!rm %s %s',SaveScript,RawImage));
