![]() | This module is rated as alpha. It is ready for third-party input, and may be used on a few pages to see if problems arise, but should be watched. Suggestions for new features or changes in their input and output mechanisms are welcome. |
This is the Shindo module. Named after the Japanese term used for classifying earthquakes 震度, this template provides utility for seismic intensity scales.
Seismic intensity data generated on Module:Shindo/data. Messages on Module:Shindo/messages.
{{#invoke:Shindo|color}}
returns styling for a given seismic intensity on a scale.{{#invoke:Shindo|formatInWikitable}}
returns styled text for a given seismic intensity on a scale, best suited for a wikitable.{{#invoke:Shindo|formatTag}}
returns styled text for a given seismic intensity on a scale, as an HTML tag.{{#invoke:Shindo|format}}
returns styled text for a given seismic intensity on a scale, as an HTML tag or table, depending on whether the parameter "format" is "wikitable".{{#invoke:Shindo|convert}}
will convert a given peak ground acceleration into a seismic intensity.{{#invoke:Shindo|scale}}
returns styled text for a specific scale, while shifting all the numbered arguments left, useful for invoking within templates that give specific scales.scale
: Which seismic intensity scale to be used. This is the first parameter.intensity
: The seismic intensity corresponding to the first scale. This is the second parameter (or first parameter when the scale is invoked as a function). If multiple intensities are desired they should be separated with a slash (/) with no spaces between them.scale2
: If specified, a second seismic intensity scale will be shown in parentheses (but colored the same as the original). Only used when formatting as a "tag". This is the third parameter (or second parameter when the scale is invoked as a function).intensity2
: The seismic intensity corresponding to the second scale. This is the fourth parameter (or third parameter when the scale is invoked as a function).format
: Either "wikitable" or "tag".link
: Whether to link to the relevant article. Default is true
.color
: Whether to color the box for "formatTag" and "formatInWikitable". Default is true
. If not then there will be no additional tag and the text will be unstyled.style
: Additional styling for the wikitable/tag, formatted as inline CSS.tag
: What tag to use in "formatTag". Default is "span".tagProps
: Properties for the tag (or wikitable) entry.labelScale
: Whether to label the ranking with the name of the scale (useful when the scale is relatively unknown to most).pga
: The peak ground acceleration in the convert
function, in
%g's. Same as the second parameter.
local p = {}
local getArgs = require("Module:Arguments").getArgs
local data = mw.loadData("Module:Shindo/data")
local messages = mw.loadData("Module:Shindo/messages")
local makeInvokeFunc = require("Module:MakeInvokeFunc")(p)
local message = require("Module:Message")(messages)
local yn = require("Module:Yesno")
local function getValueOfColor(tbl)
return (tbl1 + tbl2 + tbl3]) / 3 / 255
end
local function rgbColor(tbl)
return tbl1 .. (tbl2 and ", " .. tbl2 .. (tbl3 and ", " .. tbl3 or "") or "")
end
local function averageColor(tbl)
local colors = {}
for k,v in pairs(tbl) do
colorsk = v
end
local avg = {0, 0, 0}
for _,color in pairs(colors) do
avg1 = avg1 + color1
avg2 = avg2 + color2
avg3 = avg3 + color3
end
avg1 = math.ceil(avg1 / #colors) - avg1 / #colors > 0.5 and math.floor(avg1 / #colors) or math.ceil(avg1 / #colors)
avg2 = math.ceil(avg2 / #colors) - avg2 / #colors > 0.5 and math.floor(avg2 / #colors) or math.ceil(avg2 / #colors)
avg3 = math.ceil(avg3 / #colors) - avg3 / #colors > 0.5 and math.floor(avg3 / #colors) or math.ceil(avg3 / #colors)
return avg
end
local function listIntensitiesFromScale(scale, intensities, labelScale, scale2)
local out = ''
if yn(labelScale) then
out = out .. datascale].short .. ' '
end
local categoriesAreSame = true
local category = ""
for k,v in pairs(intensities) do
if datascale].ranksv == nil then error(message("invalidIntensity", {v, scale})) end
out = out .. datascale].ranksv].label
if k ~= #intensities then
if #intensities == 2 then
out = out .. '–'
else
out = out .. '/'
end
end
if category == "" and categoriesAreSame then
category = datascale].ranksv].category or ""
end
categoriesAreSame = categoriesAreSame and category ~= "" and (datascale].ranksv].category and datascale].ranksv].category == category or false)
end
if categoriesAreSame and category ~= "" and not scale2 then
out = out .. " (''" .. category .. "'')"
end
return out
end
p._color = function(args)
local scale = string.lower(args.scale or args1 or error(message("noScaleShortCode")))
local intensity = string.upper(args.intensity or args2 or error(message("noIntensity")))
if datascale == nil then error(message("invalidScale", {scale})) end
if datascale].ranksintensity == nil then error(message("invalidIntensity", {intensity, scale})) end
local order = datascale].ranksintensity].order
local color = datascale].colorsorder
local colorIntensity = getValueOfColor(color)
return 'background-color:rgb(' .. rgbColor(color) .. '); color:' .. (colorIntensity < 0.5 and "white" or "black") .. ";"
end
p._formatInWikitable = function(args)
local scale = string.lower(args.scale or args1 or error(message("noScaleShortCode")))
local link = args.link ~= nil and args.link or true
local labelScale = args.labelScale ~= nil and args.labelScale or true
local doColor = args.color ~= nil and args.color or true
local intensity = string.upper(args.intensity or args2 or error(message("noIntensity")))
local intensities = mw.text.split(intensity, "/") or { intensity }
if datascale == nil then error(message("invalidScale", {scale})) end
local colors = {}
for k,v in pairs(intensities) do
local order = datascale].ranksv].order
colorsk = datascale].colorsorder
end
local color = averageColor(colors)
local colorIntensity = getValueOfColor(color)
local out = ""
if yn(args.header or false) then
out = out .. "! "
else
out = out .. "| "
end
out = out .. (args.tagProps ~= nil and args.tagProps or "")
if (yn(doColor)) then
out = out .. 'style="background-color:rgba(' .. rgbColor(color) .. '); color:' .. (colorIntensity < 0.5 and "white" or "black") .. ';' .. (args.style or "") .. '" | '
elseif (args.style) then
out = out .. 'style="' .. (args.style or "") .. '" | '
end
if yn(link) then
out = out .. '[[' .. datascale].name .. "#" .. datascale].id_prefix .. datascale].ranksintensity].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, false)
if yn(doColor) then
out = out .. '</span>'
end
if yn(link) then
out = out .. "]]"
end
return out
end
p._formatTag = function(args)
local scale = string.lower(args.scale or args1 or error(message("noScaleShortCode")))
local scale2 = args.scale2 or args3 or nil
if scale2 ~= nil then scale2 = string.lower(scale2) end
local link = args.link ~= nil and args.link or true
local labelScale = args.labelScale ~= nil and args.labelScale or true
local doColor = args.color ~= nil and args.color or true
local intensity = string.upper(args.intensity or args2 or error(message("noIntensity")))
local intensities = mw.text.split(intensity, "/") or { intensity }
local intensity2 = args.intensity2 or args4 or (scale2 and error(message("noIntensity"))) or nil
local intensities2 = {}
if intensity2 ~= nil then
intensity2 = string.upper(intensity2)
intensities2 = mw.text.split(intensity2, "/") or { intensity2 }
end
if datascale == nil then error(message("invalidScale", {scale})) end
if scale2 and datascale2 == nil then error(message("invalidScale", {scale2})) end
local colors = {}
for k,v in pairs(intensities) do
local order = datascale].ranksv].order
colorsk = datascale].colorsorder
end
local color = averageColor(colors)
local colorIntensity = getValueOfColor(color)
local out = ''
if yn(doColor) then
out = out .. '<' .. (args.tag or "span") .. ' style="background-color:rgba(' .. rgbColor(color) .. '); padding:4px; color:' .. (colorIntensity < 0.5 and "white" or "black") .. '; '
elseif args.style then
out = out .. '<' .. (args.tag or "span") .. ' style="'
else
out = out .. '<' .. (args.tag or "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 .. '[[' .. datascale].name .. "#" .. datascale].id_prefix .. datascale].ranksintensities1]].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 .. '[[' .. datascale2].name .. "#" .. datascale2].id_prefix .. datascale2].ranksintensities21]].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 args1 or error(message("noScaleShortCode")))
if datascale == nil then error(message("invalidScale", {scale})) end
local out = ''
if yn(args.link or true) then
out = out .. '[[' .. datascale].name .. '|'
end
out = out .. datascale].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, left, right)
left = left ~= nil and left or 0
right = right ~= nil and right or #ranksSorted
index = math.floor((left + right) / 2)
local lower = ranksranksSortedindex + 1]].pga
local upper = ranksSortedindex + 2 and ranksranksSortedindex + 2]].pga or math.huge
if (pga >= upper) then
return convert(pga, ranks, ranksSorted, index, right)
elseif (pga < lower) then
return convert(pga, ranks, ranksSorted, left, index)
else
return ranksSortedindex + 1
end
end
p._convert = function(args)
local scale = string.lower(args.scale or args1 or error(message("noScaleShortCode")))
if datascale == nil then error(message("invalidScale", {scale})) end
local ranksSorted = {}
for k,v in ipairs(data"mmi"].ranksSorted) do ranksSortedk = v end
return convert(tonumber(args.pga or args2]), datascale].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 .. '|' .. 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 args1 or nil
args"scale2" = args"scale2" or args2 or nil
args"intensity2" = args"intensity2" or args3 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
pmw.ustring.sub(k, 2, #k)] = makeInvokeFunc(k)
end
end
return p
![]() | This module is rated as alpha. It is ready for third-party input, and may be used on a few pages to see if problems arise, but should be watched. Suggestions for new features or changes in their input and output mechanisms are welcome. |
This is the Shindo module. Named after the Japanese term used for classifying earthquakes 震度, this template provides utility for seismic intensity scales.
Seismic intensity data generated on Module:Shindo/data. Messages on Module:Shindo/messages.
{{#invoke:Shindo|color}}
returns styling for a given seismic intensity on a scale.{{#invoke:Shindo|formatInWikitable}}
returns styled text for a given seismic intensity on a scale, best suited for a wikitable.{{#invoke:Shindo|formatTag}}
returns styled text for a given seismic intensity on a scale, as an HTML tag.{{#invoke:Shindo|format}}
returns styled text for a given seismic intensity on a scale, as an HTML tag or table, depending on whether the parameter "format" is "wikitable".{{#invoke:Shindo|convert}}
will convert a given peak ground acceleration into a seismic intensity.{{#invoke:Shindo|scale}}
returns styled text for a specific scale, while shifting all the numbered arguments left, useful for invoking within templates that give specific scales.scale
: Which seismic intensity scale to be used. This is the first parameter.intensity
: The seismic intensity corresponding to the first scale. This is the second parameter (or first parameter when the scale is invoked as a function). If multiple intensities are desired they should be separated with a slash (/) with no spaces between them.scale2
: If specified, a second seismic intensity scale will be shown in parentheses (but colored the same as the original). Only used when formatting as a "tag". This is the third parameter (or second parameter when the scale is invoked as a function).intensity2
: The seismic intensity corresponding to the second scale. This is the fourth parameter (or third parameter when the scale is invoked as a function).format
: Either "wikitable" or "tag".link
: Whether to link to the relevant article. Default is true
.color
: Whether to color the box for "formatTag" and "formatInWikitable". Default is true
. If not then there will be no additional tag and the text will be unstyled.style
: Additional styling for the wikitable/tag, formatted as inline CSS.tag
: What tag to use in "formatTag". Default is "span".tagProps
: Properties for the tag (or wikitable) entry.labelScale
: Whether to label the ranking with the name of the scale (useful when the scale is relatively unknown to most).pga
: The peak ground acceleration in the convert
function, in
%g's. Same as the second parameter.
local p = {}
local getArgs = require("Module:Arguments").getArgs
local data = mw.loadData("Module:Shindo/data")
local messages = mw.loadData("Module:Shindo/messages")
local makeInvokeFunc = require("Module:MakeInvokeFunc")(p)
local message = require("Module:Message")(messages)
local yn = require("Module:Yesno")
local function getValueOfColor(tbl)
return (tbl1 + tbl2 + tbl3]) / 3 / 255
end
local function rgbColor(tbl)
return tbl1 .. (tbl2 and ", " .. tbl2 .. (tbl3 and ", " .. tbl3 or "") or "")
end
local function averageColor(tbl)
local colors = {}
for k,v in pairs(tbl) do
colorsk = v
end
local avg = {0, 0, 0}
for _,color in pairs(colors) do
avg1 = avg1 + color1
avg2 = avg2 + color2
avg3 = avg3 + color3
end
avg1 = math.ceil(avg1 / #colors) - avg1 / #colors > 0.5 and math.floor(avg1 / #colors) or math.ceil(avg1 / #colors)
avg2 = math.ceil(avg2 / #colors) - avg2 / #colors > 0.5 and math.floor(avg2 / #colors) or math.ceil(avg2 / #colors)
avg3 = math.ceil(avg3 / #colors) - avg3 / #colors > 0.5 and math.floor(avg3 / #colors) or math.ceil(avg3 / #colors)
return avg
end
local function listIntensitiesFromScale(scale, intensities, labelScale, scale2)
local out = ''
if yn(labelScale) then
out = out .. datascale].short .. ' '
end
local categoriesAreSame = true
local category = ""
for k,v in pairs(intensities) do
if datascale].ranksv == nil then error(message("invalidIntensity", {v, scale})) end
out = out .. datascale].ranksv].label
if k ~= #intensities then
if #intensities == 2 then
out = out .. '–'
else
out = out .. '/'
end
end
if category == "" and categoriesAreSame then
category = datascale].ranksv].category or ""
end
categoriesAreSame = categoriesAreSame and category ~= "" and (datascale].ranksv].category and datascale].ranksv].category == category or false)
end
if categoriesAreSame and category ~= "" and not scale2 then
out = out .. " (''" .. category .. "'')"
end
return out
end
p._color = function(args)
local scale = string.lower(args.scale or args1 or error(message("noScaleShortCode")))
local intensity = string.upper(args.intensity or args2 or error(message("noIntensity")))
if datascale == nil then error(message("invalidScale", {scale})) end
if datascale].ranksintensity == nil then error(message("invalidIntensity", {intensity, scale})) end
local order = datascale].ranksintensity].order
local color = datascale].colorsorder
local colorIntensity = getValueOfColor(color)
return 'background-color:rgb(' .. rgbColor(color) .. '); color:' .. (colorIntensity < 0.5 and "white" or "black") .. ";"
end
p._formatInWikitable = function(args)
local scale = string.lower(args.scale or args1 or error(message("noScaleShortCode")))
local link = args.link ~= nil and args.link or true
local labelScale = args.labelScale ~= nil and args.labelScale or true
local doColor = args.color ~= nil and args.color or true
local intensity = string.upper(args.intensity or args2 or error(message("noIntensity")))
local intensities = mw.text.split(intensity, "/") or { intensity }
if datascale == nil then error(message("invalidScale", {scale})) end
local colors = {}
for k,v in pairs(intensities) do
local order = datascale].ranksv].order
colorsk = datascale].colorsorder
end
local color = averageColor(colors)
local colorIntensity = getValueOfColor(color)
local out = ""
if yn(args.header or false) then
out = out .. "! "
else
out = out .. "| "
end
out = out .. (args.tagProps ~= nil and args.tagProps or "")
if (yn(doColor)) then
out = out .. 'style="background-color:rgba(' .. rgbColor(color) .. '); color:' .. (colorIntensity < 0.5 and "white" or "black") .. ';' .. (args.style or "") .. '" | '
elseif (args.style) then
out = out .. 'style="' .. (args.style or "") .. '" | '
end
if yn(link) then
out = out .. '[[' .. datascale].name .. "#" .. datascale].id_prefix .. datascale].ranksintensity].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, false)
if yn(doColor) then
out = out .. '</span>'
end
if yn(link) then
out = out .. "]]"
end
return out
end
p._formatTag = function(args)
local scale = string.lower(args.scale or args1 or error(message("noScaleShortCode")))
local scale2 = args.scale2 or args3 or nil
if scale2 ~= nil then scale2 = string.lower(scale2) end
local link = args.link ~= nil and args.link or true
local labelScale = args.labelScale ~= nil and args.labelScale or true
local doColor = args.color ~= nil and args.color or true
local intensity = string.upper(args.intensity or args2 or error(message("noIntensity")))
local intensities = mw.text.split(intensity, "/") or { intensity }
local intensity2 = args.intensity2 or args4 or (scale2 and error(message("noIntensity"))) or nil
local intensities2 = {}
if intensity2 ~= nil then
intensity2 = string.upper(intensity2)
intensities2 = mw.text.split(intensity2, "/") or { intensity2 }
end
if datascale == nil then error(message("invalidScale", {scale})) end
if scale2 and datascale2 == nil then error(message("invalidScale", {scale2})) end
local colors = {}
for k,v in pairs(intensities) do
local order = datascale].ranksv].order
colorsk = datascale].colorsorder
end
local color = averageColor(colors)
local colorIntensity = getValueOfColor(color)
local out = ''
if yn(doColor) then
out = out .. '<' .. (args.tag or "span") .. ' style="background-color:rgba(' .. rgbColor(color) .. '); padding:4px; color:' .. (colorIntensity < 0.5 and "white" or "black") .. '; '
elseif args.style then
out = out .. '<' .. (args.tag or "span") .. ' style="'
else
out = out .. '<' .. (args.tag or "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 .. '[[' .. datascale].name .. "#" .. datascale].id_prefix .. datascale].ranksintensities1]].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 .. '[[' .. datascale2].name .. "#" .. datascale2].id_prefix .. datascale2].ranksintensities21]].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 args1 or error(message("noScaleShortCode")))
if datascale == nil then error(message("invalidScale", {scale})) end
local out = ''
if yn(args.link or true) then
out = out .. '[[' .. datascale].name .. '|'
end
out = out .. datascale].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, left, right)
left = left ~= nil and left or 0
right = right ~= nil and right or #ranksSorted
index = math.floor((left + right) / 2)
local lower = ranksranksSortedindex + 1]].pga
local upper = ranksSortedindex + 2 and ranksranksSortedindex + 2]].pga or math.huge
if (pga >= upper) then
return convert(pga, ranks, ranksSorted, index, right)
elseif (pga < lower) then
return convert(pga, ranks, ranksSorted, left, index)
else
return ranksSortedindex + 1
end
end
p._convert = function(args)
local scale = string.lower(args.scale or args1 or error(message("noScaleShortCode")))
if datascale == nil then error(message("invalidScale", {scale})) end
local ranksSorted = {}
for k,v in ipairs(data"mmi"].ranksSorted) do ranksSortedk = v end
return convert(tonumber(args.pga or args2]), datascale].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 .. '|' .. 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 args1 or nil
args"scale2" = args"scale2" or args2 or nil
args"intensity2" = args"intensity2" or args3 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
pmw.ustring.sub(k, 2, #k)] = makeInvokeFunc(k)
end
end
return p