<resource schema="bgds">
	<meta name="title">Bochum Galactic Disk Survey BGDS</meta>
	<meta name="description">
		The Bochum Galactic Disk Survey is a project to monitor the stellar content
		of the Galactic disk in a 6 degree wide stripe centered on the Galactic
		plane. The data has been recorded from September 2010 to September 2019
		with the RoBoTT Telecsope at the Universitaetssternwarte Bochum near Cerro
		Armazones in the Chilean Atacama desert. It contains measurements of more
		than 2x10^7 stars. The second and final data release contains follow-up
		observations from January 2017 to September 2019 in Sloan r and i and
		intermittent measurements in Johnson UVB, Sloan z and the narrowbands
		OIII, NB, Halpha and SII.
	</meta>
	<meta name="creationDate">2017-12-02T09:16:00</meta>
	<meta name="schema-rank">20</meta>

	<meta name="subject">surveys</meta>
	<meta name="subject">galaxy-planes</meta>
	<meta name="subject">milky-way-galaxy</meta>
	<meta name="subject">variable-stars</meta>
	<meta name="subject">broad-band-photometry</meta>

	<meta name="creator">Hackstein, M.; Blex, J.; Haas, M.; Fein, C.; Chini, R.</meta>
	<meta name="instrument">Robotic Bochum Twin Telescope (RoBoTT)</meta>
	<meta name="facility">Universitätssternwarte Bochum near Cerro
		Armazones</meta>

	<meta name="source">2015AN....336..590H</meta>
	<meta name="contentLevel">Research</meta>
	<meta name="type">Survey</meta>

	<meta name="copyright" format="rst">
		If you use GDS data, please cite
		:bibcode:`2015AN....336..590H`.
	</meta>

	<meta name="coverage">
		<meta name="waveband">Optical</meta>
	</meta>
	
	<table id="data" onDisk="true" mixin="//siap#pgs">
		<index columns="field"/>
		<index columns="bandpassId"/>
		<index columns="bandpassLo"/>
		<index columns="bandpassHi"/>
		<index columns="dateObs"/>

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

		<mixin
			calibLevel="2"
			collectionName="'BGDS'"
			expTime="exposure"
			facilityName="'RoBoTT'"
			createDIDIndex="True"
			>//obscore#publishSIAP</mixin>

		<column name="field" type="text"
			ucd="meta.id;obs.field"
			tablehead="Field"
			description="Survey field observed."
			verbLevel="15"/>
		<column name="exposure"
			unit="s" ucd="time.duration;obs.exposure"
			tablehead="Exp. time"
			description="Exposure time (per frame)"
			verbLevel="15"/>
		<column name="airmass"
			ucd="obs.airMass"
			tablehead="Airmass"
			description="Airmass at mean epoch"
			verbLevel="21"/>
		<column name="moondist"
			unit="deg" ucd="obs.param"
			tablehead="Moon dist."
			description="Moon distance at mean epoch"
			verbLevel="25"/>
		<column name="pub_did" type="text"
			 ucd="meta.id;meta.main"
			 tablehead="P. DID"
			 description="Dataset identifier assigned by the publisher"
			 verbLevel="15"/>
		<column original="dateObs">
			<values min="55019.0355555556" max="58751.3467824073"/>
		</column>
	</table>

	<coverage>
		<!-- there's a copy of this in bgds/l - manually copy it if this
			changes.-->
		<updater sourceTable="data" mocOrder="4"/>
			<spectral>1.83421e-19 6.62149e-19</spectral>
			<temporal>55019 58751.4</temporal>
			<spatial>3/325,336,338,344-345,448,450-451,454-455,460-461,472,583,589,591-592,594,600,602,613,615,621,623,634,650-653,658-659,662-664,672 4/1309,1311,1348,1350,1356,1358-1359,1384-1385,1388,1798-1799,1810,1824-1825,1828-1829,1831,1852-1853,1866-1867,1892,1894-1897,1900,2325-2327,2355,2361,2363,2372,2374,2449,2451,2457,2459,2481,2483,2489,2496,2498,2504,2506,2528,2530-2531,2587,2590-2591,2595,2597-2599,2616-2618,2620,2631,2642-2643,2645-2647,2660-2661,2672-2673,2676-2677,2692,2696</spatial>
	</coverage>

	<data id="import">
		<property key="previewDir">previews</property>

		<sources recurse="True" pattern="data/gds_big/*.fits.fz">
			<ignoreSources
				pattern="master*"
				fromdb="select accref from \schema.data"/>
		</sources>

		<fitsProdGrammar qnd="False">
			<rowfilter procDef="//products#define">
				<bind key="table">"\schema.data"</bind>
				<bind key="preview_mime">"image/jpeg"</bind>
				<bind key="preview">\splitPreviewPath{.jpeg}</bind>
			</rowfilter>
		</fitsProdGrammar>

		<make table="data">
			<rowmaker>
				<apply procDef="//procs#dictMap">
					<bind name="default">"UNKNOWN"</bind>
					<bind name="mapping">{
						'B': "Johnson B",
						'B_j': "Johnson B",
						'Halpha': "Astrodon Halpha",
						'Ha': "Astrodon Halpha",
						'SII': "Astrodon SII",
						'OII': "Astrodon OIII",
						'OIII': "Astrodon OIII",
						'NB': "Astrodon NB",
						'i': "SDSS i'",
						'i_s': "SDSS i'",
						'g_s': "SDSS g'",
						'r': "SDSS r'",
						'r_s': "SDSS r'",
						'u_s': "SDSS u'",
						'R': "Johnson R",
						'R_j': "Johnson R",
						'U': "Johnson U",
						'U_j': "Johnson U",
						'V_j': "Johnson V",
						'I_j': "Johnson I",
						'V': "Johnson V",
						'z': "SDSS z'",
						'z_s': "SDSS z'",
					}</bind>
					<bind name="key">"FILTER"</bind>
				</apply>

				<apply procDef="//siap#setMeta">
					<bind name="bandpassId">@FILTER.strip()</bind>
					<bind name="dateObs">parseISODT(@DATE_OBS)</bind>
					<bind name="instrument">str(rd.getMeta("instrument"))</bind>
					<bind name="pixflags">"F"</bind>
					<bind name="title">"BGDS %s %s %s"%(
						@TARGET.strip(), @FILTER.strip(), @DATE_OBS)</bind>
				</apply>
				<apply procDef="//siap#computePGS"/>
				<apply procDef="//siap#getBandFromFilter"/>

				<map key="pub_did">\standardPubDID</map>
				<map key="field">@TARGET</map>
				<map key="exposure">@EXPTIME</map>
				<map key="airmass">@AIRMASS</map>
				<map key="moondist">@MOONDIST</map>
			</rowmaker>
		</make>
	</data>

	<service id="dl" allowed="dlget,dlmeta">
		<meta name="title">BGDS Datalink</meta>
		<meta name="description">This service lets you access cutouts
			from BGDS images and retrieve scaled versions.</meta>
		<datalinkCore>
			<descriptorGenerator procDef="//soda#fits_genDesc" name="genFITSDesc">
				<bind key="accrefPrefix">'bgds'</bind>
				<bind key="qnd">False</bind>
				<code>
					# Check for extra, local and magic pseudo-id coming from
					# the time series (l.rd/l2.rd): MAGIC/field/band/mjd
					if pubDID.startswith("MAGIC"):
						# translate to the accref of the closest image
						with base.getTableConn() as conn:
							field, filter, dateObs = pubDID.split("/")[1:]
							if filter.startswith("SDSS ") and not filter.endswith("'"):
								filter = filter+"'"

							dateObs = float(dateObs)
							field = "GDS_"+field

							res = list(conn.query("select accref from \schema.data"
								" where bandpassid=%(filter)s"
								"   and field=%(field)s"
								"   and dateobs between %(dateObs)s-0.007"
								"     and %(dateObs)s+0.007",
								locals()))
							if len(res)!=1:
								raise ValueError("%s matches for %s"%(
									len(res), pubDID))

						pubDID = getStandardPubDID(res[0][0])

					return getFITSDescriptor(pubDID, accrefPrefix, cls=descClass)
				</code>
			</descriptorGenerator>

			<FEED source="//soda#fits_standardDLFuncs" stcs=" "/>
		</datalinkCore>
	</service>

	<dbCore queriedTable="data" id="siacore">
		<condDesc>
			<inputKey original="field" showItems="10">
				<values fromdb="field from \schema.data
					where field!=''
					order by field"/>
			</inputKey>
		</condDesc>
		<condDesc buildFrom="dateObs"/>
		<condDesc>
			<inputKey original="bandpassId">
				<values fromdb="bandpassId from \schema.data order by bandpassId"/>
			</inputKey>
		</condDesc>
		<condDesc original="//siap#protoInput"/>
		<condDesc original="//siap#humanInput"/>
	</dbCore>

	<service id="web" core="siacore">
		<meta name="shortName">bgds web</meta>
		<meta name="_related" title="BGDS SIAP service"
			>/bgds/q/sia/info</meta>
		<meta name="_related" title="BGDS ObsTAP">/tap</meta>
		<meta name="title">Web access to Bochum Galactic Disk Survey (BGDS)
			images</meta>
		<meta name="_intro" format="rst">You can obtain individual cutouts through
			the datalinks given; for mass operations, prefer the Virtual Observatory
			access modes (SIAP_ or the ivoa.obscore table in `our TAP service`_; the
			BGDS collection).

			.. _SIAP: https://dc.g-vo.org/bgds/q/sia/info
			.. _our TAP service: https://dc.g-vo.org/tap
			</meta>
		
		<outputTable>
			<outputField original="accref" tablehead="Image"
				displayHint="type=product"/>
			<outputField name="dlurl" select="accref"
				tablehead="Datalink Access"
				description="URL of a datalink document for the dataset
					(cutouts, different formats, etc)">
				<formatter>
					yield T.a(href=getDatalinkMetaLink(
						rd.getById("dl"), data)
						)["Datalink"]
				</formatter>
				<property name="targetType"
					>application/x-votable+xml;content=datalink</property>
				<property name="targetTitle">Datalink</property>
			</outputField>
			<outputField original="dateObs" displayHint="type=humanDate"/>
			<outputField original="bandpassId"/>
			<outputField name="plateCenter" type="text"
				select="array[centeralpha,centerdelta]"
				tablehead="Plate Center"
				description="Plate Canter RA and Dec">
				<formatter>
					return "%.2f,%+.2f"%(data[0], data[1])
				</formatter>
			</outputField>
			<outputField original="imageTitle"/>
		</outputTable>
	</service>

	<service id="sia" core="siacore" allowed="siap.xml">
		<publish render="siap.xml" sets="ivo_managed"/>
		<publish render="form" sets="ivo_managed,local" service="web"/>
		<meta name="shortName">bgds sia</meta>
		<meta name="title">Bochum Galactic Disk Survey (BGDS) images</meta>
	
		<meta name="sia.type">Pointed</meta>
		<meta name="testQuery">
			<meta>
				pos.ra:250.54
				pos.dec:-46.22
				size.ra: 0.01
				size.dec: 0.01
			</meta>
		</meta>
	</service>

	<regSuite title="BGDS functionality">
		<regTest title="BGDS web returns plausible info.">
			<url parSet="form" dateObs="2017-11-24T05:36:25 +/- 0.01"
				bandpassId="Astrodon Halpha" field="GDS_0644-0035">web/form</url>
			<code>
				self.assertHasStrings(
					# datalink link
					"?ID=ivo%3A%2F%2Forg.gavo.dc%2F~%3Fbgds%2Fdata%2Fgds_big%2FGDS_0644-0035%2FHa%2Feq030000ms%2F20171123.comb_avg.0001.fits.fz",
					# filter name properly mapped, title generation
					"BGDS GDS_0644-0035 Astrodon Halpha 2017-11-24T05:36:25",
					# filter selector generated
					">SDSS i'&lt;")
			</code>
		</regTest>

		<regTest title="BGDS datalink returns a cutout">
			<url
				CIRCLE="101 0.2 0.05"
				ID="ivo://org.gavo.dc/~?bgds/data/gds_big/GDS_0644-0035/Ha/eq030000ms/20171123.comb_avg.0001.fits.fz"
				>/bgds/q/dl/dlget</url>
			<code>
				from gavo.utils import pyfits
				from io import BytesIO
				hdus = pyfits.open(BytesIO(self.data))
				hdr = hdus[1].header
				self.assertEqual(hdr["NAXIS1"], 451)
				self.assertEqual(hdr["FILTER"], 'Halpha')
				self.assertTrue(abs(hdus[1].data[0][0]-129)&lt;1,
					"BGDS cutout values?")
			</code>
		</regTest>

		<regTest title="BGDS also accepts magic pubdids from the time series">
			<url
				CIRCLE="101 0.2 0.05"
				ID="MAGIC/0644-0035/Astrodon Halpha/58081.23362268507"
				>/bgds/q/dl/dlget</url>
			<code>
				from gavo.utils import pyfits
				from io import BytesIO
				hdus = pyfits.open(BytesIO(self.data))
				hdr = hdus[1].header
				self.assertEqual(hdr["NAXIS1"], 451)
				self.assertEqual(hdr["FILTER"], 'Halpha')
				self.assertTrue(abs(hdus[1].data[0][0]-129)&lt;1,
					"BGDS cutout values?")
			</code>
		</regTest>

		<regTest title="BGDS SIAP works, and preview is available">
			<url MAXREC="1" POS="101,-0.2" SIZE="1,1">sia/siap.xml</url>
			<code>
				row = self.getFirstVOTableRow()
				self.assertTrue(row["exposure"] is not None, "Empty exposure time?")
				previewData = utils.urlopenRemote(row["accref"]+"?preview=True").read()
				self.assertTrue(b"JFIF" in previewData, "No preview?")
			</code>
		</regTest>
	</regSuite>
</resource>
