Module:Weather box/row explained

local math_mod = require('Module:Math')

local traceTextlocal ValueValue = 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') endend

local function makeLine(label, first_values, second_values, color_values) local result = 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, "
(" .. 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 month_names = local str local values = if suffix

nil then 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 valuesend

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') endend

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 endend

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 units if M_values[i].string

then if I_values[i]:isValid then if v_class

1 then units = 'in2cm' else units = 'in2mm' end M_values[i]:setConvert(I_values[i], units) 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 units = 'cm2in' else units = 'mm2in' end I_values[i]:setConvert(M_values[i], units) end end endend

local function _buildRow(definition, args, options) options = options or local wbc = require('Module:Weather box/colors' .. (options.sandbox or )) local mode = (definition.mode or 'basic'):lower local group_name = definition.group_name local first_values, second_values local color_values local color_scheme = definition.color_scheme or 't' local scale_factor = math_mod._cleanNumber(definition.scale_factor) or 1 local date_mode = checkFlag(definition.date_mode, false) local label = definition.label or local annual_mode = (definition.annual_mode or 'avg'):lower local include_space = checkFlag(definition.include_space, true) local second_line = checkFlag(definition.second_line, false) local prefer_cm = checkFlag(definition.prefer_cm, false) local imperial_first = checkFlag(args['imperial first']) 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,, include_space) second_values = getInputs(args, group_name,, include_space) reconcileTemperature(first_values, second_values) elseif mode

"precipitation" then first_values = getInputs(args, group_name,, include_space) second_values = getInputs(args, group_name,, 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 = 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 _buildRow(frame.args, frame:getParent.args) -- row definition, template parametersend

return