/*
rhcalc.js - v1.0 (c) holsoft 2002
relative humidity calculator
belongs to rhcalc1.htm-page
*/

// constants:
svp_0 = 610.7; // saturated vapour pressure [Pa], water, at 0 Celcius
aa = 7.5;
bb = 237.3; // [K]
// Tzero = 273.13; // [K]
Gamma = 66; // [Pa/K]

// calc saturated vapour pressure at Celcius-temp T:
function CalcSVP(T) {
	var tmp = aa*T/(bb+T);
	return svp_0*Math.pow(10,tmp);
}

// calc partial vapour pressure at Celcius-temp T, given wet-bulb Tw:
function CalcVapPress(T,Tw) {
	var A = 66.87*(1+0.00115*Tw); // at 1 atm!!
	return CalcSVP(Tw) - A*(T-Tw);
}

// calc relative humidity at Celcius-temp T, given wet-bulb Tw:
function CalcRH(T,Tw) {
	var tmp = Math.floor(CalcVapPress(T,Tw)/CalcSVP(T)*1000+0.5)/10;
	if (tmp<0) {
		window.alert("Wet-bulb temperature below physical limit");
		return 0;
	}
	else {
		return tmp;
	}
}

// base function for calculating and displaying relative humidity:
function getHum() {
	// read Tdry, Twet:
	Tdry = parseFloat(document.form1.tdry.value); // Celcius
	Twet = parseFloat(document.form1.twet.value); // Celcius
	// check 'acceptable values' for Tdry, Twet:
	if ((Tdry<0)||(Twet<0)) { window.alert('Temperatures should be above zero.'); }
	else {
		if (Tdry<Twet) { window.alert('Dry-bulb temperature cannot be lower than wet-bulb...'); }
		else { // everything okay now...
			relhum = CalcRH(Tdry,Twet);
			document.form1.rhum.value = " "+relhum;
		}
	}
}
