Module:Sandbox/Anon423/ClimateCommons

local p = {}

function p.getMonthHighestTemp(month, files) local max

month = string.format("%02d", month)

for file,data in pairs(files) do   field_index = p.getColumnIndex("extremeMax", data) for index,values in pairs(data.data) do     if string.find(string.sub(values[1], 1, 3), month, 1, true) then if max == nil or values[field_index] > max then max = values[field_index] end end end end

return max end function p.getMonthLowestTemp(month, files) local min

month = string.format("%02d", month)

for file,data in pairs(files) do   field_index = p.getColumnIndex("extremeMin", data) for index,values in pairs(data.data) do     if string.find(string.sub(values[1], 1, 3), month, 1, true) then if min == nil or values[field_index] < min then min = values[field_index] end end end end

return min end

function p.getMonthHighestRainfall(month, files) local max

month = string.format("%02d", month)

for file,data in pairs(files) do   field_index = p.getColumnIndex("extremeRainfall", data) for index,values in pairs(data.data) do     if string.find(string.sub(values[1], 1, 3), month, 1, true) then if max == nil or values[field_index] > max then max = values[field_index] end end end end

return max end

function p.getMonthHighestSnowfall(month, files) local max

month = string.format("%02d", month)

for file,data in pairs(files) do   field_index = p.getColumnIndex("extremeSnowfall", data) for index,values in pairs(data.data) do     if string.find(string.sub(values[1], 1, 3), month, 1, true) then if max == nil or values[field_index] > max then max = values[field_index] end end end end

return max end

function p.getColumnIndex(name, file) for index,value in pairs(file["schema"]["fields"]) do   if value["name"] == name then return index end end

return nil end

function p.main(frame) debug = "" local args = frame:getParent.args local template_args = {} local donnees = args["données"] local files = {} local months = {"jan", "fev", "mar", "avr", "mai", "jui", "jul", "aou", "sep", "oct", "nov", "dec"}

-- Duplicate in args for key,value in pairs(args) do   template_args[key] = args[key] end

-- Open data files for file in string.gmatch(donnees, "[^%s]+") do   files[file] = mw.ext.data.get(file) end

-- Temperature extreme high for num,month in pairs(months) do   if template_args["tmax-record-" .. month] == nil or template_args["tmax-record-" .. month] == "" then template_args["tmax-record-" .. month] = p.getMonthHighestTemp(num, files) template_args["tmax-record-date-" .. month] = nil elseif template_args["tmax-record-" .. month] == "-" then template_args["tmax-record-" .. month] = nil end end

-- Temperature extreme low for num,month in pairs(months) do   if template_args["tmin-record-" .. month] == nil or template_args["tmin-record-" .. month] == "" then template_args["tmin-record-" .. month] = p.getMonthLowestTemp(num, files) template_args["tmin-record-date-" .. month] = nil elseif template_args["tmin-record-" .. month] == "-" then template_args["tmin-record-" .. month] = nil end end

-- Rainfall max for num,month in pairs(months) do   if template_args["pluie-record-" .. month] == nil or template_args["pluie-record-" .. month] == "" then template_args["pluie-record-" .. month] = p.getMonthHighestRainfall(num, files) template_args["pluie-record-date-" .. month] = nil elseif template_args["pluie-record-" .. month] == "-" then template_args["pluie-record-" .. month] = nil end end

-- Snowfall max for num,month in pairs(months) do   if template_args["neige-record-" .. month] == nil or template_args["neige-record-" .. month] == "" then template_args["neige-record-" .. month] = p.getMonthHighestSnowfall(num, files) template_args["neige-record-date-" .. month] = nil elseif template_args["neige-record-" .. month] == "-" then template_args["neige-record-" .. month] = nil end end

-- Force debug, if any if debug ~= "" then return debug else return frame:expandTemplate{title = 'Weather box', args = template_args} end end

return p