/// <summary>
        /// Create a data point based off the Bottom Track data.
        /// </summary>
        /// <param name="data">Data from the ensemble.</param>
        /// <param name="prevBtTime">Previous Bottom Track time.</param>
        /// <param name="prevBtEast">Previous Bottom Track East velocity.</param>
        /// <param name="prevBtNorth">Previous Bottom Track North velocity.</param>
        /// <param name="declination">Declination or heading offset.</param>
        /// <param name="xOffset">X offset.</param>
        /// <param name="yOffset">Y offset.</param>
        /// <returns></returns>
        private DataPoint CalculateDistanceTraveledPoint(DbDataHelper.VelocityMagDir data, double prevBtTime, double prevBtEast, double prevBtNorth, double declination = 0.0, double xOffset = 0.0, double yOffset = 0.0)
        {
            double dT = data.BtFirstPingTime - prevBtTime;

            if (prevBtEast == BAD_VELOCITY || prevBtNorth == BAD_VELOCITY)
            {
                prevBtEast  = 0.0;
                prevBtNorth = 0.0;
            }

            double BtE = 0.5 * dT * (data.BtEastVel + prevBtEast);
            double BtN = 0.5 * dT * (data.BtNorthVel + prevBtNorth);

            double BtEarthMag = Math.Sqrt((BtE * BtE) + (BtN * BtN));
            double BtEarthDir = (Math.Atan2(BtE, BtN) * (180.0 / Math.PI)) + declination;

            if (BtEarthDir < 0.0)
            {
                BtEarthDir = 360.0 + BtEarthDir;
            }

            // Generate X,Y point
            double x = xOffset + (BtEarthMag * Math.Sin(MathHelper.DegreeToRadian(BtEarthDir)));
            double y = yOffset + (BtEarthMag * Math.Cos(MathHelper.DegreeToRadian(BtEarthDir)));

            return(new DataPoint(x, y));
        }
        /// <summary>
        /// Query the data from the database.
        /// </summary>
        /// <param name="cnn">SQLite connection.</param>
        /// <param name="query">Query for the data.</param>
        /// <param name="magScale">Magnitude scale.</param>
        /// <param name="minIndex">Minimum index.</param>
        /// <param name="maxIndex">Maximum index.</param>
        /// <returns></returns>
        private List <ShipTrackData> QueryDataFromDb(SQLiteConnection cnn, string query, double magScale, int minIndex = 0, int maxIndex = 0)
        {
            // Init list
            double prevBtEast  = DbDataHelper.BAD_VELOCITY;
            double prevBtNorth = DbDataHelper.BAD_VELOCITY;
            double prevBtTime  = 0.0;

            // Init the new series data
            List <ShipTrackData> stDataList = new List <ShipTrackData>();

            //stData.MagScale = magScale;

            // Ensure a connection was made
            if (cnn == null)
            {
                return(null);
            }

            using (DbCommand cmd = cnn.CreateCommand())
            {
                cmd.CommandText = query;

                // Get Result
                DbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    ShipTrackData stData = new ShipTrackData();

                    // Update the status
                    StatusProgress++;
                    StatusMsg = reader["EnsembleNum"].ToString();

                    // Get the Ensemble number and Date and time
                    stData.DateTime = reader["DateTime"].ToString();
                    stData.EnsNum   = reader["EnsembleNum"].ToString();

                    // Plot the lat/lon
                    //stData.LatLon = reader["Position"].ToString();

                    // Heading
                    //DbDataHelper.HPR hpr = DbDataHelper.GetHPR(reader);
                    //stData.Heading = hpr.Heading;

                    //// Get the range bin
                    //int rangeBin = DbDataHelper.GetRangeBin(reader);

                    //// Get the magnitude data
                    //string jsonEarth = reader["EarthVelocityDS"].ToString();
                    //if (!string.IsNullOrEmpty(jsonEarth))
                    //{
                    //    // Convert to a JSON object
                    //    JObject ensEarth = JObject.Parse(jsonEarth);

                    //    // Average the data
                    //    avgMag = DbDataHelper.GetAvgMag(ensEarth, IsMarkBadBelowBottom, rangeBin, DbDataHelper.BAD_VELOCITY);
                    //    avgDir = DbDataHelper.GetAvgDir(ensEarth, IsMarkBadBelowBottom, rangeBin, DbDataHelper.BAD_VELOCITY);

                    //    //Debug.WriteLine(string.Format("Avg Dir: {0} Avg Mag: {1}", avgDir, avgMag));
                    //}

                    if (IsUseGpsSpeedBackup)
                    {
                        // Get the GPS data from the database
                        DbDataHelper.GpsData gpsData = DbDataHelper.GetGpsData(reader);

                        // Check for a backup value for BT East and North speed from the GPS if a Bottom Track value is never found
                        if (Math.Round(prevBtEast, 4) == BAD_VELOCITY && gpsData != null && gpsData.IsBackShipSpeedGood)
                        {
                            prevBtEast  = gpsData.BackupShipEast;
                            prevBtNorth = gpsData.BackupShipNorth;
                        }
                    }

                    // Get the velocity
                    DbDataHelper.VelocityMagDir velMagDir = DbDataHelper.CreateVelocityVectors(reader, prevBtEast, prevBtNorth, true, true);

                    if (velMagDir.IsBtVelGood)
                    {
                        // Get the average range
                        //stData.AvgRange = DbDataHelper.GetAverageRange(reader);
                        DataPoint pt = CalculateDistanceTraveledPoint(velMagDir, prevBtTime, prevBtEast, prevBtNorth);
                        stData.ShipData.Add(pt);

                        // Store the backup value
                        prevBtEast  = velMagDir.BtEastVel;
                        prevBtNorth = velMagDir.BtNorthVel;
                        prevBtTime  = velMagDir.BtFirstPingTime;
                    }

                    // Add the data to the list
                    stDataList.Add(stData);
                }
            }

            return(stDataList);
        }