<resource schema="hppunion">

	<meta name="creationDate">2016-07-29T14:00:00</meta>
	<meta name="schema-rank">100</meta>
	<meta name="title">GAVO Historical Photographic Plate Archive HPPA</meta>

	<meta name="subject">astrophotography</meta>
	<meta name="subject">history-of-astronomy</meta>

	<meta name="creator">GAVO Data Center</meta>

	<meta name="description" format="rst">
		GAVO's historical photographic plate archive (GHHPA) is a
		collection of various digitized historical photographic
		plates.  It currently exposes:
		
		* the scans of plates of selected Kapteyn special fields obtained
		  at Potsdam
		* the Palomar-Leiden Trojan surveys, 1960-1977,
		* a collection of plates obtained at Boyden Station, South Africa,
		  kept at various German observatories.

		Other plate collections kept by GAVO include the Heidelberg
		Digitized Astronomical Plates HDAP,
		ivo://org.gavo.dc/lswscans/res/positions/siap, and the APPLAUSE
		database from Potsdam.
	</meta>

	<table id="main" mixin="//siap#pgs" onDisk="True" adql="True">
		<property name="forceStats">True</property>

		<meta name="_associatedDatalinkService">
			<meta name="serviceId">dl</meta>
			<meta name="idColumn">pub_did</meta>
		</meta>

		<column name="object" type="text"
			tablehead="Target Object"
			description="Object being observed, Simbad-resolvable form"
			ucd="meta.id" verbLevel="5"/>
		<column name="pub_did" type="text"
			ucd="meta.ref.url"
			tablehead="Pub. DID"
			description="Dataset identifier assigned by the publisher."
			verbLevel="5"/>
		<column name="datalink_url" type="text"
			tablehead="Datalink"
			description="URL of a datalink document (cutout service, ancillary
				data) for this dataset.">
			<property name="targetType"
				>application/x-votable+xml;content=datalink</property>
			<property name="targetTitle">Datalink</property>
		</column>

		<viewStatement>
			CREATE VIEW \qName AS
				SELECT \reSub{, datalink_url}{}{\colNames},
					'\getConfig{web}{serverURL}/kapteyn/q/dl/dlmeta?ID='
					|| gavo_urlescape(pub_did) as datalink_url FROM kapteyn.plates
				UNION ALL
				SELECT \colNames FROM boydende.data
				UNION ALL
				SELECT \reSub{, datalink_url}{}{\colNames},
					accref AS datalink_url FROM plts.data
				UNION ALL
				SELECT \colNames FROM flare_survey.data
		</viewStatement>
	</table>

	<coverage>
		<updater sourceTable="main"/>
	</coverage>

	<data id="import">
		<make table="main"/>
	</data>

	<service id="dl" allowed="dlmeta">
		<meta name="title">HPPIA Datalink</meta>
		<meta name="description">This is just a dispatcher to the datalink
			services on the individual services.</meta>
		<datalinkCore>
			<descriptorGenerator procDef="//datalink#fromtable">
				<bind name="idColumn">"pub_did"</bind>
				<bind name="tableName">"\schema.main"</bind>
				<code>
					descriptor = FromrowDescriptor.fromDB(pubDID, tableName, idColumn)
					dest_url = descriptor.metadata["datalink_url"]
					if dest_url is None:
						raise svcs.NotFound(f"No datalink known for {pubDID}")
					raise svcs.Found(dest_url)
				</code>
			</descriptorGenerator>
		</datalinkCore>
	</service>

	<service id="im" allowed="form,siap.xml">
		<meta name="shortName">hpp_web</meta>
		<meta name="title">Historical Photographic Plate Image Archive</meta>
		<meta name="sia.type">Pointed</meta>

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

		<dbCore id="hppunion_core" queriedTable="main">
		<condDesc original="//siap#protoInput"/>
		<condDesc original="//siap#humanInput"/>
		<condDesc buildFrom="dateObs"/>
		<condDesc buildFrom="bandpassId"/>
		<condDesc>
			<inputKey name="object" type="text"
					tablehead="Target Object"
					description="Object being observed, Simbad-resolvable form"
					ucd="meta.id" verbLevel="5" required="True" showItems="10">
					<values fromdb="object FROM hppunion.main order by object"/>
			</inputKey>
		</condDesc>
		</dbCore>

		<outputTable>
			<LOOP listItems="accref accsize dateObs object
					centerAlpha centerDelta imageTitle">
				<events>
					<outputField original="\item"/>
				</events>
			</LOOP>
			<outputField original="bandpassLo" displayHint="displayUnit=nm"/>
			<outputField original="bandpassHi" displayHint="displayUnit=nm"/>
			<outputField original="pixelScale"
				displayHint="displayUnit=arcsec/pix"/>
		</outputTable>
	</service>

	<regSuite title="Hppunion regression">
		<regTest title="hppunion SIAP plausible">
			<url POS="231.02,-58.75" SIZE="0.1,0.1" dateObs="38256.72/38256.73"
				>im/siap.xml</url>
			<code>
				row = self.getFirstVOTableRow()
				self.assertEqual(row["object"], "Circinus Field I")
				self.assertTrue(row["accref"].endswith(
					"boydende/data/fits/HAR081_007020.fits"), "accref wrong")
				self.assertTrue(row["accref"].startswith("http://"),
					"accref not morphed")
				self.assertTrue(row["datalink_url"].endswith(
					"/boydende/q/dl/dlmeta?ID="
					"ivo%3A//org.gavo.dc/~%3Fboydende/data/fits/HAR081_007020.fits"),
					"datalink URL wrong.")
			</code>
		</regTest>

	</regSuite>

	<regSuite title="Hppunion datalink operation" sequential="True">
		<regTest title="Hppunion kapteyn has datalink">
			<url POS="287.04944,15.0499" SIZE="0.1,0.1" dateObs="18885/18886"
				>im/siap.xml</url>
			<code>
				row = self.getFirstVOTableRow()
				self.assertEqual(row["instId"],
					"Zeiss Triplet 15 cm Potsdam-Telegrafenberg")
				self.assertTrue("/kapteyn/q/dl/dlmeta?ID=" in row["datalink_url"])

				# redirect URL to local site
				self.followUp.url.content_ = row["datalink_url"]
			</code>
		</regTest>

		<regTest title="Hppunion kapteyn datalink resolves">
			<url>overridden in the previous test</url>
			<code>
				bySemantics = dict((r["semantics"], r) for r in self.getVOTableRows())
				self.assertEqual(
					bySemantics["#this"]["description"],
					"The full dataset.")
				self.assertEqual(
					bySemantics["#preview"]["content_type"],
					"image/jpeg")
				self.assertEqual(
					bySemantics["#preview-image"]["description"],
					"Low-res photo with plate borders.")
				self.assertEqual(
					bySemantics["#proc"]["service_def"],
					"proc_svc")
			</code>
		</regTest>

		<regTest title="Hppunion datalink service forwards">
			<url
				httpHonorRedirects="True"
				ID="ivo://org.gavo.dc/~?flare_survey/data/plates/ESO040_004362.fits"
				>dl/dlmeta</url>
			<code>
				bySemantics = dict((r["semantics"], r)
					for r in self.getVOTableRows())
				self.assertEqual(bySemantics["#preview"]["access_url"],
					EqualingRE(r"http.*/getproduct/flare_survey/data/plates/ESO040_004362.fits\\?preview=True"))
			</code>
		</regTest>

	</regSuite>
</resource>
