static void Main(string[] args) { try { var headerData = new byte[30]; byte[] descriptorData; var fs = new FileStream(@"C:\Users\Administrator\Desktop\radar", FileMode.Open, FileAccess.Read); var bytes = new byte[fs.Length]; descriptorData = new byte[fs.Length - 30]; var descriptor = new Descriptor(); var symbology = new Symbology(); // Header fs.Read(headerData, 0, 30); var headerString = System.Text.Encoding.UTF8.GetString(headerData); headerString = headerString.Replace("\n", string.Empty).Replace("\r", " ").Replace(" ", " "); var headerStrings = headerString.Split(' '); var header = new Header { Code = headerStrings[0], Site = headerStrings[1], Date = headerStrings[2], FullProductCode = headerStrings[3] }; header.BodyCode = ReadHalfWord(fs); header.BodyDate = ReadHalfWord(fs); header.BodyTime = ReadWord(fs); header.Length = ReadWord(fs); header.SourceId = ReadHalfWord(fs); header.DestinationId = ReadHalfWord(fs); header.BlockCount = ReadHalfWord(fs); // Descriptor descriptor.Divider = ReadHalfWord(fs); descriptor.Latitude = ReadWord(fs); descriptor.Longitude = ReadWord(fs); descriptor.Height = ReadHalfWord(fs); descriptor.Code = ReadHalfWord(fs); descriptor.Mode = ReadHalfWord(fs); descriptor.VolumeCoveragePattern = ReadHalfWord(fs); descriptor.SequenceNumber = ReadHalfWord(fs); descriptor.ScanNumber = ReadHalfWord(fs); descriptor.ScanDate = ReadHalfWord(fs); descriptor.ScanTime = ReadWord(fs); descriptor.GenerationDate = ReadHalfWord(fs); descriptor.GenerationTime = ReadWord(fs); descriptor.ProductSpecific1 = ReadHalfWord(fs); descriptor.ProductSpecific2 = ReadHalfWord(fs); descriptor.ElevationNumber = ReadHalfWord(fs); descriptor.ProductSpecific3 = ReadHalfWord(fs); descriptor.Threshold1 = ReadHalfWord(fs); descriptor.Threshold2 = ReadHalfWord(fs); descriptor.Threshold3 = ReadHalfWord(fs); descriptor.Threshold4 = ReadHalfWord(fs); descriptor.Threshold5 = ReadHalfWord(fs); descriptor.Threshold6 = ReadHalfWord(fs); descriptor.Threshold7 = ReadHalfWord(fs); descriptor.Threshold8 = ReadHalfWord(fs); descriptor.Threshold9 = ReadHalfWord(fs); descriptor.Threshold10 = ReadHalfWord(fs); descriptor.Threshold11 = ReadHalfWord(fs); descriptor.Threshold12 = ReadHalfWord(fs); descriptor.Threshold13 = ReadHalfWord(fs); descriptor.Threshold14 = ReadHalfWord(fs); descriptor.Threshold15 = ReadHalfWord(fs); descriptor.Threshold16 = ReadHalfWord(fs); descriptor.ProductSpecific4 = ReadHalfWord(fs); descriptor.ProductSpecific5 = ReadHalfWord(fs); descriptor.ProductSpecific6 = ReadHalfWord(fs); descriptor.ProductSpecific7 = ReadHalfWord(fs); descriptor.ProductSpecific8 = ReadHalfWord(fs); descriptor.ProductSpecific9 = ReadHalfWord(fs); descriptor.ProductSpecific10 = ReadHalfWord(fs); descriptor.Version = ReadByte(fs); descriptor.SpotBlank = ReadByte(fs); descriptor.SymbologyOffset = ReadWord(fs); descriptor.GraphicOffset = ReadWord(fs); descriptor.TabularOffset = ReadWord(fs); if (descriptor.SymbologyOffset != 0) { symbology.Divider = ReadHalfWord(fs); symbology.BlockId = ReadHalfWord(fs); symbology.Length = ReadWord(fs); symbology.Layers = ReadHalfWord(fs); symbology.LayerDivider = ReadHalfWord(fs); symbology.DataLength = ReadWord(fs); symbology.PacketCode = ReadHalfWord(fs); symbology.IndexFirstRangeBin = ReadHalfWord(fs); symbology.RangeBinCount = ReadHalfWord(fs); symbology.SweepCenterI = ReadHalfWord(fs); symbology.SweepCenterJ = ReadHalfWord(fs); symbology.ScaleFactor = ReadHalfWord(fs); symbology.RadialCount = ReadHalfWord(fs); symbology.RadialData = new List<Radial>(); var radialCount = symbology.RadialCount; for (var i = 0; i < radialCount; i++) { var radial = new Radial(); radial.ColorValues = new List<Int32>(); radial.RleCount = ReadHalfWord(fs); radial.StartAngle = ReadHalfWord(fs) / 10; radial.AngleDelta = ReadHalfWord(fs) / 10; for (var j = 0; j < radial.RleCount * 2; j++) { var values = DecodeRLES(fs, descriptor); radial.ColorValues.AddRange(values); } symbology.RadialData.Add(radial); } } var nexradScan = new NexradScan { Header = header, Descriptor = descriptor, Symbology = symbology }; var nexradScanJson = JsonConvert.SerializeObject(nexradScan); } catch (Exception ex) { Console.WriteLine("Generic error while parsing Nexrad data", ex.ToString()); } }
private static List<Int32> DecodeRLES(FileStream fs, Descriptor descriptor) { var data = ReadByte(fs); // read a byte, MSB is the RUN value, LSB is the COLOR CODE var value = data & 15; // get MSB var length = data >> 4; // get LSB var values = new List<Int32>(); if (descriptor.Mode == 1 && descriptor.Code >= 16 && descriptor.Code <= 21) { if (value >= 8) value = value - 8; else value = 0; } for (var i = 0; i < length; i++) { values.Add(value); } return values; }