question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"negative seek position" error

See original GitHub issue

I’m trying to open ERA5 pressure level data inside a grib-file downloaded from the CDS (see the bottom of the post for the download-command).

I’m trying to open the data at different time-indices like this, which works fine for any value of t between 0 and 18:

ds = cfgrib.open_file(r'F:\era5_for_wam2\pl_2008_01.grib')
var = ds.variables['q']

t = 18
q = var.data[t,:,:,:]

However, as soon as t is larger than 18, e.g.:

q = var.data[19,:,:,:]

I get the following error:

Traceback (most recent call last):

  File "<ipython-input-30-a8a5f441ed1e>", line 1, in <module>
    q = var.data[19,:,:,:]

  File "C:\ProgramData\Miniconda3\lib\site-packages\cfgrib\dataset.py", line 259, in __getitem__
    message = self.stream.message_from_file(file, offset=offset[0])

  File "C:\ProgramData\Miniconda3\lib\site-packages\cfgrib\messages.py", line 244, in message_from_file
    return self.message_class.from_file(file=file, offset=offset, **kwargs)

  File "C:\ProgramData\Miniconda3\lib\site-packages\cfgrib\messages.py", line 66, in from_file
    file.seek(offset)

ValueError: negative seek position -5

I have many files like the one in this example (pl_2008_01.grib, pl_2008_02.grib, pl_2008_03.grib, etc.), and each of them crashes when trying to open data with a t > 18.

Any ideas what is causing this and how to solve this? I’m on a windows machine, Python=3.7.1 and cfgrib=0.9.6

I’m getting my grib files like this:

import cdsapi
c = cdsapi.Client()
c.retrieve(
            'reanalysis-era5-pressure-levels',
            {
                'product_type':'reanalysis',
                'format':'grib',
                'pressure_level':[
                    '50','150','250','350',
                    '450','550','650',
                    '750','800','850',
                    '900','950','1000'
                ],
                'month':'1',
                'day':[
                '01','02','03',
                '04','05','06',
                '07','08','09',
                '10','11','12',
                '13','14','15',
                '16','17','18',
                '19','20','21',
                '22','23','24',
                '25','26','27',
                '28','29','30',
                '31'
            ],
                'time':
                    [
                    '00:00','01:00','02:00',
                    '03:00','04:00','05:00',
                    '06:00','07:00','08:00',
                    '09:00','10:00','11:00',
                    '12:00','13:00','14:00',
                    '15:00','16:00','17:00',
                    '18:00','19:00','20:00',
                    '21:00','22:00','23:00',],
                'year':'2008',
                'variable':[
                    'specific_humidity','u_component_of_wind','v_component_of_wind',
                    'vertical_velocity'
                ]
            },
            r'F:\era5_for_wam2\pl_2008_01.grib')

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
bertcoervercommented, Jun 20, 2019

Just in case anyone else has this problem:

I had installed ecCodes with conda-forge with:

conda install -c conda-forge eccodes

but I didn’t set the environmental variables ECCODES_DEFINITION_PATH and ECCODES_SAMPLES_PATH (see here for more info) correctly. Doing that solved the issue.

0reactions
alexamicicommented, Jul 10, 2019

@bertcoerver:

  1. Did you remove the .idx file and retry?
  2. After doing 1. and still getting a negative seek error, can you attach the .idx file to the issue?

Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Negative seeking python - Stack Overflow
It's already been said that seek doesn't return the character at that position. It returns the position, and it isn't really useful to...
Read more >
Re: Negative seek offset error message while writing output
Hi John, I can provide the code (attached), it's a single class, but the input files are huge. The final size of the...
Read more >
offset can not be negative in File.seek()? - Python Forum
It's intersting I can not put the "offset" to non-zero if I set the second parameter as "1" or "2" in file.seek(), and...
Read more >
Issue 3524: IOError when attempting negative seek in file ...
Title: IOError when attempting negative seek in file (Python 3.0 tutorial) ... Since the file is a text file, such seeking is not...
Read more >
File Handling: negative offset in seek() function : r/learnpython
When I tried using a negative number as an argument for the offset parameter, python is raising an error. Why?
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found