<?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://okeanids.mbari.org/tethys/Xml/Tethys.xsd
                           Tethys/Control http://okeanids.mbari.org/tethys/Xml/Control.xsd
                           Tethys/Guidance http://okeanids.mbari.org/tethys/Xml/Guidance.xsd
                           Tethys/Trigger http://okeanids.mbari.org/tethys/Xml/Trigger.xsd
                           Tethys/Units http://okeanids.mbari.org/tethys/Xml/Units.xsd
                           Tethys/Universal http://okeanids.mbari.org/tethys/Xml/Universal.xsd"
       Id="trackStarOil">

    <Description>
        Vehicle tracks patch identified by oil concentration.
    </Description>

<!-- Vehicle safety parameters -->

    <DefineArg Name="MinAltitude"><Description>
        Minimum altitude for the entire mission.
    </Description><Units:meter/><Value>5</Value></DefineArg>

    <DefineArg Name="MaxDepth"><Description>
        Maximum depth for the entire mission.
    </Description><Units:meter/><Value>65</Value></DefineArg>

    <DefineArg Name="MinWaterDepth"><Description>
        Minimum water depth for the entire mission.
    </Description><Units:meter/><Value>10</Value></DefineArg>

    <DefineArg Name="MinOffshore"><Description>
        Minimum offshore distance for the entire mission.
    </Description><Units:meter/><Value>2000</Value></DefineArg>

    <DefineArg Name="NeedCommsTime"><Description>
        Maximum time between surfacings for communications
    </Description><Units:minute/><Value>240</Value></DefineArg>

    <DefineArg Name="MissionTimeout"><Description>
        Timeout for the entire mission
    </Description><Units:day/><Value>9</Value></DefineArg>

    <DefineArg Name="MassDefault"><Description>
        Static setting for mass during the mission. Defaults to massDefault
        setting in the Config/Control.cfg file.
    </Description><Control:VerticalControl.massDefault/></DefineArg>

    <DefineArg Name="BuoyancyNeutral"><Description>
        Buoyancy bladder position while performing the YoYo behavior. Defaults
        to buoyancyNeutral setting in the Config/Control.cfg file.
    </Description><Control:VerticalControl.buoyancyNeutral/></DefineArg>

<!-- Basic guidance settings -->

    <DefineArg Name="Depth"><Description>
        Depth during sampling.
    </Description><Units:meter/><Value>3.0</Value></DefineArg>

    <DefineArg Name="Speed"><Description>
        Vehicle speed.
    </Description><Units:meter_per_second/><Value>1</Value></DefineArg>

    <DefineArg Name="WpMaxDistance"><Description>
        Maximum length of each tracking leg
    </Description><Units:meter/><Value>7500</Value></DefineArg>

    <DefineArg Name="WpTimeout"><Description>
        Maximum duration of each tracking leg
    </Description><Units:second/><Value>8000</Value></DefineArg>

    <DefineArg Name="WaterFrame"><Description>
        Set WaterFrame true to drive towards waypoints in the water frame of
        reference
    </Description><True/></DefineArg>

    <DefineArg Name="Repeat"><Description>
        Maximum number of tracking legs
    </Description><Units:count/><Value>200</Value></DefineArg>

<!-- Constant settings for each tracking leg -->

    <DefineArg Name="Turn"><Description>
        Amount to turn at each leg 144 degrees for a pentagram 154.286 degrees
        for a heptagram
    </Description><Units:degree/><Value>144</Value></DefineArg>

    <DefineArg Name="TwoPi"><Description>
        Applied to stop run-up of angles
    </Description><Units:degree/><Value>360</Value></DefineArg>

    <DefineArg Name="FilterWidth"><Description>
        Width of boxcar filter used in horizontal patch detection
    </Description><Units:count/><Value>200</Value></DefineArg>

    <DefineArg Name="BeginThreshold"><Description>
        If non-NaN, sets threshold for starting horizontal patch detection
    </Description><Units:kilogram_per_cubic_meter/><Value>0.001</Value></DefineArg>

    <DefineArg Name="OffPeakFraction"><Description>
        When filtered horizontal value is this fraction of the peak, consider it
        outside the patch.
    </Description><Units:percent/><Value>50</Value></DefineArg>

    <DefineArg Name="PatchMode"><Description>
        If true, use center of "in peak" range as the turn-around point. If
        false use maximum horizontal peak location as the turn-around point.
    </Description><False/></DefineArg>

<!-- Variable Settings for each tracking leg -->

    <DefineArg Name="TurnNow"><Description>
        Amount to turn at at current leg. Nominally Turn * +/-1
    </Description><Arg Name="Turn"/></DefineArg>

    <DefineArg Name="WpBearing"><Description>
        Bearing being followed in the current leg. Set this to initialize start
        direction of patch tracking.
    </Description><Units:degree/><Value>-90</Value></DefineArg>

    <DefineOutput Name="PatchDetected"><Description>
        Mission sets this to false when it's time to look for a patch. It is set
        to true when a patch is found
    </Description><False/></DefineOutput>

    <DefineOutput Name="PatchMissed"><Description>
        Mission sets this to false when it's time to look for a patch. It is set
        to true when a waypoint is reached without finding the peak
    </Description><False/></DefineOutput>

    <DefineOutput Name="PatchDetectState"><Description>
        State corresponds to where we are in the pathch tracking routine:
        0-looking for a patch 1-found first sign of patch 2-confirmed patch
    </Description><Units:enum/><Value>-0</Value></DefineOutput>

    <DefineOutput Name="WpDone"><Description>
        Mission sets this to true when the cross-patch waypoint has been
        reached.
    </Description><False/></DefineOutput>

    <DefineOutput Name="LastPatchOil"><Units:microgram_per_liter/><Value>NaN</Value></DefineOutput>

    <DefineOutput Name="PatchOil"><Units:microgram_per_liter/><Value>NaN</Value></DefineOutput>

    <DefineOutput Name="PatchOilDepth"><Units:meter/><Value>NaN</Value></DefineOutput>

    <DefineOutput Name="PatchOilLatitude"><Units:degree/><Value>NaN</Value></DefineOutput>

    <DefineOutput Name="PatchOilLongitude"><Units:degree/><Value>NaN</Value></DefineOutput>

    <DefineOutput Name="PatchOilDistance"><Units:meter/><Value>0</Value></DefineOutput>

<!-- The timeout -->

    <Timeout Duration="MissionTimeout"/>

<!-- And now the mission -->

    <Guidance:AltitudeEnvelope>
        <Parallel/>
        <Setting><Guidance:AltitudeEnvelope.minAltitude/><Arg Name="MinAltitude"/></Setting>
    </Guidance:AltitudeEnvelope>

    <Guidance:DepthEnvelope>
        <Parallel/>
        <Setting><Guidance:DepthEnvelope.maxDepth/><Arg Name="MaxDepth"/></Setting>
    </Guidance:DepthEnvelope>

    <Insert Filename="Insert/Surface.xml" Id="SurfaceCommsSplit">
        <RedefineArg Name="SurfacePitch"><Units:degree/><Value>20</Value></RedefineArg>
        <RedefineArg Name="SurfaceSpeed"><Units:meter_per_second/><Value>1</Value></RedefineArg>
        <RedefineArg Name="UnpackSplitAtSurface"><True/></RedefineArg>
    </Insert>

    <Aggregate Id="SurfaceComms">

        <When>
            <Called/>
        </When>

        <Insert Filename="Insert/Surface.xml">
            <RedefineArg Name="SurfacePitch"><Units:degree/><Value>20</Value></RedefineArg>
            <RedefineArg Name="SurfaceSpeed"><Units:meter_per_second/><Value>1</Value></RedefineArg>
        </Insert>

    </Aggregate>

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

    <Aggregate Id="NeedComms">

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

        <Syslog Severity="Important">NeedComms Timeout Surfacing</Syslog>

        <Call RefId="SurfaceComms"/>

    </Aggregate>

    <Guidance:OffshoreEnvelope>
        <Parallel/>
        <Setting><Guidance:OffshoreEnvelope.minOffshore/><Arg Name="MinOffshore"/></Setting>
    </Guidance:OffshoreEnvelope>

    <Guidance:WaterDepthEnvelope>
        <Parallel/>
        <Setting><Guidance:WaterDepthEnvelope.minWaterDepth/><Arg Name="MinWaterDepth"/></Setting>
    </Guidance:WaterDepthEnvelope>

    <Guidance:Pitch>
        <Parallel/>
        <Setting><Guidance:Pitch.massPosition/><Arg Name="MassDefault"/></Setting>
    </Guidance:Pitch>

    <Guidance:Buoyancy>
        <Parallel/>
        <Setting><Guidance:Buoyancy.position /><Arg Name="BuoyancyNeutral"/></Setting>
    </Guidance:Buoyancy>

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

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

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

    <Guidance:Pitch>
        <Parallel/>
        <Setting><Guidance:Pitch.depth/><Arg Name="Depth"/></Setting>
    </Guidance:Pitch>

    <Aggregate Id="MainLoop">

        <Sequence Repeat="Repeat"/>

        <Assign><Sequence/><Arg Name="PatchMissed"/><False/></Assign>

        <Assign><Sequence/><Arg Name="PatchDetected"/><False/></Assign>

<!-- This aggregate runs in parallel with waypoint behaviors -->

        <Aggregate Id="PatchDetection">

            <When>
                <Not><Arg Name="PatchDetected"/></Not>
            </When>

            <Break><Arg Name="PatchMissed"/>
            </Break>

            <Assign><Sequence/><Arg Name="LastPatchOil"/><Arg Name="PatchOil"/></Assign>

            <Trigger:PeakDetectHorizontal Id="PeakDetectHorizontalTrigger">
                <Sequence/>
                <Break><Arg Name="WpDone"/>
                </Break>
                <Setting><Trigger:PeakDetectHorizontal.detectFrom/><Universal:mass_concentration_of_petroleum_hydrocarbons_in_sea_water/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.beginThreshold/><Arg Name="BeginThreshold"/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.offPeakFraction/><Arg Name="OffPeakFraction"/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.filterWidth/><Arg Name="FilterWidth"/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.centerPeak/><Arg Name="PatchMode"/></Setting>
                <OutputArg Name="PatchOil"><Trigger:PeakDetectHorizontal.peakDetect/></OutputArg>
                <OutputArg Name="PatchOilDepth"><Trigger:PeakDetectHorizontal.peakDepth/></OutputArg>
                <OutputArg Name="PatchOilLatitude"><Trigger:PeakDetectHorizontal.peakLatitude/></OutputArg>
                <OutputArg Name="PatchOilLongitude"><Trigger:PeakDetectHorizontal.peakLongitude/></OutputArg>
                <OutputArg Name="PatchOilDistance"><Trigger:PeakDetectHorizontal.peakDistance/></OutputArg>
            </Trigger:PeakDetectHorizontal>

            <Syslog Severity="Important">Patch value is <Arg Name="PatchOil"/><Units:kilogram_per_cubic_meter/>at <Arg Name="PatchOilLatitude"/><Units:degree/>,<Arg Name="PatchOilLongitude"/><Units:degree/>distance = <Arg Name="PatchOilDistance"/><Units:meter/></Syslog>

            <SendData Service="Express"><Arg Name="PatchOil"/><Arg Name="PatchOilDepth"/><Arg Name="PatchOilLatitude"/><Arg Name="PatchOilLongitude"/>
            </SendData>

            <Assign><Sequence/><Arg Name="PatchDetected"/><True/></Assign>

            <Aggregate>

                <Description>
                    Assign value of PatchDetectState and TurnNow after detecting
                    patch
                </Description>

                <Sequence/>

                <Aggregate Id="PatchDetectionContinuePatchLt">

                    <When>
                        <Arg Name="PatchDetectState"/>
                        <Eq><Units:enum/><Value>2</Value></Eq>
                        <And><Arg Name="PatchOil"/><Lt><Arg Name="LastPatchOil"/></Lt></And>
                    </When>

                    <Assign><Sequence/><Arg Name="TurnNow"/><Arg Name="TurnNow"/><Mult><Units:enum/><Value>-1</Value></Mult></Assign>

                </Aggregate>

                <Aggregate Id="PatchDetectionContinuePatchGt">

                    <When>
                        <Arg Name="PatchDetectState"/>
                        <Eq><Units:enum/><Value>2</Value></Eq>
                        <And><Arg Name="PatchOil"/><Gt><Arg Name="LastPatchOil"/></Gt></And>
                    </When>

                    <Assign><Sequence/><Arg Name="PatchDetectState"/><Units:enum/><Value>2</Value></Assign>

                </Aggregate>

                <Aggregate Id="PatchDetectionConfirmedPatchLt">

                    <When>
                        <Arg Name="PatchDetectState"/>
                        <Eq><Units:enum/><Value>1</Value></Eq>
                        <And><Arg Name="PatchOil"/><Lt><Arg Name="LastPatchOil"/></Lt></And>
                    </When>

                    <Assign><Sequence/><Arg Name="PatchDetectState"/><Units:enum/><Value>2</Value></Assign>

                </Aggregate>

                <Aggregate Id="PatchDetectionConfirmedPatchGt">

                    <When>
                        <Arg Name="PatchDetectState"/>
                        <Eq><Units:enum/><Value>1</Value></Eq>
                        <And><Arg Name="PatchOil"/><Gt><Arg Name="LastPatchOil"/></Gt></And>
                    </When>

                    <Assign><Sequence/><Arg Name="PatchDetectState"/><Units:enum/><Value>2</Value></Assign>

                    <Assign><Sequence/><Arg Name="TurnNow"/><Arg Name="TurnNow"/><Mult><Units:enum/><Value>-1</Value></Mult></Assign>

                </Aggregate>

                <Aggregate Id="PatchDetectionFoundPatch">

                    <When>
                        <Arg Name="PatchDetectState"/>
                        <Eq><Units:enum/><Value>0</Value></Eq>
                        <And><Arg Name="PatchOil"/><Gt><Arg Name="LastPatchOil"/></Gt></And>
                    </When>

                    <Assign><Sequence/><Arg Name="PatchDetectState"/><Units:enum/><Value>1</Value></Assign>

                    <Assign><Sequence/><Arg Name="TurnNow"/><Arg Name="TurnNow"/><Mult><Units:enum/><Value>-1</Value></Mult></Assign>

                </Aggregate>

            </Aggregate>

            <Assign><Sequence/><Arg Name="WpBearing"/><Arg Name="WpBearing"/><Add><Arg Name="TurnNow"/></Add><Mod><Arg Name="TwoPi"/>
            </Mod></Assign>

            <Syslog>Detected patch, new heading is <Arg Name="WpBearing"/><Units:degree/></Syslog>

            <Call RefId="SurfaceComms"/>

        </Aggregate>

        <Aggregate Id="Drive">

            <Description>
                This is a long leg across the width of the patch or a leg before
                the patch is detected.
            </Description>

            <Sequence/>

            <Break><Arg Name="PatchDetected"/>
            </Break>

            <Syslog>Bearing for Drive: <Arg Name="WpBearing"/><Units:degree/></Syslog>

            <Guidance:Waypoint Id="DriveWp">
                <Sequence/>
                <Timeout Duration="WpTimeout"/>
                <Setting><Guidance:Waypoint.distanceDelta/><Arg Name="WpMaxDistance"/></Setting>
                <Setting><Guidance:Waypoint.distanceDeltaBearing/><Arg Name="WpBearing"/></Setting>
                <Setting><Guidance:Waypoint.waterFrame/><Arg Name="WaterFrame"/></Setting>
            </Guidance:Waypoint>

            <Assign><Sequence/><Arg Name="PatchMissed"/><True/></Assign>

            <Assign><Sequence/><Arg Name="PatchOil"/><Units:microgram_per_liter/><Value>NaN</Value></Assign>

            <Assign><Sequence/><Arg Name="LastPatchOil"/><Units:microgram_per_liter/><Value>NaN</Value></Assign>

            <Assign><Sequence/><Arg Name="TurnNow"/><Arg Name="TurnNow"/><Mult><Units:enum/><Value>-1</Value></Mult></Assign>

            <Assign><Sequence/><Arg Name="WpBearing"/><Arg Name="WpBearing"/><Add><Arg Name="TurnNow"/></Add><Mod><Arg Name="TwoPi"/>
            </Mod></Assign>

            <Syslog>Reached waypoint, new heading is <Arg Name="WpBearing"/><Units:degree/></Syslog>

            <Call RefId="SurfaceComms"/>

        </Aggregate>

    </Aggregate>

</Mission>
