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

    <Description>
        Vehicle yo-yos with patch detect enabled for oil concentration. NOTE:
        oil concentrations may vary strongly with depth! Suggested settings for
        simulaton: Config/sim/[username]/Simulator.cfg: oceanModelData =
        "Resources/BurgerOilfieldSpill.nc:mass_concentration_of_petroleum_hydrocarbons_in_sea_water:kg/m3";
        eastCurrent = 0.05 m/s; // vel y wrto LV northCurrent = 0.005 m/s; //
        vel x wrto LV Config/sim/[username]/workSite.cfg: initLat =
        71.2225805280196 arcdeg; // Initial latitude initLon =
        -163.5093252100575 arcdeg; // Initial longitude
        Config/sim/[username]/sim.cfg: WetLabsBB2FL.loadAtStartup = 0 bool;
        WetLabsSeaOWL_UV_A.loadAtStartup = 1 bool;
        WetLabsSeaOWL_UV_A.simulateSensors = 1 bool; WetLabsSeaOWL_UV_A.serial =
        "SEAOWLA2K-019"; WetLabsSeaOWL_UV_A.scaleFactor700 = 3.204e-7
        1/m/sr/count; WetLabsSeaOWL_UV_A.darkCounts700 = 48 count;
        WetLabsSeaOWL_UV_A.scaleFactorFDOM = 8.096e-3 ppb/count;
        WetLabsSeaOWL_UV_A.darkCountsFDOM = 50 count;
        WetLabsSeaOWL_UV_A.fdomAccuracy = 8.19 ppb;
        WetLabsSeaOWL_UV_A.scaleFactorChl = 2.170e-3 ug/l/count;
        WetLabsSeaOWL_UV_A.darkCountsChl = 50 count;
        WetLabsSeaOWL_UV_A.chlAccuracy = 1.475 ug/l;
        WetLabsSeaOWL_UV_A.scaleFactorOil = 2.8 ppb/count;
        WetLabsSeaOWL_UV_A.darkCountsOil = 50 count;
        WetLabsSeaOWL_UV_A.oilAccuracy = 3.6 ppb; NavChartDb.charts =
        "US1AK90M,US2AK92M,US5AK9NM,US5AK9OM,US5AK9PM,US5AK9QM,US5AK9RM,US5AK9SM,US5AK9TM,US5AK9UM";
    </Description>

<!-- Vehicle safety parameters -->

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

    <DefineArg Name="AcousticTrackingTimeout"><Description>
        If the vehicle does not receive an acoustic signal for more than this
        length of time, it will surface for communications. Set longer than
        MissionTimeout to effectively disable.
    </Description><Units:hour/><Value>240</Value></DefineArg>

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

    <DefineArg Name="PeakDetectChl"><Description>
        Chlorophyll patch tracking.
    </Description><True/></DefineArg>

<!--debug    
    </Description><False/></DefineArg>
-->

    <DefineArg Name="PeakDetectSalinity"><Description>
        Salinity patch tracking.
    </Description><False/></DefineArg>

<!--debug    
    </Description><True/></DefineArg>
-->

    <DefineArg Name="PeakDetectOil"><Description>
        Oil patch tracking.
    </Description><False/></DefineArg>

<!--debug    
    </Description><True/></DefineArg>    
-->

    <DefineArg Name="DetectTheTrough"><Description>
        Whether to track a trough. If false, track peak; if true, track trough.
        Default is false.
    </Description><False/></DefineArg>

<!--debug    
    </Description><True/></DefineArg>
-->

    <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>50</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="MarginMinOffshore"><Description>
        If closer than MinOffshore+MarginMinOffshore, turn back from shore.
    </Description><Units:meter/><Value>100</Value></DefineArg>

    <DefineArg Name="MinDistFromPolygonSides"><Description>
        If a operational safety polygon is specified and the vehicle is closer
        than this minimum distance to the polygon sides, turn back.
    </Description><Units:meter/><Value>200</Value></DefineArg>

    <DefineArg Name="NeedCommsTimeInTransit"><Description>
        Maximum time between surfacings for communications during transit
    </Description><Units:minute/><Value>60</Value></DefineArg>

    <DefineArg Name="NeedCommsTimePatchTracking"><Description>
        Maximum time between surfacings for communications during patch
        tracking. This is checked only on every NumRoundsBtnCrossLegSurfacings
        rounds of CrossLeg.
    </Description><Units:minute/><Value>300</Value></DefineArg>

    <DefineArg Name="NeedCommsTimeMarginPatchTracking"><Description>
        In case vehicle does not surface on periodic rounds of InLeg or
        CrossLeg, vehicle will be forced to surface at
        NeedCommsTimePatchTracking+NeedCommsTimeMarginPatchTracking.
    </Description><Units:minute/><Value>60</Value></DefineArg>

    <DefineArg Name="WaitAfterBouncing"><Description>
        After the vehicle bounces off the boundary (defined by minimum distance
        to shore or polygon sides), wait this long before re-checking the
        distance.
    </Description><Units:minute/><Value>5</Value></DefineArg>

    <DefineArg Name="LatPatchTrackingStart"><Description>
        Latitude of patch tracking start location. If nan, transit is skipped.
    </Description><Units:degree/><Value>NaN</Value></DefineArg>

<!--debug    
    </Description><Units:degree/><Value>36.810</Value></DefineArg>
-->

    <DefineArg Name="LonPatchTrackingStart"><Description>
        Longitude of patch tracking start location. If nan, transit is skipped.
    </Description><Units:degree/><Value>NaN</Value></DefineArg>

<!--debug    
    </Description><Units:degree/><Value>-121.855</Value></DefineArg>
-->

    <DefineArg Name="Repeat"><Description>
        Maximum number of rounds (one round = one full set of sweeps in four
        directions)
<!--debug	
    </Description><Units:count/><Value>200</Value></DefineArg>
-->
    </Description><Units:count/><Value>5</Value></DefineArg>

    <DefineArg Name="NumRoundsBtnCrossLegSurfacings"><Description>
        Every so many rounds, vehicle surfaces on CrossLeg. Set to a big number
        to practially disable surfacing on CrossLeg.
    </Description><Units:count/><Value>5</Value></DefineArg>

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

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

    <DefineArg Name="TransitYoYoMinDepth"><Description>
        Minimum depth while performing the YoYo behavior during transit.
    </Description><Units:meter/><Value>2</Value></DefineArg>

    <DefineArg Name="TransitYoYoMaxDepth"><Description>
        Maximum depth while performing the YoYo behavior during transit.
    </Description><Units:meter/><Value>30</Value></DefineArg>

    <DefineArg Name="PatchTrackingYoYoMinDepth"><Description>
        Minimum depth while performing the YoYo behavior during patch tracking.
    </Description><Units:meter/><Value>2</Value></DefineArg>

    <DefineArg Name="PatchTrackingYoYoMaxDepth"><Description>
        Maximum depth while performing the YoYo behavior during patch tracking.
    </Description><Units:meter/><Value>25</Value></DefineArg>

    <DefineArg Name="YoYoMinAltitude"><Description>
        Minimum altitude while performing the YoYo behavior (for
        bottom-terminated YoYos).
    </Description><Units:meter/><Value>9</Value></DefineArg>

    <DefineArg Name="YoYoUpPitch"><Description>
        Vehicle up pitch while performing the YoYo behavior.
    </Description><Units:degree/><Value>20</Value></DefineArg>

    <DefineArg Name="YoYoDownPitch"><Description>
        Vehicle down pitch while performing the YoYo behavior.
    </Description><Units:degree/><Value>-20</Value></DefineArg>

    <DefineArg Name="PeakShallowBnd"><Description>
        Shallow depth bound for detecting peak on each descent or ascent
        profile.
    </Description><Units:meter/><Value>5</Value></DefineArg>

    <DefineArg Name="PeakDeepBnd"><Description>
        Deep depth bound for detecting peak on each descent or ascent profile.
    </Description><Units:meter/><Value>20</Value></DefineArg>

    <DefineArg Name="BeginThresholdChl"><Description>
        If non-NaN, sets chlorophyll threshold for starting horizontal patch
        detection
    </Description><Units:microgram_per_liter/><Value>1.0</Value></DefineArg>

    <DefineArg Name="BeginThresholdSalt"><Description>
        If non-NaN, sets salinity threshold for starting horizontal patch
        detection
    </Description><Units:psu/><Value>33.0</Value></DefineArg>

    <DefineArg Name="BeginThresholdOil"><Description>
        If non-NaN, sets threshold for starting horizontal patch detection
    </Description><Units:kilogram_per_cubic_meter/><Value>0.2</Value></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>

<!--debug    
    </Description><Units:degree/><Value>270</Value></DefineArg>
-->

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

    <DefineArg Name="DetectionTimeout"><Description>
        Look for signal for this long. If none found within this timeout, switch
        to the next leg.
    </Description><Units:minute/><Value>30</Value></DefineArg>

    <DefineArg Name="TimeWindowPeakRpt"><Description>
        If greater than zero, report a peak every such time window has elapsed.
        If NaN or zero, this variable is ignored.
    </Description><Units:minute/><Value>NaN</Value></DefineArg>

    <DefineArg Name="LowPassWindowLen"><Description>
        Low-pass window length (based on depth sensor sampling interval 0.4
        second) for low-pass filtering.
    </Description><Units:count/><Value>20</Value></DefineArg>

    <DefineArg Name="MedianFilterLength"><Description>
        Median filter length (only for chlorophyll fluorescence which tends to
        have spikes). The median-filtered signal enters the succeeding low-pass
        filter. If set to 1, then no median filtering, i.e., the raw
        fluorescence signal directly enters the low-pass filter.
    </Description><Units:count/><Value>5</Value></DefineArg>

    <DefineArg Name="NumOfProfilesSlidingwindow"><Description>
        Length of horizontal sliding window. The highest per-yoyo peak (after
        low-pass filtering by a filter of length FilterWidthHorizontal) is
        picked out within this window.
    </Description><Units:count/><Value>50</Value></DefineArg>

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

    <DefineArg Name="DepChangeThreshAttitudeFlip"><Description>
        Depth change threshold for determining vehicle attitude flip.
    </Description><Units:meter/><Value>2.0</Value></DefineArg>

    <DefineArg Name="WaterFrame"><Description>
        Set WaterFrame true to drive towards waypoints in the water frame of
        reference
    </Description><True/></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>

    <DefineArg Name="Turn"><Description>
        Amount to turn at each leg
    </Description><Units:degree/><Value>90</Value></DefineArg>

    <DefineArg Name="Reverse"><Description>
        Change in bearing which reverses vehicle direction
    </Description><Units:degree/><Value>180</Value></DefineArg>

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

<!-- Internal variable settings -->

    <DefineOutput Name="Reverse"><Description>
        Change in bearing which reverses vehicle direction
    </Description><Units:degree/><Value>180</Value></DefineOutput>

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

    <DefineOutput Name="CntRounds"><Description>
        Count of sweep rounds (initilized to 1).
    </Description><Units:count/><Value>1</Value></DefineOutput>

    <DefineOutput Name="PatchDetectOn"><Description>
        Mission sets this to true when the patch has been crossed. Initialized
        to False.
    </Description><False/></DefineOutput>

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

    <DefineOutput Name="CrossLegToPatchCenterCompleted"><Description>
        Initialized to False.
    </Description><False/></DefineOutput>

    <DefineOutput Name="CrossLegSearchForPeakStarted"><Description>
        Initialized to False.
    </Description><False/></DefineOutput>

    <DefineOutput Name="AllDone"><Description>
        Initialized to False.
    </Description><False/></DefineOutput>

    <DefineOutput Name="FlagTurnBack"><Description>
        Flag of turning back from shore. Initialized to False.
    </Description><False/></DefineOutput>

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

    <DefineOutput Name="PatchPeakSalinity"><Units:psu/><Value>NaN</Value></DefineOutput>

    <DefineOutput Name="PatchPeakOil"><Units:kilogram_per_cubic_meter/><Value>NaN</Value></DefineOutput>

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

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

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

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

    <DefineOutput Name="StoppedForNaNPatchPeak"><Description>
        Mission stopped because patch peak value is NaN. Initialized to False.
    </Description><False/></DefineOutput>

    <DefineOutput Name="StoppedForBeingOutsideOfPolygon"><Description>
        Mission stopped because the vehicle is outside of the polygon.
        Initialized to False.
    </Description><False/></DefineOutput>

    <DefineOutput Name="ElapsedSinceStartOrLastDistCheck"><Description>
        The mission sets this variable to reset the timer for checking distance
        to shore and polygon sides.
    </Description><Units:hour/><Value>0</Value></DefineOutput>

<!-- The timeout -->

    <Timeout Duration="MissionTimeout"/>

<!-- And now the mission -->

<!-- Uncomment to speed up simulation
    <Guidance:SetSpeed>
        <Parallel/>
        <Setting><Guidance:SetSpeed.period/><Units:millisecond/><Value>800</Value></Setting>
    </Guidance:SetSpeed>
-->

    <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>

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

    <Insert Filename="Insert/NeedComms.xml" Id="NeedComms"/>

    <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>

    <Insert Filename="Insert/AbortDrift.xml">
        <RedefineArg Name="AcousticTimeout"><Arg Name="AcousticTrackingTimeout"/></RedefineArg>
    </Insert>

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

    <Insert Filename="Insert/Science.xml">
        <RedefineArg Name="PeakDetectChlActive"><Arg Name="PeakDetectChl"/></RedefineArg>
        <RedefineArg Name="PeakDetectSalinityActive"><Arg Name="PeakDetectSalinity"/></RedefineArg>
        <RedefineArg Name="PeakDetectOilActive"><Arg Name="PeakDetectOil"/></RedefineArg>
        <RedefineArg Name="DetectTrough"><Arg Name="DetectTheTrough"/></RedefineArg>
        <RedefineArg Name="TimeWindowPeakReport"><Arg Name="TimeWindowPeakRpt"/></RedefineArg>
        <RedefineArg Name="LowPassWindowLength"><Arg Name="LowPassWindowLen"/></RedefineArg>
        <RedefineArg Name="MedianFilterLen"><Arg Name="MedianFilterLength"/></RedefineArg>
        <RedefineArg Name="PeakShallowBound"><Arg Name="PeakShallowBnd"/></RedefineArg>
        <RedefineArg Name="PeakDeepBound"><Arg Name="PeakDeepBnd"/></RedefineArg>
        <RedefineArg Name="DepChangeThreshForAttitudeFlip"><Arg Name="DepChangeThreshAttitudeFlip"/></RedefineArg>
    </Insert>

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

    <Aggregate Id="SyslogWpMaxDistanceReached">

        <When>
            <Called/>
        </When>

        <Aggregate Id="SyslogWpMaxDistanceReachedPatchPeakIsNaN">

            <Break><Arg Name="PeakDetectChl"/>
                <And><Arg Name="PatchPeakChl"/><Ge><Units:microgram_per_liter/><Value>0.0</Value></Ge></And>
                <Or><Arg Name="PeakDetectSalinity"/><And><Arg Name="PatchPeakSalinity"/><Ge><Units:psu/><Value>0.0</Value></Ge></And></Or>
                <Or><Arg Name="PeakDetectOil"/><And><Arg Name="PatchPeakOil"/><Ge><Units:kilogram_per_cubic_meter/><Value>0.0</Value></Ge></And></Or>
                <Or><Arg Name="StoppedForNaNPatchPeak"/></Or>
            </Break>

            <Syslog Severity="Important">WpMaxDistance reached. Patch peak value is NaN. Stopping mission.</Syslog>

            <Guidance:Execute>
                <Sequence/>
                <Setting><Guidance:Execute.command/><String>stop</String></Setting>
            </Guidance:Execute>

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

        </Aggregate>

        <Aggregate Id="SyslogWpMaxDistanceReachedChl">

            <Break>
                <Not><Arg Name="PeakDetectChl"/><And><Arg Name="PatchPeakChl"/><Ge><Units:microgram_per_liter/><Value>0.0</Value></Ge></And></Not>
            </Break>

            <Syslog Severity="Important">WpMaxDistance reached. Patch value is <Arg Name="PatchPeakChl"/><Units:microgram_per_liter/>at <Arg Name="PatchPeakLatitude"/><Units:degree/>,<Arg Name="PatchPeakLongitude"/><Units:degree/>, distance = <Arg Name="PatchPeakDistance"/><Units:meter/></Syslog>

        </Aggregate>

        <Aggregate Id="SyslogWpMaxDistanceReachedSalinity">

            <Break>
                <Not><Arg Name="PeakDetectSalinity"/><And><Arg Name="PatchPeakSalinity"/><Ge><Units:psu/><Value>0.0</Value></Ge></And></Not>
            </Break>

            <Syslog Severity="Important">WpMaxDistance reached. Patch value is <Arg Name="PatchPeakSalinity"/><Units:psu/>at <Arg Name="PatchPeakLatitude"/><Units:degree/>,<Arg Name="PatchPeakLongitude"/><Units:degree/>, distance = <Arg Name="PatchPeakDistance"/><Units:meter/></Syslog>

        </Aggregate>

        <Aggregate Id="SyslogWpMaxDistanceReachedOil">

            <Break>
                <Not><Arg Name="PeakDetectOil"/><And><Arg Name="PatchPeakOil"/><Ge><Units:kilogram_per_cubic_meter/><Value>0.0</Value></Ge></And></Not>
            </Break>

            <Syslog Severity="Important">WpMaxDistance reached. Patch value is <Arg Name="PatchPeakOil"/><Units:kilogram_per_cubic_meter/>at <Arg Name="PatchPeakLatitude"/><Units:degree/>,<Arg Name="PatchPeakLongitude"/><Units:degree/>, distance = <Arg Name="PatchPeakDistance"/><Units:meter/></Syslog>

        </Aggregate>

    </Aggregate>

    <Aggregate Id="SyslogTurnBack">

        <When>
            <Called/>
        </When>

        <Aggregate Id="SyslogTurnBackFromShoreChl">

            <Break>
                <Not><Arg Name="PeakDetectChl"/><And><Universal:distance_from_shore/><Le><Arg Name="MinOffshore"/><Add><Arg Name="MarginMinOffshore"/></Add></Le></And></Not>
            </Break>

            <Syslog Severity="Important">Latitude = <Universal:latitude/><Units:degree/>, longitude = <Universal:longitude/><Units:degree/>. Turn back from shore because distance_from_shore (<Universal:distance_from_shore/><Units:meter/>) is less than OffshoreDistanceTurnBack (<Arg Name="MinOffshore"/><Units:meter/>+<Arg Name="MarginMinOffshore"/><Units:meter/>). Patch value is <Arg Name="PatchPeakChl"/><Units:microgram_per_liter/>at <Arg Name="PatchPeakLatitude"/><Units:degree/>,<Arg Name="PatchPeakLongitude"/><Units:degree/>, distance = <Arg Name="PatchPeakDistance"/><Units:meter/></Syslog>

        </Aggregate>

        <Aggregate Id="SyslogTurnBackFromWallChl">

            <Break>
                <Not><Arg Name="PeakDetectChl"/><And><Derivation:ShortestDistanceToPolygonSidesCalculator.loadAtStartup/></And><And><Derivation:ShortestDistanceToPolygonSidesCalculator.shortest_distance_to_polygon_sides/><Le><Arg Name="MinDistFromPolygonSides"/></Le></And></Not>
            </Break>

            <Syslog Severity="Important">Latitude = <Universal:latitude/><Units:degree/>, longitude = <Universal:longitude/><Units:degree/>. Turn back from wall because shortest_distance_from_polygon_sides (<Derivation:ShortestDistanceToPolygonSidesCalculator.shortest_distance_to_polygon_sides/><Units:meter/>) to side <Derivation:ShortestDistanceToPolygonSidesCalculator.side_shortest_distance/><Units:count/> is less than MinDistFromPolygonSides (<Arg Name="MinDistFromPolygonSides"/><Units:meter/>). Patch value is <Arg Name="PatchPeakChl"/><Units:microgram_per_liter/>at <Arg Name="PatchPeakLatitude"/><Units:degree/>,<Arg Name="PatchPeakLongitude"/><Units:degree/>, distance = <Arg Name="PatchPeakDistance"/><Units:meter/></Syslog>

        </Aggregate>

        <Aggregate Id="SyslogTurnBackFromShoreSalinity">

            <Break>
                <Not><Arg Name="PeakDetectSalinity"/><And><Universal:distance_from_shore/><Le><Arg Name="MinOffshore"/><Add><Arg Name="MarginMinOffshore"/></Add></Le></And></Not>
            </Break>

            <Syslog Severity="Important">Latitude = <Universal:latitude/><Units:degree/>, longitude = <Universal:longitude/><Units:degree/>. Turn back from shore because distance_from_shore (<Universal:distance_from_shore/><Units:meter/>) is less than OffshoreDistanceTurnBack (<Arg Name="MinOffshore"/><Units:meter/>+<Arg Name="MarginMinOffshore"/><Units:meter/>). Patch value is <Arg Name="PatchPeakSalinity"/><Units:psu/>at <Arg Name="PatchPeakLatitude"/><Units:degree/>,<Arg Name="PatchPeakLongitude"/><Units:degree/>, distance = <Arg Name="PatchPeakDistance"/><Units:meter/></Syslog>

        </Aggregate>

        <Aggregate Id="SyslogTurnBackFromWallSalinity">

            <Break>
                <Not><Arg Name="PeakDetectSalinity"/><And><Derivation:ShortestDistanceToPolygonSidesCalculator.loadAtStartup/></And><And><Derivation:ShortestDistanceToPolygonSidesCalculator.shortest_distance_to_polygon_sides/><Le><Arg Name="MinDistFromPolygonSides"/></Le></And></Not>
            </Break>

            <Syslog Severity="Important">Latitude = <Universal:latitude/><Units:degree/>, longitude = <Universal:longitude/><Units:degree/>. Turn back from wall because shortest_distance_from_polygon_sides (<Derivation:ShortestDistanceToPolygonSidesCalculator.shortest_distance_to_polygon_sides/><Units:meter/>) to side <Derivation:ShortestDistanceToPolygonSidesCalculator.side_shortest_distance/><Units:count/> is less than MinDistFromPolygonSides (<Arg Name="MinDistFromPolygonSides"/><Units:meter/>). Patch value is <Arg Name="PatchPeakSalinity"/><Units:psu/>at <Arg Name="PatchPeakLatitude"/><Units:degree/>,<Arg Name="PatchPeakLongitude"/><Units:degree/>, distance = <Arg Name="PatchPeakDistance"/><Units:meter/></Syslog>

        </Aggregate>

        <Aggregate Id="SyslogTurnBackFromShoreOil">

            <Break>
                <Not><Arg Name="PeakDetectOil"/><And><Universal:distance_from_shore/><Le><Arg Name="MinOffshore"/><Add><Arg Name="MarginMinOffshore"/></Add></Le></And></Not>
            </Break>

            <Syslog Severity="Important">Latitude = <Universal:latitude/><Units:degree/>, longitude = <Universal:longitude/><Units:degree/>. Turn back from shore because distance_from_shore (<Universal:distance_from_shore/><Units:meter/>) is less than OffshoreDistanceTurnBack (<Arg Name="MinOffshore"/><Units:meter/>+<Arg Name="MarginMinOffshore"/><Units:meter/>). Patch value is <Arg Name="PatchPeakOil"/><Units:kilogram_per_cubic_meter/>at <Arg Name="PatchPeakLatitude"/><Units:degree/>,<Arg Name="PatchPeakLongitude"/><Units:degree/>, distance = <Arg Name="PatchPeakDistance"/><Units:meter/></Syslog>

        </Aggregate>

        <Aggregate Id="SyslogTurnBackFromWallOil">

            <Break>
                <Not><Arg Name="PeakDetectOil"/><And><Derivation:ShortestDistanceToPolygonSidesCalculator.loadAtStartup/></And><And><Derivation:ShortestDistanceToPolygonSidesCalculator.shortest_distance_to_polygon_sides/><Le><Arg Name="MinDistFromPolygonSides"/></Le></And></Not>
            </Break>

            <Syslog Severity="Important">Latitude = <Universal:latitude/><Units:degree/>, longitude = <Universal:longitude/><Units:degree/>. Turn back from wall because shortest_distance_from_polygon_sides (<Derivation:ShortestDistanceToPolygonSidesCalculator.shortest_distance_to_polygon_sides/><Units:meter/>) to side <Derivation:ShortestDistanceToPolygonSidesCalculator.side_shortest_distance/><Units:count/> is less than MinDistFromPolygonSides (<Arg Name="MinDistFromPolygonSides"/><Units:meter/>). Patch value is <Arg Name="PatchPeakOil"/><Units:kilogram_per_cubic_meter/>at <Arg Name="PatchPeakLatitude"/><Units:degree/>,<Arg Name="PatchPeakLongitude"/><Units:degree/>, distance = <Arg Name="PatchPeakDistance"/><Units:meter/></Syslog>

        </Aggregate>

    </Aggregate>

    <Aggregate Id="TransitToPatchTrackingStartLocation">

        <Sequence/>

        <Break>
            <IsNaN><Arg Name="LatPatchTrackingStart"/>
            </IsNaN>
            <Or><IsNaN><Arg Name="LonPatchTrackingStart"/>
            </IsNaN></Or>
        </Break>

        <Assign><Sequence/><Arg Name="NeedComms:DiveInterval"/><Arg Name="NeedCommsTimeInTransit"/></Assign>

        <Guidance:DepthEnvelope>
            <Parallel/>
            <Setting><Guidance:DepthEnvelope.minDepth/><Arg Name="TransitYoYoMinDepth"/></Setting>
            <Setting><Guidance:DepthEnvelope.maxDepth/><Arg Name="TransitYoYoMaxDepth"/></Setting>
        </Guidance:DepthEnvelope>

        <Guidance:AltitudeEnvelope>
            <Description>
                Another altitude envelope for the YoYo behavior. This envelope
                should fall within the limits of the standard safety envelopes
                in Insert/StandardEnvelopes.xml in order to avoid commanding
                high pitch angles for bottom-terminated YoYos.
            </Description>
            <Parallel/>
            <Setting><Guidance:AltitudeEnvelope.minAltitude/><Arg Name="YoYoMinAltitude"/></Setting>
            <Setting><Guidance:AltitudeEnvelope.upPitch/><Arg Name="YoYoUpPitch"/></Setting>
        </Guidance:AltitudeEnvelope>

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

        <Guidance:Waypoint>
            <Sequence/>
            <Setting><Guidance:Waypoint.latitude/><Arg Name="LatPatchTrackingStart"/></Setting>
            <Setting><Guidance:Waypoint.longitude/><Arg Name="LonPatchTrackingStart"/></Setting>
        </Guidance:Waypoint>

    </Aggregate>

    <Aggregate Id="MissionStart">

        <Sequence/>

        <Call Id="MissionStartComms" RefId="NeedComms"/>

        <Assign><Sequence/><Arg Name="ElapsedSinceStartOrLastDistCheck"/><Units:hour/><Value>0</Value></Assign>

    </Aggregate>

    <Aggregate Id="PatchTracking">

        <Sequence Repeat="Repeat"/>

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

        <Assign><Sequence/><Arg Name="NeedComms:DiveInterval"/><Arg Name="NeedCommsTimePatchTracking"/><Add><Arg Name="NeedCommsTimeMarginPatchTracking"/></Add></Assign>

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

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

        <Guidance:DepthEnvelope>
            <Parallel/>
            <Setting><Guidance:DepthEnvelope.minDepth/><Arg Name="PatchTrackingYoYoMinDepth"/></Setting>
            <Setting><Guidance:DepthEnvelope.maxDepth/><Arg Name="PatchTrackingYoYoMaxDepth"/></Setting>
        </Guidance:DepthEnvelope>

        <Guidance:AltitudeEnvelope>
            <Description>
                Another altitude envelope for the YoYo behavior. This envelope
                should fall within the limits of the standard safety envelopes
                in Insert/StandardEnvelopes.xml in order to avoid commanding
                high pitch angles for bottom-terminated YoYos.
            </Description>
            <Parallel/>
            <Setting><Guidance:AltitudeEnvelope.minAltitude/><Arg Name="YoYoMinAltitude"/></Setting>
            <Setting><Guidance:AltitudeEnvelope.upPitch/><Arg Name="YoYoUpPitch"/></Setting>
        </Guidance:AltitudeEnvelope>

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

        <Aggregate Id="OutsideOfPolygon">

            <Parallel/>

            <Break>
                <Not><Derivation:ShortestDistanceToPolygonSidesCalculator.loadAtStartup/></Not>
                <Or><Derivation:ShortestDistanceToPolygonSidesCalculator.loadAtStartup/><And><Derivation:ShortestDistanceToPolygonSidesCalculator.inside_polygon/></And></Or>
                <Or><Arg Name="StoppedForBeingOutsideOfPolygon"/></Or>
            </Break>

            <Syslog Severity="Important">Latitude = <Universal:latitude/><Units:degree/>, longitude = <Universal:longitude/><Units:degree/>. The vehicle is outside of the polygon. Stopping mission.</Syslog>

            <Guidance:Execute>
                <Sequence/>
                <Setting><Guidance:Execute.command/><String>stop</String></Setting>
            </Guidance:Execute>

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

        </Aggregate>

        <Aggregate Id="ChlPatchDetection">

            <When>
                <Arg Name="PatchDetectOn"/>
                <And><Arg Name="PeakDetectChl"/></And>
            </When>

            <Trigger:PeakDetectHorizontal Id="PatchTrackTrigger">
                <Sequence/>
                <Break><Arg Name="WpDone"/>
                </Break>
                <Setting><Trigger:PeakDetectHorizontal.patchTracking/><True/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.detectFrom/><Arg Name="Science:PeakChl"/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.detectTrough/><Arg Name="Science:DetectTrough"/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.depthFrom/><Arg Name="Science:PeakChlDepth"/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.latitudeFrom/><Arg Name="Science:PeakChlLatitude"/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.longitudeFrom/><Arg Name="Science:PeakChlLongitude"/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.beginThreshold/><Arg Name="BeginThresholdChl"/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.offPeakFraction/><Arg Name="OffPeakFraction"/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.filterWidth/><Arg Name="FilterWidthHorizontal"/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.numProfilesSlidingwindow/><Arg Name="NumOfProfilesSlidingwindow"/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.detectTimeout/><Arg Name="DetectionTimeout"/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.centerPeak/><Arg Name="PatchMode"/></Setting>
                <OutputArg Name="PatchPeakChl"><Trigger:PeakDetectHorizontal.peakDetect/></OutputArg>
                <OutputArg Name="PatchPeakDepth"><Trigger:PeakDetectHorizontal.peakDepth/></OutputArg>
                <OutputArg Name="PatchPeakLatitude"><Trigger:PeakDetectHorizontal.peakLatitude/></OutputArg>
                <OutputArg Name="PatchPeakLongitude"><Trigger:PeakDetectHorizontal.peakLongitude/></OutputArg>
                <OutputArg Name="PatchPeakDistance"><Trigger:PeakDetectHorizontal.peakDistance/></OutputArg>
            </Trigger:PeakDetectHorizontal>

            <Syslog Severity="Important">Peak found before WpMaxDistance is reached. Patch value is <Arg Name="PatchPeakChl"/><Units:microgram_per_liter/>at <Arg Name="PatchPeakLatitude"/><Units:degree/>,<Arg Name="PatchPeakLongitude"/><Units:degree/>, distance = <Arg Name="PatchPeakDistance"/><Units:meter/></Syslog>

            <SendData Service="Express"><Arg Name="PatchPeakChl"/><Arg Name="PatchPeakDepth"/><Arg Name="PatchPeakLatitude"/><Arg Name="PatchPeakLongitude"/>
            </SendData>

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

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

        </Aggregate>

        <Aggregate Id="SalinityPatchDetection">

            <When>
                <Arg Name="PatchDetectOn"/>
                <And><Arg Name="PeakDetectSalinity"/></And>
            </When>

            <Trigger:PeakDetectHorizontal Id="PatchTrackTrigger">
                <Sequence/>
                <Break><Arg Name="WpDone"/>
                </Break>
                <Setting><Trigger:PeakDetectHorizontal.patchTracking/><True/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.detectFrom/><Arg Name="Science:PeakSalt"/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.depthFrom/><Arg Name="Science:PeakSaltDepth"/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.latitudeFrom/><Arg Name="Science:PeakSaltLatitude"/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.longitudeFrom/><Arg Name="Science:PeakSaltLongitude"/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.beginThreshold/><Arg Name="BeginThresholdSalt"/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.offPeakFraction/><Arg Name="OffPeakFraction"/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.filterWidth/><Arg Name="FilterWidthHorizontal"/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.numProfilesSlidingwindow/><Arg Name="NumOfProfilesSlidingwindow"/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.detectTimeout/><Arg Name="DetectionTimeout"/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.centerPeak/><Arg Name="PatchMode"/></Setting>
                <OutputArg Name="PatchPeakSalinity"><Trigger:PeakDetectHorizontal.peakDetect/></OutputArg>
                <OutputArg Name="PatchPeakDepth"><Trigger:PeakDetectHorizontal.peakDepth/></OutputArg>
                <OutputArg Name="PatchPeakLatitude"><Trigger:PeakDetectHorizontal.peakLatitude/></OutputArg>
                <OutputArg Name="PatchPeakLongitude"><Trigger:PeakDetectHorizontal.peakLongitude/></OutputArg>
                <OutputArg Name="PatchPeakDistance"><Trigger:PeakDetectHorizontal.peakDistance/></OutputArg>
            </Trigger:PeakDetectHorizontal>

            <Syslog Severity="Important">Peak found before WpMaxDistance is reached. Patch value is <Arg Name="PatchPeakSalinity"/><Units:psu/>at <Arg Name="PatchPeakLatitude"/><Units:degree/>,<Arg Name="PatchPeakLongitude"/><Units:degree/>, distance = <Arg Name="PatchPeakDistance"/><Units:meter/></Syslog>

            <SendData Service="Express"><Arg Name="PatchPeakSalinity"/><Arg Name="PatchPeakDepth"/><Arg Name="PatchPeakLatitude"/><Arg Name="PatchPeakLongitude"/>
            </SendData>

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

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

        </Aggregate>

        <Aggregate Id="OilPatchDetection">

            <When>
                <Arg Name="PatchDetectOn"/>
                <And><Arg Name="PeakDetectOil"/></And>
            </When>

            <Trigger:PeakDetectHorizontal Id="PatchTrackTrigger">
                <Sequence/>
                <Break><Arg Name="WpDone"/>
                </Break>
                <Setting><Trigger:PeakDetectHorizontal.patchTracking/><True/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.detectFrom/><Arg Name="Science:PeakOil"/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.depthFrom/><Arg Name="Science:PeakOilDepth"/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.latitudeFrom/><Arg Name="Science:PeakOilLatitude"/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.longitudeFrom/><Arg Name="Science:PeakOilLongitude"/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.beginThreshold/><Arg Name="BeginThresholdOil"/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.offPeakFraction/><Arg Name="OffPeakFraction"/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.filterWidth/><Arg Name="FilterWidthHorizontal"/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.numProfilesSlidingwindow/><Arg Name="NumOfProfilesSlidingwindow"/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.detectTimeout/><Arg Name="DetectionTimeout"/></Setting>
                <Setting><Trigger:PeakDetectHorizontal.centerPeak/><Arg Name="PatchMode"/></Setting>
                <OutputArg Name="PatchPeakOil"><Trigger:PeakDetectHorizontal.peakDetect/></OutputArg>
                <OutputArg Name="PatchPeakDepth"><Trigger:PeakDetectHorizontal.peakDepth/></OutputArg>
                <OutputArg Name="PatchPeakLatitude"><Trigger:PeakDetectHorizontal.peakLatitude/></OutputArg>
                <OutputArg Name="PatchPeakLongitude"><Trigger:PeakDetectHorizontal.peakLongitude/></OutputArg>
                <OutputArg Name="PatchPeakDistance"><Trigger:PeakDetectHorizontal.peakDistance/></OutputArg>
            </Trigger:PeakDetectHorizontal>

            <Syslog Severity="Important">Peak found before WpMaxDistance is reached. Patch value is <Arg Name="PatchPeakOil"/><Units:kilogram_per_cubic_meter/>at <Arg Name="PatchPeakLatitude"/><Units:degree/>,<Arg Name="PatchPeakLongitude"/><Units:degree/>, distance = <Arg Name="PatchPeakDistance"/><Units:meter/></Syslog>

            <SendData Service="Express"><Arg Name="PatchPeakOil"/><Arg Name="PatchPeakDepth"/><Arg Name="PatchPeakLatitude"/><Arg Name="PatchPeakLongitude"/>
            </SendData>

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

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

        </Aggregate>

        <Aggregate Id="CrossLegToPatchCenter">

            <Description>
                This is a short run back to patch center.
            </Description>

            <Sequence/>

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

<!--debug

            <Syslog Severity="Important">CntRounds= <Arg Name="CntRounds"/><Units:count/>, Repeat= <Arg Name="Repeat"/><Units:count/></Syslog>

-->

            <Syslog Severity="Important">Bearing for CrossLeg: <Arg Name="WpBearing"/><Units:degree/></Syslog>

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

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

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

            <Assign Id="PresetValueBeforeCrossLegSearchForPeak"><Sequence/><Arg Name="PatchPeakDistance"/><Units:meter/><Value>0</Value></Assign>

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

        </Aggregate>

        <Aggregate Id="SurfaceAfterCrossLegToPatchCenter">

            <Sequence/>

            <Break><Arg Name="CntRounds"/>
                <Mod><Arg Name="Repeat"/>
                </Mod>
                <Eq><Units:count/><Value>0</Value></Eq>
            </Break>

            <When>
                <Arg Name="CntRounds"/>
                <Mod><Arg Name="NumRoundsBtnCrossLegSurfacings"/>
                </Mod>
                <Eq><Units:count/><Value>0</Value></Eq>
                <And><Arg Name="CrossLegToPatchCenterCompleted"/></And>
                <And><Not><Arg Name="CrossLegSearchForPeakStarted"/></Not></And>
                <Or><Elapsed><Universal:time_fix/></Elapsed><Gt><Arg Name="NeedCommsTimePatchTracking"/></Gt><And><Arg Name="CrossLegToPatchCenterCompleted"/></And><And><Not><Arg Name="CrossLegSearchForPeakStarted"/></Not></And></Or>
            </When>

            <Call Id="CrossLegComms" RefId="NeedComms"/>

        </Aggregate>

        <Aggregate Id="TerminateMissionAfterCrossLegToPatchCenter">

            <Sequence/>

            <Break>
                <Not><Arg Name="CntRounds"/><Mod><Arg Name="Repeat"/>
                </Mod><Eq><Units:count/><Value>0</Value></Eq><And><Arg Name="CrossLegToPatchCenterCompleted"/></And></Not>
            </Break>

            <Syslog Severity="Important">Desired rounds of crosslegs completed and back to patch center. Terminate mission.</Syslog>

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

        </Aggregate>

        <Aggregate Id="CrossLegSearchForPeak">

            <Description>
                This is a long run to the edge of the patch to search for the
                peak.
            </Description>

            <Sequence/>

            <Break><Arg Name="WpDone"/>
                <Or><Arg Name="PatchDetectOn"/><Eq><False/></Eq></Or>
            </Break>

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

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

            <Syslog Severity="Important">Bearing for continued CrossLeg: <Arg Name="WpBearing"/><Units:degree/></Syslog>

            <Aggregate Id="CrossLegSearchForPeakNormal">

                <Break>
                    <Universal:distance_from_shore/>
                    <Le><Arg Name="MinOffshore"/><Add><Arg Name="MarginMinOffshore"/></Add></Le>
                    <And><Elapsed><Arg Name="ElapsedSinceStartOrLastDistCheck"/></Elapsed><Gt><Arg Name="WaitAfterBouncing"/></Gt></And>
                    <Or><Derivation:ShortestDistanceToPolygonSidesCalculator.loadAtStartup/><And><Derivation:ShortestDistanceToPolygonSidesCalculator.shortest_distance_to_polygon_sides/><Le><Arg Name="MinDistFromPolygonSides"/></Le></And><And><Elapsed><Arg Name="ElapsedSinceStartOrLastDistCheck"/></Elapsed><Gt><Arg Name="WaitAfterBouncing"/></Gt></And></Or>
                </Break>

<!--debug
                <Syslog Severity="Important">Continued CrossLeg: distance_from_shore = <Universal:distance_from_shore/><Units:meter/>. <Arg Name="MinOffshore"/><Units:meter/>+<Arg Name="MarginMinOffshore"/><Units:meter/>. </Syslog>

                <Syslog>CrossLeg. latitude = <Universal:latitude/><Units:degree/>, longitude = <Universal:longitude/><Units:degree/>, shortest_distance_to_polygon_sides = <Derivation:ShortestDistanceToPolygonSidesCalculator.shortest_distance_to_polygon_sides/><Units:meter/>.</Syslog>	    
-->

                <Guidance:Waypoint Id="CrossPeakWp">
                    <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>

                <Call Id="SyslogWpMaxDistanceReachedOnCrossLeg" RefId="SyslogWpMaxDistanceReached"/>

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

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

            </Aggregate>

            <Aggregate Id="CrossLegSearchForPeakTurnBack">

                <Break><Arg Name="FlagTurnBack"/>
                    <Or><Not><Universal:distance_from_shore/><Le><Arg Name="MinOffshore"/><Add><Arg Name="MarginMinOffshore"/></Add></Le><And><Elapsed><Arg Name="ElapsedSinceStartOrLastDistCheck"/></Elapsed><Gt><Arg Name="WaitAfterBouncing"/></Gt></And><Or><Derivation:ShortestDistanceToPolygonSidesCalculator.loadAtStartup/><And><Derivation:ShortestDistanceToPolygonSidesCalculator.shortest_distance_to_polygon_sides/><Le><Arg Name="MinDistFromPolygonSides"/></Le></And><And><Elapsed><Arg Name="ElapsedSinceStartOrLastDistCheck"/></Elapsed><Gt><Arg Name="WaitAfterBouncing"/></Gt></And></Or></Not></Or>
                </Break>

                <Call Id="SyslogTurnBackOnCrossLeg" RefId="SyslogTurnBack"/>

                <Assign><Sequence/><Arg Name="ElapsedSinceStartOrLastDistCheck"/><Units:hour/><Value>0</Value></Assign>

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

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

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

            </Aggregate>

        </Aggregate>

        <Aggregate Id="InLeg">

            <Sequence/>

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

            <Syslog Severity="Important">Bearing for InLeg: <Arg Name="WpBearing"/><Units:degree/></Syslog>

            <Guidance:Waypoint Id="InLegWp">
                <Description>
                    Simply drive back towards the patch center
                </Description>
                <Sequence/>
                <Timeout Duration="WpTimeout"/>
                <Setting><Guidance:Waypoint.distanceDelta/><Arg Name="PatchPeakDistance"/></Setting>
                <Setting><Guidance:Waypoint.distanceDeltaBearing/><Arg Name="WpBearing"/></Setting>
                <Setting><Guidance:Waypoint.waterFrame/><Arg Name="WaterFrame"/></Setting>
            </Guidance:Waypoint>

            <Assign><Sequence/><Arg Name="WpBearing"/><Arg Name="WpBearing"/><Sub><Arg Name="Turn"/></Sub><Mod><Arg Name="TwoPi"/>
            </Mod></Assign>

            <Assign Id="FlipSignOfTurnAngle"><Sequence/><Arg Name="Turn"/><Arg Name="Turn"/><Add><Arg Name="Reverse"/></Add><Mod><Arg Name="TwoPi"/>
            </Mod></Assign>

            <Syslog Severity="Important">Bearing for OutLeg: <Arg Name="WpBearing"/><Units:degree/></Syslog>

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

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

            <Assign Id="PresetValueBeforeOutLeg"><Sequence/><Arg Name="PatchPeakDistance"/><Units:meter/><Value>0</Value></Assign>

        </Aggregate>

        <Aggregate Id="OutLeg">

            <Description>
                Drive outward until edge of patch is detected.
            </Description>

            <Sequence/>

            <Break><Arg Name="WpDone"/>
                <Or><Arg Name="PatchDetectOn"/><Eq><False/></Eq></Or>
            </Break>

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

            <Assign><Sequence/><Arg Name="CntRounds"/><Arg Name="CntRounds"/><Add><Units:count/> <Value>1</Value></Add></Assign>

            <Aggregate Id="OutLegNormal">

                <Break>
                    <Universal:distance_from_shore/>
                    <Le><Arg Name="MinOffshore"/><Add><Arg Name="MarginMinOffshore"/></Add></Le>
                    <And><Elapsed><Arg Name="ElapsedSinceStartOrLastDistCheck"/></Elapsed><Gt><Arg Name="WaitAfterBouncing"/></Gt></And>
                    <Or><Derivation:ShortestDistanceToPolygonSidesCalculator.loadAtStartup/><And><Derivation:ShortestDistanceToPolygonSidesCalculator.shortest_distance_to_polygon_sides/><Le><Arg Name="MinDistFromPolygonSides"/></Le></And><And><Elapsed><Arg Name="ElapsedSinceStartOrLastDistCheck"/></Elapsed><Gt><Arg Name="WaitAfterBouncing"/></Gt></And></Or>
                </Break>

<!--debug
                <Syslog Severity="Important">OutLeg: distance_from_shore = <Universal:distance_from_shore/><Units:meter/>. <Arg Name="MinOffshore"/><Units:meter/>+<Arg Name="MarginMinOffshore"/><Units:meter/>. </Syslog>
-->

                <Guidance:Waypoint Id="OutLegWp">
                    <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>

                <Call Id="SyslogWpMaxDistanceReachedOnOutLeg" RefId="SyslogWpMaxDistanceReached"/>

                <Assign Id="AssignPatchDetectOffInOutLeg"><Sequence/><Arg Name="PatchDetectOn"/><False/></Assign>

                <Assign Id="AssignWpDoneInOutLeg"><Sequence/><Arg Name="WpDone"/><True/></Assign>

            </Aggregate>

            <Aggregate Id="OutLegTurnBack">

                <Break><Arg Name="FlagTurnBack"/>
                    <Or><Not><Universal:distance_from_shore/><Le><Arg Name="MinOffshore"/><Add><Arg Name="MarginMinOffshore"/></Add></Le><And><Elapsed><Arg Name="ElapsedSinceStartOrLastDistCheck"/></Elapsed><Gt><Arg Name="WaitAfterBouncing"/></Gt></And><Or><Derivation:ShortestDistanceToPolygonSidesCalculator.loadAtStartup/><And><Derivation:ShortestDistanceToPolygonSidesCalculator.shortest_distance_to_polygon_sides/><Le><Arg Name="MinDistFromPolygonSides"/></Le></And><And><Elapsed><Arg Name="ElapsedSinceStartOrLastDistCheck"/></Elapsed><Gt><Arg Name="WaitAfterBouncing"/></Gt></And></Or></Not></Or>
                </Break>

                <Call Id="SyslogTurnBackOnOutLeg" RefId="SyslogTurnBack"/>

                <Assign><Sequence/><Arg Name="ElapsedSinceStartOrLastDistCheck"/><Units:hour/><Value>0</Value></Assign>

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

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

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

            </Aggregate>

        </Aggregate>

    </Aggregate>

</Mission>
