%
disp(' ')
disp(' time_history_nastran.m  ver 1.1  March 1, 2014 ');
disp(' ');
disp(' by Tom Irvine ');
disp(' ');
%
disp(' This script converts a time history to format which can be imported ');
disp(' to FEMAP and used for Nastran. ');
disp(' ');
disp(' The input file must have two columns:  time(sec) & amplitude ');
%
clear t;
clear a;
%
[t,a,dt,sr,tmx,tmi,n,ncontinue]=enter_time_history();
%
disp('  Multiply by amplitude by 386. ? ');
disp('   1=yes  2=no ');
iscale=input('  ');
%
if(iscale==1)
    a=a*386.;
end
%
for i=1:n
    if(abs(a(i))<0.00001 )  
	      a(i)=0.;
    end
end
%
k = floor( double(n)/4.);
%
out4=sprintf('\n Enter the external ASCII output nastran filename: ');
disp(out4);
output_filename = input(' ','s');
%
%  old method
%
fid = fopen(output_filename,'w');
%
fprintf(fid,'ID  \n');
fprintf(fid,'SOL SEMTRAN \n');
fprintf(fid,'TIME 10000 \n');
fprintf(fid,'CEND \n');
fprintf(fid,' TITLE = Transient \n');
fprintf(fid,'BEGIN BULK \n');
fprintf(fid,'TABLED2        9      0.                                                +    \n');
%
for i=1:4:(n-3)
%
	   x=t(i);
       [iflag,ss]=datafix(x);
	   s1=ss;
%
	   x=a(i);
       [iflag,ss]=datafix(x);
	   s2=ss;
%
	   x=t(i+1);
       [iflag,ss]=datafix(x);
	   s3=ss;
%
	   x=a(i+1);
       [iflag,ss]=datafix(x);
       s4=ss;
%
       if( t(i)==0 && t(i+1)==0 )
		   printf('\n double zero error \n');
       end
%
	   x=t(i+2);
       [iflag,ss]=datafix(x);
       s5=ss;
%
	   x=a(i+2);
       [iflag,ss]=datafix(x);
       s6=ss;
%
	   x=t(i+3);
       [iflag,ss]=datafix(x);
       s7=ss;
%
	   x=a(i+3);
       [iflag,ss]=datafix(x);
       s8=ss;
%
       if(    str2double(s1) < str2double(s3) ...
		   && str2double(s3) < str2double(s5) ...
		   && str2double(s5) < str2double(s7) )
%       
			fprintf(fid,'+       %s%s%s%s%s%s%s%s+\n',s1,s2,s3,s4,s5,s6,s7,s8);
       end

       if(    str2double(s1) < str2double(s3) ...
		   && str2double(s3) < str2double(s5) ...
		   && str2double(s5) >= str2double(s7) )
%	   
		   fprintf(fid,'+       %s%s%s%s%s%s',s1,s2,s3,s4,s5,s6);
		   iflag=2;
%
		   break;
       end
%
       if(    str2double(s1) < str2double(s3) && str2double(s3) >= str2double(s5) )
%	   
		   fprintf(fid,'+       %s%s%s%s',s1,s2,s3,s4);
%
		   iflag=2;
		   break;
       end
       if(    str2double(s1) >= str2double(s3) )
%	   
		   fprintf(fid,'+       %s%s',s1,s2);
%
		   iflag=2;
%
		   break;
       end
%
end
%
if(iflag==1)
    fprintf(fid,'+,ENDT\n');
	fprintf(fid,'ENDDATA');
end
if(iflag==2)
	fprintf(fid,'ENDT\n');
	fprintf(fid,'ENDDATA');
end
%
fclose(fid);
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%