It is currently April 16th, 2024, 8:13 pm

Using Lua to calculate "feels like" on a weather skin

Discuss the use of Lua in Script measures.
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Using Lua to calculate "feels like" on a weather skin

Post by jsmorley »

Some weather sites provide a "feels like" temperature and some don't. If the one you use doesn't, and you would like to calculate that value based on the formulas used by the National Oceanic and Atmospheric Administration (NOAA) then you can use this .lua.

This is from my skin, and needs some changes to retrieve the correct measures and return values to meters as you need, but the core of it should be helpful in getting you started.

Code: Select all

PROPERTIES =
{

}

function Initialize()

	MeasureTemp = SKIN:GetMeasure("MeasureNowWeatherTemp")
	MeasureHumidity = SKIN:GetMeasure("MeasureNowHumidity")
	MeasureWind = SKIN:GetMeasure("MeasureNowWind")
	StartingUnit = SKIN:GetVariable("Unit")
	
end -->Initialize

function Update()

	SkinTemp = MeasureTemp:GetValue()
	SkinWind = MeasureWind:GetValue()
	Humid = MeasureHumidity:GetValue()
	
	if string.upper(StartingUnit) == "C" then
		TempF = round((9/5)*SkinTemp+32)
		WindM = round(SkinWind * 1.60934400061)
	else
		TempF = SkinTemp
		WindM = SkinWind
	end
	
	if TempF > 80 and Humid > 40 then
		Feels = -42.379 + 2.04901523 * TempF + 10.14333127 * Humid - 0.22475541 * TempF * Humid - 6.83783 * 10^(-3)*(TempF^(2)) - 5.481717 * 10^(-2)*(Humid^(2)) + 1.22874 * 10^(-3)*(TempF^(2))*(Humid) + 8.5282 * 10^(-4)*(TempF)*(Humid^(2)) - 1.99 * 10^(-6)*(TempF^(2))*(Humid^(2))
	elseif  TempF < 41 and WindM > 4 then
		Feels = 35.74 + 0.6215 * TempF - 35.75 * WindM ^ 0.16 + 0.4275 * TempF * WindM ^ 0.16
	else
		Feels = TempF
	end	
	
	if string.upper(StartingUnit) == "C" then
		Feels = round((5/9)*(Feels-32))
	else
		Feels = round(Feels)
	end	
	
	return Feels
	
end -->Update

function round (x)
  if x >= 0 then
    return math.floor (x + 0.5)
  end  -->round if positive
  return math.ceil (x - 0.5)
end -->round if negative