noritada / grib-rs

GRIB format parser for Rust
Apache License 2.0
56 stars 9 forks source link

Parsing message from non-0 start position panics. #38

Closed LafeWessel closed 1 year ago

LafeWessel commented 1 year ago

Simple Description on the Bug

When attempting to read the next message based on starting reading a GRIB file from an offset, the code panics instead of parsing the values even though it can correctly read the section information.

Steps to Reproduce

Here is the test I ran with the test file found here. Attempting to use other offsets that are found in the GRIB index file (second value, delimited by :) will produce similar results, sometimes on the first message instead of the second. Starting from an offset of 0 allows the reader to read the file normally with no errors encountered.

#[test]
fn test_manual_offset_read_grib() {
    // offset to the second submessage
    let offset = 983_303;
    let mut f = File::open(GRIB_TEST_FILE).unwrap();
    f.seek(SeekFrom::Start(offset)).unwrap(); // TOGGLE COMMENTING THIS LINE TO GET ERROR
    let buf = BufReader::new(f);
    let gr = grib::from_reader(buf).unwrap();

    for (i, (_, lyr)) in gr.submessages().enumerate() {
        println!("{}", lyr.describe());

        let d = Grib2SubmessageDecoder::from(lyr).unwrap();
        let v = d.dispatch().unwrap(); // SHOULD RETURN AN ERROR ON THE SECOND SUBMESSAGE

        for i in v.take(5) {
            println!("{:?}", i);
        }

        println!("index: {}", i);
    }
}

Expected Behavior

The message offset-to and each following message are properly parsed.

Actual Behavior

When testing on the test file that I mentioned above, the decoder can properly handle all the submessages in the GRIB file. However, when attempting to start from an offset in the file greater than 0, the decoder returns a ComplexDecodingError for the second message.

Additional Context

No response

noritada commented 1 year ago

@LafeWessel Sorry for the late reply. Thanks again for reporting the issue!

There are many cases where we need to read from the middle of a file, as in the case you reported, but by mistake, I had stored the offset as zero internally at the start of the read.

The problem has been fixed in my local environment. I will merge it later.