<?xml version="1.0" encoding="UTF-8"?>
<pipeline
   xmlns="http://glast-ground.slac.stanford.edu/pipeline"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://glast-ground.slac.stanford.edu/pipeline http://glast-ground.slac.stanford.edu/pipeline/schemas/2.0/pipeline.xsd">

   <!--
   This is a pseudo-XML definition of the automated level-1 processing task.

   The overall processing sequence is as follows:

   1. For each chunk of datagrams from a downlink, do the following:
   a. create merged sequence of event+context information
   b. digitize
   c. reconstruct and write out the merit tuple

   2. When all merit tuple chunks are complete, merge the chunks into
   a single ROOT file.

   3. Create FT1 and FT2 files from the merged merit file, and optionally
   FASTCopy them to GSSC

   This task is triggered by the CHS "science dispatcher" application, which 
   prepares a working directory for the task containing chunks of raw CCSDS
   packet data.  Each chunk contains a sequence of LSEP or LSEC datagrams.

   The dispatcher generates a "downlink ID" which is used as the "stream number"
   when the task instance is created.  The downlink ID is a 9-digit number of the
   following form:

   YYMMDDNNN, where YYMMDD is the day the downlink tarball was received, and 
   NNN is the ordinal downlink tarball number in that day.

   The dispatcher passes the following variables into the task:

   downlinkID == the downlink id number described above
   numChunks  == the number of datagram chunks in the downlink
   taskBase   == the absolute path to the base directory for the task,
   such that the input files are located in the directory
		 $taskBase/output/$downlinkID
   productVer == A component of the L1 product file names.  Will be set
   to 0 for the automated initial processing, and must
   be incremented when data is reprocessed/redelivered.
   fastCopy   == Either 0, or the name of the destination to which
   the L1 product files should be sent (usually "GSSC")
   -->

   <task>
      <name>CHS-level1</name>
      <version>
         <major>1</major>
         <minor>0</minor>
      </version>
      <type>Data</type>
      <notation> </notation>
      <prerequisites>
         <prerequisite name="downlinkID" type="integer"/>
         <prerequisite name="numChunks" type="integer"/>
         <prerequisite name="taskBase" type="string"/>
         <prerequisite name="productVer" type="integer"/>
         <prerequisite name="fastCopy" type="string"/>
      </prerequisites>
      <process name="start" type="script">
         <script language="python">
            <![CDATA[
            for i in range(0, numChunks):
               pipeline.createSubstream("doChunk", i+1)
            ]]>
         </script>
         <createsSubtasks>
            <subtask>doChunk</subtask>
         </createsSubtasks>
      </process>

      <process name="mergeMerit" type="batch">
         <job executable="${taskBase}/config/mergeMerit.sh" maxCPU="500">
            <environment>
               <var name="L1_DOWNLINKID">${format(downlinkId, "%09d")}</var>
               <var name="L1_NUMCHUNKS">${numChunks}</var>
               <var name="L1_TASKBASE">${taskBase}</var>
               <var name="L1_PRODUCTVER">${format(productVer, "%02d")}</var>
               <var name="L1_FASTCOPY">${fastCopy}</var>
            </environment>
         </job>
         <depends>
            <after process="doChunk.makeMerit" status="SUCCESS"/>
         </depends>
      </process>

      <process name="makeFITS" type="batch">
         <job executable="${taskBase}/config/makeFITS.sh" maxCPU="500">
            <environment>
               <var name="L1_DOWNLINKID">${format(downlinkId, "%09d")}</var>
               <var name="L1_NUMCHUNKS">${numChunks}</var>
               <var name="L1_TASKBASE">${taskBase}</var>
               <var name="L1_PRODUCTVER">${format(productVer, "%02d")}</var>
               <var name="L1_FASTCOPY">${fastCopy}</var>
            </environment>
         </job>
         <depends>
            <after process="mergeMerit" status="SUCCESS"/>
         </depends>
      </process>

      <task>
         <name>doChunk</name>
         <version>
            <major>1</major>
            <minor>0</minor>
         </version>
         <type>Data</type>
         <notation> </notation>
         <process name="makeEvt" type="batch">
            <job executable="${taskBase}/config/makeEvt.sh" maxCPU="500">
               <environment>
                  <var name="L1_CHUNKID">${format(pipeline.getStream(), "%05d")}</var>
                  <var name="L1_DOWNLINKID">${format(downlinkId, "%09d")}</var>
                  <var name="L1_NUMCHUNKS">${numChunks}</var>
                  <var name="L1_TASKBASE">${taskBase}</var>
                  <var name="L1_PRODUCTVER">${format(productVer, "%02d")}</var>
                  <var name="L1_FASTCOPY">${fastCopy}</var>
               </environment>
            </job>
         </process>

         <process name="makeDigi" type="batch">
            <job executable="${taskBase}/config/makeDigi.sh" maxCPU="1000">
               <environment>
                  <var name="L1_CHUNKID">${format(pipeline.getStream(), "%05d")}</var>
                  <var name="L1_DOWNLINKID">${format(downlinkId, "%09d")}</var>
                  <var name="L1_NUMCHUNKS">${numChunks}</var>
                  <var name="L1_TASKBASE">${taskBase}</var>
                  <var name="L1_PRODUCTVER">${format(productVer, "%02d")}</var>
                  <var name="L1_FASTCOPY">${fastCopy}</var>
               </environment>
            </job>
            <depends>
               <after process="makeEvt" status="SUCCESS"/>
            </depends>
         </process>

         <process name="makeMerit" type="batch">
            <job executable="${taskBase}/config/makeMerit.sh" maxCPU="10000">
               <environment>
                  <var name="L1_CHUNKID">${format(pipeline.getStream(), "%05d")}</var>
                  <var name="L1_DOWNLINKID">${format(downlinkId, "%09d")}</var>
                  <var name="L1_NUMCHUNKS">${numChunks}</var>
                  <var name="L1_TASKBASE">${taskBase}</var>
                  <var name="L1_PRODUCTVER">${format(productVer, "%02d")}</var>
                  <var name="L1_FASTCOPY">${fastCopy}</var>
               </environment>
            </job>
            <depends>
               <after process="makeDigi" status="SUCCESS"/>
            </depends>
         </process>
      </task>
   </task>
</pipeline>