Module:Weather box/sandbox: Difference between revisions

Content deleted Content added
refactor so makeFrame kludge is not required; define wantSingleLine in one place for consistency and to change it so default is true; require sandbox module if called from sandbox
sync + try "Percentage possible sunshine" per talk proposal
 
(35 intermediate revisions by 9 users not shown)
Line 1:
-- Implement [[Template:Weather box]].
 
local precision_precision = require('Module: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)
Line 24 ⟶ 34:
if isAny(args, ' ' .. what .. ' cm') then
first = 'cm'
if elseif isAny(args, ' ' .. what .. ' mm') then
first = 'mm'
else
first = (what:sub(1, 4) == 'snow' or
if isAny(args, ' ' .. what .. ' mm') then
precision(args['Jan ' .. what .. ' inch'] or '0') < 1)
first = 'mm'
and 'cm'
else
first = (what == or 'snowmm' or
precision(args['Jan ' .. what .. ' inch'] or '0') < 1)
and 'cm'
or 'mm'
end
end
local second = 'inches'
Line 66 ⟶ 74:
 
local function wantSingle(parm)
--------------------------------------------------------------------------------
-- Originally needed "|single line=yes" or "|single line=<any non-blank>" for
-- Result before July 2022 for single_line setting:
-- single line output (default was separate lines for metric/imperial units).
-- blank/omitted : separate table rows for metric and imperial (two rows)
-- That was changed in June 2020 so single line is the default and
-- N : bug: only one row with either metric or imperial (not both)
-- now need "|single line=no" or "|single line=n" for separate lines.
-- but heading like "cm (inches)"
-- Y/junk : single row with both metric/imperial in same cell
--------------------------------------------------------------------------------
-- ThatIntend was changedchanging in JuneJuly 20202022 so single linesingle_line=Y is the default and
-- now need "|single line=no" or "|single linesingle_line=n"N for separate lines.
-- However, this release (1 July 2022) keeps blank/omitted as meaning "no"
-- so the other large changes can be tested before switching the default.
parm = (parm or ''):lower()
return not (parm == 'no' or parm == 'n' or parm == '')
end
 
local function getDefinitions(frame, args, options)
-- Return a list of tables or strings that define each row.
local navbar = require('Module:Navbar')._navbar
local function _if(parm, a, b)
return stripToNil(args[parm]) and a or b or ''
Line 88 ⟶ 102:
local function _ifSingle(a, b)
return options.wantSingleLine and a or b or ''
end
local function _colorscheme(what, default)
return stripToNil(args[what .. ' colour']) or stripToNil(args[what .. ' color']) or default
end
local function _margin()
if stripToNil(args.margin) then
return 'margin:' .. args.margin .. ';'
end
return ''
end
local ___location = _if('___location', args.___location, '{{{___location}}}') -- show "{{{___location}}}" to alert editor if parameter is blank
local navbarText
if not stripToNil(args.open) then
if stripToNil(args.name) then
local navbar = require('Module:Navbar')._navbar
navbarText = navbar({'Climate data for ' .. _if('___location', args.___location, '{{{___location}}}'), args.name, collapsible=1}),
end
end
return {
 
----------- HEADER ----------
'{| class="wikitable ' .. _if('open', '', 'mw-collapsible' .. _if('collapsed', ' mw-collapsed')) ..
'" style="width:' .. _ifset('width', '100%auto') ..
'; text-align:center; line-height: 1.2em; margin:' ..
_ifset_margin('margin', 'auto') .. ';"' ..
_if('open',
'\n|+Climate data for ' .. _if('___location', args.___location, '{{{___location}}}'),
-- Following intentionally shows "{{{___location}}}" if parameter is blank to alert editor.
'\n|+Climate data for ' .. _if('___location', args.___location, '{{{___location}}}'),
'\n|-' ..
'\n!colspan="14" | ' .. _if('name', navbarText, 'Climate data for ' .. ___location)
_if('name',
navbar({'Climate data for ' .. _if('___location', args.___location, '{{{___location}}}'), args.name, collapsible=1}),
'Climate data for ' .. _if('___location', args.___location, '{{{___location}}}')
)
) ..
[=[
Line 129 ⟶ 155:
mode = 'basic',
group_name = 'maximum humidex',
color_scheme = _ifset_colorscheme('temperature colour', 't'),
scale_factor = '1',
label = 'Record high [[humidex]]',
Line 138 ⟶ 164:
mode = 'temperature',
group_name = 'record high',
color_scheme = _ifset_colorscheme('temperature colour', 't'),
scale_factor = '1',
label = 'Record high °' .. _if('metric first', 'C', 'F') .. _ifSingle(' (°' .. _if('metric first', 'F', 'C') .. ')'),
Line 147 ⟶ 173:
mode = 'temperature',
group_name = 'avg record high',
color_scheme = _ifset_colorscheme('temperature colour', 't'),
scale_factor = '1',
label = 'Mean maximum °' .. _if('metric first', 'C', 'F') .. _ifSingle(' (°' .. _if('metric first', 'F', 'C') .. ')'),
annual_mode = 'max',
},
{---------- FIRST LINE AVERAGEMEAN HIGHDAILY MAXIMUM TEMPERATURES ----------
WANTROW = _ifany(' high C') or _ifany(' high F'),
mode = 'temperature',
group_name = 'high',
color_scheme = _ifset_colorscheme('temperature colour', 't'),
scale_factor = '1',
label = 'AverageMean highdaily maximum °' .. _if('metric first', 'C', 'F') .. _ifSingle(' (°' .. _if('metric first', 'F', 'C') .. ')'),
annual_mode = 'avg',
},
Line 165 ⟶ 191:
mode = 'temperature',
group_name = 'mean',
color_scheme = _ifset_colorscheme('temperature colour', 't'),
scale_factor = '1',
label = 'Daily mean °' .. _if('metric first', 'C', 'F') .. _ifSingle(' (°' .. _if('metric first', 'F', 'C') .. ')'),
annual_mode = 'avg',
},
{---------- FIRST LINE AVERAGEMEAN LOWDAILY MINIMUM TEMPERATURES ----------
WANTROW = _ifany(' low C') or _ifany(' low F'),
mode = 'temperature',
group_name = 'low',
color_scheme = _ifset_colorscheme('temperature colour', 't'),
scale_factor = '1',
label = 'AverageMean lowdaily minimum °' .. _if('metric first', 'C', 'F') .. _ifSingle(' (°' .. _if('metric first', 'F', 'C') .. ')'),
annual_mode = 'avg',
},
Line 183 ⟶ 209:
mode = 'temperature',
group_name = 'avg record low',
color_scheme = _ifset_colorscheme('temperature colour', 't'),
scale_factor = '1',
label = 'Mean minimum °' .. _if('metric first', 'C', 'F') .. _ifSingle(' (°' .. _if('metric first', 'F', 'C') .. ')'),
Line 192 ⟶ 218:
mode = 'temperature',
group_name = 'record low',
color_scheme = _ifset_colorscheme('temperature colour', 't'),
scale_factor = '1',
label = 'Record low °' .. _if('metric first', 'C', 'F') .. _ifSingle(' (°' .. _if('metric first', 'F', 'C') .. ')'),
Line 201 ⟶ 227:
mode = 'basic',
group_name = 'chill',
color_scheme = _ifset_colorscheme('temperature colour', 't'),
scale_factor = '1',
label = 'Record low [[wind chill]]',
Line 210 ⟶ 236:
mode = 'precipitation',
group_name = 'precipitation',
color_scheme = _ifset_colorscheme('precipitation colour', 'p'),
date_mode = true,
scale_factor = '1',
Line 221 ⟶ 247:
mode = 'precipitation',
group_name = 'rain',
color_scheme = _ifset_colorscheme('rain colour', 'p'),
date_mode = true,
scale_factor = '1',
Line 233 ⟶ 259:
group_name = 'snow',
prefer_cm = true,
color_scheme = _ifset_colorscheme('snow colour', 'p'),
date_mode = true,
scale_factor = '1',
label = makeLabel(args, options, true, 'Average snowfall', 'snow'),
annual_mode = 'sum',
},
{---------- FIRST LINE AVERAGE EXTREME SNOW DEPTH ----------
WANTROW = _ifany(' snow depth cm') or _ifany(' snow depth mm') or _ifany(' snow depth inch'),
mode = 'precipitation',
group_name = 'snow depth',
prefer_cm = true,
color_scheme = _colorscheme('snow', 'p'),
scale_factor = '0.2',
label = makeLabel(args, options, true, 'Average extreme snow depth', 'snow depth'),
annual_mode = 'max',
},
{---------- SECOND LINE MAXIMUM HUMIDEX ----------
Line 243 ⟶ 279:
mode = 'basic',
group_name = 'maximum humidex',
color_scheme = _ifset_colorscheme('temperature colour', 't'),
scale_factor = '1',
label = '[[Humidex]]',
Line 254 ⟶ 290:
group_name = 'record high',
second_line = true,
color_scheme = _ifset_colorscheme('temperature colour', 't'),
scale_factor = '1',
label = 'Record high °' .. _if('metric first', 'F', 'C'),
annual_mode = 'max',
},
{---------- SECOND LINE AVERAGEMEAN HIGHDAILY MAXIMUM TEMPERATURES ----------
WANTROW = not options.wantSingleLine and (_ifany(' high C') or _ifany(' high F')),
mode = 'temperature',
group_name = 'high',
second_line = true,
color_scheme = _ifset_colorscheme('temperature colour', 't'),
scale_factor = '1',
label = 'AverageMean highdaily maximum °' .. _if('metric first', 'F', 'C'),
annual_mode = 'avg',
},
Line 274 ⟶ 310:
group_name = 'mean',
second_line = true,
color_scheme = _ifset_colorscheme('temperature colour', 't'),
scale_factor = '1',
label = 'Daily mean °' .. _if('metric first', 'F', 'C'),
Line 280 ⟶ 316:
annual_mode = 'avg',
},
{---------- SECOND LINE AVERAGEMEAN LOWDAILY MINIMUM TEMPERATURES ----------
WANTROW = not options.wantSingleLine and (_ifany(' low C') or _ifany(' low F')),
mode = 'temperature',
group_name = 'low',
second_line = true,
color_scheme = _ifset_colorscheme('temperature colour', 't'),
scale_factor = '1',
label = 'AverageMean lowdaily minimum °' .. _if('metric first', 'F', 'C'),
show = _if('metric first', '2', '1'),
annual_mode = 'avg',
Line 296 ⟶ 332:
group_name = 'record low',
second_line = true,
color_scheme = _ifset_colorscheme('temperature colour', 't'),
scale_factor = '1',
label = 'Record low °' .. _if('metric first', 'F', 'C'),
Line 306 ⟶ 342:
mode = 'basic',
group_name = 'chill',
color_scheme = _ifset_colorscheme('temperature colour', 't'),
scale_factor = '1',
label = '[[Wind chill]]',
Line 316 ⟶ 352:
group_name = 'precipitation',
second_line = true,
color_scheme = _ifset_colorscheme('precipitation colour', 'p'),
date_mode = true,
scale_factor = '1',
Line 328 ⟶ 364:
group_name = 'rain',
second_line = true,
color_scheme = _ifset_colorscheme('rain colour', 'p'),
date_mode = true,
scale_factor = '1',
Line 341 ⟶ 377:
second_line = true,
prefer_cm = true,
color_scheme = _ifset_colorscheme('snow colour', 'p'),
date_mode = true,
scale_factor = '1',
label = makeLabel(args, options, false, 'Average snowfall', 'snow'),
annual_mode = 'sum',
},
{---------- SECOND LINE AVERAGE EXTREME SNOW DEPTH ----------
WANTROW = not options.wantSingleLine and (_ifany(' snow depth cm') or _ifany(' snow depth mm') or _ifany(' snow depth inch')),
mode = 'precipitation',
group_name = 'snow depth',
second_line = true,
prefer_cm = true,
color_scheme = _colorscheme('snow', 'p'),
scale_factor = '0.2',
label = makeLabel(args, options, false, 'Average extreme snow depth', 'snow depth'),
annual_mode = 'max',
},
{---------- PRECIPITATION DAYS ----------
Line 351 ⟶ 398:
mode = 'basic',
group_name = 'precipitation days',
color_scheme = _ifset_colorscheme('precip days colour', 'd'),
date_mode = true,
scale_factor = '1',
Line 361 ⟶ 408:
mode = 'basic',
group_name = 'rain days',
color_scheme = _ifset_colorscheme('precip days colour', 'd'),
date_mode = true,
scale_factor = '1',
Line 371 ⟶ 418:
mode = 'basic',
group_name = 'snow days',
color_scheme = _ifset_colorscheme('precip days colour', 'd'),
date_mode = true,
scale_factor = '1',
Line 381 ⟶ 428:
mode = 'basic',
group_name = 'humidity',
color_scheme = _ifset_colorscheme('humidity colour', 'h'),
scale_factor = '1',
label = 'Average [[relative humidity]] (%)' ..
Line 392 ⟶ 439:
mode = 'basic',
group_name = 'afthumidity',
color_scheme = _ifset_colorscheme('humidity colour', 'h'),
scale_factor = '1',
label = 'Average afternoon [[relative humidity]] (%)' ..
Line 403 ⟶ 450:
mode = 'temperature',
group_name = 'dew point',
color_scheme = _ifset_colorscheme('temperature colour', 't'),
scale_factor = '1',
label = 'Average [[dew point]] °' .. _if('metric first', 'C', 'F') .. _ifSingle(' (°' .. _if('metric first', 'F', 'C') .. ')'),
Line 413 ⟶ 460:
group_name = 'dew point',
second_line = true,
color_scheme = _ifset_colorscheme('temperature colour', 't'),
scale_factor = '1',
label = 'Average [[dew point]] °' .. _if('metric first', 'F', 'C'),
Line 423 ⟶ 470:
mode = 'basic',
group_name = 'sun',
color_scheme = _ifset_colorscheme('sun colour', 's'),
date_mode = true,
scale_factor = '1',
Line 433 ⟶ 480:
mode = 'basic',
group_name = 'd sun',
color_scheme = _ifset_colorscheme('sun colour', 's'),
include_space = false,
scale_factor = '30.44',
Line 443 ⟶ 490:
mode = 'basic',
group_name = ' light',
color_scheme = _ifset_colorscheme('sun colour', 's'),
include_space = false,
scale_factor = '30.44',
Line 453 ⟶ 500:
mode = 'basic',
group_name = 'percentsun',
color_scheme = _ifset_colorscheme('sun colour', 's'),
scale_factor = '7.2',
label = 'PercentPercentage [[Sunshine duration|possible sunshine]]',
annual_mode = 'avg',
},
Line 462 ⟶ 509:
mode = 'basic',
group_name = 'uv',
color_scheme = _ifset_colorscheme('uv colour', 'u'),
scale_factor = '1',
label = 'Average [[ultraviolet index]]',