Module:Tropical cyclone season effects

MyWikiBiz, Author Your Legacy — Saturday April 27, 2024
Jump to navigationJump to search

Template:Module rating

Usage

This module is available only as a template since it requires a frame. You might want to visit the documentation for the template instead.



-- 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["start-year"] or args["year"]) == nil then
		error("Year (or start year) not specified")
	elseif not yesno(args["no-header"]) and args["start-year"] == nil and args["end-year"] ~= nil then
		error("End year specified but start year not specified")
	end
	
    local tableEntries = args[1] or ""
    
    local totalStorms = 0
    local strongestWinds = 0
    local tableWindsUnit = nil
    local lowestPressure = 2000
    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
    	
	    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-sort"] = totalStorms == 0 and "yes" or nil,
			["no-header"] = args["no-header"],
			["basin"] = args["basin"],
			["start-year"] = args["start-year"] or args["year"],
			["end-year"] = args["end-year"],
			["currency-link"] = args["currency-link"]
		}
	}

	-- Template parameters not yet standardized. Hence the usage of capitalized
	-- parameter names.
    local tcFooter = frame:expandTemplate{
    	title = "Tropical cyclone season effects (bottom)",
    	args = {
			["TC's"] = totalStorms .. " system" .. (totalStorms == 1 and "" or "s"),
			["dates"] = totalStorms == 0 and "Season not started" or
				dateperiod(
					earliestFormed, 
					(latestDissipated == "present" or yesno(args["active"])) 
					    and "Season ongoing" or latestDissipated,
					(latestDissipated == "present" or yesno(args["active"]))
						and nil or true
				),
			["winds"] = (totalStorms == 0 or strongestWinds == 0) and "" or
				(
					tableWindsUnit == "kn" and (
						frame:expandTemplate{
							title = "convert",
							args = {
								strongestWinds, 
								tableWindsUnit,
								args["winds-target"] or "kph",
								round = "5",
								abbr = "on",
								disp = "out"
							}
						} .. (args["winds-target2"] ~= "none" and " (" .. frame:expandTemplate{
							title = "convert",
							args = {
								strongestWinds, 
								tableWindsUnit,
								args["winds-target2"] or "mph",
								round = "5",
								abbr = "on",
								disp = "out"
							}
						} .. ")" or "")
					) or frame:expandTemplate{
						title = "convert",
						args = {
							strongestWinds, 
							tableWindsUnit,
							args["winds-target"] or "",
							round = "5",
							abbr = "on"
						}
					}
				),
			["pres"] = (totalStorms == 0 or lowestPressure == 0) and "" or
				frame:expandTemplate{
					title = "convert",
					args = {
						lowestPressure, 
						tablePressureUnit,
						args["pressure-target"] or "inHg",
						comma = "off",
						sigfig = 4,
						abbr = "on"
					}
				},
			["damage"] = (totalStorms == 0 or totalDamages == 0) and "" or
				frame:expandTemplate{
					title = totalDamages == 0 and "nts" or "ntsp",
					args = { totalDamages, "", totalDamages ~= 0 and (args["currency-symbol"] or "$") }
				},
			["deaths"] = (totalStorms == 0 or totalDeaths == 0) and "" or 
				frame:expandTemplate{
					title = "nts",
					args = { totalDeaths }
				},
			["Refs"] = args["footer-refs"] or ""
		}
	}
	
	return tcHeader .. "\n" .. tableEntries .. "\n" .. tcFooter
end

return p