////////////////////////////////////////////////////////////////////////////
//       TEMPERATURE UNITS - IMPORTANT!
//
// You need to tell this script if VWS is reporting temperature in C or F.
// This will also be used as the output units.  
// Use "C" or "F" on the line below...
//
var TempUnits = "C";
//
//       IMAGES PATH:
//
// If you store your images separate from your HTML pages, you need to put
// the path to the images below.  If the images are in the same directory
// as this HTML page (not the HTX template) then leave this blank ("").
// The path should end with a slash (/) Don't use dos-style backslashes (\)
//
var ImagesPath = "imgs/";
//
// This technique of stretching images is crude but it does have the neat
// side-effect of allowing the mercury to go off the top of the scale.
// The whole thermometer appears to stretch and the scale slides down.
//
//                                          Morgan Sandercock, November 2002
////////////////////////////////////////////////////////////////////////////
function VapourPressure(t) {
	return 6.112 * Math.exp((17.67 * t) / (243.5 + t));
}
function convFtoC(t) {
	return (t-32) * 5 / 9;
}
function convCtoF(t) {
	return t * 9 / 5 + 32;
}
function convPress(p) {
	if (p < 20) return 1013;
	if (p < 50) return p * 33.8653;
	if (p < 900) return p * 1.3332;
	if (p < 1200) return p;
	return 1013;
}
function WetBulb(t, dp, p) {
	if (TempUnits == "F") {
		t = convFtoC(t);
		dp = convFtoC(dp);
	}
	p = convPress(p);
	var tmin = dp;
	var tmax = t;
	var vp = VapourPressure(dp);
	var diff = tmax-tmin;
	var tc = dp;
	var vc;
	var eq;
	
	while ((tmax - tmin) > 0.01) {
		tc = (tmax + tmin) / 2;
		vc = VapourPressure(tc);
		eq = 0.00066 * (1+0.00155*tc)*p*(t-tc);
		diff = eq - vc + vp;
		if (diff<0) tmax = tc; else tmin = tc;
	}
	return tc;
}
function writeIMG(fname, width, height) {
	document.write("<IMG SRC='");
	document.write(ImagesPath + fname);
	document.write("' width=" + width);
	document.write(" height=" + height);
	document.write(" CLASS=WnD>");
}
function writeTubeTD(fname, width, height) {
	document.write("<TD BACKGROUND='");
	document.write(ImagesPath + "tube.gif");
	document.write("' CLASS=WnD>");
}
