Difference between revisions of "Module:Weather"

From blackwiki
Jump to navigation Jump to search
blackwiki>Johnuniq
(efficient implementation of cells in tables of weather data, starting with temperature)
 
blackwiki>Johnuniq
(fix bug in color algorithm in an edit (27 August 2015) to Template:Weather box/colt (which this module copied))
Line 23: Line 23:
 
end
 
end
 
local red, green, blue
 
local red, green, blue
if value < -1000 or value > 60 then
+
if value < -42.75 or value > 60 then
 
red = 0
 
red = 0
 
elseif value < 4.47 then
 
elseif value < 4.47 then

Revision as of 10:52, 11 September 2016

This module can be used to display temperatures in a table. It is under development and is intended to be efficient so a page can hold many tables.

  • Input numbers must use a hyphen if negative (Unicode minus "−" would give an error).
  • All displayed numbers (including inputs) use Unicode minus if negative.
  • The input consists of 13 values, separated by any number of spaces.
  • Each of the 13 values must be a number. Any invalid value results in the corresponding cell being blank with no error message or tracking category.

The following functions are available:

Function Input Output
CtoF °C °C
(°F)
FfromC °C °F
(°C)
CfromF °F °C
(°F)
FtoC °F °F
(°C)

The following templates use the module:

An example using the above templates and the module is at:

The output can be examined by entering the following example at Special:ExpandTemplates. The output from each of the following lines is identical.

{{#invoke:weather|CtoF|-10 -5 0 5 10 15 20 25 30 35 40 45 50}}
----
{{#invoke:weather|CfromF|14 23 32 41 50 59 68 77 86 95 104 113 122}}

Optional parameter

  • |palette=name
    Where name is the built-in name of a palette:
    cool (default)
    cool2 (modified)
    cool2avg (modified and intended for average temperatures)

Testing

The function show provides a way to test the color schemes. It generates a graph of how the red, green, and blue portions of the color vary with temperature, and a table of the full range of temperatures in °C.

Optional parameters

  • Two unnamed parameters may be entered to specify the first and last Celsius temperatures (|-90|59 by default).
  • The palette can be specified with |palette=name as above.

The following codes produce the same result:

  • {{#invoke:weather|show}}
  • {{#invoke:weather|show|palette=cool}}

Script error: The function "show" does not exist.

Modified palette:

  • {{#invoke:weather|show|palette=cool2}}


Script error: The function "show" does not exist.

This modified palette is intended for average temperatures. It results in good colors between the extreme highest average monthly temperature of +39 °C (102 °F) in Death Valley, California, and −68 °C (−90 °F), the extreme lowest average monthly temperature at Vostok Station, at a high elevation on the Antarctic ice sheet. These are the highest and lowest known average temperatures recorded on Earth, not to be confused with the highest and lowest records, which are quite a bit hotter and colder.


  • {{#invoke:weather|show|palette=cool2avg}}

Script error: The function "show" does not exist.


-- Efficient (fast) functions to implement cells in tables of weather data.
-- Temperature conversion is built-in, but for simplicity, temperatures
-- are assumed to be for habitable locations (from -100 to 100 °C).

local MINUS = '−'  -- Unicode U+2212 MINUS SIGN

local function temperature_style(value)
	-- Return style for a table cell based on the given value which
	-- should be a temperature in °C.
	local function show(bg, fg)
		if not fg and value and (value < -23.3 or value >= 37.8) then
			fg = 'FFFFFF'
		end
		if fg then
			fg = 'color:#' .. fg .. ';'
		else
			fg = ''
		end
		return 'style="background:#' .. bg .. ';' .. fg .. ' font-size:85%;"'
	end
	if type(value) ~= 'number' then
		return show('FFFFFF', '000000')
	end
	local red, green, blue
	if value < -42.75 or value > 60 then
		red = 0
	elseif value < 4.47 then
		red = 5.4 * (42.75 + value)
	elseif value > 41.5 then
		red = 13.78 * (60 - value)
	else
		red = 255
	end
	if value < -42.75 or value > 41.5 then
		green = 0
	elseif value < 4.47 then
		green = 5.4 * (42.75 + value)
	elseif value > 4.5 then
		green = 6.89 * (41.5 - value)
	else
		green = 255
	end
	if value < -90 or value > 23 then
		blue = 0
	elseif value < -42.78 then
		blue = 5.4 * (90 + value)
	elseif value > 4.5 then
		blue = 13.78 * (23 - value)
	else
		blue = 255
	end
	return show(string.format('%02X%02X%02X', red, green, blue))
end

local function format_cell(value, intext, outtext)
	-- Return one line of wikitext to make a cell in a table.
	if not value then
		return '|\n'
	end
	local text
	if outtext then
		text = intext .. '<br>(' .. outtext .. ')'
	else
		text = intext
	end
	return '| ' .. temperature_style(value) .. ' | ' .. text .. '\n'
end

local function process_temperature(intext, inunit, swap)
	-- Convert °C to °F or vice versa, assuming the temperature is for a
	-- habitable location, well inside the range -100 to 100 °C.
	-- That simplifies determining precision and formatting (no commas are needed).
	-- Return (celsius_value, intext, outtext) if valid; otherwise return nil.
	-- The returned input and output are swapped if requested.
	-- Each returned string has a Unicode MINUS as sign, if negative.
	local invalue = tonumber(intext)
	if not invalue then return nil end
	local integer, dot, decimals = intext:match('^%s*%-?(%d+)(%.?)(%d*)%s*$')
	if not integer then return nil end
	if invalue < 0 then
		intext = MINUS .. integer .. dot .. decimals
	end
	local outtext
	if inunit == 'C' or inunit == 'F' then
		local celsius_value, outvalue
		if inunit == 'C' then
			outvalue = invalue * (9/5) + 32
			celsius_value = invalue
		else
			outvalue = (invalue - 32) * (5/9)
			celsius_value = outvalue
		end
		local precision = dot == '' and 0 or #decimals
		outtext = string.format('%.' .. precision .. 'f', math.abs(outvalue) + 2e-14)
		if outvalue < 0 and tonumber(outtext) ~= 0 then
			-- Don't show minus if result is negative but rounds to zero.
			outtext = MINUS .. outtext
		end
		if swap then
			return celsius_value, outtext, intext
		end
		return celsius_value, intext, outtext
	end
	-- LATER Think about whether a no-conversion option would be useful.
	return invalue, intext, outtext
end

local function temperature_row(row, inunit, swap)
	-- Return 13 lines specifying the style/content of 13 table cells.
	-- Input is 13 space-separated words, each a number (°C or °F).
	-- Any word that is not a number gives a blank cell ("M" for a missing cell).
	-- Any excess words are ignored.
	--
	-- Function  Input   Output
	-- ------------------------
	-- CtoF        C       C/F
	-- FfromC      C       F/C
	-- CfromF      F       C/F
	-- FtoC        F       F/C
	local nrcol = 13
	local results, n = {}, 0
	for word in row:gmatch('%S+') do
		n = n + 1
		if n > nrcol then
			break
		end
		results[n] = format_cell(process_temperature(word, inunit, swap))
	end
	for i = n + 1, nrcol do
		results[i] = format_cell()
	end
	return table.concat(results)
end

local function CtoF(frame)
	return temperature_row(frame.args[1], 'C')
end

local function CfromF(frame)
	return temperature_row(frame.args[1], 'F', true)
end

local function FtoC(frame)
	return temperature_row(frame.args[1], 'F')
end

local function FfromC(frame)
	return temperature_row(frame.args[1], 'C', true)
end

return {
	CtoF = CtoF,
	CfromF = CfromF,
	FtoC = FtoC,
	FfromC = FfromC,
}