//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 fraction 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
		//example usage idf_curve_object.hyetograph(45,5,0.25)
		
		//create a intensity array
		var intensity=[];
		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);
			}
		return intensity.hyetograph(fraction);
	}
}

function SF_IDF(returnPeriod)
{
	//SF_IDF Object
	this.intensity=intensity;
	this.depth=depth;
	this.hyetograph=hyetograph;
	this.returnPeriod = returnPeriod;
	var year = returnPeriod * 1.0; //number not text
	function depth(minutes)
	{return intensity(minutes)*minutes/60;
	}

	function intensity(minutes)
	{
		switch (year)
		{
		case 0.25:
			var b=2.655;
			var d=0.60;
			var e=0.479;
		  	break;
		case 0.50:
			var b=3.551;
			var d=0.30;
			var e=0.493;
		  	break;
		case 1:
			var b=4.605;
			var d=0.3;
			var e=0.513;
		  	break;
		case 2:
			var b=6.109;
			var d=0.80;
			var e=0.5417;
		  	break;
		case 5:
			if(minutes<39){var b=44.7; var d=9.3; var e=1;}	//1941 curve
			else
			{	if(minutes<=90){var b=8; var d=0; var e=0.587;}	//1941 curve
				else{var b=8.025455; var d=1.1; var e=0.5637287;}	//new curve from CAP
			}
		  	break;
		case 10:
			var b=8.527;
			var d=0.50;
			var e=0.548;
		  	break;
		case 25:
			var b=11.233;
			var d=1.4;
			var e=0.58;
		  	break;
		case 50:
			var b=12.593;
			var d=1.3;
			var e=0.582;
		  	break;
		case 100:
			var b=13.217;
			var d=1.0;
			var e=0.568;
		  	break;
		default:
		  document.write("<hr/>error in default for sf_idf object(Return Period).intensity(minutes)!");
		  document.write("<br/>Return Period of "+returnPeriod+" year/s is not defined.");
		  document.write("<br/>Following year return periods are defined : 0.25, 0.50,1 , 2, 5, 10, 25, 50 and 100");
		}

		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 an 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);
			}
		return intensity.hyetograph(fraction);
	}
}

SF_IDF.prototype.timeseries = function(data){
	//pass data as {duration:90, interval:5}
	//used by rainfall.html

	function hhmm(m){
	    var hh = parseInt(m/60);
	    var mm = parseInt(m%60);
	    if(hh<10){hh = '0'+hh}
	    if(mm<10){mm = '0'+mm}
	    return hh+':'+mm;
	}

	var msg = '\;\;SF Hyetograph';
	msg += '\n\;\;' + this.returnPeriod + ' YR ' + data.duration/60 + ' h';
	var h = this.hyetograph(data.duration, data.interval,0.5);
	var mm = 5;
	h.map(function(x){
		msg += '\n01/01/2010' + '\t' + hhmm(mm) +'\t' + x.toFixed(4);
		mm += 5;}
	);

	return msg;		//text lines like 01/01/2010 13:45 0.35 for proper mass-balance processing
};

