/// <summary> /// Enumerates the messages in the underlying stream. /// </summary> /// <returns>The messages in the GRIB 2 stream.</returns> public IEnumerable <Message> ReadMessages() { reader.Seek(0, SeekOrigin.Begin); do { var indicatorSection = IndicatorSection.BuildFrom(reader); var identificationSection = IdentificationSection.BuildFrom(reader); var message = new Message(indicatorSection, identificationSection); LocalUseSection localUseSection = null; do { if (reader.PeekSection().Is(SectionCode.LocalUseSection)) { localUseSection = LocalUseSection.BuildFrom(reader); } while (reader.PeekSection().Is(SectionCode.GridDefinitionSection)) { var gridDefinitionSection = GridDefinitionSection.BuildFrom(reader); while (reader.PeekSection().Is(SectionCode.ProductDefinitionSection)) { var productDefinitionSection = ProductDefinitionSection.BuildFrom(reader, indicatorSection.Discipline); var dataRepresentationSection = DataRepresentationSection.BuildFrom(reader); var bitmapSection = BitmapSection.BuildFrom(reader, dataRepresentationSection.DataPointsNumber); var dataSection = DataSection.BuildFrom(reader); message.AddDataset( localUseSection, gridDefinitionSection, productDefinitionSection, dataRepresentationSection, bitmapSection, dataSection); } } } while (!reader.PeekSection().Is(SectionCode.EndSection)); EndSection.BuildFrom(reader); // Saves and restore the current position // to avoid losing track of the current message // if a data set read happens during the enumeration var currentPosition = reader.Position; yield return(message); reader.Seek(currentPosition, SeekOrigin.Begin); } while (!reader.HasReachedStreamEnd && reader.PeekSection().Is(SectionCode.IndicatorSection)); }
public void BuildFrom_ValidGribFile_Test() { using var gribFileStream = File.OpenRead(GribFileSamples.ValidFile); var reader = new BufferedBinaryReader(gribFileStream); reader.Seek(16, SeekOrigin.Begin); var section = IdentificationSection.BuildFrom(reader); Check.That(section).IsNotNull(); Check.That(section.Length).Equals(21); Check.That(section.Section).Equals(1); Check.That(section.CenterCode).Equals(7); Check.That(section.SubCenterCode).Equals(0); Check.That(section.MasterTableVersion).Equals(2); Check.That(section.LocalTableVersion).Equals(1); Check.That(section.ProductStatus).Equals(ProductStatus.OperationalProducts); Check.That(section.ProductType).Equals(ProductType.AnalysisProducts); Check.That(section.ReferenceTimeSignificance).Equals(ReferenceTimeSignificance.Analysis); Check.That(section.ReferenceTime).Equals(new DateTime(2020, 3, 14, 0, 0, 0, 0, DateTimeKind.Utc)); }