/// <summary>
        /// Update the Bottom Track series with the latest depth.
        /// </summary>
        /// <param name="btSeries">Bottom Track series to update.</param>
        /// <param name="reader">Database reader.</param>
        /// <param name="ensIndex">Index in the plot based off the reader.</param>
        private void ParseBtData(ref AreaSeries btSeries, DbDataReader reader, int ensIndex)
        {
            try
            {
                // Convert to a JSON object
                string  jsonEnsemble = reader["EnsembleDS"].ToString();
                JObject ensData      = JObject.Parse(jsonEnsemble);
                int     numBins      = ensData["NumBins"].ToObject <int>();

                // Update the bottom track line series
                //int rangeBin = GetRangeBin(reader);
                int rangeBin = DbDataHelper.GetRangeBin(reader);
                if (rangeBin == DbDataHelper.BAD_BOTTOM_BIN && _prevGoodBottomBin != DbDataHelper.BAD_BOTTOM_BIN)
                {
                    // Use the backup value if bad
                    rangeBin = _prevGoodBottomBin;
                }

                // Store as backup
                if (rangeBin != DbDataHelper.BAD_BOTTOM_BIN)
                {
                    _prevGoodBottomBin = rangeBin;
                }


                // Only plot the range if it is found
                if (rangeBin > 0)
                {
                    // Create a new data point for the bottom track line
                    // This will be the (ensemble count, range bin)
                    btSeries.Points.Add(new DataPoint(ensIndex, rangeBin));

                    // Add the second point for the shaded area
                    if (rangeBin < numBins)
                    {
                        // Less then the number of bins, so go to the end of the number of bins
                        btSeries.Points2.Add(new DataPoint(ensIndex, numBins));
                    }
                    else
                    {
                        // This is the deepest point
                        btSeries.Points2.Add(new DataPoint(ensIndex, rangeBin));
                    }
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("Error parsing Bottom Track data", e);
                return;
            }
        }
Exemple #2
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>
        /// Process the row from the DB.  A row represents an ensemble.
        /// </summary>
        /// <param name="reader">Database connection data.</param>
        /// <returns>Direction data for a row.</returns>
        private double[] ParseDirData(DbDataReader reader)
        {
            try
            {
                // Get data
                DbDataHelper.VelocityMagDir velMagDir = DbDataHelper.CreateVelocityVectors(reader, _backupBtEast, _backupBtNorth, _IsRemoveShipSpeed, _IsMarkBadBelowBottom);

                // Store the backup value
                if (velMagDir.IsBtVelGood)
                {
                    _backupBtEast  = velMagDir.BtEastVel;
                    _backupBtNorth = velMagDir.BtNorthVel;
                }

                return(velMagDir.DirectionYNorth);
            }
            catch (Exception e)
            {
                Debug.WriteLine("Error parsing the Earth Velocity Direction data row", e);
                return(null);
            }
        }
        /// <summary>
        /// Process the row from the DB.  A row represents an ensemble.
        /// </summary>
        /// <param name="reader">Database connection data.</param>
        /// <returns>Magnitude data for a row.</returns>
        private double[] ParseAmpData(DbDataReader reader)
        {
            try
            {
                // Get Range Bin if marking bad below bottom
                int rangeBin = BAD_BOTTOM_BIN;
                if (IsMarkBadBelowBottom)
                {
                    //rangeBin = GetRangeBin(reader);
                    rangeBin = DbDataHelper.GetRangeBin(reader);
                    if (rangeBin == DbDataHelper.BAD_BOTTOM_BIN && _prevGoodBottomBin != DbDataHelper.BAD_BOTTOM_BIN)
                    {
                        // Use the backup value if bad
                        rangeBin = _prevGoodBottomBin;
                    }

                    // Store as backup
                    if (rangeBin != DbDataHelper.BAD_BOTTOM_BIN)
                    {
                        _prevGoodBottomBin = rangeBin;
                    }
                }

                // Get the data as a JSON string
                string jsonData = reader["AmplitudeDS"].ToString();

                if (!string.IsNullOrEmpty(jsonData))
                {
                    // Convert to a JSON object
                    JObject ensData = JObject.Parse(jsonData);

                    // Get the number of bins
                    int numBins  = ensData["NumElements"].ToObject <int>();
                    int numBeams = ensData["ElementsMultiplier"].ToObject <int>();

                    double[] data = new double[numBins];
                    for (int bin = 0; bin < numBins; bin++)
                    {
                        int    avgCnt = 0;
                        double avg    = 0.0;

                        // Average the amplitude for each beam data together
                        for (int beam = 0; beam < numBeams; beam++)
                        {
                            if (ensData["AmplitudeData"][bin][beam].ToObject <double>() != BAD_VELOCITY)
                            {
                                avgCnt++;
                                avg += ensData["AmplitudeData"][bin][beam].ToObject <double>();
                            }
                        }

                        // Check if Mark bad below bottom
                        if (_IsMarkBadBelowBottom && rangeBin > BAD_BOTTOM_BIN && bin >= rangeBin)
                        {
                            // Mark bad below bottom
                            data[bin] = BAD_AMPLITUDE;
                        }
                        // Add average data to the array
                        else if (avgCnt > 0)
                        {
                            data[bin] = avg / avgCnt;
                        }
                        else
                        {
                            data[bin] = BAD_AMPLITUDE;
                        }
                    }

                    return(data);
                }

                return(null);
            }
            catch (Exception e)
            {
                Debug.WriteLine("Error parsing the Amplitude data row", e);
                return(null);
            }
        }
        /// <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 backupBtEast  = DbDataHelper.BAD_VELOCITY;
            double backupBtNorth = DbDataHelper.BAD_VELOCITY;

            // 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(backupBtEast, 4) == BAD_VELOCITY && gpsData.IsBackShipSpeedGood)
                        {
                            backupBtEast  = gpsData.BackupShipEast;
                            backupBtNorth = gpsData.BackupShipNorth;
                        }
                    }

                    // Get the velocity
                    stData.VelMagDir = DbDataHelper.CreateVelocityVectors(reader, backupBtEast, backupBtNorth, true, true);

                    // Get the average range
                    stData.AvgRange = DbDataHelper.GetAverageRange(reader);

                    // Store the backup value
                    if (stData.VelMagDir.IsBtVelGood)
                    {
                        backupBtEast  = stData.VelMagDir.BtEastVel;
                        backupBtNorth = stData.VelMagDir.BtNorthVel;
                    }

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

            return(stDataList);
        }