Jump to content

Module:Sandbox/Yurik: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
No edit summary
Line 13: Line 13:
local highSum, highCount = {}, {}
local highSum, highCount = {}, {}
local lowSum, lowCount = {}, {}
local lowSum, lowCount = {}, {}
local percipSum, percipCount = {}, {}
local avgHighSum, avgHighCount = {}, {}
local avgLowSum, avgLowCount = {}, {}
local precipSum, precipCount = {}, {}
local snowfallSum, snowfallCount = {}, {}
local precipDaysSum, precipDaysCount = {}, {}
local snowfallDaysSum, snowfallDaysCount = {}, {}


for key, row in pairs(mw.ext.data.get(dataPage).data) do
for key, row in pairs(mw.ext.data.get(dataPage).data) do
-- First column is in [0], not [1], needs special handling
-- First column is in [0], not [1], needs special handling
local date = row[0]
local date = row[0]
local percip, tempLow, tempAvg, tempHigh = unpack(row)
local highTemp, avgHighTemp, avgLowTemp, lowTemp, precip, snowfall, precipDays, snowfallDays = unpack(row)


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

if tempHigh > -999 and (monthlyHigh[month] == nil or monthlyHigh[month] < tempHigh) then monthlyHigh[month] = tempHigh end
if tempLow > -999 and (monthlyLow[month] == nil or monthlyLow[month] > tempLow) then monthlyLow[month] = tempLow end
if highTemp > -999 and (monthlyHigh[month] == nil or monthlyHigh[month] < highTemp) then monthlyHigh[month] = highTemp end
if lowTemp > -999 and (monthlyLow[month] == nil or monthlyLow[month] > lowTemp) then monthlyLow[month] = lowTemp end
recordAvg(percipSum, percipCount, month, percip)
recordAvg(highSum, highCount, month, tempHigh)
recordAvg(highSum, highCount, month, highTemp)
recordAvg(lowSum, lowCount, month, tempLow)
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
end


Line 34: Line 45:
for i=1, 12 do args[months[i] .. ' avg record high F'] = string.format("%.1f", highSum[i] / highCount[i]) end
for i=1, 12 do args[months[i] .. ' avg record high F'] = string.format("%.1f", highSum[i] / highCount[i]) end
for i=1, 12 do args[months[i] .. ' avg record low F'] = string.format("%.1f", lowSum[i] / lowCount[i]) end
for i=1, 12 do args[months[i] .. ' avg record low F'] = string.format("%.1f", lowSum[i] / lowCount[i]) end
for i=1, 12 do args[months[i] .. ' precipitation inch'] = string.format("%.1f", percipSum[i] / percipCount[i]) end
for i=1, 12 do args[months[i] .. ' high F'] = string.format("%.1f", avgHighSum[i] / avgHighCount[i]) end
for i=1, 12 do args[months[i] .. ' low F'] = string.format("%.1f", avgLowSum[i] / avgLowCount[i]) end
for i=1, 12 do args[months[i] .. ' precipitation inch'] = string.format("%.1f", precipSum[i] / precipCount[i]) end
for i=1, 12 do args[months[i] .. ' snow inch'] = 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 }
return frame:expandTemplate{ title = weatherTemplate, args = args }

Revision as of 22:44, 9 December 2016

local p = {}
local getArgs = require('Module:Arguments').getArgs
local datePattern = "^(%d+)-(%d+)-(%d+)$"
local weatherTemplate = 'Template:Weather box'
local wrapsTemplate = 'User:Yurik/Weather box'

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 = {}, {}

    for key, row in pairs(mw.ext.data.get(dataPage).data) do
        -- First column is in [0], not [1], needs special handling
        local date = row[0]
        local highTemp, avgHighTemp, avgLowTemp, lowTemp, precip, snowfall, precipDays, snowfallDays = unpack(row)

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

        if highTemp > -999 and (monthlyHigh[month] == nil or monthlyHigh[month] < highTemp) then monthlyHigh[month] = highTemp end
        if lowTemp > -999 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','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'}
    for i=1, 12 do args[months[i] .. ' record high F'] = monthlyHigh[i] end
    for i=1, 12 do args[months[i] .. ' record low F'] = monthlyLow[i] end
    for i=1, 12 do args[months[i] .. ' avg record high F'] = string.format("%.1f", highSum[i] / highCount[i]) end
    for i=1, 12 do args[months[i] .. ' avg record low F'] = string.format("%.1f", lowSum[i] / lowCount[i]) end
    for i=1, 12 do args[months[i] .. ' high F'] = string.format("%.1f", avgHighSum[i] / avgHighCount[i]) end
    for i=1, 12 do args[months[i] .. ' low F'] = string.format("%.1f", avgLowSum[i] / avgLowCount[i]) end
    for i=1, 12 do args[months[i] .. ' precipitation inch'] = string.format("%.1f", precipSum[i] / precipCount[i]) end
    for i=1, 12 do args[months[i] .. ' snow inch'] = 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 > -999 then
        sumTbl[month] = (sumTbl[month] or 0) + value
        countTbl[month] = (countTbl[month] or 0) + 1
    end
end

return p