Jump to content

Module:Weather box/row/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Johnuniq (talk | contribs) at 01:11, 28 December 2018 (use an object to hold each value to allow removal of duplicate code; this is equivalent to original). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local w = {}
local math_mod = require( "Module:Math" )
local wbc = require( "Module:WeatherBoxColors" )

local Value
Value = {
	setNumber = function (self, number)
		-- check string is nil
		self.number = number
		-- other related stuff (set string?)
	end,
	setString = function (self, str)
		-- check number is nil
		self.string = str
		-- other related stuff (set num, precision?)
	end,
	new = function (val, str)
		return setmetatable({
			number = val,
			string = str,
		}, Value)
	end
}
Value.__index = Value

local function checkFlag( flag )
	if flag == nil then
		return nil
	elseif type( flag ) == 'boolean' then
		return flag
	elseif type( flag ) == 'string' then
		flag = flag:lower()
		if flag == '0' or flag == 'false' or
				flag == '' or flag == 'no' or
				flag == 'n' then
			return false
		else
			return true
		end
	else
		return error( 'Flag type not valid' )
	end
end

local function makeLine( label, first_values, second_values, color_values, color_scheme )
	local result = {'|- style="text-align: center;"\n! scope="row" style="height: 16px;" | ', label,  "\n"}
	for i = 1,13 do
		local color_str = color_values[i]

		if i == 13 then
			table.insert( result, table.concat( {'|style="', color_str, ' border-left-width:medium" | '} ) )
		else
			table.insert( result, table.concat( {'|style="', color_str, '" | '} ) )
		end

		local value_str = first_values[i].string
		if value_str ~= '' and value_str ~= nil then
			table.insert( result, value_str )
			if second_values ~= nil then
				value_str = second_values[i].string
				if value_str ~= '' and value_str ~= nil then
					table.insert( result, "<br />(" .. value_str .. ")" )
				end
			end
		else
			table.insert( result, '—' )
		end

		table.insert( result, "\n" )
	end

	return table.concat( result )
end

local function getInputs( frame, group_name, suffix, include_space )
	local month_names = { 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
		'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'year' }
	local str, str2, val

	local values = {}
	if suffix == nil then
		for i, mon in ipairs( month_names ) do
			local value
			if include_space then
				str = ( frame.args[ mon .. ' ' .. group_name ] or '' )
			else
				str = ( frame.args[ mon .. group_name ] or '' )
			end
			val, str2 = math_mod._cleanNumber( str )
			if val ~= nil then
				value = Value.new(val, str2)
			else
				value = Value.new(-9999, str)
			end
			values[i] = value
		end
	else
		for i, mon in ipairs( month_names ) do
			local value, updated
			for var, suf in ipairs( suffix ) do
				if include_space then
					str = frame.args[ mon .. ' ' .. group_name .. ' ' .. suf ]
				else
					str = frame.args[ mon .. group_name .. ' ' .. suf ]
				end
				if str ~= nil and str ~= '' then
					val, str2 = math_mod._cleanNumber( str )
					if val ~= nil then
						value = Value.new(val, str2)
					else
						value = Value.new(-9999, str)
					end
					value.variant = var
					updated = true
					break
				end
			end
			if not updated then
				value = Value.new(-9999, '')
				value.variant = 0
			end
			values[i] = value
		end
	end

	return values
end

local function getAnnualValue( values, mode )
	if mode == 'avg' or mode == 'sum' then
		local total = 0
		local p1, p2
		p1 = 0
		for i = 1, 12 do
			local val = values[i].number
			if val == -9999 then
				return Value.new(-9999, '')
			end

			p2 = math_mod._precision( val )
			if p2 > p1 then
				p1 = p2
			end

			total = total + val
		end
		if mode == 'avg' then
			total = math_mod._round( total / 12, p1 )
		end
		return Value.new(total, tostring( total ))
	elseif mode == 'min' then
		local min_val = nil
		for i = 1, 12 do
			local val = values[i].number
			if val ~= -9999 then
				if min_val == nil or val < min_val then
					min_val = val
				end
			end
		end
		return Value.new(min_val, tostring( min_val ))
	elseif mode == 'max' then
		local max_val = nil
		for i = 1, 12 do
			local val = values[i].number
			if val ~= -9999 then
				if max_val == nil or val > max_val then
					max_val = val
				end
			end
		end
		return Value.new(max_val, tostring( max_val ))
	else
		error( 'Unrecognized Annual Mode' )
	end
end

local function reconcileTemperature( C_values, F_values )
	for i = 1,13 do
		local p
		if C_values[i].string == '' then
			if F_values[i].number ~= -9999 then
				p = math.max( 0, math_mod._precision( F_values[i].string ) )
				C_values[i].number = math_mod._round( (F_values[i].number - 32)*5/9, p )
				C_values[i].string = tostring( C_values[i].number )
			end
		elseif F_values[i].string == '' then
			if C_values[i].number ~= -9999 then
				p = math.max( 0, math_mod._precision( C_values[i].string ) )
				F_values[i].number = math_mod._round( C_values[i].number*9/5 + 32, p )
				F_values[i].string = tostring( F_values[i].number )
			end
		end
	end
end

local function reconcilePrecipitation( M_values, I_values, prefer_cm )
	local v_class = 0
	for i = 1,13 do
		if M_values[i].variant == 1 then
			v_class = 1
		elseif M_values[i].variant == 2 then
			v_class = 2
		end
	end
	if v_class == 0 then
		if prefer_cm then
			v_class = 1
		else
			v_class = 2
		end
	end
	for i = 1,13 do
		local p
		if M_values[i].string == '' then
			if I_values[i].number ~= -9999 then
				if v_class == 1 then
					p = math.max( 0, math_mod._precision( I_values[i].string ) ) - 1
					M_values[i].number = math_mod._round( I_values[i].number*2.54, p )
					M_values[i].variant = v_class
				else
					p = math.max( 0, math_mod._precision( I_values[i].string ) ) - 2
					M_values[i].number = math_mod._round( I_values[i].number*25.4, p )
					M_values[i].variant = v_class
				end
				M_values[i].string = tostring( M_values[i].number )
			end
		elseif I_values[i].string == '' then
			if M_values[i].number ~= -9999 then
				if M_values[i].variant == 1 then
					p = math.max( 0, math_mod._precision( M_values[i].string ) )
					I_values[i].number = M_values[i].number/2.54
				else
					p = math.max( 0, math_mod._precision( M_values[i].string ) ) + 1
					I_values[i].number = M_values[i].number/25.4
				end
				I_values[i].number = math_mod._round( I_values[i].number, p )
				I_values[i].string = tostring( I_values[i].number )
			end
		end
	end
end

function w.buildRow( frame )
	local mode = (frame.args.mode or 'basic'):lower()
	local group_name = frame.args.group_name
	local first_values, second_values
	local color_values
	local color_scheme = frame.args.color_scheme or 't'
	local scale_factor = math_mod._cleanNumber( frame.args.scale_factor) or 1
	local date_mode = checkFlag( frame.args.date_mode or false )
	local label = frame.args.label or ''
	local annual_mode = (frame.args.annual_mode or 'avg'):lower()
	local include_space = checkFlag( frame.args.include_space or true )
	local second_line = checkFlag( frame.args.second_line ) or false
	local prefer_cm = checkFlag( frame.args.prefer_cm ) or false
	local result

	local pframe = frame:getParent()
	local imperial_first = checkFlag( frame.args['imperial first'] )
	if imperial_first == nil then imperial_first = checkFlag( pframe.args['imperial first'] ) end

	local metric_first = checkFlag( frame.args['metric first'] )
	if metric_first == nil then metric_first = checkFlag( pframe.args['metric first'] ) end

	local single_line = checkFlag( frame.args['single line'] ) or checkFlag( pframe.args['single line'] )

	if imperial_first == nil and metric_first ~= nil then
		imperial_first = not metric_first
	else
		imperial_first = true
	end

	if mode == 'basic' then
		first_values = getInputs( pframe, group_name, nil, include_space )
		second_values = nil
	elseif mode == 'temperature' then
		first_values = getInputs( pframe, group_name, {'C'}, include_space )
		second_values = getInputs( pframe, group_name, {'F'}, include_space )
		reconcileTemperature( first_values, second_values )
	elseif mode == "precipitation" then
		first_values = getInputs( pframe, group_name, {'cm', 'mm'}, include_space )
		second_values = getInputs( pframe, group_name, {'inch'}, include_space )
		reconcilePrecipitation( first_values, second_values, prefer_cm )
	else
		error( 'Requested mode not recognized' )
	end

	local good = false
	for i = 1,13 do
		if first_values[i].string ~= nil and first_values[i].string ~= '' then
			good = true
			break
		end
	end
	if not good then
		return ''
	end

	if first_values[13].string == nil or first_values[13].string == '' then
		first_values[13] = getAnnualValue( first_values, annual_mode )
	end
	if second_values ~= nil then
		if second_values[13].string == nil or second_values[13].string == '' then
			second_values[13] = getAnnualValue( second_values, annual_mode )
		end
		if mode == 'precipitation' then
			for i = 1,12 do
				if first_values[i].variant ~= 0 then
					first_values[13].variant = first_values[i].variant
					break
				end
			end
		end
	end

	color_scheme = wbc.interpret_color_code( color_scheme )

	color_values = {}
	local month_adj = { 31/30, 28.25/30, 31/30, 1, 31/30, 1,
		31/30, 31/30, 1, 31/30, 1, 31/30, 12.175 }
	for i = 1,13 do
		if first_values[i].number ~= nil and first_values[i].number ~= -9999 then
			local adj = scale_factor
			if date_mode then
				adj = adj / month_adj[i]
			end
			if mode == "precipitation" then
				if first_values[i].variant == 1 then
					adj = adj * 10
				end
			end
			table.insert( color_values, color_scheme( first_values[i].number * adj ) )
		else
			table.insert( color_values, color_scheme( nil ) )
		end
	end

	local lang = mw.getContentLanguage()
	local function formatNumber(number, number_string)
		if number ~= nil and number ~= -9999 then
			if math.abs(number) >= 1000 then
				number_string = lang:formatNum( math.abs(number) )
				if number < 0 then
					number_string = '−' .. number_string
				end
			elseif number < 0 then
				number_string = '−' .. number_string:sub(2)
			end
		end
		return number_string
	end
	for i = 1,13 do
		first_values[i].string = formatNumber(first_values[i].number, first_values[i].string)
		if second_values ~= nil then
			second_values[i].string = formatNumber(second_values[i].number, second_values[i].string)
		end
	end

	if imperial_first and second_values ~= nil then
		first_values, second_values = second_values, first_values
	end

	if not single_line then
		if second_line and second_values ~= nil then
			first_values = second_values
		end
		second_values = nil
	end

	return makeLine( label, first_values, second_values, color_values, color_scheme )
end

return w