Module:Weather box/row: Difference between revisions
Appearance
Content deleted Content added
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 |
update from Module:WeatherBox/sandbox; this moves many calculations into a Value object and displays significant trailing zeroes |
||
Line 3: | Line 3: | ||
local wbc = require( "Module:WeatherBoxColors" ) |
local wbc = require( "Module:WeatherBoxColors" ) |
||
local Value |
|||
local function checkFlag( flag ) |
|||
Value = { |
|||
lang = mw.getContentLanguage(), |
|||
getDisplay = function (self) |
|||
if not self:isValid() then |
|||
return nil |
|||
end |
|||
local display = self.string |
|||
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 |
|||
if type(v) == 'string' then |
|||
val, str = math_mod._cleanNumber(v) |
|||
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, |
|||
}, Value) |
|||
end, |
|||
setNumberRounded = function (self, number, precision) |
|||
-- precision is a small integer >= 0. |
|||
self.number = math_mod._round(number, precision) |
|||
local fmt = '%.' .. string.format('%d', precision) .. 'f' |
|||
self.string = string.format(fmt, self.number) |
|||
end, |
|||
} |
|||
Value.__index = Value |
|||
local function checkFlag( flag, default ) |
|||
if flag == nil then |
if flag == nil then |
||
return |
return default |
||
elseif type( flag ) == 'boolean' then |
elseif type( flag ) == 'boolean' then |
||
return flag |
return flag |
||
Line 22: | Line 79: | ||
end |
end |
||
local function makeLine( label, |
local function makeLine( label, first_values, second_values, color_values ) |
||
local result = {'|- style="text-align: center;"\n! scope="row" style="height: 16px;" | ', label, "\n"} |
|||
local result, color_str, value_str |
|||
result = {'|- style="text-align: center;"\n! scope="row" style="height: 16px;" | ', label, "\n"} |
|||
for i = 1,13 do |
for i = 1,13 do |
||
color_str = color_values[i] |
local color_str = color_values[i] |
||
if i == 13 then |
if i == 13 then |
||
Line 35: | Line 90: | ||
end |
end |
||
local display = first_values[i]:getDisplay() |
|||
value_str = first_value_string[i] |
|||
if |
if display then |
||
table.insert( result, |
table.insert( result, display ) |
||
if |
if second_values ~= nil then |
||
display = second_values[i]:getDisplay() |
|||
value_str = second_value_string[i] |
|||
if |
if display then |
||
table.insert( result, "<br /> |
table.insert( result, "<br />(" .. display .. ")" ) |
||
end |
end |
||
end |
end |
||
Line 57: | Line 112: | ||
local month_names = { 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', |
local month_names = { 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', |
||
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'year' } |
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'year' } |
||
local str |
local str |
||
local values = {} |
|||
local output_string = {} |
|||
local output_value = {} |
|||
local variant = {} |
|||
if suffix == nil then |
if suffix == nil then |
||
for |
for i, mon in ipairs( month_names ) do |
||
if include_space then |
if include_space then |
||
str = |
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 |
else |
||
str = frame.args[ mon .. group_name ] or '' |
|||
table.insert( output_string, str ) |
|||
table.insert( output_value, -9999 ) |
|||
end |
end |
||
values[i] = Value.new(str) |
|||
end |
end |
||
else |
else |
||
for i, mon in ipairs( month_names ) do |
|||
local updated = false |
|||
local value, updated |
|||
for _, mon in ipairs( month_names ) do |
|||
for var, suf in ipairs( suffix ) do |
|||
updated = false |
|||
for i, suf in ipairs( suffix ) do |
|||
if include_space then |
if include_space then |
||
str = frame.args[ mon .. ' ' .. group_name .. ' ' .. suf ] |
str = frame.args[ mon .. ' ' .. group_name .. ' ' .. suf ] |
||
Line 89: | Line 133: | ||
end |
end |
||
if str ~= nil and str ~= '' then |
if str ~= nil and str ~= '' then |
||
value = Value.new(str) |
|||
value.variant = var |
|||
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 |
updated = true |
||
break |
break |
||
Line 103: | Line 140: | ||
end |
end |
||
if not updated then |
if not updated then |
||
value = Value.new() |
|||
table.insert( output_string, '' ) |
|||
value.variant = 0 |
|||
table.insert( output_value, -9999 ) |
|||
table.insert( variant, 0 ) |
|||
end |
end |
||
values[i] = value |
|||
end |
end |
||
end |
end |
||
return values |
|||
return output_string, output_value, variant |
|||
end |
end |
||
local function getAnnualValue( values, mode ) |
local function getAnnualValue( values, mode ) |
||
local total = 0 |
|||
local val = 0 |
|||
if mode == 'avg' or mode == 'sum' then |
if mode == 'avg' or mode == 'sum' then |
||
local |
local total = 0 |
||
local p1, p2, variant |
|||
p1 = 0 |
p1 = 0 |
||
for i = 1, 12 do |
for i = 1, 12 do |
||
if not values[i]:isValid() then |
|||
return Value.new() |
|||
if val == -9999 then |
|||
end |
|||
return '', -9999 |
|||
if not variant then |
|||
local var = values[i].variant |
|||
if var and var ~= 0 then |
|||
variant = var |
|||
end |
|||
end |
end |
||
p2 = |
p2 = values[i]:getPrecision() |
||
if p2 > p1 then |
if p2 > p1 then |
||
p1 = p2 |
p1 = p2 |
||
end |
end |
||
total = total + |
total = total + values[i].number |
||
end |
end |
||
local value = Value.new(total) |
|||
if mode == 'avg' then |
if mode == 'avg' then |
||
value:setNumberRounded( total / 12, p1 ) |
|||
end |
end |
||
value.variant = variant |
|||
return tostring( total ), total |
|||
return value |
|||
elseif mode == 'min' then |
elseif mode == 'min' then |
||
local |
local target |
||
for i = 1, 12 do |
for i = 1, 12 do |
||
if values[i]:isValid() then |
|||
if |
if target == nil or values[i].number < target.number then |
||
target = values[i] |
|||
if min_val == nil or val < min_val then |
|||
min_val = val |
|||
end |
end |
||
end |
end |
||
end |
end |
||
return |
return target or Value.new() |
||
elseif mode == 'max' then |
elseif mode == 'max' then |
||
local |
local target |
||
for i = 1, 12 do |
for i = 1, 12 do |
||
if values[i]:isValid() then |
|||
if |
if target == nil or values[i].number > target.number then |
||
target = values[i] |
|||
if max_val == nil or val > max_val then |
|||
max_val = val |
|||
end |
end |
||
end |
end |
||
end |
end |
||
return |
return target or Value.new() |
||
else |
else |
||
error( 'Unrecognized Annual Mode' ) |
error( 'Unrecognized Annual Mode' ) |
||
Line 164: | Line 203: | ||
end |
end |
||
local function reconcileTemperature( |
local function reconcileTemperature( C_values, F_values ) |
||
local p |
|||
for i = 1,13 do |
for i = 1,13 do |
||
local p |
|||
if C_degree_strings[i] == '' then |
|||
if C_values[i].string == '' then |
|||
if F_values[i]:isValid() then |
|||
p = math.max( 0, math_mod._precision( F_degree_strings[i] ) ) |
|||
p = F_values[i]:getPrecision() |
|||
C_degree_values[i] = math_mod._round( (F_degree_values[i] - 32)*5/9, p ) |
|||
C_values[i]:setNumberRounded( (F_values[i].number - 32)*5/9, p ) |
|||
C_degree_strings[i] = tostring( C_degree_values[i] ) |
|||
end |
end |
||
elseif |
elseif F_values[i].string == '' then |
||
if |
if C_values[i]:isValid() then |
||
p = C_values[i]:getPrecision() |
|||
p = math.max( 0, math_mod._precision( C_degree_strings[i] ) ) |
|||
F_values[i]:setNumberRounded( C_values[i].number*9/5 + 32, p ) |
|||
F_degree_strings[i] = tostring( F_degree_values[i] ) |
|||
end |
end |
||
end |
end |
||
end |
end |
||
return C_degree_strings, C_degree_values, F_degree_strings, F_degree_values |
|||
end |
end |
||
local function reconcilePrecipitation( |
local function reconcilePrecipitation( M_values, I_values, prefer_cm ) |
||
I_degree_strings, I_degree_values, variant, prefer_cm ) |
|||
local p |
|||
local v_class = 0 |
local v_class = 0 |
||
for i = 1,13 do |
for i = 1,13 do |
||
if |
if M_values[i].variant == 1 then |
||
v_class = 1 |
v_class = 1 |
||
elseif |
elseif M_values[i].variant == 2 then |
||
v_class = 2 |
v_class = 2 |
||
end |
end |
||
Line 204: | Line 237: | ||
end |
end |
||
for i = 1,13 do |
for i = 1,13 do |
||
local p |
|||
if M_degree_strings[i] == '' then |
|||
if M_values[i].string == '' then |
|||
if I_values[i]:isValid() then |
|||
if v_class == 1 then |
if v_class == 1 then |
||
p = |
p = I_values[i]:getPrecision() - 1 |
||
M_values[i]:setNumberRounded( I_values[i].number*2.54, p ) |
|||
variant[i] = v_class |
|||
else |
else |
||
p = |
p = I_values[i]:getPrecision() - 2 |
||
M_values[i]:setNumberRounded( I_values[i].number*25.4, p ) |
|||
variant[i] = v_class |
|||
end |
end |
||
M_values[i].variant = v_class |
|||
M_degree_strings[i] = tostring( M_degree_values[i] ) |
|||
end |
end |
||
elseif |
elseif I_values[i].string == '' then |
||
if |
if M_values[i]:isValid() then |
||
if |
if M_values[i].variant == 1 then |
||
p = M_values[i]:getPrecision() |
|||
p = math.max( 0, math_mod._precision( M_degree_strings[i] ) ) |
|||
I_values[i].number = M_values[i].number/2.54 |
|||
else |
else |
||
p = |
p = M_values[i]:getPrecision() + 1 |
||
I_values[i].number = M_values[i].number/25.4 |
|||
end |
end |
||
I_values[i]:setNumberRounded( I_values[i].number, p ) |
|||
I_degree_strings[i] = tostring( I_degree_values[i] ) |
|||
end |
end |
||
end |
end |
||
end |
end |
||
return M_degree_strings, M_degree_values, I_degree_strings, I_degree_values, variant |
|||
end |
end |
||
Line 237: | Line 267: | ||
local mode = (frame.args.mode or 'basic'):lower() |
local mode = (frame.args.mode or 'basic'):lower() |
||
local group_name = frame.args.group_name |
local group_name = frame.args.group_name |
||
local first_values, second_values |
|||
local first_value_string, second_value_string |
|||
local |
local color_values |
||
local color_scheme = frame.args.color_scheme or 't' |
local color_scheme = frame.args.color_scheme or 't' |
||
local scale_factor = math_mod._cleanNumber( frame.args.scale_factor) or 1 |
local scale_factor = math_mod._cleanNumber( frame.args.scale_factor) or 1 |
||
local date_mode = checkFlag( frame.args.date_mode |
local date_mode = checkFlag( frame.args.date_mode, false ) |
||
local label = frame.args.label or '' |
local label = frame.args.label or '' |
||
local annual_mode = (frame.args.annual_mode or 'avg'):lower() |
local annual_mode = (frame.args.annual_mode or 'avg'):lower() |
||
local include_space = checkFlag( frame.args.include_space |
local include_space = checkFlag( frame.args.include_space, true ) |
||
local second_line = checkFlag( frame.args.second_line ) |
local second_line = checkFlag( frame.args.second_line, false ) |
||
local prefer_cm = checkFlag( frame.args.prefer_cm ) |
local prefer_cm = checkFlag( frame.args.prefer_cm, false ) |
||
local result |
|||
local variant |
|||
local pframe = frame:getParent() |
local pframe = frame:getParent() |
||
local imperial_first = checkFlag( frame.args['imperial first'] ) |
local imperial_first = checkFlag( frame.args['imperial first'] or pframe.args['imperial first'] ) |
||
local metric_first = checkFlag( frame.args['metric first'] or pframe.args['metric first'] ) |
|||
local single_line = checkFlag( frame.args['single line'] or pframe.args['single line'] ) |
|||
if imperial_first == nil then |
|||
local metric_first = checkFlag( frame.args['metric first'] ) |
|||
imperial_first = metric_first == nil and true or not 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 |
end |
||
if mode == 'basic' then |
if mode == 'basic' then |
||
first_values = getInputs( pframe, group_name, nil, include_space ) |
|||
second_values = nil |
|||
nil, include_space ) |
|||
second_value_string = nil |
|||
second_value_number = nil |
|||
elseif mode == 'temperature' then |
elseif mode == 'temperature' then |
||
first_values = getInputs( pframe, group_name, {'C'}, include_space ) |
|||
second_values = getInputs( pframe, group_name, {'F'}, include_space ) |
|||
reconcileTemperature( first_values, second_values ) |
|||
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 |
elseif mode == "precipitation" then |
||
first_values = getInputs( pframe, group_name, {'cm', 'mm'}, include_space ) |
|||
second_values = getInputs( pframe, group_name, {'inch'}, include_space ) |
|||
reconcilePrecipitation( first_values, second_values, prefer_cm ) |
|||
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 |
else |
||
error( 'Requested mode not recognized' ) |
error( 'Requested mode not recognized' ) |
||
Line 294: | Line 303: | ||
local good = false |
local good = false |
||
for i = 1,13 do |
for i = 1,13 do |
||
if |
if first_values[i].string ~= '' then |
||
good = true |
good = true |
||
break |
break |
||
Line 303: | Line 312: | ||
end |
end |
||
if |
if first_values[13].string == '' then |
||
first_values[13] = getAnnualValue( first_values, annual_mode ) |
|||
end |
end |
||
if |
if second_values ~= nil then |
||
if |
if second_values[13].string == '' then |
||
second_values[13] = getAnnualValue( second_values, 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 |
||
end |
end |
||
Line 324: | Line 325: | ||
color_values = {} |
color_values = {} |
||
local month_adj = { 31/30, 28.25/30, 31/30, 1, 31/30, 1, |
local month_adj = { 31/30, 28.25/30, 31/30, 1, 31/30, 1, |
||
31/30, 31/30, 1, 31/30, 1, 31/30, |
31/30, 31/30, 1, 31/30, 1, 31/30, 365.25/30 } |
||
local adj |
|||
for i = 1,13 do |
for i = 1,13 do |
||
if first_values[i]:isValid() then |
|||
if first_value_number[i] ~= nil and first_value_number[i] ~= -9999 then |
|||
adj = scale_factor |
local adj = scale_factor |
||
if date_mode then |
if date_mode then |
||
adj = adj / month_adj[i] |
adj = adj / month_adj[i] |
||
end |
end |
||
if mode == "precipitation" then |
if mode == "precipitation" then |
||
if |
if first_values[i].variant == 1 then |
||
adj = adj * 10 |
adj = adj * 10 |
||
end |
end |
||
end |
end |
||
table.insert( color_values, color_scheme( |
table.insert( color_values, color_scheme( first_values[i].number * adj ) ) |
||
else |
else |
||
table.insert( color_values, color_scheme( nil ) ) |
table.insert( color_values, color_scheme( nil ) ) |
||
Line 343: | Line 343: | ||
end |
end |
||
if imperial_first and second_values ~= nil then |
|||
local lang = mw.getContentLanguage() |
|||
first_values, second_values = second_values, first_values |
|||
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 |
end |
||
if not single_line then |
if not single_line then |
||
if second_line and |
if second_line and second_values ~= nil then |
||
first_values = second_values |
|||
first_value_string = second_value_string |
|||
end |
end |
||
second_values = nil |
|||
end |
end |
||
return makeLine( label, |
return makeLine( label, first_values, second_values, color_values ) |
||
end |
end |
||
Revision as of 09:00, 29 December 2018
local w = {}
local math_mod = require( "Module:Math" )
local wbc = require( "Module:WeatherBoxColors" )
local Value
Value = {
lang = mw.getContentLanguage(),
getDisplay = function (self)
if not self:isValid() then
return nil
end
local display = self.string
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
if type(v) == 'string' then
val, str = math_mod._cleanNumber(v)
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,
}, Value)
end,
setNumberRounded = function (self, number, precision)
-- precision is a small integer >= 0.
self.number = math_mod._round(number, precision)
local fmt = '%.' .. string.format('%d', precision) .. 'f'
self.string = string.format(fmt, self.number)
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 )
local result = {'|- style="text-align: center;"\n! scope="row" style="height: 16px;" | ', label, "\n"}
for i = 1,13 do
local 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
local display = first_values[i]:getDisplay()
if display then
table.insert( result, display )
if second_values ~= nil then
display = second_values[i]:getDisplay()
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( frame, group_name, suffix, include_space )
local month_names = { 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'year' }
local str
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
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 )
if mode == 'avg' or mode == 'sum' then
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 )
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
end
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
elseif M_values[i].variant == 2 then
v_class = 2
end
end
if v_class == 0 then
if prefer_cm then
v_class = 1
else
v_class = 2
end
end
for i = 1,13 do
local p
if M_values[i].string == '' then
if I_values[i]:isValid() then
if v_class == 1 then
p = I_values[i]:getPrecision() - 1
M_values[i]:setNumberRounded( I_values[i].number*2.54, p )
else
p = I_values[i]:getPrecision() - 2
M_values[i]:setNumberRounded( I_values[i].number*25.4, p )
end
M_values[i].variant = v_class
end
elseif I_values[i].string == '' then
if M_values[i]:isValid() then
if M_values[i].variant == 1 then
p = M_values[i]:getPrecision()
I_values[i].number = M_values[i].number/2.54
else
p = M_values[i]:getPrecision() + 1
I_values[i].number = M_values[i].number/25.4
end
I_values[i]:setNumberRounded( I_values[i].number, p )
end
end
end
end
function w.buildRow( frame )
local mode = (frame.args.mode or 'basic'):lower()
local group_name = frame.args.group_name
local first_values, second_values
local 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, 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, true )
local second_line = checkFlag( frame.args.second_line, false )
local prefer_cm = checkFlag( frame.args.prefer_cm, false )
local pframe = frame:getParent()
local imperial_first = checkFlag( frame.args['imperial first'] or pframe.args['imperial first'] )
local metric_first = checkFlag( frame.args['metric first'] or pframe.args['metric first'] )
local single_line = checkFlag( frame.args['single line'] or pframe.args['single line'] )
if imperial_first == nil then
imperial_first = metric_first == nil and true or not metric_first
end
if mode == 'basic' then
first_values = getInputs( pframe, group_name, nil, include_space )
second_values = nil
elseif mode == 'temperature' then
first_values = getInputs( pframe, group_name, {'C'}, include_space )
second_values = getInputs( pframe, group_name, {'F'}, include_space )
reconcileTemperature( first_values, second_values )
elseif mode == "precipitation" then
first_values = getInputs( pframe, group_name, {'cm', 'mm'}, include_space )
second_values = getInputs( pframe, 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 single_line 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
return w