Aller au contenu

Module:Sandbox/Dirac

Une page de Wikipédia, l'encyclopédie libre.
Ceci est une version archivée de cette page, en date du 17 novembre 2017 à 20:41 et modifiée en dernier par Dirac (discuter | contributions) (string). Elle peut contenir des erreurs, des inexactitudes ou des contenus vandalisés non présents dans la version actuelle.

 Documentation[créer] [purger]
local p = {}
local getArgs = require('Module:Arguments').getArgs
local datePattern = "^(%d+)-(%d+)$"
local weatherTemplate = 'Modèle:Climat'
local wrapsTemplate = 'Utilisateur:Dirac/Bac météo'


function p.weatherbox( frame )
    local args = getArgs(frame, { wrappers = wrapsTemplate })
    local dataPage = assert(args.data, 'Missing "data" parameter')
    args.data = nil

    local monthlyHigh, monthlyLow = {}, {}
    local highSum, highCount = {}, {}
    local lowSum, lowCount = {}, {}
    local avgHighSum, avgHighCount = {}, {}
    local avgLowSum, avgLowCount = {}, {}
    local precipSum, precipCount = {}, {}
    local snowfallSum, snowfallCount = {}, {}
    local precipDaysSum, precipDaysCount = {}, {}
    local snowfallDaysSum, snowfallDaysCount = {}, {}

	-- There is currently no established way to specify sources, suggestions needed
	-- For now, prepend the link to edit box to the first source
	local source = args['source'] or args['source 1']
	if source then source = '<br>' .. source end
	args['source'] = '[[commons:Data:'.. dataPage ..'|edit data]]' .. source
	args['source 1'] = nil
	args['t-max-record-jan'] = 0
	args['diagramme']='oui'

    for key, row in pairs(mw.ext.data.get(dataPage).data) do
        local date, highTemp, avgHighTemp, avgLowTemp, lowTemp, precip, snowfall, precipDays, snowfallDays = unpack(row)

        local year, month = date:match(datePattern)
        month = tonumber(month)

        if highTemp ~= nil and (monthlyHigh[month] == nil or monthlyHigh[month] < highTemp) then monthlyHigh[month] = highTemp end
        if lowTemp ~= nil and (monthlyLow[month] == nil or monthlyLow[month] > lowTemp) then monthlyLow[month] = lowTemp end
        recordAvg(highSum, highCount, month, highTemp)
        recordAvg(lowSum, lowCount, month, lowTemp)
        recordAvg(avgHighSum, avgHighCount, month, avgHighTemp)
        recordAvg(avgLowSum, avgLowCount, month, avgLowTemp)
        recordAvg(precipSum, precipCount, month, precip)
        recordAvg(snowfallSum, snowfallCount, month, snowfall)
        recordAvg(precipDaysSum, precipDaysCount, month, precipDays)
        recordAvg(snowfallDaysSum, snowfallDaysCount, month, snowfallDays)
    end

    local months = {'jan','fev','mar','avr','mai','jun','jul','aou','sep','oct','nov','dec'}
--    for i=1, 12 do args['t-max-record-' .. months[i]] = monthlyHigh[i] end
--    for i=1, 12 do args['t-min-record-' .. months[i]] = monthlyLow[i] end


--    for i=1, 12 do args[months[i] .. ' avg record high C'] = string.format("%.1f", highSum[i] / highCount[i]) end
--    for i=1, 12 do args[months[i] .. ' avg record low C'] = string.format("%.1f", lowSum[i] / lowCount[i]) end
--    for i=1, 12 do args[months[i] .. ' high C'] = string.format("%.1f", avgHighSum[i] / avgHighCount[i]) end
--    for i=1, 12 do args[months[i] .. ' low C'] = string.format("%.1f", avgLowSum[i] / avgLowCount[i]) end
--    for i=1, 12 do args[months[i] .. ' precipitation mm'] = string.format("%.1f", precipSum[i] / precipCount[i]) end
--    for i=1, 12 do args[months[i] .. ' snow mm'] = string.format("%.1f", snowfallSum[i] / snowfallCount[i]) end
--    for i=1, 12 do args[months[i] .. ' precipitation days'] = string.format("%.1f", precipDaysSum[i] / precipDaysCount[i]) end
--    for i=1, 12 do args[months[i] .. ' snow days'] = string.format("%.1f", snowfallDaysSum[i] / snowfallDaysCount[i]) end

    return frame:expandTemplate{ title = weatherTemplate, args = args }
end

function recordAvg(sumTbl, countTbl, month, value)
    if value ~= nil then
        sumTbl[month] = (sumTbl[month] or 0) + value
        countTbl[month] = (countTbl[month] or 0) + 1
    end
end

return p