Jump to content

Module:Tropical cyclone season effects

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Chlod (talk | contribs) at 17:14, 19 February 2021 (Change default pressure conversion target to "inHg"). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- Used for tropical cyclone season articles.

local invocation = require('Module:Template invocation').invocation
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local dateperiod = require('Module:Date period')._main
local Date = require('Module:Date')._Date
local p = {}

function p.main(frame)
	local args = getArgs(frame, {
		trim = true,
		removeBlanks = false
	})

    return p._main(frame, args)
end

function p._main(frame, args)
	if not yesno(args["no-header"]) and (args["Basin"] or args["basin"]) == nil then
		error("Basin not specified")
	elseif not yesno(args["no-header"]) and (args["Season start year"] or args["year"]) == nil then
		error("Year (or start year) not specified")
	elseif not yesno(args["no-header"]) and args["Season start year"] == nil and args["Season end year"] ~= nil then
		error("End year specified but start year not specified")
	end
	
    local tableEntries = args[1]
    
    local totalStorms = 0
    local strongestWinds = 0
    local tableWindsUnit = nil
    local lowestPressure = 0
    local tablePressureUnit = nil
    local totalDamages = 0
    local totalDeaths = 0
    
    local earliestFormed = nil;
    local earliestFormedDate = nil;
    local latestDissipated = nil;
    local latestDissipatedDate = nil;

    for name, formed, dissipated, winds, windsUnit, pressure, pressureUnit, damages, deaths in string.gmatch(
    	tableEntries,
    	'data%-tcse%-entry="([^|]+)|([%w%-]+)|([%w%-]+)|([%d%.]+)|(%w+)|([%d%.]+)|(%w+)|([%d%.]+)|(%d+)"'
	) do
	    totalStorms = totalStorms + 1
	    
    	if latestDissipated ~= "present" then
    		if dissipated == "present" then
	    		latestDissipated = "present"
	    	elseif latestDissipated == nil or latestDissipatedDate == nil then
    			latestDissipated = dissipated
    			latestDissipatedDate= Date(dissipated)
	    	elseif latestDissipatedDate.gsd < Date(dissipated).gsd then
			    latestDissipated = dissipated
			    latestDissipatedDate = Date(dissipated)
    		end
    	end
	    
		if earliestFormed == nil then
			earliestFormed = formed
			earliestFormedDate = Date(formed)
    	elseif earliestFormedDate.gsd > Date(formed).gsd then
		    earliestFormed = formed
		    earliestFormedDate = Date(formed)
		end
    	
    	mw.log(latestDissipated)
    	mw.log(latestDissipatedDate)
    	mw.log(earliestFormed)
    	mw.log(earliestFormedDate)
    	
	    if tableWindsUnit == nil then
	    	tableWindsUnit = windsUnit
    	elseif windsUnit ~= tableWindsUnit then
    		error("Winds unit of one storm does not match winds unit of all storms")
		end
	    if tablePressureUnit == nil then
	    	tablePressureUnit = pressureUnit
    	elseif pressureUnit ~= tablePressureUnit then
    		error("Pressure unit of one storm does not match pressure unit of all storms")
    	end
	    if strongestWinds == nil or strongestWinds < tonumber(winds) then
	    	strongestWinds = tonumber(winds)
    	end
	    if lowestPressure == nil or lowestPressure < tonumber(pressure) then
	    	lowestPressure = tonumber(pressure)
    	end
	    
        totalDamages = totalDamages + tonumber(damages)
        totalDeaths = totalDeaths + tonumber(deaths)
        
	end

	local tcHeader = frame:expandTemplate{
		title = "Tropical cyclone season effects (top)",
		args = {
			["no-header"] = args["no-header"],
			["Basin"] = args["Basin"] or args["basin"],
			["Season start year"] = args["Season start year"] or args["year"],
			["Season end year"] = args["Season end year"]
		}
	}

    local tcFooter = frame:expandTemplate{
    	title = "Tropical cyclone season effects (bottom)",
    	args = {
			["TC's"] = totalStorms,
			["dates"] = dateperiod(earliestFormed, latestDissipated),
			["winds"] = frame:expandTemplate{
				title = "convert",
				args = {
					strongestWinds, 
					tableWindsUnit,
					args["winds-target"] or "",
					abbr = "on"
				}
			},
			["pres"] = frame:expandTemplate{
				title = "convert",
				args = {
					lowestPressure, 
					tablePressureUnit,
					args["pressure-target"] or "inHg",
					abbr = "on"
				}
			},
			["damage"] = frame:expandTemplate{
				title = "ntsp",
				args = { totalDamages, "", args["currency"] or "$" }
			},
			["deaths"] = frame:expandTemplate{
				title = "nts",
				args = { totalDeaths }
			},
			["Refs"] = args["footer-refs"] or ""
		}
	}
	
	return tcHeader .. "\n" .. tableEntries .. "\n" .. tcFooter
end

return p