Modul:Vorlage:Sternkarte

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen

Die Dokumentation für dieses Modul kann unter Modul:Vorlage:Sternkarte/Doku erstellt werden

--[[
* Grafische Darstellung auf Sternkarte IAU (Module d'implementation du patron Carte de positionnement stellaire UAI.)
*
* Imported from https://fr.wikipedia.org/w/index.php?title=Module:Carte_de_positionnement_stellaire_UAI&oldid=143871472
* (which was Imported from https://it.wikipedia.org/w/index.php?title=Modulo:Mappa_di_localizzazione_IAU&oldid=84979493)
*
* Parameterliste
* Name, Sternbild, Rek, Dek, Breite, Bildtext, Ausrichtung, Symbol, Symbolgröße, NoThumb
]]--

require('Modul:No Globals')

local getArgs = require('Modul:Arguments').getArgs
local cfg = mw.loadData('Modul:Vorlage:Sternkarte/Konfiguration')
local errorCategory = '[[Kategorie:Wikipedia:Vorlagenfehler/Vorlage:Sternkarte]]'
local p = {}

local function errhandler(msg)
	local cat = mw.title.getCurrentTitle().namespace == 0 and errorCategory or ''
	return string.format('<span class="error">%s</span>%s', msg, cat)
end

--Parsen von Parameter Rek
local function parseRek(Rek)
	local h;
	local m;
	local s;
	local text = Rek;
	local pos;
	local value;
	if #text == 0 then
		return false, 0
	end
	pos = string.find(text,'/') or 0;
	h = tonumber(string.sub(text,1,pos-1)) or -1;
	if h == -1 then
		return false, 0;
	end
	text = string.sub(text,pos+1);
	pos = string.find(text,'/') or 0;
	m = tonumber(string.sub(text,1,pos-1)) or -1;
	if m == -1 then
		return false, 0;
	end
	text = string.sub(text,pos+1);
	s = tonumber(text) or -1;
	if s == -1 then
		return false, 0;
	end
	value= 15 * (h+m/60 + s/3600);
	return true, value;
end

-- Umformung von Parameter Dek Vorzeichen/Grad/Minuten/Sekunden in Dezimalgrad
-- Änderung: Ab sofort nur mit Slash hinter dem Vorzeichen: v/d/m/s
local function parseDek(Dek)
	local v;
	local d;
	local m;
	local s;
	local text = Dek;
	local pos;
	local value;
	if #text == 0 then
		return false, 0
	end
	pos = string.find(text,'/') or 0;
	v = string.sub(text,1,pos-1);
	if v=="-" then
		v = -1;
	elseif v=="+" then
		v = 1;
	else
		return false, 0;
	end
	text = string.sub(text,pos+1);
	pos = string.find(text,'/') or 0;
	d = tonumber(string.sub(text,1,pos-1)) or -1;
	if d == -1 then
		return false, 0;
	end
	text = string.sub(text,pos+1);
	pos = string.find(text,'/') or 0;
	m = tonumber(string.sub(text,1,pos-1)) or -1;
	if m == -1 then
		return false, 0;
	end
	text = string.sub(text,pos+1);
	s = tonumber(text) or -1;
	if s == -1 then
		return false, 0;
	end
	value= v * (d + m/60 + s/3600);
	return true, value;
end

-- Le funzioni getX e getY sono state ottenute dalle formule di Ysogo in
-- Discussioni_progetto:Astronomia#Posizione_nella_carte_della_costellazione:
-- X = Ax+(Aw/2)+(Ah/(DECsup-DECinf))*(DECogg-DECaxe)*sen((ARmed-ARogg)/disang)
-- Y = Ay+Ah-(Ah/(DECsup-DECinf))*((DECogg-DECaxe)*cos((ARmed-ARogg)/disang)-(DECinf-DECaxe))
local function getX(map, ar, dec, width, marksize)
	local x = map.ax + map.aw / 2 +
			  (map.ah / (map.dec_sup - map.dec_inf)) *
			  (dec - map.dec_axe) *
			  math.sin(((map.ar_med - ar) / map.dis_ang) * (math.pi / 180))
	-- scala il risultato in base a width e centra il marker
	return  math.floor(x * width / map.iw - marksize / 2 + 0.5)
end

local function getY(map, ar, dec, width, marksize)
	local y = map.ay + map.ah -
			  (map.ah / (map.dec_sup - map.dec_inf)) *
			  ((dec - map.dec_axe) * math.cos(((map.ar_med - ar) / map.dis_ang) * (math.pi / 180)) - (map.dec_inf - map.dec_axe) )
	-- scala il risultato in base a width e centra il marker
	return math.floor(y * width / map.iw - marksize / 2 + 0.5)
end

local function getThumbnail(text, image, args, width)
	local align= mw.ustring.lower(args.Ausrichtung or '');
	if align=='rechts' or align=='right' then 
		align='tright'
	elseif align=='links' or align=='left' then
		align='tleft'
	else
		align=''
	end
	local divNode = mw.html.create('div')
	divNode
		:addClass('thumb')
		:addClass(align)
		:tag('div')
			:addClass('thumbinner')
			:css('width', width .. 'px')
			:wikitext(text)
			:tag('div')
				:addClass('thumbcaption')
				:wikitext(args.Bildtext or '')
				:tag('div')
					:addClass('magnify')
					:wikitext(string.format('[[:File:%s]]', image))
	return tostring(divNode)
end


-- Per l'utilizzo da altro modulo
function p._main(args)
	local map, ar, dec, text
	local IsOk = false
	local width = tonumber(args.Breite) or cfg.options.mapwidth;
	local marksize = tonumber(args.Symbolbreite) or cfg.options.symwidth;
	local mark =  args.Symbol or cfg.options.symfile;
	local const = mw.ustring.lower(args.Sternbild or '');
	local tbl={'and','ant','aps','aql','aqr','ara','ari','aur','boo','cae','cam',
		       'cap','car','cas','cen','cep','cet','cha','cir','cma','cmi','cnc',
		       'col','com','cra','crb','crt','cru','crv','cvn','cyg','del','dor',
		       'dra','equ','eri','for','gem','gru','her','hor','hya','hyi','ind',
		       'lac','leo','lep','lib','lmi','lup','lyn','lyr','men','mic','mon',
		       'mus','nor','oct','oph','ori','pav','peg','per','phe','pic','psa',
		       'psc','pup','pyx','ret','scl','sco','sct','ser','sex','sge','sgr',
		       'tau','tel','tra','tri','tuc','uma','umi','vel','vir','vol','vul'}
	-- check the value of 'const'
	IsOk = false;
	for i,value in ipairs(tbl) do
		if const == value then
			IsOk = true;
		end
	end
	if not IsOk then
		error('Modul Vorlage:Sternkarte: Sternbildkarte nicht gefunden. Parameter Sternbild = "' .. const .. '"' , 2)
	end

	IsOk, ar  = parseRek(args.Rek or "");
	if not IsOk then
		error('Modul Vorlage:Sternkarte: Ungültige Koordinate für Rektaszension', 2)
	end

	IsOk, dec = parseDek(args.Dek or "")
	if not IsOk then
		error('Modul Vorlage:Sternkarte: Ungültige Koordinate für Deklination', 2)
	end

	-- Bei Rec > 255 Grad Serpens Cauda, sonst Serpens Caput
	if const == 'ser' then
		if ar > 255 then
			const = 'srcau'
		else
			const = 'srcap'
		end
	end
	map = cfg.mappe[const];
	-- Bei Rek = Null in der Karte: Koordinate harmonisieren
	if map.ar_med > ar + 180 then
		ar = ar + 360
	end

	-- Aufruf von Vorlage:Bild mit Markierung erweitert,
	-- um Das Bild mit Overlay zu erzeugen.
	
	local align= mw.ustring.lower(args.Ausrichtung or '');
	if align=='rechts' or align=='right' then 
		align='right'
	elseif align=='links' or align=='left' then
		align='left'
	elseif align=='mitte' or align=='center' then
		align='center'
	else
		align=''
	end
	text = mw.getCurrentFrame():expandTemplate {
		title = 'Vorlage:Bild mit Markierung erweitert',
		args = {
			Ausrichtung = align,
			Hintergrund = map.image,
			Hintergrund_Breite = width .. 'px',
			Hintergrund_Text = '',
			Markierung = mark,
			Markierung_Breite = marksize .. 'px',
			Markierung_Text = args.Name or '',
			x = getX(map, ar, dec, width, marksize),
			y = getY(map, ar, dec, width, marksize)
		}
	}
	if (args.NoThumb or "") == "" then 
		return getThumbnail(text, map.image, args, width + 2)
	else
		return text
	end
end

-- Entry-point per il template {{carte di localizzazione IAU}}
function p.main(frame)
	return select(2, xpcall(function()
		return p._main(getArgs(frame, { parentOnly = true }))
	end, errhandler))
end

return p