<?xml version="1.0" encoding="UTF-8"?>
<Mission xmlns="Tethys"
       xmlns:Control="Tethys/Control"
       xmlns:Guidance="Tethys/Guidance" 
       xmlns:Trigger="Tethys/Trigger" 
       xmlns:Units="Tethys/Units"
       xmlns:Universal="Tethys/Universal"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="Tethys http://aosn.mbari.org/tethys/Xml/Tethys.xsd
                           Tethys/Control http://aosn.mbari.org/tethys/Xml/Control.xsd
                           Tethys/Guidance http://aosn.mbari.org/tethys/Xml/Guidance.xsd
                           Tethys/Trigger http://aosn.mbari.org/tethys/Xml/Trigger.xsd
                           Tethys/Units http://aosn.mbari.org/tethys/Xml/Units.xsd
                           Tethys/Universal http://aosn.mbari.org/tethys/Xml/Universal.xsd"
       Id="track">

<!-- The following arguments are input arguments -->

    <DefineArg Name="CenterLat"><Units:degree/><Value>0.0</Value></DefineArg>

    <DefineArg Name="CenterLon"><Units:degree/><Value>0.0</Value></DefineArg>

    <DefineArg Name="Northings1"><Units:meter/><Value>1732</Value></DefineArg>

    <DefineArg Name="Eastings1"><Units:meter/><Value>1000</Value></DefineArg>

    <DefineArg Name="Northings2"><Units:meter/><Value>0</Value></DefineArg>

    <DefineArg Name="Eastings2"><Units:meter/><Value>2000</Value></DefineArg>

    <DefineArg Name="Northings3"><Units:meter/><Value>-1732</Value></DefineArg>

    <DefineArg Name="Eastings3"><Units:meter/><Value>1000</Value></DefineArg>

    <DefineArg Name="Northings4"><Units:meter/><Value>-1732</Value></DefineArg>

    <DefineArg Name="Eastings4"><Units:meter/><Value>-1000</Value></DefineArg>

    <DefineArg Name="Northings5"><Units:meter/><Value>0</Value></DefineArg>

    <DefineArg Name="Eastings5"><Units:meter/><Value>-2000</Value></DefineArg>

    <DefineArg Name="Northings6"><Units:meter/><Value>1732</Value></DefineArg>

    <DefineArg Name="Eastings6"><Units:meter/><Value>-1000</Value></DefineArg>

    <DefineArg Name="YoYoMinDepth"><Units:meter/><Value>3.0</Value></DefineArg>

    <DefineArg Name="YoYoMaxDepth"><Units:meter/><Value>18.0</Value></DefineArg>

    <DefineArg Name="YoYoUpPitch"><Units:degree/><Value>20.0</Value></DefineArg>

    <DefineArg Name="YoYoDownPitch"><Units:degree/><Value>-20.0</Value></DefineArg>

    <DefineArg Name="SampleISUS"><Units:bool/><Value>1</Value></DefineArg>

    <DefineArg Name="NeedCommsTime"><Units:minute/><Value>60</Value></DefineArg>

    <DefineArg Name="Speed"><Units:meter_per_second/><Value>1</Value></DefineArg>

    <DefineArg Name="CenterComms"><Units:bool/><Value>1</Value></DefineArg>

    <DefineArg Name="Repeat"><Units:bool/><Value>0</Value></DefineArg>

<!-- The following arguments are output arguments -->

    <DefineArg Name="PeakDepthChl"><Units:meter/><Value>0</Value></DefineArg>

    <DefineArg Name="PeakChl"><Units:microgram_per_liter/><Value>0</Value></DefineArg>

    <DefineArg Name="PeakDepthNO3"><Units:meter/><Value>0</Value></DefineArg>

    <DefineArg Name="PeakNO3"><Units:micromole_per_liter/><Value>0</Value></DefineArg>

<!-- The timeout -->

    <Timeout Duration="P1440M"/>

<!-- And now the mission -->

    <Guidance:AltitudeEnvelope RunIn="Parallel">
        <Set><Guidance:AltitudeEnvelope.minAltitude/><Units:meter/><Value>7</Value></Set>
    </Guidance:AltitudeEnvelope>

    <Guidance:DepthEnvelope RunIn="Parallel">
        <Set><Guidance:DepthEnvelope.maxDepth/><Units:meter/><Value>65</Value></Set>
    </Guidance:DepthEnvelope>

    <Guidance:OffshoreEnvelope RunIn="Parallel">
        <Set><Guidance:OffshoreEnvelope.minOffshore/><Units:kilometer/><Value>2.0</Value></Set>
        <Set><Guidance:OffshoreEnvelope.maxOffshore/><Units:kilometer/><Value>20.0</Value></Set>
    </Guidance:OffshoreEnvelope>

    <Stack Id="SurfaceComms" >

        <Guidance:GoToSurface RunIn="Progression"/>

        <Stack>

            <Preemptive><True/></Preemptive>

            <Guidance:SetSpeed RunIn="Parallel">
                <Set><Guidance:SetSpeed.speed/><Units:meter_per_second/><Value>0</Value></Set>
            </Guidance:SetSpeed>

            <Guidance:ReadSensor RunIn="Sequence">
                <SetUri><Guidance:ReadSensor.uri/><Universal:latitude_fix/></SetUri>
            </Guidance:ReadSensor>

            <Guidance:ReadSensor RunIn="Sequence">
                <Timeout Duration="P30M">
                    <Touch Id="TouchComms"><Universal:platform_communications/></Touch>
                </Timeout>
                <SetUri><Guidance:ReadSensor.uri/><Universal:platform_communications/></SetUri>
            </Guidance:ReadSensor>

            <Guidance:ReadSensor RunIn="Sequence">
                <SetUri><Guidance:ReadSensor.uri/><Universal:latitude_fix/></SetUri>
            </Guidance:ReadSensor>

        </Stack>

    </Stack>

<!-- GPS Update.  Don't go too long w/o a GPS fix. -->

    <Stack Id="NeedComms">

        <When><Universal:platform_pitch_angle/>
            <Gt/><Units:degree/><Value>0</Value>
            <And>
                <Elapsed><Universal:time_fix/></Elapsed>
                <Gt/><Arg Name="NeedCommsTime"/>
            </And>
            <Or>
                <Elapsed><Universal:time_fix/></Elapsed>
                <Gt/><Units:minute/><Value>65</Value>
            </Or>
        </When>

        <Call refId="SurfaceComms"/>

    </Stack>

<!-- See if the ISUS should get turned on -->

    <Guidance:ReadSensor RunIn="Parallel">
        <While><Arg Name="SampleISUS"/>
            <Eq/><True/>
        </While>
        <Set><Guidance:ReadSensor.uri/><Universal:mole_concentration_of_nitrate_in_sea_water/></Set>
    </Guidance:ReadSensor>

<!-- If we're executing here or below, lets' get science data! -->

    <Insert Filename="Missions/Science.xml"/>

    <Guidance:Buoyancy RunIn="Parallel">
        <Set><Guidance:Buoyancy.position/><Units:cubic_centimeter/><Value>100</Value></Set>
    </Guidance:Buoyancy>

    <Guidance:Pitch RunIn="Parallel">
        <Set><Guidance:Pitch.massPosition/><Units:centimeter/><Value>0</Value></Set>
    </Guidance:Pitch>

    <Guidance:SetSpeed RunIn="Parallel">
        <Set><Guidance:SetSpeed.speed/><Arg Name="Speed"/></Set>
    </Guidance:SetSpeed>

    <Guidance:DepthEnvelope RunIn="Parallel">
        <Set><Guidance:DepthEnvelope.minDepth/><Arg Name="YoYoMinDepth"/></Set>
        <Set><Guidance:DepthEnvelope.maxDepth/><Arg Name="YoYoMaxDepth"/></Set>
        <Set><Guidance:DepthEnvelope.downPitch/><Arg Name="YoYoDownPitch"/></Set>
        <Set><Guidance:DepthEnvelope.upPitch/><Arg Name="YoYoUpPitch"/></Set>
    </Guidance:DepthEnvelope>

    <Guidance:YoYo RunIn="Parallel">
        <Set><Guidance:YoYo.downPitch/><Arg Name="YoYoDownPitch"/></Set>
        <Set><Guidance:YoYo.upPitch/><Arg Name="YoYoUpPitch"/></Set>
    </Guidance:YoYo>

    <Stack  Id="PeakDetectChl">

        <When><True/>
        </When>

        <Trigger:PeakDetect RunIn="Sequence">
            <Set><Trigger:PeakDetect.detect/><Universal:mass_concentration_of_chlorophyll_in_sea_water/></Set>
            <OutputArg Name="PeakDepthChl"><Trigger:PeakDetect.peakIndependent/></OutputArg>
            <OutputArg Name="PeakChl"><Trigger:PeakDetect.peakDetect/></OutputArg>
        </Trigger:PeakDetect>

        <Shorelog><Arg Name="PeakDepthChl"/>
        </Shorelog>

        <Shorelog><Arg Name="PeakChl"/>
        </Shorelog>

    </Stack>

    <Stack Id="PeakDetectNO3">

        <When><Arg Name="SampleISUS"/>
            <Eq/><True/>
        </When>

        <Trigger:PeakDetect RunIn="Sequence">
            <Set><Trigger:PeakDetect.detect/><Universal:mole_concentration_of_nitrate_in_sea_water/></Set>
            <OutputArg Name="PeakDepthNO3"><Trigger:PeakDetect.peakIndependent/></OutputArg>
            <OutputArg Name="PeakNO3"><Trigger:PeakDetect.peakDetect/></OutputArg>
        </Trigger:PeakDetect>

        <Shorelog><Arg Name="PeakDepthNO3"/>
        </Shorelog>

        <Shorelog><Arg Name="PeakNO3"/>
        </Shorelog>

    </Stack>

    <Guidance:Waypoint Id="Center0" RunIn="Sequence">
        <Until><Arg Name="CenterLat"/>
            <Eq/><Units:degree/><Value>0</Value>
            <Or><Arg Name="CenterLon"/>
                <Eq/><Units:degree/><Value>0</Value>
            </Or>
        </Until>
        <Set><Guidance:Waypoint.latitude/><Arg Name="CenterLat"/></Set>
        <Set><Guidance:Waypoint.longitude/><Arg Name="CenterLon"/></Set>
    </Guidance:Waypoint>

    <Call refId="SurfaceComms"/>

    <Stack Id="Butterfly">

        <Until><Arg Name="CenterLat"/>
            <Eq/><Units:degree/><Value>0</Value>
            <Or><Arg Name="CenterLon"/>
                <Eq/><Units:degree/><Value>0</Value>
            </Or>
        </Until>

        <Guidance:Point Id="Point1" RunIn="Sequence">
            <Set><Guidance:Point.latitude/><Arg Name="CenterLat"/></Set>
            <Set><Guidance:Point.longitude/><Arg Name="CenterLon"/></Set>
            <Set><Guidance:Point.northingsDelta/><Arg Name="Northings1"/></Set>
            <Set><Guidance:Point.eastingsDelta/><Arg Name="Eastings1"/></Set>
        </Guidance:Point>

        <Call refId="SurfaceComms"/>

        <Guidance:Point Id="Point2" RunIn="Sequence">
            <Set><Guidance:Point.latitude/><Arg Name="CenterLat"/></Set>
            <Set><Guidance:Point.longitude/><Arg Name="CenterLon"/></Set>
            <Set><Guidance:Point.northingsDelta/><Arg Name="Northings2"/></Set>
            <Set><Guidance:Point.eastingsDelta/><Arg Name="Eastings2"/></Set>
        </Guidance:Point>

        <Call refId="SurfaceComms"/>

        <Guidance:Waypoint Id="Center2" RunIn="Sequence">
            <Set><Guidance:Waypoint.latitude/><Arg Name="CenterLat"/></Set>
            <Set><Guidance:Waypoint.longitude/><Arg Name="CenterLon"/></Set>
        </Guidance:Waypoint>

        <Stack>

            <Until><Arg Name="CenterComms"/>
                <Eq/><False/>
            </Until>

            <Call refId="SurfaceComms"/>

        </Stack>

        <Guidance:Point Id="Point5" RunIn="Sequence">
            <Set><Guidance:Point.latitude/><Arg Name="CenterLat"/></Set>
            <Set><Guidance:Point.longitude/><Arg Name="CenterLon"/></Set>
            <Set><Guidance:Point.northingsDelta/><Arg Name="Northings5"/></Set>
            <Set><Guidance:Point.eastingsDelta/><Arg Name="Eastings5"/></Set>
        </Guidance:Point>

        <Call refId="SurfaceComms"/>

        <Guidance:Point Id="Point6" RunIn="Sequence">
            <Set><Guidance:Point.latitude/><Arg Name="CenterLat"/></Set>
            <Set><Guidance:Point.longitude/><Arg Name="CenterLon"/></Set>
            <Set><Guidance:Point.northingsDelta/><Arg Name="Northings6"/></Set>
            <Set><Guidance:Point.eastingsDelta/><Arg Name="Eastings6"/></Set>
        </Guidance:Point>

        <Call refId="SurfaceComms"/>

        <Guidance:Waypoint Id="Center6" RunIn="Sequence">
            <Set><Guidance:Waypoint.latitude/><Arg Name="CenterLat"/></Set>
            <Set><Guidance:Waypoint.longitude/><Arg Name="CenterLon"/></Set>
        </Guidance:Waypoint>

        <Stack>

            <Until><Arg Name="CenterComms"/>
                <Eq/><False/>
            </Until>

            <Call refId="SurfaceComms"/>

        </Stack>

        <Guidance:Point Id="Point3" RunIn="Sequence">
            <Set><Guidance:Point.latitude/><Arg Name="CenterLat"/></Set>
            <Set><Guidance:Point.longitude/><Arg Name="CenterLon"/></Set>
            <Set><Guidance:Point.northingsDelta/><Arg Name="Northings3"/></Set>
            <Set><Guidance:Point.eastingsDelta/><Arg Name="Eastings3"/></Set>
        </Guidance:Point>

        <Call refId="SurfaceComms"/>

        <Guidance:Point Id="Point4" RunIn="Sequence">
            <Set><Guidance:Point.latitude/><Arg Name="CenterLat"/></Set>
            <Set><Guidance:Point.longitude/><Arg Name="CenterLon"/></Set>
            <Set><Guidance:Point.northingsDelta/><Arg Name="Northings4"/></Set>
            <Set><Guidance:Point.eastingsDelta/><Arg Name="Eastings4"/></Set>
        </Guidance:Point>

        <Call refId="SurfaceComms"/>

        <Guidance:Waypoint Id="Center4" RunIn="Sequence">
            <Set><Guidance:Waypoint.latitude/><Arg Name="CenterLat"/></Set>
            <Set><Guidance:Waypoint.longitude/><Arg Name="CenterLon"/></Set>
        </Guidance:Waypoint>

        <Stack>

            <Until><Arg Name="CenterComms"/>
                <Eq/><False/>
            </Until>

            <Call refId="SurfaceComms"/>

        </Stack>

    </Stack>

    <Stack Id="RepeatIf" >

        <Until><Arg Name="Repeat"/>
            <Eq/><Units:bool/><Value>0</Value>
            <Or><Arg Name="CenterLat"/>
                <Eq/><Units:degree/><Value>0</Value>
            </Or>
            <Or><Arg Name="CenterLon"/>
                <Eq/><Units:degree/><Value>0</Value>
            </Or>
        </Until>

        <Stack Id="Repeat" Repeat="2">

            <Call refId="Cross"/>

        </Stack>

    </Stack>

    <Stack>

        <Until><Arg Name="CenterComms"/>
            <Eq/><True/>
        </Until>

        <Call refId="SurfaceComms"/>

    </Stack>

</Mission>
