function makeFromTemplateScale(scale, name, id_prefix, short)
	local returnVal = mw.clone(scale)
	returnVal.name = name or returnVal.name
	returnVal.id_prefix = id_prefix
	returnVal.short = short
	return returnVal
end

function addField(nameOfField, objectToWriteTo, objectToReadFrom)
	for k,v in pairs(objectToWriteTo) do
		if objectToReadFrom[k] ~= nil then v[nameOfField] = objectToReadFrom[k] end
	end
end

local data = {}

-- MMI and many other scales
data["mmi"] = {
	name = "Modified Mercalli intensity scale",
	id_prefix = "mmi-",
	short = "MMI",
	colors = {
		{255, 255, 255},
		{191, 204, 255},
		{175, 217, 255},
		{160, 230, 255},
		{128, 255, 255},
		{122, 255, 147},
		{255, 255, 0},
		{255, 200, 0},
		{255, 145, 0},
		{255, 0, 0},
		{200, 0, 0},
		{164, 0, 0},
		{128, 0, 0}
	},
	order = {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII"},
	ranks = {
		["1"] = {
			id = "1",
			order = 1,
			label = "I"
		},
		["2"] = {
			id = "2",
			order = 2,
			label = "II"
		},
		["3"] = {
			id = "3",
			order = 4,
			label = "III"
		},
		["4"] = {
			id = "4",
			order = 5,
			label = "IV"
		},
		["5"] = {
			id = "5",
			order = 6,
			label = "V"
		},
		["6"] = {
			id = "6",
			order = 7,
			label = "VI"
		},
		["7"] = {
			id = "7",
			order = 8,
			label = "VII"
		},
		["8"] = {
			id = "8",
			order = 9,
			label = "VIII"
		},
		["9"] = {
			id = "9",
			order = 10,
			label = "IX"
		},
		["10"] = {
			id = "10",
			order = 11,
			label = "X"
		},
		["11"] = {
			id = "11",
			order = 12,
			label = "XI"
		},
		["12"] = {
			id = "12",
			order = 13,
			label = "XII"
		}
	}
}

-- Roman numeral aliases
data["mmi"].ranks["I"] = data["mmi"].ranks["1"]
data["mmi"].ranks["II"] = data["mmi"].ranks["2"]
data["mmi"].ranks["III"] = data["mmi"].ranks["3"]
data["mmi"].ranks["IV"] = data["mmi"].ranks["4"]
data["mmi"].ranks["V"] = data["mmi"].ranks["5"]
data["mmi"].ranks["VI"] = data["mmi"].ranks["6"]
data["mmi"].ranks["VII"] = data["mmi"].ranks["7"]
data["mmi"].ranks["VIII"] = data["mmi"].ranks["8"]
data["mmi"].ranks["IX"] = data["mmi"].ranks["9"]
data["mmi"].ranks["X"] = data["mmi"].ranks["10"]
data["mmi"].ranks["XI"] = data["mmi"].ranks["11"]
data["mmi"].ranks["XII"] = data["mmi"].ranks["12"]

-- JMA, CWS, etc.
data["jma"] = {
	name = "Japan Meteorological Agency seismic intensity scale",
	id_prefix = "jma_",
	short = "JMA",
	order = {"0", "1", "2", "3", "4", "5-", "5+", "6-", "6+", "7"},
	colors = {
		{255, 255, 255},
		{242, 242, 255},
		{160, 230, 255},
		{0, 65, 255},
		{250, 230, 150},
		{255, 230, 0},
		{255, 153, 0},
		{255, 40, 0},
		{165, 0, 33},
		{180, 0, 104}
	},
	ranks = {
		["0"] = {
			id = "0",
			order = 1,
			label = "0"
		},
		["1"] = {
			id = "1",
			order = 2,
			label = "1"
		},
		["2"] = {
			id = "2",
			order = 3,
			label = "2"
		},
		["3"] = {
			id = "3",
			order = 4,
			label = "3"
		},
		["4"] = {
			id = "4",
			order = 5,
			label = "4"
		},
		["5-"] = {
			id = "5-",
			order = 6,
			label = "5−"
		},
		["5"] = {
			id = "5",
			order = 6,
			label = "5"
		},
		["5+"] = {
			id = "5+",
			order = 7,
			label = "5+"
		},
		["6-"] = {
			id = "6-",
			order = 8,
			label = "6−"
		},
		["6"] = {
			id = "6",
			order = 8,
			label = "6"
		},
		["6+"] = {
			id = "6+",
			order = 9,
			label = "6+"
		},
		["7"] = {
			id = "7",
			order = 10,
			label = "7"
		}
	}
}

data["cwa"] = makeFromTemplateScale(data.jma, "Central Weather Administration seismic intensity scale", "cwa_", "CWA")
data["csis"] = makeFromTemplateScale(data.mmi, "China seismic intensity scale", "csis-", "CSIS")
data["ems-98"] = makeFromTemplateScale(data.mmi, "European macroseismic scale", "ems-98-", "EMS-98")
data["msk-64"] = makeFromTemplateScale(data.mmi, "Medvedev–Sponheuer–Karnik scale", "msk-", "MSK-64")
data["rfs"] = makeFromTemplateScale(data.mmi, "Rossi–Forel scale", "rfs-", "RFS")
data["rfs"].ranks["11"] = nil
data["rfs"].ranks["12"] = nil
data["rfs"].ranks["XI"] = nil
data["rfs"].ranks["XII"] = nil
data["rfs"].order[12] = nil
data["rfs"].order[11] = nil
data["peis"] = makeFromTemplateScale(data.rfs, "PHIVOLCS earthquake intensity scale", "peis-", "PEIS")

-- ranks only present with USGS
data["mmi"].ranks["2-3"] = {
	id = "2",
	order = 3,
	label = "II–III"
}

data["mmi"].ranks["II-III"] = data["mmi"].ranks["2-3"]

data["mmi"].ranks["10+"] = {
	id = "10",
	order = 11,
	label = "X+"
}

data["mmi"].ranks["X+"] = data["mmi"].ranks["10+"]

data["mmi"].order = {"I", "II", "II-III", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "X+", "XI", "XII"}

-- Add categorical rankings to data
addField("category", data["mmi"].ranks,
	{
		["1"] = "Not felt",
		["2"] = "Weak",
		["3"] = "Weak",
		["2-3"] = "Weak",
		["4"] = "Light",
		["5"] = "Moderate",
		["6"] = "Strong",
		["7"] = "Very strong",
		["8"] = "Severe",
		["9"] = "Violent",
		["10+"] = "Extreme",
		["10"] = "Extreme",
		["11"] = "Extreme",
		["12"] = "Extreme"
	}
)

addField("category", data["ems-98"].ranks,
	{
		["1"] = "Not felt",
		["2"] = "Scarcely felt",
		["3"] = "Weak",
		["4"] = "Largely observed",
		["5"] = "Strong",
		["6"] = "Slightly damaging",
		["7"] = "Damaging",
		["8"] = "Heavily damaging",
		["9"] = "Destructive",
		["10"] = "Very destructive",
		["11"] = "Devastating",
		["12"] = "Completely devastating"
	}
)

addField("category", data["msk-64"].ranks,
	{
		["1"] = "Not perceptible",
		["2"] = "Hardly perceptible",
		["3"] = "Weak",
		["4"] = "Largely observed",
		["5"] = "Fairly strong",
		["6"] = "Strong",
		["7"] = "Very strong",
		["8"] = "Damaging",
		["9"] = "Destructive",
		["10"] = "Devastating",
		["11"] = "Catastrophic",
		["12"] = "Very catastrophic"
	}
)

addField("category", data["peis"].ranks,
	{
		["1"] = "Scarcely perceptible",
		["2"] = "Slightly felt",
		["3"] = "Weak",
		["4"] = "Moderately strong",
		["5"] = "Strong",
		["6"] = "Very strong",
		["7"] = "Destructive",
		["8"] = "Very destructive",
		["9"] = "Devastating",
		["10"] = "Completely devastating"
	}
)

addField("category", data["rfs"].ranks,
	{
		["1"] = "Microseismic tremor",
		["2"] = "Extremely feeble tremor",
		["3"] = "Feeble tremor",
		["4"] = "Slight tremor",
		["5"] = "Moderate tremor",
		["6"] = "Strong tremor",
		["7"] = "Very strong tremor",
		["8"] = "Damaging tremor",
		["9"] = "Devastating tremor",
		["10"] = "Extremely high intensity tremor"
	}
)

-- lower values for peak ground acceleration for MMI see https://usgs.github.io/shakemap/manual4_0/ug_intensity.html
-- all values in units of %g and will need to be converted for other uses
-- also categorization is done in buckets with averages precomputed in Excel
--[[
Rank	PGA listed	Average for bottom buckets
1	0.05	0.175
2-3	0.3	1.55
4	2.8	4.5
5	6.2	9.1
6	12	17
7	22	31
8	40	57.5
9	75	107
10+	139	
]]

addField("pga", data["mmi"].ranks, {
	["1"] = 0,
	["2-3"] = 0.175,
	["4"] = 1.55,
	["5"] = 4.5,
	["6"] = 9.1,
	["7"] = 17,
	["8"] = 31,
	["9"] = 57.5,
	["10+"] = 107
})

data["mmi"].ranksSorted = { "1", "2-3", "4", "5", "6", "7", "8", "9", "10+" }

-- same for JMA (values from [[Japan Meterological Agency seismic intensity scale]] and converted back to %g's)
addField("pga", data["jma"].ranks, {
	["0"] = 0,
	["1"] = 0.08,
	["2"] = 0.26,
	["3"] = 0.81,
	["4"] = 2.55,
	["5-"] = 8.16,
	["5+"] = 14.29,
	["6-"] = 25.51,
	["6+"] = 32.14,
	["7"] = 40
})

data["jma"].ranksSorted = { "0", "1", "2", "3", "4", "5-", "5+", "6-", "6+", "7" }

return data