<?xml version="1.0" encoding="utf-8"?>

<resource schema="apo">
	<meta name="creationDate">2007-08-22T13:00:00</meta>
	<meta name="schema-rank">1000</meta>
	<meta name="title">Apache Point observations of lensed quasars</meta>
	<meta name="description">Observations of the lensed quasar Q2237+0305
	performed between 1995 and 1998.</meta>

	<meta name="subject">quasars</meta>
	<meta name="subject">observational-cosmology</meta>
	<meta name="subject">strong-gravitational-lensing</meta>

	<meta name="_related" title="Federated Lens Image Search"
		>\internallink{/lensunion/q/im/form}</meta>

  <meta name="coverage.waveband">Optical</meta>

  <property name="datapack-extrafiles">["res/filters.txt"]</property>

	<table id="frames" onDisk="True" mixin="//siap#pgs">
		<register services="lensunion/q#im"/>
		<meta name="servedBy" ivoId="ivo://org.gavo.dc/tap"
			>GAVO DC ObsCore</meta>
		<stc>Position FK4 "alphaRaw" "deltaRaw"</stc>
		<mixin
			facilityName="'Apache Point Observatory'"
			collectionName="'APO Q2237 run'"
			targetClass="'GravLens'"
			targetName="object"
			sResolution="0.5"
			calibLevel="2"
			t_exptime="exposure"
			did="pub_did">//obscore#publishSIAP</mixin>

		<column name="type" type="text"
			tablehead="Obs. Type"/>
		<column name="object" type="text"
			tablehead="Target Object"/>
		<column name="raw_object" type="text"
			tablehead="Target Object (Header)"/>
		<column name="filter1" type="text"
			tablehead="Filter 1" description="Filters on filter wheel 1"/>
		<column name="filter2" type="text"
			tablehead="Filter 2" description="Filters on filter wheel 2"/>
		<column name="exposure" tablehead="Exp. time"/>
		<column name="alphaRaw" type="text" description=
			"RA of center of view 1950.0"/>
		<column name="deltaRaw" type="text"
			description="Dec of center of view 1950.0"/>
		<column name="pub_did" type="text"
			description="Publisher DID for this dataset."/>
	</table>

	<coverage>
		<updater sourceTable="frames"/>
		<spectral>2.73993e-19 3.25647e-19</spectral>
		<temporal>49871.4 50825.1</temporal>
		<spatial>5/902,2554,4804 6/3614,3632-3633,3635-3636,10187,10190,10209,10211-10212,10214,10220,11584,19205,19207</spatial>
	</coverage>

	<rowmaker id="fitsToFrames">
		<var name="OBJECT">re.sub("([^_]*)_.*", r"\1", @OBJECT)</var>
		<var name="telescope">@OBSERVAT+" "+@TELESCOP</var>
		<var name="dateObs">parseDate(@UTDATE, "%d/%m/%y"
					)+parseTime(@UT)</var>
		<var name="cleanedObject">@OBJECT.split("_")[0]</var>

		<apply procDef="//procs#mapValue" name="mapFilter1">
			<bind key="sourceName">"apo/res/filters.txt"</bind>
			<bind key="logFailures">True</bind>
			<bind key="failuresAreNone">True</bind>
			<bind key="destination">"filter1"</bind>
			<bind key="value">vars["FILTER1"]</bind>
		</apply>
		<apply procDef="//procs#mapValue" name="mapFilter2">
			<bind key="sourceName">"apo/res/filters.txt"</bind>
			<bind key="logFailures">True</bind>
			<bind key="failuresAreNone">True</bind>
			<bind key="destination">"filter2"</bind>
			<bind key="value">vars["FILTER2"]</bind>
		</apply>

		<apply id="buildfilter">
			<!-- an application since it needs mapValued stuff-->
			<code>
				vars["filter"] = "+".join([
					f for f in [vars["filter1"], vars["filter2"]]
					if f and f!='EMPTY'])
			</code>
		</apply>

		<apply procDef="//procs#mapValue" name="mapObject">
			<bind key="sourceName">"maidanak/res/maydanak_targets.txt"</bind>
			<bind key="logFailures">True</bind>
			<bind key="destination">"ARI_OBJC"</bind>
			<bind key="value">vars["cleanedObject"]</bind>
		</apply>

		<apply procDef="//siap#computePGS"/>

		<apply procDef="//siap#setMeta">
			<bind key="title">vars["cleanedObject"]+" "+str(vars["DATE_OBS"])</bind>
			<bind key="instrument">"%s %s"%(
				vars["TELESCOP"], vars["OBSERVAT"])</bind>
			<bind key="dateObs">@dateObs</bind>
			<bind key="bandpassId">vars["filter"]</bind>
		</apply>

		<apply procDef="//siap#getBandFromFilter"/>

		<simplemaps>object:ARI_OBJC,
			raw_object:OBJECT,
			alphaRaw:RA, deltaRaw:DEC, exposure:EXPOSURE
			</simplemaps>
		<idmaps>filter*</idmaps>
		
		<map dest="type">"SCIENCE"</map>

		<!-- pub_did is mainly for testing Identifiers 2.0 resolution with
		served-by relationships.  If you do this yourself, just use
		the standardPubDID macro. -->
		<map key="pub_did">"ivo://org.gavo.dc/apo/res/apo/frames?"+\inputRelativePath</map>
	</rowmaker>

	<data id="import">
		<sources pattern="cd/*.fits" recurse="True"/>
		<fitsProdGrammar qnd="True">
			<rowfilter procDef="//products#define">
				<bind key="table">"apo.frames"</bind>
				<bind key="owner">"apo"</bind>
				<bind key="embargo">"2005-12-31"</bind>
			</rowfilter>
			<ignoreOn>
				<keyIs key="CD1_1" value="0.0" type="real"/>
			</ignoreOn>
		</fitsProdGrammar>

		<make table="frames" rowmaker="fitsToFrames"/>
		<recreateAfter>lensunion/q#import</recreateAfter>
	</data>

	<service id="q" allowed="form,siap.xml">
		<meta name="description">This is a service mainly intended for
			playing around.  Preferably use the lensunion to access this data.
		</meta>
		<dbCore id="query_images" queriedTable="frames">
			<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">
						<values fromdb="object FROM lensunion.main"/>
				</inputKey>
			</condDesc>
		</dbCore>
		<outputTable autoCols="*">
			<column original="accref">
				<formatter>
					fullURL = base.makeAbsoluteURL("/getproduct/"+data)
					if queryMeta["format"]=="HTML":
						return T.a(href=fullURL, onmouseover="insertPreview(this)")[
							utils.makeEllipsis(fullURL.split("/")[-1], 40)]
					else:
						return fullURL
				</formatter>
			</column>
		</outputTable>
	</service>

	<regSuite title="apo regression">
			<regTest title="apo TAP response looks plausible">
				<url parSet="TAP"
					QUERY="SELECT * FROM apo.frames WHERE
					1=intersects(circle(340.12,3.37, 0.1),
					coverage) and dateobs between 49871.438 and 49871.439">/tap/sync</url>
				<code>
					rows = self.getVOTableRows()
					self.assertEqual(len(rows), 1)
					row = rows[0]
					self.assertEqual(row["exposure"], 15.0)
					self.assertEqual(row["object"], "Q2237+0305")
				</code>
			</regTest>
	</regSuite>
</resource>
