Aller au contenu

Module:Sandbox/Dirac

Une page de Wikipédia, l'encyclopédie libre.

 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, monthlyAvgHigh, monthlyLow, monthlyAvgLow = {}, {}, {}, {}
    local highSum, highCount = {}, {}
    local lowSum, lowCount = {}, {}
    local avgHighSum, avgHighCount = {}, {}
    local avgLowSum, avgLowCount = {}, {}
    local avgSum, avgCount = {}, {}
    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['diagramme']='oui'

    for key, row in pairs(mw.ext.data.get(dataPage).data) do
        local date, avgHighTemp, avgLowTemp, avgTemp, extHighTemp, extLowTemp, precipTotalRain, precipTotalSnow, precipTotal, snowGrndLastDay, windDirMaxGust, windSpeedMaxGust = unpack(row)

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

        if avgHighTemp ~= nil and (monthlyAvgHigh[month] == nil or monthlyAvgHigh[month] < avgHighTemp) then monthlyAvgHigh[month] = avgHighTemp end
        if avgLowTemp ~= nil and (monthlyAvgLow[month] == nil or monthlyAvgLow[month] > avgLowTemp) then monthlyAvgLow[month] = avgLowTemp end

        if avgHighTemp ~= nil and (monthlyHigh[month] == nil or monthlyHigh[month] < avgHighTemp) then monthlyHigh[month] = avgHighTemp end
        if avgLowTemp ~= nil and (monthlyLow[month] == nil or monthlyLow[month] > avgLowTemp) then monthlyLow[month] = avgLowTemp end


        recordAvg(highSum, highCount, month, avgHighTemp)
        recordAvg(lowSum, lowCount, month, avgLowTemp)
        recordAvg(avgSum, avgCount, month, avgTemp)
        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','jui','jul','aou','sep','oct','nov','dec'}
    -- Record temperatures moyennes basses/hautes
    for i=1, 12 do args['tmax-haut-' .. months[i]] = monthlyAvgHigh[i] end
    for i=1, 12 do args['tmin-bas-' .. months[i]] = monthlyAvgLow[i] end
    -- Record: Average daily temperatures
    for i=1, 12 do args['tmoy-' .. months[i]] = string.format("%.1f", avgSum[i] / avgCount[i]) end
--    for i=1, 12 do args['tmin-bas-' .. months[i]] = string.format("%.1f", lowSum[i] / lowCount[i]) end
    --  Average high/low temperatures 
--    for i=1, 12 do args['tmax-' .. months[i]] = string.format("%.1f", avgHighSum[i] / avgHighCount[i]) end
--    for i=1, 12 do args['tmin-' .. months[i]] = string.format("%.1f", avgLowSum[i] / avgLowCount[i]) end

    -- Precip
--    for i=1, 12 do args['prec-' .. months[i]] = string.format("%.1f", precipSum[i] / precipCount[i]) end
--    for i=1, 12 do args['neige-' .. months[i]] = string.format("%.1f", snowfallSum[i] / snowfallCount[i]) end
--   for i=1, 12 do args['pluie-jour-' .. months[i]] = string.format("%.1f", precipDaysSum[i] / precipDaysCount[i]) end
--   for i=1, 12 do args['neige-jour-' .. months[i]] = 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