Content deleted Content added
update from Module:WeatherBox/sandbox; this moves many calculations into a Value object and displays significant trailing zeroes |
|||
Line 3:
local wbc = require( "Module:WeatherBoxColors" )
local function checkFlag( flag )
if flag == nil then
return
elseif type( flag ) == 'boolean' then
return flag
Line 79 ⟶ 22:
end
local function makeLine( label,
local result, color_str, value_str
result = {'|- style="text-align: center;"\n! scope="row" style="height: 16px;" | ', label, "\n"} for i = 1,13 do
if i == 13 then
Line 90 ⟶ 35:
end
value_str = first_value_string[i]
if
table.insert( result,
if
value_str = second_value_string[i]
if
table.insert( result, "<br /> (" ..
end
end
Line 112 ⟶ 57:
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
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( 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 updated = false
for
if include_space then
str = frame.args[ mon .. ' ' .. group_name .. ' ' .. suf ]
Line 133 ⟶ 89:
end
if str ~= nil and str ~= '' 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
Line 140 ⟶ 103:
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
local function getAnnualValue( values, mode )
local total = 0
local val = 0
if mode == 'avg' or mode == 'sum' then
local
p1 = 0
for i = 1, 12 do
if val == -9999 then
return '', -9999
end
p2 =
if p2 > p1 then
p1 = p2
end
total = total +
end
if mode == 'avg' then
end
return tostring( total ), total
elseif mode == 'min' then
local
for i = 1, 12 do
if min_val == nil or val < min_val then
min_val = val
end
end
end
return
elseif mode == 'max' then
local
for i = 1, 12 do
if max_val == nil or val > max_val then
max_val = val
end
end
end
return
else
error( 'Unrecognized Annual Mode' )
Line 203 ⟶ 164:
end
local function reconcileTemperature(
local p
for i = 1,13 do
if C_degree_strings[i] == '' then
if
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
if
p = math.max( 0, math_mod._precision( C_degree_strings[i] ) )
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 function reconcilePrecipitation(
I_degree_strings, I_degree_values, variant, prefer_cm )
local p
local v_class = 0
for i = 1,13 do
if
v_class = 1
elseif
v_class = 2
end
Line 237 ⟶ 204:
end
for i = 1,13 do
if M_degree_strings[i] == '' then
if
if v_class == 1 then
p =
variant[i] = v_class
else
p =
variant[i] = v_class
end
M_degree_strings[i] = tostring( M_degree_values[i] )
end
elseif
if
if
p = math.max( 0, math_mod._precision( M_degree_strings[i] ) )
else
p =
end
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, variant
end
Line 267 ⟶ 237:
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
local label = frame.args.label or ''
local annual_mode = (frame.args.annual_mode or 'avg'):lower()
local include_space = checkFlag( frame.args.include_space
local second_line = checkFlag( frame.args.second_line
local prefer_cm = checkFlag( frame.args.prefer_cm
local result
local variant
local pframe = frame:getParent()
local imperial_first = checkFlag( frame
local metric_first = checkFlag( frame.args['metric first'] )
local single_line = checkFlag( frame.args['single line'] )
if single_line == nil then single_line = checkFlag( pframe.args['single line'] ) end
if imperial_first == nil and metric_first ~= nil then
imperial_first = not metric_first
else
imperial_first = true
end
if mode == 'basic' then
nil, include_space )
second_value_string = nil
second_value_number = nil
elseif mode == 'temperature' then
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
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' )
Line 303 ⟶ 294:
local good = false
for i = 1,13 do
if
good = true
break
Line 312 ⟶ 303:
end
if
end
if
if
end
if mode == 'precipitation' then
for i = 1,12 do
if variant[i] ~= 0 then
variant[13] = variant[i]
break
end
end
end
end
Line 325 ⟶ 324:
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,
local adj
for i = 1,13 do
if first_value_number[i] ~= nil and first_value_number[i] ~= -9999 then
if date_mode then
adj = adj / month_adj[i]
end
if mode == "precipitation" then
if
adj = adj * 10
end
end
table.insert( color_values, color_scheme(
else
table.insert( color_values, color_scheme( nil ) )
Line 343:
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
first_value_string = second_value_string
end
end
return makeLine( label,
end
|