function [erc,ermessage] = write_uff(filename,data_set_type,i1,i2,i3,i4,i5,...
					i6,i7,i8,i9,i10,i11,i12,i13,i14,i15);
%usage:  
% for measurement data file:
%	[erc,ermessage] = write_uff(filename,58,datatype,d1,d2,date,IDs,...
%			             rptnum,rdir,eptnum,edir,fmin,fdel,data); 
%          where datatype is: 'ASPEC','XSPEC','FRF', or 'COH'
%
% for modal data file:
%	[erc,ermessage] = write_uff(filename,55,analysis_type,data_character,...
%                                                response_type,mode_num, ...
%         mode_freq,mode_mass,mode_v_damping_ratio,mode_h_damping_ratio, ...
%                                               node_num,r1,r2,r3,r4,r5,r6);
%     or
%	[erc,ermessage] = write_uff(filename,55,analysis_type,data_character,...
%                                                response_type,mode_num, ...
%                       eig_val,model_A,model_B,0,node_num,r1,r2,r3,r4,r5,r6);
%
%for Coordinate data:  (Grid pts)
%	[erc,ermessage] = write_uff(filename,15,pt_num,x_coord,y_coord,z_coord);
%
%for Display Sequence data:
%       [erc,ermessage] = write_uff(filename,82,disp_sequence);


%last modified 9-14-93 by Bryce Gardner

%  modified by Scott W. Doebling
%
%  Version SWD970528 

[fid,ermesage] = fopen(filename,'at');
fprintf(fid,'%6i%74s\n',-1,' ');

if data_set_type == 15 % Coordinate Data

%-------------------------------Start of Coordinate Data--- -------------------

  pt_num = i1;
  x_coord = i2;
  y_coord = i3;
  z_coord = i4;

  fprintf(fid,'%6i%74s\n',15,' '); 
  for i=1:length(pt_num),
    fprintf(fid,'%10i%10i%10i%10i%13.5e%13.5e%13.5e\n',pt_num(i),0,0,0, ...
					x_coord(i),y_coord(i),z_coord(i));
					      % line 1
  end

%-------------------------------End of Coordinate Data----- -------------------

elseif data_set_type == 55% Modal Data

%-------------------------------Start of Modal Data----------------------------

  analysis_type = i1;
  data_character = i2;
  response_type = i3;
  mode_num = i4;
  if analysis_type == 2,
    mode_freq = i5;
    mode_mass = i6;
    mode_v_damping_ratio = i7;
    mode_h_damping_ratio = i8;
  else,
    eig_val = i5;
    model_A = i6;
    model_B = i7;
  end
  node_num = i9;
  r1 = i10;
  r2 = i11;
  r3 = i12;
  if exist('i13')==1,
    num_data_per_pt = 6;
    r4 = i13;
    r5 = i14;
    r6 = i15;
  else,
    num_data_per_pt = 3;
  end

  for i=1:length(mode_num),
    if i~=1,
      fprintf(fid,'%6i%74s\n',-1,' ');
    end
    fprintf(fid,'%6i%74s\n',55,' '); 
    fprintf(fid,'%80s\n','NONE'); %line 1
    fprintf(fid,'%80s\n','NONE'); %line 2
    fprintf(fid,'%80s\n','NONE'); %line 3
    fprintf(fid,'%80s\n','NONE'); %line 4
    fprintf(fid,'%80s\n','NONE'); %line 5
   %if imag(r1)~=0 & imag(r2)~=0 & imag(r3)~=0, 
   if imag(r1)==0 & imag(r2)==0 & imag(r3)==0, % MODIFIED SO THAT data_type=2 is REAL MODE
      data_type = 2;
    else,
      data_type = 3;
    end
    fprintf(fid,'%10i%10i%10i%10i%10i%10i\n',1,analysis_type,data_character, ...
	response_type,data_type,num_data_per_pt); %line 6
    if analysis_type == 2,
      fprintf(fid,'%10i%10i%10i%10i\n',2,4,0,mode_num(i)); %line 7
      fprintf(fid,'%13.5e%13.5e%13.5e%13.5e\n', ...
	      mode_freq(i),mode_mass(i),mode_v_damping_ratio(i),mode_h_damping_ratio(i)); %line 8
    elseif analysis_type == 3 | analysis_type == 7,
      fprintf(fid,'%10i%10i%10i%10i\n',2,6,0,mode_num(i)); %line 7
      fprintf(fid,'%13.5e%13.5e%13.5e%13.5e%13.5e%13.5e\n', ...
	    real(eig_val),imag(eig_val),real(model_A),imag(model_A), ...
				       real(model_B),imag(model_B)); %line 8
    else
      ermessage = 'analysis type is not supported';
      disp(ermessage);
      erc = -1;
      return;
   end
    if data_type == 2,
      if num_data_per_pt == 3,
        for k=1:length(node_num);
	        fprintf(fid,'%10i\n',node_num(k));%  Write node number
           fprintf(fid,'%13.5e%13.5e%13.5e\n',r1(i,k),r2(i,k),r3(i,k));
        end
      else,
         for k=1:length(node_num);
	        fprintf(fid,'%10i\n',node_num(k));%  Write node number
           fprintf(fid,'%13.5e%13.5e%13.5e%13.5e%13.5e%13.5e\n', ...
		   r1(i,k),r2(i,k),r3(i,k),r4(i,k),r5(i,k),r6(i,k));
        end
      end
    else,
      for k=1:length(node_num);
	        fprintf(fid,'%10i\n',node_num(k));%  Write node number
           fprintf(fid,'%13.5e%13.5e%13.5e%13.5e%13.5e%13.5e\n', ...
		  real(r1(i,k)),imag(r1(i,k)), real(r2(i,k)),imag(r2(i,k)), ...
		  real(r3(i,k)),imag(r3(i,k)));
      end
    end
    if i~=length(mode_num),
      fprintf(fid,'%6i%74s\n',-1,' ');
    end
  end

%-------------------------------End of Modal Data------------------------------

elseif data_set_type == 58   % Measurement Data Type

%-------------------------------Start of Measurement Data----------------------

  datatype = i1;
  d1 = i2;
  d2 = i3;
  date = i4;
  IDs = i5;
  rptnum = i6;
  rdir = i7;
  eptnum = i8;
  edir = i9;
  fmin = i10;
  fdel = i11;
  data = i12;

  fprintf(fid,'%6i%74s\n',58,' '); 
  if length(d1)<=80,
    fprintf(fid,'%80s\n',d1);   %  line 1
  else
    fprintf(fid,'%80s\n',d1(1:80));   %  line 1
  end
  if length(d2)<=80,
    fprintf(fid,'%80s\n',d2);   %  line 2
  else
    fprintf(fid,'%80s\n',d2(1:80));   %  line 2
  end
  if length(date)<=80,
    fprintf(fid,'%80s\n',date);   %  line 3
  else
    fprintf(fid,'%80s\n',date(1:80));   %  line 3
  end
  if length(IDs)<=80,
    fprintf(fid,'%80s\n',IDs);   %  line 4
  else
    fprintf(fid,'%80s\n',IDs(1:80));   %  line 4
  end
  fprintf(fid,'%4s%76s\n','NONE',' ');    %  line 5

  if datatype == 'ASPEC';
    dtype = 2;
  elseif datatype == 'XSPEC';
    dtype = 3;
  elseif datatype == 'FRF';
    dtype = 4;
  elseif datatype == 'COH';
    dtype = 6;
  else,
    erc = 0;
    ermessage = 'data type is not supported';
    disp(ermessage)
    return
  end

  fprintf(fid,'%5i%10i%5i%10i %10s%10i%4i %10s%10i%4i\n',dtype,0,0,0,'NONE',...
	  rptnum,rdir,'NONE',eptnum,edir);    % line 6
  numpt = length(data);
  if imag(data)==0,
%single precision
%    fprintf(fid,'%10i%10i%10i%13.5e%13.5e%13.5e           \n',2,numpt,1,fmin,fdel,0);  % line 7
%double precision
    fprintf(fid,'%10i%10i%10i%13.5e%13.5e%13.5e           \n',4,numpt,1,fmin,fdel,0);  % line 7
  else,
%single precision
%    fprintf(fid,'%10i%10i%10i%13.5e%13.5e%13.5e           \n',5,numpt,1,fmin,fdel,0);  % line 7
%double precision
    fprintf(fid,'%10i%10i%10i%13.5e%13.5e%13.5e           \n',6,numpt,1,fmin,fdel,0);  % line 7
  end
  fprintf(fid,'%10i%5i%5i%5i %20s %20s             \n',18,0,0,0,'NONE','NONE');
								      % line 8
  fprintf(fid,'%10i%5i%5i%5i %20s %20s             \n',12,0,0,0,'NONE','NONE');
								      % line 9
%                                                      ^--acceleration data
% others: 0=unknown,8=displacement,11=velocity,13=excitation force,15=pressure
  fprintf(fid,'%10i%5i%5i%5i %20s %20s             \n',13,0,0,0,'NONE','NONE');
%                                                      ^--excitation force data
                                                                      % line 10
% others: 0=unknown,8=displacement,11=velocity,12=acceleration,15=pressure
  fprintf(fid,'%10i%5i%5i%5i %20s %20s             \n',0,0,0,0,'NONE','NONE');
								      % line 11
% line 12:
  if imag(data)==0,
%single precision
%    fprintf(fid,'%13.5e%13.5e%13.5e%13.5e%13.5e%13.5e  \n',data);
%double precision
    fprintf(fid,'%20.12e%20.12e%20.12e%20.12e\n',data);
    newdata=data;
  else,
    indxs=2*[1:length(data)];
    newdata(indxs)=imag(data);
    newdata(indxs-1)=real(data);
%single precision
%    fprintf(fid,'%13.5e%13.5e%13.5e%13.5e%13.5e%13.5e  \n',newdata);
%double precision
    fprintf(fid,'%20.12e%20.12e%20.12e%20.12e\n',newdata);
  end
  if rem(length(newdata),4)~=0,
    fprintf(fid,'\n');
  end
%-------------------------------End of Measurement Data------------------------


elseif data_set_type == 82      % Display Sequence Data
%-------------------------------Start of Display Data------------------------

  disp_sequence = i1;

  fprintf(fid,'%6i%74s\n',82,' '); 
  fprintf(fid,'%10i%10i%10i\n',0,length(disp_sequence),0);  % line 1
  fprintf(fid,'%80s\n','NONE'); % line 2     
  fprintf(fid,'%10i%10i%10i%10i%10i%10i%10i%10i\n',disp_sequence); % line 3
  % ADD ANOTHER CR IF A PARTIAL LINE WAS LEFT AFTER THE WRITING
  if length(disp_sequence)/8 ~= floor(length(disp_sequence)/8),
     fprintf(fid,'\n');
  end
  
%-------------------------------End of Display Data--------------------------
else
  ermessage = 'unrecognized type of data set';
  disp(ermessage);
  erc = -2;
end

fprintf(fid,'%6i%74s\n',-1,' ');
%fprintf(fid,'%6i%74s\n',-1,' ');
fclose(fid);
erc=1;
return;
