Moduuli:Weather box
Siirry navigaatioon
Siirry hakuun
[ muokkaa ] Tämä on en:module:Weather box -sivulta kopioitu moduuli ja siihen on tehty muutoksina pelkästään lokalisoinnit. Moduuli on tarkoitus pitää mahdollisimman pitkälle yhteneväisenä enwikin moduulin kanssa. Samoin ohjeet moduulin käyttöön löytyvät enwikistä.
TODO: lokalisoinnit, SI-yksiköt oletuksena käyttöön
|
-- Implement [[Template:Weather box]].
local _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)
-- 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 = { 'tammi', 'helmi', 'maalis', 'huhti', 'touko', 'kesä', 'heinä', 'elo', 'syys', 'loka', 'marras', 'joulu' }
--]]
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'
else
if isAny(args, ' ' .. what .. ' mm') then
first = 'mm'
else
first = (what:sub(1, 4) == 'snow' or
precision(args['Jan ' .. what .. ' inch'] or '0') < 1)
and 'cm'
or 'mm'
end
end
return base .. ' ' .. first
end
local function makeSources(frame, args)
local source1 = stripToNil(args.source) or stripToNil(args['source 1'])
local source2 = stripToNil(args['source 2']) or stripToNil(args['source2'])
local result = '|-\n|colspan="14" style="text-align:center;font-size:95%;"|'
local text
if source1 or source2 then
if source1 and source2 then
text = 'Lähde 1: ' .. source1 .. '\n' .. result .. 'Lähde 2: ' .. source2
else
text = 'Lähde: ' .. (source1 and source1 or source2)
end
else
text = frame:expandTemplate({ title = 'citation needed', args = {date = stripToNil(args.date)} })
end
return result .. text .. '\n|}'
end
local function wantSingle(parm)
return true
end
local function getDefinitions(frame, args, options)
-- Return a list of tables or strings that define each row.
local function _if(parm, a, b)
return stripToNil(args[parm]) and a or b or ''
end
local function _ifset(parm, a)
return stripToNil(args[parm]) and args[parm] or a
end
local function _ifany(suffix)
return isAny(args, suffix)
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({'Sää - ' .. location, args.name, collapsible=1})
end
end
return {
----------- HEADER ----------
'{| class="wikitable ' .. _if('open', '', 'mw-collapsible' .. _if('collapsed', ' mw-collapsed')) ..
'" style="width:' .. _ifset('width', 'auto') ..
'; text-align:center; line-height: 1.2em; margin:' ..
_ifset('margin', '') .. ';"' ..
_if('open',
'\n|+Sää - ' .. location,
'\n|-' ..
'\n!colspan="14" | ' .. _if('name', navbarText, 'Sää - ' .. location)
) ..
[=[
|-
!scope="row" |Kuukausi
!scope="col" |Tammi
!scope="col" |Helmi
!scope="col" |Maalis
!scope="col" |Huhti
!scope="col" |Touko
!scope="col" |Kesä
!scope="col" |Heinä
!scope="col" |Elo
!scope="col" |Syys
!scope="col" |Loka
!scope="col" |Marras
!scope="col" |Joulu
!scope="col" style="border-left-width:medium" |Vuosi
]=],
{---------- FIRST LINE MAXIMUM HUMIDEX ----------
WANTROW = _ifany(' maximum humidex'),
mode = 'basic',
group_name = 'maximum humidex',
color_scheme = _ifset('temperature colour', 't'),
scale_factor = '1',
label = 'Korkein [[helteen tukaluus]]',
annual_mode = 'max',
},
{---------- FIRST LINE RECORD HIGH TEMPERATURES ----------
WANTROW = _ifany(' record high C'),
mode = 'temperature',
group_name = 'record high',
color_scheme = _ifset('temperature colour', 't'),
scale_factor = '1',
label = 'Korkein lämpötila °C',
annual_mode = 'max',
},
{---------- FIRST-SECOND LINE AVG MONTHLY MAXIMUM TEMPERATURES ----------
WANTROW = _ifany(' avg record high C'),
mode = 'temperature',
group_name = 'avg record high',
color_scheme = _ifset('temperature colour', 't'),
scale_factor = '1',
label = 'Keskimääräinen ylin °C',
annual_mode = 'max',
},
{---------- FIRST LINE AVERAGE HIGH TEMPERATURES ----------
WANTROW = _ifany(' high C'),
mode = 'temperature',
group_name = 'high',
color_scheme = _ifset('temperature colour', 't'),
scale_factor = '1',
label = 'Keskimääräinen korkea °C',
annual_mode = 'avg',
},
{---------- FIRST LINE DAILY MEAN TEMPERATURES ----------
WANTROW = _ifany(' mean C'),
mode = 'temperature',
group_name = 'mean',
color_scheme = _ifset('temperature colour', 't'),
scale_factor = '1',
label = 'Päivittäinen °C',
annual_mode = 'avg',
},
{---------- FIRST LINE AVERAGE LOW TEMPERATURES ----------
WANTROW = _ifany(' low C'),
mode = 'temperature',
group_name = 'low',
color_scheme = _ifset('temperature colour', 't'),
scale_factor = '1',
label = 'Keskimääräinen matala °C',
annual_mode = 'avg',
},
{---------- FIRST-SECOND LINE AVG MONTHLY MINIMUM TEMPERATURES ----------
WANTROW = _ifany(' avg record low C'),
mode = 'temperature',
group_name = 'avg record low',
color_scheme = _ifset('temperature colour', 't'),
scale_factor = '1',
label = 'Keskimääräinen alin °C',
annual_mode = 'min',
},
{---------- FIRST LINE RECORD LOW TEMPERATURES ----------
WANTROW = _ifany(' record low C'),
mode = 'temperature',
group_name = 'record low',
color_scheme = _ifset('temperature colour', 't'),
scale_factor = '1',
label = 'Alin lämpötila °C',
annual_mode = 'min',
},
{---------- FIRST LINE MINIMUM WIND CHILL ----------
WANTROW = _ifany(' chill'),
mode = 'basic',
group_name = 'chill',
color_scheme = _ifset('temperature colour', 't'),
scale_factor = '1',
label = 'Alhaisin [[pakkasen purevuus]]',
annual_mode = 'min',
},
{---------- FIRST LINE TOTAL PRECIPITATION ----------
WANTROW = _ifany(' precipitation cm') or _ifany(' precipitation mm'),
mode = 'precipitation',
group_name = 'precipitation',
color_scheme = _ifset('precipitation colour', 'p'),
date_mode = true,
scale_factor = '1',
prefer_cm = true,
label = makeLabel(args, options, true, 'Sademäärä', 'precipitation'),
annual_mode = 'sum',
},
{---------- FIRST LINE RAINFALL ----------
WANTROW = _ifany(' rain cm') or _ifany(' rain mm'),
mode = 'precipitation',
group_name = 'rain',
color_scheme = _ifset('rain colour', 'p'),
date_mode = true,
scale_factor = '1',
prefer_cm = true,
label = makeLabel(args, options, true, 'Sademäärä', 'rain'),
annual_mode = 'sum',
},
{---------- FIRST LINE SNOWFALL ----------
WANTROW = _ifany(' snow cm') or _ifany(' snow mm'),
mode = 'precipitation',
group_name = 'snow',
prefer_cm = true,
color_scheme = _ifset('snow colour', 'p'),
date_mode = true,
scale_factor = '1',
label = makeLabel(args, options, true, 'Lumisade keskimäärin', 'snow'),
annual_mode = 'sum',
},
{---------- FIRST LINE AVERAGE EXTREME SNOW DEPTH ----------
WANTROW = _ifany(' snow depth cm') or _ifany(' snow depth mm'),
mode = 'precipitation',
group_name = 'snow depth',
prefer_cm = true,
color_scheme = _ifset('snow colour', 'p'),
scale_factor = '0.2',
label = makeLabel(args, options, true, 'Lumen syvyys', 'snow depth'),
annual_mode = 'max',
},
{---------- PRECIPITATION DAYS ----------
WANTROW = _ifany(' precipitation days'),
mode = 'basic',
group_name = 'precipitation days',
color_scheme = _ifset('precip days colour', 'd'),
date_mode = true,
scale_factor = '1',
label = 'Sadepäiviä keskimäärin' .. _if('unit precipitation days', ' <span style="font-size:90%;" class="nowrap">(≥ ' .. _ifset('unit precipitation days', '') .. ')</span>'),
annual_mode = 'sum',
},
{---------- RAINY DAYS ----------
WANTROW = _ifany(' rain days'),
mode = 'basic',
group_name = 'rain days',
color_scheme = _ifset('precip days colour', 'd'),
date_mode = true,
scale_factor = '1',
label = 'Sateisia päiviä' .. _if('unit rain days', ' <span style="font-size:90%;" class="nowrap">(≥ ' .. _ifset('unit rain days', '') .. ')</span>'),
annual_mode = 'sum',
},
{---------- SNOWY DAYS ----------
WANTROW = _ifany(' snow days'),
mode = 'basic',
group_name = 'snow days',
color_scheme = _ifset('precip days colour', 'd'),
date_mode = true,
scale_factor = '1',
label = 'Lumisia päiviä' .. _if('unit snow days', ' <span style="font-size:90%;" class="nowrap">(≥ ' .. _ifset('unit snow days', '') .. ')</span>'),
annual_mode = 'sum',
},
{---------- PERCENT RELATIVE HUMIDITY ----------
WANTROW = _ifany(' humidity'),
mode = 'basic',
group_name = 'humidity',
color_scheme = _ifset('humidity colour', 'h'),
scale_factor = '1',
label = 'Suhteellinen kosteusprosentti (%)' ..
_if('time day', ' <span style="font-size:90%;" class="nowrap">(at ' .. _ifset('time day', '') .. ')</span>') ..
_if('daily', ' <span style="font-size:90%;" class="nowrap">(daily average)</span>'),
annual_mode = 'avg',
},
{---------- AFTERNOON PERCENT RELATIVE HUMIDITY ----------
WANTROW = _ifany(' afthumidity'),
mode = 'basic',
group_name = 'afthumidity',
color_scheme = _ifset('humidity colour', 'h'),
scale_factor = '1',
label = 'Iltapäivän suhteellinen kosteus (%)' ..
_if('time day', ' <span style="font-size:90%;" class="nowrap">(at ' .. _ifset('time day', '') .. ')</span>') ..
_if('daily', ' <span style="font-size:90%;" class="nowrap">(daily average)</span>'),
annual_mode = 'avg',
},
{---------- FIRST LINE AVERAGE DEW POINT ----------
WANTROW = _ifany(' dew point C'),
mode = 'temperature',
group_name = 'dew point',
color_scheme = _ifset('temperature colour', 't'),
scale_factor = '1',
label = 'Keskimääräinen [[kastepiste]] °C',
annual_mode = 'avg',
},
{---------- MONTHLY SUNSHINE HOURS ----------
WANTROW = _ifany(' sun'),
mode = 'basic',
group_name = 'sun',
color_scheme = _ifset('sun colour', 's'),
date_mode = true,
scale_factor = '1',
label = 'Kuukausittainen auringonpaiste (h)',
annual_mode = 'sum',
},
{---------- DAILY SUNSHINE HOURS ----------
WANTROW = _ifany('d sun'),
mode = 'basic',
group_name = 'd sun',
color_scheme = _ifset('sun colour', 's'),
include_space = false,
scale_factor = '30.44',
label = 'Päivittäinen auringonpaiste (h)',
annual_mode = 'avg',
},
{---------- DAILY DAYLIGHT HOURS ----------
WANTROW = _ifany(' light'),
mode = 'basic',
group_name = ' light',
color_scheme = _ifset('sun colour', 's'),
include_space = false,
scale_factor = '30.44',
label = 'Päivänvaloa (h)',
annual_mode = 'avg',
},
{---------- PERCENT SUNSHINE ----------
WANTROW = _ifany(' percentsun'),
mode = 'basic',
group_name = 'percentsun',
color_scheme = _ifset('sun colour', 's'),
scale_factor = '7.2',
label = 'Auringonpaiste prosentteina',
annual_mode = 'avg',
},
{---------- ULTRAVIOLET INDEX ----------
WANTROW = _ifany(' uv'),
mode = 'basic',
group_name = 'uv',
color_scheme = _ifset('uv colour', 'u'),
scale_factor = '1',
label = 'Keskimääräinen [[UV-indeksi]]',
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('Module:Weather box/row' .. sandbox)._buildRow
local args = frame:getParent().args
local options = {
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 '<div>\n'..table.concat(results)..'\n</div>' -- prevent Scribunto from inserting a blank line before the table
end
return {
main = main,
}