<?xml version="1.0" encoding="utf-8"?>
<Mission xmlns="Tethys" 
         xmlns:Control="Tethys/Control" 
         xmlns:Guidance="Tethys/Guidance" 
         xmlns:Sensor="Tethys/Sensor"
         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/Sensor http://okeanids.mbari.org/tethys/Xml/Sensor.xsd
                             Tethys/Units http://okeanids.mbari.org/tethys/Xml/Units.xsd
                             Tethys/Universal http://okeanids.mbari.org/tethys/Xml/Universal.xsd"
         Id="square_cal_validation">

    <DefineArg Name="Depth"><Units:meter /><Value>20.0</Value></DefineArg>

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

    <DefineArg Name="LegDuration"><Units:second /><Value>1000</Value></DefineArg>

    <DefineArg Name="LegDurationSurface"><Units:second /><Value>300</Value></DefineArg>

    <DefineArg Name="CompassNorthHeading"><Units:degree /><Value>0</Value></DefineArg>

    <DefineArg Name="CompassWestHeading"><Units:degree /><Value>270</Value></DefineArg>

    <DefineArg Name="CompassSouthHeading"><Units:degree /><Value>180</Value></DefineArg>

    <DefineArg Name="CompassEastHeading"><Units:degree /><Value>90</Value></DefineArg>

    <DefineArg Name="MinAltitude"><Units:meter/><Value>7</Value></DefineArg>

    <DefineArg Name="MaxDepth"><Units:meter/><Value>30</Value></DefineArg>

    <DefineArg Name="MinWaterDepth"><Units:meter/><Value>20</Value></DefineArg>

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

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

    <DefineArg Name="TimeoutDuration"><Units:minute/><Value>270</Value></DefineArg>

    <DefineArg Name="Wpt1Lat"><Units:degree/><Value>36.805</Value></DefineArg>

    <DefineArg Name="Wpt1Lon"><Units:degree/><Value>-121.825</Value></DefineArg>

    <DefineArg Name="Wpt2Lat"><Units:degree/><Value>36.815</Value></DefineArg>

    <DefineArg Name="Wpt2Lon"><Units:degree/><Value>-121.825</Value></DefineArg>

    <DefineArg Name="Wpt3Lat"><Units:degree/><Value>36.805</Value></DefineArg>

    <DefineArg Name="Wpt3Lon"><Units:degree/><Value>-121.825</Value></DefineArg>

    <DefineArg Name="Wpt4Lat"><Units:degree/><Value>36.805</Value></DefineArg>

    <DefineArg Name="Wpt4Lon"><Units:degree/><Value>-121.837</Value></DefineArg>

    <DefineArg Name="SurfaceGPSFixes"><Units:count/><Value>20</Value></DefineArg>

    <DefineArg Name="BuoyancyNeutral"><Control:VerticalControl.buoyancyNeutral/></DefineArg>

    <Timeout Duration="TimeoutDuration"/>

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

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

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

    <Insert Filename="Insert/SurfaceSpiral.xml" Id="SURFACESPIRALCOMMS" />

    <ReadData Strategy="AllDevices">
        <Universal:platform_orientation/>
    </ReadData>

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

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

<!-- 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><Units:minute/><Value>40</Value></Gt></And>
            <Or><Elapsed><Universal:time_fix/></Elapsed><Gt><Units:minute/><Value>45</Value></Gt></Or>
        </When>

        <Call Id="NEEDCOMMS" RefId="SURFACESPIRALCOMMS"/>

    </Aggregate>

    <Aggregate Id="SurfaceGPS">

        <When>
            <Called/>
        </When>

        <Aggregate Id="SurfaceGPSLoop">

            <Sequence/>

            <Timeout Duration="LegDurationSurface"/>

            <ReadDatum Repeat="SurfaceGPSFixes">
                <Universal:time_fix/>
            </ReadDatum>

        </Aggregate>

        <Guidance:Pitch>
            <Parallel/>
            <Setting><Guidance:Pitch.depth /><Units:meter/><Value>0</Value></Setting>
        </Guidance:Pitch>

    </Aggregate>

    <Aggregate Id="DiveandPump">

        <When>
            <Called/>
        </When>

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

        <Guidance:Pitch>
            <Parallel/>
            <Setting><Guidance:Pitch.depth/><Units:meter/><Value>10</Value></Setting>
        </Guidance:Pitch>

        <Guidance:SetSpeed>
            <Parallel/>
            <Setting><Guidance:SetSpeed.speed/><Units:meter_per_second/><Value>1</Value></Setting>
        </Guidance:SetSpeed>

    </Aggregate>

<!-- ****************** Northerly Leg ****************** -->

<!-- Go to Wpt1 and start from there -->

    <Aggregate Id="WaypointOne">

        <Sequence/>

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

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

        <Guidance:Waypoint Id="Waypoint1">
            <Sequence/>
            <Setting><Guidance:Waypoint.latitude/><Arg Name="Wpt1Lat"/></Setting>
            <Setting><Guidance:Waypoint.longitude/><Arg Name="Wpt1Lon"/></Setting>
        </Guidance:Waypoint>

    </Aggregate>

    <Call RefId="SURFACESPIRALCOMMS"/>

<!-- Go North  -->

    <Aggregate Id="North">

        <Sequence/>

        <Call RefId="SurfaceGPS"/>

        <Call RefId="DiveandPump"/>

        <Guidance:Wait>
            <Sequence/>
            <Setting><Guidance:Wait.duration /><Arg Name="LegDuration" /></Setting>
        </Guidance:Wait>

        <Call RefId="SurfaceGPS"/>

        <Guidance:Point>
            <Parallel/>
            <Setting><Guidance:Point.heading /><Arg Name="CompassNorthHeading" /></Setting>
        </Guidance:Point>

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

    <Call RefId="SURFACESPIRALCOMMS"/>

<!-- ****************** Southerly Leg ****************** -->

<!-- Go to Wpt2 and start from there -->

    <Aggregate Id="WaypointTwo">

        <Sequence/>

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

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

        <Guidance:Waypoint Id="Waypoint2">
            <Sequence/>
            <Setting><Guidance:Waypoint.latitude/><Arg Name="Wpt2Lat"/></Setting>
            <Setting><Guidance:Waypoint.longitude/><Arg Name="Wpt2Lon"/></Setting>
        </Guidance:Waypoint>

    </Aggregate>

    <Call RefId="SURFACESPIRALCOMMS"/>

<!-- Go South  -->

    <Aggregate Id="South">

        <Sequence/>

        <Call RefId="SurfaceGPS"/>

        <Call RefId="DiveandPump"/>

        <Guidance:Wait>
            <Sequence/>
            <Setting><Guidance:Wait.duration /><Arg Name="LegDuration" /></Setting>
        </Guidance:Wait>

        <Call RefId="SurfaceGPS"/>

        <Guidance:Point>
            <Parallel/>
            <Setting><Guidance:Point.heading /><Arg Name="CompassSouthHeading" /></Setting>
        </Guidance:Point>

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

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

    </Aggregate>

    <Call RefId="SURFACESPIRALCOMMS"/>

<!-- ****************** Westerly Leg ****************** -->

<!-- Go to Wpt3 and start from there -->

    <Aggregate Id="WaypointThree">

        <Sequence/>

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

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

        <Guidance:Waypoint Id="WaypointW3">
            <Sequence/>
            <Setting><Guidance:Waypoint.latitude/><Arg Name="Wpt3Lat"/></Setting>
            <Setting><Guidance:Waypoint.longitude/><Arg Name="Wpt3Lon"/></Setting>
        </Guidance:Waypoint>

    </Aggregate>

    <Call RefId="SURFACESPIRALCOMMS"/>

<!-- Go West  -->

    <Aggregate Id="West">

        <Sequence/>

        <Call RefId="SurfaceGPS"/>

        <Call RefId="DiveandPump"/>

        <Guidance:Wait>
            <Sequence/>
            <Setting><Guidance:Wait.duration /><Arg Name="LegDuration" /></Setting>
        </Guidance:Wait>

        <Call RefId="SurfaceGPS"/>

        <Guidance:Point>
            <Parallel/>
            <Setting><Guidance:Point.heading /><Arg Name="CompassWestHeading" /></Setting>
        </Guidance:Point>

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

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

    </Aggregate>

    <Call RefId="SURFACESPIRALCOMMS"/>

<!-- ****************** Easterly Leg ****************** -->

<!-- Go to Wpt4b and start from there -->

    <Aggregate Id="WaypointFour">

        <Sequence/>

        <Call Id="DIVEFORWPT4" RefId="DiveandPump"/>

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

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

        <Guidance:Waypoint Id="WaypointW4">
            <Sequence/>
            <Setting><Guidance:Waypoint.latitude/><Arg Name="Wpt4Lat"/></Setting>
            <Setting><Guidance:Waypoint.longitude/><Arg Name="Wpt4Lon"/></Setting>
        </Guidance:Waypoint>

    </Aggregate>

    <Call RefId="SURFACESPIRALCOMMS"/>

<!-- Go East  -->

    <Aggregate Id="East">

        <Sequence/>

        <Call RefId="SurfaceGPS"/>

        <Call RefId="DiveandPump"/>

        <Guidance:Wait>
            <Sequence/>
            <Setting><Guidance:Wait.duration /><Arg Name="LegDuration" /></Setting>
        </Guidance:Wait>

        <Call RefId="SurfaceGPS"/>

        <Guidance:Point>
            <Parallel/>
            <Setting><Guidance:Point.heading /><Arg Name="CompassEastHeading" /></Setting>
        </Guidance:Point>

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

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

    </Aggregate>

    <Call RefId="SURFACESPIRALCOMMS"/>

</Mission>
