<!-- A template for a simple DaCHS SIAP (image search) service.
To fill it out, search and replace %.*%

Note that this doesn't expose all features of DaCHS.  For advanced
projects, you'll still have to read documentation... -->
\tpldesc{Image collections via SIAP2 and TAP}

<resource schema="\resdir" resdir=".">
  <meta name="creationDate">\now</meta>

  \commonmeta
  <table id="main" onDisk="True" adql="True">
    <mixin have_bandpass_id="True">//siap2#pgs</mixin>
    <!-- Comment in the following element once you're ready to publish;
      it will add your data to the obscore view
    <mixin preview="NULL">//obscore#publishObscoreLike</mixin>
    -->
    %add non-standard columns if you think they are useful in discovery %
  </table>

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

  <!-- if you have data that is continually added to, consider using
    updating="True" and an ignorePattern here; see also howDoI.html,
    incremental updating -->
  <data id="import">
    <sources pattern="%resdir-relative pattern, like data/*.fits%"/>

    <!-- the fitsProdGrammar should do it for whenever you have
    halfway usable FITS files.  If they're not halfway usable,
    consider running a processor to fix them first – you'll hand
    them out to users, and when DaCHS can't deal with them, chances
    are their clients can't either -->
    <fitsProdGrammar>
      <rowfilter procDef="//products#define">
        <bind key="table">"\\schema.main"</bind>
      </rowfilter>
    </fitsProdGrammar>

    <make table="main">
      <rowmaker>
        <!-- put vars here to pre-process FITS keys that you need to
          re-format in non-trivial ways. -->

        <apply procDef="//siap2#computePGS"/>

        <apply procDef="//siap2#setMeta">
          <!-- if you have an optical filter known (or translatable) to
            what DaCHS knows (dachs adm dump data/filters.txt), enter
            it here and don't bind em_min and em_max.  Otherwise, remove
            this and set em_min and em_max. -->
          <bind name="bandpassId">"%a bandpass id known to DaCHS%"</bind>

          <!-- DaCHS can deal with some time formats; otherwise, you
            may want to use parseTimestamp(@DATE_OBS, '%Y %m %d...');
            you can also set t_min and t_max instead.  If you don't
            you also have to fill t_exposure-->
          <bind key="dateObs">%something like @DATE_OBS%</bind>

          <!-- the remaining parameters are from obscore.  Refer to
            the apply doc http://docs.g-vo.org/dachs/ref.html#siap2-setmeta
            or the obscore REC for more information on it in case of doubt.
            You can remove most of these bindings without becoming
            invalid; DaCHS will complain if you hit something vital. -->

           <bind key="dataproduct_type">"image"</bind>
          <bind key="dataproduct_subtype">None</bind>
          <bind key="calib_level">2</bind>
          <bind key="obs_collection">"%short and spiffy%"</bind>

          <!-- DaCHS will fill in obs_publisher_did here if you keep it None,
            which is probably what you normally want -->
          <bind key="obs_id">None</bind>

          <!-- titles are what users usually see in a selection, so
            try to combine band, dateObs, object..., like
            "MyData {} {} {}".format(@DATE_OBS, @TARGET, @FILTER) -->
          <bind key="obs_title">"%expressive expression%"</bind>
          <bind key="obs_publisher_did">\\standardPubDID</bind>
          <bind key="obs_creator_did">None</bind>
          <bind key="target_name">%expr for the target name%</bind>
          <bind key="target_class">"%vocabulary term%"</bind>

          <!-- forget about t_min and t_max for single exposures when you
            have dateObs and t_exposure -->
          <bind key="t_min">%expr for start of obs%</bind>
          <bind key="t_max">%expr for end of obs%</bind>
          <bind key="t_exptime">%expr for exposure time%</bind>
          <bind key="t_resolution">None</bind>

          <!-- forget about em_min and em_max when you have bandpassId-->
          <bind key="em_min">%expr for lower wl [m]%</bind>
          <bind key="em_max">%expr for upper wl [m]%</bind>
          <bind key="em_res_power">None</bind>
          <bind key="em_ucd">None</bind>
          <bind key="o_ucd">"phot.count"</bind>
          <bind key="pol_states">None</bind>
          <bind key="facility_name"
            >base.getMetaText(targetTable, "facility", default=None)</bind>
          <bind key="instrument_name"
            >base.getMetaText(targetTable, "instrument", default=None)</bind>

          <!-- s_xel are set by computePGS -->
          <bind key="t_xel">1</bind>
          <bind key="em_xel">1</bind>
          <bind key="pol_xel">None</bind>
        </apply>

        <!-- remove this unless you set bandpassId above -->
        <apply procDef="//siap2#getBandFromFilter"/>

        <!-- any custom columns need to be mapped here; do *not* use
          idmaps="*" with SIAP2 -->
      </rowmaker>
    </make>
  </data>

  <!-- if you want to build an attractive form-based service from
    SIAP, you probably want to tinker here quite a bit; if you
    don't want to serve a browser service, just remove the following
    element. -->
  <service id="browse" allowed="form">
    <meta name="shortName">%short name for the browser service%</meta>
    <meta name="title">Browser interface to %whatever%</meta>
    <dbCore queriedTable="main">
      <condDesc original="//siap2#humanInput"/>
      <condDesc>
        <inputKey original="target_name" showItems="10" multiplicity="multiple">
          <values fromdb="target_name from \\schema.main order by target_name"/>
        </inputKey>
      </condDesc>
      <outputTable autoCols="target_name, access_estsize">
        <column original="access_url"/>
        <column original="s_ra" displayHint="type=hms"/>
        <column original="s_dec" displayHint="type=dms"/>
      </outputTable>
    </dbCore>
  </service>

  <service id="i" allowed="form,siap2.xml">
    <meta name="shortName">%up to 16 characters%</meta>

    <!-- other sia.types: Cutout, Mosaic, Atlas -->
    <meta name="sia.type">Pointed</meta>

    <meta name="testQuery.pos.ra">%ra one finds an image at%</meta>
    <meta name="testQuery.pos.dec">%dec one finds an image at%</meta>
    <meta name="testQuery.size.ra">0.1</meta>
    <meta name="testQuery.size.dec">0.1</meta>

    <!-- this is the VO publication -->
    <publish render="siap2.xml" sets="ivo_managed"/>
    <!-- this puts the service on the root page -->
    <publish render="form" sets="local,ivo_managed" service="browse"/>
    <!-- all publish elements only become active after you run
      dachs pub q -->

    <dbCore queriedTable="main">
      <FEED source="//siap2#parameters"/>
      <!-- enable custom parameters like this:
        <condDesc buildFrom="dateObs"/> -->
    </dbCore>
  </service>

  <regSuite title="\resdir regression">
    <!-- see http://docs.g-vo.org/DaCHS/ref.html#regression-testing
      for more info on these. -->

    <regTest title="\resdir SIAP serves some data">
      <url POS="%CIRCLE ra dec size that has a bit of data%"
        >i/siap2.xml</url>
      <code>
        <!-- to figure out some good strings to use here, run
          dachs test -D tmp.xml q
          and look at tmp.xml -->
        self.assertHasStrings(
          "%some characteristic string returned by the query%",
          "%another characteristic string returned by the query%")
      </code>
    </regTest>

    <!-- add more tests: image actually delivered, form-based service
      renders custom widgets, etc. -->
  </regSuite>
</resource>
