<resource schema="magic">

	<meta name="title">MAGIC Public Spectra</meta>
	<meta name="schema-rank">100</meta>
	<meta name="description">
		The MAGIC project observes the VHE sky (GeV~TeV) through Cherenkov
		radiation events.  The project is operating since 2004 and with the support
		from the Spain-VO team they provide data access through a VO-SSAP and web
		services.  This service re-publishes the data with homogeneized in
		flux units (given here in 'erg/(s.cm2)').  Photon energy values in
		are transfomred to frequencies.
	</meta>
	<meta name="creationDate">2016-02-02T12:00:00Z</meta>

	<meta name="_news" author="MD" date="2021-11-25" role="updated">
		Moved the resource to org.gavo.dc, including some updates,
		in particular now returning Spectral Data Model-compliant
		files by default.
	</meta>

	<meta name="subject">gamma-ray-astronomy</meta>
	<meta name="subject">gamma-ray-sources</meta>
	<meta name="subject">spectroscopy</meta>

	<meta name="creator">Carlos Brandt</meta>
	<meta name="instrument">MAGIC</meta>
	<meta name="facility">MAGIC</meta>

	<meta name="source">
		The MAGIC data center, http://magic.pic.es/
	</meta>
	<meta name="contentLevel">Research</meta>
	<meta name="type">Catalog</meta>

	<meta name="coverage">
		<meta name="waveband">Gamma-ray</meta>
		<meta name="profile">AllSky ICRS</meta>
	</meta>


	<!-- =============== -->
	<!--  Table block    -->
	<!-- =============== -->

	<table id="main" onDisk="True" adql="True">
		<mixin
			fluxCalibration="ABSOLUTE"
			fluxUnit="erg.s**-1.cm**-2"
			fluxUCD="phot.flux.density;em.freq"
			spectralUnit="Hz"
			spectralUCD="em.freq"
			>//ssap#hcd</mixin>
		<column name="ssa_reference"
						type="text"
						tablehead="Reference"/>
		<column name="reference_doi"
						type="text"
						tablehead="Article"
						verbLevel="20"/>
		<column name="ebl_corrected" required="True"
						type="smallint"
						verbLevel="10"/>
		<column name="ra_j2000"
						type="double precision"
						unit="deg" ucd="pos.eq.ra"
						tablehead="RA"
						verbLevel="20"
						description="Right Ascension"
						required="True"/>
		<column name="dec_j2000"
						type="double precision"
						unit="deg" ucd="pos.eq.dec"
						tablehead="Dec"
						verbLevel="20"
						description="Declination"
						required="True"/>
	</table>


	<table id="spectrum">
		<mixin
			ssaTable="main"
			fluxDescription="Absolute Flux"
			spectralDescription="Frequency"
			> //ssap#sdm-instance
		</mixin>
		<column name="flux_error"
			ucd="stat.error;phot.flux.density;em.freq"
			unit="erg.s**-1.cm**-2"
			description="Error in absolute flux"/>
	</table>


	<data id="import">

		<property name="previewDir">previews</property>
		<sources pattern='data/*.fits' recurse="False" />

		<fitsProdGrammar hdu="1" qnd="False" hdusField="HDUS">
			<rowfilter procDef="//products#define">
				<bind name="table">"\schema.data"</bind>
				<bind name="accref">\inputRelativePath[:-5]</bind>
				<bind name="path">(\fullDLURL{sdl})[:-5]</bind>
				<bind name="mime">"application/x-votable+xml"</bind>
				<bind name="preview">\standardPreviewPath</bind>
				<bind name="preview_mime">"image/png"</bind>
			</rowfilter>
		</fitsProdGrammar>

		<make table="main">
			<rowmaker idmaps="*">
				<var key="timeext" nullExcs="KeyError">@TOBS</var>

				<map key="reference_doi">@REFURL</map>
				<map key="dec_j2000">@DEC</map>
				<map key="ra_j2000">@RA</map>
				<map key="ebl_corrected">int(@EBL_CORR == 'TRUE')</map>

				<apply name="fixMissingKeys">
					<code>
						try:
							@instrument = @TELESCOP
						except:
							@instrument = 'MAGIC'
					</code>
				</apply>

				<apply name="getSpecCoverage">
					<code>
						cols = @HDUS[1].data
						@minFreq = min(cols["energy"])
						@maxFreq = max(cols["energy"])
					</code>
				</apply>

				<apply procDef="//ssap#setMeta" name="setMeta">
					<bind name="pubDID">\standardPubDID</bind>
					<bind name="dstitle">@OBJECT+'_'+@EXTNAME+'_'+@DATE_OBS</bind>
					<bind name="targname">@OBJECT</bind>
					<bind name="alpha">@RA</bind>
					<bind name="delta">@DEC</bind>
					<bind name="dateObs">@DATE_OBS</bind>
					<bind name="bandpass">"Gamma-ray"</bind>
					<bind name="specstart">LIGHT_C/@maxFreq</bind>
					<bind name="specend">LIGHT_C/@minFreq</bind>
					<bind name="timeExt">@timeext</bind>
					<bind name="length">@NAXIS2</bind>
				</apply>
				<apply procDef="//ssap#setMixcMeta" name="setMixcMeta">
					<bind name="instrument">@instrument</bind>
					<bind name="creator">@AUTHOR</bind>
					<bind name="reference">@REFPAPER</bind>
				</apply>
			</rowmaker>
		</make>

	</data>


	<data id="build_sdm_data" auto="False">

		<embeddedGrammar>
			<iterator>
				<setup>
					<code>
						from gavo.utils import pyfits
						from gavo.protocols import products
					</code>
				</setup>
				<code>
					fitsPath = os.path.join(
						base.getConfig("inputsDir"),
						self.sourceToken["accref"]+".fits")
					hdu = pyfits.open(fitsPath)[1]
					for row in hdu.data:
						yield {
							"spectral": row[0],
							"flux": parseWithNull(row[1], float, nullLiteral=-999.),
							"flux_error": row[3]}
				</code>
			</iterator>
		</embeddedGrammar>

		<make table="spectrum">
			<parmaker>
				<apply procDef="//ssap#feedSSAToSDM"/>
			</parmaker>
		</make>

	</data>

	<service id="sdl" allowed="dlmeta,dlget,static">
		<meta name="title">Magic spectrum datalink</meta>
		<property name="staticData">data</property>

		<datalinkCore>
			<descriptorGenerator procDef="//soda#sdm_genDesc">
				<bind name="ssaTD">"\rdId#main"</bind>
			</descriptorGenerator>
			<dataFunction procDef="//soda#sdm_genData">
  			<bind name="builder">"\rdId#build_sdm_data"</bind>
			</dataFunction>
			<FEED source="//soda#sdm_plainfluxcalib"/>
			<FEED source="//soda#sdm_cutout"/>
			<FEED source="//soda#sdm_format"/>

			<metaMaker semantics="#this">
				<code>
					yield descriptor.makeLinkFromFile(
						"data/"+descriptor.accref.split("/")[-1]+".fits",
						description="Original FITS")
				</code>
			</metaMaker>
		</datalinkCore>
	</service>

	<service id="web" defaultRenderer="form">
		<meta name="shortName">Magic Web</meta>
		<meta name="title">Magic Public Spectra Web Interface</meta>

		<dbCore queriedTable="main">
			<condDesc buildFrom="ssa_location"/>
			<condDesc buildFrom="ssa_dateObs"/>
		</dbCore>

		<outputTable>
			<autoCols>
				ssa_targname, accref, ssa_dateObs, ssa_reference,
				ssa_timeExt, ssa_instrument, ssa_length
			</autoCols>
			<FEED source="//ssap#atomicCoords"/>
			<outputField original="ssa_specstart"
				displayHint="spectralUnit=GeV,sf=0"/>
			<outputField original="ssa_specend"
				displayHint="spectralUnit=GeV,sf=0"/>
			<outputField original="ssa_reference"
					select="array[ssa_reference,reference_doi]">
				<formatter><![CDATA[
					lbl = data[0]
					url = data[1]
					yield T.a(href="%s"%url , target="_blank")["%s"%lbl]
				]]></formatter>
			</outputField>
		</outputTable>

	</service>

	<service id="ssa" allowed="ssap.xml">
		<meta name="shortName">Magic SSAP</meta>
		<meta name="title">Magic Public Spectra</meta>
		<meta name="ssap.dataSource">pointed</meta>
		<meta name="ssap.creationType">archival</meta>
		<meta name="ssap.testQuery">MAXREC=1</meta>

		<publish render="ssap.xml" sets="ivo_managed"/>
		<publish render="form" sets="local,ivo_managed" service="web"/>

		<ssapCore queriedTable="main">
			<FEED source="//ssap#hcd_condDescs"/>
		</ssapCore>

	</service>

	<regSuite title="Translated Magic spectra regression">
		<regTest title="Magic SSA query works">
			<url REQUEST="queryData" TARGETNAME="HESS J1813-178">ssa/ssap.xml</url>
			<code>
				row = self.getFirstVOTableRow()
				self.assertAlmostEqual(row["location_ra"], 273.3629200019562)
				self.assertEqual(row["mime"], "application/x-votable+xml")
				self.assertEqual(row["accref"].split("/")[3:],
					['getproduct', 'magic', 'data', 'MAGIC_2006_HESS1813_SPECTRUM'])
			</code>
		</regTest>

		<regTest title="Magic Dlget returns plausible data">
			<url httpHonorRedirects="True"
				>/getproduct/magic/data/MAGIC_2006_HESS1813_SPECTRUM</url>
			<code>
				rows = self.getVOTableRows()
				self.assertEqual(len(rows), 7)
				self.assertAlmostEqual(rows[0]["flux"], 5.253703314755575E-12)
				self.assertAlmostEqual(rows[-1]["spectral"], 1.9298093898662842E27)
			</code>
		</regTest>

		<regTest title="Magic Dlmeta has FITS link">
			<url ID="ivo://org.gavo.dc/~?magic/data/MAGIC_2006_HESS1813_SPECTRUM">sdl/dlmeta</url>
			<code>
				rows = self.getVOTableRows()
				for r in rows:
					if r["description"]=="Original FITS":
						break
				else:
					raise AssertionError("No original FITS link")

				self.assertTrue(r["access_url"].endswith(
					"/magic/q/sdl/static/MAGIC_2006_HESS1813_SPECTRUM.fits"))
				self.assertEqual(r["semantics"], "#this") # that's a bit questionable
			</code>
		</regTest>

		<regTest title="Magic spectralUnit displayHint honored">
			<url parSet="form" pos_ssa_location="273.3629,-17.8488"
				sr_ssa_location="1">web/form</url>
			<code>
				self.assertHasStrings("7981", "504")
			</code>
		</regTest>

		<regTest title="Magic previews computed">
			<url httpHonorRedirects="True"
				>/getproduct/magic/data/MAGIC_2015_CrabNebula_SPECTRUM_1?preview=True</url>
			<code>
				self.assertHasStrings("PNG", "HB*$W")
			</code>
		</regTest>
	</regSuite>
</resource>
