//hydrology20.js
//USED BY hydrology20.html
//1/5/2009 BS
//oop rainfall

function idf_curve(b,d,e)
{
	//idf_curve Object
	
	this.intensity=intensity;
	this.depth=depth;
	this.hyetograph=hyetograph;

	function intensity(minutes){
		return b/Math.pow((minutes+d),e);
	}

	function depth(minutes){
		//rainfall depth from time=0 to time =t minutes
		//unit inches
		return intensity(minutes)*minutes/60;
	}

	function hyetograph(duration, del_t, fraction)
	{
		//return a hyetograph array with peak at the franction of time duration
		
		//duration = total time in minutes
		//del_t = time interval in minutes, use 5 minutes
		//fraction= decimal fraction to represent where the peak should be placed
		//for example if fraction is 0.25, then the peak intensity will be at the one fourth time of the entire duration
		//usage idf_curve_object.hyetograph(45,5,0.25)
		
		//create a intensity array
		var intensity=new Array();
		for(var k=0;k<duration/del_t;k++)
			{
				intensity[k]=(this.depth((k+1)*del_t)-this.depth(k*del_t)) /(del_t/60);
				//console.log(k+' : '+intensity[k]);
			}
		return intensity.hyetograph(fraction);
	}
	
}

function SF_IDF(returnPeriod)
{
	//SF_IDF Object
	this.intensity=intensity;
	this.depth=depth;
	this.hyetograph=hyetograph;

	function depth(minutes)
	{return intensity(minutes)*minutes/60;
	}

	function intensity(minutes)
	{
		switch (returnPeriod)
		{
		case 0.25:
			var b=2.655;
			var d=0.60;
			var e=0.479;
			//var result=b/Math.pow((tc+d),e);
		  	break
		case 0.50:
			var b=3.551;
			var d=0.30;
			var e=0.493;
			//var result=b/Math.pow((tc+d),e);
		  	break
		case 1:
			var b=4.605;
			var d=0.3;
			var e=0.513;
			//var result=b/Math.pow((tc+d),e);
		  	break
		case 2:
			var b=6.109;
			var d=0.80;
			var e=0.5417;
			//var result=b/Math.pow((tc+d),e);
		  	break
		case 5:
			if(minutes<39){b=44.7;d=9.3;e=1;}	//1941 curve
			else
			{	if(minutes<=90){b=8;d=0;e=0.587;}	//1941 curve
				else{b=8.025455;d=1.1;e=0.5637287;}	//new curve
			}
		  	break
		case 10:
			var b=8.527;
			var d=0.50;
			var e=0.548;
			//var result=b/Math.pow((tc+d),e);
		  	break
		case 25:
			var b=11.233;
			var d=1.4;
			var e=0.58;
			//var result=b/Math.pow((tc+d),e);
		  	break

		case 50:
			var b=12.593;
			var d=1.3;
			var e=0.582;
			//var result=b/Math.pow((tc+d),e);
		  	break

		case 100:
			var b=13.217;
			var d=1.0;
			var e=0.568;
			//var result=b/Math.pow((tc+d),e);
		  	break
		default:
		  document.write("error in default for sf_idf object(Return Period).intensity(minutes)!");
		  document.write("<br/>Return Period of "+returnPeriod+" year/s is not defined.");
		}

		var idf=new idf_curve(b,d,e);
		return idf.intensity(minutes);
	}//endof intensity

	function hyetograph(duration, del_t, fraction)
	{
		//return a hyetograph array with peak at the franction of time duration
		
		//duration = total time in minutes
		//del_t = time interval in minutes, use 5 minutes
		//fraction= decimal fraction to represent where the peak should be placed
		//for example if fraction is 0.25, then the peak intensity will be at the one fourth time of the entire duration
		//usage SF_IDF OBJECT.hyetograph(45,5,0.25)
		
		//create a intensity array
		var intensity=new Array();
		for(var k=0;k<duration/del_t;k++)
			{
				intensity[k]=(this.depth((k+1)*del_t)-this.depth(k*del_t)) /(del_t/60);
				//console.log(k+' : '+intensity[k]);
			}
		return intensity.hyetograph(fraction);
	}
}

function addCommas(nStr)
{
	nStr += '';
	var x = nStr.split('.');
	var x1 = x[0];
	var x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function numOrdA(a, b){ return (a-b); }	//Prevent alphanumeric sorting
function numOrdD(a, b){ return (b-a); }	//Prevent alphanumeric sorting

function mod(divisee,base)
{	//Remainder of Division
	return Math.round(divisee - (Math.floor(divisee/base)*base));
}

