<resource schema="casa_lines">
	<meta name="creationDate">2022-02-24T13:25:12Z</meta>
	<meta name="schema-rank">500</meta>

	<meta name="title">Splatalogue CASA LineTAP</meta>
	<meta name="description" format="rst">
		This is a rendering of the `CASA Offline Splatalogue`_ in
		a draft LineTAP, mainly intended to enable verification of the standard.
		This is not recommended for production yet.  In particular, many
		InChIs are known wrong, and we have skipped several hundred lines
		because we did have no InChIs for their species at all.

		.. _CASA Offline Splatalogue: https://safe.nrao.edu/wiki/bin/view/ALMA/CASA_Offline_Splat_list
	</meta>
	<meta name="subject">spectral-line-identification</meta>
	<meta name="subject">atomic-physics</meta>
	<meta name="subject">molecular-physics</meta>

	<meta name="creator">Brogan, C.; Remijan, A. J. ; Markwick-Kemper, A.</meta>

	<meta name="source">2007AAS...21113211R</meta>
	<meta name="contentLevel">Research</meta>
	<meta name="type">Catalog</meta>

	<meta name="coverage.waveband">Radio</meta>
	<meta name="coverage.waveband">Millimeter</meta>
	<meta name="coverage.waveband">Infrared</meta>

	<table id="line_tap" adql="True">
		<meta name="shortName">Spltlg radio</meta>
		<publish/>
		<mixin>//linetap#table-0</mixin>
		<column name="nrao_formula" type="text"
			ucd="meta.id;phys.mol"
			tablehead="NRAO formula"
			description="Chemical formula in NRAO's argot."/>
		<column name="common_name" type="text"
			ucd="meta.id;phys.mol"
			tablehead="Common Name"
			description="Common name of the molecule."/>
		<column name="quantnr" type="text"
			ucd="phys.atmol.configuration"
			tablehead="Quantum Numbers"
			description="Resolved Quantum Number as per
				http://www.cv.nrao.edu/php/splat/QuantumCode.html"/>
		<column name="dipole_moment"
			unit="D"
			tablehead="Dipole moment"
			description="Molecule dipole moment Sij μ²"/>
	</table>

	<coverage>
		<updater spectralTable="line_tap"/>
	</coverage>

	<data id="import">
		<sources pattern="data/*.tsv"/>

		<reGrammar topIgnoredLines="1" fieldSep="\t"
			names="species, ignored, chem_name, restfreq, quantnr,
			intensity, dipole_moment, einstein_a, e_lower_cm, e_lower, e_upper, src"/>

		<make table="line_tap">
			<rowmaker idmaps="*">
				<simplemaps>nrao_formula: species, common_name: chem_name</simplemaps>
				<apply name="compute_inchis">
					<setup imports="gavo.protocols.linetap,gavo.api">
						<par name="KNOWN_SPECIES">{}</par>
						<code>
							# we identify species by their formula.  That's not
							# foolproof but good enough for this particular application.
							with open(
									rd.getAbsPath("res/species-key.txt"),
									encoding="utf-8") as src:
								for ln in src:
									try:
										formula, human, _, inchi, inchiKey, molw =\
											ln.split("\t")

										# if you ever add to the following code, go use
										# a proper inchi parser.
										if '/q+1' in inchi:
											charge = 1
										elif '/q-1' in inchi:
											charge = -1
										else:
											charge = 0

										KNOWN_SPECIES[formula
											] = human, inchi, inchiKey, molw, charge
									except ValueError:
										raise Exception(f"Bad species-key: {ln}")
						</code>
					</setup>
					<code>
						@commmon_name, @inchi, @inchikey, @molweight, @charge =\
							KNOWN_SPECIES[@species]
					</code>
				</apply>

				<apply procDef="//procs#dictMap">
					<bind name="key">"src"</bind>
					<bind name="mapping">{
						"CDMS": "2005JMoSt.742..215M",
						"JPL": "1998JQSRT..60..883P",
						"SLAIM":
							"https://physics.nist.gov/cgi-bin/micro/table5/start.pl",
					}</bind>
				</apply>

				<apply procDef="//linetap#populate-0">
					<bind name="einstein_a">10**float(@einstein_a)</bind>
					<bind name="inchi">@inchi</bind>
					<bind name="inchikey">@inchikey</bind>
					<bind name="ion_charge">@charge</bind>
					<bind name="line_reference">@src</bind>
<!--					<bind name="line_strength">float(@intensity) or None</bind>
					<bind name="oscillator_strength">None</bind> -->

					<bind name="lower_energy">float(@e_lower)*BOLTZMANN_K</bind>
					<bind name="upper_energy">float(@e_upper)*BOLTZMANN_K</bind>

					<bind name="mass_number">@molweight</bind>
					<bind name="method">None</bind>

					<bind name="title">@species+" "+@quantnr</bind>
					<bind name="vacuum_wavelength"
						>LIGHT_C/(float(@restfreq)*1e9)*1e10</bind>
				</apply>
				<map key="quantnr">@quantnr.replace("&amp;Omega", "Ω")</map>
			</rowmaker>
		</make>
	</data>

	<regSuite title="casa_lines regression">
		<regTest title="CASA lines present">
			<url LANG="ADQL" QUERY="SELECT * FROM casa_lines.line_tap
				WHERE inchi='InChI=1S/NO/c1-2' AND
					vacuum_wavelength BETWEEN gavo_specconv(2.03, 'GHz', 'Angstrom')
						AND gavo_specconv(2.01, 'GHz', 'Angstrom')">/tap/sync</url>
			<code>
				rows = self.getVOTableRows()
				self.assertEqual(len(rows), 1)
				row = rows[0]
				self.assertEqual(row["title"],
					'NO J=11/2,&amp;Omega=1/2,F=11/2+-9/2-')
				self.assertAlmostEqual(row["vacuum_wavelength"]/1480361547., 1)
				self.assertEqual(row["mass_number"], 30)
				self.assertAlmostEqual(row["upper_energy"], 1.1632014767066e-21)
				self.assertEqual(row["inchikey"], "MWUXSHHQAYIFBG-UHFFFAOYSA-N")
				self.assertEqual(row["line_reference"], "1998JQSRT..60..883P")
				self.assertAlmostEqual(row["einstein_a"], 5.772979296482345e-16)
			</code>
		</regTest>
	</regSuite>
</resource>
