/*
    Convert celcius degrees to farenheight.

    Args:
        val
    Returns:
        degrees farenheight, or null if val == null
*/
function c2f (val) {
    if (val == null) {
        return null;
    }
    return (9/5)*val+32;
}

/*
    Convert meters/second to miles/hour.

    from http://www.4wx.com/wxcalc/formulas/windConversion.php
    
    Args:
        val in MPS
    Returns:
        MPS, or null if val == null
*/
function mps2mph (val) {
    if (val == null) {
        return null;
    }
    return val*2.23694;
}

/*
    Convert meters/second to knots.

    from http://www.4wx.com/wxcalc/formulas/windConversion.php

    Args:
        val in MPS.
    Returns:
        knots, or null if value == null
*/
function mps2knots (val) {
    if (val == null) {
        return null;
    }
    return val*1.9438445; 
}

/*
    Convert a degree float to a text direction.

    Args:
        val - (0, 360]
    Returns:
        A text direction if (0, 360], null otherwise.
*/
function dir2txt (val) {
    if (val == null || val <= 0 || val > 360) {
        return null;
    }
    var dirs = new Array("NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW");
    
    // deal with special north case
    if ((val >= 348.75 && val <= 360) || val > 0 && val < 11.25) return "N";
    
    var i = 11.25; // 1/2 degree incerment between the directions
    for (var j=0; j<dirs.length; j++) {
        if (val >= i && val < (i + 22.5)) {
            return dirs[j];
        }
        i += 22.5;
    }
    
    // shouldn't ever get here
    return null;
}

/*
    Parse a Date from a YYYY-MM-DD HH:MM:SS time string.
*/
function parseDate (str) {
    if (str == null || str.length == 0) return null;
    str = str.replace("-", "/");
    str = str.replace("-", "/");
    var epoch = Date.parse(str);
    var date = new Date(epoch);
    return date;
}

function localDate (d) {
    var ldate = new Date();
    return new Date(d.getTime() - ldate.getTimezoneOffset()*60*1000);
}

function formatDate (d) {
    var str = ""+d.getFullYear()+"-";
    var month = d.getMonth() + 1;
    if (month < 10) str += "0" + month;
    else str += month;
    var day = d.getDate();
    if (day < 10) str += "-0"+day;
    else str += "-" + day;
    var hour = d.getHours();
    if (hour < 10) str += " 0" + hour;
    else str += " " + hour;
    var min = d.getMinutes();
    if (min < 10) str += ":0" + min;
    else str += ":" + min;
    var sec = d.getSeconds();
    if (sec < 10) str += ":0" + sec;
    else str += ":" + sec;
    return str;
}

function p2alt (p) {
  var n = .190284;
  var H = 327.538;
  var c1= .0065 * Math.pow(1013.25, n)/288.;
  var c2 = H / Math.pow( (p-.3), n);
  var ff = Math.pow( 1.+c1*c2, 1./n);
  return ((p-.3) * ff * 29.92 / 1013.25);
}

function mm2in (d) {
    return d * 0.0393700787;
}

function in2mm (d) {
	return d * 25.4;
}

function zpad (v, d) {
    var parts = (""+v).split(".");
    
}
