Moduuli:Weather box/row
Siirry navigaatioon
Siirry hakuun
Tämän moduulin ohjeistuksen voi tehdä sivulle Moduuli:Weather box/row/ohje
local math_mod = require('Module:Math')
local traceText
local Value
Value = {
lang = mw.getContentLanguage(),
getDisplay = function (self)
if not self:isValid() then
return nil
end
local display = self.string
if display == 'trace' then
return traceText or 'trace'
end
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, precision
if type(v) == 'string' then
if v == 'trace' then
val, str, precision = 0, 'trace', 0
else
val, str = math_mod._cleanNumber(v)
end
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,
precision = precision,
}, Value)
end,
setNumberRounded = function (self, number, precision)
if precision > 2 then
precision = 2
end
self.number = math_mod._round(number, precision)
if precision < 0 then
self.string = tostring(self.number)
else
local fmt = '%.' .. string.format('%d', precision) .. 'f'
self.string = string.format(fmt, self.number)
end
end,
}
Value.__index = Value
local function checkFlag(flag, default)
if flag == nil then
return default
elseif type(flag) == 'boolean' then
return flag
elseif type(flag) == 'string' then
flag = flag:lower()
if flag == '0' or flag == 'false' or
flag == '' or flag == 'no' or
flag == 'n' then
return false
else
return true
end
else
return error('Flag type not valid')
end
end
local function makeLine(label, first_values, color_values)
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
table.insert(result, table.concat( {'|style="', color_str, ' border-left-width:medium" class="notheme"| '} ))
else
table.insert(result, table.concat( {'|style="', color_str, '" class="notheme"| '} ))
end
local display = first_values[i]:getDisplay()
if display then
table.insert(result, display)
else
table.insert(result, '—')
end
table.insert(result, "\n")
end
return table.concat(result)
end
local function getInputs(args, group_name, suffix, include_space)
--[[
local month_names = { 'tammi', 'helmi', 'maalis', 'huhti', 'touko', 'kesä',
'heinä', 'elo', 'syys', 'loka', 'marras', 'joulu', 'vuosi' }
--]]
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 i, mon in ipairs(month_names) do
if include_space then
str = args[ mon .. ' ' .. group_name ] or ''
else
str = 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 = args[ mon .. ' ' .. group_name .. ' ' .. suf ]
else
str = args[ mon .. group_name .. ' ' .. suf ]
end
if str ~= nil and str ~= '' then
value = Value.new(str)
value.variant = var
updated = true
break
end
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 total = 0
local p1, p2, variant
p1 = 0
for i = 1, 12 do
if not values[i]:isValid() then
return Value.new()
end
if not variant then
local var = values[i].variant
if var and var ~= 0 then
variant = var
end
end
p2 = values[i]:getPrecision()
if p2 > p1 then
p1 = p2
end
total = total + values[i].number
end
local value = Value.new(total)
if mode == 'avg' then
value:setNumberRounded(total / 12, p1)
end
value.variant = variant
return value
elseif mode == 'min' then
local target
for i = 1, 12 do
if values[i]:isValid() then
if target == nil or values[i].number < target.number then
target = values[i]
end
end
end
return target or Value.new()
elseif mode == 'max' then
local target
for i = 1, 12 do
if values[i]:isValid() then
if target == nil or values[i].number > target.number then
target = values[i]
end
end
end
return target or Value.new()
else
error('Unrecognized Annual Mode')
end
end
local function _buildRow(definition, args, options)
options = options or {}
local wbc = require('Module:Weather box/colors' .. (options.sandbox or ''))
local mode = (definition.mode or 'basic'):lower()
local group_name = definition.group_name
local first_values
local color_values
local color_scheme = definition.color_scheme or 't'
local scale_factor = math_mod._cleanNumber(definition.scale_factor) or 1
local date_mode = checkFlag(definition.date_mode, false)
local label = definition.label or ''
local annual_mode = (definition.annual_mode or 'avg'):lower()
local include_space = checkFlag(definition.include_space, true)
local trace = args.trace
if trace and trace ~= '' then
traceText = trace
end
if mode == 'basic' then
first_values = getInputs(args, group_name, nil, include_space)
elseif mode == 'temperature' then
first_values = getInputs(args, group_name, {'C'}, include_space)
elseif mode == "precipitation" then
first_values = getInputs(args, group_name, {'cm', 'mm'}, include_space)
else
error('Requested mode not recognized')
end
local good = false
for i = 1, 13 do
if first_values[i].string ~= '' then
good = true
break
end
end
if not good then
return ''
end
if first_values[13].string == '' then
first_values[13] = getAnnualValue(first_values, annual_mode)
end
color_scheme = wbc.interpret_color_code(color_scheme)
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, 365.25/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 first_values[i].variant == 1 then
adj = adj * 10
end
end
table.insert(color_values, color_scheme(first_values[i].number * adj))
else
table.insert(color_values, color_scheme(nil))
end
end
return makeLine(label, first_values, color_values)
end
local function buildRow(frame)
return _buildRow(frame.args, frame:getParent().args) -- row definition, template parameters
end
return {
buildRow = buildRow,
_buildRow = _buildRow,
}