Content deleted Content added
update from Module:WeatherBox/sandbox; this moves many calculations into a Value object, displays significant trailing zeroes and gives more accurate conversions |
update from sandbox: tweak to reduce duplication |
||
(5 intermediate revisions by 3 users not shown) | |||
Line 1:
local
local traceText
local Value
Value = {
lang = mw.getContentLanguage(),
getDisplay = function (self, second)
if not self:isValid() then
return nil
end
local display = self.string
if second then
-- If a cell displays "cm (inch)", show "trace" not "trace (trace)".
return nil
end▼
return traceText or 'trace'
end
if math.abs(self.number) >= 1000 then
display = self.lang:formatNum(math.abs(self.number))
Line 37 ⟶ 43:
end,
new = function (v)
local val, str, precision
if type(v) == 'string' then
if v == 'trace' then
val, str = math_mod._cleanNumber(v)▼
val, str, precision = 0, 'trace', 0
else
▲ val, str = math_mod._cleanNumber(v)
end
elseif type(v) == 'number' then
val, str = v, tostring(v)
Line 49 ⟶ 59:
number = val,
string = str,
precision = precision,
}, Value)
end,
Line 59 ⟶ 70:
setConvert = function (self, invalue, units)
-- Use method modified from [[Module:Convert]] to determine precision.
if invalue.string == 'trace' then
self.number, self.string, self.precision = 0, 'trace', 0
return
end
local convert = self.converts[units] or error('Unknown units')
local outnum = invalue.number * convert.factor
Line 88 ⟶ 103:
Value.__index = Value
local function checkFlag(
if flag == nil then
return default
elseif type(
return flag
elseif type(
flag = flag:lower()
if flag == '0' or flag == 'false' or
Line 103 ⟶ 118:
end
else
return error(
end
end
local function makeLine(
local result = {'|- style="text-align: center;"\n! scope="row" style="height: 16px;" | ', label, "\n"}
for i = 1, 13 do
(i == 13 and ' border-left-width:medium"' or '"') ..
▲ if i == 13 then
' class="notheme"| '
▲ end
local display = first_values[i]:getDisplay()
if display then
table.insert(
if second_values ~= nil then
display = second_values[i]:getDisplay(true)
if display then
table.insert(
end
end
else
table.insert(
end
▲ table.insert( result, "\n" )
end
return table.concat(result)
▲ return table.concat( result )
end
local function getInputs(
local month_names = { 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'year' }
Line 143 ⟶ 153:
local values = {}
if suffix == nil then
for i, mon in ipairs(
if include_space then
str =
else
str =
end
values[i] = Value.new(str)
end
else
for i, mon in ipairs(
local value, updated
for var, suf in ipairs(
if include_space then
str =
else
str =
end
if str ~= nil and str ~= '' then
Line 177 ⟶ 187:
end
local function getAnnualValue(
if mode == 'avg' or mode == 'sum' then
local total = 0
Line 192 ⟶ 202:
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(
end
value.variant = variant
Line 227 ⟶ 235:
return target or Value.new()
else
error(
end
end
local function reconcileTemperature(
for i = 1, 13 do
local p
if C_values[i].string == '' then
if F_values[i]:isValid() then
p = F_values[i]:getPrecision()
C_values[i]:setNumberRounded(
end
elseif F_values[i].string == '' then
if C_values[i]:isValid() then
p = C_values[i]:getPrecision()
F_values[i]:setNumberRounded(
end
end
Line 248 ⟶ 256:
end
local function reconcilePrecipitation(
local v_class = 0
for i = 1, 13 do
if M_values[i].variant == 1 then
v_class = 1
Line 264 ⟶ 272:
end
end
for i = 1, 13 do
local units
if M_values[i].string == '' then
Line 273 ⟶ 281:
units = 'in2mm'
end
M_values[i]:setConvert(
M_values[i].variant = v_class
end
Line 283 ⟶ 291:
units = 'mm2in'
end
I_values[i]:setConvert(
end
end
Line 289 ⟶ 297:
end
local function
options = options or {}
local mode = (frame.args.mode or 'basic'):lower()▼
local wbc = require('Module:Weather box/colors' .. (options.sandbox or ''))
local group_name = frame.args.group_name▼
local first_values, second_values
local color_values
local color_scheme =
local scale_factor = math_mod._cleanNumber(
local date_mode = checkFlag(
local label =
local annual_mode = (
local include_space = checkFlag(
local second_line = checkFlag(
local prefer_cm = checkFlag(
local imperial_first = checkFlag(args['imperial first'])
local
local
local trace = args.trace
if trace and trace ~= '' then
traceText = trace
end
if imperial_first == nil then
imperial_first = metric_first == nil and true or not metric_first
end
if mode == 'basic' then
first_values = getInputs(
second_values = nil
elseif mode == 'temperature' then
first_values = getInputs(
second_values = getInputs(
reconcileTemperature(
elseif mode == "precipitation" then
first_values = getInputs(
second_values = getInputs(
reconcilePrecipitation(
else
error(
end
local good = false
for i = 1, 13 do
if first_values[i].string ~= '' then
good = true
Line 336 ⟶ 346:
return ''
end
if first_values[13].string == '' then
first_values[13] = getAnnualValue(
end
if second_values ~= nil then
if second_values[13].string == '' then
second_values[13] = getAnnualValue(
end
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
Line 362 ⟶ 369:
end
end
table.insert(
else
table.insert(
end
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 second_values ~= nil then
first_values = second_values
Line 378 ⟶ 383:
second_values = nil
end
end
local function buildRow(frame)
▲ return makeLine( label, first_values, second_values, color_values )
return _buildRow(frame.args, frame:getParent().args) -- row definition, template parameters
end
return
buildRow = buildRow,
_buildRow = _buildRow,
}
|