Module:Weather box/row: Difference between revisions

Content deleted Content added
Attempt to fix precision, per talk page. I tested this in the sandbox with the testcases page, so I think it doesn't break anything, but revert this edit if it does.
update from Module:WeatherBox/sandbox (see its history for details) per template talk (no substantive change): style fixes (tab indents, trailing whitespace, redundant semicolons); fix unintended globals; Erutuon's move of repeated code to a function
Line 1:
local w = {};
local math_mod = require( "Module:Math" );
local wbc = require( "Module:WeatherBoxColors" );
 
local function checkFlag( flag )
if flag == nil then
return nil;
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_value_string, second_value_string, color_values, color_scheme )
function w.buildRow( frame )
local result, color_str, value_str
local mode = (frame.args.mode or 'basic'):lower();
local group_name = frame.args.group_name;
local first_value_string, second_value_string;
local first_value_number, second_value_number, color_values;
local color_scheme = frame.args.color_scheme or 't';
local scale_factor = math_mod._cleanNumber( frame.args.scale_factor) or 1;
local date_mode = checkFlag( frame.args.date_mode or false );
local label = frame.args.label or '';
local annual_mode = (frame.args.annual_mode or 'avg'):lower();
local include_space = checkFlag( frame.args.include_space or true );
local second_line = checkFlag( frame.args.second_line ) or false;
local prefer_cm = checkFlag( frame.args.prefer_cm ) or false;
local result;
 
result = {'|- style="text-align: center;"\n! scope="row" style="height: 16px;" | ', label, "\n"}
local pframe = frame:getParent();
for i = 1,13 do
local imperial_first = checkFlag( frame.args['imperial first'] );
color_str = color_values[i]
if imperial_first == nil then imperial_first = checkFlag( pframe.args['imperial first'] ); end
local metric_first = checkFlag( frame.args['metric first'] );
if metric_first == nil then metric_first = checkFlag( pframe.args['metric first'] ); end
 
if i == 13 then
local single_line = checkFlag( frame.args['single line'] );
table.insert( result, table.concat( {'|style="', color_str, ' border-left-width:medium" | '} ) )
if single_line == nil then single_line = checkFlag( pframe.args['single line'] ); end
else
table.insert( result, table.concat( {'|style="', color_str, '" | '} ) )
end
 
value_str = first_value_string[i]
if imperial_first == nil and metric_first ~= nil then
if value_str ~= '' and value_str ~= nil then
imperial_first = not metric_first;
table.insert( result, value_str )
else
if second_value_string ~= nil then
imperial_first = true;
value_str = second_value_string[i]
end
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" )
if mode == 'basic' then
end
first_value_string, first_value_number = getInputs( pframe, group_name,
nil, include_space );
second_value_string = nil;
second_value_number = nil;
elseif mode == 'temperature' then
first_value_string, first_value_number = getInputs( pframe, group_name,
{'C'}, include_space );
second_value_string, second_value_number = getInputs( pframe, group_name,
{'F'}, include_space );
first_value_string, first_value_number, second_value_string, second_value_number =
reconcileTemperature( first_value_string, first_value_number,
second_value_string, second_value_number )
elseif mode == "precipitation" then
first_value_string, first_value_number, variant = getInputs( pframe, group_name,
{'cm', 'mm'}, include_space );
second_value_string, second_value_number = getInputs( pframe, group_name,
{'inch'}, include_space );
first_value_string, first_value_number, second_value_string, second_value_number,
variant =
reconcilePrecipitation( first_value_string, first_value_number,
second_value_string, second_value_number, variant, prefer_cm )
else
error( 'Requested mode not recognized' );
end
local good = false;
for i = 1,13 do
if first_value_string[i] ~= nil and first_value_string[i] ~= '' then
good = true;
break;
end
end
if not good then
return '';
end
 
return table.concat( result )
if first_value_string[13] == nil or first_value_string[13] == '' then
end
first_value_string[13], first_value_number[13] = getAnnualValue( first_value_number, annual_mode );
end
if second_value_string ~= nil then
if second_value_string[13] == nil or second_value_string[13] == '' then
second_value_string[13], second_value_number[13] = getAnnualValue( second_value_number, annual_mode );
end
if mode == 'precipitation' then
for i = 1,12 do
if variant[i] ~= 0 then
variant[13] = variant[i];
break;
end
end
end
end
 
local function getInputs( frame, group_name, suffix, include_space )
color_scheme = wbc.interpret_color_code( color_scheme );
local month_names = { 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'year' }
local str, str2, val
 
local color_valuesoutput_string = {};
local output_value = {}
month_adj = { 31/30, 28.25/30, 31/30, 1, 31/30, 1,
local variant = {}
31/30, 31/30, 1, 31/30, 1, 31/30, 12.175 };
if suffix == nil then
local adj;
for i = 1_,13 do mon in ipairs( month_names ) do
if include_space then
if first_value_number[i] ~= nil and first_value_number[i] ~= -9999 then
str = ( frame.args[ mon .. ' ' .. group_name ] or '' )
adj = scale_factor;
else
if date_mode then
str = ( frame.args[ mon .. group_name ] or '' )
adj = adj / month_adj[i];
end
end
val, str2 = math_mod._cleanNumber( str )
if mode == "precipitation" then
if variant[i]val =~= 1nil then
table.insert( output_string, str2 )
adj = adj * 10;
table.insert( output_value, val )
end
else
end
table.insert( output_string, str )
table.insert( color_values, color_scheme( first_value_number[i] * adj ) );
table.insert( output_value, -9999 )
else
end
table.insert( color_values, color_scheme( nil ) );
end
else
end
local updated = false
for _, mon in ipairs( month_names ) do
local lang = mw.getContentLanguage();
updated = false
for i = 1,13 do
for i, suf in ipairs( suffix ) do
if first_value_number[i] ~= nil and first_value_number[i] ~= -9999 then
if include_space then
if math.abs(first_value_number[i]) >= 1000 then
str = frame.args[ mon .. ' ' .. group_name .. ' ' .. suf ]
first_value_string[i] = lang:formatNum( math.abs(first_value_number[i]) );
else
if first_value_number[i] < 0 then
str = frame.args[ mon .. group_name .. ' ' .. suf ]
first_value_string[i] = '−' .. first_value_string[i];
end
end
if str ~= nil and str ~= '' then
elseif first_value_number[i] < 0 then
val, str2 = math_mod._cleanNumber( str )
first_value_string[i] = '−' .. first_value_string[i]:sub(2);
if val ~= nil then
end
table.insert( output_string, str2 )
end
table.insert( output_value, val )
if second_value_number ~= nil then
else
if second_value_number[i] ~= nil and second_value_number[i] ~= -9999 then
table.insert( output_string, str )
if math.abs(first_value_number[i]) >= 1000 then
table.insert( output_value, -9999 )
second_value_string[i] = lang:formatNum( math.abs(second_value_number[i]) );
end
if second_value_number[i] < 0 then
table.insert( variant, i )
second_value_string[i] = '−' .. second_value_string[i];
updated = true
end
break
elseif second_value_number[i] < 0 then
end
second_value_string[i] = '−' .. second_value_string[i]:sub(2);
end
end
if not updated then
end
table.insert( output_string, '' )
end
table.insert( output_value, -9999 )
end
table.insert( variant, 0 )
end
end
end
 
return output_string, output_value, variant
if imperial_first and second_value_string ~= nil then
end
local t = first_value_string;
first_value_string = second_value_string;
second_value_string = t;
end
 
local function getAnnualValue( values, mode )
if not single_line then
local total = 0
if second_line and second_value_string ~= nil then
local val = 0
first_value_string = second_value_string;
end
second_value_string = nil;
end
 
if mode == 'avg' or mode == 'sum' then
return makeLine( label, first_value_string, second_value_string, color_values, color_scheme );
local p1, p2
end
p1 = 0
for i = 1, 12 do
val = values[i]
if val == -9999 then
return '', -9999
end
 
p2 = math_mod._precision( val )
function makeLine( label, first_value_string, second_value_string, color_values, color_scheme )
if p2 > p1 then
local result, color_str, value_str;
p1 = p2
end
result = {'|- style="text-align: center;"\n! scope="row" style="height: 16px;" | ', label, "\n"}
for i = 1,13 do
color_str = color_values[i];
 
total = total + val
if i == 13 then
end
table.insert( result, table.concat( {'|style="', color_str, ' border-left-width:medium" | '} ) );
if mode == 'avg' then
else
total = math_mod._round( total / 12, p1 )
table.insert( result, table.concat( {'|style="', color_str, '" | '} ) );
end
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
 
local function reconcileTemperature( C_degree_strings, C_degree_values, F_degree_strings, F_degree_values )
value_str = first_value_string[i];
local p
if value_str ~= '' and value_str ~= nil then
for i = 1,13 do
table.insert( result, value_str );
if second_value_stringC_degree_strings[i] ~== nil'' then
if F_degree_values[i] ~= -9999 then
value_str = second_value_string[i];
p = math.max( 0, math_mod._precision( F_degree_strings[i] ) )
if value_str ~= '' and value_str ~= nil then
C_degree_values[i] = math_mod._round( (F_degree_values[i] - 32)*5/9, p )
table.insert( result, "<br /> (" .. value_str .. ")" );
C_degree_strings[i] = tostring( C_degree_values[i] )
end
end
end
elseif F_degree_strings[i] == '' then
else
if C_degree_values[i] ~= -9999 then
table.insert( result, '—' );
p = math.max( 0, math_mod._precision( C_degree_strings[i] ) )
end
F_degree_values[i] = math_mod._round( C_degree_values[i]*9/5 + 32, p )
F_degree_strings[i] = tostring( F_degree_values[i] )
table.insert( result, "\n" );
end
end
end
return table.concat( result );
return C_degree_strings, C_degree_values, F_degree_strings, F_degree_values
end
end
 
local function reconcilePrecipitation( M_degree_strings, M_degree_values,
function getInputs( frame, group_name, suffix, include_space )
I_degree_strings, I_degree_values, variant, prefer_cm )
local month_names = { 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
local p
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'year' };
 
local str, str2, val;
local v_class = 0
for i = 1,13 do
local output_string = {};
if variant[i] == 1 then
local output_value = {};
v_class = 1
local variant = {};
elseif if suffixvariant[i] == nil2 then
v_class = 2
for _, mon in ipairs( month_names ) do
end
if include_space then
end
str = ( frame.args[ mon .. ' ' .. group_name ] or '' );
if v_class == 0 then
else
if prefer_cm then
str = ( frame.args[ mon .. group_name ] or '' );
v_class = 1
end
else
val, str2 = math_mod._cleanNumber( str );
v_class = 2
if val ~= nil then
end
table.insert( output_string, str2 );
end
table.insert( output_value, val );
for i = 1,13 do
else
if M_degree_strings[i] == '' then
table.insert( output_string, str );
if I_degree_values[i] ~= -9999 then
table.insert( output_value, -9999 );
if v_class == 1 then
end
p = math.max( 0, math_mod._precision( I_degree_strings[i] ) ) - 1
end
M_degree_values[i] = math_mod._round( I_degree_values[i]*2.54, p )
else
variant[i] = v_class
local updated = false;
else
for _, mon in ipairs( month_names ) do
p = math.max( 0, math_mod._precision( I_degree_strings[i] ) ) - 2
updated = false;
M_degree_values[i] = math_mod._round( I_degree_values[i]*25.4, p )
for i, suf in ipairs( suffix ) do
variant[i] = v_class
if include_space then
end
str = frame.args[ mon .. ' ' .. group_name .. ' ' .. suf ];
M_degree_strings[i] = tostring( M_degree_values[i] )
else
end
str = frame.args[ mon .. group_name .. ' ' .. suf ];
elseif I_degree_strings[i] == '' then
end
if M_degree_values[i] ~= -9999 then
if str ~= nil and str ~= '' then
if variant[i] == 1 then
val, str2 = math_mod._cleanNumber( str );
p = math.max( 0, math_mod._precision( M_degree_strings[i] ) )
if val ~= nil then
I_degree_values[i] = M_degree_values[i]/2.54
table.insert( output_string, str2 );
else
table.insert( output_value, val );
p = math.max( 0, math_mod._precision( M_degree_strings[i] ) ) + 1
else
I_degree_values[i] = M_degree_values[i]/25.4
table.insert( output_string, str );
end
table.insert( output_value, -9999 );
I_degree_values[i] = math_mod._round( I_degree_values[i], p )
end
I_degree_strings[i] = tostring( I_degree_values[i] )
table.insert( variant, i );
end
updated = true;
end
break;
end
end
return M_degree_strings, M_degree_values, I_degree_strings, I_degree_values, variant
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
 
function getAnnualValuew.buildRow( values, modeframe )
local mode = (frame.args.mode or 'basic'):lower()
local total = 0;
local valgroup_name = 0;frame.args.group_name
local first_value_string, second_value_string
local first_value_number, second_value_number, color_values
if mode == 'avg' or mode == 'sum' then
local color_scheme = frame.args.color_scheme or 't'
local p1, p2;
local scale_factor = math_mod._cleanNumber( frame.args.scale_factor) or 1
p1 = 0;
local date_mode = checkFlag( frame.args.date_mode or false )
for i = 1, 12 do
local label = frame.args.label or ''
val = values[i];
local annual_mode = (frame.args.annual_mode or 'avg'):lower()
if val == -9999 then
local include_space = checkFlag( frame.args.include_space or true )
return '', -9999;
local second_line = checkFlag( frame.args.second_line ) or false
end
local prefer_cm = checkFlag( frame.args.prefer_cm ) or false
local result
p2 = math_mod._precision( val );
local variant
if p2 > p1 then
p1 = p2;
end
total = total + val;
end
if mode == 'avg' then
total = math_mod._round( total / 12, p1 );
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
 
local pframe = frame:getParent()
function reconcileTemperature( C_degree_strings, C_degree_values, F_degree_strings, F_degree_values )
local imperial_first = checkFlag( frame.args['imperial first'] )
local p;
if imperial_first == nil then imperial_first = checkFlag( pframe.args['imperial first'] ) end
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
 
local metric_first = checkFlag( frame.args['metric first'] )
function reconcilePrecipitation( M_degree_strings, M_degree_values,
if metric_first == nil then metric_first = checkFlag( pframe.args['metric first'] ) end
I_degree_strings, I_degree_values, variant, prefer_cm )
 
local p;
local single_line = checkFlag( frame.args['single line'] )
if single_line == nil then single_line = checkFlag( pframe.args['single line'] ) end
local v_class = 0;
 
for i = 1,13 do
if imperial_first == nil and metric_first ~= nil then
if variant[i] == 1 then
imperial_first = not metric_first
v_class = 1;
else
elseif variant[i] == 2 then
imperial_first = true
v_class = 2;
end
 
end
if v_classmode == 0'basic' then
first_value_string, first_value_number = getInputs( pframe, group_name,
if prefer_cm then
nil, include_space )
v_class = 1;
second_value_string = nil
else
second_value_number = nil
v_class = 2;
elseif mode == 'temperature' then
end
first_value_string, first_value_number = getInputs( pframe, group_name,
end
{'C'}, include_space )
for i = 1,13 do
second_value_string, second_value_number = getInputs( pframe, group_name,
if M_degree_strings[i] == '' then
{'F'}, include_space )
if I_degree_values[i] ~= -9999 then
first_value_string, first_value_number, second_value_string, second_value_number =
if v_class == 1 then
reconcileTemperature( first_value_string, first_value_number,
p = math.max( 0, math_mod._precision( I_degree_strings[i] ) ) - 1;
second_value_string, second_value_number )
M_degree_values[i] = math_mod._round( I_degree_values[i]*2.54, p );
elseif mode == "precipitation" then
variant[i] = v_class;
first_value_string, first_value_number, variant = getInputs( pframe, group_name,
else
{'cm', 'mm'}, include_space )
p = math.max( 0, math_mod._precision( I_degree_strings[i] ) ) - 2;
second_value_string, second_value_number = getInputs( pframe, group_name,
M_degree_values[i] = math_mod._round( I_degree_values[i]*25.4, p );
{'inch'}, include_space )
variant[i] = v_class;
first_value_string, first_value_number, second_value_string, second_value_number,
end
variant =
M_degree_strings[i] = tostring( M_degree_values[i] );
reconcilePrecipitation( first_value_string, first_value_number,
end
second_value_string, second_value_number, variant, prefer_cm )
elseif I_degree_strings[i] == '' then
else
if M_degree_values[i] ~= -9999 then
error( 'Requested mode not recognized' )
if variant[i] == 1 then
end
p = math.max( 0, math_mod._precision( M_degree_strings[i] ) ) ;
 
I_degree_values[i] = M_degree_values[i]/2.54;
local good = false
else
for i = 1,13 do
p = math.max( 0, math_mod._precision( M_degree_strings[i] ) ) + 1;
if first_value_string[i] ~= nil and first_value_string[i] ~= '' then
I_degree_values[i] = M_degree_values[i]/25.4;
good = true
end
break
I_degree_values[i] = math_mod._round( I_degree_values[i], p );
end
I_degree_strings[i] = tostring( I_degree_values[i] );
end
end
if not good then
end
return ''
end
end
return M_degree_strings, M_degree_values, I_degree_strings, I_degree_values, variant;
 
end
if first_value_string[13] == nil or first_value_string[13] == '' then
first_value_string[13], first_value_number[13] = getAnnualValue( first_value_number, annual_mode )
end
if second_value_string ~= nil then
if second_value_string[13] == nil or second_value_string[13] == '' then
second_value_string[13], second_value_number[13] = getAnnualValue( second_value_number, annual_mode )
end
if mode == 'precipitation' then
for i = 1,12 do
if variant[i] ~= 0 then
variant[13] = variant[i]
break
end
end
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, 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()
local function formatNumber(number, number_string)
if number ~= nil and number ~= -9999 then
if math.abs(number) >= 1000 then
number_string = lang:formatNum( math.abs(number) )
if number < 0 then
number_string = '−' .. number_string
end
elseif number < 0 then
number_string = '−' .. number_string:sub(2)
end
end
return number_string
end
for i = 1,13 do
first_value_string[i] = formatNumber(first_value_number[i], first_value_string[i])
if second_value_number ~= nil then
second_value_string[i] = formatNumber(second_value_number[i], second_value_string[i])
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
 
return w;