view static/iwws.js @ 13:a0213f0e707b

- Update for v12.10 - Plot raw data to show more up to date data - Average/peak winds so they don't spam the display. - Display the time of the most recent data.
author Daniel O'Connor <darius@dons.net.au>
date Thu, 10 Jan 2013 16:38:36 +1030
parents bfe6ed563ffc
children 294e6a7fbb6e
line wrap: on
line source

/*
 * Copyright 2011 Daniel O'Connor <darius@dons.net.au>
 *
 *  Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright notice, this list of
 *      conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright notice, this list
 *      of conditions and the following disclaimer in the documentation and/or other materials
 *      provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of Daniel O'Connor.
 */

var timer = null;
var datacache = null;

$.jQTouch({
    icon: 'icon.png',
    startupScreen: 'img/startup.png'
});

function draw_graph() {
    var temp_out = [];
    var dewpt = [];
    var wavg = [];
    var wgust = [];
    var rain = [];
    var pressure = [];
    var i, mint = 5, maxt = 35;
    var l = datacache['idx'].length - 1;
    var d = new Date();
    var tzofs = d.getTimezoneOffset() * 60;
    var wavgtmp = 0;
    var wgusttmp = 0;
    var winddir = 0;
    var windavgs = 0;
    var windnavg = 4;
    for (i = 0; i < l; i++) {
	// Apply offset so Flot times look right (it always shows UTC)
	t = (datacache['idx'][i] - tzofs) * 1000.0;

	// Keep track of min/max temperature
	if (datacache['temp_out'][i] < mint)
	    mint = datacache['temp_out']
	if (datacache['temp_out'][i] > maxt)
	    maxt = datacache['temp_out'][i]

	// Store outside temperature if valid
	if (datacache['temp_out'][i] != null) {
	    temp_out.push([t, datacache['temp_out'][i]]);

	    // Calculate dewpoint if outside humidity is valid
	    if (datacache['hum_out'][i] != null) {
		dewpt.push([t, hum2dp(datacache['hum_out'][i], datacache['temp_out'][i])]);
	    }
	}

	// Store baro
	pressure.push([t, datacache['abs_pressure'][i]]);

	// Store rain if there was any
	if (datacache['rain'][i] > 0)
	    rain.push([t, datacache['rain'][i]]);

	// Store wind (convert to km/r)
	// Average the average wind speed
	wavgtmp += datacache['wind_ave'][i] * 60.0 * 60.0 / 1000.0
	// Pick highest speed gusts
	var g = datacache['wind_gust'][i] * 60.0 * 60.0 / 1000.0;
	if (g > wgusttmp) {
	    wgusttmp = g;
	}
	// Accumulate speed/gust direction
	winddir += wind2angle(datacache['wind_dir'][i]);
	windavgs++;
	// Normalise
	if (windavgs == windnavg) {
	    windavgs = 0;
	    wavgtmp /= windnavg;
	    // Note: gust is peak, so nothing to do
	    winddir /= windnavg;

	    // Store
	    wavg.push([t, wavgtmp, winddir]);
	    wgust.push([t, wgusttmp, winddir]);

	    // Reset accumulators
	    wavgtmp = 0;
	    wgusttmp = 0;
	    winddir = 0;
	}
    }

    $.plot($("#graph1"), [
	{ data : pressure, label: "Baro", yaxis : 1, points : { show : true, radius : 0 }, lines : { show : true } },
	{ data : temp_out, label: "Temp.", yaxis : 2, points : { show : true, radius : 0 }, lines : { show : true } },
	{ data : dewpt, label: "Dew point", yaxis : 2, points : { show : true, radius : 0 }, lines : { show : true } },
    ],
	   { xaxis : { mode : 'time' },
	     legend : { backgroundOpacity : 0, position : 'nw' },
	     yaxis : { min : 950, max : 1050, tickFormatter : baroFormatter },
	     y2axis : { min : mint, max : maxt, tickFormatter : degCFormatter },
	   });
    $.plot($("#graph2"), [
	{ data : wavg, label: "Wind (Avg)", yaxis : 1, points : { show : true }, lines : { show : true }, direction : true },
	{ data : wgust, label: "Wind (Gust)", yaxis : 1, points : { show : true }, lines : { show : true }, direction :  true },
	{ data : rain, label: "Rain", yaxis : 2, bars : { show : true, barWidth : 0.5 * 60 * 60 * 1000, align : "centre" } },
    ],
	   { xaxis : { mode : 'time' },
	     legend : { backgroundOpacity : 0, position : 'nw' },
	     yaxis : { tickFormatter : spdFormatter },
	     y2axis : { min : 0, tickFormatter : mmFormatter }
	   });
    // Reverse tzofs calculation as Date does local time
    var dobj = new Date(datacache['lastdata'] * 1000.0);
    $('#stamp').html(sprintf("Last data: %02d/%02d/%02d %02d:%02d:%02d", dobj.getFullYear(), dobj.getMonth() + 1, dobj.getDate(),
			     dobj.getHours(), dobj.getMinutes(), dobj.getSeconds()));
}

function wind2angle(dir) {
    var a = dir * (360.0 / 16.0);
    return a;
}

// Formula from http://www.paroscientific.com/dewpoint.htm
function alpha(temp, rh, a, b) {
    return (((a * temp) / (b + temp)) + Math.log(rh / 100.0));
}

function hum2dp(rh, temp) {
    var a = 17.27;
    var b = 237.7;
    var Td = (b * alpha(temp, rh, a, b)) / (a - alpha(temp, rh, a, b));
    return Td;
}

function degCFormatter(v, axis) {
    return v.toFixed(axis.tickDecimals) +"°C";
}

function pctFormatter(v, axis) {
    return v.toFixed(axis.tickDecimals) +"%";
}

function spdFormatter(v, axis) {
    return v.toFixed(axis.tickDecimals) + "kph";
}

function mmFormatter(v, axis) {
    return v.toFixed(axis.tickDecimals) + "mm";
}

function baroFormatter(v, axis) {
    return v.toFixed(axis.tickDecimals) + "hPa";
}

function got_data(data, status) {
    if (status != "success") {
	$.log("Couldn't load data. status = " + status);
	return;
    }

    datacache = data;
    draw_graph();
}

function update_data() {
    /* Cancel any pending timeout (eg if the user pressed update) */
    if (timer != null)
	clearTimeout(timeout);
    jQuery.getJSON('iwws/getdata.json', got_data);
    /* Set to refresh in 10 minutes */
    timeout = setTimeout(update_data, 10 * 60 * 60);
}

$(document).ready(function(){
    update_data();

    $('body').bind('turn', function(event, info) {
	draw_graph();
    });
});