<resource schema="cstl">
	<meta name="creationDate">2021-03-03T09:28:44Z</meta>
	<meta name="schema-rank">100</meta>

	<meta name="title">Constellations as Polygons</meta>
	<meta name="description">
		This table contains constellation data from Davenhall et al,
		ivo://cds.vizier/vi/49, converted to ADQL-queriable polygons.
		The polygons use the J2000 points, and we skip the interpolated
		points.
	</meta>
	<meta name="subject">constellations</meta>

	<meta name="creator">Davenhall, A.C.; Legget, S.K.</meta>

	<meta name="source">1997yCat.6049....0D</meta>
	<meta name="contentLevel">Research</meta>
	<meta name="contentLevel">Amateur</meta>
	<meta name="contentLevel">General</meta>
	<meta name="type">Catalog</meta>

	<table id="geo" onDisk="True" adql="True">
		<index columns="p" method="GIST"/>
		<stc>
			Position ICRS [p]
		</stc>

		<column name="abbrev" type="text"
			ucd="meta.id;meta.main"
			tablehead="Abbrev"
			description="Abbreviation of the constellation name"
			verbLevel="1"/>
		<column name="name" type="text"
			ucd="meta.id"
			tablehead="Name"
			description="Full name of the constellation name"
			verbLevel="1"/>

		<column name="p" type="spoly"
			ucd="pos"
			tablehead="Polygon"
			description="The coverage of the constellation as an ADQL polygon"
			verbLevel="1"/>
		<column name="ra"
			unit="deg" ucd="pos.eq.ra;meta.main"
			tablehead="RA"
			description="Representative RA (actually, the mean of the vertices)"
			verbLevel="1"/>
		<column name="dec"
			unit="deg" ucd="pos.eq.dec;meta.main"
			tablehead="Dec"
			description="Representative Dec (actually, the mean of the vertices)"
			verbLevel="1"/>
	</table>

	<coverage>
		<spatial>0/0-11</spatial>
	</coverage>

	<data id="import">
		<sources pattern="data/vertices.vot"/>

		<embeddedGrammar>
			<iterator>
				<setup imports="gavo.votable"/>
				<code>
					with open(self.sourceToken, "rb") as f:
						data, metadata = votable.load(f)

					constellations = {}
					for index, row in enumerate(metadata.iterDicts(data)):
						self.location = f"Row {index}"
						if row["type"] == "O":
							constellations.setdefault(row["cst"], []).extend(
								[row["RAJ2000"], row["DEJ2000"]])

					for name, points in constellations.items():
						yield {
							"abbrev": name.strip(),
							"p": pgsphere.SPoly.fromDALI(points),
						}
				</code>
			</iterator>
		</embeddedGrammar>

		<make table="geo">
			<rowmaker idmaps="*">
				<var key="name">@abbrev</var>
				<apply>
					<code>
						@ra, @dec = @p.getCenter()
						@ra /= DEG
						@dec /= DEG
					</code>
				</apply>
				<apply procDef="//procs#dictMap">
					<!-- Fix bad abbreviations in VizieR upstream -->
					<bind name="default">base.NotGiven</bind>
					<bind name="key">"name"</bind>
					<bind name="mapping">
						{"EQU": "EQL"}
					</bind>
				</apply>

				<apply procDef="//procs#dictMap">
					<bind name="key">"name"</bind>
					<bind name="mapping">{
"AND": "Andromeda", "ANT": "Antlia", "APS": "Apus", "AQR": "Aquarius",
"AQL": "Aquila", "ARA": "Ara", "ARI": "Aries", "AUR": "Auriga",
"BOO": "Bootes",
"CAE": "Caelum", "CAM": "Camelopardalis", "CNC": "Cancer",
"CVN": "Canes Venatici", "CMA": "Canis Major", "CMI": "Canis Minor",
"CAP": "Capricornus", "CAR": "Carina", "CAS": "Cassiopeia",
"CEN": "Centaurus", "CEP": "Cepheus", "CET": "Cetus", "CHA": "Chamaeleon",
"CIR": "Circinus", "COL": "Columba", "COM": "Coma Berenices",
"CRA": "Corona Australis", "CRB": "Corona Borealis", "CRV": "Corvus",
"CRT": "Crater", "CRU": "Crux", "CYG": "Cygnus",
"DEL": "Delphinus", "DOR": "Dorado", "DRA": "Draco",
"EQL": "Equuleus", "ERI": "Eridanus",
"FOR": "Fornax",
"GEM": "Gemini", "GRU": "Grus",
"HER": "Hercules", "HOR": "Horologium", "HYA": "Hydra", "HYI": "Hydrus",
"IND": "Indus",
"LAC": "Lacerta", "LEO": "LEO", "LMI": "Leo Minor", "LEP": "Lepus",
"LIB": "Libra", "LUP": "Lupus", "LYN": "Lynx", "LYR": "Lyra",
"MEN": "Mensa", "MIC": "Microscopium", "MON": "Monoceros", "MUS": "Musca",
"NOR": "Norma",
"OCT": "Octans", "OPH": "Ophiuchus", "ORI": "Orion",
"PAV": "Pavo", "PEG": "Pegasus", "PER": "Perseus", "PHE": "Phoenix",
"PIC": "Pictor", "PSC": "Pisces", "PSA": "Piscis Austrinus",
"PUP": "Puppis", "PYX": "Pyxis",
"RET": "Reticulum",
"SGE": "Sagitta", "SGR": "Sagittarius", "SCO": "Scorpius",
"SCL": "Sculptor", "SCT": "Scutum", "SER": "Serpens", "SEX": "Sextans",
"TAU": "Taurus", "TEL": "Telescopium", "TRI": "Triangulum",
"TRA": "Triangulum Australe", "TUC": "Tucana",
"UMA": "Ursa Major", "UMI": "Ursa Minor",
"VEL": "Vela", "VIR": "Virgo", "VOL": "Volans", "VUL": "Vulpecula",
"SER1": "Serpens caput",
"SER2": "Serpens cauda",}
					</bind>
				</apply>
			</rowmaker>
		</make>
	</data>

	<service id="cone" allowed="form,scs.xml">
		<meta name="shortName">cstl cone</meta>
		<meta name="description">This cone search will return all
			constellations within the cone, together with the J2000 polygons
			of them.
		</meta>
		<meta name="testQuery">
			<meta name="ra">40</meta>
			<meta name="dec">30</meta>
			<meta name="sr">2</meta>
		</meta>

		<publish render="form" sets="ivo_managed, local"/>
		<publish render="scs.xml" sets="ivo_managed"/>

		<dbCore queriedTable="geo">
			<condDesc original="//scs#humanInput">
				<phraseMaker original="//scs#humanSCSPhrase">
					<setup original="//scs#scsSetup" id="proxQuery">
						<code><![CDATA[
							def genQuery(td, inPars, outPars):
								return ("scircle(spoint(radians(%%(%s)s), radians(%%(%s)s)),"
									" radians(%%(%s)s)) && p")%(
									base.getSQLKey("RA", inPars["RA"], outPars),
									base.getSQLKey("DEC", inPars["DEC"], outPars),
									base.getSQLKey("SR", inPars["SR"], outPars))
								]]></code>
						</setup>
				</phraseMaker>
			</condDesc>

			<condDesc original="//scs#protoInput">
				<phraseMaker original="//scs#scsPhrase">
					<setup original="proxQuery"/>
				</phraseMaker>
			</condDesc>
		</dbCore>
	</service>

	<regSuite title="constellations regression">
		<regTest title="constellations SCS serves some data">
			<url RA="30"
					DEC="35" SR="3"
				>cone/scs.xml</url>
			<code>
				rows = self.getVOTableRows()
				self.assertEqual(set(r["abbrev"] for r in rows),
					{'AND', 'TRI'})
				poly = rows[0]["p"]
				self.assertTrue(isinstance(poly, pgsphere.SPoly))
				self.assertTrue(len(poly.asDALI())>10)
			</code>
		</regTest>

		<!-- add more tests: extra tests for the web side, custom widgets,
			rendered outputFields... -->
	</regSuite>
</resource>
