Beispiel #1
0
        /// <summary>
        /// Get the GPS data from the ensemble using the database reader.
        /// </summary>
        /// <param name="reader">Database reader.</param>
        /// <returns>If NMEA data exist, pass the data, or return NULL.</returns>
        public static GpsData GetGpsData(DbDataReader reader)
        {
            // Get the NMEA data
            string jsonNmea = reader["NmeaDS"].ToString();

            DbDataHelper.GpsData gpsData = null;
            if (!string.IsNullOrEmpty(jsonNmea))
            {
                // Convert to a JSON object
                JObject  ensNmea     = JObject.Parse(jsonNmea);
                string[] nmeaStrings = ensNmea["NmeaStrings"].ToObject <string[]>();

                if (nmeaStrings != null && nmeaStrings.Length > 0)
                {
                    gpsData = DbDataHelper.DecodeNmea(nmeaStrings);
                }
            }

            // Check if we have a valid GPS speed
            if (gpsData != null && gpsData.GPVTG != null && gpsData.GPHDT != null && gpsData.GPVTG.IsValid && gpsData.GPHDT.IsValid)
            {
                // Convert the speed and east and north component
                // Speed from the GPS
                double speed = gpsData.GPVTG.Speed.ToMetersPerSecond().Value;

                // Calculate the East and North component of the GPS speed
                gpsData.BackupShipEast      = Convert.ToSingle(speed * Math.Sin(gpsData.GPHDT.Heading.ToRadians().Value));
                gpsData.BackupShipNorth     = Convert.ToSingle(speed * Math.Cos(gpsData.GPHDT.Heading.ToRadians().Value));
                gpsData.IsBackShipSpeedGood = true;
            }
            else if (gpsData != null && gpsData.GPHDT != null && gpsData.GPRMC != null && gpsData.GPRMC.IsValid && gpsData.GPHDT.IsValid)
            {
                // Convert the speed and east and north component
                // Speed from the GPS
                double speed = gpsData.GPRMC.Speed.ToMetersPerSecond().Value;

                // Calculate the East and North component of the GPS speed
                gpsData.BackupShipEast      = Convert.ToSingle(speed * Math.Sin(gpsData.GPHDT.Heading.ToRadians().Value));
                gpsData.BackupShipNorth     = Convert.ToSingle(speed * Math.Cos(gpsData.GPHDT.Heading.ToRadians().Value));
                gpsData.IsBackShipSpeedGood = true;
            }

            return(gpsData);
        }
        /// <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);
        }