Exemplo n.º 1
0
        public void JsonSerializationAndDeserializationWorks()
        {
            var target = Longitude.FromDegrees(89.5);
            var actual = SerializationTester <Longitude> .JsonSerializeAndDeserialize(target);

            Assert.AreEqual(target, actual);
        }
Exemplo n.º 2
0
        public void InitializationWithDegrees()
        {
            var target = Longitude.FromDegrees(45);

            Assert.AreEqual(45.0, target.Degrees);
            Assert.AreEqual(Math.PI / 4, target.Radians);
        }
Exemplo n.º 3
0
        public void MagneticVariationCalculationCacheTest()
        {
            var point1 = new CoordinatePoint(Latitude.FromDegrees(60.12345888), Longitude.FromDegrees(30.12345888));

            var watch           = Stopwatch.StartNew();
            var point1Variation = MagneticVariation.GetMagneticVariation(point1, 6, 0, DateTimeOffset.UtcNow,
                                                                         MagneticVariation.MagneticVariationModels.WMM2015, out _);

            watch.Stop();

            var elapsed1 = watch.Elapsed;

            point1Variation = MagneticVariation.GetMagneticVariation(point1, 6, 0, DateTimeOffset.UtcNow,
                                                                     MagneticVariation.MagneticVariationModels.WMM2015, out _);
            point1Variation = MagneticVariation.GetMagneticVariation(point1, 6, 0, DateTimeOffset.UtcNow,
                                                                     MagneticVariation.MagneticVariationModels.WMM2015, out _);
            point1Variation = MagneticVariation.GetMagneticVariation(point1, 6, 0, DateTimeOffset.UtcNow,
                                                                     MagneticVariation.MagneticVariationModels.WMM2015, out _);

            watch.Restart();
            point1Variation = MagneticVariation.GetMagneticVariation(point1, 6, 0, DateTimeOffset.UtcNow,
                                                                     MagneticVariation.MagneticVariationModels.WMM2015, out _);
            watch.Stop();

            var elapsed2 = watch.Elapsed;

            // first run takes more time
            Assert.IsTrue(elapsed1 > elapsed2);
        }
Exemplo n.º 4
0
 public void Operators()
 {
     Assert.IsTrue(Longitude.FromDegrees(1) == Longitude.FromDegrees(1));
     Assert.IsFalse(Longitude.FromDegrees(1) == Longitude.FromDegrees(2));
     Assert.IsTrue(Longitude.FromDegrees(1) != Longitude.FromDegrees(2));
     Assert.IsFalse(Longitude.FromDegrees(1) != Longitude.FromDegrees(1));
     Assert.IsTrue(Longitude.FromDegrees(1) < Longitude.FromDegrees(2));
     Assert.IsTrue(Longitude.FromDegrees(1) <= Longitude.FromDegrees(2));
     Assert.IsTrue(Longitude.FromDegrees(2) <= Longitude.FromDegrees(2));
     Assert.IsTrue(Longitude.FromDegrees(2) > Longitude.FromDegrees(1));
     Assert.IsTrue(Longitude.FromDegrees(2) >= Longitude.FromDegrees(1));
     Assert.IsTrue(Longitude.FromDegrees(2) >= Longitude.FromDegrees(2));
 }
Exemplo n.º 5
0
        public void CoordinatesRoundingAndMagVarCalculationTest()
        {
            var point1 = new CoordinatePoint(Latitude.FromDegrees(60.12345888), Longitude.FromDegrees(30.12345888));
            var point2 = new CoordinatePoint(Latitude.FromDegrees(60.12345666), Longitude.FromDegrees(30.12345666));

            var point1Variation = MagneticVariation.GetMagneticVariation(point1, 6, 0, DateTimeOffset.UtcNow,
                                                                         MagneticVariation.MagneticVariationModels.WMM2015, out _);
            var point2Variation = MagneticVariation.GetMagneticVariation(point2, 6, 0, DateTimeOffset.UtcNow,
                                                                         MagneticVariation.MagneticVariationModels.WMM2015, out _);

            Assert.AreNotEqual(point1Variation, point2Variation);

            point1Variation = MagneticVariation.GetMagneticVariation(point1, 5, 0, DateTimeOffset.UtcNow,
                                                                     MagneticVariation.MagneticVariationModels.WMM2015, out _);
            point2Variation = MagneticVariation.GetMagneticVariation(point2, 5, 0, DateTimeOffset.UtcNow,
                                                                     MagneticVariation.MagneticVariationModels.WMM2015, out _);

            Assert.AreEqual(point1Variation, point2Variation);
        }
Exemplo n.º 6
0
        /// <summary>
        /// This method returns the minimum northing value (in meters) for the specified latitude band.
        /// </summary>
        /// <param name="latBand">UTM latitude band character ('C' to 'X", not including 'I' or 'O')</param>
        /// <returns>minimum northing value for the specified latitude band</returns>
        public int GetMinNorthing(char latBand)
        {
            if (latBand < 'C' || latBand > 'X' || latBand == 'I' || latBand == 'O')
            {
                return(-1);
            }

            Longitude centLon = Longitude.FromDegrees(-3.0);
            Longitude edgeLon = Longitude.FromDegrees(-6.0);

            // min Northings are inclusive (northing must be greater than or equal to this min)
            // min Northings for Northern Hemisphere are along a central meridian, but for
            // the Southern Hemisphere, they occur at the edges of a lonZone (+/- 3 deg from CM)
            Longitude lng    = (latBand < 'N') ? edgeLon : centLon;
            Latitude  minLat = GetMinLatitude(latBand);

            double minNorthing = GeodeticConversion(minLat, lng).Northing;

            return((int)Math.Round(minNorthing));
        }
Exemplo n.º 7
0
        public void GetMagneticVariationTest2()
        {
            // expected values from https://www.ngdc.noaa.gov/geomag/WMM/data/WMM2015/WMM2015_Report.pdf, page 20

            var date1  = new DateTimeOffset(2015, 1, 1, 0, 0, 0, 0, TimeSpan.Zero);
            var point1 = new CoordinatePoint(Latitude.FromDegrees(80), Longitude.FromDegrees(0));

            var point1ExpectedDecination = -3.85 * _toRadians;

            Assert.AreEqual(point1ExpectedDecination,
                            MagneticVariation.GetMagneticVariation(point1, 5, 0, date1, MagneticVariation.MagneticVariationModels.WMM2015
                                                                   , out _), 0.00002d);


            var date2  = new DateTimeOffset(2015, 1, 1, 0, 0, 0, 0, TimeSpan.Zero);
            var point2 = new CoordinatePoint(Latitude.FromDegrees(0), Longitude.FromDegrees(120));

            var point2ExpectedDecination = 0.57 * _toRadians;

            Assert.AreEqual(point2ExpectedDecination,
                            MagneticVariation.GetMagneticVariation(point2, 5, 0, date2, MagneticVariation.MagneticVariationModels.WMM2015
                                                                   , out _), 0.00001d);
        }
Exemplo n.º 8
0
        public void PositionShouldCast()
        {
            var onemilimeter = .001;

            Coordinate actual   = new Position("4900.0", "N", "12300.0", "W");
            var        expected = new Coordinate(Latitude.FromDegrees(49), Longitude.FromDegrees(-123));

            Assert.InRange(DistanceInMeters(expected, actual), 0, onemilimeter);

            actual = new Position()
            {
                Latitude            = 49,
                LatitudeHemisphere  = NorthSouth.South,
                Longitude           = 123,
                LongitudeHemisphere = EastWest.West
            };
            expected = new Coordinate(Latitude.FromDegrees(-49), Longitude.FromDegrees(-123));
            Assert.InRange(DistanceInMeters(expected, actual), 0, onemilimeter);

            actual = new Position()
            {
                Latitude            = 49,
                LatitudeHemisphere  = NorthSouth.North,
                Longitude           = 123,
                LongitudeHemisphere = EastWest.East
            };
            expected = new Coordinate(Latitude.FromDegrees(49), Longitude.FromDegrees(123));
            Assert.InRange(DistanceInMeters(expected, actual), 0, 1);

            Position pexpected = new Position("4900.0", "N", "12300.0", "W");
            Position pactual   = (Coordinate)pexpected; // explicit cast to Coordinate, then implicit back to Position

            Assert.InRange(pactual.Latitude, 48.9, 49.1);
            Assert.Equal(pactual.LatitudeHemisphere, NorthSouth.North);
            Assert.InRange(pactual.Longitude, 122.9, 123.1);
            Assert.Equal(pactual.LongitudeHemisphere, EastWest.West);
        }
Exemplo n.º 9
0
 public void Over90DegreesThrows()
 {
     Assert.ThrowsException <ArgumentOutOfRangeException>(() => Longitude.FromDegrees(180.1));
 }
Exemplo n.º 10
0
 public void ToStringWorks()
 {
     Assert.AreEqual("45,0", Longitude.FromDegrees(45).ToString());
 }
Exemplo n.º 11
0
 public void Equals()
 {
     Assert.AreEqual(Longitude.FromDegrees(58), Longitude.FromDegrees(58));
     Assert.AreNotEqual(Longitude.FromDegrees(58), Longitude.FromDegrees(58.011));
     Assert.AreNotEqual(Longitude.FromDegrees(58), new object());
 }
Exemplo n.º 12
0
 public static Longitude MostWestern(this Position[] positions) => Longitude.FromDegrees(positions.Min(p => p.Longitude.Degrees));
Exemplo n.º 13
0
        static void Main(string[] args)
        {
            var parserResult = Parser.Default.ParseArguments <Options>(args);

            if (parserResult.Tag == ParserResultType.Parsed)
            {
                var options = ((Parsed <Options>)parserResult).Value;
                //enlarge console window width
                Console.WindowWidth = 120;

                //convert channels from string to enum
                var outputchannels = new List <ChannelType>();
                if (options.Channels != null && options.Channels.Any())
                {
                    outputchannels.AddRange(options.Channels.Select(channel => (ChannelType)Enum.Parse(typeof(ChannelType), channel)));
                }

                var stopWatch = new Stopwatch();

                //Read Files
                var data = ReadFile(options.InputFile, options.Verbose);

                if (data == null)
                {
                    Console.WriteLine("Can't read frames from file");
                    Console.WriteLine($"{Environment.NewLine}Please press any key to exit...");
                    Console.ReadKey(true);
                    return;
                }

                #region Depth Adjust

                if (!string.IsNullOrWhiteSpace(options.DepthAdjustFile))
                {
                    var adjust = ReadFile(options.DepthAdjustFile, options.Verbose);
                    var da     = new DepthAdjuster(data.Frames, adjust.Frames);

                    if (options.Verbose)
                    {
                        Console.WriteLine("Depth adjust option enabled.");
                        Console.WriteLine("Adjust file name:\"{0}\".", options.DepthAdjustFile);

                        da.NearestPointsFound += (o, e) =>
                        {
                            Console.WriteLine("Nearest points are:\nBase - {0} with {1} depth.\nAdjust - {2} with {3}.",
                                              e.FirstPoint.Point, e.FirstPoint.Depth, e.SecondPoint.Point, e.SecondPoint.Depth);
                            Console.WriteLine("Distance = {0}", e.Distance);
                        };

                        Console.WriteLine("Looking for the nearest points.....");
                    }

                    stopWatch.Start();
                    var points = da.AdjustDepth();

                    if (options.Verbose)
                    {
                        Console.WriteLine("Adjust time: " + stopWatch.Elapsed + "\n");
                    }
                    stopWatch.Reset();

                    //add points to original sequence
                    foreach (var point in points)
                    {
                        data.Frames.Add((Frame)point);
                    }
                }
                #endregion

                #region Depth Shift

                //add or substrate depth shift for all frames in data
                if (!string.IsNullOrWhiteSpace(options.DepthShift))
                {
                    if (options.Verbose)
                    {
                        Console.WriteLine("Depth shift option enabled.");
                        Console.WriteLine("Trying parse depth shift value...");
                    }

                    if (DepthShiftTryParse(options.DepthShift, out var value))
                    {
                        if (options.Verbose)
                        {
                            Console.WriteLine("Depth shift value is:{0}", value);
                        }

                        //applying depth shift for all frames in data
                        foreach (var frame in data.Frames)
                        {
                            frame.Depth = new LinearDimension(frame.Depth.GetMeters() + value, LinearDimensionUnit.Meter);
                        }
                    }
                }
                #endregion

                #region Generate channels frames
                //check generate parameters
                if (options.GenegateParams != null && options.GenegateParams.Any())
                {
                    if (options.Verbose)
                    {
                        Console.WriteLine("Generate frames for specific channel option enabled.");
                        Console.WriteLine("Try parse parameters...");
                    }
                    var dstChannel        = new List <ChannelType>();
                    var sourceChannels    = new List <ChannelType>();
                    var forceGenerate     = false;
                    var generateFromDepth = false;
                    foreach (var optionsGenegateParam in options.GenegateParams)
                    {
                        if (Enum.TryParse <ChannelType>(optionsGenegateParam, out var channelType))
                        {
                            if (!dstChannel.Any())
                            {
                                dstChannel.Add(channelType);
                            }
                            else
                            {
                                sourceChannels.Add(channelType);
                            }
                        }
                        if (string.Compare(optionsGenegateParam, "f", StringComparison.InvariantCultureIgnoreCase) == 0)
                        {
                            forceGenerate = true;
                        }
                        if (string.Compare(optionsGenegateParam, "d", StringComparison.InvariantCultureIgnoreCase) == 0)
                        {
                            generateFromDepth = true;
                        }
                    }

                    if (options.Verbose)
                    {
                        Console.Write("Source channel(s) are:");
                        foreach (var sourceChannel in sourceChannels)
                        {
                            Console.Write(sourceChannel + ",");
                        }
                        Console.Write("\n");
                        Console.WriteLine("Channel for frame generation is:{0}", dstChannel.Single());

                        Console.WriteLine("Force erase points at destination channel before generate(\"f\" option): {0}", forceGenerate);
                        Console.WriteLine("Generate sounded data from depth value(\"d\" option): {0}", generateFromDepth);

                        if (!sourceChannels.Any())
                        {
                            Console.WriteLine("There is no channels for frame generation. Skip generation option.");
                        }
                    }

                    //continue if sourceChannels.Any()
                    if (sourceChannels.Any())
                    {
                        //get unique frames(by FrameIndex) from source channel(s)
                        var unicueFrameFromSourceChanels = data.Frames
                                                           .Where(frame => sourceChannels.Contains(frame.ChannelType))
                                                           .GroupBy(frame => frame.FrameIndex)
                                                           .Select(group => group.First())
                                                           .ToList();

                        var erasedPointsCountAtDstChannel = 0;
                        //erase dstChannel frames from data
                        if (forceGenerate)
                        {
                            erasedPointsCountAtDstChannel = data.Frames.RemoveAll(frame => frame.ChannelType == dstChannel.Single());
                        }

                        //get points for existed dst channel frames
                        var dstChannelFramesPoints = data.Frames
                                                     .Where(frame => frame.ChannelType == dstChannel.Single())
                                                     .Select(frame => frame.Point).ToList();

                        //generate and add frame for each unique point
                        foreach (var unicueFrame in unicueFrameFromSourceChanels)
                        {
                            if (!dstChannelFramesPoints.Contains(unicueFrame.Point))
                            {
                                data.Frames.Add(Frame.GenerateFromOtherChannelFrame(dstChannel.Single(), unicueFrame, generateFromDepth));
                            }
                        }

                        if (options.Verbose)
                        {
                            Console.WriteLine("Unique points at source channel(s):{0}", unicueFrameFromSourceChanels.Count);
                            if (forceGenerate)
                            {
                                Console.WriteLine("Points erased at destination channel:{0}", erasedPointsCountAtDstChannel);
                            }
                            Console.WriteLine("Points added to destination channel:{0}\n", unicueFrameFromSourceChanels.Count - dstChannelFramesPoints.Count);
                        }
                    }
                }


                #endregion

                #region Flip SoundedData

                if (options.FlipSoundedData)
                {
                    if (options.Verbose)
                    {
                        Console.WriteLine("Sounded Data flip option enabled.");
                        Console.WriteLine("Flipping data for SidescanComposite channel ...\n");
                    }

                    foreach (var frame in data.Frames)
                    {
                        //flip sounded data for SidescanComposite channel
                        if (frame.ChannelType == ChannelType.SidescanComposite)
                        {
                            frame.SoundedData = frame.SoundedData.FlipSoundedData();
                        }
                    }
                }

                #endregion

                #region Research mode console output

                //if research mode, then opens file again
                if (options.SearchOffset >= 0)
                {
                    Console.WriteLine("Try research values from " + options.SearchOffset + " bytes offset...\n");
                    try
                    {
                        using (var stream = new FileStream(options.InputFile, FileMode.Open, FileAccess.Read))
                        {
                            stopWatch.Start();
                            using (var reader = new BinaryReader(stream))
                            {
                                var fileHeader = Header.ReadHeader(reader, 0);

                                var researchResult = Frame.ValuesResearch(reader, Header.Lenght, options.SearchOffset, fileHeader.FileVersion);

                                var tableHeader = $"|{"String Value",12}|{"Bytes",16}|{"Short #1",8}|{"Short #2",8}|{"Integer",10}|{"Float",15}|{"Frame Index",11}|{"Channel",17}|";
                                //Console.WriteLine("String Value \t| Bytes \t| First Short \t| Second Short \t| Integer \t| Float \t| Frame Index \t| Channel");
                                Console.WriteLine(tableHeader);
                                Console.WriteLine("------------------------------------------------------------------------------------------------------------");

                                foreach (var offset in researchResult.Keys)
                                {
                                    var tuple = researchResult[offset];

                                    //skip Console.WriteLine if frame channel is not selected
                                    if (outputchannels.Any() && !outputchannels.Contains(tuple.Item7))
                                    {
                                        continue;
                                    }

                                    //skip Console.WriteLine if frame is not inside diapason
                                    if (tuple.Item6 < options.FramesFrom || tuple.Item6 > options.FramesTo)
                                    {
                                        continue;
                                    }

                                    var stringbuilder = new StringBuilder();
                                    foreach (var onebyte in tuple.Item1)
                                    {
                                        stringbuilder.Append(onebyte + ",");
                                    }

                                    var str = $"|{BitConverter.ToString(tuple.Item1),12}|{stringbuilder,16}|{tuple.Item2,8}|{tuple.Item3,8}|{tuple.Item4,10}|{tuple.Item5,15}|{tuple.Item6,11}|{tuple.Item7,17}|";

                                    Console.WriteLine(str);
                                }
                            }
                            stopWatch.Stop();
                            Console.WriteLine("Read and research time: " + stopWatch.Elapsed + "\n");
                            stopWatch.Reset();
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                }

                #endregion

                #region Creating output file

                //makes output file if it necessary
                if (options.OutputFileVersion != null && options.OutputFileVersion.Any())
                {
                    bool FrameValidationFunc(Frame frame)
                    {
                        var isIndexValid = frame.FrameIndex >= options.FramesFrom && frame.FrameIndex <= options.FramesTo;

                        return(outputchannels.Any() ? outputchannels.Contains(frame.ChannelType) && isIndexValid : isIndexValid);
                    }

                    Console.WriteLine("Making new frames list...\n");
                    stopWatch.Start();
                    var newFrames = data.Frames.Where(FrameValidationFunc).ToList();
                    stopWatch.Stop();
                    Console.WriteLine("List created.  Creation time: " + stopWatch.Elapsed + "\n");
                    stopWatch.Reset();


                    //delete coordinates if it necessary
                    if (options.CoordinatesDelete)
                    {
                        Console.WriteLine("Anonymize coordinate points...\n");
                        stopWatch.Start();

                        //move track to random place
                        var    rnd = new Random();
                        double lat = rnd.Next(-90, 90);
                        double lon = rnd.Next(-180, 180);

                        //func for coordinate point delete
                        Frame CoordinatesDelete(Frame frame)
                        {
                            frame.Point = new CoordinatePoint(Latitude.FromDegrees(frame.Point.Latitude.ToDegrees() % 1 + lat),
                                                              Longitude.FromDegrees(frame.Point.Longitude.ToDegrees() % 1 + lon));
                            return(frame);
                        }

                        //coordinate point delete
                        newFrames = newFrames.Select(CoordinatesDelete).ToList();
                        stopWatch.Stop();
                        Console.WriteLine("Anonymizations time: " + stopWatch.Elapsed + "\n");
                        stopWatch.Reset();
                    }

                    var newData = new LowranceLogData {
                        Frames = newFrames
                    };

                    //checks output formats and write to files
                    foreach (var format in options.OutputFileVersion)
                    {
                        #region Creating SL2

                        if (string.Compare(format, "sl2", StringComparison.InvariantCultureIgnoreCase) == 0)
                        {
                            //if original header have the same format then reuse it
                            newData.Header = data.Header.FileVersion == FileVersion.SL2 ? data.Header : Header.sl2;
                            try
                            {
                                using (var stream = new FileStream(@"out.sl2", FileMode.Create, FileAccess.Write))   //- мой короткий   )
                                {
                                    Console.WriteLine("Writing \"out.sl2\" file...\n");
                                    stopWatch.Start();

                                    LowranceLogData.WriteToStream(stream, newData);

                                    stopWatch.Stop();
                                    Console.WriteLine("Writing complete.  Writing time: " + stopWatch.Elapsed + "\n");
                                    stopWatch.Reset();
                                }
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex);
                            }
                        }
                        #endregion

                        #region Creating SL3

                        if (string.Compare(format, "sl3", StringComparison.InvariantCultureIgnoreCase) == 0)
                        {
                            //if original header have the same format then reuse it
                            newData.Header = data.Header.FileVersion == FileVersion.SL3 ? data.Header : Header.sl3;
                            try
                            {
                                using (var stream = new FileStream(@"out.sl3", FileMode.Create, FileAccess.Write))   //- мой короткий   )
                                {
                                    Console.WriteLine("Writing \"out.sl3\" file...\n");
                                    stopWatch.Start();

                                    LowranceLogData.WriteToStream(stream, newData);

                                    stopWatch.Stop();
                                    Console.WriteLine("Writing complete.  Writing time: " + stopWatch.Elapsed + "\n");
                                    stopWatch.Reset();
                                }
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex);
                            }
                        }
                        #endregion

                        #region Creating CVS

                        if (string.Compare(format, "csv", StringComparison.InvariantCultureIgnoreCase) == 0)
                        {
                            //create CVSLogData object
                            var cvsData = new CsvLogData()
                            {
                                CreationDateTime = DateTimeOffset.Now,
                                Name             = "CVSLogData object",

                                //grouping points by coordinate and take point with average depth
                                Points = newFrames.GetUniqueDepthPoints()
                            };

                            //writing points to file
                            try
                            {
                                using (var stream = new FileStream(@"out.csv", FileMode.Create, FileAccess.Write))
                                {
                                    Console.WriteLine("Writing \"out.csv\" file...\n");
                                    stopWatch.Start();

                                    CsvLogData.WriteToStream(stream, cvsData);

                                    stopWatch.Stop();
                                    Console.WriteLine("{0} points writing complete.  Writing time: {1} \n", cvsData.Points.Count(), stopWatch.Elapsed);
                                    stopWatch.Reset();
                                }
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex);
                            }
                        }

                        #endregion
                    }
                }
                #endregion
            }

            Console.WriteLine($"{Environment.NewLine}Please press any key to exit...");
            Console.ReadKey(true);
        }