Module:Weather box/row: Difference between revisions

Content deleted Content added
No edit summary
update from sandbox: tweak to reduce duplication
 
(121 intermediate revisions by 9 users not shown)
Line 1:
local math_mod = require('Module:Math')
w = {};
convert = require( "Module:BaseConvert" );
math_mod = require( "Module:Math" );
 
local traceText
function hex( value )
local Value
return convert._convert( value, 16, 10, 0, 2, nil, nil, nil );
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))
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,
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 > 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, second_values, color_values)
function format_line( background, text_color )
local result = {'|- style="text-align: center;"\n! scope="row" style="height: 16px;" | ', label, "\n"}
return "background:#" .. background .. ";color:#" .. text_color .. ";font-size:85%;text-align:center;";
for i = 1, 13 do
table.insert(result,
'|style="' .. color_values[i] ..
(i == 13 and ' border-left-width:medium"' or '"') ..
' class="notheme"| '
)
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")
end
return table.concat(result)
end
 
local function range_posgetInputs( valueargs, startgroup_name, stopsuffix, include_space)
local month_names = { 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
if start < stop then
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'year' }
if value < start then
local str
return 0;
local values = {}
elseif value > stop then
if suffix == nil then
return 1;
for i, mon in ipairs(month_names) do
else
if include_space then
return (value - start) / (stop - start);
str = args[ mon .. ' ' .. group_name ] or ''
end
else
str = args[ mon .. group_name ] or ''
if value < stop then
end
return 1;
values[i] = Value.new(str)
elseif value > start then
end
return 0;
else
for i, mon in ipairs(month_names) do
return (start - value) / (start - stop);
local value, updated
end
for var, suf in ipairs(suffix) do
end
if include_space then
end
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 w.color_dgetAnnualValue(values, frame mode)
if mode == 'avg' or mode == 'sum' then
local val, item, background, text_color;
local total = 0
local p1, p2, variant
val = math_mod._cleanNumber( frame, frame.args[1] );
p1 = 0
if val == nil then
for i = 1, 12 do
return format_line( "FFFFFF", "000000" );
if not values[i]:isValid() then
end
return Value.new()
end
background = '';
if not variant then
item = hex( range_pos( val, 20, 0 )*255 );
local var = values[i].variant
background = background .. item .. item;
if var and var ~= 0 then
variant = var
item = hex( range_pos( val, 40, 20 )*255 );
end
background = background .. item;
end
p2 = values[i]:getPrecision()
if val >= 12 then
if p2 > p1 then
text_color = "FFFFFF";
p1 = p2
else
end
text_color = "000000";
total = total + values[i].number
end
end
local value = Value.new(total)
return format_line( background, text_color );
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 w.color_treconcileTemperature(C_values, frame F_values)
for i = 1, 13 do
local val, item, background, text_color;
local p
if C_values[i].string == '' then
val = math_mod._cleanNumber( frame, frame.args[1] );
if val == nilF_values[i]:isValid() then
p = F_values[i]:getPrecision()
return format_line( "FFFFFF", "000000" );
C_values[i]:setNumberRounded((F_values[i].number - 32)*5/9, p)
end
end
elseif F_values[i].string == '' then
background = '';
if val < -42.75 or val > 60C_values[i]:isValid() then
p = C_values[i]:getPrecision()
background = background .. "00";
F_values[i]:setNumberRounded(C_values[i].number*9/5 + 32, p)
elseif val < 4.47 then
end
item = 5.4*(42.75 + val)
end
background = background .. hex( item );
end
elseif val > 41.5 then
item = 13.78*(60-val);
background = background .. hex( item );
else
background = background .. "FF"
end
if val < -42.75 or val > 41.5 then
background = background .. "00";
elseif val < 4.47 then
item = 5.4*(42.75 + val)
background = background .. hex( item );
elseif val > 4.5 then
item = 6.89*(41.5 - val);
background = background .. hex( item );
else
background = background .. "FF"
end
if val < -90 or val > 23 then
background = background .. "00";
elseif val < -42.78 then
item = 5.4*(90 + val)
background = background .. hex( item );
elseif val > 4.5 then
item = 13.78*(23 - val);
background = background .. hex( item );
else
background = background .. "FF"
end
if val < -23.3 or val >= 37.8 then
text_color = "FFFFFF";
else
text_color = "000000";
end
return format_line( background, text_color );
end
 
local function reconcilePrecipitation(M_values, I_values, prefer_cm)
function w.color_t2( frame )
local v_class = 0
local val, item, background, text_color;
for i = 1, 13 do
if M_values[i].variant == 1 then
val = math_mod._cleanNumber( frame, frame.args[1] );
v_class = 1
if val == nil then
elseif M_values[i].variant == 2 then
return format_line( "FFFFFF", "000000" );
v_class = 2
end
end
end
background = '';
if valv_class <== 4.50 then
if prefer_cm then
item = range_pos( val, -42.75, 4.5 )*255;
v_class = 1
background = background .. hex( item );
else
v_class = 2
item = range_pos( val, 60, 41.5 )*255;
end
background = background .. hex( item );
end
for i = 1, 13 do
local units
if val <= 4.5 then
if M_values[i].string == '' then
item = range_pos( val, -42.75, 4.5 )*255;
if I_values[i]:isValid() then
background = background .. hex( item );
if v_class == 1 then
else
units = 'in2cm'
item = range_pos( val, 41.5, 4.5 )*255;
else
background = background .. hex( item );
units = 'in2mm'
end
end
M_values[i]:setConvert(I_values[i], units)
if val < -42.78 then
M_values[i].variant = v_class
item = range_pos( val, -90, -42.78 )*255;
end
background = background .. hex( item );
elseif I_values[i].string == '' then
else
if M_values[i]:isValid() then
item = range_pos( val, 23, 4.5 )*255;
if M_values[i].variant == 1 then
background = background .. hex( item );
units = 'cm2in'
end
else
units = 'mm2in'
if val < -23.3 or val >= 37.8 then
end
text_color = "FFFFFF";
I_values[i]:setConvert(M_values[i], units)
else
end
text_color = "000000";
end
end
return format_line( background, text_color );
end
 
local function w.color_p_buildRow(definition, frameargs, options)
options = options or {}
local val, item, background, text_color;
local wbc = require('Module:Weather box/colors' .. (options.sandbox or ''))
local mode = (definition.mode or 'basic'):lower()
val = math_mod._cleanNumber( frame, frame.args[1] );
local group_name = definition.group_name
if val == nil then
local first_values, second_values
return format_line( "FFFFFF", "000000" );
local color_values
end
local color_scheme = definition.color_scheme or 't'
local scale_factor = math_mod._cleanNumber(definition.scale_factor) or 1
background = '';
local date_mode = checkFlag(definition.date_mode, false)
item = hex( range_pos( val, 165.6, 0 )*255 );
local label = definition.label or ''
background = background .. item .. item;
local annual_mode = (definition.annual_mode or 'avg'):lower()
local include_space = checkFlag(definition.include_space, true)
item = hex( range_pos( val, 300, 165.61 )*207 + 48 );
local second_line = checkFlag(definition.second_line, false)
background = background .. item;
local prefer_cm = checkFlag(definition.prefer_cm, false)
local imperial_first = checkFlag(args['imperial first'])
if val > 90 then
local metric_first = checkFlag(args['metric first'])
text_color = "FFFFFF";
local wantSingleLine = options.wantSingleLine or checkFlag(args['single line'])
else
local trace = args.trace
text_color = "000000";
if trace and trace ~= '' then
end
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(args, group_name, nil, include_space)
second_values = nil
elseif mode == 'temperature' then
first_values = getInputs(args, group_name, {'C'}, include_space)
second_values = getInputs(args, group_name, {'F'}, include_space)
reconcileTemperature(first_values, second_values)
elseif mode == "precipitation" then
first_values = getInputs(args, group_name, {'cm', 'mm'}, include_space)
second_values = getInputs(args, 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
break
end
end
if not good then
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_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
if imperial_first and second_values ~= nil then
first_values, second_values = second_values, first_values
end
if not wantSingleLine then
if second_line and second_values ~= nil then
first_values = second_values
end
second_values = nil
end
return makeLine(label, first_values, second_values, color_values)
end
 
local function buildRow(frame)
return format_line( background, text_color );
return _buildRow(frame.args, frame:getParent().args) -- row definition, template parameters
end
 
return w;{
buildRow = buildRow,
_buildRow = _buildRow,
}