Module:Weather box/row: Difference between revisions

Content deleted Content added
update from Module:WeatherBox/sandbox; this moves many calculations into a Value object and displays significant trailing zeroes
update from sandbox: tweak to reduce duplication
 
(7 intermediate revisions by 3 users not shown)
Line 1:
local wmath_mod = {}require('Module:Math')
local math_mod = require( "Module:Math" )
local wbc = require( "Module:WeatherBoxColors" )
 
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 display == 'trace' then
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,
converts = {
in2cm = { factor = 2.54 },
in2mm = { factor = 25.4 },
cm2in = { factor = 1/2.54, p2max = 1 },
mm2in = { factor = 1/25.4, p2max = 0 },
},
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
local precision = invalue:getPrecision()
if outnum > 0 then
local adjust = math.log10(1/convert.factor) + math.log10(2)
local p1 = math.floor(precision + adjust)
local p2 = 1 - math.floor(math.log10(outnum))
if convert.p2max then
p2 = math.min(p2, convert.p2max)
end
precision = math.max(p1, p2)
end
self:setNumberRounded(outnum, precision)
end,
setNumberRounded = function (self, number, precision)
--if precision is> a small integer >=2 0.then
precision = 2
end
self.number = math_mod._round(number, precision)
if precision < 0 then
local fmt = '%.' .. string.format('%d', precision) .. 'f'
self.string = string.formattostring(fmt, 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
Line 75 ⟶ 118:
end
else
return error( 'Flag type not valid' )
end
end
 
local function makeLine( label, first_values, second_values, color_values )
local result = {'|- style="text-align: center;"\n! scope="row" style="height: 16px;" | ', label, "\n"}
for i = 1, 13 do
table.insert(result,
local color_str = color_values[i]
'|style="' .. color_values[i] ..
 
(i == 13 and ' border-left-width:medium"' or '"') ..
if i == 13 then
' class="notheme"| '
table.insert( result, table.concat( {'|style="', color_str, ' border-left-width:medium" | '} ) )
else)
table.insert( result, table.concat( {'|style="', color_str, '" | '} ) )
end
 
local display = first_values[i]:getDisplay()
if display then
table.insert( result, display )
if second_values ~= nil then
display = second_values[i]:getDisplay(true)
if display then
table.insert( result, "<br />(" .. display .. ")" )
end
end
else
table.insert( result, '—' )
end
table.insert(result, "\n")
 
table.insert( result, "\n" )
end
return table.concat(result)
 
return table.concat( result )
end
 
local function getInputs( frameargs, group_name, suffix, include_space )
local month_names = { 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'year' }
Line 115 ⟶ 153:
local values = {}
if suffix == nil then
for i, mon in ipairs( month_names ) do
if include_space then
str = frame.args[ mon .. ' ' .. group_name ] or ''
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 ]
else
str = frame.args[ mon .. group_name .. ' ' .. suf ]
end
if str ~= nil and str ~= '' then
Line 149 ⟶ 187:
end
 
local function getAnnualValue( values, mode )
if mode == 'avg' or mode == 'sum' then
local total = 0
Line 164 ⟶ 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( total / 12, p1 )
end
value.variant = variant
Line 199 ⟶ 235:
return target or Value.new()
else
error( 'Unrecognized Annual Mode' )
end
end
 
local function reconcileTemperature( C_values, F_values )
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( (F_values[i].number - 32)*5/9, p )
end
elseif F_values[i].string == '' then
if C_values[i]:isValid() then
p = C_values[i]:getPrecision()
F_values[i]:setNumberRounded( C_values[i].number*9/5 + 32, p )
end
end
Line 220 ⟶ 256:
end
 
local function reconcilePrecipitation( M_values, I_values, prefer_cm )
local v_class = 0
for i = 1, 13 do
if M_values[i].variant == 1 then
v_class = 1
Line 236 ⟶ 272:
end
end
for i = 1, 13 do
local punits
if M_values[i].string == '' then
if I_values[i]:isValid() then
if v_class == 1 then
units = 'in2cm'
p = I_values[i]:getPrecision() - 1
M_values[i]:setNumberRounded( I_values[i].number*2.54, p )
else
units = 'in2mm'
p = I_values[i]:getPrecision() - 2
M_values[i]:setNumberRounded( I_values[i].number*25.4, p )
end
M_values[i]:setConvert(I_values[i], units)
M_values[i].variant = v_class
end
Line 252 ⟶ 287:
if M_values[i]:isValid() then
if M_values[i].variant == 1 then
punits = M_values[i]:getPrecision()'cm2in'
I_values[i].number = M_values[i].number/2.54
else
units = 'mm2in'
p = M_values[i]:getPrecision() + 1
I_values[i].number = M_values[i].number/25.4
end
I_values[i]:setNumberRoundedsetConvert( I_valuesM_values[i].number, p units)
end
end
Line 264 ⟶ 297:
end
 
local function w.buildRow_buildRow(definition, frameargs, options)
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 mode = (definition.mode or 'basic'):lower()
local group_name = definition.group_name
local first_values, second_values
local color_values
local color_scheme = frame.argsdefinition.color_scheme or 't'
local scale_factor = math_mod._cleanNumber( frame.argsdefinition.scale_factor) or 1
local date_mode = checkFlag( frame.argsdefinition.date_mode, false )
local label = frame.argsdefinition.label or ''
local annual_mode = (frame.argsdefinition.annual_mode or 'avg'):lower()
local include_space = checkFlag( frame.argsdefinition.include_space, true )
local second_line = checkFlag( frame.argsdefinition.second_line, false )
local prefer_cm = checkFlag( frame.argsdefinition.prefer_cm, false )
local imperial_first = checkFlag(args['imperial first'])
local pframe = frame:getParent()
local imperial_firstmetric_first = checkFlag( frame.args['imperialmetric first'] or pframe.args['imperial first'] )
local metric_firstwantSingleLine = checkFlag( frameoptions.args['metric first']wantSingleLine or pframe.checkFlag(args['metricsingle firstline'] )
local trace = args.trace
local single_line = checkFlag( frame.args['single line'] or pframe.args['single line'] )
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( pframeargs, group_name, nil, include_space )
second_values = nil
elseif mode == 'temperature' then
first_values = getInputs( pframeargs, group_name, {'C'}, include_space )
second_values = getInputs( pframeargs, group_name, {'F'}, include_space )
reconcileTemperature( first_values, second_values )
elseif mode == "precipitation" then
first_values = getInputs( pframeargs, group_name, {'cm', 'mm'}, include_space )
second_values = getInputs( pframeargs, group_name, {'inch'}, include_space )
reconcilePrecipitation( first_values, second_values, prefer_cm )
else
error( 'Requested mode not recognized' )
end
 
local good = false
for i = 1, 13 do
if first_values[i].string ~= '' then
good = true
Line 311 ⟶ 346:
return ''
end
 
if first_values[13].string == '' then
first_values[13] = getAnnualValue( first_values, annual_mode )
end
if second_values ~= nil then
if second_values[13].string == '' then
second_values[13] = getAnnualValue( second_values, annual_mode )
end
end
color_scheme = wbc.interpret_color_code(color_scheme)
 
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 337 ⟶ 369:
end
end
table.insert( color_values, color_scheme( first_values[i].number * adj ) )
else
table.insert( color_values, color_scheme( nil ) )
end
end
 
if imperial_first and second_values ~= nil then
first_values, second_values = second_values, first_values
end
if not wantSingleLine then
 
if not single_line then
if second_line and second_values ~= nil then
first_values = second_values
Line 353 ⟶ 383:
second_values = nil
end
return makeLine(label, first_values, second_values, color_values)
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 w{
buildRow = buildRow,
_buildRow = _buildRow,
}