Content deleted Content added
update from Module:WeatherBox/sandbox (see its history for details) per template talk (no substantive change): style fixes (tab indents, trailing whitespace, redundant semicolons); fix unintended globals; Erutuon's move of repeated code to a function |
update from Module:WeatherBox/sandbox; this moves many calculations into a Value object and displays significant trailing zeroes |
||
Line 3:
local wbc = require( "Module:WeatherBoxColors" )
local Value
Value = {
lang = mw.getContentLanguage(),
getDisplay = function (self)
if not self:isValid() then
return nil
end
local display = self.string
if math.abs(self.number) >= 1000 then
display = self.lang:formatNum(math.abs(self.number))
if self.number < 0 then
display = '−' .. display
end
elseif self.number < 0 then
display = '−' .. display:sub(2)
end
return display
end,
getPrecision = function (self)
local result = rawget(self, 'precision')
if not result then
if self:isValid() then
result = math.max(0, math_mod._precision(self.string))
else
result = 0
end
rawset(self, 'precision', result)
end
return result
end,
isValid = function (self)
return self.number ~= nil and self.number ~= -9999
end,
new = function (v)
local val, str
if type(v) == 'string' then
val, str = math_mod._cleanNumber(v)
elseif type(v) == 'number' then
val, str = v, tostring(v)
end
if not val then
val, str = -9999, ''
end
return setmetatable({
number = val,
string = str,
}, Value)
end,
setNumberRounded = function (self, number, precision)
-- precision is a small integer >= 0.
self.number = math_mod._round(number, precision)
local fmt = '%.' .. string.format('%d', precision) .. 'f'
self.string = string.format(fmt, self.number)
end,
}
Value.__index = Value
local function checkFlag( flag, default )
if flag == nil then
return
elseif type( flag ) == 'boolean' then
return flag
Line 22 ⟶ 79:
end
local function makeLine( label,
local result = {'|- style="text-align: center;"\n! scope="row" style="height: 16px;" | ', label, "\n"}
for i = 1,13 do
local color_str = color_values[i]
if i == 13 then
Line 35 ⟶ 90:
end
local display = first_values[i]:getDisplay()
if
table.insert( result,
if
display = second_values[i]:getDisplay()
if
table.insert( result, "<br />
end
end
Line 57 ⟶ 112:
local month_names = { 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'year' }
local str
local values = {}
if suffix == nil then
for
if include_space then
str =
else
str = frame.args[ mon .. group_name ] or ''
end
values[i] = Value.new(str)
end
else
for i, mon in ipairs( month_names ) do
local value, updated
for var, suf in ipairs( suffix ) do
if include_space then
str = frame.args[ mon .. ' ' .. group_name .. ' ' .. suf ]
Line 89 ⟶ 133:
end
if str ~= nil and str ~= '' then
updated = true
break
Line 103 ⟶ 140:
end
if not updated then
value = Value.new()
value.variant = 0
end
values[i] = value
end
end
return values
end
local function getAnnualValue( values, mode )
if mode == 'avg' or mode == 'sum' then
local
local p1, p2, variant
p1 = 0
for i = 1, 12 do
return Value.new()
end
if not variant then
local var = values[i].variant
if var and var ~= 0 then
variant = var
end
end
p2 =
if p2 > p1 then
p1 = p2
end
total = total +
end
local value = Value.new(total)
if mode == 'avg' then
end
value.variant = variant
return value
elseif mode == 'min' then
local
for i = 1, 12 do
if
target = values[i]
end
end
end
return
elseif mode == 'max' then
local
for i = 1, 12 do
if
target = values[i]
end
end
end
return
else
error( 'Unrecognized Annual Mode' )
Line 164 ⟶ 203:
end
local function reconcileTemperature(
for i = 1,13 do
local p
if F_values[i]:isValid() then
p = F_values[i]:getPrecision()
C_values[i]:setNumberRounded( (F_values[i].number - 32)*5/9, p )
end
elseif
if
p = C_values[i]:getPrecision()
end
end
end
end
local function reconcilePrecipitation(
local v_class = 0
for i = 1,13 do
if
v_class = 1
elseif
v_class = 2
end
Line 204 ⟶ 237:
end
for i = 1,13 do
local p
if I_values[i]:isValid() then
if v_class == 1 then
p =
else
p =
end
M_values[i].variant = v_class
end
elseif
if
if
p = M_values[i]:getPrecision()
else
p =
end
end
end
end
end
Line 237 ⟶ 267:
local mode = (frame.args.mode or 'basic'):lower()
local group_name = frame.args.group_name
local first_values, second_values
local
local color_scheme = frame.args.color_scheme or 't'
local scale_factor = math_mod._cleanNumber( frame.args.scale_factor) or 1
local date_mode = checkFlag( frame.args.date_mode
local label = frame.args.label or ''
local annual_mode = (frame.args.annual_mode or 'avg'):lower()
local include_space = checkFlag( frame.args.include_space
local second_line = checkFlag( frame.args.second_line, false )
local prefer_cm = checkFlag( frame.args.prefer_cm, false )
local pframe = frame:getParent()
local imperial_first = checkFlag( frame.args['imperial first'] or pframe.args['imperial first'] )
local single_line = checkFlag( frame.args['single line'] or pframe.args['single line'] )
if imperial_first == nil then
end
if mode == 'basic' then
second_values = nil
elseif mode == 'temperature' then
reconcileTemperature( first_values, second_values )
elseif mode == "precipitation" then
reconcilePrecipitation( first_values, second_values, prefer_cm )
else
error( 'Requested mode not recognized' )
Line 294 ⟶ 303:
local good = false
for i = 1,13 do
if
good = true
break
Line 303 ⟶ 312:
end
if
end
if
if
end
end
Line 324 ⟶ 325:
color_values = {}
local month_adj = { 31/30, 28.25/30, 31/30, 1, 31/30, 1,
31/30, 31/30, 1, 31/30, 1, 31/30,
for i = 1,13 do
if first_values[i]:isValid() then
local adj = scale_factor
if date_mode then
adj = adj / month_adj[i]
end
if mode == "precipitation" then
if
adj = adj * 10
end
end
table.insert( color_values, color_scheme(
else
table.insert( color_values, color_scheme( nil ) )
Line 343:
end
if imperial_first and second_values ~= nil then
first_values, second_values = second_values, first_values
end
if not single_line then
if second_line and
first_values = second_values
end
end
return makeLine( label,
end
|