<?xml version="1.0" encoding="UTF-8"?>
<Mission xmlns="Tethys"
       xmlns:Derivation="Tethys/Derivation" 
       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/Derivation http://aosn.mbari.org/tethys/Xml/Derivation.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="Oceans2010Test1">

    <DefineArg Name="UpPitch"><Units:degree/><Value>15.0</Value></DefineArg>

    <DefineArg Name="DownPitch"><Units:degree/><Value>-15.0</Value></DefineArg>

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

    <DefineArg Name="longitudeDelta"><Units:degree/><Value>-0.1</Value></DefineArg>

    <Timeout Duration="P80M"/>

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

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

    <Guidance:OffshoreEnvelope RunIn="Parallel">
        <Set><Guidance:OffshoreEnvelope.minOffshore/><Units:kilometer/><Value>1.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="P10M">
                    <When><Universal:platform_conversation/>
                        <Eq/><False/>
                    </When>
                    <Touch Id="TouchComms"><Universal:platform_communications/></Touch>
                </Timeout>
                <SetUri><Guidance:ReadSensor.uri/><Universal:platform_communications/></SetUri>
            </Guidance:ReadSensor>

            <Guidance:PrepareToDive RunIn="Sequence"/>

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

        </Stack>

    </Stack>

    <Guidance:Buoyancy RunIn="Progression">
        <Set><Guidance:Buoyancy.position/><Units:cubic_centimeter/><Value>300</Value></Set>
    </Guidance:Buoyancy>

    <Stack Id="RunTest">

        <Timeout Duration="P30M"/>

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

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

        <Guidance:DepthEnvelope RunIn="Parallel">
            <Set><Guidance:DepthEnvelope.minDepth/><Units:meter/><Value>5</Value></Set>
        </Guidance:DepthEnvelope>

        <Stack Id="InitialYoYo">

            <Until>
                <Elapsed><Derivation:TempGradientCalculator.targetDepth/></Elapsed>
                <Lt/><Units:second/><Value>1</Value>
            </Until>

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

            <Guidance:Wait RunIn="Sequence"/>

        </Stack>

        <Stack Id="Notify">

            <When>
                <Elapsed><Derivation:TempGradientCalculator.targetDepth/></Elapsed>
                <Lt/><Units:second/><Value>0.4</Value>
            </When>

            <Syslog>Depth=<Universal:depth/><Units:meter/>, Thermocline Depth = <Derivation:TempGradientCalculator.thermoclineDepth/><Units:meter/>, Target depth = <Derivation:TempGradientCalculator.targetDepth/><Units:meter/>
            </Syslog>

        </Stack>

        <Guidance:Pitch Id="PitchDown" RunIn="Parallel">
            <While><Universal:depth/>
                <Lt/><Derivation:TempGradientCalculator.targetDepth/>
            </While>
            <Set><Guidance:Pitch.pitch/><Arg Name="DownPitch"/></Set>
        </Guidance:Pitch>

        <Guidance:Pitch Id="PitchUp" RunIn="Parallel">
            <While><Universal:depth/>
                <Gt/><Derivation:TempGradientCalculator.targetDepth/>
            </While>
            <Set><Guidance:Pitch.pitch/><Arg Name="UpPitch"/></Set>
        </Guidance:Pitch>

        <Guidance:Waypoint RunIn="Sequence">
            <Set><Guidance:Waypoint.latitudeDelta/><Arg Name="latitudeDelta"/></Set>
            <Set><Guidance:Waypoint.longitudeDelta/><Arg Name="longitudeDelta"/></Set>
        </Guidance:Waypoint>

    </Stack>

    <Call RefId="SURFACECOMMS" />

    <Syslog>Done</Syslog>

</Mission>
