function BHP=Gengine(Trpm,Tmp,Talt,Temp,MaxMP,ploton)
% function BHP=Gengine(Trpm,Tmp,Talt,Temp,MaxMP,ploton)
% Compute the brake horsepower of a 
% Generic six cylinder 300 HP aircraft engine.
% Engine conditions are descriped by the following
% Trpm is the RPM of the engine,
% Tmp  is the manifild pressure (In.Hg.) of the engine,
% Talt is the altitude (feet) of the engine,
% Temp is the Outside air temperature (degR) of the engine.
% Generate plot(s) if ploton=1.
% Do not plot if ploton=0.
% IF MaxMP=1, ignore Tmp and assume that the manifild pressure
% is the maximum for the specified altitude. 
% This makes Point D and Point A coincident.
% Points B and C are not relevant.
% Otherwise, make MaxMP=0.
% All inputs asr assumed to be scalar quantities.

% Load engine data
Genginedata

if MaxMP
	% Do this if we assume the maximum manifold pressure for a given altitude (MaxMP=1).
	% This assumption makes Point A and Point D coincident. 
	
	x=Data(:,1);
	hp=Data(:,2);
	mp=Data(:,3);
	rpm=Data(:,4);
	if ploton; 
		figure(2)
		clf
		plot(x,hp,'.'); 
		set(gca,'XTick',[])
		set(gca,'XTickLabel',[])
	end
	x2=Data2(:,1);
	tem2p=Data2(:,2);
	alt2=Data2(:,3);
	xlines=[x2,x2]';
	ylines=[zeros(size(x2)),350*ones(size(x2))]';
	l=length(x);
	
	if ploton
		hold on; plot(xlines,ylines,'k:')
		ylabel('BRAKE HORSEPOWER')
		title('ALTITUDE PERFORMANCE')
		plot(x(1:5:l),hp(1:5:l),'k-')
		plot(x(2:5:l),hp(2:5:l),'k-')
		plot(x(3:5:l),hp(3:5:l),'k-')
		plot(x(4:5:l),hp(4:5:l),'k-')
		plot(x(5:5:l),hp(5:5:l),'k-')
		istart=1:5:l;
		iend=5:5:l;
		for i=1:length(istart)
			plot(x(istart(i):iend(i)),hp(istart(i):iend(i)),'k-')
		end
		hold off
	end
	
	rpmvalues=[2700,2500,2300,2100,1900];

	% xhp and yhp are Coordinates to place pressure altitude labels.

	MP=[29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14];		  
			  
	if ploton
		str=[' 0'
		' 2'
		' 4'
		' 6'
		' 8'
		'10'
		'12'
		'14'
		'16'
		'18'
		'20'];
		text(xhp',yhp',str)
		text(.25,40,'PRESSURE ALTITUDE IN THOUSANDS OF FEET')
	
		for i=1:length(MP)
			str2=num2str(MP(i));
			strMP(i,:)=str2(:);
		end
	end
	% xmp and ymp are coordinates to place manifold pressure labels.

	RPM=[2700,2500,2300,2100,1900];		  
			  
	if ploton
		text(xmp',ymp'+3,strMP)
		text(.2,275,'MANIFOLD PRESSURE  In. Hg.')
		for i=1:length(RPM)
			str2=num2str(RPM(i));
			strRPM(i,:)=str2(:);
		end
	end
	% xrpm and yrpm are coordnates to place the rpm labels.	

	if ploton
		text(xrpm'-.019,yrpm'+2,strRPM)
		text(.44,127,'RPM')
		text(.44,345,'Generic six cylinder 300HP aircraft engine')
		text(-.19,345,'Since the maximum manifold pressure is') 
		text(-.19,335,'assumed for a given RPM and altitude,')
		text(-.19,325,'Points A and D are coincident.')
		text(-.19,315,'They are located for a given RPM and Alt.')
	end


	[xave1,hpave1]=rpmaveline(x,hp,RPM,Trpm);
	if ploton; hold on; plot(xave1,hpave1,'r--'); hold off; end
	altD=Talt;
	xD=interp1(alt2,x2,altD,'linear');
	if ploton; hold on; plot([xD,xD],[0,350],'g:'); end
	[xd,hpD]=intersection(xave1,hpave1,[xD,xD],[0,350],.01);

	if ploton
		hold on; plot(xd,hpD,'r*')
	
		str5=['Points A and D  HP= ',num2str(hpD),'  Alt= ',num2str(altD),' ft'];
		text(.5,70,str5)
		text(xD+.01,hpD,'A and D')
		plot([-.2,xD],[hpD,hpD],'g:')
		hold off;
	end
	% Correct for Outside Air Temperature
	[tempSTD,press,rho,Hgeopvector]=atmosphere4(Talt,0);
	deltaT=Temp-tempSTD;
	BHP=hpD-hpD*(deltaT/10)*.01;
	if ploton
		str6=['Horsepower corrected for Outside Air Temperature = ',num2str(BHP),',  OAT= ',num2str(Temp),' degF,  Ts= ',num2str(tempSTD),' degF'];
		text(-.19,20,str6)	
	end
	% End of section that assumes the maximum manifold pressure.
	
	
	
	
	
	
	
else
	% Do this if we use the specified manifold pressure Tmp (MaxMP=0).
	
	% First figure for Point B
	RPMvalue=[1900,2100,2300,2500,2700];
	manpress=DataSL(:,1);
	horses=DataSL(:,2);

	if ploton
		figure(1)
		clf
		plot(manpress,horses,'.')
		hold on;
		[itest1,manpresstest1,horsestest1,RPMtest1]=rpmline2(1,manpress,horses,RPMvalue);
		plot(manpresstest1,horsestest1,'k-')
		[itest2,manpresstest2,horsestest2,RPMtest2]=rpmline2(2,manpress,horses,RPMvalue);
		plot(manpresstest2,horsestest2,'k-')
		[itest3,manpresstest3,horsestest3,RPMtest3]=rpmline2(3,manpress,horses,RPMvalue);
		plot(manpresstest3,horsestest3,'k-')
		[itest4,manpresstest4,horsestest4,RPMtest4]=rpmline2(4,manpress,horses,RPMvalue);
		plot(manpresstest4,horsestest4,'k-')
		[itest5,manpresstest5,horsestest5,RPMtest5]=rpmline2(5,manpress,horses,RPMvalue);
		plot(manpresstest5,horsestest5,'k-')
		plot(SLFULLline(:,1),SLFULLline(:,2),'g--')
		text(30,200,'1900 RPM')
		text(30,240,'2100 RPM')
		text(30,273,'2300 RPM')
		text(30,300,'2500 RPM')
		text(30,315,'2700 RPM')
		xlabel('ABS. DRY MANIFOLD PRESURE IN. HG.')
		ylabel('HORSEPOWER')
		title('SEA LEVEL PERFORMANCE')
		text(19,60,'Generic six cylinder 300HP aircraft engine')
		text(29.3,188,'Full Throttle')
		text(27.3,178,'Horsepower at Zero RAM')
		text(16.2,340,'1. LOC. RPM & MAN. PR. ON ALT. CHART (PT"A")')
		text(16.2,330,'2. LOC. RPM & MAN. PR. ON S.L. CHART (PT"B")')
		text(16.2,320,'3. TRANSFER "B" TO S.L. ON ALT. CHART (PT"C")')
		text(16.2,310,'4. DRAW LINE FROM "C" THRU "A"')
		text(16.2,300,'5. LOC. PT "D" AT PR. ALT. & READ H.P.')
		text(16.2,290,'6. CORR. H.P. FOR INLET AIR TEMP AS FOLLOWS:')
		text(16.2,280,'   (A) ADD 1% FOR EACH 10degF BELOW Ts')
		text(16.2,270,'   (B) SUBTRACT 1% FOR EACH 10degF ABOVE Ts')
		text(16.2,260,'       (Ts=STANDARD ALTITUDE TEMP)')
	end
	
	% Point B
	[manpressB,horsesB]=rpmaveline2(manpress,horses,RPMvalue,Trpm);
	if ploton ; plot(manpressB,horsesB,'r:'); end
	%YI = INTERP1(X,Y,XI)
	hpB=interp1(manpressB,horsesB,Tmp);
	mpB=Tmp;
	rpmB=Trpm;
	
	if ploton
		plot(mpB,hpB,'rx')
		text(mpB+.1,hpB,'B')
		str=['x  is PT"B", HP= ',num2str(hpB),', RPM= ',num2str(rpmB),', MP= ',num2str(mpB)];
		text(19,75,str)
		plot([mpB,32],[hpB,hpB],'r:')
		text(28,hpB+6,'To Pt. C')
		plot([mpB,mpB],[50,hpB],'r:')
		hold off
	end
	
	
	% ********
	% Second figure for points A, C, and D
	if ploton
		figure(2)
		clf
	end
	

	
	x=Data(:,1);
	hp=Data(:,2);
	mp=Data(:,3);
	rpm=Data(:,4);
	if ploton; 
		plot(x,hp,'.');
		set(gca,'XTick',[])
		set(gca,'XTickLabel',[])
	end
	x2=Data2(:,1);
	tem2p=Data2(:,2);
	alt2=Data2(:,3);
	xlines=[x2,x2]';
	ylines=[zeros(size(x2)),350*ones(size(x2))]';
	l=length(x);
	
	if ploton
		hold on; plot(xlines,ylines,'k:')
		ylabel('BRAKE HORSEPOWER')
		title('ALTITUDE PERFORMANCE')
		plot(x(1:5:l),hp(1:5:l),'k-')
		plot(x(2:5:l),hp(2:5:l),'k-')
		plot(x(3:5:l),hp(3:5:l),'k-')
		plot(x(4:5:l),hp(4:5:l),'k-')
		plot(x(5:5:l),hp(5:5:l),'k-')
		istart=1:5:l;
		iend=5:5:l;
		for i=1:length(istart)
			plot(x(istart(i):iend(i)),hp(istart(i):iend(i)),'k-')
		end
		hold off
	end
	
	rpmvalues=[2700,2500,2300,2100,1900];

	% xhp and yhp are Coordinates to place pressure altitude labels.

	MP=[29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14];		  
			  
	if ploton
		str=[' 0'
		' 2'
		' 4'
		' 6'
		' 8'
		'10'
		'12'
		'14'
		'16'
		'18'
		'20'];
		text(xhp',yhp',str)
		text(.25,40,'PRESSURE ALTITUDE IN THOUSANDS OF FEET')
	
		for i=1:length(MP)
			str2=num2str(MP(i));
			strMP(i,:)=str2(:);
		end
	end
	% xmp and ymp are coordinates to place manifold pressure labels.

	RPM=[2700,2500,2300,2100,1900];		  
			  
	if ploton
		text(xmp',ymp'+3,strMP)
		text(.2,275,'MANIFOLD PRESSURE  In. Hg.')
		for i=1:length(RPM)
			str2=num2str(RPM(i));
			strRPM(i,:)=str2(:);
		end
	end
	% xrpm and yrpm are coordnates to place the rpm labels.	

	if ploton
		text(xrpm'-.019,yrpm'+2,strRPM)
		text(.44,127,'RPM')
		text(.4,340,'Generic six cylinder 300 HP aircraft engine')
	end
	
	[xave1,hpave1]=rpmaveline(x,hp,RPM,Trpm);
	if ploton; hold on; plot(xave1,hpave1,'r--'); hold off; end
	
	[xave2,hpave2]=mpaveline(x,hp,MP,Tmp);
	if ploton; hold on; plot(xave2,hpave2,'r--'); hold off; end
	
	%[xi,hpi]=intersection(xave1,hpave1,xave2,hpave2,.01);
	[xA,hpA]=intersection(xave1,hpave1,xave2,hpave2,.01);
	if ploton; hold on; plot(xA,hpA,'r*'); hold off; end
	
	% Point A
	altA=interp1(x2,alt2,xA,'linear');
	rpmA=Trpm;
	mpA=Tmp;
	if ploton
		str3=['Point A,  HP= ',num2str(hpA),',  ALT= ',num2str(altA),' ft,','  RPM= ',num2str(rpmA),',  Man.Press.= ',num2str(mpA),' In.Hg.' ];
		text(.1,80,str3)
		text(xA+.01,hpA,'A')
	end
	
	% Point C
	xC=0;
	hpC=hpB;
	if ploton
		hold on; plot(xC,hpC,'r*')
		plot([xC,xA],[hpC,hpA],'r:')
		str4=['Point C  HP= ',num2str(hpC)];
		text(.1,70,str4)
		text(xC+.01,hpC,'C')
		plot([-.2,0],[hpC,hpC],'r:')
		text(-.18,hpC+6,'From Pt. B')
	end
	
	% Point D
	altD=Talt;
	xD=interp1(alt2,x2,altD,'linear');
	if ploton; plot([xD,xD],[0,350],'g:'); end
	[xd,hpD]=intersection([xC,xA],[hpC,hpA],[xD,xD],[0,350],.01);
	
	if isempty(xd)
		disp('Vertical line at D does not intersect the line C-A. Change engine operating conditions.')
		disp('Lowering the manifold pressure generally works.')
		BHP=[];
	else
		if ploton
			hold on; plot(xd,hpD,'r*'); hold off
		
			str5=['Point D  HP= ',num2str(hpD),'  Alt= ',num2str(altD),' ft'];
			text(.5,70,str5)
			text(xD+.01,hpD,'D')
		end
		% Correct for Outside Air Temperature
		[tempSTD,press,rho,Hgeopvector]=atmosphere4(Talt,0);
		deltaT=Temp-tempSTD;
		BHP=hpD-hpD*(deltaT/10)*.01;
		if ploton
			str6=['Horsepower corrected for Outside Air Temperature = ',num2str(BHP),',  OAT= ',num2str(Temp),' degR,  Ts= ',num2str(tempSTD),' degR'];
			text(-.19,20,str6)
		end
	end % End of section for when we use Tmp and Points A, B, C and D.
end
