跳转到内容
打开/关闭菜单
  • 451 条目
  • 808 文件
  • 22 用户
  • 9259 编辑
Blockland Wiki
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

模块:Weather box:修订间差异

来自Blockland Wiki
无编辑摘要
无编辑摘要
第1行: 第1行:
-- Implement [[Template:Weather box]].
local _precision = require('Module:Math')._precision
local _precision = require('Module:Math')._precision
local function precision(text)
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)
local success, result = pcall(_precision, text)
if success then
if success then
第14行: 第9行:


local function stripToNil(text)
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
if type(text) == 'string' then
return text:match('(%S.-)%s*$')
return text:match('(%S.-)%s*$')
第76行: 第69行:


local function wantSingle(parm)
local function wantSingle(parm)
--------------------------------------------------------------------------------
-- Result before July 2022 for single_line setting:
-- blank/omitted : separate table rows for metric and imperial (two rows)
-- N            : bug: only one row with either metric or imperial (not both)
--                      but heading like "cm (inches)"
-- Y/junk        : single row with both metric/imperial in same cell
--------------------------------------------------------------------------------
-- Intend changing in July 2022 so single_line=Y is the default and
-- need single_line=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()
parm = (parm or ''):lower()
return not (parm == 'no' or parm == 'n' or parm == '')
return not (parm == 'no' or parm == 'n' or parm == '')
end
end
-- =========================================================================
-- 图表与柯本气候分类生成逻辑 (新增部分)
-- =========================================================================
local function calculateKoppen(temps, precips)
    local t_mean_annual, p_total, t_hot, t_cold = 0, 0, -99, 99
    local s_p, w_p = 0, 0
    for i = 1, 12 do
        local t, p = tonumber(temps[i]) or 0, tonumber(precips[i]) or 0
        t_mean_annual = t_mean_annual + t
        p_total = p_total + p
        if t > t_hot then t_hot = t end
        if t < t_cold then t_cold = t end
        if i >= 4 and i <= 9 then s_p = s_p + p else w_p = w_p + p end
    end
    t_mean_annual = t_mean_annual / 12
    local pth = 20 * t_mean_annual + (s_p > p_total * 0.7 and 280 or (w_p > p_total * 0.7 and 0 or 140))
    if p_total < pth then
        local suffix = t_mean_annual >= 18 and "h" or "k"
        return p_total < pth / 2 and "BW" .. suffix or "BS" .. suffix
    end
    if t_cold >= 18 then return "A" end
    if t_hot >= 10 then return t_cold > -3 and "C" or "D" end
    return t_hot > 0 and "ET" or "EF"
end
local function makeGraph(frame, args)
    local months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
    local d = { high = {}, mean = {}, low = {}, rain = {} }
    local max_t, max_p = 50, 100
    for i, m in ipairs(months) do
        d.high[i] = tonumber(args[m .. ' high C']) or 0
        d.mean[i] = tonumber(args[m .. ' mean C']) or 0
        d.low[i] = tonumber(args[m .. ' low C']) or 0
        -- 兼容 Weather box 的两种降水参数名
        d.rain[i] = tonumber(args[m .. ' precipitation mm']) or tonumber(args[m .. ' rain mm']) or 0
       
        if d.high[i] > max_t then max_t = d.high[i] end
        if d.rain[i] > max_p then max_p = d.rain[i] end
    end
   
    max_t = math.ceil(max_t / 10) * 10
    max_p = math.ceil(max_p / 20) * 20
    local w, h = 300, 400
    local padding = { t = 40, b = 40, l = 40, r = 40 }
    local chart_w, chart_h = w - padding.l - padding.r, h - padding.t - padding.b
    local function ty(v) return h - padding.b - (v / max_t * chart_h) end
    local function py(v) return h - padding.b - (v / max_p * chart_h) end
    local function tx(i) return padding.l + (i - 0.5) * (chart_w / 12) end
    local svg = string.format('<svg width="100%%" viewBox="0 0 %d %d" style="background:#fff; font-family:sans-serif;">', w, h)
   
    -- 网格线
    for v = 0, max_t, 10 do
        local y = ty(v)
        svg = svg .. string.format('<line x1="%d" y1="%d" x2="%d" y2="%d" stroke="#eee" stroke-width="1" />', padding.l, y, w - padding.r, y)
        svg = svg .. string.format('<text x="%d" y="%d" font-size="10" fill="#999" text-anchor="end">%d°</text>', padding.l - 5, y + 4, v)
    end
    -- 降水柱
    for i, p_val in ipairs(d.rain) do
        local bh = (p_val / max_p * chart_h)
        svg = svg .. string.format('<rect x="%f" y="%f" width="%f" height="%f" fill="#cee" opacity="0.8" />', tx(i)-8, py(p_val), 16, bh)
    end
    -- 气温折线
    local function drawLine(data, color, width)
        local pts = ""
        for i, v in ipairs(data) do pts = pts .. string.format("%f,%f ", tx(i), ty(v)) end
        return string.format('<polyline points="%s" fill="none" stroke="%s" stroke-width="%d" />', pts, color, width)
    end
   
    svg = svg .. drawLine(d.high, "#f44", 2)
    svg = svg .. drawLine(d.mean, "#f90", 2)
    svg = svg .. drawLine(d.low, "#48f", 2)
    -- 月份标签
    for i, m in ipairs({"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"}) do
        svg = svg .. string.format('<text x="%f" y="%d" font-size="10" fill="#666" text-anchor="middle">%s</text>', tx(i), h - padding.b + 15, m)
    end
   
    svg = svg .. '</svg>'
    local raw_svg = frame:extensionTag('html', svg)
    local koppen = calculateKoppen(d.mean, d.rain)
    return string.format([[
<div style="float: right; margin: 0 0 1em 1em; width: 280px; border: 1px solid #ccc; padding: 10px; background: #fff; box-shadow: 2px 2px 5px rgba(0,0,0,0.1); border-radius:4px;">
    <div style="text-align:center; font-weight:bold; padding:5px; background:#f0f0f0; border-radius:2px;">%s (气候图)</div>
    <div style="margin:10px 0;">%s</div>
    <div style="font-size:0.9em; line-height:1.4em; text-align:center; color:#54595d;">
        <b>柯本气候分类:</b><br><span style="color:#2a4b8d; font-weight:bold;">%s</span>
    </div>
</div>]], stripToNil(args.location) or "未知", raw_svg, koppen)
end
-- =========================================================================


local function getDefinitions(frame, args, options)
local function getDefinitions(frame, args, options)
-- Return a list of tables or strings that define each row.
local function _if(parm, a, b)
local function _if(parm, a, b)
return stripToNil(args[parm]) and a or b or ''
return stripToNil(args[parm]) and a or b or ''
第105行: 第185行:
return options.wantSingleLine and a or b or ''
return options.wantSingleLine and a or b or ''
end
end
local location = _if('location', args.location, '{{{location}}}') -- show "{{{location}}}" to alert editor if parameter is blank
local location = _if('location', args.location, '{{{location}}}')  
local navbarText
local navbarText
if not stripToNil(args.open) then
if not stripToNil(args.open) then
第115行: 第195行:
return {
return {


----------- HEADER ----------
'{| class="wikitable ' .. _if('open', '', 'mw-collapsible' .. _if('collapsed', ' mw-collapsed')) ..
'{| class="wikitable ' .. _if('open', '', 'mw-collapsible' .. _if('collapsed', ' mw-collapsed')) ..
'" style="width:' .. _ifset('width', 'auto') ..
'" style="width:' .. _ifset('width', 'auto') ..
第144行: 第223行:
]=],
]=],


{---------- FIRST LINE MAXIMUM HUMIDEX ----------
{WANTROW = _ifany(' maximum humidex') and (_ifset('metric first') or options.wantSingleLine), mode = 'basic', group_name = 'maximum humidex', color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '历史最高[[湿热指数]]', annual_mode = 'max'},
WANTROW = _ifany(' maximum humidex') and (_ifset('metric first') or options.wantSingleLine),
{WANTROW = _ifany(' record high C') or _ifany(' record high F'), mode = 'temperature', group_name = 'record high', color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '历史最高温 °' .. _if('metric first', 'C', 'F') .. _ifSingle('(°' .. _if('metric first', 'F', 'C') .. ')'), annual_mode = 'max'},
mode = 'basic',
{WANTROW = _ifany(' avg record high C') or _ifany(' avg record high F'), mode = 'temperature', group_name = 'avg record high', color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '平均最高温 °' .. _if('metric first', 'C', 'F') .. _ifSingle('(°' .. _if('metric first', 'F', 'C') .. ')'), annual_mode = 'max'},
group_name = 'maximum humidex',
{WANTROW = _ifany(' high C') or _ifany(' high F'), mode = 'temperature', group_name = 'high', color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '平均高温 °' .. _if('metric first', 'C', 'F') .. _ifSingle('(°' .. _if('metric first', 'F', 'C') .. ')'), annual_mode = 'avg'},
color_scheme = _ifset('temperature colour', 't'),
{WANTROW = _ifany(' mean C') or _ifany(' mean F'), mode = 'temperature', group_name = 'mean', color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '日均气温 °' .. _if('metric first', 'C', 'F') .. _ifSingle('(°' .. _if('metric first', 'F', 'C') .. ')'), annual_mode = 'avg'},
scale_factor = '1',
{WANTROW = _ifany(' low C') or _ifany(' low F'), mode = 'temperature', group_name = 'low', color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '平均低温 °' .. _if('metric first', 'C', 'F') .. _ifSingle('(°' .. _if('metric first', 'F', 'C') .. ')'), annual_mode = 'avg'},
label = '历史最高[[湿热指数]]',
{WANTROW = _ifany(' avg record low C') or _ifany(' avg record low F'), mode = 'temperature', group_name = 'avg record low', color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '平均最低温 °' .. _if('metric first', 'C', 'F') .. _ifSingle('(°' .. _if('metric first', 'F', 'C') .. ')'), annual_mode = 'min'},
annual_mode = 'max',
{WANTROW = _ifany(' record low C') or _ifany(' record low F'), mode = 'temperature', group_name = 'record low', color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '历史最低温 °' .. _if('metric first', 'C', 'F') .. _ifSingle('(°' .. _if('metric first', 'F', 'C') .. ')'), annual_mode = 'min'},
},
{WANTROW = _ifany(' chill') and (_ifset('metric first') or options.wantSingleLine), mode = 'basic', group_name = 'chill', color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '历史最低[[風寒指數]]', annual_mode = 'min'},
{---------- FIRST LINE RECORD HIGH TEMPERATURES ----------
{WANTROW = _ifany(' precipitation cm') or _ifany(' precipitation mm') or _ifany(' precipitation inch'), mode = 'precipitation', group_name = 'precipitation', color_scheme = _ifset('precipitation colour', 'p'), date_mode = true, scale_factor = '1', prefer_cm = precision(_ifset('Jan precipitation inch', '0')) < 1, label = makeLabel(args, options, true, '平均[[降水量]]', 'precipitation'), annual_mode = 'sum'},
WANTROW = _ifany(' record high C') or _ifany(' record high F'),
{WANTROW = _ifany(' rain cm') or _ifany(' rain mm') or _ifany(' rain inch'), mode = 'precipitation', group_name = 'rain', color_scheme = _ifset('rain colour', 'p'), date_mode = true, scale_factor = '1', prefer_cm = precision(_ifset('Jan rain inch', '0')) < 1, label = makeLabel(args, options, true, '平均降雨量', 'rain'), annual_mode = 'sum'},
mode = 'temperature',
{WANTROW = _ifany(' snow cm') or _ifany(' snow mm') or _ifany(' snow inch'), 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, '平均降雪量', 'snow'), annual_mode = 'sum'},
group_name = 'record high',
{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 = _ifset('snow colour', 'p'), scale_factor = '0.2', label = makeLabel(args, options, true, '平均最大雪深', '雪深'), annual_mode = 'max'},
color_scheme = _ifset('temperature colour', 't'),
{WANTROW = not options.wantSingleLine and _ifany(' maximum humidex'), mode = 'basic', group_name = 'maximum humidex', color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '[[湿热指数]]', annual_mode = 'max', second_line = true},
scale_factor = '1',
{WANTROW = not options.wantSingleLine and (_ifany(' record high C') or _ifany(' record high F')), mode = 'temperature', group_name = 'record high', second_line = true, color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '历史最高温 °' .. _if('metric first', 'F', 'C'), annual_mode = 'max'},
label = '历史最高温 °' .. _if('metric first', 'C', 'F') .. _ifSingle('(°' .. _if('metric first', 'F', 'C') .. ')'),
{WANTROW = not options.wantSingleLine and (_ifany(' high C') or _ifany(' high F')), mode = 'temperature', group_name = 'high', second_line = true, color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '平均高温 °' .. _if('metric first', 'F', 'C'), annual_mode = 'avg'},
annual_mode = 'max',
{WANTROW = not options.wantSingleLine and (_ifany(' mean C') or _ifany(' mean F')), mode = 'temperature', group_name = 'mean', second_line = true, color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '日均气温 °' .. _if('metric first', 'F', 'C'), show = _if('metric first', '2', '1'), annual_mode = 'avg'},
},
{WANTROW = not options.wantSingleLine and (_ifany(' low C') or _ifany(' low F')), mode = 'temperature', group_name = 'low', second_line = true, color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '平均低温 °' .. _if('metric first', 'F', 'C'), show = _if('metric first', '2', '1'), annual_mode = 'avg'},
{---------- FIRST-SECOND LINE AVG MONTHLY MAXIMUM TEMPERATURES ----------
{WANTROW = not options.wantSingleLine and (_ifany(' record low C') or _ifany(' record low F')), mode = 'temperature', group_name = 'record low', second_line = true, color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '历史最低温 °' .. _if('metric first', 'F', 'C'), show = _if('metric first', '2', '1'), annual_mode = 'min'},
WANTROW = _ifany(' avg record high C') or _ifany(' avg record high F'),
{WANTROW = not options.wantSingleLine and (_ifany(' chill') and _if('metric first')), mode = 'basic', group_name = 'chill', color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '[[風寒指數]]', annual_mode = 'min'},
mode = 'temperature',
{WANTROW = not options.wantSingleLine and (_ifany(' precipitation cm') or _ifany(' precipitation mm') or _ifany(' precipitation inch')), mode = 'precipitation', group_name = 'precipitation', second_line = true, color_scheme = _ifset('precipitation colour', 'p'), date_mode = true, scale_factor = '1', prefer_cm = precision(_ifset('Jan precipitation inch', '0')) < 1, label = makeLabel(args, options, false, '平均[[降水量]]', 'precipitation'), annual_mode = 'sum'},
group_name = 'avg record high',
{WANTROW = not options.wantSingleLine and (_ifany(' rain cm') or _ifany(' rain mm') or _ifany(' rain inch')), mode = 'precipitation', group_name = 'rain', second_line = true, color_scheme = _ifset('rain colour', 'p'), date_mode = true, scale_factor = '1', prefer_cm = precision(_ifset('Jan rain inch', '0')) < 1, label = makeLabel(args, options, false, '平均降雨量', 'rain'), annual_mode = 'sum'},
color_scheme = _ifset('temperature colour', 't'),
{WANTROW = not options.wantSingleLine and (_ifany(' snow cm') or _ifany(' snow mm') or _ifany(' snow inch')), mode = 'precipitation', group_name = 'snow', second_line = true, prefer_cm = true, color_scheme = _ifset('snow colour', 'p'), date_mode = true, scale_factor = '1', label = makeLabel(args, options, false, '平均降雪量', 'snow'), annual_mode = 'sum'},
scale_factor = '1',
{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 = _ifset('snow colour', 'p'), scale_factor = '0.2', label = makeLabel(args, options, false, '平均最大雪深', '雪深'), annual_mode = 'max'},
label = '平均最高温 °' .. _if('metric first', 'C', 'F') .. _ifSingle('(°' .. _if('metric first', 'F', 'C') .. ')'),
{WANTROW = _ifany(' precipitation days'), mode = 'basic', group_name = 'precipitation days', color_scheme = _ifset('precip days colour', 'd'), date_mode = true, scale_factor = '1', label = '平均降水天数' .. _if('unit precipitation days', '<span style="font-size:90%;" class="nowrap">(≥ ' .. _ifset('unit precipitation days', '') .. ')</span>'), annual_mode = 'sum'},
annual_mode = 'max',
{WANTROW = _ifany(' rain days'), mode = 'basic', group_name = 'rain days', color_scheme = _ifset('precip days colour', 'd'), date_mode = true, scale_factor = '1', label = '平均降雨天数' .. _if('unit rain days', '<span style="font-size:90%;" class="nowrap">(≥ ' .. _ifset('unit rain days', '') .. ')</span>'), annual_mode = 'sum'},
},
{WANTROW = _ifany(' snow days'), mode = 'basic', group_name = 'snow days', color_scheme = _ifset('precip days colour', 'd'), date_mode = true, scale_factor = '1', label = '平均降雪天数' .. _if('unit snow days', '<span style="font-size:90%;" class="nowrap">(≥ ' .. _ifset('unit snow days', '') .. ')</span>'), annual_mode = 'sum'},
{---------- FIRST LINE AVERAGE HIGH TEMPERATURES ----------
{WANTROW = _ifany(' humidity'), mode = 'basic', group_name = 'humidity', color_scheme = _ifset('humidity colour', 'h'), scale_factor = '1', label = '平均[[相對濕度]](%)' .. _if('time day', ' <span style="font-size:90%;" class="nowrap">(' .. _ifset('time day', '') .. ')</span>') .. _if('daily', ' <span style="font-size:90%;" class="nowrap">(日均)</span>'), annual_mode = 'avg'},
WANTROW = _ifany(' high C') or _ifany(' high F'),
{WANTROW = _ifany(' afthumidity'), mode = 'basic', group_name = 'afthumidity', color_scheme = _ifset('humidity colour', 'h'), scale_factor = '1', label = '平均午後[[相對濕度]](%)' .. _if('time day', ' <span style="font-size:90%;" class="nowrap">(' .. _ifset('time day', '') .. ')</span>') .. _if('daily', ' <span style="font-size:90%;" class="nowrap">(日均)</span>'), annual_mode = 'avg'},
mode = 'temperature',
{WANTROW = _ifany(' dew point C') or _ifany(' dew point F'), mode = 'temperature', group_name = 'dew point', color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '平均[[露点]] °' .. _if('metric first', 'C', 'F') .. _ifSingle('(°' .. _if('metric first', 'F', 'C') .. ')'), annual_mode = 'avg'},
group_name = 'high',
{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 = _ifset('temperature colour', 't'), scale_factor = '1', label = '平均[[露点]] °' .. _if('metric first', 'F', 'C'), show = _if('metric first', '2', '1'), annual_mode = 'avg'},
color_scheme = _ifset('temperature colour', 't'),
{WANTROW = _ifany(' sun'), mode = 'basic', group_name = 'sun', color_scheme = _ifset('sun colour', 's'), date_mode = true, scale_factor = '1', label = '月均[[日照時數]]', annual_mode = 'sum'},
scale_factor = '1',
{WANTROW = _ifany('d sun'), mode = 'basic', group_name = 'd sun', color_scheme = _ifset('sun colour', 's'), include_space = false, scale_factor = '30.44', label = '日均[[日照時數]]', annual_mode = 'avg'},
label = '平均高温 °' .. _if('metric first', 'C', 'F') .. _ifSingle('(°' .. _if('metric first', 'F', 'C') .. ')'),
{WANTROW = _ifany(' light'), mode = 'basic', group_name = ' light', color_scheme = _ifset('sun colour', 's'), include_space = false, scale_factor = '30.44', label = '日均[[白天|昼长]]', annual_mode = 'avg'},
annual_mode = 'avg',
{WANTROW = _ifany(' percentsun'), mode = 'basic', group_name = 'percentsun', color_scheme = _ifset('sun colour', 's'), scale_factor = '7.2', label = '[[日照時數|可照]]百分比', annual_mode = 'avg'},
},
{WANTROW = _ifany(' uv'), mode = 'basic', group_name = 'uv', color_scheme = _ifset('uv colour', 'u'), scale_factor = '1', label = '平均[[紫外线指数]]', annual_mode = 'avg'},
{---------- FIRST LINE DAILY MEAN TEMPERATURES ----------
WANTROW = _ifany(' mean C') or _ifany(' mean F'),
mode = 'temperature',
group_name = 'mean',
color_scheme = _ifset('temperature colour', 't'),
scale_factor = '1',
label = '日均气温 °' .. _if('metric first', 'C', 'F') .. _ifSingle('(°' .. _if('metric first', 'F', 'C') .. ')'),
annual_mode = 'avg',
},
{---------- FIRST LINE AVERAGE LOW TEMPERATURES ----------
WANTROW = _ifany(' low C') or _ifany(' low F'),
mode = 'temperature',
group_name = 'low',
color_scheme = _ifset('temperature colour', 't'),
scale_factor = '1',
label = '平均低温 °' .. _if('metric first', 'C', 'F') .. _ifSingle('(°' .. _if('metric first', 'F', 'C') .. ')'),
annual_mode = 'avg',
},
{---------- FIRST-SECOND LINE AVG MONTHLY MINIMUM TEMPERATURES ----------
WANTROW = _ifany(' avg record low C') or _ifany(' avg record low F'),
mode = 'temperature',
group_name = 'avg record low',
color_scheme = _ifset('temperature colour', 't'),
scale_factor = '1',
label = '平均最低温 °' .. _if('metric first', 'C', 'F') .. _ifSingle('(°' .. _if('metric first', 'F', 'C') .. ')'),
annual_mode = 'min',
},
{---------- FIRST LINE RECORD LOW TEMPERATURES ----------
WANTROW = _ifany(' record low C') or _ifany(' record low F'),
mode = 'temperature',
group_name = 'record low',
color_scheme = _ifset('temperature colour', 't'),
scale_factor = '1',
label = '历史最低温 °' .. _if('metric first', 'C', 'F') .. _ifSingle('(°' .. _if('metric first', 'F', 'C') .. ')'),
annual_mode = 'min',
},
{---------- FIRST LINE MINIMUM WIND CHILL ----------
WANTROW = _ifany(' chill') and (_ifset('metric first') or options.wantSingleLine),
mode = 'basic',
group_name = 'chill',
color_scheme = _ifset('temperature colour', 't'),
scale_factor = '1',
label = '历史最低[[風寒指數]]',
annual_mode = 'min',
},
{---------- FIRST LINE TOTAL PRECIPITATION ----------
WANTROW = _ifany(' precipitation cm') or _ifany(' precipitation mm') or _ifany(' precipitation inch'),
mode = 'precipitation',
group_name = 'precipitation',
color_scheme = _ifset('precipitation colour', 'p'),
date_mode = true,
scale_factor = '1',
prefer_cm = precision(_ifset('Jan precipitation inch', '0')) < 1,
label = makeLabel(args, options, true, '平均[[降水量]]', 'precipitation'),
annual_mode = 'sum',
},
{---------- FIRST LINE RAINFALL ----------
WANTROW = _ifany(' rain cm') or _ifany(' rain mm') or _ifany(' rain inch'),
mode = 'precipitation',
group_name = 'rain',
color_scheme = _ifset('rain colour', 'p'),
date_mode = true,
scale_factor = '1',
prefer_cm = precision(_ifset('Jan rain inch', '0')) < 1,
label = makeLabel(args, options, true, '平均降雨量', 'rain'),
annual_mode = 'sum',
},
{---------- FIRST LINE SNOWFALL ----------
WANTROW = _ifany(' snow cm') or _ifany(' snow mm') or _ifany(' snow inch'),
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, '平均降雪量', '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 = _ifset('snow colour', 'p'),
scale_factor = '0.2',
label = makeLabel(args, options, true, '平均最大雪深', '雪深'),
annual_mode = 'max',
},
{---------- SECOND LINE MAXIMUM HUMIDEX ----------
WANTROW = not options.wantSingleLine and _ifany(' maximum humidex'),
mode = 'basic',
group_name = 'maximum humidex',
color_scheme = _ifset('temperature colour', 't'),
scale_factor = '1',
label = '[[湿热指数]]',
annual_mode = 'max',
second_line = true,
},
{---------- SECOND LINE RECORD HIGH TEMPERATURES ----------
WANTROW = not options.wantSingleLine and (_ifany(' record high C') or _ifany(' record high F')),
mode = 'temperature',
group_name = 'record high',
second_line = true,
color_scheme = _ifset('temperature colour', 't'),
scale_factor = '1',
label = '历史最高温 °' .. _if('metric first', 'F', 'C'),
annual_mode = 'max',
},
{---------- SECOND LINE AVERAGE HIGH TEMPERATURES ----------
WANTROW = not options.wantSingleLine and (_ifany(' high C') or _ifany(' high F')),
mode = 'temperature',
group_name = 'high',
second_line = true,
color_scheme = _ifset('temperature colour', 't'),
scale_factor = '1',
label = '平均高温 °' .. _if('metric first', 'F', 'C'),
annual_mode = 'avg',
},
{---------- SECOND LINE DAILY MEAN TEMPERATURES ----------
WANTROW = not options.wantSingleLine and (_ifany(' mean C') or _ifany(' mean F')),
mode = 'temperature',
group_name = 'mean',
second_line = true,
color_scheme = _ifset('temperature colour', 't'),
scale_factor = '1',
label = '日均气温 °' .. _if('metric first', 'F', 'C'),
show = _if('metric first', '2', '1'),
annual_mode = 'avg',
},
{---------- SECOND LINE AVERAGE LOW TEMPERATURES ----------
WANTROW = not options.wantSingleLine and (_ifany(' low C') or _ifany(' low F')),
mode = 'temperature',
group_name = 'low',
second_line = true,
color_scheme = _ifset('temperature colour', 't'),
scale_factor = '1',
label = '平均低温 °' .. _if('metric first', 'F', 'C'),
show = _if('metric first', '2', '1'),
annual_mode = 'avg',
},
{---------- SECOND LINE RECORD LOW TEMPERATURES ----------
WANTROW = not options.wantSingleLine and (_ifany(' record low C') or _ifany(' record low F')),
mode = 'temperature',
group_name = 'record low',
second_line = true,
color_scheme = _ifset('temperature colour', 't'),
scale_factor = '1',
label = '历史最低温 °' .. _if('metric first', 'F', 'C'),
show = _if('metric first', '2', '1'),
annual_mode = 'min',
},
{---------- SECOND LINE MINIMUM WIND CHILL ----------
WANTROW = not options.wantSingleLine and (_ifany(' chill') and _if('metric first')),
mode = 'basic',
group_name = 'chill',
color_scheme = _ifset('temperature colour', 't'),
scale_factor = '1',
label = '[[風寒指數]]',
annual_mode = 'min',
},
{---------- SECOND LINE TOTAL PRECIPITATION ----------
WANTROW = not options.wantSingleLine and (_ifany(' precipitation cm') or _ifany(' precipitation mm') or _ifany(' precipitation inch')),
mode = 'precipitation',
group_name = 'precipitation',
second_line = true,
color_scheme = _ifset('precipitation colour', 'p'),
date_mode = true,
scale_factor = '1',
prefer_cm = precision(_ifset('Jan precipitation inch', '0')) < 1,
label = makeLabel(args, options, false, '平均[[降水量]]', 'precipitation'),
annual_mode = 'sum',
},
{---------- SECOND LINE RAINFALL ----------
WANTROW = not options.wantSingleLine and (_ifany(' rain cm') or _ifany(' rain mm') or _ifany(' rain inch')),
mode = 'precipitation',
group_name = 'rain',
second_line = true,
color_scheme = _ifset('rain colour', 'p'),
date_mode = true,
scale_factor = '1',
prefer_cm = precision(_ifset('Jan rain inch', '0')) < 1,
label = makeLabel(args, options, false, '平均降雨量', 'rain'),
annual_mode = 'sum',
},
{---------- SECOND LINE SNOWFALL ----------
WANTROW = not options.wantSingleLine and (_ifany(' snow cm') or _ifany(' snow mm') or _ifany(' snow inch')),
mode = 'precipitation',
group_name = 'snow',
second_line = true,
prefer_cm = true,
color_scheme = _ifset('snow colour', 'p'),
date_mode = true,
scale_factor = '1',
label = makeLabel(args, options, false, '平均降雪量', '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 = _ifset('snow colour', 'p'),
scale_factor = '0.2',
label = makeLabel(args, options, false, '平均最大雪深', '雪深'),
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 = '平均降水天数' .. _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 = '平均降雨天数' .. _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 = '平均降雪天数' .. _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 = '平均[[相對濕度]](%)' ..
_if('time day', ' <span style="font-size:90%;" class="nowrap">(' .. _ifset('time day', '') .. ')</span>') ..
_if('daily', ' <span style="font-size:90%;" class="nowrap">(日均)</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 = '平均午後[[相對濕度]](%)' ..
_if('time day', ' <span style="font-size:90%;" class="nowrap">(' .. _ifset('time day', '') .. ')</span>') ..
_if('daily', ' <span style="font-size:90%;" class="nowrap">(日均)</span>'),
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 = _ifset('temperature colour', 't'),
scale_factor = '1',
label = '平均[[露点]] °' .. _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 = _ifset('temperature colour', 't'),
scale_factor = '1',
label = '平均[[露点]] °' .. _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 = _ifset('sun colour', 's'),
date_mode = true,
scale_factor = '1',
label = '月均[[日照時數]]',
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 = '日均[[日照時數]]',
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 = '日均[[白天|昼长]]',
annual_mode = 'avg',
},
{---------- PERCENT SUNSHINE ----------
WANTROW = _ifany(' percentsun'),
mode = 'basic',
group_name = 'percentsun',
color_scheme = _ifset('sun colour', 's'),
scale_factor = '7.2',
label = '[[日照時數|可照]]百分比',
annual_mode = 'avg',
},
{---------- ULTRAVIOLET INDEX ----------
WANTROW = _ifany(' uv'),
mode = 'basic',
group_name = 'uv',
color_scheme = _ifset('uv colour', 'u'),
scale_factor = '1',
label = '平均[[紫外线指数]]',
annual_mode = 'avg',
},
----------- SOURCES ----------
makeSources(frame, args),
makeSources(frame, args),
}
}
第520行: 第271行:
sandbox = sandbox,
sandbox = sandbox,
}
}
-- 检测并生成图表 (如果启用了开关)
local graphHtml = ''
if stripToNil(args['show graph']) == 'Y' or stripToNil(args['show graph']) == 'y' then
    graphHtml = makeGraph(frame, args)
end
local results = {}
local results = {}
for i, definition in ipairs(getDefinitions(frame, args, options)) do
for i, definition in ipairs(getDefinitions(frame, args, options)) do
第532行: 第290行:
results[i] = row
results[i] = row
end
end
return '<div>\n'..table.concat(results)..'\n</div>' -- prevent Scribunto from inserting a blank line before the table
-- 将图表(如果存在)拼接到表格外围包装器中
return '<div class="weather-box-container" style="display: flow-root;">\n' .. graphHtml .. table.concat(results) .. '\n</div>'
end
end



2026年5月12日 (二) 16:43的版本

此模块的文档可以在模块:Weather box/doc创建

local _precision = require('Module:Math')._precision
local function precision(text)
	local success, result = pcall(_precision, text)
	if success then
		return result
	end
	return 0
end

local function stripToNil(text)
	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'
	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
	local second = 'Avs'
	if not stripToNil(args['metric first']) then
		first, second = second, first
	end
	if is_first then
		if options.wantSingleLine then
			first = first .. '(' .. second .. ')'
		end
	else
		first = second
	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 = '来源1:' .. source1 .. '\n' .. result .. '来源2:' .. source2
		else
			text = '来源:' .. (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)
	parm = (parm or ''):lower()
	return not (parm == 'no' or parm == 'n' or parm == '')
end

-- =========================================================================
-- 图表与柯本气候分类生成逻辑 (新增部分)
-- =========================================================================
local function calculateKoppen(temps, precips)
    local t_mean_annual, p_total, t_hot, t_cold = 0, 0, -99, 99
    local s_p, w_p = 0, 0
    for i = 1, 12 do
        local t, p = tonumber(temps[i]) or 0, tonumber(precips[i]) or 0
        t_mean_annual = t_mean_annual + t
        p_total = p_total + p
        if t > t_hot then t_hot = t end
        if t < t_cold then t_cold = t end
        if i >= 4 and i <= 9 then s_p = s_p + p else w_p = w_p + p end
    end
    t_mean_annual = t_mean_annual / 12
    local pth = 20 * t_mean_annual + (s_p > p_total * 0.7 and 280 or (w_p > p_total * 0.7 and 0 or 140))
    if p_total < pth then
        local suffix = t_mean_annual >= 18 and "h" or "k"
        return p_total < pth / 2 and "BW" .. suffix or "BS" .. suffix
    end
    if t_cold >= 18 then return "A" end
    if t_hot >= 10 then return t_cold > -3 and "C" or "D" end
    return t_hot > 0 and "ET" or "EF"
end

local function makeGraph(frame, args)
    local months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
    local d = { high = {}, mean = {}, low = {}, rain = {} }
    local max_t, max_p = 50, 100 

    for i, m in ipairs(months) do
        d.high[i] = tonumber(args[m .. ' high C']) or 0
        d.mean[i] = tonumber(args[m .. ' mean C']) or 0
        d.low[i] = tonumber(args[m .. ' low C']) or 0
        -- 兼容 Weather box 的两种降水参数名
        d.rain[i] = tonumber(args[m .. ' precipitation mm']) or tonumber(args[m .. ' rain mm']) or 0
        
        if d.high[i] > max_t then max_t = d.high[i] end
        if d.rain[i] > max_p then max_p = d.rain[i] end
    end
    
    max_t = math.ceil(max_t / 10) * 10
    max_p = math.ceil(max_p / 20) * 20

    local w, h = 300, 400
    local padding = { t = 40, b = 40, l = 40, r = 40 }
    local chart_w, chart_h = w - padding.l - padding.r, h - padding.t - padding.b

    local function ty(v) return h - padding.b - (v / max_t * chart_h) end
    local function py(v) return h - padding.b - (v / max_p * chart_h) end
    local function tx(i) return padding.l + (i - 0.5) * (chart_w / 12) end

    local svg = string.format('<svg width="100%%" viewBox="0 0 %d %d" style="background:#fff; font-family:sans-serif;">', w, h)
    
    -- 网格线
    for v = 0, max_t, 10 do
        local y = ty(v)
        svg = svg .. string.format('<line x1="%d" y1="%d" x2="%d" y2="%d" stroke="#eee" stroke-width="1" />', padding.l, y, w - padding.r, y)
        svg = svg .. string.format('<text x="%d" y="%d" font-size="10" fill="#999" text-anchor="end">%d°</text>', padding.l - 5, y + 4, v)
    end

    -- 降水柱
    for i, p_val in ipairs(d.rain) do
        local bh = (p_val / max_p * chart_h)
        svg = svg .. string.format('<rect x="%f" y="%f" width="%f" height="%f" fill="#cee" opacity="0.8" />', tx(i)-8, py(p_val), 16, bh)
    end

    -- 气温折线
    local function drawLine(data, color, width)
        local pts = ""
        for i, v in ipairs(data) do pts = pts .. string.format("%f,%f ", tx(i), ty(v)) end
        return string.format('<polyline points="%s" fill="none" stroke="%s" stroke-width="%d" />', pts, color, width)
    end
    
    svg = svg .. drawLine(d.high, "#f44", 2)
    svg = svg .. drawLine(d.mean, "#f90", 2)
    svg = svg .. drawLine(d.low, "#48f", 2)

    -- 月份标签
    for i, m in ipairs({"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"}) do
        svg = svg .. string.format('<text x="%f" y="%d" font-size="10" fill="#666" text-anchor="middle">%s</text>', tx(i), h - padding.b + 15, m)
    end
    
    svg = svg .. '</svg>'

    local raw_svg = frame:extensionTag('html', svg)
    local koppen = calculateKoppen(d.mean, d.rain)

    return string.format([[
<div style="float: right; margin: 0 0 1em 1em; width: 280px; border: 1px solid #ccc; padding: 10px; background: #fff; box-shadow: 2px 2px 5px rgba(0,0,0,0.1); border-radius:4px;">
    <div style="text-align:center; font-weight:bold; padding:5px; background:#f0f0f0; border-radius:2px;">%s (气候图)</div>
    <div style="margin:10px 0;">%s</div>
    <div style="font-size:0.9em; line-height:1.4em; text-align:center; color:#54595d;">
        <b>柯本气候分类:</b><br><span style="color:#2a4b8d; font-weight:bold;">%s</span>
    </div>
</div>]], stripToNil(args.location) or "未知", raw_svg, koppen)
end
-- =========================================================================

local function getDefinitions(frame, args, options)
	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 function _ifSingle(a, b)
		return options.wantSingleLine and a or b or ''
	end
	local location = _if('location', args.location, '{{{location}}}') 
	local navbarText
	if not stripToNil(args.open) then
		if stripToNil(args.name) then
			local navbar = require('Module:Navbar')._navbar
			navbarText = navbar({' ' .. location, args.name, collapsible=1})
		end
	end
	return {

'{| 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|+ ' .. location,
	'\n|-' ..
	'\n!colspan="14" | ' .. _if('name', navbarText, ' ' .. location)
) ..
[=[

|-
!scope="row" |月份
!scope="col" |1月
!scope="col" |2月
!scope="col" |3月
!scope="col" |4月
!scope="col" |5月
!scope="col" |6月
!scope="col" |7月
!scope="col" |8月
!scope="col" |9月
!scope="col" |10月
!scope="col" |11月
!scope="col" |12月
!scope="col" style="border-left-width:medium" |全年
]=],

{WANTROW = _ifany(' maximum humidex') and (_ifset('metric first') or options.wantSingleLine), mode = 'basic', group_name = 'maximum humidex', color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '历史最高[[湿热指数]]', annual_mode = 'max'},
{WANTROW = _ifany(' record high C') or _ifany(' record high F'), mode = 'temperature', group_name = 'record high', color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '历史最高温 °' .. _if('metric first', 'C', 'F') .. _ifSingle('(°' .. _if('metric first', 'F', 'C') .. ')'), annual_mode = 'max'},
{WANTROW = _ifany(' avg record high C') or _ifany(' avg record high F'), mode = 'temperature', group_name = 'avg record high', color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '平均最高温 °' .. _if('metric first', 'C', 'F') .. _ifSingle('(°' .. _if('metric first', 'F', 'C') .. ')'), annual_mode = 'max'},
{WANTROW = _ifany(' high C') or _ifany(' high F'), mode = 'temperature', group_name = 'high', color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '平均高温 °' .. _if('metric first', 'C', 'F') .. _ifSingle('(°' .. _if('metric first', 'F', 'C') .. ')'), annual_mode = 'avg'},
{WANTROW = _ifany(' mean C') or _ifany(' mean F'), mode = 'temperature', group_name = 'mean', color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '日均气温 °' .. _if('metric first', 'C', 'F') .. _ifSingle('(°' .. _if('metric first', 'F', 'C') .. ')'), annual_mode = 'avg'},
{WANTROW = _ifany(' low C') or _ifany(' low F'), mode = 'temperature', group_name = 'low', color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '平均低温 °' .. _if('metric first', 'C', 'F') .. _ifSingle('(°' .. _if('metric first', 'F', 'C') .. ')'), annual_mode = 'avg'},
{WANTROW = _ifany(' avg record low C') or _ifany(' avg record low F'), mode = 'temperature', group_name = 'avg record low', color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '平均最低温 °' .. _if('metric first', 'C', 'F') .. _ifSingle('(°' .. _if('metric first', 'F', 'C') .. ')'), annual_mode = 'min'},
{WANTROW = _ifany(' record low C') or _ifany(' record low F'), mode = 'temperature', group_name = 'record low', color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '历史最低温 °' .. _if('metric first', 'C', 'F') .. _ifSingle('(°' .. _if('metric first', 'F', 'C') .. ')'), annual_mode = 'min'},
{WANTROW = _ifany(' chill') and (_ifset('metric first') or options.wantSingleLine), mode = 'basic', group_name = 'chill', color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '历史最低[[風寒指數]]', annual_mode = 'min'},
{WANTROW = _ifany(' precipitation cm') or _ifany(' precipitation mm') or _ifany(' precipitation inch'), mode = 'precipitation', group_name = 'precipitation', color_scheme = _ifset('precipitation colour', 'p'), date_mode = true, scale_factor = '1', prefer_cm = precision(_ifset('Jan precipitation inch', '0')) < 1, label = makeLabel(args, options, true, '平均[[降水量]]', 'precipitation'), annual_mode = 'sum'},
{WANTROW = _ifany(' rain cm') or _ifany(' rain mm') or _ifany(' rain inch'), mode = 'precipitation', group_name = 'rain', color_scheme = _ifset('rain colour', 'p'), date_mode = true, scale_factor = '1', prefer_cm = precision(_ifset('Jan rain inch', '0')) < 1, label = makeLabel(args, options, true, '平均降雨量', 'rain'), annual_mode = 'sum'},
{WANTROW = _ifany(' snow cm') or _ifany(' snow mm') or _ifany(' snow inch'), 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, '平均降雪量', 'snow'), annual_mode = 'sum'},
{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 = _ifset('snow colour', 'p'), scale_factor = '0.2', label = makeLabel(args, options, true, '平均最大雪深', '雪深'), annual_mode = 'max'},
{WANTROW = not options.wantSingleLine and _ifany(' maximum humidex'), mode = 'basic', group_name = 'maximum humidex', color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '[[湿热指数]]', annual_mode = 'max', second_line = true},
{WANTROW = not options.wantSingleLine and (_ifany(' record high C') or _ifany(' record high F')), mode = 'temperature', group_name = 'record high', second_line = true, color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '历史最高温 °' .. _if('metric first', 'F', 'C'), annual_mode = 'max'},
{WANTROW = not options.wantSingleLine and (_ifany(' high C') or _ifany(' high F')), mode = 'temperature', group_name = 'high', second_line = true, color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '平均高温 °' .. _if('metric first', 'F', 'C'), annual_mode = 'avg'},
{WANTROW = not options.wantSingleLine and (_ifany(' mean C') or _ifany(' mean F')), mode = 'temperature', group_name = 'mean', second_line = true, color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '日均气温 °' .. _if('metric first', 'F', 'C'), show = _if('metric first', '2', '1'), annual_mode = 'avg'},
{WANTROW = not options.wantSingleLine and (_ifany(' low C') or _ifany(' low F')), mode = 'temperature', group_name = 'low', second_line = true, color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '平均低温 °' .. _if('metric first', 'F', 'C'), show = _if('metric first', '2', '1'), annual_mode = 'avg'},
{WANTROW = not options.wantSingleLine and (_ifany(' record low C') or _ifany(' record low F')), mode = 'temperature', group_name = 'record low', second_line = true, color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '历史最低温 °' .. _if('metric first', 'F', 'C'), show = _if('metric first', '2', '1'), annual_mode = 'min'},
{WANTROW = not options.wantSingleLine and (_ifany(' chill') and _if('metric first')), mode = 'basic', group_name = 'chill', color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '[[風寒指數]]', annual_mode = 'min'},
{WANTROW = not options.wantSingleLine and (_ifany(' precipitation cm') or _ifany(' precipitation mm') or _ifany(' precipitation inch')), mode = 'precipitation', group_name = 'precipitation', second_line = true, color_scheme = _ifset('precipitation colour', 'p'), date_mode = true, scale_factor = '1', prefer_cm = precision(_ifset('Jan precipitation inch', '0')) < 1, label = makeLabel(args, options, false, '平均[[降水量]]', 'precipitation'), annual_mode = 'sum'},
{WANTROW = not options.wantSingleLine and (_ifany(' rain cm') or _ifany(' rain mm') or _ifany(' rain inch')), mode = 'precipitation', group_name = 'rain', second_line = true, color_scheme = _ifset('rain colour', 'p'), date_mode = true, scale_factor = '1', prefer_cm = precision(_ifset('Jan rain inch', '0')) < 1, label = makeLabel(args, options, false, '平均降雨量', 'rain'), annual_mode = 'sum'},
{WANTROW = not options.wantSingleLine and (_ifany(' snow cm') or _ifany(' snow mm') or _ifany(' snow inch')), mode = 'precipitation', group_name = 'snow', second_line = true, prefer_cm = true, color_scheme = _ifset('snow colour', 'p'), date_mode = true, scale_factor = '1', label = makeLabel(args, options, false, '平均降雪量', 'snow'), annual_mode = 'sum'},
{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 = _ifset('snow colour', 'p'), scale_factor = '0.2', label = makeLabel(args, options, false, '平均最大雪深', '雪深'), annual_mode = 'max'},
{WANTROW = _ifany(' precipitation days'), mode = 'basic', group_name = 'precipitation days', color_scheme = _ifset('precip days colour', 'd'), date_mode = true, scale_factor = '1', label = '平均降水天数' .. _if('unit precipitation days', '<span style="font-size:90%;" class="nowrap">(≥ ' .. _ifset('unit precipitation days', '') .. ')</span>'), annual_mode = 'sum'},
{WANTROW = _ifany(' rain days'), mode = 'basic', group_name = 'rain days', color_scheme = _ifset('precip days colour', 'd'), date_mode = true, scale_factor = '1', label = '平均降雨天数' .. _if('unit rain days', '<span style="font-size:90%;" class="nowrap">(≥ ' .. _ifset('unit rain days', '') .. ')</span>'), annual_mode = 'sum'},
{WANTROW = _ifany(' snow days'), mode = 'basic', group_name = 'snow days', color_scheme = _ifset('precip days colour', 'd'), date_mode = true, scale_factor = '1', label = '平均降雪天数' .. _if('unit snow days', '<span style="font-size:90%;" class="nowrap">(≥ ' .. _ifset('unit snow days', '') .. ')</span>'), annual_mode = 'sum'},
{WANTROW = _ifany(' humidity'), mode = 'basic', group_name = 'humidity', color_scheme = _ifset('humidity colour', 'h'), scale_factor = '1', label = '平均[[相對濕度]](%)' .. _if('time day', ' <span style="font-size:90%;" class="nowrap">(' .. _ifset('time day', '') .. ')</span>') .. _if('daily', ' <span style="font-size:90%;" class="nowrap">(日均)</span>'), annual_mode = 'avg'},
{WANTROW = _ifany(' afthumidity'), mode = 'basic', group_name = 'afthumidity', color_scheme = _ifset('humidity colour', 'h'), scale_factor = '1', label = '平均午後[[相對濕度]](%)' .. _if('time day', ' <span style="font-size:90%;" class="nowrap">(' .. _ifset('time day', '') .. ')</span>') .. _if('daily', ' <span style="font-size:90%;" class="nowrap">(日均)</span>'), annual_mode = 'avg'},
{WANTROW = _ifany(' dew point C') or _ifany(' dew point F'), mode = 'temperature', group_name = 'dew point', color_scheme = _ifset('temperature colour', 't'), scale_factor = '1', label = '平均[[露点]] °' .. _if('metric first', 'C', 'F') .. _ifSingle('(°' .. _if('metric first', 'F', 'C') .. ')'), annual_mode = 'avg'},
{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 = _ifset('temperature colour', 't'), scale_factor = '1', label = '平均[[露点]] °' .. _if('metric first', 'F', 'C'), show = _if('metric first', '2', '1'), annual_mode = 'avg'},
{WANTROW = _ifany(' sun'), mode = 'basic', group_name = 'sun', color_scheme = _ifset('sun colour', 's'), date_mode = true, scale_factor = '1', label = '月均[[日照時數]]', annual_mode = 'sum'},
{WANTROW = _ifany('d sun'), mode = 'basic', group_name = 'd sun', color_scheme = _ifset('sun colour', 's'), include_space = false, scale_factor = '30.44', label = '日均[[日照時數]]', annual_mode = 'avg'},
{WANTROW = _ifany(' light'), mode = 'basic', group_name = ' light', color_scheme = _ifset('sun colour', 's'), include_space = false, scale_factor = '30.44', label = '日均[[白天|昼长]]', annual_mode = 'avg'},
{WANTROW = _ifany(' percentsun'), mode = 'basic', group_name = 'percentsun', color_scheme = _ifset('sun colour', 's'), scale_factor = '7.2', label = '[[日照時數|可照]]百分比', annual_mode = 'avg'},
{WANTROW = _ifany(' uv'), mode = 'basic', group_name = 'uv', color_scheme = _ifset('uv colour', 'u'), scale_factor = '1', label = '平均[[紫外线指数]]', annual_mode = 'avg'},
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 = {
		wantSingleLine = wantSingle(args['single line']),
		sandbox = sandbox,
	}
	
	-- 检测并生成图表 (如果启用了开关)
	local graphHtml = ''
	if stripToNil(args['show graph']) == 'Y' or stripToNil(args['show graph']) == 'y' then
	    graphHtml = makeGraph(frame, args)
	end
	
	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 class="weather-box-container" style="display: flow-root;">\n' .. graphHtml .. table.concat(results) .. '\n</div>'
end

return {
	main = main,
}