<resource schema="cdfspect">
  <meta name="creationDate">2015-09-22T13:00:00</meta>
  <meta name="schema-rank">1000</meta>
  <meta name="title">Optical Spectroscopy in the CDFS</meta>
  <meta name="creator">Szokoly, G. P.; Bergeron, J.; Hasinger, G.;
    Lehmann, I.; Kewley, L.; Mainieri, V.; Nonino, M.; Rosati, P.; Giacconi, R.;
    Gilli, R.; Gilmozzi, R.; Norman, C.; Romaniello, M.; Schreier, E.;
    Tozzi, P.; Wang, J. X.; Zheng, W.; Zirm, A.</meta>
  <meta name="description" format="rst">
    These spectra are optical follow-up observations of the
    X-ray sources detected in the 942 ks exposure of the
    Chandra Deep Field-South (CDFS) using FORS.
  </meta>

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

  <meta name="source">2004ApJS..155..271S</meta>
  <meta name="type">Survey</meta>
  <meta name="facility">VLT, European Southern Observatory, Chile</meta>
  <meta name="coverage.waveband">Optical</meta>

  <table id="data" onDisk="true">
    <mixin instrument="FORS"
      creationType="archival"
      creator="Szokoly, G. P."
      dataSource="survey"
      fluxCalibration="ABSOLUTE"
      reference="2004ApJS..155..271S"
      spectralCalibration="ABSOLUTE"
      spectralResolution="5e-10"
      fluxUnit="erg.cm**-2.s**-1.A**-1"
      spectralUnit="Angstrom">//ssap#hcd</mixin>
    <meta name="_associatedDatalinkService">
     	<meta name="serviceId">sdl</meta>
     	<meta name="idColumn">ssa_pubDID</meta>
    </meta>

    <FEED source="//ssap#obscore-time-index"/>
  </table>

  <coverage>
  	<updater sourceTable="data"/>
 		<spectral>1.80586e-19 6.62149e-19</spectral>
 		<temporal>51845.1 52262.2</temporal>
 		<spatial>6/35935</spatial>
  </coverage>

  <table id="spectrum">
		<mixin ssaTable="data"
			fluxDescription="Calibrated flux"
			spectralDescription="Wavelength"
			>//ssap#sdm-instance</mixin>
	</table>

  <data id="import">
    <property key="previewDir">previews</property>
    <sources pattern="data/cdf_*.fits"/>
    <fitsProdGrammar qnd="True">
      <rowfilter procDef="//products#define">
        <bind name="table">"\schema.data"</bind>
        <bind name="mime">"application/fits"</bind>
        <bind name="preview">\standardPreviewPath</bind>
        <bind name="preview_mime">"image/png"</bind>
      </rowfilter>
    </fitsProdGrammar>

    <make table="data">
      <rowmaker idmaps="ssa_*" id="build_data">
        <var name="specVal">float(@CRVAL1)</var>
        <var name="specPix">float(@CRPIX1)</var>
        <var name="specDel">float(@CDELT1)</var>
        <var name="specLen">int(@NAXIS1)</var>

        <var name="specMin">(@specVal+(1-@specPix)*@specDel)*1e-10</var>
        <var name="specMax">(@specVal+(@specLen-@specPix)*@specDel)*1e-10</var>

        <apply procDef="//ssap#setMeta">
          <bind name="dstitle">"%s %s"%(@TELESCOP, vars["ESO OBS ID"])</bind>
          <bind name="pubDID">\standardPubDID</bind>
          <bind name="targname">"ICRS %s %s"%(@RA, @DEC)</bind>
          <bind name="alpha">@RA</bind>
          <bind name="delta">@DEC</bind>
          <bind name="cdate">@DATE</bind>
          <bind name="bandpass">"IR, Optical, UV"</bind>
          <bind name="targclass">"X"</bind>
          <bind name="bandpass">"IR, Optical, UV"</bind>
          <bind name="dateObs">@DATE_OBS</bind>
          <bind name="timeExt">@EXPTIME</bind>
          <bind name="length">@specLen</bind>
          <bind name="specext">@specMax-@specMin</bind>
          <bind name="specend">@specMax</bind>
          <bind name="specstart">@specMin</bind>
          <bind name="specmid">(@specMax+@specMin)/2.</bind>
        </apply>
      </rowmaker>
    </make>
  </data>

  <data id="build_sdm_data">
    <embeddedGrammar>
      <iterator>
        <setup>
          <code>
            from gavo.protocols import products
            from gavo.utils import pyfits
            from gavo.utils import fitstools
          </code>
        </setup>
        <code>
        fitsPath = products.RAccref.fromString(
            self.sourceToken["accref"]).localpath
        hdus = pyfits.open(fitsPath)
        hdr = hdus[0].header
        # take out brokenness from header (or should we fix it in
        # the data itself?
        del hdr["CD1_1"]
        del hdr["CROTA1"]
        ax = fitstools.WCSAxis.fromHeader(hdr, 1)

        for spec, flux in enumerate(hdus[0].data):
          yield {"spectral": ax.pix0ToPhys(spec), "flux": flux}
        hdus.close()
        </code>
      </iterator>
    </embeddedGrammar>
    <make table="spectrum">
      <parmaker>
        <apply procDef="//ssap#feedSSAToSDM"/>
      </parmaker>
    </make>
  </data>

  <service id="sdl" allowed="dlget,dlmeta">
    <meta name="title">CDFS Datalink Service</meta>

    <datalinkCore>
      <descriptorGenerator procDef="//soda#sdm_genDesc">
        <bind name="ssaTD">"\rdId#data"</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"/>
    </datalinkCore>
  </service>

  <service id="web" defaultRenderer="form">
		<meta name="shortName">CDFS Web</meta>
		<meta name="title">CDFS Public Spectra Web Interface</meta>
		<meta name="_related" title="CDFS SSAP"
			>\internallink{cdfspect/q/ssa/info}</meta>

		<dbCore queriedTable="data">
			<condDesc buildFrom="ssa_location"/>
			<condDesc buildFrom="ssa_dateObs"/>
			<condDesc>
				<inputKey original="mime">
					<property name="defaultForForm">application/x-votable+xml</property>
					<values>
						<option title="SDM VOTable">application/x-votable+xml</option>
						<option title="1D FITS image">application/fits</option>
					</values>
				</inputKey>
			</condDesc>
		</dbCore>

		<outputTable>
			<autoCols>accref, mime, ssa_targname,
				ssa_aperture, ssa_dateObs</autoCols>
			<FEED source="//ssap#atomicCoords"/>
			<outputField original="ssa_specstart" displayHint="displayUnit=Angstrom"/>
			<outputField original="ssa_specend" displayHint="displayUnit=Angstrom"/>
		</outputTable>
	</service>

  <service id="ssa" allowed="form,ssap.xml">
		<meta name="shortName">CDFS SSAP</meta>
		<meta name="ssap.dataSource">pointed</meta>
		<meta name="ssap.testQuery">MAXREC=1</meta>
		<meta name="ssap.creationType">archival</meta>
		<meta name="ssap.complianceLevel">query</meta>

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

		<ssapCore queriedTable="data">
			<property name="previews">auto</property>
			<FEED source="//ssap#hcd_condDescs"/>
		</ssapCore>
	</service>

  <regSuite title="CDFS spectra">
			<regTest title="CDFS SSA interface works">
				<url REQUEST="queryData"
					PUBDID="ivo://org.gavo.dc/~?cdfspect/data/cdf_001a_036.fits"
					>ssa/ssap.xml</url>
        <code>
          rows = self.getVOTableRows()
          self.assertEqual(len(rows), 1)
          self.assertEqual(rows[0]['accsize'], 43200)
          self.assertEqual(rows[0]['ssa_dstitle'],
            'ESO-VLT-U1 200102563')
        </code>
			</regTest>

      <regTest title="CDFS preview">
  			<url preview="True">/getproduct/cdfspect/data/cdf_001a_036.fits</url>
  			<code>
  				self.assertHasStrings("PNG", "0PLTE", "uuuWWW000")
  			</code>
  		</regTest>
    </regSuite>

    <regSuite title="Datalink">
      <regTest title="Datalink Meta">
        <url ID="ivo://org.gavo.dc/~?cdfspect/data/cdf_001a_036.fits"
        	>sdl/dlmeta</url>
        <code><![CDATA[
          self.assertHasStrings(
          	'<MIN value="3',
            '<MAX value="1')
        ]]></code>
      </regTest>

      <regTest title="Datalink format conversion">
        <url ID="ivo://org.gavo.dc/~?cdfspect/data/cdf_001a_036.fits"
            FORMAT="application/x-votable+xml"
        	>sdl/dlget</url>
        <code><![CDATA[
          self.assertHasStrings('2004ApJS..155..271S',
          	'AAAAAAAAAECnlsOuv/YUAAAA')
        ]]></code>

      </regTest>
    </regSuite>

</resource>
