var WindMode = 0

//=========================================================
//
// Converts Fahrenheit to Celsius
//
//=========================================================
function Fahr2Cel(tFahr)
	{tCel = (tFahr-32) * (5/9)
	return tCel}

//=========================================================
//
// Converts Celsius to Fahrenheit
//
//=========================================================
function Cel2Fahr (tCel)
	{tFahr = tCel * (9/5) + 32
	return tFahr}

//=========================================================
//
// Converts inches to millimeters (0.2 mm rounding)
//
//=========================================================
function Inches2mm (inches)
	{mm = inches * 25.4
	 mm = Math.round(mm * 5)
   mm = (mm / 5)
	return mm}

//=========================================================
//
// Converts millibars to inches of mercury
//
//=========================================================
function mb2inches(pressMB)
	{pressIN = pressMB/33.864
	 pressIN = (Math.round(pressIN * 100) / 100)
	return pressIN}

//=========================================================
//
// Converts miles per hour to kilometers per hour
//
//=========================================================
function mph2kph(speedMPH)
	{speedKPH = speedMPH*1.609
	return speedKPH}

//=========================================================
//
// Formats wind stirng
//
//=========================================================

function NormaliseWindSpeed (wcgust) {
    if (wcgust != parseInt(wcgust)) {
        wcgust = 0 }
    return wcgust}
    
function GetWindStringNative (StaWindSpeedCurr) {
    if (CalcWindGust10 != parseInt(CalcWindGust10)) {
        CalcWindGust10 = 0 }
    if (CalcWindAvg02 != parseFloat(CalcWindAvg02)) {
        CalcWindAvg02 = 0 }
        
    WindStringNative=99    
      if (CalcWindGust10 == 0)
        {
           if (StaWindSpeedCurr == 0.0) 
             {
               WindMode = 0
             }  
           else
             {
               WindMode = 1
             }
         } 
       else 
         {
           if (CalcWindAvg02 <=3)
              { if (CalcWindGust10 == 0)
                   {
                      WindMode = 0
                   }
                 else            
                  {
                      WindMode = 2
                  }
             }    
           else
             {
               WindMode = 3
             }
          }
          SetWindStringNative (WindMode)
          return WindStringNative
       }
       
function SetWindStringNative (n)
    {
      if (WindMode == 0) 
        { 
           WindStringNative = "Calm"
        }
          
      if (WindMode == 1) 
        {
           WindStringNative =  StaWindDirCurr
           WindStringNative += ' ('
           WindStringNative += PadDigits(StaWindDegCurr, 3)
           WindStringNative += '°) at '
           WindStringNative += setDecimal(StaWindSpeedCurr,0)
           WindStringNative += ' '
           WindStringNative += WindUnit
        }  
             
       if (WindMode == 2) 
        {
           WindStringNative =  StaWindDirCurr
           WindStringNative += ' ('
           WindStringNative += PadDigits(StaWindDegCurr, 3)
           WindStringNative += '°) at '
           WindStringNative += setDecimal(CalcWindGust10,0)
           WindStringNative += ' '
           WindStringNative += WindUnit
        }        
             
             
      if (WindMode == 3) 
        {
           WindStringNative =  StaWindDirCurr
           WindStringNative += ' ('
           WindStringNative += PadDigits(StaWindDegCurr, 3)
           WindStringNative += '°) at '
           WindStringNative += setDecimal(CalcWindAvg02,1)
           WindStringNative += ' G '
           WindStringNative += setDecimal(CalcWindGust10,0)
           WindStringNative += ' '
           WindStringNative += WindUnit
         }        
       return WindStringNative
     }
       

function SetWindStringAlt (WindMode)
    {
      if (WindMode == 0) 
        { 
           WindStringAlt = ""
        }
          
      if (WindMode == 1) 
        {
           GetBeaufortString (StaWindSpeedCurr)
           WindStringAlt =  '('
           WindStringAlt += setDecimal(mph2kph(StaWindSpeedCurr),0)
           WindStringAlt += ' '
           WindStringAlt += AltWindUnit
           WindStringAlt += ') '
           WindStringAlt += BeaufortString
        }  
             
       if (WindMode == 2) 
        {
           GetBeaufortString (CalcWindGust10)
           WindStringAlt =  '('
           WindStringAlt += setDecimal(mph2kph(CalcWindGust10),0)
           WindStringAlt += ' '
           WindStringAlt += AltWindUnit
           WindStringAlt += ') '
           WindStringAlt += BeaufortString
        }        
             
             
      if (WindMode == 3) 
        {
           GetBeaufortString (CalcWindGust10)
           WindStringAlt =  '('
           WindStringAlt += setDecimal(mph2kph(CalcWindAvg02),1)
           WindStringAlt += ' G '
           WindStringAlt += setDecimal(mph2kph(CalcWindGust10),0)
           WindStringAlt += ' '
           WindStringAlt += AltWindUnit
           WindStringAlt += ') '
           WindStringAlt += BeaufortString
         }        
       return WindStringAlt
     }
     
function GetBeaufortString (speed)
    {
    BeaufortString = '- Light Air [Force 1]'
    if (speed >= 4)
      {BeaufortString = '- Light Breeze [Force 2]'}
    if (speed >= 8)
      {BeaufortString = '- Gentle Breeze [Force 3]'}
    if (speed >= 13)
      {BeaufortString = '- Moderate Breeze [Force 4]'}
    if (speed >= 19)
      {BeaufortString = '- Fresh Breeze [Force 5]'}
    if (speed >= 25)
      {BeaufortString = '- Strong Breeze [Force 6]'}
    if (speed >= 32)
      {BeaufortString = '- Moderate Gale [Force 7]'}
    if (speed >= 39)
      {BeaufortString = '- Fresh Gale [Force 8]'}
    if (speed >= 47)
      {BeaufortString = '- Strong Gale [Force 9]'}
    if (speed >= 55)
      {BeaufortString = '- Storm [Force 10]'}
    if (speed >= 64)
      {BeaufortString = '- Violent Storm [Force 11]'}
    if (speed >= 75)
      {BeaufortString = '- Hurricane Force (Cat 1)'}
    if (speed >= 96)
      {BeaufortString = '- Hurricane Force (Cat 2)'}
    if (speed >= 111)
      {BeaufortString = '- Hurricane Force (Cat 3)'}
    if (speed >= 131)
      {BeaufortString = '- Hurricane Force (Cat 4)'}
    if (speed >= 155)
      {BeaufortString = '- Hurricane Force (Cat 5)'}                  
    return BeaufortString
    }
    
//=========================================================
//
// Get UV Index Tag
//
//=========================================================

function getUVString (uvindex)
    {
    UVString = '[Nil]'
    if (uvindex > 0)
      {UVString = '[Low]'}
    if (uvindex > 2.9)
      {UVString = '[Moderate]'}
    if (uvindex > 5.9)
      {UVString = '[High]'}
    if (uvindex > 7.9)
      {UVString = '[Very High]'}
    if (uvindex > 10.9)
      {UVString = '[Extreme]'}
    return UVString
    }
    

function GetCompassPoint (dirdeg)
      {
      dirdegInt = Math.floor((dirdeg / 360 * 32) + 0.5)
      if (dirdegInt == 32) {
        dirdegInt = 0}
      return CompassPoint[dirdegInt]
      }
      
CompassPoint = new Array(32)
   CompassPoint[0] = "N"
   CompassPoint[1] = "NbE"
   CompassPoint[2] = "NNE"
   CompassPoint[3] = "NEbN"
   CompassPoint[4] = "NE"
   CompassPoint[5] = "NEbE"
   CompassPoint[6] = "ENE"
   CompassPoint[7] = "EbN"
   CompassPoint[8] = "E"
   CompassPoint[9] = "EbS"
   CompassPoint[10] = "ESE"
   CompassPoint[11] = "SEbE"
   CompassPoint[12] = "SE"
   CompassPoint[13] = "SEbS"
   CompassPoint[14] = "SSE"
   CompassPoint[15] = "SbE"
   CompassPoint[16] = "S"
   CompassPoint[17] = "SbW"
   CompassPoint[18] = "SSW"
   CompassPoint[19] = "SWbS"
   CompassPoint[20] = "SW"
   CompassPoint[21] = "SWbW"
   CompassPoint[22] = "WSW"
   CompassPoint[23] = "WbS"
   CompassPoint[24] = "W"
   CompassPoint[25] = "WbN"
   CompassPoint[26] = "WNW"
   CompassPoint[27] = "NWbW"
   CompassPoint[28] = "NW"
   CompassPoint[29] = "NWbN"
   CompassPoint[30] = "NNW"
   CompassPoint[31] = "NbW"




//=========================================================
//
// Computes wind chill
//
//=========================================================
function CalcWindChill(temp1, gust1)
	{
	var wcgust = gust1
	NormaliseWindSpeed (wcgust)
	WindChill=temp1
	if (temp1 <= 68)
    {GustRaised = Math.pow(gust1, 0.16)
   	 WindChill = (35.74 + (0.6215 * temp1) - (35.75 * GustRaised) + (0.4275 * temp1 * GustRaised))
     WindChill = Math.min(WindChill, temp1)}
     
	return WindChill}
	
	
//=========================================================
//
// Computes humidex (deg Fahr)
//
//=========================================================
function CalcHumidex(temp1, dp1)
	{
    dp1 = ((dp1 - 32) * 5 / 9) + 273.16
    temp1 = (temp1 - 32) * 5 / 9
    factor1 = ((1 / 273.16) - (1 / dp1)) * 5417.753 
    factor2 = Math.pow(2.718281828, factor1)
   	Humidex = temp1 + (0.5555 * ((6.11 * factor2) - 10))
    Humidex = (Humidex * 1.8) + 32
	   
	return Humidex}
		
		
//=========================================================
//
// Computes Temperature-Humidity Index (deg Fahr)
//
//=========================================================
function CalcTHI(temp1, rh1)
	{ 
	THI = temp1
    if (temp1 >= 80)
    {THI =
     16.923 + 
    (1.85212 * Math.pow(10, -1) * temp1) +
    (5.37941 * rh1) -
    (1.00254 * Math.pow(10, -1) * temp1 * rh1) +
    (9.41695 * Math.pow(10, -3) * Math.pow(temp1, 2)) +
    (7.28898 * Math.pow(10, -3) * Math.pow(rh1, 2)) +
    (3.45372 * Math.pow(10, -4) * Math.pow(temp1, 2) * rh1) -
    (8.14971 * Math.pow(10, -4) * temp1 * Math.pow(rh1, 2)) +
    (1.02102 * Math.pow(10, -5) * Math.pow(rh1, 2) * Math.pow(temp1, 2)) -
    (3.86460 * Math.pow(10, -5) * Math.pow(temp1, 3)) +
    (2.91583 * Math.pow(10, -5) * Math.pow(rh1, 3)) +
    (1.42721 * Math.pow(10, -6) * rh1 * Math.pow(temp1, 3)) +
    (1.97483 * Math.pow(10, -7) * Math.pow(rh1, 3) * temp1) -
    (2.18429 * Math.pow(10, -8) * Math.pow(rh1, 2) * Math.pow(temp1, 3)) +
    (8.43296 * Math.pow(10, -10) * Math.pow(rh1, 3) * Math.pow(temp1, 2)) -
    (4.81975 * Math.pow(10, -11) * Math.pow(rh1, 3) * Math.pow(temp1, 3))}
	   
	return THI}
	   
	
//=========================================================
//
// Computes wet-bulb temperature
//
//=========================================================
function calcWetbulb(pressure_mb, temp_far, dewpoint_far)
	{temp_celsius = Far2Cel(temp_far)
	dewpoint_celsius = Far2Cel(dewpoint_far)
	tmin = Math.min(dewpoint_celsius, temp_celsius)
	tmax = Math.max(dewpoint_celsius, temp_celsius)
	vapor_pressure = 6.112 * Math.pow(10, (7.5 * dewpoint_celsius) / (237.7 + dewpoint_celsius))
	while (true)
	   	{tcur = (tmax + tmin) / 2
	   	vpcur = 6.112 * Math.pow(10, (7.5 * tcur) / (237.7 + tcur))
	   	peq = 0.00066 * (1+0.00155 * tcur) * pressure_mb * (temp_celsius - tcur)
	   	diff = peq - vpcur + vapor_pressure
	   	if (Math.abs(diff) < 0.01) break
	   	if (diff < 0) {tmax = tcur} else {tmin = tcur}}
	wetbulb_far = Cel2Far(tcur)    
	return wetbulb_far}

//=========================================================
//
// Calculates monthly precip departure
//
//=========================================================
function monthlyDepart(monthprecip, dayofmonth, monthofyear) 
	{normaltodate = 0
	i = 1
	while (i <= dayofmonth) {normaltodate = normaltodate + parseFloat(normals_records [monthofyear] [i].substring(6,9)), i++}
	departmonth = monthprecip - normaltodate
	return departmonth}

//=========================================================
//
// Calculates yearly precip departure
//
//=========================================================
function yearlyDepart(monthofyear, yearprecip, norm_month_to_date)
	{normal_prior_rain = 0
	i = 1
	while (i < monthofyear) {normal_prior_rain = normal_prior_rain + monthly_avg_rain[i], i++}
	normaltotal = normal_prior_rain + norm_month_to_date
	departyear = yearprecip - normaltotal
	return departyear} 

//=========================================================
//
// Calculates monthly snowfall departure
//
//=========================================================
function monthlySnowDepart(monthsnow, dayofmonth, monthofyear) 
	{normalsnowtodate = (monthly_avg_snow[monthofyear] / days_in_month[monthofyear]) * dayofmonth
	departsnow = monthsnow - normalsnowtodate
	return departsnow}

//=========================================================
//
// Calculates yearly snowfall departure
//
//=========================================================
function seasonSnowDepart(monthofyear, seasonsnow, norm_month_to_date)
	{i = 7
	normal_through_dec = 0
	while (i <= 12) {normal_through_dec = normal_through_dec + monthly_avg_snow[i], i++}
	if (monthofyear < 7) {i = 1, normal_prior_snow = normal_through_dec} else {i = 7, normal_prior_snow = 0}
	while (i < monthofyear) {normal_prior_snow = normal_prior_snow + monthly_avg_snow[i], i++}
	normalsnow = normal_prior_snow + norm_month_to_date
	departseason = seasonsnow - normalsnow
	return departseason} 
	
