<resource schema="gedr3auto">
	<meta name="creationDate">2021-01-11T11:09:07Z</meta>
	<meta name="schema-rank">100</meta>

	<meta name="title">Gaia eDR3 Autocorrelation</meta>
	<meta name="description">
		This is a table that simply gives, for each object in Gaia eDR3, the
		identifier of its closest neighbour together with the distance of
		the pair.
	</meta>

	<meta name="doi">10.21938/AxETfJ6uamQiRH9vMLdVTA</meta>

	<meta name="_longdoc" format="rst">
		Determining the wider pairs needs a bit of trickery to avoid excessive
		run times.  If you are curious what we did, see the script embedded
		in `this table's resource descriptor`_.

		.. _this table's resource descriptor: http://svn.ari.uni-heidelberg.de/svn/gavo/hdinputs/gedr3auto/q.rd
	</meta>

	<meta name="subject">surveys</meta>
	<meta name="subject">multiple-stars</meta>
	<meta name="subject">astrometry</meta>

	<meta name="creator">Demleitner, M.</meta>
	<meta name="facility">Gaia</meta>

	<meta name="contentLevel">Research</meta>
	<meta name="type">Catalog</meta>

	<meta name="rights" format="rst"><![CDATA[
		This work has made use of data from the European Space Agency (ESA) mission
		`Gaia <https://www.cosmos.esa.int/gaia>`_, processed by the Gaia Data
		Processing and Analysis Consortium
		(`DPAC <https://www.cosmos.esa.int/web/gaia/dpac/consortium>`_). Funding
		for the DPAC has been provided by national institutions, in particular the
		institutions participating in the Gaia Multilateral Agreement.]]>
		<meta name=
			"rightsURI">https://spdx.org/licenses/CC0-1.0.html</meta>
	</meta>

	<table id="main" onDisk="True" adql="true" primary="source_id">
		<publish sets="ivo_managed,local"/>
		<primary>source_id</primary>

		<index columns="partner_id"/>
		<index columns="dist"/>
	
		<column original="gaia/q3#edr3lite.source_id"/>
		<column name="partner_id" type="bigint" required="True"
			ucd="meta.id.assoc"
			tablehead="Closest"
			description="Identifier of the closest eDR3 object"
			verbLevel="1"/>
		<column name="dist" type="double precision"
			unit="deg" ucd="pos.angDistance"
			tablehead="Dist"
			description="The estimated distance between the two objects.  No attempt
				is made to furnish this with an error estimate."
			verbLevel="1"/>
	</table>

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

			<!-- To keep things quick, we first match with a relatively small
			radius so we don't have to sort too many objects; that'll still
			cover the bulk of the objects.  We then let the index run, and
			after indexing (so source_id lookups are fast) we add lonlier
			objects with increasing match radii. -->

			<script lang="SQL" type="preIndex"
				name="build_pairs"><![CDATA[
set enable_seqscan=False;
set work_mem=2000000;

insert into \schema.main (
	select source_id, partner_id, dist from
		(select source_id, partner_id, dist,
			rank() over (partition by source_id order by dist) as d_rank from (
				select
					a.source_id as source_id, b.source_id as partner_id,
					q3c_dist(a.ra, a.dec, b.ra, b.dec) as dist
				from (
						select * from gaia.edr3lite) as a
					join gaia.edr3lite as b
					on (q3c_join(a.ra, a.dec, b.ra, b.dec, 1/240.)
						and a.source_id!=b.source_id)) as withdist
			 ) as ranked
	where d_rank<2);

do $$BEGIN raise notice '1/240 correlation done, adding index.'; END$$;

ALTER TABLE \schema.main ADD PRIMARY KEY (source_id);

do $$BEGIN raise notice 'indexing done, starting 1/120.'; END$$;

insert into \schema.main (
	select source_id, partner_id, dist from
		(select source_id, partner_id, dist,
			rank() over (partition by source_id order by dist) as d_rank from (
				select
					a.source_id as source_id, b.source_id as partner_id,
					q3c_dist(a.ra, a.dec, b.ra, b.dec) as dist
				from (
					select source_id, ra, dec
					from gaia.edr3lite as ed
						where not exists (
							select 1 from gedr3auto.main as c
							where c.source_id=ed.source_id)
						) as a
					join gaia.edr3lite as b
					on (q3c_join(a.ra, a.dec, b.ra, b.dec, 1/120.)
						and a.source_id!=b.source_id)) as withdist
			 ) as ranked
	where d_rank<2);

do $$BEGIN raise notice '1/120 correlation done, starting 1/60.'; END$$;

insert into \schema.main (
	select source_id, partner_id, dist from
		(select source_id, partner_id, dist,
			rank() over (partition by source_id order by dist) as d_rank from (
				select
					a.source_id as source_id, b.source_id as partner_id,
					q3c_dist(a.ra, a.dec, b.ra, b.dec) as dist
				from (
					select source_id, ra, dec
					from gaia.edr3lite as ed
						where not exists (
							select 1 from gedr3auto.main as c
							where c.source_id=ed.source_id)
						) as a
					join gaia.edr3lite as b
					on (q3c_join(a.ra, a.dec, b.ra, b.dec, 1/60.)
						and a.source_id!=b.source_id)) as withdist
			 ) as ranked
	where d_rank<2);

do $$BEGIN raise notice '1/60 correlation done, starting 1 deg'; END$$;

insert into \schema.main (
	select source_id, partner_id, dist from
		(select source_id, partner_id, dist,
			rank() over (partition by source_id order by dist) as d_rank from (
				select
					a.source_id as source_id, b.source_id as partner_id,
					q3c_dist(a.ra, a.dec, b.ra, b.dec) as dist
				from (
					select source_id, ra, dec
					from gaia.edr3lite as ed
						where not exists (
							select 1 from gedr3auto.main as c
							where c.source_id=ed.source_id)
						) as a
					join gaia.edr3lite as b
					on (q3c_join(a.ra, a.dec, b.ra, b.dec, 1.)
						and a.source_id!=b.source_id)) as withdist
			 ) as ranked
	where d_rank<2);

			]]></script>

		</make>
	</data>

	<regSuite title="gedr3auto regression">
		<regTest title="gedr3auto present">
			<url LANG="ADQL"
				QUERY="select * from gedr3auto.main where source_id=2062595131224249088"
				>/tap/sync</url>
			<code>
				row = self.getFirstVOTableRow()
				self.assertEqual(row["partner_id"], 2062595238604523648)
				self.assertAlmostEqual(row["dist"], 0.0013573657)
			</code>
		</regTest>
	</regSuite>
</resource>
