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 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 some(func, t)
for _, v in ipairs(t) do
if func(v) then
return true
end
end
return false
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 mean (...)
local n = select('#', ...)
local sum = 0
for i = 1, n do
sum = sum + select(i, ...)
end
return sum / n
end
local function mean_of_highs_and_lows(highs, lows)
local high_count, low_count = #highs, #lows
if not (high_count == 12 or high_count == 13)
or not (low_count == 12 or low_count == 13) then
errorf("Wrong number of highs or lows (%d, %d): expected 12 or 13 each",
high_count, low_count)
elseif high_count ~= low_count then
error("Number of highs and and number of lows are not equal")
end
local temperatures = {}
for i = 1, high_count do
if highs[i] <= lows[i] then
error("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
-- 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.
function p.Koeppen(temperatures, precipitation, Southern_Hemisphere)
if #temperatures == 2 then
local highs_and_lows = temperatures
temperatures = mean_of_highs_and_lows(unpack(temperatures))
end
local min_temp_index, max_temp_index =
min_index(temperatures), max_index(temperatures)
local min_precip_index, max_precip_index =
min_index(precipitation), max_index(precipitation)
local first_summer_month, last_winter_month
if Southern_Hemisphere then
first_summer_month, last_winter_month = 10, 3
else
first_summer_month, last_winter_month = 4, 9
end
local mean_temp = mean(unpack(temperatures))
local total_precip = mean(unpack(precipitation))
local mean_summer_precip =
mean(unpack(precipitation, first_summer_month, last_winter_month))
local threshold_addition
if total_precip / mean_summer_precip >= 0.7 then
threshold_addition = 280
elseif total_precip / mean_summer_precip >= 0.3 then
threshold_addition = 140
else
threshold_addition = 0
end
local threshold = mean_temp * 20 + threshold_addition
if total_precip <= threshold then
return "B"
.. (total_precip < threshold / 2 and "W" or "S") -- arid, semi-arid
.. (mean_temp < 0 and "k" or "h")
end
local min_month_temp, max_month_temp =
temperatures[min_temp_index], temperatures[max_temp_index]
local temp_letter = min_month_temp < 10 and "E"
or min_month_temp < 0 and "D"
or min_month_temp >= 18 and "A"
or "C"
if temp_letter == "E" then
return temp_letter .. (max_month_temp < 0 and "F" or "T")
elseif temp_letter == "D" then
end
end
return p