Module:Weather box/row: Difference between revisions

Content deleted Content added
No edit summary
update from sandbox: tweak to reduce duplication
 
(23 intermediate revisions by 9 users not shown)
Line 1:
local math_mod = require('Module:Math')
w = {};
math_mod = require( "Module:Math" );
wbc = require( "Module:WeatherBoxColors" );
 
local traceText
function checkFlag( flag )
local Value
if flag == nil then
Value = {
return nil;
lang = mw.getContentLanguage(),
elseif type( flag ) == 'boolean' then
getDisplay = function (self, second)
return flag;
if not self:isValid() then
elseif type( flag ) == 'string' then
return nil
flag = flag:lower();
end
if flag == '0' or flag == 'false' or
local display = self.string
flag == '' or flag == 'no' or
if flagdisplay == 'ntrace' then
if second then
return false;
-- If a cell displays "cm (inch)", show "trace" not "trace (trace)".
else
return true;nil
end
return traceText or 'trace'
else
end
return error( 'Flag type not valid' );
if math.abs(self.number) >= 1000 then
end
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 w.buildRow( frame )
local result = {'|- style="text-align: center;"\n! scope="row" style="height: 16px;" | ', label, "\n"}
local mode = (frame.args.mode or 'basic'):lower();
for i = 1, 13 do
local group_name = frame.args.group_name;
table.insert(result,
local first_value_string, second_value_string;
'|style="' .. color_values[i] ..
local first_value_number, second_value_number, color_values;
(i == 13 and ' border-left-width:medium"' or '"') ..
local color_scheme = frame.args.color_scheme or 't';
' class="notheme"| '
local scale_factor = math_mod._cleanNumber( frame, frame.args.scale_factor) or 1;
)
local date_mode = checkFlag( frame.args.date_mode or false );
local display = first_values[i]:getDisplay()
local label = frame.args.label or '';
if display then
local annual_mode = (frame.args.annual_mode or 'avg'):lower();
table.insert(result, display)
local include_space = checkFlag( frame.args.include_space or true );
if second_values ~= nil then
local second_line = checkFlag( frame.args.second_line ) or false;
display = second_values[i]:getDisplay(true)
local result;
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 getInputs(args, group_name, suffix, include_space)
local pframe = frame:getParent();
local month_names = { 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
local imperial_first = checkFlag( frame.args['imperial first'] );
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'year' }
if imperial_first == nil then imperial_first = checkFlag( pframe.args['imperial first'] ); end
local str
local values = {}
local metric_first = checkFlag( frame.args['metric first'] );
if suffix == nil then
if metric_first == nil then metric_first = checkFlag( pframe.args['metric first'] ); end
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)
local single_line = checkFlag( frame.args['single line'] );
if mode == 'avg' or mode == 'sum' then
if single_line == nil then single_line = checkFlag( pframe.args['single line'] ); end
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 reconcileTemperature(C_values, F_values)
if imperial_first == nil and metric_first ~= nil then
for i = 1, 13 do
imperial_first = not metric_first;
local p
else
if C_values[i].string == '' then
imperial_first = true;
if F_values[i]:isValid() then
end
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
end
end
 
local function reconcilePrecipitation(M_values, I_values, prefer_cm)
if mode == 'basic' then
local v_class = 0
first_value_string, first_value_number = getInputs( pframe, group_name,
for i = 1, 13 do
nil, include_space );
if M_values[i].variant == 1 then
second_value_string = nil;
v_class = 1
second_value_number = nil;
elseif modeM_values[i].variant == 'temperature'2 then
v_class = 2
first_value_string, first_value_number = getInputs( pframe, group_name,
end
{'C'}, include_space );
end
second_value_string, second_value_number = getInputs( pframe, group_name,
if v_class == 0 then
{'F'}, include_space );
if prefer_cm then
first_value_string, first_value_number, second_value_string, second_value_number =
v_class = 1
reconcileTemperature( first_value_string, first_value_number,
else
second_value_string, second_value_number )
v_class = 2
elseif mode == "precipitation" then
end
first_value_string, first_value_number, variant = getInputs( pframe, group_name,
end
{'cm', 'mm'}, include_space );
for i = 1, 13 do
second_value_string, second_value_number = getInputs( pframe, group_name,
local units
{'inch'}, include_space );
if M_values[i].string == '' then
first_value_string, first_value_number, second_value_string, second_value_number =
if I_values[i]:isValid() then
reconcilePrecipitation( first_value_string, first_value_number,
if v_class == 1 then
second_value_string, second_value_number, variant )
units = 'in2cm'
else
else
error( 'Requested mode not recognized' );
units = 'in2mm'
end
end
M_values[i]:setConvert(I_values[i], units)
local good = false;
M_values[i].variant = v_class
for i = 1,13 do
end
if first_value_string[i] ~= nil and first_value_string[i] ~= '' then
elseif I_values[i].string == '' then
good = true;
if M_values[i]:isValid() then
break;
if M_values[i].variant == 1 then
end
units = 'cm2in'
end
else
if not good then
units = 'mm2in'
return '';
end
I_values[i]:setConvert(M_values[i], units)
end
end
end
end
 
local function _buildRow(definition, args, options)
if first_value_string[13] == nil or first_value_string[13] == '' then
options = options or {}
first_value_string[13], first_value_number[13] = getAnnualValue( first_value_number, annual_mode );
local wbc = require('Module:Weather box/colors' .. (options.sandbox or ''))
end
local mode = (definition.mode or 'basic'):lower()
if second_value_string ~= nil then
local group_name = definition.group_name
if second_value_string[13] == nil or second_value_string[13] == '' then
local first_values, second_values
second_value_string[13], second_value_number[13] = getAnnualValue( second_value_number, annual_mode );
local color_values
end
local color_scheme = definition.color_scheme or 't'
if mode == 'precipitation' then
local scale_factor = math_mod._cleanNumber(definition.scale_factor) or 1
for i = 1,12 do
local date_mode = checkFlag(definition.date_mode, false)
if variant[i] ~= 0 then
local label = definition.label or ''
variant[13] = variant[i];
local annual_mode = (definition.annual_mode or 'avg'):lower()
break;
local include_space = checkFlag(definition.include_space, true)
end
local second_line = checkFlag(definition.second_line, false)
end
local prefer_cm = checkFlag(definition.prefer_cm, false)
end
local imperial_first = checkFlag(args['imperial first'])
end
local metric_first = checkFlag(args['metric first'])
local wantSingleLine = options.wantSingleLine or checkFlag(args['single line'])
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(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)
color_scheme = wbc.interpret_color_code( color_scheme );
return _buildRow(frame.args, frame:getParent().args) -- row definition, template parameters
 
color_values = {};
month_adj = { 31/30, 28/30, 31/30, 1, 31/30, 1,
31/30, 31/30, 1, 31/30, 1, 31/30, 12.175 };
local adj;
for i = 1,13 do
if first_value_number[i] ~= nil and first_value_number[i] ~= -9999 then
adj = scale_factor;
if date_mode then
adj = adj / month_adj[i];
end
if mode == "precipitation" then
if variant[i] == 1 then
adj = adj * 10;
end
end
table.insert( color_values, color_scheme( first_value_number[i] * adj ) );
else
table.insert( color_values, color_scheme( nil ) );
end
end
local lang = mw.getContentLanguage();
for i = 1,13 do
if first_value_number[i] ~= nil and first_value_number[i] ~= -9999 then
if math.abs(first_value_number[i]) >= 1000 then
first_value_string[i] = lang:formatNum( math.abs(first_value_number[i]) );
if first_value_number[i] < 0 then
first_value_string[i] = '−' .. first_value_string[i];
end
elseif first_value_number[i] < 0 then
first_value_string[i] = '−' .. first_value_string[i]:sub(2);
end
end
if second_value_number ~= nil then
if second_value_number[i] ~= nil and second_value_number[i] ~= -9999 then
if math.abs(first_value_number[i]) >= 1000 then
second_value_string[i] = lang:formatNum( math.abs(second_value_number[i]) );
if second_value_number[i] < 0 then
second_value_string[i] = '−' .. second_value_string[i];
end
elseif second_value_number[i] < 0 then
second_value_string[i] = '−' .. second_value_string[i]:sub(2);
end
end
end
end
 
if imperial_first and second_value_string ~= nil then
local t = first_value_string;
first_value_string = second_value_string;
second_value_string = t;
end
 
if not single_line then
if second_line and second_value_string ~= nil then
first_value_string = second_value_string;
end
second_value_string = nil;
end
 
return makeLine( label, first_value_string, second_value_string, color_values, color_scheme );
end
 
function makeLine( label, first_value_string, second_value_string, color_values, color_scheme )
local result, color_str, value_str;
result = {'|- \n! height="16" | ', label, "\n"}
for i = 1,13 do
color_str = color_values[i];
 
if i == 13 then
table.insert( result, table.concat( {'|style="', color_str, ' border-left-width:medium" | '} ) );
else
table.insert( result, table.concat( {'|style="', color_str, '" | '} ) );
end
 
value_str = first_value_string[i];
if value_str ~= '' and value_str ~= nil then
table.insert( result, value_str );
if second_value_string ~= nil then
value_str = second_value_string[i];
if value_str ~= '' and value_str ~= nil then
table.insert( result, "<br /> (" .. value_str .. ")" );
end
end
else
table.insert( result, '—' );
end
table.insert( result, "\n" );
end
return table.concat( result );
end
 
function getInputs( frame, group_name, suffix, include_space )
local month_names = { 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'year' };
local str, str2, val;
local output_string = {};
local output_value = {};
local variant = {};
if suffix == nil then
for _, 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
val, str2 = math_mod._cleanNumber( frame, str );
if val ~= nil then
table.insert( output_string, str2 );
table.insert( output_value, val );
else
table.insert( output_string, str );
table.insert( output_value, -9999 );
end
end
else
local updated = false;
for _, mon in ipairs( month_names ) do
updated = false;
for i, 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
val, str2 = math_mod._cleanNumber( frame, str );
if val ~= nil then
table.insert( output_string, str2 );
table.insert( output_value, val );
else
table.insert( output_string, str );
table.insert( output_value, -9999 );
end
table.insert( variant, i );
updated = true;
break;
end
end
if not updated then
table.insert( output_string, '' );
table.insert( output_value, -9999 );
table.insert( variant, 0 );
end
end
end
return output_string, output_value, variant;
end
 
return {
function getAnnualValue( values, mode )
buildRow = buildRow,
local total = 0;
_buildRow = _buildRow,
local val = 0;
}
if mode == 'avg' or mode == 'sum' then
local p1, p2;
p1 = 0;
for i = 1, 12 do
val = values[i];
if val == -9999 then
return '', -9999;
end
p2 = math_mod._precision( val );
if p2 > p1 then
p1 = p2;
end
total = total + val;
end
if mode == 'avg' then
total = math_mod._round( total / 12, p1 + 1 );
end
return tostring( total ), total;
elseif mode == 'min' then
local min_val = nil;
for i = 1, 12 do
val = values[i];
if val ~= -9999 then
if min_val == nil or val < min_val then
min_val = val;
end
end
end
return tostring( min_val ), min_val;
elseif mode == 'max' then
local max_val = nil;
for i = 1, 12 do
val = values[i];
if val ~= -9999 then
if max_val == nil or val > max_val then
max_val = val;
end
end
end
return tostring(max_val), max_val;
else
error( 'Unrecognized Annual Mode' );
end
end
 
function reconcileTemperature( C_degree_strings, C_degree_values, F_degree_strings, F_degree_values )
local p;
for i = 1,13 do
if C_degree_strings[i] == '' then
if F_degree_values[i] ~= -9999 then
p = math.max( 0, math_mod._precision( F_degree_strings[i] ) );
C_degree_values[i] = math_mod._round( (F_degree_values[i] - 32)*5/9, p );
C_degree_strings[i] = tostring( C_degree_values[i] );
end
elseif F_degree_strings[i] == '' then
if C_degree_values[i] ~= -9999 then
p = math.max( 0, math_mod._precision( C_degree_strings[i] ) );
F_degree_values[i] = math_mod._round( C_degree_values[i]*9/5 + 32, p );
F_degree_strings[i] = tostring( F_degree_values[i] );
end
end
end
return C_degree_strings, C_degree_values, F_degree_strings, F_degree_values;
end
 
function reconcilePrecipitation( M_degree_strings, M_degree_values,
I_degree_strings, I_degree_values, variant )
local p;
for i = 1,13 do
if M_degree_strings[i] == '' then
if I_degree_values[i] ~= -9999 then
p = math.max( 0, math_mod._precision( I_degree_strings[i] ) ) - 1;
M_degree_values[i] = math_mod._round( I_degree_values[i]*25.4, p );
M_degree_strings[i] = tostring( M_degree_values[i] );
end
elseif I_degree_strings[i] == '' then
if M_degree_values[i] ~= -9999 then
if variant[i] == 1 then
p = math.max( 0, math_mod._precision( M_degree_strings[i] ) ) + 1;
I_degree_values[i] = M_degree_values[i]/2.54;
else
p = math.max( 0, math_mod._precision( M_degree_strings[i] ) ) + 2;
I_degree_values[i] = M_degree_values[i]/25.4;
end
I_degree_values[i] = math_mod._round( I_degree_values[i], p );
I_degree_strings[i] = tostring( I_degree_values[i] );
end
end
end
return M_degree_strings, M_degree_values, I_degree_strings, I_degree_values;
end
 
return w;