// -------------------------------------------------------------------------------------------
// Funcions meteorològiques.
//

// -------------------------------------------------------------------------------------------
// Índex d'humitat.
//
function index_humitat(nTemp, nHum)
{
var ts, th
    // Passem temperatura de ºC a Fahrenheit
    ts = 1.8 * nTemp + 32;

/*
    // Índex de Thom
    th = 0.4 * (ts + (ts + 35 * Math.log(nHum))) + 4.8;
*/
/*
    //
    th  = -42.379 + 2.04901523 * ts + 10.14333127 * nHum - 0.22475541 * ts * nHum;
    th +=  - 6.83783 * 0.001 * ts - 5.481717 * 0.01 * nHum + 1.22874 * 0.001 * ts * nHum;
    th += 8.5282 *0.0001 * ts * Math.pow(nHum,2) - 1.99 * Math.pow(10,-6) * ts * Math.pow(nHum,2);
*/
/*
    //
    th  = 16.923 + 1.85212 * Math.pow(10,-1) * ts + 5.37941 * nHum - 1.00254 * Math.pow(10,-1) * ts * nHum;
    th += 9.41695 * Math.pow(10,-3) * Math.pow(ts,2) + 7.28898 * Math.pow(10,-3) * Math.pow(nHum,2);
    th += 3.45372 * Math.pow(10,-4) * Math.pow(ts,2) * nHum - 8.14971 * Math.pow(10,-4) * ts * Math.pow(nHum,2);
*/

    //
    temp = ts;
    rh = nHum;
    th  = -42.379 + 2.04901523 * temp + 10.14333127 * rh - 0.22475541 * temp * rh;
    th += -0.00683783 * Math.pow(temp,2) - .05481717 * Math.pow(rh,2);
    th += .00122874 * Math.pow(temp,2) * rh + .00085282 * temp * Math.pow(rh,2);
    th += -.00000199 * Math.pow(temp*rh,2);
    th  = Math.round(100 * th) / 100; 
    th  = (temp < 57) ? temp : th;

    // Passem temperatura de Fahrenheit a ºC
    th = (th - 32) / 1.8;
    return th;
}

// -------------------------------------------------------------------------------------------
// Temperatura de sensació / Wind Chill.
//
function wind_chill(nTemp, nVent)
{
var tf, vm, wc
    // Passem temperatura de ºC a Fahrenheit
    tf = 1.8 * nTemp + 32;
    // Passem vent de m/s a mph
    vm = nVent / 0.44704;

    // Wind Chill
    wc = 0.0817 * (3.71 * Math.pow(vm,0.5) + 5.81 - 0.25 * vm) * (tf - 91.4) + 91.4;

    // Passem temperatura de Fahrenheit a ºC
    wc = (wc - 32) / 1.8;
    return wc;
}

// -------------------------------------------------------------------------------------------
// Punt de rosada.
//
function punt_rosada(nTemp, nHum)
{
    var thisHumidity = nHum;
    var thisTemp = nTemp;
    var thisE = 0;
    var thisDp = 0;

    thisHumidity = parseFloat(thisHumidity);
    thisTemp = parseFloat(thisTemp);
    thisE = (Math.pow(10, (0.66077+((7.5*thisTemp)/(237.3+thisTemp))))*thisHumidity)/100;
    thisLogE = Math.LOG10E * Math.log(thisE);
    thisDp = ((0.66077-thisLogE) * 237.3) / (thisLogE - 8.16077);

    return thisDp;
}

// -------------------------------------------------------------------------------------------
// Temperatura de xafagor / comfort.
//
function temperatura_xafagor(nTemp, nHum)
{
    T = nTemp;
    H = nHum;
    with (Math)
         {
          logPs = 8.6821245 - (2180.28509 / (T + 273.16));
          lnPs = logPs * 2.30258;
          Ps = exp(lnPs);
          Ps = 1.333 * Ps;
          P = H * Ps / 100;
          TH = T + 5 * (P - 10) / 9;
         }
    return TH;
}
