Module:Sandbox/Erutuon/Climate
Appearance
Usage
Exports a function that calculates the Köppen climate classification of monthly average temperature and precipitation data in degrees Celsius and millimeters, based on the formulas described in the Wikipedia article.
Function
|Koeppen_template=
- Parameters:
|1=
,|2=
- Temperatures and precipitation figures (12, one per month), separated by any characters that are not digits (0-9), hyphen-minuses (-), or periods (.).
|3=yes
- Indicates that the data is from a location in the Southern Hemisphere. The default is the Northern Hemisphere. This is used to determine the high-sun and low-sun halves of the year.
|alt_CD=
- Use -3 °C as the dividing line between C and D climates. The default is 0 °C.
|alt_hk=
- Cold semi-arid and arid climates (last letter k) have mean annual temperatures below 18 °C. The default is that they have at least one month below 0 °C.
|alt_w=
- Dry-winter climates (last letter w) have more than 70% of precipitation in high-sun half of year. Default is that wettest summer month has ten times as much precipitation as driest winter month.
Examples
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
- Lua error in package.lua at line 80: module 'Module:Utility' not found.
local p = {}
local use_alt_CD_isotherm = false
local use_alt_hk_isotherm = false
local use_alt_w_isotherm = false
local map = require "Module:Utility".map
local function errorf(level, ...)
if type(level) == number then
return error(string.format(...), level + 1)
else -- level is actually the format string.
return error(string.format(level, ...), 2)
end
end
local function count(func, t)
local count = 0
for _, v in ipairs(t) do
if func(v) then
count = count + 1
end
end
return count
end
local function make_arr_and_varargs_func(varargs_func)
local function arr_func (arr, i, j)
i = i or 1
j = j or #arr
assert(i > 0 and j > 0)
if i == j then
return arr[i]
elseif i < j then -- |---i+++j---|
return varargs_func(unpack(arr, i, j))
else -- |+++j---i+++|
return varargs_func(varargs_func(unpack(arr, 1, j)),
varargs_func(unpack(arr, i)))
end
end
return function (...)
if type(...) == 'table' then -- if first argument is table
if select(2, ...) then -- if second argument
return arr_func(...)
else
return varargs_func(unpack((...)))
end
else
return varargs_func(...)
end
end
end
local function sum_varargs (...)
local n = select('#', ...)
local result = 0
for i = 1, n do
result = result + select(i, ...)
end
return result
end
local mean = make_arr_and_varargs_func(
function (...)
return sum_varargs(...) / select('#', ...)
end)
local sum = make_arr_and_varargs_func(sum_varargs)
local min_and_max = make_arr_and_varargs_func(
function (...)
local min, max = math.huge, -math.huge
for i = 1, select('#', ...) do
local val = select(i, ...)
if val < min then
min = val
end
if val > max then
max = val
end
end
return min, max
end)
local get_min_and_max_objects = make_arr_and_varargs_func(
function (...)
local min, max = math.huge, -math.huge
local min_index, max_index
for i = 1, select('#', ...) do
local val = select(i, ...)
if type(val) == "table" then
i = val.index
val = val.value
end
if val < min then
min = val
min_index = i
end
if val > max then
max = val
max_index = i
end
end
return { value = min, index = min_index }, { value = max, index = max_index}
end)
local function min_index(t)
local index
local min = math.huge
for i, v in ipairs(t) do
if v < min then
min = v
index = i
end
end
return index
end
local function max_index(t)
local index
local max = -math.huge
for i, v in ipairs(t) do
if v > max then
max = v
index = i
end
end
return index
end
local function in_range(val, low, high)
if low < high then -- |---i+++j---|
return low <= val and val <= high
else -- |+++j---i+++|
return val < high or low < val
end
end
local function mean_of_highs_and_lows(highs, lows)
local high_count, low_count = #highs, #lows
-- for now, no annual average accepted
if not (high_count == 12 and low_count == 12) then
errorf("Wrong number of highs or lows (%d, %d): expected 12 each",
high_count, low_count)
elseif high_count ~= low_count then
errorf("Number of highs (%d) is not equal to number of lows (%d)",
high_count, low_count)
end
local temperatures = {}
for i = 1, high_count do
if highs[i] <= lows[i] then
errorf("High #%d (%d) is not greater than low #%d (%d)",
i, highs[i], i, lows[i])
end
temperatures[i] = mean(highs[i], lows[i])
end
return temperatures
end
local seasons = { summer = { south = { 10, 3 }, north = { 4, 9 } } }
seasons.winter = {}
seasons.winter.south = { seasons.summer.south[2] + 1, seasons.summer.south[1] - 1 }
seasons.winter.north = { seasons.summer.north[2] + 1, seasons.summer.north[1] - 1 }
local function construct(self, val, Southern_Hemisphere)
val = val or {}
val.summer_months = seasons.summer[Southern_Hemisphere and "south" or "north"]
val.winter_months = seasons.winter[Southern_Hemisphere and "south" or "north"]
return setmetatable(val, self)
end
local precipitation_mt = {}
function precipitation_mt:__index(key)
if key == "sum" then
local sum = sum(self)
self.sum = sum
return sum
elseif key == "summer_sum" then
if not self.summer_months then error("No summer months specified") end
local summer_sum = sum(self, unpack(self.summer_months))
self.summer_sum = summer_sum
return summer_sum
elseif key == "winter_sum" then
if not self.winter_months then error("No summer months specified") end
local winter_sum = sum(self, unpack(self.winter_months))
self.winter_sum = winter_sum
return winter_sum
elseif key == "winter_min" or key == "winter_max" then
local winter_min, winter_max =
get_min_and_max_objects(self, unpack(self.winter_months))
self.winter_min, self.winter_max = winter_min, winter_max
if key == "winter_min" then
return winter_min
else
return winter_max
end
elseif key == "summer_min" or key == "summer_max" then
local summer_min, summer_max =
get_min_and_max_objects(self, unpack(self.summer_months))
self.summer_min, self.summer_max = summer_min, summer_max
if key == "summer_min" then
return summer_min
else
return summer_max
end
-- Get objects containing the value and the index of the value (to make it
-- possible to determine whether maximum or minimum precipitation occurs in
-- the high- or low-sun half of the year).
elseif key == "min" or key == "max" then
local min, max = get_min_and_max_objects(self)
self.min, self.max = min, max
if key == "min" then
return min
else
return max
end
end
end
local temperatures_mt = {}
function temperatures_mt:__index(key)
if key == "mean" then
local mean = mean(self)
self.mean = mean
return mean
-- Get objects containing the value and the index of the value (to make it
-- possible to determine whether maximum or minimum precipitation occurs in
-- the high- or low-sun half of the year).
elseif key == "min" or key == "max" then
local min, max = get_min_and_max_objects(self)
self.min, self.max = min, max
if key == "min" then
return min
else
return max
end
elseif key == "above_10" then
local above_10 = count(
function (temperature)
return temperature > 10
end,
self)
self.above_10 = above_10
return above_10
end
end
local function get_aridity_threshold(mean_temp, total_precip, total_summer_precip)
local summer_precip_fraction = total_summer_precip / total_precip
return mean_temp * 20
+ (summer_precip_fraction >= 0.7 and 280
or summer_precip_fraction >= 0.3 and 140
or 0)
end
local function get_first_letter(min_month_temp, max_month_temp)
return max_month_temp < 10 and "E"
or min_month_temp < (use_alt_CD_isotherm and -3 or 0) and "D"
or min_month_temp >= 18 and "A"
or "C"
end
-- in C: cond ? a : b
-- in Python: a if cond else b
local function ternary(cond, a, b)
if cond then
return a
else
return b
end
end
-- Temperatures and precipitation are tables of mean monthly temperature and
-- precipitation. Or temperatures can be a table containing a table of monthly
-- mean of daily highs and monthly mean of daily lows.
-- Units: °C, mm.
function p.Koeppen(temperatures, precipitation, Southern_Hemisphere)
local temperature_count = #temperatures
if temperature_count == 2 then
local highs_and_lows = temperatures
temperatures = mean_of_highs_and_lows(unpack(temperatures))
elseif temperature_count ~= 12 then
errorf("Wrong number of temperatures (expected 12, got %d)",
temperature_count)
elseif #precipitation ~= 12 then
errorf("Wrong number of precipitation stats (expected 12, got %d)",
#precipitation)
end
temperatures = construct(temperatures_mt, temperatures, Southern_Hemisphere)
precipitation = construct(precipitation_mt, precipitation, Southern_Hemisphere)
local aridity_threshold =
get_aridity_threshold(temperatures.mean, precipitation.sum, precipitation.summer_sum)
if precipitation.sum <= aridity_threshold then
return "B"
.. (precipitation.sum < aridity_threshold / 2 and "W" or "S") -- arid, semi-arid
.. (ternary(use_alt_hk_isotherm, temperatures.mean < 18, temperatures.min.value < 0)
and "k" or "h")
end
local first_letter = get_first_letter(temperatures.min.value, temperatures.max.value)
if first_letter == "E" then
return first_letter .. (temperatures.max.value < 0 and "F" or "T")
elseif first_letter == "A" then
return first_letter
.. (precipitation.min.value >= 60 and "f"
or precipitation.min.value / precipitation.sum > 0.04 and "m"
or in_range(precipitation.min.index, unpack(precipitation.summer_months))
and "s"
or "w")
else
local second_letter =
ternary(use_alt_w_isotherm, precipitation.sum / precipitation.summer_sum >= 0.7,
precipitation.summer_max.value > precipitation.winter_min.value * 10)
and "w"
or precipitation.summer_min.value < 30
and precipitation.winter_max.value > precipitation.summer_min.value * 3
and "s"
or "f"
local third_letter
if temperatures.above_10 <= 3 then
if temperatures.min.value < -38 then
third_letter = "d"
else
third_letter = "c"
end
elseif temperatures.max.value < 22 then
third_letter = "b"
else
third_letter = "a"
end
return first_letter .. second_letter .. third_letter
end
end
local function gather_numbers(str)
local arr = {}
local i = 0
for number in str:gmatch('%-?%d+%.?%d*') do
i = i + 1
arr[i] = tonumber(number)
end
return arr
end
function p.Koeppen_template(frame)
local temperature_string = frame.args[1]
local precipitation_string = frame.args[2]
local temperatures, precipitation =
gather_numbers(temperature_string), gather_numbers(precipitation_string)
local yesno = require 'Module:yesno'
local Southern_Hemisphere = yesno(frame.args[3])
if yesno(frame.args.alt_CD) then
use_alt_CD_isotherm = true
end
if yesno(frame.args.alt_hk) then
use_alt_hk_isotherm = true
end
if yesno(frame.args.alt_w) then
use_alt_w_isotherm = true
end
return p.Koeppen(temperatures, precipitation, Southern_Hemisphere)
end
return p