local intensities2 = {}
if intensity2 ~= nil then
intensity2 = string.upper(intensity2)
intensities2 = mw.text.split(intensity2, "/") or { intensity2 }
end
if data[scale] == nil then error(message("invalidScale", {scale})) end
if scale2 and data[scale2] == nil then error(message("invalidScale", {scale2})) end
local colors = {}
for k,v in pairs(intensities) do
local order = data[scale].ranks[v].order
colors[k] = data[scale].colors[order]
end
local color = averageColor(colors)
local colorIntensity = getValueOfColor(color)
local out = ''
if yn(doColor) then
out = out .. '<' .. (args.tag or "span") .. 'white" or "black") .. '; '
elseif args.style then
out = out .. '<' .. (args.tag or "span") .. 'span")
end
if args.style then
out = out .. args.style
out = out .. '" '
elseif yn(doColor) then
out = out .. '" '
end
out = out .. (args.tagProps ~= nil and args.tagProps or "")
out = out .. ">"
if yn(link) then
out = out .. '[[' .. data[scale].name .. "#" .. data[scale].id_prefix .. data[scale].ranks[intensities[1]].id .. "|"
end
if yn(doColor) then
out = out .. '<span style=\"color:' .. (colorIntensity < 0.5 and "white" or "black") .. ';">'
end
out = out .. listIntensitiesFromScale(scale, intensities, labelScale, scale2)
if yn(doColor) then
out = out .. '</span>'
end
if yn(link) then
out = out .. "]]"
end
if (scale2) then
out = out .. " ("
if yn(link) then
out = out .. '[[' .. data[scale2].name .. "#" .. data[scale2].id_prefix .. data[scale2].ranks[intensities2[1]].id .. "|"
end
if yn(doColor) then
out = out .. '<span style=\"color:' .. (colorIntensity < 0.5 and "white" or "black") .. ';">'
end
out = out .. listIntensitiesFromScale(scale2, intensities2, true, true)
if yn(doColor) then
out = out .. '</span>'
end
if yn(link) then
out = out .. "]]"
end
out = out .. ")"
end
out = out .. '</' .. (args.tag or "span") .. '>'
return mw.getCurrentFrame:preprocess(out)
end  
p._format = function(args)
if args["format"] == "wikitable" then
return p.formatInWikitable(args)
else
return p.formatTag(args)
end
end  
p._getScaleName = function(args)
local scale = string.lower(args.scale or args[1] or error(message("noScaleShortCode")))
if data[scale] == nil then error(message("invalidScale", {scale})) end
local out = ''
if yn(args.link or true) then
out = out .. '[[' .. data[scale].name .. '|'
end
out = out .. data[scale].name
if yn(args.link or true) then
out = out .. ']]'
end
return out
end  
-- uses binary search to convert a peak ground acceleration to a seismic intensity
function convert(pga, ranks, ranksSorted, , )
= ~= nil and or 0
= ~= nil and or #ranksSorted
index = math.floor(( + ) / 2)
local lower = ranks[ranksSorted[index + 1]].pga
local upper = ranksSorted[index + 2] and ranks[ranksSorted[index + 2]].pga or math.huge
if (pga >= upper) then
return convert(pga, ranks, ranksSorted, index, )
elseif (pga < lower) then
return convert(pga, ranks, ranksSorted, , index)
else
return ranksSorted[index + 1]
end
end  
p._convert = function(args)
local scale = string.lower(args.scale or args[1] or error(message("noScaleShortCode")))
if data[scale] == nil then error(message("invalidScale", {scale})) end
local ranksSorted = {}
for k,v in ipairs(data["mmi"].ranksSorted) do ranksSorted[k] = v end
return convert(tonumber(args.pga or args[2]), data[scale].ranks, ranksSorted)
end  
p.convert1 = convert --debugging  
p._list = function(args)
local out = mw.html.create('ul')
for k,v in pairs(data) do
local list = out:tag('li')
list:wikitext('<code>' .. k .. '</code>: ' .. v.name .. '')
local tb = list:tag('table'):addClass("wikitable")
tb
:tag("tr")
:tag("th"):wikitext("Seismic intensity"):done
:tag("th"):wikitext("Display"):done
:done
for l,w in pairs(v.order) do
tb:tag('tr'):wikitext('<td><code>' .. w .. '</code></td> ' .. p.format({k, w, tag = "td"})):done
end
list:done
end
out:allDone
return tostring(out)
end  
-- make each scale invokable
for k,v in pairs(data) do
if p["_" .. k] ~= nil then error(message("scaleNameInvalid", k)) end
p["_" .. k] = function(args)
args["scale"] = k
args["intensity"] = args["intensity"] or args[1] or nil
args["scale2"] = args["scale2"] or args[2] or nil
args["intensity2"] = args["intensity2"] or args[3] or nil
if args["format"] == "wikitable" then
return p.formatInWikitable(args)
else
return p.formatTag(args)
end
end
end  
-- make all functions that begin with _ invokable
local q = mw.clone(p)
for k,v in pairs(q) do
if mw.ustring.sub(k, 1, 1) == "_" then
p[mw.ustring.sub(k, 2, #k)] = makeInvokeFunc(k)
end
end  
return p