Spring til indhold

Modul:AstroOgier/Sexagesimal

Fra Wikipedia, den frie encyklopædi
-- Module for converting a decimal angle into sexagesimal units

local p = {} -- Opret en lokal variabel p med en tom tabel
             -- Alle moduler skal have en tabel, som de slutter med at returnere
             -- Tabellen vil komme til at indeholde funktioner som kan kaldes med #invoke

function p.IntDiv(frame)		-- Integer division
	local Nomin = frame.args[1]		-- Nominator
	local Denom = frame.args[2]		-- Denominator
	return math.floor(Nomin/Denom)
end	-- p.IntDiv

function p.sexagesimal (frame)
	-- Parameters:
	-- frame.args is an array of parameters taken from the calling #invoke-statement
	-- frame.args[1]: Decimal number to be converted
	-- frame.args[2]: Requiered number of decimals in the seconds value (0 to 9)
	-- frame.args[3]: Conversion format with three allowed values:
	--		"DD": DegToDMS. Output: Sign, degree, arcminute and arcsecond; ± ° ’ ”
	--		"DH": DegToHMS. Output: Degrees converted to hour measure: Hour, time minute and time second; h m s
	--		"HH": HrsToHMS. Output: Hour, time minute and time second; h m s
	-- Finds values for Degree/Hour (integer), Minute (integer) and Second (real)
	-- Rounding MUST occur at the beginning to avoid abominations such as 12:34:60

	local Degrees = frame.args[1]
	local Decimals = frame.args[2]
	local Selector = frame.args[3]
	local Sign

	if Selector == "DH" then Degrees = Degrees/15 end 
	if Selector == "DD" then
		if Degrees > 0 then 
			Sign = "+"
		elseif Degrees < 0 then
			Sign = "-"
		else
			Sign = " "
		end
	end
	Degrees = math.abs(Degrees)

	local TenPower = 10^Decimals
	local Factor = 3600*TenPower
	local I = math.floor(Factor*Degrees + 0.5)	-- Rounding
	local H = math.floor(I/Factor)         -- IntDiv(I, Factor) 
	I = I - Factor*H
	Factor = 60*TenPower
	local M = math.floor(I/Factor)         --IntDiv(I, Factor)
	-- local M = p.IntDiv(I, Factor)
	local S = I/TenPower - 60*M

	-- Return result
	local FormatStr
	if Selector == "DD" then 
		FormatStr = "%s&nbsp;%02d°&nbsp;%02d’&nbsp;%." .. Decimals .. "f”"
		return string.format (Format, Sign,H,M,S)
	else
		FormatStr = "%02d/%02d/%." .. tostring(Decimals) .. "f"
--		FormatStr = "%02d<sup>h</sup>&nbsp;%02d<sup>m</sup>&nbsp;%." .. Decimals .."f<sup>s</sup>"
--		return string.format (FormatStr, H,M,S) 
		return FormatStr
	end
end -- p.sexagesimal

return p