Mô đun:Sandbox/Giahytran/weatherbox

local _precision = require('Mô đun:Math')._precision
local function precision(text)
-- Input like 'Jan precipitation inch = trace' calls this with text = 'trace'
-- which would cause _precision to throw an error since it is not numeric.
-- Workaround: Return 0 as the precision if an error occurs.
local success, result = pcall(_precision, text)
if success then
return result
end
return 0
end

local function stripToNil(text)
-- If text is a non-empty string, return its trimmed content.
-- Otherwise, return nothing (text is an empty string or is not a string).
if type(text) == 'string' then
return text:match('(%S.-)%s$')
end
end

local function isAny(args, suffix)
local months = { 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' }
for _, month in ipairs(months) do
if stripToNil(args[month .. suffix]) then
return true
end
end
end

local function makeLabel(args, options, is_first, base, what)
local first
if isAny(args, ' ' .. what .. ' cm') then
first = 'cm'
elseif isAny(args, ' ' .. what .. ' mm') then
first = 'mm'
else
first = (what:sub(1, 4) == 'snow' or
precision(args['Jan ' .. what .. ' inch'] or '0') (≥ ' .. _ifset('unit precipitation days', ) .. ')'),
annual_mode = 'sum',
},
{---------- RAINY DAYS ----------
WANTROW = _ifany(' rain days'),
mode = 'basic',
group_name = 'rain days',
color_scheme = _colorscheme('precip days', 'd'),
date_mode = true,
scale_factor = '1',
label = 'Average rainy days' .. _if('unit rain days', ' (≥ ' .. _ifset('unit rain days', ) .. ')'),
annual_mode = 'sum',
},
{---------- SNOWY DAYS ----------
WANTROW = _ifany(' snow days'),
mode = 'basic',
group_name = 'snow days',
color_scheme = _colorscheme('precip days', 'd'),
date_mode = true,
scale_factor = '1',
label = 'Average snowy days' .. _if('unit snow days', ' (≥ ' .. _ifset('unit snow days', ) .. ')'),
annual_mode = 'sum',
},
{---------- PERCENT RELATIVE HUMIDITY ----------
WANTROW = _ifany(' humidity'),
mode = 'basic',
group_name = 'humidity',
color_scheme = _colorscheme('humidity', 'h'),
scale_factor = '1',
label = 'Average relative humidity (%)' ..
_if('time day', ' (at ' .. _ifset('time day', ) .. ')') ..
_if('daily', ' (daily average)'),
annual_mode = 'avg',
},
{---------- AFTERNOON PERCENT RELATIVE HUMIDITY ----------
WANTROW = _ifany(' afthumidity'),
mode = 'basic',
group_name = 'afthumidity',
color_scheme = _colorscheme('humidity', 'h'),
scale_factor = '1',
label = 'Average afternoon relative humidity (%)' ..
_if('time day', ' (at ' .. _ifset('time day', ) .. ')') ..
_if('daily', ' (daily average)'),
annual_mode = 'avg',
},
{---------- FIRST LINE AVERAGE DEW POINT ----------
WANTROW = _ifany(' dew point C') or _ifany(' dew point F'),
mode = 'temperature',
group_name = 'dew point',
color_scheme = _colorscheme('temperature', 't'),
scale_factor = '1',
label = 'Average dew point °' .. _if('metric first', 'C', 'F') .. _ifSingle(' (°' .. _if('metric first', 'F', 'C') .. ')'),
annual_mode = 'avg',
},
{---------- SECOND LINE AVERAGE DEW POINT----------
WANTROW = not options.wantSingleLine and (_ifany(' dew point C') or _ifany(' dew point F')),
mode = 'temperature',
group_name = 'dew point',
second_line = true,
color_scheme = _colorscheme('temperature', 't'),
scale_factor = '1',
label = 'Average dew point °' .. _if('metric first', 'F', 'C'),
show = _if('metric first', '2', '1'),
annual_mode = 'avg',
},
{---------- MONTHLY SUNSHINE HOURS ----------
WANTROW = _ifany(' sun'),
mode = 'basic',
group_name = 'sun',
color_scheme = _colorscheme('sun', 's'),
date_mode = true,
scale_factor = '1',
label = 'Mean monthly sunshine hours',
annual_mode = 'sum',
},
{---------- DAILY SUNSHINE HOURS ----------
WANTROW = _ifany('d sun'),
mode = 'basic',
group_name = 'd sun',
color_scheme = _colorscheme('sun', 's'),
include_space = false,
scale_factor = '30.44',
label = 'Mean daily sunshine hours',
annual_mode = 'avg',
},
{---------- DAILY DAYLIGHT HOURS ----------
WANTROW = _ifany(' light'),
mode = 'basic',
group_name = ' light',
color_scheme = _colorscheme('sun', 's'),
include_space = false,
scale_factor = '30.44',
label = 'Mean daily daylight hours',
annual_mode = 'avg',
},
{---------- PERCENT SUNSHINE ----------
WANTROW = _ifany(' percentsun'),
mode = 'basic',
group_name = 'percentsun',
color_scheme = _colorscheme('sun', 's'),
scale_factor = '7.2',
label = 'Percent possible sunshine',
annual_mode = 'avg',
},
{---------- ULTRAVIOLET INDEX ----------
WANTROW = _ifany(' uv'),
mode = 'basic',
group_name = 'uv',
color_scheme = _colorscheme('uv', 'u'),
scale_factor = '1',
label = 'Average ultraviolet index',
annual_mode = 'avg',
},
----------- SOURCES ----------
makeSources(frame, args),
}
end

local function main(frame)
local sandbox = frame:getTitle():find('sandbox', 1, true) and '/sandbox' or
local buildRow = require('Mô đun:Weather box/row' .. sandbox)._buildRow
local args = frame:getParent().args
local options = {
wantSingleLine = wantSingle(args['single line']),
sandbox = sandbox,
}
local results = {}
for i, definition in ipairs(getDefinitions(frame, args, options)) do
local row
if type(definition) == 'string' then
row = definition
elseif definition.WANTROW then
row = buildRow(definition, args, options)
else
row =
end
results[i] = row
end
return '\n'..table.concat(results)..'\n' -- prevent Scribunto from inserting a blank line before the table
end

return {
main = main,
}