%saves matrix across rows into a datfile,              Steve Mann 1992
%
%Examples:
%
% savedat('betty',A)         % defaults to interval 0..254, myNaN=255
% savedat('betty',A,'uchar')
%
%Types: char, schar,  short,  int,  long,  float, double,
%       uchar,        ushort, uint, ulong,
%       float32, float64, int8, int16, int32,
%       intN, uintN                               where 1 <= N <= 32
%
%overwrites file if already exists

function mycountmaybereturned = fprint_and_fwrite(myfilename,A,mytype);

if nargin==3
  % removed list of `if' statements; replaced with call to szummer's s
  table_in = [ 
 'char          '
 'char*1        '
 'uchar         '
 'unsigned char '
 'schar         '
 'signed char   '
 'short         '
 'ushort        '
 'unsigned short'
 'int           '
 'uint          '
 'unsigned int  '
 'long          '
 'ulong         '
 'unsigned long '
 'float         '
 'float32       '
 'real*4        '
 'double        '
 'float64       '
 'real*8        '  
 'int8          '
 'integer*1     '
 'int16         '
 'integer*2     '
 'int32         '
 'integer*4     '
 ];

  table_out = [
    'unsigned_1'  
    'unsigned_1'  
    'unsigned_1'  
    'unsigned_1'  
    'signed_1  '  
    'signed_1  '  
    'signed_2  '  
    'unsigned_2'  
    'unsigned_2'  
    'signed_4  '  
    'unsigned_4'  
    'unsigned_4'  
    'signed_4  '  
    'unsigned_4'  
    'unsigned_4'  
    'float     '  
    'float     '  
    'float     '  
    'double    '  
    'double    '  
    'double    '  
    'signed_1  '  
    'signed_1  '  
    'signed_2  '  
    'signed_2  '  
    'signed_4  '  
    'signed_4  '];

  if strcmp(mytype, 'char')
    disp('savedat: datfiles don''t know how to deal with ``char'''', so saving as ``uchar''''') 
    mytype = 'uchar'
  end

  table_out_entry = translate_str(table_in,table_out,mytype); % mytype in table_in

  if strcmp(mytype, 'integer*8') %      NOT SUPPORTED BY DAT!
    error('savedat: integer*8 not supported by dat')
  end%if
end%if

if nargin==2
  disp('savedat: data type unspecified; rounding to 0..254, setting NaN to 255')
  A = chnan255(A);
  mytype='uchar';
  table_out_entry = 'unsigned_1';
end%if

answer=input(sprintf('would you like to delete %s to continue?  ',myfilename),'s');
  if ~(strcmp(answer,'y') | strcmp(answer,'Y') | strcmp(answer,'yes'))
  dis(  'savedat: you decided not to delete %s',myfilename)
  error('       : thus I am exiting')
end%if

eval(sprintf('!rm -r %s',myfilename))
eval(sprintf('!mkdir %s',myfilename))

eval(sprintf('mycount=saveraw(''%s/data'',A,mytype)',myfilename))

eval(sprintf('myintfid=fopen(''%s/descriptor'',''w'');',myfilename))

[M,N]=size(A);

fprintf(myintfid,'(_data_files 1)\n');
fprintf(myintfid,'(_data_sets 1)\n');
fprintf(myintfid,'(_channels 1)\n');
fprintf(myintfid,'(_dimensions %d %d)\n',M,N);
% fprintf(myintfid,'(_data_type unsigned_1)\n');

fprintf(myintfid,'(_data_type %s)\n',table_out_entry);
if strcmp(mytype,'float')
  disp('savedat: assuming float_rep is IEEE and byte_order is msb')
  fprintf(myintfid,'(_float_rep ieee)\n');
  fprintf(myintfid,'(_byte_order msb)\n');
end%if
clockdat=fix(clock);
fprintf(myintfid,'(_comment generated on %s; %dh%d; %d, from...)\n',date,clockdat(4:6));
fprintf(myintfid,'(_comment Matlab savedat, comments to steve@media.mit.edu)\n');

fclose_return=fclose(myintfid);   % returns zero if ok, -1 if not
if fclose_return ~= 0
  disp('savedat: fclose returned nonzero value, so problem closing file');
end%if

if nargout==1
  mycountmaybereturned=mycount;
end%if

if nargout==0
  disp(sprintf(['savedat: %g of %g*%g=%g elements written to ' myfilename],...
                mycount,M,N,M*N))
end%if

