Module:Aircraft registration country

Lua
CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

Documentation for this module may be created at Module:Aircraft registration country/doc

Code

local p = {};
local string = require("string");

--- refer to https://en.wikipedia.org/wiki/List_of_aircraft_registration_prefixes
local aircraftCountry = {
	{	"N",
		"Aircraft registered in the United States have special disambig handling; see function",
	},
	{"YA-", "Afghanistan"},
	{"ZA-", "Albania"},
	{"7T-", "Algeria"},
	{"C3-", "Andorra"},
	{"D2-", "Angola"},
	{"VP-", "Anguilla"},
	{"V2-", "Antigua and Barbuda"},
	{"LV-", "Argentina"},
	{"EK-", "Armenia"},
	{"P4-", "Aruba"},
	{"VH-", "Australia"},
	{"OE-", "Austria"},
	{"4K-", "Azerbaijan"},
	{"C6-", "Bahamas"},
	{"A9C-", "Bahrain"},
	{"S2-", "Bangladesh"},
	{"8P-", "Barbados"},
	{"EW-", "Belarus"},
	{"OO-", "Belgium"},
	{"V3-", "Belize"},
	{"TY-", "Benin"},
	{"VP-", "Bermuda"},
	{"A5-", "Bhutan"},
	{"CP-", "Bolivia"},
	{"E7-", "Bosnia and Herzegovina"},
	{"A2-", "Botswana"},
	{"PP-", "Brazil"},
	{"VP-", "British Virgin Islands"},
	{"V8-", "Brunei"},
	{"LZ-", "Bulgaria"},
	{"XT-", "Burkina Faso"},
	{"9U-", "Burundi"},
	{"XU-", "Cambodia"},
	{"TJ-", "Cameroon"},
	{"C-", "Canada"},
	{"D4-", "Cape Verde"},
	{"VP-", "Cayman Islands"},
	{"TL-", "Central African Republic"},
	{"TT-", "Chad"},
	{"CC-", "Chile"},
	{"B-", "China"},
	{"HJ-", "Colombia"},
	{"D6-", "Comoros"},
	{"E5-", "Cook Islands"},
	{"TI-", "Costa Rica"},
	{"9A-", "Croatia"},
	{"CU-", "Cuba"},
	{"5B-", "Cyprus"},
	{"OK-", "Czech Republic"},
	{"OY-", "Denmark"},
	{"J2-", "Djibouti"},
	{"J7-", "Dominica"},
	{"HI", "Dominican Republic"},
	{"4W-", "East Timor"},
	{"HC-", "Ecuador"},
	{"SU-", "Egypt"},
	{"YS-", "El Salvador"},
	{"3C-", "Equatorial Guinea"},
	{"E3-", "Eritrea"},
	{"ES-", "Estonia"},
	{"3D-", "Eswatini"},
	{"ET-", "Ethiopia"},
	{"VP-", "Falkland Islands"},
	{"DQ-", "Fiji"},
	{"OH-", "Finland"},
	{"F-", "France"},
	{"F-", "French Guiana"},
	{"F-", "French Polynesia"},
	{"F-", "French West Indies"},
	{"TR-", "Gabon"},
	{"C5-", "Gambia"},
	{"4L-", "Georgia"},
	{"D-", "Germany"},
	{"9G-", "Ghana"},
	{"VP-", "Gibraltar"},
	{"SX-", "Greece"},
	{"J3-", "Grenada"},
	{"TG-", "Guatemala"},
	{"2-", "Guernsey"},
	{"3X-", "Guinea"},
	{"8R-", "Guyana"},
	{"HH-", "Haiti"},
	{"HR-", "Honduras"},
	{"B-", "Hong Kong"},
	{"HA-", "Hungary"},
	{"TF-", "Iceland"},
	{"VT-", "India"},
	{"PK-", "Indonesia"},
	{"EP-", "Iran"},
	{"YI-", "Iraq"},
	{"EI-", "Ireland"},
	{"M-", "Isle of Man"},
	{"4X-", "Israel"},
	{"4Z-", "Israel"},
	{"I-", "Italy"},
	{"TU-", "Ivory Coast"},
	{"6Y-", "Jamaica"},
	{"JA-", "Japan"},
	{"JA", "Japan"},
	{"JAA", "Japan"},
	{"JR", "Japan"},
	{"JY-", "Jordan"},
	{"4YB-", "Jordan and Iraq"},
	{"UP-", "Kazakhstan"},
	{"5Y-", "Kenya"},
	{"T3-", "Kiribati"},
	{"Z6-", "Kosovo"},
	{"9K-", "Kuwait"},
	{"EX-", "Kyrgyzstan"},
	{"RDPL-", "Laos"},
	{"YL-", "Latvia"},
	{"OD-", "Lebanon"},
	{"7P-", "Lesotho"},
	{"A8-", "Liberia"},
	{"5A-", "Libya"},
	{"HB-", "Liechtenstein"},
	{"LY-", "Lithuania"},
	{"LX-", "Luxembourg"},
	{"B-", "Macau"},
	{"5R-", "Madagascar"},
	{"7Q-", "Malawi"},
	{"9M-", "Malaysia"},
	{"8Q-", "Maldives"},
	{"TZ-", "Mali"},
	{"9H-", "Malta"},
	{"V7-", "Marshall Islands"},
	{"F-", "Martinique"},
	{"5T-", "Mauritania"},
	{"3B-", "Mauritius"},
	{"XA-", "Mexico"},
	{"XB-", "XB plus national emblem"},
	{"XC-", "XC plus national emblem"},
	{"V6-", "Micronesia"},
	{"ER-", "Moldova"},
	{"3A-", "Monaco"},
	{"JU-", "Mongolia"},
	{"4O-", "Montenegro"},
	{"VP-", "Montserrat"},
	{"CN-", "Morocco"},
	{"C9-", "Mozambique"},
	{"XY-", "Myanmar"},
	{"V5-", "Namibia"},
	{"C2-", "Nauru"},
	{"9N-", "Nepal"},
	{"PH-", "the Netherlands"},
	{"F-", "New Caledonia"},
	{"ZK-", "New Zealand"},
	{"ZL-", "ZL"},
	{"ZM-", "ZM"},
	{"YN-", "Nicaragua"},
	{"5U-", "Niger"},
	{"5N-", "Nigeria"},
	{"P-", "North Korea"},
	{"Z3-", "North Macedonia"},
	{"LN-", "Norway"},
	{"A4O-", "Oman"},
	{"AP-", "Pakistan"},
	{"HP-", "Panama"},
	{"P2-", "Papua New Guinea"},
	{"ZP-", "Paraguay"},
	{"OB-", "Peru"},
	{"RP-", "Philippines"},
	{"SP-", "Poland"},
	{"CR-", "Portugal"},
	{"A7-", "Qatar"},
	{"YR-", "Romania"},
	{"RA-", "Russia"},
	{"9XR-", "Rwanda"},
	{"V4-", "Saint Kitts and Nevis"},
	{"J6-", "Saint Lucia"},
	{"J8-", "Saint Vincent and the Grenadines"},
	{"5W-", "Samoa"},
	{"T7-", "San Marino"},
	{"HZ-", "Saudi Arabia"},
	{"6V-", "Senegal"},
	{"YU-", "Serbia"},
	{"S7-", "Seychelles"},
	{"9L-", "Sierra Leone"},
	{"9V-", "Singapore"},
	{"OM-", "Slovakia"},
	{"S5-", "Slovenia"},
	{"H4-", "Solomon Islands"},
	{"6O-", "Somalia"},
	{"ZS-", "South Africa"},
	{"HL", "South Korea"},
	{"HL-", "South Korea"},
	{"Z8-", "South Sudan"},
	{"EC-", "Spain"},
	{"4R-", "Sri Lanka"},
	{"ST-", "Sudan"},
	{"PZ-", "Suriname"},
	{"SE-", "Sweden"},
	{"HB-", "Switzerland"},
	{"YK-", "Syria"},
	{"B-", "Taiwan"}, -- will need special treatment to distinguish from mainland China aircraft
	{"EY-", "Tajikistan"},
	{"5H-", "Tanzania"},
	{"HS-", "Thailand"},
	{"5V-", "Togo"},
	{"A3-", "Tonga"},
	{"9Y-", "Trinidad and Tobago"},
	{"TS-", "Tunisia"},
	{"TC-", "Turkey"},
	{"EZ-", "Turkmenistan"},
	{"VQ-", "Turks and Caicos"},
	{"T2-", "Tuvalu"},
	{"5X-", "Uganda"},
	{"UR", "Ukraine"},
	{"UR-", "Ukraine"},
	{"A6-", "United Arab Emirates"},
	{"G-", "the United Kingdom"},
	{"CX-", "Uruguay"},
	{"UK-", "Uzbekistan"},
	{"YJ-", "Vanuatu"},
	{"YV", "Venezuela"},
	{"VN-", "Vietnam"},
	{"7O-", "Yemen"},
	{"9J-", "Zambia"},
	{"Z-", "Zimbabwe"},
}

-- given a tail number, return the country suffix to be used in categorization
p.main = function (frame) 
	local tailNumber = string.upper(frame.args[1])
	if string.sub(tailNumber, -11) == " (AIRCRAFT)" then -- remove the " (aircraft) at the end"
		tailNumber = string.sub(tailNumber, 1, -12) 
	end
	for i = 1, #aircraftCountry, 1 do 
		if (aircraftCountry[i][1] == string.sub(tailNumber, 1, #(aircraftCountry[i][1]))) then
			local trimmedTailNumber = string.sub(tailNumber, #(aircraftCountry[i][1])+1)
			if (aircraftCountry[i][1] == "N") then -- this is an American aircraft; apply additional category disambig
				local _, endNumeral = string.find(tailNumber, "%d+", 2); -- find the numeral part of the tail number
				if (endNumeral == 6) then return "the United States (10000+)" .. "|" .. trimmedTailNumber
				elseif (endNumeral == 5) then return "the United States (1000-9999)" .. "|" .. trimmedTailNumber
				elseif (endNumeral == 4) then return "the United States (100-999)" .. "|" .. trimmedTailNumber
				else return "the United States (1-99)" .. "|" .. trimmedTailNumber
				end
			end
			return aircraftCountry[i][2].."|"..trimmedTailNumber
		end
	end
end

return p