%function duration = calcDuration(range,rpm)
%function duration = calcDuration(initialdepth,enddepth,pitch,rpm)
%function duration = calcDuration(initiallat,initiallong,latitude,longitude,rpm)
%  pitch is in degrees
%  returns seconds or [] if bad inputs
%  returns at least 180 seconds
function duration = calcDuration(initiallat,initiallong,latitude,longitude,rpm)

transitSpeed = 1.0;  	%meters/second at 300 rpm (this is intentionally low)
minDuration = 180; 		%seconds

duration = [];
if nargin == 2,
	range = initiallat;
	rpm = initiallong;
	if rpm < 150,
		errorbox('Calculation not valid for rpm''s less than 150')	
		return
	elseif rpm >= 300, speed = transitSpeed;
	else speed = transitSpeed/2; end
	duration = max(range/speed,minDuration);
elseif nargin == 4,
	rpm = longitude;
	pitch = latitude;
	enddepth = initiallong;
	initialdepth = initiallat;
	
	if enddepth < 0 | initialdepth < 0,
		errorbox('Depths must be positive')
		return
	end
	
	if pitch > 20 | pitch < 0,
		errorbox('Calculation only valid for small angle ascents/descents')
		return
	end
	
	if rpm < 150,
		errorbox('Calculation not valid for rpm''s less than 150')	
		return
	elseif rpm >= 300, speed = transitSpeed;
	else speed = transitSpeed/2; end
	
	verticalSpeed = sin(deg2rad(pitch))*speed;
	rng = abs(initialdepth - enddepth);
	duration = max(approximate(rng/verticalSpeed),minDuration);
	
elseif nargin == 5,
	rng = distance('rh',initiallat,initiallong,latitude,longitude,selectGeoid);
	if rpm < 150,
		warning('Calculation not valid for rpm''s less than 150')
		return
	elseif rpm >= 300, speed = transitSpeed;
	else speed = transitSpeed/2; end
	duration = max(approximate(rng/speed),minDuration);

else,
	errorbox('Not enough arguments')
end

