<resource schema="cns5update" resdir=".">
	<FEED source="cns5/q#globalmeta"/>	
	<meta name="!schema-rank">100</meta>

	<meta name="!title">The Fifth Catalogue of Nearby Stars: Continuously	
		Updated Version (CNS5-updated)</meta>
	<meta name="!description" format="rst">
		The Fifth Catalogue of Nearby Stars (CNS5) aims to provide the most
		volume-complete sample of stars in the solar neighbourhood.  This is
		a continuously-updated version of the published CNS5
		(:bibcode:`2023A&amp;A...670A..19G`).

		The CNS5 is compiled based on trigonometric parallaxes from Gaia DR3 and
		Hipparcos, and supplemented with astrometric data from Spitzer and
		ground-based surveys carried out in the infrared.  The CNS5 catalogue is
		statistically complete down to 19.7 mag in G-band and 11.8 mag in W1-band
		absolute magnitudes, corresponding to a spectral type of L8.

		Continuous updates of observational data for nearby stars from all sources
		were collected and evaluated. For all known stars in the 25 pc sphere
		around the Sun, the best values of positions in space, velocities, and
		magnitudes in different filters are presented.
	</meta>

	<meta name="_related" title="Original CNS5">\internallink{cns5/q/cone/form}</meta>

	<meta name="_news" author="AG" date="2023-10-23" format="rst">
	RV data has been updated with RVs from Gaia DR3.

	Please note that 138 stars with Hipparcos astrometry are missing
	photometric data from Gaia DR3, 2MASS and WISE. These data will be added in
	the near future.
	</meta>

	<meta name="_news" author="AG" date="2024-08-13" format="rst">
	The primary_flag, gj_system_primary information has been corrected for 28
	objects.  cns5_id of the affected objects:

	1450, 1451, 1456, 1457, 2807, 2808, 2946, 3129, 3130, 4218, 4222, 4244, 4247,
	4270, 4271, 4272, 4332, 4333, 5044, 5110, 5111, 5281, 5282, 5459, 5506, 5655,
	5656, 5658.
	</meta>

	<table id="main" onDisk="True" adql="True">
		<stc>
			Position ICRS SPHER3 "ra" "dec" "parallax"
				Velocity "pmra" "pmdec" "rv"
		</stc>
	
		<column name="cns5_id" type="bigint" required="True"
			ucd="meta.id;meta.main"
			tablehead="Id"
			description="CNS5 designation"
			verbLevel="1"/>
		<column name="gj_id" type="text"
			ucd="meta.id.cross"
			tablehead="GJ"
			description="Gliese-Jahreiss number"
			verbLevel="15"/>
		<column name="component_id" type="text"
			ucd="meta.code.multip"
			tablehead="Comp"
			description="Designation of component(s) in binaries or multiple systems.
				This may consist of several letters if a catalogue entry corresponds to
				more than one component that is not split into individual entries."
			verbLevel="1"/>
		<column name="n_components" type="smallint"
			ucd="meta.number"
			tablehead="#C"
			description="Total number of components in the system"
			verbLevel="25">
			<values nullLiteral="-1"/>
		</column>
		<column name="primary_flag" type="smallint"
			ucd="meta.code"
			tablehead="P?"
			description="True for the primary of a multiple system"
			verbLevel="15">
			<values nullLiteral="-1"/>
		</column>
		<column name="gj_system_primary" type="text"
			ucd="meta.id.cross"
			tablehead="GJ_p"
			description="Gliese-Jahriess number of the primary component
				of the system"
			verbLevel="15"/>
		<column name="gaia_dr3_id" type="bigint"
			ucd="meta.id.cross"
			tablehead="DR3 id"
			description="Source identifier in Gaia DR3"
			verbLevel="15">
			<values nullLiteral="-1"/>
		</column>
		<column name="hip_id" type="integer"
			ucd="meta.id.cross"
			tablehead="HIP"
			description="Hipparcos identifier"
			verbLevel="25">
			<values nullLiteral="-1"/>
		</column>

		<column name="ra" type="double precision"
			unit="deg" ucd="pos.eq.ra;meta.main"
			tablehead="RA"
			description="Right ascension"
			verbLevel="1" displayHint="sf=8"/>
		<column name="dec" type="double precision"
			unit="deg" ucd="pos.eq.dec;meta.main"
			tablehead="Dec"
			description="Declination"
			verbLevel="1" displayHint="sf=8"/>
		<column name="epoch" type="double precision"
			unit="yr" ucd="time.epoch"
			tablehead="Epoch"
			description="Reference epoch for coordinates"
			verbLevel="15"/>
		<column name="coordinates_bibcode" type="text"
			ucd="meta.bib;pos.eq"
			tablehead="Src Pos"
			description="Source of the position"
			verbLevel="25" displayHint="type=bibcode"/>

		<column name="parallax" type="double precision"
			unit="mas" ucd="pos.parallax.trig"
			tablehead="ϖ"
			description="Absolute trigonometric parallax"
			verbLevel="1" displayHint="sf=2"/>
		<column name="parallax_error" type="real"
			unit="mas" ucd="stat.error;pos.parallax.trig"
			tablehead="Err. ϖ"
			description="Error in parallax"
			verbLevel="15" displayHint="sf=2"/>
		<column name="parallax_bibcode" type="text"
			ucd="meta.bib;pos.parallax.trig"
			tablehead="Src ϖ"
			description="Source of the parallax"
			verbLevel="25" displayHint="type=bibcode"/>

		<column name="pmra" type="double precision"
			unit="mas/yr" ucd="pos.pm;pos.eq.ra"
			tablehead="PM RA"
			description="Proper motion in right ascension"
			verbLevel="1" displayHint="sf=2"/>
		<column name="pmra_error" type="double precision"
			unit="mas/yr" ucd="stat.error;pos.pm;pos.eq.ra"
			tablehead="Err. PM RA"
			description="Error in pmra"
			verbLevel="15" displayHint="sf=2"/>
		<column name="pmdec" type="double precision"
			unit="mas/yr" ucd="pos.pm;pos.eq.dec"
			tablehead="PM Dec"
			description="Proper motion in declination"
			verbLevel="1" displayHint="sf=2"/>
		<column name="pmdec_error" type="double precision"
			unit="mas/yr" ucd="stat.error;pos.pm;pos.eq.dec"
			tablehead="Err. PM Dec"
			description="Error of pmdec"
			verbLevel="15" displayHint="sf=2"/>
		<column name="pm_bibcode" type="text"
			ucd="meta.bib;pos.pm"
			tablehead="Src. PM"
			description="Source of the proper motion"
			verbLevel="25" displayHint="type=bibcode"/>

		<column name="rv" type="double precision"
			unit="km/s" ucd="phys.veloc;pos.barycenter"
			tablehead="RV"
			description="Spectroscopic radial velocity"
			verbLevel="1" displayHint="sf=2"/>
		<column name="rv_error" type="double precision"
			unit="km/s" ucd="stat.error;phys.veloc;pos.barycenter"
			tablehead="Err. RV"
			description="Error in rv"
			verbLevel="15" displayHint="sf=2"/>
		<column name="rv_bibcode" type="text"
			ucd="meta.bib;phys.veloc;pos.barycenter"
			tablehead="Src. RV"
			description="Source of the radial velocity"
			verbLevel="25" displayHint="type=bibcode"/>

		<column name="g_mag" type="real"
			unit="mag" ucd="phot.mag;em.opt"
			tablehead="G"
			description="G band mean magnitude (corrected)"
			verbLevel="1" displayHint="sf=5"/>
		<column name="g_mag_error" type="double precision"
			unit="mag" ucd="stat.error;phot.mag;em.opt"
			tablehead="Err. G"
			description="Error in g_mag"
			verbLevel="15" displayHint="sf=2,displayUnit=mmag"/>
		<column name="bp_mag" type="real"
			unit="mag" ucd="phot.mag;em.opt.b"
			tablehead="BP"
			description="Gaia DR3 integrated BP mean magnitude"
			verbLevel="1" displayHint="sf=4"/>
		<column name="bp_mag_error" type="double precision"
			unit="mag" ucd="stat.error;phot.mag;em.opt.b"
			tablehead="Err. BP"
			description="Error in bp_mag"
			verbLevel="15" displayHint="sf=2,displayUnit=mmag"/>
		<column name="rp_mag" type="real"
			unit="mag" ucd="phot.mag;em.opt.r"
			tablehead="RP"
			description="Gaia DR3 integrated RP mean magnitude"
			verbLevel="1" displayHint="sf=4"/>
		<column name="rp_mag_error" type="double precision"
			unit="mag" ucd="stat.error;phot.mag;em.opt.r"
			tablehead="Err. RP"
			description="Error in rp_mag"
			verbLevel="15" displayHint="sf=2,displayUnit=mmag"/>

		<column name="g_mag_from_hip" type="double precision"
			unit="mag" ucd="phot.mag;em.opt"
			tablehead="G HIP"
			description="Hipparcos Hp magnitude converted to the G band"
			verbLevel="15" displayHint="sf=2"/>
		<column name="g_mag_from_hip_error" type="double precision"
			unit="mag" ucd="stat.error;phot.mag;em.opt"
			tablehead="Err. G HIP"
			description="Error in g_mag_from_hip"
			verbLevel="25" displayHint="sf=2"/>
		<column name="g_rp_from_hip" type="double precision"
			unit="mag" ucd="phot.color;em.opt.r;em.opt"
			tablehead="G-RP HIP"
			description="G - RP colour computed from Hipparcos V and I"
			verbLevel="15" displayHint="sf=2"/>
		<column name="g_rp_from_hip_error" type="double precision"
			unit="mag" ucd="stat.error;phot.color;em.opt.r;em.opt"
			tablehead="Err. G-RP HIP"
			description="Error in g_rp_from_hip"
			verbLevel="25" displayHint="sf=2"/>

		<column name="g_mag_resulting" type="real"
			unit="mag" ucd="phot.mag;em.opt"
			tablehead="G_r"
			description="Resulting (e.g., deblended) G band magnitude"
			verbLevel="1" displayHint="sf=5"/>
		<column name="g_mag_resulting_error" type="double precision"
			unit="mag" ucd="stat.error;phot.mag;em.opt"
			tablehead="Err. G_r"
			description="Error in g_mag_resulting"
			verbLevel="15" displayHint="sf=2,displayUnit=mmag"/>
		<column name="g_rp_resulting" type="double precision"
			unit="mag" ucd="phot.color;em.opt.r;em.opt"
			tablehead="(G-RP)_r"
			description="Resulting (e.g., deblended) G - RP colour"
			verbLevel="15" displayHint="sf=2"/>
		<column name="g_rp_resulting_error" type="double precision"
			unit="mag" ucd="stat.error;phot.color;em.opt.r;em.opt"
			tablehead="Err. (G-RP)_r"
			description="Error in g_rp_resulting"
			verbLevel="25" displayHint="sf=2"/>
		<column name="g_rp_resulting_flag" type="smallint"
			ucd="meta.code;phot.mag"
			tablehead="G-RP?"
			description="0 – G-RP is deblended; 1 – G-RP is uncorrected vs. DR3;
				2 – G-RP is converted from Hipparcos"
			verbLevel="25">
			<values nullLiteral="-1"/>
		</column>

		<column name="j_mag" type="real"
			unit="mag" ucd="phot.mag;em.ir.j"
			tablehead="J"
			description="2MASS J band magnitude"
			verbLevel="15"/>
		<column name="j_mag_error" type="real"
			unit="mag" ucd="stat.error;phot.mag;em.ir.j"
			tablehead="Err. J"
			description="Error in j_mag"
			verbLevel="25"/>
		<column name="h_mag" type="real"
			unit="mag" ucd="phot.mag;em.ir.h"
			tablehead="H"
			description="2MASS H band magnitude"
			verbLevel="15"/>
		<column name="h_mag_error" type="real"
			unit="mag" ucd="stat.error;phot.mag;em.ir.h"
			tablehead="Err. H"
			description="Error in h_mag"
			verbLevel="25"/>
		<column name="k_mag" type="real"
			unit="mag" ucd="phot.mag;em.ir.k"
			tablehead="Ks"
			description="2MASS Ks band magnitude"
			verbLevel="15"/>
		<column name="k_mag_error" type="real"
			unit="mag" ucd="stat.error;phot.mag;em.ir.k"
			tablehead="Err. Ks"
			description="Error in k_mag"
			verbLevel="25"/>
		<column name="jhk_mag_bibcode" type="text"
			ucd="meta.bib;phot.mag;em.ir"
			tablehead="Src. NIR"
			description="Source of NIR magnitudes"
			verbLevel="25" displayHint="type=bibcode"/>

		<column name="w1_mag" type="real"
			unit="mag" ucd="phot.mag;em.ir.3-4um"
			tablehead="W1"
			description="WISE W1 band magnitude"
			verbLevel="15"/>
		<column name="w1_mag_error" type="real"
			unit="mag" ucd="stat.error;phot.mag;em.ir.3-4um"
			tablehead="Err. W1"
			description="Error in w1_mag"
			verbLevel="25"/>
		<column name="w2_mag" type="real"
			unit="mag" ucd="phot.mag;em.ir.4-8um"
			tablehead="W2"
			description="WISE W2 band magnitude"
			verbLevel="1"/>
		<column name="w2_mag_error" type="real"
			unit="mag" ucd="stat.error;phot.mag;em.ir.4-8um"
			tablehead="Err. W2"
			description="Error in w2_mag"
			verbLevel="25"/>
		<column name="w3_mag" type="real"
			unit="mag" ucd="phot.mag;em.ir.8-15um"
			tablehead="W3"
			description="WISE W3 band magnitude"
			verbLevel="15"/>
		<column name="w3_mag_error" type="real"
			unit="mag" ucd="stat.error;phot.mag;em.ir.8-15um"
			tablehead="Err. W3"
			description="Error in w3_mag"
			verbLevel="25"/>
		<column name="w4_mag" type="real"
			unit="mag" ucd="phot.mag;em.ir.15-30um"
			tablehead="W4"
			description="WISE W4 band magnitude"
			verbLevel="15"/>
		<column name="w4_mag_error" type="real"
			unit="mag" ucd="stat.error;phot.mag;em.ir.15-30um"
			tablehead="Err. W4"
			description="Error in w4_mag"
			verbLevel="25"/>
		<column name="wise_mag_bibcode" type="text"
			ucd="meta.bib;phot.mag;em.ir"
			tablehead="Src. MIR"
			description="Source of MIR magnitudes"
			verbLevel="25" displayHint="type=bibcode"/>

		<column name="p_rot"
			ucd="time.period" unit="d"
			tablehead="T_rot"
			description="Rotational period taken from p_rot_bibcode."
			verbLevel="15"/>
		<column name="p_rot_error"
			ucd="stat.error;time.period" unit="d"
			tablehead="Err. T_rot"
			description="Error on p_rot"
			verbLevel="25"/>
		<column name="p_rot_bibcode" type="text"
			ucd="meta.bib.bibcode;time.period"
			tablehead="Src. T_rot"
			description="Source of p_rot"
			verbLevel="25"/>

		<column name="cns6_system_id" type="bigint" required="True"
			ucd="meta.id.parent"
			tablehead="System id"
			description="CNS6 unique identifier of the stellar system this object
				belongs to."
			verbLevel="15"/>
		<column name="n_planets" type="smallint"
			ucd="meta.number"
			tablehead="#Planets"
			description="Number of confirmed planets associated with the stellar
				system"
			verbLevel="15">
			<values nullLiteral="-1"/>
		</column>
		<column name="reference_object_cns5_id" type="text"
			ucd="meta.id.parent"
			tablehead="Parent"
			description="CNS5_id of the reference object for the object in
				this entry"
			verbLevel="15"/>
		<column name="nexa_id" type="text"
			ucd="meta.id.cross"
			tablehead="NExA"
			description="Identifier of the object in the NExA (NASA Exoplanet
				Archive)"
			verbLevel="15"/>
		<column name="multiplicity_bibcode" type="text"
			ucd="meta.bib.bibcode"
			tablehead="Src. Mult."
			description="Bibcode for literature references documenting system
				multiplicity."
			verbLevel="25"/>

		<column name="remarks" type="text"
			ucd="meta.note"
			tablehead="Remarks"
			description="Additional free-text information about the object, in
				particular warnings about possible mis-identifications or results
				that have been disputed in the literature."
			verbLevel="5"/>
	</table>

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

	<data id="import">
		<sources pattern="data/cns-latest.fits"/>

		<fitsTableGrammar/>

		<make table="main">
			<rowmaker idmaps="*">
				<apply name="map_nans">
					<code>
					for k,v in vars.items():
						if v!=v:
							vars[k] = None
						elif isinstance(v, str) and v.strip()=="":
							vars[k] = None
					</code>
				</apply>
				<map key="multiplicity_bibcode" nullExpr="'?'"/>
				<map key="p_rot_bibcode" nullExpr="'?'"/>
				<map key="nexa_id" nullExpr="''"/>
				<map key="reference_object_cns5_id" nullExpr="''"/>
				<map key="multiplicity_bibcode" nullExpr="''"/>
			</rowmaker>
		</make>
	</data>

	<service id="upload" allowed="form" limitTo="cns">
		<meta name="title">CNS5 Update Upload Facility</meta>
		<meta name="description">Use this service to upload new versions
			of CNS5 as FITS binary tables.  Since this will also trigger an
			import of the data, please have a bit of patience.  This will
			only store one file per day CET, which means that you can overwrite
			your uploads as long as you re-upload on the same day.
		</meta>
		<pythonCore>
			<inputTable>
				<inputKey name="upload" type="file" required="True"
					description="FITS binary of CNS5 update"/>
			</inputTable>
			<outputTable>
				<outputField name="column_name" type="text" tablehead="Name"/>
				<outputField name="min_value" type="text" tablehead="Min"/>
				<outputField name="median" type="text" tablehead="Median"/>
				<outputField name="max_value" type="text" tablehead="Max"/>
				<outputField name="fill_factor" type="text"
					tablehead="Non-Null"/>
			</outputTable>

			<coreProc>
				<setup imports="gavo.user.limits"/>
				<code>
					destPath = rd.getAbsPath(
						datetime.date.today().strftime("data/cns-%Y-%m-%d.fits"))
					with open(destPath, "wb") as f:
						f.write(inputTable.args["upload"][1].read())

					latest = rd.getAbsPath("data/cns-latest.fits")
					os.unlink(latest)
					os.symlink(destPath, latest)

					with base.getWritableAdminConn() as conn:
						rsc.makeData(rd.getById("import"), connection=conn)
						limits.updateForRD(rd, conn)

					with base.getTableConn() as conn:
						res = conn.queryToDicts("SELECT column_name, min_value, median,"
							" max_value, fill_factor from dc.simple_col_stats"
							" where tablename='cns5update.main'")
					return rsc.TableForDef(self.outputTable, rows=list(res))
				</code>
			</coreProc>
		</pythonCore>
	</service>

	<service id="cone" allowed="form,scs.xml">
		<meta name="shortName">CNS5 update</meta>
		<template key="response">res/response.template</template>
		<template key="form">res/response.template</template>

		<meta name="testQuery">
			<meta name="ra">312.27882</meta>
			<meta name="dec">37.4712279</meta>
			<meta name="sr">0.001</meta>
		</meta>

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

		<scsCore queriedTable="main">
			<FEED source="//scs#coreDescs"/>
			<condDesc buildFrom="g_mag_resulting"/>
			<condDesc buildFrom="parallax"/>
			<condDesc buildFrom="cns5_id"/>
			<condDesc buildFrom="gj_id"/>
		</scsCore>
	</service>

	<regSuite title="cns5update regression">
		<regTest title="cns5update SCS serves some data">
			<url RA="312.2788230257468"
					DEC="37.47122796548486" SR="0.001"
					VERB="3"
				>cone/scs.xml</url>
			<code>
				row = self.getFirstVOTableRow()
				self.assertEqual(row['coordinates_bibcode'], '2020yCat.1350....0G')
				self.assertEqual(row['w2_mag_error'], None)
				self.assertAlmostEqual(row['g_mag'], 13.035794258117676)
			</code>
		</regTest>

		<regTest title="cns5update TAP and NULLs">
			<url parSet="TAP" query="SELECT * FROM cns5update.main
				WHERE cns5_id=1078">/tap/sync</url>
			<code>
				row = self.getFirstVOTableRow()
				self.assertEqual(row["n_components"], 2)
				self.assertEqual(row['nexa_id'], '')
				self.assertEqual(row['component_id'], 'A')
				self.assertEqual(row['coordinates_bibcode'], '2020yCat.1350....0G')
			</code>
		</regTest>
	</regSuite>
</resource>
